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): aim an optimized page's Length rule at its own length (#1383)

Cihan942c4922a700
diff --git a/web/messages/en.json b/web/messages/en.json
index 4518f2565..203163b9c 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -4024,7 +4024,7 @@
"measureSteps": "How to measure, as numbered steps"
},
"hints": {
- "length": "A target, not a minimum: within 20% of it meets the rule. A page well over it is open, and Apply rules with Agent shortens it a few passages at a time.",
+ "length": "A target, not a minimum: within 20% of it meets the rule. A page brought in to optimize starts at its own length. A page well over it is open, and Apply rules with Agent shortens it a few passages at a time.",
"oneH1": "A page has one H1, its title. Make any other H1 an H2 in the toolbar: Content Agent doesn't change heading levels.",
"summary": "A short summary under the H1 that answer engines can quote.",
"fanoutH2": "Takes the top fan-outs you keep in Sources, the most searched first, not every one.",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts
index 560f241f4..2665217a0 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_use-draft-rules.ts
@@ -11,11 +11,13 @@ import {
checkRule,
draftRulesFit,
effectiveRules,
+ overridesForImport,
readPage,
ruleLength,
ruleSet,
ruleSetFits,
rulesMet,
+ setToSave,
textRuleCheck,
type ActiveRule,
type DraftRules,
@@ -144,7 +146,11 @@ export function useDraftRules({
[choices, setType, mine],
);
const rules = useMemo(
- () => activeRules(ruleSet(setType, { keywords, language }), effective),
+ () =>
+ activeRules(
+ ruleSet(setType, { keywords, language, length: effective.importLength }),
+ effective,
+ ),
[setType, keywords, language, effective],
);
// The checks follow the page a beat behind typing.
@@ -225,8 +231,10 @@ export function useDraftRules({
{
set: type === draftType ? null : type,
source,
- overrides: set?.overrides ?? {},
+ // The page's own length stays the Length rule's starting point.
+ overrides: overridesForImport(set?.overrides ?? {}, effective.importLength),
textRules: set?.textRules ?? [],
+ ...(effective.importLength ? { importLength: effective.importLength } : {}),
},
t('setPicked'),
);
@@ -264,7 +272,7 @@ export function useDraftRules({
},
saveAsMine: () => {
if (!setType) return;
- const set: SavedRuleSet = { overrides: effective.overrides, textRules: effective.textRules };
+ const set = setToSave(effective, mine[setType]);
if (!ruleSetFits(set)) {
toast.error(t('tooLongMine'));
return;
@@ -280,6 +288,16 @@ export function useDraftRules({
});
},
reset: () =>
- save({ set: choices.set, source: 'default', overrides: {}, textRules: [] }, t('resetDone')),
+ save(
+ {
+ set: choices.set,
+ source: 'default',
+ overrides: {},
+ textRules: [],
+ // The page's own length isn't a choice: Reset keeps it.
+ ...(effective.importLength ? { importLength: effective.importLength } : {}),
+ },
+ t('resetDone'),
+ ),
};
}
diff --git a/web/src/components/content/new-content-dialog.test.tsx b/web/src/components/content/new-content-dialog.test.tsx
index 52ba5224b..a6aec0563 100644
--- a/web/src/components/content/new-content-dialog.test.tsx
+++ b/web/src/components/content/new-content-dialog.test.tsx
@@ -941,6 +941,36 @@ describe('New content', () => {
expect(toast.success).not.toHaveBeenCalled();
});
+ it('aims the agent at a pasted page’s own length (F)', async () => {
+ startAgentWriting.mockResolvedValue({ started: true });
+ await render('ecommerce');
+ await click(radio('Optimize content'));
+ await click(tab('Paste text'));
+ const box = document.querySelector('textarea')!;
+ const paste = new Event('paste', { bubbles: true, cancelable: true });
+ Object.defineProperty(paste, 'clipboardData', {
+ value: {
+ getData: (type: string) =>
+ type === 'text/html'
+ ? `<h2>Returns policy</h2><p>${'30 days to return. '.repeat(10)}</p>`
+ : '',
+ },
+ });
+ await act(async () => box.dispatchEvent(paste));
+ await click(button('Continue'));
+ await click(whoOption('Agent'));
+ await click(button('Start the agent'));
+
+ // "Returns policy 30 days to return. …": as the draft started, not a new page's 3,000.
+ expect(agentWriteRequest).toHaveBeenCalledWith(
+ expect.objectContaining({
+ method: 'optimize',
+ rules: { set: null, source: null, overrides: {}, textRules: [], importLength: 204 },
+ }),
+ describeRule,
+ );
+ });
+
it('keeps the draft for me when the agent can’t start', async () => {
startAgentWriting.mockResolvedValue({ error: 'ai_budget' });
await startNewPage();
diff --git a/web/src/components/content/new-content-dialog.tsx b/web/src/components/content/new-content-dialog.tsx
index 860a21600..818c32d63 100644
--- a/web/src/components/content/new-content-dialog.tsx
+++ b/web/src/components/content/new-content-dialog.tsx
@@ -51,7 +51,7 @@ import { agentWriteRequest, useRuleDescriber } from '@/lib/content/agent-write';
import { MAX_ATTACHMENT_BYTES, textToHtml } from '@/lib/content/attachments';
import { cleanHtml } from '@/lib/content/clean-html';
import { oneH1Html } from '@/lib/content/one-h1';
-import { NO_DRAFT_RULES } from '@/lib/content/rules';
+import { startingRules } from '@/lib/content/rules';
import {
contentEditorPath,
type ContentOverlap,
@@ -552,7 +552,8 @@ export function NewContentDialog({
method: startMethod,
sourceUrl: existing?.url ?? start?.sourceUrl ?? null,
bodyHtml: bodyHtml ?? '',
- rules: NO_DRAFT_RULES,
+ // As the draft started: a page brought in aims at its own length.
+ rules: startingRules(startMethod, bodyHtml),
language: brandLanguage,
},
describeRule,
diff --git a/web/src/lib/actions/content-drafts-start.test.ts b/web/src/lib/actions/content-drafts-start.test.ts
index 587712d14..fa0a34cbb 100644
--- a/web/src/lib/actions/content-drafts-start.test.ts
+++ b/web/src/lib/actions/content-drafts-start.test.ts
@@ -402,6 +402,40 @@ describe('createContentDraft', () => {
});
});
+ it('keeps an optimized page’s own length for its Length rule (F)', async () => {
+ const page = `<h1>Pricing</h1><p>${'Plans &amp; prices. '.repeat(10)}</p>`;
+ await createContentDraft(
+ BRAND,
+ start({
+ method: 'optimize',
+ title: 'Pricing',
+ pageType: 'pricing',
+ source: { kind: 'content' },
+ bodyHtml: page,
+ }),
+ );
+ // "Pricing Plans x prices. …": the words, one space between blocks.
+ expect(inserted()).toMatchObject({ rules: { importLength: 167 } });
+ });
+
+ it('keeps the live page’s length when Start optimizes it, and sets none without it (F)', async () => {
+ const page = `<h1>Pricing</h1><p>${'Plans &amp; prices. '.repeat(10)}</p>`;
+ reads = {
+ content_opportunities: { id: OPPORTUNITY, prompt_id: PROMPT, cluster_id: null },
+ prompts: [{ id: PROMPT }],
+ };
+ await createContentDraft(
+ BRAND,
+ start({ method: 'optimize', bodyHtml: page, sourceUrl: 'https://northwind.example/pricing' }),
+ );
+ expect(inserted()).toMatchObject({ rules: { importLength: 167 } });
+
+ // A page whose live text couldn't be read starts with its title only: no rules.
+ calls.length = 0;
+ await createContentDraft(BRAND, start({ method: 'optimize' }));
+ expect(inserted()).not.toHaveProperty('rules');
+ });
+
it('starts a page brought in with the prompts its title and headings speak to', async () => {
reads = {
prompts: [
@@ -711,6 +745,8 @@ describe('startDraftFromAction', () => {
opportunity_id: OPPORTUNITY,
prompt_ids: [PROMPT],
action_id: ACTION,
+ // The live page's own length, raised to the Length rule's least.
+ rules: expect.objectContaining({ importLength: 100 }),
}),
},
]);
diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.ts
index 184783114..0f74f004e 100644
--- a/web/src/lib/actions/content-drafts.ts
+++ b/web/src/lib/actions/content-drafts.ts
@@ -42,6 +42,7 @@ import {
draftRulesSchema,
ruleSetFits,
savedRuleSetSchema,
+ startingRules,
type DraftRulesInput,
type SavedRuleSet,
} from '@/lib/content/rules';
@@ -781,6 +782,7 @@ export async function startDraftFromAction(
: title
? `<h1>${escapeHtml(title)}</h1>`
: '';
+ const rules = startingRules(method, bodyHtml !== undefined ? body : undefined);
const wantedPrompts = Array.isArray(payload.promptIds)
? payload.promptIds
@@ -834,6 +836,8 @@ export async function startDraftFromAction(
source_url: page,
prompt_ids: [...new Set((promptsRes.data ?? []).map((p) => p.id))],
action_id: actionId,
+ // A page brought in to optimize aims at its own length, not a new page's.
+ ...(rules.importLength ? { rules } : {}),
})
.select('id')
.single();
@@ -985,6 +989,8 @@ export async function createContentDraft(brandId: string, start: DraftStart): Pr
title,
html: bodyHtml !== undefined ? bodyHtmlKept : '',
});
+ // A page brought in to optimize aims at its own length, not a new page's.
+ const rules = startingRules(method, bodyHtml !== undefined ? bodyHtmlKept : undefined);
const { data, error } = await supabase
.from('content_drafts')
@@ -999,6 +1005,7 @@ export async function createContentDraft(brandId: string, start: DraftStart): Pr
opportunity_id: opportunityId,
source_url: sourceUrl ?? null,
prompt_ids: [...new Set(promptIds)].slice(0, MAX_DRAFT_PROMPTS),
+ ...(rules.importLength ? { rules } : {}),
})
.select('id')
.single();
diff --git a/web/src/lib/content/agent-write.ts b/web/src/lib/content/agent-write.ts
index 8d1360d0a..4bf20b85b 100644
--- a/web/src/lib/content/agent-write.ts
+++ b/web/src/lib/content/agent-write.ts
@@ -93,7 +93,10 @@ export async function agentWriteRequest(
const effective = effectiveRules(draft.rules, setType ? mine?.[setType] : undefined);
const rules = (
inEffect?.rules ??
- activeRules(ruleSet(setType, { language: draft.language }), effective).filter((rule) => rule.on)
+ activeRules(
+ ruleSet(setType, { language: draft.language, length: draft.rules.importLength }),
+ effective,
+ ).filter((rule) => rule.on)
)
// Heading levels are the editor's (one H1), never the agent's.
.filter((rule) => rule.check !== 'feed' && !NOT_FOR_AGENT.has(rule.key));
diff --git a/web/src/lib/content/rules.test.ts b/web/src/lib/content/rules.test.ts
index ab93a86d1..d34f64e4c 100644
--- a/web/src/lib/content/rules.test.ts
+++ b/web/src/lib/content/rules.test.ts
@@ -19,6 +19,12 @@ import {
draftRulesFit,
ruleSetFits,
storedBytes,
+ overridesForImport,
+ setToSave,
+ pageTextLength,
+ startingRules,
+ RULE_LENGTH_MIN,
+ RULE_LENGTH_MAX,
} from './rules';
const ctx: RuleContext = {
@@ -543,6 +549,69 @@ describe('My rules and text rules', () => {
expect(effectiveRules(NO_DRAFT_RULES, undefined)).toEqual(NO_DRAFT_RULES);
});
+ it('aims a page brought in to optimize at its own length, over the set’s (F)', () => {
+ const imported = { ...NO_DRAFT_RULES, importLength: 18_000 };
+ const withLength = {
+ ...mine,
+ overrides: { ...mine.overrides, length: { on: true, value: '2000' } },
+ };
+ // My rules apply, but not their length: that's for a new page.
+ expect(effectiveRules(imported, withLength).overrides).toEqual({
+ sentence: { value: '120' },
+ length: { on: true },
+ });
+ const [length] = activeRules(
+ ruleSet('blog_post', { length: imported.importLength }).filter((r) => r.key === 'length'),
+ effectiveRules(imported, withLength),
+ );
+ expect(length.current).toBe('18000');
+ // Kept through a save.
+ expect(draftRulesSchema.parse(imported).importLength).toBe(18_000);
+ // Without one, the type's length, as before.
+ expect(ruleSet('blog_post').find((r) => r.key === 'length')!.value).toBe('3000');
+ });
+
+ it('keeps the page’s own length when My rules are picked or saved (F)', () => {
+ const withLength = { length: { on: true, value: '2000' }, sentence: { value: '120' } };
+ // Picked from the set menu: as the untouched draft showed it.
+ expect(overridesForImport(withLength, 18_000)).toEqual({
+ length: { on: true },
+ sentence: { value: '120' },
+ });
+ expect(overridesForImport(withLength, undefined)).toBe(withLength);
+ // Saved as My rules: the workspace's Length value stays…
+ const imported = {
+ ...NO_DRAFT_RULES,
+ overrides: { length: { on: true }, faq: { on: false } },
+ importLength: 18_000,
+ };
+ expect(setToSave(imported, { overrides: withLength, textRules: [] })).toEqual({
+ overrides: { length: { on: true, value: '2000' }, faq: { on: false } },
+ textRules: [],
+ });
+ // …a value the person gave is theirs, and a set without one gets none.
+ const typed = { ...imported, overrides: { length: { value: '15000' } } };
+ expect(setToSave(typed, { overrides: withLength, textRules: [] }).overrides).toEqual({
+ length: { value: '15000' },
+ });
+ expect(setToSave(imported, undefined).overrides).toEqual(imported.overrides);
+ });
+
+ it('starts only a page brought in to optimize at its own length (F)', () => {
+ expect(pageTextLength('<h1>Pricing</h1><p>Plans &amp; prices</p>')).toBe(22);
+ expect(pageTextLength('<p>a<script>x()</script> b</p><style>p{}</style>')).toBe(3);
+ const page = `<h1>Pricing</h1><p>${'word '.repeat(40)}</p>`;
+ expect(startingRules('optimize', page)).toEqual({ ...NO_DRAFT_RULES, importLength: 207 });
+ // Within the range the Length rule takes.
+ expect(startingRules('optimize', '<p>Short</p>').importLength).toBe(RULE_LENGTH_MIN);
+ expect(startingRules('optimize', `<p>${'x'.repeat(60_000)}</p>`).importLength).toBe(
+ RULE_LENGTH_MAX,
+ );
+ expect(startingRules('optimize', undefined)).toBe(NO_DRAFT_RULES);
+ expect(startingRules('optimize', '<p> </p>')).toBe(NO_DRAFT_RULES);
+ expect(startingRules('create', page)).toBe(NO_DRAFT_RULES);
+ });
+
it('keeps a draft’s own choices, and a reset draft on the default', () => {
const own = { ...NO_DRAFT_RULES, overrides: { faq: { on: false } } };
expect(effectiveRules(own, mine)).toEqual(own);
diff --git a/web/src/lib/content/rules.ts b/web/src/lib/content/rules.ts
index 31209e132..2281b11fa 100644
--- a/web/src/lib/content/rules.ts
+++ b/web/src/lib/content/rules.ts
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { LANGUAGES } from '@/config/prompt-options';
import { classifyDomain, normalizeDomain } from '@/lib/citations/classify';
-import { PAGE_TYPES, type PageType } from '@/lib/content/drafts';
+import { PAGE_TYPES, type DraftMethod, type PageType } from '@/lib/content/drafts';
import { lengthTarget } from '@/lib/content/editor';
import { decodeHtmlBlock } from '@/lib/content/html-allowlist';
import { wordsOf } from '@/lib/content/sources';
@@ -255,16 +255,21 @@ export function languageRuleValue(code: string | null | undefined): string {
return label ? label.replace(/\s*\([^)]*\)$/, '') : 'English (US)';
}
-/** The rules of a set: the type's own, then the shared groups, with the type's length. */
+/**
+ * The rules of a set: the type's own, then the shared groups, with the
+ * type's length, or the page's own when it was brought in to optimize.
+ */
export function ruleSet(
pageType: PageType | null,
- defaults: { keywords?: string[]; language?: string | null } = {},
+ defaults: { keywords?: string[]; language?: string | null; length?: number | null } = {},
): RuleDef[] {
const own = (pageType && TYPE_RULES[pageType]) || [];
return [
...own,
...SHARED_RULES.map((rule) => {
- if (rule.key === 'length') return { ...rule, value: String(lengthTarget(pageType)) };
+ if (rule.key === 'length') {
+ return { ...rule, value: String(defaults.length ?? lengthTarget(pageType)) };
+ }
if (rule.key === 'language') return { ...rule, value: languageRuleValue(defaults.language) };
if (rule.key === 'keywords' && defaults.keywords?.length) {
return { ...rule, value: defaults.keywords.join(', ') };
@@ -304,6 +309,12 @@ export const draftRulesSchema = z.object({
source: z.enum(['default', 'mine']).nullable().default(null),
overrides: overridesSchema,
textRules: z.array(textRuleSchema).max(MAX_TEXT_RULES).default([]),
+ /**
+ * A page brought in to optimize: its length when it came in, in
+ * characters. Its Length rule aims there (±20%), not at the type's length
+ * for a new page; the person can still change it.
+ */
+ importLength: z.number().int().min(1).max(10_000_000).optional(),
});
export type DraftRules = z.infer<typeof draftRulesSchema>;
/** As saved: the source and text rules may be left out (rules from before 00214 have neither). */
@@ -363,11 +374,78 @@ export function effectiveRules(rules: DraftRules, mine: SavedRuleSet | undefined
const untouched =
rules.source === null && !Object.keys(rules.overrides).length && !rules.textRules.length;
if (untouched && mine) {
- return { ...rules, source: 'mine', overrides: mine.overrides, textRules: mine.textRules };
+ return {
+ ...rules,
+ source: 'mine',
+ overrides: overridesForImport(mine.overrides, rules.importLength),
+ textRules: mine.textRules,
+ };
}
return rules;
}
+/**
+ * A set's choices as a page brought in to optimize takes them: the set's
+ * Length value is for a new page, so the page's own length (importLength)
+ * stands in for it; on or off still follows the set.
+ */
+export function overridesForImport(
+ overrides: SavedRuleSet['overrides'],
+ importLength: number | undefined,
+): SavedRuleSet['overrides'] {
+ const length = overrides.length;
+ if (!importLength || length?.value === undefined) return overrides;
+ return { ...overrides, length: { on: length.on } };
+}
+
+/**
+ * The set Save as my rules keeps from a draft. A page brought in shows its
+ * own length, which isn't a choice: the workspace's Length value stays.
+ */
+export function setToSave(rules: DraftRules, mine: SavedRuleSet | undefined): SavedRuleSet {
+ const kept = mine?.overrides.length?.value;
+ const length = rules.overrides.length;
+ if (!rules.importLength || length?.value !== undefined || kept === undefined) {
+ return { overrides: rules.overrides, textRules: rules.textRules };
+ }
+ return {
+ overrides: { ...rules.overrides, length: { ...length, value: kept } },
+ textRules: rules.textRules,
+ };
+}
+
+/**
+ * A page's text length in characters, about as the editor counts it: the
+ * words without tags, runs of spaces as one (a space between blocks, where
+ * the editor counts none, so a little over). Read where there is no editor
+ * to count: on the server, and before a draft opens.
+ */
+export function pageTextLength(html: string): number {
+ return html
+ .replace(/<(script|style)\b[\s\S]*?<\/\1>/gi, ' ')
+ .replace(/<\/?(p|h[1-6]|li|td|th|blockquote|br|div)\b[^>]*>/gi, ' ')
+ .replace(/<[^>]*>/g, '')
+ .replace(/&nbsp;/g, ' ')
+ .replace(/&[#a-z0-9]+;/gi, 'x')
+ .replace(/\s+/g, ' ')
+ .trim().length;
+}
+
+/**
+ * The rules a draft starts with. A page brought in to optimize aims its
+ * Length rule at its own length (within the range the rule takes), not a
+ * new page's; anything else starts with none.
+ */
+export function startingRules(method: DraftMethod, bodyHtml: string | undefined): DraftRules {
+ if (method !== 'optimize' || bodyHtml === undefined) return NO_DRAFT_RULES;
+ const length = pageTextLength(bodyHtml);
+ if (!length) return NO_DRAFT_RULES;
+ return {
+ ...NO_DRAFT_RULES,
+ importLength: Math.min(RULE_LENGTH_MAX, Math.max(RULE_LENGTH_MIN, length)),
+ };
+}
+
/**
* Paste many: one rule per line, bullets and numbering taken off ("- ",
* "• ", "1. ", "2) "), empty lines dropped, each cut to the limit.