nodestep.workspace ¶
Files for agents, on the local disk or in memory.
LocalWorkspace and InMemoryWorkspace implement Workspace and refuse
paths that leave their root. See Workspace and memory.
| Name | What it is |
|---|---|
| Interface | |
Workspace | Files an agent can read and change, confined to a root. |
GrepResult | Matching lines by relative path, and the files that could not be read. |
| Page | Names |
|---|---|
| local | LocalWorkspace |
| virtual | InMemoryWorkspace |
Interface¶
Workspace¶
Files an agent can read and change, confined to a root.
root_dir | Path |
Absolute root directory of the workspace. |
Methods
resolve_path(path)¶
Resolve a path inside the workspace.
resolve_path(path)¶resolve_path(path) -> tuple[Path, str]
path | str | None |
Path relative to the root; |
tuple[Path, str] |
Absolute path and normalized relative path. |
PathAccessError |
If the path leaves the workspace root or breaks a rule of
|
read_file(path)¶
Read a text file.
read_file(path)¶read_file(path) -> str
path | str |
Path relative to the workspace root. |
write_file(path, content)¶
Write a text file, creating parent directories.
write_file(path, content)¶write_file(path, content) -> None
path | str |
Path relative to the workspace root. |
content | str | |
list_dir(path)¶
List a directory; subdirectories end with /.
list_dir(path)¶list_dir(path='.') -> list[str]
path | str |
Path relative to the workspace root. |
list[str] |
Sorted entry names. |
FileNotFoundError |
If the path does not exist. |
NotADirectoryError |
If the path is a file. |
exists(path)¶
Whether a file or directory exists.
exists(path)¶exists(path) -> bool
path | str |
Path relative to the workspace root. |
delete(path)¶
Delete a file or directory.
delete(path)¶delete(path) -> bool
path | str |
Path relative to the workspace root. |
bool |
Whether something was deleted. |
edit(path, old, new, ...)¶
Replace text in a file.
edit(path, old, new, ...)¶edit(path, old, new, *, replace_all=False) -> int
path | str |
Path relative to the workspace root. |
old | str |
Text to find. |
new | str |
Its replacement. |
replace_all | bool |
Replace every occurrence instead of the first. |
int |
Number of replacements. |
ValueError |
If |
glob(pattern, path)¶
List files matching a glob pattern.
glob(pattern, path)¶glob(pattern, path='.') -> list[str]
pattern | str |
Pattern such as |
path | str |
Directory to search from. |
list[str] |
Matching paths relative to the root. |
FileNotFoundError |
If |
grep(pattern, path, ...)¶
Find lines matching a regular expression.
grep(pattern, path, ...)¶grep(pattern, path='.', *, glob_pattern=None) -> GrepResult
pattern | str |
Regular expression. |
path | str |
File or directory to search. |
glob_pattern | str | None |
Only search the files of a directory |
GrepResult |
Matching lines keyed by relative path, and the relative paths of files skipped because they are too large, not UTF-8 text, not readable, or symlinks that point outside the root. |
FileNotFoundError |
If |
GrepResult¶
GrepResult(*, matches={}, skipped=[])
Matching lines by relative path, and the files that could not be read.
matches | dict[str, list[str]] |
Matching lines, keyed by the file's path relative to the root. |
skipped | list[str] |
Relative paths of files that are too large, not UTF-8 text, not readable, or symlinks that point outside the root. |