@@ -68,6 +68,7 @@ export const HandleOpenAIResponse = async (aiResponse: IAIStreamResponse | any,
6868 // Handle both IAIStreamResponse format and plain response for backward compatibility
6969 const streamResponse = aiResponse ?. streamResponse || aiResponse
7070 const tool_callback = aiResponse ?. callback
71+ const toolExecutionStatusCallback = aiResponse ?. toolExecutionStatusCallback
7172 const reader = streamResponse . body ?. getReader ( ) ;
7273 const decoder = new TextDecoder ( "utf-8" ) ;
7374 let buffer = "" ;
@@ -143,7 +144,13 @@ export const HandleOpenAIResponse = async (aiResponse: IAIStreamResponse | any,
143144
144145 // Check if this is the finish reason for tool calls
145146 if ( json . choices ?. [ 0 ] ?. finish_reason === "tool_calls" && tool_callback && toolCalls . size > 0 ) {
147+ toolExecutionStatusCallback ?.( true ) ;
146148 const response = await tool_callback ( Array . from ( toolCalls . values ( ) ) )
149+ toolExecutionStatusCallback ?.( false ) ;
150+ // Keep the callback attached for recursive calls
151+ if ( response && typeof response === 'object' ) {
152+ response . toolExecutionStatusCallback = toolExecutionStatusCallback ;
153+ }
147154 cb ( "\n\n" ) ;
148155 HandleOpenAIResponse ( response , cb , done_cb )
149156 return ;
@@ -191,6 +198,7 @@ export const HandleMistralAIResponse = async (aiResponse: IAIStreamResponse | an
191198 // Handle both IAIStreamResponse format and plain response for backward compatibility
192199 const streamResponse = aiResponse ?. streamResponse || aiResponse
193200 const tool_callback = aiResponse ?. callback
201+ const toolExecutionStatusCallback = aiResponse ?. toolExecutionStatusCallback
194202 const reader = streamResponse . body ?. getReader ( ) ;
195203 const decoder = new TextDecoder ( "utf-8" ) ;
196204 let buffer = "" ;
@@ -234,7 +242,13 @@ export const HandleMistralAIResponse = async (aiResponse: IAIStreamResponse | an
234242 const json = JSON . parse ( jsonStr ) ;
235243 threadId = json ?. id || threadId ;
236244 if ( json . choices [ 0 ] . delta . tool_calls && tool_callback ) {
245+ toolExecutionStatusCallback ?.( true ) ;
237246 const response = await tool_callback ( json . choices [ 0 ] . delta . tool_calls )
247+ // Keep the callback attached for recursive calls
248+ if ( response && typeof response === 'object' ) {
249+ response . toolExecutionStatusCallback = toolExecutionStatusCallback ;
250+ }
251+ toolExecutionStatusCallback ?.( false ) ;
238252 cb ( "\n\n" ) ;
239253 HandleMistralAIResponse ( response , cb , done_cb )
240254 } else if ( json . choices [ 0 ] . delta . content ) {
@@ -259,6 +273,7 @@ export const HandleAnthropicResponse = async (aiResponse: IAIStreamResponse | an
259273 // Handle both IAIStreamResponse format and plain response for backward compatibility
260274 const streamResponse = aiResponse ?. streamResponse || aiResponse
261275 const tool_callback = aiResponse ?. callback
276+ const toolExecutionStatusCallback = aiResponse ?. toolExecutionStatusCallback
262277 const reader = streamResponse . body ?. getReader ( ) ;
263278 const decoder = new TextDecoder ( "utf-8" ) ;
264279 let buffer = "" ;
@@ -334,7 +349,13 @@ export const HandleAnthropicResponse = async (aiResponse: IAIStreamResponse | an
334349 } ) ) ;
335350
336351 if ( toolCalls . length > 0 ) {
352+ toolExecutionStatusCallback ?.( true ) ;
337353 const response = await tool_callback ( toolCalls )
354+ toolExecutionStatusCallback ?.( false ) ;
355+ // Keep the callback attached for recursive calls
356+ if ( response && typeof response === 'object' ) {
357+ response . toolExecutionStatusCallback = toolExecutionStatusCallback ;
358+ }
338359 cb ( "\n\n" ) ;
339360 HandleAnthropicResponse ( response , cb , done_cb )
340361 return ;
@@ -361,6 +382,7 @@ export const HandleOllamaResponse = async (aiResponse: IAIStreamResponse | any,
361382 // Handle both IAIStreamResponse format and plain response for backward compatibility
362383 const streamResponse = aiResponse ?. streamResponse || aiResponse
363384 const tool_callback = aiResponse ?. callback
385+ const toolExecutionStatusCallback = aiResponse ?. toolExecutionStatusCallback
364386 const reader = streamResponse . body ?. getReader ( ) ;
365387 const decoder = new TextDecoder ( "utf-8" ) ;
366388 let resultText = "" ;
@@ -395,7 +417,13 @@ export const HandleOllamaResponse = async (aiResponse: IAIStreamResponse | any,
395417
396418 // Handle tool calls in Ollama format
397419 if ( parsed . message ?. tool_calls && tool_callback ) {
420+ toolExecutionStatusCallback ?.( true ) ;
398421 const response = await tool_callback ( parsed . message . tool_calls )
422+ toolExecutionStatusCallback ?.( false ) ;
423+ // Keep the callback attached for recursive calls
424+ if ( response && typeof response === 'object' ) {
425+ response . toolExecutionStatusCallback = toolExecutionStatusCallback ;
426+ }
399427 cb ( "\n\n" ) ;
400428 HandleOllamaResponse ( response , cb , done_cb , reasoning_cb )
401429 return ;
0 commit comments