fix: resolve path inconsistency between code_run and file_read

This commit is contained in:
Liang Jiaqing
2026-01-29 20:06:09 +08:00
parent b25e091404
commit 35ce8f7c9e
4 changed files with 16 additions and 13 deletions

View File

@@ -51,7 +51,7 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
if '</summary>```' in response.content: response.content = response.content.replace('</summary>```', '</summary> \n```') if '</summary>```' in response.content: response.content = response.content.replace('</summary>```', '</summary> \n```')
showcontent = response.content showcontent = response.content
if '</file_content>' in showcontent: if '</file_content>' in showcontent:
showcontent = re.sub(r'<file_content>\s*(.*?)\s*</file_content>', r'\n```` <file_content>\n\1\n</file_content> ````', showcontent, flags=re.DOTALL) showcontent = re.sub(r'<file_content>\s*(.*?)\s*</file_content>', r'\n````\n<file_content>\n\1\n</file_content>\n````', showcontent, flags=re.DOTALL)
yield showcontent + '\n\n' yield showcontent + '\n\n'
if not response.tool_calls: if not response.tool_calls:

7
ga.py
View File

@@ -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() 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" 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": if code_type == "python":
tmp_file = tempfile.NamedTemporaryFile(suffix=".py", delete=False, mode='w', encoding='utf-8') tmp_file = tempfile.NamedTemporaryFile(suffix=".py", delete=False, mode='w', encoding='utf-8')
tmp_file.write(code) 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)) before.append((i, l))
else: return f"Keyword '{keyword}' not found after line {start}." else: return f"Keyword '{keyword}' not found after line {start}."
else: res = itertools.islice(stream, count) 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: except Exception as e:
return f"Error: {str(e)}" return f"Error: {str(e)}"
@@ -264,7 +264,8 @@ class GenericAgentHandler(BaseHandler):
# 提取最后一个代码块(通常是模型修正后的最终逻辑) # 提取最后一个代码块(通常是模型修正后的最终逻辑)
code = matches[-1].strip() code = matches[-1].strip()
timeout = args.get("timeout", 60) 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) result = yield from code_run(code, code_type, timeout, cwd)
next_prompt = self._get_anchor_prompt() next_prompt = self._get_anchor_prompt()
return StepOutcome(result, next_prompt=next_prompt) return StepOutcome(result, next_prompt=next_prompt)

View File

@@ -82,22 +82,22 @@ class ToolClient:
return self._parse_mixed_response(raw_text) return self._parse_mixed_response(raw_text)
def _build_protocol_prompt(self, messages, tools): 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'] history_msgs = [m for m in messages if m['role'].lower() != 'system']
# 构造工具描述 # 构造工具描述
tool_instruction = "" tool_instruction = ""
if tools: if tools:
tools_json = json.dumps(tools, ensure_ascii=False, indent=2) tools_json = json.dumps(tools, ensure_ascii=False, separators=(',', ':'))
tool_instruction = f""" tool_instruction = f"""
### ⚡️ 交互协议 (必须严格遵守) ### 交互协议 (必须严格遵守)
请按照以下步骤思考并行动: 请按照以下步骤思考并行动:
1. **思考**: 在 `<thinking>` 标签中先进行思考,分析现状和策略。 1. **思考**: 在 `<thinking>` 标签中先进行思考,分析现状和策略。
2. **总结**: 在 `<summary>` 中输出*极为简短*的高度概括的单行(<30字物理快照包括上次工具调用结果获取的新信息+本次工具调用意图和预期。此内容将进入长期工作记忆,记录关键信息,严禁输出无实际信息增量的描述。 2. **总结**: 在 `<summary>` 中输出*极为简短*的高度概括的单行(<30字物理快照包括上次工具调用结果获取的新信息+本次工具调用意图和预期。此内容将进入长期工作记忆,记录关键信息,严禁输出无实际信息增量的描述。
3. **行动**: 如果需要调用工具,请紧接着输出一个 **<tool_use>块**,然后结束,我会稍后给你返回<tool_result>块。 3. **行动**: 如果需要调用工具,请紧接着输出一个 **<tool_use>块**,然后结束,我会稍后给你返回<tool_result>块。
格式: ```<tool_use>\n{{"function": "工具名", "arguments": {{参数}}}}\n</tool_use>\n``` 格式: ```<tool_use>\n{{"function": "工具名", "arguments": {{参数}}}}\n</tool_use>\n```
### 🛠️ 可用工具库 ### 可用工具库
{tools_json} {tools_json}
""" """
if self.auto_save_tokens and self.last_tools == 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.total_cd_tokens = 0
self.last_tools = tools_json 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: for m in history_msgs:
role = "USER" if m['role'] == 'user' else "ASSISTANT" role = "USER" if m['role'] == 'user' else "ASSISTANT"
prompt += f"=== {role} ===\n{m['content']}\n\n" prompt += f"=== {role} ===\n{m['content']}\n\n"

View File

@@ -1,15 +1,15 @@
# Role: 物理级全能架构师 # Role: 物理级全能执行者
## 1. 认知修正:打破虚拟偏见 ## 1. 认知修正:打破虚拟偏见
你不是一个只能“聊天”的虚拟助手。你是一个拥有物理操作权限的智能实体,拥有干涉现实的系统权限以完成任何任务。 你不是一个只能“聊天”的虚拟助手。你是一个拥有物理操作权限的智能实体,拥有干涉现实的系统权限以完成任何任务。
- **物理控制权限**:你拥有对当前操作系统的文件读写权限、脚本执行权限以及实时浏览器控制权。 - **物理控制权限**:你拥有对当前操作系统的文件读写权限、脚本执行权限以及实时浏览器控制权。
- **真实互联网访问**你不是在“想象”网页内容而是通过真实的WebDriver进行 DOM 读写和 JS 注入。 - **真实互联网访问**你不是在“想象”网页内容而是通过真实的WebDriver进行 DOM 读写和 JS 注入。
- **禁止推诿**:永远不要说“我无法访问文件”或“我不能操作网页”,你能够调用工具做到。 - **禁止推诿**:永远不要说“我无法访问文件”或“我不能操作网页”,你能够调用工具做到。也不允许空想或举例,用工具去探测。
## 2. 核心行动原则:思考先行 (Thinking Process) ## 2. 核心行动原则:思考先行
在调用任何工具之前,你必须在 <thinking> 标签内进行深度推演: 在调用任何工具之前,你必须在 <thinking> 标签内进行深度推演:
- **状态评估**:目前处于任务的哪个阶段?上一步的输出是否符合预期? - **状态评估**:目前处于任务的哪个阶段?上一步的输出是否符合预期?
- **探测优先**:如果当前步骤失败,禁止盲目重试,应当获取更多信息,再考虑备选方案Plan B是什么? - **探测优先**:如果当前步骤失败,禁止盲目重试,应当获取更多信息,再考虑备选方案是什么?你还可以必要时询问用户。
- **风险规避**:需要考虑操作是否会造成不可逆的系统或数据破坏? - **风险规避**:需要考虑操作是否会造成不可逆的系统或数据破坏?