Repository navigation
feat(content): cluster prompts by intent within each topic - #925
Merged
Merged
Conversation
Content opportunities are generated per prompt, so one need asked several ways yields near-identical suggestions and most prompts get none (#857). This adds the clusters they will be generated for. Generation does not use them yet. - Migration 00101 adds prompt_clusters, prompt_cluster_members (one cluster per prompt), prompt_cluster_scopes and prompt_cluster_queries, plus replace_prompt_clusters, which swaps a scope's clusters in one statement, and brand_prompt_fanout_queries. - lib/prompt-clusters.js clusters each topic, and the prompts with no topic, with one model call. A scope is re-clustered only when its active prompts change, and a rebuilt cluster keeps its id when most of its prompts stay. - Each cluster gets a fan-out basket: queries searched in at least two answers in the last 30 days, the 100 most searched per cluster, each judged once as included or excluded with a short reason. - The tracking worker refreshes clusters before generating opportunities. A failed refresh does not block generation. - PROMPT_CLUSTER_MODEL picks the model; it defaults to DEFAULT_SUGGESTION_MODEL.
8 tasks done
gkhngyk
added a commit
that referenced
this pull request
Oct 4, 2026
Opportunities were generated per prompt, so the same need asked several ways piled up near-identical suggestions (#857). They are now generated per prompt cluster (#925), with a score that explains itself. - One generator for the nightly run and the Generate button, which carried two copies of the same logic. The button refreshes clusters first. - Each run takes the three highest-scoring clusters that have answers and no opportunity yet, and writes one opportunity per cluster. A cluster that already has one, in any status, is not suggested again. - A cluster that needs the same content as an opportunity in another topic is merged into it (related_cluster_ids) instead of getting a near-copy. - Cluster metrics come from the Insights rollups over the last 30 days: the same figures from raw answers take about 50 s on the largest brand. The competitor gap is the strongest competitor's visibility minus the brand's; the old sum grew with the number of answers. - Score components are stored with the opportunity. Demand moves to a log scale, since summed cluster volumes put most clusters at the old cap. - The brief lists the cluster's prompts as the questions to answer and its fan-out queries as related searches. - The detail page shows the basket (prompts, included and excluded searches with reasons) and the score breakdown. - Migration 00102 adds cluster_id and related_cluster_ids to content_opportunities and the brand_prompt_opportunity_metrics function. prompt_id stays, set to the cluster's highest-volume prompt, so filters, webhooks and MCP keep working.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
First of three PRs for #857 (Phase 1 of the updated plan). It builds and maintains prompt clusters and their fan-out baskets. Opportunity generation does not read them yet; that is the next PR. Nothing changes in the UI.
Clusters
prompt_cluster_scopes.replace_prompt_clustersswaps a scope's clusters in one statement, so readers never see a scope half-rebuilt.Fan-out basket
brand_prompt_fanout_queriesreturns a brand's fan-out queries per prompt in one jsonb value: queries searched in at least two answers in the window. On the largest brand this is about 5,700 rows a month and takes about 750 ms.Running it
PROMPT_CLUSTER_MODELselects the model and falls back toDEFAULT_SUGGESTION_MODEL.Tried on production data
openai/gpt-6-luna: a 9-prompt topic became 2 clusters, and an 11-prompt topic became 4.Known limitation: queries that differ only in word order count as separate queries. The next PR merges them where basket counts are shown.
Related issue
Part of #857
Type of change
feat— New featuretest— Adding or updating testsHow to test
refreshPromptClusters('<brand id>')fromserver/src/lib/prompt-clusters.jsfor a small brand, then checkprompt_clusters,prompt_cluster_membersandprompt_cluster_queries.reclustered: 0, judged: 0.prompt-clusters.test.jscovers the fingerprint, cluster normalization, id reuse and basket ranking.Checklist
yarn lintandyarn format:checkpass (run fromserver/)yarn testpasses (run fromserver/)supabase/schema.sqlrebuilt withbuild-schema.sh🤖 Generated with Claude Code