fix: stop streaming responses breaking on a duplicate output key

With reasoning-capable models the chat froze mid-stream: the first chunk of the answer appeared, nothing followed, and the whole message only showed up once generation finished. The browser console showed a Svelte each_key_duplicate error.

When a stream event addresses an output slot past the end of the array, the missing slots were filled with the event's own item, id included, so a gap of two left two entries claiming the same id. The next chunk for that item was matched by id, landed in the first of the two, and the rendered list ended up with two items sharing a key, which Svelte refuses to update.

Only the addressed slot now takes the event's item, and the slots before it are anonymous placeholders. Replayed the reported event sequence against the real code: keys are unique again and the chunks stay in order instead of being split across the copies.
This commit is contained in:
Claude 2026-08-26 09:39:01 +00:00
parent 56d296ef1b
commit 5d7fc35eda
No known key found for this signature in database

View file

@ -462,9 +462,12 @@ function ensureOutputItem(
fallback?: OutputItem
): OutputItem {
while (output.length <= outputIndex) {
output.push(
fallback ?? { type: 'message', status: 'in_progress', role: 'assistant', content: [] }
);
// Only the addressed slot gets the event's item; filler slots must not reuse its id.
const item =
output.length === outputIndex && fallback
? { ...fallback }
: { type: 'message', status: 'in_progress', role: 'assistant', content: [] };
output.push(item);
}
output[outputIndex] = { ...output[outputIndex] };
return output[outputIndex];