Class: Phronomy::MultiAgent::Orchestrator

Inherits:
Agent::Base
  • Object
show all
Defined in:
lib/phronomy/multi_agent/orchestrator.rb

Overview

Base class for orchestrator agents that coordinate multiple subagents.

Instance Attribute Summary

Attributes inherited from Agent::Base

#agent_id, #persistence

Attributes included from Agent::Concerns::BeforeLLMInput

#before_llm_input

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Agent::Base

#__replace_root, #_add_handoff_tool, #_handoff_tools, #add_knowledge, agent_definition, #agent_root, approve, approve_async, cache_instructions, #clear_knowledge!, #clear_transcript!, #close!, context_window, create, #initialize, instructions, #journal_projection, load, max_iterations, max_output_tokens, model, #on_tool_approval_required, provider, #purge!, #reset_context!, temperature, tool_aliases, #tool_approval_policy, tools, #transcript

Methods included from Agent::Concerns::BeforeLLMInput

included

Methods included from Agent::Concerns::Filterable

#add_input_filter, #add_output_filter, #add_tool_result_filter, included

Methods included from Runnable

#batch, #invoke, #stream, #trace

Methods included from Agent::AsyncEventApi

#approve, #approve_async, #invoke, #invoke_async, #stream, #stream_async

Constructor Details

This class inherits a constructor from Phronomy::Agent::Base

Class Method Details

._subagent_tool_classesObject



60
61
62
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 60

def self._subagent_tool_classes
  @_subagent_tool_classes || []
end

.registered_subagentsObject



65
66
67
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 65

def self.registered_subagents
  @registered_subagents ||= {}
end

.subagent(name, agent_class, on_error: :raise, inherit_knowledge: true) ⇒ Object



10
11
12
13
14
15
16
17
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
54
55
56
57
58
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 10

def self.subagent(name, agent_class, on_error: :raise, inherit_knowledge: true)
  tool_class = Class.new(Phronomy::Agent::Context::Capability::Base) do
    tool_name "dispatch_to_#{name}"
    description "Dispatch work to the #{name} subagent (#{agent_class.name})"
    param :input, type: :string, desc: "The task or question for the subagent"

    attr_writer :_orchestrator_context

    define_method(:execute) do |input:|
      ctx = @_orchestrator_context || {}
      parent_ic = ctx[:invocation_context]
      task_config = ctx[:config] || {}

      if parent_ic && !task_config[:invocation_context]
        child_ic = parent_ic.merge(parent_task_id: parent_ic.task_id)
        task_config = task_config.merge(invocation_context: child_ic)
      end

      agent = agent_class.new
      if inherit_knowledge
        Array(ctx[:knowledge]).each do |entry|
          agent.add_knowledge(
            entry.fetch(:content),
            metadata: entry.fetch(:metadata, {})
          )
        end
      end

      result = agent.invoke_async(
        input,
        thread_id: ctx[:thread_id] || parent_ic&.thread_id,
        config: task_config
      ).wait_result
      result[:output]
    rescue
      raise if on_error == :raise
      nil
    end
  end

  @_subagent_tool_classes = (@_subagent_tool_classes || []) + [tool_class]
  @tools = (@tools || []) + [tool_class]
  @tool_aliases ||= {}
  registered_subagents[name] = {
    agent_class: agent_class,
    on_error: on_error,
    inherit_knowledge: inherit_knowledge
  }
end

Instance Method Details

#dispatch_parallel(*tasks, max_concurrency: nil, on_error: :raise, timeout: nil, cancellation_token: nil, invocation_context: nil, inherit_knowledge: true) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 70

def dispatch_parallel(
  *tasks,
  max_concurrency: nil,
  on_error: :raise,
  timeout: nil,
  cancellation_token: nil,
  invocation_context: nil,
  inherit_knowledge: true
)
  unless %i[raise skip].include?(on_error)
    raise ArgumentError, "unknown on_error: #{on_error.inspect}"
  end
  if max_concurrency && !(max_concurrency.is_a?(Integer) && max_concurrency.positive?)
    raise ArgumentError, "max_concurrency must be a positive Integer"
  end

  bounded_map(
    tasks,
    max_concurrency: max_concurrency,
    on_error: on_error,
    timeout: timeout,
    cancellation_token: cancellation_token,
    invocation_context: invocation_context,
    inherit_knowledge: inherit_knowledge
  )
end

#fan_out(agent:, inputs:, config: {}, thread_id: nil, max_concurrency: nil, on_error: :raise, timeout: nil, cancellation_token: nil, invocation_context: nil, inherit_knowledge: true) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 98

def fan_out(
  agent:,
  inputs:,
  config: {},
  thread_id: nil,
  max_concurrency: nil,
  on_error: :raise,
  timeout: nil,
  cancellation_token: nil,
  invocation_context: nil,
  inherit_knowledge: true
)
  dispatch_parallel(
    *inputs.map do |input|
      {agent: agent, input: input, config: config, thread_id: thread_id}
    end,
    max_concurrency: max_concurrency,
    on_error: on_error,
    timeout: timeout,
    cancellation_token: cancellation_token,
    invocation_context: invocation_context,
    inherit_knowledge: inherit_knowledge
  )
end

#subagent(agent_class, input, config: nil, thread_id: nil, inherit_knowledge: true) ⇒ Object

Programmatic single-subagent dispatch. Context propagation is explicit: pass invocation_context in config when this call must inherit a parent. Active parent Knowledge is inherited by default; pass inherit_knowledge: false to create an isolated subagent.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 128

def subagent(
  agent_class,
  input,
  config: nil,
  thread_id: nil,
  inherit_knowledge: true
)
  build_subagent(
    agent_class,
    inherit_knowledge: inherit_knowledge
  ).invoke_async(
    input,
    config: config || {},
    thread_id: thread_id
  ).wait_result
end