agents-tools 0.1.0

Shared Rust implementations of agent tools
Documentation

agents-tools

Shared Rust implementations of agent tools for Math & Magic projects.

Filesystem apply_diff

The default filesystem feature provides apply_diff(root, patch). It parses a complete patch envelope, supports multiple file blocks and multiple update sections, validates paths beneath the supplied root, and performs atomic file replacement on Linux, macOS, and Windows.

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

Each logical file is planned completely in memory before it is committed. A failure in one file leaves that file unchanged and does not prevent independent files in the same patch from being applied. The returned ApplyDiffReport contains both committed changes and per-file failures.

Overlapping paths are serialized within the process. Before every commit, the implementation verifies that the file still matches the snapshot used to build the update. Complete contents are written and synchronized before an atomic replacement, so readers see either the previous file or the complete new file.

In-memory text API

apply_diff_to_text applies a complete update body, including multiple anchored sections, to a string. apply_diff_to_text_with_mode also exposes create mode.

use agents_tools::{ApplyDiffMode, apply_diff_to_text, apply_diff_to_text_with_mode};

let updated = apply_diff_to_text("name = old\n", "-name = old\n+name = new")?;
assert_eq!(updated, "name = new\n");

let created = apply_diff_to_text_with_mode(
    "",
    "+first line\n+second line",
    ApplyDiffMode::Create,
)?;
assert_eq!(created, "first line\nsecond line");
# Ok::<(), agents_tools::ApplyDiffError>(())

Consumers that only need in-memory updates can omit the filesystem dependency set:

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

License

Licensed under the Apache License, Version 2.0.