Skip to content

The agent that grows with you

perf(desktop): stop per-event full-timeline allocations on streamed tool events

Teknium544169a41abc
diff --git a/apps/desktop/src/lib/chat-messages/tool-parts.ts b/apps/desktop/src/lib/chat-messages/tool-parts.ts
index 881a564c8a9..392ec58b4fd 100644
--- a/apps/desktop/src/lib/chat-messages/tool-parts.ts
+++ b/apps/desktop/src/lib/chat-messages/tool-parts.ts
@@ -179,10 +179,15 @@ function findToolPartIndex(
}
}
- const pendingIndices = parts
- .map((part, index) => ({ part, index }))
- .filter(({ part }) => part.type === 'tool-call' && part.toolName === name && part.result === undefined)
- .map(({ index }) => index)
+ const pendingIndices: number[] = []
+
+ for (let index = 0; index < parts.length; index += 1) {
+ const part = parts[index]
+
+ if (part.type === 'tool-call' && part.toolName === name && part.result === undefined) {
+ pendingIndices.push(index)
+ }
+ }
if (pendingIndices.length === 0) {
return -1
@@ -279,11 +284,17 @@ function toolResult(
}
function completeOpenStreamParts(parts: ChatMessagePart[], completedAt: number): ChatMessagePart[] {
- return parts.map(part =>
- (part.type === 'text' || part.type === 'reasoning') && part.completedAt === undefined
- ? ({ ...part, completedAt } as ChatMessagePart)
- : part
- )
+ const next = parts.slice()
+
+ for (let index = 0; index < next.length; index += 1) {
+ const part = next[index]
+
+ if ((part.type === 'text' || part.type === 'reasoning') && part.completedAt === undefined) {
+ next[index] = { ...part, completedAt } as ChatMessagePart
+ }
+ }
+
+ return next
}
export function upsertToolPart(
@@ -325,11 +336,11 @@ export function upsertToolPart(
} satisfies ChatMessagePart
if (index === -1) {
- return [...next, base]
+ next.push(base)
+ } else {
+ next[index] = { ...next[index], ...base }
}
- next[index] = { ...next[index], ...base }
-
return next
}
diff --git a/apps/desktop/src/lib/generated-images.test.ts b/apps/desktop/src/lib/generated-images.test.ts
index 2f08bf52107..7fb32105104 100644
--- a/apps/desktop/src/lib/generated-images.test.ts
+++ b/apps/desktop/src/lib/generated-images.test.ts
@@ -60,6 +60,12 @@ describe('generatedImageEchoSources', () => {
})
describe('dedupeGeneratedImageEchoesInParts', () => {
+ it('preserves timeline identity when there is nothing to strip', () => {
+ const parts = [{ result: {}, toolName: 'read_file', type: 'tool-call' }]
+
+ expect(dedupeGeneratedImageEchoesInParts(parts)).toBe(parts)
+ })
+
it('keeps the agent prose while removing the duplicated image', () => {
expect(
dedupeGeneratedImageEchoesInParts([
diff --git a/apps/desktop/src/lib/generated-images.ts b/apps/desktop/src/lib/generated-images.ts
index 9a9243796fe..4ee990bd6d9 100644
--- a/apps/desktop/src/lib/generated-images.ts
+++ b/apps/desktop/src/lib/generated-images.ts
@@ -68,7 +68,19 @@ export function generatedImageFromResult(result: unknown): string | null {
/** Every path/URL a generated image might appear as in prose, for de-duping. */
export function generatedImageEchoSources(parts: readonly ToolLike[]): string[] {
- return unique(parts.flatMap(part => stringFields(imageResult(part) ?? {}, ECHO_KEYS)))
+ // Runs on every streamed tool event over the whole timeline; skip the
+ // per-part array allocations for the (usual) rows that are not image results.
+ const sources: string[] = []
+
+ for (const part of parts) {
+ const result = imageResult(part)
+
+ if (result) {
+ sources.push(...stringFields(result, ECHO_KEYS))
+ }
+ }
+
+ return unique(sources)
}
/** Strip a generated image out of prose so it only ever shows in the tool slot.
@@ -94,11 +106,13 @@ export function stripGeneratedImageEchoes(text: string, sources: readonly string
/** Strip generated-image echoes from text parts, dropping any part left empty.
* The image lives in the tool slot; prose keeps the agent's actual words. */
-export function dedupeGeneratedImageEchoesInParts<T extends TextLike & ToolLike>(parts: readonly T[]): T[] {
+export function dedupeGeneratedImageEchoesInParts<T extends TextLike & ToolLike>(parts: T[]): T[] {
const sources = generatedImageEchoSources(parts)
+ // No echoes to strip: hand back the same array so React and the streaming
+ // reducer see a no-op instead of a fresh identity on every tool event.
if (!sources.length) {
- return [...parts]
+ return parts
}
return parts