Skip to content

feat: @-mention attachments in AI chat composer (Schema / Table / Current Query / Query Results)#1060

Merged
datlechin merged 3 commits into
mainfrom
feat/chat-mentions
May 7, 2026
Merged

feat: @-mention attachments in AI chat composer (Schema / Table / Current Query / Query Results)#1060
datlechin merged 3 commits into
mainfrom
feat/chat-mentions

Conversation

@datlechin

Copy link
Copy Markdown
Member

Phase 3b.1 of #1047. Attach context to a chat message via an @ menu next to the slash menu. Items render as chips above the input and inside the user message bubble. Outgoing user turns carry the items as .attachment(ContextItem) blocks plus an expanded text section appended to the prompt.

What you can attach

Schema Full schema as a markdown section (table list with column types)
Table Single table's column list
Current Query The text of the active editor query
Query Results The recent query-results summary already cached for the auto-prompt

Saved Queries and File items exist in ContextItem (defined in #1057) but are not exposed in the picker yet — those need async fetches and are deferred.

Implementation

  • ContextItem+Display.swiftdisplayLabel, symbolName, stableKey. The chip view, picker, and the dedupe in attach(_:) all key off stableKey, so two .table(id, "Customer") references collapse to one attachment.
  • AIChatViewModel:
    • attachedContext: [ContextItem] (observable) holds the currently-staged attachments.
    • attach(_:) / detach(_:) mutate the list with stable-key dedupe.
    • composePrompt(text:attachments:) joins typed text with resolveAttachment(_:) output, separated by ---.
    • sendMessage() builds a ChatTurn with [.text(prompt)] + attachments.map { .attachment($0) }. The text block is what the AI sees via plainText; the attachment blocks are UI markers for chip rendering.
    • After send, attachedContext is cleared.
  • AIChatContextChipView / AIChatContextChipStrip — small reusable chip (icon + label + optional xmark).
  • AIChatPanelView:
    • Chip strip rendered above the TextField.
    • mentionMenu button between slashCommandMenu and the Send button. Categories show only when their data is available (Schema needs connection.id, Tables needs viewModel.tables, etc.).
  • AIChatMessageView:
    • User bubbles render attachedContextItems (extracted from .attachment blocks) as a chip strip above the message text. So scrolling history shows what was attached.

Why this design

  • The text block carries the full prompt. Providers extract text via turn.plainText. They don't need to know about attachment blocks — they see the resolved text inline. No provider migration required.
  • Attachment blocks are persistent UI markers. They serialize through ChatTurn's Codable and survive across app restarts via AIChatStorage. The chip rendering in user bubbles works on stored conversations.
  • Stable-key dedupe prevents the obvious "click Schema 5 times → 5 chips" footgun.

What's NOT in this PR

  • Typed @-detection popover (filter as you type @cus... for the Customer table). Tracked as 3b.2 follow-up.
  • Saved-query and file attachments (need async fetch hooks).
  • Removing the auto-context prompt (AISchemaContext.buildSystemPrompt) — that's Phase 3c. Currently auto-context AND @-mention attachments both reach the AI; users can rely on the auto-shovel and just use mentions for emphasis. 3c will flip the default.

Tests

AIChatViewModelMentionsTests: 6 tests covering attach idempotency, detach, attachment-block embedding on send, currentQuery resolution into the prompt, and the empty-input no-op.

Test plan

  • Open AI Chat. The @ button is to the right of the slash menu and the model picker.
  • Click @. Pick Schema. A "Schema" chip appears above the input.
  • Click @ again. Pick a Tables sub-item. Second chip appears.
  • Click the X on a chip to remove it.
  • Type "What is this?" and send. The chips disappear from the input. The user message bubble shows the chips above the text. The AI receives the prompt with expanded schema/table sections.
  • Restart the app and reopen the conversation. Chips still render in the user bubble (persisted via Codable on ChatTurn.blocks).

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@datlechin datlechin merged commit 6974cf3 into main May 7, 2026
1 check passed
@datlechin datlechin deleted the feat/chat-mentions branch May 7, 2026 04:16
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.

1 participant