Class: RubyLLM::Workflow

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

Overview

Adds workflow and step context to RubyLLM instrumentation while leaving orchestration in ordinary Ruby code.

Create workflows through RubyLLM.workflow rather than constructing this class directly:

RubyLLM.workflow("Write article", id: "article-42") do |workflow|
notes = workflow.step("Research") { ResearchAgent.new.ask(topic).content }
workflow.step("Draft") { WriterAgent.new.ask(notes).content }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, id: nil, metadata: nil, config: RubyLLM.config) ⇒ Workflow

:nodoc:



22
23
24
25
26
27
# File 'lib/ruby_llm/workflow.rb', line 22

def initialize(name, id: nil, metadata: nil, config: RubyLLM.config) # :nodoc:
  @name = normalize(name, 'name')
  @id = normalize(id || SecureRandom.uuid, 'id')
  @metadata = 
  @config = config
end

Instance Attribute Details

#idObject (readonly)

Returns the workflow identifier shared by its instrumentation events.



17
18
19
# File 'lib/ruby_llm/workflow.rb', line 17

def id
  @id
end

#nameObject (readonly)

Returns the workflow name.



20
21
22
# File 'lib/ruby_llm/workflow.rb', line 20

def name
  @name
end

Instance Method Details

#runObject

:nodoc:

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/workflow.rb', line 29

def run # :nodoc:
  raise ArgumentError, 'a workflow block is required' unless block_given?

  link_parent
  Support::Instrumentation.with_workflow(workflow_context) do
    RubyLLM.instrument('workflow.ruby_llm', config: @config) { yield self }
  end
end

#step(name, id: nil, &block) ⇒ Object

Runs a named section of Ruby code and adds its identity to every RubyLLM event emitted by the block. An ID is generated when one is not supplied. Steps can contain regular Ruby control flow and may be nested. Returns the block's result.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_llm/workflow.rb', line 42

def step(name, id: nil, &block)
  raise ArgumentError, 'a workflow step block is required' unless block

  step_context = workflow_context.merge(
    workflow_step_id: normalize(id || SecureRandom.uuid, 'step id'),
    workflow_step_name: normalize(name, 'step name')
  )
  parent_id = current_step_id
  step_context[:workflow_step_parent_id] = parent_id if parent_id

  Support::Instrumentation.with_workflow(step_context.freeze) do
    RubyLLM.instrument('workflow_step.ruby_llm', config: @config) { block.call }
  end
end