From e7f9a4916fb0ef99d5e9b7a23ef3060a998b4dc7 Mon Sep 17 00:00:00 2001 From: snowdamiz Date: Thu, 11 Jun 2026 12:50:17 -0700 Subject: [PATCH] Optimize runtime stream transcript aggregation --- .../use-xero-desktop-state/runtime-stream.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/client/src/features/xero/use-xero-desktop-state/runtime-stream.ts b/client/src/features/xero/use-xero-desktop-state/runtime-stream.ts index e4dec738..47e09cae 100644 --- a/client/src/features/xero/use-xero-desktop-state/runtime-stream.ts +++ b/client/src/features/xero/use-xero-desktop-state/runtime-stream.ts @@ -336,6 +336,22 @@ function aggregateRuntimeTranscriptDeltas( } const aggregated: RuntimeStreamChannelPayload[] = [] + let pendingTranscriptTextChunks: string[] | null = null + const flushPendingTranscriptText = () => { + if (!pendingTranscriptTextChunks) { + return + } + + const previous = aggregated.at(-1) + if (previous && !isRuntimeStreamPatch(previous) && previous.item.kind === 'transcript') { + previous.item = { + ...previous.item, + text: pendingTranscriptTextChunks.join(''), + } + } + pendingTranscriptTextChunks = null + } + for (const event of events) { const previous = aggregated.at(-1) if ( @@ -344,21 +360,24 @@ function aggregateRuntimeTranscriptDeltas( !isRuntimeStreamPatch(event) && canAggregateRuntimeTranscriptDelta(previous, event) ) { + pendingTranscriptTextChunks ??= [previous.item.text ?? ''] + pendingTranscriptTextChunks.push(event.item.text ?? '') previous.item = { ...previous.item, - text: `${previous.item.text ?? ''}${event.item.text ?? ''}`, updatedSequence: runtimeStreamPayloadUpdateSequence(event), createdAt: event.item.createdAt, } continue } + flushPendingTranscriptText() aggregated.push( !isRuntimeStreamPatch(event) && event.item.kind === 'transcript' ? cloneRuntimeStreamEventForAggregation(event) : event, ) } + flushPendingTranscriptText() return aggregated }