refactor(webui): isolate websocket application orchestration (#5548)

* refactor(webui): extract session attach projection

* refactor(webui): isolate websocket application orchestration

* refactor(webui): tighten websocket application boundary

* test(webui): assert module logger for fork failures
This commit is contained in:
chengyongru authored and GitHub committed 2026-08-26 18:04:38 +08:00
1 parent a618e80887
commit f9d449ef6c
21 files changed
+1920 -1211

No files matched your search

+26 -12
View File
@@ -2,13 +2,15 @@
from __future__ import annotations
import re
import uuid
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeGuard
from typing import TYPE_CHECKING, Any, Protocol
from loguru import logger
from nanobot.session.manager import SessionManager
from nanobot.session.webui_turns import WEBUI_TITLE_METADATA_KEY, clean_generated_title
from nanobot.webui.session_identity import is_valid_webui_chat_id, webui_session_key
from nanobot.webui.transcript import (
append_fork_marker,
delete_webui_transcript,
@@ -19,13 +21,25 @@ from nanobot.webui.transcript import (
if TYPE_CHECKING:
from websockets.asyncio.server import ServerConnection
from nanobot.channels.websocket.runtime import WebSocketChannel
_WEBUI_CHAT_ID_RE = re.compile(r"^[A-Za-z0-9_:-]{1,64}$")
from nanobot.webui.gateway_services import GatewayServices
def _valid_webui_chat_id(value: Any) -> TypeGuard[str]:
return isinstance(value, str) and _WEBUI_CHAT_ID_RE.match(value) is not None
class WebUIForkHost(Protocol):
gateway: GatewayServices
async def send_webui_protocol_error(
self,
connection: ServerConnection,
detail: str,
) -> None: ...
async def attach_webui_fork(
self,
connection: ServerConnection,
*,
fork_id: str,
fork_key: str,
) -> None: ...
def create_webui_chat_fork(
@@ -37,8 +51,8 @@ def create_webui_chat_fork(
) -> tuple[str, str] | None:
"""Return ``(chat_id, session_key)`` for a new fork, or ``None`` for bad input."""
new_id = str(uuid.uuid4())
source_key = f"websocket:{source_chat_id}"
target_key = f"websocket:{new_id}"
source_key = webui_session_key(source_chat_id)
target_key = webui_session_key(new_id)
try:
forked = session_manager.fork_session_before_user_index(
source_key,
@@ -69,7 +83,7 @@ def create_webui_chat_fork(
async def handle_webui_fork_chat(
channel: WebSocketChannel,
channel: WebUIForkHost,
connection: ServerConnection,
envelope: Mapping[str, Any],
) -> None:
@@ -81,7 +95,7 @@ async def handle_webui_fork_chat(
"""
source_chat_id = envelope.get("source_chat_id")
raw_index = envelope.get("before_user_index")
if not _valid_webui_chat_id(source_chat_id):
if not is_valid_webui_chat_id(source_chat_id):
await channel.send_webui_protocol_error(connection, "invalid source_chat_id")
return
if isinstance(raw_index, bool) or not isinstance(raw_index, int) or raw_index < 0:
@@ -105,7 +119,7 @@ async def handle_webui_fork_chat(
return
fork_id, fork_key = forked
except Exception as exc:
channel.logger.warning("fork_chat failed: {}", exc)
logger.warning("fork_chat failed: {}", exc)
await channel.send_webui_protocol_error(connection, "fork_chat_failed")
return