CompositeBackend¶
CompositeBackend
¶
Bases: BackendProtocol
Routes file operations to different backends by path prefix.
Matches paths against route prefixes (longest first) and delegates to the corresponding backend. Unmatched paths use the default backend.
| ATTRIBUTE | DESCRIPTION |
|---|---|
default |
Backend for paths that don't match any route.
|
routes |
Map of path prefixes to backends (e.g., {"/memories/": store_backend}).
|
sorted_routes |
Routes sorted by length (longest first) for correct matching.
|
Examples:
composite = CompositeBackend(default=StateBackend(runtime), routes={"/memories/": StoreBackend(runtime), "/cache/": StoreBackend(runtime)})
composite.write("/temp.txt", "data")
composite.write("/memories/note.txt", "data")
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initialize composite backend. |
ls_info |
List directory contents (non-recursive). |
als_info |
Async version of ls_info. |
read |
Read file content, routing to appropriate backend. |
aread |
Async version of read. |
grep_raw |
Search files for regex pattern. |
agrep_raw |
Async version of grep_raw. |
glob_info |
Find files matching a glob pattern. |
aglob_info |
Async version of glob_info. |
write |
Create a new file, routing to appropriate backend. |
awrite |
Async version of write. |
edit |
Edit a file, routing to appropriate backend. |
aedit |
Async version of edit. |
execute |
Execute shell command via default backend. |
aexecute |
Async version of execute. |
upload_files |
Upload multiple files, batching by backend for efficiency. |
aupload_files |
Async version of upload_files. |
download_files |
Download multiple files, batching by backend for efficiency. |
adownload_files |
Async version of download_files. |
__init__
¶
__init__(
default: BackendProtocol | StateBackend, routes: dict[str, BackendProtocol]
) -> None
Initialize composite backend.
| PARAMETER | DESCRIPTION |
|---|---|
default
|
Backend for paths that don't match any route.
TYPE:
|
routes
|
Map of path prefixes to backends. Prefixes must start with "/" and should end with "/" (e.g., "/memories/").
TYPE:
|
ls_info
¶
List directory contents (non-recursive).
If path matches a route, lists only that backend. If path is "/", aggregates default backend plus virtual route directories. Otherwise lists default backend.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Absolute directory path starting with "/".
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[FileInfo]
|
List of FileInfo dicts. Directories have trailing "/" and is_dir=True. |
list[FileInfo]
|
Route prefixes are restored in returned paths. |
Examples:
read
¶
Read file content, routing to appropriate backend.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Absolute file path.
TYPE:
|
offset
|
Line offset to start reading from (0-indexed).
TYPE:
|
limit
|
Maximum number of lines to read.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Formatted file content with line numbers, or error message. |
aread
async
¶
Async version of read.
grep_raw
¶
Search files for regex pattern.
Routes to backends based on path: specific route searches one backend, "/" or None searches all backends, otherwise searches default backend.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
Regex pattern to search for.
TYPE:
|
path
|
Directory to search. None searches all backends.
TYPE:
|
glob
|
Glob pattern to filter files (e.g., ".py", "**/.txt"). Filters by filename, not content.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[GrepMatch] | str
|
List of GrepMatch dicts with path (route prefix restored), line |
list[GrepMatch] | str
|
(1-indexed), and text. Returns error string on failure. |
Examples:
agrep_raw
async
¶
agrep_raw(
pattern: str, path: str | None = None, glob: str | None = None
) -> list[GrepMatch] | str
Async version of grep_raw.
See grep_raw() for detailed documentation on routing behavior and parameters.
glob_info
¶
Find files matching a glob pattern.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
Glob pattern with wildcards to match file paths.
Supports standard glob syntax:
-
TYPE:
|
path
|
Base directory to search from. Default: "/" (root). The pattern is applied relative to this path.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[FileInfo]
|
list of FileInfo |
aglob_info
async
¶
Async version of glob_info.
write
¶
edit
¶
Edit a file, routing to appropriate backend.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Absolute file path.
TYPE:
|
old_string
|
String to find and replace.
TYPE:
|
new_string
|
Replacement string.
TYPE:
|
replace_all
|
If True, replace all occurrences.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EditResult
|
Success message or Command object, or error message on failure. |
aedit
async
¶
Async version of edit.
execute
¶
execute(command: str) -> ExecuteResponse
Execute shell command via default backend.
| PARAMETER | DESCRIPTION |
|---|---|
command
|
Shell command to execute.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ExecuteResponse
|
ExecuteResponse with output, exit code, and truncation flag. |
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If default backend doesn't implement SandboxBackendProtocol. |
Examples:
upload_files
¶
Upload multiple files, batching by backend for efficiency.
Groups files by their target backend, calls each backend's upload_files once with all files for that backend, then merges results in original order.
| PARAMETER | DESCRIPTION |
|---|---|
files
|
List of (path, content) tuples to upload. |
| RETURNS | DESCRIPTION |
|---|---|
list[FileUploadResponse]
|
List of FileUploadResponse objects, one per input file. |
list[FileUploadResponse]
|
Response order matches input order. |
aupload_files
async
¶
Async version of upload_files.
download_files
¶
Download multiple files, batching by backend for efficiency.
Groups paths by their target backend, calls each backend's download_files once with all paths for that backend, then merges results in original order.
| PARAMETER | DESCRIPTION |
|---|---|
paths
|
List of file paths to download. |
| RETURNS | DESCRIPTION |
|---|---|
list[FileDownloadResponse]
|
List of FileDownloadResponse objects, one per input path. |
list[FileDownloadResponse]
|
Response order matches input order. |