Commit ce93b50

mo khan <mo@mokhan.ca>
2025-08-12 22:36:30
fix: break out of infinite loop
1 parent 1c2f38e
Changed files (2)
lib/elelem/conversation.rb
@@ -20,8 +20,9 @@ module Elelem
     end
 
     # :TODO truncate conversation history
-    def add(role: user, content: "")
-      raise "unknown role: #{role}" unless ROLES.include?(role.to_sym)
+    def add(role: :user, content: "")
+      role = role.to_sym
+      raise "unknown role: #{role}" unless ROLES.include?(role)
 
       @items.push({ role: role, content: content }) unless content.empty?
     end
lib/elelem/state.rb
@@ -7,7 +7,7 @@ module Elelem
       input = agent.prompt("\n> ")
       agent.quit if input.nil? || input.empty? || input == "exit"
 
-      agent.conversation.add(role: "user", content: input)
+      agent.conversation.add(role: :user, content: input)
       agent.transition_to(Working.new)
     end
   end
@@ -36,7 +36,7 @@ module Elelem
         elsif message["content"] && !message["content"].empty?
           state = Talking.new(agent)
         else
-          raise message.inspect
+          state = nil
         end
 
         state&.process(message)
@@ -59,7 +59,7 @@ module Elelem
       def process(message)
         if message["tool_calls"]&.any?
           message["tool_calls"].each do |tool_call|
-            agent.conversation.add(role: "tool", content: agent.execute(tool_call))
+            agent.conversation.add(role: :tool, content: agent.execute(tool_call))
           end
         end
 
@@ -95,7 +95,8 @@ module Elelem
           state = state.process(message)
         end
 
-        break if done
+        break if state.nil?
+        break if done && agent.conversation.history.last[:role] != :tool
       end
 
       agent.transition_to(Idle.new)