mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-04 22:02:16 +02:00
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.
This commit is contained in:
parent
afc555d971
commit
857f1bb279
3 changed files with 31 additions and 2 deletions
|
|
@ -1800,6 +1800,7 @@ async def handle_new_chat(
|
||||||
mentioned_connector_ids=request.mentioned_connector_ids,
|
mentioned_connector_ids=request.mentioned_connector_ids,
|
||||||
mentioned_connectors=mentioned_connectors_payload,
|
mentioned_connectors=mentioned_connectors_payload,
|
||||||
mentioned_documents=mentioned_documents_payload,
|
mentioned_documents=mentioned_documents_payload,
|
||||||
|
mentioned_thread_ids=request.mentioned_thread_ids,
|
||||||
needs_history_bootstrap=thread.needs_history_bootstrap,
|
needs_history_bootstrap=thread.needs_history_bootstrap,
|
||||||
thread_visibility=thread.visibility,
|
thread_visibility=thread.visibility,
|
||||||
current_user_display_name=user.display_name or "A team member",
|
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_connector_ids=request.mentioned_connector_ids,
|
||||||
mentioned_connectors=mentioned_connectors_payload,
|
mentioned_connectors=mentioned_connectors_payload,
|
||||||
mentioned_documents=mentioned_documents_payload,
|
mentioned_documents=mentioned_documents_payload,
|
||||||
|
mentioned_thread_ids=request.mentioned_thread_ids,
|
||||||
checkpoint_id=target_checkpoint_id,
|
checkpoint_id=target_checkpoint_id,
|
||||||
needs_history_bootstrap=thread.needs_history_bootstrap,
|
needs_history_bootstrap=thread.needs_history_bootstrap,
|
||||||
thread_visibility=thread.visibility,
|
thread_visibility=thread.visibility,
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,10 @@ from app.agents.chat.runtime.mention_resolver import (
|
||||||
resolve_mentions,
|
resolve_mentions,
|
||||||
substitute_in_text,
|
substitute_in_text,
|
||||||
)
|
)
|
||||||
|
from app.agents.chat.runtime.referenced_chat_context import (
|
||||||
|
render_referenced_chats_block,
|
||||||
|
resolve_referenced_chats,
|
||||||
|
)
|
||||||
from app.db import (
|
from app.db import (
|
||||||
ChatVisibility,
|
ChatVisibility,
|
||||||
NewChatThread,
|
NewChatThread,
|
||||||
|
|
@ -67,6 +71,8 @@ async def build_new_chat_input_state(
|
||||||
mentioned_folder_ids: list[int] | None,
|
mentioned_folder_ids: list[int] | None,
|
||||||
mentioned_connectors: list[dict[str, Any]] | None,
|
mentioned_connectors: list[dict[str, Any]] | None,
|
||||||
mentioned_documents: 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,
|
needs_history_bootstrap: bool,
|
||||||
thread_visibility: ChatVisibility,
|
thread_visibility: ChatVisibility,
|
||||||
current_user_display_name: str | None,
|
current_user_display_name: str | None,
|
||||||
|
|
@ -112,10 +118,22 @@ async def build_new_chat_input_state(
|
||||||
mentioned_documents=mentioned_documents,
|
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(
|
final_query = _render_query_with_context(
|
||||||
agent_user_query=agent_user_query,
|
agent_user_query=agent_user_query,
|
||||||
mentioned_connectors=mentioned_connectors,
|
mentioned_connectors=mentioned_connectors,
|
||||||
recent_reports=recent_reports,
|
recent_reports=recent_reports,
|
||||||
|
referenced_chat_context=referenced_chat_context,
|
||||||
)
|
)
|
||||||
|
|
||||||
if thread_visibility == ChatVisibility.SEARCH_SPACE and current_user_display_name:
|
if thread_visibility == ChatVisibility.SEARCH_SPACE and current_user_display_name:
|
||||||
|
|
@ -203,10 +221,13 @@ def _render_query_with_context(
|
||||||
agent_user_query: str,
|
agent_user_query: str,
|
||||||
mentioned_connectors: list[dict[str, Any]] | None,
|
mentioned_connectors: list[dict[str, Any]] | None,
|
||||||
recent_reports: list[Report],
|
recent_reports: list[Report],
|
||||||
|
referenced_chat_context: str | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Prepend the ``<mentioned_connectors>`` then ``<report_context>`` blocks.
|
"""Prepend ``<mentioned_connectors>``, ``<report_context>``, then
|
||||||
|
``<referenced_chat_context>`` 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] = []
|
context_parts: list[str] = []
|
||||||
|
|
||||||
|
|
@ -233,6 +254,9 @@ def _render_query_with_context(
|
||||||
"</report_context>"
|
"</report_context>"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if referenced_chat_context:
|
||||||
|
context_parts.append(referenced_chat_context)
|
||||||
|
|
||||||
if context_parts:
|
if context_parts:
|
||||||
context = "\n\n".join(context_parts)
|
context = "\n\n".join(context_parts)
|
||||||
return f"{context}\n\n<user_query>{agent_user_query}</user_query>"
|
return f"{context}\n\n<user_query>{agent_user_query}</user_query>"
|
||||||
|
|
|
||||||
|
|
@ -129,6 +129,7 @@ async def stream_new_chat(
|
||||||
mentioned_connector_ids: list[int] | None = None,
|
mentioned_connector_ids: list[int] | None = None,
|
||||||
mentioned_connectors: list[dict[str, Any]] | None = None,
|
mentioned_connectors: list[dict[str, Any]] | None = None,
|
||||||
mentioned_documents: 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,
|
checkpoint_id: str | None = None,
|
||||||
needs_history_bootstrap: bool = False,
|
needs_history_bootstrap: bool = False,
|
||||||
thread_visibility: ChatVisibility | None = None,
|
thread_visibility: ChatVisibility | None = None,
|
||||||
|
|
@ -433,6 +434,8 @@ async def stream_new_chat(
|
||||||
mentioned_folder_ids=mentioned_folder_ids,
|
mentioned_folder_ids=mentioned_folder_ids,
|
||||||
mentioned_connectors=mentioned_connectors,
|
mentioned_connectors=mentioned_connectors,
|
||||||
mentioned_documents=mentioned_documents,
|
mentioned_documents=mentioned_documents,
|
||||||
|
mentioned_thread_ids=mentioned_thread_ids,
|
||||||
|
requesting_user_id=user_id,
|
||||||
needs_history_bootstrap=needs_history_bootstrap,
|
needs_history_bootstrap=needs_history_bootstrap,
|
||||||
thread_visibility=visibility,
|
thread_visibility=visibility,
|
||||||
current_user_display_name=current_user_display_name,
|
current_user_display_name=current_user_display_name,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue