Skip to content

latent.serve.events

Events an agent handles inside its serving pod (ADR-015 D5).

An agent method decorated with :func:event handles one named event. Studio's worker fires it by calling POST /v1/events/{source}/{name} on the version the conversation is served by; the handler returns an :class:EventResult, which travels as the contract's EventDecision. Handlers reach Studio's timeboxes and scratchpad through :class:StudioEvents (the pod-key routes), never through Studio's database.

Classes

EventContext

EventContext(conversation_id: str, thread_id: str | None, endpoint_id: str | None, channel: str | None, event_name: str, source: EventSource, studio: StudioEvents, note: str | None = None, payload: dict[str, Any] | None = None, event_id: str | None = None)

What a handler knows about the firing.

EventResult

EventResult(decision: EventNoop | EventSend | EventFinish | EventRunAgent)

What a handler decided; build it with the class methods.

EventSpec

EventSpec(name: str, source: EventSource, method: str)

One @event declaration: the wire name, its source and the method.

Secret

Secret(value: Any, key: str)

A scratchpad value stored with secret=True: readable, never rendered.

Mirrors agent-studio's in-worker Secret (agent_studio_shared). A tool's result is stringified into the model's context and the trace, so str, repr and format render <redacted:key>, also inside a dict, list or f-string. reveal() is the one unwrap.

StudioAuthError

StudioAuthError()

Studio refused the pod's project key (401/403).

StudioEvents

StudioEvents(conversation_id: str, studio_url: str | None = None, api_key: str | None = None, client: httpx.AsyncClient | None = None)

Timebox and scratchpad access for one conversation, over Studio's pod routes.

Authenticates with the pod's project key (AGENT_STUDIO_API_KEY) against AGENT_STUDIO_URL. Every failure raises: a handler that armed nothing must not believe it did.

Functions

agent_events

agent_events(agent_cls: type) -> dict[tuple[EventSource, str], EventSpec]

Every @event handler of agent_cls keyed by (source, name).

event

event(name: str, source: EventSource = 'scheduled') -> Callable[[Callable[..., Awaitable[EventResult]]], Callable[..., Awaitable[EventResult]]]

Mark an async agent method as the handler of event name.

source="scheduled" handles a timebox firing; "external" an event a caller posted to Studio. The method receives an :class:EventContext and returns an :class:EventResult. Declared events are listed in the agent's manifest, and Studio calls the pod only for a version that declares the event.

Methods

EventResult.finish

finish(text: str) -> EventResult

Send text and finish the conversation.

EventResult.noop

noop() -> EventResult

Do nothing.

EventResult.run_agent

run_agent(prompt: str) -> EventResult

Run an agent turn with prompt as its input.

EventResult.send

send(text: str) -> EventResult

Send text to the conversation as the assistant.

EventResult.to_wire

to_wire() -> dict[str, Any]

The contract EventDecision as JSON-ready data.

Secret.reveal

reveal() -> Any

The plaintext; the one place a secret becomes renderable.

StudioEvents.arm

arm(name: str, seconds: float, note: str | None = None, idempotency_key: str | None = None) -> str

Arm timebox name to fire in seconds; return the firing nonce.

A retry that passes the same idempotency_key gets the first arm's nonce and arms no second timer.

StudioEvents.cancel

cancel(name: str) -> None

Cancel timebox name.

StudioEvents.peek

peek(name: str) -> dict[str, Any] | None

The state of timebox name, or None when it was never armed.

StudioEvents.scratch_get

scratch_get(key: str) -> Any | None

The scratchpad value under key, or None when absent.

A value stored as a secret comes back as a redacting :class:Secret.

StudioEvents.scratch_remove

scratch_remove(key: str) -> None

Remove key from the scratchpad.

StudioEvents.scratch_set

scratch_set(key: str, value: Any, secret: bool | None = None) -> None

Store value (JSON) under key.

secret=True, or a :class:Secret value, stores it as a secret; secret=False stores it plain, even for a :class:Secret value. Left out, Studio keeps the flag the key already has.

Attributes

EVENT_SOURCES

EventSource