From 35ce8f7c9e392ab3533679bcce95c46cd73c27e2 Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Thu, 29 Jan 2026 20:06:09 +0800 Subject: [PATCH] fix: resolve path inconsistency between code_run and file_read --- agent_loop.py | 2 +- ga.py | 7 ++++--- sidercall.py | 12 +++++++----- sys_prompt.txt | 8 ++++---- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/agent_loop.py b/agent_loop.py index f4e8ec6..f1d2c38 100644 --- a/agent_loop.py +++ b/agent_loop.py @@ -51,7 +51,7 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, if '```' in response.content: response.content = response.content.replace('```', ' \n```') showcontent = response.content if '' in showcontent: - showcontent = re.sub(r'\s*(.*?)\s*', r'\n```` \n\1\n ````', showcontent, flags=re.DOTALL) + showcontent = re.sub(r'\s*(.*?)\s*', r'\n````\n\n\1\n\n````', showcontent, flags=re.DOTALL) yield showcontent + '\n\n' if not response.tool_calls: diff --git a/ga.py b/ga.py index 0a8bbb5..eb44228 100644 --- a/ga.py +++ b/ga.py @@ -16,7 +16,7 @@ def code_run(code: str, code_type: str = "python", timeout: int = 60, cwd: str = """ preview = (code[:60].replace('\n', ' ') + '...') if len(code) > 60 else code.strip() yield f"[Action] Running {code_type} in {os.path.basename(cwd)}: {preview}\n" - cwd = cwd or os.getcwd(); tmp_path = None + cwd = cwd or os.path.join(os.getcwd(), 'temp'); tmp_path = None if code_type == "python": tmp_file = tempfile.NamedTemporaryFile(suffix=".py", delete=False, mode='w', encoding='utf-8') tmp_file.write(code) @@ -211,7 +211,7 @@ def file_read(path, start=1, keyword=None, count=100, show_linenos=True): before.append((i, l)) else: return f"Keyword '{keyword}' not found after line {start}." else: res = itertools.islice(stream, count) - return "\n".join(f"{i}|{l}" if show_linenos else l for i, l in res) + return "\n".join(f"{i}| {l}" if show_linenos else l for i, l in res) except Exception as e: return f"Error: {str(e)}" @@ -264,7 +264,8 @@ class GenericAgentHandler(BaseHandler): # 提取最后一个代码块(通常是模型修正后的最终逻辑) code = matches[-1].strip() timeout = args.get("timeout", 60) - cwd = args.get("cwd", self.cwd) + raw_path = os.path.join(self.cwd, args.get("cwd", './')) + cwd = os.path.normpath(os.path.abspath(raw_path)) result = yield from code_run(code, code_type, timeout, cwd) next_prompt = self._get_anchor_prompt() return StepOutcome(result, next_prompt=next_prompt) diff --git a/sidercall.py b/sidercall.py index ecd50ef..5592c14 100644 --- a/sidercall.py +++ b/sidercall.py @@ -82,22 +82,22 @@ class ToolClient: return self._parse_mixed_response(raw_text) def _build_protocol_prompt(self, messages, tools): - system_content = next((m['content'] for m in messages if m['role'].lower() == 'system'), "你是一个智能助手。") + system_content = next((m['content'] for m in messages if m['role'].lower() == 'system'), "") history_msgs = [m for m in messages if m['role'].lower() != 'system'] # 构造工具描述 tool_instruction = "" if tools: - tools_json = json.dumps(tools, ensure_ascii=False, indent=2) + tools_json = json.dumps(tools, ensure_ascii=False, separators=(',', ':')) tool_instruction = f""" -### ⚡️ 交互协议 (必须严格遵守) +### 交互协议 (必须严格遵守) 请按照以下步骤思考并行动: 1. **思考**: 在 `` 标签中先进行思考,分析现状和策略。 2. **总结**: 在 `` 中输出*极为简短*的高度概括的单行(<30字)物理快照,包括上次工具调用结果获取的新信息+本次工具调用意图和预期。此内容将进入长期工作记忆,记录关键信息,严禁输出无实际信息增量的描述。 3. **行动**: 如果需要调用工具,请紧接着输出一个 **块**,然后结束,我会稍后给你返回块。 格式: ```\n{{"function": "工具名", "arguments": {{参数}}}}\n\n``` -### 🛠️ 可用工具库 +### 可用工具库 {tools_json} """ if self.auto_save_tokens and self.last_tools == tools_json: @@ -106,7 +106,9 @@ class ToolClient: self.total_cd_tokens = 0 self.last_tools = tools_json - prompt = f"=== SYSTEM ===\n{system_content}\n{tool_instruction}\n\n" + prompt = "" + if system_content: prompt += f"=== SYSTEM ===\n{system_content}\n" + prompt += f"{tool_instruction}\n\n" for m in history_msgs: role = "USER" if m['role'] == 'user' else "ASSISTANT" prompt += f"=== {role} ===\n{m['content']}\n\n" diff --git a/sys_prompt.txt b/sys_prompt.txt index 3963f42..32d5e1d 100644 --- a/sys_prompt.txt +++ b/sys_prompt.txt @@ -1,15 +1,15 @@ -# Role: 物理级全能架构师 +# Role: 物理级全能执行者 ## 1. 认知修正:打破虚拟偏见 你不是一个只能“聊天”的虚拟助手。你是一个拥有物理操作权限的智能实体,拥有干涉现实的系统权限以完成任何任务。 - **物理控制权限**:你拥有对当前操作系统的文件读写权限、脚本执行权限以及实时浏览器控制权。 - **真实互联网访问**:你不是在“想象”网页内容,而是通过真实的WebDriver进行 DOM 读写和 JS 注入。 -- **禁止推诿**:永远不要说“我无法访问文件”或“我不能操作网页”,你能够调用工具做到。 +- **禁止推诿**:永远不要说“我无法访问文件”或“我不能操作网页”,你能够调用工具做到。也不允许空想或举例,用工具去探测。 -## 2. 核心行动原则:思考先行 (Thinking Process) +## 2. 核心行动原则:思考先行 在调用任何工具之前,你必须在 标签内进行深度推演: - **状态评估**:目前处于任务的哪个阶段?上一步的输出是否符合预期? -- **探测优先**:如果当前步骤失败,禁止盲目重试,应当获取更多信息,再考虑备选方案(Plan B)是什么? +- **探测优先**:如果当前步骤失败,禁止盲目重试,应当获取更多信息,再考虑备选方案是什么?你还可以必要时询问用户。 - **风险规避**:需要考虑操作是否会造成不可逆的系统或数据破坏?