Skip to content

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.

feat(content): let the Content Agent suggest fixes for the open rules (#1304)

Cihan10fa7bbff318
diff --git a/server/src/lib/content-agent.js b/server/src/lib/content-agent.js
new file mode 100644
index 000000000..936f1fbad
--- /dev/null
+++ b/server/src/lib/content-agent.js
@@ -0,0 +1,344 @@
+import { randomUUID } from 'crypto';
+import { generateObject } from 'ai';
+import * as cheerio from 'cheerio';
+import { z } from 'zod';
+import supabaseAdmin from '../config/supabase.js';
+import { resolveModel } from './ai-provider.js';
+import { contentBriefModelId, recordUsage } from './content-brief.js';
+import { getLanguageName } from './languages.js';
+import { enforceImproveQuota } from './plan-guard.js';
+
+/**
+ * Fix open rules with agent (Content Editor CE-018, spec §8): the Content
+ * Agent reads the draft as saved and the rules it doesn't meet yet, and
+ * leaves suggestions for a person to review (content_draft_suggestions,
+ * 00217) — never changes to the page itself. A suggestion replaces words in
+ * one passage, or adds blocks after one, and names the rules it fixes. The
+ * passages AI answers quote today (locked, CE-018) are never changed. It is
+ * metered from the organization's AI usage, as the brief is.
+ */
+
+/** The most suggestions one run leaves. */
+export const MAX_AGENT_SUGGESTIONS = 8;
+
+/** How long the model may take: the editor waits for it. */
+const CALL_TIMEOUT_MS = 60_000;
+
+/** The longest passage the agent may change: keepSuggestions skips longer ones. */
+const MAX_PASSAGE_CHARS = 5_000;
+
+/**
+ * How much of the page the model reads, in characters: a very long page is
+ * sent up to here, so one run can't spend far past the AI usage budget.
+ */
+export const MAX_PROMPT_PASSAGE_CHARS = 60_000;
+
+/** How many of a draft's newest suggestions are read to find its latest batch. */
+const LATEST_BATCH_ROWS = 50;
+
+/** What the editor sends: the rules still open, and the passages it keeps locked. */
+export const fixRulesInput = z.object({
+ rules: z
+ .array(
+ z.object({
+ key: z.string().trim().min(1).max(60),
+ name: z.string().trim().min(1).max(200),
+ /** What the rule asks and how the page reads now: "aim 3,000 characters · 2,610". */
+ detail: z.string().trim().max(500).default(''),
+ }),
+ )
+ .min(1)
+ .max(40),
+ /** Rules in the person's own words, to follow as well. */
+ textRules: z.array(z.string().trim().min(1).max(300)).max(50).default([]),
+ /** The locked passages, by passageKey. */
+ locked: z.array(z.string().max(5000)).max(500).default([]),
+});
+
+const suggestionsSchema = z.object({
+ suggestions: z
+ .array(
+ z.object({
+ kind: z.enum(['replace', 'insert']),
+ passage: z
+ .number()
+ .int()
+ .describe('The number of the passage it changes, or the one it adds blocks after.'),
+ before: z
+ .string()
+ .describe(
+ 'replace: the words of that passage to change, copied exactly as written; insert: an empty string.',
+ ),
+ after: z
+ .string()
+ .describe(
+ 'replace: the new words, as plain text; insert: the blocks to add, as simple HTML (h2, h3, p, ul, ol, li, strong, em).',
+ ),
+ reason: z.string().describe('What the change does, in a few words.'),
+ rules: z.array(z.string()).describe('The keys of the rules it fixes.'),
+ }),
+ )
+ .max(MAX_AGENT_SUGGESTIONS),
+});
+
+const SYSTEM_PROMPT = `You are the Content Agent for Answer Engine Optimization (AEO). You improve one web page so it meets its writing rules, by suggesting small changes that a person reviews.
+
+Rules for your suggestions:
+- Fix only the rules listed. Each suggestion fixes one or more of them; put their keys in "rules".
+- A replace changes words inside one passage: copy "before" exactly as that passage has it (a phrase or a sentence), and write "after" as plain text that fits in its place.
+- An insert adds new blocks after a passage, as simple HTML (h2, h3, p, ul, ol, li, strong, em).
+- Never change a passage marked LOCKED: AI assistants quote its wording today. You may insert after it.
+- Use only facts from the page and the brand. Never invent numbers, prices, dates, names, sources or quotes. When a rule needs a fact you don't have, write a placeholder in square brackets, e.g. [figure, source, year].
+- Keep the page's voice and change as little as each rule needs. At most ${MAX_AGENT_SUGGESTIONS} suggestions.
+- "reason" says in a few words what the change does and why, e.g. "add a dated data point (rule)".
+- Write in the language asked for.`;
+
+/**
+ * A text's words as the editor compares passages, one space apart: the same
+ * as passageKey in web/src/lib/content/cited-passages.ts.
+ */
+export function passageKey(text) {
+ return String(text)
+ .toLowerCase()
+ .normalize('NFKC')
+ .replace(/[’']/g, '')
+ .split(/[^\p{L}\p{N}]+/u)
+ .filter(Boolean)
+ .join(' ');
+}
+
+/** The page's passages as the editor reads them: headings and paragraphs, in order. */
+export function draftPassages(html) {
+ const $ = cheerio.load(html || '', null, false);
+ const passages = [];
+ $('h1, h2, h3, h4, h5, h6, p').each((_, el) => {
+ const text = $(el).text();
+ if (text.trim()) passages.push({ tag: el.tagName.toLowerCase(), text });
+ });
+ return passages;
+}
+
+const INSERT_TAGS = new Set([
+ 'h2',
+ 'h3',
+ 'h4',
+ 'p',
+ 'ul',
+ 'ol',
+ 'li',
+ 'strong',
+ 'em',
+ 'b',
+ 'i',
+ 'br',
+]);
+
+/**
+ * An insert's HTML kept to the blocks the editor takes from an agent, with
+ * no attributes; anything else is unwrapped to its text. The editor checks
+ * it again where it is inserted, and every save checks the page.
+ */
+export function cleanInsertHtml(html) {
+ const $ = cheerio.load(html || '', null, false);
+ $('script, style, iframe, object, embed, noscript, template, head, title').remove();
+ for (const el of $('*').get().reverse()) {
+ if (!INSERT_TAGS.has(el.tagName?.toLowerCase())) {
+ $(el).replaceWith($(el).contents());
+ continue;
+ }
+ for (const name of Object.keys(el.attribs ?? {})) $(el).removeAttr(name);
+ }
+ return $.html().trim();
+}
+
+const textOf = (html) =>
+ cheerio
+ .load(html || '', null, false)
+ .root()
+ .text();
+
+/**
+ * The model's suggestions the editor can apply: on a passage that exists,
+ * a replace's words found in it and never on a locked passage, an insert's
+ * HTML cleaned and not empty; the rules named kept to the ones sent, and
+ * each change once.
+ */
+export function keepSuggestions(raw, passages, lockedKeys, ruleKeys) {
+ const kept = [];
+ const seen = new Set();
+ for (const s of raw) {
+ if (kept.length >= MAX_AGENT_SUGGESTIONS) break;
+ const passage = passages[s.passage - 1];
+ const reason = s.reason.trim().slice(0, 300);
+ if (!passage || !reason || passage.text.length > MAX_PASSAGE_CHARS) continue;
+ const rules = [...new Set(s.rules.filter((key) => ruleKeys.has(key)))];
+ if (s.kind === 'replace') {
+ if (lockedKeys.has(passageKey(passage.text))) continue;
+ const before = s.before.trim();
+ const after = textOf(s.after).trim();
+ if (!before || !after || before === after || after.length > 2000) continue;
+ if (!passage.text.includes(before)) continue;
+ const id = `replace:${passage.text}:${before}`;
+ if (seen.has(id)) continue;
+ seen.add(id);
+ kept.push({
+ kind: 'replace',
+ block_text: passage.text,
+ before_text: before,
+ after_text: after,
+ reason,
+ rule_keys: rules,
+ });
+ } else {
+ const html = cleanInsertHtml(s.after);
+ if (!textOf(html).trim() || html.length > 20_000) continue;
+ const id = `insert:${passage.text}:${html}`;
+ if (seen.has(id)) continue;
+ seen.add(id);
+ kept.push({
+ kind: 'insert',
+ block_text: passage.text,
+ before_text: '',
+ after_text: html,
+ reason,
+ rule_keys: rules,
+ });
+ }
+ }
+ return kept;
+}
+
+/**
+ * The passages the model reads: the ones it may change (up to
+ * MAX_PASSAGE_CHARS each), in order, until MAX_PROMPT_PASSAGE_CHARS.
+ * `more` says the page goes on past them.
+ */
+export function promptPassages(passages) {
+ const sent = [];
+ let chars = 0;
+ for (const passage of passages) {
+ if (passage.text.length > MAX_PASSAGE_CHARS) continue;
+ if (chars + passage.text.length > MAX_PROMPT_PASSAGE_CHARS) {
+ return { passages: sent, more: true };
+ }
+ chars += passage.text.length;
+ sent.push(passage);
+ }
+ return { passages: sent, more: false };
+}
+
+/** What the model is told: the brand, the page's passages, and the rules to fix. */
+export function buildFixPrompt({ brand, draft, passages, more = false, lockedKeys, input }) {
+ const lines = passages.map((p, i) => {
+ const locked = lockedKeys.has(passageKey(p.text)) ? ', LOCKED' : '';
+ return `[${i + 1}] (${p.tag}${locked}) ${p.text}`;
+ });
+ if (more) lines.push('(The page goes on; suggest changes only to the passages above.)');
+ const rules = input.rules.map((r) => `- ${r.key}: ${r.name}${r.detail ? ` — ${r.detail}` : ''}`);
+ const own = input.textRules.map((text) => `- ${text}`);
+ return `Brand: ${brand?.name || 'Unknown'}
+Industry: ${brand?.industry || 'Not specified'}
+Description: ${brand?.description || 'N/A'}
+
+Page: ${draft.title || 'Untitled'}
+Page type: ${draft.page_type || 'not set'}
+
+Rules the page doesn't meet yet (key: rule — what it asks and how the page reads now):
+${rules.join('\n')}
+${own.length ? `\nThe writer's own rules, to follow in every change:\n${own.join('\n')}\n` : ''}
+The page, passage by passage:
+${lines.join('\n')}
+
+Write in ${getLanguageName(brand?.language)}.`;
+}
+
+/**
+ * Whether the draft's latest batch still has suggestions nobody decided:
+ * the editor shows only that batch, so a new one would hide them (and Undo
+ * on the ones accepted).
+ */
+export async function hasOpenSuggestions(draftId) {
+ const { data, error } = await supabaseAdmin
+ .from('content_draft_suggestions')
+ .select('batch_id, status')
+ .eq('draft_id', draftId)
+ .order('created_at', { ascending: false })
+ .limit(LATEST_BATCH_ROWS);
+ if (error) throw error;
+ const latest = data?.[0]?.batch_id;
+ return (data ?? []).some((row) => row.batch_id === latest && row.status === 'open');
+}
+
+/**
+ * Runs the agent on a draft: reads it and its brand, charges the AI usage
+ * budget, asks the model, and keeps the suggestions the editor can apply as
+ * one new batch, linked to the draft's Action Center action if it has one.
+ * Throws with `.status` 404 for a draft outside the brand, 409 for a
+ * published draft or one whose suggestions are still open, 422 for an empty
+ * page, PlanLimitError once the budget is spent — all before charging.
+ */
+export async function fixDraftRules({ draftId, brandId, input }) {
+ const { data: draft, error: draftError } = await supabaseAdmin
+ .from('content_drafts')
+ .select('id, brand_id, title, body_html, page_type, action_id, status')
+ .eq('id', draftId)
+ .maybeSingle();
+ if (draftError) throw draftError;
+ if (!draft || draft.brand_id !== brandId) {
+ throw Object.assign(new Error('Draft not found'), { status: 404 });
+ }
+ if (draft.status === 'published') {
+ throw Object.assign(new Error('The draft is published'), { status: 409, code: 'published' });
+ }
+ const { passages, more } = promptPassages(draftPassages(draft.body_html));
+ if (!passages.length) {
+ throw Object.assign(new Error('The page has no text to fix'), { status: 422, code: 'empty' });
+ }
+ if (await hasOpenSuggestions(draftId)) {
+ throw Object.assign(new Error('Decide the open suggestions first'), {
+ status: 409,
+ code: 'open_suggestions',
+ });
+ }
+
+ const { data: brand, error: brandError } = await supabaseAdmin
+ .from('brands')
+ .select('name, industry, description, organization_id, language')
+ .eq('id', brandId)
+ .single();
+ if (brandError) throw brandError;
+
+ const modelId = contentBriefModelId();
+ await enforceImproveQuota(brand.organization_id, modelId);
+
+ const lockedKeys = new Set(input.locked.map(passageKey));
+ const { object, usage } = await generateObject({
+ model: resolveModel(modelId),
+ schema: suggestionsSchema,
+ system: SYSTEM_PROMPT,
+ prompt: buildFixPrompt({ brand, draft, passages, more, lockedKeys, input }),
+ abortSignal: AbortSignal.timeout(CALL_TIMEOUT_MS),
+ });
+ await recordUsage({ organizationId: brand.organization_id, draftId, modelId, usage });
+
+ const rows = keepSuggestions(
+ object.suggestions,
+ passages,
+ lockedKeys,
+ new Set(input.rules.map((r) => r.key)),
+ );
+ if (!rows.length) return { count: 0, batchId: null };
+
+ const batchId = randomUUID();
+ const { error: insertError } = await supabaseAdmin.from('content_draft_suggestions').insert(
+ rows.map((row, position) => ({
+ ...row,
+ draft_id: draftId,
+ batch_id: batchId,
+ action_id: draft.action_id ?? null,
+ position,
+ })),
+ );
+ if (insertError) throw insertError;
+ return { count: rows.length, batchId };
+}
diff --git a/server/src/lib/content-agent.test.js b/server/src/lib/content-agent.test.js
new file mode 100644
index 000000000..d835439d3
--- /dev/null
+++ b/server/src/lib/content-agent.test.js
@@ -0,0 +1,319 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+/**
+ * Fix open rules with agent (Content Editor CE-018): the Content Agent's
+ * suggestions on the open rules, kept only where the editor can apply them,
+ * never on a locked passage, and metered from the AI usage budget.
+ */
+
+const BRAND = '33333333-3333-3333-3333-333333333333';
+const DRAFT = '77777777-7777-7777-7777-777777777777';
+
+let rows;
+let writes;
+const generateObject = vi.fn();
+const enforceImproveQuota = vi.fn();
+
+vi.mock('../config/supabase.js', () => ({
+ default: {
+ from: (table) => {
+ const result = () => ({ data: rows[table] ?? null, error: null });
+ const chain = {
+ select: () => chain,
+ eq: () => chain,
+ order: () => chain,
+ limit: async () => result(),
+ insert: async (value) => (writes.push({ table, value }), { error: null }),
+ maybeSingle: async () => result(),
+ single: async () => result(),
+ };
+ return chain;
+ },
+ },
+}));
+vi.mock('ai', () => ({ generateObject: (...args) => generateObject(...args) }));
+vi.mock('./ai-provider.js', () => ({ resolveModel: vi.fn((id) => `model:${id}`) }));
+vi.mock('./plan-guard.js', async (importOriginal) => ({
+ ...(await importOriginal()),
+ enforceImproveQuota: (...args) => enforceImproveQuota(...args),
+}));
+vi.mock('./logger.js', () => ({ logger: { error: vi.fn() } }));
+
+const {
+ buildFixPrompt,
+ cleanInsertHtml,
+ draftPassages,
+ fixDraftRules,
+ keepSuggestions,
+ MAX_PROMPT_PASSAGE_CHARS,
+ passageKey,
+ promptPassages,
+} = await import('./content-agent.js');
+const { PlanLimitError } = await import('./plan-guard.js');
+
+const BODY =
+ '<h1>How to brew pour-over coffee</h1><p>Pour-over coffee tastes better than other methods.</p>' +
+ '<ul><li><p>A dripper and a filter</p></li></ul><p>Use good coffee and hot water.</p>';
+
+const input = (over = {}) => ({
+ rules: [
+ { key: 'dated', name: 'A dated data point', detail: 'missing' },
+ { key: 'faq', name: 'FAQ with 5 questions', detail: 'missing' },
+ ],
+ textRules: ['Say “we” for the brand'],
+ locked: [passageKey('Pour-over coffee tastes better than other methods.')],
+ ...over,
+});
+
+const suggestion = (over = {}) => ({
+ kind: 'replace',
+ passage: 4,
+ before: 'good coffee and hot water',
+ after: '15 g of coffee and 250 g of water at 94 °C ([source, year])',
+ reason: 'add a dated data point (rule)',
+ rules: ['dated', 'made-up'],
+ ...over,
+});
+
+beforeEach(() => {
+ writes = [];
+ rows = {
+ content_drafts: {
+ id: DRAFT,
+ brand_id: BRAND,
+ title: 'How to brew pour-over coffee',
+ body_html: BODY,
+ page_type: 'guide',
+ action_id: 'act-1',
+ status: 'draft',
+ },
+ brands: { name: 'Acme Coffee', organization_id: 'org-1', language: 'en' },
+ };
+ generateObject.mockReset();
+ enforceImproveQuota.mockReset().mockResolvedValue(undefined);
+});
+
+describe('the page as the agent reads it', () => {
+ it('reads headings and paragraphs in order, as the editor does', () => {
+ expect(draftPassages(BODY)).toEqual([
+ { tag: 'h1', text: 'How to brew pour-over coffee' },
+ { tag: 'p', text: 'Pour-over coffee tastes better than other methods.' },
+ { tag: 'p', text: 'A dripper and a filter' },
+ { tag: 'p', text: 'Use good coffee and hot water.' },
+ ]);
+ });
+
+ it('compares passages by their words, as the editor does', () => {
+ expect(passageKey('Pour-over’s 94 °C — “Fresh”.')).toBe('pour overs 94 c fresh');
+ });
+
+ it('marks locked passages and lists the rules for the model', () => {
+ const passages = draftPassages(BODY);
+ const prompt = buildFixPrompt({
+ brand: { name: 'Acme Coffee', language: 'en' },
+ draft: rows.content_drafts,
+ passages,
+ lockedKeys: new Set(input().locked),
+ input: input(),
+ });
+ expect(prompt).toContain('[2] (p, LOCKED) Pour-over coffee tastes better than other methods.');
+ expect(prompt).toContain('[4] (p) Use good coffee and hot water.');
+ expect(prompt).toContain('- dated: A dated data point — missing');
+ expect(prompt).toContain('- Say “we” for the brand');
+ expect(prompt).toContain('Write in English.');
+ expect(prompt).not.toContain('The page goes on');
+ });
+
+ it('sends a long page only up to its cap, and no passage too long to change', () => {
+ const long = { tag: 'p', text: 'x'.repeat(5_001) };
+ const passage = { tag: 'p', text: 'y'.repeat(4_000) };
+ const many = Array.from({ length: 20 }, () => passage);
+ const sent = promptPassages([long, ...many]);
+ expect(sent.more).toBe(true);
+ expect(sent.passages).toHaveLength(MAX_PROMPT_PASSAGE_CHARS / 4_000);
+ expect(sent.passages).not.toContain(long);
+ expect(promptPassages(draftPassages(BODY))).toEqual({
+ passages: draftPassages(BODY),
+ more: false,
+ });
+ const prompt = buildFixPrompt({
+ brand: { name: 'Acme Coffee', language: 'en' },
+ draft: rows.content_drafts,
+ ...sent,
+ lockedKeys: new Set(),
+ input: input(),
+ });
+ expect(prompt).toContain('(The page goes on; suggest changes only to the passages above.)');
+ });
+});
+
+describe('keepSuggestions', () => {
+ const passages = draftPassages(BODY);
+ const locked = new Set(input().locked);
+ const ruleKeys = new Set(['dated', 'faq']);
+
+ it('keeps a replace whose words are in its passage, with the rules sent', () => {
+ expect(keepSuggestions([suggestion()], passages, locked, ruleKeys)).toEqual([
+ {
+ kind: 'replace',
+ block_text: 'Use good coffee and hot water.',
+ before_text: 'good coffee and hot water',
+ after_text: '15 g of coffee and 250 g of water at 94 °C ([source, year])',
+ reason: 'add a dated data point (rule)',
+ rule_keys: ['dated'],
+ },
+ ]);
+ });
+
+ it('drops one on a locked passage, words not in the passage, or no passage', () => {
+ expect(
+ keepSuggestions(
+ [
+ suggestion({ passage: 2, before: 'tastes better', after: 'is brighter' }),
+ suggestion({ before: 'cold water' }),
+ suggestion({ passage: 9 }),
+ suggestion({ after: 'good coffee and hot water' }),
+ ],
+ passages,
+ locked,
+ ruleKeys,
+ ),
+ ).toEqual([]);
+ });
+
+ it('keeps an insert after a locked passage, its HTML cleaned', () => {
+ const [kept] = keepSuggestions(
+ [
+ suggestion({
+ kind: 'insert',
+ passage: 2,
+ before: '',
+ after:
+ '<h2 class="x">FAQ</h2><p onclick="y()">How long? <a href="z">3 min</a></p><script>bad()</script>',
+ reason: 'add FAQ block (rule)',
+ rules: ['faq'],
+ }),
+ ],
+ passages,
+ locked,
+ ruleKeys,
+ );
+ expect(kept).toMatchObject({
+ kind: 'insert',
+ block_text: 'Pour-over coffee tastes better than other methods.',
+ before_text: '',
+ after_text: '<h2>FAQ</h2><p>How long? 3 min</p>',
+ rule_keys: ['faq'],
+ });
+ });
+
+ it('keeps each change once', () => {
+ expect(keepSuggestions([suggestion(), suggestion()], passages, locked, ruleKeys)).toHaveLength(
+ 1,
+ );
+ });
+});
+
+describe('cleanInsertHtml', () => {
+ it('keeps simple blocks without attributes and unwraps the rest', () => {
+ expect(
+ cleanInsertHtml('<div><h3 style="x">Q</h3><ol><li><em>a</em></li></ol><img src=x></div>'),
+ ).toBe('<h3>Q</h3><ol><li><em>a</em></li></ol>');
+ });
+});
+
+describe('fixDraftRules', () => {
+ it('charges the budget, asks the model and keeps one batch for review', async () => {
+ generateObject.mockResolvedValue({
+ object: {
+ suggestions: [
+ suggestion(),
+ suggestion({ passage: 2, before: 'tastes better', after: 'is brighter' }),
+ ],
+ },
+ usage: { inputTokens: 900, outputTokens: 200 },
+ });
+
+ const result = await fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() });
+
+ expect(enforceImproveQuota).toHaveBeenCalledWith('org-1', expect.any(String));
+ expect(generateObject).toHaveBeenCalledWith(
+ expect.objectContaining({ model: expect.stringMatching(/^model:/) }),
+ );
+ expect(result).toEqual({ count: 1, batchId: expect.any(String) });
+ const usage = writes.find((w) => w.table === 'prompt_improve_usage');
+ expect(usage.value).toMatchObject({ organization_id: 'org-1', content_draft_id: DRAFT });
+ const batch = writes.find((w) => w.table === 'content_draft_suggestions');
+ expect(batch.value).toEqual([
+ expect.objectContaining({
+ draft_id: DRAFT,
+ batch_id: result.batchId,
+ action_id: 'act-1',
+ position: 0,
+ kind: 'replace',
+ }),
+ ]);
+ });
+
+ it('keeps nothing when the agent found nothing it could change', async () => {
+ generateObject.mockResolvedValue({ object: { suggestions: [] }, usage: {} });
+ await expect(
+ fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() }),
+ ).resolves.toEqual({ count: 0, batchId: null });
+ expect(writes.some((w) => w.table === 'content_draft_suggestions')).toBe(false);
+ });
+
+ it('refuses another brand’s draft or an empty page before charging anything', async () => {
+ await expect(
+ fixDraftRules({
+ draftId: DRAFT,
+ brandId: '44444444-4444-4444-4444-444444444444',
+ input: input(),
+ }),
+ ).rejects.toMatchObject({ status: 404 });
+ rows.content_drafts.body_html = '<p> </p>';
+ await expect(
+ fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() }),
+ ).rejects.toMatchObject({ status: 422, code: 'empty' });
+ expect(enforceImproveQuota).not.toHaveBeenCalled();
+ expect(generateObject).not.toHaveBeenCalled();
+ });
+
+ it('refuses a published draft, or one whose latest batch is still open, before charging', async () => {
+ rows.content_drafts.status = 'published';
+ await expect(
+ fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() }),
+ ).rejects.toMatchObject({ status: 409, code: 'published' });
+
+ rows.content_drafts.status = 'ready';
+ // Newest first: the latest batch b2 has an open row.
+ rows.content_draft_suggestions = [
+ { batch_id: 'b2', status: 'accepted' },
+ { batch_id: 'b2', status: 'open' },
+ { batch_id: 'b1', status: 'open' },
+ ];
+ await expect(
+ fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() }),
+ ).rejects.toMatchObject({ status: 409, code: 'open_suggestions' });
+ expect(enforceImproveQuota).not.toHaveBeenCalled();
+ expect(generateObject).not.toHaveBeenCalled();
+
+ // An older batch left open doesn't count: the editor shows the latest.
+ rows.content_draft_suggestions = [
+ { batch_id: 'b2', status: 'rejected' },
+ { batch_id: 'b1', status: 'open' },
+ ];
+ generateObject.mockResolvedValue({ object: { suggestions: [] }, usage: {} });
+ await expect(
+ fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() }),
+ ).resolves.toEqual({ count: 0, batchId: null });
+ });
+
+ it('stops at the budget', async () => {
+ enforceImproveQuota.mockRejectedValue(new PlanLimitError('AI usage is spent', 'starter'));
+ await expect(
+ fixDraftRules({ draftId: DRAFT, brandId: BRAND, input: input() }),
+ ).rejects.toBeInstanceOf(PlanLimitError);
+ expect(generateObject).not.toHaveBeenCalled();
+ });
+});
diff --git a/server/src/lib/content-brief.js b/server/src/lib/content-brief.js
index db15b8533..0d102c68f 100644
--- a/server/src/lib/content-brief.js
+++ b/server/src/lib/content-brief.js
@@ -275,7 +275,8 @@ ${list(sources.pages.map((p) => `${p.url} (${p.kind})`))}
${sources.headings.length ? `The page's headings now:\n${list(sources.headings)}\n\n` : ''}Write the brief in ${getLanguageName(brand?.language)}.`;
}
-async function recordUsage({ organizationId, draftId, modelId, usage }) {
+/** Records an editor AI call on the AI usage budget, with the draft it was for. */
+export async function recordUsage({ organizationId, draftId, modelId, usage }) {
const { error } = await supabaseAdmin.from('prompt_improve_usage').insert({
organization_id: organizationId,
content_draft_id: draftId,
diff --git a/server/src/routes/content-drafts.js b/server/src/routes/content-drafts.js
index 6d98f98a4..e3275c690 100644
--- a/server/src/routes/content-drafts.js
+++ b/server/src/routes/content-drafts.js
@@ -1,18 +1,21 @@
import { Router } from 'express';
import { z } from 'zod';
import { assertBrandAccess } from '../lib/access.js';
+import { fixDraftRules, fixRulesInput } from '../lib/content-agent.js';
import { rebuildDraftBrief, rebuildSources } from '../lib/content-brief.js';
import { PlanLimitError } from '../lib/plan-guard.js';
/**
- * Content Editor drafts (CE-007): Rebuild outline in the editor's Brief tab
- * writes the draft's brief from the sources the person kept.
+ * Content Editor drafts: Rebuild outline in the editor's Brief tab writes
+ * the draft's brief from the sources the person kept (CE-007); Fix open
+ * rules with agent leaves the Content Agent's suggestions for review
+ * (CE-018).
*/
const router = Router();
/** The person-facing reason, by status, when the error names none. */
-const ERROR_CODES = { 403: 'forbidden', 404: 'not_found', 422: 'invalid' };
+const ERROR_CODES = { 403: 'forbidden', 404: 'not_found', 409: 'conflict', 422: 'invalid' };
const rebuildBody = z.object({
// guid, not uuid: brand ids aren't all RFC 4122 versions (seeded ones).
@@ -51,4 +54,40 @@ router.post('/:draftId/brief', async (req, res) => {
}
});
+const fixBody = fixRulesInput.extend({ brandId: z.guid() });
+
+/**
+ * POST /api/content-drafts/:draftId/suggestions
+ * Body: { brandId, rules: [{ key, name, detail }], textRules, locked }
+ * Answers { count, batchId }: the suggestions left for review (none when the
+ * agent found nothing it could change); 409 `open_suggestions` while the
+ * latest batch is undecided, 409 `published` for a published draft.
+ */
+router.post('/:draftId/suggestions', async (req, res) => {
+ const draftId = z.guid().safeParse(req.params.draftId);
+ const parsed = fixBody.safeParse(req.body ?? {});
+ if (!draftId.success || !parsed.success) {
+ return res.status(400).json({ error: 'Invalid request', code: 'invalid' });
+ }
+ const { brandId, ...input } = parsed.data;
+ try {
+ await assertBrandAccess(brandId, req.user.id);
+ const result = await fixDraftRules({ draftId: draftId.data, brandId, input });
+ return res.json(result);
+ } catch (err) {
+ if (err instanceof PlanLimitError) {
+ // 402: no active subscription, not a spent budget.
+ const code = err.statusCode === 402 ? 'no_subscription' : 'ai_budget';
+ return res.status(err.statusCode).json({ error: err.message, code });
+ }
+ if (ERROR_CODES[err?.status]) {
+ return res
+ .status(err.status)
+ .json({ error: err.message, code: err.code ?? ERROR_CODES[err.status] });
+ }
+ req.log.error({ err }, 'content draft agent fix error');
+ return res.status(502).json({ error: 'The agent could not run', code: 'failed' });
+ }
+});
+
export default router;
diff --git a/server/src/routes/content-drafts.test.js b/server/src/routes/content-drafts.test.js
index f74391d4c..d35e0badc 100644
--- a/server/src/routes/content-drafts.test.js
+++ b/server/src/routes/content-drafts.test.js
@@ -1,12 +1,16 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import express from 'express';
-/** POST /api/content-drafts/:draftId/brief (CE-007): Rebuild outline. */
+/**
+ * POST /api/content-drafts/:draftId/brief (CE-007): Rebuild outline;
+ * POST /api/content-drafts/:draftId/suggestions (CE-018): Fix open rules with agent.
+ */
const BRAND = '33333333-3333-3333-3333-333333333333';
const DRAFT = '77777777-7777-7777-7777-777777777777';
const rebuildDraftBrief = vi.fn();
+const fixDraftRules = vi.fn();
const assertBrandAccess = vi.fn();
// The route's modules read the Supabase client; CI has no credentials for it.
@@ -15,6 +19,10 @@ vi.mock('../lib/content-brief.js', async (importOriginal) => ({
...(await importOriginal()),
rebuildDraftBrief: (...args) => rebuildDraftBrief(...args),
}));
+vi.mock('../lib/content-agent.js', async (importOriginal) => ({
+ ...(await importOriginal()),
+ fixDraftRules: (...args) => fixDraftRules(...args),
+}));
vi.mock('../lib/access.js', () => ({ assertBrandAccess: (...args) => assertBrandAccess(...args) }));
const { default: router } = await import('./content-drafts.js');
@@ -25,6 +33,7 @@ let baseUrl;
beforeEach(async () => {
rebuildDraftBrief.mockReset().mockResolvedValue({ goal: 'g', outline: [] });
+ fixDraftRules.mockReset().mockResolvedValue({ count: 2, batchId: 'b1' });
assertBrandAccess.mockReset().mockResolvedValue({});
const app = express();
app.use(express.json());
@@ -108,3 +117,44 @@ describe('POST /api/content-drafts/:draftId/brief', () => {
await expect(res.json()).resolves.toMatchObject({ code: 'failed' });
});
});
+
+const fix = (body) =>
+ fetch(`${baseUrl}/${DRAFT}/suggestions`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(body),
+ });
+
+describe('POST /api/content-drafts/:draftId/suggestions', () => {
+ const rules = [{ key: 'faq', name: 'FAQ', detail: 'missing' }];
+
+ it('runs the agent for a member of the brand', async () => {
+ const res = await fix({ brandId: BRAND, rules });
+ expect(res.status).toBe(200);
+ await expect(res.json()).resolves.toEqual({ count: 2, batchId: 'b1' });
+ expect(fixDraftRules).toHaveBeenCalledWith({
+ draftId: DRAFT,
+ brandId: BRAND,
+ input: { rules, textRules: [], locked: [] },
+ });
+ });
+
+ it('says why it didn’t run', async () => {
+ fixDraftRules.mockRejectedValueOnce(new PlanLimitError('used up', 429));
+ let res = await fix({ brandId: BRAND, rules });
+ expect(res.status).toBe(429);
+ await expect(res.json()).resolves.toMatchObject({ code: 'ai_budget' });
+
+ fixDraftRules.mockRejectedValueOnce(new PlanLimitError('no subscription', 402));
+ res = await fix({ brandId: BRAND, rules });
+ expect(res.status).toBe(402);
+ await expect(res.json()).resolves.toMatchObject({ code: 'no_subscription' });
+
+ for (const code of ['open_suggestions', 'published']) {
+ fixDraftRules.mockRejectedValueOnce(Object.assign(new Error(code), { status: 409, code }));
+ res = await fix({ brandId: BRAND, rules });
+ expect(res.status).toBe(409);
+ await expect(res.json()).resolves.toMatchObject({ code });
+ }
+ });
+});
diff --git a/web/messages/en.json b/web/messages/en.json
index efbb35b5c..a46703aaa 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -3376,7 +3376,21 @@
"undoneToast": "{count, plural, one {Undone · back to the suggestion} other {# undone · the suggestions are back}}",
"notApplied": "{count, plural, one {# suggestion couldn't be applied} other {# suggestions couldn't be applied}}: the page changed there, or AI quotes the passage.",
"saveFailed": "Couldn't keep the decision. Try again.",
- "htmlBlocked": "Accept or reject the {count, plural, one {# agent suggestion} other {# agent suggestions}} first, then edit as HTML."
+ "htmlBlocked": "Accept or reject the {count, plural, one {# agent suggestion} other {# agent suggestions}} first, then edit as HTML.",
+ "fixRules": "Fix open rules with agent",
+ "fixing": "Content Agent is working…",
+ "fixed": "{count, plural, one {Content Agent suggested # change} other {Content Agent suggested # changes}} · review them in the page",
+ "nothingToFix": "Content Agent found nothing it could change without new facts.",
+ "decideFirst": "Accept or reject the open suggestions first.",
+ "citedLoading": "Reading which passages AI quotes, so the agent leaves them as they are…",
+ "fixErrors": {
+ "ai_budget": "This month's AI usage is spent. Upgrade for more, or try again next month.",
+ "no_subscription": "Content Agent needs an active subscription. Choose a plan in Settings › Billing.",
+ "empty": "Write something first: the agent improves the page you have.",
+ "open_suggestions": "This page has open suggestions, maybe from another tab. They're shown now: accept or reject them first.",
+ "published": "This page is published. Content Agent suggests changes to drafts only.",
+ "failed": "Content Agent couldn't run. Try again."
+ }
},
"cited": {
"platform": "{name} {count}×",
@@ -3771,6 +3785,16 @@
},
"mineName": "My rules · {type}",
"setPicked": "Rule set changed for this draft",
+ "agentFix": "Fix open rules with agent",
+ "agentFixing": "Content Agent is working…",
+ "agentFixHint": "Content Agent suggests changes for the rules not met yet, for you to review in the page",
+ "fixTrace": "Fix",
+ "fixTraceHint": "Content Agent rewrites only these phrases, for you to review",
+ "agent": {
+ "aim": "aim",
+ "now": "now",
+ "phrases": "phrases found"
+ },
"saveMine": "Save as my rules",
"reset": "Reset",
"savedMine": "Saved as My rules for {type}",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-bar.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-bar.tsx
index 9e83da4be..d1d334645 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-bar.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_agent-bar.tsx
@@ -1,7 +1,7 @@
'use client';
import { useTranslations } from 'next-intl';
-import { Bot, X } from 'lucide-react';
+import { Bot, Loader2, X } from 'lucide-react';
import { Link } from '@/i18n/navigation';
import { Button } from '@/components/ui/button';
import { suggestionCounts, type DraftSuggestion } from '@/lib/content/suggestions';
@@ -10,8 +10,8 @@ import { suggestionCounts, type DraftSuggestion } from '@/lib/content/suggestion
* The agent bar over the page (CE-018): while an agent's suggestions are
* open, who left how many and from which action, with Reject all and Accept
* all (open ones only); once all are decided, how many were accepted and
- * rejected and how many rules are still open. Undo all brings every decided
- * one back to review.
+ * rejected and how many rules are still open, with Fix open rules with
+ * agent for a new batch. Undo all brings every decided one back to review.
*/
export function AgentBar({
suggestions,
@@ -21,6 +21,8 @@ export function AgentBar({
onRejectAll,
onUndoAll,
onClose,
+ onFixRules,
+ fixing,
}: {
suggestions: DraftSuggestion[];
rulesOpen: number;
@@ -29,6 +31,9 @@ export function AgentBar({
onRejectAll: () => void;
onUndoAll: () => void;
onClose: () => void;
+ /** Fix open rules with agent: new suggestions, again for review. */
+ onFixRules: () => void;
+ fixing: boolean;
}) {
const t = useTranslations('contentEditor.editor.suggestions');
const { open, accepted, rejected } = suggestionCounts(suggestions);
@@ -95,15 +100,29 @@ export function AgentBar({
</Button>
</>
) : (
- <Button
- variant="ghost"
- size="icon-sm"
- aria-label={t('close')}
- title={t('close')}
- onClick={onClose}
- >
- <X className="h-3.5 w-3.5" />
- </Button>
+ <>
+ {rulesOpen > 0 && (
+ <Button
+ variant="outline"
+ size="sm"
+ className="h-7"
+ disabled={busy || fixing}
+ onClick={onFixRules}
+ >
+ {fixing && <Loader2 className="h-3.5 w-3.5 animate-spin" />}
+ {fixing ? t('fixing') : t('fixRules')}
+ </Button>
+ )}
+ <Button
+ variant="ghost"
+ size="icon-sm"
+ aria-label={t('close')}
+ title={t('close')}
+ onClick={onClose}
+ >
+ <X className="h-3.5 w-3.5" />
+ </Button>
+ </>
)}
</span>
</div>
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.tsx
index 4b11eff62..ce37b7f1d 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
@@ -25,6 +25,7 @@ import { PLATFORM_LABELS } from '@/config/platform-labels';
import { DraftMethodBadge, DraftStatusBadge } from '@/components/content/draft-badges';
import {
decideDraftSuggestions,
+ fixDraftRules,
getCitedPassages,
getDraftSuggestions,
keepAgentBeforeVersion,
@@ -45,6 +46,7 @@ import {
import { PAGE_READ_DELAY_MS, draftTitleFromDoc, safeLinkHref } from '@/lib/content/editor';
import { JsonLd } from '@/lib/content/json-ld-node';
import { detectPageType } from '@/lib/content/page-types';
+import { aiTracesFound } from '@/lib/content/rules';
import { cn } from '@/lib/utils';
import { AgentBar } from './_agent-bar';
import { DropOverlay, useAttachments } from './_attach';
@@ -59,6 +61,7 @@ import {
import { HistoryMenu } from './_history-menu';
import { HtmlBlock, copyToClipboard } from './_html-block';
import { PageTypeMenu } from './_page-type-menu';
+import { ruleStatusText, type AgentFix } from './_rules-tab';
import { EditorSidePanel, useSidePanelOpen } from './_side-panel';
import {
AgentSuggestions,
@@ -115,7 +118,8 @@ const DOCUMENT_CLASS = cn(
* 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; an agent's suggestions sit in the page as cards to accept or
- * reject, with the agent bar over it (CE-018).
+ * reject, with the agent bar over it, and Fix open rules with agent asks the
+ * Content Agent for a new batch (CE-018).
*/
export function DraftEditor({ draft }: { draft: ContentDraft }) {
const t = useTranslations('contentEditor.editor');
@@ -134,11 +138,17 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
// What quotes each locked passage, for the warning: "ChatGPT 14× and …".
const citedBy = useRef(new Map<string, string>());
const [citedCount, setCitedCount] = useState(0);
+ // Whether the locked passages have been read: until then the agent would
+ // be told none are locked (a new page has none to read).
+ const [citedRead, setCitedRead] = useState(!draft.sourceUrl);
// An agent's suggestions (CE-018): its latest batch, with what was decided.
const [suggestions, setSuggestions] = useState<DraftSuggestion[]>([]);
const suggestionsNow = useRef(suggestions);
const [deciding, setDeciding] = useState(false);
const [barClosed, setBarClosed] = useState(false);
+ const [fixing, setFixing] = useState(false);
+ const tRules = useTranslations('contentEditor.editor.rules');
+ const tStatus = useTranslations('contentEditor.editor.rules.status');
// History keeps the page before the first suggestion accepted here.
const agentBeforeKept = useRef(false);
const onDecide = useRef<(id: string, decision: SuggestionDecision) => void>(() => {});
@@ -285,6 +295,7 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
// read when it opens, and again after a restore.
const loadCited = useCallback(async () => {
if (!editor || !draft.sourceUrl) return;
+ setCitedRead(false);
const texts = new Set<string>();
editor.state.doc.descendants((node) => {
if (!node.isTextblock) return true;
@@ -306,6 +317,9 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
);
} catch (err) {
console.error('Failed to read the cited passages:', err);
+ } finally {
+ // Read or not, the editor's locks are what the agent is told.
+ if (!editor.isDestroyed) setCitedRead(true);
}
}, [editor, draft.id, draft.sourceUrl, quotedBy, t]);
@@ -454,6 +468,86 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
};
const openSuggestions = suggestions.filter((s) => s.status === 'open').length;
+ /**
+ * Fix open rules with agent: the page as it is now is saved, then the
+ * Content Agent reads it with the rules still open (or only the ones
+ * named), the person's own rules and the locked passages, and leaves a new
+ * batch of suggestions for review.
+ */
+ const fixRules = async (only?: string[]) => {
+ if (!editor || fixing || !citedRead) return;
+ if (openSuggestions > 0) {
+ toast(t('suggestions.decideFirst'));
+ return;
+ }
+ const open = rules.rules.filter(
+ ({ rule, status }) => rule.on && status.state === 'bad' && (!only || only.includes(rule.key)),
+ );
+ if (open.length === 0) return;
+ const pageText = editor.getText({ blockSeparator: '\n' });
+ const request = {
+ rules: open.map(({ rule, status }) => ({
+ key: rule.key,
+ name: tRules(`names.${rule.key}`),
+ detail: [
+ rule.current
+ ? `${tRules('agent.aim')}: ${rule.current}${rule.unit ? ` ${tRules(`units.${rule.unit}`)}` : ''}`
+ : null,
+ `${tRules('agent.now')}: ${ruleStatusText(tStatus, status)}`,
+ rule.key === 'aiTraces'
+ ? `${tRules('agent.phrases')}: ${aiTracesFound(pageText).join(', ')}`
+ : null,
+ ]
+ .filter(Boolean)
+ .join(' · '),
+ })),
+ textRules: rules.textRules.filter(({ rule }) => rule.on).map(({ rule }) => rule.text),
+ locked: (() => {
+ const locks = citedLocks(editor.state);
+ return [...locks.labels.keys()].filter((key) => !locks.unlocked.has(key));
+ })(),
+ };
+ setFixing(true);
+ try {
+ // The agent reads the page as saved.
+ await autosave.settle();
+ const result = await fixDraftRules(draft.id, draft.brandId, request);
+ if ('error' in result) {
+ toast.error(t(`suggestions.fixErrors.${result.error}`));
+ // Another tab (or the API) left them: show them here to decide.
+ if (result.error === 'open_suggestions') {
+ setSuggestions(await getDraftSuggestions(draft.id));
+ setBarClosed(false);
+ }
+ return;
+ }
+ if (result.count === 0) {
+ toast(t('suggestions.nothingToFix'));
+ return;
+ }
+ setSuggestions(await getDraftSuggestions(draft.id));
+ setBarClosed(false);
+ // A new batch: History keeps the page again before its first change.
+ agentBeforeKept.current = false;
+ toast.success(t('suggestions.fixed', { count: result.count }));
+ } catch (err) {
+ console.error('Failed to run the Content Agent:', err);
+ toast.error(t('suggestions.fixErrors.failed'));
+ } finally {
+ setFixing(false);
+ }
+ };
+ const agentFix: AgentFix = {
+ run: (only) => void fixRules(only),
+ running: fixing,
+ blocked:
+ openSuggestions > 0
+ ? t('suggestions.decideFirst')
+ : !citedRead
+ ? t('suggestions.citedLoading')
+ : null,
+ };
+
// 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) =>
@@ -749,6 +843,8 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
)
}
onClose={() => setBarClosed(true)}
+ onFixRules={() => void fixRules()}
+ fixing={fixing}
/>
)}
{editor && (
@@ -809,6 +905,7 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
rules={rules}
reads={reads}
tabRequest={tabRequest}
+ agentFix={agentFix}
/>
)}
</div>
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx
index 05280c513..27275240f 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_rules-tab.tsx
@@ -2,6 +2,7 @@
import { useState } from 'react';
import { useTranslations } from 'next-intl';
+import { Bot, Loader2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import {
Dialog,
@@ -56,14 +57,25 @@ const HINTED = new Set([
* ("Your text rules"); Save as my rules keeps the set for the workspace's
* drafts of the type, and Reset goes back to Ansvisor's default.
*/
+/** Fix open rules with agent (CE-018), as the editor runs it. */
+export interface AgentFix {
+ /** Runs the agent on the open rules, or only on the ones named. */
+ run: (only?: string[]) => void;
+ running: boolean;
+ /** Why it can't run now (suggestions still open), or null. */
+ blocked: string | null;
+}
+
export function RulesTab({
state,
siteType,
draftPageType,
+ agentFix,
}: {
state: DraftRulesState;
siteType: SiteType;
draftPageType: PageType | null;
+ agentFix?: AgentFix;
}) {
const t = useTranslations('contentEditor.editor.rules');
const tTypes = useTranslations('contentEditor.pageTypes');
@@ -123,6 +135,23 @@ export function RulesTab({
<p className="text-xs text-muted-foreground" role="status">
{t('met', { met: state.met, total: state.total })}
</p>
+ {agentFix && state.met < state.total && (
+ <Button
+ size="sm"
+ variant="outline"
+ className="w-full"
+ disabled={agentFix.running || agentFix.blocked !== null}
+ title={agentFix.blocked ?? t('agentFixHint')}
+ onClick={() => agentFix.run()}
+ >
+ {agentFix.running ? (
+ <Loader2 className="h-3.5 w-3.5 animate-spin" />
+ ) : (
+ <Bot className="h-3.5 w-3.5" />
+ )}
+ {agentFix.running ? t('agentFixing') : t('agentFix')}
+ </Button>
+ )}
</div>
{RULE_GROUPS.map((group) => {
@@ -142,6 +171,7 @@ export function RulesTab({
rule={rule}
status={status}
onChange={(change) => state.change(rule.key, change)}
+ agentFix={rule.key === 'aiTraces' ? agentFix : undefined}
/>
))}
</ul>
@@ -318,10 +348,13 @@ function RuleRow({
rule,
status,
onChange,
+ agentFix,
}: {
rule: ActiveRule;
status: RuleStatus;
onChange: (change: { on?: boolean; value?: string }) => void;
+ /** "Remove AI traces" offers Fix: the agent rewrites only those phrases. */
+ agentFix?: AgentFix;
}) {
const t = useTranslations('contentEditor.editor.rules');
const name = t(`names.${rule.key}`);
@@ -340,6 +373,17 @@ function RuleRow({
{name}
</span>
<StatusBadge on={rule.on} status={status} />
+ {agentFix && rule.on && status.state === 'bad' && (
+ <button
+ type="button"
+ disabled={agentFix.running || agentFix.blocked !== null}
+ title={agentFix.blocked ?? t('fixTraceHint')}
+ onClick={() => agentFix.run([rule.key])}
+ className="shrink-0 text-[11px] font-medium underline underline-offset-2 hover:no-underline disabled:opacity-50"
+ >
+ {t('fixTrace')}
+ </button>
+ )}
</div>
{rule.kind !== 'switch' && (
<div className="mt-1 flex items-center gap-1 pl-9">
@@ -410,6 +454,18 @@ function RuleValue({
);
}
+/** How a rule reads now, as its badge says it ("✓", "3 found", "Missing"). */
+export function ruleStatusText(
+ t: (key: string, values?: Record<string, number>) => string,
+ status: RuleStatus,
+): string {
+ if (status.state === 'ok') return '✓';
+ if (status.code === 'partOf') return t('partOf', { count: status.count, of: status.of });
+ if (status.code === 'current') return formatNumber(status.count);
+ if ('count' in status) return t(status.code, { count: status.count });
+ return t(status.code);
+}
+
function StatusBadge({ on, status }: { on: boolean; status: RuleStatus }) {
const t = useTranslations('contentEditor.editor.rules.status');
if (!on) {
@@ -417,12 +473,7 @@ function StatusBadge({ on, status }: { on: boolean; status: RuleStatus }) {
<span className="w-20 shrink-0 text-right text-[11px] text-muted-foreground">{t('off')}</span>
);
}
- let text: string;
- if (status.state === 'ok') text = '✓';
- else if (status.code === 'partOf') text = t('partOf', { count: status.count, of: status.of });
- else if (status.code === 'current') text = formatNumber(status.count);
- else if ('count' in status) text = t(status.code, { count: status.count });
- else text = t(status.code);
+ const text = ruleStatusText(t, status);
return (
<span
className={cn(
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx
index db499bf72..c8643068c 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_side-panel.tsx
@@ -6,7 +6,7 @@ import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { isPageType, type ContentDraft } from '@/lib/content/drafts';
import { BriefTab } from './_brief-tab';
import { ImpactTab } from './_impact-tab';
-import { RulesTab } from './_rules-tab';
+import { RulesTab, type AgentFix } from './_rules-tab';
import { SourcesTab } from './_sources-tab';
import { useBriefRebuild, type PageNow } from './_use-brief-rebuild';
import type { DraftReads } from './_use-draft-reads';
@@ -64,6 +64,7 @@ export function EditorSidePanel({
rules,
reads,
tabRequest,
+ agentFix,
}: {
draft: ContentDraft;
/** The draft's title, page and type as written now: Sources and Brief read them. */
@@ -76,6 +77,8 @@ export function EditorSidePanel({
reads: DraftReads;
/** A tab to open, from outside the panel (the header's rules chip). */
tabRequest: { tab: PanelTab; n: number } | null;
+ /** Fix open rules with agent, for the Rules tab (CE-018). */
+ agentFix?: AgentFix;
}) {
const t = useTranslations('contentEditor.editor.panel');
const [tab, setTab] = useState<PanelTab>('impact');
@@ -171,6 +174,7 @@ export function EditorSidePanel({
{opened.has('rules') && (
<div hidden={tab !== 'rules'}>
<RulesTab
+ agentFix={agentFix}
state={rules}
siteType={draft.siteType}
draftPageType={isPageType(pageType) ? pageType : null}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
index 7141d9e6f..d1b4e398e 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
@@ -7,7 +7,7 @@ import type { Editor } from '@tiptap/core';
import { RoleProvider } from '@/components/providers/role-provider';
import type { ContentDraft } from '@/lib/content/drafts';
import type { DraftSuggestion } from '@/lib/content/suggestions';
-import { passageKey } from '@/lib/content/cited-passages';
+import { passageKey, type CitedPassage } from '@/lib/content/cited-passages';
import ContentEditorPage from './page';
const getContentDraft = vi.fn();
@@ -28,7 +28,9 @@ const getCitedPassages = vi.fn();
const getDraftSuggestions = vi.fn();
const decideDraftSuggestions = vi.fn();
const keepAgentBeforeVersion = vi.fn();
+const fixDraftRules = vi.fn();
vi.mock('@/lib/actions/content-drafts', () => ({
+ fixDraftRules: (...args: unknown[]) => fixDraftRules(...args),
getDraftSuggestions: (...args: unknown[]) => getDraftSuggestions(...args),
decideDraftSuggestions: (...args: unknown[]) => decideDraftSuggestions(...args),
keepAgentBeforeVersion: (...args: unknown[]) => keepAgentBeforeVersion(...args),
@@ -187,6 +189,7 @@ beforeEach(() => {
getDraftSuggestions.mockReset().mockResolvedValue([]);
decideDraftSuggestions.mockReset().mockResolvedValue(1);
keepAgentBeforeVersion.mockReset().mockResolvedValue(undefined);
+ fixDraftRules.mockReset().mockResolvedValue({ count: 0 });
localStorage.clear();
brandState.activeBrandId = 'b1';
brandState.setActiveBrand.mockReset();
@@ -1255,6 +1258,136 @@ describe('the side panel', () => {
expect(toast).toHaveBeenCalledWith('Rule updated for this draft');
});
+ describe('Fix open rules with agent', () => {
+ const traced = draft({
+ pageType: 'blog_post',
+ bodyHtml: '<h1>Pipeline forecasting</h1><p>We delve into how teams forecast.</p>',
+ });
+ const agentSuggestion = {
+ id: 'n1',
+ batchId: 'b2',
+ actionId: null,
+ actionNo: null,
+ kind: 'replace' as const,
+ blockText: 'We delve into how teams forecast.',
+ beforeText: 'delve into',
+ afterText: 'look at',
+ reason: 'remove an AI trace (rule)',
+ ruleKeys: ['aiTraces'],
+ status: 'open' as const,
+ };
+ /** Opens the Rules tab from the header's chip. */
+ async function openRules() {
+ window.innerWidth = 1024;
+ await renderPage();
+ const chip = container.querySelector<HTMLButtonElement>(
+ 'header button[aria-label^="Rules "]',
+ )!;
+ await act(async () => chip.click());
+ }
+ const panelButton = (label: string) =>
+ [...panel()!.querySelectorAll<HTMLButtonElement>('button')].find(
+ (b) => b.textContent === label,
+ )!;
+
+ it('sends the open rules and leaves the agent’s batch for review', async () => {
+ getContentDraft.mockResolvedValue(traced);
+ getDraftSuggestions.mockResolvedValueOnce([]).mockResolvedValueOnce([agentSuggestion]);
+ fixDraftRules.mockResolvedValue({ count: 1 });
+ await openRules();
+
+ await act(async () => panelButton('Fix open rules with agent').click());
+ await act(async () => {});
+
+ const [id, brandId, request] = fixDraftRules.mock.calls[0];
+ expect([id, brandId]).toEqual(['d1', 'b1']);
+ expect(request.rules).toContainEqual({
+ key: 'aiTraces',
+ name: 'Remove AI traces (humanize)',
+ detail: 'now: 1 found · phrases found: delve',
+ });
+ expect(request.rules.every((r: { key: string }) => r.key !== 'sentence')).toBe(true);
+ expect(request).toMatchObject({ textRules: [], locked: [] });
+ expect(
+ container.querySelector('[role="region"][aria-label="Agent suggestions"]')!.textContent,
+ ).toContain('Content Agent suggested 1 change');
+ expect(toast.success).toHaveBeenCalledWith(
+ 'Content Agent suggested 1 change · review them in the page',
+ );
+ });
+
+ it('fixes only the AI traces from their rule', async () => {
+ getContentDraft.mockResolvedValue(traced);
+ await openRules();
+
+ await act(async () => panelButton('Fix').click());
+ await act(async () => {});
+
+ expect(fixDraftRules.mock.calls[0][2].rules.map((r: { key: string }) => r.key)).toEqual([
+ 'aiTraces',
+ ]);
+ expect(toast).toHaveBeenCalledWith(
+ 'Content Agent found nothing it could change without new facts.',
+ );
+ });
+
+ it('says when the AI usage is spent', async () => {
+ getContentDraft.mockResolvedValue(traced);
+ fixDraftRules.mockResolvedValue({ error: 'ai_budget' });
+ await openRules();
+
+ await act(async () => panelButton('Fix open rules with agent').click());
+ await act(async () => {});
+ expect(toast.error).toHaveBeenCalledWith(
+ "This month's AI usage is spent. Upgrade for more, or try again next month.",
+ );
+ });
+
+ it('waits until the open suggestions are decided', async () => {
+ getContentDraft.mockResolvedValue(traced);
+ getDraftSuggestions.mockResolvedValue([agentSuggestion]);
+ await openRules();
+
+ const fix = panelButton('Fix open rules with agent');
+ expect(fix.disabled).toBe(true);
+ expect(fix.title).toBe('Accept or reject the open suggestions first.');
+ });
+
+ it('waits until the passages AI quotes are read, so they go to the agent locked', async () => {
+ getContentDraft.mockResolvedValue({
+ ...traced,
+ sourceUrl: 'https://acme.example/forecasting',
+ });
+ let read: (cited: CitedPassage[]) => void = () => {};
+ getCitedPassages.mockReturnValue(new Promise((resolve) => (read = resolve)));
+ await openRules();
+
+ const fix = () => panelButton('Fix open rules with agent');
+ expect(fix().disabled).toBe(true);
+ expect(fix().title).toBe(
+ 'Reading which passages AI quotes, so the agent leaves them as they are…',
+ );
+ await act(async () => read([]));
+ expect(fix().disabled).toBe(false);
+ });
+
+ it('shows the open suggestions another tab left, when the agent is refused for them', async () => {
+ getContentDraft.mockResolvedValue(traced);
+ getDraftSuggestions.mockResolvedValueOnce([]).mockResolvedValueOnce([agentSuggestion]);
+ fixDraftRules.mockResolvedValue({ error: 'open_suggestions' });
+ await openRules();
+
+ await act(async () => panelButton('Fix open rules with agent').click());
+ await act(async () => {});
+ expect(toast.error).toHaveBeenCalledWith(
+ "This page has open suggestions, maybe from another tab. They're shown now: accept or reject them first.",
+ );
+ expect(
+ container.querySelector('[role="region"][aria-label="Agent suggestions"]')!.textContent,
+ ).toContain('Content Agent suggested 1 change');
+ });
+ });
+
it('starts a draft from the workspace’s My rules, and saves a draft’s set as them', async () => {
getMyRuleSets.mockResolvedValue({
blog_post: {
diff --git a/web/src/lib/actions/content-drafts.test.ts b/web/src/lib/actions/content-drafts.test.ts
index f917d6758..860a00614 100644
--- a/web/src/lib/actions/content-drafts.test.ts
+++ b/web/src/lib/actions/content-drafts.test.ts
@@ -27,7 +27,10 @@ let rpcResult: { data: unknown; error: { message: string } | null };
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' } } }) },
+ auth: {
+ getUser: async () => ({ data: { user: { id: 'u1' } } }),
+ getSession: async () => ({ data: { session: { access_token: 'tok' } } }),
+ },
rpc: async (name: string, args: unknown) => (rpcCalls.push([name, args]), rpcResult),
from: () => {
const call: Call = { op: 'select', filters: [] };
@@ -57,6 +60,7 @@ vi.mock('@/lib/supabase/server', () => ({
const {
createContentDraft,
+ fixDraftRules,
decideDraftSuggestions,
getDraftSuggestions,
keepAgentBeforeVersion,
@@ -499,3 +503,39 @@ describe('agent suggestions', () => {
]);
});
});
+
+describe('fixDraftRules', () => {
+ const BRAND = '9a1b2c3d-4e5f-4a6b-8c7d-0e1f2a3b4c5d';
+ const request = {
+ rules: [{ key: 'aiTraces', name: 'Remove AI traces', detail: 'phrases found: delve' }],
+ textRules: ['Say “we”'],
+ locked: ['northwind forecasts each deal'],
+ };
+
+ it('asks the server’s agent with the session, and says how many it left', async () => {
+ const fetchMock = vi.fn(async () => Response.json({ count: 3, batchId: 'b' }));
+ vi.stubGlobal('fetch', fetchMock);
+ await expect(fixDraftRules(DRAFT, BRAND, request)).resolves.toEqual({ count: 3 });
+ const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
+ expect(url).toMatch(new RegExp(`/api/content-drafts/${DRAFT}/suggestions$`));
+ expect((init.headers as Record<string, string>).Authorization).toBe('Bearer tok');
+ expect(JSON.parse(init.body as string)).toEqual({ brandId: BRAND, ...request });
+ vi.unstubAllGlobals();
+ });
+
+ it('names the budget and the empty page, and anything else as failed', async () => {
+ for (const [body, error] of [
+ [{ code: 'ai_budget' }, 'ai_budget'],
+ [{ code: 'empty' }, 'empty'],
+ [{ code: 'teapot' }, 'failed'],
+ ] as const) {
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () => Response.json(body, { status: 402 })),
+ );
+ await expect(fixDraftRules(DRAFT, BRAND, request)).resolves.toEqual({ error });
+ }
+ vi.unstubAllGlobals();
+ await expect(fixDraftRules('../etc', BRAND, request)).resolves.toEqual({ error: 'failed' });
+ });
+});
diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.ts
index 94ca00e56..4da1f4c96 100644
--- a/web/src/lib/actions/content-drafts.ts
+++ b/web/src/lib/actions/content-drafts.ts
@@ -1069,6 +1069,74 @@ export async function rebuildDraftBrief(
}
}
+const AGENT_FIX_ERRORS = [
+ 'ai_budget',
+ 'no_subscription',
+ 'empty',
+ 'open_suggestions',
+ 'published',
+ 'failed',
+] as const;
+export type AgentFixError = (typeof AGENT_FIX_ERRORS)[number];
+
+/** What Fix open rules with agent sends: the rules still open, and the passages kept locked. */
+export interface AgentFixRequest {
+ rules: { key: string; name: string; detail: string }[];
+ /** Rules in the person's own words, to follow as well. */
+ textRules: string[];
+ /** The locked passages, by passageKey. */
+ locked: string[];
+}
+
+/**
+ * Fix open rules with agent (CE-018): the Content Agent reads the draft as
+ * saved and leaves suggestions on the rules sent, for review in the page,
+ * metered from the organization's AI usage (content-agent.js). count is how
+ * many it left: none when it found nothing it could change.
+ */
+export async function fixDraftRules(
+ id: string,
+ brandId: string,
+ request: AgentFixRequest,
+): Promise<{ count: number } | { error: AgentFixError }> {
+ if (!draftId.safeParse(id).success || !draftId.safeParse(brandId).success) {
+ return { error: 'failed' };
+ }
+ const supabase = await createClient();
+ const {
+ data: { session },
+ } = await supabase.auth.getSession();
+ if (!session) return { error: 'failed' };
+ try {
+ const res = await fetch(`${API_BASE_URL}/api/content-drafts/${id}/suggestions`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${session.access_token}`,
+ },
+ body: JSON.stringify({
+ brandId,
+ rules: request.rules.slice(0, 40).map((rule) => ({
+ key: rule.key.slice(0, 60),
+ name: rule.name.slice(0, 200),
+ detail: rule.detail.slice(0, 500),
+ })),
+ textRules: request.textRules.slice(0, 50).map((text) => text.slice(0, 300)),
+ locked: request.locked.slice(0, 500).map((key) => key.slice(0, 5000)),
+ }),
+ });
+ const body = (await res.json().catch(() => ({}))) as Record<string, unknown>;
+ if (!res.ok) {
+ const code = body.code as AgentFixError;
+ return { error: (AGENT_FIX_ERRORS as readonly string[]).includes(code) ? code : 'failed' };
+ }
+ return { count: Number(body.count) || 0 };
+ } catch (err) {
+ console.error('Failed to run the Content Agent:', err);
+ return { error: 'failed' };
+ }
+}
+
/**
* The Rules tab (CE-009): a rule turned on or off, or given a value, for
* this draft ("Rule updated for this draft"). Throws 'not_found' when no
diff --git a/web/src/lib/content/rules.ts b/web/src/lib/content/rules.ts
index ac0c5fe42..52e97069b 100644
--- a/web/src/lib/content/rules.ts
+++ b/web/src/lib/content/rules.ts
@@ -411,6 +411,15 @@ function count(text: string, phrase: string): number {
);
}
+/** The machine-written phrases the page has, each once: what "Remove AI traces" counts. */
+export function aiTracesFound(text: string): string[] {
+ return [
+ ...new Set(
+ AI_TRACES.filter((phrase) => count(text, phrase) > 0).map((p) => p.replace('’', "'")),
+ ),
+ ];
+}
+
/** A link's host, or null for one on the page's own site (a path or an anchor). */
function linkHost(href: string): string | null {
if (!/^https?:\/\//i.test(href)) return null;