feat(web): enhance chat context and mention handling with connector support

This commit is contained in:
Anish Sarkar 2026-05-26 21:11:53 +05:30
parent 701ae800b4
commit a41b16b73e
15 changed files with 773 additions and 449 deletions

View file

@ -137,15 +137,19 @@ def _build_user_content(
if doc_id is None or title is None or document_type is None:
continue
kind_raw = doc.get("kind", "doc")
kind = kind_raw if kind_raw in ("doc", "folder") else "doc"
normalized.append(
{
"id": doc_id,
"title": str(title),
"document_type": str(document_type),
"kind": kind,
}
)
kind = kind_raw if kind_raw in ("doc", "folder", "connector") else "doc"
item = {
"id": doc_id,
"title": str(title),
"document_type": str(document_type),
"kind": kind,
}
if kind == "connector":
connector_type = doc.get("connector_type") or document_type
account_name = doc.get("account_name") or title
item["connector_type"] = str(connector_type)
item["account_name"] = str(account_name)
normalized.append(item)
if normalized:
parts.append({"type": "mentioned-documents", "documents": normalized})
return parts

View file

@ -839,6 +839,8 @@ async def stream_new_chat(
mentioned_document_ids: list[int] | None = None,
mentioned_surfsense_doc_ids: list[int] | None = None,
mentioned_folder_ids: list[int] | None = None,
mentioned_connector_ids: list[int] | None = None,
mentioned_connectors: list[dict[str, Any]] | None = None,
mentioned_documents: list[dict[str, Any]] | None = None,
checkpoint_id: str | None = None,
needs_history_bootstrap: bool = False,
@ -1385,6 +1387,32 @@ async def stream_new_chat(
format_mentioned_surfsense_docs_as_context(mentioned_surfsense_docs)
)
if mentioned_connectors:
connector_lines = []
for connector in mentioned_connectors:
if not isinstance(connector, dict):
continue
connector_id = connector.get("id")
connector_type = connector.get("connector_type") or connector.get(
"document_type"
)
account_name = connector.get("account_name") or connector.get("title")
if connector_id is None or connector_type is None:
continue
connector_lines.append(
f' - connector_id={connector_id}, connector_type="{connector_type}", '
f'account="{account_name or ""}"'
)
if connector_lines:
context_parts.append(
"<mentioned_connectors>\n"
"The user selected these exact connector accounts with @. "
"For read, write, or HITL tool calls involving these services, "
"prefer the matching connector_id instead of guessing from available accounts:\n"
+ "\n".join(connector_lines)
+ "\n</mentioned_connectors>"
)
# Surface report IDs prominently so the LLM doesn't have to
# retrieve them from old tool responses in conversation history.
if recent_reports:
@ -1778,6 +1806,8 @@ async def stream_new_chat(
mentioned_folder_ids=list(
accepted_folder_ids or mentioned_folder_ids or []
),
mentioned_connector_ids=list(mentioned_connector_ids or []),
mentioned_connectors=list(mentioned_connectors or []),
request_id=request_id,
turn_id=stream_result.turn_id,
)