From 857f1bb27941a16f90f816e7d322fcf9df9e705f Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Tue, 23 Jun 2026 17:04:32 +0200 Subject: [PATCH] feat(chat): inject referenced-chat context into the new-chat agent input Thread mentioned_thread_ids from the route through the orchestrator into input-state assembly, resolve them for the requesting user, and append the rendered referenced-chat block to the agent's query context. --- .../app/routes/new_chat_routes.py | 2 ++ .../streaming/flows/new_chat/input_state.py | 28 +++++++++++++++++-- .../streaming/flows/new_chat/orchestrator.py | 3 ++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/surfsense_backend/app/routes/new_chat_routes.py b/surfsense_backend/app/routes/new_chat_routes.py index c850c7eed..951682e47 100644 --- a/surfsense_backend/app/routes/new_chat_routes.py +++ b/surfsense_backend/app/routes/new_chat_routes.py @@ -1800,6 +1800,7 @@ async def handle_new_chat( mentioned_connector_ids=request.mentioned_connector_ids, mentioned_connectors=mentioned_connectors_payload, mentioned_documents=mentioned_documents_payload, + mentioned_thread_ids=request.mentioned_thread_ids, needs_history_bootstrap=thread.needs_history_bootstrap, thread_visibility=thread.visibility, current_user_display_name=user.display_name or "A team member", @@ -2296,6 +2297,7 @@ async def regenerate_response( mentioned_connector_ids=request.mentioned_connector_ids, mentioned_connectors=mentioned_connectors_payload, mentioned_documents=mentioned_documents_payload, + mentioned_thread_ids=request.mentioned_thread_ids, checkpoint_id=target_checkpoint_id, needs_history_bootstrap=thread.needs_history_bootstrap, thread_visibility=thread.visibility, diff --git a/surfsense_backend/app/tasks/chat/streaming/flows/new_chat/input_state.py b/surfsense_backend/app/tasks/chat/streaming/flows/new_chat/input_state.py index 064843aba..7be84c992 100644 --- a/surfsense_backend/app/tasks/chat/streaming/flows/new_chat/input_state.py +++ b/surfsense_backend/app/tasks/chat/streaming/flows/new_chat/input_state.py @@ -33,6 +33,10 @@ from app.agents.chat.runtime.mention_resolver import ( resolve_mentions, substitute_in_text, ) +from app.agents.chat.runtime.referenced_chat_context import ( + render_referenced_chats_block, + resolve_referenced_chats, +) from app.db import ( ChatVisibility, NewChatThread, @@ -67,6 +71,8 @@ async def build_new_chat_input_state( mentioned_folder_ids: list[int] | None, mentioned_connectors: list[dict[str, Any]] | None, mentioned_documents: list[dict[str, Any]] | None, + mentioned_thread_ids: list[int] | None, + requesting_user_id: str | None, needs_history_bootstrap: bool, thread_visibility: ChatVisibility, current_user_display_name: str | None, @@ -112,10 +118,22 @@ async def build_new_chat_input_state( mentioned_documents=mentioned_documents, ) + # Referenced-chat context is path-independent, so resolve it in every + # filesystem mode (unlike the doc/folder mention substitution above). + referenced_chats = await resolve_referenced_chats( + session, + search_space_id=search_space_id, + requesting_user_id=requesting_user_id, + current_chat_id=chat_id, + mentioned_thread_ids=mentioned_thread_ids, + ) + referenced_chat_context = render_referenced_chats_block(referenced_chats) + final_query = _render_query_with_context( agent_user_query=agent_user_query, mentioned_connectors=mentioned_connectors, recent_reports=recent_reports, + referenced_chat_context=referenced_chat_context, ) if thread_visibility == ChatVisibility.SEARCH_SPACE and current_user_display_name: @@ -203,10 +221,13 @@ def _render_query_with_context( agent_user_query: str, mentioned_connectors: list[dict[str, Any]] | None, recent_reports: list[Report], + referenced_chat_context: str | None = None, ) -> str: - """Prepend the ```` then ```` blocks. + """Prepend ````, ````, then + ```` blocks. - Order is load-bearing for legacy parity. + Order of connectors then reports is load-bearing for legacy parity; + referenced chats are appended last as read-only background. """ context_parts: list[str] = [] @@ -233,6 +254,9 @@ def _render_query_with_context( "" ) + if referenced_chat_context: + context_parts.append(referenced_chat_context) + if context_parts: context = "\n\n".join(context_parts) return f"{context}\n\n{agent_user_query}" diff --git a/surfsense_backend/app/tasks/chat/streaming/flows/new_chat/orchestrator.py b/surfsense_backend/app/tasks/chat/streaming/flows/new_chat/orchestrator.py index 69343ffa4..0e49af249 100644 --- a/surfsense_backend/app/tasks/chat/streaming/flows/new_chat/orchestrator.py +++ b/surfsense_backend/app/tasks/chat/streaming/flows/new_chat/orchestrator.py @@ -129,6 +129,7 @@ async def stream_new_chat( mentioned_connector_ids: list[int] | None = None, mentioned_connectors: list[dict[str, Any]] | None = None, mentioned_documents: list[dict[str, Any]] | None = None, + mentioned_thread_ids: list[int] | None = None, checkpoint_id: str | None = None, needs_history_bootstrap: bool = False, thread_visibility: ChatVisibility | None = None, @@ -433,6 +434,8 @@ async def stream_new_chat( mentioned_folder_ids=mentioned_folder_ids, mentioned_connectors=mentioned_connectors, mentioned_documents=mentioned_documents, + mentioned_thread_ids=mentioned_thread_ids, + requesting_user_id=user_id, needs_history_bootstrap=needs_history_bootstrap, thread_visibility=visibility, current_user_display_name=current_user_display_name,