feat(webui): complete bang commands and literal output (NAN-291) (#6173)

* feat(webui): complete direct bang shell commands

Reuse the gateway user-shell side channel, retain failed drafts without reconnect replay, and preserve literal output across live delivery and history. Highlight the bang prefix before and after sending.

NAN-291

* fix(clients): preserve literal shell output through display metadata
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-26 23:35:26 +08:00
1 parent f23a6d1485
commit 7e5dcb79e0
43 files changed
+805 -100

No files matched your search

+51
View File
@@ -1191,3 +1191,54 @@ def test_build_response_schema(history, monkeypatch, tmp_path) -> None:
assert out["schemaVersion"] == WEBUI_TRANSCRIPT_SCHEMA_VERSION
assert out["sessionKey"] == key
assert len(_conversation_events(out)) == 1
def test_shell_output_format_survives_reload_and_paging_without_loading_model_history(history, tmp_path) -> None:
from nanobot.session.manager import SessionManager
key = "websocket:shell-output"
output = "Path\n----\n __pycache__\n```mermaid\nflowchart LR\n```\n![preview](local.png)\n"
history.append(key, {"event": "user", "text": "!pwd", "turn_id": "shell-turn"})
history.append(key, {
"event": "message", "text": output, "render_as": "text", "turn_id": "shell-turn",
})
history.append(key, {"event": "turn_end", "turn_id": "shell-turn"})
_append_numbered_turn(history, key, "shell-output", 2)
reopened = SessionManager(tmp_path)
def fail_if_loaded():
pytest.fail("Recorded output formatting must not load model history")
def build(**kwargs):
return reopened.project_history(key, lambda snapshot: transcript_module.build_webui_thread_response(
key, history=snapshot, session_messages_loader=fail_if_loaded,
augment_assistant_text=lambda text: text.replace("local.png", "/api/media/signed"), **kwargs,
))
latest = build(limit=2, direction="latest")
older = build(limit=2, before=latest["page"]["before_cursor"])
[answer] = [event for event in older["events"] if event["event"] == "message"]
assert answer["text"] == output
assert answer["render_as"] == "text"
assert history.read(key)[1]["text"] == output
assert history.read(key)[1]["render_as"] == "text"
@pytest.mark.parametrize("command", [True, False])
def test_existing_shell_output_uses_canonical_command_identity_not_bang_text(history, command) -> None:
key = "websocket:existing-shell-output"
output = "Path\n----\n__pycache__"
history.append(key, {"event": "user", "text": "!pwd", "turn_id": "shell-turn"})
history.append(key, {"event": "message", "text": output, "turn_id": "shell-turn"})
history.append(key, {"event": "turn_end", "turn_id": "shell-turn"})
messages = [
{"role": "user", "content": "/__shell pwd" if command else "!pwd",
"webui_turn_id": "shell-turn", "_command": command},
{"role": "assistant", "content": output, "_command": command},
]
response = history.build(key, session_messages=messages)
[answer] = [event for event in response["events"] if event["event"] == "message"]
assert answer["text"] == output
assert answer.get("render_as") == ("text" if command else None)
assert history.read(key)[1]["text"] == output
assert messages[1]["content"] == output
+10 -4
View File
@@ -259,13 +259,15 @@ async def test_gateway_webui_bootstrap_message_and_thread_hydration(tmp_path: Pa
await ws.send(json.dumps({
"type": "message",
"chat_id": chat_id,
"content": "!printf shell-ok",
"content": "!echo shell-ok",
"webui": True,
"user_shell": True,
"turn_id": "shell-turn",
}))
shell = await _recv_until(ws, "message")
assert "shell-ok" in shell["text"]
assert shell["text"].splitlines()[0] == "shell-ok"
assert "Exit code: 0" in shell["text"]
assert shell["render_as"] == "text"
assert shell["turn_id"] == "shell-turn"
await _recv_until(ws, "turn_end")
@@ -286,8 +288,12 @@ async def test_gateway_webui_bootstrap_message_and_thread_hydration(tmp_path: Pa
]
assert "/model" in contents
assert any("Current model: `custom/smoke-model`" in text for text in contents)
assert "!printf shell-ok" in contents
assert any("shell-ok" in text for text in contents)
assert "!echo shell-ok" in contents
assert shell["text"] in contents
[shell_history] = [event for event in thread["events"] if event.get("event") == "message"
and event.get("turn_id") == "shell-turn"]
assert shell_history["text"] == shell["text"]
assert shell_history["render_as"] == shell["render_as"]
finally:
_stop_gateway(process)