From f20d919e2d456ee8a9957a00d1a83b181c6b23f7 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sun, 18 Oct 2026 08:20:50 +0000 Subject: [PATCH] fix(chat): show a new conversation's first answer, and save answers when the client leaves The web app re-read a new conversation after its answer was saved with invalidateQueries, which waits on a first read still under way (asked before the save) instead of reading again, so the question showed with "No answer was saved". refetchFresh drops that read and reads afresh; Chat, the page assistant and a recording's Chat tab use it. The answer stream now runs on a thread of its own, so a client that leaves mid-answer no longer abandons it unsaved, and its cleanup never runs on whichever thread collects the generator. Also lists answering questions about Lens itself as built (as #578), which the requirements check needs on main. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VGkdmhXwoXaBtU2n9nbdnt --- CHANGELOG.md | 7 ++ README.md | 7 +- docs/assistant.md | 3 + docs/frontend.md | 3 + docs/requirements.md | 2 + fastapi_backend/app/api/v1/routes/chats.py | 40 +++++++++- fastapi_backend/tests/api/test_chats.py | 77 +++++++++++++++++++ .../__tests__/chat-fresh-answer.test.tsx | 52 +++++++++++++ nextjs-frontend/components/chat/chat-app.tsx | 7 +- nextjs-frontend/components/chat/data.ts | 12 ++- .../components/page-chat/page-chat.tsx | 4 +- .../components/recording/chat-tab.tsx | 4 +- 12 files changed, 202 insertions(+), 16 deletions(-) create mode 100644 nextjs-frontend/__tests__/chat-fresh-answer.test.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 15df2881e..4097c2cf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **A new conversation's first answer no longer goes missing.** A quick answer in a new conversation sometimes showed + "No answer was saved for this question" though it was saved: the conversation's first read, made before the answer + was saved, was used in place of reading it again. The web app (Chat, the page assistant and a recording's Chat tab) + now drops a read under way and reads the conversation afresh once the answer is saved. And an answer is now written + on a thread of its own, so closing the tab, losing the connection or moving to another page no longer drops it: it's + finished and saved all the same, and only Stop ends it early ([Chat](docs/assistant.md#chat)). + ## 0.15.0 October 18, 2026 {id="0.15.0"} - **The assistant sends mail, adds events and writes files, after your yes.** New tools `send_mail` (send, or save a diff --git a/README.md b/README.md index 8ea54f243..bd03cae91 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,8 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p [API](docs/api.md), [the graph as files](docs/file-tree.md), [authentication](docs/authentication.md); [checks](docs/requirements.md#connect-your-agents)) - **Ask, and get cited answers.** A chat assistant on every page, and by voice, searches your archive by words (and by - meaning, once turned on), reads it, cites it and acts on it with tools: changes wait for your approval. Once an admin + meaning, once turned on), reads it, cites it and acts on it with tools: changes wait for your approval. It answers + questions about Lens itself from its docs and this server's settings. Once an admin turns them on, it also searches the web and reads pages, cited like the archive, and runs Python and bash in a sandbox of each conversation's own. An admin's assistant searches and reads the mail, calendars and files connected to Lens (Gmail, Google Calendar, Google Drive, S3 and the rest), cites messages and events, and brings them in once you @@ -92,7 +93,7 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p its own name, instructions and memory, answering in Telegram and Slack (built in, with voice notes and files), in Discord, Matrix, WhatsApp and other chat rooms through Matterbridge, and by email at an address of its own that you email or forward things to. ([Chat](docs/assistant.md), [web search](docs/assistant.md#web-search-and-reading-pages), - [mail, calendars and files](docs/assistant.md#mail-calendars-and-files), [code sandbox](docs/assistant.md#code-sandbox), [answer records](docs/assistant.md#what-each-answer-records), + [mail, calendars and files](docs/assistant.md#mail-calendars-and-files), [code sandbox](docs/assistant.md#code-sandbox), [questions about Lens](docs/assistant.md#questions-about-lens-itself), [answer records](docs/assistant.md#what-each-answer-records), [chat rooms](docs/chat-rooms.md), [email](docs/assistant-email.md), [speech providers](docs/speech-providers.md); [checks](docs/requirements.md#ask-and-get-cited-answers)) - **Extend the assistant.** Add tools (a prompt, a web request, a graph drawn on the canvas, or sandboxed Python), @@ -241,8 +242,6 @@ still loading. ([Components](docs/components.md), These are open pull requests, not yet part of Lens: -- **The assistant** answering questions about Lens itself, from its docs and this server - ([#400](https://github.com/adeelahmad/lense/pull/400)). - **Chat:** a conversation exported as Markdown, PDF or JSON ([#474](https://github.com/adeelahmad/lense/pull/474)). - **Pictures:** low-quality images enhanced before they're read ([#479](https://github.com/adeelahmad/lense/pull/479)). - **Web clipper:** signing in, or using your API token, not only an import webhook's diff --git a/docs/assistant.md b/docs/assistant.md index 04c5b4244..a914a2be4 100644 --- a/docs/assistant.md +++ b/docs/assistant.md @@ -18,6 +18,9 @@ then. ([Titles](#titles)). - **Stopping:** Stop asks the server to end the answer after the piece or tool step it's on (whichever server process is writing it); what came before is saved, marked stopped. A model call already under way finishes first. +- **Leaving mid-answer:** an answer is written on a thread of its own, not by the connection reading it, so closing the + tab, losing the connection or moving to another page doesn't stop it: it's finished and saved, and opening the + conversation again shows it. Only Stop ends an answer early. - **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, their latest source check and what produced them. - **Versions:** a conversation is a tree, and nothing in it is deleted. Regenerating an answer writes a new answer to diff --git a/docs/frontend.md b/docs/frontend.md index 1e430b69a..4fcf4ead3 100644 --- a/docs/frontend.md +++ b/docs/frontend.md @@ -88,6 +88,9 @@ four-colour loop (Transcribe blue, Diarize red, Analyze green, Summarize and Rep yet. Refreshing data already on screen (polling, refetching after a change) and anything quicker than 150 ms don't show it. * Streams (job events, chat answers) use `streamSSE()` with the session's access token. +* **Reading again after a change** the server just made: `refetchFresh(qc, key)` (`components/chat/data.ts`), not + `invalidateQueries` alone. A query's first read still on its way was asked before the change, and invalidating waits + for that read rather than making a new one, so a new conversation showed its question without the answer just saved. * Media links in API responses are signed relative URLs; the proxy serves them from the app's origin, so they work in `