feat(chat): name a new chat in parallel with its first answer

Merged
RA
2d ago
+434-155

Description

A new chat read "New Chat" in the sidebar for the whole first answer. The web client asked the server for a name only after the answer finished (PUT /chat/rename-chat-session with no name), and each client (web, mobile) triggered naming itself. Nothing on the server made naming wait for the answer.

The server now names a new chat in parallel with its first answer:

  • Which chats: prepare_chat_turn sets ChatTurnSetup.auto_name for a chat that keeps content (not a content-free incognito chat), has no name yet, and gets its first user message. That is the same rule the web client used.
  • When: start_chat_turn starts naming on a background thread (start_thread_with_context) right after it sends the response IDs, while the answer streams. A naming failure is logged and never affects the turn.
  • How: chat/session_naming.py:name_new_chat_session names the chat from its first question, with the same model choice, rate and cost limits, tracing and fallback name as before. The generation code moved out of chat_backend.py into this module, and the rename endpoint uses it too.
  • Rename wins: db/chat.py:save_chat_session_name_if_unnamed saves the name only while the chat still has none, so a rename made during generation is kept.
  • Delivery: the server sends a chat_session_name line (ChatSessionNameInfo) on the turn's stream. The web client refreshes the sidebar from it. If the stream ended first, the name is already saved and shows on the next sidebar fetch.
  • Fallback: if the stream ends with no name line, the web client calls PUT /chat/rename-chat-session as before. That endpoint now returns a saved name instead of generating a second one, so this call, and mobile's existing call, never pay for a second naming.

Behaviour change: the name now comes from the first question only, not the question and its answer. A very short opener ("hi") can get a weaker name. The fallback name still applies when generation fails.

Changes

https://github.com/user-attachments/assets/93d73b58-d180-4b83-9f49-2eeb83ebf6c6

Docs

  • Customer-facing change: I updated documentation/ for the page's reader and task, at the level of detail of the whole page.
  • No docs change needed: the existing docs stay correct (internal, tests, CI, multi-tenant cloud only, or the UI explains it).

Additional Options

  • [Optional] Please cherry-pick this PR to the latest release version.

Summary by cubic

Names a new chat in the sidebar while its first answer still streams, so a new chat no longer reads "New Chat" for the whole answer.

The server starts naming on a background thread when a turn begins for a chat that keeps content, has no name, and gets its first user message. The name comes from the first question and is saved only while the chat is still unnamed, so a rename made during generation wins. The name is sent on the answer's stream, and web clients opt in to the line via accepts_chat_session_name to refresh the sidebar from it; the widget, which does not opt in, is unaffected. If the stream ends first, the client calls PUT /rename-chat-session as before, which now returns the already-saved name instead of generating a second one, so mobile's existing call never pays for a second naming either. A per-chat cache lock joins the turn's naming and the rename endpoint so both never generate in parallel, and a cache outage while taking or releasing the lock no longer fails a rename: naming proceeds without the lock, and the conditional save still protects renames. The naming logic moved from chat_backend.py into chat/session_naming.py, sharing the same model choice, limits, tracing, and fallback.

Behavior changes

  • The name now comes from the first question only, not the question and its answer; a very short opener can get a weaker name. The fallback name still applies when generation fails.
  • A rename made while the name is being generated wins, because the server saves only while the chat is still unnamed.

Summary by CodeRabbit

  • New Features
    • New chats are automatically named from the first question while the response is streaming. When available, the name is sent in the stream, and the chat list and projects refresh to show it.
    • Chat clients can opt in to receiving the new chat name in the response stream.
  • Bug Fixes
    • Existing chat names are preserved during automatic naming. Naming failures no longer interrupt the response, and chats that do not persist content are not automatically named.