Skip to content

feat(platform): @-mention knowledge-base documents in chat - #1864

Merged
yannickmonney merged 1 commit into
mainfrom
feat/chat-kb-mentions
Jun 11, 2026
Merged

yannickmonney merged 1 commit into
mainfrom
feat/chat-kb-mentions

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Closes #1337

What / why

There was no way to reference a specific knowledge-base document directly in chat — KB grounding was automatic-only (agent-scoped RAG) and the only manual path was attach-upload, which re-uploads and re-indexes a blob that is already in the knowledge base. This adds a Cursor-style @-mention picker to the composer that pins specific indexed documents to a single turn.

Flow: @ in the composer → anchored picker over RAG-indexed, team-accessible documents (title search) → selection adds a chip + inserts @Title prose → on send the client passes referencedDocumentIds (cap 5) → chatWithAgentTurn re-validates each reference synchronously (same org, active, team access, blob-backed, ragStatus === 'completed'; one opaque KB_REF_INVALID error for all failure modes) → the resolved refs are appended to the saved user message as the existing enriched *(fileId: …)* marker block and the turn's auto-RAG query is scoped to exactly the pinned fileIds.

Key pieces:

  • convex/lib/search/strategies/documents.ts — documents title-search strategy on the shared runEntitySearch seam (scan engine; flips to search-index with zero call-site changes when the bootstrap lands).
  • convex/documents/search_documents_for_mention.ts + public query searchDocumentsForMention — bounded scan (≤5 slices × 50 rows), post-filtered to team-accessible + RAG-indexed docs, slim rows.
  • chatWithAgentTurn → runChatTurnGeneration → startChat → startAgentChat: referencedFiles threading; kb_reference_block.ts emits the marker block (byte-compatible with ENRICHED_ATTACHMENT_MARKER, locked by a round-trip test against the client regexes), so sent-bubble chips and the rag_search tool's "prioritize fileIds found in the message" behavior work with zero new display plumbing.
  • generate_response.ts: pinnedFileIds forces knowledge-context injection, replaces the agent scope for the turn, and relaxes the similarity threshold to 0.3.
  • Composer: useKbMentions (chips state + caret-based (^|\s)@(\S*)$ trigger detection), KbMentionPopover (listbox, full keyboard nav via chat-input.tsx#handleKeyDown, aria-activedescendant combobox pattern, IME-guarded), send-failure rollback mirroring the video-link chip pattern.

No services/rag changes — the existing /api/v1/search file_ids filter does the scoping.

Decision-gate recommendations baked in (veto at review)

  • Pinned-turn retrieval scope: REPLACE the agent scope with the pinned fileIds (focused retrieval), not union.
  • Explicit mention forces knowledge-context injection even when the agent's knowledgeMode is off/tool (explicit user intent overrides admin agent config).
  • Similarity threshold relaxed to 0.3 for pinned queries (rag_search tool default; 0.51 returns nothing for "summarize @doc" phrasing).
  • Chips are the source of truth — deleting the @Title prose does not drop the pin; remove the chip instead.
  • Arena mode: @ trigger hidden in v1 (referencedDocumentIds not wired through arena_chat).
  • Picker is indexed-only (no greyed-out non-indexed rows in v1).

Merge-order note (#662 / #1517)

This PR makes no changes to agent_tools/rag/rag_search_tool.ts or to the RAG service — the tool benefit comes for free from the marker block in the message text. #662/#1517 are changing that tool's parameter surface in parallel; per the plan's merge order, #1337 rebases last if anything overlaps (expected overlap: none).

Deviations from the plan

  • KbMentionPopover is purely presentational; the SearchSource hook is consumed in chat-input.tsx (which owns keyboard nav state) instead of inside the popover — keeps Up/Down/Enter/Escape interception and the highlighted index in one place.
  • The mention helper reads fileMetadata directly (single pass also yields fileName/contentType/size) instead of getDocumentRagProjectionBatch; semantics identical (ragStatus === 'completed').
  • Error toast key is chat.toast.kbRefInvalid (the chat.errors.* namespace the plan implied doesn't exist).

Verification

Performed:

  • bun run check at repo root — green (format, lint incl. type-aware oxlint, typecheck, 366 server/pii test files).
  • Platform test:ui full suite — 283 files / 2216 tests green (includes the new use-kb-mentions, kb-mention-popover a11y, and marker round-trip tests).
  • New tests: search_documents_for_mention.test.ts (access/indexed-only filtering, title fallback), use-kb-mentions.test.ts (trigger detection incl. email/mid-word non-triggers, cap/dedupe/rollback), start_agent_chat.test.ts (marker block on saved message, pinnedFileIds in generation args, prewarm drops refs), KB-block round-trip lock in use-message-processing.test.ts against the client regexes.
  • bun run --filter @tale/docs lint && test — green (locale/structure parity for the new docs section).

Manual verification required (needs the running local stack with a live RAG deployment — not performed here):

  • Mention an indexed doc → chip renders pre/post send; the agent answer cites the pinned doc (citations flow through knowledgeContextResult.citations).
  • Agent with knowledgeMode: 'off' still injects pinned context.
  • Doc outside the agent's scope but user-accessible works; non-indexed docs absent from the picker.
  • Arena mode shows no trigger; IME composition unaffected (existing isComposingRef guards).

Pre-PR checklist

  • Ran bun run check (format, lint, typecheck, all tests).
  • No hand-rolled skeletons or magic h-[…] on skeletons — N/A (no loading states added).
  • Updated services/platform/messages/{en,de,fr}.json (composer.kbMention.*, chat.toast.kbRefInvalid; de-CH carries no override).
  • Updated /docs/{en,de,fr}/platform/chat/attachments.md ("Referencing knowledge documents with @" section).
  • Touched docs page has a real opening and closing (pre-existing; section added mid-page).
  • Ran bun run --filter @tale/docs lint and bun run --filter @tale/docs test.
  • README updates — N/A.

Typing @ in the chat composer now opens a picker over RAG-indexed,
team-accessible knowledge documents. Selected documents render as chips
(the source of truth for the pin), travel as referencedDocumentIds on
chatWithAgentTurn, are re-validated server-side (org, team access,
active, indexed), and are appended to the saved message as the existing
enriched fileId marker block so sent-bubble chips and the rag_search
tool's fileId-priority behavior work unchanged. The per-turn auto-RAG
query is forced on for pinned turns (explicit user intent overrides the
agent's knowledgeMode), scoped to exactly the pinned fileIds, and run
with a relaxed similarity threshold (0.3, the rag_search tool default).

Backed by a new documents title-search strategy on the shared
runEntitySearch seam (no services/rag changes — the existing /search
file_ids filter does the scoping). Arena mode hides the trigger in v1.
Failed sends restore the chips, mirroring the video-link rollback.
@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@yannickmonney, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 27 minutes and 32 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 24bd4b0b-fbce-47d2-9fd9-bd2a59a4ccba

📥 Commits

Reviewing files that changed from the base of the PR and between 95ea179 and 8cdd90e.

📒 Files selected for processing (29)
  • docs/de/platform/chat/attachments.md
  • docs/en/platform/chat/attachments.md
  • docs/fr/platform/chat/attachments.md
  • services/platform/app/features/chat/components/chat-input.tsx
  • services/platform/app/features/chat/components/chat-interface.tsx
  • services/platform/app/features/chat/components/documents-mention-source.ts
  • services/platform/app/features/chat/components/kb-mention-popover.test.tsx
  • services/platform/app/features/chat/components/kb-mention-popover.tsx
  • services/platform/app/features/chat/hooks/use-kb-mentions.test.ts
  • services/platform/app/features/chat/hooks/use-kb-mentions.ts
  • services/platform/app/features/chat/hooks/use-message-processing.test.ts
  • services/platform/app/features/chat/hooks/use-send-message.ts
  • services/platform/convex/agents/chat_turn.ts
  • services/platform/convex/agents/chat_turn_generate.ts
  • services/platform/convex/agents/start_chat.ts
  • services/platform/convex/documents/queries.ts
  • services/platform/convex/documents/search_documents_for_mention.test.ts
  • services/platform/convex/documents/search_documents_for_mention.ts
  • services/platform/convex/lib/agent_chat/internal_actions.ts
  • services/platform/convex/lib/agent_chat/kb_reference_block.ts
  • services/platform/convex/lib/agent_chat/start_agent_chat.test.ts
  • services/platform/convex/lib/agent_chat/start_agent_chat.ts
  • services/platform/convex/lib/agent_response/generate_response.ts
  • services/platform/convex/lib/agent_response/types.ts
  • services/platform/convex/lib/search/index.ts
  • services/platform/convex/lib/search/strategies/documents.ts
  • services/platform/messages/de.json
  • services/platform/messages/en.json
  • services/platform/messages/fr.json
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/chat-kb-mentions

Warning

Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@yannickmonney
yannickmonney merged commit b414cfa into main Jun 11, 2026
29 checks passed
@yannickmonney
yannickmonney deleted the feat/chat-kb-mentions branch June 11, 2026 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Direct knowledge base file referencing in chat (@mention or inline picker)

1 participant