Class: SkillBench::Agent::ReactAgent::LoopRunner

Inherits:
Object
  • Object
show all
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

Class Method Details

.attach_step_number(iteration, step_count) ⇒ Hash

Attaches the step number to an iteration hash.

Parameters:

  • iteration (Hash)

    The iteration metadata from a Step.

  • step_count (Integer)

    The current step number.

Returns:

  • (Hash)

    The iteration with :step_number added.



53
54
55
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 53

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.

Parameters:

  • initial_prompt (String)

    The user task the agent must complete.

  • max_iterations (Integer)

    The maximum allowed steps before aborting.

  • config (Hash)

    The configuration for the Step execution.

Returns:

  • (Hash)

    A result hash indicating success or failure.



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
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 16

def self.call(initial_prompt, max_iterations, config)
  messages = [{ role: 'user', content: initial_prompt }]
  iterations_log = []
  step_count = 0

  while step_count < max_iterations
    step_count += 1

    step_result = Step.call(messages, config)
    iteration = step_result[:iteration]
    iterations_log << attach_step_number(iteration, step_count) if iteration

    unless step_result[:continue]
      final_result = step_result[:result] || { success: false, response: { error: { message: 'Step returned no result' } } }
      return merge_iterations(final_result, iterations_log)
    end

    messages = step_result[:messages]
  end

  merge_iterations(
    { success: false, response: { error: { message: Agent::ReactAgent::MAX_ITERATIONS_REACHED } } },
    iterations_log
  )
rescue StandardError => e
  SkillBench::ErrorLogger.log_error(e, 'ReactAgent Error')
  merge_iterations(
    { success: false, response: { error: { message: e.message } } },
    iterations_log
  )
end

.merge_iterations(result, iterations_log) ⇒ Hash

Merges the collected iterations into the result response.

Parameters:

  • result (Hash)

    The final result hash from the loop.

  • iterations_log (Array<Hash>)

    Collected iteration metadata.

Returns:

  • (Hash)

    The result with :iterations injected into :response.



62
63
64
65
# File 'lib/skill_bench/agent/react_agent/loop_runner.rb', line 62

def self.merge_iterations(result, iterations_log)
  response = result[:response] || {}
  result.merge(response: response.merge(iterations: iterations_log))
end