Open MercatoDocsSearch docsAPI reference

Framework

Citations

Let a reader open the customer, document or page behind a claim. A read tool can attach a citation to its result so the chat can render a source link without asking the model to invent one.

A citation tells the operator where an answer came from. Read tools emit citations for the records, documents or pages they returned; the chat renders them as a numbered Sources list under the assistant message, and the run trace keeps them so a reviewer can check the sources later.

Citations ride on the UI parts pipeline: emitCitations enqueues one ai_assistant:citations part on the tool's per-call ctx.uiParts collector, and the part travels in the tool output's uiParts envelope.

Emitting citations from a tool

import { defineAiTool, emitCitations } from '@open-mercato/ai-assistant'

export const searchHandbook = defineAiTool({
  name: 'support.search_handbook',
  description: 'Search the support handbook.',
  isMutation: false,
  requiredFeatures: ['support.view'],
  inputSchema: z.object({ q: z.string().min(1) }),
  async handler(args, ctx) {
    const hits = await searchHandbookPages(ctx, args.q)
    emitCitations(
      ctx,
      hits.map((hit) => ({
        kind: 'handbook_page',
        ref: hit.id,
        label: hit.title,
        quote: hit.snippet,
        locator: `section ${hit.section}`,
        href: `/backend/support/handbook/${hit.id}`,
      })),
      { title: 'Handbook pages' },
    )
    return { items: hits }
  },
})

emitCitations(ctx, citations, options?):

Emitting citations does not change the tool's own result; agents that ignore the part behave exactly as before. Tell the model in the prompt that sources are shown to the operator so it does not repeat them as plain-text links.

The built-in search.hybrid_search and search.get_record_context tools emit record citations for the hits they return (ref is <entityId>:<recordId>, label is the search presenter title, href is the record URL when it is safe).

attachments.get_document_sections emits one attachment citation per section it returns with text (ref is <attachmentId>#<anchorKey>, label is the section citation, locator is the number label or p. N, quote is the first 1000 characters of the section text, href is the cited-section page /backend/storage/attachments/<attachmentId>/sections/<anchorKey>). Outline calls (includeText: false) emit no citations. The customers.account_assistant and catalog.catalog_assistant agents allow this tool. The page reads the section through the scoped structure API, so the tenant, organization and attachment access are checked again when the link is opened. No client resolver is registered for attachment; the label fallback renders it.

Payload schema

FieldTypeRules
kindstringRequired. [a-z0-9_.:-]{1,64}. Selects the client resolver.
refstringRequired. 1–512 chars. Stable id of the source within its kind.
labelstringOptional, ≤ 200. Shown as the title when no resolver supplies one.
versionstringOptional, ≤ 64. Version of the cited source.
quotestringOptional, ≤ 1000. Excerpt; clamped to three lines with a "Show more" toggle.
locatorstringOptional, ≤ 200. Page, section or anchor inside the source.
hrefstringOptional, ≤ 2048. Must pass the href safety rules below or the entry is dropped.
stepsstring[]Optional, ≤ 20 entries of ≤ 64 chars. Ids a resolver can expand into steps.

The part payload is { citations: AiCitation[], title?: string }. Types: AiCitation, AiCitationsPartPayload from @open-mercato/ai-assistant; AiCitationRef from @open-mercato/ui/ai.

Href safety

Citation text may originate from tool data the model or a user influenced, so links are checked twice – on the server (isSafeHref, @open-mercato/ai-assistant) and again in the browser (isSafeCitationHref, @open-mercato/ui/ai). Both share one test matrix. An href is safe only when it is:

Everything else is rejected: javascript:, data:, vbscript:, protocol-relative //host, backslashes, whitespace and control or invisible characters anywhere in the value. On the server an unsafe href invalidates the whole entry; in the browser an unsafe href is simply not linked and the title renders as plain text.

External links open in a new tab with rel="noopener noreferrer"; same-origin paths use in-app navigation.

Resolving citations on the client

The CitationsPart renders each entry through the citation resolver registry. Without a resolver the title is label ?? ref and the link is the (safe) href. Register a resolver per kind to compute a better title, a link, or expandable steps:

'use client'
import { registerCitationResolver } from '@open-mercato/ui/ai'

registerCitationResolver('handbook_page', async (citation) => ({
  title: citation.label ?? `Handbook page ${citation.ref}`,
  href: `/backend/support/handbook/${encodeURIComponent(citation.ref)}`,
}))

Consecutive citations parts on one assistant message are merged into a single deduplicated list.

Persistence and capture level

MUST rules

See also

Previous: Attachments & Images · Next: Decision Models