glitchgrab 1.64.0 → 1.65.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/dist/server.d.mts +44 -15
- package/dist/server.d.ts +44 -15
- package/dist/server.js +22 -9
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +22 -9
- package/dist/server.mjs.map +1 -1
- package/package.json +1 -1
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/server.ts","../src/runtime.ts","../src/utils.ts"],"sourcesContent":["/**\n * Server-side reporting — `import { reportServerError } from \"glitchgrab/server\"`.\n *\n * The rest of this SDK is `\"use client\"`: it hooks `window.onerror`, walks the\n * DOM for a screenshot, and needs a mounted provider for its token. None of\n * that exists in a cron job at 3am, an API route handler, or a queue worker —\n * which is precisely where the failures nobody is watching happen. A WhatsApp\n * send Meta rejects, an SMTP timeout, a nightly digest that throws: no tab is\n * open, so the browser SDK never sees them and no issue is ever filed.\n *\n * This entry has no `\"use client\"`, touches no DOM API, and imports no React.\n * It POSTs to the same `/api/v1/sdk/report` endpoint the browser uses, with\n * `source: \"SDK_AUTO\"` so the server-side dedup applies (24h per signature,\n * 7 days once an issue exists) — otherwise a cron failing hourly would open\n * twenty-four identical issues a day.\n *\n * Like the rest of the SDK it never throws: a reporting failure must not turn\n * a handled error into an unhandled one.\n */\nimport { createHmac } from \"node:crypto\";\nimport type { ReportPayload, ReportResult, ReportSeverity, ReportType } from \"./types\";\nimport { DEFAULT_BASE_URL, sendReport } from \"./utils\";\n\n/** Who to attribute the report to. A server has no signed-in user, so this is the job or service. */\nexport interface ServerReporterIdentity {\n /** Stable id — the cron name, the service name. Defaults to `\"server\"`. */\n id?: string;\n /** Display name shown on the report. Defaults to `\"Server\"`. */\n name?: string;\n email?: string;\n phone?: string;\n}\n\n/** Defaults applied to every server report. Set once at boot with {@link configureServerReporter}. */\nexport interface ServerReporterConfig {\n /** Repo token (`gg_…`). Falls back to `process.env.GLITCHGRAB_TOKEN`. */\n token?: string;\n /** Override the API host. Falls back to `process.env.GLITCHGRAB_BASE_URL`, then `https://glitchgrab.dev`. */\n baseUrl?: string;\n /**\n * Where this ran — `\"cron/attendance-prompts\"`, `\"worker/email-queue\"`.\n *\n * It is the grouping key, not a link: it is sent as the report's `pageUrl`\n * so two different jobs throwing the same `\"Timeout\"` stay two issues\n * instead of collapsing into one.\n */\n context?: string;\n /** Merged into every report's metadata. Good place for region, deployment id, service name. */\n metadata?: Record<string, string>;\n reporter?: ServerReporterIdentity;\n /**\n * Report from `NODE_ENV=development` too. Off by default, matching the\n * browser SDK — a laptop crashing its way through a refactor should not file\n * issues. Unlike the browser there is no `Origin` header for the API to\n * recognise, so nothing else stops a dev machine from filing.\n */\n enableInDevelopment?: boolean;\n}\n\n/** Per-call options. Anything set here wins over {@link configureServerReporter}. */\nexport interface ServerReportOptions extends ServerReporterConfig {\n /** Issue type. Defaults to `BUG`. */\n type?: ReportType;\n /** Rendered as a `severity:<value>` label on the GitHub issue. */\n severity?: ReportSeverity;\n /** Extra prose for the issue body — what the job was doing, which record it was on. */\n description?: string;\n /**\n * A real request URL, when there is one (an API route handler). Overrides\n * the synthetic URL built from `context`.\n */\n pageUrl?: string;\n /**\n * What the job printed before it failed — lines or one string.\n *\n * Attached to the issue as a `.log` file: the first part is previewed in the\n * issue body, the whole file is kept for download. A stack says where it\n * broke; the log lines before it are usually what says why. Only the last\n * {@link MAX_LOG_CHARS} characters are sent.\n */\n logs?: string | string[];\n}\n\n/** The newest end of a log is the useful end. Well under the API's per-file cap. */\nconst MAX_LOG_CHARS = 200_000;\n\nconst DEV_BLOCKED_MESSAGE = \"development\";\n\n/**\n * Node's `process`, as much of it as this file uses.\n *\n * `src/global.d.ts` declares a browser-shaped `process` with nothing but\n * `env`, and that is deliberate — the client bundle must not be able to reach\n * for Node APIs. So this entry reads the real one off `globalThis` behind its\n * own narrow type instead of pulling `@types/node` into the package.\n */\ninterface NodeProcessLike {\n env?: Record<string, string | undefined>;\n version?: string;\n on?(event: string, listener: (arg: never) => void): unknown;\n off?(event: string, listener: (arg: never) => void): unknown;\n listenerCount?(event: string): number;\n}\n\nfunction nodeProcess(): NodeProcessLike | undefined {\n try {\n const candidate = (globalThis as { process?: NodeProcessLike }).process;\n return candidate && typeof candidate === \"object\" ? candidate : undefined;\n } catch {\n return undefined;\n }\n}\n\nlet defaults: ServerReporterConfig = {};\n\n/**\n * Set the token and the defaults once, at module load, instead of passing them\n * to every call site. A later call's own options still win.\n */\nexport function configureServerReporter(config: ServerReporterConfig): void {\n defaults = { ...defaults, ...config };\n}\n\n/** Test-only — drop everything {@link configureServerReporter} set. */\nexport function resetServerReporter(): void {\n defaults = {};\n}\n\nfunction readEnv(name: string): string | undefined {\n try {\n return nodeProcess()?.env?.[name];\n } catch {\n return undefined;\n }\n}\n\n/**\n * Message and stack out of whatever was thrown.\n *\n * A thrown string, a rejected `{ code, message }`, an `undefined` from a\n * `Promise.reject()` — all of it reaches here, and all of it must produce a\n * non-empty message: the API's dedup signature is `null` without one, and a\n * report with no signature is a fresh issue every single time.\n */\nfunction describeError(error: unknown): { message: string; stack?: string } {\n try {\n if (error instanceof Error) {\n return {\n message: error.message || error.name || \"Unknown server error\",\n stack: error.stack,\n };\n }\n if (typeof error === \"string\") {\n return { message: error || \"Unknown server error\" };\n }\n if (error && typeof error === \"object\") {\n const maybe = error as { message?: unknown; stack?: unknown };\n const message = typeof maybe.message === \"string\" ? maybe.message : JSON.stringify(error);\n return {\n message: message || \"Unknown server error\",\n stack: typeof maybe.stack === \"string\" ? maybe.stack : undefined,\n };\n }\n return { message: String(error ?? \"\") || \"Unknown server error\" };\n } catch {\n return { message: \"Unknown server error\" };\n }\n}\n\n/**\n * `pageUrl` doubles as the dedup grouping key, so a job needs one even though\n * no page was involved. `server://` keeps it obviously synthetic — and keeps it\n * clear of the API's localhost check, which only matches http(s) and would\n * otherwise swallow the report with a 200 and create nothing.\n */\nfunction contextUrl(context: string | undefined): string | undefined {\n if (!context) return undefined;\n return `server://${context.replace(/^\\/+/, \"\")}`;\n}\n\n/**\n * `logs` as the API's text-attachment shape: `[{ name, dataUrl }]`, JSON in\n * `metadata.attachments` — the same slot the report dialog uses for files, so\n * the endpoint needs nothing new. Null when there is nothing to attach.\n */\nfunction logsAttachment(logs: string | string[] | undefined, context: string | undefined): string | null {\n try {\n const text = Array.isArray(logs) ? logs.join(\"\\n\") : logs;\n if (!text || !text.trim()) return null;\n const tail = text.length > MAX_LOG_CHARS ? text.slice(-MAX_LOG_CHARS) : text;\n\n // UTF-8 → base64 without Buffer: this entry stays free of Node typings.\n const bytes = new TextEncoder().encode(tail);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i += 0x8000) {\n binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));\n }\n const name = `${(context ?? \"server\").replace(/[^a-z0-9._-]+/gi, \"-\").replace(/^-+|-+$/g, \"\") || \"server\"}.log`;\n return JSON.stringify([{ name, dataUrl: `data:text/plain;base64,${btoa(binary)}` }]);\n } catch {\n return null;\n }\n}\n\nfunction runtimeMetadata(): Record<string, string> {\n try {\n const proc = nodeProcess();\n if (!proc) return { runtime: \"server\" };\n const region = proc.env?.VERCEL_REGION;\n const deploymentEnv = proc.env?.VERCEL_ENV;\n return {\n runtime: \"node\",\n ...(proc.version ? { nodeVersion: proc.version } : {}),\n ...(region ? { region } : {}),\n ...(deploymentEnv ? { deploymentEnv } : {}),\n };\n } catch {\n return { runtime: \"server\" };\n }\n}\n\n/**\n * File a server-side error as a GitHub issue.\n *\n * Await it. On a serverless platform the function may be frozen the moment your\n * handler returns, and a floating promise dies with it — the report never\n * leaves the machine.\n *\n * ```ts\n * try {\n * await sendDigest();\n * } catch (error) {\n * await reportServerError(error, { context: \"cron/daily-digest\" });\n * throw error;\n * }\n * ```\n *\n * Returns `null` when nothing was filed — no token, development mode, or the\n * API refused it. Never throws.\n */\nexport async function reportServerError(\n error: unknown,\n options: ServerReportOptions = {}\n): Promise<ReportResult | null> {\n try {\n const token = options.token ?? defaults.token ?? readEnv(\"GLITCHGRAB_TOKEN\");\n if (!token) return null;\n\n const enableInDevelopment = options.enableInDevelopment ?? defaults.enableInDevelopment ?? false;\n if (!enableInDevelopment && readEnv(\"NODE_ENV\") === DEV_BLOCKED_MESSAGE) return null;\n\n const { message, stack } = describeError(error);\n const context = options.context ?? defaults.context;\n const reporter = { ...defaults.reporter, ...options.reporter };\n\n const payload: ReportPayload = {\n token,\n source: \"SDK_AUTO\",\n type: options.type ?? \"BUG\",\n errorMessage: message,\n ...(stack ? { errorStack: stack } : {}),\n ...(options.description ? { description: options.description } : {}),\n ...(options.pageUrl ?? contextUrl(context)\n ? { pageUrl: options.pageUrl ?? contextUrl(context) }\n : {}),\n metadata: {\n ...runtimeMetadata(),\n ...defaults.metadata,\n ...options.metadata,\n ...(context ? { serverContext: context } : {}),\n ...(options.severity ? { severity: options.severity } : {}),\n sessionUserId: reporter.id ?? context ?? \"server\",\n sessionUserName: reporter.name ?? \"Server\",\n ...(reporter.email ? { sessionUserEmail: reporter.email } : {}),\n ...(reporter.phone ? { sessionUserPhone: reporter.phone } : {}),\n },\n };\n const attachments = logsAttachment(options.logs, context);\n if (attachments && payload.metadata) payload.metadata.attachments = attachments;\n\n return await sendReport(payload, options.baseUrl ?? defaults.baseUrl ?? readEnv(\"GLITCHGRAB_BASE_URL\"));\n } catch {\n return null;\n }\n}\n\n/** Options for {@link captureServerErrors}. */\nexport interface CaptureServerErrorsOptions extends ServerReportOptions {\n /**\n * Also report rejected promises nobody handled. Default `true`.\n *\n * Node crashes the process on an unhandled rejection — but only while no\n * `unhandledRejection` listener exists. Attaching one silently converts a\n * crash into a warning, so this re-throws afterwards to put the default\n * behaviour back, unless the host app has its own listener (then it is theirs\n * to decide).\n */\n captureUnhandledRejections?: boolean;\n}\n\n/**\n * Report every uncaught exception and unhandled rejection in this process.\n *\n * Call it once at boot — `instrumentation.ts` in a Next.js app. Returns a\n * function that removes the listeners again.\n *\n * Uncaught exceptions use `uncaughtExceptionMonitor`, which observes without\n * taking over: the process still crashes exactly as it would have. A reporter\n * that keeps a broken process alive is worse than no reporter.\n */\nexport function captureServerErrors(options: CaptureServerErrorsOptions = {}): () => void {\n try {\n const proc = nodeProcess();\n if (!proc || typeof proc.on !== \"function\") return () => {};\n\n const onException = (error: unknown) => {\n void reportServerError(error, { ...options, context: options.context ?? \"uncaughtException\" });\n };\n\n const onRejection = (reason: unknown) => {\n void reportServerError(reason, {\n ...options,\n context: options.context ?? \"unhandledRejection\",\n });\n\n // Ours is the only listener, so suppressing the crash was never the\n // host app's choice. Hand the default behaviour back.\n if (proc.listenerCount?.(\"unhandledRejection\") === 1) {\n setTimeout(() => {\n throw reason;\n }, 0);\n }\n };\n\n proc.on(\"uncaughtExceptionMonitor\", onException as (arg: never) => void);\n\n const captureRejections = options.captureUnhandledRejections ?? true;\n if (captureRejections) proc.on(\"unhandledRejection\", onRejection as (arg: never) => void);\n\n return () => {\n try {\n proc.off?.(\"uncaughtExceptionMonitor\", onException as (arg: never) => void);\n if (captureRejections) {\n proc.off?.(\"unhandledRejection\", onRejection as (arg: never) => void);\n }\n } catch {\n // Nothing to undo.\n }\n };\n } catch {\n return () => {};\n }\n}\n\n/** Options for {@link signSession}. */\nexport interface SignSessionOptions {\n /** Your user's id — the same value you pass as `session.userId`. */\n userId: string;\n /** The repo's signing secret (`ggs_…`). Falls back to `process.env.GLITCHGRAB_SIGNING_SECRET`. */\n secret?: string;\n /** How long the signature stays valid, in seconds. Default one day. */\n ttlSeconds?: number;\n /** Clock override in epoch ms. For tests. */\n now?: number;\n}\n\n/**\n * Vouch for the logged-in user so the SDK's AI features work when the project\n * owner turned on signed users.\n *\n * The `gg_` token ships in your JavaScript, so on its own it proves nothing\n * about who is calling. Your server holds the signing secret; call this where\n * you build the `session` prop and pass the result as `session.signature`.\n * Render it per request so it refreshes before `ttlSeconds` runs out.\n *\n * Format: `v1.<exp>.<base64url HMAC-SHA256(secret, \"v1.<exp>.<userId>\")>`.\n *\n * Unlike the rest of this SDK it throws: it runs in your own server code, where\n * a missing secret is a deploy mistake you want to see — not a user who quietly\n * loses the AI features.\n */\nexport function signSession(options: SignSessionOptions): string {\n const userId = options?.userId;\n if (typeof userId !== \"string\" || userId.length === 0) {\n throw new Error(\"glitchgrab signSession: userId is required\");\n }\n const env = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env;\n const secret = options.secret || env?.GLITCHGRAB_SIGNING_SECRET;\n if (!secret) {\n throw new Error(\n \"glitchgrab signSession: no signing secret — pass `secret` or set GLITCHGRAB_SIGNING_SECRET\"\n );\n }\n const ttl = options.ttlSeconds ?? 86_400;\n if (!Number.isFinite(ttl) || ttl <= 0) {\n throw new Error(\"glitchgrab signSession: ttlSeconds must be a positive number\");\n }\n const exp = Math.floor((options.now ?? Date.now()) / 1000) + Math.floor(ttl);\n const sig = createHmac(\"sha256\", secret).update(`v1.${exp}.${userId}`).digest(\"base64url\");\n return `v1.${exp}.${sig}`;\n}\n\n// ── Guides ───────────────────────────────────────────────────────────────────\n//\n// A team writes its how-tos in Glitchgrab and renders them on its own site from\n// `GET /api/v1/sdk/guides` (#370). Every site needs the same client — token,\n// caching, never failing the page, shape checks — so it lives here instead of\n// being rewritten, slightly differently, in each product (#409).\n\n/** One guide as the list returns it — no body. Dates are ISO strings: this came over JSON. */\nexport interface GuideSummary {\n slug: string;\n title: string;\n /** One line saying what the guide answers. */\n summary: string;\n category: string | null;\n /** The order set in Glitchgrab. The list already arrives sorted by it. */\n orderIndex: number;\n /** Always `\"PUBLISHED\"` with a `gg_` token; only the project's `ggc_` server key sees drafts. */\n status: \"DRAFT\" | \"PUBLISHED\";\n /**\n * `\"INTERNAL\"` = a team-only guide. Only the `ggc_` server key sees those,\n * and only when it asks with `audience: \"internal\"` or `\"all\"`.\n */\n audience: \"PUBLIC\" | \"INTERNAL\";\n /** When someone last re-checked the steps against the product. */\n lastVerifiedAt: string | null;\n updatedAt: string;\n /**\n * Where this guide lives on your site: its own link, else\n * `<guides base URL>/<slug>`, else null when the repo sets neither.\n */\n link: string | null;\n}\n\n/** One guide with its body. */\nexport interface Guide extends GuideSummary {\n /** The markdown as written. */\n content: string;\n /**\n * `content` rendered by Glitchgrab: raw HTML in the markdown arrives as text,\n * only allowlisted attributes survive, and links keep only http(s), mailto\n * and relative targets. Headings carry `id=\"guide-<heading text>\"`, for a\n * table of contents or a link to a single step.\n *\n * Safe to render as-is. On a domain that holds your users' sessions, a second\n * sanitize pass of your own is cheap defence in depth — this SDK ships no\n * sanitizer so it stays dependency-free.\n */\n html: string;\n}\n\n/** Options for {@link listGuides} and {@link getGuide}. */\nexport interface GuideReadOptions {\n /**\n * Repo token (`gg_…`). Falls back to {@link configureServerReporter}'s token,\n * then `GLITCHGRAB_TOKEN`, then `NEXT_PUBLIC_GLITCHGRAB_TOKEN` — the token is\n * public, so the one your provider already uses is fine.\n *\n * Never pass the `ggc_` server key for a public page: it reads drafts too, and client calls.\n */\n token?: string;\n /** Override the API host. Falls back to `GLITCHGRAB_BASE_URL`, then `https://glitchgrab.dev`. */\n baseUrl?: string;\n /**\n * Seconds Next.js may cache the response. Default 300; `0` reads fresh on\n * every request. The route is `force-dynamic`, so without this nothing is\n * cached at all. Ignored outside Next.js.\n */\n revalidate?: number;\n /** Give up and return empty after this many ms. Default 5000 — a slow Glitchgrab must not hang your render. */\n timeoutMs?: number;\n /**\n * Which guides: `\"public\"` (default), `\"internal\"` (team-only) or `\"all\"`.\n * Anything but `\"public\"` needs the `ggc_` server key — for your own admin\n * panel, behind your own login. Never on a page a customer can open.\n */\n audience?: \"public\" | \"internal\" | \"all\";\n}\n\nconst GUIDES_REVALIDATE_SECONDS = 300;\nconst SDK_API_TIMEOUT_MS = 5000;\n\n/** `RequestInit` plus Next.js's cache option, without pulling Next's types into this entry. */\ntype NextFetchInit = RequestInit & { next?: { revalidate?: number } };\n\ninterface SdkApiResult {\n /** `data` from a successful response, else null. */\n data: unknown;\n /** Null on success; the route's own `error` when it sent one, so a caller can show why. */\n error: string | null;\n /** The route's machine-readable `code`, when it sent one (e.g. ring-number errors, #654). */\n code?: string;\n}\n\n/**\n * One request to a Glitchgrab SDK route. Never throws: a refusal, a timeout, a\n * network failure and unreadable JSON all come back as `error`.\n */\nasync function callSdkApi(\n path: string,\n token: string,\n options: { baseUrl?: string; timeoutMs?: number },\n init: { method?: \"GET\" | \"POST\" | \"PUT\" | \"PATCH\" | \"DELETE\"; body?: unknown; cache: { revalidate: number } | \"no-store\" }\n): Promise<SdkApiResult> {\n const baseUrl = (\n options.baseUrl ??\n defaults.baseUrl ??\n readEnv(\"GLITCHGRAB_BASE_URL\") ??\n DEFAULT_BASE_URL\n ).replace(/\\/+$/, \"\");\n\n const controller = typeof AbortController === \"function\" ? new AbortController() : undefined;\n const timer = controller\n ? setTimeout(() => controller.abort(), options.timeoutMs ?? SDK_API_TIMEOUT_MS)\n : undefined;\n\n try {\n // A FormData body (KYC certificates) sets its own multipart boundary.\n const multipart = typeof FormData !== \"undefined\" && init.body instanceof FormData;\n const request: NextFetchInit = {\n method: init.method ?? \"GET\",\n headers: {\n Authorization: `Bearer ${token}`,\n ...(init.body === undefined || multipart ? {} : { \"Content-Type\": \"application/json\" }),\n },\n ...(init.body === undefined\n ? {}\n : { body: multipart ? (init.body as FormData) : JSON.stringify(init.body) }),\n ...(init.cache === \"no-store\" ? { cache: \"no-store\" as const } : { next: { revalidate: init.cache.revalidate } }),\n ...(controller ? { signal: controller.signal } : {}),\n };\n const response = await fetch(`${baseUrl}${path}`, request);\n const body = (await response.json().catch(() => null)) as\n | { success?: unknown; data?: unknown; error?: unknown; code?: unknown }\n | null;\n if (!response.ok || body?.success !== true) {\n return {\n data: null,\n error: typeof body?.error === \"string\" ? body.error : `HTTP ${response.status}`,\n ...(typeof body?.code === \"string\" ? { code: body.code } : {}),\n };\n }\n return { data: body.data ?? null, error: null };\n } catch {\n return { data: null, error: \"Could not reach Glitchgrab\" };\n } finally {\n if (timer) clearTimeout(timer);\n }\n}\n\n/** `data` from the guides route, or null on any failure — no token, non-2xx, timeout, bad JSON. */\nasync function readGuidesApi(query: string, options: GuideReadOptions): Promise<unknown> {\n const token =\n options.token ??\n defaults.token ??\n readEnv(\"GLITCHGRAB_TOKEN\") ??\n readEnv(\"NEXT_PUBLIC_GLITCHGRAB_TOKEN\");\n if (!token) return null;\n\n const revalidate =\n typeof options.revalidate === \"number\" && Number.isFinite(options.revalidate) && options.revalidate >= 0\n ? options.revalidate\n : GUIDES_REVALIDATE_SECONDS;\n\n const result = await callSdkApi(`/api/v1/sdk/guides${query}`, token, options, { cache: { revalidate } });\n return result.data;\n}\n\n/**\n * A guide summary out of whatever the API sent, or null when it is not one.\n * Only slug, title and summary are required — a page cannot render without\n * them. The rest default, so the returned type is honest either way.\n */\nfunction readGuideSummary(value: unknown): GuideSummary | null {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const guide = value as Record<string, unknown>;\n if (\n typeof guide.slug !== \"string\" ||\n typeof guide.title !== \"string\" ||\n typeof guide.summary !== \"string\"\n ) {\n return null;\n }\n return {\n slug: guide.slug,\n title: guide.title,\n summary: guide.summary,\n category: typeof guide.category === \"string\" ? guide.category : null,\n orderIndex: typeof guide.orderIndex === \"number\" ? guide.orderIndex : 0,\n status: guide.status === \"DRAFT\" ? \"DRAFT\" : \"PUBLISHED\",\n audience: guide.audience === \"INTERNAL\" ? \"INTERNAL\" : \"PUBLIC\",\n lastVerifiedAt: typeof guide.lastVerifiedAt === \"string\" ? guide.lastVerifiedAt : null,\n updatedAt: typeof guide.updatedAt === \"string\" ? guide.updatedAt : \"\",\n link: typeof guide.link === \"string\" ? guide.link : null,\n };\n}\n\n/** `audience=` for the guides route; nothing for the default, so old servers see the same URL. */\nfunction audienceQuery(options: GuideReadOptions, joiner: \"?\" | \"&\"): string {\n const audience = options.audience;\n return audience === \"internal\" || audience === \"all\" ? `${joiner}audience=${audience}` : \"\";\n}\n\n/**\n * Every published guide for the token's repo, in the order set in Glitchgrab.\n * No bodies — this is the index page.\n *\n * ```ts\n * // app/guides/page.tsx\n * import { listGuides } from \"glitchgrab/server\";\n *\n * export default async function GuidesPage() {\n * const guides = await listGuides();\n * return guides.map((g) => <a key={g.slug} href={`/guides/${g.slug}`}>{g.title}</a>);\n * }\n * ```\n *\n * Returns `[]` on any failure. Never throws: a Glitchgrab outage leaves the\n * page empty instead of turning it into a 500.\n */\nexport async function listGuides(options: GuideReadOptions = {}): Promise<GuideSummary[]> {\n try {\n const data = await readGuidesApi(audienceQuery(options, \"?\"), options);\n if (!Array.isArray(data)) return [];\n return data.map(readGuideSummary).filter((guide): guide is GuideSummary => guide !== null);\n } catch {\n return [];\n }\n}\n\n/**\n * One published guide with its markdown and rendered HTML.\n *\n * Returns `null` when there is no such guide and on any failure — call\n * `notFound()` on it. Never throws.\n */\nexport async function getGuide(slug: string, options: GuideReadOptions = {}): Promise<Guide | null> {\n try {\n const wanted = typeof slug === \"string\" ? slug.trim() : \"\";\n if (!wanted) return null;\n\n const data = await readGuidesApi(`?slug=${encodeURIComponent(wanted)}${audienceQuery(options, \"&\")}`, options);\n const summary = readGuideSummary(data);\n const body = data as { content?: unknown; html?: unknown } | null;\n if (!summary || typeof body?.html !== \"string\") return null;\n\n return {\n ...summary,\n content: typeof body.content === \"string\" ? body.content : \"\",\n html: body.html,\n };\n } catch {\n return null;\n }\n}\n\n// ── Calls ────────────────────────────────────────────────────────────────────\n//\n// Recorded client calls a project shared with its client (#416), for the team's\n// own admin panel. Unlike guides these are private: they need the project's\n// server key (`ggc_…`, API Tokens → Server keys), which lives on the server and never in a bundle, and\n// every response holds links that expire — so nothing here is cached.\n\n/** Someone on the call, as the recorder saw them. */\nexport interface CallParticipant {\n name: string | null;\n email: string | null;\n}\n\n/** One shared call as the list returns it — no transcript, no links. */\nexport interface CallListItem {\n id: string;\n title: string | null;\n startsAt: string | null;\n endsAt: string | null;\n durationSec: number | null;\n /** `\"bot\"` (joined the Meet) or `\"extension\"` (recorded from someone's Chrome). */\n recorder: string | null;\n /** Match a call to your customer by these emails. */\n participants: CallParticipant[];\n hasRecording: boolean;\n /** `IDLE`, `RUNNING`, `DONE` or `FAILED`. */\n transcriptStatus: string;\n /** One line on what the call was about, once Glitchgrab has read it. */\n overview: string | null;\n createdAt: string;\n}\n\nexport interface CallAudio {\n /**\n * `call` — a bot recording's single mixed track. An extension recording has\n * two instead: `client` (everyone else on the call) and `recorder` (the\n * person who recorded it).\n */\n track: \"call\" | \"client\" | \"recorder\";\n /** Plays for {@link Call.linksExpireInSeconds}. */\n url: string;\n}\n\nexport interface CallMoment {\n /** Milliseconds into the call — seek the audio here. */\n tMs: number | null;\n speaker: string | null;\n text: string;\n}\n\n/** One shared call in full. */\nexport interface Call extends CallListItem {\n meetUrl: string | null;\n audio: CallAudio[];\n /** Audio and screenshot links stop working after this many seconds. Read the call per render; never store them. */\n linksExpireInSeconds: number;\n transcript: string | null;\n summary: { overview: string; moments: CallMoment[] } | null;\n /** Screenshots grabbed during the call, a page at a time. `tMs` is on the transcript's clock. */\n frames: { total: number; offset: number; limit: number; items: { tMs: number; url: string }[] };\n}\n\n/** Options for {@link listCalls}, {@link getCall}, {@link deleteCall} and the booking settings calls. */\nexport interface CallsOptions {\n /**\n * The project's server key (`ggc_…`, API Tokens → Server keys). Falls back to `GLITCHGRAB_CALLS_KEY`.\n * Server only — never a `NEXT_PUBLIC_` variable: it plays and deletes calls.\n */\n key?: string;\n /** Override the API host. Falls back to `GLITCHGRAB_BASE_URL`, then `https://glitchgrab.dev`. */\n baseUrl?: string;\n /** Give up after this many ms. Default 5000. */\n timeoutMs?: number;\n}\n\nexport interface ListCallsOptions extends CallsOptions {\n /** Default 20, max 100. */\n limit?: number;\n /** ISO date: only calls recorded before it. Pass the last call's `createdAt` for the next page. */\n before?: string;\n}\n\nexport interface GetCallOptions extends CallsOptions {\n /** First screenshot to return. Default 0. */\n framesOffset?: number;\n /** Screenshots per page. Default 50, max 200, `0` for none. */\n framesLimit?: number;\n}\n\nexport type DeleteCallResult = { deleted: true } | { deleted: false; error: string };\n\nconst CALLS_KEY_PREFIX = \"ggc_\";\nconst NO_KEY_ERROR = \"No server key — set GLITCHGRAB_CALLS_KEY (ggc_…)\";\n\n/** Only a `ggc_` key. A `gg_` token here would only 401 — and means the public token sits where a secret belongs. */\nfunction callsKey(options: CallsOptions): string | null {\n const key = options.key ?? readEnv(\"GLITCHGRAB_CALLS_KEY\");\n return typeof key === \"string\" && key.startsWith(CALLS_KEY_PREFIX) ? key : null;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && typeof value === \"object\" && !Array.isArray(value);\n}\n\nfunction stringOrNull(value: unknown): string | null {\n return typeof value === \"string\" ? value : null;\n}\n\nfunction numberOrNull(value: unknown): number | null {\n return typeof value === \"number\" && Number.isFinite(value) ? value : null;\n}\n\nfunction readCallListItem(value: unknown): CallListItem | null {\n if (!isRecord(value) || typeof value.id !== \"string\") return null;\n return {\n id: value.id,\n title: stringOrNull(value.title),\n startsAt: stringOrNull(value.startsAt),\n endsAt: stringOrNull(value.endsAt),\n durationSec: numberOrNull(value.durationSec),\n recorder: stringOrNull(value.recorder),\n participants: Array.isArray(value.participants)\n ? value.participants.filter(isRecord).map((p) => ({ name: stringOrNull(p.name), email: stringOrNull(p.email) }))\n : [],\n hasRecording: value.hasRecording === true,\n transcriptStatus: typeof value.transcriptStatus === \"string\" ? value.transcriptStatus : \"IDLE\",\n overview: stringOrNull(value.overview),\n createdAt: typeof value.createdAt === \"string\" ? value.createdAt : \"\",\n };\n}\n\nconst AUDIO_TRACKS: ReadonlySet<string> = new Set([\"call\", \"client\", \"recorder\"]);\n\nfunction readCall(value: unknown): Call | null {\n const base = readCallListItem(value);\n if (!base || !isRecord(value)) return null;\n\n const rawSummary = value.summary;\n const summary =\n isRecord(rawSummary) && typeof rawSummary.overview === \"string\"\n ? {\n overview: rawSummary.overview,\n moments: Array.isArray(rawSummary.moments)\n ? rawSummary.moments\n .filter(isRecord)\n .filter((m) => typeof m.text === \"string\")\n .map((m) => ({ tMs: numberOrNull(m.tMs), speaker: stringOrNull(m.speaker), text: m.text as string }))\n : [],\n }\n : null;\n\n const frames = isRecord(value.frames) ? value.frames : {};\n\n return {\n ...base,\n meetUrl: stringOrNull(value.meetUrl),\n audio: Array.isArray(value.audio)\n ? value.audio\n .filter(isRecord)\n .filter((a) => typeof a.url === \"string\" && typeof a.track === \"string\" && AUDIO_TRACKS.has(a.track))\n .map((a) => ({ track: a.track as CallAudio[\"track\"], url: a.url as string }))\n : [],\n linksExpireInSeconds: numberOrNull(value.linksExpireInSeconds) ?? 0,\n transcript: stringOrNull(value.transcript),\n summary,\n frames: {\n total: numberOrNull(frames.total) ?? 0,\n offset: numberOrNull(frames.offset) ?? 0,\n limit: numberOrNull(frames.limit) ?? 0,\n items: Array.isArray(frames.items)\n ? frames.items\n .filter(isRecord)\n .filter((f) => typeof f.url === \"string\" && typeof f.tMs === \"number\")\n .map((f) => ({ tMs: f.tMs as number, url: f.url as string }))\n : [],\n },\n };\n}\n\nfunction setNumber(query: URLSearchParams, name: string, value: unknown): void {\n if (typeof value === \"number\" && Number.isFinite(value)) query.set(name, String(Math.floor(value)));\n}\n\n/**\n * Calls shared with the client on the key's project, newest first. No links —\n * open one with {@link getCall}.\n *\n * ```ts\n * // app/admin/calls/page.tsx — server component\n * import { listCalls } from \"glitchgrab/server\";\n *\n * export default async function CallsPage() {\n * const calls = await listCalls(); // GLITCHGRAB_CALLS_KEY=ggc_…\n * return calls.map((c) => <a key={c.id} href={`/admin/calls/${c.id}`}>{c.title}</a>);\n * }\n * ```\n *\n * Returns `[]` on any failure. Never throws.\n */\nexport async function listCalls(options: ListCallsOptions = {}): Promise<CallListItem[]> {\n try {\n const key = callsKey(options);\n if (!key) return [];\n\n const query = new URLSearchParams();\n setNumber(query, \"limit\", options.limit);\n if (typeof options.before === \"string\" && options.before) query.set(\"before\", options.before);\n const qs = query.toString();\n\n const { data } = await callSdkApi(`/api/v1/sdk/calls${qs ? `?${qs}` : \"\"}`, key, options, { cache: \"no-store\" });\n if (!Array.isArray(data)) return [];\n return data.map(readCallListItem).filter((call): call is CallListItem => call !== null);\n } catch {\n return [];\n }\n}\n\n/**\n * One shared call: audio links, transcript, summary and a page of screenshots.\n * Links expire after `linksExpireInSeconds` — read the call when the page\n * renders and never store them.\n *\n * Returns `null` when the call does not exist, is not shared, and on any\n * failure — call `notFound()` on it. Never throws.\n */\nexport async function getCall(id: string, options: GetCallOptions = {}): Promise<Call | null> {\n try {\n const wanted = typeof id === \"string\" ? id.trim() : \"\";\n const key = callsKey(options);\n if (!wanted || !key) return null;\n\n const query = new URLSearchParams({ id: wanted });\n setNumber(query, \"framesOffset\", options.framesOffset);\n setNumber(query, \"framesLimit\", options.framesLimit);\n\n const { data } = await callSdkApi(`/api/v1/sdk/calls?${query.toString()}`, key, options, { cache: \"no-store\" });\n return readCall(data);\n } catch {\n return null;\n }\n}\n\n/**\n * Permanently delete one shared call — the recording, the screenshots, the\n * transcript. Cannot be undone.\n *\n * Resolves `{ deleted: false, error }` instead of throwing: the reason is worth\n * showing (\"The bot is still in this call — delete it after the call ends\").\n */\nexport async function deleteCall(id: string, options: CallsOptions = {}): Promise<DeleteCallResult> {\n try {\n const wanted = typeof id === \"string\" ? id.trim() : \"\";\n if (!wanted) return { deleted: false, error: \"No call id\" };\n const key = callsKey(options);\n if (!key) return { deleted: false, error: NO_KEY_ERROR };\n\n const { error } = await callSdkApi(`/api/v1/sdk/calls?id=${encodeURIComponent(wanted)}`, key, options, {\n method: \"DELETE\",\n cache: \"no-store\",\n });\n return error === null ? { deleted: true } : { deleted: false, error };\n } catch {\n return { deleted: false, error: \"Could not delete the call\" };\n }\n}\n\n// ── Booking settings ─────────────────────────────────────────────────────────\n//\n// A project's demo booking settings — whether prospects can book, the hours,\n// slot length and the WhatsApp code — so a team's admin panel can change them\n// without a Glitchgrab login. Same server key as calls. Which Google calendar\n// demos land in is not here: it stays set on the Glitchgrab dashboard.\n\n/** `{ \"1\": [[\"09:00\", \"17:00\"]] }` — ISO weekday (1 = Monday) to opening ranges, in {@link BookingSettings.timezone}. */\nexport type BookingWorkingHours = Record<string, [string, string][]>;\n\nexport interface BookingSettings {\n /** Off means the booking dialog and WhatsApp refuse to offer slots. */\n enabled: boolean;\n /** Length of a demo, 15–180. */\n slotMinutes: number;\n /** Free minutes kept after each booking, 0–120. */\n bufferMinutes: number;\n /** IANA zone the working hours are in, e.g. `\"Asia/Kolkata\"`. */\n timezone: string;\n /** A day left out is closed. */\n workingHours: BookingWorkingHours;\n title: string | null;\n description: string | null;\n /** Who the visitor meets, shown beside the calendar in the booking dialog. */\n hostName: string | null;\n /** https URL of the host's photo. Anything that is not https is ignored on save. */\n hostAvatarUrl: string | null;\n /** How far ahead a slot can be booked, 1–90 days. */\n horizonDays: number;\n /** Earliest a slot can start from now, 0–10080 minutes. */\n noticeMinutes: number;\n /** Code in the \"Book on WhatsApp\" link. Lower case, letters, digits and `-`; unique across projects. */\n whatsappCode: string | null;\n /**\n * Who else is WhatsApped when a demo is booked, besides the project owner —\n * sales, the founders, whoever needs the lead as it lands. At most 5; a\n * ten-digit Indian mobile is stored with its 91. Send the whole list to\n * change it; leave the field out and it keeps what is stored.\n */\n notifyRecipients: BookingNotifyRecipient[];\n /**\n * What a prospect is asked before they see the calendar — in the booking\n * dialog and over WhatsApp — so the right person takes the demo. At most 5.\n * Send the whole list to change it (`[]` asks nothing); leave the field out\n * and it keeps what is stored. The answers come back on {@link Booking.answers}.\n */\n questions: BookingQuestion[];\n /**\n * Questions saved as a draft and not live yet — the dialog and the WhatsApp\n * chat still ask {@link questions}. Preview them with\n * `useGlitchgrab().openBookingPreview()`, make them live with\n * {@link publishBookingQuestions}. `null` = no draft; `[]` = a draft that\n * asks nothing.\n */\n draftQuestions: BookingQuestion[] | null;\n /**\n * Text a code to the prospect's phone before the demo is confirmed. `false`\n * books the slot the moment the form is sent — one step fewer, but a\n * mistyped number still books. Defaults to `true`.\n */\n requireOtp: boolean;\n}\n\n/** One question asked before a demo is booked. The prospect taps one of `options`. */\nexport interface BookingQuestion {\n /**\n * Stable name for the question, e.g. `\"language\"` — lower case letters,\n * digits, `-` and `_`. Leave it out on a new question and one is made from\n * the label; keep it when rewording so earlier answers still line up.\n */\n id: string;\n /** What is asked, up to 80 characters. */\n label: string;\n /** 2–9 choices, up to 40 characters each. */\n options: string[];\n /** False lets the prospect skip it. */\n required: boolean;\n}\n\n/** A question as you send it: `id` and `required` may be left out on a new one. */\nexport type BookingQuestionInput = Omit<BookingQuestion, \"id\" | \"required\"> & { id?: string; required?: boolean };\n\n/** What {@link updateBookingSettings} accepts — any subset of the settings. */\nexport type BookingSettingsChanges = Partial<Omit<BookingSettings, \"questions\" | \"draftQuestions\">> & {\n questions?: BookingQuestionInput[];\n /**\n * With `questions`: save them as a draft instead — the live dialog and chat\n * are unchanged until {@link publishBookingQuestions}. Left out, `questions`\n * go live at once (and any draft is dropped), as before.\n */\n draft?: boolean;\n};\n\n/** One answer a prospect gave, with the question as it was worded when they booked. */\nexport interface BookingAnswer {\n /** The {@link BookingQuestion.id} it answers. */\n id: string;\n question: string;\n answer: string;\n}\n\n/** One extra person alerted when a demo is booked. */\nexport interface BookingNotifyRecipient {\n name: string;\n /** Digits only, country code included. */\n phone: string;\n}\n\nexport interface BookingSettingsState {\n settings: BookingSettings;\n /** False until someone saves once — `settings` are the defaults. */\n saved: boolean;\n /** No calendar means no slots, whatever `enabled` says. Connect one in Glitchgrab → Calls → Booking. */\n calendarConnected: boolean;\n}\n\nexport type BookingSettingsResult = { ok: true; data: BookingSettingsState } | { ok: false; error: string };\n\nconst WEEKDAY = /^[1-7]$/;\n\n/** A stored question list, or null when the value is not a list at all. */\nfunction readQuestionList(value: unknown): BookingQuestion[] | null {\n if (!Array.isArray(value)) return null;\n return value\n .filter(isRecord)\n .map((row) => ({\n id: stringOrNull(row.id) ?? \"\",\n label: stringOrNull(row.label) ?? \"\",\n options: Array.isArray(row.options)\n ? row.options.filter((option): option is string => typeof option === \"string\")\n : [],\n required: row.required !== false,\n }))\n .filter((row) => row.id !== \"\" && row.label !== \"\");\n}\n\nfunction readBookingSettingsState(value: unknown): BookingSettingsState | null {\n if (!isRecord(value) || !isRecord(value.settings)) return null;\n const s = value.settings;\n const hours: BookingWorkingHours = {};\n if (isRecord(s.workingHours)) {\n for (const [day, ranges] of Object.entries(s.workingHours)) {\n if (!WEEKDAY.test(day) || !Array.isArray(ranges)) continue;\n const valid = ranges.filter(\n (r): r is [string, string] => Array.isArray(r) && typeof r[0] === \"string\" && typeof r[1] === \"string\"\n );\n if (valid.length) hours[day] = valid.map(([open, close]) => [open, close]);\n }\n }\n return {\n settings: {\n enabled: s.enabled === true,\n slotMinutes: numberOrNull(s.slotMinutes) ?? 60,\n bufferMinutes: numberOrNull(s.bufferMinutes) ?? 0,\n timezone: typeof s.timezone === \"string\" ? s.timezone : \"Asia/Kolkata\",\n workingHours: hours,\n title: stringOrNull(s.title),\n description: stringOrNull(s.description),\n hostName: stringOrNull(s.hostName),\n hostAvatarUrl: stringOrNull(s.hostAvatarUrl),\n horizonDays: numberOrNull(s.horizonDays) ?? 15,\n noticeMinutes: numberOrNull(s.noticeMinutes) ?? 120,\n whatsappCode: stringOrNull(s.whatsappCode),\n notifyRecipients: Array.isArray(s.notifyRecipients)\n ? s.notifyRecipients\n .filter(isRecord)\n .map((row) => ({\n name: stringOrNull(row.name) ?? \"\",\n phone: stringOrNull(row.phone) ?? \"\",\n }))\n .filter((row) => row.phone !== \"\")\n : [],\n questions: readQuestionList(s.questions) ?? [],\n // A server from before drafts sends no key: no draft.\n draftQuestions: readQuestionList(s.draftQuestions),\n // Missing means a server from before the switch, which always asked.\n requireOtp: s.requireOtp !== false,\n },\n saved: value.saved === true,\n calendarConnected: value.calendarConnected === true,\n };\n}\n\n/**\n * The key's project booking settings. Never throws — `{ ok: false, error }` on\n * any failure, so the page can say why instead of showing defaults as if saved.\n *\n * ```ts\n * const result = await getBookingSettings(); // GLITCHGRAB_CALLS_KEY=ggc_…\n * if (result.ok && !result.data.calendarConnected) warn(\"Connect a calendar in Glitchgrab\");\n * ```\n */\nexport async function getBookingSettings(options: CallsOptions = {}): Promise<BookingSettingsResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error } = await callSdkApi(\"/api/v1/sdk/booking-settings\", key, options, { cache: \"no-store\" });\n const state = error === null ? readBookingSettingsState(data) : null;\n return state ? { ok: true, data: state } : { ok: false, error: error ?? \"Unexpected response\" };\n } catch {\n return { ok: false, error: \"Could not read booking settings\" };\n }\n}\n\n/**\n * Change booking settings. Send only what changed — a field left out keeps its\n * value. Numbers are clamped to their range and invalid working hours are\n * ignored, so read `data.settings` back rather than trusting what you sent.\n *\n * `error` is worth showing: a WhatsApp code already taken by another project\n * comes back as one.\n */\nexport function updateBookingSettings(\n changes: BookingSettingsChanges,\n options: CallsOptions = {}\n): Promise<BookingSettingsResult> {\n return putBookingSettings(changes ?? {}, options);\n}\n\nasync function putBookingSettings(\n body: BookingSettingsChanges | { publishDraft: true } | { discardDraft: true },\n options: CallsOptions\n): Promise<BookingSettingsResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error } = await callSdkApi(\"/api/v1/sdk/booking-settings\", key, options, {\n method: \"PUT\",\n body,\n cache: \"no-store\",\n });\n const state = error === null ? readBookingSettingsState(data) : null;\n return state ? { ok: true, data: state } : { ok: false, error: error ?? \"Unexpected response\" };\n } catch {\n return { ok: false, error: \"Could not save booking settings\" };\n }\n}\n\n/**\n * Make the draft questions live — the booking dialog and the WhatsApp chat ask\n * them from now on. `error` when there is no draft to publish.\n *\n * ```ts\n * await updateBookingSettings({ questions, draft: true }); // live dialog unchanged\n * // …preview with useGlitchgrab().openBookingPreview() on your admin page…\n * await publishBookingQuestions();\n * ```\n */\nexport function publishBookingQuestions(options: CallsOptions = {}): Promise<BookingSettingsResult> {\n return putBookingSettings({ publishDraft: true }, options);\n}\n\n/** Throw the draft questions away; the live ones are untouched. */\nexport function discardBookingQuestionsDraft(options: CallsOptions = {}): Promise<BookingSettingsResult> {\n return putBookingSettings({ discardDraft: true }, options);\n}\n\n// ── Demo bookings ────────────────────────────────────────────────────────────\n//\n// The demos prospects booked on this project, so the team's own admin panel can\n// show them, move them and call them off without a Glitchgrab login. Same\n// server key as calls. Moving one patches the real Google event, so the Meet\n// link every earlier message already carries keeps working.\n\n/** One booked demo. Times are ISO strings in UTC; `timezone` is what the prospect picked in. */\nexport interface Booking {\n id: string;\n /** Who booked. */\n name: string;\n email: string;\n /** Digits only, country code included. */\n phone: string;\n startsAt: string;\n endsAt: string;\n /** IANA zone the prospect booked in, e.g. `\"Asia/Kolkata\"`. */\n timezone: string | null;\n /** What they typed when booking, if anything. */\n note: string | null;\n /** What they answered to the project's {@link BookingSettings.questions}. Empty when nothing was asked. */\n answers: BookingAnswer[];\n /** `CONFIRMED` or `CANCELLED`. Unverified holds are never returned. */\n status: string;\n /** The Google Meet room. Null until the booking is confirmed. */\n meetUrl: string | null;\n createdAt: string;\n}\n\nexport interface ListBookingsOptions extends CallsOptions {\n /** `\"upcoming\"` (default) or `\"past\"`. */\n scope?: \"upcoming\" | \"past\";\n /** Up to 100. Defaults to 50. */\n limit?: number;\n}\n\n/**\n * Whether the prospect was told. A move re-sends the approved confirmation\n * template, so `\"sent\"` is real. A cancellation can only go as free text, which\n * WhatsApp delivers only inside its 24-hour window and reports nothing about —\n * `\"attempted\"` means exactly that, and your panel should say so rather than\n * telling an admin the client knows.\n */\nexport type BookingClientNotice = \"sent\" | \"attempted\" | \"none\";\n\nexport type BookingMutationResult =\n | { ok: true; booking: Booking | null; clientNotice: BookingClientNotice }\n | { ok: false; error: string; code: string | null };\n\nfunction readBooking(value: unknown): Booking | null {\n if (!isRecord(value) || typeof value.id !== \"string\") return null;\n return {\n id: value.id,\n name: stringOrNull(value.name) ?? \"\",\n email: stringOrNull(value.email) ?? \"\",\n phone: stringOrNull(value.phone) ?? \"\",\n startsAt: stringOrNull(value.startsAt) ?? \"\",\n endsAt: stringOrNull(value.endsAt) ?? \"\",\n timezone: stringOrNull(value.timezone),\n note: stringOrNull(value.note),\n answers: Array.isArray(value.answers)\n ? value.answers\n .filter(isRecord)\n .map((row) => ({\n id: stringOrNull(row.id) ?? \"\",\n question: stringOrNull(row.question) ?? \"\",\n answer: stringOrNull(row.answer) ?? \"\",\n }))\n .filter((row) => row.answer !== \"\")\n : [],\n status: stringOrNull(value.status) ?? \"CONFIRMED\",\n meetUrl: stringOrNull(value.meetUrl),\n createdAt: stringOrNull(value.createdAt) ?? \"\",\n };\n}\n\n/**\n * The project's booked demos, soonest first. Never throws — an empty list on\n * any failure, so a panel renders \"no demos\" instead of a stack trace.\n *\n * ```ts\n * const demos = await listBookings(); // GLITCHGRAB_CALLS_KEY=ggc_…\n * ```\n */\nexport async function listBookings(options: ListBookingsOptions = {}): Promise<Booking[]> {\n try {\n const key = callsKey(options);\n if (!key) return [];\n\n const query = new URLSearchParams();\n if (options.scope === \"past\") query.set(\"scope\", \"past\");\n setNumber(query, \"limit\", options.limit);\n const qs = query.toString();\n\n const { data } = await callSdkApi(`/api/v1/sdk/bookings${qs ? `?${qs}` : \"\"}`, key, options, {\n cache: \"no-store\",\n });\n if (!Array.isArray(data)) return [];\n return data.map(readBooking).filter((booking): booking is Booking => booking !== null);\n } catch {\n return [];\n }\n}\n\nfunction readMutation(data: unknown, error: string | null): BookingMutationResult {\n if (error !== null) return { ok: false, error, code: null };\n const record = isRecord(data) ? data : {};\n const notice = stringOrNull(record.clientNotice);\n return {\n ok: true,\n booking: readBooking(record.booking),\n clientNotice: notice === \"sent\" || notice === \"attempted\" ? notice : \"none\",\n };\n}\n\n/**\n * Move a demo to a new start time. The Google event is patched, never\n * recreated, so the Meet link in every message already sent stays alive, and\n * the prospect is re-sent the confirmation with the new time.\n *\n * `{ ok: false, code: \"SLOT_TAKEN\" }` when another booking holds that slot,\n * `\"PAST\"` for a time that has gone, `\"CALENDAR\"` when Google refused — in the\n * last case nothing was changed and the demo still stands at its old time.\n */\nexport async function rescheduleBooking(\n id: string,\n startsAt: Date | string,\n options: CallsOptions = {}\n): Promise<BookingMutationResult> {\n try {\n const wanted = typeof id === \"string\" ? id.trim() : \"\";\n const key = callsKey(options);\n if (!wanted) return { ok: false, error: \"No booking id\", code: null };\n if (!key) return { ok: false, error: NO_KEY_ERROR, code: null };\n\n const when = startsAt instanceof Date ? startsAt.toISOString() : String(startsAt);\n const { data, error } = await callSdkApi(\n `/api/v1/sdk/bookings?id=${encodeURIComponent(wanted)}`,\n key,\n options,\n { method: \"PATCH\", body: { startsAt: when }, cache: \"no-store\" }\n );\n return readMutation(data, error);\n } catch {\n return { ok: false, error: \"Could not move that demo\", code: null };\n }\n}\n\n/**\n * Call a demo off: the Google event is deleted, the recorder is stood down, and\n * the prospect is messaged if WhatsApp will carry it (see\n * {@link BookingClientNotice}).\n *\n * `reason` is included in that message. Nothing is marked cancelled unless the\n * calendar agreed first, so `{ ok: false, code: \"CALENDAR\" }` means the demo is\n * still on.\n */\nexport async function cancelBooking(\n id: string,\n reason?: string,\n options: CallsOptions = {}\n): Promise<BookingMutationResult> {\n try {\n const wanted = typeof id === \"string\" ? id.trim() : \"\";\n const key = callsKey(options);\n if (!wanted) return { ok: false, error: \"No booking id\", code: null };\n if (!key) return { ok: false, error: NO_KEY_ERROR, code: null };\n\n const query = new URLSearchParams({ id: wanted });\n if (typeof reason === \"string\" && reason.trim()) query.set(\"reason\", reason.trim().slice(0, 200));\n\n const { data, error } = await callSdkApi(`/api/v1/sdk/bookings?${query.toString()}`, key, options, {\n method: \"DELETE\",\n cache: \"no-store\",\n });\n return readMutation(data, error);\n } catch {\n return { ok: false, error: \"Could not cancel that demo\", code: null };\n }\n}\n\n/**\n * `JSON.stringify` that is safe inside `<script type=\"application/ld+json\">`.\n *\n * Guide titles and summaries are written outside your codebase, and\n * `JSON.stringify` leaves `<` alone — a title containing `</script>` closes the\n * tag and whatever follows runs as HTML. The JSON unicode escape for `<` is the same\n * character to a JSON parser and inert to the HTML one. `>`, `&` and the two\n * JavaScript line separators are escaped for the same reason.\n *\n * ```tsx\n * <script\n * type=\"application/ld+json\"\n * dangerouslySetInnerHTML={{ __html: escapeJsonForScript({ \"@type\": \"HowTo\", name: guide.title }) }}\n * />\n * ```\n *\n * Never throws: a value JSON cannot represent (circular, `undefined`) becomes `null`.\n */\nexport function escapeJsonForScript(value: unknown): string {\n try {\n const json = JSON.stringify(value);\n if (json === undefined) return \"null\";\n return json\n .replace(/</g, \"\\\\u003c\")\n .replace(/>/g, \"\\\\u003e\")\n .replace(/&/g, \"\\\\u0026\")\n .replace(/\\u2028/g, \"\\\\u2028\")\n .replace(/\\u2029/g, \"\\\\u2029\");\n } catch {\n return \"null\";\n }\n}\n\n// ── Leads ────────────────────────────────────────────────────────────────────\n//\n// Contact details prospects left with `useGlitchgrab().submitLead()` (#534) —\n// the demo form, the contact page — so your own admin can show them. Same\n// server key as calls and bookings. A lead never becomes a GitHub issue.\n\n/** One lead. One per phone number per project; a repeat submission updates it. */\nexport interface Lead {\n id: string;\n name: string;\n /** Digits only, country code included. */\n phone: string;\n email: string | null;\n company: string | null;\n address: string | null;\n /** What your form passed as `source`, e.g. `\"demo\"`. */\n source: string;\n metadata: Record<string, string | number | boolean> | null;\n /** The page the lead was left on. */\n pageUrl: string | null;\n /** How many times this phone submitted. */\n submissions: number;\n /** First submission. */\n createdAt: string;\n /** Latest submission. */\n updatedAt: string;\n /** The demo this lead went on to book, matched by phone or email. */\n booking: { id: string; startsAt: string; status: string } | null;\n}\n\nexport interface ListLeadsOptions extends CallsOptions {\n /** Only leads with this `source`. */\n source?: string;\n /** Only leads first left at or after this time. */\n since?: Date | string;\n /** Up to 200. Defaults to 100. */\n limit?: number;\n}\n\nfunction readLead(value: unknown): Lead | null {\n if (!isRecord(value) || typeof value.id !== \"string\") return null;\n const metadata = isRecord(value.metadata) ? (value.metadata as Lead[\"metadata\"]) : null;\n const booking = isRecord(value.booking) && typeof value.booking.id === \"string\"\n ? {\n id: value.booking.id,\n startsAt: stringOrNull(value.booking.startsAt) ?? \"\",\n status: stringOrNull(value.booking.status) ?? \"CONFIRMED\",\n }\n : null;\n return {\n id: value.id,\n name: stringOrNull(value.name) ?? \"\",\n phone: stringOrNull(value.phone) ?? \"\",\n email: stringOrNull(value.email),\n company: stringOrNull(value.company),\n address: stringOrNull(value.address),\n source: stringOrNull(value.source) ?? \"\",\n metadata,\n pageUrl: stringOrNull(value.pageUrl),\n submissions: typeof value.submissions === \"number\" ? value.submissions : 1,\n createdAt: stringOrNull(value.createdAt) ?? \"\",\n updatedAt: stringOrNull(value.updatedAt) ?? \"\",\n booking,\n };\n}\n\n/**\n * The project's leads, newest first. Never throws — an empty list on any\n * failure.\n *\n * ```ts\n * const leads = await listLeads({ source: \"demo\" }); // GLITCHGRAB_CALLS_KEY=ggc_…\n * ```\n */\nexport async function listLeads(options: ListLeadsOptions = {}): Promise<Lead[]> {\n try {\n const key = callsKey(options);\n if (!key) return [];\n\n const query = new URLSearchParams();\n if (options.source) query.set(\"source\", options.source);\n if (options.since) {\n const since = options.since instanceof Date ? options.since : new Date(options.since);\n if (!Number.isNaN(since.getTime())) query.set(\"since\", since.toISOString());\n }\n setNumber(query, \"limit\", options.limit);\n const qs = query.toString();\n\n const { data } = await callSdkApi(`/api/v1/sdk/leads${qs ? `?${qs}` : \"\"}`, key, options, {\n cache: \"no-store\",\n });\n if (!Array.isArray(data)) return [];\n return data.map(readLead).filter((lead): lead is Lead => lead !== null);\n } catch {\n return [];\n }\n}\n\n// ─── Customer follow-ups (#630) ─────────────────────────────────────────────\n\n/** How a next call reads on a list: grey, amber, red. */\nexport type FollowUpDue = \"none\" | \"upcoming\" | \"today\" | \"overdue\";\n\nexport interface FollowUp {\n /** Glitchgrab's id; null for an externalId Glitchgrab has never seen. */\n id: string | null;\n externalId: string | null;\n /** Digits, `91` + 10. */\n phone: string | null;\n name: string | null;\n /** The customer's business (Abhyasika: the library name), as you sent it. */\n businessName: string | null;\n /** ISO. The booked next call. */\n nextCallAt: string | null;\n nextCallBookedBy: string | null;\n due: FollowUpDue;\n /** The next call with why it is happening (#670). Null when nothing is booked. */\n booking: FollowUpBooking | null;\n /**\n * ISO. Read from the last call's transcript. Since #670 a call's suggestion\n * is booked by itself (see `booking.auto`); it stays only a suggestion when\n * a person's own booking already stands.\n */\n suggestedNextCallAt: string | null;\n suggestedNextCallReason: string | null;\n lastNote: string | null;\n lastNoteAt: string | null;\n lastNoteBy: string | null;\n noteCount: number;\n lastCall: {\n id: string;\n status: string | null;\n at: string;\n durationSec: number | null;\n /** Which of the operator's phones the call is on: 0 = the one picked, 1+ = a backup (#654). */\n ringingIndex: number | null;\n /** That phone. While `status` is \"DIALING\" it is the one ringing right now. */\n ringingPhone: string | null;\n } | null;\n}\n\nexport interface FollowUpBooking {\n /** ISO. */\n at: string;\n /** \"Ravi → Sunita Patil (Shanti Study Room): set up auto-pay again\" — show it under \"Next call\". */\n reason: string | null;\n /** Booked by Glitchgrab from the call summary — label it \"Booked from the call summary\". */\n auto: boolean;\n /** ISO. The caller tapped \"Yes, keep it\" on WhatsApp. */\n confirmedAt: string | null;\n /** The \"keep this call?\" WhatsApp reached whoever made the call. */\n askedOnWhatsApp: boolean;\n bookedBy: string | null;\n}\n\nexport interface FollowUpNote {\n id: string;\n body: string;\n /** \"manual\" (Glitchgrab), \"api\" (your app), \"auto\" (written from a call). */\n source: string;\n author: string | null;\n callId: string | null;\n editedAt: string | null;\n /** Who last edited it, when your app (or an import) said so. */\n editedBy: string | null;\n createdAt: string;\n}\n\nexport interface FollowUpCall {\n id: string;\n status: string | null;\n direction: string | null;\n at: string;\n durationSec: number | null;\n /** Which of the operator's phones the call is on: 0 = the one picked, 1+ = a backup (#654). */\n ringingIndex: number | null;\n /** That phone. While `status` is \"DIALING\" it is the one ringing right now. */\n ringingPhone: string | null;\n /** E.164 number the call rang — `toPhone` when the dial sent one, else the follow-up's phone (#666). */\n to: string | null;\n /**\n * Who didn't pick up a NO_ANSWER outbound call (#666): \"operator\" = none of\n * the operator's phones (backups included), \"customer\" = the operator\n * answered and the customer didn't. Null otherwise, and on older calls.\n */\n missedBy: \"operator\" | \"customer\" | null;\n /** Who placed it. Null on inbound calls. */\n operatorName: string | null;\n /** Shared with you — calls dialled from a follow-up are; an inbound call is once the owner shares it. */\n shared: boolean;\n overview: string | null;\n /** Presigned; expires after `linksExpireInSeconds`. Null when not shared. */\n recordingUrl: string | null;\n}\n\nexport interface FollowUpDetail extends FollowUp {\n notes: FollowUpNote[];\n calls: FollowUpCall[];\n linksExpireInSeconds: number;\n}\n\n/** Someone on the project's Glitchgrab phone line — rung first on a dial, reminded on a booking. */\nexport type FollowUpOperator = { email: string } | { phone: string };\n\n/**\n * Why a dial was refused, so your screen can say exactly what is missing (#654):\n * - `NOT_ON_LINE` — the operator isn't one of the people on the project's phone line.\n * - `NO_VERIFIED_NUMBER` — they are, but have no phone Glitchgrab may ring.\n * - `RING_PHONE_NOT_YOURS` — `ringPhone` isn't one of their verified numbers.\n */\nexport type RingErrorCode = \"NOT_ON_LINE\" | \"NO_VERIFIED_NUMBER\" | \"RING_PHONE_NOT_YOURS\";\n\nexport type FollowUpWriteResult<T = Record<string, never>> =\n | ({ ok: true; followUpId: string } & T)\n | { ok: false; error: string; code?: string };\n\nconst DUES: ReadonlySet<string> = new Set([\"none\", \"upcoming\", \"today\", \"overdue\"]);\n\nfunction readFollowUp(value: unknown): FollowUp | null {\n if (!isRecord(value)) return null;\n const call = isRecord(value.lastCall) && typeof value.lastCall.id === \"string\" ? value.lastCall : null;\n return {\n id: stringOrNull(value.id),\n externalId: stringOrNull(value.externalId),\n phone: stringOrNull(value.phone),\n name: stringOrNull(value.name),\n businessName: stringOrNull(value.businessName),\n nextCallAt: stringOrNull(value.nextCallAt),\n nextCallBookedBy: stringOrNull(value.nextCallBookedBy),\n due: typeof value.due === \"string\" && DUES.has(value.due) ? (value.due as FollowUpDue) : \"none\",\n booking:\n isRecord(value.booking) && typeof value.booking.at === \"string\"\n ? {\n at: value.booking.at,\n reason: stringOrNull(value.booking.reason),\n auto: value.booking.auto === true,\n confirmedAt: stringOrNull(value.booking.confirmedAt),\n askedOnWhatsApp: value.booking.askedOnWhatsApp === true,\n bookedBy: stringOrNull(value.booking.bookedBy),\n }\n : null,\n suggestedNextCallAt: stringOrNull(value.suggestedNextCallAt),\n suggestedNextCallReason: stringOrNull(value.suggestedNextCallReason),\n lastNote: stringOrNull(value.lastNote),\n lastNoteAt: stringOrNull(value.lastNoteAt),\n lastNoteBy: stringOrNull(value.lastNoteBy),\n noteCount: numberOrNull(value.noteCount) ?? 0,\n lastCall: call\n ? {\n id: call.id as string,\n status: stringOrNull(call.status),\n at: stringOrNull(call.at) ?? \"\",\n durationSec: numberOrNull(call.durationSec),\n ringingIndex: numberOrNull(call.ringingIndex),\n ringingPhone: stringOrNull(call.ringingPhone),\n }\n : null,\n };\n}\n\nfunction readFollowUpDetail(value: unknown): FollowUpDetail | null {\n const base = readFollowUp(value);\n if (!base || !isRecord(value)) return null;\n return {\n ...base,\n linksExpireInSeconds: numberOrNull(value.linksExpireInSeconds) ?? 0,\n notes: Array.isArray(value.notes)\n ? value.notes\n .filter(isRecord)\n .filter((n) => typeof n.id === \"string\" && typeof n.body === \"string\")\n .map((n) => ({\n id: n.id as string,\n body: n.body as string,\n source: stringOrNull(n.source) ?? \"manual\",\n author: stringOrNull(n.author),\n callId: stringOrNull(n.callId),\n editedAt: stringOrNull(n.editedAt),\n editedBy: stringOrNull(n.editedBy),\n createdAt: stringOrNull(n.createdAt) ?? \"\",\n }))\n : [],\n calls: Array.isArray(value.calls)\n ? value.calls\n .filter(isRecord)\n .filter((c) => typeof c.id === \"string\")\n .map((c) => ({\n id: c.id as string,\n status: stringOrNull(c.status),\n direction: stringOrNull(c.direction),\n at: stringOrNull(c.at) ?? \"\",\n durationSec: numberOrNull(c.durationSec),\n ringingIndex: numberOrNull(c.ringingIndex),\n ringingPhone: stringOrNull(c.ringingPhone),\n to: stringOrNull(c.to),\n missedBy: c.missedBy === \"operator\" || c.missedBy === \"customer\" ? c.missedBy : null,\n operatorName: stringOrNull(c.operatorName),\n shared: c.shared === true,\n overview: stringOrNull(c.overview),\n recordingUrl: stringOrNull(c.recordingUrl),\n }))\n : [],\n };\n}\n\nfunction followUpPath(externalId: string, rest = \"\"): string | null {\n const id = typeof externalId === \"string\" ? externalId.trim() : \"\";\n return id ? `/api/v1/sdk/follow-ups/${encodeURIComponent(id)}${rest}` : null;\n}\n\nasync function writeFollowUp<T extends Record<string, unknown>>(\n path: string | null,\n method: \"POST\" | \"PUT\" | \"PATCH\",\n body: Record<string, unknown>,\n options: CallsOptions,\n pick: (data: Record<string, unknown>) => T\n): Promise<FollowUpWriteResult<T>> {\n try {\n if (!path) return { ok: false, error: \"No externalId\" };\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(path, key, options, { method, body, cache: \"no-store\" });\n if (error !== null || !isRecord(data) || typeof data.followUpId !== \"string\") {\n return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n }\n return { ok: true, followUpId: data.followUpId, ...pick(data) } as FollowUpWriteResult<T>;\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Follow-ups for a whole list page in one request — one entry per id, in the\n * order asked (≤500). An id Glitchgrab has never seen comes back empty, so\n * every row can render.\n *\n * ```ts\n * const followUps = await listFollowUps(owners.map((o) => o.id)); // GLITCHGRAB_CALLS_KEY=ggc_…\n * ```\n *\n * Returns `[]` on any failure. Never throws.\n */\nexport async function listFollowUps(externalIds: string[], options: CallsOptions = {}): Promise<FollowUp[]> {\n try {\n const key = callsKey(options);\n const ids = Array.isArray(externalIds) ? externalIds.filter((id) => typeof id === \"string\" && id.trim()) : [];\n if (!key || ids.length === 0) return [];\n const query = new URLSearchParams({ externalIds: ids.join(\",\") });\n const { data } = await callSdkApi(`/api/v1/sdk/follow-ups?${query.toString()}`, key, options, {\n cache: \"no-store\",\n });\n if (!Array.isArray(data)) return [];\n return data.map(readFollowUp).filter((f): f is FollowUp => f !== null);\n } catch {\n return [];\n }\n}\n\n/**\n * One customer's follow-up: booking, every note, their calls (recording links\n * for shared calls only, presigned). Null on failure. Never throws.\n */\nexport async function getFollowUp(externalId: string, options: CallsOptions = {}): Promise<FollowUpDetail | null> {\n try {\n const path = followUpPath(externalId);\n const key = callsKey(options);\n if (!path || !key) return null;\n const { data } = await callSdkApi(path, key, options, { cache: \"no-store\" });\n return readFollowUpDetail(data);\n } catch {\n return null;\n }\n}\n\n/**\n * Add a note from your own screen. `phone` is needed the first time Glitchgrab\n * sees this externalId. `createdAt` / `editedAt` / `editedBy` copy a note from\n * your old store with its real dates (past only; no webhook fires for it).\n * Never throws.\n */\nexport function addFollowUpNote(\n externalId: string,\n note: {\n body: string;\n author?: string;\n phone?: string;\n name?: string;\n businessName?: string;\n createdAt?: Date | string;\n editedAt?: Date | string | null;\n editedBy?: string | null;\n },\n options: CallsOptions = {}\n): Promise<FollowUpWriteResult<{ id: string }>> {\n const iso = (v: Date | string | null | undefined) => (v instanceof Date ? v.toISOString() : v);\n const body = { ...note, createdAt: iso(note.createdAt), editedAt: iso(note.editedAt) };\n return writeFollowUp(followUpPath(externalId, \"/notes\"), \"POST\", body, options, (d) => ({\n id: typeof d.id === \"string\" ? d.id : \"\",\n }));\n}\n\n/**\n * Edit a note from your own screen: your app's notes and the note written from\n * a call. A note someone wrote inside Glitchgrab can't be edited here (404).\n * `editor` is shown as who edited it. Never throws.\n */\nexport function editFollowUpNote(\n externalId: string,\n noteId: string,\n edit: { body: string; editor?: string },\n options: CallsOptions = {}\n): Promise<FollowUpWriteResult<{ id: string }>> {\n return writeFollowUp(\n followUpPath(externalId, `/notes/${encodeURIComponent(noteId)}`),\n \"PATCH\",\n edit,\n options,\n (d) => ({ id: typeof d.id === \"string\" ? d.id : \"\" })\n );\n}\n\n/**\n * Book, move (`Date`/ISO) or clear (`null`) the next call. The reminder\n * WhatsApp goes to `operator`, else the project owner. `reason` is read back\n * as `booking.reason`; left out, a moved booking keeps its reason. A booking\n * made here is never replaced by the next call's automatic one. Never throws.\n */\nexport function setFollowUpBooking(\n externalId: string,\n booking: {\n nextCallAt: Date | string | null;\n reason?: string | null;\n operator?: FollowUpOperator;\n author?: string;\n phone?: string;\n name?: string;\n businessName?: string;\n },\n options: CallsOptions = {}\n): Promise<FollowUpWriteResult<{ nextCallAt: string | null; reason: string | null }>> {\n const at = booking.nextCallAt instanceof Date ? booking.nextCallAt.toISOString() : booking.nextCallAt;\n return writeFollowUp(followUpPath(externalId, \"/booking\"), \"PUT\", { ...booking, nextCallAt: at }, options, (d) => ({\n nextCallAt: stringOrNull(d.nextCallAt),\n reason: stringOrNull(d.reason),\n }));\n}\n\n/**\n * Start a recorded call: Glitchgrab rings `operator`'s phone, then the\n * customer, from the project's line. The operator must be on that line. Once\n * the call is transcribed a note is written and the next call is booked from\n * it with a reason (#670) — unless a person's booking already stands — and the\n * operator is asked on WhatsApp to keep or remove it. `businessName` (the\n * customer's business) is named in that reason. Never throws.\n *\n * `ringPhone` picks which of the operator's verified phones rings first (list\n * them with {@link getOperatorRingNumbers}); omitted = their Default. If that\n * phone isn't picked up, their backups ring in order (`ringNumbers`). A\n * refusal carries `code` — see {@link RingErrorCode}.\n *\n * `toPhone` dials another number for this one call and leaves the follow-up's\n * saved `phone` alone (#666); `phone` rewrites the saved number.\n */\nexport function dialFollowUp(\n externalId: string,\n call: {\n operator: FollowUpOperator;\n phone?: string;\n toPhone?: string;\n name?: string;\n businessName?: string;\n lineId?: string;\n ringPhone?: string;\n },\n options: CallsOptions = {}\n): Promise<FollowUpWriteResult<{ callId: string; ringing: string; ringNumbers: string[] }>> {\n return writeFollowUp(followUpPath(externalId, \"/dial\"), \"POST\", call, options, (d) => ({\n callId: typeof d.callId === \"string\" ? d.callId : \"\",\n ringing: typeof d.ringing === \"string\" ? d.ringing : \"\",\n ringNumbers: Array.isArray(d.ringNumbers) ? d.ringNumbers.filter((n): n is string => typeof n === \"string\") : [],\n }));\n}\n\n/** One phone Glitchgrab may ring for an operator — what `ringPhone` accepts (#654). */\nexport interface OperatorRingNumber {\n /** E.164, `+91…`. Pass it back as `ringPhone`. */\n phone: string;\n label: string | null;\n isDefault: boolean;\n /** 0 = Default, then backups in the order they ring. */\n order: number;\n}\n\nexport type OperatorRingNumbersResult =\n | { ok: true; operator: { name: string | null }; numbers: OperatorRingNumber[] }\n | { ok: false; error: string; code?: string };\n\n/**\n * The phones Glitchgrab can ring for one person on the project's phone line —\n * for a \"Ring me on: [Default ▾]\" picker beside your Call button. Verified\n * numbers only. `numbers: []` = dial will refuse with `NO_VERIFIED_NUMBER`;\n * not on the line = `{ ok: false, code: \"NOT_ON_LINE\" }`. Never throws.\n *\n * ```ts\n * const r = await getOperatorRingNumbers({ email: admin.email }); // GLITCHGRAB_CALLS_KEY=ggc_…\n * if (r.ok) render(r.numbers); // pass the chosen phone to dialFollowUp(…, { ringPhone })\n * ```\n */\nexport async function getOperatorRingNumbers(\n operator: FollowUpOperator,\n options: CallsOptions = {}\n): Promise<OperatorRingNumbersResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const query = new URLSearchParams(\"email\" in operator ? { email: operator.email } : { phone: operator.phone });\n const { data, error, code } = await callSdkApi(`/api/v1/sdk/phone-lines/operator?${query.toString()}`, key, options, {\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data)) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n return { ok: true, ...operatorNumbersOf(data) };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/** `{ operator, numbers }` as `GET operator` and `POST callers` both answer it. */\nfunction operatorNumbersOf(data: Record<string, unknown>): {\n operator: { name: string | null };\n numbers: OperatorRingNumber[];\n} {\n const op = isRecord(data.operator) ? data.operator : {};\n return {\n operator: { name: stringOrNull(op.name) },\n numbers: Array.isArray(data.numbers)\n ? data.numbers\n .filter(isRecord)\n .filter((n) => typeof n.phone === \"string\")\n .map((n) => ({\n phone: n.phone as string,\n label: stringOrNull(n.label),\n isDefault: n.isDefault === true,\n order: numberOrNull(n.order) ?? 0,\n }))\n : [],\n };\n}\n\nexport type LineCallerResult =\n | { ok: true; created: boolean; operator: { name: string | null }; numbers: OperatorRingNumber[] }\n | { ok: false; error: string; code?: string };\n\n/**\n * Put someone on the project's phone line from your product (#662), so nobody\n * opens Glitchgrab to let a new admin call. Idempotent on email: someone\n * already on the line comes back with `created: false`.\n *\n * They ring only phones confirmed with press 1 — add one with\n * {@link addRingNumber} right after (`numbers: []` until then, and a dial\n * answers `NO_VERIFIED_NUMBER`). Refusals carry `code`: `NO_LINE`,\n * `CALLERS_OFF` (the line's owner switched it off), `CALLER_CAP` (10 per line).\n * Never throws.\n *\n * `ownerId` = seat them on that customer's own line (#716 — one line per\n * PracticeStacks firm). Omitted = the project's line.\n *\n * ```ts\n * const r = await addLineCaller({ email: admin.email, name: admin.name });\n * if (r.ok) openMyPhones(r.numbers);\n * ```\n */\nexport async function addLineCaller(\n caller: { email: string; name: string; phone?: string; ownerId?: string },\n options: CallsOptions = {}\n): Promise<LineCallerResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(\"/api/v1/sdk/phone-lines/callers\", key, options, {\n method: \"POST\",\n body: caller,\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data)) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n return { ok: true, created: data.created === true, ...operatorNumbersOf(data) };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/** Fields every `call.*` webhook carries (#716). `refKey`/`ownerId` are null when not set. */\ninterface CallWebhookBase {\n callId: string;\n repoId: string | null;\n lineId: string | null;\n direction: \"outbound\" | \"inbound\" | null;\n /** The other party, E.164. */\n peer: string | null;\n /** What you passed to {@link placeCall}. */\n refKey: string | null;\n /** The customer whose line carried the call. */\n ownerId: string | null;\n}\n\n/**\n * The body Glitchgrab POSTs to your webhook for a phone call, signed with\n * `X-Glitchgrab-Signature: sha256=<hex HMAC of the raw body>`. Narrow on `event`.\n */\nexport type CallWebhook =\n | { event: \"call.started\"; timestamp: string; data: CallWebhookBase & { ringing: string } }\n | { event: \"call.answered\"; timestamp: string; data: CallWebhookBase & { answeredAt: string } }\n | {\n event: \"call.completed\";\n timestamp: string;\n data: CallWebhookBase & {\n /** RECORDED = they talked; NO_ANSWER (+ missedBy); FAILED. */\n status: string;\n missedBy: \"operator\" | \"customer\" | null;\n durationSec: number | null;\n hangupCause: string | null;\n };\n }\n | {\n event: \"call.recording_ready\";\n timestamp: string;\n data: Omit<CallWebhookBase, \"lineId\"> & { durationSec: number | null };\n }\n | {\n event: \"call.summary_ready\";\n timestamp: string;\n data: Pick<CallWebhookBase, \"callId\" | \"repoId\" | \"refKey\" | \"ownerId\"> & {\n /** \"phone\", \"bot\" or \"extension\". */\n recorder: string;\n title: string | null;\n summary: string | null;\n };\n };\n\n/** `phone.*` webhooks for a customer's automatic line (#716). Same signing as {@link CallWebhook}. */\nexport type PhoneLineWebhook =\n | {\n event: \"phone.kyc_status\";\n timestamp: string;\n data: { ownerId: string; status: string; reason: string | null };\n }\n | { event: \"phone.line_ready\"; timestamp: string; data: { ownerId: string; number: string; lineId: string } };\n\nexport type PlaceCallResult =\n | { ok: true; callId: string; ringing: string; ringNumbers: string[]; from: string }\n | { ok: false; error: string; code?: string };\n\n/**\n * Start a recorded call from your own screen (#716) — Glitchgrab rings\n * `operator`'s verified phone from the line, then bridges to `to`. No\n * follow-up is created; use {@link dialFollowUp} for that.\n *\n * `ownerId` = call from that customer's own line (one line per PracticeStacks\n * firm); the operator must be seated on it ({@link addLineCaller} with the\n * same `ownerId`). `refKey` = your own key for what the call is about — every\n * `call.*` webhook (`call.started`, `call.answered`, `call.completed`,\n * `call.recording_ready`, `call.summary_ready`) carries it back with `ownerId`.\n * Check your own permissions before calling this. Refusals carry `code`:\n * `NOT_ON_LINE`, `NO_VERIFIED_NUMBER`, `RING_PHONE_NOT_YOURS`. Never throws.\n *\n * ```ts\n * const r = await placeCall({\n * operator: { email: staff.email },\n * to: client.phone,\n * ownerId: firm.id,\n * refKey: client.id,\n * }); // GLITCHGRAB_CALLS_KEY=ggc_…\n * ```\n */\nexport async function placeCall(\n call: {\n operator: FollowUpOperator;\n to: string;\n ownerId?: string;\n refKey?: string;\n title?: string;\n ringPhone?: string;\n },\n options: CallsOptions = {}\n): Promise<PlaceCallResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(\"/api/v1/sdk/phone-lines/call\", key, options, {\n method: \"POST\",\n body: call,\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data) || typeof data.callId !== \"string\") {\n return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n }\n return {\n ok: true,\n callId: data.callId,\n ringing: typeof data.ringing === \"string\" ? data.ringing : \"\",\n ringNumbers: Array.isArray(data.ringNumbers)\n ? data.ringNumbers.filter((n): n is string => typeof n === \"string\")\n : [],\n from: typeof data.from === \"string\" ? data.from : \"\",\n };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\nexport interface PhoneLineInfo {\n number: string;\n label: string | null;\n active: boolean;\n /** People seated on it; anyone else gets `NOT_ON_LINE`. */\n callers: number;\n}\n\n/** Where a customer's automatic line is up to (#716). */\nexport interface PhoneLineRequest {\n /** Plivo's KYC status: submitted | accepted | rejected | suspended | expired. */\n kycStatus: string;\n /** Why Plivo rejected it — show it to the firm so they can fix and resubmit. */\n kycReason: string | null;\n /** The firm agreed to the monthly charge ({@link enableCalling}). */\n enabled: boolean;\n /** NONE | BUYING | BOUGHT | FAILED (retried automatically). */\n buyState: string;\n buyError: string | null;\n number: string | null;\n}\n\nexport interface PhoneLineStatus {\n line: PhoneLineInfo | null;\n /** Null when no KYC was submitted for this customer. */\n request: PhoneLineRequest | null;\n}\n\nfunction lineRequestOf(value: unknown): PhoneLineRequest | null {\n if (!isRecord(value) || typeof value.kycStatus !== \"string\") return null;\n return {\n kycStatus: value.kycStatus,\n kycReason: stringOrNull(value.kycReason),\n enabled: value.enabled === true,\n buyState: typeof value.buyState === \"string\" ? value.buyState : \"NONE\",\n buyError: stringOrNull(value.buyError),\n number: stringOrNull(value.number),\n };\n}\n\n/**\n * A customer's phone line and, when you submitted their KYC, how far the\n * automatic purchase has got (#716) — for the firm's \"Calling\" settings:\n * no request → \"Submit KYC\"; `kycStatus: \"rejected\"` → show `kycReason`;\n * accepted but `enabled: false` → \"Turn on calling (₹… /month)\"; `line` set →\n * ready. `ownerId` omitted = the project's own line. `null` only on failure\n * (no key, network). Never throws.\n */\nexport async function getPhoneLine(\n params: { ownerId?: string } = {},\n options: CallsOptions = {}\n): Promise<PhoneLineStatus | null> {\n try {\n const key = callsKey(options);\n if (!key) return null;\n const query = params.ownerId ? `?ownerId=${encodeURIComponent(params.ownerId)}` : \"\";\n const { data, error } = await callSdkApi(`/api/v1/sdk/phone-lines${query}`, key, options, { cache: \"no-store\" });\n if (error !== null || !isRecord(data)) return null;\n const line = isRecord(data.line) && typeof data.line.number === \"string\" ? data.line : null;\n return {\n line: line\n ? {\n number: line.number as string,\n label: stringOrNull(line.label),\n active: line.active === true,\n callers: numberOrNull(line.callers) ?? 0,\n }\n : null,\n request: lineRequestOf(data.request),\n };\n } catch {\n return null;\n }\n}\n\nexport type LineRequestResult = { ok: true; request: PhoneLineRequest } | { ok: false; error: string; code?: string };\n\nasync function lineRequestCall(\n path: string,\n body: unknown,\n options: CallsOptions\n): Promise<LineRequestResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(path, key, options, { method: \"POST\", body, cache: \"no-store\" });\n const request = isRecord(data) ? lineRequestOf(data.request) : null;\n if (error !== null || !request) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n return { ok: true, request };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Submit a customer's KYC so they get their own phone number (#716). Plivo\n * reviews it (usually minutes, sometimes a day); the `phone.kyc_status`\n * webhook says when. Rejected → fix and call again. Certificates: PDF, JPG or\n * PNG, under 4 MB together; they go straight to Plivo, Glitchgrab keeps no\n * copy. Refusals carry `code`: `KYC_PENDING`, `KYC_DONE`, `BAD_FILE`,\n * `PROVIDER`. Nothing is bought until {@link enableCalling}. Never throws.\n */\nexport function submitPhoneKyc(\n kyc: {\n ownerId: string;\n /** Legal name, as on the certificates. */\n businessName: string;\n email: string;\n addressLine1: string;\n city: string;\n state: string;\n /** 6-digit PIN code. */\n postalCode: string;\n registrationCertificate: Blob;\n gstCertificate: Blob;\n },\n options: CallsOptions = {}\n): Promise<LineRequestResult> {\n const form = new FormData();\n for (const k of [\"ownerId\", \"businessName\", \"email\", \"addressLine1\", \"city\", \"state\", \"postalCode\"] as const) {\n form.set(k, kyc[k]);\n }\n form.set(\"registrationCertificate\", kyc.registrationCertificate, \"registration-certificate\");\n form.set(\"gstCertificate\", kyc.gstCertificate, \"gst-certificate\");\n return lineRequestCall(\"/api/v1/sdk/phone-lines/kyc\", form, options);\n}\n\n/**\n * The firm turned calling on and agreed to the monthly charge for its own\n * number (#716). `agreedBy` (who agreed) and the time are kept as the billing\n * record. Once the KYC is accepted — now or later — Glitchgrab rents the\n * number and creates the line (`phone.line_ready` webhook); then seat staff\n * with {@link addLineCaller} and the same `ownerId`. `areaCode` = STD code to\n * search under (\"20\" Pune), else any free Indian number. Never throws.\n */\nexport function enableCalling(\n params: { ownerId: string; agreedBy: string; areaCode?: string },\n options: CallsOptions = {}\n): Promise<LineRequestResult> {\n return lineRequestCall(\"/api/v1/sdk/phone-lines/enable\", { ...params, agreedToCharge: true }, options);\n}\n\n/**\n * The firm turned calling off (#716): its line stops ringing and calling at\n * once, consent is cleared, and Navibyte is told to release the number.\n * {@link enableCalling} again switches the same line back on. Never throws.\n */\nexport function disableCalling(params: { ownerId: string }, options: CallsOptions = {}): Promise<LineRequestResult> {\n return lineRequestCall(\"/api/v1/sdk/phone-lines/disable\", params, options);\n}\n\nexport interface RegisteredWebhook {\n id: string;\n url: string;\n events: string[];\n}\n\n/**\n * Register your backend's webhook from code (#716) — scoped to this project,\n * no Settings clicks. https and a public host only. The `secret` comes back\n * once: verify every delivery's `X-Glitchgrab-Signature` (`sha256=` HMAC of the\n * raw body) with it. Same URL twice → refused; remove it first to rotate.\n * Never throws.\n */\nexport async function registerWebhook(\n webhook: { url: string; events: string[] },\n options: CallsOptions = {}\n): Promise<{ ok: true; webhook: RegisteredWebhook; secret: string } | { ok: false; error: string }> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error } = await callSdkApi(\"/api/v1/sdk/webhooks\", key, options, {\n method: \"POST\",\n body: webhook,\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data) || typeof data.id !== \"string\" || typeof data.secret !== \"string\") {\n return { ok: false, error: error ?? \"Unexpected response\" };\n }\n return {\n ok: true,\n secret: data.secret,\n webhook: {\n id: data.id,\n url: typeof data.url === \"string\" ? data.url : webhook.url,\n events: Array.isArray(data.events) ? data.events.filter((e): e is string => typeof e === \"string\") : [],\n },\n };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/** This project's webhooks, without secrets. `[]` on any failure. Never throws. */\nexport async function listWebhooks(options: CallsOptions = {}): Promise<RegisteredWebhook[]> {\n try {\n const key = callsKey(options);\n if (!key) return [];\n const { data, error } = await callSdkApi(\"/api/v1/sdk/webhooks\", key, options, { cache: \"no-store\" });\n if (error !== null || !isRecord(data) || !Array.isArray(data.webhooks)) return [];\n return data.webhooks.filter(isRecord).flatMap((w) =>\n typeof w.id === \"string\" && typeof w.url === \"string\"\n ? [{ id: w.id, url: w.url, events: Array.isArray(w.events) ? w.events.filter((e): e is string => typeof e === \"string\") : [] }]\n : []\n );\n } catch {\n return [];\n }\n}\n\n/** Remove one of this project's webhooks. Never throws. */\nexport async function removeWebhook(id: string, options: CallsOptions = {}): Promise<{ ok: true } | { ok: false; error: string }> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { error } = await callSdkApi(`/api/v1/sdk/webhooks/${encodeURIComponent(id)}`, key, options, {\n method: \"DELETE\",\n cache: \"no-store\",\n });\n return error === null ? { ok: true } : { ok: false, error };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Take a caller your key added off the line, with the phones kept for them —\n * e.g. when they leave your admin team. Someone the line's owner added →\n * `code: \"NOT_KEY_CALLER\"`. Never throws.\n */\nexport function removeLineCaller(email: string, options: CallsOptions = {}): Promise<RingNumberWriteResult> {\n return ringNumberWrite(\n `/api/v1/sdk/phone-lines/callers/${encodeURIComponent(email)}`,\n \"DELETE\",\n undefined,\n options,\n () => ({})\n );\n}\n\n/** One saved ring number, verified or not — the management view (#654). */\nexport interface RingNumber {\n id: string;\n /** E.164, `+91…`. */\n phone: string;\n label: string | null;\n position: number;\n /** Only verified numbers are ever rung. */\n verified: boolean;\n /** A test ring is out right now — poll until `verified`. */\n verifying: boolean;\n isDefault: boolean;\n}\n\nexport interface RingNumberList {\n operator: { name: string | null };\n /** Their account mobile — still what rings while nothing here is verified. */\n accountPhone: string | null;\n usingAccount: boolean;\n numbers: RingNumber[];\n}\n\nexport type RingNumberWriteResult<T = object> =\n | ({ ok: true } & T)\n | { ok: false; error: string; code?: string };\n\nfunction ringNumbersPath(rest = \"\"): string {\n return `/api/v1/sdk/phone-lines/operator/numbers${rest}`;\n}\n\nasync function ringNumberWrite<T extends Record<string, unknown>>(\n path: string,\n method: \"POST\" | \"PATCH\" | \"DELETE\",\n body: Record<string, unknown> | undefined,\n options: CallsOptions,\n pick: (data: Record<string, unknown>) => T\n): Promise<RingNumberWriteResult<T>> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(path, key, options, { method, body, cache: \"no-store\" });\n if (error !== null || !isRecord(data)) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n return { ok: true, ...pick(data) } as RingNumberWriteResult<T>;\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Every phone saved for one person on the project's line, unverified included\n * — for your \"manage my phones\" screen. Ring numbers are managed only from\n * your product; Glitchgrab shows them read-only. A refusal carries `code`\n * (`NOT_ON_LINE`), so \"not on the line\" reads differently from \"network down\".\n * Never throws.\n */\nexport async function listRingNumbers(\n operator: FollowUpOperator,\n options: CallsOptions = {}\n): Promise<RingNumberWriteResult<RingNumberList>> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const query = new URLSearchParams(\"email\" in operator ? { email: operator.email } : { phone: operator.phone });\n const { data, error, code } = await callSdkApi(ringNumbersPath(`?${query.toString()}`), key, options, {\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data)) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n const op = isRecord(data.operator) ? data.operator : {};\n return {\n ok: true,\n operator: { name: stringOrNull(op.name) },\n accountPhone: stringOrNull(data.accountPhone),\n usingAccount: data.usingAccount === true,\n numbers: Array.isArray(data.numbers)\n ? data.numbers\n .filter(isRecord)\n .filter((n) => typeof n.id === \"string\" && typeof n.phone === \"string\")\n .map((n) => ({\n id: n.id as string,\n phone: n.phone as string,\n label: stringOrNull(n.label),\n position: numberOrNull(n.position) ?? 0,\n verified: n.verified === true,\n verifying: n.verifying === true,\n isDefault: n.isDefault === true,\n }))\n : [],\n };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Save a phone for `operator` and place its test ring at once: the project's\n * line calls it and whoever holds it presses 1. Never rung for a call until\n * then; the line's owner is alerted when it is verified. Max 5 per person.\n * `testRing: \"failed\"` = saved but not rung — retry with {@link testRingNumber}.\n */\nexport function addRingNumber(\n operator: FollowUpOperator,\n number: { phone: string; label?: string },\n options: CallsOptions = {}\n): Promise<RingNumberWriteResult<{ id: string; testRing: \"ringing\" | \"failed\"; verifyError: string | null }>> {\n return ringNumberWrite(ringNumbersPath(), \"POST\", { operator, ...number }, options, (d) => ({\n id: typeof d.id === \"string\" ? d.id : \"\",\n testRing: d.testRing === \"failed\" ? \"failed\" : \"ringing\",\n verifyError: stringOrNull(d.verifyError),\n }));\n}\n\n/** Rename, make Default (verified only) or move one place up/down. Never throws. */\nexport function updateRingNumber(\n id: string,\n change: { label?: string | null; makeDefault?: true; move?: \"up\" | \"down\" },\n options: CallsOptions = {}\n): Promise<RingNumberWriteResult> {\n return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}`), \"PATCH\", change, options, () => ({}));\n}\n\n/** Remove one; the next verified number becomes Default. Never throws. */\nexport function removeRingNumber(id: string, options: CallsOptions = {}): Promise<RingNumberWriteResult> {\n return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}`), \"DELETE\", undefined, options, () => ({}));\n}\n\n/** Place the test ring again (6 per person, 30 per project an hour). Never throws. */\nexport function testRingNumber(\n id: string,\n options: CallsOptions = {}\n): Promise<RingNumberWriteResult<{ verified: boolean }>> {\n return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}/verify`), \"POST\", undefined, options, (d) => ({\n verified: d.verified === true,\n }));\n}\n\nexport type { ReportResult, ReportSeverity, ReportType } from \"./types\";\n","import type { RuntimeInfo } from \"./types\";\n\n/**\n * Runtime health at the moment of the crash. `deviceInfo` says what the machine\n * is; this says what shape it was in — a leak that ran the heap to its ceiling,\n * a 2G connection that starved a fetch, a bug that only shows up after ten\n * minutes on the page or on the fifth error in a row.\n */\n\nconst pageLoadedAt = Date.now();\nlet errorCount = 0;\n\n/** Bumped by every auto-captured error so a report says which one in the run it was. */\nexport function incrementErrorCount(): number {\n errorCount += 1;\n return errorCount;\n}\n\n/** Test-only — reset the per-session counter. */\nexport function resetErrorCount(): void {\n errorCount = 0;\n}\n\ninterface MemoryCapableWindow {\n memory?: { usedJSHeapSize?: number; jsHeapSizeLimit?: number };\n}\n\ninterface NetworkInformation {\n effectiveType?: string;\n downlink?: number;\n rtt?: number;\n saveData?: boolean;\n}\n\nconst toMb = (bytes: number | undefined): number | undefined =>\n typeof bytes === \"number\" ? Math.round((bytes / 1048576) * 10) / 10 : undefined;\n\nexport function captureRuntimeInfo(): RuntimeInfo | null {\n try {\n if (typeof window === \"undefined\") return null;\n\n // Chromium-only, and absent entirely under cross-origin isolation.\n const memory = (performance as unknown as MemoryCapableWindow)?.memory;\n // Chromium + Android only.\n const connection = (navigator as unknown as { connection?: NetworkInformation })?.connection;\n\n return {\n timeOnPageMs: Date.now() - pageLoadedAt,\n errorCount,\n visibility: typeof document !== \"undefined\" ? document.visibilityState : \"unknown\",\n ...(memory\n ? {\n jsHeapUsedMb: toMb(memory.usedJSHeapSize),\n jsHeapLimitMb: toMb(memory.jsHeapSizeLimit),\n }\n : {}),\n ...(connection\n ? {\n connectionType: connection.effectiveType,\n downlinkMbps: connection.downlink,\n rttMs: connection.rtt,\n saveData: connection.saveData,\n }\n : {}),\n };\n } catch {\n return null;\n }\n}\n","import type {\n CapturedContext,\n DeviceInfo,\n FeedbackPayload,\n FeedbackResult,\n LeadInput,\n LeadResult,\n ReportPayload,\n ReportResult,\n} from \"./types\";\n// The React-free subpath: utils.ts is also bundled into `glitchgrab/server`.\nimport { assistOffline, postJson, toAssistTurnResult } from \"@glitchgrab/report-ui/assist-response\";\nimport type { AssistTurnResult } from \"@glitchgrab/report-ui\";\nimport { getBreadcrumbs } from \"./breadcrumbs\";\nimport { captureRuntimeInfo } from \"./runtime\";\nimport { getAppContext, getRelease } from \"./app-context\";\n\nconst SENSITIVE_PARAMS = [\n \"token\", \"key\", \"secret\", \"password\", \"passwd\", \"auth\", \"authorization\",\n \"session\", \"sessionid\", \"session_id\", \"api_key\", \"apikey\", \"access_token\",\n \"refresh_token\", \"client_secret\", \"code\", \"state\", \"nonce\", \"credential\", \"private\",\n];\n\nexport function sanitizeUrl(url: string): string {\n try {\n const parsed = new URL(url);\n const params = new URLSearchParams(parsed.search);\n let modified = false;\n for (const key of Array.from(params.keys())) {\n if (SENSITIVE_PARAMS.some((s) => key.toLowerCase().includes(s))) {\n params.set(key, \"[REDACTED]\");\n modified = true;\n }\n }\n if (modified) parsed.search = params.toString();\n return parsed.toString();\n } catch {\n return url.split(\"?\")[0] ?? url;\n }\n}\n\nexport function captureDeviceInfo(): DeviceInfo | null {\n try {\n if (typeof window === \"undefined\") return null;\n return {\n screenWidth: screen.width,\n screenHeight: screen.height,\n viewportWidth: window.innerWidth,\n viewportHeight: window.innerHeight,\n platform: navigator.platform ?? \"unknown\",\n language: navigator.language ?? \"unknown\",\n online: navigator.onLine,\n colorScheme: window.matchMedia(\"(prefers-color-scheme: dark)\").matches ? \"dark\" : \"light\",\n devicePixelRatio: window.devicePixelRatio ?? 1,\n };\n } catch {\n return null;\n }\n}\n\nexport function captureContext(visitedPages: string[]): CapturedContext {\n try {\n return {\n url: sanitizeUrl(typeof window !== \"undefined\" ? window.location.href : \"\"),\n userAgent: typeof navigator !== \"undefined\" ? navigator.userAgent : \"\",\n timestamp: new Date().toISOString(),\n visitedPages: visitedPages.map(sanitizeUrl),\n breadcrumbs: getBreadcrumbs(),\n deviceInfo: captureDeviceInfo(),\n runtime: captureRuntimeInfo(),\n appContext: getAppContext(),\n release: getRelease(),\n };\n } catch {\n return {\n url: \"\",\n userAgent: \"\",\n timestamp: new Date().toISOString(),\n visitedPages: [],\n breadcrumbs: [],\n deviceInfo: null,\n runtime: null,\n appContext: {},\n };\n }\n}\n\n/**\n * Metadata every report carries regardless of how it was raised — release,\n * runtime health, and the host app's own context. One builder so a user-filed\n * report and an auto-captured crash describe the same world; the payload sites\n * only add what is specific to them.\n *\n * App context is prefixed so a key named `timestamp` or `status` can't quietly\n * overwrite a field the dashboard relies on.\n */\nexport function contextMetadata(context: CapturedContext): Record<string, string> {\n try {\n const runtime = context.runtime;\n const appContext = Object.entries(context.appContext ?? {}).reduce<Record<string, string>>(\n (acc, [key, value]) => {\n acc[`ctx_${key}`] = value;\n return acc;\n },\n {}\n );\n\n return {\n ...(context.release ? { release: context.release } : {}),\n ...(runtime\n ? {\n timeOnPageMs: String(runtime.timeOnPageMs),\n errorCount: String(runtime.errorCount),\n visibility: runtime.visibility,\n ...(runtime.jsHeapUsedMb !== undefined\n ? { jsHeapUsedMb: String(runtime.jsHeapUsedMb) }\n : {}),\n ...(runtime.jsHeapLimitMb !== undefined\n ? { jsHeapLimitMb: String(runtime.jsHeapLimitMb) }\n : {}),\n ...(runtime.connectionType ? { connectionType: runtime.connectionType } : {}),\n ...(runtime.downlinkMbps !== undefined\n ? { downlinkMbps: String(runtime.downlinkMbps) }\n : {}),\n ...(runtime.rttMs !== undefined ? { rttMs: String(runtime.rttMs) } : {}),\n ...(runtime.saveData !== undefined ? { saveData: String(runtime.saveData) } : {}),\n }\n : {}),\n ...appContext,\n };\n } catch {\n return {};\n }\n}\n\nexport const DEFAULT_BASE_URL = \"https://glitchgrab.dev\";\nconst MAX_RETRIES = 3;\nconst RETRY_BASE_MS = 500;\n\n/**\n * How long one report attempt may take before it is abandoned and retried.\n * The server files a report in a few seconds; on a flaky mobile network the\n * response can get lost and fetch then hangs ~30s before failing (#557). The\n * body share assumes a slow ~100 KB/s uplink, so a large screenshot is never\n * cut off mid-upload. A retry of a report the server already has is answered\n * with that report, not filed again. The final attempt runs without one.\n */\nexport function reportAttemptTimeoutMs(bodyLength: number): number {\n return 10_000 + Math.ceil(bodyLength / 100_000) * 1_000;\n}\n\n/**\n * Send a report and return the result.\n * Never throws — returns null on failure.\n */\nexport async function sendReport(\n payload: ReportPayload,\n baseUrl?: string\n): Promise<ReportResult | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/sdk/report`;\n const body = JSON.stringify(payload);\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${payload.token}`,\n };\n\n // keepalive has a 64KB body limit in browsers — skip it for large payloads (e.g., screenshots)\n const useKeepalive = body.length < 60_000;\n const timeoutMs = reportAttemptTimeoutMs(body.length);\n\n for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {\n // The last attempt has no deadline: a slow uplink still gets there, and\n // sendBeacon can't carry a screenshot-sized body if it gave up.\n const isLast = attempt === MAX_RETRIES - 1;\n const controller = !isLast && typeof AbortController === \"function\" ? new AbortController() : null;\n const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;\n try {\n const response = await fetch(url, {\n method: \"POST\",\n headers,\n body,\n ...(useKeepalive ? { keepalive: true } : {}),\n ...(controller ? { signal: controller.signal } : {}),\n });\n\n if (response.ok) {\n const envelope = (await response.json()) as {\n success: boolean;\n data?: Omit<ReportResult, \"success\">;\n };\n return { success: envelope.success, ...(envelope.data ?? {}) };\n }\n\n if (response.status >= 400 && response.status < 500 && response.status !== 429) {\n return null;\n }\n } catch {\n // Will retry\n } finally {\n if (timer) clearTimeout(timer);\n }\n\n if (attempt < MAX_RETRIES - 1) {\n await new Promise((resolve) =>\n setTimeout(resolve, RETRY_BASE_MS * Math.pow(2, attempt))\n );\n }\n }\n\n // Fallback: sendBeacon (fire-and-forget, no result)\n if (typeof navigator !== \"undefined\" && navigator.sendBeacon) {\n try {\n navigator.sendBeacon(url, new Blob([body], { type: \"application/json\" }));\n } catch {\n // Silently fail\n }\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\n/**\n * Save a star rating your end-user left about your app.\n * Never throws — returns null on failure.\n *\n * Unlike `sendReport`, this is always a deliberate user action with a dialog\n * open in front of them, so it retries on transient failure but skips the\n * sendBeacon fallback: the caller needs a real result to show a thank-you.\n */\nexport async function sendFeedback(\n payload: FeedbackPayload,\n baseUrl?: string\n): Promise<FeedbackResult | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/sdk/feedback`;\n const body = JSON.stringify(payload);\n\n for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {\n try {\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${payload.token}`,\n },\n body,\n });\n\n if (response.ok) {\n const envelope = (await response.json()) as {\n success: boolean;\n data?: Omit<FeedbackResult, \"success\">;\n };\n return { success: envelope.success, ...(envelope.data ?? {}) };\n }\n\n // 4xx other than rate limiting is a caller mistake — retrying won't help.\n if (response.status >= 400 && response.status < 500 && response.status !== 429) {\n return null;\n }\n } catch {\n // Will retry\n }\n\n if (attempt < MAX_RETRIES - 1) {\n await new Promise((resolve) =>\n setTimeout(resolve, RETRY_BASE_MS * Math.pow(2, attempt))\n );\n }\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\n/**\n * POST a lead (#534). Retries transient failures like {@link sendFeedback}; a\n * 4xx other than 429 is a caller mistake and returns null at once.\n */\nexport async function sendLead(\n token: string,\n lead: LeadInput & { pageUrl?: string },\n baseUrl?: string\n): Promise<LeadResult | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/sdk/leads`;\n const body = JSON.stringify(lead);\n\n for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {\n try {\n const response = await fetch(url, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\", Authorization: `Bearer ${token}` },\n body,\n });\n\n if (response.ok) {\n const envelope = (await response.json()) as {\n success: boolean;\n data?: { leadId?: string; created?: boolean };\n };\n return { success: envelope.success, id: envelope.data?.leadId, created: envelope.data?.created };\n }\n\n if (response.status >= 400 && response.status < 500 && response.status !== 429) {\n return null;\n }\n } catch {\n // Will retry\n }\n\n if (attempt < MAX_RETRIES - 1) {\n await new Promise((resolve) => setTimeout(resolve, RETRY_BASE_MS * Math.pow(2, attempt)));\n }\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\nexport interface EnhanceContext {\n url?: string;\n visitedPages?: string[];\n breadcrumbs?: Array<{ type: string; message: string }>;\n}\n\n/**\n * The signed-in user as the host's server vouched for them (`signSession`).\n *\n * Rides in the request BODY, never a header: every token route sets its own\n * CORS allow-list of headers, so a new header would fail preflight on all of\n * them. Omitted entirely when there is no signature — an unsigned id proves\n * nothing, and the server reads its absence as \"unsigned\".\n */\nexport interface SignedIdentity {\n userId?: string | null;\n signature?: string | null;\n}\n\n/** `{ userId, signature }` to spread into a JSON body, or nothing. */\nexport function signedFields(identity?: SignedIdentity | null): { userId?: string; signature?: string } {\n if (!identity?.signature || !identity.userId) return {};\n return { userId: identity.userId, signature: identity.signature };\n}\n\n/**\n * Polish raw description text via the Glitchgrab AI enhance endpoint.\n * Returns the polished text on success, or the original text on any failure.\n * Never throws.\n */\nexport async function enhanceText(\n text: string,\n token: string,\n baseUrl?: string,\n screenshot?: string | null,\n context?: EnhanceContext | null,\n identity?: SignedIdentity | null\n): Promise<string> {\n try {\n return await enhanceTextOrThrow(text, token, baseUrl, screenshot, context, identity);\n } catch {\n return text;\n }\n}\n\n/**\n * `enhanceText` for the report dialog: throws an Error whose message the\n * reporter can read, so the button can say why nothing changed (#573) instead\n * of silently handing the text back.\n */\nexport async function enhanceTextOrThrow(\n text: string,\n token: string,\n baseUrl?: string,\n screenshot?: string | null,\n context?: EnhanceContext | null,\n identity?: SignedIdentity | null\n): Promise<string> {\n const trimmed = text.trim();\n if (!trimmed) return text;\n const unavailable = \"AI enhance isn't available right now — send your report as written.\";\n let response: Response;\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/ai/enhance-text`;\n response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({\n text: trimmed,\n ...(screenshot ? { screenshot } : {}),\n ...(context ? { context } : {}),\n ...signedFields(identity),\n }),\n });\n } catch {\n throw new Error(unavailable);\n }\n if (response.status === 429) {\n throw new Error(\"Too many AI enhance tries — wait a while, or send your report as written.\");\n }\n const envelope = (await response.json().catch(() => null)) as {\n success: boolean;\n data?: { text?: string };\n error?: string;\n } | null;\n if (response.ok && envelope?.success && typeof envelope.data?.text === \"string\") {\n return envelope.data.text;\n }\n // 400 is about the reporter's own text (\"Text too long — max 5000\n // characters\") — worth showing as is. Everything else is ours.\n if (response.status === 400 && typeof envelope?.error === \"string\") {\n throw new Error(envelope.error);\n }\n throw new Error(unavailable);\n}\n\n/**\n * \"Is this already filed?\" for the report dialog's plain form. Asked when Send\n * is pressed; a match lets the reporter add to the open issue instead of\n * opening a second one.\n *\n * `[]` when the project has it off, `null` on any failure — the dialog sends\n * straight away on either. Never throws.\n */\nexport async function findSimilarIssues(\n description: string,\n token: string,\n baseUrl?: string,\n identity?: SignedIdentity | null,\n /** Attached files as text (#563) — the server reads a short summary of them. */\n files?: { name: string; content: string }[],\n /** The assistant chat the draft came from (#697) — its declined issues are left out. */\n conversationId?: string\n): Promise<{ number: number; title: string; url: string; status?: string }[] | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/reports/similar`;\n // No `keepalive`: with files the body can pass its 64 KB cap, and the\n // request would fail without a word.\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({\n text: description,\n ...(files?.length ? { files } : {}),\n ...(conversationId ? { conversationId } : {}),\n ...signedFields(identity),\n }),\n });\n const envelope = (await response.json().catch(() => null)) as {\n success?: boolean;\n data?: { enabled?: boolean; matches?: { number: number; title: string; url: string; status?: string }[] };\n } | null;\n if (!response.ok || !envelope?.success) return null;\n if (!envelope.data?.enabled) return [];\n return Array.isArray(envelope.data.matches) ? envelope.data.matches : [];\n } catch {\n return null;\n }\n}\n\n/**\n * The text inside a PDF the reporter attached (#563), so the assistant and the\n * duplicate check can read it. Called once per PDF, when it is attached.\n *\n * `\"\"` for a PDF with no text layer (a scan); `null` when the project has the\n * assistant off, the file is too big, or anything failed. Never throws.\n */\nexport async function readAttachment(\n file: { name: string; dataUrl: string },\n token: string,\n baseUrl?: string,\n identity?: SignedIdentity | null\n): Promise<string | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/ai/read-file`;\n // No `keepalive`: a PDF is far past its 64 KB body cap.\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({ name: file.name, data: file.dataUrl, ...signedFields(identity) }),\n });\n const envelope = (await response.json().catch(() => null)) as {\n success?: boolean;\n data?: { enabled?: boolean; text?: string };\n } | null;\n if (!response.ok || !envelope?.success || !envelope.data?.enabled) return null;\n return typeof envelope.data.text === \"string\" ? envelope.data.text : null;\n } catch {\n return null;\n }\n}\n\n/**\n * One turn of the AI report assistant (#330).\n *\n * Same never-throw contract as everything else in the SDK: a failure comes back\n * as `degraded` and the dialog quietly falls back to the plain form. An\n * end-user of the host app must never see a Glitchgrab error, and must never be\n * blocked from filing a bug because a model was busy.\n */\nexport async function assistTurn(\n params: {\n messages: { role: \"user\" | \"assistant\"; content: string }[];\n conversationId: string | null;\n screenshot?: string | null;\n context?: Record<string, unknown> | null;\n /**\n * Who the host app says is logged in, from the provider's `session` prop.\n * Stored against the conversation so a chat and the report it produced\n * agree on who filed — one SDK token covers every end user of the host app,\n * so without this every stranger's chat looks like the same person.\n */\n reporter?: {\n key?: string | null;\n name?: string | null;\n email?: string | null;\n /** `signSession()` output for `key`; absent when the host did not sign. */\n signature?: string | null;\n };\n },\n token: string,\n baseUrl?: string,\n /** Body upload progress, 0–1 — the sheet's \"Sending…\" line (#646). */\n onUploadProgress?: (fraction: number) => void\n): Promise<AssistTurnResult> {\n const offline = assistOffline();\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/ai/report-chat`;\n const response = await postJson(url, params, {\n headers: { Authorization: `Bearer ${token}` },\n onUploadProgress,\n });\n // One mapping for every host (packages/report-ui/src/assist-response.ts):\n // the server's degrade reason passes through, and nothing the sheet renders\n // is dropped.\n return toAssistTurnResult(response.ok, response.body);\n } catch {\n return offline;\n }\n}\n\n/**\n * Send an audio Blob to the Glitchgrab STT proxy (which calls Sarvam).\n * Returns the transcript string, or \"\" on any failure. Never throws.\n */\nexport async function transcribeAudio(\n blob: Blob,\n token: string,\n baseUrl?: string,\n identity?: SignedIdentity | null\n): Promise<string> {\n try {\n if (blob.size === 0) return \"\";\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/sdk/stt`;\n const form = new FormData();\n form.append(\"file\", blob, \"audio.webm\");\n const signed = signedFields(identity);\n if (signed.userId && signed.signature) {\n form.append(\"userId\", signed.userId);\n form.append(\"signature\", signed.signature);\n }\n const response = await fetch(url, {\n method: \"POST\",\n headers: { Authorization: `Bearer ${token}` },\n body: form,\n });\n if (!response.ok) return \"\";\n const envelope = (await response.json()) as {\n success: boolean;\n data?: { transcript?: string };\n };\n if (envelope?.success && typeof envelope.data?.transcript === \"string\") {\n return envelope.data.transcript;\n }\n return \"\";\n } catch {\n return \"\";\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA,yBAA2B;;;ACV3B,IAAM,eAAe,KAAK,IAAI;;;AC8HvB,IAAM,mBAAmB;AAChC,IAAM,cAAc;AACpB,IAAM,gBAAgB;AAUf,SAAS,uBAAuB,YAA4B;AACjE,SAAO,MAAS,KAAK,KAAK,aAAa,GAAO,IAAI;AACpD;AAMA,eAAs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AAE;AACJ;","names":["_a","_b","_c"]}
|
|
1
|
+
{"version":3,"sources":["../src/server.ts","../src/runtime.ts","../src/utils.ts"],"sourcesContent":["/**\n * Server-side reporting — `import { reportServerError } from \"glitchgrab/server\"`.\n *\n * The rest of this SDK is `\"use client\"`: it hooks `window.onerror`, walks the\n * DOM for a screenshot, and needs a mounted provider for its token. None of\n * that exists in a cron job at 3am, an API route handler, or a queue worker —\n * which is precisely where the failures nobody is watching happen. A WhatsApp\n * send Meta rejects, an SMTP timeout, a nightly digest that throws: no tab is\n * open, so the browser SDK never sees them and no issue is ever filed.\n *\n * This entry has no `\"use client\"`, touches no DOM API, and imports no React.\n * It POSTs to the same `/api/v1/sdk/report` endpoint the browser uses, with\n * `source: \"SDK_AUTO\"` so the server-side dedup applies (24h per signature,\n * 7 days once an issue exists) — otherwise a cron failing hourly would open\n * twenty-four identical issues a day.\n *\n * Like the rest of the SDK it never throws: a reporting failure must not turn\n * a handled error into an unhandled one.\n */\nimport { createHmac } from \"node:crypto\";\nimport type { ReportPayload, ReportResult, ReportSeverity, ReportType } from \"./types\";\nimport { DEFAULT_BASE_URL, sendReport } from \"./utils\";\n\n/** Who to attribute the report to. A server has no signed-in user, so this is the job or service. */\nexport interface ServerReporterIdentity {\n /** Stable id — the cron name, the service name. Defaults to `\"server\"`. */\n id?: string;\n /** Display name shown on the report. Defaults to `\"Server\"`. */\n name?: string;\n email?: string;\n phone?: string;\n}\n\n/** Defaults applied to every server report. Set once at boot with {@link configureServerReporter}. */\nexport interface ServerReporterConfig {\n /** Repo token (`gg_…`). Falls back to `process.env.GLITCHGRAB_TOKEN`. */\n token?: string;\n /** Override the API host. Falls back to `process.env.GLITCHGRAB_BASE_URL`, then `https://glitchgrab.dev`. */\n baseUrl?: string;\n /**\n * Where this ran — `\"cron/attendance-prompts\"`, `\"worker/email-queue\"`.\n *\n * It is the grouping key, not a link: it is sent as the report's `pageUrl`\n * so two different jobs throwing the same `\"Timeout\"` stay two issues\n * instead of collapsing into one.\n */\n context?: string;\n /** Merged into every report's metadata. Good place for region, deployment id, service name. */\n metadata?: Record<string, string>;\n reporter?: ServerReporterIdentity;\n /**\n * Report from `NODE_ENV=development` too. Off by default, matching the\n * browser SDK — a laptop crashing its way through a refactor should not file\n * issues. Unlike the browser there is no `Origin` header for the API to\n * recognise, so nothing else stops a dev machine from filing.\n */\n enableInDevelopment?: boolean;\n}\n\n/** Per-call options. Anything set here wins over {@link configureServerReporter}. */\nexport interface ServerReportOptions extends ServerReporterConfig {\n /** Issue type. Defaults to `BUG`. */\n type?: ReportType;\n /** Rendered as a `severity:<value>` label on the GitHub issue. */\n severity?: ReportSeverity;\n /** Extra prose for the issue body — what the job was doing, which record it was on. */\n description?: string;\n /**\n * A real request URL, when there is one (an API route handler). Overrides\n * the synthetic URL built from `context`.\n */\n pageUrl?: string;\n /**\n * What the job printed before it failed — lines or one string.\n *\n * Attached to the issue as a `.log` file: the first part is previewed in the\n * issue body, the whole file is kept for download. A stack says where it\n * broke; the log lines before it are usually what says why. Only the last\n * {@link MAX_LOG_CHARS} characters are sent.\n */\n logs?: string | string[];\n}\n\n/** The newest end of a log is the useful end. Well under the API's per-file cap. */\nconst MAX_LOG_CHARS = 200_000;\n\nconst DEV_BLOCKED_MESSAGE = \"development\";\n\n/**\n * Node's `process`, as much of it as this file uses.\n *\n * `src/global.d.ts` declares a browser-shaped `process` with nothing but\n * `env`, and that is deliberate — the client bundle must not be able to reach\n * for Node APIs. So this entry reads the real one off `globalThis` behind its\n * own narrow type instead of pulling `@types/node` into the package.\n */\ninterface NodeProcessLike {\n env?: Record<string, string | undefined>;\n version?: string;\n on?(event: string, listener: (arg: never) => void): unknown;\n off?(event: string, listener: (arg: never) => void): unknown;\n listenerCount?(event: string): number;\n}\n\nfunction nodeProcess(): NodeProcessLike | undefined {\n try {\n const candidate = (globalThis as { process?: NodeProcessLike }).process;\n return candidate && typeof candidate === \"object\" ? candidate : undefined;\n } catch {\n return undefined;\n }\n}\n\nlet defaults: ServerReporterConfig = {};\n\n/**\n * Set the token and the defaults once, at module load, instead of passing them\n * to every call site. A later call's own options still win.\n */\nexport function configureServerReporter(config: ServerReporterConfig): void {\n defaults = { ...defaults, ...config };\n}\n\n/** Test-only — drop everything {@link configureServerReporter} set. */\nexport function resetServerReporter(): void {\n defaults = {};\n}\n\nfunction readEnv(name: string): string | undefined {\n try {\n return nodeProcess()?.env?.[name];\n } catch {\n return undefined;\n }\n}\n\n/**\n * Message and stack out of whatever was thrown.\n *\n * A thrown string, a rejected `{ code, message }`, an `undefined` from a\n * `Promise.reject()` — all of it reaches here, and all of it must produce a\n * non-empty message: the API's dedup signature is `null` without one, and a\n * report with no signature is a fresh issue every single time.\n */\nfunction describeError(error: unknown): { message: string; stack?: string } {\n try {\n if (error instanceof Error) {\n return {\n message: error.message || error.name || \"Unknown server error\",\n stack: error.stack,\n };\n }\n if (typeof error === \"string\") {\n return { message: error || \"Unknown server error\" };\n }\n if (error && typeof error === \"object\") {\n const maybe = error as { message?: unknown; stack?: unknown };\n const message = typeof maybe.message === \"string\" ? maybe.message : JSON.stringify(error);\n return {\n message: message || \"Unknown server error\",\n stack: typeof maybe.stack === \"string\" ? maybe.stack : undefined,\n };\n }\n return { message: String(error ?? \"\") || \"Unknown server error\" };\n } catch {\n return { message: \"Unknown server error\" };\n }\n}\n\n/**\n * `pageUrl` doubles as the dedup grouping key, so a job needs one even though\n * no page was involved. `server://` keeps it obviously synthetic — and keeps it\n * clear of the API's localhost check, which only matches http(s) and would\n * otherwise swallow the report with a 200 and create nothing.\n */\nfunction contextUrl(context: string | undefined): string | undefined {\n if (!context) return undefined;\n return `server://${context.replace(/^\\/+/, \"\")}`;\n}\n\n/**\n * `logs` as the API's text-attachment shape: `[{ name, dataUrl }]`, JSON in\n * `metadata.attachments` — the same slot the report dialog uses for files, so\n * the endpoint needs nothing new. Null when there is nothing to attach.\n */\nfunction logsAttachment(logs: string | string[] | undefined, context: string | undefined): string | null {\n try {\n const text = Array.isArray(logs) ? logs.join(\"\\n\") : logs;\n if (!text || !text.trim()) return null;\n const tail = text.length > MAX_LOG_CHARS ? text.slice(-MAX_LOG_CHARS) : text;\n\n // UTF-8 → base64 without Buffer: this entry stays free of Node typings.\n const bytes = new TextEncoder().encode(tail);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i += 0x8000) {\n binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));\n }\n const name = `${(context ?? \"server\").replace(/[^a-z0-9._-]+/gi, \"-\").replace(/^-+|-+$/g, \"\") || \"server\"}.log`;\n return JSON.stringify([{ name, dataUrl: `data:text/plain;base64,${btoa(binary)}` }]);\n } catch {\n return null;\n }\n}\n\nfunction runtimeMetadata(): Record<string, string> {\n try {\n const proc = nodeProcess();\n if (!proc) return { runtime: \"server\" };\n const region = proc.env?.VERCEL_REGION;\n const deploymentEnv = proc.env?.VERCEL_ENV;\n return {\n runtime: \"node\",\n ...(proc.version ? { nodeVersion: proc.version } : {}),\n ...(region ? { region } : {}),\n ...(deploymentEnv ? { deploymentEnv } : {}),\n };\n } catch {\n return { runtime: \"server\" };\n }\n}\n\n/**\n * File a server-side error as a GitHub issue.\n *\n * Await it. On a serverless platform the function may be frozen the moment your\n * handler returns, and a floating promise dies with it — the report never\n * leaves the machine.\n *\n * ```ts\n * try {\n * await sendDigest();\n * } catch (error) {\n * await reportServerError(error, { context: \"cron/daily-digest\" });\n * throw error;\n * }\n * ```\n *\n * Returns `null` when nothing was filed — no token, development mode, or the\n * API refused it. Never throws.\n */\nexport async function reportServerError(\n error: unknown,\n options: ServerReportOptions = {}\n): Promise<ReportResult | null> {\n try {\n const token = options.token ?? defaults.token ?? readEnv(\"GLITCHGRAB_TOKEN\");\n if (!token) return null;\n\n const enableInDevelopment = options.enableInDevelopment ?? defaults.enableInDevelopment ?? false;\n if (!enableInDevelopment && readEnv(\"NODE_ENV\") === DEV_BLOCKED_MESSAGE) return null;\n\n const { message, stack } = describeError(error);\n const context = options.context ?? defaults.context;\n const reporter = { ...defaults.reporter, ...options.reporter };\n\n const payload: ReportPayload = {\n token,\n source: \"SDK_AUTO\",\n type: options.type ?? \"BUG\",\n errorMessage: message,\n ...(stack ? { errorStack: stack } : {}),\n ...(options.description ? { description: options.description } : {}),\n ...(options.pageUrl ?? contextUrl(context)\n ? { pageUrl: options.pageUrl ?? contextUrl(context) }\n : {}),\n metadata: {\n ...runtimeMetadata(),\n ...defaults.metadata,\n ...options.metadata,\n ...(context ? { serverContext: context } : {}),\n ...(options.severity ? { severity: options.severity } : {}),\n sessionUserId: reporter.id ?? context ?? \"server\",\n sessionUserName: reporter.name ?? \"Server\",\n ...(reporter.email ? { sessionUserEmail: reporter.email } : {}),\n ...(reporter.phone ? { sessionUserPhone: reporter.phone } : {}),\n },\n };\n const attachments = logsAttachment(options.logs, context);\n if (attachments && payload.metadata) payload.metadata.attachments = attachments;\n\n return await sendReport(payload, options.baseUrl ?? defaults.baseUrl ?? readEnv(\"GLITCHGRAB_BASE_URL\"));\n } catch {\n return null;\n }\n}\n\n/** Options for {@link captureServerErrors}. */\nexport interface CaptureServerErrorsOptions extends ServerReportOptions {\n /**\n * Also report rejected promises nobody handled. Default `true`.\n *\n * Node crashes the process on an unhandled rejection — but only while no\n * `unhandledRejection` listener exists. Attaching one silently converts a\n * crash into a warning, so this re-throws afterwards to put the default\n * behaviour back, unless the host app has its own listener (then it is theirs\n * to decide).\n */\n captureUnhandledRejections?: boolean;\n}\n\n/**\n * Report every uncaught exception and unhandled rejection in this process.\n *\n * Call it once at boot — `instrumentation.ts` in a Next.js app. Returns a\n * function that removes the listeners again.\n *\n * Uncaught exceptions use `uncaughtExceptionMonitor`, which observes without\n * taking over: the process still crashes exactly as it would have. A reporter\n * that keeps a broken process alive is worse than no reporter.\n */\nexport function captureServerErrors(options: CaptureServerErrorsOptions = {}): () => void {\n try {\n const proc = nodeProcess();\n if (!proc || typeof proc.on !== \"function\") return () => {};\n\n const onException = (error: unknown) => {\n void reportServerError(error, { ...options, context: options.context ?? \"uncaughtException\" });\n };\n\n const onRejection = (reason: unknown) => {\n void reportServerError(reason, {\n ...options,\n context: options.context ?? \"unhandledRejection\",\n });\n\n // Ours is the only listener, so suppressing the crash was never the\n // host app's choice. Hand the default behaviour back.\n if (proc.listenerCount?.(\"unhandledRejection\") === 1) {\n setTimeout(() => {\n throw reason;\n }, 0);\n }\n };\n\n proc.on(\"uncaughtExceptionMonitor\", onException as (arg: never) => void);\n\n const captureRejections = options.captureUnhandledRejections ?? true;\n if (captureRejections) proc.on(\"unhandledRejection\", onRejection as (arg: never) => void);\n\n return () => {\n try {\n proc.off?.(\"uncaughtExceptionMonitor\", onException as (arg: never) => void);\n if (captureRejections) {\n proc.off?.(\"unhandledRejection\", onRejection as (arg: never) => void);\n }\n } catch {\n // Nothing to undo.\n }\n };\n } catch {\n return () => {};\n }\n}\n\n/** Options for {@link signSession}. */\nexport interface SignSessionOptions {\n /** Your user's id — the same value you pass as `session.userId`. */\n userId: string;\n /** The repo's signing secret (`ggs_…`). Falls back to `process.env.GLITCHGRAB_SIGNING_SECRET`. */\n secret?: string;\n /** How long the signature stays valid, in seconds. Default one day. */\n ttlSeconds?: number;\n /** Clock override in epoch ms. For tests. */\n now?: number;\n}\n\n/**\n * Vouch for the logged-in user so the SDK's AI features work when the project\n * owner turned on signed users.\n *\n * The `gg_` token ships in your JavaScript, so on its own it proves nothing\n * about who is calling. Your server holds the signing secret; call this where\n * you build the `session` prop and pass the result as `session.signature`.\n * Render it per request so it refreshes before `ttlSeconds` runs out.\n *\n * Format: `v1.<exp>.<base64url HMAC-SHA256(secret, \"v1.<exp>.<userId>\")>`.\n *\n * Unlike the rest of this SDK it throws: it runs in your own server code, where\n * a missing secret is a deploy mistake you want to see — not a user who quietly\n * loses the AI features.\n */\nexport function signSession(options: SignSessionOptions): string {\n const userId = options?.userId;\n if (typeof userId !== \"string\" || userId.length === 0) {\n throw new Error(\"glitchgrab signSession: userId is required\");\n }\n const env = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env;\n const secret = options.secret || env?.GLITCHGRAB_SIGNING_SECRET;\n if (!secret) {\n throw new Error(\n \"glitchgrab signSession: no signing secret — pass `secret` or set GLITCHGRAB_SIGNING_SECRET\"\n );\n }\n const ttl = options.ttlSeconds ?? 86_400;\n if (!Number.isFinite(ttl) || ttl <= 0) {\n throw new Error(\"glitchgrab signSession: ttlSeconds must be a positive number\");\n }\n const exp = Math.floor((options.now ?? Date.now()) / 1000) + Math.floor(ttl);\n const sig = createHmac(\"sha256\", secret).update(`v1.${exp}.${userId}`).digest(\"base64url\");\n return `v1.${exp}.${sig}`;\n}\n\n// ── Guides ───────────────────────────────────────────────────────────────────\n//\n// A team writes its how-tos in Glitchgrab and renders them on its own site from\n// `GET /api/v1/sdk/guides` (#370). Every site needs the same client — token,\n// caching, never failing the page, shape checks — so it lives here instead of\n// being rewritten, slightly differently, in each product (#409).\n\n/** One guide as the list returns it — no body. Dates are ISO strings: this came over JSON. */\nexport interface GuideSummary {\n slug: string;\n title: string;\n /** One line saying what the guide answers. */\n summary: string;\n category: string | null;\n /** The order set in Glitchgrab. The list already arrives sorted by it. */\n orderIndex: number;\n /** Always `\"PUBLISHED\"` with a `gg_` token; only the project's `ggc_` server key sees drafts. */\n status: \"DRAFT\" | \"PUBLISHED\";\n /**\n * `\"INTERNAL\"` = a team-only guide. Only the `ggc_` server key sees those,\n * and only when it asks with `audience: \"internal\"` or `\"all\"`.\n */\n audience: \"PUBLIC\" | \"INTERNAL\";\n /** When someone last re-checked the steps against the product. */\n lastVerifiedAt: string | null;\n updatedAt: string;\n /**\n * Where this guide lives on your site: its own link, else\n * `<guides base URL>/<slug>`, else null when the repo sets neither.\n */\n link: string | null;\n}\n\n/** One guide with its body. */\nexport interface Guide extends GuideSummary {\n /** The markdown as written. */\n content: string;\n /**\n * `content` rendered by Glitchgrab: raw HTML in the markdown arrives as text,\n * only allowlisted attributes survive, and links keep only http(s), mailto\n * and relative targets. Headings carry `id=\"guide-<heading text>\"`, for a\n * table of contents or a link to a single step.\n *\n * Safe to render as-is. On a domain that holds your users' sessions, a second\n * sanitize pass of your own is cheap defence in depth — this SDK ships no\n * sanitizer so it stays dependency-free.\n */\n html: string;\n}\n\n/** Options for {@link listGuides} and {@link getGuide}. */\nexport interface GuideReadOptions {\n /**\n * Repo token (`gg_…`). Falls back to {@link configureServerReporter}'s token,\n * then `GLITCHGRAB_TOKEN`, then `NEXT_PUBLIC_GLITCHGRAB_TOKEN` — the token is\n * public, so the one your provider already uses is fine.\n *\n * Never pass the `ggc_` server key for a public page: it reads drafts too, and client calls.\n */\n token?: string;\n /** Override the API host. Falls back to `GLITCHGRAB_BASE_URL`, then `https://glitchgrab.dev`. */\n baseUrl?: string;\n /**\n * Seconds Next.js may cache the response. Default 300; `0` reads fresh on\n * every request. The route is `force-dynamic`, so without this nothing is\n * cached at all. Ignored outside Next.js.\n */\n revalidate?: number;\n /** Give up and return empty after this many ms. Default 5000 — a slow Glitchgrab must not hang your render. */\n timeoutMs?: number;\n /**\n * Which guides: `\"public\"` (default), `\"internal\"` (team-only) or `\"all\"`.\n * Anything but `\"public\"` needs the `ggc_` server key — for your own admin\n * panel, behind your own login. Never on a page a customer can open.\n */\n audience?: \"public\" | \"internal\" | \"all\";\n}\n\nconst GUIDES_REVALIDATE_SECONDS = 300;\nconst SDK_API_TIMEOUT_MS = 5000;\n\n/** `RequestInit` plus Next.js's cache option, without pulling Next's types into this entry. */\ntype NextFetchInit = RequestInit & { next?: { revalidate?: number } };\n\ninterface SdkApiResult {\n /** `data` from a successful response, else null. */\n data: unknown;\n /** Null on success; the route's own `error` when it sent one, so a caller can show why. */\n error: string | null;\n /** The route's machine-readable `code`, when it sent one (e.g. ring-number errors, #654). */\n code?: string;\n}\n\n/**\n * One request to a Glitchgrab SDK route. Never throws: a refusal, a timeout, a\n * network failure and unreadable JSON all come back as `error`.\n */\nasync function callSdkApi(\n path: string,\n token: string,\n options: { baseUrl?: string; timeoutMs?: number },\n init: { method?: \"GET\" | \"POST\" | \"PUT\" | \"PATCH\" | \"DELETE\"; body?: unknown; cache: { revalidate: number } | \"no-store\" }\n): Promise<SdkApiResult> {\n const baseUrl = (\n options.baseUrl ??\n defaults.baseUrl ??\n readEnv(\"GLITCHGRAB_BASE_URL\") ??\n DEFAULT_BASE_URL\n ).replace(/\\/+$/, \"\");\n\n const controller = typeof AbortController === \"function\" ? new AbortController() : undefined;\n const timer = controller\n ? setTimeout(() => controller.abort(), options.timeoutMs ?? SDK_API_TIMEOUT_MS)\n : undefined;\n\n try {\n // A FormData body (KYC certificates) sets its own multipart boundary.\n const multipart = typeof FormData !== \"undefined\" && init.body instanceof FormData;\n const request: NextFetchInit = {\n method: init.method ?? \"GET\",\n headers: {\n Authorization: `Bearer ${token}`,\n ...(init.body === undefined || multipart ? {} : { \"Content-Type\": \"application/json\" }),\n },\n ...(init.body === undefined\n ? {}\n : { body: multipart ? (init.body as FormData) : JSON.stringify(init.body) }),\n ...(init.cache === \"no-store\" ? { cache: \"no-store\" as const } : { next: { revalidate: init.cache.revalidate } }),\n ...(controller ? { signal: controller.signal } : {}),\n };\n const response = await fetch(`${baseUrl}${path}`, request);\n const body = (await response.json().catch(() => null)) as\n | { success?: unknown; data?: unknown; error?: unknown; code?: unknown }\n | null;\n if (!response.ok || body?.success !== true) {\n return {\n data: null,\n error: typeof body?.error === \"string\" ? body.error : `HTTP ${response.status}`,\n ...(typeof body?.code === \"string\" ? { code: body.code } : {}),\n };\n }\n return { data: body.data ?? null, error: null };\n } catch {\n return { data: null, error: \"Could not reach Glitchgrab\" };\n } finally {\n if (timer) clearTimeout(timer);\n }\n}\n\n/** `data` from the guides route, or null on any failure — no token, non-2xx, timeout, bad JSON. */\nasync function readGuidesApi(query: string, options: GuideReadOptions): Promise<unknown> {\n const token =\n options.token ??\n defaults.token ??\n readEnv(\"GLITCHGRAB_TOKEN\") ??\n readEnv(\"NEXT_PUBLIC_GLITCHGRAB_TOKEN\");\n if (!token) return null;\n\n const revalidate =\n typeof options.revalidate === \"number\" && Number.isFinite(options.revalidate) && options.revalidate >= 0\n ? options.revalidate\n : GUIDES_REVALIDATE_SECONDS;\n\n const result = await callSdkApi(`/api/v1/sdk/guides${query}`, token, options, { cache: { revalidate } });\n return result.data;\n}\n\n/**\n * A guide summary out of whatever the API sent, or null when it is not one.\n * Only slug, title and summary are required — a page cannot render without\n * them. The rest default, so the returned type is honest either way.\n */\nfunction readGuideSummary(value: unknown): GuideSummary | null {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const guide = value as Record<string, unknown>;\n if (\n typeof guide.slug !== \"string\" ||\n typeof guide.title !== \"string\" ||\n typeof guide.summary !== \"string\"\n ) {\n return null;\n }\n return {\n slug: guide.slug,\n title: guide.title,\n summary: guide.summary,\n category: typeof guide.category === \"string\" ? guide.category : null,\n orderIndex: typeof guide.orderIndex === \"number\" ? guide.orderIndex : 0,\n status: guide.status === \"DRAFT\" ? \"DRAFT\" : \"PUBLISHED\",\n audience: guide.audience === \"INTERNAL\" ? \"INTERNAL\" : \"PUBLIC\",\n lastVerifiedAt: typeof guide.lastVerifiedAt === \"string\" ? guide.lastVerifiedAt : null,\n updatedAt: typeof guide.updatedAt === \"string\" ? guide.updatedAt : \"\",\n link: typeof guide.link === \"string\" ? guide.link : null,\n };\n}\n\n/** `audience=` for the guides route; nothing for the default, so old servers see the same URL. */\nfunction audienceQuery(options: GuideReadOptions, joiner: \"?\" | \"&\"): string {\n const audience = options.audience;\n return audience === \"internal\" || audience === \"all\" ? `${joiner}audience=${audience}` : \"\";\n}\n\n/**\n * Every published guide for the token's repo, in the order set in Glitchgrab.\n * No bodies — this is the index page.\n *\n * ```ts\n * // app/guides/page.tsx\n * import { listGuides } from \"glitchgrab/server\";\n *\n * export default async function GuidesPage() {\n * const guides = await listGuides();\n * return guides.map((g) => <a key={g.slug} href={`/guides/${g.slug}`}>{g.title}</a>);\n * }\n * ```\n *\n * Returns `[]` on any failure. Never throws: a Glitchgrab outage leaves the\n * page empty instead of turning it into a 500.\n */\nexport async function listGuides(options: GuideReadOptions = {}): Promise<GuideSummary[]> {\n try {\n const data = await readGuidesApi(audienceQuery(options, \"?\"), options);\n if (!Array.isArray(data)) return [];\n return data.map(readGuideSummary).filter((guide): guide is GuideSummary => guide !== null);\n } catch {\n return [];\n }\n}\n\n/**\n * One published guide with its markdown and rendered HTML.\n *\n * Returns `null` when there is no such guide and on any failure — call\n * `notFound()` on it. Never throws.\n */\nexport async function getGuide(slug: string, options: GuideReadOptions = {}): Promise<Guide | null> {\n try {\n const wanted = typeof slug === \"string\" ? slug.trim() : \"\";\n if (!wanted) return null;\n\n const data = await readGuidesApi(`?slug=${encodeURIComponent(wanted)}${audienceQuery(options, \"&\")}`, options);\n const summary = readGuideSummary(data);\n const body = data as { content?: unknown; html?: unknown } | null;\n if (!summary || typeof body?.html !== \"string\") return null;\n\n return {\n ...summary,\n content: typeof body.content === \"string\" ? body.content : \"\",\n html: body.html,\n };\n } catch {\n return null;\n }\n}\n\n// ── Calls ────────────────────────────────────────────────────────────────────\n//\n// Recorded client calls a project shared with its client (#416), for the team's\n// own admin panel. Unlike guides these are private: they need the project's\n// server key (`ggc_…`, API Tokens → Server keys), which lives on the server and never in a bundle, and\n// every response holds links that expire — so nothing here is cached.\n\n/** Someone on the call, as the recorder saw them. */\nexport interface CallParticipant {\n name: string | null;\n email: string | null;\n}\n\n/** One shared call as the list returns it — no transcript, no links. */\nexport interface CallListItem {\n id: string;\n title: string | null;\n startsAt: string | null;\n endsAt: string | null;\n durationSec: number | null;\n /** `\"bot\"` (joined the Meet) or `\"extension\"` (recorded from someone's Chrome). */\n recorder: string | null;\n /** Match a call to your customer by these emails. */\n participants: CallParticipant[];\n hasRecording: boolean;\n /** `IDLE`, `RUNNING`, `DONE` or `FAILED`. */\n transcriptStatus: string;\n /** One line on what the call was about, once Glitchgrab has read it. */\n overview: string | null;\n createdAt: string;\n}\n\nexport interface CallAudio {\n /**\n * `call` — a bot recording's single mixed track. An extension recording has\n * two instead: `client` (everyone else on the call) and `recorder` (the\n * person who recorded it).\n */\n track: \"call\" | \"client\" | \"recorder\";\n /** Plays for {@link Call.linksExpireInSeconds}. */\n url: string;\n}\n\nexport interface CallMoment {\n /** Milliseconds into the call — seek the audio here. */\n tMs: number | null;\n speaker: string | null;\n text: string;\n}\n\n/** One shared call in full. */\nexport interface Call extends CallListItem {\n meetUrl: string | null;\n audio: CallAudio[];\n /** Audio and screenshot links stop working after this many seconds. Read the call per render; never store them. */\n linksExpireInSeconds: number;\n transcript: string | null;\n summary: { overview: string; moments: CallMoment[] } | null;\n /** Screenshots grabbed during the call, a page at a time. `tMs` is on the transcript's clock. */\n frames: { total: number; offset: number; limit: number; items: { tMs: number; url: string }[] };\n}\n\n/** Options for {@link listCalls}, {@link getCall}, {@link deleteCall} and the booking settings calls. */\nexport interface CallsOptions {\n /**\n * The project's server key (`ggc_…`, API Tokens → Server keys). Falls back to `GLITCHGRAB_CALLS_KEY`.\n * Server only — never a `NEXT_PUBLIC_` variable: it plays and deletes calls.\n */\n key?: string;\n /** Override the API host. Falls back to `GLITCHGRAB_BASE_URL`, then `https://glitchgrab.dev`. */\n baseUrl?: string;\n /** Give up after this many ms. Default 5000. */\n timeoutMs?: number;\n}\n\nexport interface ListCallsOptions extends CallsOptions {\n /** Default 20, max 100. */\n limit?: number;\n /** ISO date: only calls recorded before it. Pass the last call's `createdAt` for the next page. */\n before?: string;\n}\n\nexport interface GetCallOptions extends CallsOptions {\n /** First screenshot to return. Default 0. */\n framesOffset?: number;\n /** Screenshots per page. Default 50, max 200, `0` for none. */\n framesLimit?: number;\n}\n\nexport type DeleteCallResult = { deleted: true } | { deleted: false; error: string };\n\nconst CALLS_KEY_PREFIX = \"ggc_\";\nconst NO_KEY_ERROR = \"No server key — set GLITCHGRAB_CALLS_KEY (ggc_…)\";\n\n/** Only a `ggc_` key. A `gg_` token here would only 401 — and means the public token sits where a secret belongs. */\nfunction callsKey(options: CallsOptions): string | null {\n const key = options.key ?? readEnv(\"GLITCHGRAB_CALLS_KEY\");\n return typeof key === \"string\" && key.startsWith(CALLS_KEY_PREFIX) ? key : null;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && typeof value === \"object\" && !Array.isArray(value);\n}\n\nfunction stringOrNull(value: unknown): string | null {\n return typeof value === \"string\" ? value : null;\n}\n\nfunction numberOrNull(value: unknown): number | null {\n return typeof value === \"number\" && Number.isFinite(value) ? value : null;\n}\n\nfunction readCallListItem(value: unknown): CallListItem | null {\n if (!isRecord(value) || typeof value.id !== \"string\") return null;\n return {\n id: value.id,\n title: stringOrNull(value.title),\n startsAt: stringOrNull(value.startsAt),\n endsAt: stringOrNull(value.endsAt),\n durationSec: numberOrNull(value.durationSec),\n recorder: stringOrNull(value.recorder),\n participants: Array.isArray(value.participants)\n ? value.participants.filter(isRecord).map((p) => ({ name: stringOrNull(p.name), email: stringOrNull(p.email) }))\n : [],\n hasRecording: value.hasRecording === true,\n transcriptStatus: typeof value.transcriptStatus === \"string\" ? value.transcriptStatus : \"IDLE\",\n overview: stringOrNull(value.overview),\n createdAt: typeof value.createdAt === \"string\" ? value.createdAt : \"\",\n };\n}\n\nconst AUDIO_TRACKS: ReadonlySet<string> = new Set([\"call\", \"client\", \"recorder\"]);\n\nfunction readCall(value: unknown): Call | null {\n const base = readCallListItem(value);\n if (!base || !isRecord(value)) return null;\n\n const rawSummary = value.summary;\n const summary =\n isRecord(rawSummary) && typeof rawSummary.overview === \"string\"\n ? {\n overview: rawSummary.overview,\n moments: Array.isArray(rawSummary.moments)\n ? rawSummary.moments\n .filter(isRecord)\n .filter((m) => typeof m.text === \"string\")\n .map((m) => ({ tMs: numberOrNull(m.tMs), speaker: stringOrNull(m.speaker), text: m.text as string }))\n : [],\n }\n : null;\n\n const frames = isRecord(value.frames) ? value.frames : {};\n\n return {\n ...base,\n meetUrl: stringOrNull(value.meetUrl),\n audio: Array.isArray(value.audio)\n ? value.audio\n .filter(isRecord)\n .filter((a) => typeof a.url === \"string\" && typeof a.track === \"string\" && AUDIO_TRACKS.has(a.track))\n .map((a) => ({ track: a.track as CallAudio[\"track\"], url: a.url as string }))\n : [],\n linksExpireInSeconds: numberOrNull(value.linksExpireInSeconds) ?? 0,\n transcript: stringOrNull(value.transcript),\n summary,\n frames: {\n total: numberOrNull(frames.total) ?? 0,\n offset: numberOrNull(frames.offset) ?? 0,\n limit: numberOrNull(frames.limit) ?? 0,\n items: Array.isArray(frames.items)\n ? frames.items\n .filter(isRecord)\n .filter((f) => typeof f.url === \"string\" && typeof f.tMs === \"number\")\n .map((f) => ({ tMs: f.tMs as number, url: f.url as string }))\n : [],\n },\n };\n}\n\nfunction setNumber(query: URLSearchParams, name: string, value: unknown): void {\n if (typeof value === \"number\" && Number.isFinite(value)) query.set(name, String(Math.floor(value)));\n}\n\n/**\n * Calls shared with the client on the key's project, newest first. No links —\n * open one with {@link getCall}. Every {@link placeCall} call is shared from\n * the start, firm lines (`ownerId`) included.\n *\n * ```ts\n * // app/admin/calls/page.tsx — server component\n * import { listCalls } from \"glitchgrab/server\";\n *\n * export default async function CallsPage() {\n * const calls = await listCalls(); // GLITCHGRAB_CALLS_KEY=ggc_…\n * return calls.map((c) => <a key={c.id} href={`/admin/calls/${c.id}`}>{c.title}</a>);\n * }\n * ```\n *\n * Returns `[]` on any failure. Never throws.\n */\nexport async function listCalls(options: ListCallsOptions = {}): Promise<CallListItem[]> {\n try {\n const key = callsKey(options);\n if (!key) return [];\n\n const query = new URLSearchParams();\n setNumber(query, \"limit\", options.limit);\n if (typeof options.before === \"string\" && options.before) query.set(\"before\", options.before);\n const qs = query.toString();\n\n const { data } = await callSdkApi(`/api/v1/sdk/calls${qs ? `?${qs}` : \"\"}`, key, options, { cache: \"no-store\" });\n if (!Array.isArray(data)) return [];\n return data.map(readCallListItem).filter((call): call is CallListItem => call !== null);\n } catch {\n return [];\n }\n}\n\n/**\n * One shared call: audio links, transcript, summary and a page of screenshots.\n * Links expire after `linksExpireInSeconds` — read the call when the page\n * renders and never store them.\n *\n * A {@link placeCall} call is shared from the start, so `getCall(callId)` with\n * the `callId` from `placeCall` or any `call.*` webhook plays its recording —\n * firm lines (`ownerId`) included (#723). `call.recording_ready` carries no\n * link on purpose: fetch a fresh one here when someone presses play.\n *\n * Returns `null` when the call does not exist, is not shared, and on any\n * failure — call `notFound()` on it. Never throws.\n */\nexport async function getCall(id: string, options: GetCallOptions = {}): Promise<Call | null> {\n try {\n const wanted = typeof id === \"string\" ? id.trim() : \"\";\n const key = callsKey(options);\n if (!wanted || !key) return null;\n\n const query = new URLSearchParams({ id: wanted });\n setNumber(query, \"framesOffset\", options.framesOffset);\n setNumber(query, \"framesLimit\", options.framesLimit);\n\n const { data } = await callSdkApi(`/api/v1/sdk/calls?${query.toString()}`, key, options, { cache: \"no-store\" });\n return readCall(data);\n } catch {\n return null;\n }\n}\n\n/**\n * Permanently delete one shared call — the recording, the screenshots, the\n * transcript. Cannot be undone.\n *\n * Resolves `{ deleted: false, error }` instead of throwing: the reason is worth\n * showing (\"The bot is still in this call — delete it after the call ends\").\n */\nexport async function deleteCall(id: string, options: CallsOptions = {}): Promise<DeleteCallResult> {\n try {\n const wanted = typeof id === \"string\" ? id.trim() : \"\";\n if (!wanted) return { deleted: false, error: \"No call id\" };\n const key = callsKey(options);\n if (!key) return { deleted: false, error: NO_KEY_ERROR };\n\n const { error } = await callSdkApi(`/api/v1/sdk/calls?id=${encodeURIComponent(wanted)}`, key, options, {\n method: \"DELETE\",\n cache: \"no-store\",\n });\n return error === null ? { deleted: true } : { deleted: false, error };\n } catch {\n return { deleted: false, error: \"Could not delete the call\" };\n }\n}\n\n// ── Booking settings ─────────────────────────────────────────────────────────\n//\n// A project's demo booking settings — whether prospects can book, the hours,\n// slot length and the WhatsApp code — so a team's admin panel can change them\n// without a Glitchgrab login. Same server key as calls. Which Google calendar\n// demos land in is not here: it stays set on the Glitchgrab dashboard.\n\n/** `{ \"1\": [[\"09:00\", \"17:00\"]] }` — ISO weekday (1 = Monday) to opening ranges, in {@link BookingSettings.timezone}. */\nexport type BookingWorkingHours = Record<string, [string, string][]>;\n\nexport interface BookingSettings {\n /** Off means the booking dialog and WhatsApp refuse to offer slots. */\n enabled: boolean;\n /** Length of a demo, 15–180. */\n slotMinutes: number;\n /** Free minutes kept after each booking, 0–120. */\n bufferMinutes: number;\n /** IANA zone the working hours are in, e.g. `\"Asia/Kolkata\"`. */\n timezone: string;\n /** A day left out is closed. */\n workingHours: BookingWorkingHours;\n title: string | null;\n description: string | null;\n /** Who the visitor meets, shown beside the calendar in the booking dialog. */\n hostName: string | null;\n /** https URL of the host's photo. Anything that is not https is ignored on save. */\n hostAvatarUrl: string | null;\n /** How far ahead a slot can be booked, 1–90 days. */\n horizonDays: number;\n /** Earliest a slot can start from now, 0–10080 minutes. */\n noticeMinutes: number;\n /** Code in the \"Book on WhatsApp\" link. Lower case, letters, digits and `-`; unique across projects. */\n whatsappCode: string | null;\n /**\n * Who else is WhatsApped when a demo is booked, besides the project owner —\n * sales, the founders, whoever needs the lead as it lands. At most 5; a\n * ten-digit Indian mobile is stored with its 91. Send the whole list to\n * change it; leave the field out and it keeps what is stored.\n */\n notifyRecipients: BookingNotifyRecipient[];\n /**\n * What a prospect is asked before they see the calendar — in the booking\n * dialog and over WhatsApp — so the right person takes the demo. At most 5.\n * Send the whole list to change it (`[]` asks nothing); leave the field out\n * and it keeps what is stored. The answers come back on {@link Booking.answers}.\n */\n questions: BookingQuestion[];\n /**\n * Questions saved as a draft and not live yet — the dialog and the WhatsApp\n * chat still ask {@link questions}. Preview them with\n * `useGlitchgrab().openBookingPreview()`, make them live with\n * {@link publishBookingQuestions}. `null` = no draft; `[]` = a draft that\n * asks nothing.\n */\n draftQuestions: BookingQuestion[] | null;\n /**\n * Text a code to the prospect's phone before the demo is confirmed. `false`\n * books the slot the moment the form is sent — one step fewer, but a\n * mistyped number still books. Defaults to `true`.\n */\n requireOtp: boolean;\n}\n\n/** One question asked before a demo is booked. The prospect taps one of `options`. */\nexport interface BookingQuestion {\n /**\n * Stable name for the question, e.g. `\"language\"` — lower case letters,\n * digits, `-` and `_`. Leave it out on a new question and one is made from\n * the label; keep it when rewording so earlier answers still line up.\n */\n id: string;\n /** What is asked, up to 80 characters. */\n label: string;\n /** 2–9 choices, up to 40 characters each. */\n options: string[];\n /** False lets the prospect skip it. */\n required: boolean;\n}\n\n/** A question as you send it: `id` and `required` may be left out on a new one. */\nexport type BookingQuestionInput = Omit<BookingQuestion, \"id\" | \"required\"> & { id?: string; required?: boolean };\n\n/** What {@link updateBookingSettings} accepts — any subset of the settings. */\nexport type BookingSettingsChanges = Partial<Omit<BookingSettings, \"questions\" | \"draftQuestions\">> & {\n questions?: BookingQuestionInput[];\n /**\n * With `questions`: save them as a draft instead — the live dialog and chat\n * are unchanged until {@link publishBookingQuestions}. Left out, `questions`\n * go live at once (and any draft is dropped), as before.\n */\n draft?: boolean;\n};\n\n/** One answer a prospect gave, with the question as it was worded when they booked. */\nexport interface BookingAnswer {\n /** The {@link BookingQuestion.id} it answers. */\n id: string;\n question: string;\n answer: string;\n}\n\n/** One extra person alerted when a demo is booked. */\nexport interface BookingNotifyRecipient {\n name: string;\n /** Digits only, country code included. */\n phone: string;\n}\n\nexport interface BookingSettingsState {\n settings: BookingSettings;\n /** False until someone saves once — `settings` are the defaults. */\n saved: boolean;\n /** No calendar means no slots, whatever `enabled` says. Connect one in Glitchgrab → Calls → Booking. */\n calendarConnected: boolean;\n}\n\nexport type BookingSettingsResult = { ok: true; data: BookingSettingsState } | { ok: false; error: string };\n\nconst WEEKDAY = /^[1-7]$/;\n\n/** A stored question list, or null when the value is not a list at all. */\nfunction readQuestionList(value: unknown): BookingQuestion[] | null {\n if (!Array.isArray(value)) return null;\n return value\n .filter(isRecord)\n .map((row) => ({\n id: stringOrNull(row.id) ?? \"\",\n label: stringOrNull(row.label) ?? \"\",\n options: Array.isArray(row.options)\n ? row.options.filter((option): option is string => typeof option === \"string\")\n : [],\n required: row.required !== false,\n }))\n .filter((row) => row.id !== \"\" && row.label !== \"\");\n}\n\nfunction readBookingSettingsState(value: unknown): BookingSettingsState | null {\n if (!isRecord(value) || !isRecord(value.settings)) return null;\n const s = value.settings;\n const hours: BookingWorkingHours = {};\n if (isRecord(s.workingHours)) {\n for (const [day, ranges] of Object.entries(s.workingHours)) {\n if (!WEEKDAY.test(day) || !Array.isArray(ranges)) continue;\n const valid = ranges.filter(\n (r): r is [string, string] => Array.isArray(r) && typeof r[0] === \"string\" && typeof r[1] === \"string\"\n );\n if (valid.length) hours[day] = valid.map(([open, close]) => [open, close]);\n }\n }\n return {\n settings: {\n enabled: s.enabled === true,\n slotMinutes: numberOrNull(s.slotMinutes) ?? 60,\n bufferMinutes: numberOrNull(s.bufferMinutes) ?? 0,\n timezone: typeof s.timezone === \"string\" ? s.timezone : \"Asia/Kolkata\",\n workingHours: hours,\n title: stringOrNull(s.title),\n description: stringOrNull(s.description),\n hostName: stringOrNull(s.hostName),\n hostAvatarUrl: stringOrNull(s.hostAvatarUrl),\n horizonDays: numberOrNull(s.horizonDays) ?? 15,\n noticeMinutes: numberOrNull(s.noticeMinutes) ?? 120,\n whatsappCode: stringOrNull(s.whatsappCode),\n notifyRecipients: Array.isArray(s.notifyRecipients)\n ? s.notifyRecipients\n .filter(isRecord)\n .map((row) => ({\n name: stringOrNull(row.name) ?? \"\",\n phone: stringOrNull(row.phone) ?? \"\",\n }))\n .filter((row) => row.phone !== \"\")\n : [],\n questions: readQuestionList(s.questions) ?? [],\n // A server from before drafts sends no key: no draft.\n draftQuestions: readQuestionList(s.draftQuestions),\n // Missing means a server from before the switch, which always asked.\n requireOtp: s.requireOtp !== false,\n },\n saved: value.saved === true,\n calendarConnected: value.calendarConnected === true,\n };\n}\n\n/**\n * The key's project booking settings. Never throws — `{ ok: false, error }` on\n * any failure, so the page can say why instead of showing defaults as if saved.\n *\n * ```ts\n * const result = await getBookingSettings(); // GLITCHGRAB_CALLS_KEY=ggc_…\n * if (result.ok && !result.data.calendarConnected) warn(\"Connect a calendar in Glitchgrab\");\n * ```\n */\nexport async function getBookingSettings(options: CallsOptions = {}): Promise<BookingSettingsResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error } = await callSdkApi(\"/api/v1/sdk/booking-settings\", key, options, { cache: \"no-store\" });\n const state = error === null ? readBookingSettingsState(data) : null;\n return state ? { ok: true, data: state } : { ok: false, error: error ?? \"Unexpected response\" };\n } catch {\n return { ok: false, error: \"Could not read booking settings\" };\n }\n}\n\n/**\n * Change booking settings. Send only what changed — a field left out keeps its\n * value. Numbers are clamped to their range and invalid working hours are\n * ignored, so read `data.settings` back rather than trusting what you sent.\n *\n * `error` is worth showing: a WhatsApp code already taken by another project\n * comes back as one.\n */\nexport function updateBookingSettings(\n changes: BookingSettingsChanges,\n options: CallsOptions = {}\n): Promise<BookingSettingsResult> {\n return putBookingSettings(changes ?? {}, options);\n}\n\nasync function putBookingSettings(\n body: BookingSettingsChanges | { publishDraft: true } | { discardDraft: true },\n options: CallsOptions\n): Promise<BookingSettingsResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error } = await callSdkApi(\"/api/v1/sdk/booking-settings\", key, options, {\n method: \"PUT\",\n body,\n cache: \"no-store\",\n });\n const state = error === null ? readBookingSettingsState(data) : null;\n return state ? { ok: true, data: state } : { ok: false, error: error ?? \"Unexpected response\" };\n } catch {\n return { ok: false, error: \"Could not save booking settings\" };\n }\n}\n\n/**\n * Make the draft questions live — the booking dialog and the WhatsApp chat ask\n * them from now on. `error` when there is no draft to publish.\n *\n * ```ts\n * await updateBookingSettings({ questions, draft: true }); // live dialog unchanged\n * // …preview with useGlitchgrab().openBookingPreview() on your admin page…\n * await publishBookingQuestions();\n * ```\n */\nexport function publishBookingQuestions(options: CallsOptions = {}): Promise<BookingSettingsResult> {\n return putBookingSettings({ publishDraft: true }, options);\n}\n\n/** Throw the draft questions away; the live ones are untouched. */\nexport function discardBookingQuestionsDraft(options: CallsOptions = {}): Promise<BookingSettingsResult> {\n return putBookingSettings({ discardDraft: true }, options);\n}\n\n// ── Demo bookings ────────────────────────────────────────────────────────────\n//\n// The demos prospects booked on this project, so the team's own admin panel can\n// show them, move them and call them off without a Glitchgrab login. Same\n// server key as calls. Moving one patches the real Google event, so the Meet\n// link every earlier message already carries keeps working.\n\n/** One booked demo. Times are ISO strings in UTC; `timezone` is what the prospect picked in. */\nexport interface Booking {\n id: string;\n /** Who booked. */\n name: string;\n email: string;\n /** Digits only, country code included. */\n phone: string;\n startsAt: string;\n endsAt: string;\n /** IANA zone the prospect booked in, e.g. `\"Asia/Kolkata\"`. */\n timezone: string | null;\n /** What they typed when booking, if anything. */\n note: string | null;\n /** What they answered to the project's {@link BookingSettings.questions}. Empty when nothing was asked. */\n answers: BookingAnswer[];\n /** `CONFIRMED` or `CANCELLED`. Unverified holds are never returned. */\n status: string;\n /** The Google Meet room. Null until the booking is confirmed. */\n meetUrl: string | null;\n createdAt: string;\n}\n\nexport interface ListBookingsOptions extends CallsOptions {\n /** `\"upcoming\"` (default) or `\"past\"`. */\n scope?: \"upcoming\" | \"past\";\n /** Up to 100. Defaults to 50. */\n limit?: number;\n}\n\n/**\n * Whether the prospect was told. A move re-sends the approved confirmation\n * template, so `\"sent\"` is real. A cancellation can only go as free text, which\n * WhatsApp delivers only inside its 24-hour window and reports nothing about —\n * `\"attempted\"` means exactly that, and your panel should say so rather than\n * telling an admin the client knows.\n */\nexport type BookingClientNotice = \"sent\" | \"attempted\" | \"none\";\n\nexport type BookingMutationResult =\n | { ok: true; booking: Booking | null; clientNotice: BookingClientNotice }\n | { ok: false; error: string; code: string | null };\n\nfunction readBooking(value: unknown): Booking | null {\n if (!isRecord(value) || typeof value.id !== \"string\") return null;\n return {\n id: value.id,\n name: stringOrNull(value.name) ?? \"\",\n email: stringOrNull(value.email) ?? \"\",\n phone: stringOrNull(value.phone) ?? \"\",\n startsAt: stringOrNull(value.startsAt) ?? \"\",\n endsAt: stringOrNull(value.endsAt) ?? \"\",\n timezone: stringOrNull(value.timezone),\n note: stringOrNull(value.note),\n answers: Array.isArray(value.answers)\n ? value.answers\n .filter(isRecord)\n .map((row) => ({\n id: stringOrNull(row.id) ?? \"\",\n question: stringOrNull(row.question) ?? \"\",\n answer: stringOrNull(row.answer) ?? \"\",\n }))\n .filter((row) => row.answer !== \"\")\n : [],\n status: stringOrNull(value.status) ?? \"CONFIRMED\",\n meetUrl: stringOrNull(value.meetUrl),\n createdAt: stringOrNull(value.createdAt) ?? \"\",\n };\n}\n\n/**\n * The project's booked demos, soonest first. Never throws — an empty list on\n * any failure, so a panel renders \"no demos\" instead of a stack trace.\n *\n * ```ts\n * const demos = await listBookings(); // GLITCHGRAB_CALLS_KEY=ggc_…\n * ```\n */\nexport async function listBookings(options: ListBookingsOptions = {}): Promise<Booking[]> {\n try {\n const key = callsKey(options);\n if (!key) return [];\n\n const query = new URLSearchParams();\n if (options.scope === \"past\") query.set(\"scope\", \"past\");\n setNumber(query, \"limit\", options.limit);\n const qs = query.toString();\n\n const { data } = await callSdkApi(`/api/v1/sdk/bookings${qs ? `?${qs}` : \"\"}`, key, options, {\n cache: \"no-store\",\n });\n if (!Array.isArray(data)) return [];\n return data.map(readBooking).filter((booking): booking is Booking => booking !== null);\n } catch {\n return [];\n }\n}\n\nfunction readMutation(data: unknown, error: string | null): BookingMutationResult {\n if (error !== null) return { ok: false, error, code: null };\n const record = isRecord(data) ? data : {};\n const notice = stringOrNull(record.clientNotice);\n return {\n ok: true,\n booking: readBooking(record.booking),\n clientNotice: notice === \"sent\" || notice === \"attempted\" ? notice : \"none\",\n };\n}\n\n/**\n * Move a demo to a new start time. The Google event is patched, never\n * recreated, so the Meet link in every message already sent stays alive, and\n * the prospect is re-sent the confirmation with the new time.\n *\n * `{ ok: false, code: \"SLOT_TAKEN\" }` when another booking holds that slot,\n * `\"PAST\"` for a time that has gone, `\"CALENDAR\"` when Google refused — in the\n * last case nothing was changed and the demo still stands at its old time.\n */\nexport async function rescheduleBooking(\n id: string,\n startsAt: Date | string,\n options: CallsOptions = {}\n): Promise<BookingMutationResult> {\n try {\n const wanted = typeof id === \"string\" ? id.trim() : \"\";\n const key = callsKey(options);\n if (!wanted) return { ok: false, error: \"No booking id\", code: null };\n if (!key) return { ok: false, error: NO_KEY_ERROR, code: null };\n\n const when = startsAt instanceof Date ? startsAt.toISOString() : String(startsAt);\n const { data, error } = await callSdkApi(\n `/api/v1/sdk/bookings?id=${encodeURIComponent(wanted)}`,\n key,\n options,\n { method: \"PATCH\", body: { startsAt: when }, cache: \"no-store\" }\n );\n return readMutation(data, error);\n } catch {\n return { ok: false, error: \"Could not move that demo\", code: null };\n }\n}\n\n/**\n * Call a demo off: the Google event is deleted, the recorder is stood down, and\n * the prospect is messaged if WhatsApp will carry it (see\n * {@link BookingClientNotice}).\n *\n * `reason` is included in that message. Nothing is marked cancelled unless the\n * calendar agreed first, so `{ ok: false, code: \"CALENDAR\" }` means the demo is\n * still on.\n */\nexport async function cancelBooking(\n id: string,\n reason?: string,\n options: CallsOptions = {}\n): Promise<BookingMutationResult> {\n try {\n const wanted = typeof id === \"string\" ? id.trim() : \"\";\n const key = callsKey(options);\n if (!wanted) return { ok: false, error: \"No booking id\", code: null };\n if (!key) return { ok: false, error: NO_KEY_ERROR, code: null };\n\n const query = new URLSearchParams({ id: wanted });\n if (typeof reason === \"string\" && reason.trim()) query.set(\"reason\", reason.trim().slice(0, 200));\n\n const { data, error } = await callSdkApi(`/api/v1/sdk/bookings?${query.toString()}`, key, options, {\n method: \"DELETE\",\n cache: \"no-store\",\n });\n return readMutation(data, error);\n } catch {\n return { ok: false, error: \"Could not cancel that demo\", code: null };\n }\n}\n\n/**\n * `JSON.stringify` that is safe inside `<script type=\"application/ld+json\">`.\n *\n * Guide titles and summaries are written outside your codebase, and\n * `JSON.stringify` leaves `<` alone — a title containing `</script>` closes the\n * tag and whatever follows runs as HTML. The JSON unicode escape for `<` is the same\n * character to a JSON parser and inert to the HTML one. `>`, `&` and the two\n * JavaScript line separators are escaped for the same reason.\n *\n * ```tsx\n * <script\n * type=\"application/ld+json\"\n * dangerouslySetInnerHTML={{ __html: escapeJsonForScript({ \"@type\": \"HowTo\", name: guide.title }) }}\n * />\n * ```\n *\n * Never throws: a value JSON cannot represent (circular, `undefined`) becomes `null`.\n */\nexport function escapeJsonForScript(value: unknown): string {\n try {\n const json = JSON.stringify(value);\n if (json === undefined) return \"null\";\n return json\n .replace(/</g, \"\\\\u003c\")\n .replace(/>/g, \"\\\\u003e\")\n .replace(/&/g, \"\\\\u0026\")\n .replace(/\\u2028/g, \"\\\\u2028\")\n .replace(/\\u2029/g, \"\\\\u2029\");\n } catch {\n return \"null\";\n }\n}\n\n// ── Leads ────────────────────────────────────────────────────────────────────\n//\n// Contact details prospects left with `useGlitchgrab().submitLead()` (#534) —\n// the demo form, the contact page — so your own admin can show them. Same\n// server key as calls and bookings. A lead never becomes a GitHub issue.\n\n/** One lead. One per phone number per project; a repeat submission updates it. */\nexport interface Lead {\n id: string;\n name: string;\n /** Digits only, country code included. */\n phone: string;\n email: string | null;\n company: string | null;\n address: string | null;\n /** What your form passed as `source`, e.g. `\"demo\"`. */\n source: string;\n metadata: Record<string, string | number | boolean> | null;\n /** The page the lead was left on. */\n pageUrl: string | null;\n /** How many times this phone submitted. */\n submissions: number;\n /** First submission. */\n createdAt: string;\n /** Latest submission. */\n updatedAt: string;\n /** The demo this lead went on to book, matched by phone or email. */\n booking: { id: string; startsAt: string; status: string } | null;\n}\n\nexport interface ListLeadsOptions extends CallsOptions {\n /** Only leads with this `source`. */\n source?: string;\n /** Only leads first left at or after this time. */\n since?: Date | string;\n /** Up to 200. Defaults to 100. */\n limit?: number;\n}\n\nfunction readLead(value: unknown): Lead | null {\n if (!isRecord(value) || typeof value.id !== \"string\") return null;\n const metadata = isRecord(value.metadata) ? (value.metadata as Lead[\"metadata\"]) : null;\n const booking = isRecord(value.booking) && typeof value.booking.id === \"string\"\n ? {\n id: value.booking.id,\n startsAt: stringOrNull(value.booking.startsAt) ?? \"\",\n status: stringOrNull(value.booking.status) ?? \"CONFIRMED\",\n }\n : null;\n return {\n id: value.id,\n name: stringOrNull(value.name) ?? \"\",\n phone: stringOrNull(value.phone) ?? \"\",\n email: stringOrNull(value.email),\n company: stringOrNull(value.company),\n address: stringOrNull(value.address),\n source: stringOrNull(value.source) ?? \"\",\n metadata,\n pageUrl: stringOrNull(value.pageUrl),\n submissions: typeof value.submissions === \"number\" ? value.submissions : 1,\n createdAt: stringOrNull(value.createdAt) ?? \"\",\n updatedAt: stringOrNull(value.updatedAt) ?? \"\",\n booking,\n };\n}\n\n/**\n * The project's leads, newest first. Never throws — an empty list on any\n * failure.\n *\n * ```ts\n * const leads = await listLeads({ source: \"demo\" }); // GLITCHGRAB_CALLS_KEY=ggc_…\n * ```\n */\nexport async function listLeads(options: ListLeadsOptions = {}): Promise<Lead[]> {\n try {\n const key = callsKey(options);\n if (!key) return [];\n\n const query = new URLSearchParams();\n if (options.source) query.set(\"source\", options.source);\n if (options.since) {\n const since = options.since instanceof Date ? options.since : new Date(options.since);\n if (!Number.isNaN(since.getTime())) query.set(\"since\", since.toISOString());\n }\n setNumber(query, \"limit\", options.limit);\n const qs = query.toString();\n\n const { data } = await callSdkApi(`/api/v1/sdk/leads${qs ? `?${qs}` : \"\"}`, key, options, {\n cache: \"no-store\",\n });\n if (!Array.isArray(data)) return [];\n return data.map(readLead).filter((lead): lead is Lead => lead !== null);\n } catch {\n return [];\n }\n}\n\n// ─── Customer follow-ups (#630) ─────────────────────────────────────────────\n\n/** How a next call reads on a list: grey, amber, red. */\nexport type FollowUpDue = \"none\" | \"upcoming\" | \"today\" | \"overdue\";\n\nexport interface FollowUp {\n /** Glitchgrab's id; null for an externalId Glitchgrab has never seen. */\n id: string | null;\n externalId: string | null;\n /** Digits, `91` + 10. */\n phone: string | null;\n name: string | null;\n /** The customer's business (Abhyasika: the library name), as you sent it. */\n businessName: string | null;\n /** ISO. The booked next call. */\n nextCallAt: string | null;\n nextCallBookedBy: string | null;\n due: FollowUpDue;\n /** The next call with why it is happening (#670). Null when nothing is booked. */\n booking: FollowUpBooking | null;\n /**\n * ISO. Read from the last call's transcript. Since #670 a call's suggestion\n * is booked by itself (see `booking.auto`); it stays only a suggestion when\n * a person's own booking already stands.\n */\n suggestedNextCallAt: string | null;\n suggestedNextCallReason: string | null;\n lastNote: string | null;\n lastNoteAt: string | null;\n lastNoteBy: string | null;\n noteCount: number;\n lastCall: {\n id: string;\n status: string | null;\n at: string;\n durationSec: number | null;\n /** Which of the operator's phones the call is on: 0 = the one picked, 1+ = a backup (#654). */\n ringingIndex: number | null;\n /** That phone. While `status` is \"DIALING\" it is the one ringing right now. */\n ringingPhone: string | null;\n } | null;\n}\n\nexport interface FollowUpBooking {\n /** ISO. */\n at: string;\n /** \"Ravi → Sunita Patil (Shanti Study Room): set up auto-pay again\" — show it under \"Next call\". */\n reason: string | null;\n /** Booked by Glitchgrab from the call summary — label it \"Booked from the call summary\". */\n auto: boolean;\n /** ISO. The caller tapped \"Yes, keep it\" on WhatsApp. */\n confirmedAt: string | null;\n /** The \"keep this call?\" WhatsApp reached whoever made the call. */\n askedOnWhatsApp: boolean;\n bookedBy: string | null;\n}\n\nexport interface FollowUpNote {\n id: string;\n body: string;\n /** \"manual\" (Glitchgrab), \"api\" (your app), \"auto\" (written from a call). */\n source: string;\n author: string | null;\n callId: string | null;\n editedAt: string | null;\n /** Who last edited it, when your app (or an import) said so. */\n editedBy: string | null;\n createdAt: string;\n}\n\nexport interface FollowUpCall {\n id: string;\n status: string | null;\n direction: string | null;\n at: string;\n durationSec: number | null;\n /** Which of the operator's phones the call is on: 0 = the one picked, 1+ = a backup (#654). */\n ringingIndex: number | null;\n /** That phone. While `status` is \"DIALING\" it is the one ringing right now. */\n ringingPhone: string | null;\n /** E.164 number the call rang — `toPhone` when the dial sent one, else the follow-up's phone (#666). */\n to: string | null;\n /**\n * Who didn't pick up a NO_ANSWER outbound call (#666): \"operator\" = none of\n * the operator's phones (backups included), \"customer\" = the operator\n * answered and the customer didn't. Null otherwise, and on older calls.\n */\n missedBy: \"operator\" | \"customer\" | null;\n /** Who placed it. Null on inbound calls. */\n operatorName: string | null;\n /** Shared with you — calls dialled from a follow-up are; an inbound call is once the owner shares it. */\n shared: boolean;\n overview: string | null;\n /** Presigned; expires after `linksExpireInSeconds`. Null when not shared. */\n recordingUrl: string | null;\n}\n\nexport interface FollowUpDetail extends FollowUp {\n notes: FollowUpNote[];\n calls: FollowUpCall[];\n linksExpireInSeconds: number;\n}\n\n/** Someone on the project's Glitchgrab phone line — rung first on a dial, reminded on a booking. */\nexport type FollowUpOperator = { email: string } | { phone: string };\n\n/**\n * Why a dial was refused, so your screen can say exactly what is missing (#654):\n * - `NOT_ON_LINE` — the operator isn't one of the people on the project's phone line.\n * - `NO_VERIFIED_NUMBER` — they are, but have no phone Glitchgrab may ring.\n * - `RING_PHONE_NOT_YOURS` — `ringPhone` isn't one of their verified numbers.\n */\nexport type RingErrorCode = \"NOT_ON_LINE\" | \"NO_VERIFIED_NUMBER\" | \"RING_PHONE_NOT_YOURS\";\n\nexport type FollowUpWriteResult<T = Record<string, never>> =\n | ({ ok: true; followUpId: string } & T)\n | { ok: false; error: string; code?: string };\n\nconst DUES: ReadonlySet<string> = new Set([\"none\", \"upcoming\", \"today\", \"overdue\"]);\n\nfunction readFollowUp(value: unknown): FollowUp | null {\n if (!isRecord(value)) return null;\n const call = isRecord(value.lastCall) && typeof value.lastCall.id === \"string\" ? value.lastCall : null;\n return {\n id: stringOrNull(value.id),\n externalId: stringOrNull(value.externalId),\n phone: stringOrNull(value.phone),\n name: stringOrNull(value.name),\n businessName: stringOrNull(value.businessName),\n nextCallAt: stringOrNull(value.nextCallAt),\n nextCallBookedBy: stringOrNull(value.nextCallBookedBy),\n due: typeof value.due === \"string\" && DUES.has(value.due) ? (value.due as FollowUpDue) : \"none\",\n booking:\n isRecord(value.booking) && typeof value.booking.at === \"string\"\n ? {\n at: value.booking.at,\n reason: stringOrNull(value.booking.reason),\n auto: value.booking.auto === true,\n confirmedAt: stringOrNull(value.booking.confirmedAt),\n askedOnWhatsApp: value.booking.askedOnWhatsApp === true,\n bookedBy: stringOrNull(value.booking.bookedBy),\n }\n : null,\n suggestedNextCallAt: stringOrNull(value.suggestedNextCallAt),\n suggestedNextCallReason: stringOrNull(value.suggestedNextCallReason),\n lastNote: stringOrNull(value.lastNote),\n lastNoteAt: stringOrNull(value.lastNoteAt),\n lastNoteBy: stringOrNull(value.lastNoteBy),\n noteCount: numberOrNull(value.noteCount) ?? 0,\n lastCall: call\n ? {\n id: call.id as string,\n status: stringOrNull(call.status),\n at: stringOrNull(call.at) ?? \"\",\n durationSec: numberOrNull(call.durationSec),\n ringingIndex: numberOrNull(call.ringingIndex),\n ringingPhone: stringOrNull(call.ringingPhone),\n }\n : null,\n };\n}\n\nfunction readFollowUpDetail(value: unknown): FollowUpDetail | null {\n const base = readFollowUp(value);\n if (!base || !isRecord(value)) return null;\n return {\n ...base,\n linksExpireInSeconds: numberOrNull(value.linksExpireInSeconds) ?? 0,\n notes: Array.isArray(value.notes)\n ? value.notes\n .filter(isRecord)\n .filter((n) => typeof n.id === \"string\" && typeof n.body === \"string\")\n .map((n) => ({\n id: n.id as string,\n body: n.body as string,\n source: stringOrNull(n.source) ?? \"manual\",\n author: stringOrNull(n.author),\n callId: stringOrNull(n.callId),\n editedAt: stringOrNull(n.editedAt),\n editedBy: stringOrNull(n.editedBy),\n createdAt: stringOrNull(n.createdAt) ?? \"\",\n }))\n : [],\n calls: Array.isArray(value.calls)\n ? value.calls\n .filter(isRecord)\n .filter((c) => typeof c.id === \"string\")\n .map((c) => ({\n id: c.id as string,\n status: stringOrNull(c.status),\n direction: stringOrNull(c.direction),\n at: stringOrNull(c.at) ?? \"\",\n durationSec: numberOrNull(c.durationSec),\n ringingIndex: numberOrNull(c.ringingIndex),\n ringingPhone: stringOrNull(c.ringingPhone),\n to: stringOrNull(c.to),\n missedBy: c.missedBy === \"operator\" || c.missedBy === \"customer\" ? c.missedBy : null,\n operatorName: stringOrNull(c.operatorName),\n shared: c.shared === true,\n overview: stringOrNull(c.overview),\n recordingUrl: stringOrNull(c.recordingUrl),\n }))\n : [],\n };\n}\n\nfunction followUpPath(externalId: string, rest = \"\"): string | null {\n const id = typeof externalId === \"string\" ? externalId.trim() : \"\";\n return id ? `/api/v1/sdk/follow-ups/${encodeURIComponent(id)}${rest}` : null;\n}\n\nasync function writeFollowUp<T extends Record<string, unknown>>(\n path: string | null,\n method: \"POST\" | \"PUT\" | \"PATCH\",\n body: Record<string, unknown>,\n options: CallsOptions,\n pick: (data: Record<string, unknown>) => T\n): Promise<FollowUpWriteResult<T>> {\n try {\n if (!path) return { ok: false, error: \"No externalId\" };\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(path, key, options, { method, body, cache: \"no-store\" });\n if (error !== null || !isRecord(data) || typeof data.followUpId !== \"string\") {\n return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n }\n return { ok: true, followUpId: data.followUpId, ...pick(data) } as FollowUpWriteResult<T>;\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Follow-ups for a whole list page in one request — one entry per id, in the\n * order asked (≤500). An id Glitchgrab has never seen comes back empty, so\n * every row can render.\n *\n * ```ts\n * const followUps = await listFollowUps(owners.map((o) => o.id)); // GLITCHGRAB_CALLS_KEY=ggc_…\n * ```\n *\n * Returns `[]` on any failure. Never throws.\n */\nexport async function listFollowUps(externalIds: string[], options: CallsOptions = {}): Promise<FollowUp[]> {\n try {\n const key = callsKey(options);\n const ids = Array.isArray(externalIds) ? externalIds.filter((id) => typeof id === \"string\" && id.trim()) : [];\n if (!key || ids.length === 0) return [];\n const query = new URLSearchParams({ externalIds: ids.join(\",\") });\n const { data } = await callSdkApi(`/api/v1/sdk/follow-ups?${query.toString()}`, key, options, {\n cache: \"no-store\",\n });\n if (!Array.isArray(data)) return [];\n return data.map(readFollowUp).filter((f): f is FollowUp => f !== null);\n } catch {\n return [];\n }\n}\n\n/**\n * One customer's follow-up: booking, every note, their calls (recording links\n * for shared calls only, presigned). Null on failure. Never throws.\n */\nexport async function getFollowUp(externalId: string, options: CallsOptions = {}): Promise<FollowUpDetail | null> {\n try {\n const path = followUpPath(externalId);\n const key = callsKey(options);\n if (!path || !key) return null;\n const { data } = await callSdkApi(path, key, options, { cache: \"no-store\" });\n return readFollowUpDetail(data);\n } catch {\n return null;\n }\n}\n\n/**\n * Add a note from your own screen. `phone` is needed the first time Glitchgrab\n * sees this externalId. `createdAt` / `editedAt` / `editedBy` copy a note from\n * your old store with its real dates (past only; no webhook fires for it).\n * Never throws.\n */\nexport function addFollowUpNote(\n externalId: string,\n note: {\n body: string;\n author?: string;\n phone?: string;\n name?: string;\n businessName?: string;\n createdAt?: Date | string;\n editedAt?: Date | string | null;\n editedBy?: string | null;\n },\n options: CallsOptions = {}\n): Promise<FollowUpWriteResult<{ id: string }>> {\n const iso = (v: Date | string | null | undefined) => (v instanceof Date ? v.toISOString() : v);\n const body = { ...note, createdAt: iso(note.createdAt), editedAt: iso(note.editedAt) };\n return writeFollowUp(followUpPath(externalId, \"/notes\"), \"POST\", body, options, (d) => ({\n id: typeof d.id === \"string\" ? d.id : \"\",\n }));\n}\n\n/**\n * Edit a note from your own screen: your app's notes and the note written from\n * a call. A note someone wrote inside Glitchgrab can't be edited here (404).\n * `editor` is shown as who edited it. Never throws.\n */\nexport function editFollowUpNote(\n externalId: string,\n noteId: string,\n edit: { body: string; editor?: string },\n options: CallsOptions = {}\n): Promise<FollowUpWriteResult<{ id: string }>> {\n return writeFollowUp(\n followUpPath(externalId, `/notes/${encodeURIComponent(noteId)}`),\n \"PATCH\",\n edit,\n options,\n (d) => ({ id: typeof d.id === \"string\" ? d.id : \"\" })\n );\n}\n\n/**\n * Book, move (`Date`/ISO) or clear (`null`) the next call. The reminder\n * WhatsApp goes to `operator`, else the project owner. `reason` is read back\n * as `booking.reason`; left out, a moved booking keeps its reason. A booking\n * made here is never replaced by the next call's automatic one. Never throws.\n */\nexport function setFollowUpBooking(\n externalId: string,\n booking: {\n nextCallAt: Date | string | null;\n reason?: string | null;\n operator?: FollowUpOperator;\n author?: string;\n phone?: string;\n name?: string;\n businessName?: string;\n },\n options: CallsOptions = {}\n): Promise<FollowUpWriteResult<{ nextCallAt: string | null; reason: string | null }>> {\n const at = booking.nextCallAt instanceof Date ? booking.nextCallAt.toISOString() : booking.nextCallAt;\n return writeFollowUp(followUpPath(externalId, \"/booking\"), \"PUT\", { ...booking, nextCallAt: at }, options, (d) => ({\n nextCallAt: stringOrNull(d.nextCallAt),\n reason: stringOrNull(d.reason),\n }));\n}\n\n/**\n * Start a recorded call: Glitchgrab rings `operator`'s phone, then the\n * customer, from the project's line. The operator must be on that line. Once\n * the call is transcribed a note is written and the next call is booked from\n * it with a reason (#670) — unless a person's booking already stands — and the\n * operator is asked on WhatsApp to keep or remove it. `businessName` (the\n * customer's business) is named in that reason. Never throws.\n *\n * `ringPhone` picks which of the operator's verified phones rings first (list\n * them with {@link getOperatorRingNumbers}); omitted = their Default. If that\n * phone isn't picked up, their backups ring in order (`ringNumbers`). A\n * refusal carries `code` — see {@link RingErrorCode}.\n *\n * `toPhone` dials another number for this one call and leaves the follow-up's\n * saved `phone` alone (#666); `phone` rewrites the saved number.\n */\nexport function dialFollowUp(\n externalId: string,\n call: {\n operator: FollowUpOperator;\n phone?: string;\n toPhone?: string;\n name?: string;\n businessName?: string;\n lineId?: string;\n ringPhone?: string;\n },\n options: CallsOptions = {}\n): Promise<FollowUpWriteResult<{ callId: string; ringing: string; ringNumbers: string[] }>> {\n return writeFollowUp(followUpPath(externalId, \"/dial\"), \"POST\", call, options, (d) => ({\n callId: typeof d.callId === \"string\" ? d.callId : \"\",\n ringing: typeof d.ringing === \"string\" ? d.ringing : \"\",\n ringNumbers: Array.isArray(d.ringNumbers) ? d.ringNumbers.filter((n): n is string => typeof n === \"string\") : [],\n }));\n}\n\n/** One phone Glitchgrab may ring for an operator — what `ringPhone` accepts (#654). */\nexport interface OperatorRingNumber {\n /** E.164, `+91…`. Pass it back as `ringPhone`. */\n phone: string;\n label: string | null;\n isDefault: boolean;\n /** 0 = Default, then backups in the order they ring. */\n order: number;\n}\n\nexport type OperatorRingNumbersResult =\n | { ok: true; operator: { name: string | null }; numbers: OperatorRingNumber[] }\n | { ok: false; error: string; code?: string };\n\n/**\n * Options for calls about one person on a line. `ownerId` = that customer's own\n * line (#723 — one line per PracticeStacks firm, the same id you pass to\n * {@link addLineCaller}): the person must sit on it, and test rings come from\n * its number. Omitted = any of the project's lines.\n */\nexport interface LineCallsOptions extends CallsOptions {\n ownerId?: string;\n}\n\n/** `?ownerId=…`, empty when not set. */\nfunction ownerQuery(options: LineCallsOptions): string {\n return options.ownerId ? `?ownerId=${encodeURIComponent(options.ownerId)}` : \"\";\n}\n\n/** `?email=` / `?phone=` for one operator, plus `ownerId` when set. */\nfunction operatorQuery(operator: FollowUpOperator, options: LineCallsOptions): string {\n const query = new URLSearchParams(\"email\" in operator ? { email: operator.email } : { phone: operator.phone });\n if (options.ownerId) query.set(\"ownerId\", options.ownerId);\n return query.toString();\n}\n\n/**\n * The phones Glitchgrab can ring for one person on the project's phone line —\n * for a \"Ring me on: [Default ▾]\" picker beside your Call button. Verified\n * numbers only. `numbers: []` = dial will refuse with `NO_VERIFIED_NUMBER`;\n * not on the line = `{ ok: false, code: \"NOT_ON_LINE\" }`. Pass `{ ownerId }`\n * for someone on a customer's line. Never throws.\n *\n * ```ts\n * const r = await getOperatorRingNumbers({ email: admin.email }); // GLITCHGRAB_CALLS_KEY=ggc_…\n * if (r.ok) render(r.numbers); // pass the chosen phone to dialFollowUp(…, { ringPhone })\n * ```\n */\nexport async function getOperatorRingNumbers(\n operator: FollowUpOperator,\n options: LineCallsOptions = {}\n): Promise<OperatorRingNumbersResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(`/api/v1/sdk/phone-lines/operator?${operatorQuery(operator, options)}`, key, options, {\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data)) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n return { ok: true, ...operatorNumbersOf(data) };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/** `{ operator, numbers }` as `GET operator` and `POST callers` both answer it. */\nfunction operatorNumbersOf(data: Record<string, unknown>): {\n operator: { name: string | null };\n numbers: OperatorRingNumber[];\n} {\n const op = isRecord(data.operator) ? data.operator : {};\n return {\n operator: { name: stringOrNull(op.name) },\n numbers: Array.isArray(data.numbers)\n ? data.numbers\n .filter(isRecord)\n .filter((n) => typeof n.phone === \"string\")\n .map((n) => ({\n phone: n.phone as string,\n label: stringOrNull(n.label),\n isDefault: n.isDefault === true,\n order: numberOrNull(n.order) ?? 0,\n }))\n : [],\n };\n}\n\nexport type LineCallerResult =\n | { ok: true; created: boolean; operator: { name: string | null }; numbers: OperatorRingNumber[] }\n | { ok: false; error: string; code?: string };\n\n/**\n * Put someone on the project's phone line from your product (#662), so nobody\n * opens Glitchgrab to let a new admin call. Idempotent on email: someone\n * already on the line comes back with `created: false`.\n *\n * They ring only phones confirmed with press 1 — add one with\n * {@link addRingNumber} right after (`numbers: []` until then, and a dial\n * answers `NO_VERIFIED_NUMBER`). Refusals carry `code`: `NO_LINE`,\n * `CALLERS_OFF` (the line's owner switched it off), `CALLER_CAP` (10 per line).\n * Never throws.\n *\n * `ownerId` = seat them on that customer's own line (#716 — one line per\n * PracticeStacks firm). Omitted = the project's line.\n *\n * ```ts\n * const r = await addLineCaller({ email: admin.email, name: admin.name });\n * if (r.ok) openMyPhones(r.numbers);\n * ```\n */\nexport async function addLineCaller(\n caller: { email: string; name: string; phone?: string; ownerId?: string },\n options: CallsOptions = {}\n): Promise<LineCallerResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(\"/api/v1/sdk/phone-lines/callers\", key, options, {\n method: \"POST\",\n body: caller,\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data)) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n return { ok: true, created: data.created === true, ...operatorNumbersOf(data) };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/** Fields every `call.*` webhook carries (#716). `refKey`/`ownerId` are null when not set. */\ninterface CallWebhookBase {\n callId: string;\n repoId: string | null;\n lineId: string | null;\n direction: \"outbound\" | \"inbound\" | null;\n /** The other party, E.164. */\n peer: string | null;\n /** What you passed to {@link placeCall}. */\n refKey: string | null;\n /** The customer whose line carried the call. */\n ownerId: string | null;\n}\n\n/**\n * The body Glitchgrab POSTs to your webhook for a phone call, signed with\n * `X-Glitchgrab-Signature: sha256=<hex HMAC of the raw body>`. Narrow on `event`.\n */\nexport type CallWebhook =\n | { event: \"call.started\"; timestamp: string; data: CallWebhookBase & { ringing: string } }\n | { event: \"call.answered\"; timestamp: string; data: CallWebhookBase & { answeredAt: string } }\n | {\n event: \"call.completed\";\n timestamp: string;\n data: CallWebhookBase & {\n /** RECORDED = they talked; NO_ANSWER (+ missedBy); FAILED. */\n status: string;\n missedBy: \"operator\" | \"customer\" | null;\n durationSec: number | null;\n hangupCause: string | null;\n };\n }\n | {\n event: \"call.recording_ready\";\n timestamp: string;\n data: Omit<CallWebhookBase, \"lineId\"> & { durationSec: number | null };\n }\n | {\n event: \"call.summary_ready\";\n timestamp: string;\n data: Pick<CallWebhookBase, \"callId\" | \"repoId\" | \"refKey\" | \"ownerId\"> & {\n /** \"phone\", \"bot\" or \"extension\". */\n recorder: string;\n title: string | null;\n summary: string | null;\n };\n };\n\n/** `phone.*` webhooks for a customer's automatic line (#716). Same signing as {@link CallWebhook}. */\nexport type PhoneLineWebhook =\n | {\n event: \"phone.kyc_status\";\n timestamp: string;\n data: { ownerId: string; status: string; reason: string | null };\n }\n | { event: \"phone.line_ready\"; timestamp: string; data: { ownerId: string; number: string; lineId: string } };\n\nexport type PlaceCallResult =\n | { ok: true; callId: string; ringing: string; ringNumbers: string[]; from: string }\n | { ok: false; error: string; code?: string };\n\n/**\n * Start a recorded call from your own screen (#716) — Glitchgrab rings\n * `operator`'s verified phone from the line, then bridges to `to`. No\n * follow-up is created; use {@link dialFollowUp} for that.\n *\n * `ownerId` = call from that customer's own line (one line per PracticeStacks\n * firm); the operator must be seated on it ({@link addLineCaller} with the\n * same `ownerId`). `refKey` = your own key for what the call is about — every\n * `call.*` webhook (`call.started`, `call.answered`, `call.completed`,\n * `call.recording_ready`, `call.summary_ready`) carries it back with `ownerId`.\n * The recording plays through {@link getCall}(`callId`).\n * Check your own permissions before calling this. Refusals carry `code`:\n * `NOT_ON_LINE`, `NO_VERIFIED_NUMBER`, `RING_PHONE_NOT_YOURS`. Never throws.\n *\n * ```ts\n * const r = await placeCall({\n * operator: { email: staff.email },\n * to: client.phone,\n * ownerId: firm.id,\n * refKey: client.id,\n * }); // GLITCHGRAB_CALLS_KEY=ggc_…\n * ```\n */\nexport async function placeCall(\n call: {\n operator: FollowUpOperator;\n to: string;\n ownerId?: string;\n refKey?: string;\n title?: string;\n ringPhone?: string;\n },\n options: CallsOptions = {}\n): Promise<PlaceCallResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(\"/api/v1/sdk/phone-lines/call\", key, options, {\n method: \"POST\",\n body: call,\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data) || typeof data.callId !== \"string\") {\n return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n }\n return {\n ok: true,\n callId: data.callId,\n ringing: typeof data.ringing === \"string\" ? data.ringing : \"\",\n ringNumbers: Array.isArray(data.ringNumbers)\n ? data.ringNumbers.filter((n): n is string => typeof n === \"string\")\n : [],\n from: typeof data.from === \"string\" ? data.from : \"\",\n };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\nexport interface PhoneLineInfo {\n number: string;\n label: string | null;\n active: boolean;\n /** People seated on it; anyone else gets `NOT_ON_LINE`. */\n callers: number;\n}\n\n/** Where a customer's automatic line is up to (#716). */\nexport interface PhoneLineRequest {\n /** Plivo's KYC status: submitted | accepted | rejected | suspended | expired. */\n kycStatus: string;\n /** Why Plivo rejected it — show it to the firm so they can fix and resubmit. */\n kycReason: string | null;\n /** The firm agreed to the monthly charge ({@link enableCalling}). */\n enabled: boolean;\n /** NONE | BUYING | BOUGHT | FAILED (retried automatically). */\n buyState: string;\n buyError: string | null;\n number: string | null;\n}\n\nexport interface PhoneLineStatus {\n line: PhoneLineInfo | null;\n /** Null when no KYC was submitted for this customer. */\n request: PhoneLineRequest | null;\n}\n\nfunction lineRequestOf(value: unknown): PhoneLineRequest | null {\n if (!isRecord(value) || typeof value.kycStatus !== \"string\") return null;\n return {\n kycStatus: value.kycStatus,\n kycReason: stringOrNull(value.kycReason),\n enabled: value.enabled === true,\n buyState: typeof value.buyState === \"string\" ? value.buyState : \"NONE\",\n buyError: stringOrNull(value.buyError),\n number: stringOrNull(value.number),\n };\n}\n\n/**\n * A customer's phone line and, when you submitted their KYC, how far the\n * automatic purchase has got (#716) — for the firm's \"Calling\" settings:\n * no request → \"Submit KYC\"; `kycStatus: \"rejected\"` → show `kycReason`;\n * accepted but `enabled: false` → \"Turn on calling (₹… /month)\"; `line` set →\n * ready. `ownerId` omitted = the project's own line. `null` only on failure\n * (no key, network). Never throws.\n */\nexport async function getPhoneLine(\n params: { ownerId?: string } = {},\n options: CallsOptions = {}\n): Promise<PhoneLineStatus | null> {\n try {\n const key = callsKey(options);\n if (!key) return null;\n const query = params.ownerId ? `?ownerId=${encodeURIComponent(params.ownerId)}` : \"\";\n const { data, error } = await callSdkApi(`/api/v1/sdk/phone-lines${query}`, key, options, { cache: \"no-store\" });\n if (error !== null || !isRecord(data)) return null;\n const line = isRecord(data.line) && typeof data.line.number === \"string\" ? data.line : null;\n return {\n line: line\n ? {\n number: line.number as string,\n label: stringOrNull(line.label),\n active: line.active === true,\n callers: numberOrNull(line.callers) ?? 0,\n }\n : null,\n request: lineRequestOf(data.request),\n };\n } catch {\n return null;\n }\n}\n\nexport type LineRequestResult = { ok: true; request: PhoneLineRequest } | { ok: false; error: string; code?: string };\n\nasync function lineRequestCall(\n path: string,\n body: unknown,\n options: CallsOptions\n): Promise<LineRequestResult> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(path, key, options, { method: \"POST\", body, cache: \"no-store\" });\n const request = isRecord(data) ? lineRequestOf(data.request) : null;\n if (error !== null || !request) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n return { ok: true, request };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Submit a customer's KYC so they get their own phone number (#716). Plivo\n * reviews it (usually minutes, sometimes a day); the `phone.kyc_status`\n * webhook says when. Rejected → fix and call again. Certificates: PDF, JPG or\n * PNG, under 4 MB together; they go straight to Plivo, Glitchgrab keeps no\n * copy. Refusals carry `code`: `KYC_PENDING`, `KYC_DONE`, `BAD_FILE`,\n * `PROVIDER`. Nothing is bought until {@link enableCalling}. Never throws.\n */\nexport function submitPhoneKyc(\n kyc: {\n ownerId: string;\n /** Legal name, as on the certificates. */\n businessName: string;\n email: string;\n addressLine1: string;\n city: string;\n state: string;\n /** 6-digit PIN code. */\n postalCode: string;\n registrationCertificate: Blob;\n gstCertificate: Blob;\n },\n options: CallsOptions = {}\n): Promise<LineRequestResult> {\n const form = new FormData();\n for (const k of [\"ownerId\", \"businessName\", \"email\", \"addressLine1\", \"city\", \"state\", \"postalCode\"] as const) {\n form.set(k, kyc[k]);\n }\n form.set(\"registrationCertificate\", kyc.registrationCertificate, \"registration-certificate\");\n form.set(\"gstCertificate\", kyc.gstCertificate, \"gst-certificate\");\n return lineRequestCall(\"/api/v1/sdk/phone-lines/kyc\", form, options);\n}\n\n/**\n * The firm turned calling on and agreed to the monthly charge for its own\n * number (#716). `agreedBy` (who agreed) and the time are kept as the billing\n * record. Once the KYC is accepted — now or later — Glitchgrab rents the\n * number and creates the line (`phone.line_ready` webhook); then seat staff\n * with {@link addLineCaller} and the same `ownerId`. `areaCode` = STD code to\n * search under (\"20\" Pune), else any free Indian number. Never throws.\n */\nexport function enableCalling(\n params: { ownerId: string; agreedBy: string; areaCode?: string },\n options: CallsOptions = {}\n): Promise<LineRequestResult> {\n return lineRequestCall(\"/api/v1/sdk/phone-lines/enable\", { ...params, agreedToCharge: true }, options);\n}\n\n/**\n * The firm turned calling off (#716): its line stops ringing and calling at\n * once, consent is cleared, and Navibyte is told to release the number.\n * {@link enableCalling} again switches the same line back on. Never throws.\n */\nexport function disableCalling(params: { ownerId: string }, options: CallsOptions = {}): Promise<LineRequestResult> {\n return lineRequestCall(\"/api/v1/sdk/phone-lines/disable\", params, options);\n}\n\nexport interface RegisteredWebhook {\n id: string;\n url: string;\n events: string[];\n}\n\n/**\n * Register your backend's webhook from code (#716) — scoped to this project,\n * no Settings clicks. https and a public host only. The `secret` comes back\n * once: verify every delivery's `X-Glitchgrab-Signature` (`sha256=` HMAC of the\n * raw body) with it. Same URL twice → refused; remove it first to rotate.\n * Never throws.\n */\nexport async function registerWebhook(\n webhook: { url: string; events: string[] },\n options: CallsOptions = {}\n): Promise<{ ok: true; webhook: RegisteredWebhook; secret: string } | { ok: false; error: string }> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error } = await callSdkApi(\"/api/v1/sdk/webhooks\", key, options, {\n method: \"POST\",\n body: webhook,\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data) || typeof data.id !== \"string\" || typeof data.secret !== \"string\") {\n return { ok: false, error: error ?? \"Unexpected response\" };\n }\n return {\n ok: true,\n secret: data.secret,\n webhook: {\n id: data.id,\n url: typeof data.url === \"string\" ? data.url : webhook.url,\n events: Array.isArray(data.events) ? data.events.filter((e): e is string => typeof e === \"string\") : [],\n },\n };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/** This project's webhooks, without secrets. `[]` on any failure. Never throws. */\nexport async function listWebhooks(options: CallsOptions = {}): Promise<RegisteredWebhook[]> {\n try {\n const key = callsKey(options);\n if (!key) return [];\n const { data, error } = await callSdkApi(\"/api/v1/sdk/webhooks\", key, options, { cache: \"no-store\" });\n if (error !== null || !isRecord(data) || !Array.isArray(data.webhooks)) return [];\n return data.webhooks.filter(isRecord).flatMap((w) =>\n typeof w.id === \"string\" && typeof w.url === \"string\"\n ? [{ id: w.id, url: w.url, events: Array.isArray(w.events) ? w.events.filter((e): e is string => typeof e === \"string\") : [] }]\n : []\n );\n } catch {\n return [];\n }\n}\n\n/** Remove one of this project's webhooks. Never throws. */\nexport async function removeWebhook(id: string, options: CallsOptions = {}): Promise<{ ok: true } | { ok: false; error: string }> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { error } = await callSdkApi(`/api/v1/sdk/webhooks/${encodeURIComponent(id)}`, key, options, {\n method: \"DELETE\",\n cache: \"no-store\",\n });\n return error === null ? { ok: true } : { ok: false, error };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Take a caller your key added off the line, with the phones kept for them —\n * e.g. when they leave your admin team. Someone the line's owner added →\n * `code: \"NOT_KEY_CALLER\"`; not on it → `code: \"NOT_ON_LINE\"`. Never throws.\n *\n * `{ ownerId }` = only their seat on that customer's line (#723): staff of two\n * firms keep the other one, and access + phones go with their last seat.\n * Omitted = off every line of the project.\n *\n * ```ts\n * await removeLineCaller(staff.email, { ownerId: firm.id });\n * ```\n */\nexport function removeLineCaller(email: string, options: LineCallsOptions = {}): Promise<RingNumberWriteResult> {\n return ringNumberWrite(\n `/api/v1/sdk/phone-lines/callers/${encodeURIComponent(email)}${ownerQuery(options)}`,\n \"DELETE\",\n undefined,\n options,\n () => ({})\n );\n}\n\n/** One saved ring number, verified or not — the management view (#654). */\nexport interface RingNumber {\n id: string;\n /** E.164, `+91…`. */\n phone: string;\n label: string | null;\n position: number;\n /** Only verified numbers are ever rung. */\n verified: boolean;\n /** A test ring is out right now — poll until `verified`. */\n verifying: boolean;\n isDefault: boolean;\n}\n\nexport interface RingNumberList {\n operator: { name: string | null };\n /** Their account mobile — still what rings while nothing here is verified. */\n accountPhone: string | null;\n usingAccount: boolean;\n numbers: RingNumber[];\n}\n\nexport type RingNumberWriteResult<T = object> =\n | ({ ok: true } & T)\n | { ok: false; error: string; code?: string };\n\nfunction ringNumbersPath(rest = \"\"): string {\n return `/api/v1/sdk/phone-lines/operator/numbers${rest}`;\n}\n\nasync function ringNumberWrite<T extends Record<string, unknown>>(\n path: string,\n method: \"POST\" | \"PATCH\" | \"DELETE\",\n body: Record<string, unknown> | undefined,\n options: CallsOptions,\n pick: (data: Record<string, unknown>) => T\n): Promise<RingNumberWriteResult<T>> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(path, key, options, { method, body, cache: \"no-store\" });\n if (error !== null || !isRecord(data)) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n return { ok: true, ...pick(data) } as RingNumberWriteResult<T>;\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Every phone saved for one person on the project's line, unverified included\n * — for your \"manage my phones\" screen. Ring numbers are managed only from\n * your product; Glitchgrab shows them read-only. A refusal carries `code`\n * (`NOT_ON_LINE`), so \"not on the line\" reads differently from \"network down\".\n * `{ ownerId }` for someone on a customer's line. Never throws.\n */\nexport async function listRingNumbers(\n operator: FollowUpOperator,\n options: LineCallsOptions = {}\n): Promise<RingNumberWriteResult<RingNumberList>> {\n try {\n const key = callsKey(options);\n if (!key) return { ok: false, error: NO_KEY_ERROR };\n const { data, error, code } = await callSdkApi(ringNumbersPath(`?${operatorQuery(operator, options)}`), key, options, {\n cache: \"no-store\",\n });\n if (error !== null || !isRecord(data)) return { ok: false, error: error ?? \"Unexpected response\", ...(code ? { code } : {}) };\n const op = isRecord(data.operator) ? data.operator : {};\n return {\n ok: true,\n operator: { name: stringOrNull(op.name) },\n accountPhone: stringOrNull(data.accountPhone),\n usingAccount: data.usingAccount === true,\n numbers: Array.isArray(data.numbers)\n ? data.numbers\n .filter(isRecord)\n .filter((n) => typeof n.id === \"string\" && typeof n.phone === \"string\")\n .map((n) => ({\n id: n.id as string,\n phone: n.phone as string,\n label: stringOrNull(n.label),\n position: numberOrNull(n.position) ?? 0,\n verified: n.verified === true,\n verifying: n.verifying === true,\n isDefault: n.isDefault === true,\n }))\n : [],\n };\n } catch {\n return { ok: false, error: \"Could not reach Glitchgrab\" };\n }\n}\n\n/**\n * Save a phone for `operator` and place its test ring at once: the project's\n * line calls it and whoever holds it presses 1. Never rung for a call until\n * then; the line's owner is alerted when it is verified. Max 5 per person.\n * `testRing: \"failed\"` = saved but not rung — retry with {@link testRingNumber}.\n * `{ ownerId }` = they sit on that customer's line, and its number test-rings.\n */\nexport function addRingNumber(\n operator: FollowUpOperator,\n number: { phone: string; label?: string },\n options: LineCallsOptions = {}\n): Promise<RingNumberWriteResult<{ id: string; testRing: \"ringing\" | \"failed\"; verifyError: string | null }>> {\n const body = { operator, ...number, ...(options.ownerId ? { ownerId: options.ownerId } : {}) };\n return ringNumberWrite(ringNumbersPath(), \"POST\", body, options, (d) => ({\n id: typeof d.id === \"string\" ? d.id : \"\",\n testRing: d.testRing === \"failed\" ? \"failed\" : \"ringing\",\n verifyError: stringOrNull(d.verifyError),\n }));\n}\n\n/** Rename, make Default (verified only) or move one place up/down. `{ ownerId }` as in {@link LineCallsOptions}. Never throws. */\nexport function updateRingNumber(\n id: string,\n change: { label?: string | null; makeDefault?: true; move?: \"up\" | \"down\" },\n options: LineCallsOptions = {}\n): Promise<RingNumberWriteResult> {\n return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}${ownerQuery(options)}`), \"PATCH\", change, options, () => ({}));\n}\n\n/** Remove one; the next verified number becomes Default. `{ ownerId }` as in {@link LineCallsOptions}. Never throws. */\nexport function removeRingNumber(id: string, options: LineCallsOptions = {}): Promise<RingNumberWriteResult> {\n return ringNumberWrite(\n ringNumbersPath(`/${encodeURIComponent(id)}${ownerQuery(options)}`),\n \"DELETE\",\n undefined,\n options,\n () => ({})\n );\n}\n\n/**\n * Place the test ring again (6 per person, 30 per project an hour). `{ ownerId }`\n * = rings from that customer's line, e.g. retrying a firm caller's failed ring. Never throws.\n */\nexport function testRingNumber(\n id: string,\n options: LineCallsOptions = {}\n): Promise<RingNumberWriteResult<{ verified: boolean }>> {\n return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}/verify${ownerQuery(options)}`), \"POST\", undefined, options, (d) => ({\n verified: d.verified === true,\n }));\n}\n\nexport type { ReportResult, ReportSeverity, ReportType } from \"./types\";\n","import type { RuntimeInfo } from \"./types\";\n\n/**\n * Runtime health at the moment of the crash. `deviceInfo` says what the machine\n * is; this says what shape it was in — a leak that ran the heap to its ceiling,\n * a 2G connection that starved a fetch, a bug that only shows up after ten\n * minutes on the page or on the fifth error in a row.\n */\n\nconst pageLoadedAt = Date.now();\nlet errorCount = 0;\n\n/** Bumped by every auto-captured error so a report says which one in the run it was. */\nexport function incrementErrorCount(): number {\n errorCount += 1;\n return errorCount;\n}\n\n/** Test-only — reset the per-session counter. */\nexport function resetErrorCount(): void {\n errorCount = 0;\n}\n\ninterface MemoryCapableWindow {\n memory?: { usedJSHeapSize?: number; jsHeapSizeLimit?: number };\n}\n\ninterface NetworkInformation {\n effectiveType?: string;\n downlink?: number;\n rtt?: number;\n saveData?: boolean;\n}\n\nconst toMb = (bytes: number | undefined): number | undefined =>\n typeof bytes === \"number\" ? Math.round((bytes / 1048576) * 10) / 10 : undefined;\n\nexport function captureRuntimeInfo(): RuntimeInfo | null {\n try {\n if (typeof window === \"undefined\") return null;\n\n // Chromium-only, and absent entirely under cross-origin isolation.\n const memory = (performance as unknown as MemoryCapableWindow)?.memory;\n // Chromium + Android only.\n const connection = (navigator as unknown as { connection?: NetworkInformation })?.connection;\n\n return {\n timeOnPageMs: Date.now() - pageLoadedAt,\n errorCount,\n visibility: typeof document !== \"undefined\" ? document.visibilityState : \"unknown\",\n ...(memory\n ? {\n jsHeapUsedMb: toMb(memory.usedJSHeapSize),\n jsHeapLimitMb: toMb(memory.jsHeapSizeLimit),\n }\n : {}),\n ...(connection\n ? {\n connectionType: connection.effectiveType,\n downlinkMbps: connection.downlink,\n rttMs: connection.rtt,\n saveData: connection.saveData,\n }\n : {}),\n };\n } catch {\n return null;\n }\n}\n","import type {\n CapturedContext,\n DeviceInfo,\n FeedbackPayload,\n FeedbackResult,\n LeadInput,\n LeadResult,\n ReportPayload,\n ReportResult,\n} from \"./types\";\n// The React-free subpath: utils.ts is also bundled into `glitchgrab/server`.\nimport { assistOffline, postJson, toAssistTurnResult } from \"@glitchgrab/report-ui/assist-response\";\nimport type { AssistTurnResult } from \"@glitchgrab/report-ui\";\nimport { getBreadcrumbs } from \"./breadcrumbs\";\nimport { captureRuntimeInfo } from \"./runtime\";\nimport { getAppContext, getRelease } from \"./app-context\";\n\nconst SENSITIVE_PARAMS = [\n \"token\", \"key\", \"secret\", \"password\", \"passwd\", \"auth\", \"authorization\",\n \"session\", \"sessionid\", \"session_id\", \"api_key\", \"apikey\", \"access_token\",\n \"refresh_token\", \"client_secret\", \"code\", \"state\", \"nonce\", \"credential\", \"private\",\n];\n\nexport function sanitizeUrl(url: string): string {\n try {\n const parsed = new URL(url);\n const params = new URLSearchParams(parsed.search);\n let modified = false;\n for (const key of Array.from(params.keys())) {\n if (SENSITIVE_PARAMS.some((s) => key.toLowerCase().includes(s))) {\n params.set(key, \"[REDACTED]\");\n modified = true;\n }\n }\n if (modified) parsed.search = params.toString();\n return parsed.toString();\n } catch {\n return url.split(\"?\")[0] ?? url;\n }\n}\n\nexport function captureDeviceInfo(): DeviceInfo | null {\n try {\n if (typeof window === \"undefined\") return null;\n return {\n screenWidth: screen.width,\n screenHeight: screen.height,\n viewportWidth: window.innerWidth,\n viewportHeight: window.innerHeight,\n platform: navigator.platform ?? \"unknown\",\n language: navigator.language ?? \"unknown\",\n online: navigator.onLine,\n colorScheme: window.matchMedia(\"(prefers-color-scheme: dark)\").matches ? \"dark\" : \"light\",\n devicePixelRatio: window.devicePixelRatio ?? 1,\n };\n } catch {\n return null;\n }\n}\n\nexport function captureContext(visitedPages: string[]): CapturedContext {\n try {\n return {\n url: sanitizeUrl(typeof window !== \"undefined\" ? window.location.href : \"\"),\n userAgent: typeof navigator !== \"undefined\" ? navigator.userAgent : \"\",\n timestamp: new Date().toISOString(),\n visitedPages: visitedPages.map(sanitizeUrl),\n breadcrumbs: getBreadcrumbs(),\n deviceInfo: captureDeviceInfo(),\n runtime: captureRuntimeInfo(),\n appContext: getAppContext(),\n release: getRelease(),\n };\n } catch {\n return {\n url: \"\",\n userAgent: \"\",\n timestamp: new Date().toISOString(),\n visitedPages: [],\n breadcrumbs: [],\n deviceInfo: null,\n runtime: null,\n appContext: {},\n };\n }\n}\n\n/**\n * Metadata every report carries regardless of how it was raised — release,\n * runtime health, and the host app's own context. One builder so a user-filed\n * report and an auto-captured crash describe the same world; the payload sites\n * only add what is specific to them.\n *\n * App context is prefixed so a key named `timestamp` or `status` can't quietly\n * overwrite a field the dashboard relies on.\n */\nexport function contextMetadata(context: CapturedContext): Record<string, string> {\n try {\n const runtime = context.runtime;\n const appContext = Object.entries(context.appContext ?? {}).reduce<Record<string, string>>(\n (acc, [key, value]) => {\n acc[`ctx_${key}`] = value;\n return acc;\n },\n {}\n );\n\n return {\n ...(context.release ? { release: context.release } : {}),\n ...(runtime\n ? {\n timeOnPageMs: String(runtime.timeOnPageMs),\n errorCount: String(runtime.errorCount),\n visibility: runtime.visibility,\n ...(runtime.jsHeapUsedMb !== undefined\n ? { jsHeapUsedMb: String(runtime.jsHeapUsedMb) }\n : {}),\n ...(runtime.jsHeapLimitMb !== undefined\n ? { jsHeapLimitMb: String(runtime.jsHeapLimitMb) }\n : {}),\n ...(runtime.connectionType ? { connectionType: runtime.connectionType } : {}),\n ...(runtime.downlinkMbps !== undefined\n ? { downlinkMbps: String(runtime.downlinkMbps) }\n : {}),\n ...(runtime.rttMs !== undefined ? { rttMs: String(runtime.rttMs) } : {}),\n ...(runtime.saveData !== undefined ? { saveData: String(runtime.saveData) } : {}),\n }\n : {}),\n ...appContext,\n };\n } catch {\n return {};\n }\n}\n\nexport const DEFAULT_BASE_URL = \"https://glitchgrab.dev\";\nconst MAX_RETRIES = 3;\nconst RETRY_BASE_MS = 500;\n\n/**\n * How long one report attempt may take before it is abandoned and retried.\n * The server files a report in a few seconds; on a flaky mobile network the\n * response can get lost and fetch then hangs ~30s before failing (#557). The\n * body share assumes a slow ~100 KB/s uplink, so a large screenshot is never\n * cut off mid-upload. A retry of a report the server already has is answered\n * with that report, not filed again. The final attempt runs without one.\n */\nexport function reportAttemptTimeoutMs(bodyLength: number): number {\n return 10_000 + Math.ceil(bodyLength / 100_000) * 1_000;\n}\n\n/**\n * Send a report and return the result.\n * Never throws — returns null on failure.\n */\nexport async function sendReport(\n payload: ReportPayload,\n baseUrl?: string\n): Promise<ReportResult | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/sdk/report`;\n const body = JSON.stringify(payload);\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${payload.token}`,\n };\n\n // keepalive has a 64KB body limit in browsers — skip it for large payloads (e.g., screenshots)\n const useKeepalive = body.length < 60_000;\n const timeoutMs = reportAttemptTimeoutMs(body.length);\n\n for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {\n // The last attempt has no deadline: a slow uplink still gets there, and\n // sendBeacon can't carry a screenshot-sized body if it gave up.\n const isLast = attempt === MAX_RETRIES - 1;\n const controller = !isLast && typeof AbortController === \"function\" ? new AbortController() : null;\n const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;\n try {\n const response = await fetch(url, {\n method: \"POST\",\n headers,\n body,\n ...(useKeepalive ? { keepalive: true } : {}),\n ...(controller ? { signal: controller.signal } : {}),\n });\n\n if (response.ok) {\n const envelope = (await response.json()) as {\n success: boolean;\n data?: Omit<ReportResult, \"success\">;\n };\n return { success: envelope.success, ...(envelope.data ?? {}) };\n }\n\n if (response.status >= 400 && response.status < 500 && response.status !== 429) {\n return null;\n }\n } catch {\n // Will retry\n } finally {\n if (timer) clearTimeout(timer);\n }\n\n if (attempt < MAX_RETRIES - 1) {\n await new Promise((resolve) =>\n setTimeout(resolve, RETRY_BASE_MS * Math.pow(2, attempt))\n );\n }\n }\n\n // Fallback: sendBeacon (fire-and-forget, no result)\n if (typeof navigator !== \"undefined\" && navigator.sendBeacon) {\n try {\n navigator.sendBeacon(url, new Blob([body], { type: \"application/json\" }));\n } catch {\n // Silently fail\n }\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\n/**\n * Save a star rating your end-user left about your app.\n * Never throws — returns null on failure.\n *\n * Unlike `sendReport`, this is always a deliberate user action with a dialog\n * open in front of them, so it retries on transient failure but skips the\n * sendBeacon fallback: the caller needs a real result to show a thank-you.\n */\nexport async function sendFeedback(\n payload: FeedbackPayload,\n baseUrl?: string\n): Promise<FeedbackResult | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/sdk/feedback`;\n const body = JSON.stringify(payload);\n\n for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {\n try {\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${payload.token}`,\n },\n body,\n });\n\n if (response.ok) {\n const envelope = (await response.json()) as {\n success: boolean;\n data?: Omit<FeedbackResult, \"success\">;\n };\n return { success: envelope.success, ...(envelope.data ?? {}) };\n }\n\n // 4xx other than rate limiting is a caller mistake — retrying won't help.\n if (response.status >= 400 && response.status < 500 && response.status !== 429) {\n return null;\n }\n } catch {\n // Will retry\n }\n\n if (attempt < MAX_RETRIES - 1) {\n await new Promise((resolve) =>\n setTimeout(resolve, RETRY_BASE_MS * Math.pow(2, attempt))\n );\n }\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\n/**\n * POST a lead (#534). Retries transient failures like {@link sendFeedback}; a\n * 4xx other than 429 is a caller mistake and returns null at once.\n */\nexport async function sendLead(\n token: string,\n lead: LeadInput & { pageUrl?: string },\n baseUrl?: string\n): Promise<LeadResult | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/sdk/leads`;\n const body = JSON.stringify(lead);\n\n for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {\n try {\n const response = await fetch(url, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\", Authorization: `Bearer ${token}` },\n body,\n });\n\n if (response.ok) {\n const envelope = (await response.json()) as {\n success: boolean;\n data?: { leadId?: string; created?: boolean };\n };\n return { success: envelope.success, id: envelope.data?.leadId, created: envelope.data?.created };\n }\n\n if (response.status >= 400 && response.status < 500 && response.status !== 429) {\n return null;\n }\n } catch {\n // Will retry\n }\n\n if (attempt < MAX_RETRIES - 1) {\n await new Promise((resolve) => setTimeout(resolve, RETRY_BASE_MS * Math.pow(2, attempt)));\n }\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\nexport interface EnhanceContext {\n url?: string;\n visitedPages?: string[];\n breadcrumbs?: Array<{ type: string; message: string }>;\n}\n\n/**\n * The signed-in user as the host's server vouched for them (`signSession`).\n *\n * Rides in the request BODY, never a header: every token route sets its own\n * CORS allow-list of headers, so a new header would fail preflight on all of\n * them. Omitted entirely when there is no signature — an unsigned id proves\n * nothing, and the server reads its absence as \"unsigned\".\n */\nexport interface SignedIdentity {\n userId?: string | null;\n signature?: string | null;\n}\n\n/** `{ userId, signature }` to spread into a JSON body, or nothing. */\nexport function signedFields(identity?: SignedIdentity | null): { userId?: string; signature?: string } {\n if (!identity?.signature || !identity.userId) return {};\n return { userId: identity.userId, signature: identity.signature };\n}\n\n/**\n * Polish raw description text via the Glitchgrab AI enhance endpoint.\n * Returns the polished text on success, or the original text on any failure.\n * Never throws.\n */\nexport async function enhanceText(\n text: string,\n token: string,\n baseUrl?: string,\n screenshot?: string | null,\n context?: EnhanceContext | null,\n identity?: SignedIdentity | null\n): Promise<string> {\n try {\n return await enhanceTextOrThrow(text, token, baseUrl, screenshot, context, identity);\n } catch {\n return text;\n }\n}\n\n/**\n * `enhanceText` for the report dialog: throws an Error whose message the\n * reporter can read, so the button can say why nothing changed (#573) instead\n * of silently handing the text back.\n */\nexport async function enhanceTextOrThrow(\n text: string,\n token: string,\n baseUrl?: string,\n screenshot?: string | null,\n context?: EnhanceContext | null,\n identity?: SignedIdentity | null\n): Promise<string> {\n const trimmed = text.trim();\n if (!trimmed) return text;\n const unavailable = \"AI enhance isn't available right now — send your report as written.\";\n let response: Response;\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/ai/enhance-text`;\n response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({\n text: trimmed,\n ...(screenshot ? { screenshot } : {}),\n ...(context ? { context } : {}),\n ...signedFields(identity),\n }),\n });\n } catch {\n throw new Error(unavailable);\n }\n if (response.status === 429) {\n throw new Error(\"Too many AI enhance tries — wait a while, or send your report as written.\");\n }\n const envelope = (await response.json().catch(() => null)) as {\n success: boolean;\n data?: { text?: string };\n error?: string;\n } | null;\n if (response.ok && envelope?.success && typeof envelope.data?.text === \"string\") {\n return envelope.data.text;\n }\n // 400 is about the reporter's own text (\"Text too long — max 5000\n // characters\") — worth showing as is. Everything else is ours.\n if (response.status === 400 && typeof envelope?.error === \"string\") {\n throw new Error(envelope.error);\n }\n throw new Error(unavailable);\n}\n\n/**\n * \"Is this already filed?\" for the report dialog's plain form. Asked when Send\n * is pressed; a match lets the reporter add to the open issue instead of\n * opening a second one.\n *\n * `[]` when the project has it off, `null` on any failure — the dialog sends\n * straight away on either. Never throws.\n */\nexport async function findSimilarIssues(\n description: string,\n token: string,\n baseUrl?: string,\n identity?: SignedIdentity | null,\n /** Attached files as text (#563) — the server reads a short summary of them. */\n files?: { name: string; content: string }[],\n /** The assistant chat the draft came from (#697) — its declined issues are left out. */\n conversationId?: string\n): Promise<{ number: number; title: string; url: string; status?: string }[] | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/reports/similar`;\n // No `keepalive`: with files the body can pass its 64 KB cap, and the\n // request would fail without a word.\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({\n text: description,\n ...(files?.length ? { files } : {}),\n ...(conversationId ? { conversationId } : {}),\n ...signedFields(identity),\n }),\n });\n const envelope = (await response.json().catch(() => null)) as {\n success?: boolean;\n data?: { enabled?: boolean; matches?: { number: number; title: string; url: string; status?: string }[] };\n } | null;\n if (!response.ok || !envelope?.success) return null;\n if (!envelope.data?.enabled) return [];\n return Array.isArray(envelope.data.matches) ? envelope.data.matches : [];\n } catch {\n return null;\n }\n}\n\n/**\n * The text inside a PDF the reporter attached (#563), so the assistant and the\n * duplicate check can read it. Called once per PDF, when it is attached.\n *\n * `\"\"` for a PDF with no text layer (a scan); `null` when the project has the\n * assistant off, the file is too big, or anything failed. Never throws.\n */\nexport async function readAttachment(\n file: { name: string; dataUrl: string },\n token: string,\n baseUrl?: string,\n identity?: SignedIdentity | null\n): Promise<string | null> {\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/ai/read-file`;\n // No `keepalive`: a PDF is far past its 64 KB body cap.\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${token}`,\n },\n body: JSON.stringify({ name: file.name, data: file.dataUrl, ...signedFields(identity) }),\n });\n const envelope = (await response.json().catch(() => null)) as {\n success?: boolean;\n data?: { enabled?: boolean; text?: string };\n } | null;\n if (!response.ok || !envelope?.success || !envelope.data?.enabled) return null;\n return typeof envelope.data.text === \"string\" ? envelope.data.text : null;\n } catch {\n return null;\n }\n}\n\n/**\n * One turn of the AI report assistant (#330).\n *\n * Same never-throw contract as everything else in the SDK: a failure comes back\n * as `degraded` and the dialog quietly falls back to the plain form. An\n * end-user of the host app must never see a Glitchgrab error, and must never be\n * blocked from filing a bug because a model was busy.\n */\nexport async function assistTurn(\n params: {\n messages: { role: \"user\" | \"assistant\"; content: string }[];\n conversationId: string | null;\n screenshot?: string | null;\n context?: Record<string, unknown> | null;\n /**\n * Who the host app says is logged in, from the provider's `session` prop.\n * Stored against the conversation so a chat and the report it produced\n * agree on who filed — one SDK token covers every end user of the host app,\n * so without this every stranger's chat looks like the same person.\n */\n reporter?: {\n key?: string | null;\n name?: string | null;\n email?: string | null;\n /** `signSession()` output for `key`; absent when the host did not sign. */\n signature?: string | null;\n };\n },\n token: string,\n baseUrl?: string,\n /** Body upload progress, 0–1 — the sheet's \"Sending…\" line (#646). */\n onUploadProgress?: (fraction: number) => void\n): Promise<AssistTurnResult> {\n const offline = assistOffline();\n try {\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/ai/report-chat`;\n const response = await postJson(url, params, {\n headers: { Authorization: `Bearer ${token}` },\n onUploadProgress,\n });\n // One mapping for every host (packages/report-ui/src/assist-response.ts):\n // the server's degrade reason passes through, and nothing the sheet renders\n // is dropped.\n return toAssistTurnResult(response.ok, response.body);\n } catch {\n return offline;\n }\n}\n\n/**\n * Send an audio Blob to the Glitchgrab STT proxy (which calls Sarvam).\n * Returns the transcript string, or \"\" on any failure. Never throws.\n */\nexport async function transcribeAudio(\n blob: Blob,\n token: string,\n baseUrl?: string,\n identity?: SignedIdentity | null\n): Promise<string> {\n try {\n if (blob.size === 0) return \"\";\n const url = `${baseUrl ?? DEFAULT_BASE_URL}/api/v1/sdk/stt`;\n const form = new FormData();\n form.append(\"file\", blob, \"audio.webm\");\n const signed = signedFields(identity);\n if (signed.userId && signed.signature) {\n form.append(\"userId\", signed.userId);\n form.append(\"signature\", signed.signature);\n }\n const response = await fetch(url, {\n method: \"POST\",\n headers: { Authorization: `Bearer ${token}` },\n body: form,\n });\n if (!response.ok) return \"\";\n const envelope = (await response.json()) as {\n success: boolean;\n data?: { transcript?: string };\n };\n if (envelope?.success && typeof envelope.data?.transcript === \"string\") {\n return envelope.data.transcript;\n }\n return \"\";\n } catch {\n return \"\";\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA,yBAA2B;;;ACV3B,IAAM,eAAe,KAAK,IAAI;;;AC8HvB,IAAM,mBAAmB;AAChC,IAAM,cAAc;AACpB,IAAM,gBAAgB;AAUf,SAAS,uBAAuB,YAA4B;AACjE,SAAO,MAAS,KAAK,KAAK,aAAa,GAAO,IAAI;AACpD;AAMA,eAAsB,WACpB,SACA,SAC8B;AA9JhC;AA+JE,MAAI;AACF,UAAM,MAAM,GAAG,4BAAW,gBAAgB;AAC1C,UAAM,OAAO,KAAK,UAAU,OAAO;AACnC,UAAM,UAAkC;AAAA,MACtC,gBAAgB;AAAA,MAChB,eAAe,UAAU,QAAQ,KAAK;AAAA,IACxC;AAGA,UAAM,eAAe,KAAK,SAAS;AACnC,UAAM,YAAY,uBAAuB,KAAK,MAAM;AAEpD,aAAS,UAAU,GAAG,UAAU,aAAa,WAAW;AAGtD,YAAM,SAAS,YAAY,cAAc;AACzC,YAAM,aAAa,CAAC,UAAU,OAAO,oBAAoB,aAAa,IA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