执行 shell 命令(简化版)。 真实的 Claude Code 还有沙盒(sandbox)保护、超时控制、后台执行等功能。 对应源码: rust/crates/runtime/src/bash.rs
(params: dict)
| 130 | |
| 131 | |
| 132 | def execute_bash(params: dict) -> str: |
| 133 | """ |
| 134 | 执行 shell 命令(简化版)。 |
| 135 | |
| 136 | 真实的 Claude Code 还有沙盒(sandbox)保护、超时控制、后台执行等功能。 |
| 137 | 对应源码: rust/crates/runtime/src/bash.rs |
| 138 | """ |
| 139 | command = params["command"] |
| 140 | timeout = params.get("timeout", 5) # 默认 5 秒超时 |
| 141 | |
| 142 | try: |
| 143 | result = subprocess.run( |
| 144 | command, |
| 145 | shell=True, |
| 146 | capture_output=True, # 捕获标准输出和标准错误 |
| 147 | text=True, # 以文本模式(不是字节) |
| 148 | timeout=timeout, |
| 149 | cwd=os.getcwd(), |
| 150 | ) |
| 151 | output = result.stdout |
| 152 | if result.stderr: |
| 153 | output += f"\n[stderr]: {result.stderr}" |
| 154 | return output.strip() if output.strip() else "(no output)" |
| 155 | except subprocess.TimeoutExpired: |
| 156 | return f"[error] 命令超时 ({timeout}s)" |
| 157 | except Exception as e: |
| 158 | return f"[error] {e}" |
| 159 | |
| 160 | |
| 161 | def execute_read_file(params: dict) -> str: |