Skip to content

feat(prompts): Query Fan-out intent column — classifyPromptIntent + on-demand cache (closes #333) - #353

Merged
gkhngyk merged 2 commits into
mainfrom
feature/fanout-intent
Jul 6, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
feature/fanout-intent

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds the Intent column to the Query Fan-out tab — the final piece of #333 after the core tab shipped in #342. Each observed sub-query is labelled with the same 7-value taxonomy as the Insights "intent" column, derived on-demand and cached.

Server

  • intent-extraction.js — classifyPromptIntent(text, aiModel): intent-only (no keyword extraction, no volume work — a cheaper call than extractIntentKeywords), reusing the exact enum from intentKeywordSchema so the two intent columns can't drift.
  • Migration 00022 — fanout_query_intents: a brand-independent cache keyed by the normalized (trimmed, whitespace-collapsed, lower-cased) sub-query. The intent of a string is the same for every brand, so one classification is reused everywhere. RLS enabled (server/service-role only; not client-exposed). Applied to the hosted DB; schema.sql regenerated.
  • POST /api/prompts/fanout-intents: normalize + dedup the queries, read the cache, and classify only the misses (bounded concurrency 6, batch cap 200), then upsert. On-demand at read time, never during tracking ingest — controls LLM cost.

Web

  • config/intent-labels.ts (new) — single source for INTENT_LABELS / INTENT_COLORS, now shared by the Insights Prompt Volumes table and the fan-out tab (removed the duplicated local copy in the prompts page).
  • actions/fanout.ts — classifyFanoutIntents(queries) → the server route (session-token fetch, mirrors the volumes flow).
  • _fanout-tab.tsx — Intent column between Sourced prompts and Track. Intents load on-demand and non-blocking: the table paints immediately and the badges fill in as the (cached) classifications resolve; a — shows until then.

Verified live

Applied the migration, then the cache populated with real sub-queries from tracked brands — all classified into correct, valid-enum intents, e.g.:

  • "enterprise vs open source aeo tools cost comparison" → comparison
  • "transition from traditional seo to generative engine optimization" → how-to
  • "top github repositories open source aeo projects" → best-top
  • "benefits of open source aeo for startups" → what-is

Related issue

Closes #333. Depends on #332/#341 (the search_queries capture) and #342 (the core tab), both merged. The explicit non-goals from #333 stay out: no Google volume/competition, no competitor/citation coverage, no co-occurrence badge, no LLM-generated fan-out.

Type of change

  • feat — New feature

How to test

  1. From server/: npm run lint, npm run format:check, npm run test (99) pass. From web/: yarn typecheck, yarn lint, yarn format:check, yarn test pass.
  2. /dashboard/prompts → Query Fan-out: each row gets an Intent badge (Comparison / How-to / What is / Best / Top / …); it appears shortly after the table (on-demand), then is instant on later loads (cached).
  3. The Insights Prompt Volumes intent column is unchanged (now sourced from the shared intent-labels config).
  4. No LLM call fires for a sub-query already in the cache.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint / npm run lint pass
  • yarn typecheck passes (from web/)
  • Migration applied + schema.sql regenerated
  • Changes are focused — one concern per PR

…che)

Adds the intent column to the Query Fan-out tab (#333) — the last piece of
the feature after the core tab (#342). Each observed sub-query is labelled
with the same 7-value taxonomy as the Insights "intent" column.

Server:
- intent-extraction.js: classifyPromptIntent(text, aiModel) — intent-only
  (no keywords, no volume work), reusing the exact enum from
  intentKeywordSchema so the two intent columns can't drift.
- migration 00022: fanout_query_intents — a brand-independent cache keyed by
  the normalized (lower-cased) sub-query, RLS enabled (server-only). Intent
  of a string is the same for every brand, so one classification is reused.
- POST /api/prompts/fanout-intents: normalize + dedup, read the cache, and
  classify only the misses (bounded concurrency 6, batch cap 200), then
  upsert. On-demand at read time, never during tracking ingest.

Web:
- config/intent-labels.ts: single source for INTENT_LABELS/COLORS, now
  shared by the Insights Prompt Volumes table and the fan-out tab.
- actions/fanout.ts: classifyFanoutIntents(queries) → the server route.
- _fanout-tab.tsx: Intent column between Sourced prompts and Track; intents
  load on-demand and non-blocking (the table paints first, badges fill in).

Closes #333. Verified live: the route classifies real sub-queries into
correct intents (comparison / how-to / what-is / best-top / …) and caches
them.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds intent classification to the Query Fan-out tab by introducing a server-side, brand-independent intent cache and a lightweight intent-only classifier, then wiring the web UI to fetch and render intent badges on-demand using a shared label/color config (aligned with Insights intent taxonomy).

Changes:

  • Server: add classifyPromptIntent (intent-only) and POST /api/prompts/fanout-intents that normalizes/dedups queries, reads/writes the fanout_query_intents cache, and classifies cache misses with bounded concurrency.
  • DB: add migration 00022 creating fanout_query_intents (RLS enabled, no policies) and regenerate supabase/schema.sql.
  • Web: add shared intent-labels config, add a server action to call the new endpoint, and render a new non-blocking Intent column in the fan-out table.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
web/src/lib/actions/fanout.ts Adds classifyFanoutIntents() server action to call the new server endpoint with the user session token.
web/src/config/intent-labels.ts New shared source of intent labels + badge colors for both Insights and Fan-out.
web/src/app/[locale]/(dashboard)/dashboard/prompts/page.tsx Switches Insights intent label/color usage to the shared config.
web/src/app/[locale]/(dashboard)/dashboard/prompts/_fanout-tab.tsx Adds Intent column and async, on-demand intent fetching + badge rendering.
supabase/migrations/00022_fanout_query_intents.sql Creates the global intent cache table with RLS enabled.
supabase/schema.sql Regenerated schema including the new cache table.
server/src/routes/prompts.js Adds POST /api/prompts/fanout-intents cache-read + classify-misses + upsert flow.
server/src/lib/intent-extraction.js Adds intent-only schema/system prompt and classifyPromptIntent().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/src/routes/prompts.js Outdated
Comment on lines +546 to +553
// 1. Read the cache.
const { data: cached } = await supabaseAdmin
.from('fanout_query_intents')
.select('query, intent')
.in('query', queries);
const intents = {};
for (const row of cached ?? []) intents[row.query] = row.intent;

Comment on lines +9 to +27
export const INTENT_LABELS: Record<string, string> = {
comparison: 'Comparison',
'how-to': 'How-to',
'what-is': 'What is',
'best-top': 'Best / Top',
'vs-review': 'vs. / Review',
recommendation: 'Recommendation',
'problem-solving': 'Problem Solving',
};

export const INTENT_COLORS: Record<string, string> = {
comparison: 'border-indigo-500/30 bg-indigo-500/10 text-indigo-600 dark:text-indigo-400',
'how-to': 'border-violet-500/30 bg-violet-500/10 text-violet-600 dark:text-violet-400',
'what-is': 'border-purple-400/30 bg-purple-400/10 text-purple-600 dark:text-purple-400',
'best-top': 'border-blue-400/30 bg-blue-400/10 text-blue-600 dark:text-blue-400',
'vs-review': 'border-cyan-500/30 bg-cyan-500/10 text-cyan-600 dark:text-cyan-400',
recommendation: 'border-emerald-500/30 bg-emerald-500/10 text-emerald-600 dark:text-emerald-400',
'problem-solving': 'border-amber-500/30 bg-amber-500/10 text-amber-600 dark:text-amber-400',
};
…pilot review)

- /api/prompts/fanout-intents read the cache with a single `.in('query', ...)`
  over up to 200 text values, which can exceed PostgREST's URL length limit,
  and it swallowed the Supabase error (a failed read silently classified
  everything via the LLM). Read in bounded chunks of 40 and throw on error.
- The intent enum includes `other`, which the endpoint can return, but the
  shared INTENT_LABELS/COLORS omitted it — the UI rendered a raw, unstyled
  "other". Add an "Other" label + a neutral slate badge color (also tidies
  the Insights intent column for the same value).

Addresses the Copilot review on #353.
@gkhngyk
gkhngyk merged commit e01990d into main Jul 6, 2026
3 checks passed
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(prompts): Query Fan-out tab — observed engine sub-queries with times-searched + one-click track

2 participants