AS AgentScopeAPI 手册 / API Reference
类class agentscope.app.workspace_manager

DaytonaWorkspaceManager

class DaytonaWorkspaceManager(WorkspaceManagerBase)
源码Source agentscope/app/workspace_manager/_daytona_workspace_manager.py:40–339

Daytona 工作区管理器,承载智能体的部署、调度与服务化运行。位于 agentscope.app.workspace_manager 包,继承自 WorkspaceManagerBase。核心成员包括 get_workspace、close、close_all。

Use the manager as an async with context manager: entering it starts the TTL sweeper task, exiting it stops the sweeper and then closes every cached workspace via :meth:close_all.

构造器摘要Constructor Summary

构造器Constructor说明Description
def __init__(*, isolation: IsolationPolicy = IsolationPolicy.PER_AGENT, api_key: str = '', api_url: str = '', target: s…
初始化 `DaytonaWorkspaceManager` 实例。
Initialize the Daytona workspace manager.

方法摘要Method Summary

方法Method说明Description
async def get_workspace(user_id: str, agent_id: str, session_id: str, workspace_id: str | None = None) -> DaytonaWorksp…async
获取工作区。
Return an initialized workspace, reattaching on cache miss.
async def close(workspace_id: str) -> Noneasync
关闭连接并释放相关资源。
Close (= gracefully stop the sandbox) and evict a workspace.
async def close_all() -> Noneasync
关闭所有。
Close every cached workspace in parallel.
async def __aenter__() -> Selfasync
进入异步上下文并返回自身。
Start the TTL sweeper task.
async def __aexit__(*exc: object) -> Noneasync
退出异步上下文并清理资源。
Stop the TTL sweeper task, then close every cached workspace.

构造器详情Constructor Detail

DaytonaWorkspaceManager

def __init__(*, isolation: IsolationPolicy = IsolationPolicy.PER_AGENT, api_key: str = '', api_url: str = '', target: str = '', timeout_seconds: int = DEFAULT_TIMEOUT, gateway_port: int = DEFAULT_GATEWAY_PORT, env: dict[str, str] | None = None, sandbox_metadata: dict[str, str] | None = None, extra_pip: list[str] | None = None, default_mcps: list[MCPClient] | None = None, skill_paths: list[str] | None = None, os_user: str | None = None, ttl: float = 3600.0, sweep_interval: float = DEFAULT_SWEEP_INTERVAL) -> None

初始化 DaytonaWorkspaceManager 实例。

Initialize the Daytona workspace manager.

参数Parameters
名称Name类型Type说明Description
isolationkw默认default IsolationPolicy.PER_AGENTIsolationPolicy隔离(类型 `IsolationPolicy`)。Isolation grain for :meth:`assign_workspace_id`. See :class:`DockerWorkspaceManager` for semantics.
api_keykw默认default ''strAPI 密钥(类型 `str`)。Daytona API key forwarded to every workspace. `""` lets the Daytona SDK read credentials from the environment.
api_urlkw默认default ''strAPI URL(类型 `str`)。Optional Daytona API URL for self-hosted or non-default deployments.
targetkw默认default ''str目标(类型 `str`)。Optional Daytona target / region.
timeout_secondskw默认default DEFAULT_TIMEOUTint超时秒数(类型 `int`)。Timeout forwarded to workspace create/start/recover/stop operations.
gateway_portkw默认default DEFAULT_GATEWAY_PORTint网关 port(类型 `int`)。TCP port the in-sandbox gateway listens on.
envkw默认default Nonedict[str, str] | Noneenv(类型 `dict[str, str] | None`)。Environment variables passed to Daytona create params.
sandbox_metadatakw默认default Nonedict[str, str] | None沙箱元数据(类型 `dict[str, str] | None`)。Extra labels merged with the per-workspace `agentscope.workspace.id` / `agentscope.user.id` / `agentscope.agent.id` labels.
extra_pipkw默认default Nonelist[str] | Noneextra pip(类型 `list[str] | None`)。Extra Python packages installed into the gateway venv during bootstrap.
default_mcpskw默认default Nonelist[MCPClient] | None默认 mcps(类型 `list[MCPClient] | None`)。MCP clients seeded into brand-new workspaces. Persisted `.mcp` state wins on reattach.
skill_pathskw默认default Nonelist[str] | None技能 paths(类型 `list[str] | None`)。Skill directories seeded into brand-new workspaces.
os_userkw默认default Nonestr | Noneos 用户(类型 `str | None`)。Optional Daytona OS user forwarded to workspace create params. `None` leaves user selection to Daytona.
ttlkw默认default 3600.0float存活时间(类型 `float`)。Seconds before an idle cached workspace is evicted and its sandbox stopped.
sweep_intervalkw默认default DEFAULT_SWEEP_INTERVALfloatsweep 间隔(类型 `float`)。How often (seconds) the background sweeper wakes up to look for idle workspaces.
源码Source agentscope/app/workspace_manager/_daytona_workspace_manager.py:48–128

方法详情Method Detail

get_workspace

async def get_workspace(user_id: str, agent_id: str, session_id: str, workspace_id: str | None = None) -> DaytonaWorkspace
async

获取工作区。

On miss the manager constructs DaytonaWorkspace with the requested workspace_id and lets its initialize find a matching sandbox by label or create a fresh sandbox otherwise.

参数Parameters
名称Name类型Type说明Description
user_idstr用户标识(类型 `str`)。Owning user identifier, forwarded as sandbox label only.
agent_idstr智能体标识(类型 `str`)。Agent identifier, forwarded as sandbox label only.
session_idstr会话标识(类型 `str`)。Session identifier (unused; sessions partition under `sessions/ /` inside a workspace).
workspace_id默认default Nonestr | None工作区标识(类型 `str | None`)。Stable workspace identifier and cache key. When unset, the manager falls back to :meth:`assign_workspace_id`.
返回Returns DaytonaWorkspace
返回DaytonaWorkspace。`DaytonaWorkspace`:
源码Source agentscope/app/workspace_manager/_daytona_workspace_manager.py:179–239

close

async def close(workspace_id: str) -> None
async

关闭连接并释放相关资源。

No-op when the workspace_id is not tracked.

参数Parameters
名称Name类型Type说明Description
workspace_idstr工作区到关闭。The workspace to close.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/app/workspace_manager/_daytona_workspace_manager.py:241–255

close_all

async def close_all() -> None
async

关闭所有。

sandbox.stop() is a remote round-trip per sandbox; doing it sequentially on app shutdown produces a noticeable stall, so we fan the calls out with :func:asyncio.gather.

返回Returns None
无返回值。No value is returned.
源码Source agentscope/app/workspace_manager/_daytona_workspace_manager.py:257–272

__aenter__

async def __aenter__() -> Self
async

进入异步上下文并返回自身。

Start the TTL sweeper task.

返回Returns Self
返回自身。Returns self.
源码Source agentscope/app/workspace_manager/_daytona_workspace_manager.py:276–280

__aexit__

async def __aexit__(*exc: object) -> None
async

退出异步上下文并清理资源。

Stop the TTL sweeper task, then close every cached workspace.

参数Parameters
名称Name类型Type说明Description
excobjectexc(类型 `object`)。The exc.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/app/workspace_manager/_daytona_workspace_manager.py:282–291

继承成员Inherited Members

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.