Commit 33a2f06

mo khan <mo@mokhan.ca>
2026-01-18 05:25:13
refactor: tidy up the code
1 parent d4148d1
Changed files (2)
lib/elelem/agent.rb
@@ -40,33 +40,33 @@ module Elelem
 
     def turn(input)
       history << { role: "user", content: input }
-      ctx, errors = [], 0
+      ctx = []
 
       loop do
         terminal.waiting
         content, tool_calls = fetch_response(ctx)
-        terminal.newline
-        return if content.nil?
-
-        terminal.say(terminal.markdown(content)) unless content.empty?
-        ctx << { role: "assistant", content: content, tool_calls: tool_calls.empty? ? nil : tool_calls }.compact
-
+        terminal.say(terminal.markdown(content))
         break if tool_calls.empty?
 
+        ctx << { role: "assistant", content: content, tool_calls: tool_calls }.compact
         tool_calls.each do |tool_call|
-          name, args = tool_call[:name], tool_call[:arguments]
-          terminal.say "\n#{format_tool_display(name, args)}"
-          result = toolbox.run(name.to_s, args)
-          terminal.say format_tool_result(name, result)
-          ctx << { role: "tool", tool_call_id: tool_call[:id], content: result.to_json }
-          errors += 1 if result[:error]
+          ctx << { role: "tool", tool_call_id: tool_call[:id], content: process(tool_call).to_json }
         end
-
-        break if errors >= 3
       end
 
-      final_content = ctx.reverse.find { |m| m[:role] == "assistant" }&.[](:content) || ""
-      history << { role: "assistant", content: final_content }
+      history << { role: "assistant", content: summarize(ctx) }
+    end
+
+    def summarize(ctx)
+      ctx.reverse.find { |m| m[:role] == "assistant" }&.[](:content) || ""
+    end
+
+    def process(tool_call)
+      name, args = tool_call[:name], tool_call[:arguments]
+      terminal.say format_tool_display(name, args)
+      toolbox.run(name.to_s, args).tap do |result|
+        terminal.say format_tool_result(name, result)
+      end
     end
 
     def fetch_response(ctx)
@@ -86,14 +86,14 @@ module Elelem
     end
 
     def format_tool_display(name, args)
-      "+ #{name.to_s.then { _1.empty? ? "?" : _1 }}(#{args})"
+      "\n+ #{name.to_s.then { _1.empty? ? "?" : _1 }}(#{args})"
     end
 
     def format_tool_result(name, result)
       return if result[:exit_status]
 
       text = result[:content] || result[:error] || ""
-      return nil if text.strip.empty?
+      return if text.strip.empty?
 
       result[:error] ? "  ! #{text.lines.first&.strip}" : text
     end
lib/elelem/terminal.rb
@@ -13,20 +13,25 @@ module Elelem
     end
 
     def dim(text)
+      return if blank?(text)
+
       "\e[2m#{text}\e[0m"
     end
 
     def markdown(text)
+      return if blank?(text)
+
+      newline
       width = $stdout.winsize[1] rescue 80
       IO.popen([
         "bat",
         "--squeeze-blank",
         "--style=plain",
         "--paging=never",
-        "--color=always",
-        "--language",
-        "markdown",
-        "--terminal-width", width.to_s,
+        "--force-colorization",
+        "--language=markdown",
+        "--theme=auto:always",
+        "--terminal-width=#{width}",
         "-"
       ], "r+") do |io|
         io.write(text)
@@ -37,18 +42,22 @@ module Elelem
       text
     end
 
-    def print(message)
+    def print(text)
+      return if blank?(text)
+
       stop_dots
-      $stdout.print message
+      $stdout.print text
     end
 
-    def say(message)
+    def say(text)
+      return if blank?(text)
+
       stop_dots
-      $stdout.puts message
+      $stdout.puts text
     end
 
     def newline
-      say("")
+      $stdout.puts("")
     end
 
     def file(path)
@@ -72,6 +81,10 @@ module Elelem
 
     private
 
+    def blank?(text)
+      text.nil? || text.strip.empty?
+    end
+
     def stop_dots
       return unless @dots_thread