Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
Merge remote-tracking branch 'origin/feature/insights-leaderboard' into feature/insights-trend
Cihan60d57100023d
diff --cc supabase/schema.sqlindex fb47702b7,fd4a847ca..f6be5a7a9--- a/supabase/schema.sql+++ b/supabase/schema.sql@@@ -16489,365 -16489,37 +16489,395 @@@ as $);$$;+ -- ─────────────────────────────────────────────────────────────────────────+ -- migrations/00134_starter_copilot.sql+ -- ─────────────────────────────────────────────────────────────────────────+ -- Starter tracks ChatGPT and Microsoft Copilot, no longer ChatGPT and+ -- Perplexity.+ --+ -- The plan's engine list lives in config/plans (web and server), and new or+ -- edited prompts follow it. The tracking worker, though, runs each prompt's+ -- stored `platforms`, so prompts saved under the old list would keep running+ -- Perplexity and never get Copilot. This swaps the engine on the prompts of+ -- every organization on the Starter plan, as alignPromptsToPlanForOrg does on+ -- a plan change. Past Perplexity results stay as they are.+ --+ -- Only cloud organizations are touched: a Stripe customer exists only there.+ -- Self-hosted installs run every engine, whatever `plan` holds.++ update public.prompts p+ set platforms = case+ -- Already tracking Copilot: drop Perplexity rather than list Copilot twice.+ when 'copilot-web' = any (p.platforms) then array_remove(p.platforms, 'perplexity-web')+ else array_replace(p.platforms, 'perplexity-web', 'copilot-web')+ end+ from public.prompt_sets ps+ join public.brands b on b.id = ps.brand_id+ join public.organizations o on o.id = b.organization_id+ where p.prompt_set_id = ps.id+ and o.plan = 'starter'+ and o.stripe_customer_id is not null+ and 'perplexity-web' = any (p.platforms);+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00135_insights_change_drivers.sql+-- ─────────────────────────────────────────────────────────────────────────+-- Insights "Why it changed" (VIS-009).+--+-- The v2 mockup explains the Visibility change beside the By AI platform+-- table: the platforms, topics and prompts that moved it most, each with its+-- own score before → after and its effect on the overall score, plus a line+-- for the prompts added this period ("509 new prompts · Sep 29 · own+-- baseline, not counted").+--+-- These return the Visibility components per prompt and per (model,+-- platform) in both comparison windows, over the prompts answered in both+-- when p_same_prompts (00130), with each prompt's text and topic; the app+-- folds engines into platforms and prompts into topics, and scores every+-- slice with the card's formula. They also count the prompts answered this+-- period but not the one before, and when the ones created this period were+-- added. The rollups serve whole days, so the box costs what the cards cost;+-- prompt_results serves the 24h and topic views, like+-- insights_platform_compare (00132).+-- `complete` is false while the prompt-grain rollups behind the window are+-- not backfilled (00130).++create or replace function public.insights_change_drivers_daily(+ p_brand_id uuid,+ p_cur_from date,+ p_cur_to date,+ p_prev_from date,+ p_prev_to date,+ p_models text[] default null,+ p_region text default null,+ p_same_prompts boolean default true+)+returns jsonb+language sql+stable+security invoker+set search_path to 'public'+as $$+ with prompt_rows as (+ select d.*,+ case when d.day between p_cur_from and p_cur_to then 'cur' else 'prev' end as win+ from public.insights_prompt_daily d+ where d.brand_id = p_brand_id+ and (p_models is null or d.model_used = any (p_models))+ and (p_region is null or d.region = p_region)+ and (d.day between p_cur_from and p_cur_to or d.day between p_prev_from and p_prev_to)+ ),+ presence as (+ select prompt_id,+ bool_or(win = 'cur') as in_cur,+ bool_or(win = 'prev') as in_prev+ from prompt_rows+ group by prompt_id+ ),+ scoped as (+ select r.* from prompt_rows r join presence p using (prompt_id)+ where not p_same_prompts or (p.in_cur and p.in_prev)+ ),+ by_prompt as (+ select prompt_id, win,+ sum(answer_count) as answers,+ sum(mention_answers) as mention_answers,+ sum(citation_answers) as citation_answers,+ sum(sum_inv_position) as sum_inv_position,+ sum(position_count) as position_count+ from scoped+ group by prompt_id, win+ ),+ by_engine as (+ select model_used, platform, win,+ sum(answer_count) as answers,+ sum(mention_answers) as mention_answers,+ sum(citation_answers) as citation_answers,+ sum(sum_inv_position) as sum_inv_position,+ sum(position_count) as position_count+ from scoped+ group by model_used, platform, win+ ),+ added as (+ select p.created_at+ from presence pr+ join public.prompts p on p.id = pr.prompt_id+ where pr.in_cur and not pr.in_prev+ )+ select jsonb_build_object(+ 'complete', not exists (select 1 from prompt_rows where mention_answers is null),+ 'prompts', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'prompt_id', b.prompt_id,+ 'win', b.win,+ 'answers', b.answers,+ 'mention_answers', b.mention_answers,+ 'citation_answers', b.citation_answers,+ 'sum_inv_position', b.sum_inv_position,+ 'position_count', b.position_count))+ from by_prompt b),+ '[]'::jsonb),+ 'engines', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'model_used', e.model_used,+ 'platform', e.platform,+ 'win', e.win,+ 'answers', e.answers,+ 'mention_answers', e.mention_answers,+ 'citation_answers', e.citation_answers,+ 'sum_inv_position', e.sum_inv_position,+ 'position_count', e.position_count))+ from by_engine e),+ '[]'::jsonb),+ 'prompt_info', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'id', p.id,+ 'text', p.text,+ 'topic_id', p.topic_id,+ 'topic_name', t.name))+ from public.prompts p+ left join public.topics t on t.id = p.topic_id+ where p.id in (select prompt_id from by_prompt)),+ '[]'::jsonb),+ -- Dates only for prompts created this period: one answered again after+ -- a pause is new to the comparison, not newly added.+ 'new_prompts', (select jsonb_build_object(+ 'count', count(*),+ 'first_added', min(created_at) filter (+ where created_at >= (p_cur_from::timestamp at time zone 'utc')),+ 'last_added', max(created_at) filter (+ where created_at >= (p_cur_from::timestamp at time zone 'utc')))+ from added)+ );+$$;++create or replace function public.insights_change_drivers(+ p_brand_id uuid,+ p_cur_from timestamptz,+ p_cur_to timestamptz,+ p_prev_from timestamptz,+ p_prev_to timestamptz,+ p_models text[] default null,+ p_region text default null,+ p_topic_id uuid default null,+ p_same_prompts boolean default true+)+returns jsonb+language sql+stable+security invoker+set search_path to 'public'+as $$+ with filtered as (+ select pr.prompt_id, pr.model_used, pr.platform, pr.created_at,+ pr.mention_count, pr.citation_count, pr.mention_position+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.platform <> 'chatgpt-shopping'+ and (p_models is null or pr.model_used = any (p_models))+ and (p_region is null or pr.region = p_region)+ and pr.created_at >= p_prev_from+ and pr.created_at <= p_cur_to+ and (p_topic_id is null or exists (+ select 1 from public.prompts p+ where p.id = pr.prompt_id and p.topic_id = p_topic_id))+ ),+ -- Each window selected on its own, inclusive at both ends like the card.+ windowed as (+ select 'cur' as win, f.* from filtered f+ where f.created_at >= p_cur_from and f.created_at <= p_cur_to+ union all+ select 'prev' as win, f.* from filtered f+ where f.created_at >= p_prev_from and f.created_at <= p_prev_to+ ),+ presence as (+ select prompt_id,+ bool_or(win = 'cur') as in_cur,+ bool_or(win = 'prev') as in_prev+ from windowed+ group by prompt_id+ ),+ scoped as (+ select w.* from windowed w join presence p using (prompt_id)+ where not p_same_prompts or (p.in_cur and p.in_prev)+ ),+ by_prompt as (+ select prompt_id, win,+ count(*) as answers,+ count(*) filter (where mention_count > 0) as mention_answers,+ count(*) filter (where citation_count > 0) as citation_answers,+ sum(1.0 / mention_position) filter (where mention_position is not null)+ as sum_inv_position,+ count(*) filter (where mention_position is not null) as position_count+ from scoped+ group by prompt_id, win+ ),+ by_engine as (+ select model_used, platform, win,+ count(*) as answers,+ count(*) filter (where mention_count > 0) as mention_answers,+ count(*) filter (where citation_count > 0) as citation_answers,+ sum(1.0 / mention_position) filter (where mention_position is not null)+ as sum_inv_position,+ count(*) filter (where mention_position is not null) as position_count+ from scoped+ group by model_used, platform, win+ ),+ added as (+ select p.created_at+ from presence pr+ join public.prompts p on p.id = pr.prompt_id+ where pr.in_cur and not pr.in_prev+ )+ select jsonb_build_object(+ 'complete', true,+ 'prompts', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'prompt_id', b.prompt_id,+ 'win', b.win,+ 'answers', b.answers,+ 'mention_answers', b.mention_answers,+ 'citation_answers', b.citation_answers,+ 'sum_inv_position', b.sum_inv_position,+ 'position_count', b.position_count))+ from by_prompt b),+ '[]'::jsonb),+ 'engines', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'model_used', e.model_used,+ 'platform', e.platform,+ 'win', e.win,+ 'answers', e.answers,+ 'mention_answers', e.mention_answers,+ 'citation_answers', e.citation_answers,+ 'sum_inv_position', e.sum_inv_position,+ 'position_count', e.position_count))+ from by_engine e),+ '[]'::jsonb),+ 'prompt_info', coalesce(+ (select jsonb_agg(jsonb_build_object(+ 'id', p.id,+ 'text', p.text,+ 'topic_id', p.topic_id,+ 'topic_name', t.name))+ from public.prompts p+ left join public.topics t on t.id = p.topic_id+ where p.id in (select prompt_id from by_prompt)),+ '[]'::jsonb),+ -- Dates only for prompts created this period: one answered again after+ -- a pause is new to the comparison, not newly added.+ 'new_prompts', (select jsonb_build_object(+ 'count', count(*),+ 'first_added', min(created_at) filter (where created_at >= p_cur_from),+ 'last_added', max(created_at) filter (where created_at >= p_cur_from))+ from added)+ );+$$;++revoke all on function public.insights_change_drivers_daily(+ uuid, date, date, date, date, text[], text, boolean+) from public;+grant execute on function public.insights_change_drivers_daily(+ uuid, date, date, date, date, text[], text, boolean+) to authenticated, service_role;++revoke all on function public.insights_change_drivers(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid, boolean+) from public;+grant execute on function public.insights_change_drivers(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid, boolean+) to authenticated, service_role;++comment on function public.insights_change_drivers_daily(+ uuid, date, date, date, date, text[], text, boolean+) is+ 'Insights "Why it changed" (VIS-009, 00135): Visibility components per prompt and per (model, platform) in both comparison windows, over the prompts answered in both when p_same_prompts, with prompt text and topic, and the prompts new this period, from the prompt-grain rollups. complete=false until they are backfilled (00130).';++comment on function public.insights_change_drivers(+ uuid, timestamptz, timestamptz, timestamptz, timestamptz, text[], text, uuid, boolean+) is+ 'Insights "Why it changed" (VIS-009, 00135) for timestamp windows (24h, topic views), from prompt_results. Same output as insights_change_drivers_daily.';++-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00136_insights_trend.sql+-- ─────────────────────────────────────────────────────────────────────────+-- Insights visibility trend (VIS-007).+--+-- The v2 mockup's trend keeps the competitors you pin (up to five) on the+-- chart whatever the range, and marks the days that explain a step in the+-- line: prompts added ("+509 prompts added · Sep 29") and Action Center+-- actions sent ("AC-61 sent · Oct 4").+--+-- Pins are saved on the brand, so the whole team sees the same lines; like+-- every brand setting they're written by admins and managers (the brands+-- update policy). Ids of competitors deleted since are ignored on read.++alter table public.brands+ add column if not exists insights_pinned_competitors uuid[] not null default '{}'+ constraint brands_insights_pinned_competitors_max+ check (cardinality(insights_pinned_competitors) <= 5);++comment on column public.brands.insights_pinned_competitors is+ 'Competitors pinned on the Insights visibility trend (VIS-007, 00136), in pin order; at most five.';++-- Per UTC day (the trend's days), the prompts created for the brand (in the+-- topic, when the trend is filtered to one) and the actions a person sent+-- from it. Actions the nightly generator created have no actor on their+-- "created" event and aren't markers.+create or replace function public.insights_trend_markers(+ p_brand_id uuid,+ p_from timestamptz default null,+ p_to timestamptz default null,+ p_topic_id uuid default null+)+returns jsonb+language sql+stable+security invoker+set search_path to 'public'+as $$+ select jsonb_build_object(+ 'prompts_added', coalesce(+ (select jsonb_agg(jsonb_build_object('day', d.day, 'count', d.n) order by d.day)+ from (+ select (p.created_at at time zone 'utc')::date as day, count(*) as n+ from public.prompts p+ join public.prompt_sets ps on ps.id = p.prompt_set_id+ where ps.brand_id = p_brand_id+ and (p_topic_id is null or p.topic_id = p_topic_id)+ and (p_from is null or p.created_at >= p_from)+ and (p_to is null or p.created_at <= p_to)+ group by 1+ ) d),+ '[]'::jsonb),+ 'actions_sent', coalesce(+ (select jsonb_agg(jsonb_build_object('day', d.day, 'action_nos', d.nos) order by d.day)+ from (+ select (a.created_at at time zone 'utc')::date as day,+ array_agg(a.action_no order by a.action_no) as nos+ from public.actions a+ where a.brand_id = p_brand_id+ and (p_from is null or a.created_at >= p_from)+ and (p_to is null or a.created_at <= p_to)+ and exists (+ select 1 from public.action_events e+ where e.action_id = a.id+ and e.event = 'created'+ and e.actor_id is not null)+ group by 1+ ) d),+ '[]'::jsonb)+ );+$$;++revoke all on function public.insights_trend_markers(uuid, timestamptz, timestamptz, uuid) from public;+grant execute on function public.insights_trend_markers(uuid, timestamptz, timestamptz, uuid)+ to authenticated, service_role;++comment on function public.insights_trend_markers(uuid, timestamptz, timestamptz, uuid) is+ 'Insights trend markers (VIS-007, 00136): per UTC day, the prompts created for the brand (optionally in one topic) and the Action Center actions a person sent, between two optional bounds. Security invoker.';++-- ─────────────────────────────────────────────────────────────────────────-- migrations/00147_insights_same_prompts_definer.sql-- ─────────────────────────────────────────────────────────────────────────-- "Compare: Same prompts" timed out on the largest brand (#1036 review).