Class: SwarmSDK::Workflow::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/workflow/executor.rb

Overview

Handles workflow execution orchestration

Extracted from Workflow#execute to reduce complexity and improve maintainability. Orchestrates node execution, transformer handling, and control flow.

Examples:

executor = Executor.new(workflow)
result = executor.run("Build auth system") { |entry| puts entry }

Defined Under Namespace

Classes: ExecutionState, OutputTransformerContext

Instance Method Summary collapse

Constructor Details

#initialize(workflow) ⇒ Executor

Returns a new instance of Executor.



39
40
41
# File 'lib/swarm_sdk/workflow/executor.rb', line 39

def initialize(workflow)
  @workflow = workflow
end

Instance Method Details

#run(prompt, inherit_subscriptions: true) {|Hash| ... } ⇒ Result

Execute the workflow with a prompt

Parameters:

  • prompt (String)

    Initial prompt for the workflow

  • inherit_subscriptions (Boolean) (defaults to: true)

    Whether to inherit parent log subscriptions

Yields:

  • (Hash)

    Log entry if block given (for streaming)

Returns:

  • (Result)

    Final result from last node execution



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/swarm_sdk/workflow/executor.rb', line 49

def run(prompt, inherit_subscriptions: true, &block)
  @parent_subscriptions = capture_parent_subscriptions if inherit_subscriptions
  setup_logging(inherit_subscriptions: inherit_subscriptions, &block)
  setup_fiber_context
  @workflow.original_prompt = prompt

  state = ExecutionState.new(
    current_input: prompt,
    results: {},
    last_result: nil,
    execution_index: 0,
    logs: [],
  )

  execute_nodes(state)
ensure
  cleanup_fiber_context
  reset_logging
end