refactor(webui): extract outbound wire encoding (NAN-102)

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-04 17:09:45 +08:00
1 parent e2df7df9f4
commit e93b92c2d0
5 files changed
+228 -90

No files matched your search

+23 -23
View File
@@ -25,14 +25,18 @@ from nanobot.webui.metadata import (
WEBUI_SYSTEM_COMMAND_TURN_PREFIX,
WEBUI_TURN_METADATA_KEY,
)
from nanobot.webui.outbound_wire import (
WebUIWirePayload,
WebUIWirePersistence,
encode_recovery_state,
encode_turn_end,
)
from nanobot.webui.session_identity import webui_session_key
from nanobot.webui.session_projection import WebUISessionProjection
if TYPE_CHECKING:
from websockets.asyncio.server import ServerConnection
from nanobot.providers.base import LLMUsage
class WebUIOutboundTransport(Protocol):
"""Wire operations required by the outbound application projector."""
@@ -65,7 +69,15 @@ class WebUIOutboundTransport(Protocol):
provenance: dict[str, Any],
) -> None: ...
async def send_recovery_state(self, chat_id: str, event: RecoveryStateEvent) -> None: ...
async def send_payload(
self,
chat_id: str,
payload: WebUIWirePayload,
*,
persistence: WebUIWirePersistence,
metadata: dict[str, Any] | None = None,
turn_owner: str | None = None,
) -> None: ...
async def send_goal_state(self, chat_id: str, blob: dict[str, Any]) -> None: ...
@@ -78,19 +90,6 @@ class WebUIOutboundTransport(Protocol):
turn_id: str | None = None,
) -> None: ...
async def send_turn_end(
self,
chat_id: str,
latency_ms: int | None = None,
*,
goal_state: dict[str, Any] | None = None,
usage: LLMUsage | None = None,
round_usages: tuple[LLMUsage, ...] = (),
context_window_tokens: int | None = None,
metadata: dict[str, Any] | None = None,
turn_owner: str | None = None,
) -> None: ...
async def send_session_updated(self, chat_id: str, *, scope: str | None = None) -> None: ...
async def send_file_edit_events(
@@ -182,7 +181,11 @@ class WebUIOutboundProjector:
return
if isinstance(event, RecoveryStateEvent):
if conns:
await self._transport.send_recovery_state(msg.chat_id, event)
await self._transport.send_payload(
msg.chat_id,
encode_recovery_state(msg.chat_id, event),
persistence="transient",
)
return
if isinstance(event, GoalStateSyncEvent):
if conns:
@@ -221,13 +224,10 @@ class WebUIOutboundProjector:
else "thread"
)
turn_owner = (msg.metadata or {}).get(WEBSOCKET_TURN_OWNER_METADATA_KEY)
await self._transport.send_turn_end(
await self._transport.send_payload(
msg.chat_id,
latency_ms=event.latency_ms,
goal_state=event.goal_state,
usage=event.usage,
round_usages=event.round_usages,
context_window_tokens=event.context_window_tokens,
encode_turn_end(msg.chat_id, event, msg.metadata),
persistence="turn_complete",
metadata=msg.metadata,
turn_owner=turn_owner if isinstance(turn_owner, str) else None,
)
+81
View File
@@ -0,0 +1,81 @@
"""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