Overview
TheAgentOrchestrator class manages multiple AI agents running concurrently. It handles agent spawning, event multiplexing, and relay request management for permission-based tool execution.
Key Features
- Spawn multiple agents that run concurrently
- Multiplex events from all agents into a single stream
- Manage relay requests for permission-based tool execution
- Support for agent hierarchies with subagent spawning
- Automatic pause/resume when awaiting relay responses
Constructor
Optional harness module. Defaults to the agent harness wrapping OpenRouter.
Methods
spawn()
Spawn a new agent and begin event production.Parameters for agent invocation
The LLM model to use (e.g.,
"openai/gpt-4", "anthropic/claude-3-5-sonnet")Array of conversation messages
Available tools for the agent to use
Tool execution permissions with allowlist/allowOnce/deny rules
Additional context to inject into the conversation
Environment context including parentId, spawn function, and fileTime
Unique identifier for the spawned agent (UUID v7)
events()
Stream all events from all registered agents.The agent that produced this event
The event data. Relay events have the
respond callback stripped - use resolveRelay() instead.resolveRelay()
Resolve a pending relay request and resume the agent.The ID of the relay event awaiting resolution
The resolution response
Whether to approve or deny the tool execution
If true, derive a permission and add to allowlist for future auto-approval
Reason for denial (only used when approved is false)
Returns true if the relay was found and resolved, false otherwise
kill()
Terminate an agent and clean up its resources.The ID of the agent to terminate
Types
PendingRelay
Internal state for a relay awaiting resolution.ResolveResponse
Response type for relay resolution.ConsumerHarnessEvent
HarnessEvent with relay respond callbacks stripped.Complete Example
Relay Flow
When an agent needs permission to execute a tool:- Agent is automatically paused
- Relay event is yielded with tool details
- The
respondcallback is stashed internally - Consumer calls
resolveRelay()to approve/deny - Agent is resumed and continues execution
Always-Allow Pattern
When resolving withalways: true, the orchestrator:
- Calls the tool’s
derivePermission()function if available - Adds the derived permission to the agent’s allowlist
- Future matching tool calls are auto-approved without relay events
