执行单个shell命令 / Execute single shell command Args: command: 要执行的命令 / Command to execute working_directory: 工作目录 / Working directory Returns: 执行结果 / Execution result
(
command: str, working_directory: str
)
| 191 | |
| 192 | |
| 193 | async def execute_single_command( |
| 194 | command: str, working_directory: str |
| 195 | ) -> list[types.TextContent]: |
| 196 | """ |
| 197 | 执行单个shell命令 / Execute single shell command |
| 198 | |
| 199 | Args: |
| 200 | command: 要执行的命令 / Command to execute |
| 201 | working_directory: 工作目录 / Working directory |
| 202 | |
| 203 | Returns: |
| 204 | 执行结果 / Execution result |
| 205 | """ |
| 206 | try: |
| 207 | # 确保工作目录存在 / Ensure working directory exists |
| 208 | Path(working_directory).mkdir(parents=True, exist_ok=True) |
| 209 | |
| 210 | # 执行命令 / Execute command |
| 211 | result = subprocess.run( |
| 212 | command, |
| 213 | shell=True, |
| 214 | cwd=working_directory, |
| 215 | capture_output=True, |
| 216 | text=True, |
| 217 | timeout=30, |
| 218 | ) |
| 219 | |
| 220 | # 格式化输出 / Format output |
| 221 | output = format_single_command_result(command, working_directory, result) |
| 222 | |
| 223 | return [types.TextContent(type="text", text=output)] |
| 224 | |
| 225 | except subprocess.TimeoutExpired: |
| 226 | return [ |
| 227 | types.TextContent( |
| 228 | type="text", text=f"⏱️ 命令超时 / Command timeout: {command}" |
| 229 | ) |
| 230 | ] |
| 231 | except Exception as e: |
| 232 | return [ |
| 233 | types.TextContent( |
| 234 | type="text", text=f"💥 命令执行错误 / Command execution error: {str(e)}" |
| 235 | ) |
| 236 | ] |
| 237 | |
| 238 | |
| 239 | def generate_execution_summary( |
no test coverage detected