Class: Roast::Workflow::RepeatStep

Inherits:
BaseIterationStep show all
Defined in:
lib/roast/workflow/repeat_step.rb

Overview

Executes steps repeatedly until a condition is met or max_iterations is reached

Constant Summary

Constants inherited from BaseIterationStep

BaseIterationStep::DEFAULT_MAX_ITERATIONS

Instance Attribute Summary collapse

Attributes inherited from BaseIterationStep

#config_hash, #steps

Attributes inherited from BaseStep

#available_tools, #coerce_to, #context_path, #json, #model, #name, #params, #print_response, #resource, #workflow

Instance Method Summary collapse

Methods included from ExpressionUtils

#bash_command?, #extract_command, #extract_expression, #ruby_expression?

Constructor Details

#initialize(workflow, steps:, until_condition:, max_iterations: DEFAULT_MAX_ITERATIONS, **kwargs) ⇒ RepeatStep

Returns a new instance of RepeatStep.



9
10
11
12
13
14
15
16
# File 'lib/roast/workflow/repeat_step.rb', line 9

def initialize(workflow, steps:, until_condition:, max_iterations: DEFAULT_MAX_ITERATIONS, **kwargs)
  super(workflow, steps: steps, **kwargs)
  @until_condition = until_condition
  @max_iterations = max_iterations.to_i

  # Ensure max_iterations is at least 1
  @max_iterations = 1 if @max_iterations < 1
end

Instance Attribute Details

#max_iterationsObject (readonly)

Returns the value of attribute max_iterations.



7
8
9
# File 'lib/roast/workflow/repeat_step.rb', line 7

def max_iterations
  @max_iterations
end

#until_conditionObject (readonly)

Returns the value of attribute until_condition.



7
8
9
# File 'lib/roast/workflow/repeat_step.rb', line 7

def until_condition
  @until_condition
end

Instance Method Details

#callObject



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
51
52
53
# File 'lib/roast/workflow/repeat_step.rb', line 18

def call
  iteration = 0
  results = []

  $stderr.puts "Starting repeat loop with max_iterations: #{@max_iterations}"

  begin
    # Loop until condition is met or max_iterations is reached
    # Process the until_condition based on its type with configured coercion
    until process_iteration_input(@until_condition, workflow, coerce_to: @coerce_to) || (iteration >= @max_iterations)
      $stderr.puts "Repeat loop iteration #{iteration + 1}"

      # Execute the nested steps
      step_results = execute_nested_steps(@steps, workflow)
      results << step_results

      # Increment iteration counter
      iteration += 1

      # Save state after each iteration if the workflow supports it
      save_iteration_state(iteration) if workflow.respond_to?(:session_name) && workflow.session_name
    end

    if iteration >= @max_iterations
      $stderr.puts "Repeat loop reached maximum iterations (#{@max_iterations})"
    else
      $stderr.puts "Repeat loop condition satisfied after #{iteration} iterations"
    end

    # Return the results of all iterations
    results
  rescue => e
    $stderr.puts "Error in repeat loop: #{e.message}"
    raise
  end
end