refactor(chat): improve user query handling and mention chip functionality

This commit is contained in:
Anish Sarkar 2026-05-12 20:57:15 +05:30
parent c43bfdb1d9
commit 8ea042e88c
3 changed files with 107 additions and 42 deletions

View file

@ -1507,14 +1507,20 @@ async def stream_new_chat(
# Resolve @-mention chips to canonical virtual paths and rewrite
# the user-typed text so the LLM sees ``\`/documents/...\``` instead
# of bare ``@title``. The persisted user-message text keeps
# ``@title`` so chip rendering on reload is unchanged — see
# ``persistence._build_user_content``.
# of bare ``@title``. The substitution lands in ``agent_user_query``
# ONLY — the original ``user_query`` (with ``@title`` tokens) flows
# untouched into ``persist_user_turn`` below so chip rendering on
# reload still works (``UserTextPart`` → ``parseMentionSegments``
# matches ``@title``, not ``\`/documents/...\```). It also feeds
# the human-readable surfaces — SSE "Processing X" status, auto
# thread title, memory seed — which all want what the user typed.
# See ``persistence._build_user_content``.
#
# Cloud mode only: local-folder mode keeps the legacy
# ``@title`` text path; mention support there is a follow-up
# task because the path scheme (mount-rooted) and the picker
# UI both need separate work.
agent_user_query = user_query
accepted_folder_ids: list[int] = []
if fs_mode == FilesystemMode.CLOUD.value and (
mentioned_document_ids
@ -1549,11 +1555,13 @@ async def stream_new_chat(
mentioned_surfsense_doc_ids=mentioned_surfsense_doc_ids,
mentioned_folder_ids=mentioned_folder_ids,
)
user_query = substitute_in_text(user_query, resolved.token_to_path)
agent_user_query = substitute_in_text(user_query, resolved.token_to_path)
accepted_folder_ids = resolved.mentioned_folder_ids
# Format the user query with context (SurfSense docs + reports only)
final_query = user_query
# Format the user query with context (SurfSense docs + reports only).
# Uses ``agent_user_query`` so the LLM sees backtick-wrapped paths
# instead of bare ``@title`` tokens.
final_query = agent_user_query
context_parts = []
if mentioned_surfsense_docs:
@ -1584,7 +1592,7 @@ async def stream_new_chat(
if context_parts:
context = "\n\n".join(context_parts)
final_query = f"{context}\n\n<user_query>{user_query}</user_query>"
final_query = f"{context}\n\n<user_query>{agent_user_query}</user_query>"
if visibility == ChatVisibility.SEARCH_SPACE and current_user_display_name:
final_query = f"**[{current_user_display_name}]:** {final_query}"