88 ToolResultPart ,
99 ToolSet ,
1010 tool as makeTool ,
11+ ToolCallPart ,
1112} from 'ai' ;
1213import chalk from 'chalk' ;
1314
@@ -117,6 +118,11 @@ function processResponse(response: Anthropic.Message) {
117118}
118119*/
119120
121+ type ErrorResult = {
122+ errorMessage : string ;
123+ errorType : string ;
124+ } ;
125+
120126async function executeTools (
121127 toolCalls : ToolUseContent [ ] ,
122128 tools : Tool [ ] ,
@@ -159,8 +165,12 @@ async function executeTools(
159165 tokenTracker : new TokenTracker ( call . name , context . tokenTracker ) ,
160166 } ) ;
161167 } catch ( error : any ) {
162- toolResult = `Error: Exception thrown during tool execution. Type: ${ error . constructor . name } , Message: ${ error . message } ` ;
168+ toolResult = JSON . stringify ( {
169+ errorMessage : error . message ,
170+ errorType : error . constructor . name ,
171+ } ) ;
163172 }
173+
164174 return {
165175 type : 'tool-result' ,
166176 toolCallId : call . id ,
@@ -173,7 +183,8 @@ async function executeTools(
173183 const sequenceCompletedTool = toolResults . find (
174184 ( r ) => r . toolName === 'sequenceComplete' ,
175185 ) ;
176- const completionResult = sequenceCompletedTool ?. result as string ;
186+ const completionResult = ( sequenceCompletedTool ?. result as { result : string } )
187+ . result ;
177188
178189 messages . push ( {
179190 role : 'tool' ,
@@ -292,7 +303,13 @@ export const toolAgent = async (
292303 messages : messagesWithCacheControl ,
293304 tools : toolSet ,
294305 } ;
295- const { text, toolCalls } = await generateText ( generateTextProps ) ;
306+ const { text, toolCalls, ...other } = await generateText ( generateTextProps ) ;
307+
308+ //console.log(
309+ // 'providerMetadata',
310+ // JSON.stringify(other.providerMetadata, null, 2),
311+ //);
312+ //console.log('other data', JSON.stringify(other, null, 2));
296313
297314 const localToolCalls : ToolUseContent [ ] = toolCalls . map ( ( call ) => ( {
298315 type : 'tool_use' ,
@@ -329,6 +346,20 @@ export const toolAgent = async (
329346 logger . info ( text ) ;
330347 }
331348
349+ if ( toolCalls . length > 0 ) {
350+ const toolCallParts : Array < ToolCallPart > = toolCalls . map ( ( toolCall ) => ( {
351+ type : 'tool-call' ,
352+ toolCallId : toolCall . toolCallId ,
353+ toolName : toolCall . toolName ,
354+ args : toolCall . args ,
355+ } ) ) ;
356+
357+ messages . push ( {
358+ role : 'assistant' ,
359+ content : toolCallParts ,
360+ } ) ;
361+ }
362+
332363 /*logger.log(
333364 tokenTracker.logLevel,
334365 chalk.blue(`[Token Usage/Message] ${tokenUsagePerMessage.toString()}`),
0 commit comments