Commit 3ac1303

mo khan <mo@mokhan.ca>
2025-08-12 18:05:17
refactor: move states to a separate file
1 parent e67b0d6
Changed files (3)
lib/elelem/agent.rb
@@ -1,61 +1,6 @@
 # frozen_string_literal: true
 
 module Elelem
-  class Idle
-    attr_reader :configuration
-
-    def initialize(configuration)
-      @configuration = configuration
-    end
-
-    def run(agent)
-      input = agent.prompt("\n> ")
-      agent.quit if input.nil? || input.empty? || input == "exit"
-
-      configuration.conversation.add(role: "user", content: input)
-      agent.transition_to(ProcessingInput.new(configuration))
-    end
-  end
-
-  class ProcessingInput
-    attr_reader :configuration, :conversation, :tools
-
-    def initialize(configuration)
-      @configuration = configuration
-      @conversation = configuration.conversation
-      @tools = configuration.tools
-    end
-
-    def run(agent)
-      done = false
-
-      loop do
-        configuration.api.chat(conversation.history, tools) do |chunk|
-          response = JSON.parse(chunk)
-          done = response["done"]
-          message = response["message"] || {}
-
-          if message["thinking"]
-            configuration.tui.say(message["thinking"], colour: :gray, newline: false)
-          elsif message["tool_calls"]&.any?
-            message["tool_calls"].each do |t|
-              conversation.add(role: "tool", content: tools.execute(t))
-            end
-            done = false
-          elsif message["content"].to_s.strip
-            configuration.tui.say(message["content"], colour: :default, newline: false)
-          else
-            raise chunk.inspect
-          end
-        end
-
-        break if done
-      end
-
-      agent.transition_to(Idle.new(configuration))
-    end
-  end
-
   class Agent
     attr_reader :configuration, :current_state
 
@@ -72,6 +17,10 @@ module Elelem
       configuration.tui.prompt(message)
     end
 
+    def say(message, colour: :default, newline: false)
+      configuration.tui.say(message, colour: colour, newline: newline)
+    end
+
     def quit
       exit
     end
lib/elelem/state.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+module Elelem
+  class Idle
+    attr_reader :configuration
+
+    def initialize(configuration)
+      @configuration = configuration
+    end
+
+    def run(agent)
+      input = agent.prompt("\n> ")
+      agent.quit if input.nil? || input.empty? || input == "exit"
+
+      configuration.conversation.add(role: "user", content: input)
+      agent.transition_to(ProcessingInput.new(configuration))
+    end
+  end
+
+  class ProcessingInput
+    attr_reader :configuration, :conversation, :tools
+
+    def initialize(configuration)
+      @configuration = configuration
+      @conversation = configuration.conversation
+      @tools = configuration.tools
+    end
+
+    def run(agent)
+      done = false
+
+      loop do
+        configuration.api.chat(conversation.history, tools) do |chunk|
+          response = JSON.parse(chunk)
+          done = response["done"]
+          message = response["message"] || {}
+
+          if message["thinking"]
+            agent.say(message["thinking"], colour: :gray, newline: false)
+          elsif message["tool_calls"]&.any?
+            message["tool_calls"].each do |t|
+              conversation.add(role: "tool", content: tools.execute(t))
+            end
+            done = false
+          elsif message["content"].to_s.strip
+            agent.say(message["content"], colour: :default, newline: false)
+          else
+            raise chunk.inspect
+          end
+        end
+
+        break if done
+      end
+
+      agent.transition_to(Idle.new(configuration))
+    end
+  end
+end
lib/elelem.rb
@@ -12,6 +12,7 @@ require_relative "elelem/api"
 require_relative "elelem/application"
 require_relative "elelem/configuration"
 require_relative "elelem/conversation"
+require_relative "elelem/state"
 require_relative "elelem/tools"
 require_relative "elelem/tui"
 require_relative "elelem/version"