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): mark pages sent to Webflow published once they go live there (Send to CMS #1331, phase 2 part 6: Webflow published) (#1509)
Cihan5778345c1178
diff --git a/server/src/lib/cms/webflow-delivery.js b/server/src/lib/cms/webflow-delivery.jsindex 45584d67f..ca4c00222 100644--- a/server/src/lib/cms/webflow-delivery.js+++ b/server/src/lib/cms/webflow-delivery.js@@ -11,7 +11,12 @@ import { fieldDataFor, toWebflowRichText, webflowSlug } from './webflow-items.js* batches the deliveries as it does for the webhook.*/-/** The item the page's last Webflow send made in this collection, if any. */+/**+ * The item the page's last Webflow send made in this collection, if any,+ * whatever became of that send: one closed as "Removed in Webflow" is tried+ * too, as a wrong "removed" costs nothing (updateItem's not_found makes a+ * new item).+ */async function lastItem(draftId, collectionId, deliveryId) {const { data, error } = await supabaseAdmin.from('content_draft_deliveries')@@ -19,7 +24,6 @@ async function lastItem(draftId, collectionId, deliveryId) {.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 })diff --git a/server/src/lib/cms/webflow-delivery.test.js b/server/src/lib/cms/webflow-delivery.test.jsindex e35b53ab0..ce1352573 100644--- a/server/src/lib/cms/webflow-delivery.test.js+++ b/server/src/lib/cms/webflow-delivery.test.js@@ -94,12 +94,12 @@ describe('sendToWebflow', () => {'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.+ // The page's last item is looked for in this collection, from any send that made one:+ // one closed as removed in Webflow too, in case it wasn't.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'],]);diff --git a/server/src/lib/cms/webflow-status.js b/server/src/lib/cms/webflow-status.jsnew file mode 100644index 000000000..a10bd2213--- /dev/null+++ b/server/src/lib/cms/webflow-status.js@@ -0,0 +1,190 @@+import supabaseAdmin from '../../config/supabase.js';+import { isSubscriptionActive } from '../../config/plans.js';+import { logger } from '../logger.js';+import { isWebflowConfigured } from '../composio.js';+import { markDeliveryPublished } from '../content-published.js';+import { WebflowError, getItem, listCollections, listSites } from './webflow.js';++/**+ * Pages sent to Webflow, published there since (Send to CMS #1331, phase+ * 2): nothing calls back when a Webflow item goes live, so Ansvisor reads+ * the items its sends made, and marks one published at the page's address+ * on the site (its host, the collection's slug and the item's: the+ * collection page Webflow makes) once Webflow published it after the send.+ * An item published before (a live page sent again) waits for its next+ * publish. An item deleted in Webflow closes its send. Read when the+ * Library opens (a brand's newest sends, throttled by its route) and once a+ * day for all.+ */++/** Items the Library's check reads: the newest sends. The daily sweep reads the rest. */+export const ITEMS_ON_DEMAND = 10;+/** Items the daily sweep reads a brand. */+export const ITEMS_DAILY = 100;+/** Sends older than this aren't read again: a draft left that long stays one. */+const LOOK_BACK_MS = 60 * 24 * 60 * 60 * 1000;+/** Item reads that fail in a row before a check stops: Webflow or Composio is down. */+const FAILURES_IN_A_ROW = 3;+/** Connections the daily sweep reads, shuffled so a deadline doesn't leave the same brands out. */+const SWEEP_CONNECTIONS = 1000;+const SWEEP_DEADLINE_MS = 10 * 60 * 1000;++const none = { checked: 0, published: [] };++/** The token was refused while reading: 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 }, '[webflow-status] refusal not kept');+}++/**+ * A send whose item Webflow says is gone: closed. The page's next send+ * tries the item again (lastItem), and makes a new one if it is gone.+ */+async function closeRemoved(send) {+ const { error } = await supabaseAdmin+ .from('content_draft_deliveries')+ .update({ status: 'failed', error: 'Removed in Webflow' })+ .eq('id', send.id)+ .eq('status', 'sent');+ if (error) logger.warn({ err: error, deliveryId: send.id }, '[webflow-status] removal not kept');+}++const logged = (err) => ({ name: err?.name, code: err?.code });++/**+ * Reads the brand's pages sent to Webflow and not yet seen live (each+ * page's latest such send, newest first, up to `limit`), and marks the ones+ * Webflow published after the send published. One item's failure doesn't+ * stop the others (three in a row do); a token refused stops the check and+ * marks the connection.+ *+ * @returns {Promise<{ checked: number, published: { draftId: string|null, url: string }[] }>}+ */+export async function checkWebflowPublished(brandId, { limit = ITEMS_ON_DEMAND } = {}) {+ if (!isWebflowConfigured()) return none;+ const { data: connection, error } = await supabaseAdmin+ .from('brand_cms_connections')+ .select('brand_id, composio_account_id, composio_entity_id, status, site_id')+ .eq('brand_id', brandId)+ .eq('provider', 'webflow')+ .maybeSingle();+ if (error) throw new Error(error.message);+ if (!connection || connection.status !== 'connected' || !connection.site_id) return none;++ const { data: rows, error: rowsError } = await supabaseAdmin+ .from('content_draft_deliveries')+ .select('id, draft_id, status, published_url, external_id, target_collection, sent_at')+ .eq('brand_id', brandId)+ .eq('target', 'webflow')+ .eq('status', 'sent')+ .not('external_id', 'is', null)+ .not('target_collection', 'is', null)+ .gte('sent_at', new Date(Date.now() - LOOK_BACK_MS).toISOString())+ .order('sent_at', { ascending: false })+ .limit(limit * 4);+ if (rowsError) throw new Error(rowsError.message);+ // A page's latest send: an earlier one made the same item, updated since.+ const seen = new Set();+ const sends = [];+ for (const row of rows ?? []) {+ const key = row.draft_id ?? row.id;+ if (seen.has(key)) continue;+ seen.add(key);+ sends.push(row);+ if (sends.length === limit) break;+ }+ if (!sends.length) return none;++ const account = {+ entityId: connection.composio_entity_id,+ accountId: connection.composio_account_id,+ };+ const published = [];+ let checked = 0;+ try {+ const site = (await listSites(account)).find((s) => s.id === connection.site_id);+ if (!site?.host) return none;+ // A send to a collection since removed, or on another site, isn't read.+ const slugOf = new Map(+ (await listCollections(account, site.id)).filter((c) => c.slug).map((c) => [c.id, c.slug]),+ );+ let failures = 0;+ for (const send of sends) {+ const collection = slugOf.get(send.target_collection);+ if (!collection) continue;+ let item;+ try {+ item = await getItem(account, send.target_collection, send.external_id);+ failures = 0;+ } catch (err) {+ if (err instanceof WebflowError && err.code === 'auth_failed') throw err;+ logger.warn({ err: logged(err), deliveryId: send.id }, '[webflow-status] item not read');+ if (++failures === FAILURES_IN_A_ROW) break;+ continue;+ }+ checked++;+ if (!item) {+ await closeRemoved(send);+ continue;+ }+ // Live since the send: a live item sent again isn't published until Webflow publishes it again.+ const publishedAt = Date.parse(item.lastPublished ?? '');+ const sentAt = Date.parse(send.sent_at ?? '');+ if (!item.live || !item.slug || !(publishedAt >= sentAt)) continue;+ const url = `https://${site.host}/${encodeURIComponent(collection)}/${encodeURIComponent(item.slug)}`;+ await markDeliveryPublished(send, url);+ published.push({ draftId: send.draft_id, url });+ }+ } catch (err) {+ if (err instanceof WebflowError && err.code === 'auth_failed') {+ await markRefused(connection);+ return { checked, published };+ }+ throw err;+ }+ return { checked, published };+}++/**+ * The daily check, for brands with Webflow connected (on an active+ * subscription), in a shuffled order so the deadline leaves out different+ * brands each day; one brand's failure is logged and the next goes on.+ */+export async function sweepWebflowPublished() {+ if (!isWebflowConfigured()) return { brands: 0, published: 0 };+ const started = Date.now();+ const { data, error } = await supabaseAdmin+ .from('brand_cms_connections')+ .select('brand_id, brands!inner(organizations!inner(subscription_status))')+ .eq('provider', 'webflow')+ .eq('status', 'connected')+ .not('site_id', 'is', null)+ .limit(SWEEP_CONNECTIONS);+ if (error) throw new Error(`brand_cms_connections read: ${error.message}`);+ const due = (data ?? []).filter((row) =>+ isSubscriptionActive(row.brands?.organizations?.subscription_status),+ );+ for (let i = due.length - 1; i > 0; i--) {+ const j = Math.floor(Math.random() * (i + 1));+ [due[i], due[j]] = [due[j], due[i]];+ }+ let brands = 0;+ let published = 0;+ for (const row of due) {+ if (Date.now() - started > SWEEP_DEADLINE_MS) break;+ try {+ const result = await checkWebflowPublished(row.brand_id, { limit: ITEMS_DAILY });+ brands++;+ published += result.published.length;+ } catch (err) {+ logger.error({ err: logged(err), brandId: row.brand_id }, '[webflow-status] check failed');+ }+ }+ return { brands, published };+}diff --git a/server/src/lib/cms/webflow-status.test.js b/server/src/lib/cms/webflow-status.test.jsnew file mode 100644index 000000000..b4c6b1e89--- /dev/null+++ b/server/src/lib/cms/webflow-status.test.js@@ -0,0 +1,225 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++/**+ * Pages sent to Webflow, published there since (Send to CMS #1331, phase 2):+ * read from the items the sends made, marked published at their address.+ */++const db = { connection: null, sends: [], brands: [], queries: [], updates: [] };+vi.mock('../../config/supabase.js', () => ({+ default: {+ from: (table) => {+ const q = { table, filters: [] };+ const result = () => {+ if (q.update) {+ db.updates.push({ table, update: q.update, filters: q.filters });+ return { data: null, error: null };+ }+ if (table === 'brand_cms_connections') {+ return { data: q.single ? db.connection : db.brands, error: null };+ }+ db.queries.push(q);+ return { data: db.sends, error: null };+ };+ const chain = {+ select: () => chain,+ eq: (col, value) => (q.filters.push(['eq', col, value]), chain),+ not: (col, op, value) => (q.filters.push(['not', col, op, value]), chain),+ gte: (col, value) => (q.filters.push(['gte', col, value]), chain),+ order: () => chain,+ limit: () => chain,+ update: (row) => ((q.update = row), chain),+ maybeSingle: async () => ((q.single = true), result()),+ then: (resolve) => resolve(result()),+ };+ return chain;+ },+ },+}));+const configured = vi.hoisted(() => ({ on: true }));+vi.mock('../composio.js', () => ({ isWebflowConfigured: () => configured.on }));+const markDeliveryPublished = vi.fn();+vi.mock('../content-published.js', () => ({+ markDeliveryPublished: (...args) => markDeliveryPublished(...args),+}));+const webflow = { listSites: vi.fn(), listCollections: vi.fn(), getItem: vi.fn() };+vi.mock('./webflow.js', async (importOriginal) => ({+ ...(await importOriginal()),+ listSites: (...args) => webflow.listSites(...args),+ listCollections: (...args) => webflow.listCollections(...args),+ getItem: (...args) => webflow.getItem(...args),+}));+const logged = [];+vi.mock('../logger.js', () => ({+ logger: { warn: () => {}, error: (...args) => logged.push(JSON.stringify(args)) },+}));++const { checkWebflowPublished, sweepWebflowPublished } = await import('./webflow-status.js');+const { WebflowError } = await import('./webflow.js');++const SENT_AT = '2026-10-24T10:00:00Z';+const send = (id, draftId, over = {}) => ({+ id,+ draft_id: draftId,+ status: 'sent',+ published_url: null,+ external_id: `item-${id}`,+ target_collection: 'c1',+ sent_at: SENT_AT,+ ...over,+});++beforeEach(() => {+ configured.on = true;+ db.connection = {+ brand_id: 'b1',+ composio_account_id: 'ca_1',+ composio_entity_id: 'brand_b1',+ status: 'connected',+ site_id: 's1',+ };+ db.sends = [send('d1', 'dr1'), send('d0', 'dr1'), send('d2', 'dr2')];+ db.brands = [];+ db.queries = [];+ db.updates = [];+ logged.length = 0;+ markDeliveryPublished.mockReset().mockResolvedValue(undefined);+ webflow.listSites+ .mockReset()+ .mockResolvedValue([{ id: 's1', name: 'Acme', host: 'www.acme.example' }]);+ webflow.listCollections+ .mockReset()+ .mockResolvedValue([{ id: 'c1', name: 'Blog Posts', slug: 'blog' }]);+ webflow.getItem.mockReset().mockImplementation(async (_account, _collection, itemId) => ({+ id: itemId,+ slug: itemId === 'item-d1' ? 'cold-brew' : 'espresso',+ live: itemId === 'item-d1',+ lastPublished: itemId === 'item-d1' ? '2026-10-24T12:00:00Z' : null,+ }));+});++describe('checkWebflowPublished', () => {+ it('marks a page whose item is live published at its address, and leaves a draft one', async () => {+ expect(await checkWebflowPublished('b1')).toEqual({+ checked: 2,+ published: [{ draftId: 'dr1', url: 'https://www.acme.example/blog/cold-brew' }],+ });+ // A page's latest send only: d0 made the same item.+ expect(webflow.getItem.mock.calls.map((call) => call[2])).toEqual(['item-d1', 'item-d2']);+ expect(markDeliveryPublished).toHaveBeenCalledTimes(1);+ expect(markDeliveryPublished).toHaveBeenCalledWith(+ expect.objectContaining({ id: 'd1' }),+ 'https://www.acme.example/blog/cold-brew',+ );+ // The sends read: the brand's, to Webflow, sent and not seen live, with their item.+ expect(db.queries[0].filters).toEqual([+ ['eq', 'brand_id', 'b1'],+ ['eq', 'target', 'webflow'],+ ['eq', 'status', 'sent'],+ ['not', 'external_id', 'is', null],+ ['not', 'target_collection', 'is', null],+ ['gte', 'sent_at', expect.any(String)],+ ]);+ });++ it('waits for Webflow’s next publish when a live page is sent again', async () => {+ // Published before this send: the item is live with what was sent before.+ db.sends = [send('d1', 'dr1', { sent_at: '2026-10-25T09:00:00Z' })];+ expect(await checkWebflowPublished('b1')).toEqual({ checked: 1, published: [] });+ expect(markDeliveryPublished).not.toHaveBeenCalled();+ });++ it('closes a send whose item was deleted in Webflow, and goes on past one that can’t be read', async () => {+ db.sends = [send('d3', 'dr3'), send('d4', 'dr4'), send('d1', 'dr1')];+ webflow.getItem+ .mockResolvedValueOnce(null)+ .mockRejectedValueOnce(new WebflowError('failed', 'down'));+ expect(await checkWebflowPublished('b1')).toEqual({+ checked: 2,+ published: [{ draftId: 'dr1', url: 'https://www.acme.example/blog/cold-brew' }],+ });+ expect(db.updates).toEqual([+ {+ table: 'content_draft_deliveries',+ update: { status: 'failed', error: 'Removed in Webflow' },+ filters: [+ ['eq', 'id', 'd3'],+ ['eq', 'status', 'sent'],+ ],+ },+ ]);+ });++ it('stops after three items fail in a row, and reads only as many as asked', async () => {+ db.sends = ['a', 'b', 'c', 'd'].map((id) => send(id, `dr-${id}`));+ webflow.getItem.mockRejectedValue(new WebflowError('failed', 'down'));+ expect(await checkWebflowPublished('b1')).toEqual({ checked: 0, published: [] });+ expect(webflow.getItem).toHaveBeenCalledTimes(3);+ webflow.getItem+ .mockReset()+ .mockResolvedValue({ id: 'x', slug: 'x', live: false, lastPublished: null });+ await checkWebflowPublished('b1', { limit: 2 });+ expect(webflow.getItem).toHaveBeenCalledTimes(2);+ });++ it('reads nothing without Webflow set up, connected and on a site', async () => {+ configured.on = false;+ expect(await checkWebflowPublished('b1')).toEqual({ checked: 0, published: [] });+ configured.on = true;+ db.connection = { ...db.connection, status: 'auth_failed' };+ expect(await checkWebflowPublished('b1')).toEqual({ checked: 0, published: [] });+ db.connection = null;+ expect(await checkWebflowPublished('b1')).toEqual({ checked: 0, published: [] });+ expect(webflow.listSites).not.toHaveBeenCalled();+ });++ it('skips a send to a collection no longer on the site, and a site with no host', async () => {+ db.sends = [send('d1', 'dr1', { target_collection: 'c9' })];+ expect(await checkWebflowPublished('b1')).toEqual({ checked: 0, published: [] });+ expect(webflow.getItem).not.toHaveBeenCalled();+ webflow.listSites.mockResolvedValue([{ id: 's1', name: 'Acme', host: null }]);+ db.sends = [send('d1', 'dr1')];+ expect(await checkWebflowPublished('b1')).toEqual({ checked: 0, published: [] });+ });++ it('stops at a refused token, marking only that connection', async () => {+ webflow.getItem.mockRejectedValueOnce(+ new WebflowError('auth_failed', 'Webflow refused the site token'),+ );+ expect(await checkWebflowPublished('b1')).toEqual({ checked: 0, published: [] });+ expect(db.updates).toEqual([+ {+ table: 'brand_cms_connections',+ update: { status: 'auth_failed', updated_at: expect.any(String) },+ filters: [+ ['eq', 'brand_id', 'b1'],+ ['eq', 'provider', 'webflow'],+ ['eq', 'composio_account_id', 'ca_1'],+ ],+ },+ ]);+ // Webflow or Composio down before the items: the check fails, for the caller to log.+ webflow.listSites.mockRejectedValueOnce(new WebflowError('failed', 'down'));+ await expect(checkWebflowPublished('b1')).rejects.toMatchObject({ code: 'failed' });+ });+});++describe('sweepWebflowPublished', () => {+ it('checks each connected brand on an active subscription, going on past one that fails', async () => {+ db.brands = [+ { brand_id: 'b1', brands: { organizations: { subscription_status: 'active' } } },+ { brand_id: 'b2', brands: { organizations: { subscription_status: 'canceled' } } },+ { brand_id: 'b3', brands: { organizations: { subscription_status: 'active' } } },+ ];+ // On the cloud, where subscriptions count.+ vi.stubEnv('IS_CLOUD', 'true');+ webflow.listSites.mockRejectedValueOnce(new Error('down'));+ expect(await sweepWebflowPublished()).toEqual({ brands: 1, published: 1 });+ vi.unstubAllEnvs();+ // The brand that failed (the order is shuffled daily) is logged; the other went on.+ expect(logged).toHaveLength(1);+ expect(logged[0]).toMatch(/"brandId":"b[13]"/);+ configured.on = false;+ expect(await sweepWebflowPublished()).toEqual({ brands: 0, published: 0 });+ });+});diff --git a/server/src/lib/cms/webflow.js b/server/src/lib/cms/webflow.jsindex fa3fa7fc7..58a943cd9 100644--- a/server/src/lib/cms/webflow.js+++ b/server/src/lib/cms/webflow.js@@ -96,7 +96,32 @@ const objectOf = (data) => data?.response_data ?? data?.data ?? data ?? {};const text = (value, max = 300) => (typeof value === 'string' ? value.trim().slice(0, max) : '');-/** The sites the token reaches: a site token reaches its own. */+const HOST =+ /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/i;++/**+ * The host a site's pages live on: the custom domain published to last,+ * else its webflow.io address (a site published only there), else its+ * first custom domain; null when Webflow names none.+ */+function siteHost(site) {+ const domains = (Array.isArray(site.customDomains) ? site.customDomains : [])+ .map((d) => ({+ host: text(d?.url)+ .replace(/^https?:\/\//i, '')+ .replace(/\/.*$/, '')+ .toLowerCase(),+ at: Date.parse(d?.lastPublished ?? '') || 0,+ }))+ .filter((d) => HOST.test(d.host));+ const published = domains.filter((d) => d.at).sort((a, b) => b.at - a.at)[0];+ if (published) return published.host;+ const short = text(site.shortName).toLowerCase();+ if (short && HOST.test(`${short}.webflow.io`)) return `${short}.webflow.io`;+ return domains[0]?.host ?? null;+}++/** The sites the token reaches (a site token reaches its own), with the host their pages live on. */export async function listSites(account) {const data = await run('WEBFLOW_LIST_WEBFLOW_SITES', account, {});return listOf(data, 'sites')@@ -104,6 +129,7 @@ export async function listSites(account) {.map((site) => ({id: site.id,name: text(site.displayName) || text(site.shortName) || site.id,+ host: siteHost(site),}));}@@ -139,6 +165,34 @@ export async function getCollection(account, collectionId) {};}+/**+ * An item as Webflow has it: its slug, whether it's live (published, not a+ * draft, not archived) and when it was last published. Null when it's gone.+ */+export async function getItem(account, collectionId, itemId) {+ let item;+ try {+ item = objectOf(+ await run('WEBFLOW_GET_COLLECTION_ITEM', account, {+ collection_id: collectionId,+ item_id: itemId,+ }),+ );+ } catch (err) {+ if (err instanceof WebflowError && err.code === 'not_found') return null;+ throw err;+ }+ if (item?.item) item = item.item;+ const fields = item.fieldData ?? item.field_data ?? {};+ const lastPublished = typeof item.lastPublished === 'string' ? item.lastPublished : null;+ return {+ id: typeof item.id === 'string' && item.id ? item.id : itemId,+ slug: text(fields.slug, 256),+ live: Boolean(lastPublished) && item.isDraft !== true && item.isArchived !== true,+ lastPublished,+ };+}+/** An item's id from Webflow's answer, however Composio wraps it. */function itemIdOf(data) {const item = objectOf(data);diff --git a/server/src/lib/cms/webflow.test.js b/server/src/lib/cms/webflow.test.jsindex 9ba0c967a..2fb111591 100644--- a/server/src/lib/cms/webflow.test.js+++ b/server/src/lib/cms/webflow.test.js@@ -12,6 +12,7 @@ const {checkFieldMap,createItem,getCollection,+ getItem,listCollections,listSites,suggestFieldMap,@@ -40,13 +41,15 @@ describe('reading the site', () => {{ displayName: 'no id' },],});- expect(await listSites(account)).toEqual([{ id: 's1', name: 'Ansvisor' }]);+ expect(await listSites(account)).toEqual([+ { id: 's1', name: 'Ansvisor', host: 'ansvisor.webflow.io' },+ ]);expect(executeWebflowTool).toHaveBeenCalledWith('WEBFLOW_LIST_WEBFLOW_SITES', account, {});executeWebflowTool.mockResolvedValueOnce({response_data: { sites: [{ id: 's2', shortName: 'acme' }] },});- expect(await listSites(account)).toEqual([{ id: 's2', name: 'acme' }]);+ expect(await listSites(account)).toEqual([{ id: 's2', name: 'acme', host: 'acme.webflow.io' }]);});it('lists a site’s collections, and a collection’s fields a person can fill', async () => {@@ -240,3 +243,80 @@ describe('items', () => {});});});++describe('where pages live', () => {+ it('reads a site’s host: its custom domain published to, else its first, else webflow.io', async () => {+ executeWebflowTool.mockResolvedValueOnce({+ sites: [+ {+ id: 's1',+ shortName: 'acme',+ customDomains: [+ { url: 'staging.acme.example', lastPublished: null },+ { url: 'https://www.acme.example/', lastPublished: '2026-10-20T10:00:00Z' },+ ],+ },+ // Never published to its domain: published on webflow.io only.+ { id: 's2', shortName: 'beta', customDomains: [{ url: 'Beta.Example' }] },+ { id: 's3', customDomains: [{ url: 'not a host' }] },+ { id: 's4', customDomains: [{ url: 'gamma.example' }] },+ // Both published: the one published to last (the canonical one, as Webflow redirects).+ {+ id: 's5',+ shortName: 'delta',+ customDomains: [+ { url: 'delta.example', lastPublished: '2026-10-01T10:00:00Z' },+ { url: 'www.delta.example', lastPublished: '2026-10-21T10:00:00Z' },+ ],+ },+ ],+ });+ expect((await listSites(account)).map((s) => s.host)).toEqual([+ 'www.acme.example',+ 'beta.webflow.io',+ null,+ 'gamma.example',+ 'www.delta.example',+ ]);+ });++ it('reads whether an item is live, and its slug; a deleted one is none', async () => {+ executeWebflowTool.mockResolvedValueOnce({+ id: 'i1',+ lastPublished: '2026-10-25T10:00:00Z',+ isDraft: false,+ isArchived: false,+ fieldData: { slug: 'cold-brew', name: 'Cold brew' },+ });+ expect(await getItem(account, 'c1', 'i1')).toEqual({+ id: 'i1',+ slug: 'cold-brew',+ live: true,+ lastPublished: '2026-10-25T10:00:00Z',+ });+ expect(executeWebflowTool).toHaveBeenLastCalledWith('WEBFLOW_GET_COLLECTION_ITEM', account, {+ collection_id: 'c1',+ item_id: 'i1',+ });+ executeWebflowTool.mockResolvedValueOnce({+ response_data: { id: 'i1', lastPublished: null, isDraft: true, fieldData: { slug: 'x' } },+ });+ expect(await getItem(account, 'c1', 'i1')).toMatchObject({ live: false });+ executeWebflowTool.mockResolvedValueOnce({+ id: 'i1',+ lastPublished: '2026-10-25T10:00:00Z',+ isDraft: true,+ fieldData: { slug: 'x' },+ });+ expect(await getItem(account, 'c1', 'i1')).toMatchObject({ live: false });+ // Wrapped as { item }.+ executeWebflowTool.mockResolvedValueOnce({+ item: { id: 'i1', lastPublished: '2026-10-25T10:00:00Z', fieldData: { slug: 'y' } },+ });+ expect(await getItem(account, 'c1', 'i1')).toMatchObject({ slug: 'y', live: true });+ executeWebflowTool.mockRejectedValueOnce(new Error('404 resource_not_found'));+ expect(await getItem(account, 'c1', 'i1')).toBeNull();+ executeWebflowTool.mockRejectedValueOnce(new Error('401 Unauthorized'));+ await expect(getItem(account, 'c1', 'i1')).rejects.toMatchObject({ code: 'auth_failed' });+ });+});diff --git a/server/src/lib/content-published.js b/server/src/lib/content-published.jsnew file mode 100644index 000000000..4eb7558f5--- /dev/null+++ b/server/src/lib/content-published.js@@ -0,0 +1,67 @@+import supabaseAdmin from '../config/supabase.js';+import { logger } from './logger.js';++/**+ * A sent page now live at `url` (Send to CMS #1331), as a webhook's+ * callback says (phase 1) or Webflow shows (phase 2): the delivery is+ * published there, and its page too when it is the page's latest delivery+ * (an older one's late word doesn't move the page's address back), keeping+ * the page in History as a "publish" version unless History's latest+ * version is already this page.+ *+ * A delivery published at the same address already changes nothing, but a+ * CMS id it now names. A new delivery of a page live at the same address+ * (the post updated in place) is the page as published again. The delivery+ * is written last: a write before it that fails throws, and the next try,+ * finding it not yet published, does it all again.+ *+ * @param {{ id: string, draft_id: string|null, status: string, published_url: string|null }} delivery+ * @param {string} url+ * @param {{ externalId?: string|null }} [options]+ */+export async function markDeliveryPublished(delivery, url, { externalId = null } = {}) {+ const now = new Date().toISOString();+ const own = externalId ? { external_id: externalId } : {};++ if (delivery.status === 'published' && delivery.published_url === url) {+ if (externalId) {+ const { error } = await supabaseAdmin+ .from('content_draft_deliveries')+ .update(own)+ .eq('id', delivery.id);+ if (error) throw new Error(error.message);+ }+ return;+ }+ if (delivery.draft_id) {+ const { data: latest, error: latestError } = await supabaseAdmin+ .from('content_draft_deliveries')+ .select('id')+ .eq('draft_id', delivery.draft_id)+ .order('created_at', { ascending: false })+ .limit(1)+ .maybeSingle();+ if (latestError) throw new Error(latestError.message);+ if (latest?.id === delivery.id) {+ const { error: draftError } = await supabaseAdmin+ .from('content_drafts')+ .update({ status: 'published', published_url: url, published_at: now })+ .eq('id', delivery.draft_id);+ if (draftError) throw new Error(draftError.message);+ // The page as published, in History (00215 keeps "publish" for the+ // server), unless History's latest version is already this page.+ const { error: keepError } = await supabaseAdmin.rpc('content_draft_snapshot', {+ p_draft_id: delivery.draft_id,+ p_origin: 'publish',+ });+ if (keepError) {+ logger.warn({ err: keepError }, '[content-published] publish version not kept');+ }+ }+ }+ const { error: deliveryError } = await supabaseAdmin+ .from('content_draft_deliveries')+ .update({ status: 'published', published_url: url, published_at: now, ...own })+ .eq('id', delivery.id);+ if (deliveryError) throw new Error(deliveryError.message);+}diff --git a/server/src/routes/content-drafts-send.test.js b/server/src/routes/content-drafts-send.test.jsindex 6c5aa1009..91e16fa19 100644--- a/server/src/routes/content-drafts-send.test.js+++ b/server/src/routes/content-drafts-send.test.js@@ -25,6 +25,10 @@ vi.mock('../config/supabase.js', () => ({},}));vi.mock('../lib/access.js', () => ({ assertBrandAccess: (...args) => assertBrandAccess(...args) }));+const checkWebflowPublished = vi.fn();+vi.mock('../lib/cms/webflow-status.js', () => ({+ checkWebflowPublished: (...args) => checkWebflowPublished(...args),+}));vi.mock('../lib/content-delivery.js', async (importOriginal) => ({...(await importOriginal()),sendContentPages: (...args) => sendContentPages(...args),@@ -133,3 +137,47 @@ describe('POST /api/content-drafts/send', () => {expect(await res.json()).toMatchObject({ code: 'failed' });});});++describe('POST /api/content-drafts/webflow-status', () => {+ const check = (body) =>+ fetch(`${baseUrl}/webflow-status`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify(body),+ });++ it('reads the brand’s Webflow pages for any member, a brand at most every two minutes', async () => {+ role = 'member';+ const brand = '44444444-4444-4444-4444-444444444444';+ checkWebflowPublished.mockReset().mockResolvedValue({+ checked: 1,+ published: [{ draftId: DRAFT, url: 'https://a.example/b/c' }],+ });+ const first = await check({ brandId: brand });+ expect(await first.json()).toEqual({+ checked: 1,+ published: [{ draftId: DRAFT, url: 'https://a.example/b/c' }],+ });+ expect(assertBrandAccess).toHaveBeenCalledWith(brand, 'u1');+ expect(await (await check({ brandId: brand })).json()).toEqual({+ checked: 0,+ published: [],+ recent: true,+ });+ expect(checkWebflowPublished).toHaveBeenCalledTimes(1);+ });++ it('refuses another workspace’s brand and a malformed one, and names a failure', async () => {+ assertBrandAccess.mockRejectedValueOnce(+ Object.assign(new Error('nope'), { status: 403, code: 'not_in_workspace' }),+ );+ const other = await check({ brandId: '55555555-5555-5555-5555-555555555555' });+ expect(other.status).toBe(403);+ expect(await other.json()).toMatchObject({ code: 'not_in_workspace' });+ expect((await check({ brandId: 'x' })).status).toBe(400);+ checkWebflowPublished.mockReset().mockRejectedValue(new Error('down'));+ const failed = await check({ brandId: '66666666-6666-6666-6666-666666666666' });+ expect(failed.status).toBe(502);+ expect(await failed.json()).toMatchObject({ code: 'failed' });+ });+});diff --git a/server/src/routes/content-drafts.js b/server/src/routes/content-drafts.jsindex 50feaa252..fd94b11f2 100644--- a/server/src/routes/content-drafts.js+++ b/server/src/routes/content-drafts.js@@ -6,6 +6,8 @@ import { handOffDraft, handOffInput } from '../lib/content-handoff.js';import { qualityPhase } from '../lib/content-quality.js';import { rebuildDraftBrief, rebuildSources } from '../lib/content-brief.js';import { DeliveryError, sendContentPages, sendInput } from '../lib/content-delivery.js';+import { checkWebflowPublished } from '../lib/cms/webflow-status.js';+import { cmsLimiter } from '../middleware/rate-limiter.js';import { stopDraftRun } from '../lib/content-runs.js';import { startDraftWriting, writeDraftInput } from '../lib/content-writer.js';import supabaseAdmin from '../config/supabase.js';@@ -75,6 +77,44 @@ router.post('/send', async (req, res) => {}});+/** A brand's Webflow pages read at most this often on demand (the daily sweep reads them too). */+const WEBFLOW_STATUS_EVERY_MS = 2 * 60 * 1000;+const webflowChecked = new Map();+const webflowStatusBody = z.object({ brandId: z.guid() });++/**+ * POST /api/content-drafts/webflow-status+ * Body: { brandId }. Pages the brand sent to Webflow, published there since,+ * marked published at their address (cms/webflow-status.js). For any member+ * of the brand's workspace (the Library asks when it opens), a brand at most+ * every two minutes: { checked, published: [{ draftId, url }], recent? }.+ */+router.post('/webflow-status', cmsLimiter, async (req, res) => {+ const parsed = webflowStatusBody.safeParse(req.body ?? {});+ if (!parsed.success) return res.status(400).json({ error: 'Invalid request', code: 'invalid' });+ const { brandId } = parsed.data;+ try {+ await assertBrandAccess(brandId, req.user.id);+ const last = webflowChecked.get(brandId) ?? 0;+ if (Date.now() - last < WEBFLOW_STATUS_EVERY_MS) {+ return res.json({ checked: 0, published: [], recent: true });+ }+ webflowChecked.set(brandId, Date.now());+ return res.json(await checkWebflowPublished(brandId));+ } catch (err) {+ 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: { name: err?.name, code: err?.code } },+ 'content draft webflow status error',+ );+ return res.status(502).json({ error: 'Webflow could not be read', code: 'failed' });+ }+});+const rebuildBody = z.object({// guid, not uuid: brand ids aren't all RFC 4122 versions (seeded ones).brandId: z.guid(),diff --git a/server/src/routes/webhook-content-draft-callback.js b/server/src/routes/webhook-content-draft-callback.jsindex 9cf5c4722..86c09d3f1 100644--- a/server/src/routes/webhook-content-draft-callback.js+++ b/server/src/routes/webhook-content-draft-callback.js@@ -7,6 +7,7 @@ import {webhookCallbackAuthFailureLimiter,} from '../middleware/rate-limiter.js';import { webhookSecretsMatch } from '../lib/webhook-secret.js';+import { markDeliveryPublished } from '../lib/content-published.js';/*** Send to CMS's callback (#1331): the workspace's automation tells how a sent@@ -119,7 +120,6 @@ router.post('/:deliveryId/status', async (req, res) => {});}const { status, externalId, reason } = parsed.data;- const now = new Date().toISOString();const own = externalId ? { external_id: externalId } : {};if (status === 'failed') {@@ -139,53 +139,8 @@ router.post('/:deliveryId/status', async (req, res) => {return res.json({ success: true, status: failed?.length ? 'failed' : 'published' });}- // This delivery said so already (a workflow's retry): nothing changes,- // its time included, but a CMS id the retry adds. A new delivery of a- // page live at the same address (the post updated in place) is the page- // as published again.- if (delivery.status === 'published' && delivery.published_url === url) {- if (externalId) {- const { error: idError } = await supabaseAdmin- .from('content_draft_deliveries')- .update(own)- .eq('id', delivery.id);- if (idError) throw new Error(idError.message);- }- return res.json({ success: true, status: 'published' });- }- if (delivery.draft_id) {- const { data: latest, error: latestError } = await supabaseAdmin- .from('content_draft_deliveries')- .select('id')- .eq('draft_id', delivery.draft_id)- .order('created_at', { ascending: false })- .limit(1)- .maybeSingle();- if (latestError) throw new Error(latestError.message);- if (latest?.id === delivery.id) {- const { error: draftError } = await supabaseAdmin- .from('content_drafts')- .update({ status: 'published', published_url: url, published_at: now })- .eq('id', delivery.draft_id);- if (draftError) throw new Error(draftError.message);- // The page as published, in History (00215 keeps "publish" for the- // server), unless History's latest version is already this page.- const { error: keepError } = await supabaseAdmin.rpc('content_draft_snapshot', {- p_draft_id: delivery.draft_id,- p_origin: 'publish',- });- if (keepError) {- req.log?.warn?.({ err: keepError }, 'content draft publish version not kept');- }- }- }- // The delivery last: a write above that fails answers 500, and the- // workflow's retry, finding it not yet published, does it all again.- const { error: deliveryError } = await supabaseAdmin- .from('content_draft_deliveries')- .update({ status: 'published', published_url: url, published_at: now, ...own })- .eq('id', delivery.id);- if (deliveryError) throw new Error(deliveryError.message);+ // A workflow's retry changes nothing but a CMS id it adds (content-published.js).+ await markDeliveryPublished(delivery, url, { externalId });return res.json({ success: true, status: 'published' });} catch (error) {req.log?.error?.({ err: error }, 'content draft status callback error');diff --git a/server/src/server.js b/server/src/server.jsindex cb6bad217..59e74ce30 100644--- a/server/src/server.js+++ b/server/src/server.js@@ -29,6 +29,7 @@ import { sweepActionValidation } from './lib/action-center/validate.js';import { sweepImproveOutcomes } from './lib/action-center/improve-outcome.js';import { sweepBrandFacts } from './lib/brand-facts-sweep.js';import { sweepCatalogSources } from './lib/catalog/sources.js';+import { sweepWebflowPublished } from './lib/cms/webflow-status.js';import { parseScraperResponse } from './lib/cloro-scraper.js';import { handleScraperResult } from './lib/cloro-result-handler.js';import { verifyCloroWebhook } from './lib/cloro-webhook-verify.js';@@ -290,6 +291,11 @@ async function runDailyTracking() {logger.error({ err }, '[catalog] daily sweep failed');});+ // Pages sent to Webflow, published there since (#1331): read once a day.+ sweepWebflowPublished().catch((err) => {+ logger.error({ err: { name: err?.name } }, '[webflow-status] daily sweep failed');+ });+// Google Analytics sync (#704) — same contract as the Search Console sync// above: independent, never blocks or fails the tracking run, no-ops when// Composio or the Analytics auth config isn't set.diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsxindex 9d4bbf624..546353d23 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsx@@ -48,8 +48,10 @@ vi.mock('@/lib/download', () => ({ saveBlob }));const readCmsStatuses = vi.hoisted(() => vi.fn());const readDraftCms = vi.hoisted(() => vi.fn());const sendPagesToCms = vi.hoisted(() => vi.fn());+const refreshWebflowPublished = vi.hoisted(() => vi.fn());vi.mock('@/lib/content/cms-send-client', () => ({readCmsStatuses,+ refreshWebflowPublished,readDraftCms,sendPagesToCms,readKeptFields: vi.fn(async () => new Map()),@@ -123,6 +125,7 @@ beforeEach(() => {document.body.appendChild(container);root = createRoot(container);getContentDrafts.mockReset();+ refreshWebflowPublished.mockReset().mockResolvedValue([]);getContentStartOptions.mockReset().mockResolvedValue({opportunities: [],prompts: [],@@ -498,6 +501,40 @@ describe('Send to CMS from a Library row (#1331)', () => {expect(container.querySelector('[aria-label^="In Webflow as a draft item since Oct 24, 2026"]'),).not.toBeNull();+ // Webflow says when it goes live: asked once as the Library opens.+ expect(refreshWebflowPublished).toHaveBeenCalledTimes(1);+ expect(refreshWebflowPublished).toHaveBeenCalledWith('brand-1');+ });++ it('reads the list again when pages sent to Webflow went live there', async () => {+ getContentDrafts.mockResolvedValue([row({})]);+ readCmsStatuses.mockResolvedValue(+ new Map([+ [+ 'd1',+ {+ status: 'sent',+ target: 'webflow',+ at: '2026-10-24T10:00:00Z',+ httpStatus: null,+ error: null,+ publishedUrl: null,+ },+ ],+ ]),+ );+ refreshWebflowPublished.mockResolvedValue(['d1']);+ await renderPage();+ await act(async () => {});+ expect(getContentDrafts).toHaveBeenCalledTimes(2);+ // Once a brand per visit, however often the list is read.+ expect(refreshWebflowPublished).toHaveBeenCalledTimes(1);+ });++ it('doesn’t ask Webflow for a brand with no page there', async () => {+ getContentDrafts.mockResolvedValue([row({})]);+ await renderPage();+ expect(refreshWebflowPublished).not.toHaveBeenCalled();});it('says the workflow’s reason for a page its webhook took that then failed', async () => {diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.tsxindex 083a6ef7f..9f3e574f9 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.tsx@@ -1,6 +1,6 @@'use client';-import { useEffect, useState } from 'react';+import { useEffect, useRef, useState } from 'react';import { useTranslations } from 'next-intl';import { toast } from 'sonner';import { Plus, Send } from 'lucide-react';@@ -35,7 +35,11 @@ import {setContentDraftStatus,} from '@/lib/actions/content-drafts';import { CMS_PAGES_PER_SEND, type CmsSource } from '@/lib/content/cms-send';-import { readCmsStatuses, type CmsStatus } from '@/lib/content/cms-send-client';+import {+ readCmsStatuses,+ refreshWebflowPublished,+ type CmsStatus,+} from '@/lib/content/cms-send-client';import {AGENT_POLL_MS,CREATED_RANGES,@@ -169,6 +173,23 @@ export default function ContentLibraryPage() {return () => clearTimeout(timer);}, [cmsGoing, cms]);+ // Pages in Webflow as draft items: Webflow says when one goes live, so the+ // API reads them once as the Library opens for the brand (and daily); the+ // list is read again when it marked any published.+ const inWebflow = [...cms.values()].some(+ (status) => status.target === 'webflow' && status.status === 'sent',+ );+ const webflowRead = useRef(new Set<string>());+ useEffect(() => {+ if (!inWebflow || !activeBrandId || webflowRead.current.has(activeBrandId)) return;+ webflowRead.current.add(activeBrandId);+ // Not cancelled with the effect: a filter or search changing meanwhile+ // still reads the list again, the brand's pages marked.+ void refreshWebflowPublished(activeBrandId).then((published) => {+ if (published.length) setAttempt((n) => n + 1);+ });+ }, [inWebflow, activeBrandId]);+const reload = () => setAttempt((n) => n + 1);const titleOf = (draft: ContentDraftListItem) => draft.title.trim() || t('untitled');const failed = (err: unknown) => {diff --git a/web/src/lib/content/cms-send-client.test.ts b/web/src/lib/content/cms-send-client.test.tsindex bd15b09ba..a744bf856 100644--- a/web/src/lib/content/cms-send-client.test.ts+++ b/web/src/lib/content/cms-send-client.test.ts@@ -39,8 +39,14 @@ vi.mock('@/lib/supabase/client', () => ({}),}));-const { readCmsStatuses, readDeliveries, readDraftCms, readKeptFields, sendPagesToCms } =- await import('./cms-send-client');+const {+ readCmsStatuses,+ readDeliveries,+ readDraftCms,+ readKeptFields,+ refreshWebflowPublished,+ sendPagesToCms,+} = await import('./cms-send-client');const page: CmsPage = {draftId: 'd1',@@ -163,6 +169,20 @@ describe('sendPagesToCms', () => {});});+describe('refreshWebflowPublished', () => {+ it('asks the API to read the brand’s Webflow pages, and gives the drafts it marked published', async () => {+ answer(200, { checked: 2, published: [{ draftId: 'd1', url: 'https://a.example/b/c' }] });+ expect(await refreshWebflowPublished('brand-1')).toEqual(['d1']);+ expect(fetchMock).toHaveBeenCalledWith('/api/aeo/api/content-drafts/webflow-status', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json', Authorization: 'Bearer token-1' },+ body: JSON.stringify({ brandId: 'brand-1' }),+ });+ answer(502, { code: 'failed' });+ expect(await refreshWebflowPublished('brand-1')).toEqual([]);+ });+});+describe('readDraftCms', () => {it('gives the kept fields, where the page went live and its last send', async () => {reads = {diff --git a/web/src/lib/content/cms-send-client.ts b/web/src/lib/content/cms-send-client.tsindex ba00a6b4c..c18f6e451 100644--- a/web/src/lib/content/cms-send-client.ts+++ b/web/src/lib/content/cms-send-client.ts@@ -113,6 +113,36 @@ export async function sendPagesToCms(}}+/**+ * Asks the API to read the brand's pages sent to Webflow, and mark the ones+ * published there since (#1331, phase 2). The pages it marked, by draft;+ * none when it couldn't read or read a moment ago.+ */+export async function refreshWebflowPublished(brandId: string): Promise<string[]> {+ try {+ const {+ data: { session },+ } = await createClient().auth.getSession();+ if (!session) return [];+ const res = await fetch(`${BROWSER_API_BASE_URL}/api/content-drafts/webflow-status`, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ Authorization: `Bearer ${session.access_token}`,+ },+ body: JSON.stringify({ brandId }),+ });+ if (!res.ok) return [];+ const body = (await res.json().catch(() => ({}))) as {+ published?: { draftId?: string | null }[];+ };+ return (body.published ?? []).flatMap((p) => (p.draftId ? [p.draftId] : []));+ } catch (err) {+ console.error('Failed to read the pages sent to Webflow:', err);+ return [];+ }+}+/** A draft's last send, as the Library shows it beside its status. */export interface CmsStatus {status: CmsDelivery['status'];