@@ -169,126 +169,114 @@ export const toolAgent = async (
169
169
let totalOutputTokens = 0 ;
170
170
let interactions = 0 ;
171
171
172
- try {
173
- const apiKey = process . env . ANTHROPIC_API_KEY ;
174
- if ( ! apiKey ) throw new Error ( getAnthropicApiKeyError ( ) ) ;
172
+ const apiKey = process . env . ANTHROPIC_API_KEY ;
173
+ if ( ! apiKey ) throw new Error ( getAnthropicApiKeyError ( ) ) ;
175
174
176
- const client = new Anthropic ( { apiKey } ) ;
177
- const messages : Message [ ] = [
178
- {
179
- role : "user" ,
180
- content : [ { type : "text" , text : initialPrompt } ] ,
181
- } ,
182
- ] ;
175
+ const client = new Anthropic ( { apiKey } ) ;
176
+ const messages : Message [ ] = [
177
+ {
178
+ role : "user" ,
179
+ content : [ { type : "text" , text : initialPrompt } ] ,
180
+ } ,
181
+ ] ;
183
182
184
- logger . debug ( "User message:" , initialPrompt ) ;
183
+ logger . debug ( "User message:" , initialPrompt ) ;
185
184
186
- // Get the system prompt once at the start
187
- const systemPrompt = await config . getSystemPrompt ( ) ;
185
+ // Get the system prompt once at the start
186
+ const systemPrompt = await config . getSystemPrompt ( ) ;
188
187
189
- for ( let i = 0 ; i < config . maxIterations ; i ++ ) {
190
- logger . verbose (
191
- `Requesting completion ${ i + 1 } with ${ messages . length } messages with ${
192
- JSON . stringify ( messages ) . length
193
- } bytes`
194
- ) ;
195
-
196
- interactions ++ ;
197
- const response = await client . messages . create ( {
198
- model : config . model ,
199
- max_tokens : config . maxTokens ,
200
- temperature : config . temperature ,
201
- messages,
202
- system : systemPrompt ,
203
- tools : tools . map ( ( t ) => ( {
204
- name : t . name ,
205
- description : t . description ,
206
- input_schema : t . parameters as Anthropic . Tool . InputSchema ,
207
- } ) ) ,
208
- tool_choice : { type : "auto" } ,
209
- } ) ;
210
-
211
- if ( ! response . content . length ) {
212
- const result = {
213
- result :
214
- "Agent returned empty message implying it is done its given task" ,
215
- tokens : {
216
- input : totalInputTokens ,
217
- output : totalOutputTokens ,
218
- } ,
219
- interactions,
220
- } ;
221
- logger . verbose (
222
- `Agent completed with ${ result . tokens . input } input tokens, ${ result . tokens . output } output tokens in ${ result . interactions } interactions`
223
- ) ;
224
- return result ;
225
- }
188
+ for ( let i = 0 ; i < config . maxIterations ; i ++ ) {
189
+ logger . verbose (
190
+ `Requesting completion ${ i + 1 } with ${ messages . length } messages with ${
191
+ JSON . stringify ( messages ) . length
192
+ } bytes`
193
+ ) ;
226
194
227
- totalInputTokens += response . usage . input_tokens ;
228
- totalOutputTokens += response . usage . output_tokens ;
195
+ interactions ++ ;
196
+ const response = await client . messages . create ( {
197
+ model : config . model ,
198
+ max_tokens : config . maxTokens ,
199
+ temperature : config . temperature ,
200
+ messages,
201
+ system : systemPrompt ,
202
+ tools : tools . map ( ( t ) => ( {
203
+ name : t . name ,
204
+ description : t . description ,
205
+ input_schema : t . parameters as Anthropic . Tool . InputSchema ,
206
+ } ) ) ,
207
+ tool_choice : { type : "auto" } ,
208
+ } ) ;
209
+
210
+ if ( ! response . content . length ) {
211
+ const result = {
212
+ result :
213
+ "Agent returned empty message implying it is done its given task" ,
214
+ tokens : {
215
+ input : totalInputTokens ,
216
+ output : totalOutputTokens ,
217
+ } ,
218
+ interactions,
219
+ } ;
229
220
logger . verbose (
230
- ` Token usage: ${ response . usage . input_tokens } input, ${ response . usage . output_tokens } output`
221
+ `Agent completed with ${ result . tokens . input } input tokens , ${ result . tokens . output } output tokens in ${ result . interactions } interactions `
231
222
) ;
223
+ return result ;
224
+ }
232
225
233
- const { content, toolCalls } = processResponse ( response ) ;
234
- messages . push ( { role : "assistant" , content } ) ;
235
-
236
- // Log the assistant's message
237
- const assistantMessage = content
238
- . filter ( ( c ) => c . type === "text" )
239
- . map ( ( c ) => ( c as TextContent ) . text )
240
- . join ( "\\n" ) ;
241
- if ( assistantMessage ) {
242
- logger . info ( assistantMessage ) ;
243
- }
226
+ totalInputTokens += response . usage . input_tokens ;
227
+ totalOutputTokens += response . usage . output_tokens ;
228
+ logger . verbose (
229
+ ` Token usage: ${ response . usage . input_tokens } input, ${ response . usage . output_tokens } output`
230
+ ) ;
244
231
245
- const { sequenceCompleted, completionResult } = await executeTools (
246
- toolCalls ,
247
- tools ,
248
- messages ,
249
- logger
250
- ) ;
232
+ const { content, toolCalls } = processResponse ( response ) ;
233
+ messages . push ( { role : "assistant" , content } ) ;
251
234
252
- if ( sequenceCompleted ) {
253
- const result = {
254
- result :
255
- completionResult ??
256
- "Sequence explicitly completed with an empty result" ,
257
- tokens : {
258
- input : totalInputTokens ,
259
- output : totalOutputTokens ,
260
- } ,
261
- interactions,
262
- } ;
263
- logger . verbose (
264
- `Agent completed with ${ result . tokens . input } input tokens, ${ result . tokens . output } output tokens in ${ result . interactions } interactions`
265
- ) ;
266
- return result ;
267
- }
235
+ // Log the assistant's message
236
+ const assistantMessage = content
237
+ . filter ( ( c ) => c . type === "text" )
238
+ . map ( ( c ) => ( c as TextContent ) . text )
239
+ . join ( "\\n" ) ;
240
+ if ( assistantMessage ) {
241
+ logger . info ( assistantMessage ) ;
268
242
}
269
243
270
- logger . warn ( "Maximum iterations reached" ) ;
271
- const result = {
272
- result :
273
- "Maximum sub-agent iterations reach without successful completion" ,
274
- tokens : {
275
- input : totalInputTokens ,
276
- output : totalOutputTokens ,
277
- } ,
278
- interactions,
279
- } ;
280
- logger . verbose (
281
- `Agent completed with ${ result . tokens . input } input tokens, ${ result . tokens . output } output tokens in ${ result . interactions } interactions`
244
+ const { sequenceCompleted, completionResult } = await executeTools (
245
+ toolCalls ,
246
+ tools ,
247
+ messages ,
248
+ logger
282
249
) ;
283
- return result ;
284
- } catch ( error ) {
285
- const errorMessage =
286
- error instanceof Error ? error . message : "Unknown error" ;
287
- logger . error (
288
- "Agent execution failed" ,
289
- errorMessage ,
290
- ( error as Error ) ?. stack
291
- ) ;
292
- throw error ;
250
+
251
+ if ( sequenceCompleted ) {
252
+ const result = {
253
+ result :
254
+ completionResult ??
255
+ "Sequence explicitly completed with an empty result" ,
256
+ tokens : {
257
+ input : totalInputTokens ,
258
+ output : totalOutputTokens ,
259
+ } ,
260
+ interactions,
261
+ } ;
262
+ logger . verbose (
263
+ `Agent completed with ${ result . tokens . input } input tokens, ${ result . tokens . output } output tokens in ${ result . interactions } interactions`
264
+ ) ;
265
+ return result ;
266
+ }
293
267
}
268
+
269
+ logger . warn ( "Maximum iterations reached" ) ;
270
+ const result = {
271
+ result : "Maximum sub-agent iterations reach without successful completion" ,
272
+ tokens : {
273
+ input : totalInputTokens ,
274
+ output : totalOutputTokens ,
275
+ } ,
276
+ interactions,
277
+ } ;
278
+ logger . verbose (
279
+ `Agent completed with ${ result . tokens . input } input tokens, ${ result . tokens . output } output tokens in ${ result . interactions } interactions`
280
+ ) ;
281
+ return result ;
294
282
} ;
0 commit comments