Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

feat(traffic): connect AI bots from Cloudflare or Vercel in the sources strip (#1181)

Cihanc3c1dae095a0
diff --git a/web/messages/en.json b/web/messages/en.json
index 18c542c3a..febf6ccb9 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -3344,12 +3344,82 @@
"notConnectedGsc": "Not connected · see if AI is taking Google clicks",
"unmapped": "Connected · choose a property for this brand",
"notConfigured": "Not set up on this server",
- "botsSoon": "Cloudflare / Vercel · coming soon",
+ "botsLoading": "Cloudflare / Vercel",
+ "botsFailed": "Cloudflare / Vercel · couldn't be read",
+ "botsLocked": "Cloudflare / Vercel · Growth+",
+ "botsNotConnected": "Cloudflare / Vercel · see which AI bots read your pages",
+ "botsWaiting": "Connected · waiting for the first AI-bot request",
+ "botsActive": "Cloudflare / Vercel · last event {when}",
+ "upgrade": "Upgrade",
"connect": "Connect",
"connecting": "Connecting…",
"manage": "Manage",
"connectNotAllowed": "Only admins and managers can connect a source."
},
+ "bots": {
+ "title": "AI bots reading your site",
+ "description": "See which AI bots read your pages, from your own Cloudflare or Vercel. Verified bots only: the user agent and the operator's published IP ranges must both match. No change to your site's speed.",
+ "connected": "Connected",
+ "choose": "How should your site send AI-bot requests?",
+ "addAnother": "Add another way",
+ "recommended": "Recommended",
+ "create": "Create connection",
+ "notAllowed": "Only admins and managers can connect a source.",
+ "planLimit": "The AI bot report comes with the Growth plan.",
+ "lastEvent": "Last request {when}",
+ "waiting": "Waiting for the first request",
+ "revoke": "Revoke",
+ "revokeConfirm": "Revoke: stop reading it",
+ "cancel": "Cancel",
+ "revoked": "Connection revoked. Its sender is no longer read.",
+ "usage": "This month: {stored} of {cap} detailed rows.",
+ "usageNoCap": "This month: {stored} detailed {count, plural, one {row} other {rows}}.",
+ "paused": "Detailed rows paused until {day}, totals still counted.",
+ "secretOnce": "Copy these settings now: the secret in them isn't shown again. Lost it? Revoke this connection and create a new one.",
+ "copy": "Copy",
+ "copied": "Copied",
+ "copyWorker": "Worker code",
+ "copyDestination": "Destination",
+ "copyLogpushJob": "Job settings (Cloudflare API: POST /zones/ZONE_ID/logpush/jobs)",
+ "copyEndpoint": "Endpoint URL",
+ "copyHeader": "Custom header",
+ "vercelFormat": "Format: JSON. Log sources: {sources}.",
+ "done": "Done",
+ "kinds": {
+ "cloudflare_worker": {
+ "name": "Cloudflare Worker",
+ "what": "A small Worker on your site's route passes every request through and sends only AI-bot requests, in batches.",
+ "cost": "On your Cloudflare bill: free up to 100,000 requests a day, then $5 a month for 10 million and $0.30 per extra million. It runs on every request, so busy sites may need the paid plan."
+ },
+ "cloudflare_logpush": {
+ "name": "Cloudflare Logpush",
+ "what": "Cloudflare sends only the log lines of AI bots, filtered at its edge. Nothing runs on your requests.",
+ "cost": "Logpush is on all Cloudflare plans and avoids the per-request Worker cost; whether the HTTP requests dataset is available depends on your account."
+ },
+ "vercel_drain": {
+ "name": "Vercel Drain",
+ "what": "Vercel sends your request logs; Ansvisor keeps the AI bots and drops the rest.",
+ "cost": "Vercel Pro and Enterprise only, at $0.50 per GB of drained data on your Vercel bill."
+ }
+ },
+ "steps": {
+ "cloudflare_worker": [
+ "In Cloudflare, open Workers & Pages and create a Worker.",
+ "Replace its code with the Worker code below and deploy it.",
+ "Add a route for your site, such as example.com/*, so the Worker sees its requests."
+ ],
+ "cloudflare_logpush": [
+ "In Cloudflare, open Analytics & Logs › Logpush and create a job for the HTTP requests dataset.",
+ "Choose HTTP destination and paste the destination below.",
+ "Pick the fields and the filter from the job settings below, or create the job through the API with them."
+ ],
+ "vercel_drain": [
+ "In Vercel, open Team Settings › Drains and add a Logs drain for your site's project.",
+ "Choose Custom endpoint and paste the endpoint URL below.",
+ "Set the format to JSON and add the custom header below."
+ ]
+ }
+ },
"gate": {
"needs": "Needs {source}",
"notConnectedTitle": "Connect {source} to see this",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_bots-connect.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_bots-connect.test.tsx
new file mode 100644
index 000000000..a77ba352d
--- /dev/null
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_bots-connect.test.tsx
@@ -0,0 +1,187 @@
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { NextIntlClientProvider } from 'next-intl';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import messages from '../../../../../../messages/en.json';
+import type { BotConnections, NewBotConnection } from '@/lib/actions/ai-bots';
+
+/**
+ * The AI bots card and its dialog (AT-010, spec §6.1, §6.4): Upgrade on a
+ * plan without the report, Connect, then Manage; the customer's own cost
+ * said before connecting, and the settings with the secret shown once.
+ */
+
+const getBotConnections = vi.fn();
+const createBotConnection = vi.fn();
+const revokeBotConnection = vi.fn();
+vi.mock('@/lib/actions/ai-bots', () => ({
+ getBotConnections: (...a: unknown[]) => getBotConnections(...a),
+ createBotConnection: (...a: unknown[]) => createBotConnection(...a),
+ revokeBotConnection: (...a: unknown[]) => revokeBotConnection(...a),
+}));
+vi.mock('@/i18n/navigation', () => ({
+ Link: ({ href, ...props }: { href: string }) => <a href={href} {...props} />,
+}));
+const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() }));
+vi.mock('sonner', () => ({ toast }));
+
+const { BotsSource } = await import('./_bots-connect');
+
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT =
+ true;
+
+const info = (over: Partial<BotConnections> = {}): BotConnections => ({
+ connections: [],
+ usage: { month: '2026-10-01', stored: 0, counted: 0, cappedAt: null, cap: 2_000_000 },
+ ...over,
+});
+const hoursAgo = (h: number) => new Date(Date.now() - h * 3_600_000).toISOString();
+
+let container: HTMLDivElement;
+let root: Root;
+
+beforeEach(() => {
+ getBotConnections.mockReset().mockResolvedValue(info());
+ createBotConnection.mockReset();
+ revokeBotConnection.mockReset().mockResolvedValue({ ok: true });
+ toast.success.mockReset();
+ toast.error.mockReset();
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+});
+
+afterEach(() => {
+ act(() => root.unmount());
+ container.remove();
+ document.body.innerHTML = '';
+});
+
+async function render(canConnect = true) {
+ await act(async () => {
+ root.render(
+ <NextIntlClientProvider locale="en" messages={messages} timeZone="UTC">
+ <BotsSource brandId="b1" canConnect={canConnect} />
+ </NextIntlClientProvider>,
+ );
+ });
+}
+
+const card = () => container.textContent ?? '';
+const page = () => document.body.textContent ?? '';
+const button = (label: string) =>
+ [...document.querySelectorAll('button')].find((b) => b.textContent?.includes(label));
+
+describe('BotsSource', () => {
+ it('offers Upgrade on a plan without the bot report', async () => {
+ getBotConnections.mockResolvedValue(info({ usage: { ...info().usage, cap: 0 } }));
+ await render();
+ expect(card()).toContain('Cloudflare / Vercel · Growth+');
+ expect(container.querySelector('a')?.getAttribute('href')).toBe(
+ '/dashboard/settings?tab=billing',
+ );
+ });
+
+ it("says what each way costs on the customer's bill, then shows the settings once", async () => {
+ await render();
+ expect(card()).toContain('see which AI bots read your pages');
+ await act(async () => button('Connect')!.click());
+ expect(page()).toContain('free up to 100,000 requests a day');
+ expect(page()).toContain('$0.50 per GB of drained data');
+ expect(page()).toContain('avoids the per-request Worker cost');
+
+ const created: NewBotConnection = {
+ connection: { id: 'c1', kind: 'vercel_drain', createdAt: 't', lastEventAt: null },
+ secret: 'abot_s3cret',
+ endpoint: 'https://api.example/bots/ingest',
+ workerScript: null,
+ logpushJob: null,
+ vercelDrain: {
+ endpoint: 'https://api.example/bots/ingest',
+ format: 'json',
+ headers: ['Authorization: Bearer abot_s3cret'],
+ sources: ['static', 'lambda'],
+ },
+ };
+ createBotConnection.mockResolvedValue(created);
+ const vercel = [...document.querySelectorAll('[role="radio"]')].find((r) =>
+ r.textContent?.includes('Vercel Drain'),
+ ) as HTMLElement;
+ await act(async () => vercel.click());
+ await act(async () => button('Create connection')!.click());
+ expect(createBotConnection).toHaveBeenCalledWith('b1', 'vercel_drain');
+ expect(page()).toContain("the secret in them isn't shown again");
+ expect(page()).toContain('Authorization: Bearer abot_s3cret');
+ expect(page()).toContain('Format: JSON. Log sources: static, lambda.');
+ expect(getBotConnections).toHaveBeenCalledTimes(2);
+ });
+
+ it('shows a Logpush job to copy: the destination and the settings', async () => {
+ await render();
+ await act(async () => button('Connect')!.click());
+ createBotConnection.mockResolvedValue({
+ connection: { id: 'c3', kind: 'cloudflare_logpush', createdAt: 't', lastEventAt: null },
+ secret: 'abot_s3cret',
+ endpoint: 'https://api.example/bots/ingest',
+ workerScript: null,
+ logpushJob: {
+ dataset: 'http_requests',
+ destination_conf:
+ 'https://api.example/bots/ingest?header_Authorization=Bearer%20abot_s3cret',
+ },
+ vercelDrain: null,
+ } satisfies NewBotConnection);
+ const logpush = [...document.querySelectorAll('[role="radio"]')].find((r) =>
+ r.textContent?.includes('Cloudflare Logpush'),
+ ) as HTMLElement;
+ await act(async () => logpush.click());
+ await act(async () => button('Create connection')!.click());
+ expect(page()).toContain('Job settings (Cloudflare API: POST /zones/ZONE_ID/logpush/jobs)');
+ expect(page()).toContain('header_Authorization=Bearer%20abot_s3cret');
+ expect(page()).toContain('"dataset": "http_requests"');
+ expect(page()).not.toContain('traffic.bots.');
+ });
+
+ it('manages connected senders: last request, use this month, revoke', async () => {
+ getBotConnections.mockResolvedValue(
+ info({
+ connections: [
+ { id: 'c1', kind: 'cloudflare_worker', createdAt: 't', lastEventAt: hoursAgo(1) },
+ ],
+ usage: {
+ month: '2026-10-01',
+ stored: 2_000_000,
+ counted: 2_400_000,
+ cappedAt: '2026-10-25T00:00:00Z',
+ cap: 2_000_000,
+ },
+ }),
+ );
+ await render();
+ expect(card()).toContain('Cloudflare / Vercel · last event 1h ago');
+ await act(async () => button('Manage')!.click());
+ expect(page()).toContain('This month: 2,000,000 of 2,000,000 detailed rows.');
+ expect(page()).toContain('Detailed rows paused until Nov 1, totals still counted.');
+
+ await act(async () => button('Revoke')!.click());
+ await act(async () => button('Revoke: stop reading it')!.click());
+ expect(revokeBotConnection).toHaveBeenCalledWith('b1', 'c1');
+ expect(toast.success).toHaveBeenCalled();
+ });
+
+ it('waits for the first request, and keeps members from connecting', async () => {
+ getBotConnections.mockResolvedValue(
+ info({
+ connections: [{ id: 'c1', kind: 'cloudflare_logpush', createdAt: 't', lastEventAt: null }],
+ }),
+ );
+ await render();
+ expect(card()).toContain('Connected · waiting for the first AI-bot request');
+
+ act(() => root.unmount());
+ root = createRoot(container);
+ getBotConnections.mockResolvedValue(info());
+ await render(false);
+ expect(button('Connect')!.disabled).toBe(true);
+ });
+});
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_bots-connect.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_bots-connect.tsx
new file mode 100644
index 000000000..d2e1de3f5
--- /dev/null
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_bots-connect.tsx
@@ -0,0 +1,415 @@
+'use client';
+
+import { useCallback, useEffect, useState } from 'react';
+import { useTranslations } from 'next-intl';
+import { toast } from 'sonner';
+import { Check, Copy, Loader2, TriangleAlert } from 'lucide-react';
+import { Link } from '@/i18n/navigation';
+import { Button } from '@/components/ui/button';
+import { buttonVariants } from '@/components/ui/button-variants';
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle,
+} from '@/components/ui/dialog';
+import { cn } from '@/lib/utils';
+import { formatNumber } from '@/lib/format';
+import { formatRelative } from '@/lib/format-relative';
+import { botsCardState, lastBotEvent, rowsPausedUntil } from '@/lib/ai-bots';
+import {
+ createBotConnection,
+ getBotConnections,
+ revokeBotConnection,
+ type BotConnectionKind,
+ type BotConnections,
+ type NewBotConnection,
+} from '@/lib/actions/ai-bots';
+import { SourceCard } from './_source-card';
+
+const KINDS: BotConnectionKind[] = ['cloudflare_worker', 'cloudflare_logpush', 'vercel_drain'];
+
+/** Where Upgrade goes, as on the Prompts page's usage cards. */
+const PLAN_PAGE = '/dashboard/settings?tab=billing';
+
+/** A value to copy, in a box that scrolls rather than overflowing the dialog. */
+function CopyBlock({
+ label,
+ value,
+ code = false,
+}: {
+ label: string;
+ value: string;
+ code?: boolean;
+}) {
+ const t = useTranslations('traffic.bots');
+ const [copied, setCopied] = useState(false);
+ const copy = () => {
+ void navigator.clipboard.writeText(value);
+ setCopied(true);
+ setTimeout(() => setCopied(false), 2000);
+ };
+ return (
+ <div className="min-w-0 space-y-1">
+ <div className="flex items-center justify-between gap-2">
+ <p className="min-w-0 break-words text-xs font-medium">{label}</p>
+ <Button variant="ghost" size="sm" className="h-6 gap-1 px-2 text-xs" onClick={copy}>
+ {copied ? <Check className="h-3 w-3 text-green-500" /> : <Copy className="h-3 w-3" />}
+ {copied ? t('copied') : t('copy')}
+ </Button>
+ </div>
+ <pre
+ className={cn(
+ 'max-h-56 overflow-auto rounded-md bg-muted/50 px-3 py-2 font-mono text-[11px]',
+ code ? 'whitespace-pre' : 'whitespace-pre-wrap break-all',
+ )}
+ >
+ {value}
+ </pre>
+ </div>
+ );
+}
+
+/** What to do in Cloudflare or Vercel with the new connection's secret, shown once. */
+function SetupSteps({ created }: { created: NewBotConnection }) {
+ const t = useTranslations('traffic.bots');
+ const kind = created.connection.kind;
+ return (
+ <div className="space-y-3">
+ <p className="flex items-start gap-2 rounded-md border border-amber-500/30 bg-amber-500/10 px-3 py-2 text-xs text-amber-800 dark:text-amber-300">
+ <TriangleAlert className="mt-0.5 h-3.5 w-3.5 shrink-0" />
+ {t('secretOnce')}
+ </p>
+ <ol className="list-decimal space-y-1 pl-5 text-xs text-muted-foreground">
+ {(t.raw(`steps.${kind}`) as string[]).map((step) => (
+ <li key={step}>{step}</li>
+ ))}
+ </ol>
+ {kind === 'cloudflare_worker' && created.workerScript && (
+ <CopyBlock label={t('copyWorker')} value={created.workerScript} code />
+ )}
+ {kind === 'cloudflare_logpush' && created.logpushJob && (
+ <>
+ <CopyBlock
+ label={t('copyDestination')}
+ value={String(created.logpushJob.destination_conf)}
+ />
+ <CopyBlock
+ label={t('copyLogpushJob')}
+ value={JSON.stringify(created.logpushJob, null, 2)}
+ code
+ />
+ </>
+ )}
+ {kind === 'vercel_drain' && created.vercelDrain && (
+ <>
+ <CopyBlock label={t('copyEndpoint')} value={created.vercelDrain.endpoint} />
+ <CopyBlock label={t('copyHeader')} value={created.vercelDrain.headers.join('\n')} />
+ <p className="text-xs text-muted-foreground">
+ {t('vercelFormat', { sources: created.vercelDrain.sources.join(', ') })}
+ </p>
+ </>
+ )}
+ </div>
+ );
+}
+
+/**
+ * Connect or manage the brand's AI-bot senders (AT-010, spec §6.1, §6.4):
+ * pick a Cloudflare Worker, a Logpush job or a Vercel drain, with what each
+ * costs on the customer's own bill said before connecting; then copy the
+ * settings, with the secret in them once. Existing connections can be
+ * revoked; the month's use shows, and when the cap paused detailed rows.
+ */
+export function BotsDialog({
+ brandId,
+ open,
+ info,
+ canConnect,
+ onOpenChange,
+ onChanged,
+}: {
+ brandId: string;
+ open: boolean;
+ info: BotConnections;
+ canConnect: boolean;
+ onOpenChange: (open: boolean) => void;
+ /** A connection was made or revoked: read them again. */
+ onChanged: () => void;
+}) {
+ const t = useTranslations('traffic.bots');
+ const tCommon = useTranslations('common');
+ const [kind, setKind] = useState<BotConnectionKind>('cloudflare_worker');
+ const [creating, setCreating] = useState(false);
+ const [created, setCreated] = useState<NewBotConnection | null>(null);
+ const [revoking, setRevoking] = useState<string | null>(null);
+ const [confirming, setConfirming] = useState<string | null>(null);
+
+ const close = (next: boolean) => {
+ if (!next) {
+ setCreated(null);
+ setConfirming(null);
+ }
+ onOpenChange(next);
+ };
+
+ const create = async () => {
+ setCreating(true);
+ const result = await createBotConnection(brandId, kind);
+ setCreating(false);
+ if ('error' in result) {
+ toast.error(result.code === 'plan_limit' ? t('planLimit') : result.error);
+ return;
+ }
+ setCreated(result);
+ onChanged();
+ };
+
+ const revoke = async (id: string) => {
+ setRevoking(id);
+ const result = await revokeBotConnection(brandId, id);
+ setRevoking(null);
+ setConfirming(null);
+ if ('error' in result) {
+ toast.error(result.error);
+ return;
+ }
+ toast.success(t('revoked'));
+ onChanged();
+ };
+
+ const { usage, connections } = info;
+ const pausedUntil = rowsPausedUntil(usage);
+
+ return (
+ <Dialog open={open} onOpenChange={close}>
+ <DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-2xl">
+ <DialogHeader>
+ <DialogTitle>{t('title')}</DialogTitle>
+ <DialogDescription>{t('description')}</DialogDescription>
+ </DialogHeader>
+
+ {/* min-w-0: the dialog is a grid, and a long code line would widen its cell. */}
+ {created ? (
+ <div className="min-w-0 space-y-4">
+ <SetupSteps created={created} />
+ <div className="flex justify-end">
+ <Button size="sm" onClick={() => close(false)}>
+ {t('done')}
+ </Button>
+ </div>
+ </div>
+ ) : (
+ <div className="min-w-0 space-y-4">
+ {connections.length > 0 && (
+ <section className="space-y-2">
+ <h3 className="text-xs font-semibold">{t('connected')}</h3>
+ <ul className="divide-y rounded-md border">
+ {connections.map((c) => (
+ <li key={c.id} className="flex items-center gap-3 px-3 py-2 text-xs">
+ <div className="min-w-0 flex-1">
+ <p className="font-medium">{t(`kinds.${c.kind}.name`)}</p>
+ <p className="text-muted-foreground">
+ {c.lastEventAt
+ ? t('lastEvent', { when: formatRelative(c.lastEventAt, tCommon) })
+ : t('waiting')}
+ </p>
+ </div>
+ {canConnect &&
+ (confirming === c.id ? (
+ <span className="flex items-center gap-1">
+ <Button
+ size="sm"
+ variant="destructive"
+ className="h-7 gap-1.5 text-xs"
+ disabled={revoking === c.id}
+ onClick={() => void revoke(c.id)}
+ >
+ {revoking === c.id && <Loader2 className="h-3 w-3 animate-spin" />}
+ {t('revokeConfirm')}
+ </Button>
+ <Button
+ size="sm"
+ variant="ghost"
+ className="h-7 text-xs"
+ onClick={() => setConfirming(null)}
+ >
+ {t('cancel')}
+ </Button>
+ </span>
+ ) : (
+ <Button
+ size="sm"
+ variant="outline"
+ className="h-7 text-xs"
+ onClick={() => setConfirming(c.id)}
+ >
+ {t('revoke')}
+ </Button>
+ ))}
+ </li>
+ ))}
+ </ul>
+ <p className="text-xs text-muted-foreground">
+ {usage.cap < 0
+ ? t('usageNoCap', { stored: formatNumber(usage.stored), count: usage.stored })
+ : t('usage', {
+ stored: formatNumber(usage.stored),
+ cap: formatNumber(usage.cap),
+ })}
+ {pausedUntil &&
+ ` ${t('paused', { day: new Date(`${pausedUntil}T00:00:00Z`).toLocaleDateString('en-US', { month: 'short', day: 'numeric', timeZone: 'UTC' }) })}`}
+ </p>
+ </section>
+ )}
+
+ <section className="space-y-2">
+ <h3 className="text-xs font-semibold">
+ {connections.length > 0 ? t('addAnother') : t('choose')}
+ </h3>
+ <div role="radiogroup" aria-label={t('choose')} className="grid gap-2">
+ {KINDS.map((k) => (
+ <button
+ key={k}
+ type="button"
+ role="radio"
+ aria-checked={kind === k}
+ onClick={() => setKind(k)}
+ className={cn(
+ 'rounded-md border px-3 py-2 text-left transition-colors',
+ kind === k ? 'border-primary bg-primary/5' : 'hover:bg-muted/50',
+ )}
+ >
+ <p className="flex items-center gap-2 text-sm font-medium">
+ {t(`kinds.${k}.name`)}
+ {k === 'cloudflare_worker' && (
+ <span className="rounded border px-1.5 text-[10px] font-medium text-muted-foreground">
+ {t('recommended')}
+ </span>
+ )}
+ </p>
+ <p className="text-xs text-muted-foreground">{t(`kinds.${k}.what`)}</p>
+ <p className="mt-1 text-xs text-muted-foreground">{t(`kinds.${k}.cost`)}</p>
+ </button>
+ ))}
+ </div>
+ </section>
+
+ <div className="flex items-center justify-end gap-2">
+ {!canConnect && <p className="text-xs text-muted-foreground">{t('notAllowed')}</p>}
+ <Button
+ size="sm"
+ className="gap-1.5"
+ disabled={!canConnect || creating}
+ onClick={() => void create()}
+ >
+ {creating && <Loader2 className="h-3 w-3 animate-spin" />}
+ {t('create')}
+ </Button>
+ </div>
+ </div>
+ )}
+ </DialogContent>
+ </Dialog>
+ );
+}
+
+/**
+ * The sources strip's AI bots card (AT-001, AT-010): locked with Upgrade on
+ * a plan without the bot report (Starter), Connect when nothing sends yet,
+ * waiting until the first request, then the last request's time; Manage
+ * opens the same dialog.
+ */
+export function BotsSource({ brandId, canConnect }: { brandId: string; canConnect: boolean }) {
+ const t = useTranslations('traffic.sources');
+ const tCommon = useTranslations('common');
+ const [info, setInfo] = useState<BotConnections | { error: string } | null>(null);
+ const [open, setOpen] = useState(false);
+
+ const load = useCallback(() => {
+ getBotConnections(brandId)
+ .then(setInfo)
+ .catch((err: Error) => setInfo({ error: err.message }));
+ }, [brandId]);
+
+ useEffect(() => {
+ load();
+ }, [load]);
+
+ const title = t('bots');
+ if (!info) return <SourceCard tone="off" title={title} line={t('botsLoading')} />;
+ if ('error' in info) return <SourceCard tone="off" title={title} line={t('botsFailed')} />;
+
+ const state = botsCardState(info);
+ const button = (label: string, primary: boolean) => (
+ <Button
+ size="sm"
+ variant={primary ? 'default' : 'outline'}
+ className="h-7 text-xs"
+ onClick={() => setOpen(true)}
+ >
+ {label}
+ </Button>
+ );
+
+ return (
+ <>
+ {state === 'locked' ? (
+ <SourceCard
+ tone="off"
+ title={title}
+ line={t('botsLocked')}
+ action={
+ <Link
+ href={PLAN_PAGE}
+ className={cn(buttonVariants({ variant: 'outline', size: 'sm' }), 'h-7 text-xs')}
+ >
+ {t('upgrade')}
+ </Link>
+ }
+ />
+ ) : state === 'off' ? (
+ <SourceCard
+ tone="off"
+ title={title}
+ line={t('botsNotConnected')}
+ action={
+ // A span carries the tooltip: a disabled button gets no hover.
+ <span title={canConnect ? undefined : t('connectNotAllowed')}>
+ <Button
+ size="sm"
+ className="h-7 text-xs"
+ disabled={!canConnect}
+ onClick={() => setOpen(true)}
+ >
+ {t('connect')}
+ </Button>
+ </span>
+ }
+ />
+ ) : (
+ <SourceCard
+ tone={state === 'on' ? 'on' : 'sync'}
+ title={title}
+ line={
+ state === 'on'
+ ? t('botsActive', { when: formatRelative(lastBotEvent(info.connections), tCommon) })
+ : t('botsWaiting')
+ }
+ action={button(t('manage'), false)}
+ />
+ )}
+ {state !== 'locked' && (
+ <BotsDialog
+ brandId={brandId}
+ open={open}
+ info={info}
+ canConnect={canConnect}
+ onOpenChange={setOpen}
+ onChanged={load}
+ />
+ )}
+ </>
+ );
+}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_source-card.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_source-card.tsx
new file mode 100644
index 000000000..1b9b19557
--- /dev/null
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_source-card.tsx
@@ -0,0 +1,38 @@
+'use client';
+
+import type { ReactNode } from 'react';
+import { cn } from '@/lib/utils';
+
+export type Tone = 'on' | 'sync' | 'off';
+
+const DOT: Record<Tone, string> = {
+ on: 'bg-emerald-500',
+ sync: 'bg-amber-500',
+ off: 'bg-muted-foreground/30',
+};
+
+/** One source: a status dot by the title, a sub-line, and its button. */
+export function SourceCard({
+ tone,
+ title,
+ line,
+ action,
+}: {
+ tone: Tone;
+ title: string;
+ line: string;
+ action?: ReactNode;
+}) {
+ return (
+ <div className="flex min-w-0 items-start gap-2.5 bg-card px-4 py-3">
+ <span className={cn('mt-1.5 h-2 w-2 shrink-0 rounded-full', DOT[tone])} aria-hidden />
+ <div className="min-w-0 flex-1">
+ <p className="truncate text-[13px] font-semibold" title={title}>
+ {title}
+ </p>
+ <p className="line-clamp-2 text-xs text-muted-foreground">{line}</p>
+ </div>
+ {action && <div className="shrink-0 self-center">{action}</div>}
+ </div>
+ );
+}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_sources-strip.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_sources-strip.test.tsx
index cca38c0de..735c43ea7 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_sources-strip.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_sources-strip.test.tsx
@@ -18,6 +18,9 @@ vi.mock('@/hooks/use-integration-connect', () => ({
vi.mock('@/i18n/navigation', () => ({
Link: ({ href, ...props }: { href: string }) => <a href={href} {...props} />,
}));
+// The AI bots card reads its connections on its own (AT-010, tested in
+// _bots-connect.test.tsx): here it stays loading.
+vi.mock('@/lib/actions/ai-bots', () => ({ getBotConnections: () => new Promise(() => {}) }));
const { SourcesStrip } = await import('./_sources-strip');
@@ -56,6 +59,7 @@ function render(sources: TrafficSources | null, { canConnect = true } = {}) {
root.render(
<NextIntlClientProvider locale="en" messages={messages} timeZone="UTC">
<SourcesStrip
+ brandId="b1"
sources={sources}
domain="acme-coffee.example.com"
hasTrackingCode
@@ -95,7 +99,7 @@ describe('SourcesStrip', () => {
line: 'acme-coffee.example.com (GSC) · synced 5h ago',
button: 'Manage',
},
- { title: 'AI bots', line: 'Cloudflare / Vercel · coming soon', button: null },
+ { title: 'AI bots', line: 'Cloudflare / Vercel', button: null },
]);
expect(container.querySelector('a')!.getAttribute('href')).toBe(
'/dashboard/settings?tab=integrations',
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_sources-strip.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_sources-strip.tsx
index fa9c78986..92abfe8ec 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/traffic/_sources-strip.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/_sources-strip.tsx
@@ -1,6 +1,5 @@
'use client';
-import type { ReactNode } from 'react';
import { useTranslations } from 'next-intl';
import { Code, Loader2 } from 'lucide-react';
import { Link } from '@/i18n/navigation';
@@ -8,6 +7,8 @@ import { Button } from '@/components/ui/button';
import { buttonVariants } from '@/components/ui/button-variants';
import { Skeleton } from '@/components/ui/skeleton';
import { cn } from '@/lib/utils';
+import { SourceCard } from './_source-card';
+import { BotsSource } from './_bots-connect';
import { formatRelative } from '@/lib/format-relative';
import { useIntegrationConnect } from '@/hooks/use-integration-connect';
import type { IntegrationProvider, SourceState } from '@/lib/actions/integrations';
@@ -19,40 +20,6 @@ import {
sourceHost,
} from '@/lib/traffic-sources';
-type Tone = 'on' | 'sync' | 'off';
-
-const DOT: Record<Tone, string> = {
- on: 'bg-emerald-500',
- sync: 'bg-amber-500',
- off: 'bg-muted-foreground/30',
-};
-
-/** One source: a status dot by the title, a sub-line, and its button. */
-function SourceCard({
- tone,
- title,
- line,
- action,
-}: {
- tone: Tone;
- title: string;
- line: string;
- action?: ReactNode;
-}) {
- return (
- <div className="flex min-w-0 items-start gap-2.5 bg-card px-4 py-3">
- <span className={cn('mt-1.5 h-2 w-2 shrink-0 rounded-full', DOT[tone])} aria-hidden />
- <div className="min-w-0 flex-1">
- <p className="truncate text-[13px] font-semibold" title={title}>
- {title}
- </p>
- <p className="line-clamp-2 text-xs text-muted-foreground">{line}</p>
- </div>
- {action && <div className="shrink-0 self-center">{action}</div>}
- </div>
- );
-}
-
/**
* Google Analytics or Google Search Console: connected with its property and
* last sync, syncing with how long the first data takes, or a Connect that
@@ -147,9 +114,10 @@ function GoogleSource({
* snippet, Google Analytics, Google Search Console and AI bots — and what's
* missing, with a way to connect it without leaving the page (fixes F8).
* The snippet's code moves behind its Code button once it sends visits
- * (F3). AI bots come with phase 3 (AT-010), so that card says so for now.
+ * (F3). AI bots come from the brand's Cloudflare or Vercel (AT-010).
*/
export function SourcesStrip({
+ brandId,
sources,
domain,
hasTrackingCode,
@@ -157,6 +125,7 @@ export function SourcesStrip({
onShowCode,
onConnected,
}: {
+ brandId: string;
/** Null while loading. */
sources: TrafficSources | null;
/** The brand's primary domain, for the GA4 property line. */
@@ -217,7 +186,7 @@ export function SourcesStrip({
canConnect={canConnect}
onConnected={onConnected}
/>
- <SourceCard tone="off" title={t('bots')} line={t('botsSoon')} />
+ <BotsSource brandId={brandId} canConnect={canConnect} />
</section>
);
}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx
index c212cb4fd..0930020dd 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx
@@ -677,6 +677,7 @@ function TrafficPageContent({ brand }: { brand: Brand }) {
{!sourcesFailed && (
<SourcesStrip
+ brandId={brand.id}
sources={sources}
domain={primaryDomain}
hasTrackingCode={Boolean(brand.trackingCode)}
diff --git a/web/src/lib/actions/ai-bots.test.ts b/web/src/lib/actions/ai-bots.test.ts
new file mode 100644
index 000000000..a56167fb3
--- /dev/null
+++ b/web/src/lib/actions/ai-bots.test.ts
@@ -0,0 +1,88 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+/** The AI bot report's connections (AT-010), read and made through the server. */
+
+let session: { access_token: string } | null = { access_token: 'tok' };
+vi.mock('@/lib/supabase/server', () => ({
+ createClient: async () => ({ auth: { getSession: async () => ({ data: { session } }) } }),
+}));
+vi.mock('@/config/api', () => ({ API_BASE_URL: 'http://api.test' }));
+
+import { createBotConnection, getBotConnections, revokeBotConnection } from './ai-bots';
+
+const fetchMock = vi.fn();
+const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status });
+
+beforeEach(() => {
+ session = { access_token: 'tok' };
+ fetchMock.mockReset();
+ vi.stubGlobal('fetch', fetchMock);
+});
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+describe('AI bot connection actions', () => {
+ it("reads the brand's connections and this month's use", async () => {
+ fetchMock.mockResolvedValue(
+ json({
+ connections: [{ id: 'c1', kind: 'vercel_drain', created_at: 't0', last_event_at: null }],
+ usage: { month: '2026-10-01', stored: '12', counted: 15, cappedAt: null, cap: 2000000 },
+ }),
+ );
+ expect(await getBotConnections('b1')).toEqual({
+ connections: [{ id: 'c1', kind: 'vercel_drain', createdAt: 't0', lastEventAt: null }],
+ usage: { month: '2026-10-01', stored: 12, counted: 15, cappedAt: null, cap: 2000000 },
+ });
+ expect(fetchMock).toHaveBeenCalledWith('http://api.test/api/bots/connections/b1', {
+ headers: { Authorization: 'Bearer tok' },
+ });
+ });
+
+ it('makes a connection, and says when the plan has no bot report', async () => {
+ fetchMock.mockResolvedValue(
+ json(
+ {
+ connection: {
+ id: 'c2',
+ kind: 'cloudflare_worker',
+ created_at: 't1',
+ last_event_at: null,
+ },
+ secret: 'abot_x',
+ endpoint: 'https://api.test/bots/ingest',
+ workerScript: '// worker',
+ logpushJob: null,
+ vercelDrain: null,
+ },
+ 201,
+ ),
+ );
+ const made = await createBotConnection('b1', 'cloudflare_worker');
+ expect(made).toMatchObject({
+ secret: 'abot_x',
+ workerScript: '// worker',
+ connection: { id: 'c2' },
+ });
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
+ method: 'POST',
+ body: JSON.stringify({ kind: 'cloudflare_worker' }),
+ });
+
+ fetchMock.mockResolvedValue(json({ error: 'plan_limit', message: 'Growth plan' }, 403));
+ expect(await createBotConnection('b1', 'cloudflare_worker')).toEqual({
+ error: 'Growth plan',
+ code: 'plan_limit',
+ });
+ });
+
+ it('revokes one, and fails without a session', async () => {
+ fetchMock.mockResolvedValue(new Response(null, { status: 204 }));
+ expect(await revokeBotConnection('b1', 'c2')).toEqual({ ok: true });
+ expect(fetchMock.mock.calls[0][0]).toBe('http://api.test/api/bots/connections/b1/c2');
+
+ session = null;
+ expect(await getBotConnections('b1')).toEqual({ error: 'Not authenticated' });
+ });
+});
diff --git a/web/src/lib/actions/ai-bots.ts b/web/src/lib/actions/ai-bots.ts
new file mode 100644
index 000000000..1177c3859
--- /dev/null
+++ b/web/src/lib/actions/ai-bots.ts
@@ -0,0 +1,139 @@
+'use server';
+
+import { createClient } from '@/lib/supabase/server';
+import { API_BASE_URL } from '@/config/api';
+
+/**
+ * The AI bot report's connections (AT-010): each way a brand sends AI-bot
+ * requests from its own Cloudflare or Vercel, read and made through the
+ * server (/api/bots/connections). The secret comes back once, with what to
+ * copy into Cloudflare or Vercel; only its hash is kept.
+ *
+ * Errors are returned, not thrown: a thrown error's message is masked in
+ * production.
+ */
+
+export type BotConnectionKind = 'cloudflare_worker' | 'cloudflare_logpush' | 'vercel_drain';
+
+export interface BotConnection {
+ id: string;
+ kind: BotConnectionKind;
+ createdAt: string;
+ /** The last request it sent; null until the first. */
+ lastEventAt: string | null;
+}
+
+export interface BotUsage {
+ /** The month's first day, "2026-10-01". */
+ month: string;
+ /** Verified rows stored this month. */
+ stored: number;
+ /** Verified requests counted this month, stored or past the cap. */
+ counted: number;
+ /** When this month's cap was reached; null while under it. */
+ cappedAt: string | null;
+ /** Rows a month: -1 no cap, 0 the report is locked (Starter). */
+ cap: number;
+}
+
+export interface BotConnections {
+ connections: BotConnection[];
+ usage: BotUsage;
+}
+
+/** What to copy into Cloudflare or Vercel, with the secret in it once. */
+export interface NewBotConnection {
+ connection: BotConnection;
+ secret: string;
+ endpoint: string;
+ workerScript: string | null;
+ logpushJob: Record<string, unknown> | null;
+ vercelDrain: { endpoint: string; format: string; headers: string[]; sources: string[] } | null;
+}
+
+type Failure = { error: string; code?: 'plan_limit' };
+
+function connectionOf(row: Record<string, unknown>): BotConnection {
+ return {
+ id: String(row.id),
+ kind: row.kind as BotConnectionKind,
+ createdAt: String(row.created_at),
+ lastEventAt: typeof row.last_event_at === 'string' ? row.last_event_at : null,
+ };
+}
+
+async function call(
+ path: string,
+ init: RequestInit = {},
+): Promise<{ ok: true; status: number; body: Record<string, unknown> } | Failure> {
+ const supabase = await createClient();
+ const {
+ data: { session },
+ } = await supabase.auth.getSession();
+ if (!session) return { error: 'Not authenticated' };
+ const res = await fetch(`${API_BASE_URL}/api/bots${path}`, {
+ ...init,
+ headers: {
+ ...(init.body ? { 'Content-Type': 'application/json' } : {}),
+ Authorization: `Bearer ${session.access_token}`,
+ },
+ });
+ const body = (await res.json().catch(() => ({}))) as Record<string, unknown>;
+ if (!res.ok) {
+ return {
+ error: String(body.message || body.error || `Server error: ${res.status}`),
+ ...(body.error === 'plan_limit' ? { code: 'plan_limit' as const } : {}),
+ };
+ }
+ return { ok: true, status: res.status, body };
+}
+
+export async function getBotConnections(brandId: string): Promise<BotConnections | Failure> {
+ const res = await call(`/connections/${encodeURIComponent(brandId)}`);
+ if ('error' in res) return res;
+ const usage = (res.body.usage ?? {}) as Record<string, unknown>;
+ const n = (v: unknown) => Number(v) || 0;
+ return {
+ connections: (Array.isArray(res.body.connections) ? res.body.connections : []).map((r) =>
+ connectionOf(r as Record<string, unknown>),
+ ),
+ usage: {
+ month: String(usage.month ?? ''),
+ stored: n(usage.stored),
+ counted: n(usage.counted),
+ cappedAt: typeof usage.cappedAt === 'string' ? usage.cappedAt : null,
+ cap: Number(usage.cap ?? 0),
+ },
+ };
+}
+
+export async function createBotConnection(
+ brandId: string,
+ kind: BotConnectionKind,
+): Promise<NewBotConnection | Failure> {
+ const res = await call(`/connections/${encodeURIComponent(brandId)}`, {
+ method: 'POST',
+ body: JSON.stringify({ kind }),
+ });
+ if ('error' in res) return res;
+ const b = res.body;
+ return {
+ connection: connectionOf((b.connection ?? {}) as Record<string, unknown>),
+ secret: String(b.secret),
+ endpoint: String(b.endpoint),
+ workerScript: typeof b.workerScript === 'string' ? b.workerScript : null,
+ logpushJob: (b.logpushJob as Record<string, unknown> | null) ?? null,
+ vercelDrain: (b.vercelDrain as NewBotConnection['vercelDrain']) ?? null,
+ };
+}
+
+export async function revokeBotConnection(
+ brandId: string,
+ id: string,
+): Promise<{ ok: true } | Failure> {
+ const res = await call(`/connections/${encodeURIComponent(brandId)}/${encodeURIComponent(id)}`, {
+ method: 'DELETE',
+ });
+ if ('error' in res) return res;
+ return { ok: true };
+}
diff --git a/web/src/lib/ai-bots.test.ts b/web/src/lib/ai-bots.test.ts
new file mode 100644
index 000000000..7c855b377
--- /dev/null
+++ b/web/src/lib/ai-bots.test.ts
@@ -0,0 +1,53 @@
+import { describe, expect, it } from 'vitest';
+import { botsCardState, lastBotEvent, rowsPausedUntil } from './ai-bots';
+import type { BotConnections, BotUsage } from '@/lib/actions/ai-bots';
+
+/** The AI bots source card's state (AT-010). */
+
+const usage = (over: Partial<BotUsage> = {}): BotUsage => ({
+ month: '2026-10-01',
+ stored: 0,
+ counted: 0,
+ cappedAt: null,
+ cap: 2_000_000,
+ ...over,
+});
+const conn = (lastEventAt: string | null, id = 'c1') => ({
+ id,
+ kind: 'cloudflare_worker' as const,
+ createdAt: '2026-10-18T09:00:00Z',
+ lastEventAt,
+});
+
+describe('botsCardState', () => {
+ it('is locked without the report, off with nothing connected, waiting, then on', () => {
+ const state = (info: BotConnections) => botsCardState(info);
+ expect(state({ connections: [conn('2026-10-19T09:00:00Z')], usage: usage({ cap: 0 }) })).toBe(
+ 'locked',
+ );
+ expect(state({ connections: [], usage: usage() })).toBe('off');
+ expect(state({ connections: [conn(null)], usage: usage() })).toBe('waiting');
+ expect(
+ state({ connections: [conn(null), conn('2026-10-19T09:00:00Z', 'c2')], usage: usage() }),
+ ).toBe('on');
+ });
+});
+
+describe('lastBotEvent and rowsPausedUntil', () => {
+ it('reads the newest request and the day detailed rows start again', () => {
+ expect(
+ lastBotEvent([
+ conn('2026-10-18T09:00:00Z'),
+ conn('2026-10-19T08:00:00Z', 'c2'),
+ conn(null, 'c3'),
+ ]),
+ ).toBe('2026-10-19T08:00:00Z');
+ expect(lastBotEvent([])).toBeNull();
+ expect(rowsPausedUntil(usage({ cappedAt: '2026-10-20T00:00:00Z' }))).toBe('2026-11-01');
+ expect(rowsPausedUntil(usage({ month: '2026-12-01', cappedAt: '2026-12-20T00:00:00Z' }))).toBe(
+ '2027-01-01',
+ );
+ expect(rowsPausedUntil(usage())).toBeNull();
+ expect(rowsPausedUntil(usage({ cap: -1, cappedAt: '2026-10-20T00:00:00Z' }))).toBeNull();
+ });
+});
diff --git a/web/src/lib/ai-bots.ts b/web/src/lib/ai-bots.ts
new file mode 100644
index 000000000..041486f5e
--- /dev/null
+++ b/web/src/lib/ai-bots.ts
@@ -0,0 +1,39 @@
+/**
+ * The AI bots source card's state (AT-010), kept apart from the card so it is
+ * tested on its own.
+ */
+
+import type { BotConnection, BotConnections, BotUsage } from '@/lib/actions/ai-bots';
+
+/**
+ * locked — the plan has no bot report (Starter: Upgrade);
+ * off — nothing connected yet;
+ * waiting — connected, no request has arrived yet;
+ * on — requests arrive.
+ */
+export type BotsCardState = 'locked' | 'off' | 'waiting' | 'on';
+
+/** The newest request any connection sent, or null. */
+export function lastBotEvent(connections: BotConnection[]): string | null {
+ return connections.reduce<string | null>(
+ (latest, c) => (c.lastEventAt && (!latest || c.lastEventAt > latest) ? c.lastEventAt : latest),
+ null,
+ );
+}
+
+export function botsCardState({ connections, usage }: BotConnections): BotsCardState {
+ if (usage.cap === 0) return 'locked';
+ if (connections.length === 0) return 'off';
+ return lastBotEvent(connections) ? 'on' : 'waiting';
+}
+
+/**
+ * The day detailed rows start again when this month's cap was reached
+ * ("Detailed rows paused until Nov 1, totals still counted", spec §6.3);
+ * null while under the cap.
+ */
+export function rowsPausedUntil(usage: BotUsage): string | null {
+ if (!usage.cappedAt || usage.cap <= 0 || !/^\d{4}-\d{2}-01$/.test(usage.month)) return null;
+ const [y, m] = usage.month.split('-').map(Number);
+ return new Date(Date.UTC(y, m, 1)).toISOString().slice(0, 10);
+}