Commit b3ecab2

mo khan <mo@mokhan.ca>
2026-01-19 23:17:38
feat: add a patch tool
1 parent 5852457
lib/elelem/agent.rb
@@ -113,9 +113,9 @@ module Elelem
         - execute: shell command
 
         # Editing
-        Use sed: `sed -i'' 's/old/new/' file`
-        Escape: / & \\ [ ] . *
-        Multi-line: use write
+        Use `patch` tool for multi-line changes (unified diff format)
+        Use sed for single-line changes: `sed -i'' 's/old/new/' file`
+        Use write for new files or full rewrites
 
         # Search
         Use `rg` for text search: `rg -n "pattern" .`
lib/elelem/toolbox.rb
@@ -27,6 +27,18 @@ module Elelem
         params: { command: { type: "string" } },
         required: ["command"],
         fn: ->(a) { Elelem.sh("bash", args: ["-c", a["command"]]) { |x| $stdout.print(x) } }
+      },
+      "patch" => {
+        desc: "Apply unified diff",
+        params: { diff: { type: "string", description: "Unified diff content" } },
+        required: ["diff"],
+        fn: lambda do |a|
+          Tempfile.create("patch") do |f|
+            f.write(a["diff"])
+            f.flush
+            Elelem.sh("patch", args: ["-p0", "-i", f.path])
+          end
+        end
       }
     }.freeze
 
lib/elelem.rb
@@ -8,6 +8,7 @@ require "open3"
 require "pathname"
 require "reline"
 require "stringio"
+require "tempfile"
 
 require_relative "elelem/agent"
 require_relative "elelem/mcp"
elelem.gemspec
@@ -48,4 +48,5 @@ Gem::Specification.new do |spec|
   spec.add_dependency "pathname", "~> 0.1"
   spec.add_dependency "reline", "~> 0.6"
   spec.add_dependency "stringio", "~> 3.0"
+  spec.add_dependency "tempfile", "~> 0.3"
 end
Gemfile.lock
@@ -10,6 +10,7 @@ PATH
       pathname (~> 0.1)
       reline (~> 0.6)
       stringio (~> 3.0)
+      tempfile (~> 0.3)
 
 GEM
   remote: https://rubygems.org/
@@ -68,6 +69,7 @@ GEM
       rspec-support (~> 3.13.0)
     rspec-support (3.13.6)
     stringio (3.2.0)
+    tempfile (0.3.1)
     tsort (0.2.0)
     uri (1.1.1)