From 0270e17a76101fb80ae98d83b9e982dab5fd1cd9 Mon Sep 17 00:00:00 2001 From: JianFeeeee <109188060+JianFeeeee@users.noreply.github.com> Date: Thu, 16 Apr 2026 23:27:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20context=5Frewrite?= =?UTF-8?q?=20=E5=B7=A5=E5=85=B7=E8=B0=83=E7=94=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复 context_rewrite 导致的 API 错误 - 删除错误的 tool 结果消息添加逻辑 - 添加 context_compressed 标记让 AI 识别压缩结果 - 将 context_rewrite 记录到 tool_calls 以便 TUI 显示 2. 优化系统提示词 - 添加强制触发条件:每5次记忆工具调用必须调用 context_rewrite - 强化指代词处理规则:必须通过工作记忆链解析 - 扩展时间指代词:添加刚刚 - 更新执行检查清单 Generated with CodeArts Agent --- core/prompts/templates/system_prompt.md | 47 ++++++++++++++++++++++--- core/server.py | 20 ++++++----- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/core/prompts/templates/system_prompt.md b/core/prompts/templates/system_prompt.md index ea6d59e..719d6e8 100644 --- a/core/prompts/templates/system_prompt.md +++ b/core/prompts/templates/system_prompt.md @@ -107,6 +107,34 @@ ## context_rewrite 使用规则 +### ⚠️ 强制触发条件 + +**每调用 5 次记忆相关工具,必须调用一次 context_rewrite!** + +记忆相关工具包括: +- `memory_recall` - 检索记忆 +- `memory_commit` - 写入记忆 +- `memory_purge` - 删除记忆 +- `memory_introspect` - 查看状态 +- `persona_update` - 更新人设 +- `persona_clear` - 清除人设 +- `task_create` - 创建任务 +- `task_set_state` - 设置状态 +- `task_delete` - 删除任务 +- `task_link_info` - 关联信息 + +**触发规则**: +- 累计调用 5 次记忆工具 → 必须调用 context_rewrite +- 累计调用 10 次记忆工具 → 必须调用 context_rewrite +- 以此类推... + +**目的**: +- 保持上下文精简,只保留AI真正需要的信息 +- 避免无用的JSON细节填满上下文 +- 提高后续推理效率 + +### 使用场景 + 当你已经执行了多次工具调用,且: - 工具结果的JSON细节你已经理解,不再需要原始格式 - 但你需要记住"我调用了哪些工具、得到了什么结论" @@ -230,20 +258,27 @@ AI操作: - 查询意图: "TaskNode,工作记忆,任务链" - 目的: 获取之前的任务上下文,了解对话历史 -2. **用户提到"刚才"、"之前"、"上次"** +2. **用户提到"刚才"、"之前"、"上次"、"刚刚"** - 例: "刚才我们聊了什么?" - 例: "继续刚才的话题" - 例: "关于刚才的成语接龙..." + - 例: "我不是刚刚给你讲了个故事嘛" -3. **用户询问对话历史** +3. **用户使用指代词(这个故事、那个故事、这件事等)** + - 例: "你给我整体讲一下这个故事吧" → 必须查询工作记忆链确定"这个故事"指什么 + - 例: "继续那个任务" → 必须查询工作记忆链确定"那个任务"是什么 + - 例: "复述一下" → 必须查询工作记忆链确定要复述什么 + - **关键**: 指代词必须通过工作记忆链解析,不能凭空猜测! + +4. **用户询问对话历史** - 例: "我们之前说了什么?" - 例: "我们聊过X吗?" -4. **连续性任务被打断后恢复** +5. **连续性任务被打断后恢复** - 例: 用户突然回到之前的话题 - 例: 用户要求继续之前的任务 -5. **涉及上下文的引用** +6. **涉及上下文的引用** - 例: "那个东西"(需要查询上下文) - 例: "继续"(需要查询当前任务) @@ -374,7 +409,9 @@ AI操作步骤: - [ ] 步骤3: 是否根据人设和工作记忆链生成回复? - [ ] 步骤4: 是否更新了工作记忆链? - [ ] 涉及上下文引用时是否查询了工作记忆链? -- [ ] 用户提到"刚才/之前/上次"时是否查询了工作记忆链? +- [ ] 用户提到"刚才/之前/上次/刚刚"时是否查询了工作记忆链? +- [ ] 用户使用指代词(这个故事、那个任务等)时是否通过工作记忆链解析? +- [ ] 累计调用5次记忆工具后是否调用了 context_rewrite? --- diff --git a/core/server.py b/core/server.py index c0024d9..b0c8037 100644 --- a/core/server.py +++ b/core/server.py @@ -243,19 +243,23 @@ class BackendServer: result = execute_tool(self._graph, tool_call.function.name, args) result_data = json.loads(result) + # 记录到 tool_calls,让 TUI 显示这个工具调用 + tool_calls.append({ + "name": tool_call.function.name, + "arguments": args, + "result": result + }) + if result_data.get("status") == "success": user_msg = messages_history[0] + # 添加特殊标记,让 AI 知道这是上下文压缩的结果 + compressed_content = f"\n{result_data['summary']}\n" messages_history[:] = [ user_msg, - {"role": "assistant", "content": result_data["summary"]} + {"role": "assistant", "content": compressed_content} ] - - tool_result_msg = { - "role": "tool", - "tool_call_id": tool_call.id, - "content": result - } - current_tool_results.append(tool_result_msg) + # context_rewrite 压缩上下文后,不需要添加 tool 结果消息 + # 因为 messages_history 已经被重写为压缩后的状态 continue result = execute_tool(self._graph, tool_call.function.name, args)