@simulacra-ai/orchestration 0.0.1

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.
Files changed (50) hide show
  1. package/README.md +123 -0
  2. package/dist/background-agent-pool.d.ts +56 -0
  3. package/dist/background-agent-pool.d.ts.map +1 -0
  4. package/dist/background-agent-pool.js +102 -0
  5. package/dist/background-agent-pool.js.map +1 -0
  6. package/dist/background-agent.d.ts +59 -0
  7. package/dist/background-agent.d.ts.map +1 -0
  8. package/dist/background-agent.js +135 -0
  9. package/dist/background-agent.js.map +1 -0
  10. package/dist/index.d.ts +8 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +8 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/orchestrator.d.ts +43 -0
  15. package/dist/orchestrator.d.ts.map +1 -0
  16. package/dist/orchestrator.js +124 -0
  17. package/dist/orchestrator.js.map +1 -0
  18. package/dist/parallel-agent.d.ts +17 -0
  19. package/dist/parallel-agent.d.ts.map +1 -0
  20. package/dist/parallel-agent.js +33 -0
  21. package/dist/parallel-agent.js.map +1 -0
  22. package/dist/subagent.d.ts +18 -0
  23. package/dist/subagent.d.ts.map +1 -0
  24. package/dist/subagent.js +19 -0
  25. package/dist/subagent.js.map +1 -0
  26. package/dist/tools/background-worker-pool.d.ts +16 -0
  27. package/dist/tools/background-worker-pool.d.ts.map +1 -0
  28. package/dist/tools/background-worker-pool.js +126 -0
  29. package/dist/tools/background-worker-pool.js.map +1 -0
  30. package/dist/tools/index.d.ts +9 -0
  31. package/dist/tools/index.d.ts.map +1 -0
  32. package/dist/tools/index.js +15 -0
  33. package/dist/tools/index.js.map +1 -0
  34. package/dist/tools/parallel-agent-task.d.ts +16 -0
  35. package/dist/tools/parallel-agent-task.d.ts.map +1 -0
  36. package/dist/tools/parallel-agent-task.js +68 -0
  37. package/dist/tools/parallel-agent-task.js.map +1 -0
  38. package/dist/tools/subagent-task.d.ts +14 -0
  39. package/dist/tools/subagent-task.d.ts.map +1 -0
  40. package/dist/tools/subagent-task.js +58 -0
  41. package/dist/tools/subagent-task.js.map +1 -0
  42. package/dist/tools/utils.d.ts +8 -0
  43. package/dist/tools/utils.d.ts.map +1 -0
  44. package/dist/tools/utils.js +25 -0
  45. package/dist/tools/utils.js.map +1 -0
  46. package/dist/types.d.ts +90 -0
  47. package/dist/types.d.ts.map +1 -0
  48. package/dist/types.js +2 -0
  49. package/dist/types.js.map +1 -0
  50. package/package.json +23 -0
package/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # Simulacra Agent Orchestration
2
+
3
+ Multi-agent orchestration patterns for the Simulacra conversation engine. Child agents run independently with their own conversations and tool access.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @simulacra-ai/core @simulacra-ai/orchestration
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ All orchestration patterns require a `WorkflowManager` wrapping a conversation with tools:
14
+
15
+ ```typescript
16
+ import { Conversation, WorkflowManager } from "@simulacra-ai/core";
17
+
18
+ const conversation = new Conversation(provider);
19
+ conversation.toolkit = [/* your tools */];
20
+ const workflowManager = new WorkflowManager(conversation);
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### SubagentOrchestrator
26
+
27
+ Spawns a child agent and blocks until it completes.
28
+
29
+ ```typescript
30
+ import { SubagentOrchestrator } from "@simulacra-ai/orchestration";
31
+
32
+ const agent = new SubagentOrchestrator(workflowManager);
33
+ const result = await agent.execute("Analyze this data and report findings");
34
+
35
+ console.log(result.end_reason); // "complete" | "cancel" | "error"
36
+ console.log(result.messages);
37
+ ```
38
+
39
+ ### BackgroundOrchestrator
40
+
41
+ A single background worker. Call `execute` once to start, then inspect or collect.
42
+
43
+ ```typescript
44
+ import { BackgroundOrchestrator } from "@simulacra-ai/orchestration";
45
+
46
+ using worker = new BackgroundOrchestrator(workflowManager);
47
+ worker.execute("Monitor the feed for changes");
48
+
49
+ // later
50
+ worker.status; // "running" | "completed" | "cancelled"
51
+ worker.done; // true when completed or cancelled
52
+ worker.rounds; // number of agentic turns
53
+ worker.tool_call_count; // total tool calls made
54
+ worker.latest_message; // last assistant text
55
+
56
+ const result = await worker.collect(); // await completion, get full result
57
+ worker.cancel(); // cancel if still running
58
+ // automatically disposed at end of scope
59
+ ```
60
+
61
+ ### BackgroundAgentPool
62
+
63
+ Manages multiple background workers with batch operations.
64
+
65
+ ```typescript
66
+ import { BackgroundAgentPool } from "@simulacra-ai/orchestration";
67
+
68
+ using pool = new BackgroundAgentPool(workflowManager);
69
+
70
+ const id1 = pool.start("Research topic A");
71
+ const id2 = pool.start("Research topic B");
72
+ const id3 = pool.start("Research topic C");
73
+
74
+ pool.list(); // [id1, id2, id3]
75
+ pool.state(id1, id2); // WorkerState[] with status, rounds, tool_call_count, latest_message
76
+ pool.cancel(id2); // cancel a specific worker
77
+
78
+ const done = pool.ack(); // pop all completed workers, returns their states
79
+ // all remaining workers cancelled and cleaned up at end of scope
80
+ ```
81
+
82
+ ### ParallelOrchestrator
83
+
84
+ Runs multiple prompts concurrently and waits for all to complete.
85
+
86
+ ```typescript
87
+ import { ParallelOrchestrator } from "@simulacra-ai/orchestration";
88
+
89
+ const agent = new ParallelOrchestrator(workflowManager);
90
+ const results = await agent.execute([
91
+ { prompt: "Analyze dataset A" },
92
+ { prompt: "Analyze dataset B" },
93
+ { prompt: "Analyze dataset C", system: "Focus on outliers" },
94
+ ]);
95
+ ```
96
+
97
+ ## LLM Tools
98
+
99
+ Each pattern is available as a `ToolClass` for model-driven orchestration.
100
+
101
+ > **Note:** By default, orchestration tools are stripped from child agents to prevent recursive nesting. Pass `strip_tools: false` to the `Orchestrator` constructor to allow it.
102
+
103
+ ```typescript
104
+ import { OrchestrationToolkit } from "@simulacra-ai/orchestration";
105
+
106
+ conversation.toolkit = [...conversation.toolkit, ...OrchestrationToolkit];
107
+ ```
108
+
109
+ Individual tools (`SubagentTask`, `BackgroundWorkerPool`, `ParallelAgentTask`) are also exported if you want to pick selectively.
110
+
111
+ `BackgroundWorkerPool` exposes a worker pool to the model with these actions:
112
+
113
+ Action|Description
114
+ -|-
115
+ `start`|Launch one or more background workers (requires `prompts`)
116
+ `list`|List all worker IDs
117
+ `state`|Get status, rounds, tool calls, latest message (optional `ids`)
118
+ `cancel`|Stop workers (requires `ids`)
119
+ `ack`|Pop completed workers and return their states (optional `ids`)
120
+
121
+ ## License
122
+
123
+ MIT
@@ -0,0 +1,56 @@
1
+ import type { Workflow, WorkflowManager } from "@simulacra-ai/core";
2
+ import type { SubagentOptions, WorkerState } from "./types.ts";
3
+ /**
4
+ * Manages a pool of background worker agents.
5
+ *
6
+ * Provides a registry for starting, listing, inspecting, cancelling,
7
+ * and acknowledging (collecting + removing) completed workers.
8
+ */
9
+ export declare class BackgroundAgentPool {
10
+ #private;
11
+ /**
12
+ * @param source - A `WorkflowManager` or `Workflow` to spawn workers from.
13
+ */
14
+ constructor(source: WorkflowManager | Workflow);
15
+ /**
16
+ * Update the workflow/manager reference used to spawn new workers.
17
+ * This must be called when a persisted pool is reused by a new workflow,
18
+ * so that new workers are spawned from the current (live) workflow
19
+ * instead of a previously disposed one.
20
+ */
21
+ update_source(source: WorkflowManager | Workflow): void;
22
+ /**
23
+ * Launch a new background worker and return its ID.
24
+ *
25
+ * @param prompt - The instruction for the worker.
26
+ * @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).
27
+ */
28
+ start(prompt: string, options?: SubagentOptions): string;
29
+ /**
30
+ * Return all worker IDs in the pool.
31
+ */
32
+ list(): string[];
33
+ /**
34
+ * Get state snapshots for the given worker IDs, or all workers if omitted.
35
+ *
36
+ * @param ids - Worker IDs to query. Omit to query all.
37
+ */
38
+ state(...ids: string[]): WorkerState[];
39
+ /**
40
+ * Cancel a running worker by ID.
41
+ *
42
+ * @param id - The worker ID to cancel.
43
+ */
44
+ cancel(id: string): void;
45
+ /**
46
+ * Pop completed workers from the pool and return their states. Skips workers still running.
47
+ *
48
+ * @param ids - Worker IDs to acknowledge. Omit to acknowledge all completed.
49
+ */
50
+ ack(...ids: string[]): WorkerState[];
51
+ /**
52
+ * Dispose all workers in the pool, cancelling any that are still running.
53
+ */
54
+ [Symbol.dispose](): void;
55
+ }
56
+ //# sourceMappingURL=background-agent-pool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-agent-pool.d.ts","sourceRoot":"","sources":["../src/background-agent-pool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAEpE,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE/D;;;;;GAKG;AACH,qBAAa,mBAAmB;;IAI9B;;OAEG;gBACS,MAAM,EAAE,eAAe,GAAG,QAAQ;IAI9C;;;;;OAKG;IACH,aAAa,CAAC,MAAM,EAAE,eAAe,GAAG,QAAQ,GAAG,IAAI;IAIvD;;;;;OAKG;IACH,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,MAAM;IAOxD;;OAEG;IACH,IAAI,IAAI,MAAM,EAAE;IAIhB;;;;OAIG;IACH,KAAK,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,GAAG,WAAW,EAAE;IAWtC;;;;OAIG;IACH,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAQxB;;;;OAIG;IACH,GAAG,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,GAAG,WAAW,EAAE;IAiBpC;;OAEG;IACH,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI;CAMzB"}
@@ -0,0 +1,102 @@
1
+ import { BackgroundOrchestrator } from "./background-agent.js";
2
+ /**
3
+ * Manages a pool of background worker agents.
4
+ *
5
+ * Provides a registry for starting, listing, inspecting, cancelling,
6
+ * and acknowledging (collecting + removing) completed workers.
7
+ */
8
+ export class BackgroundAgentPool {
9
+ #source;
10
+ #agents = new Map();
11
+ /**
12
+ * @param source - A `WorkflowManager` or `Workflow` to spawn workers from.
13
+ */
14
+ constructor(source) {
15
+ this.#source = source;
16
+ }
17
+ /**
18
+ * Update the workflow/manager reference used to spawn new workers.
19
+ * This must be called when a persisted pool is reused by a new workflow,
20
+ * so that new workers are spawned from the current (live) workflow
21
+ * instead of a previously disposed one.
22
+ */
23
+ update_source(source) {
24
+ this.#source = source;
25
+ }
26
+ /**
27
+ * Launch a new background worker and return its ID.
28
+ *
29
+ * @param prompt - The instruction for the worker.
30
+ * @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).
31
+ */
32
+ start(prompt, options) {
33
+ const agent = new BackgroundOrchestrator(this.#source);
34
+ agent.execute(prompt, options);
35
+ this.#agents.set(agent.id, agent);
36
+ return agent.id;
37
+ }
38
+ /**
39
+ * Return all worker IDs in the pool.
40
+ */
41
+ list() {
42
+ return [...this.#agents.keys()];
43
+ }
44
+ /**
45
+ * Get state snapshots for the given worker IDs, or all workers if omitted.
46
+ *
47
+ * @param ids - Worker IDs to query. Omit to query all.
48
+ */
49
+ state(...ids) {
50
+ const target_ids = ids.length > 0 ? ids : [...this.#agents.keys()];
51
+ return target_ids.map((id) => {
52
+ const agent = this.#agents.get(id);
53
+ if (!agent) {
54
+ throw new Error(`no worker with id ${id}`);
55
+ }
56
+ return agent.get_state();
57
+ });
58
+ }
59
+ /**
60
+ * Cancel a running worker by ID.
61
+ *
62
+ * @param id - The worker ID to cancel.
63
+ */
64
+ cancel(id) {
65
+ const agent = this.#agents.get(id);
66
+ if (!agent) {
67
+ throw new Error(`no worker with id ${id}`);
68
+ }
69
+ agent.cancel();
70
+ }
71
+ /**
72
+ * Pop completed workers from the pool and return their states. Skips workers still running.
73
+ *
74
+ * @param ids - Worker IDs to acknowledge. Omit to acknowledge all completed.
75
+ */
76
+ ack(...ids) {
77
+ const target_ids = ids.length > 0 ? ids : [...this.#agents.keys()];
78
+ const results = [];
79
+ for (const id of target_ids) {
80
+ const agent = this.#agents.get(id);
81
+ if (!agent) {
82
+ continue;
83
+ }
84
+ if (!agent.done) {
85
+ continue;
86
+ }
87
+ results.push(agent.get_state());
88
+ this.#agents.delete(id);
89
+ }
90
+ return results;
91
+ }
92
+ /**
93
+ * Dispose all workers in the pool, cancelling any that are still running.
94
+ */
95
+ [Symbol.dispose]() {
96
+ for (const agent of this.#agents.values()) {
97
+ agent[Symbol.dispose]();
98
+ }
99
+ this.#agents.clear();
100
+ }
101
+ }
102
+ //# sourceMappingURL=background-agent-pool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-agent-pool.js","sourceRoot":"","sources":["../src/background-agent-pool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAG/D;;;;;GAKG;AACH,MAAM,OAAO,mBAAmB;IAC9B,OAAO,CAA6B;IAC3B,OAAO,GAAG,IAAI,GAAG,EAAkC,CAAC;IAE7D;;OAEG;IACH,YAAY,MAAkC;QAC5C,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED;;;;;OAKG;IACH,aAAa,CAAC,MAAkC;QAC9C,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,MAAc,EAAE,OAAyB;QAC7C,MAAM,KAAK,GAAG,IAAI,sBAAsB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvD,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAC/B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QAClC,OAAO,KAAK,CAAC,EAAE,CAAC;IAClB,CAAC;IAED;;OAEG;IACH,IAAI;QACF,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,GAAG,GAAa;QACpB,MAAM,UAAU,GAAG,GAAG,CAAC,MAAM,GAA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C,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC;YAChC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC1B,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;OAEG;IACH,CAAC,MAAM,CAAC,OAAO,CAAC;QACd,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;YAC1C,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;QAC1B,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;CACF"}
@@ -0,0 +1,59 @@
1
+ import { Orchestrator } from "./orchestrator.ts";
2
+ import type { SubagentOptions, SubagentResult, WorkerState } from "./types.ts";
3
+ /**
4
+ * A single background worker agent.
5
+ *
6
+ * Each instance represents one background task. Call `execute` once to
7
+ * start it, then use `status`, `done`, `collect`, and `cancel` to manage it.
8
+ */
9
+ export declare class BackgroundOrchestrator extends Orchestrator {
10
+ #private;
11
+ /**
12
+ * Start the background worker. Can only be called once.
13
+ *
14
+ * @param prompt - The instruction for the worker.
15
+ * @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).
16
+ */
17
+ execute(prompt: string, options?: SubagentOptions): void;
18
+ /**
19
+ * The unique identifier of this worker. Throws if not started.
20
+ */
21
+ get id(): string;
22
+ /**
23
+ * Current state: `idle`, `running`, `completed`, or `cancelled`.
24
+ */
25
+ get status(): "running" | "completed" | "cancelled" | "idle";
26
+ /**
27
+ * `true` when the worker has completed or been cancelled.
28
+ */
29
+ get done(): boolean;
30
+ /**
31
+ * Number of agentic turns (assistant messages) so far.
32
+ */
33
+ get rounds(): number;
34
+ /**
35
+ * Total number of tool calls made across all rounds.
36
+ */
37
+ get tool_call_count(): number;
38
+ /**
39
+ * The text content of the most recent assistant message, if any.
40
+ */
41
+ get latest_message(): string | undefined;
42
+ /**
43
+ * Snapshot of the worker's current state.
44
+ */
45
+ get_state(): WorkerState;
46
+ /**
47
+ * Await completion and return the full result.
48
+ */
49
+ collect(): Promise<SubagentResult>;
50
+ /**
51
+ * Cancel the running worker. Throws if not running.
52
+ */
53
+ cancel(): void;
54
+ /**
55
+ * Dispose the worker, cancelling it if still running.
56
+ */
57
+ [Symbol.dispose](): void;
58
+ }
59
+ //# sourceMappingURL=background-agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-agent.d.ts","sourceRoot":"","sources":["../src/background-agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAoB,eAAe,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEjG;;;;;GAKG;AACH,qBAAa,sBAAuB,SAAQ,YAAY;;IAItD;;;;;OAKG;IACH,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,IAAI;IAaxD;;OAEG;IACH,IAAI,EAAE,IAAI,MAAM,CAKf;IAED;;OAEG;IACH,IAAI,MAAM,mDAET;IAED;;OAEG;IACH,IAAI,IAAI,IAAI,OAAO,CAElB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAKnB;IAED;;OAEG;IACH,IAAI,eAAe,IAAI,MAAM,CAO5B;IAED;;OAEG;IACH,IAAI,cAAc,IAAI,MAAM,GAAG,SAAS,CAevC;IAED;;OAEG;IACH,SAAS,IAAI,WAAW;IAUxB;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,cAAc,CAAC;IAOxC;;OAEG;IACH,MAAM,IAAI,IAAI;IAUd;;OAEG;IACH,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI;CASzB"}
@@ -0,0 +1,135 @@
1
+ import { Orchestrator } from "./orchestrator.js";
2
+ /**
3
+ * A single background worker agent.
4
+ *
5
+ * Each instance represents one background task. Call `execute` once to
6
+ * start it, then use `status`, `done`, `collect`, and `cancel` to manage it.
7
+ */
8
+ export class BackgroundOrchestrator extends Orchestrator {
9
+ #handle;
10
+ #status = "idle";
11
+ /**
12
+ * Start the background worker. Can only be called once.
13
+ *
14
+ * @param prompt - The instruction for the worker.
15
+ * @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).
16
+ */
17
+ execute(prompt, options) {
18
+ if (this.#status !== "idle") {
19
+ throw new Error("invalid state");
20
+ }
21
+ this.#handle = this.spawn(prompt, options);
22
+ this.#status = "running";
23
+ this.#handle.promise.then(() => {
24
+ if (this.#status === "running") {
25
+ this.#status = "completed";
26
+ }
27
+ });
28
+ }
29
+ /**
30
+ * The unique identifier of this worker. Throws if not started.
31
+ */
32
+ get id() {
33
+ if (!this.#handle) {
34
+ throw new Error("not started");
35
+ }
36
+ return this.#handle.id;
37
+ }
38
+ /**
39
+ * Current state: `idle`, `running`, `completed`, or `cancelled`.
40
+ */
41
+ get status() {
42
+ return this.#status;
43
+ }
44
+ /**
45
+ * `true` when the worker has completed or been cancelled.
46
+ */
47
+ get done() {
48
+ return this.#status === "completed" || this.#status === "cancelled";
49
+ }
50
+ /**
51
+ * Number of agentic turns (assistant messages) so far.
52
+ */
53
+ get rounds() {
54
+ if (!this.#handle) {
55
+ return 0;
56
+ }
57
+ return this.#handle.workflow.messages.filter((m) => m.role === "assistant").length;
58
+ }
59
+ /**
60
+ * Total number of tool calls made across all rounds.
61
+ */
62
+ get tool_call_count() {
63
+ if (!this.#handle) {
64
+ return 0;
65
+ }
66
+ return this.#handle.workflow.messages
67
+ .filter((m) => m.role === "assistant")
68
+ .reduce((count, m) => count + m.content.filter((c) => c.type === "tool").length, 0);
69
+ }
70
+ /**
71
+ * The text content of the most recent assistant message, if any.
72
+ */
73
+ get latest_message() {
74
+ if (!this.#handle) {
75
+ return undefined;
76
+ }
77
+ const messages = this.#handle.workflow.messages;
78
+ for (let i = messages.length - 1; i >= 0; i--) {
79
+ const msg = messages[i];
80
+ if (msg.role === "assistant") {
81
+ const text = msg.content?.find((c) => c.type === "text");
82
+ if (text?.type === "text") {
83
+ return text.text;
84
+ }
85
+ }
86
+ }
87
+ return undefined;
88
+ }
89
+ /**
90
+ * Snapshot of the worker's current state.
91
+ */
92
+ get_state() {
93
+ return {
94
+ id: this.id,
95
+ status: this.#status,
96
+ rounds: this.rounds,
97
+ tool_call_count: this.tool_call_count,
98
+ latest_message: this.latest_message,
99
+ };
100
+ }
101
+ /**
102
+ * Await completion and return the full result.
103
+ */
104
+ async collect() {
105
+ if (!this.#handle) {
106
+ throw new Error("not started");
107
+ }
108
+ return this.#handle.promise;
109
+ }
110
+ /**
111
+ * Cancel the running worker. Throws if not running.
112
+ */
113
+ cancel() {
114
+ if (this.#status !== "running" || !this.#handle) {
115
+ throw new Error("invalid state");
116
+ }
117
+ if (this.#handle.workflow.state !== "disposed") {
118
+ this.#handle.cancel();
119
+ }
120
+ this.#status = "cancelled";
121
+ }
122
+ /**
123
+ * Dispose the worker, cancelling it if still running.
124
+ */
125
+ [Symbol.dispose]() {
126
+ if (this.#status === "running" && this.#handle) {
127
+ if (this.#handle.workflow.state !== "disposed") {
128
+ this.#handle.cancel();
129
+ }
130
+ this.#status = "cancelled";
131
+ }
132
+ this.#handle = undefined;
133
+ }
134
+ }
135
+ //# sourceMappingURL=background-agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"background-agent.js","sourceRoot":"","sources":["../src/background-agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAGjD;;;;;GAKG;AACH,MAAM,OAAO,sBAAuB,SAAQ,YAAY;IACtD,OAAO,CAAoB;IAC3B,OAAO,GAAmD,MAAM,CAAC;IAEjE;;;;;OAKG;IACH,OAAO,CAAC,MAAc,EAAE,OAAyB;QAC/C,IAAI,IAAI,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;QACnC,CAAC;QACD,IAAI,CAAC,OAAO,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AAC,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;QACjC,CAAC;QACD,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;IAC9B,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,IAAI,CAAC,OAAO,KAAK,SAAS,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;YAChD,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;QACnC,CAAC;QACD,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;YAC/C,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;QACxB,CAAC;QACD,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,CAAC,MAAM,CAAC,OAAO,CAAC;QACd,IAAI,IAAI,CAAC,OAAO,KAAK,SAAS,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAC/C,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;gBAC/C,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YACxB,CAAC;YACD,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC;QAC7B,CAAC;QACD,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC;IAC3B,CAAC;CACF"}
@@ -0,0 +1,8 @@
1
+ export * from "./types.ts";
2
+ export * from "./orchestrator.ts";
3
+ export * from "./subagent.ts";
4
+ export * from "./background-agent.ts";
5
+ export * from "./background-agent-pool.ts";
6
+ export * from "./parallel-agent.ts";
7
+ export * from "./tools/index.ts";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./types.js";
2
+ export * from "./orchestrator.js";
3
+ export * from "./subagent.js";
4
+ export * from "./background-agent.js";
5
+ export * from "./background-agent-pool.js";
6
+ export * from "./parallel-agent.js";
7
+ export * from "./tools/index.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,43 @@
1
+ import { Workflow, WorkflowManager } from "@simulacra-ai/core";
2
+ import type { Conversation } from "@simulacra-ai/core";
3
+ import type { BackgroundHandle, SubagentOptions } from "./types.ts";
4
+ /**
5
+ * Base class for orchestration patterns.
6
+ *
7
+ * Accepts a `WorkflowManager` (for programmatic use) or a `Workflow`
8
+ * (for tool integration). Provides the shared child-spawning logic
9
+ * that all orchestration patterns build on.
10
+ *
11
+ * By default, orchestration tools are stripped from child agents to
12
+ * prevent nesting. Pass `strip_tools: false` to allow it.
13
+ */
14
+ export declare abstract class Orchestrator {
15
+ #private;
16
+ /**
17
+ * @param source - A `WorkflowManager` or `Workflow` to spawn children from.
18
+ * @param options.strip_tools - Remove orchestration tools from child agents. Defaults to `true`.
19
+ */
20
+ constructor(source: WorkflowManager | Workflow, { strip_tools }?: {
21
+ strip_tools?: boolean | undefined;
22
+ });
23
+ /**
24
+ * The conversation associated with the orchestrator.
25
+ */
26
+ protected get conversation(): Conversation;
27
+ /**
28
+ * The parent workflow, if available. Used to establish parent-child workflow relationships.
29
+ */
30
+ protected get parent_workflow(): Workflow | undefined;
31
+ /**
32
+ * Spawn a child agent with its own conversation and workflow.
33
+ *
34
+ * Creates a child conversation, assigns tools (stripping orchestration
35
+ * tools unless disabled), and starts the workflow. Returns a handle
36
+ * for awaiting, inspecting, or cancelling the child.
37
+ *
38
+ * @param prompt - The instruction to send to the child agent.
39
+ * @param options - Configuration for the child agent (system prompt, tools, session forking, custom ID).
40
+ */
41
+ protected spawn(prompt: string, options?: SubagentOptions): BackgroundHandle;
42
+ }
43
+ //# sourceMappingURL=orchestrator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../src/orchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAa,MAAM,oBAAoB,CAAC;AAClE,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAkB,MAAM,YAAY,CAAC;AAapF;;;;;;;;;GASG;AACH,8BAAsB,YAAY;;IAKhC;;;OAGG;gBACS,MAAM,EAAE,eAAe,GAAG,QAAQ,EAAE,EAAE,WAAkB,EAAE;;KAAK;IAS3E;;OAEG;IACH,SAAS,KAAK,YAAY,IAAI,YAAY,CAQzC;IAED;;OAEG;IACH,SAAS,KAAK,eAAe,IAAI,QAAQ,GAAG,SAAS,CAKpD;IAED;;;;;;;;;OASG;IACH,SAAS,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,gBAAgB;CA+D7E"}