Commit 4598269

mo khan <mo@mokhan.ca>
2026-09-06 13:58:01
refactor: reorganize gems
1 parent d4a6d11
lib/elelem/tools/edit.rb
@@ -7,11 +7,11 @@ Elelem::Plugins.register(:edit) do |agent|
     description: "Replace first occurrence of text in file",
     params: { path: { type: "string" }, old: { type: "string" }, new: { type: "string" } },
     required: ["path", "old", "new"]
-  ) do |a|
-    path = Pathname.new(a["path"]).expand_path
+  ) do |args|
+    path = Pathname.new(args["path"]).expand_path
     content = path.read
     agent.toolbox
-      .run("write", { "path" => a["path"], "content" => content.sub(a["old"], a["new"]) })
-      .merge(replaced: a["old"], with: a["new"])
+      .run("write", { "path" => args["path"], "content" => content.sub(args["old"], args["new"]) })
+      .merge(replaced: args["old"], with: args["new"])
   end
 end
lib/elelem/tools/git.rb
@@ -1,17 +1,12 @@
 # frozen_string_literal: true
 
 Elelem::Plugins.register(:git) do |agent|
-  allowed = %w[status diff log show branch checkout add reset stash].freeze
-
   agent.toolbox.add("git",
     description: "Run git command",
     params: { command: { type: "string" }, args: { type: "array", items: { type: "string" } } },
     required: ["command"]
-  ) do |a|
-    cmd = a["command"]
-    next { error: "not allowed: #{cmd}" } unless allowed.include?(cmd)
-
-    agent.toolbox.exec("git", cmd, *(a["args"] || []))
+  ) do |args|
+    agent.toolbox.exec("git", args["command"], *(args["args"] || []))
   end
 
   agent.toolbox.after("git") do |_, result|
lib/elelem/tools/glob.rb
@@ -5,9 +5,9 @@ Elelem::Plugins.register(:glob) do |agent|
     description: "Find files matching pattern",
     params: { pattern: { type: "string" }, path: { type: "string" } },
     required: ["pattern"]
-  ) do |a|
-    path = a["path"].to_s.empty? ? "." : a["path"]
-    result = agent.toolbox.exec("fd", "--glob", a["pattern"], path)
-    result[:ok] ? result : agent.toolbox.exec("find", path, "-name", a["pattern"])
+  ) do |args|
+    path = args["path"].to_s.empty? ? "." : args["path"]
+    result = agent.toolbox.exec("fd", "--glob", args["pattern"], path)
+    result[:ok] ? result : agent.toolbox.exec("find", path, "-name", args["pattern"])
   end
 end
lib/elelem/tools/grep.rb
@@ -5,17 +5,18 @@ Elelem::Plugins.register(:grep) do |agent|
     description: "Search file contents",
     params: { pattern: { type: "string" }, path: { type: "string" }, glob: { type: "string" } },
     required: ["pattern"]
-  ) do |a|
-    path = a["path"].to_s.empty? ? "." : a["path"]
-    glob = a["glob"]
-    rg_args = ["rg", "-n", a["pattern"], path]
+  ) do |args|
+    path = args["path"].to_s.empty? ? "." : args["path"]
+    glob = args["glob"]
+
+    rg_args = ["rg", "-n", args["pattern"], path]
     rg_args += ["-g", glob] if glob
     result = agent.toolbox.exec(*rg_args)
     next result if result[:ok]
 
     grep_args = ["grep", "-rn"]
     grep_args += ["--include", glob] if glob
-    grep_args += [a["pattern"], path]
+    grep_args += [args["pattern"], path]
     agent.toolbox.exec(*grep_args)
   end
 end
lib/elelem/tools/list.rb
@@ -6,9 +6,9 @@ Elelem::Plugins.register(:list) do |agent|
     params: { path: { type: "string" }, recursive: { type: "boolean" } },
     required: [],
     aliases: ["ls"]
-  ) do |a|
-    path = a["path"] && !a["path"].empty? ? a["path"] : "."
-    flags = a["recursive"] ? "-laR" : "-la"
+  ) do |args|
+    path = args["path"] && !args["path"].empty? ? args["path"] : "."
+    flags = args["recursive"] ? "-laR" : "-la"
     agent.toolbox.exec("ls", flags, path)
   end
 end
lib/elelem/tools/task.rb
@@ -5,10 +5,9 @@ Elelem::Plugins.register(:task) do |agent|
     description: "Delegate subtask to focused agent (complex searches, multi-file analysis)",
     params: { prompt: { type: "string" } },
     required: ["prompt"]
-  ) do |a|
-    sub = Elelem::Agent.new(agent.client, toolbox: agent.toolbox, terminal: agent.terminal,
-      system_prompt: "Research agent. Search, analyze, report. Be concise.")
-    sub.turn(a["prompt"])
+  ) do |args|
+    sub = agent.fork(system_prompt: "Research agent. Search, analyze, report. Be concise.")
+    sub.turn(args["prompt"])
     { result: sub.conversation.last[:content] }
   end
 end
lib/elelem/tools/verify.rb
@@ -1,49 +1,19 @@
 # frozen_string_literal: true
 
-module Elelem
-  module Contrib
-    module Verifiers
-      SYNTAX = {
-        ".rb" => "ruby -c %{path}",
-        ".erb" => "erb -x %{path} | ruby -c",
-        ".py" => "python -m py_compile %{path}",
-        ".go" => "go vet %{path}",
-        ".rs" => "cargo check --quiet",
-        ".ts" => "npx tsc --noEmit %{path}",
-        ".js" => "node --check %{path}",
-      }.freeze
+Elelem::Plugins.register(:verify) do |agent|
+  agent.toolbox.add("verify",
+    description: "Verify file syntax and run tests",
+    params: { path: { type: "string" } },
+    required: ["path"]
+  ) do |args|
+    path = args["path"]
+    Elelem::Plugins::Verifiers.for(path).inject({ verified: [] }) do |memo, cmd|
+      agent.terminal.say agent.toolbox.header("execute", { "command" => cmd })
+      v = agent.toolbox.run("execute", { "command" => cmd })
+      break v.merge(path: path, command: cmd) if v[:exit_status] != 0
 
-      def self.for(path)
-        return [] unless path
-
-        cmds = []
-        ext = File.extname(path)
-        cmds << (SYNTAX[ext] % { path: path }) if SYNTAX[ext]
-        cmds << test_runner
-        cmds.compact
-      end
-
-      def self.test_runner
-        %w[bin/test script/test].find { |s| File.executable?(s) }
-      end
-    end
-  end
-
-  Plugins.register(:verify) do |agent|
-    agent.toolbox.add("verify",
-      description: "Verify file syntax and run tests",
-      params: { path: { type: "string" } },
-      required: ["path"]
-    ) do |a|
-      path = a["path"]
-      Elelem::Contrib::Verifiers.for(path).inject({ verified: [] }) do |memo, cmd|
-        agent.terminal.say agent.toolbox.header("execute", { "command" => cmd })
-        v = agent.toolbox.run("execute", { "command" => cmd })
-        break v.merge(path: path, command: cmd) if v[:exit_status] != 0
-
-        memo[:verified] << cmd
-        memo
-      end
+      memo[:verified] << cmd
+      memo
     end
   end
 end