Class: Ask::Agent::Loop
- Inherits:
-
Object
- Object
- Ask::Agent::Loop
- Defined in:
- lib/ask/agent/loop.rb
Constant Summary collapse
- LOOP_DETECTION_WINDOW =
3- MAX_CONSECUTIVE_TOOL_TURNS =
6
Instance Attribute Summary collapse
-
#turn_count ⇒ Object
readonly
Returns the value of attribute turn_count.
Instance Method Summary collapse
-
#initialize(max_turns: 25) ⇒ Loop
constructor
A new instance of Loop.
- #reset! ⇒ Object
- #run_turn(chat:, message:, tools:, tool_executor:, compactor:, hooks:, event_emitter:, session_id: nil) ⇒ Object
Constructor Details
#initialize(max_turns: 25) ⇒ Loop
Returns a new instance of Loop.
11 12 13 14 15 16 17 |
# File 'lib/ask/agent/loop.rb', line 11 def initialize(max_turns: 25) @max_turns = max_turns @turn_count = 0 @recent_results = [] @loop_detected = false @consecutive_tool_turns = 0 end |
Instance Attribute Details
#turn_count ⇒ Object (readonly)
Returns the value of attribute turn_count.
9 10 11 |
# File 'lib/ask/agent/loop.rb', line 9 def turn_count @turn_count end |
Instance Method Details
#reset! ⇒ Object
88 89 90 91 92 |
# File 'lib/ask/agent/loop.rb', line 88 def reset! @turn_count = 0 @recent_results = [] @loop_detected = false end |
#run_turn(chat:, message:, tools:, tool_executor:, compactor:, hooks:, event_emitter:, session_id: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ask/agent/loop.rb', line 19 def run_turn(chat:, message:, tools:, tool_executor:, compactor:, hooks:, event_emitter:, session_id: nil) raise MaxTurnsExceeded if @turn_count >= @max_turns event_emitter.emit(Events::TurnStart.new) response = chat.ask() do |chunk| if chunk.content.to_s.strip.length > 0 event_emitter.emit(Events::TextDelta.new(content: chunk.content)) end if chunk.tool_call? chunk.tool_calls.each do |id, tc| event_emitter.emit(Events::ToolCallDelta.new( name: tc.name, arguments: tc.arguments, id: tc.id )) end end end event_emitter.emit(Events::MessageEnd.new(tool_calls: response.tool_call?)) @turn_count += 1 unless response.tool_call? @consecutive_tool_turns = 0 return response.content.to_s end @consecutive_tool_turns += 1 # Execute tools with concurrent result streaming tool_results = tool_executor.execute_parallel( response.tool_calls, tools, hooks, event_emitter, ToolAbortController.new ) do |tool_call_id, result| # Add each tool result as it completes (concurrent streaming) tc = response.tool_calls[tool_call_id] chat.(role: :tool, content: result[:message].to_s, tool_call_id: tool_call_id) if tc end # Check loop detection if loop_detected?(tool_results) raise LoopDetected, tool_results.last[:tool_name] end if @consecutive_tool_turns >= MAX_CONSECUTIVE_TOOL_TURNS summary = tool_results.map { |r| r[:message].to_s.truncate(80) }.first(2).join("; ") return "Based on my investigation: #{summary}" end event_emitter.emit(Events::TurnEnd.new(tool_results: tool_results, turn_number: @turn_count)) if compactor && compactor.should_compact? compactor.run(event_emitter: event_emitter) end raise MaxTurnsExceeded if @turn_count >= @max_turns # Recursive call — LLM processes tool results run_turn( chat: chat, message: "", tools: tools, tool_executor: tool_executor, compactor: compactor, hooks: hooks, event_emitter: event_emitter, session_id: session_id ) end |