fix(ai): make a reloaded chat match the live chat #2163
main fromfix/reload-equals-live-chatmerged by AlemTuzlak View on GitHubAlemTuzlak pushed80a104d
- package.json line 80Outdated@@ -77,3 +77,4 @@7777 "@tanstack/ai-grok-build": "workspace:*",7878 "@tanstack/ai-sandbox": "workspace:*",7979 "@tanstack/ai-sandbox-docker": "workspace:*",80+ "@tanstack/oracles": "https://pkg.pr.new/TanStack/oracles/@tanstack/oracles@78fa1b7",packages/ai/src/activities/chat/messages.ts line 1370Outdated@@ -1352,4 +1366,5 @@13521366 const raw = source.metadata13531367 if (raw != null && typeof raw === 'object' && !Array.isArray(raw)) {13541368 metadata = raw as NonNullable<UIMessage['metadata']>1355- next = { ...next, metadata }1369+ const kept = withoutWireRowFields(metadata)1370+ if (kept !== undefined) next = { ...next, metadata: kept }packages/ai/src/activities/chat/messages.ts line 1557Outdated@@ -1521,4 +1552,6 @@15211552 }15221553 }152315541524- return uiMessages1555+ return uiMessages.filter(1556+ (message) =>1557+ message.role !== 'assistant' || !isWhitespaceOnlyMessage(message),packages/ai/src/activities/chat/stream/message-updaters.ts line 524Outdated@@ -508,14 +513,12 @@508513 )509514510- // A hydrated message carries its thinking without a stepId: the stored form511- // has no field for one, so `modelMessageToUIMessage` cannot put it back.512- // When a run is rejoined mid-stream the replayed reasoning is keyed by513- // stepId, matches nothing, and gets appended -- leaving a second thinking514- // part sitting after the answer text. Adopt the first stepId-less thinking515- // part instead, so the replay lands on the part it belongs to. Live516- // streaming always writes a stepId, so the only parts this can match are517- // hydrated ones.515+ // A hydrated message's thinking parts belong to no step. When a run is516+ // rejoined mid-stream the replayed reasoning matches nothing and would be517+ // appended -- leaving a second thinking part sitting after the answer518+ // text. Adopt the first thinking part with no step instead, so the replay519+ // lands on the part it belongs to. Live streaming always records a step,520+ // so the only parts this can match are hydrated ones.518521 let adopted: ThinkingPart | undefined519522 if (thinkingPartIndex < 0) {520523 thinkingPartIndex = parts.findIndex(521- (p) => p.type === 'thinking' && p.stepId === undefined,524+ (p) => p.type === 'thinking' && !thinkingStepOf.has(p),packages/ai/src/activities/chat/stream/processor.ts line 495Outdated@@ -480,10 +483,13 @@480483 const content = normalizeToolResult(output)481484 const toolResultState: ToolResultState = error ? 'error' : 'complete'482485486+ const textMark = textToolResultMark(output)483487 updatedMessages = updateToolResultPart(484488 updatedMessages,485489 messageWithToolCall.id,486490 toolCallId,487491 content,488492 toolResultState,489493 error,494+ undefined,495+ textMark && { tanstack: textMark },
tanstack-source reviewed
packages/ai/src/activities/chat/stream/processor.ts line 2650Outdated@@ -2639,12 +2645,6 @@26392645 toolResultOutcome !== undefined2640264626412647 // Step 1: Update the tool-call part's output field2642- let output: unknown2643- try {2644- output =2645- typeof chunk.content === 'string'2646- ? JSON.parse(chunk.content)2647- : chunk.content2648- } catch {2649- output = chunk.content2650- }2648+ const output: unknown =2649+ typeof chunk.content === 'string'2650+ ? parseToolOutput(chunk.content, chunk.metadata)
Applied a fix from CI6a8c6ad
tanstack-source reviewed
packages/ai/src/activities/chat/messages.ts line 1370Outdated@@ -1352,4 +1366,5 @@13521366 const raw = source.metadata13531367 if (raw != null && typeof raw === 'object' && !Array.isArray(raw)) {13541368 metadata = raw as NonNullable<UIMessage['metadata']>1355- next = { ...next, metadata }1369+ const kept = withoutWireRowFields(metadata)1370+ if (kept !== undefined) next = { ...next, metadata: kept }packages/ai/src/activities/chat/messages.ts line 1556Outdated@@ -1521,4 +1551,6 @@15211551 }15221552 }152315531524- return uiMessages1554+ return uiMessages.filter(1555+ (message) =>1556+ message.role !== 'assistant' || !isWhitespaceOnlyMessage(message),packages/ai/src/activities/chat/stream/processor.ts line 495Outdated@@ -487,3 +491,5 @@487491 content,488492 toolResultState,489493 error,494+ undefined,495+ textMark && { tanstack: textMark },packages/ai/src/activities/chat/index.ts line 4379Outdated@@ -4376,4 +4374,6 @@43764374 content: wireContent,43774375 role: 'tool' as const,43784376 }4379- // An outcome always comes with output-error.4377+ // An outcome always comes with output-error. A string result says so,4378+ // or the client would parse '42' into 42.4379+ const textMark = textToolResultMark(result.result)
Applied a fix from CIeec4409
tanstack-source reviewed
packages/ai/src/activities/chat/index.ts line 3937Outdated@@ -3938,3 +3932,6 @@39383932 message.id ||39393933 `snapshot_${this.runIdOverride ?? this.requestId}_${index}`,39403934 }))3935+ // Store the ids the snapshot gives, so a reload of the stored messages3936+ // names each message as the live chat does.3937+ this.messages = withIdspackages/ai/src/activities/chat/index.ts line 3694Outdated@@ -3688,13 +3689,6 @@36883689 // Parse content back to original output (was stringified by36893690 // uiMessageToModelMessages). Multimodal results carry an36903691 // Array<ContentPart> directly — pass it through without parsing.3691- let output: unknown3692- if (Array.isArray(message.content)) {3693- output = message.content3694- } else {3695- try {3696- output = JSON.parse(message.content as string)3697- } catch {3698- output = message.content3699- }3700- }3692+ const output: unknown = Array.isArray(message.content)3693+ ? message.content3694+ : parseToolOutput(message.content as string, message.metadata)
AlemTuzlak pushed1e8d1cf
- packages/ai/src/activities/chat/index.ts line 4432Outdated@@ -4425,4 +4429,4 @@44254429 ? this.messages[existingToolResultIdx]44264430 : undefined44274431 const errorField = result.state === 'output-error' && {4428- error: toolResultErrorText(parseToolOutput(wireContent)),4432+ error: toolResultErrorText(result.result),
tanstack-source reviewed
packages/ai/src/activities/chat/index.ts line 3937Outdated@@ -3938,3 +3932,6 @@39383932 message.id ||39393933 `snapshot_${this.runIdOverride ?? this.requestId}_${index}`,39403934 }))3935+ // Store the ids the snapshot gives, so a reload of the stored messages3936+ // names each message as the live chat does.3937+ this.messages = withIdspackages/ai/src/activities/chat/index.ts line 3693Outdated@@ -3688,13 +3689,5 @@36883689 // Parse content back to original output (was stringified by36893690 // uiMessageToModelMessages). Multimodal results carry an36903691 // Array<ContentPart> directly — pass it through without parsing.3691- let output: unknown3692- if (Array.isArray(message.content)) {3693- output = message.content3694- } else {3695- try {3696- output = JSON.parse(message.content as string)3697- } catch {3698- output = message.content3699- }3700- }3692+ const output: unknown = Array.isArray(message.content)3693+ ? message.content- packages/ai/src/activities/chat/index.ts lines 4436 to 4437Outdated@@ -4424,5 +4428,10 @@44244428 existingToolResultIdx >= 044254429 ? this.messages[existingToolResultIdx]44264430 : undefined4431+ // An `{ error }` object, also as JSON text, gives its message. Any other4432+ // result gives itself, so a string error '42' stays '42'.4433+ const parsedError = parseToolOutput(wireContent)44274434 const errorField = result.state === 'output-error' && {4428- error: toolResultErrorText(parseToolOutput(wireContent)),4435+ error: toolResultErrorText(4436+ typeof parsedError === 'object' && parsedError !== null4437+ ? parsedError
Applied a fix from CI80eb085
tanstack-source reviewed
packages/ai/src/activities/chat/index.ts line 4440Outdated@@ -4424,3 +4435,6 @@44244435 existingToolResultIdx >= 044254436 ? this.messages[existingToolResultIdx]44264437 : undefined4438+ // An `{ error }` object, also as JSON text, gives its message. Any other4439+ // result gives itself, so a string error '42' stays '42'.4440+ const parsedError = parseToolOutput(wireContent)packages/ai/src/activities/chat/stream/message-updaters.ts line 516Outdated@@ -486,3 +510,7 @@486510 })487511 }488512513+// The reasoning step that wrote each live thinking part. The step id is the514+// processor's key; it is not on the part, so stored and wire forms have no515+// field for it. A part from storage or the wire has no entry.516+const thinkingStepOf = new WeakMap<ThinkingPart, string>()
AlemTuzlak pushed8c22a03
tanstack-source reviewed
packages/ai/src/activities/chat/index.ts line 4441Outdated@@ -4424,4 +4435,7 @@44244435 existingToolResultIdx >= 044254436 ? this.messages[existingToolResultIdx]44264437 : undefined4438+ // An `{ error }` object, also as JSON text, gives its message. Any other4439+ // result gives itself, so a string error '42' stays '42'.4440+ const parsedError = parseToolOutput(wireContent)44274441 const errorField = result.state === 'output-error' && {
Applied a fix from CI7152bbc
tanstack-source reviewed
packages/ai/src/activities/chat/stream/message-updaters.ts line 550Outdated@@ -510,14 +539,12 @@510- // A hydrated message carries its thinking without a stepId: the stored form511- // has no field for one, so `modelMessageToUIMessage` cannot put it back.512- // When a run is rejoined mid-stream the replayed reasoning is keyed by513- // stepId, matches nothing, and gets appended -- leaving a second thinking514- // part sitting after the answer text. Adopt the first stepId-less thinking515- // part instead, so the replay lands on the part it belongs to. Live516- // streaming always writes a stepId, so the only parts this can match are517- // hydrated ones.539+ // A hydrated message's thinking parts belong to no step. When a run is540+ // rejoined mid-stream the replayed reasoning matches nothing and would be541+ // appended -- leaving a second thinking part sitting after the answer542+ // text. Adopt the first thinking part with no step instead, so the replay543+ // lands on the part it belongs to. Live streaming always records a step,544+ // so the only parts this can match are hydrated ones.518545 let adopted: ThinkingPart | undefined519546 if (thinkingPartIndex < 0) {520547 thinkingPartIndex = parts.findIndex(521- (p) => p.type === 'thinking' && p.stepId === undefined,548+ (p) => p.type === 'thinking' && !thinkingStepOf.has(p),522549 )523550 const candidate = parts[thinkingPartIndex]
Applied a fix from CId9a6743
tanstack-source reviewed
packages/ai/src/activities/chat/index.ts line 4453Outdated@@ -4424,3 +4448,6 @@44244448 existingToolResultIdx >= 044254449 ? this.messages[existingToolResultIdx]44264450 : undefined4451+ // An `{ error }` object, also as JSON text, gives its message. Any other4452+ // result gives itself, so a string error '42' stays '42'.4453+ const parsedError = parseToolOutput(wireContent)
AlemTuzlak pushedad712c5
tanstack-source reviewed
packages/ai/src/activities/chat/index.ts line 4453Outdated@@ -4424,3 +4448,6 @@44244448 existingToolResultIdx >= 044254449 ? this.messages[existingToolResultIdx]44264450 : undefined4451+ // An `{ error }` object, also as JSON text, gives its message. Any other4452+ // result gives itself, so a string error '42' stays '42'.4453+ const parsedError = parseToolOutput(wireContent)
Applied a fix from CIe6f281c
github-actions[bot](GitHub profile, opens in a new tab) assignedjherr
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
AlemTuzlak updated the branch from the base4f5eda4
AlemTuzlak turned on merge when ready (merge)
AlemTuzlak merged this40d1c61
AlemTuzlak merged this40d1c61
AlemTuzlak closed this
Sign in to join this conversation.
Useful: this is in-scope chat reload work, and I didn’t find an open PR that already does it. The description doesn’t link an issue. Related: #100004 is stacked on this branch.
TanStack Source bot, value agent