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): send Content Editor pages to the brand's Webflow collection as draft items (Send to CMS #1331, phase 2 part 2: Webflow send) (#1504)
Cihan1525d0da8b25
diff --git a/server/src/lib/cms/webflow-delivery.js b/server/src/lib/cms/webflow-delivery.jsnew file mode 100644index 000000000..45584d67f--- /dev/null+++ b/server/src/lib/cms/webflow-delivery.js@@ -0,0 +1,88 @@+import supabaseAdmin from '../../config/supabase.js';+import { logger } from '../logger.js';+import { WebflowError, createItem, updateItem } from './webflow.js';+import { fieldDataFor, toWebflowRichText, webflowSlug } from './webflow-items.js';++/**+ * Send to CMS to Webflow (#1331, phase 2): a page goes to the brand's+ * Webflow collection (00283) as a draft item, or updates the item its last+ * Webflow send made in that collection. Nothing is published: Webflow's own+ * publish puts an item live. content-delivery.js reserves, writes and+ * batches the deliveries as it does for the webhook.+ */++/** The item the page's last Webflow send made in this collection, if any. */+async function lastItem(draftId, collectionId, deliveryId) {+ const { data, error } = await supabaseAdmin+ .from('content_draft_deliveries')+ .select('external_id')+ .eq('draft_id', draftId)+ .eq('target', 'webflow')+ .eq('target_collection', collectionId)+ .in('status', ['sent', 'published'])+ .not('external_id', 'is', null)+ .neq('id', deliveryId)+ .order('created_at', { ascending: false })+ .limit(1)+ .maybeSingle();+ if (error) throw new Error(error.message);+ return data?.external_id ?? null;+}++/** The token the send was refused with: Settings asks for a new one. */+async function markRefused(connection) {+ const { error } = await supabaseAdmin+ .from('brand_cms_connections')+ .update({ status: 'auth_failed', updated_at: new Date().toISOString() })+ .eq('brand_id', connection.brand_id)+ .eq('provider', 'webflow')+ .eq('composio_account_id', connection.composio_account_id);+ if (error) logger.warn({ err: error }, '[content-delivery] webflow refusal not kept');+}++/**+ * Sends one page to the brand's Webflow collection: the item its last send+ * made there is updated (a new one is made when it was deleted in Webflow),+ * else a new draft item is made. What went wrong is said in our words.+ *+ * @returns {Promise<{ taken: boolean, failure?: string, refused?: boolean, externalId?: string,+ * collectionId?: string, updated?: boolean, changed?: { tables: number, images: number } }>}+ */+export async function sendToWebflow({ delivery, page, html, jsonLd, connection }) {+ const account = {+ entityId: connection.composio_entity_id,+ accountId: connection.composio_account_id,+ };+ const collectionId = connection.collection_id;+ const { html: body, changed } = toWebflowRichText(html);+ // A slug in another script has nothing left: the draft's own, the same every send.+ const slug = webflowSlug(page.slug) ?? webflowSlug(page.title) ?? `page-${page.draftId}`;+ const fieldData = fieldDataFor({ page, slug, body, jsonLd }, connection.field_map ?? {});+ try {+ const before = await lastItem(page.draftId, collectionId, delivery.id);+ let itemId = null;+ if (before) {+ try {+ itemId = await updateItem(account, collectionId, before, fieldData);+ } catch (err) {+ if (!(err instanceof WebflowError && err.code === 'not_found')) throw err;+ }+ }+ const updated = Boolean(itemId);+ itemId ??= await createItem(account, collectionId, fieldData);+ return { taken: true, externalId: itemId, collectionId, updated, changed };+ } catch (err) {+ if (err instanceof WebflowError) {+ if (err.code === 'auth_failed') {+ await markRefused(connection);+ return { taken: false, failure: err.message, refused: true };+ }+ return { taken: false, failure: err.message };+ }+ logger.warn(+ { err: { name: err?.name }, deliveryId: delivery.id },+ '[content-delivery] webflow send failed',+ );+ return { taken: false, failure: 'Webflow could not be reached' };+ }+}diff --git a/server/src/lib/cms/webflow-delivery.test.js b/server/src/lib/cms/webflow-delivery.test.jsnew file mode 100644index 000000000..e35b53ab0--- /dev/null+++ b/server/src/lib/cms/webflow-delivery.test.js@@ -0,0 +1,169 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++/**+ * Send to CMS to Webflow (#1331, phase 2): a page goes to the brand's+ * collection as a draft item, or updates the item its last send made there.+ */++let previous;+const queries = [];+const updates = [];+vi.mock('../../config/supabase.js', () => ({+ default: {+ from: (table) => {+ const q = { table, filters: [] };+ const chain = {+ select: () => chain,+ eq: (col, value) => (q.filters.push(['eq', col, value]), chain),+ neq: (col, value) => (q.filters.push(['neq', col, value]), chain),+ in: (col, value) => (q.filters.push(['in', col, value]), chain),+ not: (col, op, value) => (q.filters.push(['not', col, op, value]), chain),+ order: () => chain,+ limit: () => chain,+ update: (row) => (updates.push({ table, row, filters: q.filters }), chain),+ maybeSingle: async () => {+ queries.push(q);+ return { data: previous ? { external_id: previous } : null, error: null };+ },+ then: (resolve) => resolve({ data: null, error: null }),+ };+ return chain;+ },+ },+}));+const logged = [];+vi.mock('../logger.js', () => ({+ logger: { warn: (...args) => logged.push(JSON.stringify(args)) },+}));+const webflow = { createItem: vi.fn(), updateItem: vi.fn() };+vi.mock('./webflow.js', async (importOriginal) => ({+ ...(await importOriginal()),+ createItem: (...args) => webflow.createItem(...args),+ updateItem: (...args) => webflow.updateItem(...args),+}));++const { sendToWebflow } = await import('./webflow-delivery.js');+const { WebflowError } = await import('./webflow.js');++const connection = {+ brand_id: 'b1',+ composio_entity_id: 'brand_b1',+ composio_account_id: 'ca_1',+ collection_id: 'c1',+ field_map: { body: 'post-body', metaDescription: 'post-summary' },+};+const account = { entityId: 'brand_b1', accountId: 'ca_1' };+const send = (over = {}) =>+ sendToWebflow({+ delivery: { id: 'd2345678-0000-0000-0000-000000000000' },+ page: {+ draftId: 'dr1',+ title: 'Cold brew at home',+ slug: 'cold-brew-at-home',+ metaTitle: '',+ metaDescription: 'How to make it.',+ ...over,+ },+ html: '<h2>Why</h2><p>Smooth.</p><table><tr><td>a</td></tr></table><img src="https://x.example/a.png">',+ jsonLd: [],+ connection,+ });++beforeEach(() => {+ previous = null;+ queries.length = 0;+ updates.length = 0;+ logged.length = 0;+ webflow.createItem.mockReset().mockResolvedValue('item-new');+ webflow.updateItem.mockReset().mockResolvedValue('item-old');+});++describe('sendToWebflow', () => {+ it('makes a draft item of a page sent for the first time', async () => {+ expect(await send()).toEqual({+ taken: true,+ externalId: 'item-new',+ collectionId: 'c1',+ updated: false,+ changed: { tables: 1, images: 1 },+ });+ expect(webflow.createItem).toHaveBeenCalledWith(account, 'c1', {+ name: 'Cold brew at home',+ slug: 'cold-brew-at-home',+ 'post-body': '<h2>Why</h2><p>Smooth.</p><ul><li>a</li></ul>',+ 'post-summary': 'How to make it.',+ });+ expect(webflow.updateItem).not.toHaveBeenCalled();+ // The page's last item is looked for in this collection, from a send that went.+ expect(queries[0].filters).toEqual([+ ['eq', 'draft_id', 'dr1'],+ ['eq', 'target', 'webflow'],+ ['eq', 'target_collection', 'c1'],+ ['in', 'status', ['sent', 'published']],+ ['not', 'external_id', 'is', null],+ ['neq', 'id', 'd2345678-0000-0000-0000-000000000000'],+ ]);+ });++ it('updates the item the page’s last send made, or makes a new one when it was deleted in Webflow', async () => {+ previous = 'item-old';+ expect(await send()).toMatchObject({ taken: true, externalId: 'item-old', updated: true });+ expect(webflow.updateItem).toHaveBeenCalledWith(account, 'c1', 'item-old', expect.any(Object));+ expect(webflow.createItem).not.toHaveBeenCalled();++ webflow.updateItem.mockRejectedValueOnce(new WebflowError('not_found', 'gone'));+ expect(await send()).toMatchObject({ taken: true, externalId: 'item-new', updated: false });++ // Any other refusal of the update is the send's: no second item.+ webflow.createItem.mockClear();+ webflow.updateItem.mockRejectedValueOnce(+ new WebflowError('invalid', 'Webflow refused the item'),+ );+ expect(await send()).toMatchObject({ taken: false, failure: 'Webflow refused the item' });+ expect(webflow.createItem).not.toHaveBeenCalled();+ });++ it('gives Webflow a slug it takes, from the title when the page’s has nothing left', async () => {+ await send({ slug: 'soğuk.demleme' });+ expect(webflow.createItem.mock.calls[0][2].slug).toBe('soguk-demleme');+ await send({ slug: '…', title: 'Café crème' });+ expect(webflow.createItem.mock.calls[1][2].slug).toBe('cafe-creme');+ // Another script: the draft's own, the same on every send.+ await send({ slug: 'привет-мир', title: '日本語' });+ await send({ slug: 'привет-мир', title: '日本語' });+ expect(webflow.createItem.mock.calls[2][2].slug).toBe('page-dr1');+ expect(webflow.createItem.mock.calls[3][2].slug).toBe('page-dr1');+ });++ it('marks the token refused when Webflow refuses it, and says what went wrong in its own words', async () => {+ webflow.createItem.mockRejectedValueOnce(+ new WebflowError('auth_failed', 'Webflow refused the site token'),+ );+ expect(await send()).toEqual({+ taken: false,+ failure: 'Webflow refused the site token',+ refused: true,+ });+ expect(updates).toEqual([+ {+ table: 'brand_cms_connections',+ row: { status: 'auth_failed', updated_at: expect.any(String) },+ filters: [+ ['eq', 'brand_id', 'b1'],+ ['eq', 'provider', 'webflow'],+ ['eq', 'composio_account_id', 'ca_1'],+ ],+ },+ ]);++ webflow.createItem.mockRejectedValueOnce(+ new WebflowError('slug_taken', 'Another item in the collection has this slug'),+ );+ expect(await send()).toMatchObject({ failure: 'Another item in the collection has this slug' });+ expect(updates).toHaveLength(1);++ webflow.createItem.mockRejectedValueOnce(new Error('socket hang up at token wf_secret'));+ expect(await send()).toEqual({ taken: false, failure: 'Webflow could not be reached' });+ expect(logged.join('')).not.toContain('wf_secret');+ });+});diff --git a/server/src/lib/cms/webflow-items.js b/server/src/lib/cms/webflow-items.jsnew file mode 100644index 000000000..35ab0651e--- /dev/null+++ b/server/src/lib/cms/webflow-items.js@@ -0,0 +1,247 @@+import * as cheerio from 'cheerio';++/**+ * A page as a Webflow CMS item (Send to CMS #1331, phase 2): its rich text+ * as Webflow keeps it, a slug Webflow takes, and the item's fields by the+ * brand's field map (brand_cms_connections.field_map, 00283).+ */++/** What Webflow's rich text keeps; other elements give way to their content. */+const KEEP = new Set([+ 'h2',+ 'h3',+ 'h4',+ 'h5',+ 'h6',+ 'p',+ 'blockquote',+ 'ul',+ 'ol',+ 'li',+ 'a',+ 'strong',+ 'b',+ 'em',+ 'i',+ 'sup',+ 'sub',+ 'br',+]);+/** The blocks rich text keeps at the top: anything else there goes in a paragraph. */+const BLOCKS = new Set(['h2', 'h3', 'h4', 'h5', 'h6', 'p', 'blockquote', 'ul', 'ol']);+/** What a table cell keeps: its words, links and emphasis. */+const CELL_KEEP = new Set(['a', 'strong', 'b', 'em', 'i']);+/** Media, left out until workspace files (CHAT-06, #1331). */+const MEDIA = 'img, picture, video, audio, svg, canvas, iframe, embed, object';+/** Left out with what they hold: scripts (the JSON-LD has its own field), forms and rules. */+const DROP =+ 'script, style, source, track, map, hr, noscript, template, form, button, input, select, textarea';+/** The meta description's fallback: the page's opening words, as long as a search result shows. */+const OPENING_CHARS = 160;+/** Webflow's longest name and plain text it's sent here. */+const NAME_CHARS = 256;++const escape = (value) =>+ String(value).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');+const squash = (value) => String(value).replace(/\s+/g, ' ').trim();++/** Attributes go but a link's address and target. */+function bare($, el) {+ const tag = el.tagName?.toLowerCase();+ for (const name of Object.keys(el.attribs ?? {})) {+ if (tag === 'a' && (name === 'href' || name === 'target')) continue;+ $(el).removeAttr(name);+ }+}++/**+ * A table cell as one line: its paragraphs, items and lines a space apart,+ * its links and emphasis kept.+ */+function cellHtml($, cell) {+ const $cell = $(cell).clone();+ $cell.find('br').replaceWith(' ');+ $cell+ .find('p, div, li, ul, ol, h1, h2, h3, h4, h5, h6, blockquote, dt, dd')+ .prepend(' ')+ .append(' ');+ for (const el of $cell.find('*').toArray().reverse()) {+ if (CELL_KEEP.has(el.tagName?.toLowerCase())) bare($, el);+ else $(el).replaceWith($(el).contents());+ }+ return squash($cell.html() ?? '');+}++/** A table's own rows, not those of a table inside one of its cells. */+const ownRows = ($, table) =>+ $(table)+ .find('tr')+ .toArray()+ .filter((row) => $(row).closest('table')[0] === table);++/**+ * A page's publishable HTML as Webflow's rich text keeps it:+ * - a table becomes a list, a row an item ("Header: cell · …"), a table in+ * a cell first: rich text has no tables (#1331);+ * - images and media are left out until workspace files (CHAT-06, #1331),+ * a figure of one with its caption; scripts (the JSON-LD has its own+ * field), embeds, forms and rules go too;+ * - the page's title is the item's name, so a heading 1 left in the body+ * reads as a heading 2;+ * - a code block, a definition list, a caption and words outside a block+ * are paragraphs, an item outside a list too;+ * - any other element gives way to its content, and attributes go but a+ * link's address and target.+ * Returns the HTML, and how many tables and images it changed (the send's note).+ */+export function toWebflowRichText(html) {+ const $ = cheerio.load(String(html ?? ''), null, false);+ const changed = {+ tables: $('table').filter((_, t) => !$(t).parents('table').length).length,+ images:+ $('img, video, svg').length + $('picture').filter((_, p) => !$(p).find('img').length).length,+ };+ $(DROP).remove();+ $('*')+ .contents()+ .add($.root().contents())+ .filter((_, node) => node.type === 'comment')+ .remove();++ // Inner tables first: a cell holding one reads it as its list.+ for (const table of $('table').toArray().reverse()) {+ const rows = ownRows($, table);+ const own = (row) =>+ $(row)+ .children('th, td')+ .toArray()+ .map((cell) => cellHtml($, cell));+ const head =+ rows.length && $(rows[0]).children('th').length+ ? $(rows[0])+ .children('th, td')+ .toArray()+ .map((cell) => squash($(cell).text()))+ : null;+ const items = (head ? rows.slice(1) : rows)+ .map((row) =>+ own(row)+ .map((value, i) =>+ value && head?.[i] ? `<strong>${escape(head[i])}</strong>: ${value}` : value,+ )+ .filter(Boolean)+ .join(' · '),+ )+ .filter(Boolean);+ $(table).replaceWith(+ items.length ? `<ul>${items.map((item) => `<li>${item}</li>`).join('')}</ul>` : '',+ );+ }++ // A figure of media goes with its caption; a figure of words (a table's) keeps them.+ $('figure').each((_, figure) => {+ if ($(figure).find(MEDIA).length) $(figure).remove();+ });+ $(MEDIA).remove();+ $('h1').each((_, el) => {+ el.tagName = 'h2';+ });+ $('pre').each((_, el) => {+ $(el).replaceWith(`<p>${escape($(el).text().trim())}</p>`);+ });+ $('dt, dd, figcaption, caption').each((_, el) => {+ $(el).replaceWith(`<p>${$(el).html() ?? ''}</p>`);+ });++ // Inside out, so an element's children are settled before it gives way.+ for (const el of $('*').toArray().reverse()) {+ if (KEEP.has(el.tagName?.toLowerCase())) bare($, el);+ else $(el).replaceWith($(el).contents());+ }+ $('li').each((_, el) => {+ if (!$(el).parent().is('ul, ol')) el.tagName = 'p';+ });++ // Words and inline elements at the top go in paragraphs, a run at a time.+ let run = [];+ const wrap = () => {+ if (run.some((node) => squash($(node).text()))) $(run).wrapAll('<p></p>');+ else $(run).remove();+ run = [];+ };+ for (const node of $.root().contents().toArray()) {+ if (node.type === 'tag' && BLOCKS.has(node.tagName.toLowerCase())) wrap();+ else if (node.type === 'tag' || node.type === 'text') run.push(node);+ }+ wrap();++ // The paragraphs, items and lists a removal left empty.+ $('p, li, h2, h3, h4, h5, h6, blockquote').each((_, el) => {+ if (!squash($(el).text()) && !$(el).find('br').length) $(el).remove();+ });+ $('ul, ol').each((_, el) => {+ if (!$(el).children('li').length) $(el).remove();+ });+ return { html: $.html().trim(), changed };+}++/** Letters Unicode doesn't take apart into a base letter and its mark. */+const LETTERS = { ı: 'i', ß: 'ss', æ: 'ae', œ: 'oe', ø: 'o', đ: 'd', ł: 'l', þ: 'th' };++/**+ * A slug Webflow takes: lowercase letters, digits, '-' and '_', starting+ * with a letter, digit or '_' (marks taken off, "ç" → "c"). Null when+ * nothing of it is left (a slug in another script).+ */+export function webflowSlug(value) {+ const slug = String(value ?? '')+ .toLowerCase()+ .replace(/[ıßæœøđłþ]/g, (c) => LETTERS[c])+ .normalize('NFKD')+ .replace(/\p{M}/gu, '')+ .replace(/[^a-z0-9_-]+/g, '-')+ .replace(/-{2,}/g, '-')+ .replace(/^-+|-+$/g, '')+ .slice(0, 200)+ .replace(/-+$/, '');+ return slug || null;+}++/** The page's opening words, cut at a word: its meta description when it has none. */+function openingWords(html) {+ const text = squash(+ cheerio+ .load(String(html ?? ''), null, false)+ .root()+ .text(),+ );+ if (text.length <= OPENING_CHARS) return text;+ const cut = text.slice(0, OPENING_CHARS - 1);+ const space = cut.lastIndexOf(' ');+ return `${(space > 80 ? cut.slice(0, space) : cut).trim()}…`;+}++/**+ * The item's fields: its name and slug, and each part of the page in the+ * field the brand's map gives it. The meta title falls back to the title,+ * the meta description to the page's opening words. JSON-LD goes as its+ * JSON (for an embed in the collection's template), empty when the page has+ * none, so an item sent again loses what the page no longer says.+ */+export function fieldDataFor({ page, slug, body, jsonLd }, fieldMap) {+ const data = { name: squash(page.title).slice(0, NAME_CHARS), slug };+ if (fieldMap.body) data[fieldMap.body] = body;+ if (fieldMap.metaTitle) {+ data[fieldMap.metaTitle] = squash(page.metaTitle || page.title).slice(0, NAME_CHARS);+ }+ if (fieldMap.metaDescription) {+ data[fieldMap.metaDescription] = squash(page.metaDescription) || openingWords(body);+ }+ if (fieldMap.jsonLd) {+ // Every "<" escaped: nothing in it can end or open a tag in the embed's script.+ data[fieldMap.jsonLd] = jsonLd?.length+ ? JSON.stringify(jsonLd.length === 1 ? jsonLd[0] : jsonLd).replace(/</g, '\\u003c')+ : '';+ }+ return data;+}diff --git a/server/src/lib/cms/webflow-items.test.js b/server/src/lib/cms/webflow-items.test.jsnew file mode 100644index 000000000..aa2b2b735--- /dev/null+++ b/server/src/lib/cms/webflow-items.test.js@@ -0,0 +1,150 @@+import { describe, expect, it } from 'vitest';+import { fieldDataFor, toWebflowRichText, webflowSlug } from './webflow-items.js';++/** A page as a Webflow CMS item (Send to CMS #1331, phase 2). */++describe('toWebflowRichText', () => {+ it('keeps what Webflow’s rich text keeps, a link’s address and target only', () => {+ const { html, changed } = toWebflowRichText(+ '<h2 id="x" class="big">Why</h2><p style="color:red">Cold <strong>brew</strong> is <a href="https://acme.example/brew" target="_blank" rel="noopener" class="l">smooth</a>.</p><ul><li>One</li></ul><blockquote><p>Said so.</p></blockquote>',+ );+ expect(html).toBe(+ '<h2>Why</h2><p>Cold <strong>brew</strong> is <a href="https://acme.example/brew" target="_blank">smooth</a>.</p><ul><li>One</li></ul><blockquote><p>Said so.</p></blockquote>',+ );+ expect(changed).toEqual({ tables: 0, images: 0 });+ });++ it('turns a table into a list, a row an item, its header naming each cell', () => {+ const { html, changed } = toWebflowRichText(+ '<table><thead><tr><th>Plan</th><th>Price</th></tr></thead><tbody><tr><td>Starter</td><td>$49</td></tr><tr><td>Growth</td><td></td></tr></tbody></table>',+ );+ expect(html).toBe(+ '<ul><li><strong>Plan</strong>: Starter · <strong>Price</strong>: $49</li><li><strong>Plan</strong>: Growth</li></ul>',+ );+ expect(changed.tables).toBe(1);+ // No header row: the cells, one after another.+ expect(toWebflowRichText('<table><tr><td>a <b></td><td>c</td></tr></table>').html).toBe(+ '<ul><li>a <b> · c</li></ul>',+ );+ });++ it('leaves images, media, scripts and embeds out, and counts the images', () => {+ const { html, changed } = toWebflowRichText(+ '<p>Intro</p><figure><img src="https://acme.example/a.png" alt="A"><figcaption>A</figcaption></figure><p><img src="https://acme.example/b.png"></p><script type="application/ld+json">{"@type":"Article"}</script><iframe src="https://x.example"></iframe><hr><p>End</p>',+ );+ expect(html).toBe('<p>Intro</p><p>End</p>');+ expect(changed.images).toBe(2);+ });++ it('reads a heading 1 as a heading 2, a code block and a definition list as paragraphs, and unwraps the rest', () => {+ expect(+ toWebflowRichText(+ '<div><h1>Title again</h1><section><p><span>Hi</span> <mark>there</mark></p></section><pre><code>npm i <x></code></pre><dl><dt>Term</dt><dd>Meaning</dd></dl></div>',+ ).html,+ ).toBe('<h2>Title again</h2><p>Hi there</p><p>npm i <x></p><p>Term</p><p>Meaning</p>');+ });+});++describe('toWebflowRichText, on what pages hold', () => {+ it('reads a cell’s paragraphs, items and lines a space apart, and keeps its links', () => {+ expect(+ toWebflowRichText(+ '<table><tr><th>Tool</th><th>Notes</th></tr><tr><td><p>Acme</p></td><td><p>First</p><p>Second, <a href="https://acme.example/src" class="x">source</a></p><ul><li>a</li><li>b</li></ul>x<br>y</td></tr></table>',+ ).html,+ ).toBe(+ '<ul><li><strong>Tool</strong>: Acme · <strong>Notes</strong>: First Second, <a href="https://acme.example/src">source</a> a b x y</li></ul>',+ );+ });++ it('keeps a table a figure wraps, and a table in a cell once', () => {+ const wrapped = toWebflowRichText(+ '<figure class="wp-block-table"><table><tr><td>a</td><td>b</td></tr></table><figcaption>Prices</figcaption></figure>',+ );+ expect(wrapped.html).toBe('<ul><li>a · b</li></ul><p>Prices</p>');+ expect(wrapped.changed.tables).toBe(1);+ const nested = toWebflowRichText(+ '<table><tr><td>Plan<table><tr><td>x</td><td>y</td></tr></table></td><td>z</td></tr></table>',+ );+ expect(nested.html).toBe('<ul><li>Plan x · y · z</li></ul>');+ expect(nested.changed.tables).toBe(1);+ });++ it('puts words outside a block in a paragraph, an item outside a list too, and counts a picture once', () => {+ const { html, changed } = toWebflowRichText(+ '<div>Loose <strong>words</strong></div><h2>Next</h2><menu><li>Orphan</li></menu><!-- note --><picture><source srcset="a.webp"><img src="a.png"></picture>',+ );+ expect(html).toBe('<p>Loose <strong>words</strong></p><h2>Next</h2><p>Orphan</p>');+ expect(changed.images).toBe(1);+ });+});++describe('webflowSlug', () => {+ it('makes a slug Webflow takes', () => {+ expect(webflowSlug('soğuk-demleme-kahve')).toBe('soguk-demleme-kahve');+ expect(webflowSlug('Größe_und.Preis')).toBe('grosse_und-preis');+ expect(webflowSlug('--Ilık su--')).toBe('ilik-su');+ expect(webflowSlug('…')).toBeNull();+ expect(webflowSlug('a'.repeat(250))).toHaveLength(200);+ });+});++describe('fieldDataFor', () => {+ const page = {+ title: 'Cold brew at home',+ metaTitle: '',+ metaDescription: '',+ };+ const body = `<p>${'Cold brew is coffee steeped in cold water for 12 to 18 hours. '.repeat(4)}</p>`;++ it('puts each part in the field the map gives it, falling back where a page has none', () => {+ const data = fieldDataFor(+ {+ page,+ slug: 'cold-brew-at-home',+ body,+ jsonLd: [{ '@type': 'Article', headline: '</script>' }],+ },+ {+ body: 'post-body',+ metaTitle: 'seo-title',+ metaDescription: 'post-summary',+ jsonLd: 'schema',+ },+ );+ expect(data.name).toBe('Cold brew at home');+ expect(data.slug).toBe('cold-brew-at-home');+ expect(data['post-body']).toBe(body);+ expect(data['seo-title']).toBe('Cold brew at home');+ // The opening words, cut at a word.+ expect(data['post-summary']).toMatch(/^Cold brew is coffee steeped .{60,}…$/);+ expect(data['post-summary'].length).toBeLessThanOrEqual(160);+ expect(data.schema).toBe('{"@type":"Article","headline":"\\u003c/script>"}');+ expect(JSON.parse(data.schema)).toEqual({ '@type': 'Article', headline: '</script>' });+ });++ it('sends the page’s own meta, several JSON-LD blocks as a list, and nothing for a part the map leaves out', () => {+ const data = fieldDataFor(+ {+ page: { ...page, metaTitle: 'Cold brew, simply', metaDescription: 'How to make it.' },+ slug: 's',+ body: '<p>x</p>',+ jsonLd: [{ '@type': 'Article' }, { '@type': 'FAQPage' }],+ },+ { body: 'post-body', metaDescription: 'post-summary', jsonLd: 'schema' },+ );+ expect(data).toEqual({+ name: 'Cold brew at home',+ slug: 's',+ 'post-body': '<p>x</p>',+ 'post-summary': 'How to make it.',+ schema: '[{"@type":"Article"},{"@type":"FAQPage"}]',+ });+ // No JSON-LD: the field is emptied, so an item sent again loses what the page dropped.+ expect(+ fieldDataFor(+ { page, slug: 's', body: '<p>x</p>', jsonLd: [] },+ { body: 'b', jsonLd: 'schema' },+ ),+ ).toHaveProperty('schema', '');+ });+});diff --git a/server/src/lib/cms/webflow.js b/server/src/lib/cms/webflow.jsindex 217661a3c..fa3fa7fc7 100644--- a/server/src/lib/cms/webflow.js+++ b/server/src/lib/cms/webflow.js@@ -21,7 +21,7 @@ const OWN_FIELDS = new Set(['name', 'slug']);/** A Webflow call that didn't go, with the code the web names to the person. */export class WebflowError extends Error {- /** @param {'auth_failed'|'missing_scopes'|'no_access'|'not_found'|'failed'} code */+ /** @param {'auth_failed'|'missing_scopes'|'no_access'|'not_found'|'slug_taken'|'invalid'|'failed'} code */constructor(code, message) {super(message);this.code = code;@@ -36,6 +36,10 @@ const ACCOUNT_GONE = /connected[_ ]?account[_ ]?not[_ ]?found/i;const REFUSED = /\b401\b|unauthori[sz]ed|not[_ ]authori[sz]ed|invalid[_ ](api[_ ])?(token|key)/i;const FORBIDDEN = /\b403\b|forbidden/i;const NOT_FOUND = /\b404\b|not[_ ]found/i;+/** An item Webflow won't take: its slug another item's, or a field's value. */+const SLUG_TAKEN =+ /slug[\s\S]{0,80}(unique|already|duplicate|taken|exists)|(unique|already|duplicate|taken|exists)[\s\S]{0,80}slug/i;+const INVALID = /\b(400|409|422)\b|validation|invalid[_ ](field|value|request)/i;/*** A Webflow tool's data. What went wrong is told in our words only: the@@ -63,6 +67,18 @@ async function run(slug, account, args) {if (NOT_FOUND.test(said)) {throw new WebflowError('not_found', 'Webflow has no such site or collection');}+ if (SLUG_TAKEN.test(said)) {+ throw new WebflowError(+ 'slug_taken',+ 'Another Webflow item has this slug. Change the page’s slug, or remove that item in Webflow.',+ );+ }+ if (INVALID.test(said)) {+ throw new WebflowError(+ 'invalid',+ 'Webflow refused the item: a field doesn’t take what was sent',+ );+ }throw new WebflowError('failed', 'Webflow could not be reached');}}@@ -123,6 +139,41 @@ export async function getCollection(account, collectionId) {};}+/** An item's id from Webflow's answer, however Composio wraps it. */+function itemIdOf(data) {+ const item = objectOf(data);+ const id = item?.id ?? item?.item?.id ?? item?.items?.[0]?.id;+ if (typeof id !== 'string' || !id) throw new WebflowError('failed', 'Webflow gave no item back');+ return id;+}++/** A new item in the collection, as a draft: Webflow's own publish puts it live. Its id. */+export async function createItem(account, collectionId, fieldData) {+ const data = await run('WEBFLOW_CREATE_COLLECTION_ITEM', account, {+ collection_id: collectionId,+ field_data: fieldData,+ is_draft: true,+ });+ return itemIdOf(data);+}++/**+ * The item's fields written again. Its draft or live state is left as it is:+ * a live item's change waits for Webflow's next publish. Its id.+ */+export async function updateItem(account, collectionId, itemId, fieldData) {+ const data = await run('WEBFLOW_UPDATE_COLLECTION_ITEM_V2', account, {+ collection_id: collectionId,+ item_id: itemId,+ fieldData,+ });+ try {+ return itemIdOf(data);+ } catch {+ return itemId;+ }+}+/** The words a field's slug or name is read for, by part: the first field that has them is suggested. */const HINTS = {body: [/(^|-)(post-)?body($|-)/, /content/, /article/, /rich/],diff --git a/server/src/lib/cms/webflow.test.js b/server/src/lib/cms/webflow.test.jsindex eb24d63a8..9ba0c967a 100644--- a/server/src/lib/cms/webflow.test.js+++ b/server/src/lib/cms/webflow.test.js@@ -7,8 +7,16 @@ vi.mock('../composio.js', () => ({executeWebflowTool: (...args) => executeWebflowTool(...args),}));-const { WebflowError, checkFieldMap, getCollection, listCollections, listSites, suggestFieldMap } =- await import('./webflow.js');+const {+ WebflowError,+ checkFieldMap,+ createItem,+ getCollection,+ listCollections,+ listSites,+ suggestFieldMap,+ updateItem,+} = await import('./webflow.js');const account = { entityId: 'brand_b1', accountId: 'ca_1' };@@ -193,3 +201,42 @@ describe('checkFieldMap', () => {]);});});++describe('items', () => {+ it('makes a draft item, and writes an item again leaving its state as it is', async () => {+ executeWebflowTool.mockResolvedValueOnce({ id: 'item-1', isDraft: true });+ expect(await createItem(account, 'c1', { name: 'A', slug: 'a' })).toBe('item-1');+ expect(executeWebflowTool).toHaveBeenLastCalledWith('WEBFLOW_CREATE_COLLECTION_ITEM', account, {+ collection_id: 'c1',+ field_data: { name: 'A', slug: 'a' },+ is_draft: true,+ });+ executeWebflowTool.mockResolvedValueOnce({ response_data: { id: 'item-1' } });+ expect(await updateItem(account, 'c1', 'item-1', { name: 'B', slug: 'a' })).toBe('item-1');+ expect(executeWebflowTool).toHaveBeenLastCalledWith(+ 'WEBFLOW_UPDATE_COLLECTION_ITEM_V2',+ account,+ { collection_id: 'c1', item_id: 'item-1', fieldData: { name: 'B', slug: 'a' } },+ );+ // An answer with no item: the one written.+ executeWebflowTool.mockResolvedValueOnce({});+ expect(await updateItem(account, 'c1', 'item-1', {})).toBe('item-1');+ executeWebflowTool.mockResolvedValueOnce({});+ await expect(createItem(account, 'c1', {})).rejects.toMatchObject({ code: 'failed' });+ });++ it('names an item Webflow won’t take', async () => {+ const said = async (message) => {+ executeWebflowTool.mockRejectedValueOnce(new Error(message));+ return createItem(account, 'c1', {}).catch((err) => err);+ };+ expect(+ await said("400 validation_error: Unique value is already in database: 'slug'"),+ ).toMatchObject({+ code: 'slug_taken',+ });+ expect(await said('400 Bad Request: validation_error on field post-summary')).toMatchObject({+ code: 'invalid',+ });+ });+});diff --git a/server/src/lib/content-delivery.js b/server/src/lib/content-delivery.jsindex ea470e16c..3c2097c86 100644--- a/server/src/lib/content-delivery.js+++ b/server/src/lib/content-delivery.js@@ -6,6 +6,8 @@ import { logger } from './logger.js';import { PageFetchError } from './safe-fetch.js';import { languageCodeOf } from './languages.js';import { postJson } from './safe-post.js';+import { isWebflowConfigured } from './composio.js';+import { sendToWebflow } from './cms/webflow-delivery.js';/*** Send to CMS, phase 1 (#1331): an admin or manager sends Content Editor@@ -21,6 +23,10 @@ import { postJson } from './safe-post.js';* in the background, one page after another, and the Library follows its* rows. A row a restarted server left queued is closed as interrupted by* the workspace's next send (reserve_content_deliveries).+ *+ * Phase 2: a page can go to the brand's Webflow collection instead (00283,+ * cms/webflow-delivery.js), as a draft item, under the same cap, batches+ * and delivery rows; a delivery says where it went (`target`, 00284).*/export const DELIVERY_EVENT = 'content_draft.sent';@@ -54,15 +60,22 @@ export const deliveryPage = z.object({.default(null),});+/** Where pages go: the brand's webhook (phase 1) or its Webflow collection (phase 2). */+const DELIVERY_TARGETS = ['webhook', 'webflow'];+export const sendInput = z.object({// guid, not uuid: brand ids aren't all RFC 4122 versions (seeded ones).brandId: z.guid(),pages: z.array(deliveryPage).min(1).max(PAGES_PER_SEND),+ target: z.enum(DELIVERY_TARGETS).default('webhook'),});/** A send that can't go, with the code the web names to the person. */export class DeliveryError extends Error {- /** @param {'no_webhook'|'webhook_not_subscribed'|'not_found'|'daily_limit'} code */+ /**+ * @param {'no_webhook'|'webhook_not_subscribed'|'webflow_not_configured'|'no_webflow'|+ * 'webflow_refused'|'webflow_not_ready'|'in_progress'|'not_found'|'daily_limit'} code+ */constructor(code, message, status) {super(message);this.code = code;@@ -215,6 +228,38 @@ async function webhooksFor(brandId) {return subscribed;}+/** The brand's Webflow connection, ready to take pages (a collection with a body field). */+async function webflowFor(brandId) {+ if (!isWebflowConfigured()) {+ throw new DeliveryError('webflow_not_configured', 'Webflow is not set up on this server.', 503);+ }+ const { data, error } = await supabaseAdmin+ .from('brand_cms_connections')+ .select('brand_id, composio_account_id, composio_entity_id, status, collection_id, field_map')+ .eq('brand_id', brandId)+ .eq('provider', 'webflow')+ .maybeSingle();+ if (error) throw new Error(error.message);+ if (!data) {+ throw new DeliveryError('no_webflow', 'Connect Webflow in Settings › Integrations.', 409);+ }+ if (data.status === 'auth_failed') {+ throw new DeliveryError(+ 'webflow_refused',+ 'Webflow refused the site token. Connect it again in Settings › Integrations.',+ 409,+ );+ }+ if (!data.collection_id || !data.field_map?.body) {+ throw new DeliveryError(+ 'webflow_not_ready',+ 'Pick the Webflow collection pages go to in Settings › Integrations.',+ 409,+ );+ }+ return data;+}+async function brandFor(brandId) {const [{ data: brand }, { data: domains }] = await Promise.all([supabaseAdmin.from('brands').select('id, name, industry, language').eq('id', brandId).single(),@@ -235,15 +280,10 @@ async function brandFor(brandId) {}/**- * Sends one queued delivery to the brand's webhooks and writes how it went:- * sent once any of them takes it (with that one's answer), else failed with- * the last reason. A callback can land before the webhook answers (an- * automation that publishes, or fails, first): then the delivery keeps what- * the callback said, and that is what comes back.+ * The page to the brand's webhooks: taken once any of them takes it (with+ * that one's answer), else the last answer and reason.*/-async function deliver({ delivery, page, brand, configs }) {- const html = cleanPublishHtml(page.html);- const jsonLd = jsonLdOf(html);+async function sendToWebhooks({ delivery, page, brand, html, jsonLd, configs }) {let taken = null;let last = null;let failure = null;@@ -272,19 +312,65 @@ async function deliver({ delivery, page, brand, configs }) {}}const answered = taken ?? last;+ return {+ taken: Boolean(taken),+ failure,+ fields: {+ http_status: answered?.status ?? null,+ response_excerpt: answered?.answer ? answered.answer.slice(0, EXCERPT_CHARS) : null,+ },+ };+}++/**+ * Sends one queued delivery, to the brand's webhooks or its Webflow+ * collection, and writes how it went: sent, or failed with the reason. A+ * Webflow item's id and collection are kept on the delivery, so the page's+ * next send updates the item. A webhook's callback can land before the+ * webhook answers (an automation that publishes, or fails, first): then the+ * delivery keeps what the callback said, and that is what comes back.+ */+async function deliver({ delivery, page, brand, configs, webflow }) {+ const html = cleanPublishHtml(page.html);+ const jsonLd = jsonLdOf(html);+ const sent = webflow+ ? await sendToWebflow({ delivery, page, html, jsonLd, connection: webflow })+ : await sendToWebhooks({ delivery, page, brand, html, jsonLd, configs });const update = {- status: taken ? 'sent' : 'failed',- http_status: answered?.status ?? null,- response_excerpt: answered?.answer ? answered.answer.slice(0, EXCERPT_CHARS) : null,- error: taken ? null : (failure ?? 'Not sent').slice(0, 500),+ status: sent.taken ? 'sent' : 'failed',+ ...(sent.fields ?? { http_status: null, response_excerpt: null }),+ error: sent.taken ? null : (sent.failure ?? 'Not sent').slice(0, 500),sent_at: new Date().toISOString(),+ ...(sent.externalId+ ? { external_id: sent.externalId, target_collection: sent.collectionId }+ : {}),};+ // How Webflow took it, for the one page's answer (and the batch); not kept.+ const how = webflow+ ? sent.taken+ ? { updated: sent.updated, changed: sent.changed }+ : { refused: Boolean(sent.refused) }+ : {};const { data: written, error } = await supabaseAdmin.from('content_draft_deliveries').update(update).eq('id', delivery.id).eq('status', 'queued').select('id');+ if (sent.externalId && (error || (written && !written.length))) {+ // The item is in Webflow whatever the row says: its id is kept, or the+ // page's next send makes a second item and fails on its slug.+ const { error: idError } = await supabaseAdmin+ .from('content_draft_deliveries')+ .update({ external_id: sent.externalId, target_collection: sent.collectionId })+ .eq('id', delivery.id);+ if (idError) {+ logger.error(+ { err: idError, deliveryId: delivery.id },+ '[content-delivery] item id not kept',+ );+ }+ }if (error) {logger.error({ err: error, deliveryId: delivery.id }, '[content-delivery] write failed');} else if (written && !written.length) {@@ -294,9 +380,9 @@ async function deliver({ delivery, page, brand, configs }) {.select('status, error, published_url').eq('id', delivery.id).maybeSingle();- if (told) return { id: delivery.id, draftId: page.draftId, ...update, ...told };+ if (told) return { id: delivery.id, draftId: page.draftId, ...update, ...told, ...how };}- return { id: delivery.id, draftId: page.draftId, ...update };+ return { id: delivery.id, draftId: page.draftId, ...update, ...how };}/** A queued delivery that doesn't go, closed as failed with the reason. */@@ -311,9 +397,17 @@ async function closeQueued(delivery, reason) {}}-/** The batch's pages, one after another; one deleted or archived before its turn doesn't go. */-async function deliverBatch(going, { brand, configs, batchId }) {+/**+ * The batch's pages, one after another; one deleted or archived before its+ * turn doesn't go, and once Webflow refuses the token, the rest don't either.+ */+async function deliverBatch(going, { brand, configs, webflow, batchId }) {+ let refused = null;for (const [i, item] of going.entries()) {+ if (refused) {+ await closeQueued(item.delivery, refused).catch(() => {});+ continue;+ }// A short pause between pages, none before the first.if (i > 0) await new Promise((resolve) => setTimeout(resolve, PAUSE_MS));try {@@ -327,7 +421,8 @@ async function deliverBatch(going, { brand, configs, batchId }) {await closeQueued(item.delivery, 'The page was deleted or archived before it was sent');continue;}- await deliver({ ...item, brand, configs });+ const sent = await deliver({ ...item, brand, configs, webflow });+ if (sent.refused) refused = sent.error ?? 'Webflow refused the site token';} catch (err) {logger.error({ err, batchId, deliveryId: item.delivery.id },@@ -363,7 +458,8 @@ async function myRuleSets(orgId, drafts) {/*** Sends pages of one brand: checks they are the brand's (not archived), that- * a webhook takes the event, reserves them against the day's cap (those past+ * a webhook takes the event (or the brand's Webflow collection is ready, for+ * target 'webflow'), reserves them against the day's cap (those past* it don't go), keeps each page's slug and meta on its draft, then sends* them. One page is sent before this returns; more go on in the background,* so a CMS isn't sent 25 posts at once.@@ -372,7 +468,13 @@ async function myRuleSets(orgId, drafts) {* deliveries: the sent one (one page) or the queued ones (more); skipped:* pages past the day's cap*/-export async function sendContentPages({ orgId, brandId, userId, pages: asked }) {+export async function sendContentPages({+ orgId,+ brandId,+ userId,+ pages: asked,+ target = 'webhook',+}) {// A page named twice goes once.const byId = new Map(asked.map((p) => [p.draftId, p]));const pages = [...byId.values()];@@ -387,7 +489,8 @@ export async function sendContentPages({ orgId, brandId, userId, pages: asked })if ((drafts ?? []).length !== ids.length) {throw new DeliveryError('not_found', 'A page to send is not in this brand', 404);}- const configs = await webhooksFor(brandId);+ const webflow = target === 'webflow' ? await webflowFor(brandId) : null;+ const configs = webflow ? [] : await webhooksFor(brandId);const batchId = pages.length > 1 ? crypto.randomUUID() : null;const { data: queued, error: reserveError } = await supabaseAdmin.rpc(@@ -399,9 +502,20 @@ export async function sendContentPages({ orgId, brandId, userId, pages: asked })p_batch_id: batchId,p_items: pages.map((p) => ({ draft_id: p.draftId, title: p.title, slug: p.slug })),p_limit: DAILY_DELIVERIES,+ p_target: target,},);- if (reserveError) throw new Error(reserveError.message);+ if (reserveError) {+ // 00284: a page already on its way to Webflow isn't sent again meanwhile.+ if (/delivery_in_progress/.test(reserveError.message ?? '')) {+ throw new DeliveryError(+ 'in_progress',+ 'A page is already being sent to Webflow. Wait for it, then send again.',+ 409,+ );+ }+ throw new Error(reserveError.message);+ }const rows = queued ?? [];if (!rows.length) {throw new DeliveryError(@@ -463,10 +577,10 @@ export async function sendContentPages({ orgId, brandId, userId, pages: asked })};}if (pages.length === 1) {- const sent = await deliver({ ...going[0], brand, configs });+ const sent = await deliver({ ...going[0], brand, configs, webflow });return { batchId, deliveries: [sent], skipped: 0 };}- void deliverBatch(going, { brand, configs, batchId });+ void deliverBatch(going, { brand, configs, webflow, batchId });return {batchId,deliveries: rows.map((r) => ({ id: r.id, draftId: r.draft_id, status: r.status })),diff --git a/server/src/lib/content-delivery.test.js b/server/src/lib/content-delivery.test.jsindex e52b2f9cb..055fafffb 100644--- a/server/src/lib/content-delivery.test.js+++ b/server/src/lib/content-delivery.test.js@@ -13,6 +13,9 @@ const db = {// What a callback that landed first left on the delivery.told: null,configs: [],+ // The brand's Webflow connection (00283), and whether this server has Webflow set up.+ webflow: null,+ webflowConfigured: true,queued: [],updates: [],rpc: [],@@ -39,6 +42,7 @@ function from(table) {return { data: db.drafts.filter((d) => !ids || ids.includes(d.id)), error: null };}if (table === 'webhook_configs') return { data: db.configs, error: null };+ if (table === 'brand_cms_connections') return { data: db.webflow, error: null };if (table === 'brands') return { data: db.brand, error: null };if (table === 'brand_domains') return { data: [{ domain: 'acme.example' }], error: null };return { data: null, error: null };@@ -52,6 +56,7 @@ function from(table) {limit: () => chain,single: async () => result(),maybeSingle: async () => {+ if (table === 'brand_cms_connections') return result();const { data } = result();const id = q.filters.find(([op, col]) => op === 'eq' && col === 'id')?.[2];return { data: (data ?? []).find((d) => d.id === id) ?? null, error: null };@@ -67,12 +72,17 @@ vi.mock('../config/supabase.js', () => ({from: (t) => from(t),rpc: async (fn, args) => {db.rpc.push({ fn, args });- return { data: db.queued, error: null };+ return db.rpcError ? { data: null, error: db.rpcError } : { data: db.queued, error: null };},},}));const postJson = vi.fn();vi.mock('./safe-post.js', () => ({ postJson: (...args) => postJson(...args) }));+vi.mock('./composio.js', () => ({ isWebflowConfigured: () => db.webflowConfigured }));+const sendToWebflow = vi.fn();+vi.mock('./cms/webflow-delivery.js', () => ({+ sendToWebflow: (...args) => sendToWebflow(...args),+}));const { DAILY_DELIVERIES, DeliveryError, PAUSE_MS, cleanPublishHtml, sendContentPages, signBody } =await import('./content-delivery.js');@@ -121,7 +131,11 @@ beforeEach(() => {db.queued = [row(page().draftId)];db.updates = [];db.rpc = [];+ db.webflow = null;+ db.webflowConfigured = true;+ db.rpcError = null;postJson.mockReset().mockResolvedValue({ status: 200, answer: '{"ok":true}' });+ sendToWebflow.mockReset();});afterEach(() => {@@ -185,6 +199,7 @@ describe('sendContentPages', () => {p_batch_id: null,p_items: [{ draft_id: page().draftId, title: page().title, slug: 'best-aeo-tools' }],p_limit: DAILY_DELIVERIES,+ p_target: 'webhook',},},]);@@ -431,3 +446,134 @@ describe('sendContentPages', () => {expect(postJson).not.toHaveBeenCalled();});});++describe('sendContentPages to Webflow (phase 2)', () => {+ const connection = {+ brand_id: 'b1',+ composio_account_id: 'ca_1',+ composio_entity_id: 'brand_b1',+ status: 'connected',+ collection_id: 'c1',+ field_map: { body: 'post-body' },+ };+ const toWebflow = (pages = [page()]) =>+ sendContentPages({ orgId: 'o1', brandId: 'b1', userId: 'u1', pages, target: 'webflow' });++ it('sends a page to the brand’s collection, not its webhooks, and keeps the item on the delivery', async () => {+ db.webflow = connection;+ sendToWebflow.mockResolvedValue({+ taken: true,+ externalId: 'item-1',+ collectionId: 'c1',+ updated: false,+ changed: { tables: 0, images: 0 },+ });+ const sent = await toWebflow();+ expect(db.rpc[0].args.p_target).toBe('webflow');+ expect(postJson).not.toHaveBeenCalled();+ expect(sendToWebflow).toHaveBeenCalledWith(+ expect.objectContaining({+ connection,+ html: '<h2>Why</h2><p>Because.</p>',+ jsonLd: [],+ page: expect.objectContaining({ draftId: page().draftId, slug: 'best-aeo-tools' }),+ }),+ );+ const write = db.updates.find((u) => u.table === 'content_draft_deliveries');+ expect(write.update).toMatchObject({+ status: 'sent',+ error: null,+ http_status: null,+ external_id: 'item-1',+ target_collection: 'c1',+ });+ expect(sent.deliveries[0]).toMatchObject({ status: 'sent', updated: false });+ });++ it('writes a page Webflow didn’t take as failed, with the reason', async () => {+ db.webflow = connection;+ sendToWebflow.mockResolvedValue({+ taken: false,+ failure: 'Another item in the collection has this slug',+ });+ const sent = await toWebflow();+ const write = db.updates.find((u) => u.table === 'content_draft_deliveries');+ expect(write.update).toMatchObject({+ status: 'failed',+ error: 'Another item in the collection has this slug',+ });+ expect(write.update).not.toHaveProperty('external_id');+ expect(sent.deliveries[0]).not.toHaveProperty('updated');+ });++ it('refuses before reserving when Webflow isn’t ready to take pages', async () => {+ db.webflowConfigured = false;+ await expect(toWebflow()).rejects.toMatchObject({+ code: 'webflow_not_configured',+ status: 503,+ });+ db.webflowConfigured = true;+ await expect(toWebflow()).rejects.toMatchObject({ code: 'no_webflow', status: 409 });+ db.webflow = { ...connection, status: 'auth_failed' };+ await expect(toWebflow()).rejects.toMatchObject({ code: 'webflow_refused', status: 409 });+ db.webflow = { ...connection, collection_id: null };+ await expect(toWebflow()).rejects.toMatchObject({ code: 'webflow_not_ready', status: 409 });+ db.webflow = { ...connection, field_map: {} };+ await expect(toWebflow()).rejects.toMatchObject({ code: 'webflow_not_ready' });+ expect(db.rpc).toEqual([]);+ expect(sendToWebflow).not.toHaveBeenCalled();+ });++ it('sends a batch one page after another, to Webflow', async () => {+ vi.useFakeTimers();+ db.webflow = connection;+ const second = page({ draftId: 'c0ffee00-0000-4000-8000-000000000002', slug: 'second' });+ db.drafts = [draft(page().draftId), draft(second.draftId)];+ db.queued = [row(page().draftId), row(second.draftId)];+ sendToWebflow.mockResolvedValue({ taken: true, externalId: 'i', collectionId: 'c1' });+ await toWebflow([page(), second]);+ await vi.runAllTimersAsync();+ expect(sendToWebflow).toHaveBeenCalledTimes(2);+ expect(postJson).not.toHaveBeenCalled();+ });++ it('refuses a page already on its way to Webflow', async () => {+ db.webflow = connection;+ db.rpcError = { message: 'delivery_in_progress' };+ await expect(toWebflow()).rejects.toMatchObject({ code: 'in_progress', status: 409 });+ expect(sendToWebflow).not.toHaveBeenCalled();+ });++ it('stops a batch once Webflow refuses the token, closing the rest with the reason', async () => {+ vi.useFakeTimers();+ db.webflow = connection;+ const second = page({ draftId: 'c0ffee00-0000-4000-8000-000000000002', slug: 'second' });+ db.drafts = [draft(page().draftId), draft(second.draftId)];+ db.queued = [row(page().draftId), row(second.draftId)];+ sendToWebflow.mockResolvedValue({+ taken: false,+ failure: 'Webflow refused the site token',+ refused: true,+ });+ await toWebflow([page(), second]);+ await vi.runAllTimersAsync();+ expect(sendToWebflow).toHaveBeenCalledTimes(1);+ const closed = db.updates.filter(+ (u) =>+ u.table === 'content_draft_deliveries' &&+ u.update.error === 'Webflow refused the site token',+ );+ expect(closed).toHaveLength(2);+ });++ it('keeps the item’s id even when the delivery’s row was written meanwhile', async () => {+ db.webflow = connection;+ db.told = { status: 'failed', error: 'Not sent: interrupted', published_url: null };+ sendToWebflow.mockResolvedValue({ taken: true, externalId: 'item-9', collectionId: 'c1' });+ await toWebflow();+ expect(db.updates.map((u) => u.update)).toContainEqual({+ external_id: 'item-9',+ target_collection: 'c1',+ });+ });+});diff --git a/server/src/routes/content-drafts-send.test.js b/server/src/routes/content-drafts-send.test.jsindex 76abdc96d..6c5aa1009 100644--- a/server/src/routes/content-drafts-send.test.js+++ b/server/src/routes/content-drafts-send.test.js@@ -86,6 +86,7 @@ describe('POST /api/content-drafts/send', () => {brandId: BRAND,userId: 'u1',pages: [{ ...page, metaTitle: '', metaDescription: '', author: null, featuredImage: null }],+ target: 'webhook',});});diff --git a/server/src/routes/content-drafts.js b/server/src/routes/content-drafts.jsindex 64c42c135..50feaa252 100644--- a/server/src/routes/content-drafts.js+++ b/server/src/routes/content-drafts.js@@ -18,7 +18,7 @@ import { PlanLimitError } from '../lib/plan-guard.js';* (CE-018); Hand off has it fix the page's open Quality checks (CE-051);* Start's Agent has the Content Agent do the page (CE-015);* Stop ends the agent's run under way, changing nothing (v1.1 CE-027); and- * Send to CMS sends pages to the brand's webhook (#1331).+ * Send to CMS sends pages to the brand's webhook or its Webflow collection (#1331).*/const router = Router();@@ -44,7 +44,7 @@ async function assertSender(userId) {/*** POST /api/content-drafts/send- * Body: { brandId, pages: [{ draftId, title, slug, metaTitle, metaDescription, html, author, featuredImage }] }+ * Body: { brandId, target?: 'webhook' | 'webflow', pages: [{ draftId, title, slug, metaTitle, metaDescription, html, author, featuredImage }] }* One page is sent before this answers: { deliveries: [{ id, status, http_status, error }] };* more (up to 25) go on in the background: { batchId, deliveries: queued, skipped }.*/@@ -55,11 +55,11 @@ router.post('/send', async (req, res) => {const field = parsed.error.issues[0]?.path.join('.') ?? null;return res.status(400).json({ error: 'Invalid request', code: 'invalid', field });}- const { brandId, pages } = parsed.data;+ const { brandId, pages, target } = parsed.data;try {const { orgId } = await assertBrandAccess(brandId, req.user.id);await assertSender(req.user.id);- const sent = await sendContentPages({ orgId, brandId, userId: req.user.id, pages });+ const sent = await sendContentPages({ orgId, brandId, userId: req.user.id, pages, target });return res.json(sent);} catch (err) {if (err instanceof DeliveryError) {diff --git a/server/src/routes/webhook-content-draft-callback.js b/server/src/routes/webhook-content-draft-callback.jsindex 07e795243..9cf5c4722 100644--- a/server/src/routes/webhook-content-draft-callback.js+++ b/server/src/routes/webhook-content-draft-callback.js@@ -88,10 +88,12 @@ router.post('/:deliveryId/status', async (req, res) => {// answer the same, so this route doesn't tell which deliveries exist.if (!UUID.test(deliveryId)) return res.status(401).json(DENIED);+ // A webhook's delivery only: a Webflow one (00284) is the API's own to write.const { data: delivery, error } = await supabaseAdmin.from('content_draft_deliveries').select('id, brand_id, draft_id, status, published_url').eq('id', deliveryId)+ .eq('target', 'webhook').maybeSingle();if (error) throw new Error(error.message);if (!delivery) return res.status(401).json(DENIED);diff --git a/server/src/routes/webhook-content-draft-callback.test.js b/server/src/routes/webhook-content-draft-callback.test.jsindex 64aaa08dc..81d3bc708 100644--- a/server/src/routes/webhook-content-draft-callback.test.js+++ b/server/src/routes/webhook-content-draft-callback.test.js@@ -69,6 +69,7 @@ beforeEach(async () => {brand_id: 'b1',draft_id: 'd1',status: 'sent',+ target: 'webhook',created_at: '2026-10-24T10:00:00Z',},];@@ -152,6 +153,7 @@ describe('POST /webhooks/content-drafts/:deliveryId/status', () => {brand_id: 'b1',draft_id: 'd1',status: 'sent',+ target: 'webhook',created_at: '2026-10-24T12:00:00Z',});const res = await call(@@ -274,12 +276,20 @@ describe('POST /webhooks/content-drafts/:deliveryId/status', () => {expect(state.rpcs).toHaveLength(1);});+ it('answers a Webflow delivery’s id as it does an unknown one: the API writes those itself', async () => {+ state.deliveries[0].target = 'webflow';+ const res = await published('https://acme.example/live');+ expect(res.status).toBe(401);+ expect(state.deliveries[0].status).toBe('sent');+ });+it('moves the page’s address only for its latest delivery', async () => {state.deliveries.push({id: 'newer',brand_id: 'b1',draft_id: 'd1',status: 'sent',+ target: 'webhook',created_at: '2026-10-24T11:00:00Z',});const res = await published('https://acme.example/old');diff --git a/supabase/migrations/00284_content_delivery_targets.sql b/supabase/migrations/00284_content_delivery_targets.sqlnew file mode 100644index 000000000..ca5a3bb34--- /dev/null+++ b/supabase/migrations/00284_content_delivery_targets.sql@@ -0,0 +1,108 @@+-- ============================================================================+-- 00284: where a page was sent (Send to CMS #1331, phase 2)+-- ============================================================================+-- Phase 1 sent every page to the brand's webhook (00262). A page now goes to+-- the webhook or to the brand's Webflow collection (00283), so a delivery+-- says which:+--+-- * `target`: 'webhook' (every delivery so far) or 'webflow'.+-- * `target_collection`: the Webflow collection the item was written to.+-- The item's id is the delivery's external_id, as a CMS's own id is for+-- the webhook. A page sent again updates its item while the brand still+-- sends to that collection, and makes a new one after it changes.+--+-- reserve_content_deliveries takes the target, 'webhook' when it's left out:+-- the API before this change calls it with six arguments and still works,+-- so the migration goes first. For Webflow it refuses (delivery_in_progress)+-- pages a send still has queued: two sends of one page at once would both+-- make an item, the second refused on its slug.+--+-- Adding the columns checks content_draft_deliveries' rows once (the check+-- constraints), under its lock: a few hundred rows at most so far.++alter table public.content_draft_deliveries+ add column if not exists target text not null default 'webhook'+ check (target in ('webhook', 'webflow')),+ add column if not exists target_collection text+ check (target_collection is null or char_length(target_collection) between 1 and 100);++comment on column public.content_draft_deliveries.target is+ 'Where the page was sent (00284): the brand''s webhook, or its Webflow collection (00283).';+comment on column public.content_draft_deliveries.target_collection is+ 'The Webflow collection the page''s item was written to (00284); the item''s id is external_id.';++drop function if exists public.reserve_content_deliveries(uuid, uuid, uuid, uuid, jsonb, integer);++-- ----------------------------------------------------------------------------+-- As 00262, with the target each queued row goes to, and no second Webflow+-- send of a page while one is queued.+-- ----------------------------------------------------------------------------+create or replace function public.reserve_content_deliveries(+ p_organization_id uuid,+ p_brand_id uuid,+ p_sent_by uuid,+ p_batch_id uuid,+ p_items jsonb,+ p_limit integer,+ p_target text default 'webhook'+)+returns setof public.content_draft_deliveries+language plpgsql+security definer+set search_path = public+as $$+declare+ used integer;+ room integer;+begin+ perform pg_advisory_xact_lock(hashtext('content_deliveries:' || p_organization_id::text));++ -- sent_at stays empty: it was never sent, and its time stays its own.+ update public.content_draft_deliveries+ set status = 'failed', error = 'Not sent: interrupted'+ where organization_id = p_organization_id+ and status = 'queued'+ and created_at < now() - interval '30 minutes';++ select count(*) into used+ from public.content_draft_deliveries+ where organization_id = p_organization_id+ and created_at > now() - interval '24 hours';++ room := greatest(0, p_limit - used);+ if room = 0 or jsonb_typeof(p_items) <> 'array' then+ return;+ end if;++ -- Under the lock, so two sends of one page can't both pass.+ if p_target = 'webflow' and exists (+ select 1+ from public.content_draft_deliveries d+ join jsonb_array_elements(p_items) as items(item)+ on d.draft_id = (item ->> 'draft_id')::uuid+ where d.organization_id = p_organization_id+ and d.target = 'webflow'+ and d.status = 'queued'+ ) then+ raise exception 'delivery_in_progress' using errcode = 'P0001';+ end if;++ return query+ insert into public.content_draft_deliveries+ (organization_id, brand_id, draft_id, sent_by, batch_id, title, slug, target)+ select p_organization_id, p_brand_id, (item ->> 'draft_id')::uuid, p_sent_by, p_batch_id,+ left(item ->> 'title', 300), nullif(left(item ->> 'slug', 200), ''),+ coalesce(p_target, 'webhook')+ from jsonb_array_elements(p_items) with ordinality as items(item, position)+ order by position+ limit room+ returning *;+end;+$$;++revoke all on function public.reserve_content_deliveries(uuid, uuid, uuid, uuid, jsonb, integer, text)+ from public, anon, authenticated;+grant execute on function public.reserve_content_deliveries(uuid, uuid, uuid, uuid, jsonb, integer, text)+ to service_role;++notify pgrst, 'reload schema';diff --git a/supabase/schema.sql b/supabase/schema.sqlindex ea2f9b240..9111682ca 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -40066,3 +40066,115 @@ comment on table public.brand_cms_connections isnotify pgrst, 'reload schema';+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00284_content_delivery_targets.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00284: where a page was sent (Send to CMS #1331, phase 2)+-- ============================================================================+-- Phase 1 sent every page to the brand's webhook (00262). A page now goes to+-- the webhook or to the brand's Webflow collection (00283), so a delivery+-- says which:+--+-- * `target`: 'webhook' (every delivery so far) or 'webflow'.+-- * `target_collection`: the Webflow collection the item was written to.+-- The item's id is the delivery's external_id, as a CMS's own id is for+-- the webhook. A page sent again updates its item while the brand still+-- sends to that collection, and makes a new one after it changes.+--+-- reserve_content_deliveries takes the target, 'webhook' when it's left out:+-- the API before this change calls it with six arguments and still works,+-- so the migration goes first. For Webflow it refuses (delivery_in_progress)+-- pages a send still has queued: two sends of one page at once would both+-- make an item, the second refused on its slug.+--+-- Adding the columns checks content_draft_deliveries' rows once (the check+-- constraints), under its lock: a few hundred rows at most so far.++alter table public.content_draft_deliveries+ add column if not exists target text not null default 'webhook'+ check (target in ('webhook', 'webflow')),+ add column if not exists target_collection text+ check (target_collection is null or char_length(target_collection) between 1 and 100);++comment on column public.content_draft_deliveries.target is+ 'Where the page was sent (00284): the brand''s webhook, or its Webflow collection (00283).';+comment on column public.content_draft_deliveries.target_collection is+ 'The Webflow collection the page''s item was written to (00284); the item''s id is external_id.';++drop function if exists public.reserve_content_deliveries(uuid, uuid, uuid, uuid, jsonb, integer);++-- ----------------------------------------------------------------------------+-- As 00262, with the target each queued row goes to, and no second Webflow+-- send of a page while one is queued.+-- ----------------------------------------------------------------------------+create or replace function public.reserve_content_deliveries(+ p_organization_id uuid,+ p_brand_id uuid,+ p_sent_by uuid,+ p_batch_id uuid,+ p_items jsonb,+ p_limit integer,+ p_target text default 'webhook'+)+returns setof public.content_draft_deliveries+language plpgsql+security definer+set search_path = public+as $$+declare+ used integer;+ room integer;+begin+ perform pg_advisory_xact_lock(hashtext('content_deliveries:' || p_organization_id::text));++ -- sent_at stays empty: it was never sent, and its time stays its own.+ update public.content_draft_deliveries+ set status = 'failed', error = 'Not sent: interrupted'+ where organization_id = p_organization_id+ and status = 'queued'+ and created_at < now() - interval '30 minutes';++ select count(*) into used+ from public.content_draft_deliveries+ where organization_id = p_organization_id+ and created_at > now() - interval '24 hours';++ room := greatest(0, p_limit - used);+ if room = 0 or jsonb_typeof(p_items) <> 'array' then+ return;+ end if;++ -- Under the lock, so two sends of one page can't both pass.+ if p_target = 'webflow' and exists (+ select 1+ from public.content_draft_deliveries d+ join jsonb_array_elements(p_items) as items(item)+ on d.draft_id = (item ->> 'draft_id')::uuid+ where d.organization_id = p_organization_id+ and d.target = 'webflow'+ and d.status = 'queued'+ ) then+ raise exception 'delivery_in_progress' using errcode = 'P0001';+ end if;++ return query+ insert into public.content_draft_deliveries+ (organization_id, brand_id, draft_id, sent_by, batch_id, title, slug, target)+ select p_organization_id, p_brand_id, (item ->> 'draft_id')::uuid, p_sent_by, p_batch_id,+ left(item ->> 'title', 300), nullif(left(item ->> 'slug', 200), ''),+ coalesce(p_target, 'webhook')+ from jsonb_array_elements(p_items) with ordinality as items(item, position)+ order by position+ limit room+ returning *;+end;+$$;++revoke all on function public.reserve_content_deliveries(uuid, uuid, uuid, uuid, jsonb, integer, text)+ from public, anon, authenticated;+grant execute on function public.reserve_content_deliveries(uuid, uuid, uuid, uuid, jsonb, integer, text)+ to service_role;++notify pgrst, 'reload schema';+