Class: Phronomy::MultiAgent::Orchestrator
Overview
Base class for orchestrator agents that coordinate multiple subagents.
Instance Attribute Summary
Attributes inherited from Agent::Base
#agent_id, #persistence
#before_llm_input
Class Method Summary
collapse
Instance Method Summary
collapse
-
#dispatch_parallel(*tasks, max_concurrency: nil, on_error: :raise, timeout: nil, cancellation_token: nil, invocation_context: nil, inherit_knowledge: true) ⇒ Object
-
#dispatch_parallel_async(*tasks, max_concurrency: nil, on_error: :raise, timeout: nil, cancellation_token: nil, invocation_context: nil, inherit_knowledge: true) ⇒ Object
-
#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
-
#fan_out_async(agent:, inputs:, config: {}, thread_id: nil, max_concurrency: nil, on_error: :raise, timeout: nil, cancellation_token: nil, invocation_context: nil, inherit_knowledge: true) ⇒ Object
-
#subagent(agent_class, input, config: nil, thread_id: nil, inherit_knowledge: true) ⇒ Object
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
included
#add_input_filter, #add_output_filter, #add_tool_result_filter, included
Methods included from Runnable
#batch, #invoke, #stream, #trace
#approve, #approve_async, #invoke, #invoke_async, #stream, #stream_async
Class Method Details
87
88
89
|
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 87
def self._subagent_tool_classes
@_subagent_tool_classes || []
end
|
.registered_subagents ⇒ Object
91
92
93
|
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 91
def self.registered_subagents
@registered_subagents ||= {}
end
|
.subagent(name, agent_class, on_error: :raise, inherit_knowledge: true) ⇒ Object
9
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
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
|
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 9
def self.subagent(name, agent_class, on_error: :raise, inherit_knowledge: true)
tool_class = Class.new(Phronomy::Tools::Agent) do
tool_name "dispatch_to_#{name}"
description "Dispatch work to the #{name} subagent (#{agent_class.name})"
attr_writer :_orchestrator_context
define_method(:execute) do |input:, cancellation_token: nil|
execute_async(
input: input,
cancellation_token: cancellation_token,
config: {}
).wait_result
end
define_method(:execute_async) do |input:, cancellation_token: nil, config: {}|
ctx = @_orchestrator_context || {}
parent_ic = ctx[:invocation_context]
task_config = (ctx[:config] || {}).merge(config || {})
if cancellation_token && !task_config[:cancellation_token]
task_config = task_config.merge(cancellation_token: cancellation_token)
end
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
source = agent.invoke_async(
input,
thread_id: ctx[:thread_id] || parent_ic&.thread_id,
config: task_config
)
result_task = Phronomy::Task.deferred(
name: "subagent-tool-#{name}"
)
source.on_complete do |result, error|
if error
(on_error == :raise) ? result_task.fail(error) : result_task.complete(nil)
else
result_task.complete(result[:output])
end
end
result_task
rescue => error
result_task ||= Phronomy::Task.deferred(
name: "subagent-tool-#{name}"
)
(on_error == :raise) ? result_task.fail(error) : result_task.complete(nil)
result_task
end
private :execute_async
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 95
def dispatch_parallel(
*tasks,
max_concurrency: nil,
on_error: :raise,
timeout: nil,
cancellation_token: nil,
invocation_context: nil,
inherit_knowledge: true
)
if Phronomy::Runtime.in_event_loop_context?
raise Phronomy::EventLoopReentrancyError,
"dispatch_parallel cannot block the EventLoop; use dispatch_parallel_async"
end
dispatch_parallel_async(
*tasks,
max_concurrency: max_concurrency,
on_error: on_error,
timeout: timeout,
cancellation_token: cancellation_token,
invocation_context: invocation_context,
inherit_knowledge: inherit_knowledge
).wait_result
end
|
#dispatch_parallel_async(*tasks, max_concurrency: nil, on_error: :raise, timeout: nil, cancellation_token: nil, invocation_context: nil, inherit_knowledge: true) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 119
def dispatch_parallel_async(
*tasks,
max_concurrency: nil,
on_error: :raise,
timeout: nil,
cancellation_token: nil,
invocation_context: nil,
inherit_knowledge: true
)
validate_parallel_options!(tasks, max_concurrency, on_error)
return Phronomy::Task.deferred(name: "fan-out-empty").tap { |task| task.complete([]) } if tasks.empty?
children = build_fan_out_children(
tasks,
cancellation_token: cancellation_token,
invocation_context: invocation_context,
inherit_knowledge: inherit_knowledge
)
invocation = FanOutInvocation.new(
children: children,
max_concurrency: max_concurrency || children.length,
on_error: on_error
)
effective_token = cancellation_token || invocation_context&.cancellation_token
FanOutSessionBuilder.start(
invocation: invocation,
timeout: timeout,
cancellation_token: effective_token
)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 150
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
|
#fan_out_async(agent:, inputs:, config: {}, thread_id: nil, max_concurrency: nil, on_error: :raise, timeout: nil, cancellation_token: nil, invocation_context: nil, inherit_knowledge: true) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 175
def fan_out_async(
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_async(
*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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/phronomy/multi_agent/orchestrator.rb', line 200
def subagent(
agent_class,
input,
config: nil,
thread_id: nil,
inherit_knowledge: true
)
if Phronomy::Runtime.in_event_loop_context?
raise Phronomy::EventLoopReentrancyError,
"subagent cannot block the EventLoop; use the async Agent API"
end
build_subagent(
agent_class,
inherit_knowledge: inherit_knowledge
).invoke_async(
input,
config: config || {},
thread_id: thread_id
).wait_result
end
|