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): lock the passages AI answers quote, and ask before changing one (#1302)
Cihandec33efc0072
diff --git a/supabase/migrations/00216_content_draft_cited_answers.sql b/supabase/migrations/00216_content_draft_cited_answers.sqlnew file mode 100644index 000000000..f7eea5f62--- /dev/null+++ b/supabase/migrations/00216_content_draft_cited_answers.sql@@ -0,0 +1,109 @@+-- ============================================================================+-- 00216: the AI answers that cite a draft's page, for its locked passages+-- ============================================================================+-- Locked passages (Content Editor spec v2 §8, CE-018): a passage of the page+-- AI answers quote today gets a green border and "Cited by ChatGPT 14× and+-- Perplexity 6× in 30 days · keep wording", and a person is warned before+-- changing it.+--+-- content_draft_cited_answers(draft, days) returns the answers of the last N+-- days (30 by default) whose citations name the page the draft optimizes,+-- the latest first: one {platform, response} each, at most 200, each+-- response cut to 12,000 characters. The editor finds the passages they+-- quote. ChatGPT Shopping cards are left out, as the Citations page does.+-- The page is content_drafts.source_url's path on that URL's host, which+-- must be one of the brand's domains, as in content_draft_sources (00211):+-- /pricing on another site of the brand's isn't it. The 200 answers are+-- picked first, and only their responses are read.+--+-- Security definer with an organization check through the draft's brand:+-- citation_urls has no member select policy. A draft without a page gets [];+-- one the caller can't read gets null.+create or replace function public.content_draft_cited_answers(+ p_draft_id uuid,+ p_days integer default 30+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_brand_id uuid;+ v_page_key text;+ v_host text;+ v_since timestamptz;+ v_result jsonb;+begin+ select d.brand_id, lower(public.normalize_page_path(d.source_url)),+ -- Lower-cased first: a scheme may be written HTTPS://.+ regexp_replace(substring(lower(d.source_url) from '^https?://([^/:?#]+)'), '^www\.', '')+ into v_brand_id, v_page_key, v_host+ from public.content_drafts d+ where d.id = p_draft_id;++ if v_brand_id is null or (+ not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = v_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role'+ ) then+ return null;+ end if;+ if p_days is null or p_days < 1 or p_days > 90 then+ raise exception 'content_draft_cited_answers: 1 to 90 days are needed' using errcode = '22023';+ end if;+ -- No page, or a page on a site that isn't the brand's: nothing cites it.+ if v_page_key is null or v_host is null or not exists (+ select 1 from public.brand_domains bd+ where bd.brand_id = v_brand_id+ and lower(regexp_replace(bd.domain, '^www\.', '')) = v_host+ ) then+ return '[]'::jsonb;+ end if;++ v_since := now() - make_interval(days => p_days);++ with+ -- Each answer once, however many of its citations name the page.+ citing as (+ select distinct prc.prompt_result_id+ from public.citation_urls cu+ join public.prompt_result_citations prc+ on prc.url_id = cu.id+ and prc.brand_id = v_brand_id+ and prc.created_at >= v_since+ where cu.domain = v_host+ and lower(public.normalize_page_path(cu.url)) = v_page_key+ ),+ -- The latest 200 first; only their responses are read.+ latest as (+ select pr.id, pr.created_at+ from citing c+ join public.prompt_results pr on pr.id = c.prompt_result_id+ where pr.platform is distinct from 'chatgpt-shopping'+ and pr.response <> ''+ order by pr.created_at desc+ limit 200+ )+ select coalesce(+ jsonb_agg(+ jsonb_build_object('platform', pr.platform, 'response', left(pr.response, 12000))+ order by l.created_at desc+ ),+ '[]'::jsonb+ )+ into v_result+ from latest l+ join public.prompt_results pr on pr.id = l.id;++ return v_result;+end;+$$;++revoke all on function public.content_draft_cited_answers(uuid, integer) from public, anon;+grant execute on function public.content_draft_cited_answers(uuid, integer)+ to authenticated, service_role;diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 08912ed2a..216d43a33 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -30198,3 +30198,116 @@ revoke all on function public.content_draft_versions_prune(timestamptz, integer)grant execute on function public.content_draft_versions_prune(timestamptz, integer)to service_role;+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00216_content_draft_cited_answers.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00216: the AI answers that cite a draft's page, for its locked passages+-- ============================================================================+-- Locked passages (Content Editor spec v2 §8, CE-018): a passage of the page+-- AI answers quote today gets a green border and "Cited by ChatGPT 14× and+-- Perplexity 6× in 30 days · keep wording", and a person is warned before+-- changing it.+--+-- content_draft_cited_answers(draft, days) returns the answers of the last N+-- days (30 by default) whose citations name the page the draft optimizes,+-- the latest first: one {platform, response} each, at most 200, each+-- response cut to 12,000 characters. The editor finds the passages they+-- quote. ChatGPT Shopping cards are left out, as the Citations page does.+-- The page is content_drafts.source_url's path on that URL's host, which+-- must be one of the brand's domains, as in content_draft_sources (00211):+-- /pricing on another site of the brand's isn't it. The 200 answers are+-- picked first, and only their responses are read.+--+-- Security definer with an organization check through the draft's brand:+-- citation_urls has no member select policy. A draft without a page gets [];+-- one the caller can't read gets null.+create or replace function public.content_draft_cited_answers(+ p_draft_id uuid,+ p_days integer default 30+)+returns jsonb+language plpgsql+stable+security definer+set search_path to 'public'+set plan_cache_mode to 'force_custom_plan'+as $$+declare+ v_brand_id uuid;+ v_page_key text;+ v_host text;+ v_since timestamptz;+ v_result jsonb;+begin+ select d.brand_id, lower(public.normalize_page_path(d.source_url)),+ -- Lower-cased first: a scheme may be written HTTPS://.+ regexp_replace(substring(lower(d.source_url) from '^https?://([^/:?#]+)'), '^www\.', '')+ into v_brand_id, v_page_key, v_host+ from public.content_drafts d+ where d.id = p_draft_id;++ if v_brand_id is null or (+ not exists (+ select 1 from public.brands b+ join public.profiles pf on pf.organization_id = b.organization_id+ where b.id = v_brand_id and pf.id = auth.uid()+ ) and auth.role() is distinct from 'service_role'+ ) then+ return null;+ end if;+ if p_days is null or p_days < 1 or p_days > 90 then+ raise exception 'content_draft_cited_answers: 1 to 90 days are needed' using errcode = '22023';+ end if;+ -- No page, or a page on a site that isn't the brand's: nothing cites it.+ if v_page_key is null or v_host is null or not exists (+ select 1 from public.brand_domains bd+ where bd.brand_id = v_brand_id+ and lower(regexp_replace(bd.domain, '^www\.', '')) = v_host+ ) then+ return '[]'::jsonb;+ end if;++ v_since := now() - make_interval(days => p_days);++ with+ -- Each answer once, however many of its citations name the page.+ citing as (+ select distinct prc.prompt_result_id+ from public.citation_urls cu+ join public.prompt_result_citations prc+ on prc.url_id = cu.id+ and prc.brand_id = v_brand_id+ and prc.created_at >= v_since+ where cu.domain = v_host+ and lower(public.normalize_page_path(cu.url)) = v_page_key+ ),+ -- The latest 200 first; only their responses are read.+ latest as (+ select pr.id, pr.created_at+ from citing c+ join public.prompt_results pr on pr.id = c.prompt_result_id+ where pr.platform is distinct from 'chatgpt-shopping'+ and pr.response <> ''+ order by pr.created_at desc+ limit 200+ )+ select coalesce(+ jsonb_agg(+ jsonb_build_object('platform', pr.platform, 'response', left(pr.response, 12000))+ order by l.created_at desc+ ),+ '[]'::jsonb+ )+ into v_result+ from latest l+ join public.prompt_results pr on pr.id = l.id;++ return v_result;+end;+$$;++revoke all on function public.content_draft_cited_answers(uuid, integer) from public, anon;+grant execute on function public.content_draft_cited_answers(uuid, integer)+ to authenticated, service_role;+diff --git a/web/messages/en.json b/web/messages/en.jsonindex 288920dde..6aa63325a 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -3352,6 +3352,16 @@"insert": "Add block"},"markedReady": "Marked ready. Publish it on your site when you're ready.",+ "cited": {+ "platform": "{name} {count}×",+ "label": "✓ Cited by {platforms} in {days} days · keep wording",+ "chip": "{count, plural, one {# cited passage kept} other {# cited passages kept}}",+ "chipHint": "Passages AI answers quote today. Agents never change them, and you're asked before you do.",+ "holdTitle": "AI quotes this passage",+ "holdDescription": "{platforms} quoted this wording in the last {days} days. Changing it may cost the page those citations.",+ "keep": "Keep wording",+ "editAnyway": "Edit anyway"+ },"history": {"button": "History","hint": "Earlier versions of this page, to restore any of them",diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.test.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.test.tsnew file mode 100644index 000000000..b6d4631f4--- /dev/null+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.test.ts@@ -0,0 +1,105 @@+import { Editor } from '@tiptap/core';+import StarterKit from '@tiptap/starter-kit';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { passageKey } from '@/lib/content/cited-passages';+import {+ CITED_EDIT_ALLOWED,+ CitedPassages,+ citedPassagesKey,+ lockedPassageCount,+ type HeldEdit,+} from './_cited-passages';++vi.mock('@/lib/content/cited-passages', async (importOriginal) => {+ const actual = await importOriginal<typeof import('@/lib/content/cited-passages')>();+ return { ...actual, passageKey: vi.fn(actual.passageKey) };+});++if (!('getClientRects' in Range.prototype)) {+ Object.assign(Range.prototype, {+ getClientRects: () => [] as unknown as DOMRectList,+ getBoundingClientRect: () => new DOMRect(),+ });+}++const QUOTED = 'Northwind forecasts each deal from real activity.';+const page = Array.from({ length: 200 }, (_, i) =>+ i === 5 ? `<p>${QUOTED}</p>` : `<p>Paragraph ${i} of the guide.</p>`,+).join('');++let editor: Editor;+const held: HeldEdit[] = [];++/** The position of the paragraph holding this text. */+function paragraphAt(text: string): { from: number; to: number } {+ let found = { from: -1, to: -1 };+ editor.state.doc.descendants((node, pos) => {+ if (node.isTextblock && node.textContent === text)+ found = { from: pos, to: pos + node.nodeSize };+ return !node.isTextblock;+ });+ return found;+}++beforeEach(() => {+ held.length = 0;+ editor = new Editor({+ extensions: [StarterKit, CitedPassages.configure({ onHeld: (edit) => held.push(edit) })],+ content: page,+ });+ editor.view.dispatch(+ editor.state.tr.setMeta(citedPassagesKey, {+ labels: new Map([[passageKey(QUOTED), 'Cited by ChatGPT 3×']]),+ unlocked: new Set(),+ }),+ );+ vi.mocked(passageKey).mockClear();+});++afterEach(() => editor.destroy());++describe('locked passages', () => {+ it('marks the quoted passage and counts it', () => {+ expect(lockedPassageCount(editor.state)).toBe(1);+ expect(editor.view.dom.querySelectorAll('.cited-passage')).toHaveLength(1);+ });++ it('reads only the paragraph a keystroke changes, not the whole page', () => {+ const { from } = paragraphAt('Paragraph 100 of the guide.');+ editor.view.dispatch(editor.state.tr.insertText('x', from + 2));+ // The changed paragraph, before and after; 199 others untouched.+ expect(vi.mocked(passageKey).mock.calls.length).toBeLessThanOrEqual(2);+ expect(lockedPassageCount(editor.state)).toBe(1);+ expect(held).toEqual([]);+ });++ it('keeps the mark when typing in the paragraph just before or after the quoted one', () => {+ for (const neighbour of ['Paragraph 4 of the guide.', 'Paragraph 6 of the guide.']) {+ const { from } = paragraphAt(neighbour);+ editor.view.dispatch(editor.state.tr.insertText('x', from + 2));+ }+ expect(editor.view.dom.querySelectorAll('.cited-passage')).toHaveLength(1);+ expect(lockedPassageCount(editor.state)).toBe(1);+ });++ it('holds a change that takes the quoted passage out, and lets one through when allowed', () => {+ const { from, to } = paragraphAt(QUOTED);+ editor.view.dispatch(editor.state.tr.delete(from, to));+ expect(held).toHaveLength(1);+ expect(paragraphAt(QUOTED).from).toBe(from);++ editor.view.dispatch(editor.state.tr.delete(from, to).setMeta(CITED_EDIT_ALLOWED, true));+ expect(paragraphAt(QUOTED).from).toBe(-1);+ expect(lockedPassageCount(editor.state)).toBe(0);+ });++ it('lets the quoted passage move: it is still in the page', () => {+ const { from, to } = paragraphAt(QUOTED);+ const slice = editor.state.doc.slice(from, to);+ const end = editor.state.doc.content.size;+ editor.view.dispatch(editor.state.tr.insert(end, slice.content).delete(from, to));+ expect(held).toEqual([]);+ expect(paragraphAt(QUOTED).from).toBeGreaterThan(from);+ expect(lockedPassageCount(editor.state)).toBe(1);+ });+});diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.tsnew file mode 100644index 000000000..adec49a4b--- /dev/null+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_cited-passages.ts@@ -0,0 +1,226 @@+import { Extension } from '@tiptap/core';+import type { Node as PmNode } from '@tiptap/pm/model';+import { Plugin, PluginKey, type EditorState, type Transaction } from '@tiptap/pm/state';+import { Mapping } from '@tiptap/pm/transform';+import { Decoration, DecorationSet } from '@tiptap/pm/view';+import { passageKey } from '@/lib/content/cited-passages';++/**+ * Locked passages in the editor (CE-018): a passage AI answers quote today+ * is marked, green with "Cited by … · keep wording" above it, and a change to+ * it waits for the person to say so. Typing in one, deleting it, pasting+ * over it or joining it to another is held and asked about; "Edit anyway"+ * lets it through. Passages are matched by their words, so one changed+ * stops being the quoted one.+ */++/** Meta on a transaction allowed to change locked passages: confirmed, or a whole new page. */+export const CITED_EDIT_ALLOWED = 'citedEditAllowed';++export interface CitedLocks {+ /** The label of each quoted passage, by its key (passageKey). */+ labels: Map<string, string>;+ /** Passages the person chose to edit anyway, by key. */+ unlocked: Set<string>;+}++/** A change held because it touches a locked passage. */+export interface HeldEdit {+ /** The passage it touches. */+ key: string;+ /** The change itself, to apply on "Edit anyway"; none for typing, which is typed again. */+ tr: Transaction | null;+}++/** The plugin's state: the locks, and the passages marked in the page. */+interface CitedState {+ locks: CitedLocks;+ marks: DecorationSet;+}++export const citedPassagesKey = new PluginKey<CitedState>('citedPassages');++const NO_LOCKS: CitedLocks = { labels: new Map(), unlocked: new Set() };++/** The locks the editor has now. */+export function citedLocks(state: EditorState): CitedLocks {+ return citedPassagesKey.getState(state)?.locks ?? NO_LOCKS;+}++/**+ * A textblock's passage key. A change replaces only the nodes it touches;+ * every other block is the same node object in the next doc, so its key is+ * worked out once, not on every keystroke.+ */+const passageKeys = new WeakMap<PmNode, string>();+function keyOf(node: PmNode): string {+ let key = passageKeys.get(node);+ if (key === undefined) {+ key = passageKey(node.textContent);+ passageKeys.set(node, key);+ }+ return key;+}++const isLocked = (key: string, locks: CitedLocks) =>+ locks.labels.has(key) && !locks.unlocked.has(key);++/** The textblocks in or beside these ranges of the doc, by position. */+function blocksIn(doc: PmNode, ranges: [number, number][]): { pos: number; node: PmNode }[] {+ const found = new Map<number, PmNode>();+ for (const [a, b] of ranges) {+ const from = Math.max(0, Math.min(a, b) - 1);+ const to = Math.min(doc.content.size, Math.max(a, b) + 1);+ doc.nodesBetween(from, to, (node, pos) => {+ if (!node.isTextblock) return true;+ found.set(pos, node);+ return false;+ });+ }+ return [...found].map(([pos, node]) => ({ pos, node }));+}++/**+ * What a transaction changed: the ranges of the doc before it and of the+ * doc after it. Every other block is the same in both.+ */+function changedRanges(tr: Transaction): { before: [number, number][]; after: [number, number][] } {+ const maps = tr.mapping.maps;+ const before: [number, number][] = [];+ const after: [number, number][] = [];+ maps.forEach((map, i) => {+ const back = new Mapping(maps.slice(0, i)).invert();+ const ahead = new Mapping(maps.slice(i + 1));+ map.forEach((oldStart, oldEnd, newStart, newEnd) => {+ before.push([back.map(oldStart, -1), back.map(oldEnd, 1)]);+ after.push([ahead.map(newStart, -1), ahead.map(newEnd, 1)]);+ });+ });+ return { before, after };+}++/** How many times each locked passage is among these blocks. */+function lockedCounts(blocks: { node: PmNode }[], locks: CitedLocks): Map<string, number> {+ const counts = new Map<string, number>();+ for (const { node } of blocks) {+ const key = keyOf(node);+ if (isLocked(key, locks)) counts.set(key, (counts.get(key) ?? 0) + 1);+ }+ return counts;+}++/** The mark of a quoted passage, if this block is one. */+function markOf(node: PmNode, pos: number, locks: CitedLocks): Decoration | null {+ const label = locks.labels.get(keyOf(node));+ return label+ ? Decoration.node(pos, pos + node.nodeSize, {+ class: 'cited-passage',+ 'data-cited-label': label,+ })+ : null;+}++/** Every quoted passage in the page, marked: when the locks change. */+function markAll(doc: PmNode, locks: CitedLocks): DecorationSet {+ if (locks.labels.size === 0) return DecorationSet.empty;+ const marks: Decoration[] = [];+ doc.descendants((node, pos) => {+ if (!node.isTextblock) return true;+ const mark = markOf(node, pos, locks);+ if (mark) marks.push(mark);+ return false;+ });+ return DecorationSet.create(doc, marks);+}++/** The locked passage the selection is in or covers, if any. */+function lockedAtSelection(state: EditorState, locks: CitedLocks): string | null {+ const { from, to } = state.selection;+ let found: string | null = null;+ state.doc.nodesBetween(from, to, (node) => {+ if (found) return false;+ if (!node.isTextblock) return true;+ const key = keyOf(node);+ if (isLocked(key, locks)) found = key;+ return false;+ });+ return found;+}++/** How many quoted passages the page has now (the marks the plugin keeps). */+export function lockedPassageCount(state: EditorState): number {+ return citedPassagesKey.getState(state)?.marks.find().length ?? 0;+}++export const CitedPassages = Extension.create<{ onHeld: (edit: HeldEdit) => void }>({+ name: 'citedPassages',++ addOptions() {+ return { onHeld: () => {} };+ },++ addProseMirrorPlugins() {+ const { onHeld } = this.options;+ return [+ new Plugin<CitedState>({+ key: citedPassagesKey,+ state: {+ init: () => ({ locks: NO_LOCKS, marks: DecorationSet.empty }),+ apply: (tr, value) => {+ const locks = tr.getMeta(citedPassagesKey) as CitedLocks | undefined;+ if (locks) return { locks, marks: markAll(tr.doc, locks) };+ if (!tr.docChanged || value.locks.labels.size === 0) return value;+ // The marks move with the page; only the blocks the change+ // touched can gain or lose one.+ let marks = value.marks.map(tr.mapping, tr.doc);+ for (const { pos, node } of blocksIn(tr.doc, changedRanges(tr).after)) {+ // find() also returns the marks that only touch the block's+ // edges (its neighbours'): only its own is taken off.+ const end = pos + node.nodeSize;+ marks = marks.remove(+ marks.find(pos, end).filter((d) => d.from === pos && d.to === end),+ );+ const mark = markOf(node, pos, value.locks);+ if (mark) marks = marks.add(tr.doc, [mark]);+ }+ return { locks: value.locks, marks };+ },+ },+ // A change that would take a locked passage out of the page waits.+ // Only the blocks it touched can change that: their locked passages+ // before and after are compared, so moving one is fine.+ filterTransaction: (tr, state) => {+ if (!tr.docChanged || tr.getMeta(CITED_EDIT_ALLOWED)) return true;+ const locks = citedLocks(state);+ if (locks.labels.size === 0) return true;+ const ranges = changedRanges(tr);+ const before = lockedCounts(blocksIn(tr.before, ranges.before), locks);+ if (before.size === 0) return true;+ const after = lockedCounts(blocksIn(tr.doc, ranges.after), locks);+ for (const [key, n] of before) {+ if ((after.get(key) ?? 0) < n) {+ onHeld({ key, tr });+ return false;+ }+ }+ return true;+ },+ props: {+ decorations: (state) => citedPassagesKey.getState(state)?.marks ?? null,+ // Typed text and deletes go straight to the page, before any+ // transaction: they are held here, on the key.+ handleKeyDown: (view, event) => {+ if (event.isComposing || event.ctrlKey || event.metaKey || event.altKey) return false;+ const edits =+ event.key.length === 1 || event.key === 'Backspace' || event.key === 'Delete';+ if (!edits) return false;+ const key = lockedAtSelection(view.state, citedLocks(view.state));+ if (!key) return false;+ onHeld({ key, tr: null });+ return true;+ },+ },+ }),+ ];+ },+});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 8d0849a11..36bd7dccc 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@@ -1,7 +1,7 @@'use client';-import { useEffect, useRef, useState } from 'react';-import { useTranslations } from 'next-intl';+import { useCallback, useEffect, useRef, useState } from 'react';+import { useFormatter, useTranslations } from 'next-intl';import { toast } from 'sonner';import { EditorContent, useEditor } from '@tiptap/react';import { TextSelection } from '@tiptap/pm/state';@@ -13,13 +13,24 @@ import { ArrowLeft, Check, Copy, Loader2, PanelRight } from 'lucide-react';import { Link } from '@/i18n/navigation';import { Button } from '@/components/ui/button';import { buttonVariants } from '@/components/ui/button-variants';+import {+ Dialog,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from '@/components/ui/dialog';+import { PLATFORM_LABELS } from '@/config/platform-labels';import { DraftMethodBadge, DraftStatusBadge } from '@/components/content/draft-badges';import {+ getCitedPassages,markContentDraftReady,restoreDraftVersion,setContentDraftPageType,} from '@/lib/actions/content-drafts';import { CONTENT_LIBRARY_PATH, type ContentDraft, type PageType } from '@/lib/content/drafts';+import { CITED_DAYS, MAX_PASSAGES, type CitedPassage } from '@/lib/content/cited-passages';import type { DraftVersion } from '@/lib/content/history';import {cleanHtml,@@ -32,6 +43,14 @@ import { JsonLd } from '@/lib/content/json-ld-node';import { detectPageType } from '@/lib/content/page-types';import { cn } from '@/lib/utils';import { DropOverlay, useAttachments } from './_attach';+import {+ CITED_EDIT_ALLOWED,+ CitedPassages,+ citedLocks,+ citedPassagesKey,+ lockedPassageCount,+ type HeldEdit,+} from './_cited-passages';import { HistoryMenu } from './_history-menu';import { HtmlBlock, copyToClipboard } from './_html-block';import { PageTypeMenu } from './_page-type-menu';@@ -54,6 +73,9 @@ const DOCUMENT_CLASS = cn(// Cells and list items hold paragraphs; their margins would pad every row.'[&_td>p]:my-0 [&_th>p]:my-0 [&_li>p]:my-0 prose-li:my-1','[&_.selectedCell]:bg-primary/10 [&_img]:rounded-md [&_.ProseMirror-selectednode]:ring-2 [&_.ProseMirror-selectednode]:ring-ring/50',+ // Locked passages (CE-018): a green line, and what quotes them above.+ '[&_.cited-passage]:rounded-r-md [&_.cited-passage]:border-l-[3px] [&_.cited-passage]:border-emerald-300 [&_.cited-passage]:bg-emerald-50/70 [&_.cited-passage]:py-1 [&_.cited-passage]:pr-2 [&_.cited-passage]:pl-3 dark:[&_.cited-passage]:border-emerald-700 dark:[&_.cited-passage]:bg-emerald-950/30',+ '[&_.cited-passage]:before:mb-0.5 [&_.cited-passage]:before:block [&_.cited-passage]:before:text-[11.5px] [&_.cited-passage]:before:leading-snug [&_.cited-passage]:before:font-medium [&_.cited-passage]:before:tracking-normal [&_.cited-passage]:before:text-emerald-700 [&_.cited-passage]:before:content-[attr(data-cited-label)] dark:[&_.cited-passage]:before:text-emerald-400','[&_.is-editor-empty:first-child]:before:pointer-events-none [&_.is-editor-empty:first-child]:before:float-left [&_.is-editor-empty:first-child]:before:h-0 [&_.is-editor-empty:first-child]:before:text-muted-foreground [&_.is-editor-empty:first-child]:before:content-[attr(data-placeholder)]',);@@ -74,7 +96,9 @@ const DOCUMENT_CLASS = cn(* (Impact, CE-016), its brief and the sources it draws on (CE-007, CE-008)* and the rules it is written to (CE-009); the header's chip says how many* of those rules the page meets now. History keeps earlier versions (the- * imported live page, each Mark ready) and restores any of them (CE-018).+ * imported live page, each Mark ready) and restores any of them; passages+ * AI answers quote today are locked: marked, and changed only after a+ * warning (CE-018).*/export function DraftEditor({ draft }: { draft: ContentDraft }) {const t = useTranslations('contentEditor.editor');@@ -87,6 +111,12 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {const [status, setStatus] = useState(draft.status);const [marking, setMarking] = useState(false);const [restoring, setRestoring] = useState(false);+ const format = useFormatter();+ // A change to a locked passage, waiting for the person (CE-018).+ const [held, setHeld] = useState<HeldEdit | null>(null);+ // What quotes each locked passage, for the warning: "ChatGPT 14× and …".+ const citedBy = useRef(new Map<string, string>());+ const [citedCount, setCitedCount] = useState(0);// A picked page type, or the one the page shows as it is written.const [picked, setPicked] = useState(draft.pageTypeSource === 'user' ? draft.pageType : null);const [detectedType, setDetectedType] = useState(() =>@@ -126,6 +156,12 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {label: (type) => (type ? t('jsonLd', { type }) : t('jsonLdUntyped')),}),HtmlBlock,+ CitedPassages.configure({+ // The first change held is the one asked about: some keyboards+ // still send the text of a key whose keydown was held, and the+ // person can't type while the question is open.+ onHeld: (edit) => setHeld((current) => current ?? edit),+ }),],content: draft.bodyHtml,// Rendered on the client only, so the server's HTML can't disagree with it.@@ -200,6 +236,86 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {attachRef.current = (files) => void attachments.attach(files);});+ /** "ChatGPT 14× and Perplexity 6×": the platforms quoting a passage, the most first. */+ const quotedBy = useCallback(+ (passage: CitedPassage) => {+ const byName = new Map<string, number>();+ for (const { platform, answers } of passage.platforms) {+ const name = PLATFORM_LABELS[platform] ?? platform;+ byName.set(name, (byName.get(name) ?? 0) + answers);+ }+ return format.list(+ [...byName]+ .sort((a, b) => b[1] - a[1])+ .map(([name, count]) => t('cited.platform', { name, count })),+ { type: 'conjunction' },+ );+ },+ [format, t],+ );++ // The page's passages AI answers quote, for a draft of an existing page:+ // read when it opens, and again after a restore.+ const loadCited = useCallback(async () => {+ if (!editor || !draft.sourceUrl) return;+ const texts = new Set<string>();+ editor.state.doc.descendants((node) => {+ if (!node.isTextblock) return true;+ if (node.textContent.trim()) texts.add(node.textContent.slice(0, 5_000));+ return false;+ });+ try {+ const cited = await getCitedPassages(draft.id, [...texts].slice(0, MAX_PASSAGES));+ if (!cited || editor.isDestroyed) return;+ const labels = new Map<string, string>();+ citedBy.current = new Map();+ for (const passage of cited) {+ const platforms = quotedBy(passage);+ citedBy.current.set(passage.key, platforms);+ labels.set(passage.key, t('cited.label', { platforms, days: CITED_DAYS }));+ }+ editor.view.dispatch(+ editor.state.tr.setMeta(citedPassagesKey, { labels, unlocked: new Set() }),+ );+ } catch (err) {+ console.error('Failed to read the cited passages:', err);+ }+ }, [editor, draft.id, draft.sourceUrl, quotedBy, t]);++ useEffect(() => {+ void loadCited();+ // Once per editor: a restore reads them again itself.+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [editor]);++ // The header's count follows the page.+ useEffect(() => {+ if (!editor) return;+ const count = () => setCitedCount(lockedPassageCount(editor.state));+ editor.on('transaction', count);+ return () => {+ editor.off('transaction', count);+ };+ }, [editor]);++ /** "Edit anyway": the passage is the person's to change now. */+ const editAnyway = () => {+ if (!editor || !held) return;+ const locks = citedLocks(editor.state);+ editor.view.dispatch(+ editor.state.tr.setMeta(citedPassagesKey, {+ ...locks,+ unlocked: new Set(locks.unlocked).add(held.key),+ }),+ );+ // A held paste, cut or join goes through; typed text is typed again.+ if (held.tr && held.tr.before.eq(editor.state.doc)) {+ editor.view.dispatch(held.tr.setMeta(CITED_EDIT_ALLOWED, true));+ }+ setHeld(null);+ editor.commands.focus();+ };+// A drag with files over the page shows where they go; one dropped beside// the page (not on its text) goes in at the cursor.const withFiles = (e: React.DragEvent) =>@@ -243,8 +359,9 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {keepImages: true,keepHtmlBlocks: true,});- // Emits an update, so the title and the autosave follow.- editor.commands.setContent(cleaned.html);+ // Emits an update, so the title and the autosave follow. HTML edited by+ // hand may change locked passages: it was the person's to write.+ editor.chain().setMeta(CITED_EDIT_ALLOWED, true).setContent(cleaned.html).run();setHtmlSource(null);toast(cleaned.unsafeRemoved@@ -306,7 +423,11 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {try {await autosave.settle();const restored = await restoreDraftVersion(version.id);- editor.commands.setContent(restored.bodyHtml, { emitUpdate: false });+ editor+ .chain()+ .setMeta(CITED_EDIT_ALLOWED, true)+ .setContent(restored.bodyHtml, { emitUpdate: false })+ .run();// The server has this page now: nothing from before it is still to// save, and saves go on from the restored version.autosave.discard(restored);@@ -315,6 +436,7 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {setPageHtml(html);setDetectedType(detectPageType({ title: restored.title, html }, draft.siteType));toast.success(t('history.restored', { version: label }));+ void loadCited();} catch (err) {console.error('Failed to restore the version:', err);toast.error(t('history.restoreFailed'));@@ -392,6 +514,17 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {{t('rulesChipShort', { met: rules.met, total: rules.total })}</span></button>+ {/* Wide windows only: the page's green lines show them anyway, and+ the header stays one line at 1024px. */}+ {citedCount > 0 && (+ <span+ title={t('cited.chipHint')}+ className="hidden shrink-0 items-center gap-1.5 rounded-full border border-emerald-200 bg-emerald-50 px-2 py-0.5 text-xs font-medium text-emerald-700 xl:inline-flex dark:border-emerald-900 dark:bg-emerald-950 dark:text-emerald-300"+ >+ <span aria-hidden className="h-1.5 w-1.5 rounded-full bg-current" />+ {t('cited.chip', { count: citedCount })}+ </span>+ )}<div className="ml-auto flex shrink-0 items-center gap-3"><SaveIndicator state={autosave.state} onRetry={() => void autosave.retry()} /><HistoryMenu@@ -510,6 +643,33 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {/>)}</div>++ <Dialog open={held !== null} onOpenChange={(open) => !open && setHeld(null)}>+ <DialogContent className="max-w-md">+ <DialogHeader>+ <DialogTitle>{t('cited.holdTitle')}</DialogTitle>+ <DialogDescription>+ {t('cited.holdDescription', {+ platforms: (held && citedBy.current.get(held.key)) ?? '',+ days: CITED_DAYS,+ })}+ </DialogDescription>+ </DialogHeader>+ <DialogFooter>+ <Button variant="outline" onClick={editAnyway}>+ {t('cited.editAnyway')}+ </Button>+ <Button+ onClick={() => {+ setHeld(null);+ editor?.commands.focus();+ }}+ >+ {t('cited.keep')}+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog></div>);}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 6079bc25b..1d2f4b7f8 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@@ -3,8 +3,10 @@ import { createRoot, type Root } from 'react-dom/client';import { NextIntlClientProvider } from 'next-intl';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import messages from '../../../../../../../../messages/en.json';+import type { Editor } from '@tiptap/core';import { RoleProvider } from '@/components/providers/role-provider';import type { ContentDraft } from '@/lib/content/drafts';+import { passageKey } from '@/lib/content/cited-passages';import ContentEditorPage from './page';const getContentDraft = vi.fn();@@ -21,7 +23,9 @@ const getMyRuleSets = vi.fn();const saveMyRuleSet = vi.fn();const getDraftVersions = vi.fn();const restoreDraftVersion = vi.fn();+const getCitedPassages = vi.fn();vi.mock('@/lib/actions/content-drafts', () => ({+ getCitedPassages: (...args: unknown[]) => getCitedPassages(...args),getDraftVersions: (...args: unknown[]) => getDraftVersions(...args),restoreDraftVersion: (...args: unknown[]) => restoreDraftVersion(...args),getMyRuleSets: (...args: unknown[]) => getMyRuleSets(...args),@@ -172,6 +176,7 @@ beforeEach(() => {saveMyRuleSet.mockReset().mockResolvedValue(undefined);getDraftVersions.mockReset().mockResolvedValue([]);restoreDraftVersion.mockReset();+ getCitedPassages.mockReset().mockResolvedValue([]);localStorage.clear();brandState.activeBrandId = 'b1';brandState.setActiveBrand.mockReset();@@ -480,6 +485,146 @@ describe('the content editor', () => {});});+ describe('locked passages', () => {+ const QUOTED =+ 'Northwind forecasts each deal from real activity: emails, meetings and stage changes.';+ const page = draft({+ sourceUrl: 'https://acme.example/forecasting',+ bodyHtml: `<h1>Pipeline forecasting</h1><p>${QUOTED}</p><p>Teams trust it.</p>`,+ });+ const cited = [+ {+ key: passageKey(QUOTED),+ platforms: [+ { platform: 'chatgpt-web', answers: 14 },+ { platform: 'perplexity-web', answers: 6 },+ ],+ },+ ];+ const editorOf = () =>+ (container.querySelector('.ProseMirror') as HTMLElement & { editor: Editor }).editor;+ /** Where the quoted passage's text starts in the page. */+ const quotedAt = () => {+ let at = 0;+ editorOf().state.doc.descendants((node, pos) => {+ if (node.textContent === QUOTED && node.isTextblock) at = pos + 1;+ return !at;+ });+ return at;+ };+ const dialogText = () => document.querySelector('[role="dialog"]')?.textContent ?? null;+ const dialogButton = (name: string) =>+ [...document.querySelectorAll<HTMLButtonElement>('[role="dialog"] button')].find(+ (b) => b.textContent === name,+ )!;++ it('marks the passages AI answers quote, and counts them in the header', async () => {+ getContentDraft.mockResolvedValue(page);+ getCitedPassages.mockResolvedValue(cited);+ await renderPage();++ expect(getCitedPassages).toHaveBeenCalledWith('d1', [+ 'Pipeline forecasting',+ QUOTED,+ 'Teams trust it.',+ ]);+ const marked = container.querySelectorAll('.ProseMirror .cited-passage');+ expect(marked).toHaveLength(1);+ expect(marked[0].textContent).toBe(QUOTED);+ expect(marked[0].getAttribute('data-cited-label')).toBe(+ '✓ Cited by ChatGPT 14× and Perplexity 6× in 30 days · keep wording',+ );+ expect(container.querySelector('header')!.textContent).toContain('1 cited passage kept');+ });++ it('reads nothing for a new page', async () => {+ getContentDraft.mockResolvedValue(draft({ sourceUrl: null }));+ await renderPage();+ expect(getCitedPassages).not.toHaveBeenCalled();+ expect(container.querySelector('.cited-passage')).toBeNull();+ });++ it('asks before typing in one, and keeps the wording', async () => {+ getContentDraft.mockResolvedValue(page);+ getCitedPassages.mockResolvedValue(cited);+ await renderPage();++ await act(async () => {+ editorOf().commands.setTextSelection(quotedAt() + 4);+ editorOf().view.dom.dispatchEvent(+ new KeyboardEvent('keydown', { key: 'x', bubbles: true, cancelable: true }),+ );+ });+ expect(dialogText()).toContain('AI quotes this passage');+ expect(dialogText()).toContain(+ 'ChatGPT 14× and Perplexity 6× quoted this wording in the last 30 days.',+ );++ await act(async () => dialogButton('Keep wording').click());+ expect(dialogText()).toBeNull();+ expect(container.querySelector('.cited-passage')!.textContent).toBe(QUOTED);+ });++ it('holds a change over one until the person says to edit anyway', async () => {+ getContentDraft.mockResolvedValue(page);+ getCitedPassages.mockResolvedValue(cited);+ await renderPage();++ const from = quotedAt();+ await act(async () => {+ editorOf().commands.insertContentAt({ from, to: from + QUOTED.length }, 'Rewritten.');+ });+ expect(container.querySelector('.cited-passage')!.textContent).toBe(QUOTED);+ expect(saveContentDraft).not.toHaveBeenCalled();+ expect(dialogText()).toContain('AI quotes this passage');++ await act(async () => dialogButton('Edit anyway').click());+ expect(dialogText()).toBeNull();+ expect(container.querySelector('.ProseMirror')!.textContent).toBe(+ 'Pipeline forecastingRewritten.Teams trust it.',+ );+ // No longer the quoted wording: no longer locked or counted.+ expect(container.querySelector('.cited-passage')).toBeNull();+ expect(container.querySelector('header')!.textContent).not.toContain('cited passage');+ });++ it('holds a key once, even when its text follows the held keydown', async () => {+ getContentDraft.mockResolvedValue(page);+ getCitedPassages.mockResolvedValue(cited);+ await renderPage();++ const at = quotedAt() + 4;+ await act(async () => {+ editorOf().commands.setTextSelection(at);+ editorOf().view.dom.dispatchEvent(+ new KeyboardEvent('keydown', { key: 'x', bubbles: true, cancelable: true }),+ );+ // The same keystroke's text, as some keyboards still send it.+ editorOf().commands.insertContentAt(at, 'x');+ });+ await act(async () => dialogButton('Edit anyway').click());+ expect(container.querySelector('.cited-passage')!.textContent).toBe(QUOTED);+ });++ it('lets typing through once the person chose to edit it', async () => {+ getContentDraft.mockResolvedValue(page);+ getCitedPassages.mockResolvedValue(cited);+ await renderPage();+ const typeX = () =>+ act(async () => {+ editorOf().commands.setTextSelection(quotedAt() + 4);+ editorOf().view.dom.dispatchEvent(+ new KeyboardEvent('keydown', { key: 'x', bubbles: true, cancelable: true }),+ );+ });++ await typeX();+ await act(async () => dialogButton('Edit anyway').click());+ await typeX();+ expect(dialogText()).toBeNull();+ });+ });+it('shows the page as HTML, and cleans it when it switches back', async () => {getContentDraft.mockResolvedValue(draft({diff --git a/web/src/lib/actions/content-drafts.test.ts b/web/src/lib/actions/content-drafts.test.tsindex 24339cade..7898881b9 100644--- a/web/src/lib/actions/content-drafts.test.ts+++ b/web/src/lib/actions/content-drafts.test.ts@@ -57,6 +57,7 @@ vi.mock('@/lib/supabase/server', () => ({const {createContentDraft,+ getCitedPassages,getContentDraft,getDraftVersions,markContentDraftReady,@@ -402,3 +403,31 @@ describe('getContentDraft', () => {expect(calls).toEqual([]);});});++describe('getCitedPassages', () => {+ const QUOTED = 'Northwind forecasts each deal from real activity: emails, meetings.';++ it('matches the page’s passages against the answers citing it, on the server', async () => {+ rpcResult = {+ data: [+ { platform: 'chatgpt-web', response: `They say: ${QUOTED}` },+ { platform: 'gemini-web', response: 'Unrelated.' },+ ],+ error: null,+ };+ await expect(getCitedPassages(DRAFT, [QUOTED, 'Teams trust it.'])).resolves.toEqual([+ {+ key: 'northwind forecasts each deal from real activity emails meetings',+ platforms: [{ platform: 'chatgpt-web', answers: 1 }],+ },+ ]);+ expect(rpcCalls).toEqual([['content_draft_cited_answers', { p_draft_id: DRAFT, p_days: 30 }]]);+ });++ it('is null for a draft the member can’t read, and refuses a malformed request', async () => {+ rpcResult = { data: null, error: null };+ await expect(getCitedPassages(DRAFT, [QUOTED])).resolves.toBeNull();+ await expect(getCitedPassages('../etc', [QUOTED])).rejects.toThrow('invalid');+ await expect(getCitedPassages(DRAFT, ['x'.repeat(5_001)])).rejects.toThrow('invalid');+ });+});diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.tsindex 304488a14..47dab91b9 100644--- a/web/src/lib/actions/content-drafts.ts+++ b/web/src/lib/actions/content-drafts.ts@@ -43,6 +43,13 @@ import {type RebuildSources,} from '@/lib/content/brief';import type { ContentBrief } from '@/types';+import {+ CITED_DAYS,+ MAX_PASSAGES,+ quotedPassages,+ type CitedAnswer,+ type CitedPassage,+} from '@/lib/content/cited-passages';import {HISTORY_DAYS,HISTORY_LIMIT,@@ -721,6 +728,31 @@ export async function restoreDraftVersion(versionId: string): Promise<RestoredDr};}+const citedInput = z.array(z.string().max(5_000)).max(MAX_PASSAGES);++/**+ * Locked passages (CE-018): which of the page's passages the AI answers that+ * cite it quote, over the last 30 days (00216 content_draft_cited_answers),+ * matched here so the answers stay on the server. Null when the member can't+ * read the draft; none for a draft without a page.+ */+export async function getCitedPassages(+ id: string,+ passages: string[],+): Promise<CitedPassage[] | null> {+ if (!draftId.safeParse(id).success || !citedInput.safeParse(passages).success) {+ throw new Error('invalid');+ }+ const supabase = await createClient();+ const { data, error } = await supabase.rpc('content_draft_cited_answers', {+ p_draft_id: id,+ p_days: CITED_DAYS,+ });+ if (error) throw new Error(error.message);+ if (!data) return null;+ return quotedPassages(passages, data as unknown as CitedAnswer[]);+}+/*** The Sources tab (CE-008): the brand's data a draft draws on, over the last* 30 days (content_draft_sources, 00211), with the person's choices on it.diff --git a/web/src/lib/content/cited-passages.test.ts b/web/src/lib/content/cited-passages.test.tsnew file mode 100644index 000000000..b589f24f1--- /dev/null+++ b/web/src/lib/content/cited-passages.test.ts@@ -0,0 +1,86 @@+import { describe, expect, it } from 'vitest';+import { passageKey, passageWords, quotedPassages } from './cited-passages';++const LEAD =+ 'Northwind forecasts each deal from real activity: emails, meetings and stage changes.';+const SHORT = 'Spreadsheets only show what reps typed.';++describe('passage words', () => {+ it('compares words without case, punctuation or Markdown', () => {+ expect(passageWords('**Northwind** forecasts — each deal’s “real” activity!')).toEqual([+ 'northwind',+ 'forecasts',+ 'each',+ 'deals',+ 'real',+ 'activity',+ ]);+ expect(passageKey('Spreadsheets show what\nreps typed.')).toBe(+ 'spreadsheets show what reps typed',+ );+ });+});++describe('quotedPassages', () => {+ it('finds a passage an answer quotes eight words in a row, per platform', () => {+ const answers = [+ {+ platform: 'chatgpt-web',+ response: 'Northwind **forecasts each deal from real activity: emails**, and calls.',+ },+ {+ platform: 'chatgpt-web',+ response: '- Northwind forecasts each deal from real activity: emails, meetings…',+ },+ {+ platform: 'perplexity-web',+ response:+ 'It reads "each deal from real activity: emails, meetings and stage changes" [1].',+ },+ // Six words in a row is a paraphrase, not the wording kept.+ { platform: 'gemini-web', response: 'It forecasts each deal from real activity.' },+ ];+ expect(+ quotedPassages([LEAD, 'Something else entirely, never quoted anywhere.'], answers),+ ).toEqual([+ {+ key: passageKey(LEAD),+ platforms: [+ { platform: 'chatgpt-web', answers: 2 },+ { platform: 'perplexity-web', answers: 1 },+ ],+ },+ ]);+ });++ it('counts a short passage only when an answer has all of it', () => {+ expect(+ quotedPassages(+ [SHORT],+ [+ {+ platform: 'gemini-web',+ response: 'As they put it: spreadsheets only show what reps typed.',+ },+ { platform: 'gemini-web', response: 'Spreadsheets only show what reps did.' },+ ],+ ),+ ).toEqual([{ key: passageKey(SHORT), platforms: [{ platform: 'gemini-web', answers: 1 }] }]);+ });++ it('leaves out headings and labels too short to call a quote', () => {+ expect(+ quotedPassages(+ ['Pricing plans', 'How it works'],+ [{ platform: 'chatgpt-web', response: 'See pricing plans and how it works.' }],+ ),+ ).toEqual([]);+ });++ it('counts an answer once for a passage it quotes twice, and a passage written twice once', () => {+ const response = `${LEAD} Again: ${LEAD}`;+ expect(quotedPassages([LEAD, LEAD], [{ platform: 'chatgpt-web', response }])).toEqual([+ { key: passageKey(LEAD), platforms: [{ platform: 'chatgpt-web', answers: 1 }] },+ ]);+ });+});diff --git a/web/src/lib/content/cited-passages.ts b/web/src/lib/content/cited-passages.tsnew file mode 100644index 000000000..d8849103f--- /dev/null+++ b/web/src/lib/content/cited-passages.ts@@ -0,0 +1,106 @@+/**+ * Locked passages (Content Editor spec v2 §8, CE-018): the passages of a page+ * AI answers quote today, from the answers that cite it+ * (content_draft_cited_answers, 00216). A person is warned before changing+ * one; agents never change them.+ *+ * A passage is quoted when an answer has a run of QUOTE_WORDS of its words in+ * a row, as written: answers reword most of what they read, so a run that+ * long is the wording kept. A shorter passage counts when the answer has all+ * of it. Words compare without case, punctuation or Markdown.+ */++/** The window the answers are read over. */+export const CITED_DAYS = 30;++/** A run of this many words in a row, as the page has them, is a quote. */+export const QUOTE_WORDS = 8;++/** Passages shorter than this are too common to call a quote (headings, labels). */+export const MIN_PASSAGE_WORDS = 6;++/** The most passages a page sends to be checked. */+export const MAX_PASSAGES = 1000;++/** An answer that cites the page. */+export interface CitedAnswer {+ platform: string;+ response: string;+}++/** A passage AI answers quote, by its key, with the answers quoting it per platform. */+export interface CitedPassage {+ key: string;+ /** Platforms by answers quoting the passage, the most first. */+ platforms: { platform: string; answers: number }[];+}++/** A text's words, lowercase, without punctuation or Markdown. */+export function passageWords(text: string): string[] {+ return text+ .toLowerCase()+ .normalize('NFKC')+ .replace(/[’']/g, '')+ .split(/[^\p{L}\p{N}]+/u)+ .filter(Boolean);+}++/** The passage as it is compared: its words, one space apart. */+export function passageKey(text: string): string {+ return passageWords(text).join(' ');+}++/** Each run of n words in a row. */+function runs(words: string[], n: number): string[] {+ const out: string[] = [];+ for (let i = 0; i + n <= words.length; i++) out.push(words.slice(i, i + n).join(' '));+ return out;+}++/**+ * The passages the answers quote, each with how many answers on each+ * platform quote it. Passages are matched by key, so one written twice is+ * one passage.+ */+export function quotedPassages(passages: string[], answers: CitedAnswer[]): CitedPassage[] {+ // Each passage's runs, and which passages each run belongs to; only these+ // runs are looked for in the answers.+ const byRun = new Map<string, Set<string>>();+ const lengths = new Set<number>();+ for (const text of passages.slice(0, MAX_PASSAGES)) {+ const words = passageWords(text);+ if (words.length < MIN_PASSAGE_WORDS) continue;+ const key = words.join(' ');+ const n = Math.min(QUOTE_WORDS, words.length);+ lengths.add(n);+ for (const run of runs(words, n)) {+ const keys = byRun.get(run) ?? new Set<string>();+ keys.add(key);+ byRun.set(run, keys);+ }+ }+ if (byRun.size === 0) return [];++ const counts = new Map<string, Map<string, number>>();+ for (const answer of answers) {+ const words = passageWords(answer.response);+ const quoted = new Set<string>();+ for (const n of lengths) {+ for (const run of runs(words, n)) {+ for (const key of byRun.get(run) ?? []) quoted.add(key);+ }+ }+ for (const key of quoted) {+ const byPlatform = counts.get(key) ?? new Map<string, number>();+ byPlatform.set(answer.platform, (byPlatform.get(answer.platform) ?? 0) + 1);+ counts.set(key, byPlatform);+ }+ }++ return [...counts].map(([key, byPlatform]) => ({+ key,+ platforms: [...byPlatform]+ .map(([platform, n]) => ({ platform, answers: n }))+ .sort((a, b) => b.answers - a.answers || a.platform.localeCompare(b.platform)),+ }));+}diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 30992b058..b7c3da3bb 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -3552,6 +3552,10 @@ export type Database = {Args: { p_days?: number; p_draft_id: string }Returns: Json}+ content_draft_cited_answers: {+ Args: { p_days?: number; p_draft_id: string }+ Returns: Json+ }content_draft_member: { Args: { p_draft_id: string }; Returns: boolean }content_draft_restore: { Args: { p_version_id: string }; Returns: Json }content_draft_snapshot: {