refactor(events): unify scoped runtime notifications across clients (#5670)
* refactor(events): unify scoped runtime notifications across clients * style(tests): sort auto-compaction event imports * refactor(events): scope connections and remove retry callback plumbing * docs: remove runtime notification design write-up * refactor(bus): route operation events through the message bus * fix(channels): isolate outbound sends by destination * style(events): group shared notification imports * refactor(events): converge runtime and hook delivery on MessageBus * test(events): migrate fixtures to unified message bus
This commit is contained in:
1 parent
3495281282
commit
192e2e90dc
78 files changed
+1620
-1194
No files matched your search
@@ -90,6 +90,9 @@ def iter_webui_source_files(source_dir: Path) -> list[Path]:
|
||||
if not root.is_dir():
|
||||
continue
|
||||
files.extend(path for path in root.rglob("*") if path.is_file())
|
||||
shared_root = source_dir.parent / "packages" / "client-events"
|
||||
if shared_root.is_dir():
|
||||
files.extend(path for path in shared_root.rglob("*") if path.is_file())
|
||||
channel_root = source_dir.parent / "nanobot" / "channels"
|
||||
if channel_root.is_dir():
|
||||
for channel_webui in channel_root.glob("*/webui"):
|
||||
|
||||
@@ -12,13 +12,11 @@ from nanobot.bus.outbound_events import (
|
||||
GoalStateSyncEvent,
|
||||
GoalStatusEvent,
|
||||
ProgressEvent,
|
||||
RecoveryStateEvent,
|
||||
RuntimeModelUpdatedEvent,
|
||||
SessionUpdatedEvent,
|
||||
TurnEndEvent,
|
||||
TurnModelUpdatedEvent,
|
||||
UserInputEvent,
|
||||
outbound_event_from_message,
|
||||
)
|
||||
from nanobot.session.webui_turns import clear_websocket_turn_if_current
|
||||
from nanobot.webui.metadata import (
|
||||
@@ -29,9 +27,8 @@ from nanobot.webui.metadata import (
|
||||
from nanobot.webui.outbound_wire import (
|
||||
WebUIWirePayload,
|
||||
WebUIWirePersistence,
|
||||
encode_context_compaction,
|
||||
encode_recovery_state,
|
||||
encode_turn_end,
|
||||
project_notification,
|
||||
)
|
||||
from nanobot.webui.session_identity import webui_session_key
|
||||
from nanobot.webui.session_projection import WebUISessionProjection
|
||||
@@ -136,7 +133,7 @@ class WebUIOutboundProjector:
|
||||
)
|
||||
|
||||
async def send(self, msg: OutboundMessage) -> None:
|
||||
event = outbound_event_from_message(msg)
|
||||
event = msg.event
|
||||
progress_event = event if isinstance(event, ProgressEvent) else None
|
||||
if isinstance(event, RuntimeModelUpdatedEvent):
|
||||
await self._transport.send_runtime_model_updated(
|
||||
@@ -182,21 +179,17 @@ class WebUIOutboundProjector:
|
||||
provenance=event.provenance,
|
||||
)
|
||||
return
|
||||
if isinstance(event, RecoveryStateEvent):
|
||||
if conns:
|
||||
notification = project_notification(msg.chat_id, event)
|
||||
if notification is not None:
|
||||
if conns or notification.deliver_offline:
|
||||
kwargs: dict[str, Any] = (
|
||||
{"metadata": msg.metadata} if notification.attach_turn_metadata else {}
|
||||
)
|
||||
await self._transport.send_payload(
|
||||
msg.chat_id,
|
||||
encode_recovery_state(msg.chat_id, event),
|
||||
persistence="transient",
|
||||
msg.chat_id, notification.payload,
|
||||
persistence=notification.persistence,
|
||||
**kwargs,
|
||||
)
|
||||
return
|
||||
if isinstance(event, ContextCompactionEvent):
|
||||
await self._transport.send_payload(
|
||||
msg.chat_id,
|
||||
encode_context_compaction(msg.chat_id, event),
|
||||
persistence="transient" if event.phase == "started" else "turn_activity",
|
||||
metadata=msg.metadata,
|
||||
)
|
||||
return
|
||||
if isinstance(event, GoalStateSyncEvent):
|
||||
if conns:
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Literal, NotRequired, TypeAlias, TypedDict
|
||||
|
||||
from nanobot.bus.outbound_events import (
|
||||
@@ -10,6 +11,7 @@ from nanobot.bus.outbound_events import (
|
||||
RecoveryStateEvent,
|
||||
TurnEndEvent,
|
||||
)
|
||||
from nanobot.events import AgentEvent
|
||||
from nanobot.webui.metadata import WEBUI_TURN_METADATA_KEY
|
||||
|
||||
|
||||
@@ -39,7 +41,7 @@ class TurnEndWirePayload(_ChatWirePayload):
|
||||
class ContextCompactionWirePayload(_ChatWirePayload):
|
||||
event: Literal["context_compaction"]
|
||||
compaction_id: str
|
||||
phase: str
|
||||
phase: Literal["started", "succeeded", "failed", "cancelled"]
|
||||
|
||||
|
||||
WebUIWirePayload: TypeAlias = (
|
||||
@@ -85,6 +87,30 @@ def encode_context_compaction(
|
||||
return payload
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class NotificationProjection:
|
||||
"""Allowlisted public payload and its durability policy."""
|
||||
|
||||
payload: WebUIWirePayload
|
||||
persistence: WebUIWirePersistence = "transient"
|
||||
deliver_offline: bool = False
|
||||
attach_turn_metadata: bool = False
|
||||
|
||||
|
||||
def project_notification(chat_id: str, event: AgentEvent | None) -> NotificationProjection | None:
|
||||
"""Keep notification serialization and persistence decisions at one boundary."""
|
||||
if isinstance(event, ContextCompactionEvent):
|
||||
return NotificationProjection(
|
||||
encode_context_compaction(chat_id, event),
|
||||
persistence="transient" if event.phase == "started" else "turn_activity",
|
||||
deliver_offline=True,
|
||||
attach_turn_metadata=True,
|
||||
)
|
||||
if isinstance(event, RecoveryStateEvent):
|
||||
return NotificationProjection(encode_recovery_state(chat_id, event))
|
||||
return None
|
||||
|
||||
|
||||
def encode_turn_end(
|
||||
chat_id: str,
|
||||
event: TurnEndEvent,
|
||||
|
||||
Reference in new issue
Block a user