Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 37 additions & 2 deletions packages/ai/src/generate-text/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,25 @@ class DefaultStreamTextResult<
return;
}

activeText.text += part.text;
// PATCH: O(N²)→O(N) text accumulation. Init _chunks lazily; replace .text
// with a lazy getter that materializes from the chunks array on read.
if (!(activeText as any)._chunks) {
(activeText as any)._chunks = [activeText.text || ''];
Object.defineProperty(activeText, 'text', {
get(): string {
const c = (this as any)._chunks;
return c.length === 1
? c[0]
: ((this as any)._chunks = [c.join('')])[0];
},
set(v: string) {
(this as any)._chunks = [v];
},
enumerable: true,
configurable: true,
});
}
(activeText as any)._chunks.push(part.text);
activeText.providerMetadata =
part.providerMetadata ?? activeText.providerMetadata;
}
Expand Down Expand Up @@ -1084,7 +1102,24 @@ class DefaultStreamTextResult<
return;
}

activeReasoning.text += part.text;
// PATCH: O(N²)→O(N) reasoning accumulation. Same pattern as activeText above.
if (!(activeReasoning as any)._chunks) {
(activeReasoning as any)._chunks = [activeReasoning.text || ''];
Object.defineProperty(activeReasoning, 'text', {
get(): string {
const c = (this as any)._chunks;
return c.length === 1
? c[0]
: ((this as any)._chunks = [c.join('')])[0];
},
set(v: string) {
(this as any)._chunks = [v];
},
enumerable: true,
configurable: true,
});
}
(activeReasoning as any)._chunks.push(part.text);
activeReasoning.providerMetadata =
part.providerMetadata ?? activeReasoning.providerMetadata;
}
Expand Down
37 changes: 35 additions & 2 deletions packages/ai/src/ui/process-ui-message-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,22 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({
providerMetadata: chunk.providerMetadata,
state: 'streaming',
};
// PATCH: O(N²)→O(N) text accumulation via _chunks + lazy getter.
// See https://github.com/vercel/ai/issues/<NEW> for context.
(textPart as any)._chunks = [textPart.text || ''];
Comment thread
vercel[bot] marked this conversation as resolved.
Resolved
Object.defineProperty(textPart, 'text', {
get(): string {
const c = (this as any)._chunks;
return c.length === 1
? c[0]
: ((this as any)._chunks = [c.join('')])[0];
},
set(v: string) {
(this as any)._chunks = [v];
},
enumerable: true,
configurable: true,
});
state.activeTextParts[chunk.id] = textPart;
state.message.parts.push(textPart);
write();
Expand All @@ -407,7 +423,8 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({
`Ensure a "text-start" chunk is sent before any "text-delta" chunks.`,
});
}
textPart.text += chunk.delta;
// PATCH: see text-start above — chunks instead of += to avoid O(N²).
(textPart as any)._chunks.push(chunk.delta);
textPart.providerMetadata =
chunk.providerMetadata ?? textPart.providerMetadata;
write();
Expand Down Expand Up @@ -451,6 +468,21 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({
providerMetadata: chunk.providerMetadata,
state: 'streaming',
};
// PATCH: O(N²)→O(N) reasoning accumulation via _chunks + lazy getter.
(reasoningPart as any)._chunks = [reasoningPart.text || ''];
Object.defineProperty(reasoningPart, 'text', {
get(): string {
const c = (this as any)._chunks;
return c.length === 1
? c[0]
: ((this as any)._chunks = [c.join('')])[0];
},
set(v: string) {
(this as any)._chunks = [v];
},
enumerable: true,
configurable: true,
});
state.activeReasoningParts[chunk.id] = reasoningPart;
state.message.parts.push(reasoningPart);
write();
Expand All @@ -468,7 +500,8 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({
`Ensure a "reasoning-start" chunk is sent before any "reasoning-delta" chunks.`,
});
}
reasoningPart.text += chunk.delta;
// PATCH: see reasoning-start above — chunks instead of += to avoid O(N²).
(reasoningPart as any)._chunks.push(chunk.delta);
reasoningPart.providerMetadata =
chunk.providerMetadata ?? reasoningPart.providerMetadata;
write();
Expand Down