
如何用 CAMEL TerminalToolkit 以后台会话方式运行长时命令并管理输出与终止【免费下载链接】camel CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org项目地址: https://gitcode.com/GitHub_Trending/ca/camel当你的 CAMEL Agent 需要执行一个长时间运行的命令数据任务、服务进程、交互式 Python REPL时默认阻塞式等待会把调用卡住甚至超时。TerminalToolkit提供了后台会话机制用shell_exec以blockFalse启动命令或阻塞命令超时后自动转为后台会话之后用同一个会话id通过shell_view增量取输出、shell_write_to_process写输入、shell_kill_process终止进程。本文基于 Terminal Toolkit 模块文档 和 示例脚本走通启动后台会话 → 管理输出 → 终止这条完整路径。准备条件Python 版本需满足 CAMEL 的安装要求Python 3.10 and 3.14见 安装文档。安装 CAMELpip install camel-aiTerminalToolkit位于camel.toolkits下。本地后端默认safe_modeTrueworking_directory默认是./workspace所有写文件和执行命令都限制在该沙箱目录内危险命令如rm默认会被拦截。本地后端初始化时会尝试在 workspace 内创建隔离的 Python 环境优先用uv不可用时回退到标准venv。如果你希望克隆当前环境可传clone_current_envTrue需要预装依赖时传install_dependencies[...]。初始化 Toolkit 与关键参数from camel.toolkits import TerminalToolkit # 默认safe_mode ONworking_directory 为 ./workspace terminal_toolkit TerminalToolkit() # 或指定沙箱目录、默认阻塞超时单位秒默认 20.0 terminal_toolkit TerminalToolkit( working_directory./my_safe_workspace, timeout20.0, )参数要点依据 terminal_toolkit.py 的参数定义timeout阻塞命令的默认超时秒数默认20.0。阻塞命令超过该时长不会失败而是自动转成后台会话继续运行。working_directory本地后端下作为安全沙箱Docker 后端下是容器内工作目录。session_logs_dir会话日志目录默认是 workspace 下的terminal_logs子目录。allowed_commands配合safe_modeTrue时可把执行范围限制在显式白名单内例如allowed_commands[ls, cat, python, pip, uv]适合只允许特定命令的生产部署。启动后台会话blockFalse 与超时自动转换shell_exec是执行命令的入口每条命令通过唯一的会话id标识。两种进入后台会话的方式方式一显式非阻塞启动blockFalse适合交互式或长时任务# 启动一个长时进程立即返回不等待完成 terminal_toolkit.shell_exec( idlong_process, commandsleep 100, blockFalse, )非阻塞模式返回的是带会话id的确认信息而不是命令输出源码中返回 Session ... started. 并列出shell_view/shell_write_to_process/shell_kill_process的用法见 shell_exec 非阻塞分支。文档中的交互式 REPL 例子同样走这条路# 在新会话中启动 python REPL terminal_toolkit.shell_exec( idinteractive_session, commandpython, blockFalse, ) # 向进程写入一行代码 terminal_toolkit.shell_write_to_process( idinteractive_session, commandprint(Hello, interactive world!) ) # 查看输出 output terminal_toolkit.shell_view(idinteractive_session) print(output)方式二阻塞执行 超时自动转换。给阻塞命令设置较短的timeout命令未能在时限内完成时进程不会被打断而是自动转为受跟踪的后台会话源码注释process keeps running without restart# 超过 timeout 未完成的命令会被自动转为后台会话 output terminal_toolkit.shell_exec( idlong_task, commandpython long_running_job.py, blockTrue, timeout5, ) print(output)此时shell_exec返回类似 Command did not complete within 5 seconds. Process continues in background as session long_task. 的提示并附上后续可用的shell_view(long_task)和shell_kill_process(long_task)见 超时转换分支。这就是想要快速响应、但又不想杀掉进程的场景的标准用法。注意如果非阻塞启动时同一个id已存在且仍在运行工具会直接拒绝并提示更换 ID 或先终止旧会话Error: Session ... already exists and is running.。增量读取输出shell_view 的语义与状态判断shell_view(id)返回自上次调用以来的新增输出不会等待、不阻塞立即返回当前可用内容。因此文档建议周期性调用它来流式取日志# 隔一段时间调用一次取新增输出 print(terminal_toolkit.shell_view(idlong_task))根据源码返回值有几种形态可直接用于判断下一步会话仍在运行且有新输出返回新增的 stdout/stderr 文本会话在运行但没有新输出返回 [No new output] 提示并建议交互式会话用shell_write_to_process()发输入长任务则稍后再查dont poll too frequently会话已终止返回剩余输出加--- SESSION TERMINATED ---无剩余输出时为--- SESSION TERMINATED (no new output) ---id不存在返回 Error: No session found with ID ....说明会话未启动或id写错。判断任务是否结束的依据就是这些状态标记不需要额外查进程。向运行中的进程写输入对交互式进程如 REPL用shell_write_to_process(id, command)向标准输入写入文本输入末尾会自动追加换行符并返回进程重新空闲后的输出terminal_toolkit.shell_write_to_process( idinteractive_session, commandprint(Hello, interactive world!) )如果写入后进程没有产生输出返回 Input sent to session ... successfully (no output).如果该id没有处于运行中的非阻塞会话返回 Error: No active non-blocking session found with ID ....。另外文档还提供了shell_ask_user_for_help(id, prompt)当 Agent 卡住时暂停并请求人工在控制台接管该会话人工输入会写入同一进程后把控制权交还属于可选的人机协作分支。终止后台会话shell_kill_process确认任务完成或不再需要进程时用shell_kill_process强制终止# 在进程结束前杀掉它 result terminal_toolkit.shell_kill_process(idlong_process) print(result)成功时返回 Process in session ... has been terminated.id不存在或会话已不在运行时返回 Error: No active session found with ID ....。本地后端的终止逻辑是先发 terminate 信号半秒后仍在运行则升级到 kill见 shell_kill_process 实现。这是不可逆操作执行前请确认该会话对应的命令确实应该被终止。终止后再调用shell_view(id)会看到--- SESSION TERMINATED ---标记这可以作为会话生命周期的收尾验证。会话日志落在哪里每个会话的原始输出会写入session_logs_dir默认working_directory/terminal_logs/下的session_id.log阻塞命令另有一份blocking_commands.log记录执行时间、命令与输出。初始化 Toolkit 时该目录会被创建且目录内已有的.log文件会被清理因此不要把需要长期保留的日志放在该默认目录。需要固定位置时通过session_logs_dir参数显式指定。边界与限制本地后端下safe_mode默认为开危险命令例如工作区外的rm会被拒绝并返回 Error: Command rejected by TerminalToolkit safe mode.cd/pushd等目录操作也不能逃出working_directory无法安全校验的目录变更链同样会被拦。需要更强隔离时可选 Docker 后端TerminalToolkit(use_docker_backendTrue, docker_container_namecamel-runtime, working_directory/workspace)前提是容器已存在且处于运行状态不存在会报 Container ... not found.。超时转换与后台会话机制对本地和 Docker 后端都成立行为一致超时后进程继续运行shell_view/shell_kill_process按同一套接口管理。接入 Agent可选如果目标是让 LLM Agent 自己管理这些命令把get_tools()的返回值交给ChatAgent即可Agent 会自行调用shell_exec、shell_view等工具。完整示例见 examples/toolkits/terminal_toolkit.pyfrom camel.agents import ChatAgent from camel.toolkits import TerminalToolkit tools TerminalToolkit(working_directory./workspace).get_tools() # 之后把 tools 传给 ChatAgent(toolstools)并按 examples 中的方式创建 modelget_tools()暴露的工具包括shell_exec、shell_view、shell_write_content_to_file、shell_write_to_process、shell_kill_process、shell_ask_user_for_help见 get_tools。其中shell_write_content_to_file(content, file_path)适合直接写大文件相对路径按working_directory解析safe mode 下路径必须留在该目录内。下一步查看模块参数与后端选择的完整说明camel/toolkits/terminal_toolkit/terminal_toolkit.py查看带 Agent 与 Docker 后端的端到端示例输出examples/toolkits/terminal_toolkit.py。【免费下载链接】camel CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org项目地址: https://gitcode.com/GitHub_Trending/ca/camel创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考