Skip to content

feat(agent): add source_filter to list_citations so the assistant can isolate owned/competitor citations - #311

Merged
gkhngyk merged 1 commit into
mainfrom
feature/citations-source-filter
Jun 28, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/citations-source-filter

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

The in-product assistant couldn't isolate a brand's own cited URLs. list_citations already classifies every citation by source (you / competitor / editorial / forum / social / review / …) but exposed no parameter to filter on that classification, so top_domains / top_urls came back mixed and the model had no clean way to answer "which of our URLs did AI cite?".

Change — add a source_filter parameter to list_citations:

  • owned — only citations on the brand's own domains (category you)
  • competitor — only tracked-competitor domains
  • external — everything else (editorial / forum / social / review / institutional / other)
  • all — no filter (default, backward compatible)

The filter is applied inside listCitationsFor() against the category it already computes — no new query or classification logic. totals, source_type_breakdown, top_domains, and top_urls are recomputed over the filtered set, and results_with_citations only counts results that contribute a matching citation. The all path skips the matcher entirely and takes the original code path, so its output is byte-identical to before.

Surfaces wired (kept in parity):

  • web/src/lib/agent/tools.ts — agent tool schema + description
  • web/src/lib/mcp/server.ts — MCP list_citations tool
  • web/src/lib/mcp/data.ts — listCitationsFor() filtering + CitationSourceFilter type
  • web/src/app/api/mcp/citations/route.ts — REST parity (?source_filter=, validated)
  • web/src/lib/agent/system-prompt.ts — nudges the agent to pass source_filter: "owned" for owned-URL questions

Related issue

Closes #297

Type of change

  • feat — New feature

How to test

  1. Ask the assistant "Which of our own URLs did AI engines cite for brand X this week?" → it calls list_citations with source_filter: "owned" and the response lists only owned-domain URLs.
  2. source_filter: "competitor" returns only competitor-domain citations; "external" returns neither.
  3. Omitting source_filter (or "all") reproduces today's output exactly.
  4. A brand with no owned citations in the window returns empty top_urls instead of a generic list.
  5. REST: GET /api/mcp/citations?brand_id=…&source_filter=owned; an invalid value returns 400.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

…ternal)

The assistant could not isolate a brand's own cited URLs — list_citations
classified each citation by source but exposed no way to filter on it, so
'which of OUR URLs did AI cite?' returned a mixed report.

Add a source_filter param (all | owned | competitor | external) resolved
against the category listCitationsFor already computes, before aggregation.
totals / source_type_breakdown / top_domains / top_urls are recomputed over
the filtered set. 'all' is the default and takes the original code path, so
its output is byte-identical (no regression).

Wired through the agent tool, the MCP tool, and the REST parity route; the
system prompt nudges the agent to pass source_filter='owned' for owned-URL
questions.
@gkhngyk
gkhngyk merged commit 40197cf into main Jun 28, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the feature/citations-source-filter branch June 28, 2026 16:18
@gkhngyk gkhngyk mentioned this pull request Jul 6, 2026
5 tasks done
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.

feat(agent): add source_filter to list_citations so the assistant can isolate owned/competitor citations

1 participant