Delegation¶
Background task delegation via child rooms. See the Agent Delegation guide for usage examples.
Task Runner¶
TaskRunner ¶
Bases: ABC
ABC for executing delegated tasks in the background.
Follows the same pluggable-backend pattern as ConversationStore
and RoomLockManager.
submit
abstractmethod
async
¶
Start background execution of task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kit
|
RoomKit
|
The RoomKit instance (used to interact with rooms/channels). |
required |
task
|
DelegatedTask
|
The delegated task handle. |
required |
context
|
dict[str, Any] | None
|
Optional context passed to the agent. |
None
|
on_complete
|
OnCompleteCallback | None
|
Callback invoked when the task finishes. |
None
|
cancel
abstractmethod
async
¶
Cancel a running task. Returns True if found and cancelled.
InMemoryTaskRunner ¶
Task Models¶
DelegatedTask ¶
Handle for a running delegated task.
Not a Pydantic model — holds mutable state and an asyncio.Event
for callers that want to block until the task completes.
DelegatedTaskResult ¶
Bases: BaseModel
Result of a completed delegated task.
Tool Integration¶
DELEGATE_TOOL
module-attribute
¶
DELEGATE_TOOL = AITool(name='delegate_task', description='Delegate a task to a background agent. The agent works in its own room and you can continue the current conversation. Use when: a task needs a specialist, the user wants something done in the background, or you need to run work in parallel.', parameters={'type': 'object', 'properties': {'agent': {'type': 'string', 'description': 'Target agent ID for the delegated task'}, 'task': {'type': 'string', 'description': 'Clear description of what the agent should do'}, 'context': {'type': 'object', 'description': 'Optional context to pass to the agent (e.g. email, repo URL)'}, 'share_channels': {'type': 'array', 'items': {'type': 'string'}, 'description': 'Channel IDs from the current room to share with the background agent'}}, 'required': ['agent', 'task']})
build_delegate_tool ¶
Build a delegate tool with constrained agent enum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
list[tuple[str, str | None]]
|
List of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
An |
AITool
|
class: |
AITool
|
restricting the AI to only the listed agent IDs. |
DelegateHandler ¶
DelegateHandler(kit, *, notify=None, default_share_channels=None, delivery_strategy=None, cache=None, serialize_per_room=False)
Processes delegate_task tool calls by calling kit.delegate().
Supports optional dedup via :class:CompletedTaskCache and
per-room concurrency control via asyncio locks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kit
|
RoomKit
|
The RoomKit instance. |
required |
notify
|
str | None
|
Channel ID to notify on completion (defaults to calling agent). |
None
|
default_share_channels
|
list[str] | None
|
Channels shared with background agents by default. |
None
|
delivery_strategy
|
DeliveryStrategy | None
|
How to deliver results back proactively. |
None
|
cache
|
CompletedTaskCache | None
|
Optional completed-task cache for dedup. |
None
|
serialize_per_room
|
bool
|
If True, only one delegation per room at a time. |
False
|
setup_delegation ¶
Wire delegation into an AIChannel's tool chain.
Injects the delegate tool and wraps the tool handler to intercept
delegate_task calls. Same pattern as setup_handoff().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
AIChannel
|
The AI channel to wire delegation into. |
required |
handler
|
DelegateHandler
|
The delegate handler that processes tool calls. |
required |
tool
|
AITool | None
|
Optional custom delegate tool (e.g. from :func: |
None
|
setup_realtime_delegation ¶
Wire delegation into a RealtimeVoiceChannel's tool chain.
Injects the delegate tool dict into channel._tools and wraps
channel._tool_handler to intercept delegate_task calls.
Room ID is resolved from the current voice session via
get_current_voice_session() + channel.session_rooms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
RealtimeVoiceChannel
|
The realtime voice channel to wire delegation into. |
required |
handler
|
DelegateHandler
|
The delegate handler that processes tool calls. |
required |
tool
|
AITool | None
|
Optional custom delegate tool (e.g. from :func: |
None
|
Delegation State Tracking¶
CompletedTaskCache ¶
In-memory TTL cache for completed delegation results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ttl_seconds
|
float
|
Seconds before a cached result expires. Defaults to 300 (5 min). |
300.0
|
recent_context ¶
Return summaries of recent tasks for a room (for context injection).
Returns task descriptions from newest to oldest, up to limit. Expired entries are skipped.
Delivery Strategies¶
DeliveryStrategy ¶
Bases: ABC
Controls when and how content is delivered to a channel.
Immediate ¶
WaitForIdle ¶
Bases: DeliveryStrategy
Wait for TTS/speech to finish, then send.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buffer
|
float
|
Extra seconds to wait after playback ends (default 1.0). |
1.0
|
playback_timeout
|
float
|
Max seconds to wait for playback (default 15.0). |
15.0
|
Queued ¶
Bases: DeliveryStrategy
Add to queue, deliver at next idle window.
Multiple deliveries are batched into a single message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buffer
|
float
|
Extra seconds to wait after playback ends (default 1.0). |
1.0
|
playback_timeout
|
float
|
Max seconds to wait for playback (default 15.0). |
15.0
|
separator
|
str
|
Text between batched items (default newline). |
'\n\n'
|