Skip to content

fs ​

Apply text replacements or unified patches, and manage files and directories. Mutations require approval through a handler or an interactive response.

ts
import { edit } from "std::fs"

node main() {
  const result = edit("notes.txt", [
    { oldText: "draft", newText: "final", replaceAll: true },
  ]) with approve
}

Effects ​

std::edit ​

ts
@alwaysUnder(dir)
effect std::edit {
  dir: string;
  filename: string;
  edits: Edit[];
  before: string;
  after: string
}

(source)

std::applyPatch ​

ts
effect std::applyPatch {
  patch: string;
  files: string[]
}

(source)

std::mkdir ​

ts
@alwaysUnder(dir)
effect std::mkdir {
  dir: string
}

(source)

std::copy ​

ts
@alwaysUnder(src, dest)
effect std::copy {
  src: string;
  dest: string
}

(source)

std::move ​

ts
@alwaysUnder(src, dest)
effect std::move {
  src: string;
  dest: string
}

(source)

std::remove ​

ts
@alwaysUnder(target)
effect std::remove {
  target: string
}

(source)

Functions ​

edit ​

ts
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:

NameTypeDefaultDescription
filenamestringRelative path within dir, without upward traversal or symlink components. To reach another directory, pass it as dir.
editsEdit[]Replacements to apply in order; replaceAll replaces every occurrence.
dirstring"."Directory containing the file.
useAgentCwdbooleanfalseResolve a relative dir against the agent working directory when set.

Returns: Result

Throws: std::edit

Needs: fileRead, fileWrite

(source)

applyPatch ​

ts
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:

NameTypeDefaultDescription
patchstring
allowedPathsstring[][]Restrict affected files to these directories, or leave empty for no restriction.

Returns: Result

Throws: std::applyPatch

Needs: fileRead, fileWrite

(source)

mkdir ​

ts
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:

NameTypeDefaultDescription
dirstring
allowedPathsstring[][]Restrict creation to these directories, or leave empty for no restriction.
useAgentCwdbooleanfalseResolve a relative dir against the agent working directory when set.

Returns: Result

Throws: std::mkdir

Needs: fileRead, fileWrite

(source)

copy ​

ts
copy(
  src: string,
  dest: string,
  allowedPaths: string[] = [],
  useAgentCwd: boolean = false,
): Result raises <std::copy>

Copy a file or directory recursively.

Parameters:

NameTypeDefaultDescription
srcstring
deststring
allowedPathsstring[][]Restrict paths to these directories, or leave empty for no restriction.
useAgentCwdbooleanfalseResolve relative paths against the agent working directory when set.

Returns: Result

Throws: std::copy

Needs: fileRead, fileWrite

(source)

move ​

ts
move(
  src: string,
  dest: string,
  allowedPaths: string[] = [],
  useAgentCwd: boolean = false,
): Result raises <std::move>

Move or rename a file or directory.

Parameters:

NameTypeDefaultDescription
srcstring
deststring
allowedPathsstring[][]Restrict paths to these directories, or leave empty for no restriction.
useAgentCwdbooleanfalseResolve relative paths against the agent working directory when set.

Returns: Result

Throws: std::move

Needs: fileRead, fileWrite

(source)

remove ​

ts
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:

NameTypeDefaultDescription
targetstring
allowedPathsstring[][]Restrict paths to these directories, or leave empty for no restriction.
useAgentCwdbooleanfalseResolve relative paths against the agent working directory when set.

Returns: Result

Throws: std::remove

Needs: fileRead, fileWrite

(source)