From 98c9e51dc202369332fa3c182c047ada439beda8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=89=BE=E8=BD=AE=20=E5=8D=A7=E5=A3=B3?= Date: Sun, 26 Apr 2026 23:34:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E8=AF=8D=E4=B8=AD=E7=9A=84'=E6=8F=92=E4=BB=B6'=E6=9C=AF?= =?UTF-8?q?=E8=AF=AD=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=B8=BA'=E5=B7=A5?= =?UTF-8?q?=E5=85=B7'=EF=BC=8C=E5=BC=BA=E5=88=B6=20JSON=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E8=B0=83=E7=94=A8=E9=98=B2=E6=AD=A2=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E6=8B=BC=E6=8E=A5=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/code_editor.py | 4 ++-- plugins/tool_search.py | 12 ++++++------ xcli_core/cli_core.py | 35 ++++++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/plugins/code_editor.py b/plugins/code_editor.py index 0ba87dd..48b779e 100644 --- a/plugins/code_editor.py +++ b/plugins/code_editor.py @@ -1,5 +1,5 @@ """ -代码工具插件 - 统一的代码编辑与搜索能力 +代码工具 - 统一的代码编辑与搜索能力 合并原 code_editor + code_search,消除重复 """ import os @@ -49,7 +49,7 @@ class Plugin: def get_tool_info(self): return { "name": "code_editor", - "description": "代码编辑与搜索 - 精准替换、批量编辑、代码搜索、符号提取、依赖分析、diff对比", + "description": "代码编辑与搜索 — 精准替换、批量编辑、代码搜索、符号提取、依赖分析、diff对比", "keywords": ["编辑", "修改", "代码", "文件", "edit", "replace", "search", "diff", "find", "grep", "符号", "structure", "imports", "todo", "callers"], "usage": self.usage diff --git a/plugins/tool_search.py b/plugins/tool_search.py index b1fb5e4..a576b1b 100644 --- a/plugins/tool_search.py +++ b/plugins/tool_search.py @@ -1,11 +1,11 @@ """ -工具搜索插件 - 支持按需发现工具 +工具搜索助手 - 支持按需发现工具 """ import json class Liugin: - """工具搜索插件 - 允许 AI 按需搜索和发现可用工具""" + """工具搜索助手 - 允许 AI 按需搜索和发现可用工具""" def __init__(self): self.usage = """工具搜索使用方法: @@ -16,7 +16,7 @@ tool_search <关键词> - 根据关键词搜索可用工具 - tool_search 网络 - 搜索与网络相关的工具 - tool_search 图片 - 搜索与图片相关的工具 -此插件帮助 AI 在需要特定功能时快速找到合适的工具, +此工具帮助 AI 在需要特定功能时快速找到合适的工具, 而不是预加载所有工具到上下文中。""" self.cli = None @@ -27,7 +27,7 @@ tool_search <关键词> - 根据关键词搜索可用工具 def get_tool_info(self): return { "name": "tool_search", - "description": "工具搜索插件,支持按需发现和搜索可用工具。当你不确定有哪些工具可用时,可以使用此工具搜索相关功能的工具。", + "description": "工具搜索助手,支持按需发现和搜索可用工具。当你不确定有哪些工具可用时,可以使用此工具搜索相关功能的工具。", "keywords": ["搜索", "工具", "发现", "search", "tool", "find", "可用工具", "工具列表"], "usage": """tool_search 工具使用说明: JSON格式示例: @@ -45,7 +45,7 @@ JSON格式示例: def get_mcp_definition(self): return { "name": "tool_search", - "description": "工具搜索插件,搜索可用工具的用法和信息", + "description": "工具搜索助手,搜索可用工具的用法和信息", "inputSchema": { "type": "object", "properties": { @@ -120,7 +120,7 @@ JSON格式示例: return "\n".join(result_lines) else: - return "错误:无法访问插件管理器。" + return "错误:无法访问工具管理器。" except Exception as e: return f"工具搜索失败: {str(e)}" \ No newline at end of file diff --git a/xcli_core/cli_core.py b/xcli_core/cli_core.py index 631ff46..2045eba 100644 --- a/xcli_core/cli_core.py +++ b/xcli_core/cli_core.py @@ -100,11 +100,21 @@ class AICLI(BaseAICLI, ClawliMixin, ToolMixin, CodeExecMixin, DisplayMixin, Hist 在调用任何工具之前,你必须先使用 tool_search 查询该工具的详细用法、参数格式和示例。 这是硬性要求,没有例外。即使你认为自己知道工具的用法,也必须先查询确认。 +【⚠️ 工具调用格式 — 必须使用 JSON】 +所有工具都必须通过 JSON 指令调用,格式如下: +{{"action": "use_tool", "tool": "工具名", "args": "操作 参数"}} + +禁止直接写 "工具名 操作 参数",必须包在 JSON 里。 +例如: + ✅ 正确: {{"action": "use_tool", "tool": "code_editor", "args": "find . keyword *.py"}} + ❌ 错误: code_editor find . keyword *.py + ❌ 错误: code_editor("find . keyword *.py") + 执行流程: 第一步:用 tool_search 查询工具用法 -{"action": "use_tool", "tool": "tool_search", "args": "你要用的工具名"} +{{"action": "use_tool", "tool": "tool_search", "args": "你要用的工具名"}} 第二步:阅读 tool_search 返回的详细说明(包括参数格式、操作列表、示例) -第三步:根据返回的说明,正确构造工具调用指令 +第三步:根据返回的说明,用 JSON 格式正确调用工具 为什么要这样做: - 工具的参数格式可能已更新,你的记忆可能过时 @@ -116,17 +126,20 @@ class AICLI(BaseAICLI, ClawliMixin, ToolMixin, CodeExecMixin, DisplayMixin, Hist 用户: "帮我修复 src/app.py 第 10 行的 bug" 你的思考过程: 1. 需要先查看代码 → 先查 code_editor 怎么读取文件 -2. tool_search "code_editor" → 发现 read_range 操作 -3. 调用 code_editor read_range src/app.py 5 15 → 拿到代码 +2. {{"action": "use_tool", "tool": "tool_search", "args": "code_editor"}} → 发现 read_range 操作 +3. {{"action": "use_tool", "tool": "code_editor", "args": "read_range src/app.py 5 15"}} → 拿到代码 4. 需要修改代码 → 已经在第一步查过 code_editor,知道 edit 格式 -5. 调用 code_editor edit src/app.py 旧代码 <<<>>> 新代码 +5. {{"action": "use_tool", "tool": "code_editor", "args": "edit src/app.py +旧代码 +<<<>>> +新代码"}} 示例流程(Git 操作): 用户: "帮我提交代码" 你的思考过程: 1. 需要 git 操作 → 先查 git_tools 怎么用 -2. tool_search "git_tools" → 发现 smart-commit 操作 -3. 调用 git_tools smart-commit → 自动提交""" +2. {{"action": "use_tool", "tool": "tool_search", "args": "git_tools"}} → 发现 smart-commit 操作 +3. {{"action": "use_tool", "tool": "git_tools", "args": "smart-commit"}} → 自动提交""" if liugin_prompts: prompt = f"""{base_prompt} @@ -226,9 +239,9 @@ multi 操作支持一次修改多处: return response.strip() def get_liugin_usage_prompts(self): - """获取插件和技能提示词""" + """获取可用工具提示词""" if not self.liugin_manager.tools: - return "当前没有可用的工具插件." + return "当前没有可用的工具." prompts = [] prompts.append("可用的工具列表:\n") @@ -243,14 +256,14 @@ multi 操作支持一次修改多处: plugins.append(tool) if plugins: - prompts.append("## Plugin 工具(使用 tool_search 查询详细用法):") + prompts.append("## 内置工具(使用 tool_search 查询详细用法):") for tool in plugins: tool_name = tool.get('name', '') tool_desc = tool.get('description', '') prompts.append(f"- {tool_name}: {tool_desc}") if skills: - prompts.append("\n## Agent Skills(已加载详细指令):") + prompts.append("\n## 扩展技能(已加载详细指令):") for tool in skills: tool_name = tool.get('name', '') tool_desc = tool.get('description', '')