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/fix-web-stream-text-wrapping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

web: Fix streamed assistant text rendering with unintended line breaks between chunks.
7 changes: 5 additions & 2 deletions apps/kimi-web/src/composables/messagesToTurns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,11 @@ export function messagesToTurns(
g.textParts.push(c.text);
// Append to a trailing text block, else open a new one — so a tool
// call between two text segments splits them into separate blocks.
// Stream chunks already contain the model's whitespace (including
// paragraph breaks). Inserting a newline here turns token-sized
// chunks into one line per word, especially for Chinese output.
const last = g.blocks.at(-1);
if (last && last.kind === 'text') last.text += '\n' + c.text;
if (last && last.kind === 'text') last.text += c.text;
else g.blocks.push({ kind: 'text', text: c.text });
}
} else if (c.type === 'thinking') {
Expand All @@ -651,7 +654,7 @@ export function messagesToTurns(
// Ordered block too: thinking renders WHERE it happened in the turn,
// merging consecutive segments (same rule as text blocks above).
const last = g.blocks.at(-1);
if (last && last.kind === 'thinking') last.thinking += '\n' + c.thinking;
if (last && last.kind === 'thinking') last.thinking += c.thinking;
else g.blocks.push({ kind: 'thinking', thinking: c.thinking });
}
} else if (c.type === 'toolUse') {
Expand Down
22 changes: 22 additions & 0 deletions apps/kimi-web/test/turn-logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ describe('messagesToTurns', () => {
expect(turns.map((turn) => turn.text)).toEqual(['one', 'two']);
});

it('preserves whitespace when merging streamed text around empty thinking parts', () => {
const turns = messagesToTurns(
[
message('a1', 'assistant', [
{ type: 'text', text: 'one' },
{ type: 'thinking', thinking: '' },
{ type: 'text', text: ' ' },
{ type: 'thinking', thinking: '' },
{ type: 'text', text: 'two' },
{ type: 'thinking', thinking: '' },
{ type: 'text', text: ' ' },
{ type: 'text', text: 'three' },
]),
],
[],
undefined,
false,
);

expect(turns[0]?.blocks).toEqual([{ kind: 'text', text: 'one two three' }]);
});

it('renders compaction summaries as divider turns', () => {
const turns = messagesToTurns(
[
Expand Down