Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/anthropic-relay-missing-text-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Fix a crash when an Anthropic-compatible relay streams a text block without a text field.
13 changes: 9 additions & 4 deletions apps/kimi-code/src/tui/controllers/session-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,9 @@ export class SessionEventHandler {
// moon spinner while no ThinkingComponent is ever created (it needs visible
// text), leaving a blank, spinner-less gap until the first real text/tool
// token arrives. Keep the moon up until actual thinking text shows up.
if (event.delta.trim().length === 0 && !streamingUI.hasThinkingDraft()) return;
streamingUI.appendThinkingDelta(event.delta);
const delta = typeof event.delta === 'string' ? event.delta : '';
if (delta.trim().length === 0 && !streamingUI.hasThinkingDraft()) return;
streamingUI.appendThinkingDelta(delta);
this.host.patchLivePane({ mode: 'idle' });
if (state.appState.streamingPhase !== 'thinking') {
this.host.setAppState({ streamingPhase: 'thinking', streamingStartTime: Date.now() });
Expand All @@ -562,15 +563,19 @@ export class SessionEventHandler {

private handleAssistantDelta(event: AssistantDeltaEvent): void {
const { state, streamingUI } = this.host;
// A compatible relay may stream a text block whose `text` field is absent;
// the wire event then carries no `delta` at all. Coerce it to an empty
// string so a malformed record can never crash the renderer.
const delta = typeof event.delta === 'string' ? event.delta : '';
if (streamingUI.hasThinkingDraft()) {
streamingUI.flushThinkingToTranscript('idle');
}

if (event.delta.trim().length > 0) {
if (delta.trim().length > 0) {
this.currentTurnHasAssistantText = true;
this.pendingModelBlockedFallback = undefined;
}
streamingUI.appendAssistantDelta(event.delta);
streamingUI.appendAssistantDelta(delta);

this.host.patchLivePane({
mode: 'idle',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ class AnthropicStreamedMessage implements StreamedMessage {
const blockIndex = blockEvt.index;
switch (block.type) {
case 'text':
yield { type: 'text', text: block.text };
yield { type: 'text', text: block.text ?? '' };
break;
case 'thinking':
yield { type: 'think', think: block.thinking ?? '' };
Expand Down Expand Up @@ -720,7 +720,7 @@ class AnthropicStreamedMessage implements StreamedMessage {
const blockIndex = deltaEvt.index;
switch (delta.type) {
case 'text_delta':
yield { type: 'text', text: delta.text };
yield { type: 'text', text: delta.text ?? '' };
break;
case 'thinking_delta':
yield { type: 'think', think: delta.thinking ?? '' };
Expand Down
4 changes: 2 additions & 2 deletions packages/kosong/src/providers/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ class AnthropicStreamedMessage implements StreamedMessage {
// eslint-disable-next-line typescript-eslint/switch-exhaustiveness-check
switch (block.type) {
case 'text':
yield { type: 'text', text: block.text };
yield { type: 'text', text: block.text ?? '' };
break;
case 'thinking':
yield { type: 'think', think: block.thinking ?? '' };
Expand Down Expand Up @@ -842,7 +842,7 @@ class AnthropicStreamedMessage implements StreamedMessage {
// eslint-disable-next-line typescript-eslint/switch-exhaustiveness-check
switch (delta.type) {
case 'text_delta':
yield { type: 'text', text: delta.text };
yield { type: 'text', text: delta.text ?? '' };
break;
case 'thinking_delta':
yield { type: 'think', think: delta.thinking ?? '' };
Expand Down
34 changes: 34 additions & 0 deletions packages/kosong/test/anthropic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2816,6 +2816,40 @@ describe('AnthropicChatProvider', () => {
});
});

it('coerces a missing text field to an empty string for relays that omit it', async () => {
const provider = createStreamProvider();
const stream = mockStream([
{
type: 'message_start',
message: {
id: 'msg_stream_002',
usage: { input_tokens: 10 },
},
},
{ type: 'content_block_start', index: 0, content_block: { type: 'text' } },
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta' } },
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'ok' } },
{ type: 'message_delta', delta: {}, usage: { output_tokens: 5 } },
{ type: 'message_stop' },
]);

(provider as any)._client.messages.create = vi.fn().mockResolvedValue(stream) as never;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Stub the relay through the public client factory

This regression test reaches into the private _client field, so a refactor of the provider's client storage or initialization would break the test even if streaming behavior remains correct. Construct the provider with the supported clientFactory boundary instead; the existing collectAnthropicStreamParts helper already does this and can exercise the same malformed relay events through generate() without depending on private implementation details.

Useful? React with 👍 / 👎.


const result = await provider.generate(
'',
[],
[{ role: 'user', content: [{ type: 'text', text: 'Hi' }], toolCalls: [] }],
);

const parts = await collectParts(result);

expect(parts).toEqual([
{ type: 'text', text: '' },
{ type: 'text', text: '' },
{ type: 'text', text: 'ok' },
]);
});

it('yields thinking delta and signature from stream events', async () => {
const provider = createStreamProvider();
const stream = mockStream([
Expand Down