fix: v0.5.2 紧急修复 — 工具调用完全失效(引擎懒创建不带工具)+ DeepSeek 余额解析错误
P0 工具调用失效(用户实测反馈:Agnes/DeepSeek 均无法调用工具): - 根因:AgentEngineManager.createEngine 未调用 setTools。引擎是懒创建的 (首次 sendMessage 时 getEngine),启动期的 setToolsAll 调用时 engines Map 为空(全是 no-op)→ 新引擎 this.tools=[] → LLM 请求不带 tools → 模型无法发起 tool_call。症状与用户反馈完全吻合:模型口头说要调工具 (模仿历史消息中的工具调用模式),实际不调,凭记忆瞎编结果。 - 引入点:v0.4.0 P2-10 每会话引擎重构(v0.3.x 全局单引擎时代 setTools 直接作用于唯一引擎,无此问题)。 - 修复:createEngine 从 toolRegistry 拉取当前启用工具(registry 是启用 状态的唯一事实源,MCP 后注册/工具开关场景均一致)。 P1 DeepSeek 余额显示错误(用户实测反馈:显示的不是真实余额): - 根因:DeepSeek 官方 /user/balance 实际返回 balance_infos 数组格式, 此前按扁平字段解析(data.total_balance)→ 恒为 undefined → 界面恒显示 0。 - 修复:优先解析 balance_infos[0],回退扁平格式(网关兼容);URL 规范化 (剥离尾斜杠与 /v1 前缀 — 余额端点在根路径,chat 端点两种写法都合法)。 测试(215 → 224 用例): - 新增 AgentEngineManager 回归测试 ×4:懒创建引擎的 LLM 请求必须携带 registry 工具定义(本次事故的直接拦截测试)/ 禁用工具不出现 / setToolsAll 热更新 / 无 registry 时行为不回归 - 新增 DeepSeek 余额解析测试 ×5:官方数组格式 / 扁平回退 / URL 规范化 / 非 2xx / 网络异常 - 补强引擎链路测试:mock adapter 记录请求并断言 tools 契约 — 此前 mock 无条件吐 tool_call 事件,掩盖了"请求未携带工具定义"的缺陷(复检盲区 的直接教训:mock 必须断言请求契约,否则测试是道具) 验证: lint 0 / typecheck 双工程 0 / test:electron 224 全过 / build 成功
This commit is contained in:
@@ -39,14 +39,16 @@ export class AgentEngineManager extends EventEmitter {
|
||||
private baseConfig: Partial<AgentLoopConfig>;
|
||||
private workspacePath = '';
|
||||
|
||||
constructor(private opts: {
|
||||
/** adapter 工厂(每次调用返回新实例;闭包内读取最新配置) */
|
||||
buildAdapter: () => IMetonaProviderAdapter;
|
||||
baseConfig: Partial<AgentLoopConfig>;
|
||||
toolRegistry?: ToolRegistry;
|
||||
preToolHooks?: PreToolHook[];
|
||||
postToolHooks?: PostToolHook[];
|
||||
}) {
|
||||
constructor(
|
||||
private opts: {
|
||||
/** adapter 工厂(每次调用返回新实例;闭包内读取最新配置) */
|
||||
buildAdapter: () => IMetonaProviderAdapter;
|
||||
baseConfig: Partial<AgentLoopConfig>;
|
||||
toolRegistry?: ToolRegistry;
|
||||
preToolHooks?: PreToolHook[];
|
||||
postToolHooks?: PostToolHook[];
|
||||
},
|
||||
) {
|
||||
super();
|
||||
this.primaryAdapter = opts.buildAdapter();
|
||||
this.baseConfig = { ...opts.baseConfig };
|
||||
@@ -153,8 +155,19 @@ export class AgentEngineManager extends EventEmitter {
|
||||
);
|
||||
engine.setFallbackAdapter(this.fallbackAdapter);
|
||||
if (this.workspacePath) engine.setWorkspacePath(this.workspacePath);
|
||||
// v0.5.2 关键修复: 新建引擎从 registry 拉取当前启用工具。
|
||||
// setToolsAll 只同步"已存在"的引擎 — 引擎是懒创建的(首次 sendMessage 时 getEngine),
|
||||
// 启动期的 setToolsAll 调用时 engines Map 为空,全是 no-op。
|
||||
// 此前缺此调用 → 新引擎 this.tools=[] → LLM 请求不带 tools →
|
||||
// 模型无法发起 tool_call(症状:模型口头说要调工具,实际不调,凭历史记忆瞎编)。
|
||||
// v0.4.0 P2-10 引入每会话引擎时遗留的回归,v0.5.2 修复。
|
||||
if (this.opts.toolRegistry) {
|
||||
engine.setTools(this.opts.toolRegistry.listTools());
|
||||
}
|
||||
this.forwardEngineEvents(engine, sessionId);
|
||||
log.debug(`[EngineManager] engine created for session ${sessionId} (total: ${this.engines.size + 1})`);
|
||||
log.debug(
|
||||
`[EngineManager] engine created for session ${sessionId} (total: ${this.engines.size + 1})`,
|
||||
);
|
||||
return engine;
|
||||
}
|
||||
|
||||
@@ -167,7 +180,8 @@ export class AgentEngineManager extends EventEmitter {
|
||||
const payload = { ...data, sessionId: data.sessionId || sessionId };
|
||||
// 维护运行中集合(LRU 淘汰保护)
|
||||
if (payload.state === 'INIT' || payload.current === 'INIT') this.running.add(sessionId);
|
||||
if (payload.state === 'TERMINATED' || payload.current === 'TERMINATED') this.running.delete(sessionId);
|
||||
if (payload.state === 'TERMINATED' || payload.current === 'TERMINATED')
|
||||
this.running.delete(sessionId);
|
||||
this.emit('stateChange', payload);
|
||||
});
|
||||
engine.on('complete', (data) => {
|
||||
|
||||
Reference in New Issue
Block a user