yera.runtime.router

Route output-event blocks to target-specific handler instances.

The router owns handler lifetime. It creates one handler for each open block, passes every chunk with that block identity to the same instance, and closes the handler when the block's terminal event arrives. Parent identities bind nested handlers to their still-open container handler.

Symbols

class EventRouter — Route ordered output events through block-scoped handlers.

EventRouter

Route ordered output events through block-scoped handlers.

The first ordinary event for a block creates and enters a handler. Later chunks with the same block identity reuse it, allowing streaming handlers to retain state. A terminal block event exits and removes that handler.

Prompt and lifecycle events are handled as one-shot boundaries and returned to the caller. Lifecycle events first close every open handler. If routing raises, all open handlers are unwound before the exception propagates.

Methods

__init__ — Initialize a router with no open block handlers.
process_stream — Route events until the host must handle a prompt or lifecycle event.

EventRouter.__init__

__init__(
    handlers: EventHandlerRegistry,
) → None

Initialize a router with no open block handlers.

Parameters

handlers
type: EventHandlerRegistry

Registry used to construct a handler for each event block.

EventRouter.process_stream

process_stream(
    events: Iterator[OutputEvent],
) → OutputEvent

Route events until the host must handle a prompt or lifecycle event.

Ordinary block chunks are forwarded to their block-scoped handlers. Terminal block events close their handlers without being rendered. A prompt event is rendered once and returned so the caller can collect input. A lifecycle event closes all open handlers, is rendered once, and is returned to the caller.

Parameters

events
type: Iterator[OutputEvent]

Ordered output events from the app runtime.

Returns

type: OutputEvent

The prompt or lifecycle event that paused routing.

Raises

StreamEndedError

If the iterator ends without a prompt or lifecycle boundary.