Skip to content

Commit d21a345

Browse files
committed
refactor: 移除 TurnExecutor 类并简化 Agent 实现
1 parent 0e0a795 commit d21a345

3 files changed

Lines changed: 14 additions & 152 deletions

File tree

src/agent/Agent.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
*/
55

66
import { EventEmitter } from 'events';
7+
import type { ChatCompletionMessageToolCall } from 'openai/resources/chat';
78
import { ConfigManager } from '../config/config-manager.js';
89
import { PromptBuilder } from '../prompts/index.js';
910
import { ChatService, type Message } from '../services/ChatService.js';
1011
import { getBuiltinTools } from '../tools/builtin/index.js';
11-
import type { Tool } from '../tools/types/index.js';
1212
import { 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';
1414
import { getEnvironmentContext } from '../utils/environment.js';
1515
import { type ContextManager, ExecutionEngine } from './ExecutionEngine.js';
1616
import {
1717
type LoopDetectionConfig,
1818
LoopDetectionService,
1919
} from './LoopDetectionService.js';
20-
import { TurnExecutor } from './TurnExecutor.js';
2120
import 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
);

src/agent/TurnExecutor.ts

Lines changed: 0 additions & 113 deletions
This file was deleted.

src/agent/types.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import type { ChatCompletionMessageToolCall } from 'openai/resources/chat';
66
import type { ChatConfig, Message } from '../services/ChatService.js';
77

8-
98
/**
109
* 聊天上下文接口
1110
*/
@@ -122,9 +121,14 @@ export interface LoopOptions {
122121
signal?: AbortSignal;
123122
stream?: boolean;
124123
onTurnStart?: (data: { turn: number; maxTurns: number }) => void;
125-
onToolUse?: (toolCall: ChatCompletionMessageToolCall) => Promise<ChatCompletionMessageToolCall | void>;
124+
onToolUse?: (
125+
toolCall: ChatCompletionMessageToolCall
126+
) => Promise<ChatCompletionMessageToolCall | void>;
126127
onToolApprove?: (toolCall: ChatCompletionMessageToolCall) => Promise<boolean>;
127-
onToolResult?: (toolCall: ChatCompletionMessageToolCall, result: any) => Promise<any | void>;
128+
onToolResult?: (
129+
toolCall: ChatCompletionMessageToolCall,
130+
result: any
131+
) => Promise<any | void>;
128132
}
129133

130134
export interface LoopResult {
@@ -141,26 +145,3 @@ export interface LoopResult {
141145
duration: number;
142146
};
143147
}
144-
145-
// ===== TurnExecutor Types =====
146-
147-
export interface TurnOptions {
148-
maxRetries?: number;
149-
stream?: boolean;
150-
onTextDelta?: (text: string) => void;
151-
onReasoning?: (reasoning: string) => void;
152-
}
153-
154-
export interface TurnResult {
155-
content: string;
156-
toolCalls?: ChatCompletionMessageToolCall[];
157-
usage?: {
158-
promptTokens: number;
159-
completionTokens: number;
160-
totalTokens: number;
161-
};
162-
}
163-
164-
export interface TurnExecutorConfig {
165-
// 预留配置项
166-
}

0 commit comments

Comments
 (0)