Whole chat history with infinite scroll back; fix switching between sidebar tabs

Chat is an append-only chat.jsonl paged by sequence number: the welcome
carries the latest 50 messages and GET /rigshare/api/chat pages further back.
The panel loads older pages as you scroll up, keeps your place, and adds date
dividers. ComfyUI reuses one slot element between custom sidebar tabs, so
each tab now replaces the slot's content instead of appending to it.
This commit is contained in:
Edouard Durand
2026-09-26 03:09:24 +00:00
parent a97ade0974
commit 3f1ddfed7f
14 changed files with 310 additions and 26 deletions
+41
View File
@@ -0,0 +1,41 @@
"""Chat history: append-only log paged by sequence number."""
import json
import os
from rigshare_core.chatlog import ChatLog
def test_pages_back_through_the_whole_history(tmp_path):
chat = ChatLog(str(tmp_path))
for i in range(130):
assert chat.append({"text": f"m{i}", "ts": i})["seq"] == i
page, more = chat.page(limit=50)
assert [m["text"] for m in page] == [f"m{i}" for i in range(80, 130)] and more
seen = page
while more:
page, more = chat.page(before=seen[0]["seq"], limit=50)
seen = page + seen
assert [m["seq"] for m in seen] == list(range(130)), "every message exactly once, in order"
assert chat.page(before=0) == ([], False)
def test_survives_restart_a_torn_line_and_imports_old_chat(tmp_path):
folder = str(tmp_path)
chat = ChatLog(folder, legacy=[{"text": "old 1"}, {"text": "old 2"}])
chat.append({"text": "new"})
with open(os.path.join(folder, "chat.jsonl"), "ab") as f:
f.write(b'{"text": "torn') # the server died mid-write
chat = ChatLog(folder, legacy=[{"text": "never imported twice"}])
assert [m["text"] for m in chat.page()[0]] == ["old 1", "old 2", "new"]
chat.append({"text": "after"})
assert chat.page()[0][-1] == {"text": "after", "seq": 3}
def test_optional_trim_and_unicode(tmp_path):
chat = ChatLog(str(tmp_path))
for i in range(10):
chat.append({"text": f"héllo {i} 🎉"})
chat = ChatLog(str(tmp_path), keep=4)
assert [m["text"] for m in chat.page()[0]] == [f"héllo {i} 🎉" for i in range(6, 10)]
assert len(chat) == 4
+32
View File
@@ -292,3 +292,35 @@ def test_moves_follow_open_tabs(run):
for sock in socks.values():
await sock.close()
run(scenario())
def test_chat_history_pages(run):
async def scenario():
async with rig_server() as rig:
await rig.setup_admin()
sock = await rig.ws("boss")
for i in range(70):
await sock.send({"type": "chat", "text": f"hello {i}"})
await asyncio.sleep(0.3)
await sock.close()
sock = await rig.ws("boss")
await sock.close()
# The welcome carries only the newest page; the rest comes over HTTP.
h = rig.headers["boss"]
session = rig.http
ws = await session.ws_connect(rig.url("/rigshare/ws"), headers=h)
await ws.send_json({"type": "hello"})
while True:
welcome = json.loads((await ws.receive()).data)
if welcome["type"] == "welcome":
break
await ws.close()
assert len(welcome["chat"]) == 50 and welcome["chat_more"]
oldest = welcome["chat"][0]["seq"]
older = await (await session.get(rig.url(f"/rigshare/api/chat?before={oldest}&limit=50"), headers=h)).json()
texts = [m["text"] for m in older["messages"]]
assert not older["more"] and older["messages"][0]["seq"] == 0
seqs = [m["seq"] for m in older["messages"] + welcome["chat"]]
assert "hello 0" in texts and seqs == list(range(len(seqs))), "no gaps, no repeats"
assert (await session.get(rig.url("/rigshare/api/chat?before=x"))).status in (400, 401)
run(scenario())