尧图精选

AgentScope 怎么配置 Apple Container 沙箱工作区运行工具调用?

🕒 发布时间:2026/9/12 13:55:42 📁 来源:尧图网络
AgentScope 怎么配置 Apple Container 沙箱工作区运行工具调用【免费下载链接】agentscopeBuild and run agents you can see, understand and trust.项目地址: https://gitcode.com/GitHub_Trending/ag/agentscope在 Apple silicon 的 Mac 上跑 AgentScope 时你可能希望 Agent 的 Bash、Read、Write、Edit、Grep、Glob 这些工具调用不直接落在宿主机上而是执行在一个隔离的 Linux VM 里。AgentScope 提供的AppleContainerWorkspace就是为此设计的它基于 Apple 官方containerCLI 创建并管理一个 Linux 容器把内置工具调用全部重定向到容器内部容器里的工作目录固定为/workspace。完成本文操作后你会得到一个可运行工具调用、可验证输出、可关闭清理的沙箱工作区。该方案的适用前提比较严格macOS 26 且必须是 Apple siliconIntel Mac 不受 Apple Container 支持且需要安装Apple Container 1.0.0 或更高版本官方在 1.0.0 和 1.1.0 上测试过可从 Apple Container 开发者站点安装。相关文档见 examples/workspace/apple-container-workspace.md核心实现见 AppleContainerWorkspace 与 AppleContainerBackend。准备条件确认 container CLI 与网络创建任何工作区之前必须先确保 Apple Container 的系统服务在运行container system start另外有一个容易忽略的前置条件首次initialize()期间容器 VM 需要能访问外网。引导bootstrap阶段会通过apt-get安装系统包curl、ripgrep并下载安装uv。如果容器 VM 无法联网initialize 会在 bootstrap 步骤以 apt-get 或 curl 错误失败。容器 VM 默认共享宿主机的网络栈。如果你的宿主机使用代理可以先验证容器能否访问外部主机# container-id 替换为你自己的容器标识容器 id 或 name container exec container-id curl -I https://pypi.org文档提示VM 内的 DNS 解析默认应直接可用。如果出现“DNS 能解析但 TCP 连接超时”检查宿主机防火墙是否拦截了来自容器 VM 的流量。支持的镜像任何预装了python3的 Debian/Ubuntu 系 OCI 镜像都可以用默认镜像是python:3.11-slim。官方 Docker library 镜像的短名和全名是等价的# 这两种写法等价 AppleContainerWorkspace(base_imagepython:3.11-slim) AppleContainerWorkspace(base_imagedocker.io/library/python:3.11-slim)如果换一个镜像必须满足两个条件提供python3且基于 Debian/Ubuntubootstrap 依赖apt-get。配置工作区参数从agentscope.workspace导入AppleContainerWorkspace按需传参构造。各参数的默认值如下与 constants 一致参数默认值说明workspace_id自动生成的 UUID稳定标识符同时用作容器名后缀as_ws_idbase_imagepython:3.11-slim要运行的 OCI 镜像。必须提供python3且基于 Debian/Ubuntugateway_port5600容器内 MCP gateway 监听的 TCP 端口cpus2分配给容器的虚拟 CPU 数memory2G内存上限如512M、4Genv{}注入容器内的环境变量extra_pip[]bootstrap 阶段额外安装到 gateway venv 的 pip 包配置示例from agentscope.workspace import AppleContainerWorkspace ws AppleContainerWorkspace( workspace_idmy-workspace, # 可选省略时自动生成 base_imagepython:3.11-slim, # 默认值 gateway_port5600, # 容器内 MCP gateway 的 TCP 端口 cpus2, # 分配给容器的虚拟 CPU memory2G, # 内存上限如 512M、4G env{MY_VAR: value}, # 容器内环境变量 extra_pip[requests], # bootstrap 时装入 gateway venv 的额外包 )构造本身不会启动容器——容器要等initialize()或使用async with时才创建。启动工作区并运行工具调用工作区生命周期用async with管理等价于手动调用initialize()/close()import asyncio from agentscope.workspace import AppleContainerWorkspace async def main(): async with AppleContainerWorkspace() as ws: # 此时容器已创建、bootstrap 完成、gateway 正在运行 backend ws.get_backend() result await backend.exec_shell([echo, hello]) print(result.stdout) # 离开 with 块后容器被停止并移除 asyncio.run(main())initialize()内部依次完成校验containerCLI 可用执行container system version --format json失败会抛出带container system start提示的RuntimeError、按需拉取基础镜像、以container run -d创建并启动容器、在容器内 bootstrap gateway venvapt-get uv pip每个容器生命周期内最多执行一次。进入容器后AppleContainerBackend把内置工具落到三条原语上exec_shell通过container exec在容器内执行程序read_file通过容器内cat读文件write_file通过container cp把宿主机临时文件拷进容器。Bash、Read、Write、Edit、Grep、Glob 六个内置工具就是基于这三条原语绑定到容器后端的默认工作目录是/workspace。验证结果最直接的成功信号是上面的脚本initialize完成后backend.exec_shell([echo, hello])能打印出命令的 stdout文档示例输出为hello。仓库里的测试 tests/workspace_applecontainer_test.py 给出了更完整的核对方式可按场景借用其中的断言思路构造校验不需要真实容器AppleContainerWorkspace()构造后ws.workdir /workspace、ws.base_image python:3.11-slim、ws.cpus 2、ws.memory 2G且初始ws.is_alive为False。生命周期校验需要 macOS container CLIasync with ws:进入后ws.is_alive为True且ws._backend是AppleContainerBackend实例退出后is_alive回到False。工具绑定校验await ws.list_tools()返回的工具名中应包含Bash、Read、Write、Edit、Glob、Grep六个内置工具。完整链路校验live 测试测试中设置了环境开关APPLE_CONTAINER_LIVE1后会验证 bootstrap 安装的rg --version、uv --version、python3 --version均能执行直接调用 Bash 工具跑echo tool_call_ok能拿到输出容器内写入/workspace/e2e_test.txt再读回内容一致。运行真实链路测试前需要自行设置该环境变量对应测试类的跳过条件是APPLE_CONTAINER_LIVE not set。如果initialize()抛RuntimeError按报错文本对号入座提示 “not installed” 说明container二进制缺失提示 “not available ...container system start” 说明 CLI 已装但系统服务没启动。限制与生命周期注意事项状态不持久容器文件系统就是全部持久层没有宿主机侧workdir参数默认也不使用 volume。close()会执行container stopcontainer rm -f文件系统状态随之丢弃。需要保留文件时应在工作区内自行处理不能依赖关闭后再读取。gateway 端口不做宿主机映射gateway_port默认 5600是容器内 gateway 监听端口没有宿主机端口映射宿主机侧通过容器内 shim 驱动 gateway不需要你手动开端口。幂等性如果同名容器as_ws_workspace_id还在运行第二次initialize()是空操作如果同名容器处于停止状态工作区会重新container start它reattach而不是报错。平台限制仅限 macOS 26 Apple siliconIntel Mac 上没有可用的 Apple Container。需要多工作区管理TTL 缓存、后台 sweeper 驱逐空闲容器时可参考 AppleContainerWorkspaceManager它持有统一的base_image/cpus/memory/gateway_port/env/extra_pip参数get_workspace()命中缓存则复用、未命中则创建并initialize()默认 TTL 为 3600 秒作为async with上下文退出时close_all()并行关闭所有容器。【免费下载链接】agentscopeBuild and run agents you can see, understand and trust.项目地址: https://gitcode.com/GitHub_Trending/ag/agentscope创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
上一篇/下一篇内容由系统自动关联 返回资讯列表 →