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
feat(content): start a page brought in with the prompts it answers (#1374)
Cihanb0aec6f36209
diff --git a/web/messages/en.json b/web/messages/en.jsonindex 598dc2ebb..4518f2565 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -4072,6 +4072,8 @@"aiCheck": "AI check","needsFeed": "Needs feed","noFanouts": "No fan-outs",+ "addPrompts": "Add prompts",+ "noFanoutsHint": "No fan-outs yet: they come from the page's prompts. Add its prompts in Sources.","addKeywords": "Add keywords","notHowTo": "Not a how-to","englishOnly": "English only",diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsxindex e4f46aa54..eb243fbfa 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx@@ -73,11 +73,17 @@ export function RulesTab({siteType,draftPageType,agentFix,+ onOpenSources,}: {state: DraftRulesState;siteType: SiteType;draftPageType: PageType | null;agentFix?: AgentFix;+ /**+ * Opens the Sources tab, where a draft with no prompts (so no fan-outs)+ * gets them; none while the sources are read, or when the draft has prompts.+ */+ onOpenSources?: () => void;}) {const t = useTranslations('contentEditor.editor.rules');const tTypes = useTranslations('contentEditor.pageTypes');@@ -174,6 +180,7 @@ export function RulesTab({status={status}onChange={(change) => state.change(rule.key, change)}agentFix={rule.key === 'aiTraces' ? agentFix : undefined}+ onOpenSources={onOpenSources}/>))}</ul>@@ -351,12 +358,14 @@ function RuleRow({status,onChange,agentFix,+ onOpenSources,}: {rule: ActiveRule;status: RuleStatus;onChange: (change: { on?: boolean; value?: string }) => void;/** "Remove AI traces" offers Fix: the agent rewrites only those phrases. */agentFix?: AgentFix;+ onOpenSources?: () => void;}) {const t = useTranslations('contentEditor.editor.rules');const name = t(`names.${rule.key}`);@@ -379,7 +388,7 @@ function RuleRow({>{name}</span>- <StatusBadge on={rule.on} status={status} />+ <StatusBadge on={rule.on} status={status} onOpenSources={onOpenSources} />{agentFix && rule.on && status.state === 'bad' && (<buttontype="button"@@ -487,8 +496,29 @@ export function ruleStatusDetail(return ruleStatusText(t, status);}-function StatusBadge({ on, status }: { on: boolean; status: RuleStatus }) {+function StatusBadge({+ on,+ status,+ onOpenSources,+}: {+ on: boolean;+ status: RuleStatus;+ onOpenSources?: () => void;+}) {const t = useTranslations('contentEditor.editor.rules.status');+ if (on && onOpenSources && status.state === 'info' && status.code === 'noFanouts') {+ // No fan-outs means no prompts yet: where to add them.+ return (+ <button+ type="button"+ title={t('noFanoutsHint')}+ onClick={onOpenSources}+ className="w-20 shrink-0 truncate text-right text-[11px] font-medium text-primary underline underline-offset-2 hover:no-underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50"+ >+ {t('addPrompts')}+ </button>+ );+ }if (!on) {return (<span className="w-20 shrink-0 text-right text-[11px] text-muted-foreground">{t('off')}</span>diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsxindex c10c6546e..c9fe2a4e1 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx@@ -201,6 +201,12 @@ export function EditorSidePanel({<div hidden={tab !== 'rules'}><RulesTabagentFix={agentFix}+ // Only a draft with no prompts gets no fan-outs for want of them.+ onOpenSources={+ reads.sources && !reads.sources.failed && reads.sources.data.prompts.length === 0+ ? () => open('sources')+ : undefined+ }state={rules}siteType={draft.siteType}draftPageType={isPageType(pageType) ? pageType : null}diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsxindex 05b2ce95f..4540faac1 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx@@ -1554,6 +1554,66 @@ describe('the side panel', () => {expect(rail()).not.toBeNull();});+ it('points a page without fan-outs to Sources, where its prompts are added', async () => {+ getDraftSources.mockResolvedValue({+ data: {+ prompts: [],+ fanouts: [],+ citedPages: [],+ gscQueries: [],+ brandDomains: [],+ competitorDomains: [],+ },+ choices: [],+ });+ getContentDraft.mockResolvedValue(draft({ sourceUrl: 'https://acme.example.com/pricing' }));+ window.innerWidth = 1440;+ await renderPage();++ const add = [...panel()!.querySelectorAll<HTMLButtonElement>('button')].find(+ (b) => b.textContent === 'Add prompts',+ )!;+ expect(add.title).toBe(+ "No fan-outs yet: they come from the page's prompts. Add its prompts in Sources.",+ );+ await act(async () => add.click());+ const selected = [...panel()!.querySelectorAll<HTMLElement>('[role="tab"]')].find(+ (el) => el.getAttribute('aria-selected') === 'true',+ );+ expect(selected!.textContent).toBe('Sources');+ });++ it('keeps "No fan-outs" for a draft with prompts whose answers ran no searches', async () => {+ getDraftSources.mockResolvedValue({+ data: {+ prompts: [+ {+ id: 'p1',+ text: 'Best CRM?',+ inDraft: true,+ citesPage: false,+ answers: 2,+ mentioned: 0,+ },+ ],+ fanouts: [],+ citedPages: [],+ gscQueries: [],+ brandDomains: [],+ competitorDomains: [],+ },+ choices: [],+ });+ getContentDraft.mockResolvedValue(draft({ sourceUrl: 'https://acme.example.com/pricing' }));+ window.innerWidth = 1440;+ await renderPage();++ expect(+ [...panel()!.querySelectorAll('button')].some((b) => b.textContent === 'Add prompts'),+ ).toBe(false);+ expect(panel()!.textContent).toContain('No fan-outs');+ });+it('opens Sources only when its tab is picked, and reads Impact only when it is', async () => {getDraftSources.mockResolvedValue({data: {diff --git a/web/src/lib/actions/content-drafts-start.test.ts b/web/src/lib/actions/content-drafts-start.test.tsindex cb626a6a2..587712d14 100644--- a/web/src/lib/actions/content-drafts-start.test.ts+++ b/web/src/lib/actions/content-drafts-start.test.ts@@ -402,6 +402,28 @@ describe('createContentDraft', () => {});});+ it('starts a page brought in with the prompts its title and headings speak to', async () => {+ reads = {+ prompts: [+ { id: 'p1', text: 'Pricing plans for small teams?' },+ { id: 'p2', text: 'Best espresso grinder' },+ { id: 'p3', text: 'Pricing of espresso machines' },+ ],+ };+ await createContentDraft(+ BRAND,+ start({+ method: 'optimize',+ title: 'Pricing',+ pageType: 'pricing',+ source: { kind: 'content' },+ bodyHtml: '<h1>Pricing</h1><h2>Plans for small teams</h2><p>From 12 a month.</p>',+ }),+ );+ // p3 shares one word only.+ expect(inserted()).toMatchObject({ prompt_ids: ['p1'] });+ });+it('writes nothing for a source the member can’t read, or a start that isn’t valid', async () => {await expect(createContentDraft(BRAND, start())).rejects.toThrow('not_found');await expect(diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.tsindex 2c911ea07..184783114 100644--- a/web/src/lib/actions/content-drafts.ts+++ b/web/src/lib/actions/content-drafts.ts@@ -35,6 +35,7 @@ import {} from '@/lib/content/editor';import { detectPageType, siteTypeOf, type SiteType } from '@/lib/content/page-types';import { sanitizeDraftHtml } from '@/lib/content/sanitize-draft-html';+import { matchPagePrompts } from '@/lib/content/page-prompts';import {NO_DRAFT_RULES,draftRulesFit,@@ -487,17 +488,40 @@ const startInput = z.object({/** The most prompt ids a draft keeps (00206). */const MAX_DRAFT_PROMPTS = 500;+/** The brand's active prompts read to match a page brought in (matchPagePrompts). */+const PAGE_PROMPTS_READ = 1_000;+/*** The tracked prompts a page started from this source should answer: an- * opportunity's (its clusters', or its one prompt), the prompt picked, or a- * topic's active prompts. Throws 'not_found' for one the member can't read.+ * opportunity's (its clusters', or its one prompt), the prompt picked, a+ * topic's active prompts, or, for a page brought in (pasted, read from its+ * URL or a file), the active prompts its title and headings speak to.+ * Throws 'not_found' for one the member can't read.*/async function promptsFor(supabase: Awaited<ReturnType<typeof createClient>>,brandId: string,source: DraftStart['source'],+ /** The page brought in; none for the overlap check, which has no page yet. */+ page: { title: string; html: string } = { title: '', html: '' },): Promise<{ promptIds: string[]; opportunityId: string | null }> {- if (source.kind === 'content') return { promptIds: [], opportunityId: null };+ if (source.kind === 'content') {+ if (!page.html) return { promptIds: [], opportunityId: null };+ const { data, error } = await supabase+ .from('prompts')+ .select('id, text, prompt_sets!inner(brand_id)')+ .eq('prompt_sets.brand_id', brandId)+ .eq('is_active', true)+ // The newest first, for a brand with more than are read.+ .order('created_at', { ascending: false })+ .limit(PAGE_PROMPTS_READ);+ // Without them the page still starts, with its prompts to add in Sources.+ if (error) {+ console.error('Failed to read the prompts to match an imported page:', error);+ return { promptIds: [], opportunityId: null };+ }+ return { promptIds: matchPagePrompts(page.title, page.html, data ?? []), opportunityId: null };+ }if (source.kind === 'opportunity') {const { data, error } = await supabase@@ -903,7 +927,7 @@ const overlapSource = startInput.shape.source;* Start's overlap check (CE-015): the brand's own pages AI cites for the* prompts a page started from this source would answer (00207), with prompts* in common, citations and Google position. Nothing to check for content- * brought to optimize: it has no prompts yet.+ * brought to optimize: it is the page itself.*/export async function getContentOverlap(brandId: string,@@ -951,7 +975,16 @@ export async function createContentDraft(brandId: string, start: DraftStart): Prdata: { user },} = await supabase.auth.getUser();if (!user) throw new Error('unauthorized');- const { promptIds, opportunityId } = await promptsFor(supabase, brandId, source);+ const bodyHtmlKept =+ bodyHtml !== undefined+ ? sanitizeDraftHtml(bodyHtml)+ : title+ ? `<h1>${escapeHtml(title)}</h1>`+ : '';+ const { promptIds, opportunityId } = await promptsFor(supabase, brandId, source, {+ title,+ html: bodyHtml !== undefined ? bodyHtmlKept : '',+ });const { data, error } = await supabase.from('content_drafts')@@ -962,12 +995,7 @@ export async function createContentDraft(brandId: string, start: DraftStart): Prtitle,page_type: pageType,page_type_source: typePicked ? 'user' : 'detected',- body_html:- bodyHtml !== undefined- ? sanitizeDraftHtml(bodyHtml)- : title- ? `<h1>${escapeHtml(title)}</h1>`- : '',+ body_html: bodyHtmlKept,opportunity_id: opportunityId,source_url: sourceUrl ?? null,prompt_ids: [...new Set(promptIds)].slice(0, MAX_DRAFT_PROMPTS),diff --git a/web/src/lib/content/page-prompts.test.ts b/web/src/lib/content/page-prompts.test.tsnew file mode 100644index 000000000..75e7267de--- /dev/null+++ b/web/src/lib/content/page-prompts.test.ts@@ -0,0 +1,34 @@+import { describe, expect, it } from 'vitest';+import { matchPagePrompts, pageHeadingWords } from './page-prompts';++/** The prompts a page brought in answers: its title's and headings' words. */+describe('matchPagePrompts', () => {+ const prompts = [+ { id: 'guide', text: 'What is a coffee subscription?' },+ { id: 'price', text: 'How much does a coffee subscription cost?' },+ { id: 'beans', text: 'Best single origin beans for espresso' },+ { id: 'one', text: 'Coffee gifts' },+ ];++ it('keeps the prompts most of whose words the page uses, the most in common first', () => {+ const html =+ '<h1>How to choose a coffee subscription</h1><p>Body text about espresso beans.</p>' ++ '<h2>What does a subscription cost?</h2>';+ expect(matchPagePrompts('Coffee subscriptions', html, prompts)).toEqual(['price', 'guide']);+ });++ it('reads only the title and headings, not the body', () => {+ expect(pageHeadingWords('Guide', '<h3>Fresh & local</h3><p>espresso</p>')).toEqual(+ new Set(['guide', 'fresh', 'local']),+ );+ });++ it('keeps none for one shared word, and at most the cap', () => {+ expect(matchPagePrompts('Coffee', '<h1>Coffee</h1>', prompts)).toEqual([]);+ const many = Array.from({ length: 12 }, (_, i) => ({+ id: `p${i}`,+ text: 'coffee subscription',+ }));+ expect(matchPagePrompts('Coffee subscription', '', many)).toHaveLength(10);+ });+});diff --git a/web/src/lib/content/page-prompts.ts b/web/src/lib/content/page-prompts.tsnew file mode 100644index 000000000..4aed57883--- /dev/null+++ b/web/src/lib/content/page-prompts.ts@@ -0,0 +1,52 @@+import { wordsOf } from './sources';++/**+ * The tracked prompts a page brought in to optimize answers (Content Agent+ * rule gaps, E). A page pasted, read from its URL or from a file starts with+ * no prompts, so the Sources tab and the fan-out rules had no fan-outs for it+ * ("No fan-outs") unless AI answers cited that very page. It now starts with+ * the brand's prompts its title and headings speak to; the person can leave+ * any of them out in Sources.+ */++/** The most prompts such a draft starts with. */+export const MAX_PAGE_PROMPTS = 10;+/** The share of a prompt's words the page's title and headings must use… */+const PROMPT_OVERLAP = 0.6;+/** …and at least this many of them: one shared word is chance. */+const MIN_SHARED_WORDS = 2;++/** A page's title and headings (H1–H3), as words. Read from its HTML on the server. */+export function pageHeadingWords(title: string, html: string): Set<string> {+ const headings = [...html.matchAll(/<h([1-3])\b[^>]*>([\s\S]*?)<\/h\1>/gi)].map((m) =>+ m[2].replace(/<[^>]*>/g, ' ').replace(/&[#a-z0-9]+;/gi, ' '),+ );+ return new Set(wordsOf([title, ...headings].join(' ')));+}++/**+ * The prompts whose words the page's title and headings use the most, the+ * most words in common first: at least PROMPT_OVERLAP of each prompt's words+ * and MIN_SHARED_WORDS of them, up to `max`.+ */+export function matchPagePrompts(+ title: string,+ html: string,+ prompts: { id: string; text: string }[],+ max = MAX_PAGE_PROMPTS,+): string[] {+ const page = pageHeadingWords(title, html);+ return (+ prompts+ .map((prompt) => {+ const words = [...new Set(wordsOf(prompt.text))];+ const shared = words.filter((word) => page.has(word)).length;+ return { id: prompt.id, shared, share: words.length ? shared / words.length : 0 };+ })+ .filter((m) => m.shared >= MIN_SHARED_WORDS && m.share >= PROMPT_OVERLAP)+ // More words in common first: a short, broad prompt matches too easily.+ .sort((a, b) => b.shared - a.shared || b.share - a.share)+ .slice(0, max)+ .map((m) => m.id)+ );+}