@simulacra-ai/orchestration 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +706 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +342 -0
- package/dist/index.d.ts +342 -8
- package/dist/index.js +669 -7
- package/dist/index.js.map +1 -1
- package/package.json +11 -5
- package/dist/background-agent-pool.d.ts +0 -59
- package/dist/background-agent-pool.d.ts.map +0 -1
- package/dist/background-agent-pool.js +0 -107
- package/dist/background-agent-pool.js.map +0 -1
- package/dist/background-agent.d.ts +0 -59
- package/dist/background-agent.d.ts.map +0 -1
- package/dist/background-agent.js +0 -143
- package/dist/background-agent.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/orchestrator.d.ts +0 -48
- package/dist/orchestrator.d.ts.map +0 -1
- package/dist/orchestrator.js +0 -134
- package/dist/orchestrator.js.map +0 -1
- package/dist/parallel-agent.d.ts +0 -17
- package/dist/parallel-agent.d.ts.map +0 -1
- package/dist/parallel-agent.js +0 -33
- package/dist/parallel-agent.js.map +0 -1
- package/dist/subagent.d.ts +0 -19
- package/dist/subagent.d.ts.map +0 -1
- package/dist/subagent.js +0 -20
- package/dist/subagent.js.map +0 -1
- package/dist/tools/background-task-pool.d.ts +0 -21
- package/dist/tools/background-task-pool.d.ts.map +0 -1
- package/dist/tools/background-task-pool.js +0 -133
- package/dist/tools/background-task-pool.js.map +0 -1
- package/dist/tools/index.d.ts +0 -9
- package/dist/tools/index.d.ts.map +0 -1
- package/dist/tools/index.js +0 -15
- package/dist/tools/index.js.map +0 -1
- package/dist/tools/parallel-agent-task.d.ts +0 -21
- package/dist/tools/parallel-agent-task.d.ts.map +0 -1
- package/dist/tools/parallel-agent-task.js +0 -75
- package/dist/tools/parallel-agent-task.js.map +0 -1
- package/dist/tools/subagent-task.d.ts +0 -19
- package/dist/tools/subagent-task.d.ts.map +0 -1
- package/dist/tools/subagent-task.js +0 -65
- package/dist/tools/subagent-task.js.map +0 -1
- package/dist/tools/utils.d.ts +0 -8
- package/dist/tools/utils.d.ts.map +0 -1
- package/dist/tools/utils.js +0 -25
- package/dist/tools/utils.js.map +0 -1
- package/dist/types.d.ts +0 -90
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
BackgroundAgentPool: () => BackgroundAgentPool,
|
|
24
|
+
BackgroundOrchestrator: () => BackgroundOrchestrator,
|
|
25
|
+
BackgroundTaskPool: () => BackgroundTaskPool,
|
|
26
|
+
ORCHESTRATION_DEPTH_KEY: () => ORCHESTRATION_DEPTH_KEY,
|
|
27
|
+
OrchestrationToolkit: () => OrchestrationToolkit,
|
|
28
|
+
Orchestrator: () => Orchestrator,
|
|
29
|
+
ParallelAgentTask: () => ParallelAgentTask,
|
|
30
|
+
ParallelOrchestrator: () => ParallelOrchestrator,
|
|
31
|
+
SubagentOrchestrator: () => SubagentOrchestrator,
|
|
32
|
+
SubagentTask: () => SubagentTask
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(index_exports);
|
|
35
|
+
|
|
36
|
+
// src/orchestrator.ts
|
|
37
|
+
var import_core = require("@simulacra-ai/core");
|
|
38
|
+
var ORCHESTRATION_TOOL_NAMES = /* @__PURE__ */ new Set(["subagent", "background", "parallel"]);
|
|
39
|
+
var ORCHESTRATION_DEPTH_KEY = "__orchestration_depth";
|
|
40
|
+
function strip_orchestration_tools(toolkit) {
|
|
41
|
+
return toolkit.filter((t) => !ORCHESTRATION_TOOL_NAMES.has(t.get_definition().name));
|
|
42
|
+
}
|
|
43
|
+
var Orchestrator = class {
|
|
44
|
+
#manager;
|
|
45
|
+
#workflow;
|
|
46
|
+
#recursive_depth;
|
|
47
|
+
/**
|
|
48
|
+
* @param source - A `WorkflowManager` or `Workflow` to spawn children from.
|
|
49
|
+
* @param options.recursive_depth - How many levels of recursive orchestration to allow. `0` (default) strips orchestration tools from children, `-1` allows unlimited nesting.
|
|
50
|
+
*/
|
|
51
|
+
constructor(source, { recursive_depth = 0 } = {}) {
|
|
52
|
+
if (!Number.isInteger(recursive_depth) || recursive_depth < -1) {
|
|
53
|
+
throw new Error("invalid value for recursive_depth");
|
|
54
|
+
}
|
|
55
|
+
if (source instanceof import_core.WorkflowManager) {
|
|
56
|
+
this.#manager = source;
|
|
57
|
+
} else {
|
|
58
|
+
this.#workflow = source;
|
|
59
|
+
}
|
|
60
|
+
this.#recursive_depth = recursive_depth;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The conversation associated with the orchestrator.
|
|
64
|
+
*/
|
|
65
|
+
get conversation() {
|
|
66
|
+
if (this.#manager) {
|
|
67
|
+
return this.#manager.conversation;
|
|
68
|
+
}
|
|
69
|
+
if (this.#workflow) {
|
|
70
|
+
return this.#workflow.conversation;
|
|
71
|
+
}
|
|
72
|
+
throw new Error("no source");
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The parent workflow, if available. Used to establish parent-child workflow relationships.
|
|
76
|
+
*/
|
|
77
|
+
get parent_workflow() {
|
|
78
|
+
if (this.#manager) {
|
|
79
|
+
return this.#manager.current_workflow;
|
|
80
|
+
}
|
|
81
|
+
return this.#workflow;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Spawn a child agent with its own conversation and workflow.
|
|
85
|
+
*
|
|
86
|
+
* Creates a child conversation, assigns tools (stripping orchestration
|
|
87
|
+
* tools when depth is exhausted), and starts the workflow. Returns a
|
|
88
|
+
* handle for awaiting, inspecting, or cancelling the child.
|
|
89
|
+
*
|
|
90
|
+
* @param prompt - The instruction to send to the child agent.
|
|
91
|
+
* @param options - Configuration for the child agent (system prompt, tools, session forking, custom ID).
|
|
92
|
+
*/
|
|
93
|
+
spawn(prompt, options) {
|
|
94
|
+
const conversation = this.conversation;
|
|
95
|
+
const child = conversation.spawn_child(
|
|
96
|
+
options?.fork_session,
|
|
97
|
+
options?.id,
|
|
98
|
+
options?.system ?? conversation.system
|
|
99
|
+
);
|
|
100
|
+
const toolkit = options?.toolkit ?? [...conversation.toolkit];
|
|
101
|
+
child.toolkit = this.#recursive_depth === 0 ? strip_orchestration_tools(toolkit) : toolkit;
|
|
102
|
+
const next_depth = this.#recursive_depth === -1 ? -1 : Math.max(0, this.#recursive_depth - 1);
|
|
103
|
+
const child_context = { [ORCHESTRATION_DEPTH_KEY]: next_depth };
|
|
104
|
+
const parent = this.parent_workflow;
|
|
105
|
+
const child_workflow = parent && parent.state !== "disposed" ? parent.spawn_child(child, options?.id, child_context) : new import_core.Workflow(child, { context_data: child_context });
|
|
106
|
+
let settled = false;
|
|
107
|
+
const promise = new Promise((resolve) => {
|
|
108
|
+
child_workflow.once("workflow_end", (event) => {
|
|
109
|
+
settled = true;
|
|
110
|
+
resolve({
|
|
111
|
+
id: child_workflow.id,
|
|
112
|
+
messages: child_workflow.messages,
|
|
113
|
+
end_reason: event.reason
|
|
114
|
+
});
|
|
115
|
+
if (child.state !== "disposed") {
|
|
116
|
+
child[Symbol.dispose]();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
try {
|
|
120
|
+
child_workflow.start();
|
|
121
|
+
child.prompt(prompt);
|
|
122
|
+
} catch {
|
|
123
|
+
if (!settled) {
|
|
124
|
+
settled = true;
|
|
125
|
+
resolve({
|
|
126
|
+
id: child_workflow.id,
|
|
127
|
+
messages: child_workflow.messages,
|
|
128
|
+
end_reason: "cancel"
|
|
129
|
+
});
|
|
130
|
+
if (child.state !== "disposed") {
|
|
131
|
+
child[Symbol.dispose]();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
get id() {
|
|
138
|
+
return child_workflow.id;
|
|
139
|
+
},
|
|
140
|
+
get workflow() {
|
|
141
|
+
return child_workflow;
|
|
142
|
+
},
|
|
143
|
+
promise,
|
|
144
|
+
get done() {
|
|
145
|
+
return settled;
|
|
146
|
+
},
|
|
147
|
+
cancel: () => child_workflow.cancel()
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// src/subagent.ts
|
|
153
|
+
var SubagentOrchestrator = class extends Orchestrator {
|
|
154
|
+
/**
|
|
155
|
+
* Run a prompt as an autonomous child agent and return when it completes.
|
|
156
|
+
*
|
|
157
|
+
* @param prompt - The instruction for the child agent.
|
|
158
|
+
* @param options - Configuration for the child agent (system prompt, tools, session forking, custom ID).
|
|
159
|
+
*/
|
|
160
|
+
async execute(prompt, options) {
|
|
161
|
+
return this.spawn(prompt, options).promise;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/background-agent.ts
|
|
166
|
+
var BackgroundOrchestrator = class extends Orchestrator {
|
|
167
|
+
#handle;
|
|
168
|
+
#status = "idle";
|
|
169
|
+
/**
|
|
170
|
+
* Start the background worker. Can only be called once.
|
|
171
|
+
*
|
|
172
|
+
* @param prompt - The instruction for the worker.
|
|
173
|
+
* @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).
|
|
174
|
+
*/
|
|
175
|
+
execute(prompt, options) {
|
|
176
|
+
if (this.#status !== "idle") {
|
|
177
|
+
throw new Error("invalid state");
|
|
178
|
+
}
|
|
179
|
+
this.#handle = this.spawn(prompt, options);
|
|
180
|
+
this.#status = "running";
|
|
181
|
+
this.#handle.promise.then(() => {
|
|
182
|
+
if (this.#status === "running") {
|
|
183
|
+
this.#status = "completed";
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The unique identifier of this worker. Throws if not started.
|
|
189
|
+
*/
|
|
190
|
+
get id() {
|
|
191
|
+
if (!this.#handle) {
|
|
192
|
+
throw new Error("not started");
|
|
193
|
+
}
|
|
194
|
+
return this.#handle.id;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Current state: `idle`, `running`, `completed`, or `cancelled`.
|
|
198
|
+
*/
|
|
199
|
+
get status() {
|
|
200
|
+
return this.#status;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* `true` when the worker has completed or been cancelled.
|
|
204
|
+
*/
|
|
205
|
+
get done() {
|
|
206
|
+
return this.#status === "completed" || this.#status === "cancelled";
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Number of agentic turns (assistant messages) so far.
|
|
210
|
+
*/
|
|
211
|
+
get rounds() {
|
|
212
|
+
if (!this.#handle) {
|
|
213
|
+
return 0;
|
|
214
|
+
}
|
|
215
|
+
return this.#handle.workflow.messages.filter((m) => m.role === "assistant").length;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Total number of tool calls made across all rounds.
|
|
219
|
+
*/
|
|
220
|
+
get tool_call_count() {
|
|
221
|
+
if (!this.#handle) {
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
224
|
+
return this.#handle.workflow.messages.filter((m) => m.role === "assistant").reduce((count, m) => count + m.content.filter((c) => c.type === "tool").length, 0);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The text content of the most recent assistant message, if any.
|
|
228
|
+
*/
|
|
229
|
+
get latest_message() {
|
|
230
|
+
if (!this.#handle) {
|
|
231
|
+
return void 0;
|
|
232
|
+
}
|
|
233
|
+
const messages = this.#handle.workflow.messages;
|
|
234
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
235
|
+
const msg = messages[i];
|
|
236
|
+
if (msg.role === "assistant") {
|
|
237
|
+
const text = msg.content?.find((c) => c.type === "text");
|
|
238
|
+
if (text?.type === "text") {
|
|
239
|
+
return text.text;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return void 0;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Snapshot of the worker's current state.
|
|
247
|
+
*/
|
|
248
|
+
get_state() {
|
|
249
|
+
if (this.#status === "idle") {
|
|
250
|
+
return {
|
|
251
|
+
id: "",
|
|
252
|
+
status: "idle",
|
|
253
|
+
rounds: 0,
|
|
254
|
+
tool_call_count: 0
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
id: this.id,
|
|
259
|
+
status: this.#status,
|
|
260
|
+
rounds: this.rounds,
|
|
261
|
+
tool_call_count: this.tool_call_count,
|
|
262
|
+
latest_message: this.latest_message
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Await completion and return the full result.
|
|
267
|
+
*/
|
|
268
|
+
async collect() {
|
|
269
|
+
if (!this.#handle) {
|
|
270
|
+
throw new Error("not started");
|
|
271
|
+
}
|
|
272
|
+
return this.#handle.promise;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Cancel the running worker. Throws if not running.
|
|
276
|
+
*/
|
|
277
|
+
cancel() {
|
|
278
|
+
if (this.#status !== "running" || !this.#handle) {
|
|
279
|
+
throw new Error("invalid state");
|
|
280
|
+
}
|
|
281
|
+
if (this.#handle.workflow.state !== "disposed") {
|
|
282
|
+
this.#handle.cancel();
|
|
283
|
+
}
|
|
284
|
+
this.#status = "cancelled";
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Dispose the worker, cancelling it if still running.
|
|
288
|
+
*/
|
|
289
|
+
[Symbol.dispose]() {
|
|
290
|
+
if (this.#status === "running" && this.#handle) {
|
|
291
|
+
if (this.#handle.workflow.state !== "disposed") {
|
|
292
|
+
this.#handle.cancel();
|
|
293
|
+
}
|
|
294
|
+
this.#status = "cancelled";
|
|
295
|
+
}
|
|
296
|
+
this.#handle = void 0;
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// src/background-agent-pool.ts
|
|
301
|
+
var BackgroundAgentPool = class {
|
|
302
|
+
#source;
|
|
303
|
+
#recursive_depth;
|
|
304
|
+
#agents = /* @__PURE__ */ new Map();
|
|
305
|
+
/**
|
|
306
|
+
* @param source - A `WorkflowManager` or `Workflow` to spawn workers from.
|
|
307
|
+
* @param options.recursive_depth - How many levels of recursive orchestration to allow in workers. Defaults to `0`.
|
|
308
|
+
*/
|
|
309
|
+
constructor(source, { recursive_depth = 0 } = {}) {
|
|
310
|
+
this.#source = source;
|
|
311
|
+
this.#recursive_depth = recursive_depth;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Update the workflow/manager reference used to spawn new workers.
|
|
315
|
+
* This must be called when a persisted pool is reused by a new workflow,
|
|
316
|
+
* so that new workers are spawned from the current (live) workflow
|
|
317
|
+
* instead of a previously disposed one.
|
|
318
|
+
*/
|
|
319
|
+
update_source(source) {
|
|
320
|
+
this.#source = source;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Launch a new background worker and return its ID.
|
|
324
|
+
*
|
|
325
|
+
* @param prompt - The instruction for the worker.
|
|
326
|
+
* @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).
|
|
327
|
+
*/
|
|
328
|
+
start(prompt, options) {
|
|
329
|
+
const agent = new BackgroundOrchestrator(this.#source, {
|
|
330
|
+
recursive_depth: this.#recursive_depth
|
|
331
|
+
});
|
|
332
|
+
agent.execute(prompt, options);
|
|
333
|
+
this.#agents.set(agent.id, agent);
|
|
334
|
+
return agent.id;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Return all worker IDs in the pool.
|
|
338
|
+
*/
|
|
339
|
+
list() {
|
|
340
|
+
return [...this.#agents.keys()];
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Get state snapshots for the given worker IDs, or all workers if omitted.
|
|
344
|
+
*
|
|
345
|
+
* @param ids - Worker IDs to query. Omit to query all.
|
|
346
|
+
*/
|
|
347
|
+
state(...ids) {
|
|
348
|
+
const target_ids = ids.length > 0 ? ids : [...this.#agents.keys()];
|
|
349
|
+
return target_ids.map((id) => {
|
|
350
|
+
const agent = this.#agents.get(id);
|
|
351
|
+
if (!agent) {
|
|
352
|
+
throw new Error(`no worker with id ${id}`);
|
|
353
|
+
}
|
|
354
|
+
return agent.get_state();
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Cancel a running worker by ID.
|
|
359
|
+
*
|
|
360
|
+
* @param id - The worker ID to cancel.
|
|
361
|
+
*/
|
|
362
|
+
cancel(id) {
|
|
363
|
+
const agent = this.#agents.get(id);
|
|
364
|
+
if (!agent) {
|
|
365
|
+
throw new Error(`no worker with id ${id}`);
|
|
366
|
+
}
|
|
367
|
+
agent.cancel();
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Pop completed workers from the pool and return their states. Skips workers still running.
|
|
371
|
+
*
|
|
372
|
+
* @param ids - Worker IDs to acknowledge. Omit to acknowledge all completed.
|
|
373
|
+
*/
|
|
374
|
+
ack(...ids) {
|
|
375
|
+
const target_ids = ids.length > 0 ? ids : [...this.#agents.keys()];
|
|
376
|
+
const results = [];
|
|
377
|
+
for (const id of target_ids) {
|
|
378
|
+
const agent = this.#agents.get(id);
|
|
379
|
+
if (!agent) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (!agent.done) {
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
results.push(agent.get_state());
|
|
386
|
+
this.#agents.delete(id);
|
|
387
|
+
}
|
|
388
|
+
return results;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Dispose all workers in the pool, cancelling any that are still running.
|
|
392
|
+
*/
|
|
393
|
+
[Symbol.dispose]() {
|
|
394
|
+
for (const agent of this.#agents.values()) {
|
|
395
|
+
agent[Symbol.dispose]();
|
|
396
|
+
}
|
|
397
|
+
this.#agents.clear();
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
// src/parallel-agent.ts
|
|
402
|
+
var ParallelOrchestrator = class extends Orchestrator {
|
|
403
|
+
/**
|
|
404
|
+
* Run all tasks concurrently and return when every child agent has completed.
|
|
405
|
+
*
|
|
406
|
+
* @param tasks - Array of task configs, each with a `prompt` and optional `SubagentOptions`.
|
|
407
|
+
*/
|
|
408
|
+
async execute(tasks) {
|
|
409
|
+
const handles = [];
|
|
410
|
+
try {
|
|
411
|
+
for (const { prompt, ...options } of tasks) {
|
|
412
|
+
handles.push(this.spawn(prompt, options));
|
|
413
|
+
}
|
|
414
|
+
} catch (error) {
|
|
415
|
+
for (const handle of handles) {
|
|
416
|
+
try {
|
|
417
|
+
handle.cancel();
|
|
418
|
+
} catch {
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
throw error;
|
|
422
|
+
}
|
|
423
|
+
return Promise.all(handles.map((h) => h.promise));
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
// src/tools/background-task-pool.ts
|
|
428
|
+
var BackgroundTaskPoolImpl = class {
|
|
429
|
+
#pool;
|
|
430
|
+
constructor(context) {
|
|
431
|
+
const POOL_KEY = "__background_agent_pool";
|
|
432
|
+
if (!context[POOL_KEY]) {
|
|
433
|
+
const depth = context[ORCHESTRATION_DEPTH_KEY] ?? 0;
|
|
434
|
+
context[POOL_KEY] = new BackgroundAgentPool(context.workflow, { recursive_depth: depth });
|
|
435
|
+
}
|
|
436
|
+
const pool = context[POOL_KEY];
|
|
437
|
+
pool.update_source(context.workflow);
|
|
438
|
+
this.#pool = pool;
|
|
439
|
+
}
|
|
440
|
+
async execute({ action, prompts, system, fork_session, ids }) {
|
|
441
|
+
try {
|
|
442
|
+
switch (action) {
|
|
443
|
+
case "start": {
|
|
444
|
+
if (!prompts?.length) {
|
|
445
|
+
return {
|
|
446
|
+
result: false,
|
|
447
|
+
message: "prompts is required for start"
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
const started_ids = prompts.map((p) => this.#pool.start(p, { system, fork_session }));
|
|
451
|
+
return { result: true, ids: started_ids };
|
|
452
|
+
}
|
|
453
|
+
case "list": {
|
|
454
|
+
const worker_ids = this.#pool.list();
|
|
455
|
+
return { result: true, ids: worker_ids };
|
|
456
|
+
}
|
|
457
|
+
case "state": {
|
|
458
|
+
const workers = ids?.length ? this.#pool.state(...ids) : this.#pool.state();
|
|
459
|
+
return { result: true, workers };
|
|
460
|
+
}
|
|
461
|
+
case "cancel": {
|
|
462
|
+
if (!ids?.length) {
|
|
463
|
+
return {
|
|
464
|
+
result: false,
|
|
465
|
+
message: "ids is required for cancel"
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
const errors = [];
|
|
469
|
+
for (const id of ids) {
|
|
470
|
+
try {
|
|
471
|
+
this.#pool.cancel(id);
|
|
472
|
+
} catch (e) {
|
|
473
|
+
errors.push(`${id}: ${e instanceof Error ? e.message : String(e)}`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (errors.length) {
|
|
477
|
+
return {
|
|
478
|
+
result: false,
|
|
479
|
+
message: `some cancels failed: ${errors.join("; ")}`
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
return { result: true, ids };
|
|
483
|
+
}
|
|
484
|
+
case "ack": {
|
|
485
|
+
const workers = ids?.length ? this.#pool.ack(...ids) : this.#pool.ack();
|
|
486
|
+
return { result: true, workers };
|
|
487
|
+
}
|
|
488
|
+
default: {
|
|
489
|
+
return {
|
|
490
|
+
result: false,
|
|
491
|
+
message: `unknown action: ${action}`
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
} catch (error) {
|
|
496
|
+
return {
|
|
497
|
+
result: false,
|
|
498
|
+
message: error instanceof Error ? error.message : String(error),
|
|
499
|
+
error
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
static get_definition() {
|
|
504
|
+
return {
|
|
505
|
+
name: "background",
|
|
506
|
+
description: "Manage background worker agents. Workers run independently with their own conversations and tools. Use 'start' to launch workers, 'list' to see all worker IDs, 'state' to check progress (rounds, tool calls, latest message), 'cancel' to stop workers, 'ack' to collect results from completed workers and remove them from the pool.",
|
|
507
|
+
parameters: [
|
|
508
|
+
{
|
|
509
|
+
name: "action",
|
|
510
|
+
description: "The operation to perform",
|
|
511
|
+
type: "string",
|
|
512
|
+
required: true,
|
|
513
|
+
enum: ["start", "list", "state", "cancel", "ack"]
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
name: "prompts",
|
|
517
|
+
description: "Instructions for workers to start, one per worker. Required and must not be empty when action is 'start'.",
|
|
518
|
+
type: "array",
|
|
519
|
+
required: false,
|
|
520
|
+
items: { type: "string", required: true }
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
name: "system",
|
|
524
|
+
description: "Override the system prompt (only for 'start')",
|
|
525
|
+
type: "string",
|
|
526
|
+
required: false
|
|
527
|
+
},
|
|
528
|
+
{
|
|
529
|
+
name: "fork_session",
|
|
530
|
+
description: "Start with current conversation history (only for 'start')",
|
|
531
|
+
type: "boolean",
|
|
532
|
+
required: false
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
name: "ids",
|
|
536
|
+
description: "Worker IDs to operate on (for 'state', 'cancel', 'ack'). Omit for all.",
|
|
537
|
+
type: "array",
|
|
538
|
+
required: false,
|
|
539
|
+
items: { type: "string", required: true }
|
|
540
|
+
}
|
|
541
|
+
],
|
|
542
|
+
parallelizable: false
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
var BackgroundTaskPool = BackgroundTaskPoolImpl;
|
|
547
|
+
|
|
548
|
+
// src/tools/utils.ts
|
|
549
|
+
function extract_response(result) {
|
|
550
|
+
for (let i = result.messages.length - 1; i >= 0; i--) {
|
|
551
|
+
const message = result.messages[i];
|
|
552
|
+
if (message.role !== "assistant") {
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
const content = message.content;
|
|
556
|
+
if (!content) {
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
for (let j = content.length - 1; j >= 0; j--) {
|
|
560
|
+
const block = content[j];
|
|
561
|
+
if (block.type === "text" && block.text) {
|
|
562
|
+
return block.text;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return "(no text response)";
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// src/tools/parallel-agent-task.ts
|
|
570
|
+
var ParallelAgentTaskImpl = class {
|
|
571
|
+
#agent;
|
|
572
|
+
constructor(context) {
|
|
573
|
+
const depth = context[ORCHESTRATION_DEPTH_KEY] ?? 0;
|
|
574
|
+
this.#agent = new ParallelOrchestrator(context.workflow, { recursive_depth: depth });
|
|
575
|
+
}
|
|
576
|
+
async execute({ prompts, system, fork_session }) {
|
|
577
|
+
try {
|
|
578
|
+
if (!prompts?.length) {
|
|
579
|
+
return {
|
|
580
|
+
result: false,
|
|
581
|
+
message: "prompts is required and must not be empty"
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
const tasks = prompts.map((prompt) => ({ prompt, system, fork_session }));
|
|
585
|
+
const results = await this.#agent.execute(tasks);
|
|
586
|
+
return {
|
|
587
|
+
result: true,
|
|
588
|
+
responses: results.map((r) => ({
|
|
589
|
+
id: r.id,
|
|
590
|
+
response: extract_response(r),
|
|
591
|
+
end_reason: r.end_reason
|
|
592
|
+
}))
|
|
593
|
+
};
|
|
594
|
+
} catch (error) {
|
|
595
|
+
return {
|
|
596
|
+
result: false,
|
|
597
|
+
message: error instanceof Error ? error.message : String(error),
|
|
598
|
+
error
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
static get_definition() {
|
|
603
|
+
return {
|
|
604
|
+
name: "parallel",
|
|
605
|
+
description: "Run multiple prompts as concurrent sub-agents. All agents start immediately and the tool returns when every agent has completed. Each sub-agent has its own conversation and access to the same tools. Use when tasks are independent and can benefit from concurrent execution.",
|
|
606
|
+
parameters: [
|
|
607
|
+
{
|
|
608
|
+
name: "prompts",
|
|
609
|
+
description: "Array of instructions, one per sub-agent",
|
|
610
|
+
type: "array",
|
|
611
|
+
required: true,
|
|
612
|
+
items: { type: "string", required: true }
|
|
613
|
+
},
|
|
614
|
+
{
|
|
615
|
+
name: "system",
|
|
616
|
+
description: "Override the system prompt for all sub-agents",
|
|
617
|
+
type: "string",
|
|
618
|
+
required: false
|
|
619
|
+
},
|
|
620
|
+
{
|
|
621
|
+
name: "fork_session",
|
|
622
|
+
description: "If true, each sub-agent starts with the current conversation history",
|
|
623
|
+
type: "boolean",
|
|
624
|
+
required: false
|
|
625
|
+
}
|
|
626
|
+
],
|
|
627
|
+
parallelizable: false
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
var ParallelAgentTask = ParallelAgentTaskImpl;
|
|
632
|
+
|
|
633
|
+
// src/tools/subagent-task.ts
|
|
634
|
+
var SubagentTaskImpl = class {
|
|
635
|
+
#agent;
|
|
636
|
+
constructor(context) {
|
|
637
|
+
const depth = context[ORCHESTRATION_DEPTH_KEY] ?? 0;
|
|
638
|
+
this.#agent = new SubagentOrchestrator(context.workflow, { recursive_depth: depth });
|
|
639
|
+
}
|
|
640
|
+
async execute({ prompt, system, fork_session }) {
|
|
641
|
+
try {
|
|
642
|
+
const result = await this.#agent.execute(prompt, { system, fork_session });
|
|
643
|
+
return {
|
|
644
|
+
result: true,
|
|
645
|
+
id: result.id,
|
|
646
|
+
response: extract_response(result),
|
|
647
|
+
end_reason: result.end_reason
|
|
648
|
+
};
|
|
649
|
+
} catch (error) {
|
|
650
|
+
return {
|
|
651
|
+
result: false,
|
|
652
|
+
message: error instanceof Error ? error.message : String(error),
|
|
653
|
+
error
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
static get_definition() {
|
|
658
|
+
return {
|
|
659
|
+
name: "subagent",
|
|
660
|
+
description: "Run a prompt as an autonomous sub-agent with its own conversation. The sub-agent has access to the same tools and runs to completion, returning its final response. Use for tasks that require independent multi-step reasoning or tool use.",
|
|
661
|
+
parameters: [
|
|
662
|
+
{
|
|
663
|
+
name: "prompt",
|
|
664
|
+
description: "The instruction for the sub-agent",
|
|
665
|
+
type: "string",
|
|
666
|
+
required: true
|
|
667
|
+
},
|
|
668
|
+
{
|
|
669
|
+
name: "system",
|
|
670
|
+
description: "Override the system prompt for this sub-agent",
|
|
671
|
+
type: "string",
|
|
672
|
+
required: false
|
|
673
|
+
},
|
|
674
|
+
{
|
|
675
|
+
name: "fork_session",
|
|
676
|
+
description: "If true, the sub-agent starts with a copy of the current conversation history",
|
|
677
|
+
type: "boolean",
|
|
678
|
+
required: false
|
|
679
|
+
}
|
|
680
|
+
],
|
|
681
|
+
parallelizable: true
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
var SubagentTask = SubagentTaskImpl;
|
|
686
|
+
|
|
687
|
+
// src/tools/index.ts
|
|
688
|
+
var OrchestrationToolkit = [
|
|
689
|
+
SubagentTask,
|
|
690
|
+
ParallelAgentTask,
|
|
691
|
+
BackgroundTaskPool
|
|
692
|
+
];
|
|
693
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
694
|
+
0 && (module.exports = {
|
|
695
|
+
BackgroundAgentPool,
|
|
696
|
+
BackgroundOrchestrator,
|
|
697
|
+
BackgroundTaskPool,
|
|
698
|
+
ORCHESTRATION_DEPTH_KEY,
|
|
699
|
+
OrchestrationToolkit,
|
|
700
|
+
Orchestrator,
|
|
701
|
+
ParallelAgentTask,
|
|
702
|
+
ParallelOrchestrator,
|
|
703
|
+
SubagentOrchestrator,
|
|
704
|
+
SubagentTask
|
|
705
|
+
});
|
|
706
|
+
//# sourceMappingURL=index.cjs.map
|