Commit 9febc6d

mo khan <mo@mokhan.ca>
2025-08-10 18:47:30
refactor: extract the idle state
1 parent 580bd42
Changed files (2)
lib/elelem/agent.rb
@@ -1,6 +1,21 @@
 # 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)
+    end
+  end
+
   class Agent
     attr_reader :configuration, :conversation, :tools
 
@@ -8,15 +23,27 @@ module Elelem
       @configuration = configuration
       @conversation = configuration.conversation
       @tools = configuration.tools
+      @current_state = Idle.new(configuration)
+    end
+
+    def prompt(message)
+      print(message)
+      $stdin.gets&.chomp
+    end
+
+    def quit
+      exit
     end
 
     def repl
       loop do
-        print "\n> "
-        input = $stdin.gets&.chomp
-        break if input.nil? || input.empty? || input == "exit"
+        @current_state.run(self)
+
+        # print "\n> "
+        # input = $stdin.gets&.chomp
+        # break if input.nil? || input.empty? || input == "exit"
 
-        conversation.add(role: "user", content: input)
+        # conversation.add(role: "user", content: input)
 
         done = false
         loop do
lib/elelem/conversation.rb
@@ -9,7 +9,7 @@ module Elelem
       Reasoning: high
     SYS
 
-    ROLES = %w[system user tool].freeze
+    ROLES = [:system, :user, :tool].freeze
 
     def initialize(items = [{ role: "system", content: SYSTEM_MESSAGE }])
       @items = items
@@ -20,8 +20,8 @@ module Elelem
     end
 
     # :TODO truncate conversation history
-    def add(role: "user", content: "")
-      raise "unknown role: #{role}" unless ROLES.include?(role)
+    def add(role: user, content: "")
+      raise "unknown role: #{role}" unless ROLES.include?(role.to_sym)
 
       @items << { role: role, content: content }
     end