refactor: enforce BasedPyright strict type checking (#5158)

This commit is contained in:
chengyongru authored and GitHub committed 2026-07-29 21:37:11 +08:00
1 parent e703481755
commit 757ad9c764
166 files changed
+4728 -2621

No files matched your search

+20 -17
View File
@@ -5,7 +5,7 @@ from __future__ import annotations
import re
import uuid
from collections.abc import Mapping
from typing import Any
from typing import TYPE_CHECKING, Any, TypeGuard
from nanobot.session.manager import SessionManager
from nanobot.session.webui_turns import WEBUI_TITLE_METADATA_KEY, clean_generated_title
@@ -16,10 +16,15 @@ from nanobot.webui.transcript import (
write_session_messages_as_transcript,
)
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}$")
def _valid_webui_chat_id(value: Any) -> bool:
def _valid_webui_chat_id(value: Any) -> TypeGuard[str]:
return isinstance(value, str) and _WEBUI_CHAT_ID_RE.match(value) is not None
@@ -63,7 +68,11 @@ def create_webui_chat_fork(
return new_id, target_key
async def handle_webui_fork_chat(channel: Any, connection: Any, envelope: Mapping[str, Any]) -> None:
async def handle_webui_fork_chat(
channel: WebSocketChannel,
connection: ServerConnection,
envelope: Mapping[str, Any],
) -> None:
"""Handle the WebUI ``fork_chat`` websocket command.
``websocket.py`` owns the transport. This module owns WebUI fork semantics:
@@ -73,15 +82,15 @@ async def handle_webui_fork_chat(channel: Any, connection: Any, envelope: Mappin
source_chat_id = envelope.get("source_chat_id")
raw_index = envelope.get("before_user_index")
if not _valid_webui_chat_id(source_chat_id):
await channel._send_event(connection, "error", detail="invalid 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:
await channel._send_event(connection, "error", detail="invalid before_user_index")
await channel.send_webui_protocol_error(connection, "invalid before_user_index")
return
session_manager = channel.gateway.session_manager
if session_manager is None:
await channel._send_event(connection, "error", detail="session_manager_unavailable")
await channel.send_webui_protocol_error(connection, "session_manager_unavailable")
return
try:
@@ -92,22 +101,16 @@ async def handle_webui_fork_chat(channel: Any, connection: Any, envelope: Mappin
title=envelope.get("title") if isinstance(envelope.get("title"), str) else None,
)
if forked is None:
await channel._send_event(connection, "error", detail="invalid fork source or index")
await channel.send_webui_protocol_error(connection, "invalid fork source or index")
return
fork_id, fork_key = forked
except Exception as exc:
channel.logger.warning("fork_chat failed: {}", exc)
await channel._send_event(connection, "error", detail="fork_chat_failed")
await channel.send_webui_protocol_error(connection, "fork_chat_failed")
return
scope = channel._workspaces.scope_for_session_key(fork_key)
channel._attach(connection, fork_id)
await channel._send_event(connection, "attached", chat_id=fork_id)
await channel._send_event(
await channel.attach_webui_fork(
connection,
"session_updated",
chat_id=fork_id,
scope="metadata",
workspace_scope=scope.payload(),
fork_id=fork_id,
fork_key=fork_key,
)
await channel._hydrate_after_subscribe(fork_id)