agentscope.toolLocalBackend
class LocalBackend(BackendBase)
agentscope/tool/_builtin/_backend.py:741–1089本地 Backend,让智能体以受控方式调用外部能力。位于 agentscope.tool 包,继承自 BackendBase。核心成员包括 exec_shell、read_file、write_file、write_stream、read_stream、getcwd。
Uses asyncio.create_subprocess_exec, aiofiles, and the
os module. This is the default backend injected when no
explicit one is given to a builtin tool. Commands are spawned
directly from their argument vector (no shell), which avoids the
POSIX-vs-cmd.exe quoting mismatch and makes the backend work on
Windows. The derived filesystem helpers are overridden with native
os.* calls — faster and more robust than shelling out, and
portable to Windows where test / find / stat are
unavailable.
属性摘要Attribute Summary
| 属性Attribute | 类型Type | 说明Description | 值Value |
|---|---|---|---|
os_name | — | os 名称字段。 | os.name |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
async def exec_shell(command: list[str], *, cwd: str | None = None, timeout: float | None = None) -> ExecResultasync | 执行 `exec_shell` 方法。 Run a program via ``asyncio.create_subprocess_exec``. |
async def read_file(path: str) -> bytesasync | 设置读取文件。 Read a local file as raw bytes. |
async def write_file(path: str, data: bytes) -> Noneasync | 执行写入文件相关操作。 Write *data* to a local file, creating parent dirs. |
async def write_stream(path: str, stream: AsyncIterator[bytes]) -> Noneasync | 执行写入流相关操作。 Write a byte stream to a local file, chunk by chunk. |
async def read_stream(path: str, chunk_size: int = DEFAULT_READ_CHUNK_SIZE) -> AsyncIterator[bytes]async | 执行读取流相关操作。 Read a local file chunk by chunk, never holding it whole. |
async def getcwd() -> strasync | 返回 `getcwd` 结果。 Return the host process's current working directory. |
async def expanduser(path: str) -> strasync | 设置该参数。 Expand ``~`` using the host process's ``$HOME``. |
async def file_exists(path: str) -> boolasync | 设置该参数。 Check if a local path exists. |
async def is_dir(path: str) -> boolasync | 检查if 本地路径是目录。 Check if a local path is a directory. |
async def list_dir(path: str, *, recursive: bool = False) -> list[str]async | 列出目录。 List local directory entries. |
async def scandir(path: str) -> list[DirEntry]async | 设置该参数。 List a local directory with each entry's metadata. |
async def stat(path: str) -> DirEntry | Noneasync | 设置该参数。 Return one local path's type, size and mtime. |
async def stat_mtime(path: str) -> float | Noneasync | 设置该参数。 Return the modification time of a local file. |
async def delete_path(path: str) -> Noneasync | 删除路径。 Delete a local file or directory tree. |
属性详情Attribute Detail
os_name
= os.nameos 名称字段。
The os name field.
agentscope/tool/_builtin/_backend.py:762方法详情Method Detail
exec_shell
async def exec_shell(command: list[str], *, cwd: str | None = None, timeout: float | None = None) -> ExecResult
执行 exec_shell 方法。
The program is spawned directly from command without an intervening shell, so no argument quoting is required and the same code path works on POSIX and Windows.
| 名称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 for the subprocess. When `None` the current process working directory is used. |
timeoutkw默认default None | float | None | 超时时间(类型 `float | None`)。Maximum number of seconds to wait before the process is killed and an `exit_code` of `-1` is returned. |
agentscope/tool/_builtin/_backend.py:764–837read_file
async def read_file(path: str) -> bytes
设置读取文件。
Read a local file as raw bytes.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to the local file. |
agentscope/tool/_builtin/_backend.py:839–851write_file
async def write_file(path: str, data: bytes) -> None
执行写入文件相关操作。
Write data to a local file, creating parent dirs.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Destination path on the local filesystem. |
data | bytes | 原始字节到写入。The raw bytes to write. |
agentscope/tool/_builtin/_backend.py:853–866write_stream
async def write_stream(path: str, stream: AsyncIterator[bytes]) -> None
执行写入流相关操作。
Write a byte stream to a local file, chunk by chunk.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Destination path on the local filesystem. |
stream | AsyncIterator[bytes] | 流(类型 `AsyncIterator[bytes]`)。The chunks to write, in order. |
agentscope/tool/_builtin/_backend.py:868–886read_stream
async def read_stream(path: str, chunk_size: int = DEFAULT_READ_CHUNK_SIZE) -> AsyncIterator[bytes]
执行读取流相关操作。
Read a local file chunk by chunk, never holding it whole.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to the local file. |
chunk_size默认default DEFAULT_READ_CHUNK_SIZE | int | 分块大小(类型 `int`)。The size of each yielded chunk. |
agentscope/tool/_builtin/_backend.py:888–907getcwd
async def getcwd() -> str
返回 getcwd 结果。
Return the host process's current working directory.
agentscope/tool/_builtin/_backend.py:909–916expanduser
async def expanduser(path: str) -> str
设置该参数。
Expand ~ using the host process's $HOME.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。A local path, possibly starting with `~`. |
agentscope/tool/_builtin/_backend.py:918–930file_exists
async def file_exists(path: str) -> bool
设置该参数。
Check if a local path exists.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to test. |
agentscope/tool/_builtin/_backend.py:932–943is_dir
async def is_dir(path: str) -> bool
检查if 本地路径是目录。
Check if a local path is a directory.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to test. |
agentscope/tool/_builtin/_backend.py:945–956list_dir
async def list_dir(path: str, *, recursive: bool = False) -> list[str]
列出目录。
Mirrors the base contract using native os calls.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Directory to list. |
recursivekw默认default False | bool | recursive(类型 `bool`)。When `True`, return file paths underneath `path` (`os.walk`). When `False` (default), return the immediate children's base names (`os.listdir`). |
agentscope/tool/_builtin/_backend.py:958–986scandir
async def scandir(path: str) -> list[DirEntry]
设置该参数。
os.scandir carries the type in the directory record itself, so this is cheaper than os.listdir followed by a stat per entry — the same reason the base class batches its find.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Directory to list. |
agentscope/tool/_builtin/_backend.py:988–1026stat
async def stat(path: str) -> DirEntry | None
设置该参数。
Return one local path's type, size and mtime.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to stat. |
agentscope/tool/_builtin/_backend.py:1028–1051stat_mtime
async def stat_mtime(path: str) -> float | None
设置该参数。
Return the modification time of a local file.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to stat. |
agentscope/tool/_builtin/_backend.py:1053–1068delete_path
async def delete_path(path: str) -> None
删除路径。
Symbolic links are removed without following their targets, including when the target no longer exists.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | str | 路径(类型 `str`)。Path to delete. |
agentscope/tool/_builtin/_backend.py:1070–1089