44 */
55
66import { EventEmitter } from 'events' ;
7+ import type { ChatCompletionMessageToolCall } from 'openai/resources/chat' ;
78import { ConfigManager } from '../config/config-manager.js' ;
89import { PromptBuilder } from '../prompts/index.js' ;
910import { ChatService , type Message } from '../services/ChatService.js' ;
1011import { getBuiltinTools } from '../tools/builtin/index.js' ;
11- import type { Tool } from '../tools/types/index.js' ;
1212import { ToolRegistry } from '../tools/registry/ToolRegistry.js' ;
13- import type { ToolResult } from '../tools/types/index.js' ;
13+ import type { Tool , ToolResult } from '../tools/types/index.js' ;
1414import { getEnvironmentContext } from '../utils/environment.js' ;
1515import { type ContextManager , ExecutionEngine } from './ExecutionEngine.js' ;
1616import {
1717 type LoopDetectionConfig ,
1818 LoopDetectionService ,
1919} from './LoopDetectionService.js' ;
20- import { TurnExecutor } from './TurnExecutor.js' ;
2120import type {
2221 AgentConfig ,
2322 AgentOptions ,
@@ -266,9 +265,6 @@ export class Agent extends EventEmitter {
266265 let turnsCount = 0 ;
267266 const allToolResults : ToolResult [ ] = [ ] ;
268267
269- // 创建 TurnExecutor 实例(带重试机制)
270- const turnExecutor = new TurnExecutor ( this . chatService , { } ) ;
271-
272268 while ( turnsCount < maxTurns ) {
273269 // === 检查中断信号 ===
274270 if ( options ?. signal ?. aborted ) {
@@ -293,12 +289,8 @@ export class Agent extends EventEmitter {
293289 this . emit ( 'loopTurnStart' , { turn : turnsCount , maxTurns } ) ;
294290 options ?. onTurnStart ?.( { turn : turnsCount , maxTurns } ) ;
295291
296- // 3. 调用 TurnExecutor 执行单轮对话(带重试机制)
297- const turnResult = await turnExecutor . execute ( messages , tools , {
298- maxRetries : 3 ,
299- stream : options ?. stream ,
300- onTextDelta : ( text ) => this . emit ( 'textDelta' , { text, turn : turnsCount } ) ,
301- } ) ;
292+ // 3. 直接调用 ChatService(OpenAI SDK 已内置重试机制)
293+ const turnResult = await this . chatService . chat ( messages , tools ) ;
302294
303295 // 4. 检查是否需要工具调用(任务完成条件)
304296 if ( ! turnResult . toolCalls || turnResult . toolCalls . length === 0 ) {
@@ -388,7 +380,9 @@ export class Agent extends EventEmitter {
388380
389381 // 7. 循环检测 - 检测是否陷入死循环
390382 const loopDetected = await this . loopDetector . detect (
391- turnResult . toolCalls . filter ( ( tc ) => tc . type === 'function' ) ,
383+ turnResult . toolCalls . filter (
384+ ( tc : ChatCompletionMessageToolCall ) => tc . type === 'function'
385+ ) ,
392386 turnsCount ,
393387 messages
394388 ) ;
0 commit comments