Skip to main content

Crate agents_tools

Crate agents_tools 

Source
Expand description

Shared, reusable implementations of agent editing tools.

The crate exposes two layers:

  • apply_diff_to_text applies a complete update body to an in-memory string. It supports multiple anchored sections and is always available.
  • apply_diff parses a complete multi-file patch and applies it beneath a filesystem root. It is available with the default filesystem feature.

§Patch format

Filesystem patches use a single envelope containing one or more file operations:

*** Begin Patch
*** Add File: src/new.rs
+pub fn new() {}
*** Update File: src/lib.rs
@@ pub fn old() {
-    before();
+    after();
*** Update File: src/name.rs
*** Move to: src/renamed.rs
*** Delete File: src/unused.rs
*** End Patch

Update bodies may contain multiple @@ sections. Lines beginning with a space are context, - removes a line, and + inserts a line. Add blocks require every content line to begin with +.

§Failure and concurrency model

Patch syntax and unsafe paths are validated before filesystem work begins. Each connected logical file is then planned fully in memory and committed independently. A failed file remains unchanged while unrelated files in the same patch can still succeed; inspect ApplyDiffReport::failed for those failures.

File replacements are written completely, synchronized, and atomically installed, so readers do not observe partially written contents. Overlapping paths are serialized within the process, and the on-disk snapshot is checked again immediately before each commit to detect observed external changes.

§Feature flags

The filesystem feature is enabled by default and provides the filesystem API and serializable reports. Text-only users can disable default features:

agents-tools = { version = "0.1", default-features = false }

§In-memory example

use agents_tools::apply_diff_to_text;

let updated = apply_diff_to_text(
    "fn run() {\n    before();\n}\n",
    "@@ fn run() {\n-    before();\n+    after();",
)?;

assert_eq!(updated, "fn run() {\n    after();\n}\n");

Structs§

AppliedFile
A committed file change.
ApplyDiffError
An error produced when a patch cannot be parsed or applied safely.
ApplyDiffReport
The result of applying a multi-file patch.
FailedFile
A logical file transaction that could not be applied.

Enums§

ApplyDiffMode
Selects how an in-memory diff body is interpreted.
FileChangeKind
The kind of filesystem change made by apply_diff.

Functions§

apply_diff
Applies a complete patch to files beneath root.
apply_diff_to_text
Applies a complete update body to in-memory text.
apply_diff_to_text_with_mode
Applies a diff body to input using the requested mode.