Commit 49ffe4f

mo khan <mo@mokhan.ca>
2026-01-21 19:46:14
refactor: convert default tools into default plugins
1 parent f336e71
Changed files (5)
lib/elelem/builtins/execute.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+Elelem::Plugins.register(:execute) do |toolbox|
+  toolbox.add("execute",
+    description: "Run shell command (supports pipes and redirections)",
+    params: { command: { type: "string" } },
+    required: ["command"],
+    aliases: ["bash", "sh", "exec"]
+  ) do |a|
+    Elelem.sh("bash", args: ["-c", a["command"]]) { |x| $stdout.print(x) }
+  end
+end
lib/elelem/builtins/mcp.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+Elelem::Plugins.register(:mcp) do |toolbox|
+  mcp = Elelem::MCP.new
+  mcp.tools.each do |name, tool|
+    fn = tool[:fn]
+    toolbox.add(name,
+      description: tool[:description],
+      params: tool[:params],
+      required: tool[:required],
+      &fn
+    )
+  end
+end
lib/elelem/builtins/read.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+Elelem::Plugins.register(:read) do |toolbox|
+  toolbox.add("read",
+    description: "Read file",
+    params: { path: { type: "string" } },
+    required: ["path"],
+    aliases: ["open"]
+  ) do |a|
+    path = Pathname.new(a["path"]).expand_path
+    path.exist? ? { content: path.read, path: a["path"] } : { error: "not found" }
+  end
+
+  toolbox.after("read") do |_, result|
+    if result[:error]
+      $stdout.puts "  ! #{result[:error]}"
+    else
+      system("bat", "--style=plain", "--paging=never", result[:path])
+    end
+  end
+end
lib/elelem/builtins/verify.rb
@@ -30,12 +30,15 @@ module Elelem
     toolbox.after("write") do |_, result|
       next if result[:error]
 
-      result[:verify] = {}
       Verifiers.for(result[:path]).each do |cmd|
-        $stdout.puts "\n  → verify: #{cmd}"
+        $stdout.puts "\n  -> verify: #{cmd}"
         v = Elelem.sh("bash", args: ["-c", cmd]) { |x| $stdout.print(x) }
-        result[:verify][cmd] = v
-        break if v[:exit_status] != 0
+        status = v[:exit_status] == 0 ? "\u2713" : "\u2717"
+        $stdout.puts "  #{status} #{cmd}"
+        if v[:exit_status] != 0
+          $stdout.puts v[:content].lines.first(5).map { |l| "    #{l}" }.join
+          break
+        end
       end
     end
   end
lib/elelem/builtins/write.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+Elelem::Plugins.register(:write) do |toolbox|
+  toolbox.add("write",
+    description: "Write file",
+    params: { path: { type: "string" }, content: { type: "string" } },
+    required: ["path", "content"]
+  ) do |a|
+    path = Pathname.new(a["path"]).expand_path
+    FileUtils.mkdir_p(path.dirname)
+    { bytes: path.write(a["content"]), path: a["path"] }
+  end
+
+  toolbox.after("write") do |_, result|
+    if result[:error]
+      $stdout.puts "  ! #{result[:error]}"
+    else
+      $stdout.puts "  -> #{result[:path]}"
+    end
+  end
+end