From c8cafbe58e1833c64412d6af477f792d695f9d8d Mon Sep 17 00:00:00 2001 From: thzxx Date: Wed, 10 Jun 2026 14:48:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DAgent=20Loop=E8=BF=87?= =?UTF-8?q?=E6=97=A9=E5=81=9C=E6=AD=A2=20=E2=80=94=20=E6=97=A0tool=5Fcalls?= =?UTF-8?q?=E6=97=B6=E6=A3=80=E6=B5=8B=E6=98=AF=E5=90=A6=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=9C=AA=E5=AE=8C=E6=88=90+=E6=B3=A8=E5=85=A5=E7=BB=A7?= =?UTF-8?q?=E7=BB=AD=E6=8F=90=E7=A4=BA+AGENT.md=E5=8A=A0=E5=BC=BA=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/public/AGENT.md | 11 ++++++----- src/renderer/services/agent-engine.ts | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/renderer/public/AGENT.md b/src/renderer/public/AGENT.md index b359772..6026cc7 100644 --- a/src/renderer/public/AGENT.md +++ b/src/renderer/public/AGENT.md @@ -5,11 +5,12 @@ ## 核心规则 1. **直接调用工具**:不要只说"我来帮你xxx"然后结束。说了要做就必须调用工具。 -2. **多步任务逐步完成**:搜索→抓取→回答。抓取失败时直接用搜索结果,不要卡住。 -3. **需要最新信息时先搜索**:版本号、新闻、日期等必须用工具获取。**【严禁使用知识库日期】**——你不是活在一个特定日期,以搜索结果为唯一可信来源。 -4. **出错换方法重试**:最多 2 次,同一 URL 连续失败后换其他途径。 -5. **不重复调用**:相同参数的同一工具不重复执行。 -6. **善用搜索结果**:snippet 已有关键信息,优先从中提取答案。 +2. **不要过早停止**:工具调用后如果信息还不完整,必须继续调用工具或给出下一步计划。不要在2-3轮后就给出不完整的回答。如果任务未完成,不要说"我已经完成了"。 +3. **多步任务逐步完成**:搜索→抓取→回答。抓取失败时直接用搜索结果,不要卡住。 +4. **需要最新信息时先搜索**:版本号、新闻、日期等必须用工具获取。**【严禁使用知识库日期】**——你不是活在一个特定日期,以搜索结果为唯一可信来源。 +5. **出错换方法重试**:最多 2 次,同一 URL 连续失败后换其他途径。 +6. **不重复调用**:相同参数的同一工具不重复执行。 +7. **善用搜索结果**:snippet 已有关键信息,优先从中提取答案。 ## 链式调用模式 diff --git a/src/renderer/services/agent-engine.ts b/src/renderer/services/agent-engine.ts index 59a2719..c81cd7d 100644 --- a/src/renderer/services/agent-engine.ts +++ b/src/renderer/services/agent-engine.ts @@ -835,7 +835,25 @@ Shell: ${osInfo.shell} } if (toolCalls.length === 0) { - // 真正的最终回答(有内容或首次循环就无工具) + // ── 反过早结束:如果之前有工具调用,但模型返回的内容不像最终回答,引导继续 ── + if (allToolRecords.length > 0 && !isFinalAnswer && loopCount < maxLoops - 1) { + const contentLower = content.toLowerCase(); + const isThinking = contentLower.includes('让我') || contentLower.includes('看看') || + contentLower.includes('观察') || contentLower.includes('分析') || contentLower.includes('检查'); + const isContinuation = content.length < 300 && (isThinking || contentLower.includes('接下来') || contentLower.includes('继续')); + + if (isContinuation || content.length < 100) { + logWarn('模型可能过早停止,注入继续提示', `${content.length}字`); + messages.pop(); // 移除空/过渡性的 assistant 消息 + messages.push({ + role: 'user', + content: '请继续完成任务。你可以调用工具获取更多信息,或者根据已有结果给出完整的最终回答。如果任务已完成,请明确说出"任务完成"或"最终回答"。' + }); + continue; + } + } + + // 真正的最终回答 logInfo('无工具调用,ReAct Agent Loop 结束'); // 自动提取记忆(仅在对话结束后)