Skip to content

fix(chat): read the Topics table, Query Fan-outs, a competitor head to head, Overview's map and Opportunities, AI bots and Search Console pages - #2466

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/chat-visibility-extra-reads
Nov 17, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/chat-visibility-extra-reads

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Step 6c of the owner's plan "Chat must read everything a page shows".

Chat opened on Prompts, Competitors, Visibility or AI Traffic can now read the tables those pages show that no read gave it. Before this, Chat had no read for the Topics table, Query Fan-outs outside a skill, a competitor's head to head, the Overview's heat map and Opportunities card, AI bots, or Search Console pages. So it answered "I couldn't read it".

Seven new reads, in a new registry file. Each one calls the page's own function or database function. Each reads daily rollups or runs one aggregate in the database. None reads tracked answers (prompt_results) row by row.

Citations › Gaps is not in this PR. Its database functions refuse Chat's service-role client, so the read needs a migration (see the last section).

Changes

Read Page / tab it matches What it returns Data source Gates
get_topics_overview Prompts › Topics Per topic: AI Visibility Score, visible and active prompts, change, mentions, citations, share of voice, top competitor, last run, daily trend. The tab's cards (best, weakest, biggest gain). Highest score first, 50 a page. getTopicsOverview → topics_overview_window (rollups, 00536) Brand access. No plan gate, as on the tab.
get_query_fanouts Prompts › Query Fan-outs, for a plain question (no skill) view queries: sub-queries, most searched first, with engines, answers, prompts, tracked. prompts: the By prompt view. gaps: sub-queries whose answers never named the brand. 7 days by default, 30 at most, 50 a page. One or two aggregates in the database: brand_prompt_fanout_queries, report_query_fanout(_engines), ae_fanout_gaps Brand access. Every engine counts, as on the tab.
get_competitor_head_to_head Competitors › head to head (Gaps and Strengths tabs) and the timeline Both averages, by platform. Each prompt with both scores and the difference: side losing (biggest gap first), winning or all. Weekly mentions and the competitor's page events. getHeadToHeadComparison → head_to_head_daily (rollups, 00169); getCompetitorTimeline (daily rollups) Brand access. The competitor by id, name or domain, among the brand's own; not_found otherwise.
get_topics_vs_competitors Visibility › Overview: Topics vs competitors, trend markers Per topic: the brand's and every competitor's score, the leader and the gap, in the map's order. Days prompts were added, and the AI visibility goal. insights_topic_competitors_daily (rollups), insights_trend_markers, kpi_definitions Brand access. The plan's platforms (model).
get_visibility_opportunities Visibility › Overview: Opportunities card The card's items in its order (fit × demand × gap), with monthly AI searches and who leads the topic. Stored prompt_suggestions and the same rollup as the map Brand access. The plan's platforms.
get_ai_bots AI Referral Traffic › AI bots Requests by type and by bot, the 50 most read pages, cited pages no search bot read. State: data, waiting or not connected. getTrafficBots → ai_bot_report (daily bot rollups) Brand access. Locked on Starter, as on the page. A Growth trial sees it.
get_gsc_pages AI Referral Traffic › Is AI taking Google clicks? Pages whose impressions rose while click-through fell, the 50 that lost most clicks first. getTrafficGooglePages → ai_traffic_google_pages (Search Console daily figures) Brand access. "Not connected" when Search Console isn't.

Other changes:

  • getTopicsOverview, getCompetitorTimeline, getTrafficBots and getTrafficGooglePages read through dbClient() instead of createClient(). The page reads exactly as before. Chat runs them with the service role after brandAllowed, as it already does for other loaders.
  • sourcesOf in google-reads.ts is exported (the same one-line change feat(chat): customers and revenue from AI visits, with the setup each answer needs #2451 makes).
  • scope.ts: the fan-out read opens on the tab's 7 days when no range is set.
  • The skill-only fan-out refusal now points to get_query_fanouts.
  • Inputs use the names other reads use (brand_id, date_from, date_to, model, region, competitor_id, days), so fix(chat): read over the page's dates and filters in the side panel #2459's page filters fill them once it lands.
  • Labels in en.json; one Chat Help section; approved-articles.json regenerated.

Safety

  • Every read starts with brandAllowed. A brand of another workspace, or one not shared with the person, reads as not found. Nothing else of it is read and no database function runs.
  • Every table read filters by the brand. Prompt reads join prompt_sets on the brand. A competitor id of another brand is not_found.
  • No new read touches prompt_results. A test runs every read and checks.
  • Outside text (prompt and sub-query text, domains, page addresses, suggestion text) is untrusted: none of these reads is on the trusted list.
  • No migration, no new dependency, no lockfile change. No new server action. No 'use server' export changed.
  • No change to chat-thread-view.tsx, page-pane.tsx, page-open.ts, the content editor or Action Center site-type files.

How to test

  1. Open Prompts › Topics with the Chat panel. Ask "Which topic is weakest, and who leads it?" Chat names the topic and its top competitor, as the table shows.
  2. On the same tab, ask "Konularımızın görünürlüğü nasıl? Hangisi en çok yükseldi?" The answer is in Turkish and names the topic with the biggest gain.
  3. Open Prompts › Query Fan-outs. Ask "Which sub-queries do AI engines search most for us?" (no skill). Chat lists them with how often each was searched. It doesn't say it can't read fan-outs.
  4. Ask "Hangi alt sorgularda markamız hiç geçmiyor?" Chat reads the gaps view and lists sub-queries.
  5. Open Competitors. Ask "Acme'ye karşı hangi promptlarda kaybediyoruz?" (use a competitor of the brand). Chat names the prompts where the competitor is ahead, biggest gap first, with both scores.
  6. Ask about a name that isn't a competitor, such as "Globex'e karşı durumumuz ne?". Chat says plainly that the brand has no such competitor and offers no other.
  7. Open Visibility › Overview. Ask "Which topics do competitors lead?" and "What should we track next?" Chat answers from the Topics vs competitors map and the Opportunities card.
  8. Open AI Referral Traffic. Ask "Which AI bots read our site this week?" and "Is AI taking our Google clicks?" On Starter, Chat says the bot report needs Growth. Without Search Console, it says it isn't connected.
  9. As a Selected-brands member, ask about a brand not shared with you. Chat doesn't read it.

Checks

  • npx tsc --noEmit -p .: clean.
  • Vitest on src/lib/chat, src/lib/mcp, src/components/chat, the Prompts, Visibility (Overview, Mentions, Citations), Citation URL, Competitors and Traffic pages, src/lib/help, src/i18n, use-server-exports, subscription-guard-coverage, the topic, traffic and competitor-timeline tests and src/app/api/traffic: 392 files, 5,119 passed, 1 skipped.
  • New tests: 12 on the reads (Topics, head to head, the map, Opportunities, AI bots and Search Console pages each equal the page's own read on the same fixture; fan-outs on their aggregates; scope; plan gates; paging; no prompt_results), 9 on the registry rows (inputs, scope, plan, brand access), 7 brand-scope.test.ts rows, and a scripted eval scenario (row 38) with 2 harness tests: on Competitors, "Acme CRM'e karşı hangi promptlarda kaybediyoruz?" must read the head-to-head and name the prompts.
  • ESLint and Prettier clean on the changed code.
  • git merge-tree --write-tree HEAD upstream/main: clean. Also clean against feat(chat): customers and revenue from AI visits, with the setup each answer needs #2451 and fix(chat): read over the page's dates and filters in the side panel #2459. With fix(chat): read Today, your agents and their runs, History over a range, and the Signals summary #2465 (Today), both add eval scenario "row 38": whichever lands second renumbers its row when main is merged in.

Not in this PR / Needs a migration

  • Citations › Gaps (get_citation_gaps): needs a migration. citation_gap_domains(_daily) and citation_competitor_sources(_daily) refuse the service role (00426). Through them, Chat would read "no gaps" for every brand. Reading their rollup tables directly instead could mean tens of thousands of rows a month for a large brand, summed in the app. The proposed SQL lets the service role through the four functions, as 00434 and 00435 did for the others. It is schema only. Once it is applied, get_citation_gaps is asService(getCitationGaps), with getCitationGaps on dbClient(). The maintainer gives the migration number; it can go together with the citations aggregate migration fix(chat): rank and count as the pages do #2462 names.
  • Citation URL page (audit row 11): no new read. fix(chat): look up any listed item by its code, id, words or address #2460's list_citations urls lookup already returns the prompts whose answers cite a page, with how many answers each. Positions per answer are not in it.
  • Fan-outs outside a skill: the By prompt view has each prompt's sub-queries but not its coverage (runs with fan-out out of all runs). The engine strip has no "last fan-out" date. Both need the per-answer read, which get_fanouts keeps inside skills.
  • AI traffic visit log, conversions and AI pipeline: in feat(chat): customers and revenue from AI visits, with the setup each answer needs #2451.

Choices made

  • Fan-outs count every engine, as the tab does. The skill-only get_fanouts / get_fanout_gaps keep to the plan's platforms, as before.
  • Topics vs competitors gives every live competitor a column. The page shows the trend's pinned or strongest four; Chat can answer about any of the brand's competitors on a topic.

Generated by Claude Code

…o head, Overview's map and Opportunities, AI bots and Search Console pages

Chat opened on Prompts, Competitors, Visibility or AI Traffic now reads
what those pages show that no read gave it (step 6c of "Chat must read
everything a page shows"). Seven reads in a new registry file, each on the
page's own function or database function, after brandAllowed, from daily
rollups or one aggregate in the database; none reads the tracked answers:

- get_topics_overview: Prompts › Topics, getTopicsOverview
  (topics_overview_window), highest AI Visibility Score first, paged.
- get_query_fanouts: Query Fan-outs for a plain question, outside a skill:
  sub-queries, the By prompt view and gaps, 7 days by default, 30 at most.
- get_competitor_head_to_head: getHeadToHeadComparison and the competitor
  timeline; the competitor by id, name or domain, not_found otherwise.
- get_topics_vs_competitors and get_visibility_opportunities: the
  Overview's heat map, trend markers and Opportunities card.
- get_ai_bots and get_gsc_pages: getTrafficBots and getTrafficGooglePages,
  the bot report locked on Starter as on the page.

The page loaders these reuse read through dbClient() so Chat runs them with
the service role. Citations › Gaps needs a migration (its functions refuse
the service role) and is left out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Cost note: getQueryFanoutsFor (queries view) runs three raw aggregates over prompt_results (brand_prompt_fanout_queries with p_min_answers 1 and an unlimited jsonb_agg, report_query_fanout, report_query_fanout_engines) even to show one page of 50; consider p_min_answers 2 or a limit, taking times_searched from report_query_fanout, or marking it costlier.

@gkhngyk
gkhngyk merged commit 95aa9c4 into ansvisor:main Nov 17, 2026
13 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 18, 2026
…erge

The merged base runs #2462's page periods over the reads #2464 and #2466
add: Prompts › Tracked's locations, the Topics tab, and AI Referral
Traffic's bots and Search Console pages cut a preset as their pages do (the
last N × 24 hours to today), and the eval set now has rows 38 (Today's first
action) and 39 (losses to a competitor). Their tests say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
gkhngyk pushed a commit that referenced this pull request Nov 18, 2026
…vice role through (00541)

Chat's reads run with the service role after brandAllowed, but the citation
functions (00426) refuse it: can_access_brand answers no without a person, so
they return nothing (the Gaps list empty, its totals at zero). #2466 left
Citations › Gaps out for this reason.

- 00541 (number provisional): citation_gap_domains(_daily),
  citation_competitor_sources(_daily), citations_window_stats,
  citations_domains, citations_urls, citations_urls_daily,
  citation_url_candidates and citation_url_occurrences let the service role
  through, as 00434 and 00435 do. Each is its 00426 definition with only the
  check changed; a person still needs can_access_brand. Functions and
  comments only, no data touched; schema.sql regenerated.
- get_citation_gaps: the Gaps tab's own read (getCitationGaps, now on
  dbClient()) under asService after brandAllowed: the Gap list strongest
  first, or one competitor's sources (By competitor), 50 a page; the
  person's range as whole UTC days, all time with none; a topic or
  competitor of another brand is not found.
- brand-access-functions.test.ts checks each 00541 definition against 00426.
- Chat Help: ask which sites cite your competitors but not you.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
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.

3 participants