fix: Agent Loop 注入工具路径上下文引导,避免连续操作时路径丢失

This commit is contained in:
thzxx
2026-04-07 11:05:52 +08:00
parent 4e11bd549c
commit 06e1900435
4 changed files with 21 additions and 65 deletions
+9 -1
View File
@@ -26,6 +26,12 @@ const MAX_LOOP_TIME = 300000; // 5 分钟总超时
const TOOL_EXEC_TIMEOUT = 30000; // 工具执行超时 30 秒
const STREAM_TIMEOUT = 120000; // 单次流式调用超时 2 分钟
const TOOL_USAGE_GUIDE = `【工具使用规则】
1. 路径上下文:当用户说"修改这个文件"、"删除它"、"查看里面"等未指定路径时,必须从本次对话中最近的工具调用结果中提取路径。例如用户之前让你读取了 /home/user/project/config.json,后续说"修改配置"就应继续使用该路径,而不是推测新路径。
2. 工具调用结果中的 path 字段是权威的文件路径,优先使用它。
3. 如果对话中从未出现过相关路径,应先用 search_files 或 list_directory 定位,不要凭空猜测路径。
4. 对同一文件的连续操作(读取→修改、读取→删除),路径必须一致。`;
export interface AgentCallbacks {
onThinking: (text: string) => void;
onContent: (text: string) => void;
@@ -77,7 +83,9 @@ export async function runAgentLoop(
}
if (systemPromptParts.length > 0) {
messages.push({ role: 'system', content: systemPromptParts.join('\n\n') });
messages.push({ role: 'system', content: systemPromptParts.join('\n\n') + '\n\n' + TOOL_USAGE_GUIDE });
} else {
messages.push({ role: 'system', content: TOOL_USAGE_GUIDE });
}
for (const msg of historyMessages) {