refactor(webui): centralize outbound delivery (NAN-365)

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-28 22:17:53 +08:00
1 parent 4a14faa47e
commit 2a3d29ce73
13 files changed
+846 -897

No files matched your search

+5 -4
View File
@@ -352,11 +352,12 @@ async def test_runner_records_each_provider_when_streaming_times_out_and_recover
@pytest.mark.parametrize("temporary", [False, True])
async def test_websocket_wire_and_disk_replay_preserve_sources(history, tmp_path, monkeypatch, temporary):
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
channel = WebSocketChannel({"allowFrom": ["*"]}, MessageBus(), gateway=MagicMock())
channel._media.rewrite_local_markdown_images.side_effect = lambda text: text
channel._transcripts = WebUITranscriptRecorder(history.sessions)
gateway = MagicMock()
gateway.media.rewrite_local_markdown_images.side_effect = lambda text: text
gateway.transcripts = WebUITranscriptRecorder(history.sessions)
gateway.temporary_chats.should_persist_transcript = lambda _: not temporary
channel = WebSocketChannel({"allowFrom": ["*"]}, MessageBus(), gateway=gateway)
generation, _ = await history.sessions.state.open_with_metadata("websocket:source-chat", {})
channel._temporary_chats.should_persist_transcript = lambda _: not temporary
channel._safe_send_to = AsyncMock()
channel._subs["source-chat"] = {MagicMock()}
sources = [{"provider": "xai", "model": "grok", "preset": "snapshot name", "fallback": True}]
+16 -11
View File
@@ -119,19 +119,24 @@ def test_storage_and_security_do_not_depend_on_adapters(module: str, rule: str)
]
def test_websocket_only_permits_progress_event_as_a_type_dependency() -> None:
source = (
"from typing import TYPE_CHECKING\n"
"if TYPE_CHECKING:\n"
" from nanobot.bus.outbound_events import ProgressEvent\n"
" from nanobot.command import router\n"
"else:\n"
" from nanobot.bus.outbound_events import ProgressEvent\n"
)
@pytest.mark.parametrize(
"dependency",
[
"nanobot.bus.outbound_events.ProgressEvent",
"nanobot.command.router",
"nanobot.pairing.events.PairingChanged",
"nanobot.session.webui_turns.clear_websocket_turn_if_current",
"nanobot.webui.transcript.WebUITranscriptRecorder",
"nanobot.webui.outbound_wire.project_notification",
],
)
def test_websocket_transport_leaves_event_and_history_policy_to_delivery(dependency: str) -> None:
module, name = dependency.rsplit(".", 1)
source = f"from {module} import {name}\n"
assert find_violations(source, "nanobot.channels.websocket.runtime") == [
Violation("websocket-transport", "nanobot.command.router", 4),
Violation("websocket-transport", "nanobot.bus.outbound_events.ProgressEvent", 6),
Violation("websocket-transport", dependency, 1),
]
assert find_violations(source, "nanobot.webui.outbound_delivery") == []
@pytest.mark.parametrize(
@@ -49,7 +49,7 @@ async def channel(tmp_path, monkeypatch):
assert not channel._commands.request_tasks
async def test_send_delegates_the_outbound_message_to_the_projector(channel, monkeypatch):
async def test_send_delegates_the_outbound_message_to_delivery(channel, monkeypatch):
send = AsyncMock()
hydrate = AsyncMock()
monkeypatch.setattr(channel._outbound, "send", send)
@@ -71,12 +71,34 @@ async def test_subscribe_hydration_delegates_the_requested_chat(channel, monkeyp
monkeypatch.setattr(channel._outbound, "hydrate", hydrate)
monkeypatch.setattr(channel._outbound, "send", send)
await channel._hydrate_after_subscribe("chat-2")
await channel._dispatch_envelope(
AsyncMock(), "client-1", {"type": "attach", "chat_id": "chat-2"},
)
hydrate.assert_awaited_once_with("chat-2")
send.assert_not_awaited()
@pytest.mark.parametrize(
("method", "args", "kwargs"),
[
("send_delta", ("chat-1", "part", {"webui_turn_id": "turn-1"}), {
"stream_id": "s1", "stream_end": True, "resuming": True, "merge_next": True,
}),
("send_reasoning_delta", ("chat-1", "plan", {"webui_turn_id": "turn-1"}), {"stream_id": "r1"}),
("send_reasoning_end", ("chat-1", {"webui_turn_id": "turn-1"}), {"stream_id": "r1"}),
("send_file_edit_events", ("chat-1", [{"path": "a.py"}], {"webui_turn_id": "turn-1"}), {}),
],
)
async def test_streaming_capabilities_use_the_same_delivery_owner(channel, monkeypatch, method, args, kwargs):
deliver = AsyncMock()
monkeypatch.setattr(channel._outbound, method, deliver)
await getattr(channel, method)(*args, **kwargs)
deliver.assert_awaited_once_with(*args, **kwargs)
async def test_envelope_delegates_connection_client_and_payload_to_application(channel, monkeypatch):
dispatch = AsyncMock()
monkeypatch.setattr(channel._commands, "dispatch", dispatch)