fix: Agent Loop 多轮迭代消息保存和 token 统计

- 修复 onDone 多轮迭代时更新旧消息而非创建新消息,导致 post-tool 回复丢失
- 修复 pre-tool 消息的 toolCalls 未保存(onNewIteration 未传递 toolCalls)
- 修复最后两条 AI 消息无 token 显示(统计附着到被覆盖的消息上)
- onNewIteration 回调增加 toolCalls 参数,保存消息时附带工具调用记录
- handleRetry 同步修复
This commit is contained in:
thzxx
2026-04-21 14:08:07 +08:00
parent dc3d6e4b69
commit 9915b71da1
2 changed files with 60 additions and 42 deletions
+2 -2
View File
@@ -359,7 +359,7 @@ export interface AgentCallbacks {
onDone: (finalContent: string, toolRecords?: ToolCallRecord[], stats?: { eval_count?: number; total_duration?: number }) => void;
onConfirmTool: (call: ToolCall) => Promise<boolean>;
/** Agent Loop 新迭代开始(前一轮工具执行完毕,下一轮流式输出即将开始) */
onNewIteration?: () => void;
onNewIteration?: (toolCalls?: ToolCall[]) => void;
}
/** 保存执行轨迹到 SQLite */
@@ -535,7 +535,7 @@ export async function runAgentLoop(
// 非首轮迭代:通知 UI 创建新的消息气泡(防止每轮内容互相覆盖)
if (loopCount > 1 && callbacks.onNewIteration) {
callbacks.onNewIteration();
callbacks.onNewIteration(toolCalls.length > 0 ? toolCalls : undefined);
}
logAgentLoop(loopCount, maxLoops);