Expand description
Transport-neutral runtime invocation facade.
This module exposes a Socket.IO-inspired emit / on interaction model
over AgentRuntime without binding to any wire protocol. Transport
adapters (Socket.IO, gRPC, SSE, …) build on top of these types; the core
runtime stays free of transport concerns.
§emit / on semantics
emit starts a run — it builds an
EmitRequest, converts it to a RunRequest, and hands it to
AgentRuntime::run. Returns RunOutput when the run completes.
on subscribes to the runtime’s event bus.
Every event from any run managed by the same AgentRuntime is
received; the EventKind filter discards non-matching events before
the handler fires. Handlers run on independently spawned tasks so a
slow handler does not block event delivery for other subscribers.
§Event ordering
A typical agent run produces events in this order:
RunStarted
→ ContextBuilt
→ ModelStarted → TextDelta* → ToolCallStarted? → ToolCallDelta* →
ToolCallCompleted? → ToolExecutionStarted? → ToolExecutionFinished?
→ AssistantMessageCommitted → UsageRecorded
→ (loop: ContextBuilt → …)
→ RunCompleted | RunFailed | RunCancelledEvery event carries a run_id so handlers can
correlate events belonging to the same run.
§Event delivery guarantees
- At-least-once: events are delivered via a
tokio::sync::broadcastchannel. Lagging receivers miss events (signaled viatokio::sync::broadcast::error::RecvError::Lagged), which is silently skipped — the receiver misses those events. - No backpressure:
emitdoes not wait foronhandlers to complete. The runtime publishes events to the broadcast channel and proceeds immediately. - Ordered per-run: events from a single run are always published in order. Events from concurrent runs may interleave.
- Handler concurrency: if
Control::set_concurrency_limitis set, a semaphore gates how many handler tasks may run concurrently. Without a limit, every matching event spawns an unbounded task.
§Handler lifecycle
on returns an InvocationHandle. The
underlying listener task runs until:
- The
InvocationHandleis dropped (aborts the task), or - The broadcast channel is closed (all
AgentRuntimesenders dropped).
Handler tasks already dispatched before the listener stops run to completion.
§Cancellation
Cancellation is cooperative. Control::cancel sets a flag.
emit checks the flag before invoking the
request closure and before calling AgentRuntime::run; the listener
loop inside on checks it before every event
dispatch. The underlying runtime does not yet support hard cancellation
of an in-flight model call.
§Core surface
EmitRequestbuilds a run request and hands it toAgentRuntime::run.RuntimeInvocation::onsubscribes to the runtime event bus and dispatches matching events to user handlers on independent tasks.Controlcarries cooperative cancellation/timeout/concurrency state, plus a type-erased extension map for injecting shared data into handlers.InvocationHandleaborts its listener on drop.
§Transport-adapter surface
InvocationEvent::Chat and the Chat* variants of EventKind are
defined here so adapters reuse the same matching logic, but the core
on implementation only surfaces AgentEvents from
AgentRuntime::subscribe; chat-stream events require a streaming
chat adapter that populates the Chat variant.
Structs§
- Control
- Cooperative lifecycle handle shared across an invocation.
- Emit
Request - Transport-neutral request for a single runtime invocation.
- File
Session Data Store - File-system
SessionDataStorebacked by JSON files. - Invocation
Handle - Handle to a background listener task started by
RuntimeInvocation::on. - Invocation
Session - Invocation-time session context with temporary KV storage.
- Memory
Session Data Store - In-memory
SessionDataStorebacked by aHashMap. - Runtime
Invocation - Transport-neutral invocation facade over
AgentRuntime.
Enums§
- Event
Kind - Kind of event a caller can subscribe to via
RuntimeInvocation::on. - Invocation
Error - Errors raised by the invocation facade.
- Invocation
Event - Unified event envelope wrapping runtime and chat-stream events.
- Session
Data Error - Errors returned by
SessionDataStoreimplementations.
Traits§
- Session
Data Store - Pluggable backend for per-session temporary key-value data.