82 lines
2.6 KiB
Python
82 lines
2.6 KiB
Python
"""Encode typed outbound events for the WebUI wire protocol."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
from typing import Any, Literal, NotRequired, TypeAlias, TypedDict
|
|
|
|
from nanobot.bus.outbound_events import RecoveryStateEvent, TurnEndEvent
|
|
from nanobot.webui.metadata import WEBUI_TURN_METADATA_KEY
|
|
|
|
|
|
class _ChatWirePayload(TypedDict):
|
|
chat_id: str
|
|
turn_id: NotRequired[str]
|
|
|
|
|
|
class RecoveryStateWirePayload(_ChatWirePayload):
|
|
event: Literal["recovery_state"]
|
|
status: str
|
|
recovery_id: str
|
|
attempts: int
|
|
reason: NotRequired[str]
|
|
can_continue: NotRequired[bool]
|
|
|
|
|
|
class TurnEndWirePayload(_ChatWirePayload):
|
|
event: Literal["turn_end"]
|
|
latency_ms: NotRequired[int]
|
|
goal_state: NotRequired[dict[str, Any]]
|
|
usage: NotRequired[dict[str, int]]
|
|
round_usages: NotRequired[list[dict[str, int]]]
|
|
context_window_tokens: NotRequired[int]
|
|
|
|
|
|
WebUIWirePayload: TypeAlias = RecoveryStateWirePayload | TurnEndWirePayload
|
|
WebUIWirePersistence: TypeAlias = Literal["transient", "turn_complete"]
|
|
|
|
|
|
def encode_recovery_state(
|
|
chat_id: str,
|
|
event: RecoveryStateEvent,
|
|
) -> RecoveryStateWirePayload:
|
|
"""Project one transient recovery transition onto its stable wire shape."""
|
|
payload: RecoveryStateWirePayload = {
|
|
"event": "recovery_state",
|
|
"chat_id": chat_id,
|
|
"status": event.status,
|
|
"recovery_id": event.recovery_id,
|
|
"attempts": event.attempts,
|
|
}
|
|
if event.reason:
|
|
payload["reason"] = event.reason
|
|
if event.can_continue is not None:
|
|
payload["can_continue"] = event.can_continue
|
|
return payload
|
|
|
|
|
|
def encode_turn_end(
|
|
chat_id: str,
|
|
event: TurnEndEvent,
|
|
metadata: Mapping[str, object] | None = None,
|
|
) -> TurnEndWirePayload:
|
|
"""Project a completed turn without leaking internal metadata onto the wire."""
|
|
payload: TurnEndWirePayload = {
|
|
"event": "turn_end",
|
|
"chat_id": chat_id,
|
|
}
|
|
turn_id = (metadata or {}).get(WEBUI_TURN_METADATA_KEY)
|
|
if isinstance(turn_id, str) and turn_id:
|
|
payload["turn_id"] = turn_id
|
|
if event.latency_ms is not None:
|
|
payload["latency_ms"] = int(event.latency_ms)
|
|
if event.goal_state is not None:
|
|
payload["goal_state"] = event.goal_state
|
|
if event.usage is not None:
|
|
payload["usage"] = event.usage.to_turn_dict()
|
|
if event.round_usages:
|
|
payload["round_usages"] = [item.to_turn_dict() for item in event.round_usages]
|
|
if event.context_window_tokens is not None:
|
|
payload["context_window_tokens"] = int(event.context_window_tokens)
|
|
return payload
|