Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/typescript/ai-anthropic/src/adapters/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ export class AnthropicTextAdapter<
toolCallId: existing.id,
toolCallName: existing.name,
toolName: existing.name,
parentMessageId: messageId,
model,
timestamp,
index: currentToolIndex,
Expand Down Expand Up @@ -725,6 +726,7 @@ export class AnthropicTextAdapter<
toolCallId: existing.id,
toolCallName: existing.name,
toolName: existing.name,
parentMessageId: messageId,
model,
timestamp,
index: currentToolIndex,
Expand Down
67 changes: 67 additions & 0 deletions packages/typescript/ai-anthropic/tests/anthropic-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,4 +686,71 @@ describe('Anthropic stream processing', () => {
type: 'RUN_FINISHED',
})
})

it('emits parentMessageId on tool-first tool call chunks', async () => {
const mockStream = (async function* () {
yield {
type: 'content_block_start',
index: 0,
content_block: {
type: 'tool_use',
id: 'toolu_weather',
name: 'lookup_weather',
input: {},
},
}
yield {
type: 'content_block_delta',
index: 0,
delta: {
type: 'input_json_delta',
partial_json: '{"location":"Berlin"}',
},
}
yield { type: 'content_block_stop', index: 0 }
yield {
type: 'content_block_start',
index: 1,
content_block: { type: 'text', text: '' },
}
yield {
type: 'content_block_delta',
index: 1,
delta: { type: 'text_delta', text: 'It is sunny.' },
}
yield { type: 'content_block_stop', index: 1 }
yield {
type: 'message_delta',
delta: { stop_reason: 'end_turn' },
usage: { output_tokens: 7 },
}
yield { type: 'message_stop' }
})()

mocks.betaMessagesCreate.mockResolvedValueOnce(mockStream)

const adapter = createAdapter('claude-3-7-sonnet-20250219')

const chunks: StreamChunk[] = []
for await (const chunk of chat({
adapter,
messages: [{ role: 'user', content: 'What is the weather in Berlin?' }],
tools: [weatherTool],
})) {
chunks.push(chunk)
}

const textStart = chunks.find(
(chunk): chunk is Extract<StreamChunk, { type: 'TEXT_MESSAGE_START' }> =>
chunk.type === 'TEXT_MESSAGE_START',
)
const toolStart = chunks.find(
(chunk): chunk is Extract<StreamChunk, { type: 'TOOL_CALL_START' }> =>
chunk.type === 'TOOL_CALL_START',
)

expect(textStart).toBeDefined()
expect(toolStart).toBeDefined()
expect(toolStart?.parentMessageId).toBe(textStart?.messageId)
})
})
47 changes: 47 additions & 0 deletions packages/typescript/ai/tests/stream-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2485,6 +2485,53 @@ describe('StreamProcessor', () => {
expect(toolCallPart.state).toBe('input-complete')
}
})

it('should preserve the server message id in tool-first flows when parentMessageId is provided', () => {
const processor = new StreamProcessor()

processor.processChunk(
chunk('TOOL_CALL_START', {
toolCallId: 'tc-1',
toolCallName: 'lookupWeather',
toolName: 'lookupWeather',
parentMessageId: 'anthropic-msg-1',
}),
)

let messages = processor.getMessages()
expect(messages).toHaveLength(1)
expect(messages[0]?.id).toBe('anthropic-msg-1')

processor.processChunk(ev.toolArgs('tc-1', '{"location":"Berlin"}'))
processor.processChunk(ev.toolEnd('tc-1', 'lookupWeather'))

processor.processChunk(
chunk('TEXT_MESSAGE_START', {
messageId: 'anthropic-msg-1',
role: 'assistant' as const,
}),
)
processor.processChunk(ev.textContent('It is sunny.', 'anthropic-msg-1'))
processor.processChunk(ev.textEnd('anthropic-msg-1'))
processor.finalizeStream()

messages = processor.getMessages()
expect(messages).toHaveLength(1)
expect(messages[0]?.id).toBe('anthropic-msg-1')
expect(messages[0]?.parts).toEqual([
{
type: 'tool-call',
id: 'tc-1',
name: 'lookupWeather',
arguments: '{"location":"Berlin"}',
state: 'input-complete',
},
{
type: 'text',
content: 'It is sunny.',
},
])
})
})

describe('double onStreamEnd guard', () => {
Expand Down