fs
Apply text replacements or unified patches, and manage files and directories. Mutations require approval through a handler or an interactive response.
import { edit } from "std::fs"
node main() {
const result = edit("notes.txt", [
{ oldText: "draft", newText: "final", replaceAll: true },
]) with approve
}Effects
std::edit
@alwaysUnder(dir)
effect std::edit {
dir: string;
filename: string;
edits: Edit[];
before: string;
after: string
}(source)
std::applyPatch
effect std::applyPatch {
patch: string;
files: string[]
}(source)
std::mkdir
@alwaysUnder(dir)
effect std::mkdir {
dir: string
}(source)
std::copy
@alwaysUnder(src, dest)
effect std::copy {
src: string;
dest: string
}(source)
std::move
@alwaysUnder(src, dest)
effect std::move {
src: string;
dest: string
}(source)
std::remove
@alwaysUnder(target)
effect std::remove {
target: string
}(source)
Functions
edit
edit(
filename: string,
edits: Edit[],
dir: string = ".",
useAgentCwd: boolean = false,
): Result raises <std::edit>Apply text replacements in order, writing only if every edit succeeds. Prefer one call with multiple edits over many separate calls. Each oldText must match uniquely unless replaceAll is true. Use separate edits in the same call for distant changes, with just enough oldText to identify each match.
The std::edit interrupt carries the full before and after file contents in its data, so a handler can render a diff itself. A handler receives the whole interrupt object, so the contents are at data.data.before / data.data.after (e.g. print(diff(data.data.before, data.data.after, color: true))).
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| filename | string | Relative path within dir, without upward traversal or symlink components. To reach another directory, pass it as dir. | |
| edits | Edit[] | Replacements to apply in order; replaceAll replaces every occurrence. | |
| dir | string | "." | Directory containing the file. |
| useAgentCwd | boolean | false | Resolve a relative dir against the agent working directory when set. |
Returns: Result
Throws: std::edit
Needs: fileRead, fileWrite
(source)
applyPatch
applyPatch(
patch: string,
allowedPaths: string[] = [],
): Result raises <std::applyPatch>Apply a unified diff to files, including file creation. Fails on malformed patches or mismatched context.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| patch | string | ||
| allowedPaths | string[] | [] | Restrict affected files to these directories, or leave empty for no restriction. |
Returns: Result
Throws: std::applyPatch
Needs: fileRead, fileWrite
(source)
mkdir
mkdir(
dir: string,
allowedPaths: string[] = [],
useAgentCwd: boolean = false,
): Result raises <std::mkdir>Create a directory and any missing parents. Succeeds if it already exists.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| dir | string | ||
| allowedPaths | string[] | [] | Restrict creation to these directories, or leave empty for no restriction. |
| useAgentCwd | boolean | false | Resolve a relative dir against the agent working directory when set. |
Returns: Result
Throws: std::mkdir
Needs: fileRead, fileWrite
(source)
copy
copy(
src: string,
dest: string,
allowedPaths: string[] = [],
useAgentCwd: boolean = false,
): Result raises <std::copy>Copy a file or directory recursively.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| src | string | ||
| dest | string | ||
| allowedPaths | string[] | [] | Restrict paths to these directories, or leave empty for no restriction. |
| useAgentCwd | boolean | false | Resolve relative paths against the agent working directory when set. |
Returns: Result
Throws: std::copy
Needs: fileRead, fileWrite
(source)
move
move(
src: string,
dest: string,
allowedPaths: string[] = [],
useAgentCwd: boolean = false,
): Result raises <std::move>Move or rename a file or directory.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| src | string | ||
| dest | string | ||
| allowedPaths | string[] | [] | Restrict paths to these directories, or leave empty for no restriction. |
| useAgentCwd | boolean | false | Resolve relative paths against the agent working directory when set. |
Returns: Result
Throws: std::move
Needs: fileRead, fileWrite
(source)
remove
remove(
target: string,
allowedPaths: string[] = [],
useAgentCwd: boolean = false,
): Result raises <std::remove>Delete a file or directory recursively. Succeeds if the target is missing.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| target | string | ||
| allowedPaths | string[] | [] | Restrict paths to these directories, or leave empty for no restriction. |
| useAgentCwd | boolean | false | Resolve relative paths against the agent working directory when set. |
Returns: Result
Throws: std::remove
Needs: fileRead, fileWrite
(source)