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): check for overlapping pages before a draft is started (#1287)
Cihana6213a98076e
diff --git a/supabase/migrations/00207_content_page_overlap.sql b/supabase/migrations/00207_content_page_overlap.sqlnew file mode 100644index 000000000..b2efe7bcf--- /dev/null+++ b/supabase/migrations/00207_content_page_overlap.sql@@ -0,0 +1,132 @@+-- ============================================================================+-- 00207: the overlap check before a page is written (Content Editor, CE-015)+-- ============================================================================+-- Start checks for overlap before anything is written (Content Editor spec v2+-- §5): which of the brand's own pages AI already cites for the prompts the+-- new page should answer, how many of those prompts each one answers, how+-- often it is cited, and where it ranks on Google. One page that already+-- answers most of them is better optimized than competed with.+--+-- * content_page_overlap(brand, prompt ids): the brand's pages cited in+-- tracked answers to those prompts since p_since (30 days by default),+-- one row per page (lower(normalize_page_path) key): prompts in common,+-- citations, and the impression-weighted Google position over the last+-- 28 days of Search Console data, when there is some. Most prompts in+-- common first. Security definer with an organization check, as+-- ai_traffic_page_citations (00176): citation_urls has no member select+-- policy. Read through citation_urls' domain index first, as+-- ga_page_ai_visibility does. The window's answers to the prompts come+-- from idx_prompt_results_brand_created_cover alone (an index-only scan,+-- hash-joined), never from prompt_results' wide rows one citation at a+-- time; ChatGPT Shopping is left out, as in Impact (00209). Planned for+-- each call's own values (plan_cache_mode): a generic plan of these+-- estimates is far slower.+-- * content_drafts.source_url: the existing page a draft optimizes, when+-- there is one (Start's "Optimize … instead"); the editor shows it beside+-- the page type.++alter table public.content_drafts+ add column if not exists source_url text+ check (+ source_url is null+ or (char_length(source_url) <= 2048 and source_url ~* '^https?://')+ );++comment on column public.content_drafts.source_url is+ 'Content Editor (CE-015): the existing page this draft optimizes.';++create or replace function public.content_page_overlap(+ p_brand_id uuid,+ p_prompt_ids uuid[],+ p_since timestamptz default now() - interval '30 days'+)+returns table (+ page text,+ title text,+ prompts_in_common integer,+ citations bigint,+ gsc_position numeric+)+language plpgsql+stable+security definer+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role' then+ return;+ end if;+ if coalesce(cardinality(p_prompt_ids), 0) = 0 then+ return;+ end if;++ return query+ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ),+ own_urls as materialized (+ select cu.id, cu.url, cu.title, lower(public.normalize_page_path(cu.url)) as page_key+ from public.citation_urls cu+ where cu.domain in (select domain from own_domains)+ ),+ -- The window's answers to these prompts, from the (brand_id, created_at)+ -- cover index alone.+ answers as materialized (+ select pr.id, pr.prompt_id+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.created_at >= p_since+ and pr.prompt_id = any(p_prompt_ids)+ and pr.platform is distinct from 'chatgpt-shopping'+ ),+ cited as (+ select ou.page_key, ou.url, ou.title, a.prompt_id+ from public.prompt_result_citations prc+ join answers a on a.id = prc.prompt_result_id+ join own_urls ou on ou.id = prc.url_id+ where prc.brand_id = p_brand_id+ and prc.created_at >= p_since+ ),+ pages as (+ select+ c.page_key,+ -- The address cited most often stands for the page.+ mode() within group (order by c.url) as page,+ max(c.title) as title,+ count(distinct c.prompt_id)::integer as prompts_in_common,+ count(*)::bigint as citations+ from cited c+ group by c.page_key+ ),+ google as (+ select+ g.page_key,+ round((sum(g.position * g.impressions) / nullif(sum(g.impressions), 0))::numeric, 1)+ as position+ from public.gsc_page_stats g+ where g.brand_id = p_brand_id+ and g.date >= (now() at time zone 'utc')::date - 28+ and g.page_key in (select p.page_key from pages p)+ group by g.page_key+ )+ select p.page, p.title, p.prompts_in_common, p.citations, gl.position+ from pages p+ left join google gl on gl.page_key = p.page_key+ order by p.prompts_in_common desc, p.citations desc, p.page+ limit 10;+end;+$$;++revoke all on function public.content_page_overlap(uuid, uuid[], timestamptz) from public, anon;+grant execute on function public.content_page_overlap(uuid, uuid[], timestamptz)+ to authenticated, service_role;++comment on function public.content_page_overlap(uuid, uuid[], timestamptz) is+ 'Content Editor (CE-015): the brand''s own pages cited for these prompts since p_since, with prompts in common, citations and Google position, for Start''s overlap check. Security definer with an organization check.';diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 14c9bb703..2efd18d36 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -28934,3 +28934,139 @@ create or replace trigger prompts_forget_in_content_draftsreferencing old table as deleted_promptsfor each statement execute function public.forget_deleted_draft_prompts();+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00207_content_page_overlap.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00207: the overlap check before a page is written (Content Editor, CE-015)+-- ============================================================================+-- Start checks for overlap before anything is written (Content Editor spec v2+-- §5): which of the brand's own pages AI already cites for the prompts the+-- new page should answer, how many of those prompts each one answers, how+-- often it is cited, and where it ranks on Google. One page that already+-- answers most of them is better optimized than competed with.+--+-- * content_page_overlap(brand, prompt ids): the brand's pages cited in+-- tracked answers to those prompts since p_since (30 days by default),+-- one row per page (lower(normalize_page_path) key): prompts in common,+-- citations, and the impression-weighted Google position over the last+-- 28 days of Search Console data, when there is some. Most prompts in+-- common first. Security definer with an organization check, as+-- ai_traffic_page_citations (00176): citation_urls has no member select+-- policy. Read through citation_urls' domain index first, as+-- ga_page_ai_visibility does. The window's answers to the prompts come+-- from idx_prompt_results_brand_created_cover alone (an index-only scan,+-- hash-joined), never from prompt_results' wide rows one citation at a+-- time; ChatGPT Shopping is left out, as in Impact (00209). Planned for+-- each call's own values (plan_cache_mode): a generic plan of these+-- estimates is far slower.+-- * content_drafts.source_url: the existing page a draft optimizes, when+-- there is one (Start's "Optimize … instead"); the editor shows it beside+-- the page type.++alter table public.content_drafts+ add column if not exists source_url text+ check (+ source_url is null+ or (char_length(source_url) <= 2048 and source_url ~* '^https?://')+ );++comment on column public.content_drafts.source_url is+ 'Content Editor (CE-015): the existing page this draft optimizes.';++create or replace function public.content_page_overlap(+ p_brand_id uuid,+ p_prompt_ids uuid[],+ p_since timestamptz default now() - interval '30 days'+)+returns table (+ page text,+ title text,+ prompts_in_common integer,+ citations bigint,+ gsc_position numeric+)+language plpgsql+stable+security definer+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+begin+ if not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = p_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role' then+ return;+ end if;+ if coalesce(cardinality(p_prompt_ids), 0) = 0 then+ return;+ end if;++ return query+ with own_domains as (+ select lower(regexp_replace(bd.domain, '^www\.', '')) as domain+ from public.brand_domains bd+ where bd.brand_id = p_brand_id+ ),+ own_urls as materialized (+ select cu.id, cu.url, cu.title, lower(public.normalize_page_path(cu.url)) as page_key+ from public.citation_urls cu+ where cu.domain in (select domain from own_domains)+ ),+ -- The window's answers to these prompts, from the (brand_id, created_at)+ -- cover index alone.+ answers as materialized (+ select pr.id, pr.prompt_id+ from public.prompt_results pr+ where pr.brand_id = p_brand_id+ and pr.created_at >= p_since+ and pr.prompt_id = any(p_prompt_ids)+ and pr.platform is distinct from 'chatgpt-shopping'+ ),+ cited as (+ select ou.page_key, ou.url, ou.title, a.prompt_id+ from public.prompt_result_citations prc+ join answers a on a.id = prc.prompt_result_id+ join own_urls ou on ou.id = prc.url_id+ where prc.brand_id = p_brand_id+ and prc.created_at >= p_since+ ),+ pages as (+ select+ c.page_key,+ -- The address cited most often stands for the page.+ mode() within group (order by c.url) as page,+ max(c.title) as title,+ count(distinct c.prompt_id)::integer as prompts_in_common,+ count(*)::bigint as citations+ from cited c+ group by c.page_key+ ),+ google as (+ select+ g.page_key,+ round((sum(g.position * g.impressions) / nullif(sum(g.impressions), 0))::numeric, 1)+ as position+ from public.gsc_page_stats g+ where g.brand_id = p_brand_id+ and g.date >= (now() at time zone 'utc')::date - 28+ and g.page_key in (select p.page_key from pages p)+ group by g.page_key+ )+ select p.page, p.title, p.prompts_in_common, p.citations, gl.position+ from pages p+ left join google gl on gl.page_key = p.page_key+ order by p.prompts_in_common desc, p.citations desc, p.page+ limit 10;+end;+$$;++revoke all on function public.content_page_overlap(uuid, uuid[], timestamptz) from public, anon;+grant execute on function public.content_page_overlap(uuid, uuid[], timestamptz)+ to authenticated, service_role;++comment on function public.content_page_overlap(uuid, uuid[], timestamptz) is+ 'Content Editor (CE-015): the brand''s own pages cited for these prompts since p_since, with prompts in common, citations and Google position, for Start''s overlap check. Security definer with an organization check.';+diff --git a/web/messages/en.json b/web/messages/en.jsonindex 498f59bf2..eb04408a1 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -3170,8 +3170,48 @@"pageType": "Page type","detectedTitle": "Picked from the title.","detectedContent": "Picked from the content.",+ "detectedPage": "Picked from the page's address.","openEditor": "Open editor","failed": "Couldn't start the draft. Try again."+ },+ "overlap": {+ "label": "Overlap check",+ "checking": "Checking your pages that answer the same prompts…",+ "failed": "Couldn't check for overlapping pages.",+ "retry": "Try again",+ "none": "No existing page answers these prompts",+ "noPrompts": "No tracked prompts to check this against yet",+ "safe": "Safe to create",+ "covered": "An existing page already answers these prompts",+ "partial": "{count, plural, one {# existing page answers} other {# existing pages answer}} some of these prompts",+ "columns": {+ "page": "Page",+ "prompts": "Same prompts",+ "citations": "AI citations",+ "google": "Google pos."+ },+ "promptsValue": "{count} of {total}",+ "recommended": "Recommended",+ "options": {+ "optimizeInstead": {+ "title": "Optimize {page} instead",+ "reason": "It already answers {count} of {total} prompts and is cited {citations}×",+ "reasonRanked": "It already answers {count} of {total} prompts, is cited {citations}× and ranks #{position} on Google"+ },+ "createAnyway": {+ "title": "Create a new page anyway",+ "reason": "Risk: two pages compete for the same prompts"+ },+ "createLinked": {+ "title": "Create a new page, link {page} to it",+ "titleMany": "Create a new page, link these pages to it",+ "reason": "One clear page for these prompts"+ },+ "addSection": {+ "title": "Add a section to {page}",+ "reason": "A smaller change than a new page"+ }+ }}},"editor": {diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsxindex 9a1ccef2c..d55f96808 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx@@ -294,6 +294,7 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {pageType={pageType}detected={picked === null}isNew={draft.method === 'create'}+ sourceUrl={draft.sourceUrl}onPick={(type) => void pickPageType(type)}/><DraftMethodBadge method={draft.method} />diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_page-type-menu.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_page-type-menu.tsxindex 7c3e90864..bce27b66c 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_page-type-menu.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_page-type-menu.tsx@@ -16,6 +16,7 @@ import {DropdownMenuTrigger,} from '@/components/ui/dropdown-menu';import { isPageType, type PageType } from '@/lib/content/drafts';+import { pagePath } from '@/lib/content/overlap';import { SITE_PAGE_TYPES, type SiteType } from '@/lib/content/page-types';import { cn } from '@/lib/utils';@@ -31,6 +32,7 @@ export function PageTypeMenu({pageType,detected,isNew,+ sourceUrl,onPick,}: {brandId: string;@@ -40,6 +42,8 @@ export function PageTypeMenu({detected: boolean;/** A new page: the tag says so after the type. */isNew: boolean;+ /** The existing page an optimize draft is for: the tag shows its path. */+ sourceUrl: string | null;onPick: (type: PageType) => void;}) {const t = useTranslations('contentEditor.editor');@@ -54,14 +58,16 @@ export function PageTypeMenu({<DropdownMenu><DropdownMenuTriggeraria-label={t('pageType.change', { type: label })}- title={detected ? t('pageType.detectedHint') : undefined}+ title={detected ? t('pageType.detectedHint') : (sourceUrl ?? undefined)}className={cn(badgeVariants({ variant: 'outline' }),'cursor-pointer gap-1 font-normal hover:bg-muted',!current && 'border-dashed text-muted-foreground',)}>- {[label, isNew ? t('newPage') : null].filter(Boolean).join(' · ')}+ {[label, isNew ? t('newPage') : sourceUrl ? pagePath(sourceUrl) : null]+ .filter(Boolean)+ .join(' · ')}<ChevronDown className="h-3 w-3 opacity-60" /></DropdownMenuTrigger><DropdownMenuContent align="start" className="w-60">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 45ed79784..e7a16a135 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@@ -87,6 +87,7 @@ const draft = (over: Partial<ContentDraft> = {}): ContentDraft => ({pageType: 'feature_page',pageTypeSource: 'user',siteType: 'b2b',+ sourceUrl: null,method: 'optimize',status: 'in_review',bodyHtml: '<h1>Pipeline forecasting</h1><p>Teams trust it.</p>',@@ -273,6 +274,15 @@ describe('the content editor', () => {expect(typeMenu().textContent).toBe('Product page');});+ it('names the existing page an optimize draft is for beside its type', async () => {+ getContentDraft.mockResolvedValue(+ draft({ sourceUrl: 'https://northwind.example/features/pipeline-forecasting/' }),+ );+ await renderPage();+ expect(typeMenu().textContent).toBe('Feature page · /features/pipeline-forecasting');+ expect(typeMenu().title).toBe('https://northwind.example/features/pipeline-forecasting/');+ });+it('says when no type is detected yet', async () => {getContentDraft.mockResolvedValue(draft({ pageType: null, pageTypeSource: 'detected', method: 'create' }),diff --git a/web/src/components/content/new-content-dialog.test.tsx b/web/src/components/content/new-content-dialog.test.tsxindex f2ee10c0a..6b5dd2a43 100644--- a/web/src/components/content/new-content-dialog.test.tsx+++ b/web/src/components/content/new-content-dialog.test.tsx@@ -8,9 +8,11 @@ import { NewContentDialog } from './new-content-dialog';const getContentStartOptions = vi.fn();const createContentDraft = vi.fn();+const getContentOverlap = vi.fn();vi.mock('@/lib/actions/content-drafts', () => ({getContentStartOptions: (...args: unknown[]) => getContentStartOptions(...args),createContentDraft: (...args: unknown[]) => createContentDraft(...args),+ getContentOverlap: (...args: unknown[]) => getContentOverlap(...args),}));const push = vi.fn();vi.mock('@/i18n/navigation', () => ({ useRouter: () => ({ push }) }));@@ -87,6 +89,7 @@ beforeEach(() => {root = createRoot(container);getContentStartOptions.mockReset().mockResolvedValue(OPTIONS);createContentDraft.mockReset().mockResolvedValue('d9');+ getContentOverlap.mockReset().mockResolvedValue({ totalPrompts: 2, pages: [] });push.mockReset();toast.error.mockReset();});@@ -172,6 +175,8 @@ describe('New content', () => {source: { kind: 'content' },bodyHtml: '<h2>Returns policy</h2><p>30 days.</p>',});+ // Content brought to optimize has no prompts to check.+ expect(getContentOverlap).not.toHaveBeenCalled();});it('takes typed plain text as paragraphs, and a blog post when nothing tells the type', async () => {@@ -225,4 +230,116 @@ describe('New content', () => {expect(getContentStartOptions).toHaveBeenCalledTimes(2);expect(button('Pick an opportunity')).toBeDefined();});++ describe('the overlap check', () => {+ const GUIDE = {+ url: 'https://acme.example/guides/espresso-machines-for-beginners/',+ title: 'Espresso machines for beginners',+ promptsInCommon: 3,+ citations: 4,+ gscPosition: 3.5,+ };+ /** Starts a new page from the first opportunity and waits for the check. */+ async function startFromOpportunity() {+ await render();+ await click(button('Pick an opportunity'));+ const option = [...document.querySelectorAll<HTMLElement>('[role="option"]')].find(+ (o) => o.textContent === 'Northwind vs HubSpot',+ );+ await click(option);+ await click(button('Continue'));+ await act(async () => {});+ }+ const overlapOption = (name: string) =>+ [...document.querySelectorAll<HTMLElement>('[role="radio"]')].find((r) =>+ r.textContent?.startsWith(name),+ )!;++ it('says a new page is safe when no page of the brand answers its prompts', async () => {+ await startFromOpportunity();+ expect(getContentOverlap).toHaveBeenCalledWith('b1', { kind: 'opportunity', id: 'o1' });+ expect(document.body.textContent).toContain('No existing page answers these prompts');+ expect(document.body.textContent).toContain('Safe to create');+ });++ it('recommends optimizing a page that already answers most of the prompts', async () => {+ getContentOverlap.mockResolvedValue({ totalPrompts: 4, pages: [GUIDE] });+ await startFromOpportunity();++ expect(document.body.textContent).toContain('An existing page already answers these prompts');+ expect(document.body.textContent).toContain('3 of 4');+ expect(document.body.textContent).toContain('#4');+ const optimize = overlapOption('Optimize /guides/espresso-machines-for-beginners instead');+ expect(optimize.getAttribute('aria-checked')).toBe('true');+ expect(optimize.textContent).toContain('Recommended');+ expect(optimize.textContent).toContain(+ 'It already answers 3 of 4 prompts, is cited 4× and ranks #4 on Google',+ );+ // The draft becomes that page's: its title, and the type its address shows.+ expect(document.body.textContent).toContain('Optimize: Espresso machines for beginners');+ expect(checkedType()).toBe('Guide');+ expect(document.body.textContent).toContain("Picked from the page's address.");++ await click(button('Open editor'));+ expect(createContentDraft).toHaveBeenCalledWith('b1', {+ method: 'optimize',+ title: 'Espresso machines for beginners',+ pageType: 'guide',+ // Read from the page's address, so it follows the page.+ typePicked: false,+ source: { kind: 'opportunity', id: 'o1' },+ bodyHtml: undefined,+ sourceUrl: GUIDE.url,+ });+ });++ it('goes back to the new page when creating one anyway', async () => {+ getContentOverlap.mockResolvedValue({ totalPrompts: 4, pages: [GUIDE] });+ await startFromOpportunity();+ await click(overlapOption('Create a new page anyway'));+ expect(document.body.textContent).toContain('Create: Northwind vs HubSpot');+ expect(checkedType()).toBe('Comparison');+ await click(button('Open editor'));+ expect(createContentDraft.mock.calls[0][1]).toMatchObject({+ method: 'create',+ title: 'Northwind vs HubSpot',+ pageType: 'comparison',+ sourceUrl: undefined,+ });+ });++ it('recommends a new page linked from pages that answer only some prompts', async () => {+ getContentOverlap.mockResolvedValue({+ totalPrompts: 4,+ pages: [+ {+ ...GUIDE,+ url: 'https://acme.example/blog/grinders',+ promptsInCommon: 1,+ gscPosition: null,+ },+ ],+ });+ await startFromOpportunity();+ expect(document.body.textContent).toContain('1 existing page answers some of these prompts');+ expect(document.body.textContent).toContain('—');+ expect(+ overlapOption('Create a new page, link /blog/grinders to it').getAttribute('aria-checked'),+ ).toBe('true');+ expect(overlapOption('Add a section to /blog/grinders')).toBeDefined();+ expect(document.body.textContent).toContain('Create: Northwind vs HubSpot');+ });++ it('lets the page start when the check fails, and tries again', async () => {+ vi.spyOn(console, 'error').mockImplementation(() => {});+ getContentOverlap.mockRejectedValueOnce(new Error('down'));+ await startFromOpportunity();+ expect(document.body.textContent).toContain("Couldn't check for overlapping pages.");+ expect(button('Open editor')!.disabled).toBe(false);+ await click(button('Try again'));+ await act(async () => {});+ expect(getContentOverlap).toHaveBeenCalledTimes(2);+ expect(document.body.textContent).toContain('Safe to create');+ });+ });});diff --git a/web/src/components/content/new-content-dialog.tsx b/web/src/components/content/new-content-dialog.tsxindex 61e434a28..5e4ed04e8 100644--- a/web/src/components/content/new-content-dialog.tsx+++ b/web/src/components/content/new-content-dialog.tsx@@ -3,7 +3,7 @@import { useEffect, useRef, useState } from 'react';import { useTranslations } from 'next-intl';import { toast } from 'sonner';-import { ArrowLeft, FileText, Loader2, X } from 'lucide-react';+import { AlertTriangle, ArrowLeft, Check, FileText, Loader2, X } from 'lucide-react';import { useRouter } from '@/i18n/navigation';import { Button } from '@/components/ui/button';import {@@ -28,18 +28,29 @@ import {import { Label } from '@/components/ui/label';import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';import { Textarea } from '@/components/ui/textarea';-import { createContentDraft, getContentStartOptions } from '@/lib/actions/content-drafts';+import {+ createContentDraft,+ getContentOverlap,+ getContentStartOptions,+} from '@/lib/actions/content-drafts';import { MAX_ATTACHMENT_BYTES, textToHtml } from '@/lib/content/attachments';import { cleanHtml } from '@/lib/content/clean-html';import {contentEditorPath,+ type ContentOverlap,type DraftMethod,type DraftSource,type DraftStartOptions,type PageType,} from '@/lib/content/drafts';import { DOCUMENT_ACCEPT, readDocumentFile } from '@/lib/content/import-document';-import { SITE_PAGE_TYPES, type SiteType } from '@/lib/content/page-types';+import {+ adviseOverlap,+ pagePath,+ type OverlapChoice,+ type OverlapOption,+} from '@/lib/content/overlap';+import { SITE_PAGE_TYPES, detectPageType, type SiteType } from '@/lib/content/page-types';import {fileTitle,startPageType,@@ -68,6 +79,9 @@ interface BroughtContent extends ContentSummary {imagesRemoved: number;}+/** Where Start's first page type came from, if it was read from somewhere. */+type DetectedFrom = 'title' | 'content' | 'page' | null;+/** What Start opens the editor with. */interface StartChoice {method: DraftMethod;@@ -75,9 +89,20 @@ interface StartChoice {source: DraftSource;bodyHtml?: string;pageType: PageType;- detected: boolean;- /** The person picked the type from the chips. */+ detectedFrom: DetectedFrom;+ /** The person picked the type from the chips; otherwise it follows the page. */picked: boolean;+ /** The page type for a new page, to go back to from optimizing an existing one. */+ newPageType: { pageType: PageType; detectedFrom: DetectedFrom; picked: boolean };+ /** The overlap check's pick: write the new page, or optimize an existing one. */+ choice: OverlapChoice;+}++/** The overlap check for one source: null while it runs. */+interface OverlapRead {+ key: string;+ overlap: ContentOverlap | null;+ failed: boolean;}/**@@ -114,6 +139,8 @@ export function NewContentDialog({const [reading, setReading] = useState(false);const [start, setStart] = useState<StartChoice | null>(null);const [opening, setOpening] = useState(false);+ const [overlapRead, setOverlapRead] = useState<OverlapRead | null>(null);+ const [overlapAttempt, setOverlapAttempt] = useState(0);const fileRef = useRef<HTMLInputElement>(null);// Each opening starts over and reads what there is to start from.@@ -135,6 +162,32 @@ export function NewContentDialog({};}, [open, brandId, attempt]);+ // Start checks the pages that already answer a new page's prompts. Content+ // brought to optimize has no prompts to check. A page type picked in Start+ // keeps the same source, so the check runs once per source.+ const startSource = start?.source;+ const overlapKey = startSource && startSource.kind !== 'content' ? sourceKey(startSource) : null;+ useEffect(() => {+ if (!startSource || startSource.kind === 'content') return;+ const key = sourceKey(startSource);+ let cancelled = false;+ getContentOverlap(brandId, startSource)+ .then((overlap) => {+ if (cancelled) return;+ setOverlapRead({ key, overlap, failed: false });+ // The recommended option is picked first.+ const recommended = adviseOverlap(overlap).options[0];+ if (recommended) setStart((s) => (s ? withChoice(s, recommended.choice, siteType) : s));+ })+ .catch((err) => {+ console.error('Failed to check for overlapping pages:', err);+ if (!cancelled) setOverlapRead({ key, overlap: null, failed: true });+ });+ return () => {+ cancelled = true;+ };+ }, [brandId, startSource, siteType, overlapAttempt]);+const reset = () => {setMethod('create');setKind('opportunity');@@ -144,6 +197,7 @@ export function NewContentDialog({setContent(null);setStart(null);setOpening(false);+ setOverlapRead(null);};const changeOpen = (next: boolean) => {if (!next) reset();@@ -213,12 +267,18 @@ export function NewContentDialog({if (!picked) return;const title = picked.pageTitle ?? picked.label;const type = startPageType({ title }, siteType);+ const newPageType = {+ pageType: type.pageType,+ detectedFrom: type.detected ? ('title' as const) : null,+ picked: false,+ };setStart({method,title,source: { kind, id: picked.value },- ...type,- picked: false,+ ...newPageType,+ newPageType,+ choice: { kind: 'create' },});return;}@@ -227,27 +287,39 @@ export function NewContentDialog({(typed ? { ...summarizeContent(textToHtml(typed)), html: textToHtml(typed) } : null);if (!brought) return;const type = startPageType({ title: brought.title, html: brought.html }, siteType);+ const newPageType = {+ pageType: type.pageType,+ detectedFrom: type.detected ? ('content' as const) : null,+ picked: false,+ };setStart({method,title: brought.title,source: { kind: 'content' },bodyHtml: brought.html,- ...type,- picked: false,+ ...newPageType,+ newPageType,+ choice: { kind: 'create' },});};+ // Optimizing an existing page instead: the draft is that page's.+ const existing = start?.choice.kind === 'optimize' ? start.choice.page : null;+ const startMethod: DraftMethod = existing ? 'optimize' : (start?.method ?? 'create');+ const startTitle = existing ? existing.title || pagePath(existing.url) : (start?.title ?? '');+const openEditor = async () => {if (!start) return;setOpening(true);try {const id = await createContentDraft(brandId, {- method: start.method,- title: start.title,+ method: startMethod,+ title: startTitle,pageType: start.pageType,typePicked: start.picked,source: start.source,bodyHtml: start.bodyHtml,+ sourceUrl: existing?.url,});router.push(contentEditorPath(id));} catch (err) {@@ -263,9 +335,9 @@ export function NewContentDialog({{start ? (<><DialogHeader>- <DialogTitle className="truncate pr-8" title={start.title}>- {t(start.method === 'create' ? 'start.titleCreate' : 'start.titleOptimize', {- title: start.title || t('start.untitled'),+ <DialogTitle className="truncate pr-8" title={startTitle}>+ {t(startMethod === 'create' ? 'start.titleCreate' : 'start.titleOptimize', {+ title: startTitle || t('start.untitled'),})}</DialogTitle><DialogDescription>{t('start.description')}</DialogDescription>@@ -282,19 +354,44 @@ export function NewContentDialog({key={type}selected={start.pageType === type}onSelect={() =>- setStart({ ...start, pageType: type, detected: false, picked: true })+ setStart({+ ...start,+ pageType: type,+ detectedFrom: null,+ picked: true,+ ...(start.choice.kind === 'create' && {+ newPageType: { pageType: type, detectedFrom: null, picked: true },+ }),+ })}>{tTypes(type)}</ChoiceChip>))}</div>- {start.detected && (+ {start.detectedFrom && (<p className="text-xs text-muted-foreground">- {t(start.bodyHtml ? 'start.detectedContent' : 'start.detectedTitle')}+ {t(+ {+ title: 'start.detectedTitle',+ content: 'start.detectedContent',+ page: 'start.detectedPage',+ }[start.detectedFrom],+ )}</p>)}</div>+ {overlapKey && (+ <OverlapCheck+ read={overlapRead?.key === overlapKey ? overlapRead : null}+ choice={start.choice}+ onChoose={(choice) => setStart(withChoice(start, choice, siteType))}+ onRetry={() => {+ setOverlapRead(null);+ setOverlapAttempt((n) => n + 1);+ }}+ />+ )}<DialogFooter><Button variant="ghost" onClick={() => setStart(null)} disabled={opening}><ArrowLeft className="h-4 w-4" />@@ -514,6 +611,198 @@ export function NewContentDialog({);}+function sourceKey(source: Exclude<DraftSource, { kind: 'content' }>) {+ return `${source.kind}:${source.id}`;+}++/** Start with this overlap pick: an existing page brings its own title and type. */+function withChoice(start: StartChoice, choice: OverlapChoice, siteType: SiteType): StartChoice {+ if (choice.kind === 'create') return { ...start, choice, ...start.newPageType };+ const detected = detectPageType({ url: choice.page.url, title: choice.page.title }, siteType);+ return {+ ...start,+ choice,+ pageType: detected ?? start.pageType,+ detectedFrom: detected ? 'page' : null,+ picked: detected ? false : start.picked,+ };+}++/**+ * Start's overlap check (CE-015): the brand's pages AI already cites for these+ * prompts, and what to do about them, the recommended option first.+ */+function OverlapCheck({+ read,+ choice,+ onChoose,+ onRetry,+}: {+ read: OverlapRead | null;+ choice: OverlapChoice;+ onChoose: (choice: OverlapChoice) => void;+ onRetry: () => void;+}) {+ const t = useTranslations('contentEditor.newContent.overlap');+ const overlap = read?.overlap ?? null;+ const advice = overlap ? adviseOverlap(overlap) : null;+ const shown = overlap?.pages.slice(0, 3) ?? [];+ const top = overlap?.pages[0];+ const isChosen = (option: OverlapOption) =>+ option.choice.kind === choice.kind &&+ (option.choice.kind === 'create' ||+ (choice.kind === 'optimize' && option.choice.page.url === choice.page.url));++ const optionText = (option: OverlapOption) => {+ const page = top ? pagePath(top.url) : '';+ switch (option.key) {+ case 'optimizeInstead':+ return {+ title: t('options.optimizeInstead.title', { page }),+ reason: t(+ top?.gscPosition != null+ ? 'options.optimizeInstead.reasonRanked'+ : 'options.optimizeInstead.reason',+ {+ count: top?.promptsInCommon ?? 0,+ total: overlap?.totalPrompts ?? 0,+ citations: formatNumber(top?.citations ?? 0),+ position: Math.round(top?.gscPosition ?? 0),+ },+ ),+ };+ case 'createAnyway':+ return {+ title: t('options.createAnyway.title'),+ reason: t('options.createAnyway.reason'),+ };+ case 'createLinked':+ return {+ title:+ (overlap?.pages.length ?? 0) > 1+ ? t('options.createLinked.titleMany')+ : t('options.createLinked.title', { page }),+ reason: t('options.createLinked.reason'),+ };+ case 'addSection':+ return {+ title: t('options.addSection.title', { page }),+ reason: t('options.addSection.reason'),+ };+ }+ };++ return (+ <div className="space-y-2">+ <Label>{t('label')}</Label>+ {!read ? (+ <p className="flex items-center gap-2 text-xs text-muted-foreground" aria-busy="true">+ <Loader2 className="h-3.5 w-3.5 animate-spin" />+ {t('checking')}+ </p>+ ) : read.failed || !advice || !overlap ? (+ <p className="flex items-center gap-2 text-xs text-muted-foreground">+ {t('failed')}+ <button type="button" className="font-medium underline" onClick={onRetry}>+ {t('retry')}+ </button>+ </p>+ ) : advice.verdict === 'none' ? (+ <div className="flex items-center justify-between gap-3 rounded-lg border px-3 py-2.5 text-sm">+ <span>{overlap.totalPrompts ? t('none') : t('noPrompts')}</span>+ {overlap.totalPrompts > 0 && (+ <span className="flex shrink-0 items-center gap-1 text-xs font-medium text-emerald-600 dark:text-emerald-400">+ <Check className="h-3.5 w-3.5" />+ {t('safe')}+ </span>+ )}+ </div>+ ) : (+ <>+ <div className="rounded-lg border border-amber-500/40 bg-amber-500/10 px-3 py-2.5 text-sm">+ <p className="flex items-center gap-1.5 font-medium text-amber-700 dark:text-amber-400">+ <AlertTriangle className="h-3.5 w-3.5 shrink-0" />+ {advice.verdict === 'covered'+ ? t('covered')+ : t('partial', { count: overlap.pages.length })}+ </p>+ <table className="mt-2 w-full table-fixed text-xs">+ <thead className="text-[11px] tracking-wide whitespace-nowrap text-muted-foreground uppercase">+ <tr>+ <th className="py-1 text-left font-medium">{t('columns.page')}</th>+ <th className="w-28 py-1 pl-3 text-right font-medium">{t('columns.prompts')}</th>+ <th className="w-28 py-1 pl-3 text-right font-medium">+ {t('columns.citations')}+ </th>+ <th className="w-24 py-1 pl-3 text-right font-medium">{t('columns.google')}</th>+ </tr>+ </thead>+ <tbody>+ {shown.map((page) => (+ <tr key={page.url} className="border-t border-amber-500/20">+ <td className="truncate py-1.5 pr-2" title={page.url}>+ {pagePath(page.url)}+ </td>+ <td className="py-1.5 pl-3 text-right tabular-nums">+ {t('promptsValue', {+ count: page.promptsInCommon,+ total: overlap.totalPrompts,+ })}+ </td>+ <td className="py-1.5 pl-3 text-right tabular-nums">+ {formatNumber(page.citations)}+ </td>+ <td className="py-1.5 pl-3 text-right tabular-nums">+ {page.gscPosition == null ? '—' : `#${Math.round(page.gscPosition)}`}+ </td>+ </tr>+ ))}+ </tbody>+ </table>+ </div>+ <div role="radiogroup" aria-label={t('label')} className="grid gap-1.5">+ {advice.options.map((option) => {+ const text = optionText(option);+ const chosen = isChosen(option);+ return (+ <button+ key={option.key}+ type="button"+ role="radio"+ aria-checked={chosen}+ onClick={() => onChoose(option.choice)}+ className={cn(+ 'flex items-start gap-2.5 rounded-xl border px-3 py-2.5 text-left transition-colors hover:bg-muted/50',+ chosen && 'border-foreground ring-1 ring-inset ring-foreground',+ )}+ >+ <span+ className={cn(+ 'mt-0.5 h-4 w-4 shrink-0 rounded-full border-[1.5px] border-muted-foreground/50',+ chosen && 'border-[5px] border-foreground',+ )}+ />+ <span className="min-w-0">+ <span className="flex flex-wrap items-center gap-1.5 text-sm font-medium">+ {text.title}+ {option.recommended && (+ <span className="rounded-md bg-muted px-1.5 text-[10.5px] font-medium text-muted-foreground">+ {t('recommended')}+ </span>+ )}+ </span>+ <span className="block text-xs text-muted-foreground">{text.reason}</span>+ </span>+ </button>+ );+ })}+ </div>+ </>+ )}+ </div>+ );+}+function ChoiceChip({selected,onSelect,diff --git a/web/src/lib/actions/content-drafts-start.test.ts b/web/src/lib/actions/content-drafts-start.test.tsindex b3462769d..7df668816 100644--- a/web/src/lib/actions/content-drafts-start.test.ts+++ b/web/src/lib/actions/content-drafts-start.test.ts@@ -20,11 +20,17 @@ interface Call {let calls: Call[];// What a read of each table returns: a row for maybeSingle, rows otherwise.let reads: Record<string, unknown>;+let rpcCalls: { fn: string; args: unknown }[];+let rpcRows: unknown[];vi.mock('@/lib/action-center/assignees', () => ({ readAssignees: vi.fn(async () => new Map()) }));vi.mock('@/lib/supabase/server', () => ({createClient: vi.fn(async () => ({auth: { getUser: async () => ({ data: { user: { id: 'u1' } } }) },+ rpc: async (fn: string, args: unknown) => {+ rpcCalls.push({ fn, args });+ return { data: rpcRows, error: null };+ },from: (table: string) => {const call: Call = { table, op: 'select', filters: [] };calls.push(call);@@ -48,13 +54,16 @@ vi.mock('@/lib/supabase/server', () => ({})),}));-const { createContentDraft, getContentStartOptions } = await import('./content-drafts');+const { createContentDraft, getContentOverlap, getContentStartOptions } =+ await import('./content-drafts');const inserted = () => calls.find((call) => call.op === 'insert')?.value as Record<string, unknown>;beforeEach(() => {calls = [];reads = {};+ rpcCalls = [];+ rpcRows = [];});describe('getContentStartOptions', () => {@@ -145,6 +154,7 @@ describe('createContentDraft', () => {body_html: '<h1>CRM <for> agents</h1>',opportunity_id: OPPORTUNITY,prompt_ids: [PROMPT, 'p2'],+ source_url: null,});});@@ -235,3 +245,98 @@ describe('createContentDraft', () => {expect(calls.some((call) => call.op === 'insert')).toBe(false);});});++describe('getContentOverlap', () => {+ it('checks the source’s prompts against the brand’s cited pages', async () => {+ reads = { prompts: [{ id: PROMPT }, { id: 'p2' }, { id: PROMPT }] };+ rpcRows = [+ {+ page: 'https://acme.example/guides/espresso/',+ title: 'Espresso guide',+ prompts_in_common: 2,+ citations: '4',+ gsc_position: '3.5',+ },+ {+ page: 'https://acme.example/blog/x',+ title: null,+ prompts_in_common: 1,+ citations: 1,+ gsc_position: null,+ },+ ];+ await expect(getContentOverlap(BRAND, { kind: 'topic', id: TOPIC })).resolves.toEqual({+ totalPrompts: 2,+ pages: [+ {+ url: 'https://acme.example/guides/espresso/',+ title: 'Espresso guide',+ promptsInCommon: 2,+ citations: 4,+ gscPosition: 3.5,+ },+ {+ url: 'https://acme.example/blog/x',+ title: null,+ promptsInCommon: 1,+ citations: 1,+ gscPosition: null,+ },+ ],+ });+ expect(rpcCalls).toEqual([+ { fn: 'content_page_overlap', args: { p_brand_id: BRAND, p_prompt_ids: [PROMPT, 'p2'] } },+ ]);+ });++ it('has nothing to check for content to optimize, or a source with no prompts', async () => {+ await expect(getContentOverlap(BRAND, { kind: 'content' })).resolves.toEqual({+ totalPrompts: 0,+ pages: [],+ });+ reads = {+ content_opportunities: {+ id: OPPORTUNITY,+ prompt_id: null,+ cluster_id: null,+ related_cluster_ids: [],+ },+ };+ await expect(+ getContentOverlap(BRAND, { kind: 'opportunity', id: OPPORTUNITY }),+ ).resolves.toEqual({ totalPrompts: 0, pages: [] });+ expect(rpcCalls).toEqual([]);+ });+});++describe('optimizing an existing page instead (overlap check)', () => {+ it('keeps the page’s address and starts the draft with its title as H1', async () => {+ reads = { prompts: [{ id: PROMPT }] };+ await createContentDraft(BRAND, {+ method: 'optimize',+ title: 'Espresso guide',+ pageType: 'buying_guide',+ source: { kind: 'topic', id: TOPIC },+ sourceUrl: 'https://acme.example/guides/espresso/',+ });+ expect(inserted()).toMatchObject({+ method: 'optimize',+ body_html: '<h1>Espresso guide</h1>',+ source_url: 'https://acme.example/guides/espresso/',+ prompt_ids: [PROMPT],+ });+ });++ it('refuses an address that isn’t a web page’s', async () => {+ await expect(+ createContentDraft(BRAND, {+ method: 'optimize',+ title: 'x',+ pageType: 'guide',+ source: { kind: 'content' },+ sourceUrl: 'javascript:alert(1)',+ }),+ ).rejects.toThrow('invalid');+ expect(calls).toEqual([]);+ });+});diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.tsindex 75df83934..45be1de54 100644--- a/web/src/lib/actions/content-drafts.ts+++ b/web/src/lib/actions/content-drafts.ts@@ -8,6 +8,7 @@ import {PAGE_TYPES,type ContentDraft,type ContentDraftListItem,+ type ContentOverlap,type DraftMethod,type DraftStart,type DraftStartOptions,@@ -103,7 +104,7 @@ export async function getContentDraft(id: string): Promise<ContentDraft | null>const { data, error } = await supabase.from('content_drafts').select(- `id, brand_id, title, page_type, page_type_source, method, status, body_html, author, updated_at, ${BRAND_SITE}`,+ `id, brand_id, title, page_type, page_type_source, method, status, body_html, author, source_url, updated_at, ${BRAND_SITE}`,).eq('id', id).maybeSingle();@@ -116,6 +117,7 @@ export async function getContentDraft(id: string): Promise<ContentDraft | null>pageType: data.page_type,pageTypeSource: data.page_type_source === 'user' ? 'user' : 'detected',siteType: brandSiteType(data.brands),+ sourceUrl: data.source_url,method: data.method as DraftMethod,status: draftStatus(data.status),bodyHtml: data.body_html,@@ -198,6 +200,11 @@ const startInput = z.object({z.object({ kind: z.literal('content') }),]),bodyHtml: z.string().max(MAX_BODY_HTML_LENGTH).optional(),+ sourceUrl: z+ .string()+ .max(2048)+ .regex(/^https?:\/\//i)+ .optional(),});/** The most prompt ids a draft keeps (00206). */@@ -258,17 +265,53 @@ async function promptsFor(return { promptIds: data.map((p) => p.id), opportunityId: null };}+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.+ */+export async function getContentOverlap(+ brandId: string,+ source: DraftStart['source'],+): Promise<ContentOverlap> {+ const parsed = overlapSource.safeParse(source);+ if (!draftId.safeParse(brandId).success || !parsed.success) throw new Error('invalid');+ const supabase = await createClient();+ const { promptIds } = await promptsFor(supabase, brandId, parsed.data);+ const prompts = [...new Set(promptIds)];+ if (!prompts.length) return { totalPrompts: 0, pages: [] };+ const { data, error } = await supabase.rpc('content_page_overlap', {+ p_brand_id: brandId,+ p_prompt_ids: prompts,+ });+ if (error) throw new Error(error.message);+ return {+ totalPrompts: prompts.length,+ pages: (data ?? []).map((row) => ({+ url: row.page,+ title: row.title,+ promptsInCommon: row.prompts_in_common,+ citations: Number(row.citations),+ gscPosition: row.gsc_position === null ? null : Number(row.gsc_position),+ })),+ };+}+/*** A draft started from New content and Start (CE-003, CE-015), by the* signed-in member: its page type, what it is for, and its first text. A new- * page starts with its title as the H1; content to optimize is checked- * against the draft allow-list, as a save is. A type the person picked stays;- * Start's guess is stored as detected, so it follows the page (CE-023).+ * page, or an existing one Start chose to optimize, starts with its title as+ * the H1; content brought to optimize is checked against the draft+ * allow-list, as a save is. A type the person picked stays; Start's guess is+ * stored as detected, so it follows the page (CE-023).*/export async function createContentDraft(brandId: string, start: DraftStart): Promise<string> {const parsed = startInput.safeParse(start);if (!draftId.safeParse(brandId).success || !parsed.success) throw new Error('invalid');- const { method, pageType, typePicked, source, bodyHtml } = parsed.data;+ const { method, pageType, typePicked, source, bodyHtml, sourceUrl } = parsed.data;const title = parsed.data.title.trim().slice(0, MAX_TITLE_LENGTH).trim();const supabase = await createClient();@@ -288,12 +331,13 @@ export async function createContentDraft(brandId: string, start: DraftStart): Prpage_type: pageType,page_type_source: typePicked ? 'user' : 'detected',body_html:- method === 'optimize'- ? sanitizeDraftHtml(bodyHtml ?? '')+ bodyHtml !== undefined+ ? sanitizeDraftHtml(bodyHtml): title? `<h1>${escapeHtml(title)}</h1>`: '',opportunity_id: opportunityId,+ source_url: sourceUrl ?? null,prompt_ids: [...new Set(promptIds)].slice(0, MAX_DRAFT_PROMPTS),}).select('id')diff --git a/web/src/lib/content/drafts.ts b/web/src/lib/content/drafts.tsindex 4d3e2676e..00cd7d203 100644--- a/web/src/lib/content/drafts.ts+++ b/web/src/lib/content/drafts.ts@@ -93,6 +93,8 @@ export interface ContentDraft {pageTypeSource: PageTypeSource;/** The brand's site type, which sets the page types on offer. */siteType: SiteType;+ /** The existing page an optimize draft is for, when Start picked one. */+ sourceUrl: string | null;method: DraftMethod;status: DraftStatus;bodyHtml: string;@@ -118,6 +120,24 @@ export interface DraftStart {source: DraftSource;/** The content to optimize, as HTML. */bodyHtml?: string;+ /** The existing page to optimize instead of writing a new one (overlap check). */+ sourceUrl?: string;+}++/** One of the brand's pages AI already cites for a new page's prompts (CE-015). */+export interface OverlapPage {+ url: string;+ title: string | null;+ promptsInCommon: number;+ citations: number;+ /** Average Google position over the last 28 days; null without Search Console data. */+ gscPosition: number | null;+}++/** Start's overlap check: the prompts a new page would answer, and the pages that answer them. */+export interface ContentOverlap {+ totalPrompts: number;+ pages: OverlapPage[];}/** What New content offers to start a page from. */diff --git a/web/src/lib/content/overlap.test.ts b/web/src/lib/content/overlap.test.tsnew file mode 100644index 000000000..b87c9999a--- /dev/null+++ b/web/src/lib/content/overlap.test.ts@@ -0,0 +1,51 @@+import { describe, expect, it } from 'vitest';+import type { OverlapPage } from './drafts';+import { adviseOverlap, pagePath } from './overlap';++const page = (over: Partial<OverlapPage> = {}): OverlapPage => ({+ url: 'https://acme.example/guides/espresso',+ title: 'Espresso guide',+ promptsInCommon: 3,+ citations: 4,+ gscPosition: 3.5,+ ...over,+});++describe('adviseOverlap', () => {+ it('finds nothing to weigh with no page, or no prompts to check', () => {+ expect(adviseOverlap({ totalPrompts: 4, pages: [] })).toEqual({ verdict: 'none', options: [] });+ expect(adviseOverlap({ totalPrompts: 0, pages: [page()] }).verdict).toBe('none');+ });++ it('recommends optimizing a page that already answers half the prompts or more', () => {+ const top = page({ promptsInCommon: 2 });+ expect(+ adviseOverlap({ totalPrompts: 4, pages: [top, page({ url: 'https://acme.example/b' })] }),+ ).toEqual({+ verdict: 'covered',+ options: [+ { key: 'optimizeInstead', choice: { kind: 'optimize', page: top }, recommended: true },+ { key: 'createAnyway', choice: { kind: 'create' }, recommended: false },+ ],+ });+ });++ it('recommends a new page when the pages answer fewer, with a section as the smaller change', () => {+ const top = page({ promptsInCommon: 1 });+ expect(adviseOverlap({ totalPrompts: 3, pages: [top] })).toEqual({+ verdict: 'partial',+ options: [+ { key: 'createLinked', choice: { kind: 'create' }, recommended: true },+ { key: 'addSection', choice: { kind: 'optimize', page: top }, recommended: false },+ ],+ });+ });+});++describe('pagePath', () => {+ it('names a page by its path, the home page by its host', () => {+ expect(pagePath('https://acme.example/guides/espresso/')).toBe('/guides/espresso');+ expect(pagePath('https://acme.example/')).toBe('acme.example');+ expect(pagePath('not a url')).toBe('not a url');+ });+});diff --git a/web/src/lib/content/overlap.ts b/web/src/lib/content/overlap.tsnew file mode 100644index 000000000..1d57c281b--- /dev/null+++ b/web/src/lib/content/overlap.ts@@ -0,0 +1,65 @@+import type { ContentOverlap, OverlapPage } from './drafts';++/**+ * Start's overlap check (Content Editor CE-015, spec §5): what to recommend+ * when the brand's own pages already answer a new page's prompts.+ *+ * * none: safe to create.+ * * covered: the top page answers at least half the prompts. Optimize it+ * instead; a new page would compete with it for the same prompts.+ * * partial: the pages answer fewer. Create the page and link them to it,+ * so the prompts get one clear page, or add a section to the top one.+ */++/** The share of the prompts a page answers for Start to recommend optimizing it. */+export const COVERS_SHARE = 0.5;++/** What Open editor does: write a new page, or optimize an existing one. */+export type OverlapChoice = { kind: 'create' } | { kind: 'optimize'; page: OverlapPage };++export type OverlapOptionKey = 'optimizeInstead' | 'createAnyway' | 'createLinked' | 'addSection';++export interface OverlapOption {+ key: OverlapOptionKey;+ choice: OverlapChoice;+ recommended: boolean;+}++export interface OverlapAdvice {+ verdict: 'none' | 'covered' | 'partial';+ /** The recommended option first; empty when nothing overlaps. */+ options: OverlapOption[];+}++export function adviseOverlap(overlap: ContentOverlap): OverlapAdvice {+ const top = overlap.pages[0];+ if (!top || overlap.totalPrompts === 0) return { verdict: 'none', options: [] };+ const optimizeTop: OverlapChoice = { kind: 'optimize', page: top };+ if (top.promptsInCommon / overlap.totalPrompts >= COVERS_SHARE) {+ return {+ verdict: 'covered',+ options: [+ { key: 'optimizeInstead', choice: optimizeTop, recommended: true },+ { key: 'createAnyway', choice: { kind: 'create' }, recommended: false },+ ],+ };+ }+ return {+ verdict: 'partial',+ options: [+ { key: 'createLinked', choice: { kind: 'create' }, recommended: true },+ { key: 'addSection', choice: optimizeTop, recommended: false },+ ],+ };+}++/** A page as Start names it: its path on the site, or its host for the home page. */+export function pagePath(url: string): string {+ try {+ const parsed = new URL(url);+ const path = parsed.pathname.replace(/\/+$/, '');+ return path || parsed.host;+ } catch {+ return url;+ }+}diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 81f900103..ce556553f 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -838,6 +838,7 @@ export type Database = {page_type: string | nullpage_type_source: stringprompt_ids: string[]+ source_url: string | nullscore_after: number | nullscore_before: number | nullstatus: string@@ -856,6 +857,7 @@ export type Database = {page_type?: string | nullpage_type_source?: stringprompt_ids?: string[]+ source_url?: string | nullscore_after?: number | nullscore_before?: number | nullstatus?: string@@ -874,6 +876,7 @@ export type Database = {page_type?: string | nullpage_type_source?: stringprompt_ids?: string[]+ source_url?: string | nullscore_after?: number | nullscore_before?: number | nullstatus?: string@@ -3426,6 +3429,16 @@ export type Database = {sent_count: number}[]}+ content_page_overlap: {+ Args: { p_brand_id: string; p_prompt_ids: string[]; p_since?: string }+ Returns: {+ citations: number+ gsc_position: number | null+ page: string+ prompts_in_common: number+ title: string | null+ }[]+ }ga_page_ai_visibility: {Args: { p_brand_id: string; p_since: string }Returns: {