Class: Google::ADK::LoopAgent
- Defined in:
- lib/google/adk/agents/workflow_agents/loop_agent.rb
Overview
Agent that executes another agent in a loop
Constant Summary
Constants inherited from BaseAgent
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#loop_condition ⇒ Object
readonly
Returns the value of attribute loop_condition.
-
#max_iterations ⇒ Object
readonly
Returns the value of attribute max_iterations.
Attributes inherited from BaseAgent
#after_agent_callback, #before_agent_callback, #description, #name, #parent_agent, #sub_agents
Instance Method Summary collapse
-
#initialize(name:, agent:, description: nil, loop_condition: nil, max_iterations: 10, before_agent_callback: nil, after_agent_callback: nil) ⇒ LoopAgent
constructor
Initialize a loop agent.
-
#run_async(message, context: nil) {|Event| ... } ⇒ Object
Run agent in a loop.
Methods inherited from BaseAgent
#clone, #find_agent, #find_sub_agent, from_config, #run_live
Constructor Details
#initialize(name:, agent:, description: nil, loop_condition: nil, max_iterations: 10, before_agent_callback: nil, after_agent_callback: nil) ⇒ LoopAgent
Initialize a loop agent
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/google/adk/agents/workflow_agents/loop_agent.rb', line 18 def initialize(name:, agent:, description: nil, loop_condition: nil, max_iterations: 10, before_agent_callback: nil, after_agent_callback: nil) super( name: name, description: description || "Executes #{agent.name} in a loop", sub_agents: [agent], before_agent_callback: before_agent_callback, after_agent_callback: after_agent_callback ) @agent = agent @loop_condition = loop_condition @max_iterations = max_iterations end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
7 8 9 |
# File 'lib/google/adk/agents/workflow_agents/loop_agent.rb', line 7 def agent @agent end |
#loop_condition ⇒ Object (readonly)
Returns the value of attribute loop_condition.
7 8 9 |
# File 'lib/google/adk/agents/workflow_agents/loop_agent.rb', line 7 def loop_condition @loop_condition end |
#max_iterations ⇒ Object (readonly)
Returns the value of attribute max_iterations.
7 8 9 |
# File 'lib/google/adk/agents/workflow_agents/loop_agent.rb', line 7 def max_iterations @max_iterations end |
Instance Method Details
#run_async(message, context: nil) {|Event| ... } ⇒ Object
Run agent in a loop
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/google/adk/agents/workflow_agents/loop_agent.rb', line 39 def run_async(, context: nil) Enumerator.new do |yielder| invocation_id = context&.invocation_id || "loop-#{SecureRandom.uuid}" # Yield start event start_event = Event.new( invocation_id: invocation_id, author: @name, content: "Starting loop execution with agent #{@agent.name}" ) yielder << start_event current_input = iteration = 0 last_result = nil # Loop while condition is met or until max iterations while iteration < @max_iterations # Check loop condition if provided if @loop_condition && !@loop_condition.call(last_result, iteration) break end # Yield iteration event iteration_event = Event.new( invocation_id: invocation_id, author: @name, content: "Iteration #{iteration + 1}/#{@max_iterations}" ) yielder << iteration_event begin # Run the agent agent_output = nil if @agent.respond_to?(:run_async) @agent.run_async(current_input, context: context).each do |event| yielder << event # Capture last content as result agent_output = event.content if event.content end else error_event = Event.new( invocation_id: invocation_id, author: @name, content: "Agent #{@agent.name} does not implement run_async" ) yielder << error_event end # Update for next iteration last_result = agent_output || current_input current_input = last_result iteration += 1 rescue StandardError => e # Handle errors error_event = Event.new( invocation_id: invocation_id, author: @name, content: "Error in iteration #{iteration + 1}: #{e.}" ) yielder << error_event iteration += 1 end end # Yield completion event end_event = Event.new( invocation_id: invocation_id, author: @name, content: "Completed loop execution after #{iteration} iterations. Final result: #{last_result}" ) yielder << end_event end end |