@@ -15,7 +15,12 @@ import {
1515} from './message-updaters'
1616import { DefaultChatClientEventEmitter } from './events'
1717import type { ModelMessage } from '@tanstack/ai'
18- import type { ChatClientOptions , ToolCallPart , UIMessage } from './types'
18+ import type {
19+ ChatClientOptions ,
20+ ToolCallPart ,
21+ ToolResultState ,
22+ UIMessage ,
23+ } from './types'
1924import type { ConnectionAdapter } from './connection-adapters'
2025import type { ChunkStrategy , StreamParser } from './stream/types'
2126import type { ChatClientEventEmitter } from './events'
@@ -392,17 +397,45 @@ export class ChatClient {
392397 result . state || 'output-available' ,
393398 )
394399
395- // Update the tool call part with the output
396- this . setMessages (
397- updateToolCallWithOutput (
398- this . messages ,
399- result . toolCallId ,
400- result . output ,
401- result . state === 'output-error' ? 'input-complete' : undefined ,
402- result . errorText ,
400+ // Find the message containing this tool call
401+ const messageWithToolCall = this . messages . find ( ( msg ) =>
402+ msg . parts . some (
403+ ( p ) : p is ToolCallPart =>
404+ p . type === 'tool-call' && p . id === result . toolCallId ,
403405 ) ,
404406 )
405407
408+ // Step 1: Update the tool call part with the output
409+ let updatedMessages = updateToolCallWithOutput (
410+ this . messages ,
411+ result . toolCallId ,
412+ result . output ,
413+ result . state === 'output-error' ? 'input-complete' : undefined ,
414+ result . errorText ,
415+ )
416+
417+ // Step 2: Also create a tool-result part (needed for LLM conversation history)
418+ if ( messageWithToolCall ) {
419+ const content =
420+ typeof result . output === 'string'
421+ ? result . output
422+ : JSON . stringify ( result . output )
423+ const toolResultState : ToolResultState = result . errorText
424+ ? 'error'
425+ : 'complete'
426+
427+ updatedMessages = updateToolResultPart (
428+ updatedMessages ,
429+ messageWithToolCall . id ,
430+ result . toolCallId ,
431+ content ,
432+ toolResultState ,
433+ result . errorText ,
434+ )
435+ }
436+
437+ this . setMessages ( updatedMessages )
438+
406439 // Check if we should auto-send
407440 if ( this . shouldAutoSend ( ) ) {
408441 // Continue the flow without adding a new message
0 commit comments