Class: Ragents::Orchestrator
- Inherits:
-
Object
- Object
- Ragents::Orchestrator
- Defined in:
- lib/ragents/orchestrator.rb
Overview
Orchestrator for coordinating multiple agents. Supports parallel execution, sequential workflows, and agent routing.
Instance Attribute Summary collapse
-
#agents ⇒ Object
readonly
Returns the value of attribute agents.
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
-
#fetch_context(name) ⇒ Object
Retrieve a stored context.
-
#initialize(provider: nil, **options) ⇒ Orchestrator
constructor
A new instance of Orchestrator.
-
#parallel(*tasks) ⇒ Array<Context>
Run multiple agents in parallel using Ractors.
-
#register(name, agent_class, **options) ⇒ Object
Register an agent class with a name.
-
#run(name, input: nil, context: nil, **options) ⇒ Context
Run a single agent.
-
#store_context(name, context) ⇒ Object
Store a context for later retrieval.
-
#supervised(name, max_restarts: 3, restart_delay: 1, **options) ⇒ Object
Run agents with supervisor pattern.
-
#workflow {|Workflow| ... } ⇒ Context
Execute a sequential workflow.
Constructor Details
#initialize(provider: nil, **options) ⇒ Orchestrator
Returns a new instance of Orchestrator.
29 30 31 32 33 34 |
# File 'lib/ragents/orchestrator.rb', line 29 def initialize(provider: nil, **) @provider = provider @agents = {} @contexts = {} @options = end |
Instance Attribute Details
#agents ⇒ Object (readonly)
Returns the value of attribute agents.
27 28 29 |
# File 'lib/ragents/orchestrator.rb', line 27 def agents @agents end |
#contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
27 28 29 |
# File 'lib/ragents/orchestrator.rb', line 27 def contexts @contexts end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
27 28 29 |
# File 'lib/ragents/orchestrator.rb', line 27 def provider @provider end |
Instance Method Details
#fetch_context(name) ⇒ Object
Retrieve a stored context
115 116 117 |
# File 'lib/ragents/orchestrator.rb', line 115 def fetch_context(name) @contexts[name.to_sym] end |
#parallel(*tasks) ⇒ Array<Context>
Run multiple agents in parallel using Ractors
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ragents/orchestrator.rb', line 58 def parallel(*tasks) ractors = tasks.map do |(name, opts)| opts ||= {} agent_config = @agents[name.to_sym] raise ArgumentError, "Unknown agent: #{name}" unless agent_config agent_class = agent_config[:class] merged_opts = agent_config[:options].merge(opts) context = merged_opts.delete(:context) input = merged_opts.delete(:input) agent_class.run_async( provider: @provider, context: context, input: input, **merged_opts ) end # Collect results from all Ractors (Ruby 4.x API) ractors.map(&:value) end |
#register(name, agent_class, **options) ⇒ Object
Register an agent class with a name
40 41 42 43 |
# File 'lib/ragents/orchestrator.rb', line 40 def register(name, agent_class, **) @agents[name.to_sym] = { class: agent_class, options: } self end |
#run(name, input: nil, context: nil, **options) ⇒ Context
Run a single agent
50 51 52 53 |
# File 'lib/ragents/orchestrator.rb', line 50 def run(name, input: nil, context: nil, **) agent = build_agent(name, context: context, **) agent.run(input: input) end |
#store_context(name, context) ⇒ Object
Store a context for later retrieval
110 111 112 |
# File 'lib/ragents/orchestrator.rb', line 110 def store_context(name, context) @contexts[name.to_sym] = context end |
#supervised(name, max_restarts: 3, restart_delay: 1, **options) ⇒ Object
Run agents with supervisor pattern
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ragents/orchestrator.rb', line 94 def supervised(name, max_restarts: 3, restart_delay: 1, **) restarts = 0 begin run(name, **) rescue StandardError => e restarts += 1 if restarts <= max_restarts sleep(restart_delay) retry end raise SupervisorError, "Agent #{name} failed after #{max_restarts} restarts: #{e.}" end end |