From da066675347350b8ee6f4473bb5c3d20f7f1e279 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Fri, 16 Oct 2026 23:55:52 +0000 Subject: [PATCH 1/3] feat(chat): versions: regenerate, edit and switch in a conversation tree A conversation is now a tree: chat_message.parent and chat.head. Regenerate writes a new answer beside the old one, edit makes a new version of a question beside it instead of deleting what followed, and parent goes on from any message. GET /chats/{cid} adds tree, head and each message's versions; PUT /chats/{cid}/head switches. The model reads only the version showing; a summary written on another version is ignored and rewritten for this one. Upgrade step chat-tree lines existing conversations up, safe to run twice. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01HXPWXcSCQb9hWPr7xkZu7B --- CHANGELOG.md | 6 + docs/assistant.md | 17 +- docs/database.md | 11 + fastapi_backend/app/api/v1/routes/chats.py | 114 +++++++-- fastapi_backend/app/domain/bridge.py | 4 +- fastapi_backend/app/domain/chat.py | 199 +++++++++++++--- fastapi_backend/app/domain/migrations.py | 4 +- fastapi_backend/app/schemas/chats.py | 39 +++- .../tests/api/test_chat_versions.py | 136 +++++++++++ fastapi_backend/tests/api/test_chats.py | 15 +- fastapi_backend/tests/api/test_compaction.py | 14 +- .../tests/domain/test_migrations.py | 41 +++- nextjs-frontend/app/openapi-client/index.ts | 8 + nextjs-frontend/app/openapi-client/sdk.gen.ts | 36 ++- .../app/openapi-client/types.gen.ts | 128 +++++++++- nextjs-frontend/openapi.json | 218 +++++++++++++++++- 16 files changed, 899 insertions(+), 91 deletions(-) create mode 100644 fastapi_backend/tests/api/test_chat_versions.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 08bbfb731..b34479b88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,12 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version `automation_actions`, `save_automation`, `test_automation` and `run_automation`, with problems in plain words. The assistant saves them switched off and asks before switching one on or running it ([Agents](docs/automations.md#agents)). +- **Versions in conversations.** A conversation is now a tree: editing a question makes a new version beside it + instead of deleting what followed, regenerating an answer (`regenerate`, with the same model or another) writes one + beside it, and a question can go on from any message (`parent`). `GET /api/v1/chats/` shows each message's + versions and every version's messages, and `PUT /api/v1/chats//head` switches between them; the model reads + only the version showing. The web app's switcher and Regenerate button are in progress. Existing conversations are + upgraded on the next start (`chat-tree`) ([Versions](docs/assistant.md)). - **Describe an automation in words.** `POST /api/v1/automations/draft` asks the model once to turn a sentence into a draft automation to try and save, and says what didn't fit and what it couldn't express ([Describe it in words](docs/automations.md#describe-it-in-words)). diff --git a/docs/assistant.md b/docs/assistant.md index bacf0168f..9f1ff4852 100644 --- a/docs/assistant.md +++ b/docs/assistant.md @@ -18,13 +18,22 @@ then. is writing it); what came before is saved, marked stopped. A model call already under way finishes first. - **Reopening:** a conversation's answers keep the tools the assistant used (and with what), any notice (the model couldn't use tools), the error when there was no answer, and their latest source check. -- **Editing a question:** Edit on a question you asked asks it again as edited; the answer and everything after it - are replaced (`POST /api/v1/chats//messages` with `edit`). +- **Versions:** a conversation is a tree, and nothing in it is deleted. Regenerating an answer writes a new answer to + the same question beside it, with the same model or another (`POST /api/v1/chats//messages` with `regenerate`: + the answer's id, and `model`); editing a question makes a new version of it beside the old one, which keeps what + followed it (`edit`: the question's id, with the new `content`). The version showing is what the model reads and + what the next question goes on from; `parent` goes on from any other message instead. `GET /api/v1/chats/` + gives the version showing as `messages`, each with its `versions` (`index` from 1, `count`, `ids`: it and the + others in its place, oldest first), every version's messages as `tree`, and `head`, the last message showing. `PUT + /api/v1/chats//head` with `{"message": }` shows the version that message is in, to its newest message. A + new version streams `branch` first (`message`, `parent`, `versions`). Chat rooms ([Chat rooms](chat-rooms.md)) and + automations go on from the version showing. In the web app, Edit makes a version; switching between versions and + Regenerate are in progress. - **Long conversations:** the model reads the latest six messages word for word. Once eight more have built up behind them, they're folded into a running summary kept on the conversation (one model call, after an answer), and every answer gets that summary plus the messages it doesn't cover yet. Room conversations (chat-rooms.md) go on for - good, so they rely on it most. Editing a question the summary covers drops the summary; it's made again from what's - left. A conversation that fills half of the model's context window (**Chat model's context**, `ai.context_tokens`, + good, so they rely on it most. A summary covers the version it was written on: on a version that leaves what it + covers, the model reads that version's messages, and the next summary is made from them. A conversation that fills half of the model's context window (**Chat model's context**, `ai.context_tokens`, 16000 when empty; about four characters a token) is folded in early, before the next answer, whatever the count. **Model for summaries** (`ai.compact_model`) writes them with a smaller, cheaper model on the same provider; empty, the chat's model does. Settings › AI assistant › Summarise long conversations (`ai.compact`, on) turns it off; off, diff --git a/docs/database.md b/docs/database.md index 7473ad304..867d07128 100644 --- a/docs/database.md +++ b/docs/database.md @@ -102,6 +102,17 @@ tables, or changing or removing an index that already exists, is a named step in * `lens migrations` lists every step (done, pending, failed, or unknown when a newer Lens ran it) without running anything, so it works while an upgrade is failing; `lens migrations --run` runs the pending ones. +The steps so far, in the order they run: + +* `access-levels`: IIIF-only access levels (public, transcript, signed-in, private) become the access setting. +* `collection-homes`: every namespace gets its default collection, and every asset a home in one. +* `tour-seen`: people who signed in before the welcome tour existed don't get it. +* `document-read-step`: documents and images queued to be transcribed are read instead, and failed ones run again. +* `chat-tree`: conversations become trees of versions ([Versions](assistant.md)): each message of a conversation + (`chat_message`) gets the one before it as its `parent`, and the conversation (`chat`) its last message as its + `head`. Only conversations without a `head` are touched, so it's safe to run twice; until it has run, a conversation + without a `head` is read as one line, and saved as one before anything is added to it. + To add a step: 1. Write a function `fn(db)` next to the code it serves. It must be safe to run twice, since a start that stops half diff --git a/fastapi_backend/app/api/v1/routes/chats.py b/fastapi_backend/app/api/v1/routes/chats.py index aadedbd19..203a229b0 100644 --- a/fastapi_backend/app/api/v1/routes/chats.py +++ b/fastapi_backend/app/api/v1/routes/chats.py @@ -1,6 +1,7 @@ """Conversations with the archive, the assistant's approvals, and checking an answer against its sources. -A question streams back server-sent events: ``scoped`` (the namespace a conversation over everything was narrowed +A question streams back server-sent events: ``branch`` (a new version: regenerated, edited, or beside others), +``scoped`` (the namespace a conversation over everything was narrowed to) or ``suggested`` (namespaces to offer when it isn't clear, new ones included), ``step`` (a tool the assistant used), ``approval`` (work it proposed that waits for you), ``notice``, ``passages`` (the numbered excerpts), ``token`` (answer text), ``error`` and ``done``. The assistant only ever sees, and cites, what the asker can read. """ @@ -33,6 +34,7 @@ ChatScope, ChatSummary, ChatUpdate, + HeadUpdate, MessageCreate, StopResult, ) @@ -111,9 +113,28 @@ def create_chat(user: Writer, acl: Acl, db: Db, cfg: Cfg, body: ChatCreate | Non @router.get("/chats/{cid}") def get_chat(cid: int, user: CurrentUser, acl: Acl, db: Db) -> Chat: - """A conversation with its messages. Citations follow your current access: ones you can no longer read are left out.""" + """A conversation with the version showing (`messages`, each with its `versions`) and every version's messages + (`tree`). Citations follow your current access: ones you can no longer read are left out.""" + return _chat_view(db, acl, _own_chat(db, cid, user)) + + +@router.put("/chats/{cid}/head") +def switch_version(cid: int, body: HeadUpdate, user: Writer, acl: Acl, db: Db) -> Chat: + """Show another version: the one `message` is in, from the first message to the newest one below `message`. The + next question goes on from there. Returns the conversation as GET /chats/{cid} does.""" c = _own_chat(db, cid, user) - msgs, ok = chat.history(db, cid), set(acl.roles) + try: + c["head"] = chat.switch(db, cid, body.message) + except KeyError: + raise HTTPException(404, "no such message in this conversation") from None + return _chat_view(db, acl, c) + + +def _chat_view(db: DB, acl: Access, c: dict[str, Any]) -> Chat: + nodes = chat.tree(db, c["id"]) + msgs, ok, versions = chat.path(db, c["id"], c.get("head"), nodes), set(acl.roles), chat.versions(nodes) + for m in msgs: + m["versions"] = versions.get(m["id"]) ids = {p["recording_id"] for m in msgs for p in m.get("passages") or []} visible = ( { @@ -126,7 +147,7 @@ def get_chat(cid: int, user: CurrentUser, acl: Acl, db: Db) -> Chat: ) for m in msgs: m["passages"] = [p for p in m.get("passages") or [] if p["recording_id"] in visible] - return Chat.model_validate(sign_urls({**c, "messages": msgs}, full=True)) + return Chat.model_validate(sign_urls({**c, "messages": msgs, "tree": nodes}, full=True)) @router.patch("/chats/{cid}") @@ -179,9 +200,14 @@ async def send_message(cid: int, body: MessageCreate, user: Writer, acl: Acl, db reads with the question). Streams events: step, approval, notice, passages, token (answer text), error, stopped (POST /chats/{cid}/stop: what came before is saved, marked stopped), done (the saved message id). - With `edit`, one of your earlier questions is edited: it and everything after it are replaced by this question and - its new answer (404 if it isn't a question in this conversation). Without `context`, it keeps the page it was asked - from (where, and the highlighted text; not the page's text, which isn't kept). + A conversation is a tree of versions (GET /chats/{cid}: `tree`, and each message's `versions`); the question goes + on from `parent` when given (any message of the conversation), else from the version showing (`head`). With + `regenerate` (an answer's id) its question is answered again, as a new version beside that answer (`content` isn't + needed; `model` may pick another). With `edit`, one of your earlier questions is edited: this question becomes a new + version beside it and is answered, and the old one stays with what followed it (404 if it isn't a question in this + conversation). Either keeps the page the question was asked from (where, and the highlighted text; not the page's + text, which isn't kept) unless `context` is given, and streams `branch` first ({message, parent, versions}: the + new version, the message it follows and its versions); so does a question beside others under `parent`. `/clear` makes the model start afresh (the messages stay to read) and `/compact [what to keep]` folds the conversation so far into its summary now; each streams cleared or compacted, then its reply as an answer. @@ -190,17 +216,37 @@ async def send_message(cid: int, body: MessageCreate, user: Writer, acl: Acl, db browser while the side panel is connected: open pages, read them, click, type and download files into the conversation (docs/browser-assistant.md).""" c = await run_in_threadpool(_own_chat, db, cid, user) - q = body.content.strip()[:4000] - files = await run_in_threadpool(_attachments, db, body.attachments, user) - if not q and files: - q = "I attached " + ("this file." if len(files) == 1 else "these files.") - if not q: - raise HTTPException(400, "ask something") - cmd = chat.command(q) if not files and body.edit is None else None + if body.regenerate is not None and body.edit is not None: + raise HTTPException(400, "regenerate an answer or edit a question, not both") + if body.parent is not None and (body.regenerate is not None or body.edit is not None): + raise HTTPException(400, "parent is for a new question: regenerate and edit go beside their message") + again: dict[str, Any] | None = None # regenerating: the question answered again + under: Any = chat.HEAD # the message the question follows + if body.regenerate is not None: + try: + again = found = await run_in_threadpool(chat.question_of, db, cid, body.regenerate) + except KeyError: + raise HTTPException(404, "no such answer in this conversation") from None + q, files = found["content"], found.get("attachments") or [] + if chat.command(q): + raise HTTPException(400, f"{q.split()[0]} isn't answered by the model, so there's nothing to regenerate") + else: + q = body.content.strip()[:4000] + files = await run_in_threadpool(_attachments, db, body.attachments, user) + if not q and files: + q = "I attached " + ("this file." if len(files) == 1 else "these files.") + if not q: + raise HTTPException(400, "ask something") + if body.parent is not None: + try: + under = (await run_in_threadpool(chat.said, db, cid, body.parent))["id"] + except KeyError: + raise HTTPException(404, "no such message in this conversation") from None + cmd = chat.command(q) if not files and body.edit is None and again is None else None if cmd: # /clear or /compact: done here, answered like a question, without asking the model anything else def run() -> Iterator[str]: - reply, mid = chat.run_command(db, cfg, cid, *cmd) + reply, mid = chat.run_command(db, cfg, cid, cmd[0], cmd[1], parent=under) yield _ev({"clear": "cleared", "compact": "compacted"}[cmd[0]], {"message": mid}) yield _ev("passages", []) yield _ev("token", {"text": reply}) @@ -214,11 +260,16 @@ def run() -> Iterator[str]: title = c["title"] page = body.context.model_dump() if body.context else None kept = chat.shared_context(page) # what the question keeps of the page - if body.edit is not None: # only after the question checks out, so a refused edit removes nothing + if again is not None: + under = again.get("parent") + if page is None and again.get("context"): + page = again["context"] + if body.edit is not None: try: - old = await run_in_threadpool(chat.rewind, db, cid, body.edit) + old = await run_in_threadpool(chat.said, db, cid, body.edit, "user") except KeyError: raise HTTPException(404, "no such question in this conversation") from None + under = old.get("parent") if title == old["content"][:80]: # titled after the question being edited: retitle it after the edit title = "New conversation" if page is None and old.get("context"): @@ -227,17 +278,28 @@ def run() -> Iterator[str]: asked = chat.with_context(q, page, cfg) + chat.attached_note(files) readable = set(acl.roles) summary: dict[str, Any] | None = None # what compaction keeps of the earlier messages (chat.compact) + question: int | None = again["id"] if again else None # the question being answered + answer_id = 0 # the answer's message id, taken once the question is saved (a reply is newer than what it follows) + branched: dict[str, Any] | None = None # the `branch` event, when this makes a new version def prepare() -> tuple[list[dict[str, Any]], list[dict[str, Any]], Any]: - nonlocal summary + nonlocal summary, under, question, answer_id, branched + under = chat.head(db, cid) if under == chat.HEAD else under + past = chat.path(db, cid, under) # the version it goes on from: all the model reads of the conversation try: # a conversation that fills the model's context is folded into its summary before it's answered - chat.compact(db, cfg, cid, full_only=True) + chat.compact(db, cfg, cid, full_only=True, line=past) except Exception: # noqa: BLE001 - answered with what fits; tried again after the answer log.warning("chat %s: couldn't update the summary", cid, exc_info=True) - past = chat.history(db, cid) - summary = chat.memory(db, cfg, cid) - chat.add(db, cid, "user", q, attachments=files, context=kept) - if not past and title == "New conversation": + summary = chat.memory(db, cfg, cid, past) + if again is None: + question = chat.add(db, cid, "user", q, attachments=files, context=kept, parent=under) + answer_id = db.next_id("chat_message") + if again is not None: + branched = chat.branch(db, cid, answer_id, question, "assistant") + elif body.edit is not None or body.parent is not None: # beside other questions, unless it went on from a leaf + made = chat.branch(db, cid, question, under, "user") + branched = made if made["versions"]["count"] > 1 else None + if not past and again is None and title == "New conversation": db.q("UPDATE $r SET title = $t", r=R("chat", cid), t=q[:80]) # A conversation over everything gets the namespace its first question is about, when that's clear. if past or c.get("kind") == "setup" or c.get("scope"): @@ -267,7 +329,9 @@ def prepare() -> tuple[list[dict[str, Any]], list[dict[str, Any]], Any]: def save(text: str, cited: list[dict[str, Any]], **kw: Any) -> int: wrote = (model or cfg["llm"].get("model")) if llm.configured(cfg) else None - return int(chat.add(db, cid, "assistant", text, cited, steps=steps, notice=notice, model=wrote, **kw)) + return int( + chat.add(db, cid, "assistant", text, cited, steps=steps, notice=notice, model=wrote, parent=question, mid=answer_id, **kw) + ) def stopped(text: str, cited: list[dict[str, Any]]) -> Iterator[str]: yield _ev("stopped", {"message": "Stopped"}) @@ -350,6 +414,8 @@ def gen() -> Iterator[str]: on = chat.Answering(db, cid) saved = False try: + if branched: + yield _ev("branch", branched) if picked: yield _ev("scoped", {"namespaces": c["scope"]["namespaces"], "confidence": picked["confidence"], "by": picked["by"]}) yield _ev("step", steps[0]) diff --git a/fastapi_backend/app/domain/bridge.py b/fastapi_backend/app/domain/bridge.py index 491b2f4f7..07ae75146 100644 --- a/fastapi_backend/app/domain/bridge.py +++ b/fastapi_backend/app/domain/bridge.py @@ -327,8 +327,8 @@ def _answer(db, cfg, base, account, cid, q, files, act_on, outside): chat.compact(db, cfg, cid, full_only=True) except Exception: # noqa: BLE001 - answered with what fits; tried again after the answer log.warning("bridge: couldn't update the summary of chat %s", cid, exc_info=True) - past = chat.history(db, cid) - summary = chat.memory(db, cfg, cid) + past = chat.path(db, cid) # a room goes on from the head (the version showing, should it be switched in the app) + summary = chat.memory(db, cfg, cid, past) scope = (db.one("SELECT scope FROM $r", r=R("chat", cid)) or {}).get("scope") or None chat.add(db, cid, "user", q, attachments=list(files) or None) asked = q + chat.attached_note(list(files)) diff --git a/fastapi_backend/app/domain/chat.py b/fastapi_backend/app/domain/chat.py index a3b3180e4..ea1c657a2 100644 --- a/fastapi_backend/app/domain/chat.py +++ b/fastapi_backend/app/domain/chat.py @@ -334,22 +334,138 @@ def create(db, account, title=None, scope=None, model=None, kind="chat"): def get(db, cid, account): c = db.one( - "SELECT record::id(id) AS id, account, title, scope, model, kind ?? 'chat' AS kind, created_at, updated_at FROM $r", + "SELECT record::id(id) AS id, account, title, scope, model, kind ?? 'chat' AS kind, head, created_at, updated_at FROM $r", r=R("chat", cid), ) if not c or c["account"] != account: raise KeyError(cid) + if c.get("head") is None: # before its first message, or from before versions + c["head"] = head(db, cid) return c -def history(db, cid): +def history(db, cid, ids=None): + """A conversation's messages, oldest first: every version's, or only those with `ids` (a path).""" return db.rows( "SELECT record::id(id) AS id, role, content, passages, created_at, stopped ?? false AS stopped, steps ?? [] AS steps, " - "attachments ?? [] AS attachments, notice, error, check, model, context FROM chat_message WHERE chat = $c ORDER BY id", + "attachments ?? [] AS attachments, notice, error, check, model, context, parent FROM chat_message WHERE chat = $c" + + (" AND id IN $ids" if ids is not None else "") + + " ORDER BY id", c=cid, + ids=[R("chat_message", i) for i in ids or []], ) +# ---------- versions: a conversation is a tree, each message under the one it follows; the head is the version showing +HEAD = "head" # where add, path and run_command go on from by default: the conversation's head + + +def _head(db, cid): + return (db.one("SELECT head FROM $r", r=R("chat", cid)) or {}).get("head") + + +def head(db, cid): + """The message the conversation goes on from (the last one of the version showing); None before the first.""" + h = _head(db, cid) + if h is None and (nodes := tree(db, cid)): # from before versions: its newest message + h = nodes[-1]["id"] + return h + + +def tree(db, cid): + """Every message of a conversation, every version's, as {id, parent, role, created_at}, oldest first. A reply is + always newer than what it follows. One from before versions is saved as one line first, as the chat-tree upgrade + step does (reads stay right until it has run).""" + nodes = db.rows("SELECT record::id(id) AS id, parent, role, created_at FROM chat_message WHERE chat = $c ORDER BY id", c=cid) + if len(nodes) > 1 and not any(n.get("parent") for n in nodes) and _head(db, cid) is None: + _line_up(db, cid, [n["id"] for n in nodes]) + for before, n in zip(nodes, nodes[1:]): + n["parent"] = before["id"] + return nodes + + +def _line_up(db, cid, ids): + """Save a conversation from before versions as one line: each message under the one before, the last the head.""" + pairs = list(zip(ids, ids[1:])) + for at in range(0, len(pairs), 200): + db.run([f"UPDATE chat_message:{after} SET parent = {before}" for before, after in pairs[at : at + 200]]) + db.q("UPDATE $r SET head = $h", r=R("chat", cid), h=ids[-1]) + + +def lineage(nodes, leaf): + """The ids from the first message to `leaf`, following parents (`nodes` from tree).""" + up, out = {n["id"]: n.get("parent") for n in nodes}, [] + while leaf in up and len(out) < len(up): + out.append(leaf) + leaf = up[leaf] + return out[::-1] + + +def path(db, cid, leaf=HEAD, nodes=None): + """The messages from the first to `leaf` (by default the head), as history() gives them: one version of the + conversation, which the model reads and the app shows. [] for None, before the first message.""" + leaf = head(db, cid) if leaf == HEAD else leaf + ids = lineage(tree(db, cid) if nodes is None else nodes, leaf) if leaf is not None else [] + return history(db, cid, ids) if ids else [] + + +def versions(nodes): + """Each message's versions: it and the others said in its place (the same parent and role), oldest first, as + {id: {index: which one it is from 1, count, ids}}.""" + groups = defaultdict(list) + for n in nodes: + groups[(n.get("parent"), n["role"])].append(n["id"]) + return {i: {"index": k + 1, "count": len(ids), "ids": ids} for ids in groups.values() for k, i in enumerate(ids)} + + +def newest_below(nodes, mid): + """The newest message on `mid`'s branch, itself when nothing follows it: always a leaf, as replies are newer.""" + below, newest = {mid}, mid + for n in nodes: # oldest first, so a parent comes before what follows it + if n.get("parent") in below: + below.add(n["id"]) + newest = n["id"] + return newest + + +def switch(db, cid, mid): + """Show the version `mid` is in: the head moves to the newest message below it, which is returned. KeyError when + `mid` isn't one of the conversation's messages.""" + nodes = tree(db, cid) + if mid not in {n["id"] for n in nodes}: + raise KeyError(mid) + leaf = newest_below(nodes, mid) + db.q("UPDATE $r SET head = $h", r=R("chat", cid), h=leaf) + return leaf + + +def said(db, cid, mid, role=None): + """One of the conversation's messages ({id, role, content, context, attachments, parent}); KeyError when it isn't + one, or isn't of that `role`.""" + m = db.one( + "SELECT record::id(id) AS id, chat, role, content, context, attachments ?? [] AS attachments, parent FROM $r", + r=R("chat_message", mid), + ) + if not m or m["chat"] != cid or (role and m["role"] != role): + raise KeyError(mid) + return m + + +def question_of(db, cid, aid): + """The question one of the conversation's answers answers (as said gives it); KeyError when `aid` isn't an answer.""" + parent = said(db, cid, aid, "assistant").get("parent") + if parent is None: + raise KeyError(aid) + return said(db, cid, parent, "user") + + +def branch(db, cid, mid, parent, role): + """The stream's `branch` event for a new version `mid` (saved, or its id taken for an answer about to be): + {message, parent, versions}.""" + ids = [n["id"] for n in tree(db, cid) if n.get("parent") == parent and n["role"] == role and n["id"] != mid] + [mid] + return {"message": mid, "parent": parent, "versions": {"index": len(ids), "count": len(ids), "ids": ids}} + + def add( db, cid, @@ -364,11 +480,16 @@ def add( attachments=None, context=None, origin=None, + parent=HEAD, + mid=None, ): - """Save a message; an answer keeps the tool steps it took, any notice (e.g. the model can't use tools) and error; a - question asked from a page keeps what it shared of it (shared_context). A question an automation asked keeps its - `origin` ({automation, run, node}), so it isn't taken for a person's message (`chat.message`).""" - mid = db.next_id("chat_message") + """Save a message under `parent` (by default the head; None: a first message) and make it the head; an answer + keeps the tool steps it took, any notice (e.g. the model can't use tools) and error; a question asked from a page + keeps what it shared of it (shared_context). A question an automation asked keeps its `origin` ({automation, run, + node}), so it isn't taken for a person's message (`chat.message`). `mid`: an id taken earlier with + db.next_id("chat_message"), after the parent was saved.""" + parent = head(db, cid) if parent == HEAD else parent + mid = mid or db.next_id("chat_message") db.q( "CREATE $r CONTENT $d", r=R("chat_message", mid), @@ -387,23 +508,21 @@ def add( "model": model, "context": context, "origin": origin or None, + "parent": parent, } ), ) - db.q("UPDATE $r SET updated_at = $t", r=R("chat", cid), t=store.now()) + db.q("UPDATE $r SET updated_at = $t, head = $m", r=R("chat", cid), t=store.now(), m=mid) return mid -def rewind(db, cid, mid): - """Remove a question of yours and everything said after it, to ask it again as edited; returns the old question - (its content and the page context it was asked with).""" - m = db.one("SELECT chat, role, content, context FROM $r", r=R("chat_message", mid)) - if not m or m["chat"] != cid or m["role"] != "user": - raise KeyError(mid) - db.q("DELETE chat_message WHERE chat = $c AND record::id(id) >= $m", c=cid, m=mid) - # a summary that covers what was removed no longer holds: the messages left are folded in again - db.q("UPDATE $r SET summary = NONE WHERE summary.upto >= $m", r=R("chat", cid), m=mid) - return m +def migrate_tree(db): + """Upgrade step chat-tree: conversations from before versions become one line each, every message following the one + before it and the last the head. Only conversations without a head, whose head is set last, so it's safe to run + twice.""" + for cid in db.values("SELECT VALUE record::id(id) FROM chat WHERE head = NONE"): + if ids := [m["id"] for m in db.rows("SELECT record::id(id) AS id FROM chat_message WHERE chat = $c ORDER BY id", c=cid)]: + _line_up(db, cid, ids) # ---------- compaction: a long conversation keeps a summary of what the model no longer sees word for word ---------- @@ -423,17 +542,23 @@ def rewind(db, cid, mid): ) -def _stored(db, cid): - return (db.one("SELECT summary FROM $r", r=R("chat", cid)) or {}).get("summary") or {"text": "", "upto": 0} +def _stored(db, cid, line=None): + """The conversation's summary. It covers the messages up to its `upto` on one version, so with `line` (a version's + messages, from path) one written on another version, whose `upto` isn't on this one, counts as none yet.""" + had = (db.one("SELECT summary FROM $r", r=R("chat", cid)) or {}).get("summary") or {} + if not had or (line is not None and had.get("upto") and had["upto"] not in {m["id"] for m in line}): + return {"text": "", "upto": 0} + return had -def memory(db, cfg, cid): +def memory(db, cfg, cid, line=None): """What compaction knows of a conversation: {"text", "upto": the last message it covers, "at"}, empty before the first summary; None when compaction is off and neither /clear nor /compact was used (the model then sees the last 6 - messages only, as before).""" + messages only, as before). Of the version `line` is (by default the head's): a summary written on another version + isn't used, and the next compaction writes this one's.""" if not llm.configured(cfg): return None - had = _stored(db, cid) + had = _stored(db, cid, path(db, cid) if line is None else line) if cfg["ai"].get("compact", True): return had return {**had, "window": RECENT} if had.get("upto") else None @@ -466,16 +591,18 @@ def full(cfg, messages): return tokens(messages) > (cfg["ai"].get("context_tokens") or CONTEXT_TOKENS) * FULL -def compact(db, cfg, cid, force=False, focus=None, full_only=False): +def compact(db, cfg, cid, force=False, focus=None, full_only=False, line=None): """Fold the messages older than the latest RECENT into the conversation's summary, once COMPACT_AFTER of them have built up or they fill FULL of the model's context window (`full_only`: only then; ai.compact, on by default; needs a model). `force` (/compact) folds every message in now, whatever the setting, with the person's `focus` (what to - keep) if any. The summary is written by ai.compact_model when set, a cheaper model than the chat's. Returns the new - summary (with "folded": how many messages), or None when nothing changed.""" + keep) if any. The summary is written by ai.compact_model when set, a cheaper model than the chat's. It's of the + version `line` is (by default the head's), afresh when the summary was another version's. Returns the new summary + (with "folded": how many messages), or None when nothing changed.""" if not llm.configured(cfg) or not (force or cfg["ai"].get("compact", True)): return None - had = _stored(db, cid) - fresh = [m for m in history(db, cid) if m["id"] > (had.get("upto") or 0)] + line = path(db, cid) if line is None else line + had = _stored(db, cid, line) + fresh = [m for m in line if m["id"] > (had.get("upto") or 0)] # ids grow along a version old = fresh if force else fresh[:-RECENT] if not old: return None @@ -503,10 +630,12 @@ def command(text): return (m.group(1).lower(), (m.group(2) or "").strip()) if m else None -def run_command(db, cfg, cid, name, rest=""): +def run_command(db, cfg, cid, name, rest="", parent=HEAD): """Do a chat command and save it with its reply, which the model won't see afterwards. /clear: the model starts - afresh (the messages stay to read); /compact: everything so far is folded into the summary now. Returns (reply, - the reply's message id).""" + afresh (the messages stay to read); /compact: everything so far is folded into the summary now. On the version + that goes on from `parent` (by default the head). Returns (reply, the reply's message id).""" + parent = head(db, cid) if parent == HEAD else parent + line = path(db, cid, parent) if name == "clear": reply = "Cleared. I'll start afresh from here; the messages above stay for you to read, but I won't use them." keep = None @@ -515,20 +644,20 @@ def run_command(db, cfg, cid, name, rest=""): keep = None else: try: - done, failed = compact(db, cfg, cid, force=True, focus=rest or None), None + done, failed = compact(db, cfg, cid, force=True, focus=rest or None, line=line), None except llm.LLMError as e: done, failed = None, str(e) - keep = _stored(db, cid) + keep = _stored(db, cid, line) if failed: reply, keep = f"I couldn't write the summary, so nothing changed: {failed}", None elif done: n = done["folded"] reply = f"Compacted {n} message{'s' if n != 1 else ''} into a summary; from here on I read it instead:\n\n{done['text']}" - elif any(m["id"] > (keep.get("upto") or 0) for m in history(db, cid)): + elif any(m["id"] > (keep.get("upto") or 0) for m in line): reply, keep = "The model didn't write a summary, so nothing changed. Try again, or /clear to start afresh.", None else: reply = "Nothing new to compact yet." - add(db, cid, "user", f"/{name}" + (f" {rest}" if rest else "")) + add(db, cid, "user", f"/{name}" + (f" {rest}" if rest else ""), parent=parent) mid = add(db, cid, "assistant", reply, []) if name == "clear": keep = {"text": "", "upto": mid, "at": store.now()} diff --git a/fastapi_backend/app/domain/migrations.py b/fastapi_backend/app/domain/migrations.py index 78e7de9e3..afc25e77a 100644 --- a/fastapi_backend/app/domain/migrations.py +++ b/fastapi_backend/app/domain/migrations.py @@ -49,7 +49,7 @@ def steps(): """[(name, fn(db))] in the order they run. Append only.""" - from . import access, auth, hierarchy, jobs + from . import access, auth, chat, hierarchy, jobs return [ # IIIF-only access levels (public, transcript, signed-in, private) become the access setting @@ -60,6 +60,8 @@ def steps(): ("tour-seen", auth.migrate_tours), # documents and images queued to be transcribed are read instead, and those that failed there run again ("document-read-step", jobs.migrate_document_steps), + # conversations become trees of versions: each message follows the one before it, and the last is the head + ("chat-tree", chat.migrate_tree), ] diff --git a/fastapi_backend/app/schemas/chats.py b/fastapi_backend/app/schemas/chats.py index 2cf82c334..159178dd9 100644 --- a/fastapi_backend/app/schemas/chats.py +++ b/fastapi_backend/app/schemas/chats.py @@ -113,6 +113,14 @@ class Attachment(ResponseModel): recording: int | None = Field(None, description="the asset it became, once imported") +class Versions(ResponseModel): + """A message's versions: it and the others with the same parent and role, oldest first.""" + + index: int = Field(description="which of them this one is, from 1") + count: int + ids: list[int] + + class ChatMessage(ResponseModel): id: int role: Literal["user", "assistant"] @@ -127,6 +135,17 @@ class ChatMessage(ResponseModel): error: str | None = Field(None, description="why there's no answer") check: AnswerCheck | None = Field(None, description="its latest source check (POST .../messages/{mid}/check)") model: str | None = Field(None, description="the model that wrote the answer") + parent: int | None = Field(None, description="the message it follows; null for a first question") + versions: Versions | None = Field(None, description="it and the others said in its place, from regenerating or editing") + + +class ChatNode(ResponseModel): + """A message of any version, as `tree` lists them.""" + + id: int + parent: int | None = None + role: Literal["user", "assistant"] + created_at: str | None = None class ChatCapabilities(ResponseModel): @@ -147,7 +166,13 @@ class StopResult(ResponseModel): class Chat(ChatSummary): account: int - messages: list[ChatMessage] = [] + head: int | None = Field(None, description="the last message of the version showing, which the next question follows") + messages: list[ChatMessage] = Field(default_factory=list, description="the version showing, from the first message to `head`") + tree: list[ChatNode] = Field(default_factory=list, description="every message of every version, oldest first") + + +class HeadUpdate(RequestModel): + message: int = Field(description="a message of the version to show: the head moves to the newest message below it") class MessageCreate(RequestModel): @@ -161,10 +186,18 @@ class MessageCreate(RequestModel): model: str | None = Field( None, description="answer this one with another model (one of GET /chats/capabilities `models`), e.g. to retry" ) + parent: int | None = Field( + None, description="the message to go on from (one of this conversation's, of any version); null: the conversation's `head`" + ) + regenerate: int | None = Field( + None, + description="an answer's message id: write a new answer to its question, as another version beside it (`content` is " + "not needed; `model` picks another model)", + ) edit: int | None = Field( None, - description="edit one of your earlier questions (its message id): it and everything after it are replaced by this " - "question and a new answer", + description="edit one of your earlier questions (its message id): this question becomes a new version beside it, and " + "is answered; the old one and what followed it stay as the other version", ) browser: str | None = Field( None, diff --git a/fastapi_backend/tests/api/test_chat_versions.py b/fastapi_backend/tests/api/test_chat_versions.py new file mode 100644 index 000000000..07357e7ae --- /dev/null +++ b/fastapi_backend/tests/api/test_chat_versions.py @@ -0,0 +1,136 @@ +"""Versions: a conversation is a tree; regenerating an answer or editing a question makes a version beside it, and +the one showing (the head) is what the model reads and the next question goes on from.""" + +from __future__ import annotations + +import pytest + +from app.domain import chat +from tests import fake_llm +from tests.api._assist import sse, start_llm +from tests.helpers import login, make_user, seed + + +@pytest.fixture +def llm(cfg): + srv = start_llm(cfg) + cfg["ai"]["tools"] = False # the plain search-and-answer path + yield fake_llm.Handler + srv.shutdown() + + +def ask(client, h, cid, **body): + r = client.post(f"/api/v1/chats/{cid}/messages", headers=h, json=body) + assert r.status_code == 200, r.text + return sse(r.text) + + +def asked_of(seen): + """The questions the model was given, earlier ones first, the one it answers last.""" + return [m["content"] for m in seen[-1]["messages"] if m["role"] == "user"] + + +def test_regenerate_edit_switch_and_go_on(client, new_client, db, cfg, folder, llm): + seed(db, cfg, folder) + make_user(db, "ed@x.io", "editor password 1", roles={"pods": "editor", "calls": "editor"}) + h = login(client, "ed@x.io", "editor password 1") + cid = client.post("/api/v1/chats", headers=h, json={}).json()["id"] + for q in ("When does the shipment leave?", "Who is shipping it?"): + assert "branch" not in ask(client, h, cid, content=q) # going on from the head makes no version + c = client.get(f"/api/v1/chats/{cid}", headers=h).json() + q1, a1, q2, a2 = (m["id"] for m in c["messages"]) + assert [m["parent"] for m in c["messages"]] == [None, q1, a1, q2] and c["head"] == a2 + assert c["messages"][1]["versions"] == {"index": 1, "count": 1, "ids": [a1]} + assert [(n["parent"], n["role"]) for n in c["tree"]] == [(None, "user"), (q1, "assistant"), (a1, "user"), (q2, "assistant")] + + # regenerating the first answer, with another model: a version beside it, reading only what came before it + llm.seen.clear() + ev = ask(client, h, cid, regenerate=a1, model="fake-large") + b1 = ev["done"][0]["message"] + assert ev["branch"] == [{"message": b1, "parent": q1, "versions": {"index": 2, "count": 2, "ids": [a1, b1]}}] + assert asked_of(llm.seen) == [asked_of(llm.seen)[-1]] and "When does the shipment leave?" in asked_of(llm.seen)[-1] + assert llm.seen[-1]["model"] == "fake-large" + c = client.get(f"/api/v1/chats/{cid}", headers=h).json() + assert [m["id"] for m in c["messages"]] == [q1, b1] and c["messages"][1]["model"] == "fake-large" + assert c["messages"][1]["versions"] == {"index": 2, "count": 2, "ids": [a1, b1]} and len(c["tree"]) == 5 + + # going on from this version: the model reads it, not the other + llm.seen.clear() + ask(client, h, cid, content="And where to?") + assert len(asked_of(llm.seen)) == 2 and not any("Who is shipping it?" in s for s in asked_of(llm.seen)) + q3, a3 = (m["id"] for m in client.get(f"/api/v1/chats/{cid}", headers=h).json()["messages"][2:]) + + # switching back shows the first version to its end, and the next question goes on from there + c = client.put(f"/api/v1/chats/{cid}/head", headers=h, json={"message": a1}).json() + assert [m["id"] for m in c["messages"]] == [q1, a1, q2, a2] and c["head"] == a2 + assert c["messages"][1]["versions"]["index"] == 1 + llm.seen.clear() + ask(client, h, cid, content="Is it insured?") + assert any("Who is shipping it?" in s for s in asked_of(llm.seen)) and not any("And where to?" in s for s in asked_of(llm.seen)) + + # editing a question makes a version of it, beside it; the old one keeps what followed it + ev = ask(client, h, cid, content="When does the dyno shipment leave?", edit=q1) + e1 = ev["branch"][0]["message"] + assert ev["branch"][0]["parent"] is None and ev["branch"][0]["versions"] == {"index": 2, "count": 2, "ids": [q1, e1]} + c = client.get(f"/api/v1/chats/{cid}", headers=h).json() + assert [m["content"] for m in c["messages"] if m["role"] == "user"] == ["When does the dyno shipment leave?"] + assert c["messages"][0]["versions"] == {"index": 2, "count": 2, "ids": [q1, e1]} + + # a question can go on from any message, of any version: beside the others there, it's a version too + llm.seen.clear() + ev = ask(client, h, cid, content="Who packs it?", parent=b1) + assert ev["branch"][0]["versions"] == {"index": 2, "count": 2, "ids": [q3, ev["branch"][0]["message"]]} + assert "When does the shipment leave?" in asked_of(llm.seen)[0] and len(asked_of(llm.seen)) == 2 + c = client.get(f"/api/v1/chats/{cid}", headers=h).json() + assert [m["id"] for m in c["messages"]][:2] == [q1, b1] and c["messages"][2]["content"] == "Who packs it?" + assert a3 in {n["id"] for n in c["tree"]} # nothing is deleted + + +def test_what_versions_refuse(client, new_client, db, cfg, folder, llm): + seed(db, cfg, folder) + make_user(db, "ed@x.io", "editor password 1", roles={"pods": "editor"}) + make_user(db, "vi@x.io", "viewer password 1", roles={"pods": "viewer"}) + h = login(client, "ed@x.io", "editor password 1") + other = new_client() + hv = login(other, "vi@x.io", "viewer password 1") + cid = client.post("/api/v1/chats", headers=h, json={}).json()["id"] + ask(client, h, cid, content="When does the shipment leave?") + ask(client, h, cid, content="/clear") + q1, a1, clear, cleared = (m["id"] for m in client.get(f"/api/v1/chats/{cid}", headers=h).json()["messages"]) + elsewhere = client.post("/api/v1/chats", headers=h, json={}).json()["id"] + + def post(json, c=client, hh=h, to=cid): + return c.post(f"/api/v1/chats/{to}/messages", headers=hh, json=json).status_code + + assert post({"regenerate": q1}) == 404 # a question isn't an answer + assert post({"regenerate": 999999}) == 404 + assert post({"regenerate": a1}, to=elsewhere) == 404 + assert post({"regenerate": a1}, other, hv) == 404 + assert post({"regenerate": cleared}) == 400 # a command's reply isn't the model's + assert post({"regenerate": a1, "edit": q1, "content": "x"}) == 400 + assert post({"content": "x", "edit": q1, "parent": a1}) == 400 + assert post({"content": "x", "parent": 999999}) == 404 + assert post({"content": "x", "parent": a1}, to=elsewhere) == 404 + assert client.put(f"/api/v1/chats/{cid}/head", headers=h, json={"message": 999999}).status_code == 404 + assert client.put(f"/api/v1/chats/{elsewhere}/head", headers=h, json={"message": a1}).status_code == 404 + assert other.put(f"/api/v1/chats/{cid}/head", headers=hv, json={"message": a1}).status_code == 404 + assert len(client.get(f"/api/v1/chats/{cid}", headers=h).json()["tree"]) == 4 # nothing was added + + # /clear on another version applies to that version only + client.put(f"/api/v1/chats/{cid}/head", headers=h, json={"message": a1}) + assert client.get(f"/api/v1/chats/{cid}", headers=h).json()["head"] == cleared # the newest below it + ask(client, h, cid, regenerate=a1) + assert chat.memory(db, cfg, cid) == {"text": "", "upto": 0} # the /clear was said on the other version + ask(client, h, cid, content="/clear", parent=q1) # a command can go on from any message too + path = chat.path(db, cid) + assert [m["content"] for m in path][-2:][0] == "/clear" and path[1]["parent"] == q1 + assert chat.memory(db, cfg, cid)["upto"] == path[-1]["id"] + + +def test_rooms_and_automations_go_on_in_one_line(db, cfg, llm): + cid = chat.create(db, 1, kind="bridge") + for i in range(4): + chat.add(db, cid, "user" if i % 2 == 0 else "assistant", f"m{i}") + nodes = chat.tree(db, cid) + assert [n.get("parent") for n in nodes] == [None, *(n["id"] for n in nodes[:-1])] and chat.head(db, cid) == nodes[-1]["id"] + assert all(v["count"] == 1 for v in chat.versions(nodes).values()) diff --git a/fastapi_backend/tests/api/test_chats.py b/fastapi_backend/tests/api/test_chats.py index 917cda647..04e09ba46 100644 --- a/fastapi_backend/tests/api/test_chats.py +++ b/fastapi_backend/tests/api/test_chats.py @@ -478,18 +478,19 @@ def test_editing_a_question(plain, client, new_client, db, cfg, folder, llm): assert other.post(f"/api/v1/chats/{cid}/messages", headers=hv, json={"content": "x", "edit": second}).status_code == 404 elsewhere = client.post("/api/v1/chats", headers=h, json={}).json()["id"] assert client.post(f"/api/v1/chats/{elsewhere}/messages", headers=h, json={"content": "x", "edit": second}).status_code == 404 - assert len(client.get(f"/api/v1/chats/{cid}", headers=h).json()["messages"]) == 6 # nothing was removed + assert len(client.get(f"/api/v1/chats/{cid}", headers=h).json()["tree"]) == 6 # nothing changed - # editing the second question replaces it and everything after it; the model sees only what came before + # editing the second question makes a version beside it, and the old one keeps what followed it; the model sees + # only what came before llm.seen.clear() ev = sse(client.post(f"/api/v1/chats/{cid}/messages", headers=h, json={"content": "Who ships it, exactly?", "edit": second}).text) - assert ev["done"] + assert ev["done"] and ev["branch"][0]["versions"] == {"index": 2, "count": 2, "ids": [second, ev["branch"][0]["message"]]} sent = [m["content"] for m in llm.seen[-1]["messages"] if m["role"] == "user"] assert "When does the shipment leave?" in sent[0] and not any("Who is shipping it?" in s or "And where to?" in s for s in sent) c = client.get(f"/api/v1/chats/{cid}", headers=h).json() assert [m["content"] for m in c["messages"] if m["role"] == "user"] == ["When does the shipment leave?", "Who ships it, exactly?"] assert [m["role"] for m in c["messages"]] == ["user", "assistant", "user", "assistant"] - assert c["title"] == "When does the shipment leave?" + assert c["title"] == "When does the shipment leave?" and len(c["tree"]) == 8 # editing the first question retitles a conversation titled after it, but keeps a title you set first = c["messages"][0]["id"] @@ -504,12 +505,12 @@ def test_editing_a_question(plain, client, new_client, db, cfg, folder, llm): sse(client.post(f"/api/v1/chats/{cid}/messages", headers=h, json={"content": "When does it ship?", "edit": first}).text) assert client.get(f"/api/v1/chats/{cid}", headers=h).json()["title"] == "Shipping" - # a refused edit (a model that isn't offered) removes nothing - n = len(client.get(f"/api/v1/chats/{cid}", headers=h).json()["messages"]) + # a refused edit (a model that isn't offered) changes nothing + n = len(client.get(f"/api/v1/chats/{cid}", headers=h).json()["tree"]) assert ( client.post(f"/api/v1/chats/{cid}/messages", headers=h, json={"content": "x", "edit": first, "model": "gpt-9"}).status_code == 400 ) - assert len(client.get(f"/api/v1/chats/{cid}", headers=h).json()["messages"]) == n + assert len(client.get(f"/api/v1/chats/{cid}", headers=h).json()["tree"]) == n # a question asked from a page keeps the page and the highlighted text when edited page = {"url": "/library", "title": "Library", "text": "Shipment report", "selection": "Dyno shipment"} diff --git a/fastapi_backend/tests/api/test_compaction.py b/fastapi_backend/tests/api/test_compaction.py index c16c7d140..5c2d1e531 100644 --- a/fastapi_backend/tests/api/test_compaction.py +++ b/fastapi_backend/tests/api/test_compaction.py @@ -52,8 +52,18 @@ def test_older_messages_are_folded_into_a_summary(db, cfg, llm): assert chat.compact(db, cfg, cid)["upto"] == chat.history(db, cid)[-chat.RECENT - 1]["id"] assert "Summary so far:\nOK" in summaries(llm.seen)[1]["messages"][1]["content"] - # editing a question the summary covers drops the summary; it's made again from what's left - chat.rewind(db, cid, ids[2]) + # on a version beside a question the summary covers, it isn't used; it's made again from that version's messages + last = chat.head(db, cid) + chat.add(db, cid, "user", "message 2, edited", parent=ids[1]) + assert chat.memory(db, cfg, cid) == {"text": "", "upto": 0} + llm.seen.clear() + talk(db, cid, chat.RECENT + chat.COMPACT_AFTER - 3) # with the 3 messages of this version, enough to fold in + again = chat.compact(db, cfg, cid) + asked = summaries(llm.seen)[0]["messages"][1]["content"] + assert "Summary so far:\n(none yet)" in asked and "Person: message 2, edited" in asked and "message 3\n" not in asked + assert again["upto"] == chat.path(db, cid)[-chat.RECENT - 1]["id"] + # switching back to the first version: the summary is now the other's, so its messages are read until it's made again + assert chat.switch(db, cid, ids[2]) == last == chat.head(db, cid) assert chat.memory(db, cfg, cid) == {"text": "", "upto": 0} diff --git a/fastapi_backend/tests/domain/test_migrations.py b/fastapi_backend/tests/domain/test_migrations.py index 650bbcdd7..23ca1dfce 100644 --- a/fastapi_backend/tests/domain/test_migrations.py +++ b/fastapi_backend/tests/domain/test_migrations.py @@ -14,7 +14,7 @@ # Every step that has shipped, in order. A database remembers steps by name, so these may never be renamed, reordered # or removed: add new steps to the end of migrations.steps() and here. -SHIPPED = ["access-levels", "collection-homes", "tour-seen", "document-read-step"] +SHIPPED = ["access-levels", "collection-homes", "tour-seen", "document-read-step", "chat-tree"] def test_steps_are_append_only(): @@ -62,8 +62,8 @@ def test_steps_an_older_lens_counted_are_not_run_again(db): db.q("DELETE migration") db.q("UPSERT $r SET n = 1", r=R("seq", "migrations")) rid = _old_shape(db) - assert migrations.pending(db) == ["collection-homes", "tour-seen", "document-read-step"] - assert store.migrate(db) == ["collection-homes", "tour-seen", "document-read-step"] + assert migrations.pending(db) == ["collection-homes", "tour-seen", "document-read-step", "chat-tree"] + assert store.migrate(db) == ["collection-homes", "tour-seen", "document-read-step", "chat-tree"] row = db.one("SELECT meta_json, collection FROM $r", r=R("recording", rid)) assert json.loads(row["meta_json"]) == {"access": "signed-in"} # access-levels was counted as done, so not rerun assert row["collection"] == store.default_collection(db, store.ns_id(db, "pods")) @@ -139,7 +139,7 @@ def test_lens_migrations_lists_and_runs(tmp_path, monkeypatch, capsys): assert "pending access-levels" in out and "pending collection-homes" in out cli.main(["--config", str(cfg_path), "migrations", "--run"]) out = capsys.readouterr().out - assert "ran 4 upgrade(s): access-levels, collection-homes, tour-seen, document-read-step" in out + assert "ran 5 upgrade(s): access-levels, collection-homes, tour-seen, document-read-step, chat-tree" in out assert "done access-levels" in out @@ -265,3 +265,36 @@ def test_documents_waiting_to_be_transcribed_are_read_instead(db): assert [s["type"] for s in job[n]["steps"]] == ["read", "analyze", "report"] assert (job[n]["next_step"], job[n]["status"]) == ("read", "queued") assert job["song"]["next_step"] == "transcribe" and len(job["song"]["steps"]) == 4 + + +def test_conversations_from_before_versions_become_one_line(db): + from app.domain import chat + + def older(n): + """A conversation as an older Lens saved it: messages with no parent, and no head.""" + cid, ids = chat.create(db, 1), [] + for i in range(n): + ids.append(db.next_id("chat_message")) + said = {"chat": cid, "role": "user" if i % 2 == 0 else "assistant", "content": f"m{i}"} + db.q("CREATE $r CONTENT $d", r=R("chat_message", ids[-1]), d=said) + return cid, ids + + read, _ = older(3) + assert [m["content"] for m in chat.path(db, read)] == ["m0", "m1", "m2"] # read right before the step runs, too + old, ids = older(5) + empty = chat.create(db, 1) + branched = chat.create(db, 1) # a conversation with versions already is left alone + first = chat.add(db, branched, "user", "q") + chat.add(db, branched, "assistant", "a") + chat.add(db, branched, "user", "q, edited", parent=None) + before = db.rows("SELECT * FROM chat_message WHERE chat = $c ORDER BY id", c=branched) + dict(migrations.steps())["chat-tree"](db) + once = _everything(db) + dict(migrations.steps())["chat-tree"](db) # safe to run twice + assert _everything(db) == once + assert [n.get("parent") for n in chat.tree(db, old)] == [None, *ids[:-1]] and chat.head(db, old) == ids[-1] + assert [m["content"] for m in chat.path(db, old)] == [f"m{i}" for i in range(5)] + assert chat.head(db, empty) is None + assert db.rows("SELECT * FROM chat_message WHERE chat = $c ORDER BY id", c=branched) == before + assert [m["content"] for m in chat.path(db, branched)] == ["q, edited"] + assert chat.versions(chat.tree(db, branched))[first]["count"] == 2 diff --git a/nextjs-frontend/app/openapi-client/index.ts b/nextjs-frontend/app/openapi-client/index.ts index b5db04b3b..eca869891 100644 --- a/nextjs-frontend/app/openapi-client/index.ts +++ b/nextjs-frontend/app/openapi-client/index.ts @@ -310,6 +310,7 @@ export type { ChatCompletionRequest, ChatCreate, ChatMessage, + ChatNode, ChatScope, ChatSummary, ChatUpdate, @@ -1492,6 +1493,7 @@ export type { GraphVerifyResponses, Handling, HandlingChange, + HeadUpdate, Health, HealthCounts, HealthDatabase, @@ -3107,6 +3109,11 @@ export type { SuggestTermsErrors, SuggestTermsResponse, SuggestTermsResponses, + SwitchVersionData, + SwitchVersionError, + SwitchVersionErrors, + SwitchVersionResponse, + SwitchVersionResponses, SyncFedoraData, SyncFedoraResponse, SyncFedoraResponses, @@ -3588,6 +3595,7 @@ export type { VaultPasskey, VaultStart, VaultStatus, + Versions, VersionSaved, VideoImportRequest, ViewCreate, diff --git a/nextjs-frontend/app/openapi-client/sdk.gen.ts b/nextjs-frontend/app/openapi-client/sdk.gen.ts index 552691e6a..d381ba153 100644 --- a/nextjs-frontend/app/openapi-client/sdk.gen.ts +++ b/nextjs-frontend/app/openapi-client/sdk.gen.ts @@ -1488,6 +1488,9 @@ import type { SuggestTermsData, SuggestTermsErrors, SuggestTermsResponses, + SwitchVersionData, + SwitchVersionErrors, + SwitchVersionResponses, SyncFedoraData, SyncFedoraResponses, TagRecordingsData, @@ -9942,7 +9945,8 @@ export class Chats { /** * Get Chat * - * A conversation with its messages. Citations follow your current access: ones you can no longer read are left out. + * A conversation with the version showing (`messages`, each with its `versions`) and every version's messages + * (`tree`). Citations follow your current access: ones you can no longer read are left out. */ public static getChat( options: Options, @@ -9971,6 +9975,25 @@ export class Chats { }); } + /** + * Switch Version + * + * Show another version: the one `message` is in, from the first message to the newest one below `message`. The + * next question goes on from there. Returns the conversation as GET /chats/{cid} does. + */ + public static switchVersion( + options: Options, + ): RequestResult { + return (options.client ?? client).put({ + url: "/api/v1/chats/{cid}/head", + ...options, + headers: { + "Content-Type": "application/json", + ...options.headers, + }, + }); + } + /** * Send Message * @@ -9978,9 +10001,14 @@ export class Chats { * reads with the question). Streams events: step, approval, notice, passages, token (answer text), error, stopped (POST * /chats/{cid}/stop: what came before is saved, marked stopped), done (the saved message id). * - * With `edit`, one of your earlier questions is edited: it and everything after it are replaced by this question and - * its new answer (404 if it isn't a question in this conversation). Without `context`, it keeps the page it was asked - * from (where, and the highlighted text; not the page's text, which isn't kept). + * A conversation is a tree of versions (GET /chats/{cid}: `tree`, and each message's `versions`); the question goes + * on from `parent` when given (any message of the conversation), else from the version showing (`head`). With + * `regenerate` (an answer's id) its question is answered again, as a new version beside that answer (`content` isn't + * needed; `model` may pick another). With `edit`, one of your earlier questions is edited: this question becomes a new + * version beside it and is answered, and the old one stays with what followed it (404 if it isn't a question in this + * conversation). Either keeps the page the question was asked from (where, and the highlighted text; not the page's + * text, which isn't kept) unless `context` is given, and streams `branch` first ({message, parent, versions}: the + * new version, the message it follows and its versions); so does a question beside others under `parent`. * * `/clear` makes the model start afresh (the messages stay to read) and `/compact [what to keep]` folds the * conversation so far into its summary now; each streams cleared or compacted, then its reply as an answer. diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index 06ed72de5..1feb6eb15 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -3395,10 +3395,24 @@ export type Chat = { * Account */ account: number; + /** + * Head + * + * the last message of the version showing, which the next question follows + */ + head?: number | null; /** * Messages + * + * the version showing, from the first message to `head` */ messages?: Array; + /** + * Tree + * + * every message of every version, oldest first + */ + tree?: Array; [key: string]: unknown; }; @@ -3618,6 +3632,41 @@ export type ChatMessage = { * the model that wrote the answer */ model?: string | null; + /** + * Parent + * + * the message it follows; null for a first question + */ + parent?: number | null; + /** + * it and the others said in its place, from regenerating or editing + */ + versions?: Versions | null; + [key: string]: unknown; +}; + +/** + * ChatNode + * + * A message of any version, as `tree` lists them. + */ +export type ChatNode = { + /** + * Id + */ + id: number; + /** + * Parent + */ + parent?: number | null; + /** + * Role + */ + role: "user" | "assistant"; + /** + * Created At + */ + created_at?: string | null; [key: string]: unknown; }; @@ -7807,6 +7856,18 @@ export type HandlingChange = { digest?: boolean | null; }; +/** + * HeadUpdate + */ +export type HeadUpdate = { + /** + * Message + * + * a message of the version to show: the head moves to the newest message below it + */ + message: number; +}; + /** * Health */ @@ -10644,10 +10705,22 @@ export type MessageCreate = { * answer this one with another model (one of GET /chats/capabilities `models`), e.g. to retry */ model?: string | null; + /** + * Parent + * + * the message to go on from (one of this conversation's, of any version); null: the conversation's `head` + */ + parent?: number | null; + /** + * Regenerate + * + * an answer's message id: write a new answer to its question, as another version beside it (`content` is not needed; `model` picks another model) + */ + regenerate?: number | null; /** * Edit * - * edit one of your earlier questions (its message id): it and everything after it are replaced by this question and a new answer + * edit one of your earlier questions (its message id): this question becomes a new version beside it, and is answered; the old one and what followed it stay as the other version */ edit?: number | null; /** @@ -21990,6 +22063,29 @@ export type VersionSaved = { [key: string]: unknown; }; +/** + * Versions + * + * A message's versions: it and the others with the same parent and role, oldest first. + */ +export type Versions = { + /** + * Index + * + * which of them this one is, from 1 + */ + index: number; + /** + * Count + */ + count: number; + /** + * Ids + */ + ids: Array; + [key: string]: unknown; +}; + /** * VideoImportRequest */ @@ -38306,6 +38402,36 @@ export type UpdateChatResponses = { export type UpdateChatResponse = UpdateChatResponses[keyof UpdateChatResponses]; +export type SwitchVersionData = { + body: HeadUpdate; + path: { + /** + * Cid + */ + cid: number; + }; + query?: never; + url: "/api/v1/chats/{cid}/head"; +}; + +export type SwitchVersionErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type SwitchVersionError = SwitchVersionErrors[keyof SwitchVersionErrors]; + +export type SwitchVersionResponses = { + /** + * Successful Response + */ + 200: Chat; +}; + +export type SwitchVersionResponse = SwitchVersionResponses[keyof SwitchVersionResponses]; + export type SendMessageData = { body: MessageCreate; path: { diff --git a/nextjs-frontend/openapi.json b/nextjs-frontend/openapi.json index 285f91af5..22e7c2c99 100644 --- a/nextjs-frontend/openapi.json +++ b/nextjs-frontend/openapi.json @@ -24824,7 +24824,7 @@ "chats" ], "summary": "Get Chat", - "description": "A conversation with its messages. Citations follow your current access: ones you can no longer read are left out.", + "description": "A conversation with the version showing (`messages`, each with its `versions`) and every version's messages\n(`tree`). Citations follow your current access: ones you can no longer read are left out.", "operationId": "get_chat", "parameters": [ { @@ -24952,13 +24952,66 @@ } } }, + "/api/v1/chats/{cid}/head": { + "put": { + "tags": [ + "chats" + ], + "summary": "Switch Version", + "description": "Show another version: the one `message` is in, from the first message to the newest one below `message`. The\nnext question goes on from there. Returns the conversation as GET /chats/{cid} does.", + "operationId": "switch_version", + "parameters": [ + { + "name": "cid", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "title": "Cid" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HeadUpdate" + } + } + } + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Chat" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, "/api/v1/chats/{cid}/messages": { "post": { "tags": [ "chats" ], "summary": "Send Message", - "description": "Ask a question, optionally from a page (`context`: the page, its text and any highlighted part, which the model\nreads with the question). Streams events: step, approval, notice, passages, token (answer text), error, stopped (POST\n/chats/{cid}/stop: what came before is saved, marked stopped), done (the saved message id).\n\nWith `edit`, one of your earlier questions is edited: it and everything after it are replaced by this question and\nits new answer (404 if it isn't a question in this conversation). Without `context`, it keeps the page it was asked\nfrom (where, and the highlighted text; not the page's text, which isn't kept).\n\n`/clear` makes the model start afresh (the messages stay to read) and `/compact [what to keep]` folds the\nconversation so far into its summary now; each streams cleared or compacted, then its reply as an answer.\n\nWith `browser` (the session of a browser's Lens side panel, POST /browser/sessions), the assistant can use that\nbrowser while the side panel is connected: open pages, read them, click, type and download files into the\nconversation (docs/browser-assistant.md).", + "description": "Ask a question, optionally from a page (`context`: the page, its text and any highlighted part, which the model\nreads with the question). Streams events: step, approval, notice, passages, token (answer text), error, stopped (POST\n/chats/{cid}/stop: what came before is saved, marked stopped), done (the saved message id).\n\nA conversation is a tree of versions (GET /chats/{cid}: `tree`, and each message's `versions`); the question goes\non from `parent` when given (any message of the conversation), else from the version showing (`head`). With\n`regenerate` (an answer's id) its question is answered again, as a new version beside that answer (`content` isn't\nneeded; `model` may pick another). With `edit`, one of your earlier questions is edited: this question becomes a new\nversion beside it and is answered, and the old one stays with what followed it (404 if it isn't a question in this\nconversation). Either keeps the page the question was asked from (where, and the highlighted text; not the page's\ntext, which isn't kept) unless `context` is given, and streams `branch` first ({message, parent, versions}: the\nnew version, the message it follows and its versions); so does a question beside others under `parent`.\n\n`/clear` makes the model start afresh (the messages stay to read) and `/compact [what to keep]` folds the\nconversation so far into its summary now; each streams cleared or compacted, then its reply as an answer.\n\nWith `browser` (the session of a browser's Lens side panel, POST /browser/sessions), the assistant can use that\nbrowser while the side panel is connected: open pages, read them, click, type and download files into the\nconversation (docs/browser-assistant.md).", "operationId": "send_message", "parameters": [ { @@ -36013,13 +36066,33 @@ "type": "integer", "title": "Account" }, + "head": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "title": "Head", + "description": "the last message of the version showing, which the next question follows" + }, "messages": { "items": { "$ref": "#/components/schemas/ChatMessage" }, "type": "array", "title": "Messages", - "default": [] + "description": "the version showing, from the first message to `head`" + }, + "tree": { + "items": { + "$ref": "#/components/schemas/ChatNode" + }, + "type": "array", + "title": "Tree", + "description": "every message of every version, oldest first" } }, "additionalProperties": true, @@ -36383,6 +36456,29 @@ ], "title": "Model", "description": "the model that wrote the answer" + }, + "parent": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "title": "Parent", + "description": "the message it follows; null for a first question" + }, + "versions": { + "anyOf": [ + { + "$ref": "#/components/schemas/Versions" + }, + { + "type": "null" + } + ], + "description": "it and the others said in its place, from regenerating or editing" } }, "additionalProperties": true, @@ -36394,6 +36490,52 @@ ], "title": "ChatMessage" }, + "ChatNode": { + "properties": { + "id": { + "type": "integer", + "title": "Id" + }, + "parent": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "title": "Parent" + }, + "role": { + "type": "string", + "enum": [ + "user", + "assistant" + ], + "title": "Role" + }, + "created_at": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Created At" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "id", + "role" + ], + "title": "ChatNode", + "description": "A message of any version, as `tree` lists them." + }, "ChatScope": { "properties": { "namespaces": { @@ -43618,6 +43760,21 @@ "title": "HandlingChange", "description": "A setting left out stays as it is; null goes back to the settings' default; 0 days keeps them for good." }, + "HeadUpdate": { + "properties": { + "message": { + "type": "integer", + "title": "Message", + "description": "a message of the version to show: the head moves to the newest message below it" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "message" + ], + "title": "HeadUpdate" + }, "Health": { "properties": { "database": { @@ -48372,6 +48529,30 @@ "title": "Model", "description": "answer this one with another model (one of GET /chats/capabilities `models`), e.g. to retry" }, + "parent": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "title": "Parent", + "description": "the message to go on from (one of this conversation's, of any version); null: the conversation's `head`" + }, + "regenerate": { + "anyOf": [ + { + "type": "integer" + }, + { + "type": "null" + } + ], + "title": "Regenerate", + "description": "an answer's message id: write a new answer to its question, as another version beside it (`content` is not needed; `model` picks another model)" + }, "edit": { "anyOf": [ { @@ -48382,7 +48563,7 @@ } ], "title": "Edit", - "description": "edit one of your earlier questions (its message id): it and everything after it are replaced by this question and a new answer" + "description": "edit one of your earlier questions (its message id): this question becomes a new version beside it, and is answered; the old one and what followed it stay as the other version" }, "browser": { "anyOf": [ @@ -67802,6 +67983,35 @@ ], "title": "VersionSaved" }, + "Versions": { + "properties": { + "index": { + "type": "integer", + "title": "Index", + "description": "which of them this one is, from 1" + }, + "count": { + "type": "integer", + "title": "Count" + }, + "ids": { + "items": { + "type": "integer" + }, + "type": "array", + "title": "Ids" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "index", + "count", + "ids" + ], + "title": "Versions", + "description": "A message's versions: it and the others with the same parent and role, oldest first." + }, "VideoImportRequest": { "properties": { "url": { From 659748599d5b8a4a3f27a1e2a057cf4b85790ed0 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 17 Oct 2026 00:06:10 +0000 Subject: [PATCH 2/3] feat(chat): switch between versions and regenerate answers in the web app MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A question or an answer with versions shows ‹ 2 / 3 › to move between them (arrow keys too); a version already seen shows at once, then PUT /chats/{cid}/head saves it. Regenerate (and Regenerate with… another model) replaces Try another model and streams the new answer in place, as does an edit. The next question goes on from the version showing, and approvals show in the version they were proposed in. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01HXPWXcSCQb9hWPr7xkZu7B --- CHANGELOG.md | 5 +- docs/assistant.md | 15 +- .../__tests__/chat-versions.test.tsx | 141 ++++++++++++++++++ nextjs-frontend/components/chat/answer.tsx | 54 ++++--- nextjs-frontend/components/chat/chat-app.tsx | 113 +++++++++++--- .../components/chat/user-bubble.tsx | 12 +- nextjs-frontend/components/chat/versions.tsx | 110 ++++++++++++++ 7 files changed, 399 insertions(+), 51 deletions(-) create mode 100644 nextjs-frontend/__tests__/chat-versions.test.tsx create mode 100644 nextjs-frontend/components/chat/versions.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index b34479b88..bbed153fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,8 +17,9 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version instead of deleting what followed, regenerating an answer (`regenerate`, with the same model or another) writes one beside it, and a question can go on from any message (`parent`). `GET /api/v1/chats/` shows each message's versions and every version's messages, and `PUT /api/v1/chats//head` switches between them; the model reads - only the version showing. The web app's switcher and Regenerate button are in progress. Existing conversations are - upgraded on the next start (`chat-tree`) ([Versions](docs/assistant.md)). + only the version showing. In the web app, Regenerate (and Regenerate with… another model) replaces Try another + model, and `‹ 2 / 3 ›` under a question or an answer moves between its versions. Existing conversations are upgraded + on the next start (`chat-tree`) ([Versions](docs/assistant.md)). - **Describe an automation in words.** `POST /api/v1/automations/draft` asks the model once to turn a sentence into a draft automation to try and save, and says what didn't fit and what it couldn't express ([Describe it in words](docs/automations.md#describe-it-in-words)). diff --git a/docs/assistant.md b/docs/assistant.md index 9f1ff4852..9cb031a98 100644 --- a/docs/assistant.md +++ b/docs/assistant.md @@ -27,14 +27,17 @@ then. others in its place, oldest first), every version's messages as `tree`, and `head`, the last message showing. `PUT /api/v1/chats//head` with `{"message": }` shows the version that message is in, to its newest message. A new version streams `branch` first (`message`, `parent`, `versions`). Chat rooms ([Chat rooms](chat-rooms.md)) and - automations go on from the version showing. In the web app, Edit makes a version; switching between versions and - Regenerate are in progress. + automations go on from the version showing. In the web app, Edit on a question and Regenerate on an answer (its menu: + Regenerate with… another model) make a version, which streams in place; `‹ 2 / 3 ›` under a question or an answer + with versions moves between them (the arrow keys too, while it has focus), showing what followed each, and the next + question goes on from the version showing. Approvals proposed in one version show in that version. - **Long conversations:** the model reads the latest six messages word for word. Once eight more have built up behind them, they're folded into a running summary kept on the conversation (one model call, after an answer), and every answer gets that summary plus the messages it doesn't cover yet. Room conversations (chat-rooms.md) go on for good, so they rely on it most. A summary covers the version it was written on: on a version that leaves what it - covers, the model reads that version's messages, and the next summary is made from them. A conversation that fills half of the model's context window (**Chat model's context**, `ai.context_tokens`, - 16000 when empty; about four characters a token) is folded in early, before the next answer, whatever the count. + covers, the model reads that version's messages, and the next summary is made from them. A conversation that fills + half of the model's context window (**Chat model's context**, `ai.context_tokens`, 16000 when empty; about four + characters a token) is folded in early, before the next answer, whatever the count. **Model for summaries** (`ai.compact_model`) writes them with a smaller, cheaper model on the same provider; empty, the chat's model does. Settings › AI assistant › Summarise long conversations (`ai.compact`, on) turns it off; off, or with no model, only the last six messages are read. @@ -45,8 +48,8 @@ then. reply, is read by the model afterwards. - **Changed access:** old citations are filtered by the person's current access when a conversation is reopened. - **Choosing the model:** a conversation can use any model an admin offers (`llm.chat_models`, else whatever the model - server lists); Try another model asks a question again with a different one. Each answer records the model that - wrote it. + server lists); Regenerate with… writes a new answer with a different one, beside the old. Each answer records the + model that wrote it. - **Chat on any page:** every page except Chat itself has an Ask button that opens a chat panel beside the page (full screen on phones). Its conversation follows you between pages, and across visits in the same browser, until New chat; it is an ordinary conversation, also listed in Chat. Each question can share the page's text (the page chip, diff --git a/nextjs-frontend/__tests__/chat-versions.test.tsx b/nextjs-frontend/__tests__/chat-versions.test.tsx new file mode 100644 index 000000000..5b012d612 --- /dev/null +++ b/nextjs-frontend/__tests__/chat-versions.test.tsx @@ -0,0 +1,141 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import type { Chat, ChatMessage, ChatNode } from "@/app/openapi-client/types.gen"; +import { Answer } from "@/components/chat/answer"; +import { lineage, newestBelow, switched, versionsOf, VersionSwitch } from "@/components/chat/versions"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ Chats: { checkMessage: jest.fn() }, Search: {} })); +jest.mock("@/lib/api/browser", () => ({ useApiClient: () => ({}), data: jest.fn() })); +jest.mock("@/lib/hooks/session", () => ({ + useArchive: () => ({ can: () => true, namespaces: [], roleIn: () => "editor" }), +})); + +// q1 → a1 → q2 → a2, a1 regenerated as b1 (→ q3 → a3), and q1 edited as e1 (→ f1) +const node = (id: number, parent: number | null, role: "user" | "assistant"): ChatNode => ({ id, parent, role }); +const TREE = [ + node(1, null, "user"), + node(2, 1, "assistant"), + node(3, 2, "user"), + node(4, 3, "assistant"), + node(5, 1, "assistant"), + node(6, 5, "user"), + node(7, 6, "assistant"), + node(8, null, "user"), + node(9, 8, "assistant"), +]; +const said = (id: number, role: "user" | "assistant" = id % 2 ? "user" : "assistant"): ChatMessage => ({ + id, + role, + content: `m${id}`, +}); + +describe("versions of a conversation", () => { + it("groups versions by what they follow and who said them, oldest first", () => { + const v = versionsOf(TREE); + expect(v.get(5)).toEqual({ index: 2, count: 2, ids: [2, 5] }); + expect(v.get(8)).toEqual({ index: 2, count: 2, ids: [1, 8] }); + expect(v.get(3)).toEqual({ index: 1, count: 1, ids: [3] }); + }); + + it("shows a version to its newest message", () => { + expect(newestBelow(TREE, 2)).toBe(4); + expect(newestBelow(TREE, 1)).toBe(7); + expect(newestBelow(TREE, 9)).toBe(9); + expect(lineage(TREE, 7)).toEqual([1, 5, 6, 7]); + expect(lineage(TREE, 99)).toEqual([]); + }); + + it("switches at once to a version already seen, and waits for one that isn't", () => { + const chat = { + id: 1, + title: "t", + account: 1, + head: 4, + tree: TREE, + messages: [1, 2, 3, 4].map((i) => said(i)), + } as Chat; + const seen = new Map([1, 2, 3, 4, 5].map((i) => [i, said(i, i === 5 ? "assistant" : undefined)])); + expect(switched(chat, 5, seen)).toBeNull(); // 6 and 7 haven't been loaded + for (const i of [6, 7]) seen.set(i, said(i)); + const out = switched(chat, 5, seen)!; + expect(out.head).toBe(7); + expect(out.messages!.map((m) => m.id)).toEqual([1, 5, 6, 7]); + expect(out.messages![1].versions).toEqual({ index: 2, count: 2, ids: [2, 5] }); + }); +}); + +describe("the version switch", () => { + const wrap = (ui: React.ReactNode) => render({ui}); + + it("moves between versions with its arrows and the arrow keys, and stops at either end", () => { + const onPick = jest.fn(); + wrap(); + expect(screen.getByRole("group", { name: "Versions of this answer" })).toHaveTextContent("2 / 3"); + fireEvent.click(screen.getByRole("button", { name: "Previous version of this answer" })); + fireEvent.click(screen.getByRole("button", { name: "Next version of this answer" })); + fireEvent.keyDown(screen.getByRole("button", { name: "Next version of this answer" }), { key: "ArrowLeft" }); + expect(onPick.mock.calls).toEqual([[2], [10], [2]]); + }); + + it("doesn't go past the first or last", () => { + const onPick = jest.fn(); + wrap(); + const back = screen.getByRole("button", { name: "Previous version of this question" }); + expect(back).toHaveAttribute("aria-disabled", "true"); + fireEvent.click(back); + fireEvent.keyDown(back, { key: "ArrowLeft" }); + expect(onPick).not.toHaveBeenCalled(); + fireEvent.keyDown(back, { key: "ArrowRight" }); + expect(onPick).toHaveBeenCalledWith(8); + }); + + it("shows nothing for a message with one version", () => { + const { container } = wrap(); + expect(container).toBeEmptyDOMElement(); + }); +}); + +describe("regenerating an answer", () => { + const answer = (onRegenerate?: (m?: string) => void) => + render( + + + {}} + onPreview={() => {}} + /> + + , + ); + + it("writes a new answer with the same model, or another one", async () => { + const onRegenerate = jest.fn(); + answer(onRegenerate); + fireEvent.click(screen.getByRole("button", { name: "Regenerate" })); + expect(onRegenerate).toHaveBeenLastCalledWith(); + fireEvent.keyDown(screen.getByRole("button", { name: "Regenerate with another model" }), { key: "Enter" }); + expect(screen.queryByRole("menuitem", { name: "fake" })).not.toBeInTheDocument(); // it wrote this one + fireEvent.click(await screen.findByRole("menuitem", { name: "fake-large" })); + await waitFor(() => expect(onRegenerate).toHaveBeenLastCalledWith("fake-large")); // a menu acts once it has closed + }); + + it("isn't offered without a handler, e.g. while another answer is written", () => { + answer(); + expect(screen.queryByRole("button", { name: "Regenerate" })).not.toBeInTheDocument(); + }); +}); diff --git a/nextjs-frontend/components/chat/answer.tsx b/nextjs-frontend/components/chat/answer.tsx index 1775be1f4..d01e06c57 100644 --- a/nextjs-frontend/components/chat/answer.tsx +++ b/nextjs-frontend/components/chat/answer.tsx @@ -1,7 +1,7 @@ "use client"; import { useMutation, useQuery } from "@tanstack/react-query"; -import { ChevronDown, RotateCcw, ShieldCheck, Sparkles, Square } from "lucide-react"; +import { ChevronDown, RefreshCw, RotateCcw, ShieldCheck, Square } from "lucide-react"; import Link from "next/link"; import { useMemo } from "react"; @@ -22,7 +22,7 @@ import { RichText } from "@/components/chat/rich-text"; import { scopeWords, type Scope } from "@/components/chat/scope"; import { ToolSteps } from "@/components/chat/steps"; import type { ToolStep, TurnStatus } from "@/components/chat/stream"; -import { Button } from "@/components/ui/button"; +import { Button, IconButton } from "@/components/ui/button"; import { Menu, MenuContent, MenuItem, MenuLabel, MenuTrigger } from "@/components/ui/menu"; import { data, useApiClient } from "@/lib/api/browser"; import { plural } from "@/lib/format"; @@ -137,9 +137,12 @@ export type AnswerProps = { /** Stop was pressed: the answer ends after the piece or step it's on. */ stopping?: boolean; onRetry?: () => void; - /** Ask the same question again with another of these models (the one that wrote this answer is left out). */ + /** Write a new answer to the same question, as another version beside this one: with this answer's model, or with + * another of `models` (the one that wrote this answer is left out). */ models?: string[]; - onRetryWith?: (model: string) => void; + onRegenerate?: (model?: string) => void; + /** The switch between this answer's versions (VersionSwitch), shown with its actions. */ + versions?: React.ReactNode; onAddScope?: (ns: string) => void; onShowSources?: () => void; active?: boolean; @@ -178,6 +181,7 @@ export function Answer(p: AnswerProps) { const result = p.check ?? check.data ?? null; const unsupported = (result?.verdicts ?? []).filter((v) => v.supported === false).map((v) => String(v.claim ?? "")); const from = fromWhere(p.passages); + const others = (p.models ?? []).filter((m) => m !== p.model); return (
@@ -306,24 +310,30 @@ export function Answer(p: AnswerProps) { {p.stopping ? "Stopping…" : "Stop"} )} - {!streaming && p.onRetryWith && (p.models ?? []).filter((m) => m !== p.model).length > 0 && ( - - - - - - Ask again with - {(p.models ?? []) - .filter((m) => m !== p.model) - .map((m) => ( - p.onRetryWith?.(m)}> - {m} - - ))} - - + {p.versions} + {!streaming && p.onRegenerate && ( + + p.onRegenerate?.()} className="[&_svg]:size-4"> + + + {others.length > 0 && ( + + + + + + + + Regenerate with… + {others.map((m) => ( + p.onRegenerate?.(m)}> + {m} + + ))} + + + )} + )} {p.status === "done" && p.messageId != null && p.chatId != null && cites.length > 0 && !noModel && !result && (