2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
- import { Tool } from '../tools/Tools.js' ;
6
- import { OpenAIClient , OpenAIResponse } from '../core/OpenAIClient.js' ;
7
- import { ChatMessage , ChatMessageEntity , ModelChatMessage , ToolResultMessage } from '../ui/ChatView.js' ;
8
5
import { ChatPromptFormatter } from '../core/Graph.js' ;
9
6
import { enhancePromptWithPageContext } from '../core/PageInfoManager.js' ;
10
- import { ConfigurableAgentArgs , ConfigurableAgentResult , AgentRunTerminationReason , ConfigurableAgentTool , ToolRegistry , HandoffConfig , HandoffTrigger /*, HandoffContextTransform, ContextFilterRegistry*/ } from './ConfigurableAgentTool.js' ;
7
+ import { UnifiedLLMClient , type UnifiedLLMResponse , type ParsedLLMAction } from '../core/UnifiedLLMClient.js' ;
8
+ import type { Tool } from '../tools/Tools.js' ;
9
+ import { ChatMessageEntity , type ChatMessage , type ModelChatMessage , type ToolResultMessage } from '../ui/ChatView.js' ;
10
+
11
+ import { ConfigurableAgentTool , ToolRegistry , HandoffTrigger , type ConfigurableAgentArgs , type ConfigurableAgentResult , type AgentRunTerminationReason , type HandoffConfig /* , HandoffContextTransform, ContextFilterRegistry*/ } from './ConfigurableAgentTool.js' ;
11
12
12
13
/**
13
14
* Configuration for the AgentRunner
@@ -16,7 +17,7 @@ export interface AgentRunnerConfig {
16
17
apiKey : string ;
17
18
modelName : string ;
18
19
systemPrompt : string ;
19
- tools : Tool < any , any > [ ] ;
20
+ tools : Array < Tool < any , any > > ;
20
21
maxIterations : number ;
21
22
temperature ?: number ;
22
23
}
@@ -103,11 +104,13 @@ export class AgentRunner {
103
104
104
105
// Enhance the target agent's system prompt with page context
105
106
const enhancedSystemPrompt = await enhancePromptWithPageContext ( targetConfig . systemPrompt ) ;
106
-
107
+
107
108
// Construct Runner Config & Hooks for the target agent
108
109
const targetRunnerConfig : AgentRunnerConfig = {
109
- apiKey : apiKey ,
110
- modelName : targetConfig . modelName || defaultModelName ,
110
+ apiKey,
111
+ modelName : typeof targetConfig . modelName === 'function'
112
+ ? targetConfig . modelName ( )
113
+ : ( targetConfig . modelName || defaultModelName ) ,
111
114
systemPrompt : enhancedSystemPrompt ,
112
115
tools : targetConfig . tools
113
116
. map ( toolName => ToolRegistry . getRegisteredTool ( toolName ) )
@@ -153,21 +156,21 @@ export class AgentRunner {
153
156
intermediateSteps : combinedIntermediateSteps ,
154
157
terminationReason : handoffResult . terminationReason || 'handed_off' ,
155
158
} ;
156
- } else {
157
- // Otherwise (default), omit the target's intermediate steps
158
- console . log ( `[AgentRunner] Omitting intermediateSteps from ${ targetAgentTool . name } based on its config (default or flag set to false).` ) ;
159
- // Return result from target, ensuring intermediateSteps are omitted
160
- const finalResult = {
161
- ...handoffResult ,
162
- terminationReason : handoffResult . terminationReason || 'handed_off' ,
163
- } ;
164
- // Explicitly delete intermediateSteps if they somehow exist on handoffResult (shouldn't due to target config)
165
- delete finalResult . intermediateSteps ;
166
- return finalResult ;
167
159
}
160
+ // Otherwise (default), omit the target's intermediate steps
161
+ console . log ( `[AgentRunner] Omitting intermediateSteps from ${ targetAgentTool . name } based on its config (default or flag set to false).` ) ;
162
+ // Return result from target, ensuring intermediateSteps are omitted
163
+ const finalResult = {
164
+ ...handoffResult ,
165
+ terminationReason : handoffResult . terminationReason || 'handed_off' ,
166
+ } ;
167
+ // Explicitly delete intermediateSteps if they somehow exist on handoffResult (shouldn't due to target config)
168
+ delete finalResult . intermediateSteps ;
169
+ return finalResult ;
170
+
168
171
}
169
172
170
- public static async run (
173
+ static async run (
171
174
initialMessages : ChatMessage [ ] ,
172
175
args : ConfigurableAgentArgs ,
173
176
config : AgentRunnerConfig ,
@@ -195,7 +198,7 @@ export class AgentRunner {
195
198
} ) ) ;
196
199
197
200
// Add handoff tools based on the executing agent's config
198
- if ( executingAgent && executingAgent . config . handoffs ) {
201
+ if ( executingAgent ? .config . handoffs ) {
199
202
// Iterate over the configured handoffs
200
203
for ( const handoffConfig of executingAgent . config . handoffs ) {
201
204
// Only add handoffs triggered by LLM tool calls to the schema
@@ -226,7 +229,7 @@ export class AgentRunner {
226
229
227
230
for ( iteration = 0 ; iteration < maxIterations ; iteration ++ ) {
228
231
console . log ( `[AgentRunner] ${ agentName } Iteration ${ iteration + 1 } /${ maxIterations } ` ) ;
229
-
232
+
230
233
// Prepare prompt and call LLM
231
234
const iterationInfo = `
232
235
## Current Progress
@@ -238,10 +241,10 @@ export class AgentRunner {
238
241
const currentSystemPrompt = await enhancePromptWithPageContext ( systemPrompt + iterationInfo ) ;
239
242
240
243
const promptText = promptFormatter . format ( { messages } ) ;
241
- let openAIResponse : OpenAIResponse ;
244
+ let llmResponse : UnifiedLLMResponse ;
242
245
try {
243
246
console . log ( `[AgentRunner] ${ agentName } Calling LLM. Prompt size: ${ promptText . length } ` ) ;
244
- openAIResponse = await OpenAIClient . callOpenAI (
247
+ llmResponse = await UnifiedLLMClient . callLLMWithResponse (
245
248
apiKey ,
246
249
modelName ,
247
250
promptText ,
@@ -268,7 +271,7 @@ export class AgentRunner {
268
271
}
269
272
270
273
// Parse LLM response
271
- const parsedAction = OpenAIClient . parseOpenAIResponse ( openAIResponse ) ;
274
+ const parsedAction = UnifiedLLMClient . parseResponse ( llmResponse ) ;
272
275
273
276
// Process parsed action
274
277
try {
@@ -279,10 +282,10 @@ export class AgentRunner {
279
282
newModelMessage = {
280
283
entity : ChatMessageEntity . MODEL ,
281
284
action : 'tool' ,
282
- toolName : toolName ,
283
- toolArgs : toolArgs ,
285
+ toolName,
286
+ toolArgs,
284
287
isFinalAnswer : false ,
285
- reasoning : openAIResponse . reasoning ?. summary ,
288
+ reasoning : llmResponse . reasoning ?. summary ,
286
289
} ;
287
290
messages . push ( newModelMessage ) ;
288
291
console . log ( `[AgentRunner] ${ agentName } LLM requested tool: ${ toolName } ` ) ;
@@ -324,7 +327,8 @@ export class AgentRunner {
324
327
// LLM tool handoff replaces the current agent's execution entirely
325
328
return handoffResult ;
326
329
327
- } else if ( ! toolToExecute ) { // Regular tool, but not found
330
+ }
331
+ if ( ! toolToExecute ) { // Regular tool, but not found
328
332
throw new Error ( `Agent requested unknown tool: ${ toolName } ` ) ;
329
333
} else {
330
334
// *** Regular tool execution ***
@@ -356,7 +360,7 @@ export class AgentRunner {
356
360
// Add tool result message
357
361
const toolResultMessage : ToolResultMessage = {
358
362
entity : ChatMessageEntity . TOOL_RESULT ,
359
- toolName : toolName ,
363
+ toolName,
360
364
resultText : toolResultText ,
361
365
isError : toolIsError ,
362
366
...( toolIsError && { error : toolResultText } ) , // Include raw error message if error occurred
@@ -370,9 +374,9 @@ export class AgentRunner {
370
374
newModelMessage = {
371
375
entity : ChatMessageEntity . MODEL ,
372
376
action : 'final' ,
373
- answer : answer ,
377
+ answer,
374
378
isFinalAnswer : true ,
375
- reasoning : openAIResponse . reasoning ?. summary ,
379
+ reasoning : llmResponse . reasoning ?. summary ,
376
380
} ;
377
381
messages . push ( newModelMessage ) ;
378
382
console . log ( `[AgentRunner] ${ agentName } LLM provided final answer.` ) ;
@@ -403,7 +407,7 @@ export class AgentRunner {
403
407
// Max iterations reached - Check for 'max_iterations' handoff trigger
404
408
console . warn ( `[AgentRunner] ${ agentName } Reached max iterations (${ maxIterations } ) without completion.` ) ;
405
409
406
- if ( executingAgent && executingAgent . config . handoffs ) {
410
+ if ( executingAgent ? .config . handoffs ) {
407
411
const maxIterHandoffConfig = executingAgent . config . handoffs . find ( h => h . trigger === 'max_iterations' ) ;
408
412
409
413
if ( maxIterHandoffConfig ) {
@@ -427,4 +431,4 @@ export class AgentRunner {
427
431
console . warn ( `[AgentRunner] ${ agentName } No 'max_iterations' handoff configured. Returning error.` ) ;
428
432
return createErrorResult ( `Agent reached maximum iterations (${ maxIterations } )` , messages , 'max_iterations' ) ;
429
433
}
430
- }
434
+ }
0 commit comments