agentscope.toolBackendBase
class BackendBase(ABC)
agentscope/tool/_builtin/_backend.py:138–712Backend 基类,让智能体以受控方式调用外部能力。位于 agentscope.tool 包,继承自 ABC。核心成员包括 join_path、dirname、basename、isabs、normpath、abspath。
Subclasses must implement three abstract primitives — exec_shell,
read_file, and write_file — which are the only operations
whose mechanism genuinely differs per environment. The remaining
filesystem helpers are implemented here on top of exec_shell and
work for any backend whose shell is POSIX-like; a backend with a
cheaper native path may override them (see :class:LocalBackend).
属性摘要Attribute Summary
| 属性Attribute | 类型Type | 说明Description | 值Value |
|---|---|---|---|
os_name | str | os 名称字段。 | 'posix' |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
def join_path(path: str, *paths: str) -> str | 连接路径。 Join one or more path components using the backend's separator. |
def dirname(path: str) -> str | 设置该参数。 Return the directory component of ``path``. |
def basename(path: str) -> str | 设置该参数。 Return the final component of ``path``. |
def isabs(path: str) -> bool | 执行 `isabs` 方法。 Return ``True`` if ``path`` is absolute in the backend. |
def normpath(path: str) -> str | 设置该参数。 Normalize ``path`` (collapse ``..``, ``.``, duplicate seps). |
def abspath(path: str, *, cwd: str) -> str | 执行 `abspath` 方法。 Return an absolute, normalized version of ``path``. |
async def exec_shell(command: list[str], *, cwd: str | None = None, timeout: float | None = None) -> ExecResultasyncabstractabstractmethod | 执行 `exec_shell` 方法。 Run a program directly from an argument vector. |
async def read_file(path: str) -> bytesasyncabstractabstractmethod | 设置读取文件。 Read the full contents of ``path`` as raw bytes. |
async def write_file(path: str, data: bytes) -> Noneasyncabstractabstractmethod | 执行写入文件相关操作。 Write ``data`` to ``path``, creating parent directories. |
async def write_stream(path: str, stream: AsyncIterator[bytes]) -> Noneasync | 执行写入流相关操作。 Write a byte stream to ``path``, creating parent directories. |
async def read_stream(path: str, chunk_size: int = DEFAULT_READ_CHUNK_SIZE) -> AsyncIterator[bytes]async | 执行读取流相关操作。 Read ``path`` as a byte stream. |
async def getcwd() -> strasync | 返回 `getcwd` 结果。 Return the backend environment's current working directory. |
async def expanduser(path: str) -> strasync | 设置该参数。 Expand a leading ``~`` / ``~/`` to the backend's home directory. |
async def file_exists(path: str) -> boolasync | 设置该参数。 Return ``True`` if ``path`` exists (file or directory). |
async def is_dir(path: str) -> boolasync | 判断是否目录。 Return ``True`` if ``path`` is an existing directory. |
async def list_dir(path: str, *, recursive: bool = False) -> list[str]async | 列出目录。 List entries under ``path``. |
async def scandir(path: str) -> list[DirEntry]async | 设置该参数。 List one directory level with each entry's metadata. |
async def stat(path: str) -> DirEntry | Noneasync | 设置该参数。 Return one path's type, size and mtime in a single call. |
async def stat_mtime(path: str) -> float | Noneasync | 设置该参数。 Return the modification time of ``path``, or ``None``. |
async def delete_path(path: str) -> Noneasync | 删除路径。 Delete ``path`` (file or directory tree). |
属性详情Attribute Detail
os_name
str = 'posix'os 名称字段。声明为 str。
The os name field.
agentscope/tool/_builtin/_backend.py:186方法详情Method Detail
join_path
def join_path(path: str, *paths: str) -> str
连接路径。
Join one or more path components using the backend's separator.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。The first path component. |
paths | str | paths(类型 `str`)。The paths. |
agentscope/tool/_builtin/_backend.py:190–204dirname
def dirname(path: str) -> str
设置该参数。
Return the directory component of path.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。A path inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:206–218basename
def basename(path: str) -> str
设置该参数。
Return the final component of path.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。A path inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:220–232isabs
def isabs(path: str) -> bool
执行 isabs 方法。
Return True if path is absolute in the backend.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。A path inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:234–246normpath
def normpath(path: str) -> str
设置该参数。
Pure string operation — does not touch the filesystem.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。A path inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:248–261abspath
def abspath(path: str, *, cwd: str) -> str
执行 abspath 方法。
Unlike :func:os.path.abspath, this helper never reads the host process's working directory: when path is relative it is joined with the explicitly supplied cwd, which must itself be a path that is meaningful inside the backend's environment.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。A path inside the backend's environment. |
cwdkw | str | cwd(类型 `str`)。Directory to resolve a relative `path` against. Ignored when `path` is already absolute. |
agentscope/tool/_builtin/_backend.py:263–289exec_shell
async def exec_shell(command: list[str], *, cwd: str | None = None, timeout: float | None = None) -> ExecResult
执行 exec_shell 方法。
command* is an executable followed by its arguments — it is not passed through a shell, so callers never have to quote or escape arguments and there is no platform-specific quoting bug.
| 名称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 to run the command in. When `None` the backend's default working directory is used. |
timeoutkw默认default None | float | None | 超时时间(类型 `float | None`)。Maximum number of seconds to wait. When `None` the call waits indefinitely. On timeout the result carries an `exit_code` of `-1`. |
agentscope/tool/_builtin/_backend.py:294–328read_file
async def read_file(path: str) -> bytes
设置读取文件。
Read the full contents of path as raw bytes.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to the file inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:331–341write_file
async def write_file(path: str, data: bytes) -> None
执行写入文件相关操作。
Write data to path, creating parent directories.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Destination path inside the backend's environment. |
data | bytes | 原始字节到写入。The raw bytes to write. |
agentscope/tool/_builtin/_backend.py:344–352write_stream
async def write_stream(path: str, stream: AsyncIterator[bytes]) -> None
执行写入流相关操作。
The default buffers the whole stream and delegates to :meth:write_file, so peak memory is the payload size; only backends that override this (LocalBackend, BubblewrapBackend, K8sBackend) are constant-memory.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Destination path inside the backend's environment. |
stream | AsyncIterator[bytes] | 流(类型 `AsyncIterator[bytes]`)。The chunks to write, in order. |
agentscope/tool/_builtin/_backend.py:354–375read_stream
async def read_stream(path: str, chunk_size: int = DEFAULT_READ_CHUNK_SIZE) -> AsyncIterator[bytes]
执行读取流相关操作。
The default reads the whole file through :meth:read_file and re-slices it, so peak memory is the file size; only backends that override this (LocalBackend) are constant-memory.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to the file inside the backend's environment. |
chunk_size默认default DEFAULT_READ_CHUNK_SIZE | int | 分块大小(类型 `int`)。The size of each yielded chunk. |
agentscope/tool/_builtin/_backend.py:377–402getcwd
async def getcwd() -> str
返回 getcwd 结果。
This is the directory that bare exec_shell invocations (those with cwd=None) execute in.
agentscope/tool/_builtin/_backend.py:406–426expanduser
async def expanduser(path: str) -> str
设置该参数。
Tools should call this — instead of :func:os.path.expanduser — whenever they need to expand ~ in a path that lives inside the backend's environment, because the host process's $HOME is meaningless for remote backends.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。A path inside the backend's environment, possibly starting with `~`. |
agentscope/tool/_builtin/_backend.py:428–462file_exists
async def file_exists(path: str) -> bool
设置该参数。
Return True if path exists (file or directory).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to test inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:464–476is_dir
async def is_dir(path: str) -> bool
判断是否目录。
Return True if path is an existing directory.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to test inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:478–490list_dir
async def list_dir(path: str, *, recursive: bool = False) -> list[str]
列出目录。
Output is NUL-delimited (find -print0 / -printf '%f\0') and split on \0 so that file names containing spaces or newlines are handled correctly.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Directory to list inside the backend's environment. |
recursivekw默认default False | bool | recursive(类型 `bool`)。When `True`, return all files underneath `path` as paths (like `find path -type f`). When `False` (default), return the immediate children's base names (like `os.listdir`). |
agentscope/tool/_builtin/_backend.py:492–540scandir
async def scandir(path: str) -> list[DirEntry]
设置该参数。
The type, size and mtime come back from the same find run as the names, so listing a directory of N entries costs one round trip rather than 1 + 3N.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Directory to list inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:542–576stat
async def stat(path: str) -> DirEntry | None
设置该参数。
The single-path counterpart of :meth:scandir, the way :func:os.stat sits beside :func:os.scandir.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to stat inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:578–601stat_mtime
async def stat_mtime(path: str) -> float | None
设置该参数。
Tries GNU stat -c %Y first and falls back to BSD stat -f %m so the same call works across coreutils and BSD/macOS userlands.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to stat inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:669–700delete_path
async def delete_path(path: str) -> None
删除路径。
If path does not exist the call is a silent no-op (like rm -rf).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to delete inside the backend's environment. |
agentscope/tool/_builtin/_backend.py:702–712