Class: Roast::Workflow::WorkflowExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/workflow/workflow_executor.rb

Overview

Handles the execution of workflow steps, including orchestration and threading

Constant Summary collapse

DEFAULT_MODEL =
"anthropic:claude-3-7-sonnet"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow, config_hash, context_path) ⇒ WorkflowExecutor

Returns a new instance of WorkflowExecutor.



15
16
17
18
19
# File 'lib/roast/workflow/workflow_executor.rb', line 15

def initialize(workflow, config_hash, context_path)
  @workflow = workflow
  @config_hash = config_hash
  @context_path = context_path
end

Instance Attribute Details

#config_hashObject (readonly)

Returns the value of attribute config_hash.



13
14
15
# File 'lib/roast/workflow/workflow_executor.rb', line 13

def config_hash
  @config_hash
end

#context_pathObject (readonly)

Returns the value of attribute context_path.



13
14
15
# File 'lib/roast/workflow/workflow_executor.rb', line 13

def context_path
  @context_path
end

#workflowObject (readonly)

Returns the value of attribute workflow.



13
14
15
# File 'lib/roast/workflow/workflow_executor.rb', line 13

def workflow
  @workflow
end

Instance Method Details

#execute_step(name) ⇒ Object



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
87
88
89
90
91
# File 'lib/roast/workflow/workflow_executor.rb', line 36

def execute_step(name)
  start_time = Time.now
  # For tests, make sure that we handle this gracefully
  resource_type = workflow.respond_to?(:resource) ? workflow.resource&.type : nil

  ActiveSupport::Notifications.instrument("roast.step.start", {
    step_name: name,
    resource_type: resource_type,
  })

  $stderr.puts "Executing: #{name} (Resource type: #{resource_type || "unknown"})"

  result = if name.starts_with?("$(")
    strip_and_execute(name).tap do |output|
      # Add the command and output to the transcript for reference in following steps
      workflow.transcript << { user: "I just executed the following command: ```\n#{name}\n```\n\nHere is the output:\n\n```\n#{output}\n```" }
      workflow.transcript << { assistant: "Noted, thank you." }
    end
  elsif name.include?("*") && (!workflow.respond_to?(:resource) || !workflow.resource)
    # Only use the glob method if we don't have a resource object yet
    # This is for backward compatibility
    glob(name)
  else
    step_object = find_and_load_step(name)
    step_result = step_object.call
    workflow.output[name] = step_result

    # Save state after each step if the workflow supports it
    save_state(name, step_result) if workflow.respond_to?(:session_name) && workflow.session_name

    step_result
  end

  execution_time = Time.now - start_time

  ActiveSupport::Notifications.instrument("roast.step.complete", {
    step_name: name,
    resource_type: resource_type,
    success: true,
    execution_time: execution_time,
    result_size: result.to_s.length,
  })

  result
rescue => e
  execution_time = Time.now - start_time

  ActiveSupport::Notifications.instrument("roast.step.error", {
    step_name: name,
    resource_type: resource_type,
    error: e.class.name,
    message: e.message,
    execution_time: execution_time,
  })
  raise
end

#execute_steps(steps) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/roast/workflow/workflow_executor.rb', line 21

def execute_steps(steps)
  steps.each do |step|
    case step
    when Hash
      execute_hash_step(step)
    when Array
      execute_parallel_steps(step)
    when String
      execute_string_step(step)
    else
      raise "Unknown step type: #{step.inspect}"
    end
  end
end