feat(webui): add temporary chat mode
This commit is contained in:
1 parent
113e8d67ad
commit
c9a6145878
41 files changed
+1500
-119
No files matched your search
@@ -13,7 +13,9 @@ from websockets.exceptions import ConnectionClosed
|
||||
from websockets.frames import Close
|
||||
|
||||
from nanobot.bus.events import (
|
||||
INBOUND_META_RUNTIME_CONTROL,
|
||||
OUTBOUND_META_AGENT_UI,
|
||||
RUNTIME_CONTROL_TRANSIENT_SESSION_DISCARD,
|
||||
OutboundMessage,
|
||||
)
|
||||
from nanobot.bus.outbound_events import (
|
||||
@@ -192,6 +194,149 @@ def isolate_webui_workspace_state(tmp_path, monkeypatch) -> None:
|
||||
wth._WEBSOCKET_TURN_OWNERS.clear()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_temporary_chat_is_connection_owned_and_never_persisted(bus, tmp_path) -> None:
|
||||
sessions = SessionManager(tmp_path)
|
||||
channel = WebSocketChannel(
|
||||
{"enabled": True, "allowFrom": ["*"]},
|
||||
bus,
|
||||
gateway=_basic_handler(
|
||||
bus,
|
||||
session_manager=sessions,
|
||||
workspace_path=tmp_path,
|
||||
),
|
||||
)
|
||||
connection = AsyncMock()
|
||||
connection.remote_address = ("127.0.0.1", 5000)
|
||||
channel._webui_connections.add(connection)
|
||||
chat_id = "temporary-test"
|
||||
|
||||
await channel._dispatch_envelope(
|
||||
connection,
|
||||
"webui-client",
|
||||
{
|
||||
"type": "message",
|
||||
"chat_id": chat_id,
|
||||
"content": "read this",
|
||||
"media": [{"data_url": "data:text/plain;base64,aGVsbG8=", "name": "note.txt"}],
|
||||
"cli_apps": [{"name": "drawio"}],
|
||||
"turn_id": "turn-1",
|
||||
"webui": True,
|
||||
},
|
||||
)
|
||||
|
||||
inbound = bus.publish_inbound.await_args_list[0].args[0]
|
||||
assert inbound.session_key == f"websocket:{chat_id}"
|
||||
assert inbound.transient_session is True
|
||||
assert inbound.metadata["cli_apps"] == [{"name": "drawio"}]
|
||||
assert Path(inbound.media[0]).read_text(encoding="utf-8") == "hello"
|
||||
assert sessions.get_cached(inbound.session_key).transient is True
|
||||
assert read_transcript_lines(inbound.session_key) == []
|
||||
assert [payload["event"] for payload in _sent_ws_payloads(connection)] == [
|
||||
"message_accepted",
|
||||
]
|
||||
|
||||
await channel._dispatch_envelope(
|
||||
connection,
|
||||
"webui-client",
|
||||
{"type": "discard_temporary_chat", "chat_id": chat_id},
|
||||
)
|
||||
|
||||
control = bus.publish_inbound.await_args_list[1].args[0]
|
||||
assert control.session_key == inbound.session_key
|
||||
assert control.metadata[INBOUND_META_RUNTIME_CONTROL] == (
|
||||
RUNTIME_CONTROL_TRANSIENT_SESSION_DISCARD
|
||||
)
|
||||
assert sessions.is_transient_active(inbound.session_key) is False
|
||||
assert Path(inbound.media[0]).exists() is False
|
||||
assert read_transcript_lines(inbound.session_key) == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_temporary_chat_rejects_unowned_messages(bus, tmp_path) -> None:
|
||||
sessions = SessionManager(tmp_path)
|
||||
channel = WebSocketChannel(
|
||||
{"enabled": True, "allowFrom": ["*"]},
|
||||
bus,
|
||||
gateway=_basic_handler(
|
||||
bus,
|
||||
session_manager=sessions,
|
||||
workspace_path=tmp_path,
|
||||
),
|
||||
)
|
||||
owner = AsyncMock()
|
||||
owner.remote_address = ("127.0.0.1", 5000)
|
||||
intruder = AsyncMock()
|
||||
intruder.remote_address = ("127.0.0.1", 5001)
|
||||
channel._webui_connections.update({owner, intruder})
|
||||
|
||||
await channel._dispatch_envelope(
|
||||
owner,
|
||||
"webui-client",
|
||||
{
|
||||
"type": "message",
|
||||
"chat_id": "temporary-owned",
|
||||
"content": "hello",
|
||||
"webui": True,
|
||||
},
|
||||
)
|
||||
|
||||
await channel._dispatch_envelope(
|
||||
intruder,
|
||||
"webui-client",
|
||||
{
|
||||
"type": "message",
|
||||
"chat_id": "temporary-owned",
|
||||
"content": "hello",
|
||||
"webui": True,
|
||||
},
|
||||
)
|
||||
|
||||
assert bus.publish_inbound.await_count == 1
|
||||
assert json.loads(intruder.send.await_args.args[0]) == {
|
||||
"event": "error",
|
||||
"detail": "temporary_chat_not_owned",
|
||||
"chat_id": "temporary-owned",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_disconnect_discards_temporary_chat(bus, tmp_path) -> None:
|
||||
sessions = SessionManager(tmp_path)
|
||||
channel = WebSocketChannel(
|
||||
{"enabled": True, "allowFrom": ["*"]},
|
||||
bus,
|
||||
gateway=_basic_handler(
|
||||
bus,
|
||||
session_manager=sessions,
|
||||
workspace_path=tmp_path,
|
||||
),
|
||||
)
|
||||
connection = AsyncMock()
|
||||
channel._webui_connections.add(connection)
|
||||
|
||||
await channel._dispatch_envelope(
|
||||
connection,
|
||||
"webui-client",
|
||||
{
|
||||
"type": "message",
|
||||
"chat_id": "temporary-disconnect",
|
||||
"content": "hello",
|
||||
"webui": True,
|
||||
},
|
||||
)
|
||||
await channel._cleanup_connection(connection)
|
||||
|
||||
session_key = "websocket:temporary-disconnect"
|
||||
control = bus.publish_inbound.await_args_list[-1].args[0]
|
||||
assert control.session_key == session_key
|
||||
assert control.metadata[INBOUND_META_RUNTIME_CONTROL] == (
|
||||
RUNTIME_CONTROL_TRANSIENT_SESSION_DISCARD
|
||||
)
|
||||
assert sessions.is_transient_active(session_key) is False
|
||||
assert "temporary-disconnect" not in channel._subs
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_session_updated_broadcasts_to_other_webui_connections(bus) -> None:
|
||||
class Conn:
|
||||
|
||||
Reference in new issue
Block a user