@simulacra-ai/orchestration 0.0.3 → 0.0.4
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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/orchestrator.ts","../src/subagent.ts","../src/background-agent.ts","../src/background-agent-pool.ts","../src/parallel-agent.ts","../src/tools/background-task-pool.ts","../src/tools/utils.ts","../src/tools/parallel-agent-task.ts","../src/tools/subagent-task.ts","../src/tools/index.ts"],"sourcesContent":["export * from \"./types.ts\";\nexport * from \"./orchestrator.ts\";\nexport * from \"./subagent.ts\";\nexport * from \"./background-agent.ts\";\nexport * from \"./background-agent-pool.ts\";\nexport * from \"./parallel-agent.ts\";\nexport * from \"./tools/index.ts\";\n","import { Workflow, WorkflowManager } from \"@simulacra-ai/core\";\nimport type { Conversation, ToolClass } from \"@simulacra-ai/core\";\nimport type { BackgroundHandle, SubagentOptions, SubagentResult } from \"./types.ts\";\n\nconst ORCHESTRATION_TOOL_NAMES = new Set([\"subagent\", \"background\", \"parallel\"]);\n\n/**\n * Context key used to propagate the remaining orchestration depth to child agents.\n */\nexport const ORCHESTRATION_DEPTH_KEY = \"__orchestration_depth\";\n\n/**\n * Remove orchestration tools from a toolkit to prevent child agents from nesting.\n *\n * @param toolkit - The toolkit to filter.\n */\nfunction strip_orchestration_tools(toolkit: ToolClass[]): ToolClass[] {\n return toolkit.filter((t) => !ORCHESTRATION_TOOL_NAMES.has(t.get_definition().name));\n}\n\n/**\n * Base class for orchestration patterns.\n *\n * Accepts a `WorkflowManager` (for programmatic use) or a `Workflow`\n * (for tool integration). Provides the shared child-spawning logic\n * that all orchestration patterns build on.\n *\n * By default, `recursive_depth` is `0`, which strips orchestration tools\n * from child agents to prevent nesting. Set to a positive number to allow\n * that many levels of recursive orchestration, or `-1` for unlimited depth.\n */\nexport abstract class Orchestrator {\n readonly #manager?: WorkflowManager;\n readonly #workflow?: Workflow;\n readonly #recursive_depth: number;\n\n /**\n * @param source - A `WorkflowManager` or `Workflow` to spawn children from.\n * @param options.recursive_depth - How many levels of recursive orchestration to allow. `0` (default) strips orchestration tools from children, `-1` allows unlimited nesting.\n */\n constructor(source: WorkflowManager | Workflow, { recursive_depth = 0 } = {}) {\n if (!Number.isInteger(recursive_depth) || recursive_depth < -1) {\n throw new Error(\"invalid value for recursive_depth\");\n }\n if (source instanceof WorkflowManager) {\n this.#manager = source;\n } else {\n this.#workflow = source;\n }\n this.#recursive_depth = recursive_depth;\n }\n\n /**\n * The conversation associated with the orchestrator.\n */\n protected get conversation(): Conversation {\n if (this.#manager) {\n return this.#manager.conversation;\n }\n if (this.#workflow) {\n return this.#workflow.conversation;\n }\n throw new Error(\"no source\");\n }\n\n /**\n * The parent workflow, if available. Used to establish parent-child workflow relationships.\n */\n protected get parent_workflow(): Workflow | undefined {\n if (this.#manager) {\n return this.#manager.current_workflow;\n }\n return this.#workflow;\n }\n\n /**\n * Spawn a child agent with its own conversation and workflow.\n *\n * Creates a child conversation, assigns tools (stripping orchestration\n * tools when depth is exhausted), and starts the workflow. Returns a\n * handle for awaiting, inspecting, or cancelling the child.\n *\n * @param prompt - The instruction to send to the child agent.\n * @param options - Configuration for the child agent (system prompt, tools, session forking, custom ID).\n */\n protected spawn(prompt: string, options?: SubagentOptions): BackgroundHandle {\n const conversation = this.conversation;\n const child = conversation.spawn_child(\n options?.fork_session,\n options?.id,\n options?.system ?? conversation.system,\n );\n const toolkit = options?.toolkit ?? [...conversation.toolkit];\n child.toolkit = this.#recursive_depth === 0 ? strip_orchestration_tools(toolkit) : toolkit;\n\n const next_depth = this.#recursive_depth === -1 ? -1 : Math.max(0, this.#recursive_depth - 1);\n const child_context = { [ORCHESTRATION_DEPTH_KEY]: next_depth };\n\n const parent = this.parent_workflow;\n const child_workflow =\n parent && parent.state !== \"disposed\"\n ? parent.spawn_child(child, options?.id, child_context)\n : new Workflow(child, { context_data: child_context });\n\n let settled = false;\n\n const promise = new Promise<SubagentResult>((resolve) => {\n child_workflow.once(\"workflow_end\", (event) => {\n settled = true;\n resolve({\n id: child_workflow.id,\n messages: child_workflow.messages,\n end_reason: event.reason,\n });\n if (child.state !== \"disposed\") {\n child[Symbol.dispose]();\n }\n });\n\n try {\n child_workflow.start();\n child.prompt(prompt);\n } catch {\n if (!settled) {\n settled = true;\n resolve({\n id: child_workflow.id,\n messages: child_workflow.messages,\n end_reason: \"cancel\",\n });\n if (child.state !== \"disposed\") {\n child[Symbol.dispose]();\n }\n }\n }\n });\n\n return {\n get id() {\n return child_workflow.id;\n },\n get workflow() {\n return child_workflow;\n },\n promise,\n get done() {\n return settled;\n },\n cancel: () => child_workflow.cancel(),\n };\n }\n}\n","import { Orchestrator } from \"./orchestrator.ts\";\nimport type { SubagentOptions, SubagentResult } from \"./types.ts\";\n\n/**\n * Spawns a child agent and blocks until it completes.\n *\n * The child runs its own agentic loop with access to the parent's tools.\n * Orchestration tools are excluded when `recursive_depth` is 0 (the default)\n * and included when a positive depth is configured.\n */\nexport class SubagentOrchestrator extends Orchestrator {\n /**\n * Run a prompt as an autonomous child agent and return when it completes.\n *\n * @param prompt - The instruction for the child agent.\n * @param options - Configuration for the child agent (system prompt, tools, session forking, custom ID).\n */\n async execute(prompt: string, options?: SubagentOptions): Promise<SubagentResult> {\n return this.spawn(prompt, options).promise;\n }\n}\n","import { Orchestrator } from \"./orchestrator.ts\";\nimport type { BackgroundHandle, SubagentOptions, SubagentResult, WorkerState } from \"./types.ts\";\n\n/**\n * A single background worker agent.\n *\n * Each instance represents one background task. Call `execute` once to\n * start it, then use `status`, `done`, `collect`, and `cancel` to manage it.\n */\nexport class BackgroundOrchestrator extends Orchestrator {\n #handle?: BackgroundHandle;\n #status: \"idle\" | \"running\" | \"completed\" | \"cancelled\" = \"idle\";\n\n /**\n * Start the background worker. Can only be called once.\n *\n * @param prompt - The instruction for the worker.\n * @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).\n */\n execute(prompt: string, options?: SubagentOptions): void {\n if (this.#status !== \"idle\") {\n throw new Error(\"invalid state\");\n }\n this.#handle = this.spawn(prompt, options);\n this.#status = \"running\";\n this.#handle.promise.then(() => {\n if (this.#status === \"running\") {\n this.#status = \"completed\";\n }\n });\n }\n\n /**\n * The unique identifier of this worker. Throws if not started.\n */\n get id(): string {\n if (!this.#handle) {\n throw new Error(\"not started\");\n }\n return this.#handle.id;\n }\n\n /**\n * Current state: `idle`, `running`, `completed`, or `cancelled`.\n */\n get status() {\n return this.#status;\n }\n\n /**\n * `true` when the worker has completed or been cancelled.\n */\n get done(): boolean {\n return this.#status === \"completed\" || this.#status === \"cancelled\";\n }\n\n /**\n * Number of agentic turns (assistant messages) so far.\n */\n get rounds(): number {\n if (!this.#handle) {\n return 0;\n }\n return this.#handle.workflow.messages.filter((m) => m.role === \"assistant\").length;\n }\n\n /**\n * Total number of tool calls made across all rounds.\n */\n get tool_call_count(): number {\n if (!this.#handle) {\n return 0;\n }\n return this.#handle.workflow.messages\n .filter((m) => m.role === \"assistant\")\n .reduce((count, m) => count + m.content.filter((c) => c.type === \"tool\").length, 0);\n }\n\n /**\n * The text content of the most recent assistant message, if any.\n */\n get latest_message(): string | undefined {\n if (!this.#handle) {\n return undefined;\n }\n const messages = this.#handle.workflow.messages;\n for (let i = messages.length - 1; i >= 0; i--) {\n const msg = messages[i];\n if (msg.role === \"assistant\") {\n const text = msg.content?.find((c) => c.type === \"text\");\n if (text?.type === \"text\") {\n return text.text;\n }\n }\n }\n return undefined;\n }\n\n /**\n * Snapshot of the worker's current state.\n */\n get_state(): WorkerState {\n if (this.#status === \"idle\") {\n return {\n id: \"\",\n status: \"idle\",\n rounds: 0,\n tool_call_count: 0,\n };\n }\n return {\n id: this.id,\n status: this.#status,\n rounds: this.rounds,\n tool_call_count: this.tool_call_count,\n latest_message: this.latest_message,\n };\n }\n\n /**\n * Await completion and return the full result.\n */\n async collect(): Promise<SubagentResult> {\n if (!this.#handle) {\n throw new Error(\"not started\");\n }\n return this.#handle.promise;\n }\n\n /**\n * Cancel the running worker. Throws if not running.\n */\n cancel(): void {\n if (this.#status !== \"running\" || !this.#handle) {\n throw new Error(\"invalid state\");\n }\n if (this.#handle.workflow.state !== \"disposed\") {\n this.#handle.cancel();\n }\n this.#status = \"cancelled\";\n }\n\n /**\n * Dispose the worker, cancelling it if still running.\n */\n [Symbol.dispose](): void {\n if (this.#status === \"running\" && this.#handle) {\n if (this.#handle.workflow.state !== \"disposed\") {\n this.#handle.cancel();\n }\n this.#status = \"cancelled\";\n }\n this.#handle = undefined;\n }\n}\n","import type { Workflow, WorkflowManager } from \"@simulacra-ai/core\";\nimport { BackgroundOrchestrator } from \"./background-agent.ts\";\nimport type { SubagentOptions, WorkerState } from \"./types.ts\";\n\n/**\n * Manages a pool of background worker agents.\n *\n * Provides a registry for starting, listing, inspecting, cancelling,\n * and acknowledging (collecting + removing) completed workers.\n */\nexport class BackgroundAgentPool {\n #source: WorkflowManager | Workflow;\n readonly #recursive_depth: number;\n readonly #agents = new Map<string, BackgroundOrchestrator>();\n\n /**\n * @param source - A `WorkflowManager` or `Workflow` to spawn workers from.\n * @param options.recursive_depth - How many levels of recursive orchestration to allow in workers. Defaults to `0`.\n */\n constructor(source: WorkflowManager | Workflow, { recursive_depth = 0 } = {}) {\n this.#source = source;\n this.#recursive_depth = recursive_depth;\n }\n\n /**\n * Update the workflow/manager reference used to spawn new workers.\n * This must be called when a persisted pool is reused by a new workflow,\n * so that new workers are spawned from the current (live) workflow\n * instead of a previously disposed one.\n */\n update_source(source: WorkflowManager | Workflow): void {\n this.#source = source;\n }\n\n /**\n * Launch a new background worker and return its ID.\n *\n * @param prompt - The instruction for the worker.\n * @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).\n */\n start(prompt: string, options?: SubagentOptions): string {\n const agent = new BackgroundOrchestrator(this.#source, {\n recursive_depth: this.#recursive_depth,\n });\n agent.execute(prompt, options);\n this.#agents.set(agent.id, agent);\n return agent.id;\n }\n\n /**\n * Return all worker IDs in the pool.\n */\n list(): string[] {\n return [...this.#agents.keys()];\n }\n\n /**\n * Get state snapshots for the given worker IDs, or all workers if omitted.\n *\n * @param ids - Worker IDs to query. Omit to query all.\n */\n state(...ids: string[]): WorkerState[] {\n const target_ids = ids.length > 0 ? ids : [...this.#agents.keys()];\n return target_ids.map((id) => {\n const agent = this.#agents.get(id);\n if (!agent) {\n throw new Error(`no worker with id ${id}`);\n }\n return agent.get_state();\n });\n }\n\n /**\n * Cancel a running worker by ID.\n *\n * @param id - The worker ID to cancel.\n */\n cancel(id: string): void {\n const agent = this.#agents.get(id);\n if (!agent) {\n throw new Error(`no worker with id ${id}`);\n }\n agent.cancel();\n }\n\n /**\n * Pop completed workers from the pool and return their states. Skips workers still running.\n *\n * @param ids - Worker IDs to acknowledge. Omit to acknowledge all completed.\n */\n ack(...ids: string[]): WorkerState[] {\n const target_ids = ids.length > 0 ? ids : [...this.#agents.keys()];\n const results: WorkerState[] = [];\n for (const id of target_ids) {\n const agent = this.#agents.get(id);\n if (!agent) {\n continue;\n }\n if (!agent.done) {\n continue;\n }\n results.push(agent.get_state());\n this.#agents.delete(id);\n }\n return results;\n }\n\n /**\n * Dispose all workers in the pool, cancelling any that are still running.\n */\n [Symbol.dispose](): void {\n for (const agent of this.#agents.values()) {\n agent[Symbol.dispose]();\n }\n this.#agents.clear();\n }\n}\n","import { Orchestrator } from \"./orchestrator.ts\";\nimport type { BackgroundHandle, SubagentOptions, SubagentResult } from \"./types.ts\";\n\n/**\n * Runs multiple prompts concurrently, each as an independent child agent.\n * Returns when all complete.\n */\nexport class ParallelOrchestrator extends Orchestrator {\n /**\n * Run all tasks concurrently and return when every child agent has completed.\n *\n * @param tasks - Array of task configs, each with a `prompt` and optional `SubagentOptions`.\n */\n async execute(tasks: Array<{ prompt: string } & SubagentOptions>): Promise<SubagentResult[]> {\n const handles: BackgroundHandle[] = [];\n try {\n for (const { prompt, ...options } of tasks) {\n handles.push(this.spawn(prompt, options));\n }\n } catch (error) {\n for (const handle of handles) {\n try {\n handle.cancel();\n } catch {\n /* ignore */\n }\n }\n throw error;\n }\n return Promise.all(handles.map((h) => h.promise));\n }\n}\n","import type {\n Tool,\n ToolClass,\n ToolContext,\n ToolDefinition,\n ToolErrorResult,\n ToolSuccessResult,\n} from \"@simulacra-ai/core\";\nimport { BackgroundAgentPool } from \"../background-agent-pool.ts\";\nimport { ORCHESTRATION_DEPTH_KEY } from \"../orchestrator.ts\";\nimport type { WorkerState } from \"../types.ts\";\n\ntype BackgroundParams = {\n action: \"start\" | \"list\" | \"state\" | \"cancel\" | \"ack\";\n prompts?: string[];\n system?: string;\n fork_session?: boolean;\n ids?: string[];\n};\n\ntype BackgroundToolSuccess = ToolSuccessResult & {\n ids?: string[];\n workers?: WorkerState[];\n};\n\nclass BackgroundTaskPoolImpl implements Tool<BackgroundParams, BackgroundToolSuccess> {\n readonly #pool: BackgroundAgentPool;\n\n constructor(context: ToolContext) {\n const POOL_KEY = \"__background_agent_pool\";\n if (!context[POOL_KEY]) {\n const depth = (context[ORCHESTRATION_DEPTH_KEY] as number) ?? 0;\n context[POOL_KEY] = new BackgroundAgentPool(context.workflow, { recursive_depth: depth });\n }\n const pool = context[POOL_KEY] as BackgroundAgentPool;\n pool.update_source(context.workflow);\n this.#pool = pool;\n }\n\n async execute({ action, prompts, system, fork_session, ids }: BackgroundParams) {\n try {\n switch (action) {\n case \"start\": {\n if (!prompts?.length) {\n return {\n result: false as const,\n message: \"prompts is required for start\",\n } as ToolErrorResult;\n }\n const started_ids = prompts.map((p) => this.#pool.start(p, { system, fork_session }));\n return { result: true as const, ids: started_ids };\n }\n case \"list\": {\n const worker_ids = this.#pool.list();\n return { result: true as const, ids: worker_ids };\n }\n case \"state\": {\n const workers = ids?.length ? this.#pool.state(...ids) : this.#pool.state();\n return { result: true as const, workers };\n }\n case \"cancel\": {\n if (!ids?.length) {\n return {\n result: false as const,\n message: \"ids is required for cancel\",\n } as ToolErrorResult;\n }\n const errors: string[] = [];\n for (const id of ids) {\n try {\n this.#pool.cancel(id);\n } catch (e) {\n errors.push(`${id}: ${e instanceof Error ? e.message : String(e)}`);\n }\n }\n if (errors.length) {\n return {\n result: false as const,\n message: `some cancels failed: ${errors.join(\"; \")}`,\n } as ToolErrorResult;\n }\n return { result: true as const, ids };\n }\n case \"ack\": {\n const workers = ids?.length ? this.#pool.ack(...ids) : this.#pool.ack();\n return { result: true as const, workers };\n }\n default: {\n return {\n result: false as const,\n message: `unknown action: ${action}`,\n } as ToolErrorResult;\n }\n }\n } catch (error) {\n return {\n result: false as const,\n message: error instanceof Error ? error.message : String(error),\n error,\n } as ToolErrorResult;\n }\n }\n\n static get_definition(): ToolDefinition {\n return {\n name: \"background\",\n description:\n \"Manage background worker agents. Workers run independently with their own \" +\n \"conversations and tools. Use 'start' to launch workers, 'list' to see all \" +\n \"worker IDs, 'state' to check progress (rounds, tool calls, latest message), \" +\n \"'cancel' to stop workers, 'ack' to collect results from completed workers \" +\n \"and remove them from the pool.\",\n parameters: [\n {\n name: \"action\",\n description: \"The operation to perform\",\n type: \"string\",\n required: true,\n enum: [\"start\", \"list\", \"state\", \"cancel\", \"ack\"],\n },\n {\n name: \"prompts\",\n description:\n \"Instructions for workers to start, one per worker. Required and must not be empty when action is 'start'.\",\n type: \"array\",\n required: false,\n items: { type: \"string\", required: true },\n },\n {\n name: \"system\",\n description: \"Override the system prompt (only for 'start')\",\n type: \"string\",\n required: false,\n },\n {\n name: \"fork_session\",\n description: \"Start with current conversation history (only for 'start')\",\n type: \"boolean\",\n required: false,\n },\n {\n name: \"ids\",\n description: \"Worker IDs to operate on (for 'state', 'cancel', 'ack'). Omit for all.\",\n type: \"array\",\n required: false,\n items: { type: \"string\", required: true },\n },\n ],\n parallelizable: false,\n };\n }\n}\n\n/**\n * Tool that lets a model manage a pool of background worker agents. Supports\n * starting tasks, listing workers, checking state, cancelling, and collecting\n * results without blocking the main conversation.\n */\nexport const BackgroundTaskPool: ToolClass<BackgroundParams, BackgroundToolSuccess> =\n BackgroundTaskPoolImpl;\n","import type { SubagentResult } from \"../types.ts\";\n\n/**\n * Extract the final text response from a subagent result.\n *\n * @param result - The completed subagent result.\n */\nexport function extract_response(result: SubagentResult): string {\n for (let i = result.messages.length - 1; i >= 0; i--) {\n const message = result.messages[i];\n if (message.role !== \"assistant\") {\n continue;\n }\n const content = message.content;\n if (!content) {\n continue;\n }\n for (let j = content.length - 1; j >= 0; j--) {\n const block = content[j];\n if (block.type === \"text\" && block.text) {\n return block.text;\n }\n }\n }\n return \"(no text response)\";\n}\n","import type {\n Tool,\n ToolClass,\n ToolContext,\n ToolDefinition,\n ToolErrorResult,\n ToolSuccessResult,\n} from \"@simulacra-ai/core\";\nimport { ORCHESTRATION_DEPTH_KEY } from \"../orchestrator.ts\";\nimport { ParallelOrchestrator } from \"../parallel-agent.ts\";\nimport { extract_response } from \"./utils.ts\";\n\ntype ParallelParams = {\n prompts: string[];\n system?: string;\n fork_session?: boolean;\n};\n\ntype ParallelToolSuccess = ToolSuccessResult & {\n responses: Array<{\n id: string;\n response: string;\n end_reason: string;\n }>;\n};\n\nclass ParallelAgentTaskImpl implements Tool<ParallelParams, ParallelToolSuccess> {\n readonly #agent: ParallelOrchestrator;\n\n constructor(context: ToolContext) {\n const depth = (context[ORCHESTRATION_DEPTH_KEY] as number) ?? 0;\n this.#agent = new ParallelOrchestrator(context.workflow, { recursive_depth: depth });\n }\n\n async execute({ prompts, system, fork_session }: ParallelParams) {\n try {\n if (!prompts?.length) {\n return {\n result: false as const,\n message: \"prompts is required and must not be empty\",\n } as ToolErrorResult;\n }\n const tasks = prompts.map((prompt) => ({ prompt, system, fork_session }));\n const results = await this.#agent.execute(tasks);\n return {\n result: true as const,\n responses: results.map((r) => ({\n id: r.id,\n response: extract_response(r),\n end_reason: r.end_reason,\n })),\n };\n } catch (error) {\n return {\n result: false as const,\n message: error instanceof Error ? error.message : String(error),\n error,\n } as ToolErrorResult;\n }\n }\n\n static get_definition(): ToolDefinition {\n return {\n name: \"parallel\",\n description:\n \"Run multiple prompts as concurrent sub-agents. All agents start immediately \" +\n \"and the tool returns when every agent has completed. Each sub-agent has its \" +\n \"own conversation and access to the same tools. Use when tasks are independent \" +\n \"and can benefit from concurrent execution.\",\n parameters: [\n {\n name: \"prompts\",\n description: \"Array of instructions, one per sub-agent\",\n type: \"array\",\n required: true,\n items: { type: \"string\", required: true },\n },\n {\n name: \"system\",\n description: \"Override the system prompt for all sub-agents\",\n type: \"string\",\n required: false,\n },\n {\n name: \"fork_session\",\n description: \"If true, each sub-agent starts with the current conversation history\",\n type: \"boolean\",\n required: false,\n },\n ],\n parallelizable: false,\n };\n }\n}\n\n/**\n * Tool that lets a model run multiple subtasks concurrently, each handled by its\n * own subagent with a separate conversation. All subagents start immediately and\n * the tool returns once every subagent has completed.\n */\nexport const ParallelAgentTask: ToolClass<ParallelParams, ParallelToolSuccess> =\n ParallelAgentTaskImpl;\n","import type {\n Tool,\n ToolClass,\n ToolContext,\n ToolDefinition,\n ToolErrorResult,\n ToolSuccessResult,\n} from \"@simulacra-ai/core\";\nimport { ORCHESTRATION_DEPTH_KEY } from \"../orchestrator.ts\";\nimport { SubagentOrchestrator } from \"../subagent.ts\";\nimport { extract_response } from \"./utils.ts\";\n\ntype SubagentParams = {\n prompt: string;\n system?: string;\n fork_session?: boolean;\n};\n\ntype SubagentToolSuccess = ToolSuccessResult & {\n id: string;\n response: string;\n end_reason: string;\n};\n\nclass SubagentTaskImpl implements Tool<SubagentParams, SubagentToolSuccess> {\n readonly #agent: SubagentOrchestrator;\n\n constructor(context: ToolContext) {\n const depth = (context[ORCHESTRATION_DEPTH_KEY] as number) ?? 0;\n this.#agent = new SubagentOrchestrator(context.workflow, { recursive_depth: depth });\n }\n\n async execute({ prompt, system, fork_session }: SubagentParams) {\n try {\n const result = await this.#agent.execute(prompt, { system, fork_session });\n return {\n result: true as const,\n id: result.id,\n response: extract_response(result),\n end_reason: result.end_reason,\n };\n } catch (error) {\n return {\n result: false as const,\n message: error instanceof Error ? error.message : String(error),\n error,\n } as ToolErrorResult;\n }\n }\n\n static get_definition(): ToolDefinition {\n return {\n name: \"subagent\",\n description:\n \"Run a prompt as an autonomous sub-agent with its own conversation. \" +\n \"The sub-agent has access to the same tools and runs to completion, \" +\n \"returning its final response. Use for tasks that require independent \" +\n \"multi-step reasoning or tool use.\",\n parameters: [\n {\n name: \"prompt\",\n description: \"The instruction for the sub-agent\",\n type: \"string\",\n required: true,\n },\n {\n name: \"system\",\n description: \"Override the system prompt for this sub-agent\",\n type: \"string\",\n required: false,\n },\n {\n name: \"fork_session\",\n description:\n \"If true, the sub-agent starts with a copy of the current conversation history\",\n type: \"boolean\",\n required: false,\n },\n ],\n parallelizable: true,\n };\n }\n}\n\n/**\n * Tool that lets a model spawn an autonomous subagent with its own conversation\n * to handle a subtask independently. The subagent runs to completion and returns\n * its final response.\n */\nexport const SubagentTask: ToolClass<SubagentParams, SubagentToolSuccess> = SubagentTaskImpl;\n","import type { ToolClass } from \"@simulacra-ai/core\";\nexport { BackgroundTaskPool } from \"./background-task-pool.ts\";\nexport { ParallelAgentTask } from \"./parallel-agent-task.ts\";\nexport { SubagentTask } from \"./subagent-task.ts\";\nimport { BackgroundTaskPool } from \"./background-task-pool.ts\";\nimport { ParallelAgentTask } from \"./parallel-agent-task.ts\";\nimport { SubagentTask } from \"./subagent-task.ts\";\n\n/**\n * All orchestration tools as a ready-to-use toolkit.\n */\nexport const OrchestrationToolkit: ToolClass[] = [\n SubagentTask,\n ParallelAgentTask,\n BackgroundTaskPool,\n];\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA0C;AAI1C,IAAM,2BAA2B,oBAAI,IAAI,CAAC,YAAY,cA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AAA,QACjC,YAAY,OAAO;AAAA,MACrB;AAAA,IACF,SAAS,OAAO;AACd,aAAO;AAAA,QACL,QAAQ;AAAA,QACR,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAAA,QAC9D;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,OAAO,iBAAiC;AACtC,WAAO;AAAA,MACL,MAAM;AAAA,MACN,aACE;AAAA,MAIF,YAAY;AAAA,QACV;AAAA,UACE,MAAM;AAAA,UACN,aAAa;AAAA,UACb,MAAM;AAAA,UACN,UAAU;AAAA,QACZ;AAAA,QACA;AAAA,UACE,MAAM;AAAA,UACN,aAAa;AAAA,UACb,MAAM;AAAA,UACN,UAAU;AAAA,QACZ;AAAA,QACA;AAAA,UACE,MAAM;AAAA,UACN,aACE;AAAA,UACF,MAAM;AAAA,UACN,UAAU;AAAA,QACZ;AAAA,MACF;AAAA,MACA,gBAAgB;AAAA,IAClB;AAAA,EACF;AACF;AAOO,IAAM,eAA+D;;;AC9ErE,IAAM,uBAAoC;AAAA,EAC/C;AAAA,EACA;AAAA,EACA;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { ToolClass, Workflow, Message, WorkflowManager, Conversation, ToolSuccessResult } from '@simulacra-ai/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for spawning a subagent.
|
|
5
|
+
*/
|
|
6
|
+
interface SubagentOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The system prompt for the subagent's conversation.
|
|
9
|
+
*/
|
|
10
|
+
system?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The tools available to the subagent.
|
|
13
|
+
*/
|
|
14
|
+
toolkit?: ToolClass[];
|
|
15
|
+
/**
|
|
16
|
+
* Whether the subagent starts with a copy of the parent's conversation history.
|
|
17
|
+
*/
|
|
18
|
+
fork_session?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* A custom identifier for the subagent.
|
|
21
|
+
*/
|
|
22
|
+
id?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The result of a completed subagent execution.
|
|
26
|
+
*/
|
|
27
|
+
interface SubagentResult {
|
|
28
|
+
/**
|
|
29
|
+
* The unique identifier of the subagent.
|
|
30
|
+
*/
|
|
31
|
+
id: string;
|
|
32
|
+
/**
|
|
33
|
+
* The complete message history from the subagent's conversation.
|
|
34
|
+
*/
|
|
35
|
+
messages: readonly Readonly<Message>[];
|
|
36
|
+
/**
|
|
37
|
+
* The reason the subagent's execution ended.
|
|
38
|
+
*/
|
|
39
|
+
end_reason: "complete" | "cancel" | "error";
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A handle for managing a background subagent task.
|
|
43
|
+
*/
|
|
44
|
+
interface BackgroundHandle {
|
|
45
|
+
/**
|
|
46
|
+
* The unique identifier of the background task.
|
|
47
|
+
*/
|
|
48
|
+
readonly id: string;
|
|
49
|
+
/**
|
|
50
|
+
* The workflow instance running the background task.
|
|
51
|
+
*/
|
|
52
|
+
readonly workflow: Workflow;
|
|
53
|
+
/**
|
|
54
|
+
* A promise that resolves when the background task completes.
|
|
55
|
+
*/
|
|
56
|
+
readonly promise: Promise<SubagentResult>;
|
|
57
|
+
/**
|
|
58
|
+
* Whether the background task has finished execution.
|
|
59
|
+
*/
|
|
60
|
+
readonly done: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Cancels the background task.
|
|
63
|
+
*/
|
|
64
|
+
cancel(): void;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Status snapshot of a background worker.
|
|
68
|
+
*/
|
|
69
|
+
interface WorkerState {
|
|
70
|
+
/**
|
|
71
|
+
* The unique identifier of the worker.
|
|
72
|
+
*/
|
|
73
|
+
id: string;
|
|
74
|
+
/**
|
|
75
|
+
* The worker's current execution state.
|
|
76
|
+
*/
|
|
77
|
+
status: "idle" | "running" | "completed" | "cancelled";
|
|
78
|
+
/**
|
|
79
|
+
* Number of agentic turns (assistant messages) completed.
|
|
80
|
+
*/
|
|
81
|
+
rounds: number;
|
|
82
|
+
/**
|
|
83
|
+
* Total number of tool calls made across all rounds.
|
|
84
|
+
*/
|
|
85
|
+
tool_call_count: number;
|
|
86
|
+
/**
|
|
87
|
+
* The text content of the most recent assistant message, if any.
|
|
88
|
+
*/
|
|
89
|
+
latest_message?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Context key used to propagate the remaining orchestration depth to child agents.
|
|
94
|
+
*/
|
|
95
|
+
declare const ORCHESTRATION_DEPTH_KEY = "__orchestration_depth";
|
|
96
|
+
/**
|
|
97
|
+
* Base class for orchestration patterns.
|
|
98
|
+
*
|
|
99
|
+
* Accepts a `WorkflowManager` (for programmatic use) or a `Workflow`
|
|
100
|
+
* (for tool integration). Provides the shared child-spawning logic
|
|
101
|
+
* that all orchestration patterns build on.
|
|
102
|
+
*
|
|
103
|
+
* By default, `recursive_depth` is `0`, which strips orchestration tools
|
|
104
|
+
* from child agents to prevent nesting. Set to a positive number to allow
|
|
105
|
+
* that many levels of recursive orchestration, or `-1` for unlimited depth.
|
|
106
|
+
*/
|
|
107
|
+
declare abstract class Orchestrator {
|
|
108
|
+
#private;
|
|
109
|
+
/**
|
|
110
|
+
* @param source - A `WorkflowManager` or `Workflow` to spawn children from.
|
|
111
|
+
* @param options.recursive_depth - How many levels of recursive orchestration to allow. `0` (default) strips orchestration tools from children, `-1` allows unlimited nesting.
|
|
112
|
+
*/
|
|
113
|
+
constructor(source: WorkflowManager | Workflow, { recursive_depth }?: {
|
|
114
|
+
recursive_depth?: number | undefined;
|
|
115
|
+
});
|
|
116
|
+
/**
|
|
117
|
+
* The conversation associated with the orchestrator.
|
|
118
|
+
*/
|
|
119
|
+
protected get conversation(): Conversation;
|
|
120
|
+
/**
|
|
121
|
+
* The parent workflow, if available. Used to establish parent-child workflow relationships.
|
|
122
|
+
*/
|
|
123
|
+
protected get parent_workflow(): Workflow | undefined;
|
|
124
|
+
/**
|
|
125
|
+
* Spawn a child agent with its own conversation and workflow.
|
|
126
|
+
*
|
|
127
|
+
* Creates a child conversation, assigns tools (stripping orchestration
|
|
128
|
+
* tools when depth is exhausted), and starts the workflow. Returns a
|
|
129
|
+
* handle for awaiting, inspecting, or cancelling the child.
|
|
130
|
+
*
|
|
131
|
+
* @param prompt - The instruction to send to the child agent.
|
|
132
|
+
* @param options - Configuration for the child agent (system prompt, tools, session forking, custom ID).
|
|
133
|
+
*/
|
|
134
|
+
protected spawn(prompt: string, options?: SubagentOptions): BackgroundHandle;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Spawns a child agent and blocks until it completes.
|
|
139
|
+
*
|
|
140
|
+
* The child runs its own agentic loop with access to the parent's tools.
|
|
141
|
+
* Orchestration tools are excluded when `recursive_depth` is 0 (the default)
|
|
142
|
+
* and included when a positive depth is configured.
|
|
143
|
+
*/
|
|
144
|
+
declare class SubagentOrchestrator extends Orchestrator {
|
|
145
|
+
/**
|
|
146
|
+
* Run a prompt as an autonomous child agent and return when it completes.
|
|
147
|
+
*
|
|
148
|
+
* @param prompt - The instruction for the child agent.
|
|
149
|
+
* @param options - Configuration for the child agent (system prompt, tools, session forking, custom ID).
|
|
150
|
+
*/
|
|
151
|
+
execute(prompt: string, options?: SubagentOptions): Promise<SubagentResult>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A single background worker agent.
|
|
156
|
+
*
|
|
157
|
+
* Each instance represents one background task. Call `execute` once to
|
|
158
|
+
* start it, then use `status`, `done`, `collect`, and `cancel` to manage it.
|
|
159
|
+
*/
|
|
160
|
+
declare class BackgroundOrchestrator extends Orchestrator {
|
|
161
|
+
#private;
|
|
162
|
+
/**
|
|
163
|
+
* Start the background worker. Can only be called once.
|
|
164
|
+
*
|
|
165
|
+
* @param prompt - The instruction for the worker.
|
|
166
|
+
* @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).
|
|
167
|
+
*/
|
|
168
|
+
execute(prompt: string, options?: SubagentOptions): void;
|
|
169
|
+
/**
|
|
170
|
+
* The unique identifier of this worker. Throws if not started.
|
|
171
|
+
*/
|
|
172
|
+
get id(): string;
|
|
173
|
+
/**
|
|
174
|
+
* Current state: `idle`, `running`, `completed`, or `cancelled`.
|
|
175
|
+
*/
|
|
176
|
+
get status(): "idle" | "running" | "completed" | "cancelled";
|
|
177
|
+
/**
|
|
178
|
+
* `true` when the worker has completed or been cancelled.
|
|
179
|
+
*/
|
|
180
|
+
get done(): boolean;
|
|
181
|
+
/**
|
|
182
|
+
* Number of agentic turns (assistant messages) so far.
|
|
183
|
+
*/
|
|
184
|
+
get rounds(): number;
|
|
185
|
+
/**
|
|
186
|
+
* Total number of tool calls made across all rounds.
|
|
187
|
+
*/
|
|
188
|
+
get tool_call_count(): number;
|
|
189
|
+
/**
|
|
190
|
+
* The text content of the most recent assistant message, if any.
|
|
191
|
+
*/
|
|
192
|
+
get latest_message(): string | undefined;
|
|
193
|
+
/**
|
|
194
|
+
* Snapshot of the worker's current state.
|
|
195
|
+
*/
|
|
196
|
+
get_state(): WorkerState;
|
|
197
|
+
/**
|
|
198
|
+
* Await completion and return the full result.
|
|
199
|
+
*/
|
|
200
|
+
collect(): Promise<SubagentResult>;
|
|
201
|
+
/**
|
|
202
|
+
* Cancel the running worker. Throws if not running.
|
|
203
|
+
*/
|
|
204
|
+
cancel(): void;
|
|
205
|
+
/**
|
|
206
|
+
* Dispose the worker, cancelling it if still running.
|
|
207
|
+
*/
|
|
208
|
+
[Symbol.dispose](): void;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Manages a pool of background worker agents.
|
|
213
|
+
*
|
|
214
|
+
* Provides a registry for starting, listing, inspecting, cancelling,
|
|
215
|
+
* and acknowledging (collecting + removing) completed workers.
|
|
216
|
+
*/
|
|
217
|
+
declare class BackgroundAgentPool {
|
|
218
|
+
#private;
|
|
219
|
+
/**
|
|
220
|
+
* @param source - A `WorkflowManager` or `Workflow` to spawn workers from.
|
|
221
|
+
* @param options.recursive_depth - How many levels of recursive orchestration to allow in workers. Defaults to `0`.
|
|
222
|
+
*/
|
|
223
|
+
constructor(source: WorkflowManager | Workflow, { recursive_depth }?: {
|
|
224
|
+
recursive_depth?: number | undefined;
|
|
225
|
+
});
|
|
226
|
+
/**
|
|
227
|
+
* Update the workflow/manager reference used to spawn new workers.
|
|
228
|
+
* This must be called when a persisted pool is reused by a new workflow,
|
|
229
|
+
* so that new workers are spawned from the current (live) workflow
|
|
230
|
+
* instead of a previously disposed one.
|
|
231
|
+
*/
|
|
232
|
+
update_source(source: WorkflowManager | Workflow): void;
|
|
233
|
+
/**
|
|
234
|
+
* Launch a new background worker and return its ID.
|
|
235
|
+
*
|
|
236
|
+
* @param prompt - The instruction for the worker.
|
|
237
|
+
* @param options - Configuration for the worker (system prompt, tools, session forking, custom ID).
|
|
238
|
+
*/
|
|
239
|
+
start(prompt: string, options?: SubagentOptions): string;
|
|
240
|
+
/**
|
|
241
|
+
* Return all worker IDs in the pool.
|
|
242
|
+
*/
|
|
243
|
+
list(): string[];
|
|
244
|
+
/**
|
|
245
|
+
* Get state snapshots for the given worker IDs, or all workers if omitted.
|
|
246
|
+
*
|
|
247
|
+
* @param ids - Worker IDs to query. Omit to query all.
|
|
248
|
+
*/
|
|
249
|
+
state(...ids: string[]): WorkerState[];
|
|
250
|
+
/**
|
|
251
|
+
* Cancel a running worker by ID.
|
|
252
|
+
*
|
|
253
|
+
* @param id - The worker ID to cancel.
|
|
254
|
+
*/
|
|
255
|
+
cancel(id: string): void;
|
|
256
|
+
/**
|
|
257
|
+
* Pop completed workers from the pool and return their states. Skips workers still running.
|
|
258
|
+
*
|
|
259
|
+
* @param ids - Worker IDs to acknowledge. Omit to acknowledge all completed.
|
|
260
|
+
*/
|
|
261
|
+
ack(...ids: string[]): WorkerState[];
|
|
262
|
+
/**
|
|
263
|
+
* Dispose all workers in the pool, cancelling any that are still running.
|
|
264
|
+
*/
|
|
265
|
+
[Symbol.dispose](): void;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Runs multiple prompts concurrently, each as an independent child agent.
|
|
270
|
+
* Returns when all complete.
|
|
271
|
+
*/
|
|
272
|
+
declare class ParallelOrchestrator extends Orchestrator {
|
|
273
|
+
/**
|
|
274
|
+
* Run all tasks concurrently and return when every child agent has completed.
|
|
275
|
+
*
|
|
276
|
+
* @param tasks - Array of task configs, each with a `prompt` and optional `SubagentOptions`.
|
|
277
|
+
*/
|
|
278
|
+
execute(tasks: Array<{
|
|
279
|
+
prompt: string;
|
|
280
|
+
} & SubagentOptions>): Promise<SubagentResult[]>;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
type BackgroundParams = {
|
|
284
|
+
action: "start" | "list" | "state" | "cancel" | "ack";
|
|
285
|
+
prompts?: string[];
|
|
286
|
+
system?: string;
|
|
287
|
+
fork_session?: boolean;
|
|
288
|
+
ids?: string[];
|
|
289
|
+
};
|
|
290
|
+
type BackgroundToolSuccess = ToolSuccessResult & {
|
|
291
|
+
ids?: string[];
|
|
292
|
+
workers?: WorkerState[];
|
|
293
|
+
};
|
|
294
|
+
/**
|
|
295
|
+
* Tool that lets a model manage a pool of background worker agents. Supports
|
|
296
|
+
* starting tasks, listing workers, checking state, cancelling, and collecting
|
|
297
|
+
* results without blocking the main conversation.
|
|
298
|
+
*/
|
|
299
|
+
declare const BackgroundTaskPool: ToolClass<BackgroundParams, BackgroundToolSuccess>;
|
|
300
|
+
|
|
301
|
+
type ParallelParams = {
|
|
302
|
+
prompts: string[];
|
|
303
|
+
system?: string;
|
|
304
|
+
fork_session?: boolean;
|
|
305
|
+
};
|
|
306
|
+
type ParallelToolSuccess = ToolSuccessResult & {
|
|
307
|
+
responses: Array<{
|
|
308
|
+
id: string;
|
|
309
|
+
response: string;
|
|
310
|
+
end_reason: string;
|
|
311
|
+
}>;
|
|
312
|
+
};
|
|
313
|
+
/**
|
|
314
|
+
* Tool that lets a model run multiple subtasks concurrently, each handled by its
|
|
315
|
+
* own subagent with a separate conversation. All subagents start immediately and
|
|
316
|
+
* the tool returns once every subagent has completed.
|
|
317
|
+
*/
|
|
318
|
+
declare const ParallelAgentTask: ToolClass<ParallelParams, ParallelToolSuccess>;
|
|
319
|
+
|
|
320
|
+
type SubagentParams = {
|
|
321
|
+
prompt: string;
|
|
322
|
+
system?: string;
|
|
323
|
+
fork_session?: boolean;
|
|
324
|
+
};
|
|
325
|
+
type SubagentToolSuccess = ToolSuccessResult & {
|
|
326
|
+
id: string;
|
|
327
|
+
response: string;
|
|
328
|
+
end_reason: string;
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Tool that lets a model spawn an autonomous subagent with its own conversation
|
|
332
|
+
* to handle a subtask independently. The subagent runs to completion and returns
|
|
333
|
+
* its final response.
|
|
334
|
+
*/
|
|
335
|
+
declare const SubagentTask: ToolClass<SubagentParams, SubagentToolSuccess>;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* All orchestration tools as a ready-to-use toolkit.
|
|
339
|
+
*/
|
|
340
|
+
declare const OrchestrationToolkit: ToolClass[];
|
|
341
|
+
|
|
342
|
+
export { BackgroundAgentPool, type BackgroundHandle, BackgroundOrchestrator, BackgroundTaskPool, ORCHESTRATION_DEPTH_KEY, OrchestrationToolkit, Orchestrator, ParallelAgentTask, ParallelOrchestrator, type SubagentOptions, SubagentOrchestrator, type SubagentResult, SubagentTask, type WorkerState };
|