Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/preserve-multimodal-tool-results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/ai': patch
---

Preserve multimodal server tool results when they round-trip through the client.
15 changes: 14 additions & 1 deletion packages/ai/src/activities/chat/stream/message-updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { parsePartialJSON } from './json-parser'
import { isContentPartArray } from '../../../utilities/tool-result'
import type {
ContentPart,
StructuredOutputPart,
Expand Down Expand Up @@ -118,6 +119,18 @@ export function updateToolResultPart(
state: ToolResultState,
error?: string,
): Array<UIMessage> {
let resolvedContent = content
if (typeof content === 'string') {
try {
const parsed = JSON.parse(content)
if (isContentPartArray(parsed)) {
resolvedContent = parsed
}
} catch {
// Keep non-JSON tool output as the original string.
}
}

return messages.map((msg) => {
if (msg.id !== messageId) {
return msg
Expand All @@ -132,7 +145,7 @@ export function updateToolResultPart(
const toolResultPart: ToolResultPart = {
type: 'tool-result',
toolCallId,
content,
content: resolvedContent,
state,
...(error && { error }),
}
Expand Down
70 changes: 70 additions & 0 deletions packages/ai/tests/multimodal-tool-roundtrip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { describe, expect, it } from 'vitest'
import { StreamProcessor } from '../src/activities/chat/stream/processor'
import { uiMessagesToWire } from '../src/utilities/ag-ui-wire'
import type { ContentPart, StreamChunk } from '../src/types'

function chunk<T extends StreamChunk['type']>(
type: T,
fields: Record<string, unknown>,
): Extract<StreamChunk, { type: T }> {
return { type, timestamp: Date.now(), ...fields } as Extract<
StreamChunk,
{ type: T }
>
}

const MULTIMODAL_RESULT: Array<ContentPart> = [
{ type: 'text', content: 'Layout rule 1.' },
{
type: 'image',
source: {
type: 'data',
value: 'iVBORw0KGgoAAAANSUhEUg==',
mimeType: 'image/png',
},
},
]

describe('multimodal TOOL_CALL_RESULT round trip', () => {
it('keeps structured content in the tool result metadata', () => {
const processor = new StreamProcessor()
const toolCallId = 'call-mm'

processor.processChunk(
chunk('TOOL_CALL_START', {
toolCallId,
toolCallName: 'getLayoutRules',
parentMessageId: 'assistant-1',
}),
)
processor.processChunk(
chunk('TOOL_CALL_ARGS', { toolCallId, delta: '{}' }),
)
processor.processChunk(chunk('TOOL_CALL_END', { toolCallId }))
processor.processChunk(
chunk('TOOL_CALL_RESULT', {
toolCallId,
messageId: 'tool-result-1',
content: JSON.stringify(MULTIMODAL_RESULT),
}),
)

const toolResult = processor
.getMessages()[0]!
.parts.find((part) => part.type === 'tool-result')

expect(toolResult?.content).toEqual(MULTIMODAL_RESULT)

const wire = uiMessagesToWire(processor.getMessages())
const toolMessage = wire.find((message) => message.role === 'tool')
const metadata = toolMessage?.metadata as
| {
tanstack?: {
toolResult?: { content?: Array<ContentPart> }
}
}
| undefined

expect(metadata?.tanstack?.toolResult?.content).toEqual(MULTIMODAL_RESULT)
})
})
63 changes: 63 additions & 0 deletions testing/e2e/tests/multimodal-tool-result-roundtrip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
StreamProcessor,
uiMessagesToWire,
type ContentPart,
type StreamChunk,
} from '@tanstack/ai'
import { test, expect } from './fixtures'

function chunk<T extends StreamChunk['type']>(
type: T,
fields: Record<string, unknown>,
): Extract<StreamChunk, { type: T }> {
return { type, timestamp: Date.now(), ...fields } as Extract<
StreamChunk,
{ type: T }
>
}

test('multimodal TOOL_CALL_RESULT keeps structured content on the wire', () => {
const content: Array<ContentPart> = [
{ type: 'text', content: 'Layout rule 1.' },
{
type: 'image',
source: {
type: 'data',
value: 'iVBORw0KGgoAAAANSUhEUg==',
mimeType: 'image/png',
},
},
]
const processor = new StreamProcessor()
const toolCallId = 'call-mm'

processor.processChunk(
chunk('TOOL_CALL_START', {
toolCallId,
toolCallName: 'getLayoutRules',
parentMessageId: 'assistant-1',
}),
)
processor.processChunk(chunk('TOOL_CALL_ARGS', { toolCallId, delta: '{}' }))
processor.processChunk(chunk('TOOL_CALL_END', { toolCallId }))
processor.processChunk(
chunk('TOOL_CALL_RESULT', {
toolCallId,
messageId: 'tool-result-1',
content: JSON.stringify(content),
}),
)

const toolMessage = uiMessagesToWire(processor.getMessages()).find(
(message) => message.role === 'tool',
)
const metadata = toolMessage?.metadata as
| {
tanstack?: {
toolResult?: { content?: Array<ContentPart> }
}
}
| undefined

expect(metadata?.tanstack?.toolResult?.content).toEqual(content)
})