Class: SkillBench::Agent::ReactAgent::LoopRunner
- Inherits:
-
Object
- Object
- SkillBench::Agent::ReactAgent::LoopRunner
- Defined in:
- lib/skill_bench/agent/react_agent/loop_runner.rb
Overview
Executes the ReAct loop iterations until completion or max iterations.
Class Method Summary collapse
-
.add_usage(total, usage) ⇒ Hash
Adds a single step's usage onto a running total.
-
.attach_step_number(iteration, step_count) ⇒ Hash
Attaches the step number to an iteration hash.
-
.call(initial_prompt, max_iterations, config) ⇒ Hash
Executes the loop.
-
.empty_usage ⇒ Hash
A zeroed token-usage accumulator.
-
.finalize(result, iterations_log, total_usage) ⇒ Hash
Merges the collected iterations and accumulated usage into the response.
-
.token_count(usage, key) ⇒ Integer
Reads a token count from a usage hash, tolerating string keys.
Class Method Details
.add_usage(total, usage) ⇒ Hash
Adds a single step's usage onto a running total.
84 85 86 87 88 89 90 91 |
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 84 def self.add_usage(total, usage) usage ||= {} { prompt_tokens: total[:prompt_tokens] + token_count(usage, :prompt_tokens), completion_tokens: total[:completion_tokens] + token_count(usage, :completion_tokens), total_tokens: total[:total_tokens] + token_count(usage, :total_tokens) } end |
.attach_step_number(iteration, step_count) ⇒ Hash
Attaches the step number to an iteration hash.
57 58 59 |
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 57 def self.attach_step_number(iteration, step_count) iteration.merge(step_number: step_count) end |
.call(initial_prompt, max_iterations, config) ⇒ Hash
Executes the loop.
16 17 18 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 |
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 16 def self.call(initial_prompt, max_iterations, config) = [{ role: 'user', content: initial_prompt }] iterations_log = [] total_usage = empty_usage step_count = 0 while step_count < max_iterations step_count += 1 step_result = Step.call(, config) iteration = step_result[:iteration] iterations_log << attach_step_number(iteration, step_count) if iteration total_usage = add_usage(total_usage, step_result[:usage]) unless step_result[:continue] final_result = step_result[:result] || { success: false, response: { error: { message: 'Step returned no result' } } } return finalize(final_result, iterations_log, total_usage) end = step_result[:messages] end finalize( { success: false, response: { error: { message: Agent::ReactAgent::MAX_ITERATIONS_REACHED } } }, iterations_log, total_usage ) rescue StandardError => e SkillBench::ErrorLogger.log_error(e, 'ReactAgent Error') finalize( { success: false, response: { error: { message: e. } } }, iterations_log, total_usage ) end |
.empty_usage ⇒ Hash
A zeroed token-usage accumulator.
75 76 77 |
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 75 def self.empty_usage { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } end |
.finalize(result, iterations_log, total_usage) ⇒ Hash
Merges the collected iterations and accumulated usage into the response.
67 68 69 70 |
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 67 def self.finalize(result, iterations_log, total_usage) response = result[:response] || {} result.merge(response: response.merge(iterations: iterations_log, usage: total_usage)) end |
.token_count(usage, key) ⇒ Integer
Reads a token count from a usage hash, tolerating string keys.
98 99 100 |
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 98 def self.token_count(usage, key) (usage[key] || usage[key.to_s] || 0).to_i end |