agentscope.workspaceDockerBackend
class DockerBackend(BackendBase)
agentscope/workspace/_docker/_docker_backend.py:25–193Docker Backend,在隔离环境中安全执行代码和工具。位于 agentscope.workspace 包,继承自 BackendBase。核心成员包括 getcwd、exec_shell、read_file、write_file。
Only the three abstract primitives (exec_shell, read_file,
write_file) are implemented here; the derived filesystem helpers
are inherited from :class:BackendBase.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(container: Any, workdir: str) -> None | 初始化 `DockerBackend` 实例。 Initialize the Docker backend. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
async def getcwd() -> strasync | 返回 `getcwd` 结果。 Return the container's default working directory. |
async def exec_shell(command: list[str], *, cwd: str | None = None, timeout: float | None = None) -> ExecResultasync | 执行 `exec_shell` 方法。 Run a program directly inside the container. |
async def read_file(path: str) -> bytesasync | 设置读取文件。 Fetch a file from the container via ``get_archive``. |
async def write_file(path: str, data: bytes) -> Noneasync | 执行写入文件相关操作。 Write raw bytes to a file inside the container. |
构造器详情Constructor Detail
DockerBackend
def __init__(container: Any, workdir: str) -> None
初始化 DockerBackend 实例。
Initialize the Docker backend.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
container | Any | 一种已启动`aiodocker`容器对象。A started `aiodocker` container object. |
workdir | str | workdir(类型 `str`)。Default working directory for `exec_shell` calls inside the container. |
agentscope/workspace/_docker/_docker_backend.py:40–51方法详情Method Detail
getcwd
async def getcwd() -> str
返回 getcwd 结果。
Overrides the base class default (which would shell out to pwd) with the cached workdir supplied at construction, avoiding a per-call container exec round-trip.
agentscope/workspace/_docker/_docker_backend.py:55–66exec_shell
async def exec_shell(command: list[str], *, cwd: str | None = None, timeout: float | None = None) -> ExecResult
执行 exec_shell 方法。
command* is an argv list executed via the container exec API without an intervening shell.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
command | list[str] | 命令(类型 `list[str]`)。Executable path/name followed by its arguments. |
cwdkw默认default None | str | None | cwd(类型 `str | None`)。Working directory inside the container. When `None` the backend's default `workdir` is used. |
timeoutkw默认default None | float | None | 超时时间(类型 `float | None`)。Maximum number of seconds to wait before returning an `exit_code` of `-1`. When `None` the call waits indefinitely. |
agentscope/workspace/_docker/_docker_backend.py:68–132read_file
async def read_file(path: str) -> bytes
设置读取文件。
Fetch a file from the container via get_archive.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to the file inside the container. |
- 可能抛出 `Exception`。`FileNotFoundError`:
- 可能抛出 `Exception`。If the path does not exist inside the container.
agentscope/workspace/_docker/_docker_backend.py:136–170write_file
async def write_file(path: str, data: bytes) -> None
执行写入文件相关操作。
Creates the parent directory first since put_archive requires it to exist.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Destination path inside the container. |
data | bytes | 原始字节到写入。The raw bytes to write. |
agentscope/workspace/_docker/_docker_backend.py:172–193