@binaryandthread/mcp 0.4.14 → 0.4.15

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/dist/bin.js CHANGED
@@ -632,12 +632,16 @@ function supplierOf(p) {
632
632
  var PRINTFUL_BLANK_COLOURS = {
633
633
  "ascolour-5082": ["black", "white", "coal", "eucalyptus", "bone", "grey", "khaki", "indigo"],
634
634
  // TB124, TB126
635
- "bellacanvas-4711": ["asphalt", "dust", "navy", "maroon", "forest", "jet"]
635
+ "bellacanvas-4711": ["asphalt", "dust", "navy", "maroon", "forest", "jet"],
636
636
  // TB128: the sweatshirt; TB129: + jet (its Black)
637
+ "yupoong-6606": ["blackmesh"]
638
+ // TB268: the cap
637
639
  };
638
640
  var PRINTFUL_BLANK_SIZES = {
639
641
  "ascolour-5082": ["s", "m", "l", "xl", "2xl"],
640
- "bellacanvas-4711": ["s", "m", "l", "xl", "2xl"]
642
+ "bellacanvas-4711": ["s", "m", "l", "xl", "2xl"],
643
+ "yupoong-6606": ["os"]
644
+ // TB268: the cap is one size
641
645
  };
642
646
  function printfulBlankOf(p) {
643
647
  const v = (p.metadata ?? {})["bt_blank"];
@@ -816,7 +820,9 @@ var COLOUR_LABELS = {
816
820
  grey: "Faded Grey",
817
821
  khaki: "Faded Khaki",
818
822
  indigo: "Faded Indigo",
819
- jet: "Black"
823
+ jet: "Black",
824
+ // TB268: the cap (Printful 252 "Black/ White"): black front, white mesh back.
825
+ blackmesh: "Black with white mesh"
820
826
  };
821
827
  function colourLabel(colour) {
822
828
  const c = colour.trim();
@@ -1107,7 +1113,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
1107
1113
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
1108
1114
 
1109
1115
  // src/version.ts
1110
- var VERSION = "0.4.14";
1116
+ var VERSION = "0.4.15";
1111
1117
 
1112
1118
  // src/onboarding.ts
1113
1119
  var GETTING_STARTED_URI = "bt://getting-started";
package/dist/bin.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../../store-client/src/redact.ts","../../store-client/src/client.ts","../../store-client/src/defaults.ts","../../store-client/src/host-allowlist.ts","../../store-client/src/fit.ts","../../store-client/src/colour.ts","../../store-client/src/order-stage.ts","../../store-client/src/cart-ship-to.ts","../../store-client/src/product.ts","../../store-client/src/tax.ts","../../store-client/src/money-rows.ts","../src/server.ts","../src/version.ts","../src/onboarding.ts","../src/tools.ts","../src/sanitize.ts","../src/images.ts","../src/eggs.ts","../src/jwt.ts","../src/config.ts","../src/bin.ts"],"sourcesContent":["// Secret/PII redaction for logs and error envelopes (SECURITY G10).\n//\n// The single rule for the whole channel: Authorization headers, tokens, and full\n// emails NEVER reach a log line, an error message, or an MCP/CLI transcript. This\n// function is applied by the StoreClient's logger and error mapper before anything\n// is emitted. It is consumed by BOTH the CLI and (Wave 4) the MCP — one redactor.\n\nconst SENSITIVE_KEYS = new Set([\n \"authorization\",\n \"token\",\n \"access_token\",\n \"refresh_token\",\n \"jwt\",\n \"idempotency-key\",\n \"x-publishable-api-key\",\n \"password\",\n \"secret\",\n \"cookie\",\n \"set-cookie\",\n])\n\nconst MASK = \"[REDACTED]\"\n\n// Bearer-token-in-a-string (e.g. \"Authorization: Bearer abc.def.ghi\" or a stray \"Bearer xyz\").\nconst BEARER_RE = /Bearer\\s+[A-Za-z0-9._\\-]+/gi\n// A plausible email anywhere in a string.\nconst EMAIL_RE = /([^\\s@]+)@([^\\s@]+\\.[^\\s@]+)/g\n\n/** Mask an email to `d***@x` form (first char + stars, host first char). */\nexport function maskEmail(email: string): string {\n const at = email.indexOf(\"@\")\n if (at <= 0) {\n return email\n }\n const local = email.slice(0, at)\n const host = email.slice(at + 1)\n const localMasked = local[0] + \"*\".repeat(Math.max(2, local.length - 1))\n const hostMasked = host.length > 0 ? host[0] : \"x\"\n return `${localMasked}@${hostMasked}`\n}\n\nfunction redactString(s: string): string {\n return s.replace(BEARER_RE, \"Bearer [REDACTED]\").replace(EMAIL_RE, (_m, _l, _h) =>\n maskEmail(`${_l}@${_h}`)\n )\n}\n\n/**\n * Deep-redact an object/array/string. Returns a structurally-similar value with\n * sensitive keys masked, Bearer tokens stripped, and emails masked. Never throws.\n *\n * `_seen` memoises input-object → its REDACTED output. This does double duty: it makes the\n * function cycle-safe (a revisited object resolves to its own — possibly still-filling — output\n * container) AND ensures a REPEATED reference is redacted every time. The previous WeakSet guard\n * returned the *original* object on revisit, leaking any token/email reachable via a 2nd path (G10).\n * The output container is registered BEFORE recursing so a cycle points at the redacted copy.\n */\nexport function redact<T>(input: T, _seen?: Map<object, unknown>): T {\n if (input == null) {\n return input\n }\n if (typeof input === \"string\") {\n return redactString(input) as unknown as T\n }\n if (typeof input !== \"object\") {\n return input\n }\n\n const seen = _seen ?? new Map<object, unknown>()\n const existing = seen.get(input as object)\n if (existing !== undefined) {\n return existing as T\n }\n\n if (Array.isArray(input)) {\n const out: unknown[] = []\n seen.set(input as object, out) // register before recursing (cycle-safe)\n for (const v of input) {\n out.push(redact(v, seen))\n }\n return out as unknown as T\n }\n\n const out: Record<string, unknown> = {}\n seen.set(input as object, out) // register before recursing (cycle-safe)\n for (const [key, value] of Object.entries(input as Record<string, unknown>)) {\n if (SENSITIVE_KEYS.has(key.toLowerCase())) {\n out[key] = MASK\n } else if (key.toLowerCase() === \"email\" && typeof value === \"string\") {\n out[key] = maskEmail(value)\n } else {\n out[key] = redact(value, seen)\n }\n }\n return out as unknown as T\n}\n","// StoreClient — the ONE shared HTTP client over the B&T Medusa Store API.\n//\n// Design (ROBUSTNESS §0): the clients are DUMB. Every dangerous property lives in the\n// backend. This module is the single place the channel's HTTP-client invariants live so\n// they are written once and contract-tested once (the CLI and the Wave-4 MCP both use it):\n//\n// - publishable key on EVERY request (public by design, T6); customer JWT when present\n// - Idempotency-Key (crypto.randomUUID) on EVERY mutation, STABLE across a retry — honoured by\n// the checkout MINT (per-cart session reuse) and by the /store/carts idempotency middleware,\n// which answers 409 IDEMPOTENCY_CONFLICT + Retry-After while the first request is in flight (§2)\n// - retry 5xx, network errors, 429 and 409 IDEMPOTENCY_CONFLICT; NEVER retry another 4xx (F-A1/F-I2)\n// - region resolved before pricing — never a bare currency literal (F-A2/INV-11)\n// - 404, or a COMPLETED-cart answer, on a cart mutation → RecoverableCartError (CLI makes a fresh\n// cart). Any other 409 is a plain StoreApiError, never a reason to throw a live cart away (F-B2)\n// - all logs + error envelopes pass through redact() — no token/email ever leaks (G10)\n// - NO secret key is ever stored or sent — publishable key only (T6)\n\nimport { redact } from \"./redact\"\nimport type {\n CartLike,\n ClientLogger,\n DropRow,\n HeadlessCheckout,\n OrderSummary,\n PollResult,\n ProductLike,\n RegionLike,\n RegionResolution,\n ReportResult,\n RequestLinkResult,\n RetryPolicy,\n ShippingOptionLike,\n StoreClientOptions,\n VerifyLinkResult,\n WhoAmI,\n} from \"./types\"\n\nconst DEFAULT_RETRY: RetryPolicy = {\n attempts: 3,\n baseDelayMs: 200,\n maxDelayMs: 2000,\n}\n\n/** Cap on how long we'll honor a server's Retry-After, so a hostile/huge value can't hang us. */\nconst RETRY_AFTER_MAX_MS = 10_000\n\n/**\n * Per-attempt request timeout. A black-holed connection (socket accepted, no response) would\n * otherwise never settle, hanging every command/tool AND defeating the checkout poll's 20-min\n * deadline (F-I3) — the deadline can't be re-checked while stuck awaiting a dead fetch. 15s is\n * generous for the Store API while still failing closed on a hang.\n */\nconst DEFAULT_TIMEOUT_MS = 15_000\n\n/**\n * Extra fields to request on product reads. `+metadata` ADDS metadata to Medusa's default field set\n * (it doesn't replace it — variants + calculated_price still come through). metadata is where lore /\n * lore_sources / per-product spec fields live, and the Store API omits it unless asked. The `+` is\n * URL-encoded to %2B by URLSearchParams, which Medusa decodes back to `+metadata`.\n */\nconst PRODUCT_FIELDS = \"+metadata\"\n\n/**\n * Parse an HTTP Retry-After header → ms, or null if absent/unparseable. Accepts both forms:\n * delta-seconds (\"2\") and an HTTP-date. Capped at RETRY_AFTER_MAX_MS (d12).\n */\nfunction parseRetryAfterMs(res: Response): number | null {\n let raw: string | null = null\n try {\n raw = res.headers?.get?.(\"retry-after\") ?? null\n } catch {\n return null\n }\n if (!raw) {\n return null\n }\n const secs = Number(raw)\n let ms: number\n if (Number.isFinite(secs)) {\n ms = secs * 1000\n } else {\n const dateMs = Date.parse(raw)\n if (!Number.isFinite(dateMs)) {\n return null\n }\n ms = dateMs - Date.now()\n }\n return Math.min(Math.max(0, ms), RETRY_AFTER_MAX_MS)\n}\n\n/** A cart error the CLI maps to \"discard local cart_id, create a fresh cart, re-add\" (F-B2). */\nexport class RecoverableCartError extends Error {\n readonly recoverable = true as const\n readonly status: number\n constructor(message: string, status: number) {\n super(message)\n this.name = \"RecoverableCartError\"\n this.status = status\n }\n}\n\n/** A non-recoverable API error. Its message/envelope is already redacted (G10). */\nexport class StoreApiError extends Error {\n readonly status: number\n readonly path: string\n constructor(message: string, status: number, path: string) {\n super(message)\n this.name = \"StoreApiError\"\n this.status = status\n this.path = path\n }\n}\n\ntype HttpMethod = \"GET\" | \"POST\" | \"DELETE\"\n\ninterface RequestOpts {\n body?: unknown\n /** Mutations carry an Idempotency-Key. */\n idempotent?: boolean\n /** Treat a 404 or a completed-cart answer as a RecoverableCartError (cart mutations). */\n cartRecovery?: boolean\n query?: Record<string, string | number | undefined>\n /**\n * Statuses for which a 4xx is EXPECTED, not an error — the client_error envelope is NOT\n * logged (BUG 3: a guest whoami 401 is the normal unauthenticated path, not noise to dump).\n * The error is still thrown so the caller can branch on it.\n */\n expected4xx?: number[]\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n\n/** crypto.randomUUID with a defensive fallback (older runtimes). */\nfunction newUuid(): string {\n const g = globalThis as unknown as { crypto?: { randomUUID?: () => string } }\n if (g.crypto?.randomUUID) {\n return g.crypto.randomUUID()\n }\n // RFC4122-ish fallback — never the happy path, just defensive.\n return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0\n const v = c === \"x\" ? r : (r & 0x3) | 0x8\n return v.toString(16)\n })\n}\n\nexport class StoreClient {\n private readonly baseUrl: string\n private readonly publishableKey: string\n private readonly jwt?: string\n private readonly fetchImpl: typeof fetch\n private readonly logger: ClientLogger\n private readonly retry: RetryPolicy\n private readonly sleepImpl: (ms: number) => Promise<void>\n private readonly timeoutMs: number\n\n /** Cached resolved regions (read-only catalog optimisation). */\n private regionsCache: RegionLike[] | null = null\n\n constructor(opts: StoreClientOptions) {\n if (!opts.baseUrl) {\n throw new Error(\"StoreClient: baseUrl is required\")\n }\n if (!opts.publishableKey) {\n throw new Error(\"StoreClient: publishableKey is required\")\n }\n // Guard against accidentally shipping a secret key (T6 — defensive).\n if (/^sk_/.test(opts.publishableKey)) {\n throw new Error(\"StoreClient: refusing a secret key — the client carries only the publishable key (T6)\")\n }\n this.baseUrl = opts.baseUrl.replace(/\\/+$/, \"\")\n this.publishableKey = opts.publishableKey\n this.jwt = opts.jwt\n this.fetchImpl = opts.fetchImpl ?? (globalThis.fetch as typeof fetch)\n this.logger = opts.logger ?? console\n this.retry = { ...DEFAULT_RETRY, ...(opts.retry ?? {}) }\n this.sleepImpl = opts.sleepImpl ?? sleep\n this.timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS\n }\n\n // ----- the request engine --------------------------------------------------\n\n private buildUrl(path: string, query?: RequestOpts[\"query\"]): string {\n const url = new URL(this.baseUrl + path)\n if (query) {\n for (const [k, v] of Object.entries(query)) {\n if (v !== undefined && v !== null && v !== \"\") {\n url.searchParams.set(k, String(v))\n }\n }\n }\n return url.toString()\n }\n\n private baseHeaders(): Record<string, string> {\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n \"x-publishable-api-key\": this.publishableKey,\n }\n if (this.jwt) {\n headers[\"Authorization\"] = `Bearer ${this.jwt}`\n }\n return headers\n }\n\n private backoffMs(attemptIndex: number): number {\n // exponential base with full jitter, capped.\n const exp = this.retry.baseDelayMs * 2 ** attemptIndex\n const capped = Math.min(exp, this.retry.maxDelayMs)\n return Math.floor(Math.random() * (capped + 1))\n }\n\n private async request<T>(method: HttpMethod, path: string, opts: RequestOpts = {}): Promise<T> {\n const headers = this.baseHeaders()\n // Generate the idempotency key ONCE and reuse it across retries (ROBUSTNESS §2). The server\n // honours it: the checkout MINT reuses the open per-cart session, and the /store/carts\n // idempotency middleware replays a finished request's response for the same key, or answers\n // 409 IDEMPOTENCY_CONFLICT + Retry-After while the first is still in flight. That 409 is retried\n // below like a 429, with this same key, so a retried createCart/addLineItem never double-applies.\n if (opts.idempotent) {\n headers[\"Idempotency-Key\"] = newUuid()\n }\n const url = this.buildUrl(path, opts.query)\n const init: RequestInit = {\n method,\n headers,\n ...(opts.body !== undefined ? { body: JSON.stringify(opts.body) } : {}),\n }\n\n let lastErr: unknown\n for (let attempt = 0; attempt < this.retry.attempts; attempt++) {\n // A server-dictated delay for THIS attempt's backoff (429 Retry-After); null → jittered backoff.\n let retryAfterMs: number | null = null\n // Bound THIS attempt: a hung connection is aborted after timeoutMs. The AbortError lands in\n // the catch below and is treated as a retryable network error (so the backoff + retry, and\n // ultimately the exhausted-retries StoreApiError, all apply). Fresh controller per attempt.\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), this.timeoutMs)\n try {\n const res = await this.fetchImpl(url as unknown as string, { ...init, signal: controller.signal })\n if (res.ok) {\n // 204 / empty body tolerance\n const text = await res.text()\n return (text ? JSON.parse(text) : {}) as T\n }\n\n // Read the error body defensively (may not be JSON).\n let parsed: Record<string, unknown> = {}\n try {\n const t = await res.text()\n parsed = t ? (JSON.parse(t) as Record<string, unknown>) : {}\n } catch {\n parsed = {}\n }\n\n // The same Idempotency-Key is still in flight server-side (the /store/carts middleware).\n // NOT a dead cart: wait Retry-After and replay with the same key.\n const idempotencyConflict = res.status === 409 && parsed.code === \"IDEMPOTENCY_CONFLICT\"\n\n // Cart recovery: a stale/completed cart on a cart mutation (F-B2). A 404, or an answer that SAYS\n // the cart completed (the backend guard's 409 { type: \"cart_completed\" }, or Medusa's own words).\n // A bare 409 is not enough: read as a dead cart, a live one would be thrown away and replaced.\n const parsedType = String(parsed.type ?? \"\")\n if (\n opts.cartRecovery &&\n (res.status === 404 ||\n parsedType === \"cart_completed\" ||\n parsedType.includes(\"cart.completed\") ||\n /cart.*complete/i.test(String(parsed.message ?? \"\")))\n ) {\n throw new RecoverableCartError(\n `cart is stale or completed (HTTP ${res.status})`,\n res.status\n )\n }\n\n // 429 Too Many Requests, or 409 IDEMPOTENCY_CONFLICT — RETRYABLE, honoring Retry-After (d12).\n // Must be handled BEFORE the generic 4xx-no-retry rule below, or either becomes a hard failure.\n if (idempotencyConflict) {\n retryAfterMs = parseRetryAfterMs(res)\n lastErr = new StoreApiError(`request in flight (HTTP 409)`, 409, path)\n this.logWarn(\"store.request.idempotency_conflict\", { path, attempt: attempt + 1, retryAfterMs })\n } else if (res.status === 429) {\n retryAfterMs = parseRetryAfterMs(res)\n lastErr = new StoreApiError(`rate limited (HTTP 429)`, 429, path)\n this.logWarn(\"store.request.rate_limited\", {\n path,\n attempt: attempt + 1,\n retryAfterMs,\n })\n } else if (res.status >= 400 && res.status < 500) {\n // other 4xx is a client error — do NOT retry.\n // Suppress the envelope for EXPECTED 4xx (e.g. a guest whoami 401 — BUG 3): it's a\n // normal branch the caller handles, not noise to dump to the user's terminal.\n if (!(opts.expected4xx ?? []).includes(res.status)) {\n this.logError(\"store.request.client_error\", { path, status: res.status, body: parsed })\n }\n throw new StoreApiError(\n `request failed (HTTP ${res.status})`,\n res.status,\n path\n )\n } else {\n // 5xx — retryable. Fall through to the retry loop.\n lastErr = new StoreApiError(`upstream error (HTTP ${res.status})`, res.status, path)\n this.logWarn(\"store.request.retryable\", { path, status: res.status, attempt: attempt + 1 })\n }\n } catch (e) {\n // RecoverableCartError / StoreApiError(4xx) must propagate immediately.\n if (e instanceof RecoverableCartError) {\n throw e\n }\n if (e instanceof StoreApiError && e.status >= 400 && e.status < 500) {\n throw e\n }\n // Network error, an aborted (timed-out) request, or a 5xx StoreApiError → retryable.\n lastErr = e\n this.logWarn(\"store.request.network_retry\", { path, attempt: attempt + 1 })\n } finally {\n // Always clear the per-attempt timeout — on success-return, on a rethrow, or before a retry.\n clearTimeout(timer)\n }\n\n // Backoff before the next attempt (skip after the final attempt). A 429 Retry-After, when\n // present, dictates the delay; otherwise jittered exponential backoff (d12).\n if (attempt < this.retry.attempts - 1) {\n await this.sleepImpl(retryAfterMs ?? this.backoffMs(attempt))\n }\n }\n\n // Exhausted retries. The thrown envelope is already free of secrets (we only ever\n // log/throw redacted, derived data — never the request headers/body).\n this.logError(\"store.request.exhausted\", { path, attempts: this.retry.attempts })\n if (lastErr instanceof StoreApiError) {\n throw lastErr\n }\n throw new StoreApiError(\n `request failed after ${this.retry.attempts} attempts: ${path}`,\n 0,\n path\n )\n }\n\n private logWarn(event: string, ctx: Record<string, unknown>): void {\n this.logger.warn(event, redact(ctx))\n }\n\n private logError(event: string, ctx: Record<string, unknown>): void {\n this.logger.error(event, redact(ctx))\n }\n\n // ----- regions (F-A2 / INV-11) ---------------------------------------------\n\n async getRegions(): Promise<RegionLike[]> {\n const data = await this.request<{ regions: RegionLike[] }>(\"GET\", \"/store/regions\")\n this.regionsCache = data.regions ?? []\n return this.regionsCache\n }\n\n /**\n * Resolve a region by country code, WITH provenance (matched / home / requested), so callers on\n * the money path can fail closed on an unmatched explicit country instead of silently pricing in\n * the wrong currency (F-A2). Region #1 is the launch/home default — SEK today, derived, never a\n * hardcoded literal.\n *\n * Semantics:\n * - country given AND a region ships there → { matched: true, home: false }.\n * - country given, none ships there → { matched: false, home: false } (money surfaces MUST\n * refuse; discovery may show the home region + a warning). We still return a region so a\n * discovery command can render *something*, but `matched:false` is the signal to fail closed.\n * - no country given → { matched: false, home: true } (the legitimate\n * launch default — not a \"wrong\" currency).\n */\n async resolveRegionResult(countryCode?: string): Promise<RegionResolution> {\n const regions = this.regionsCache ?? (await this.getRegions())\n if (regions.length === 0) {\n // Fail closed: refuse to price without a resolved region (F-A2).\n throw new Error(\"no regions configured — cannot resolve a region for pricing\")\n }\n const home = regions[0]! // region #1 — the launch region (SEK today, derived not hardcoded)\n if (countryCode) {\n const cc = countryCode.toLowerCase()\n const match = regions.find((r) =>\n (r.countries ?? []).some((c) => (c.iso_2 ?? \"\").toLowerCase() === cc)\n )\n if (match) {\n return { region: match, matched: true, requested: cc, home: false }\n }\n // A country code was given but no region ships there. Warn (stderr, redacted) so the\n // mis-resolution is observable, and report matched:false so money surfaces fail closed.\n this.logWarn(\"store.region.unmatched\", { countryCode: cc, fallback: home.id })\n return { region: home, matched: false, requested: cc, home: false }\n }\n return { region: home, matched: false, requested: undefined, home: true }\n }\n\n /**\n * Resolve a region by country code, falling back to region #1. A thin wrapper over\n * resolveRegionResult for callers that only need the region (back-compat).\n * SEK is region #1 by configuration — NEVER a hardcoded literal.\n */\n async resolveRegion(countryCode?: string): Promise<RegionLike> {\n return (await this.resolveRegionResult(countryCode)).region\n }\n\n // ----- products (region resolved BEFORE pricing) ---------------------------\n\n async listProducts(opts?: { region?: RegionLike; countryCode?: string }): Promise<ProductLike[]> {\n const region = opts?.region ?? (await this.resolveRegion(opts?.countryCode))\n // Paginate (L12): a single limit:100 call silently drops the 101st product. Page through until a\n // short page, with a hard cap so a misbehaving API can never spin this unbounded.\n const PAGE = 100\n const HARD_CAP = 1000\n const all: ProductLike[] = []\n for (let offset = 0; offset < HARD_CAP; offset += PAGE) {\n const data = await this.request<{ products: ProductLike[] }>(\"GET\", \"/store/products\", {\n // +metadata: Medusa omits product.metadata by default, and that's where lore/lore_sources\n // (and any per-product spec fields) live — without this the CLI/MCP never see them.\n query: { region_id: region.id, limit: PAGE, offset, fields: PRODUCT_FIELDS },\n })\n const page = data.products ?? []\n all.push(...page)\n if (page.length < PAGE) {\n break\n }\n }\n return all\n }\n\n async getProduct(handle: string, opts?: { region?: RegionLike; countryCode?: string }): Promise<ProductLike | null> {\n const region = opts?.region ?? (await this.resolveRegion(opts?.countryCode))\n const data = await this.request<{ products: ProductLike[] }>(\"GET\", \"/store/products\", {\n query: { handle, region_id: region.id, limit: 1, fields: PRODUCT_FIELDS },\n })\n return (data.products ?? [])[0] ?? null\n }\n\n // ----- cart (mutations carry Idempotency-Key) ------------------------------\n\n async createCart(region: RegionLike): Promise<CartLike> {\n const data = await this.request<{ cart: CartLike }>(\"POST\", \"/store/carts\", {\n idempotent: true,\n body: { region_id: region.id },\n })\n return data.cart\n }\n\n async addLineItem(cartId: string, variantId: string, quantity: number): Promise<CartLike> {\n const data = await this.request<{ cart: CartLike }>(\n \"POST\",\n `/store/carts/${encodeURIComponent(cartId)}/line-items`,\n {\n idempotent: true,\n cartRecovery: true,\n body: { variant_id: variantId, quantity },\n }\n )\n return data.cart\n }\n\n async getCart(cartId: string): Promise<CartLike> {\n const data = await this.request<{ cart: CartLike }>(\n \"GET\",\n `/store/carts/${encodeURIComponent(cartId)}`,\n { cartRecovery: true }\n )\n return data.cart\n }\n\n /** Remove a staged line item (bt unstage). Returns the updated cart (Medusa's `parent`). */\n async removeLineItem(cartId: string, itemId: string): Promise<CartLike> {\n const data = await this.request<{ parent?: CartLike; cart?: CartLike }>(\n \"DELETE\",\n `/store/carts/${encodeURIComponent(cartId)}/line-items/${encodeURIComponent(itemId)}`,\n { idempotent: true, cartRecovery: true }\n )\n return (data.parent ?? data.cart) as CartLike\n }\n\n /**\n * Ensure the cart has a shipping method before checkout (BUG 1 — the headless\n * /store/headless/checkout route guards on it, and neither client ever set one).\n *\n * - Idempotent: if the cart already carries a shipping method, no-op (no POST).\n * - Lists options via the STANDARD store route GET /store/shipping-options?cart_id=…\n * (NOT a /headless route — standard routes are always available, no kill-switch).\n * - Picks deterministically: exactly one → use it; several → the cheapest by amount.\n * - The SE launch option is region-scoped (price_type:\"flat\"), so listing by cart_id\n * returns it without a shipping address being set first (confirmed: seed-shipping.ts).\n *\n * TODO(04.5): buyer-choice / intl multiple shipping options — today we auto-pick the\n * cheapest; an explicit buyer chooser belongs to the Phase-6 international pass.\n */\n async ensureCartShippingMethod(cartId: string): Promise<CartLike> {\n // 1. read the authoritative cart; a stale/completed cart → RecoverableCartError (F-B2).\n const cart = await this.getCart(cartId)\n if (cart.shipping_methods && cart.shipping_methods.length > 0) {\n // idempotent: already satisfied.\n return cart\n }\n\n // 2. list options via the STANDARD store route (always available, no kill-switch).\n const data = await this.request<{ shipping_options: ShippingOptionLike[] }>(\n \"GET\",\n \"/store/shipping-options\",\n { query: { cart_id: cartId } }\n )\n const options = data.shipping_options ?? []\n if (options.length === 0) {\n // fail closed: never mint a checkout we know the backend will reject (BUG 1).\n throw new Error(\n \"no shipping option is available for this cart — cannot attach a shipping method\"\n )\n }\n\n // 3. pick deterministically: one → it; several → the cheapest by amount.\n const chosen =\n options.length === 1\n ? options[0]!\n : [...options].sort(\n (a, b) => (a.amount ?? Number.POSITIVE_INFINITY) - (b.amount ?? Number.POSITIVE_INFINITY)\n )[0]!\n\n // 4. attach it — a mutation, so it carries an Idempotency-Key + cart-recovery.\n const res = await this.request<{ cart: CartLike }>(\n \"POST\",\n `/store/carts/${encodeURIComponent(cartId)}/shipping-methods`,\n {\n idempotent: true,\n cartRecovery: true,\n body: { option_id: chosen.id },\n }\n )\n return res.cart\n }\n\n // ----- headless contract (04.5-01) -----------------------------------------\n\n // `channel` is REQUIRED (L11): a default silently mis-attributes analytics for a future caller that\n // forgets it. Both current callers (cli, mcp) pass it explicitly.\n async mintCheckout(cartId: string, email: string | undefined, channel: string): Promise<HeadlessCheckout> {\n return this.request<HeadlessCheckout>(\"POST\", \"/store/headless/checkout\", {\n idempotent: true,\n body: { cart_id: cartId, email, channel },\n })\n }\n\n async poll(sessionId: string): Promise<PollResult> {\n return this.request<PollResult>(\n \"GET\",\n `/store/headless/poll/${encodeURIComponent(sessionId)}`\n )\n }\n\n /** JWT-bound: returns the authenticated customer's own profile (G2). */\n async whoami(): Promise<WhoAmI> {\n // A guest (no/expired JWT) gets a 401/403 — that's the EXPECTED unauthenticated branch the\n // CLI turns into the friendly \"browsing as a guest\" line, NOT an error to dump (BUG 3).\n return this.request<WhoAmI>(\"GET\", \"/store/headless/whoami\", { expected4xx: [401, 403] })\n }\n\n // ----- magic-link auth (04.5-05) -------------------------------------------\n // Passwordless login — magic-link is the only grant (Medusa has no OAuth pairing-grant, G1).\n // request-link emails a single-use link; verify exchanges the link's token for a customer-scoped\n // JWT. The JWT is stored in the OS keychain by the CLI — never here, never in plaintext config (G7/G8).\n\n /** Start a magic-link login. Returns a UNIFORM ack regardless of whether the email exists (G12). */\n async requestMagicLink(email: string): Promise<RequestLinkResult> {\n return this.request<RequestLinkResult>(\"POST\", \"/store/headless/auth/request-link\", {\n idempotent: true,\n body: { email },\n })\n }\n\n /** Exchange a magic-link token for a customer-scoped JWT (the customer is materialized server-side). */\n async verifyMagicLink(token: string): Promise<VerifyLinkResult> {\n return this.request<VerifyLinkResult>(\"POST\", \"/store/headless/auth/verify\", {\n idempotent: true,\n body: { token },\n })\n }\n\n async listDrops(): Promise<DropRow[]> {\n const data = await this.request<{ drops: DropRow[]; count: number }>(\n \"GET\",\n \"/store/headless/drops\"\n )\n return data.drops ?? []\n }\n\n /**\n * File a developer bug report (d14 — `bt report` / MCP report_issue). Returns a correlation\n * reference. Additive: no money/cart path. The backend redacts authoritatively; callers SHOULD\n * pass an already-redacted context (the doctor snapshot). A mutation → carries an Idempotency-Key\n * so a retry can't email the report twice.\n */\n async report(message: string, context?: Record<string, unknown>): Promise<ReportResult> {\n return this.request<ReportResult>(\"POST\", \"/store/headless/report\", {\n idempotent: true,\n body: { message, ...(context !== undefined ? { context } : {}) },\n })\n }\n\n /**\n * The authenticated customer's order history (d13 — `bt log`). JWT-bound; the backend derives the\n * customer from the bearer token and returns MINIMAL fields (status/timeline only — never\n * email/address, F-H3). A guest (no/expired JWT) gets a 401 — the EXPECTED unauthenticated branch\n * we turn into the honest empty history (`[]`), NOT an error to dump (BUG 3).\n */\n async listOrders(): Promise<OrderSummary[]> {\n try {\n const data = await this.request<{ orders: OrderSummary[] }>(\"GET\", \"/store/headless/orders\", {\n expected4xx: [401, 403],\n })\n return data.orders ?? []\n } catch (e) {\n if (e instanceof StoreApiError && (e.status === 401 || e.status === 403)) {\n return []\n }\n throw e\n }\n }\n\n /**\n * Guest order lookup (AUD-8) — POST /store/order-lookup with the order id (display id OR internal\n * id) + the EXACT email in the BODY (never the query string, so the email is not access-logged).\n * The backend returns 404 on any mismatch (no enumeration) → the client surfaces null. Rate-limited\n * server-side; returns minimal status only, never PII (F-H1 / F-H3).\n */\n async getOrder(idOrDisplayId: string | number, email: string): Promise<unknown | null> {\n try {\n return await this.request<unknown>(\"POST\", \"/store/order-lookup\", {\n body: { id: String(idOrDisplayId), email },\n })\n } catch (e) {\n if (e instanceof StoreApiError && e.status === 404) {\n return null\n }\n throw e\n }\n }\n}\n","// Baked-in PUBLIC defaults for the Binary & Thread production channel.\n//\n// These let `npx @binaryandthread/cli` and the MCP server reach the live store with ZERO\n// configuration — the point of the soft-launch tarball (P4.5-7b) and decision #79 (d2).\n// store-client is bundled into both the CLI and the MCP server (tsup `noExternal`), so this\n// is the single source of truth for \"which store\" both clients talk to by default.\n//\n// SAFETY (CLAUDE.md rule 1 / SECURITY T6): the publishable key is PUBLIC BY DESIGN. It is the\n// SAME key embedded in the public storefront's client bundle (binaryandthread.com) and carries\n// no privilege beyond read + cart over the Store API — every dangerous operation lives behind\n// the backend. It is therefore NOT a secret; rule 1 governs secrets, and this is deliberately\n// published. Any env var (BT_API_URL / BT_STORE_URL / BT_PUBLISHABLE_KEY / MEDUSA_*) OVERRIDES\n// these for local dev or staging.\n\n/** The Binary & Thread production Medusa Store API base URL. */\nexport const BT_DEFAULT_STORE_URL = \"https://backend-production-511f5.up.railway.app\"\n\n// The PUBLIC Medusa publishable key (read + cart only; safe to ship — T6). Identical to the key\n// served in the public storefront bundle. NOT a secret (CLAUDE.md rule 1); the inline allow keeps\n// the gitleaks pre-commit hook from flagging a deliberately-published value.\nexport const BT_DEFAULT_PUBLISHABLE_KEY = \"pk_0d7d706860568a0414dff0436aadb9713a2609de3a1b153ef2a6f9694d23f907\" // gitleaks:allow\n","// Image-host allowlist — the canonical source for the WHOLE channel (CLAUDE.md rule 6 / SECURITY G9).\n//\n// We fetch product images ONLY from our own Vercel Blob store. Both the bt CLI (its terminal\n// image ladder) and the Wave-4 MCP (its server-side ImageContent fetch) gate every outbound\n// image fetch on this allowlist. It lives HERE in the shared client — not in the CLI — so the\n// MCP does not take a runtime dependency on the CLI package; the CLI re-exports it from here.\n//\n// HISTORY: a bare `endsWith(allowed)` suffix match was a CRITICAL allowlist-bypass bug fixed in\n// Wave 2 (it would admit `evilblob.vercel-storage.com`). The dot-delimited-subdomain check below\n// is load-bearing security — `host === allowed || host.endsWith(\".\" + allowed)`, never a bare\n// suffix. Do NOT \"simplify\" it.\n\n/**\n * Allowlisted image hosts. We fetch product images ONLY from our own Vercel Blob store\n * (public). NEVER a public temp host (uguu/0x0/catbox/transfer.sh) and NEVER an arbitrary\n * URL — that would be SSRF + a brand-IP leak (CLAUDE.md rule 6).\n */\nexport const BLOB_HOST_ALLOWLIST: readonly string[] = [\n \"blob.vercel-storage.com\", // matches the exact host + *.blob.vercel-storage.com (dot-delimited subdomains only)\n]\n\n/**\n * True if the URL's host is on the Blob allowlist.\n *\n * Exact host OR a true dot-delimited subdomain. NOT a bare suffix: a bare\n * `endsWith(allowed)` would admit `evilblob.vercel-storage.com` (allowlist bypass →\n * SSRF / brand-IP leak, CLAUDE.md rule 6). The dot guard is load-bearing.\n */\nexport function isAllowedImageHost(url: string): boolean {\n let host: string\n try {\n host = new URL(url).hostname.toLowerCase()\n } catch {\n return false\n }\n return BLOB_HOST_ALLOWLIST.some(\n (allowed) => host === allowed || host.endsWith(\".\" + allowed)\n )\n}\n","// The Fit axis (CAT-31 / D10). One product per design with options Fit × Colour × Size; only printable\n// combinations exist as variants. Written once here so the CLI and the MCP resolve fit the SAME way\n// (memory: never parse the variant title positionally; a wrong garment is a money-path defect).\n//\n// FAIL CLOSED: a product with a fit axis and no fit chosen is an error that lists the fits with their\n// prices. We never pick a fit for the buyer.\nimport type { ProductLike, VariantLike } from \"./types\"\nimport { variantColour, sizeOfVariant, colourKey, productColours } from \"./colour\"\n\nexport type Fit = \"regular\" | \"oversize\"\nconst FITS: readonly Fit[] = [\"regular\", \"oversize\"]\n\nexport function parseFit(v: unknown): Fit | null {\n const s = typeof v === \"string\" ? v.trim().toLowerCase() : \"\"\n return s === \"regular\" || s === \"oversize\" ? s : null\n}\n\nexport function fitLabel(f: Fit): \"Regular\" | \"Oversize\" {\n return f === \"oversize\" ? \"Oversize\" : \"Regular\"\n}\n\n/** The variant's Fit option value, or null when the product has no fit axis (legacy Size × Colour). */\nexport function variantFit(v: VariantLike): Fit | null {\n const opt = (v.options ?? []).find((o) => (o.option?.title ?? \"\").toLowerCase() === \"fit\")\n return opt ? parseFit(opt.value) : null\n}\n\nexport function hasFitAxis(p: ProductLike): boolean {\n return (p.variants ?? []).some((v) => variantFit(v) !== null)\n}\n\n/** The fits this product is sold in, regular first; [] for a legacy product. Derived from VARIANTS. */\nexport function productFits(p: ProductLike): Fit[] {\n const seen = new Set<Fit>()\n for (const v of p.variants ?? []) {\n const f = variantFit(v)\n if (f) seen.add(f)\n }\n return FITS.filter((f) => seen.has(f))\n}\n\n// ----- region rule (mirrors backend geography.ts + storefront availability.ts; hand-kept in sync) -----\n\nexport type Blank = \"gildan-5000\" | \"sols-03996\" | \"shaka-shmhss\"\n\n/** Colours we sell per blank, lower-cased. Khaki exists ONLY on Shaka (US oversize). */\nexport const BLANK_COLOURS: Record<Blank, readonly string[]> = {\n \"gildan-5000\": [\"black\", \"white\"],\n \"sols-03996\": [\"black\", \"white\"],\n \"shaka-shmhss\": [\"black\", \"white\", \"khaki\"],\n}\n\n/**\n * ISO-3166-1 alpha-2 (lower-case) → the Gelato STOCK REGION that country produces in. Mirrors the\n * backend `geography.ts` STOCK_REGION_BY_COUNTRY, which is canonical; kept in sync by hand and\n * asserted by `docs/verify-blank-rules.mjs`, because these files cannot import each other.\n *\n * SAR-435: this replaced \"is the country the US? then Shaka, otherwise SOL'S\" — a shortcut that\n * returned a printable blank for every country on earth. The backend resolver fails closed at\n * FULFILMENT, which is after the money has moved, so the buyer-facing answer has to fail closed too.\n * A country that is missing here is a country we cannot print for: say no before the card, not after.\n */\nexport const STOCK_REGION_BY_COUNTRY: Record<string, string> = {\n us: \"US-CA\",\n se: \"EU\",\n at: \"EU\", be: \"EU\", bg: \"EU\", hr: \"EU\", cy: \"EU\", cz: \"EU\", dk: \"EU\", ee: \"EU\",\n fi: \"EU\", fr: \"EU\", de: \"EU\", gr: \"EU\", hu: \"EU\", ie: \"EU\", it: \"EU\", lv: \"EU\",\n lt: \"EU\", lu: \"EU\", mt: \"EU\", nl: \"EU\", pl: \"EU\", pt: \"EU\", ro: \"EU\", sk: \"EU\",\n si: \"EU\", es: \"EU\",\n}\n\n/**\n * fit × stock region → the blank that prints it. `\"*\"` = produced in every region. A (fit, region)\n * with no entry is not produced there: OC (AU/NZ) and UK have NEITHER oversize blank, which is the\n * gap this exists to express. Mirrors the backend GARMENT_BY_REGION.\n */\nconst BLANK_BY_REGION: Record<Fit, Record<string, Blank>> = {\n regular: { \"*\": \"gildan-5000\" },\n oversize: { EU: \"sols-03996\", \"US-CA\": \"shaka-shmhss\" },\n}\n\n/**\n * The blank that prints (fit, ship-to country) — or **null when nothing we can print ships there**.\n * Null is the honest answer, and every caller treats it as \"not buyable\" rather than guessing.\n */\nexport function blankFor(fit: Fit, countryCode: string | null | undefined): Blank | null {\n const region = STOCK_REGION_BY_COUNTRY[(countryCode ?? \"\").trim().toLowerCase()]\n if (!region) return null\n const map = BLANK_BY_REGION[fit]\n return map[\"*\"] ?? map[region] ?? null\n}\n\n/**\n * The sizes each blank can PRINT, lower-cased, in OUR canonical option values (not Gelato's tokens —\n * SOL'S `xxl` is our \"2XL\"). Read off the LIVE Gelato catalog 2026-09-04 (`products:search`, the\n * `_gsi_<size>_` token per blank). Like BLANK_COLOURS this is the BLANK's capability, not our range:\n * a variant must ALSO exist, so listing 4XL/5XL here is harmless.\n *\n * CAT-37: the two oversize blanks differ — SOL'S (EU) prints XS and stops at 3XL, Shaka (US) starts at\n * S and runs to 5XL. Gating colour but not size let a US buyer stage and PAY for an Oversize XS that\n * Gelato cannot print; the order then fails closed at fulfillment, i.e. after the money moved.\n * Mirrored in the backend GARMENT_BLANKS (`catalog-variants.ts`, canonical) + storefront availability.ts.\n */\nexport const BLANK_SIZES: Record<Blank, readonly string[]> = {\n \"gildan-5000\": [\"s\", \"m\", \"l\", \"xl\", \"2xl\", \"3xl\", \"4xl\", \"5xl\"],\n \"shaka-shmhss\": [\"s\", \"m\", \"l\", \"xl\", \"2xl\", \"3xl\", \"4xl\", \"5xl\"],\n \"sols-03996\": [\"xs\", \"s\", \"m\", \"l\", \"xl\", \"2xl\", \"3xl\"],\n}\n\n// ----- the Printful blank (Phase 7 D1, TC1) -----\n//\n// A product is fulfilled by ONE supplier, named on the product (`metadata.bt_supplier`, written by the\n// backend when it clones a design onto Printful). A Printful product must be judged by the Printful\n// blank: judged by the Gildan row above it would offer 3XL-5XL, sizes we do not sell it in.\n// Mirrors the backend `printful-blanks.ts` (PRINTFUL_BLANKS colours + PRINTFUL_SALE_SIZES, canonical);\n// hand-kept in sync and asserted by `docs/verify-blank-rules.mjs`. The sizes are what we SELL (S-2XL,\n// Gelato parity), not the blank's capability (Black XS-3XL, White S-3XL): selling S-2XL also keeps the\n// colour-dependent White-has-no-XS rule out of this table.\n\nexport type Supplier = \"gelato\" | \"printful\"\n\n/**\n * The supplier that fulfils this product, matched EXACTLY: absent = the Gelato path (every product that\n * predates Phase 7 carries no `bt_supplier`), \"gelato\" / \"printful\" = that supplier, anything else = null.\n * Null is refused by every gate (`blankRule`): an unrecognised value judged by the Gelato blank is the\n * wrong-blank bug this exists to prevent.\n */\nexport function supplierOf(p: ProductLike): Supplier | null {\n const v = (p.metadata ?? {})[\"bt_supplier\"]\n if (v === undefined || v === null) return \"gelato\"\n return v === \"gelato\" || v === \"printful\" ? v : null\n}\n\nexport type PrintfulBlank = \"ascolour-5082\" | \"bellacanvas-4711\"\n\nexport const PRINTFUL_BLANK_COLOURS: Record<PrintfulBlank, readonly string[]> = {\n \"ascolour-5082\": [\"black\", \"white\", \"coal\", \"eucalyptus\", \"bone\", \"grey\", \"khaki\", \"indigo\"], // TB124, TB126\n \"bellacanvas-4711\": [\"asphalt\", \"dust\", \"navy\", \"maroon\", \"forest\", \"jet\"], // TB128: the sweatshirt; TB129: + jet (its Black)\n}\n\nexport const PRINTFUL_BLANK_SIZES: Record<PrintfulBlank, readonly string[]> = {\n \"ascolour-5082\": [\"s\", \"m\", \"l\", \"xl\", \"2xl\"],\n \"bellacanvas-4711\": [\"s\", \"m\", \"l\", \"xl\", \"2xl\"],\n}\n\n/**\n * TB128: the garment a Printful product is made on, from `metadata.bt_blank`: absent (or null) = the 5082 (every\n * product before TB128), an exact known key = that garment, anything else = null (refused by every gate). Mirrors the\n * backend printful-blanks.ts printfulBlankOfProduct.\n */\nexport function printfulBlankOf(p: ProductLike): PrintfulBlank | null {\n const v = (p.metadata ?? {})[\"bt_blank\"]\n if (v === undefined || v === null) return \"ascolour-5082\"\n return typeof v === \"string\" && Object.prototype.hasOwnProperty.call(PRINTFUL_BLANK_COLOURS, v) ? (v as PrintfulBlank) : null\n}\n\n/**\n * The countries a Printful product ships to: every country of every backend market (`markets.ts`\n * MARKETS[*].countries, canonical; each market carries a Printful selling region). Its own list, not\n * STOCK_REGION_BY_COUNTRY (that is Gelato's geography); asserted equal to markets.ts by\n * `docs/verify-blank-rules.mjs`.\n */\nexport const PRINTFUL_SERVED_COUNTRIES: readonly string[] = [\n \"se\",\n \"us\",\n \"at\", \"be\", \"bg\", \"hr\", \"cy\", \"cz\", \"dk\", \"ee\", \"fi\", \"fr\", \"de\", \"gr\", \"hu\",\n \"ie\", \"it\", \"lv\", \"lt\", \"lu\", \"mt\", \"nl\", \"pl\", \"pt\", \"ro\", \"sk\", \"si\", \"es\",\n]\n\n/**\n * The Printful blank for (fit, ship-to country), or null (refuse). Mirrors the backend\n * `printfulBlankKeyFor`: a country no market serves gets null, regular -> the 5082, oversize -> null.\n */\nexport function printfulBlankFor(\n fit: Fit,\n countryCode: string | null | undefined,\n /** TB128: the product's garment (printfulBlankOf); null = unknown = refused. */\n productBlank: PrintfulBlank | null = \"ascolour-5082\"\n): PrintfulBlank | null {\n if (!PRINTFUL_SERVED_COUNTRIES.includes((countryCode ?? \"\").trim().toLowerCase())) return null\n if (productBlank === null) return null\n return fit === \"regular\" ? productBlank : null\n}\n\n/** Canonical size key. \"XXL\" and \"2XL\" name the same size; BLANK_SIZES is keyed on the \"2xl\" form. */\nfunction sizeKey(size: string): string {\n const s = size.trim().toLowerCase()\n return s === \"xxl\" ? \"2xl\" : s === \"xxxl\" ? \"3xl\" : s\n}\n\n/**\n * The colour + size rule of the blank that prints (fit, country) for this SUPPLIER, or null when nothing\n * we can print ships there. The one place the supplier decides which table judges the buyer.\n */\nfunction blankRule(\n fit: Fit,\n countryCode: string | null | undefined,\n supplier: Supplier | null,\n productBlank: PrintfulBlank | null\n): { colours: readonly string[]; sizes: readonly string[] } | null {\n if (supplier === null) return null // an unrecognised supplier: no table can judge it, refuse\n if (supplier === \"printful\") {\n const pb = printfulBlankFor(fit, countryCode, productBlank)\n return pb ? { colours: PRINTFUL_BLANK_COLOURS[pb], sizes: PRINTFUL_BLANK_SIZES[pb] } : null\n }\n const blank = blankFor(fit, countryCode)\n return blank ? { colours: BLANK_COLOURS[blank], sizes: BLANK_SIZES[blank] } : null\n}\n\nexport function colourBuyable(\n fit: Fit,\n colour: string,\n countryCode: string | null | undefined,\n supplier: Supplier | null = \"gelato\",\n /** TB128: a Printful product's garment (printfulBlankOf). Omitted = the 5082 (its colours are disjoint from the 4711's). */\n printfulBlank: PrintfulBlank | null = \"ascolour-5082\"\n): boolean {\n const rule = blankRule(fit, countryCode, supplier, printfulBlank)\n return !!rule && rule.colours.includes(colour.trim().toLowerCase())\n}\n\n/** Can the blank that prints this fit HERE make this size? (CAT-37 — the size half of colourBuyable.) */\nexport function sizeBuyable(\n fit: Fit,\n size: string,\n countryCode: string | null | undefined,\n supplier: Supplier | null = \"gelato\",\n printfulBlank: PrintfulBlank | null = \"ascolour-5082\"\n): boolean {\n const rule = blankRule(fit, countryCode, supplier, printfulBlank)\n return !!rule && rule.sizes.includes(sizeKey(size))\n}\n\n/**\n * The fit of a LEGACY product — one with no Fit option, which is every product in the live catalog\n * until CAT-31 Phase E migrates it. Read from metadata.fit → title → handle, the same pre-merge rule\n * the storefront uses (`detectLegacyFit` / `isOversizeText`), so both channels agree.\n */\nexport function legacyFit(p: ProductLike): Fit {\n const md = (p.metadata ?? {}) as Record<string, unknown>\n const hay = [typeof md.fit === \"string\" ? md.fit : \"\", p.title ?? \"\", p.handle ?? \"\"].join(\" \")\n return /oversiz|drop.?shoulder/i.test(hay) ? \"oversize\" : \"regular\"\n}\n\n/**\n * The fit the REGION gates should judge by — which is not always the fit axis. The gates ask \"what\n * blank prints this, where you ship?\", and the blank follows the fit, so on a legacy product (no Fit\n * option) they must use its implied fit. Without this every `colourBuyable`/`sizeBuyable` check behind\n * a `fit &&` guard silently no-ops on the ENTIRE live catalog — the CAT-37 review found exactly that.\n * Returns null only when the product HAS a fit axis and no fit has been chosen: then there is no single\n * blank to judge by, and the caller is failing closed on the missing fit anyway.\n */\nexport function gateFit(p: ProductLike, fit: Fit | null): Fit | null {\n if (fit) return fit\n return hasFitAxis(p) ? null : legacyFit(p)\n}\n\n/** The variants of one fit; `null` = all variants (legacy product). */\nexport function variantsOfFit(p: ProductLike, fit: Fit | null): VariantLike[] {\n const all = p.variants ?? []\n return fit === null ? all : all.filter((v) => variantFit(v) === fit)\n}\n\n/**\n * Distinct colours of a fit (lower-cased, first-seen order). When `countryCode` is given (even null),\n * only the colours BUYABLE there are returned; `undefined` = no region filter.\n */\nexport function coloursFor(p: ProductLike, fit: Fit | null, countryCode?: string | null): string[] {\n const gate = gateFit(p, fit) // legacy product: its implied fit, so the gate is not skipped\n const supplier = supplierOf(p)\n const pb = printfulBlankOf(p)\n const out: string[] = []\n for (const v of variantsOfFit(p, fit)) {\n const c = variantColour(v)\n if (!c || out.includes(c)) continue\n if (gate && countryCode !== undefined && !colourBuyable(gate, c, countryCode, supplier, pb)) continue\n out.push(c)\n }\n return out\n}\n\n/**\n * The sizes of a (fit, colour), in variant order (callers sort canonically for display). When\n * `countryCode` is given (even null), only the sizes the ship-to blank can PRINT are returned;\n * `undefined` = no region filter — same convention as `coloursFor`.\n */\nexport function sizesFor(p: ProductLike, fit: Fit | null, colour: string, countryCode?: string | null): string[] {\n const gate = gateFit(p, fit) // legacy product: its implied fit, so the gate is not skipped\n const supplier = supplierOf(p)\n const pb = printfulBlankOf(p)\n const target = colour.trim().toLowerCase()\n const out: string[] = []\n for (const v of variantsOfFit(p, fit)) {\n if (variantColour(v) !== target) continue\n const s = sizeOfVariant(v)\n if (!s || out.includes(s)) continue\n if (gate && countryCode !== undefined && !sizeBuyable(gate, s, countryCode, supplier, pb)) continue\n out.push(s)\n }\n return out\n}\n\nexport type PriceTriple = { amount: number | null; currency: string | null; taxInclusive: boolean | null }\n\nfunction cheapest(vs: VariantLike[]): PriceTriple {\n const priced = vs\n .filter((v) => typeof v.calculated_price?.calculated_amount === \"number\")\n .sort(\n (a, b) => (a.calculated_price!.calculated_amount as number) - (b.calculated_price!.calculated_amount as number)\n )\n const v = priced[0]\n return {\n amount: v?.calculated_price?.calculated_amount ?? null,\n currency: v?.calculated_price?.currency_code ?? null,\n taxInclusive: v?.calculated_price?.is_calculated_price_tax_inclusive ?? null,\n }\n}\n\n/** The cheapest price across the product; `from` is true when the fits are priced differently. */\nexport function priceFrom(p: ProductLike): PriceTriple & { from: boolean } {\n const fits = productFits(p)\n const base = cheapest(p.variants ?? [])\n const amounts = new Set(fits.map((f) => cheapest(variantsOfFit(p, f)).amount))\n return { ...base, from: fits.length > 1 && amounts.size > 1 }\n}\n\nexport function priceFor(p: ProductLike, fit: Fit): PriceTriple {\n return cheapest(variantsOfFit(p, fit))\n}\n\nexport type ResolveVariantResult =\n | { ok: true; variant: VariantLike; fit: Fit | null; colour: string | null; size: string | null }\n | { ok: false; reason: \"ambiguous_fit\"; fits: Array<{ fit: Fit } & PriceTriple> }\n | { ok: false; reason: \"fit_not_offered\"; requested: string; fits: Fit[] }\n | { ok: false; reason: \"fit_unavailable\"; fit: Fit; colour: string | null }\n | { ok: false; reason: \"size_unavailable\"; fit: Fit; size: string; sizes: string[] }\n | {\n ok: false\n reason: \"not_found\"\n fit: Fit | null\n colour: string | null\n size: string | null\n colours: string[]\n sizes: string[]\n }\n\n/**\n * Resolve (fit, colour, size) → a variant, or a typed reason the caller turns into ONE clear message.\n * - a fit product with no (or an unknown) fit → `ambiguous_fit` with the fits + prices (fail closed)\n * - a colour the blank for this country does not offer → `fit_unavailable`\n * - a SIZE the blank for this country cannot print → `size_unavailable` (CAT-37; the two oversize\n * blanks have different size ranges, so this is region-dependent, not product-dependent)\n * - otherwise `not_found`, with the colours/sizes that DO exist\n * The colour defaults to the first colour buyable in the fit (black first by variant order). A legacy\n * product (no fit axis) ignores `fit` and behaves exactly as before.\n */\nexport function resolveVariant(\n p: ProductLike,\n opts: { fit?: string | null; size?: string | null; colour?: string | null; countryCode?: string | null }\n): ResolveVariantResult {\n const fits = productFits(p)\n const requested = parseFit(opts.fit)\n const askedFor = typeof opts.fit === \"string\" ? opts.fit.trim() : \"\"\n let fit: Fit | null = null\n if (fits.length > 0) {\n if (requested && fits.includes(requested)) fit = requested\n // An EXPLICIT fit the design isn't sold in is never substituted — on a single-fit design that would\n // silently stage/charge the other garment (review 2026-09-03). Only an ABSENT fit may be implied.\n else if (askedFor) return { ok: false, reason: \"fit_not_offered\", requested: askedFor, fits }\n else if (fits.length === 1) fit = fits[0]!\n else return { ok: false, reason: \"ambiguous_fit\", fits: fits.map((f) => ({ fit: f, ...priceFor(p, f) })) }\n }\n const pool = variantsOfFit(p, fit)\n // The gates below judge by the BLANK, so on a legacy product (no Fit option — the whole live catalog\n // today) they use its implied fit. `fit` stays the fit-axis answer, which is what we report back.\n const gate = gateFit(p, fit)\n const supplier = supplierOf(p)\n const pb = printfulBlankOf(p)\n const colours = coloursFor(p, fit, opts.countryCode)\n // TB129: key first, a label only among the product's own colours (\"Black\" on a jet product = jet; on a 5082, black).\n const colour = colourKey(opts.colour ?? \"\", productColours(p)) || (colours[0] ?? null)\n if (gate && colour && opts.countryCode !== undefined && !colourBuyable(gate, colour, opts.countryCode, supplier, pb)) {\n return { ok: false, reason: \"fit_unavailable\", fit: gate, colour }\n }\n const size = (opts.size ?? \"\").trim() || null\n const sizeLc = size?.toLowerCase()\n const buyableSizes = colour ? sizesFor(p, fit, colour, opts.countryCode) : []\n // CAT-37 — an EXPLICIT size the ship-to blank cannot print is its own answer, not \"no such variant\":\n // the variant usually DOES exist (it is printable elsewhere), so the buyer needs the region reason.\n // Only when the size is real on this product though: a typo is \"no such variant\", not a region rule.\n const sizeExists =\n !!size && pool.some((x) => (sizeOfVariant(x) ?? x.title ?? \"\").toLowerCase() === size.toLowerCase())\n if (gate && size && sizeExists && opts.countryCode !== undefined && !sizeBuyable(gate, size, opts.countryCode, supplier, pb)) {\n return { ok: false, reason: \"size_unavailable\", fit: gate, size, sizes: buyableSizes }\n }\n // ...and with NO size given we must not fall onto an unprintable one (XS is FIRST in variant order\n // on an oversize design, so the US default would have been the one size Shaka cannot make).\n const sellable =\n gate && opts.countryCode !== undefined\n ? pool.filter((x) => {\n const s = sizeOfVariant(x)\n return !s || sizeBuyable(gate, s, opts.countryCode, supplier, pb)\n })\n : pool\n const v = sellable.find((x) => {\n const colourOk = !colour || variantColour(x) === colour\n const sizeOk =\n !sizeLc || (sizeOfVariant(x) ?? \"\").toLowerCase() === sizeLc || (x.title ?? \"\").toLowerCase() === sizeLc\n return colourOk && sizeOk\n })\n if (!v) {\n return { ok: false, reason: \"not_found\", fit, colour, size, colours, sizes: buyableSizes }\n }\n return { ok: true, variant: v, fit, colour: variantColour(v), size: sizeOfVariant(v) }\n}\n","// Colour resolution — DERIVED from the variant matrix, never hardcoded (CAT-3 forward-compat).\n//\n// The dual-colour catalog (memory: dual-colour-catalog) gives each design ONE product with a\n// Colour × Size variant matrix. A variant's colour comes from, in order of trust:\n// 1. the variant option titled \"Colour\" (Model A: options {Size, Colour}, value title-cased)\n// 2. the variant title, format \"<size> / <Colour>\" (e.g. \"S / White\", \"M / Black\")\n// We NEVER hardcode white/black — future colours (CAT-3) fall out of the data for free.\n//\n// This lives in @binaryandthread/store-client (the canonical types home) so the CLI and the MCP\n// resolve colour the SAME way — written once, tested once.\n\nimport type { ProductLike, VariantLike, ImageLike } from \"./types\"\nimport { productFits, parseFit, variantFit, type Fit } from \"./fit\"\n\n// Title-token helpers for the LEGACY fallback path (a payload without expanded options). Tokens, never\n// positions: with a Fit option the title is \"Oversize / White / M\", so \"last segment = colour\" is wrong.\nconst SIZE_TOKEN = /^(xxs|xs|s|m|l|xl|xxl|2xl|3xl|xxxl|4xl|5xl)$/i\nconst FIT_TOKEN = /^(regular|oversize)$/i\nfunction titleSegments(title: string | null | undefined): string[] {\n return (title ?? \"\")\n .split(\"/\")\n .map((s) => s.trim())\n .filter(Boolean)\n}\n\n/**\n * The CUSTOMER-FACING name of a colour (BNT-256). Display only: the stored Colour option value\n * (\"Black\"/\"White\") stays the key for image matching, availability tables and ?color= URLs.\n * black → \"Faded Black\", white → \"Faded White\", anything else is title-cased unchanged.\n * (Copies, because the storefront and backend do not import this package:\n * apps/storefront/src/lib/util/colour-label.ts and apps/backend/src/lib/colour-label.ts. Keep in sync.)\n */\n// TB124: + coal, eucalyptus, bone; TB126: + grey, khaki, indigo (the 5082 colours wired for sale). TB129: + jet, the 4711's\n// Black (its own key, so the 5082's black keeps \"Faded Black\"). Keep the three copies in sync.\nconst COLOUR_LABELS: Record<string, string> = {\n black: \"Faded Black\",\n white: \"Faded White\",\n coal: \"Faded Coal\",\n eucalyptus: \"Faded Eucalyptus\",\n bone: \"Faded Bone\",\n grey: \"Faded Grey\",\n khaki: \"Faded Khaki\",\n indigo: \"Faded Indigo\",\n jet: \"Black\",\n}\nexport function colourLabel(colour: string): string {\n const c = colour.trim()\n if (!c) return c\n return COLOUR_LABELS[c.toLowerCase()] ?? c.charAt(0).toUpperCase() + c.slice(1).toLowerCase()\n}\n\n/**\n * The stored key for a typed colour, lowercased. TB129 (plan-check binding 1): an exact stored KEY wins first, then a\n * display LABEL (\"Faded Black\" → \"black\", \"Black\" → \"jet\"). With `available` (the PRODUCT's own colour keys, e.g.\n * productColours(p)) both steps look only among them, so `black` on a 5082 product stays `black` while `Black` on a\n * product sold in jet (the 4711's Black, displayed \"Black\") is `jet`. Without it, a key wins over any label (`black` is\n * never read as jet). Anything unmatched comes back lowercased, for the caller's own \"no such colour\".\n */\nexport function colourKey(colour: string, available?: readonly string[] | null): string {\n const c = colour.trim().toLowerCase()\n if (available) {\n const keys = available.map((k) => k.trim().toLowerCase())\n if (keys.includes(c)) return c\n return keys.find((k) => colourLabel(k).toLowerCase() === c) ?? c\n }\n if (Object.prototype.hasOwnProperty.call(COLOUR_LABELS, c)) return c\n const key = Object.entries(COLOUR_LABELS).find(([, label]) => label.toLowerCase() === c)?.[0]\n return key ?? c\n}\n\n/** A variant title (\"Regular / White / M\") with its colour segment relabelled for display. */\nexport function labelVariantTitle(title: string | null | undefined): string {\n return (title ?? \"\")\n .split(\"/\")\n .map((seg) => {\n const t = seg.trim()\n const k = t.toLowerCase()\n return Object.prototype.hasOwnProperty.call(COLOUR_LABELS, k) ? colourLabel(t) : t\n })\n .join(\" / \")\n}\n\n/** Title-case a colour token for display (\"white\" → \"Faded White\"); see colourLabel. */\nexport function displayColour(colour: string): string {\n return colourLabel(colour)\n}\n\n/**\n * The colour of a single variant, lowercased — or null if it carries no colour axis (a\n * legacy Size-only product). Reads the \"Colour\" option first, then parses the \"<size> / <Colour>\"\n * title. NEVER assumes a fixed palette.\n */\nexport function variantColour(v: VariantLike): string | null {\n // 1) the explicit Colour option (the authoritative source — memory: never parse the title).\n const opt = (v.options ?? []).find(\n (o) => (o.option?.title ?? \"\").toLowerCase() === \"colour\" || (o.option?.title ?? \"\").toLowerCase() === \"color\"\n )\n if (opt && typeof opt.value === \"string\" && opt.value.trim()) {\n return opt.value.trim().toLowerCase()\n }\n // 2) fall back to the title: the segment that is neither a size nor a fit token (≥2 segments only).\n const segs = titleSegments(v.title)\n if (segs.length >= 2) {\n const seg = segs.find((s) => !SIZE_TOKEN.test(s) && !FIT_TOKEN.test(s))\n if (seg) return seg.toLowerCase()\n }\n return null\n}\n\n/**\n * The DISTINCT colours offered by a product, in first-seen order, lowercased. Empty for a\n * legacy Size-only product (no Colour axis) — callers treat that as \"no colour to show\".\n */\nexport function productColours(p: ProductLike): string[] {\n const seen: string[] = []\n for (const v of p.variants ?? []) {\n const c = variantColour(v)\n if (c && !seen.includes(c)) {\n seen.push(c)\n }\n }\n return seen\n}\n\n/** The distinct sizes offered FOR a given colour (lowercased colour match), preserving order. */\nexport function sizesForColour(p: ProductLike, colour: string): string[] {\n const target = colour.trim().toLowerCase()\n const sizes: string[] = []\n for (const v of p.variants ?? []) {\n if (variantColour(v) !== target) continue\n const size = sizeOfVariant(v)\n if (size && !sizes.includes(size)) {\n sizes.push(size)\n }\n }\n return sizes\n}\n\n/** The size label of a variant — the Size option, else the title's leading \"<size>\" segment. */\nexport function sizeOfVariant(v: VariantLike): string | null {\n const opt = (v.options ?? []).find((o) => (o.option?.title ?? \"\").toLowerCase() === \"size\")\n if (opt && typeof opt.value === \"string\" && opt.value.trim()) {\n return opt.value.trim()\n }\n const segs = titleSegments(v.title)\n const token = segs.find((s) => SIZE_TOKEN.test(s))\n if (token) return token\n // No colour axis → the whole title IS the size (legacy Size-only product); a multi-segment title with\n // no recognisable size token yields its first segment (old behaviour).\n return segs.length === 1 ? segs[0]! : segs[0] ?? null\n}\n\n/**\n * Resolve a variant by size AND colour (the real buyability gap — bt stage / MCP add_to_cart).\n * - size + colour given → exact match on both (case-insensitive).\n * - size only → first variant of that size.\n * - colour only → first variant of that colour.\n * - neither → first variant.\n * Returns undefined when the requested size+colour combination does not exist (the caller MUST\n * surface a clear error — never silently substitute another colour the buyer didn't ask for).\n */\nexport function findVariant(\n p: ProductLike,\n opts: { fit?: string; size?: string; colour?: string } = {}\n): VariantLike | undefined {\n // Fit axis (CAT-31): a product sold in several fits needs the fit named; none → undefined (fail\n // closed, the caller reports the fits via resolveVariant). A single-fit product implies its fit.\n const fits = productFits(p)\n let fit: Fit | null = null\n if (fits.length > 0) {\n const wanted = parseFit(opts.fit)\n const askedFor = typeof opts.fit === \"string\" ? opts.fit.trim() : \"\"\n if (wanted && fits.includes(wanted)) fit = wanted\n // An explicit fit the design isn't sold in never falls back to the other garment (see resolveVariant).\n else if (askedFor) return undefined\n else if (fits.length === 1) fit = fits[0]!\n else return undefined\n }\n const variants = (p.variants ?? []).filter((v) => fit === null || variantFit(v) === fit)\n const size = opts.size?.trim().toLowerCase()\n const colour = opts.colour ? colourKey(opts.colour, productColours(p)) : undefined\n\n const sizeMatches = (v: VariantLike): boolean => {\n if (!size) return true\n return (sizeOfVariant(v) ?? \"\").toLowerCase() === size || (v.title ?? \"\").toLowerCase() === size\n }\n const colourMatches = (v: VariantLike): boolean => {\n if (!colour) return true\n return variantColour(v) === colour\n }\n\n if (size && colour) {\n // both specified → require BOTH; no fallback to a different colour (a wrong colour is worse\n // than a clear \"that combo isn't available\").\n return variants.find((v) => sizeMatches(v) && colourMatches(v))\n }\n if (colour && !size) {\n return variants.find((v) => colourMatches(v))\n }\n if (size && !colour) {\n // size specified → require it; NO fallback to variants[0] (L10) — a silently-wrong size is worse\n // than undefined, which lets the caller (stage.ts) surface a clear \"no matching variant\" error.\n return variants.find((v) => sizeMatches(v))\n }\n return variants[0]\n}\n\n// The gallery scene we surface in the terminal: the flat-lay \"plp\" shot (the tee laid flat on the\n// photographic/concrete plate) — the SAME scene as the storefront thumbnail. Pinning to one scene is\n// what makes every tee read CONSISTENTLY (never a mix of on-model here / hero there); the product\n// image order alone is unreliable (D9). Filenames are `<slug>-<scene>-<colour>.webp`.\nconst PREFERRED_SCENE = \"plp\"\n\n/** The URL of a specific SCENE for a colour (or any colour when `colour` is null) — matches\n * `-<scene>-` in the filename + the colour token. null if no such image. */\nfunction sceneImage(\n images: Array<ImageLike> | null | undefined,\n scene: string,\n colour: string | null\n): string | null {\n for (const img of images ?? []) {\n const u = (img?.url ?? \"\").trim()\n if (!u || !u.toLowerCase().includes(`-${scene}-`)) continue\n if (colour === null || new RegExp(`(^|[^a-z])${escapeRegex(colour)}([^a-z]|$)`, \"i\").test(u.toLowerCase())) {\n return u\n }\n }\n return null\n}\n\n/**\n * The image URL to render for a given colour. Resolves, in order:\n * 1. the flat-lay \"plp\" scene for the colour — the CONSISTENT, thumbnail-matching choice (D9), so\n * every tee shows the same flat-on-concrete shot rather than an order-dependent on-model/hero.\n * 2. the matching variant's own images[]\n * 3. a product image whose url/metadata names the colour\n * 4. null → the caller falls back to product.thumbnail + a \"black available\" note.\n * Never throws; a missing colour just yields null.\n */\nexport function imageForColour(p: ProductLike, colour: string): string | null {\n const target = colour.trim().toLowerCase()\n if (!target) {\n return sceneImage(p.images, PREFERRED_SCENE, null) ?? p.thumbnail ?? firstImageUrl(p.images) ?? null\n }\n\n // 1) the flat-lay plp scene for this colour — consistent across every product (matches thumbnail).\n const plp = sceneImage(p.images, PREFERRED_SCENE, target)\n if (plp) return plp\n\n // 2) the colour's variant images.\n for (const v of p.variants ?? []) {\n if (variantColour(v) !== target) continue\n const url = firstImageUrl(v.images)\n if (url) return url\n }\n\n // 3) a product image that names the colour (url substring or metadata.colour tag).\n for (const img of p.images ?? []) {\n const tag = (img.metadata?.[\"colour\"] ?? img.metadata?.[\"color\"] ?? \"\")\n .toString()\n .toLowerCase()\n if (tag === target) {\n const u = (img.url ?? \"\").trim()\n if (u) return u\n }\n }\n for (const img of p.images ?? []) {\n const u = (img.url ?? \"\").trim()\n if (u && new RegExp(`(^|[^a-z])${escapeRegex(target)}([^a-z]|$)`, \"i\").test(u)) {\n return u\n }\n }\n return null\n}\n\n/**\n * The image for a (fit, colour): the `plp` scene by image METADATA first (fit-specific, then fit-neutral),\n * then the legacy filename resolution (`imageForColour`). null when the colour has no shot at all (e.g. a\n * colour sold on one blank only) so the caller can fall back to the thumbnail + a note.\n */\nexport function imageFor(p: ProductLike, sel: { fit: Fit | null; colour: string }): string | null {\n const colour = sel.colour.trim().toLowerCase()\n if (!colour) return null\n const metas = (p.images ?? []).map((img) => ({\n url: (img?.url ?? \"\").trim(),\n scene: typeof img?.metadata?.[\"scene\"] === \"string\" ? String(img.metadata[\"scene\"]) : null,\n fit: parseFit(img?.metadata?.[\"fit\"]),\n colour: typeof img?.metadata?.[\"colour\"] === \"string\" ? String(img.metadata[\"colour\"]).toLowerCase() : null,\n }))\n const tagged = metas.filter((m) => m.url && m.colour === colour && m.scene === PREFERRED_SCENE)\n const exact = tagged.find((m) => sel.fit !== null && m.fit === sel.fit)\n if (exact) return exact.url\n const neutral = tagged.find((m) => m.fit === null)\n if (neutral) return neutral.url\n // A plp shot tagged for the OTHER fit only → this colour has no shot in THIS fit, so show nothing rather\n // than the wrong garment (the legacy filename resolution below would match the other fit's `-plp-` file).\n const otherFitOnly = tagged.length > 0 && sel.fit !== null\n if (otherFitOnly) return null\n return imageForColour(p, colour)\n}\n\nfunction firstImageUrl(images: Array<ImageLike> | null | undefined): string | null {\n for (const img of images ?? []) {\n const u = (img?.url ?? \"\").trim()\n if (u) return u\n }\n return null\n}\n\nfunction escapeRegex(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")\n}\n\n/** The segment of a variant title that IS a size (\"Regular / Coal / M\" → \"M\"), or null. Unlike\n * sizeOfVariant, never falls back to another segment: \"Regular / Coal\" has no size (TB252). */\nexport function sizeTokenOf(title: string | null | undefined): string | null {\n return titleSegments(title).find((s) => SIZE_TOKEN.test(s)) ?? null\n}\n\n// Canonical garment size order — so a size list always reads S · M · L · XL · 2XL, never the\n// arbitrary variant order the API happens to return (E2). Unknown sizes sort to the end, alpha.\nconst SIZE_ORDER = [\"xxs\", \"xs\", \"s\", \"m\", \"l\", \"xl\", \"2xl\", \"xxl\", \"3xl\", \"xxxl\", \"4xl\", \"5xl\"]\n\n/** Sort garment sizes into the canonical XS→5XL order (case-insensitive; unknowns last). */\nexport function sortSizes(sizes: string[]): string[] {\n const rank = (s: string): number => {\n const i = SIZE_ORDER.indexOf(s.trim().toLowerCase())\n return i === -1 ? SIZE_ORDER.length : i\n }\n return [...sizes].sort((a, b) => rank(a) - rank(b) || a.localeCompare(b))\n}\n\n/** A cart line's variant options (present when the cart was fetched with `*items.variant.options.option`). */\nexport type LineLike = {\n variant_title?: string | null\n title?: string | null\n variant?: { options?: Array<{ value?: string | null; option?: { title?: string | null } | null }> | null } | null\n}\n\n/** A cart line's value for the first variant option whose title (lowercased) is in `titles`; null when absent. */\nexport function lineOption(item: LineLike, titles: string[]): string | null {\n const o = item.variant?.options?.find((x) => titles.includes((x.option?.title ?? \"\").toLowerCase()))\n const v = typeof o?.value === \"string\" ? o.value.trim() : \"\"\n return v ? v : null\n}\n\n/**\n * The colour carried by a cart line (A3): the variant's Colour OPTION first, else a colour token in the\n * variant title (never a positional split: with a Fit option the title is \"Oversize / Black / M\").\n * Returns null when the line has no colour axis. Shared by the CLI and the MCP.\n */\nexport function lineColour(item: LineLike): string | null {\n const opt = lineOption(item, [\"colour\", \"color\"])\n if (opt) return opt.toLowerCase()\n const vt = (item.variant_title ?? \"\").toString()\n return vt ? variantColour({ id: \"x\", title: vt }) : null\n}\n\n/** The size carried by a cart line (TB252): the Size OPTION, else a real size token in the title; never another segment. */\nexport function lineSize(item: LineLike): string | null {\n return lineOption(item, [\"size\"]) ?? sizeTokenOf(item.variant_title)\n}\n","// The lifecycle stage of an order, for every channel (bt order, bt log; D1 review). ONE mapping, an\n// EXACT switch over Medusa's FulfillmentStatus: the substring version read \"not_fulfilled\" as a later\n// stage, because it contains \"fulfilled\". Medusa's own words, the same for every supplier.\n//\n// Brand rule (Sam 2026-10-02, memory customer-sees-shipped-or-not): a customer only learns placed\n// (committed), shipped, delivered or reverted, never a production stage. The backend already answers\n// not_fulfilled for made-not-shipped; Medusa's raw fulfilled / partially_fulfilled still read committed\n// here, so an older backend (or any route that forwards Medusa's word) cannot bring the stage back.\n\nexport type OrderStage = \"committed\" | \"shipped\" | \"delivered\" | \"reverted\"\n\nexport interface OrderStageInput {\n status?: string | null\n fulfillment_status?: string | null\n}\n\nexport function orderStage(o: OrderStageInput): OrderStage {\n const status = (o.status ?? \"\").trim().toLowerCase()\n if (status === \"canceled\" || status === \"refunded\") return \"reverted\"\n switch ((o.fulfillment_status ?? \"\").trim().toLowerCase()) {\n case \"canceled\":\n return \"reverted\"\n case \"delivered\":\n case \"partially_delivered\":\n return \"delivered\"\n case \"shipped\":\n case \"partially_shipped\":\n return \"shipped\"\n case \"fulfilled\": // made, not shipped: to the customer it is simply not shipped yet\n case \"partially_fulfilled\":\n case \"not_fulfilled\":\n default:\n // placed, not shipped yet (not a payment signal).\n return \"committed\"\n }\n}\n\n/**\n * The fulfilment word a channel shows a customer (MCP get_order_status). The same rule as the backend's\n * customerFulfillmentStatus (apps/backend/src/lib/order-fulfillment-status.ts, the packages cannot import it):\n * made-not-shipped (fulfilled / partially_fulfilled) reads not_fulfilled; everything else passes through.\n */\nexport function customerFulfillmentStatus(word: string | null | undefined): string | null {\n const w = (word ?? \"\").trim().toLowerCase()\n if (!w) return null\n return w === \"fulfilled\" || w === \"partially_fulfilled\" ? \"not_fulfilled\" : w\n}\n","// Where a REUSED cart ships, for the colour/size gates (D1 money-path review, HIGH). One rule for the CLI\n// (`bt stage` onto config.cart_id) and the MCP (`add_to_cart` with a cart_id): a reused cart is judged by\n// where IT ships, never by the caller's region argument or a server/config default. Before this, a US cart\n// reused under an SE default took an Oversize XS (SOL'S makes it, Shaka does not): paid, then held at\n// fulfilment.\nimport type { CartLike, RegionLike } from \"./types\"\n\nexport interface CartShipTo {\n /** The cart's own region (with its countries). */\n region: RegionLike\n /** The gate country: the cart's shipping address, else its region's first country. Lower-case ISO-2. */\n country: string\n /** True when the country came from the shipping address (an exact destination, not a region default). */\n fromAddress: boolean\n /** Every country of the cart's region, lower-case. */\n regionCountries: string[]\n}\n\n/**\n * Where `cart` ships, or null when that cannot be read (no region we know, no country): the caller must\n * refuse, because a cart whose destination is unknown cannot be gated. `regions` is looked up by the cart's\n * region_id only when the cart did not carry its region's countries.\n */\nexport async function cartShipTo(cart: CartLike, regions: () => Promise<RegionLike[]>): Promise<CartShipTo | null> {\n const embedded = cart.region?.countries?.length ? cart.region : null\n const id = cart.region_id ?? cart.region?.id ?? null\n const region = embedded ?? (id ? (await regions()).find((r) => r.id === id) ?? null : null)\n if (!region) return null\n const regionCountries = (region.countries ?? []).map((c) => (c.iso_2 ?? \"\").trim().toLowerCase()).filter(Boolean)\n const address = (cart.shipping_address?.country_code ?? \"\").trim().toLowerCase() || null\n const country = address ?? regionCountries[0] ?? null\n if (!country) return null\n return { region, country, fromAddress: address !== null, regionCountries }\n}\n\n/**\n * Does an explicitly requested ship-to country disagree with where the cart ships? Same country: no. A\n * country inside the cart's region when the cart has no address yet: no (same region, same currency, and\n * every country of a region produces in the same place). Anything else: yes, refuse.\n */\nexport function shipToDisagrees(requested: string | null | undefined, shipTo: CartShipTo): boolean {\n const cc = (requested ?? \"\").trim().toLowerCase()\n if (!cc || cc === shipTo.country) return false\n return shipTo.fromAddress || !shipTo.regionCountries.includes(cc)\n}\n","// Canonical product-content accessors — the ONE source for a piece's README, spec sheet, and lore,\n// shared by the CLI (`bt view` / `bt blame`), the MCP (`get_product`), and (aspirationally) the\n// storefront PDP. Keeping these here means the three channels can't drift on what a piece \"says\".\n//\n// The spec fields + fallbacks mirror the storefront's buildSpecs (apps/storefront .../templates).\n\nimport type { ProductLike } from \"./types\"\nimport { supplierOf } from \"./fit\"\n\n/** One row of the spec sheet. */\nexport interface SpecEntry {\n key: string\n value: string\n}\n\n/** A lore link (a real source behind the pun/idea). */\nexport interface LoreSource {\n url: string\n label: string\n}\n\n/** A piece's lore: the story text + any sources. Empty text = no lore on record. */\nexport interface ProductLore {\n text: string\n sources: LoreSource[]\n}\n\n/** A non-empty string, or the fallback. */\nfunction str(v: unknown, fallback: string): string {\n return typeof v === \"string\" && v.length > 0 ? v : fallback\n}\n\n/** The piece's README — the human description shown on the PDP's README toggle. */\nexport function productReadme(p: ProductLike): string {\n return typeof p.description === \"string\" ? p.description : \"\"\n}\n\n// Fallback spec rows per supplier. MIRRORS apps/storefront/src/lib/util/specs.ts + delivery.ts (packages cannot import\n// each other); change a sentence in both. Printful wording is a DRAFT until Sam signs it off; the delivery line is\n// identical for both on purpose (Sam 2026-10-01: today's wording everywhere).\nconst GELATO_FALLBACK = {\n material: \"100% cotton\",\n weight: \"180 g/m²\",\n fit: \"Unisex heavyweight crewneck\",\n delivery: \"Ships in 2–5 business days + transit\",\n care: \"Cold wash, line dry\",\n}\nconst PRINTFUL_FALLBACK = {\n material: \"100% cotton\",\n weight: \"240 g/m²\",\n fit: \"Unisex heavyweight crewneck, garment-dyed\",\n delivery: \"Ships in 2–5 business days + transit\",\n care: \"Machine wash cold, inside out. Do not tumble dry or iron the print. Line dry in shade.\",\n}\n\n/**\n * The spec sheet — the 6 canonical rows, in PDP order, with the same fallbacks as the storefront's\n * buildSpecs (the single source of truth for what a piece is made of / how it ships).\n */\nexport function productSpecs(p: ProductLike, fit?: \"regular\" | \"oversize\" | null): SpecEntry[] {\n const md = (p.metadata ?? {}) as Record<string, unknown>\n // CAT-31: a merged product carries `specs_by_fit` (one block per fit); a legacy product the flat keys.\n const byFit = (md.specs_by_fit ?? null) as Record<string, Record<string, unknown>> | null\n const fitKey = fit ?? (byFit && Object.keys(byFit).length === 1 ? Object.keys(byFit)[0] : undefined)\n const src: Record<string, unknown> = (fitKey && byFit?.[fitKey]) || md\n const pick = (k: string, fallback: string) => str(src[k] ?? md[k], fallback)\n const d = supplierOf(p) === \"printful\" ? PRINTFUL_FALLBACK : GELATO_FALLBACK\n return [\n { key: \"material\", value: pick(\"material\", d.material) },\n { key: \"weight\", value: pick(\"weight\", d.weight) },\n { key: \"fit\", value: pick(\"fit\", d.fit) },\n { key: \"print\", value: pick(\"print\", \"Front print\") },\n // Delivery: the product's own line first (Sam 2026-10-01, as the storefront since U1), else the same\n // fallback wording for both suppliers. No per-region times.\n { key: \"delivery\", value: pick(\"delivery\", d.delivery) },\n { key: \"care\", value: pick(\"care\", d.care) },\n ]\n}\n\n/** The piece's lore — the story behind the pun + any real sources. Empty text = none on record. */\nexport function productLore(p: ProductLike): ProductLore {\n const md = (p.metadata ?? {}) as Record<string, unknown>\n const text = typeof md.lore === \"string\" ? md.lore : \"\"\n const raw = Array.isArray(md.lore_sources) ? md.lore_sources : []\n const sources: LoreSource[] = raw\n .filter(\n (s): s is { url: string; label: string } =>\n !!s &&\n typeof s === \"object\" &&\n typeof (s as { url?: unknown }).url === \"string\" &&\n typeof (s as { label?: unknown }).label === \"string\"\n )\n .map((s) => ({ url: s.url, label: s.label }))\n return { text, sources }\n}\n","// Tax-inclusivity — the single source of truth for the \"incl. VAT\" price label across EVERY\n// channel (storefront, the bt CLI, the MCP server). Kept here (in the shared client) so the CLI\n// and MCP agree with each other and with the backend on how the flag is read.\n\nimport type { CartLike } from \"./types\"\n\n/**\n * Whether a cart's prices are VAT-inclusive (EU/SE) vs tax-exclusive (US).\n *\n * Medusa exposes tax-inclusivity PER LINE ITEM (and per shipping method), never on the cart root —\n * there is no `cart.is_tax_inclusive` in @medusajs/types 2.15.5. So we DERIVE it from the items.\n * It is uniform per region (every line shares the region's tax setting), so `.some` is exact and a\n * mixed cart cannot occur. A US (tax-exclusive) cart → false → NO \"incl. VAT\" label; an SE cart →\n * true → \" incl. VAT\". This is NEVER keyed off a currency or country literal (INV-11): the label\n * follows the market's own tax flag, so it stays correct as new regions come online.\n *\n * Fails closed: an empty/absent cart or items array → false (no phantom VAT label on nothing).\n */\nexport function isCartTaxInclusive(cart: Pick<CartLike, \"items\"> | null | undefined): boolean {\n return (cart?.items ?? []).some((i) => i.is_tax_inclusive === true)\n}\n","/**\n * ONE definition of what a money block contains, shared by every channel (CAT-44).\n *\n * Sam, 2026-09-07, looking at `bt commit`: *\"terminal is missing to display shipping costs\"* and\n * *\"actually not showing discount either\"*. The terminal printed a single `total` with the shipping\n * silently folded in, so a buyer could not see what they were paying for. The same week, the\n * order-confirmation email was found printing a total that ignored discounts entirely (CAT-42).\n * Both are the same failure: a surface inventing its own arithmetic and showing only the answer.\n *\n * So the ROWS live here, once, and each surface only decides how to paint them. The order is the\n * ordinary receipt order, matching the confirmation email and the storefront checkout:\n *\n * subtotal → discount(s) → shipping → total\n *\n * This module is pure and formats nothing: amounts stay numbers in the cart's own currency\n * (data-price format, never cents), so callers keep using their own fmtPrice + VAT suffix.\n */\n\nexport type MoneyRowKind = \"subtotal\" | \"discount\" | \"shipping\" | \"total\"\n\nexport type MoneyRow = {\n kind: MoneyRowKind\n /** Lowercase label, e.g. \"subtotal\" / \"dev channel\" / \"shipping\" / \"total\". */\n label: string\n /** Always POSITIVE. A discount row is rendered with a leading minus by the caller. */\n amount: number\n /** Rendered instead of the amount when set, e.g. \"free\" or \"added at checkout\". */\n display?: string\n /** A trailing parenthetical, e.g. \"10% — for shopping the terminal\". */\n note?: string\n}\n\nexport type MoneyRowsInput = {\n /**\n * Optional. NOT used for the subtotal — see cartMoneyRows for why the subtotal is DERIVED. Kept\n * only to decide whether a cart has items at all.\n */\n itemTotal?: number | null\n /** Discount to show. On a terminal cart this is 0 until checkout mints it (see discountNote). */\n discountTotal?: number | null\n /** What the discount is called, e.g. \"dev channel\". Defaults to \"discount\". */\n discountLabel?: string | null\n /** e.g. \"10% — for shopping the terminal\". */\n discountNote?: string | null\n /** What shipping costs. 0 can mean \"free\" or \"not priced yet\" — hasShippingMethod decides which. */\n shippingTotal?: number | null\n /** False before checkout attaches a method, when shipping is simply not known yet. */\n hasShippingMethod?: boolean\n /** The final amount. Always shown, even when everything above is absent. */\n total?: number | null\n}\n\nconst num = (v: number | null | undefined) => {\n const n = Number(v ?? 0)\n return Number.isFinite(n) ? n : 0\n}\n\n/**\n * Build the rows for a cart or checkout review.\n *\n * The block is CONSISTENT: with items present you always get subtotal, shipping and total, whether\n * or not shipping has been priced yet — \"shipping: added at checkout\" is itself the information a\n * buyer needs, because it says the total is not final. Only a zero discount is omitted; the first\n * draft of this also collapsed a one-line cart to a bare total, and that conditional immediately\n * produced two tests contradicting each other, which is a good sign a money block should not be\n * clever. A zero discount is the one thing worth hiding, since \"−0\" tells nobody anything.\n */\nexport function cartMoneyRows(input: MoneyRowsInput): MoneyRow[] {\n const itemTotal = num(input.itemTotal)\n const discount = Math.abs(num(input.discountTotal))\n const shipping = num(input.shippingTotal)\n const total = num(input.total)\n const hasMethod = Boolean(input.hasShippingMethod)\n\n const rows: MoneyRow[] = []\n // A cart with money in it gets a shipping row even when we cannot see its item subtotal (an older\n // backend, or a caller that only has a total). Dropping the row there would silently re-open A2:\n // the buyer would read a pre-shipping figure with nothing saying shipping is still to come.\n const hasMoney = itemTotal > 0 || total > 0\n\n // The subtotal is DERIVED from the total, never read off the cart. Medusa's `item_total` is\n // NET of promotions once one is applied, so printing it above a discount line double-counts the\n // discount and the column stops adding up: shipped 0.4.3 briefly showed\n // subtotal 359.10 · discount −39.90 · shipping 59 · total 418.10\n // where the first three make 378.20. Deriving guarantees the block reconciles by construction —\n // the total is the authoritative number (it is what the buyer pays), so everything else is\n // expressed relative to it.\n const subtotal = Math.round((total - shipping + discount) * 100) / 100\n\n if (subtotal > 0) {\n rows.push({ kind: \"subtotal\", label: \"subtotal\", amount: subtotal })\n }\n if (discount > 0) {\n rows.push({\n kind: \"discount\",\n label: input.discountLabel || \"discount\",\n amount: discount,\n ...(input.discountNote ? { note: input.discountNote } : {}),\n })\n }\n if (hasMoney) {\n rows.push(\n hasMethod\n ? // A method is attached, so the number is real: 0 here means a promotion covered it.\n { kind: \"shipping\", label: \"shipping\", amount: shipping, ...(shipping === 0 ? { display: \"free\" } : {}) }\n : // No method yet — say so rather than implying shipping is free (A2: a pre-shipping figure\n // must never read as the final price).\n { kind: \"shipping\", label: \"shipping\", amount: 0, display: \"excl. shipping — added at checkout\" }\n )\n }\n rows.push({ kind: \"total\", label: \"total\", amount: total })\n return rows\n}\n","// The B&T MCP server — stdio transport, registering exactly the channel's deterministic tools.\n//\n// The server is a THIN registrar: each tool is a deterministic wrapper over the shared\n// StoreClient (no free-form execution, no admin op — SECURITY T10/G8). The tool DEFINITIONS\n// (names, input schemas, handlers) live in tools.ts; this module just builds the McpServer and\n// wires the stdio transport so a developer's AI agent can call them over stdin/stdout.\n\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\"\nimport type { ZodRawShape } from \"zod\"\nimport type { ImageBlock } from \"./images\"\nimport { VERSION } from \"./version\"\nimport { GETTING_STARTED, GETTING_STARTED_URI, buyPromptText } from \"./onboarding\"\n\n/** What a tool handler returns — MCP content blocks (image and/or text). */\nexport interface ToolResult {\n content: ImageBlock[]\n /** Set when the tool reports a user-facing error (e.g. a 404 lookup) — not a transport error. */\n isError?: boolean\n /**\n * Machine-readable result (ids/handles like cart_id/session_id/order_id) so an agent can chain\n * tools WITHOUT regexing the prose (d9). Already redacted (G10). Mirrors the human text block.\n */\n structuredContent?: Record<string, unknown>\n}\n\n/**\n * A deterministic tool definition. `inputSchema` is a Zod raw shape (the SDK turns it into a JSON\n * schema for the agent). The handler receives the parsed args and returns content blocks. Eggs\n * (VOICE §3.2 M2/M3) may live in the `description` text but NEVER alter behavior.\n */\nexport interface ToolDef<S extends ZodRawShape = ZodRawShape> {\n name: string\n description: string\n inputSchema: S\n handler: (args: Record<string, unknown>) => Promise<ToolResult>\n}\n\n// Advertised to the host in the initialize handshake. Version is single-sourced from version.ts\n// (mirrors package.json; version.test.ts locks them together) — no more silent drift.\nexport const SERVER_INFO = {\n name: \"binary-and-thread\",\n version: VERSION,\n} as const\n\n/**\n * Build an McpServer and register every tool. Returns the server plus the registered tool names\n * (so callers/tests can assert exactly the 10 tools are present). Pure — no transport, no I/O.\n */\nexport function buildServer(tools: ToolDef[]): {\n server: McpServer\n toolNames: string[]\n resourceNames: string[]\n promptNames: string[]\n} {\n const server = new McpServer(SERVER_INFO)\n const toolNames: string[] = []\n // The SDK's registerTool generic over a ZodRawShape input schema infers excessively deeply\n // (TS2589); we register through a structurally-typed view of the method to keep the call site\n // simple. The runtime contract is unchanged — name, { description, inputSchema }, handler.\n const register = server.registerTool.bind(server) as unknown as (\n name: string,\n config: { description: string; inputSchema: ZodRawShape },\n cb: (args: Record<string, unknown>) => Promise<{\n content: ImageBlock[]\n isError?: boolean\n structuredContent?: Record<string, unknown>\n }>\n ) => void\n for (const t of tools) {\n register(\n t.name,\n { description: t.description, inputSchema: t.inputSchema },\n // The SDK validates args against inputSchema before calling us. We hand the parsed args to\n // the deterministic handler and return its content blocks (+ optional structuredContent, d9).\n async (args: Record<string, unknown>) => {\n const res = await t.handler(args ?? {})\n return {\n content: res.content,\n ...(res.isError ? { isError: true } : {}),\n ...(res.structuredContent ? { structuredContent: res.structuredContent } : {}),\n }\n }\n )\n toolNames.push(t.name)\n }\n\n // Agent onboarding: a getting-started RESOURCE + a `buy` PROMPT (neither can touch money — they're\n // read-only orientation). Registered through structurally-typed views of the SDK methods to avoid\n // the deep generic inference the tool registration also side-steps.\n const registerResource = server.registerResource.bind(server) as unknown as (\n name: string,\n uri: string,\n metadata: { title?: string; description?: string; mimeType?: string },\n cb: (uri: URL) => { contents: Array<{ uri: string; text: string; mimeType?: string }> }\n ) => void\n registerResource(\n \"getting-started\",\n GETTING_STARTED_URI,\n { title: \"binary & thread — getting started\", description: \"the buy flow + the money boundary, for a cold agent\", mimeType: \"text/plain\" },\n () => ({ contents: [{ uri: GETTING_STARTED_URI, text: GETTING_STARTED, mimeType: \"text/plain\" }] })\n )\n const resourceNames = [\"getting-started\"]\n\n const registerPrompt = server.registerPrompt.bind(server) as unknown as (\n name: string,\n config: { title?: string; description?: string; argsSchema?: ZodRawShape },\n cb: (args: Record<string, unknown>) => { messages: Array<{ role: \"user\"; content: { type: \"text\"; text: string } }> }\n ) => void\n registerPrompt(\n \"buy\",\n { title: \"buy a piece\", description: \"walk through buying a piece (respecting the card-stays-with-the-human boundary)\" },\n (args) => ({\n messages: [\n {\n role: \"user\",\n content: {\n type: \"text\",\n text: buyPromptText(\n typeof args?.handle === \"string\" ? args.handle : undefined,\n typeof args?.region === \"string\" ? args.region : undefined\n ),\n },\n },\n ],\n })\n )\n const promptNames = [\"buy\"]\n\n return { server, toolNames, resourceNames, promptNames }\n}\n\n/** Build the server with the real tools and start it on stdio. Used by bin.ts. */\nexport async function startServer(tools: ToolDef[]): Promise<McpServer> {\n const { server } = buildServer(tools)\n const transport = new StdioServerTransport()\n await server.connect(transport)\n return server\n}\n","// The single source of truth for the MCP server version string.\n//\n// Mirrored into mcp/package.json's \"version\" field; version.test.ts asserts the two never drift.\n// We don't import package.json directly (JSON imports complicate the ESM dist build), so this\n// literal is the canonical value the server advertises to a host in the `initialize` handshake\n// (SERVER_INFO.version). Before this, SERVER_INFO hardcoded \"0.1.0\" and drifted from the published\n// package — a host inspecting the server saw a stale version, and bug reports mis-attributed it.\nexport const VERSION = \"0.4.14\"\n","// Agent onboarding surfaces — a getting-started RESOURCE + a `buy` PROMPT. The MCP parallel to the\n// CLI's provenance line + `bt init`: a cold agent shouldn't have to infer the flow (or the money\n// boundary) from ten tool descriptions. Deadpan-competent dev voice; brand-voice clean (never\n// \"tee\"/\"t-shirt\"/print-on-demand/Gelato — enforced by brand-voice.test.ts scanning src/).\n\nexport const GETTING_STARTED_URI = \"bt://getting-started\"\n\nexport const GETTING_STARTED = `binary & thread — buying from your agent.\n\nthe flow (each tool returns machine-readable structuredContent, so you can chain without regexing prose):\n 1. list_products [region] what's in stock, priced in the ship-to market's currency\n 2. get_product <handle> full detail + an authoritative variant_id + sizes + colours\n 3. add_to_cart … start or reuse a cart (returns a durable cart_id handle)\n 4. get_cart <cart_id> the authoritative cart — note: the total is PRE-shipping\n 5. checkout <cart_id> mint a pay link + the SHIPPING-INCLUSIVE total the buyer pays\n 6. check_payment <session_id> poll once for the result (call again if still pending)\n\nthe dev-channel discount: every purchase through this MCP (or the bt CLI) is 10% off the web —\napplied automatically at checkout, no code. checkout returns the discounted total; quote that.\n\nthe boundary (by design): payment + ship-to details are entered by the human on the storefront.\nthey never enter this chat, and you cannot complete a purchase yourself — you walk them to the door.\n\nregion + currency: prices and shipping follow the ship-to country. pass region (ISO-2, e.g. se / us /\nde) to list_products / get_product / add_to_cart, or the host can set BT_REGION. a country we don't\nship to is refused rather than priced in the wrong currency.`\n\n/** The `buy` prompt body — a templated walk-through of the flow + the boundary, optionally seeded\n * with a handle/region the user named. Returned as a single user-turn message. */\nexport function buyPromptText(handle?: string, region?: string): string {\n const target = handle ? `\"${handle}\"` : \"the piece the user wants\"\n const reg = region ? ` Price it for region \"${region}\".` : \"\"\n return `Help me buy ${target} from binary & thread through the MCP tools.${reg}\n\nSteps:\n1. If you don't have an exact variant_id, call get_product to get valid sizes/colours + the variant_id.\n2. add_to_cart with the variant_id (or handle + size + colour). Keep the returned cart_id.\n3. Show me the cart total, and remind me it excludes shipping until checkout (where a 10%\n dev-channel discount also applies — the terminal's cut).\n4. When I confirm, call checkout and give me the pay link + the shipping-inclusive, discounted total.\n5. Do NOT ask me for card or address details — I enter those in the browser. After I pay, use\n check_payment with the session_id to confirm the order.`\n}\n","// The 10 deterministic MCP tools over the SHARED StoreClient (SECURITY G8/T10; ROBUSTNESS §4.2/4.3).\n//\n// These are thin wrappers — no free-form execution, no admin op, no money self-completion. They\n// reuse the same idempotency / region-resolution / retry / redaction that the CLI uses (it all\n// lives inside the StoreClient — written once, contract-tested once).\n//\n// The hard invariants this file enforces (the MCP red-team gates):\n// - checkout (G8 confused-deputy / F-I4): returns {checkout_url, session_id} INSTANTLY. It NEVER\n// polls, NEVER opens a browser, NEVER sets a shipping address. Its result text tells the human\n// to open the link and explicitly says \"I can't complete this for you\". The human pays on the\n// storefront (Stripe Elements) — payment details NEVER enter the chat. check_payment polls; the\n// agent re-calls at its own cadence (async handle-ID).\n// (NB: there is deliberately NO payment-detail field anywhere in this package; the security\n// verify greps for those field tokens and must come back clean.)\n// - list views are TEXT-ONLY (no per-item images — token-bloat guard, ROBUSTNESS §4.3).\n// - every backend string is sanitize()'d (G8); every output object is redactForOutput()'d (G10).\n// - get_order_status returns status/timeline only — NO address (F-H3); id + exact-email, 404 on\n// miss (F-H1).\n\nimport { z } from \"zod\"\nimport {\n RecoverableCartError,\n StoreApiError,\n productColours,\n colourLabel,\n variantColour,\n sizesForColour,\n sizeOfVariant,\n productSpecs,\n productFits,\n priceFrom,\n priceFor,\n coloursFor,\n sizesFor,\n resolveVariant,\n variantFit,\n colourBuyable,\n sizeBuyable,\n gateFit,\n supplierOf,\n printfulBlankOf,\n parseFit,\n fitLabel,\n imageFor,\n productLore,\n productReadme,\n isCartTaxInclusive,\n cartMoneyRows,\n cartShipTo,\n shipToDisagrees,\n orderStage,\n customerFulfillmentStatus,\n lineColour,\n lineSize,\n sortSizes,\n} from \"@binaryandthread/store-client\"\nimport type {\n StoreClient,\n CartLike,\n RegionLike,\n} from \"@binaryandthread/store-client\"\nimport { sanitize, redactForOutput } from \"./sanitize\"\nimport { buildImageContent } from \"./images\"\nimport type { ImageBlock } from \"./images\"\nimport type { ToolDef, ToolResult } from \"./server\"\nimport { TEAPOT_TEXT, FORTUNES, fortune } from \"./eggs\"\n\nexport interface ToolsOpts {\n /** ISO-2 country code to resolve pricing (never a hardcoded currency literal). */\n region?: string\n}\n\n// ----- result helpers ---------------------------------------------------------\n\nfunction text(s: string): ToolResult {\n return { content: [{ type: \"text\", text: s }] }\n}\n\nfunction errorText(s: string): ToolResult {\n return { content: [{ type: \"text\", text: s }], isError: true }\n}\n\n/** A text block PLUS machine-readable structuredContent (ids/handles) for agent chaining (d9). */\nfunction result(textBody: string, structured: Record<string, unknown>): ToolResult {\n return {\n content: [{ type: \"text\", text: textBody }],\n structuredContent: redactForOutput(structured) as Record<string, unknown>,\n }\n}\n\n/** Price line — currency comes from the variant/region, NEVER a literal (F-A2/INV-11). */\nfunction fmtPrice(amount: number | null | undefined, currency: string | null | undefined): string {\n if (amount == null) {\n return \"—\"\n }\n const cur = (currency ?? \"\").toUpperCase()\n return cur ? `${amount} ${cur}` : `${amount}`\n}\n\n/**\n * The tax suffix for a price line: \" incl. VAT\" ONLY for a tax-inclusive market (EU/SE), \"\" for a\n * tax-exclusive one (US). Driven off the cart's / variant's OWN is_tax_inclusive flag, NEVER a\n * currency literal (INV-11) — mirrors the CLI + storefront. Pass isCartTaxInclusive(cart) for a cart\n * total, or a variant's calculated_price.is_calculated_price_tax_inclusive for a product price.\n */\nfunction taxSuffix(isTaxInclusive?: boolean | null): string {\n return isTaxInclusive ? \" incl. VAT\" : \"\"\n}\n\n// Machine-readable cart line items so an agent can itemize the cart from structuredContent instead\n// of re-parsing the prose (D2). Untrusted titles sanitize()'d; ids/numbers pass through.\n// TB252: + size and colour (the stored key, e.g. \"coal\"), so an agent can read back what it added.\nfunction cartItems(cart: CartLike): Array<{\n variant_id: string | null\n title: string\n size: string | null\n colour: string | null\n qty: number\n total: number | null\n}> {\n return (cart.items ?? []).map((li) => {\n const size = lineSize(li)\n const colour = lineColour(li)\n return {\n variant_id: li.variant_id ?? null,\n title: sanitize(li.product_title ?? li.title ?? \"item\", 80),\n size: size ? sanitize(size, 20) : null,\n colour: colour ? sanitize(colour, 20) : null,\n qty: li.quantity ?? 1,\n total: li.total ?? null,\n }\n })\n}\n\nfunction cartSummary(cart: CartLike, region?: RegionLike): string {\n const lines: string[] = []\n lines.push(`cart ${cart.id}`)\n const items = cart.items ?? []\n if (items.length === 0) {\n lines.push(\" (empty)\")\n } else {\n for (const li of items) {\n const title = sanitize(li.product_title ?? li.title ?? \"item\", 80)\n // TB129 review: the colour as the buyer sees it (\"Black\", \"Faded Black\"), never the stored key (\"jet\").\n const rawColour = lineColour(li)\n const colour = rawColour ? colourLabel(rawColour) : null\n const size = lineSize(li)\n const tags = [colour, size].filter((x): x is string => !!x).map((x) => ` · ${sanitize(x, 20)}`).join(\"\")\n lines.push(` ${li.quantity}x ${title}${tags} — ${fmtPrice(li.total, cart.currency_code ?? region?.currency_code)}`)\n }\n }\n // CAT-44 — the full money block (subtotal → discount → shipping → total), identical to what the\n // CLI and the confirmation email print. A single `total` with shipping folded into it left both a\n // buyer and an agent unable to see what they were paying for. Shipping stays honestly labelled\n // \"added at checkout\" until a method is attached, so an agent never quotes a pre-shipping figure\n // as final (A2).\n const cur = cart.currency_code ?? region?.currency_code\n for (const row of cartMoneyRows({\n itemTotal: cart.item_total,\n discountTotal: cart.discount_total,\n shippingTotal: cart.shipping_total,\n hasShippingMethod: (cart.shipping_methods ?? []).length > 0,\n total: cart.total,\n })) {\n const value = row.display ?? `${row.kind === \"discount\" ? \"−\" : \"\"}${fmtPrice(row.amount, cur)}`\n const suffix = row.kind === \"total\" ? taxSuffix(isCartTaxInclusive(cart)) : \"\"\n lines.push(` ${row.label} ${value}${suffix}${row.note ? ` (${row.note})` : \"\"}`)\n }\n return lines.join(\"\\n\")\n}\n\n// ----- the 10 tools ------------------------------------------------------------\n\n/** Build the 10 tool definitions bound to a StoreClient. */\nexport function makeTools(client: StoreClient, opts: ToolsOpts = {}): ToolDef[] {\n const regionCC = opts.region\n\n // list_products — TEXT-ONLY catalog (no per-item images — token-bloat guard, §4.3).\n const list_products: ToolDef = {\n name: \"list_products\",\n description:\n \"List the pieces in stock (text-only — no images, to keep the agent's context lean). \" +\n \"Returns each product's handle, price (VAT included where applicable), and stage. Use get_product for one item's image. \" +\n \"Prices are in the region's currency — pass `region` (an ISO-2 country) to price for a specific market, else the server default.\",\n inputSchema: {\n stage: z.string().optional().describe(\"filter by channel stage: 'main' or 'next'\"),\n region: z.string().optional().describe(\"ISO-2 ship-to country to price for (e.g. se / us / de); overrides the server default\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const cc = (typeof args.region === \"string\" && args.region.trim()) || regionCC\n const rr = await client.resolveRegionResult(cc)\n const region = rr.region\n const products = await client.listProducts({ region })\n const stage = typeof args.stage === \"string\" ? args.stage.toLowerCase() : undefined\n const rows = products\n .map((p) => ({ p, stage: String(p.metadata?.[\"channel_stage\"] ?? \"main\") }))\n .filter((r) => (stage ? r.stage.toLowerCase() === stage : true))\n if (rows.length === 0) {\n return result(\"nothing in stock on this branch yet.\", { products: [], currency: region.currency_code, region: rr.requested ?? null })\n }\n // CAT-31: a design sold in two fits (Regular 349 / Oversize 399) is ONE row priced \"from <cheapest>\",\n // with its fits listed so an agent knows to pass `fit` to get_product / add_to_cart.\n const lines = rows.map(({ p, stage }) => {\n const { amount, currency, taxInclusive, from } = priceFrom(p)\n const fits = productFits(p)\n const handle = sanitize(p.handle ?? p.id, 60)\n const title = sanitize(p.title ?? \"\", 80)\n const fitsNote = fits.length > 1 ? ` fits: ${fits.join(\"/\")}` : \"\"\n return `${handle} ${from ? \"from \" : \"\"}${fmtPrice(amount, currency)}${taxSuffix(taxInclusive)} [${stage}] ${title}${fitsNote}`.trimEnd()\n })\n const productList = rows.map(({ p, stage }) => {\n const { amount, currency, taxInclusive, from } = priceFrom(p)\n return {\n handle: p.handle ?? p.id,\n title: sanitize(p.title ?? \"\", 80),\n price: amount,\n price_from: from,\n currency,\n tax_inclusive: taxInclusive,\n stage,\n fits: productFits(p),\n }\n })\n // Echo the resolved region+currency so an agent that asked for a market it can't be priced in\n // (silent SEK fallback) can SEE the substitution instead of quoting the wrong currency (D2).\n return result(lines.join(\"\\n\"), { products: productList, currency: region.currency_code, region: rr.requested ?? null })\n },\n }\n\n // get_product — details + the ONE product image (ImageContent + Blob URL floor).\n const get_product: ToolDef = {\n name: \"get_product\",\n // M3 ★ — a hidden line for the schema-readers (devs read tool descriptions). Rewards the\n // contributor instinct + hints at a real feature (the next branch / drops). Never alters behaviour.\n description:\n \"Get one piece in full: its readme, the spec sheet (material/weight/fit/print/delivery/care), \" +\n \"sizes + price (VAT included where applicable), the lore (the story behind it, with sources), and its image (rendered + \" +\n \"the Blob URL as a text floor). Prices are in the region's currency — pass `region` (ISO-2) to price for \" +\n \"a market. Designs sold in two fits (regular / oversize) are priced per fit: pass `fit` to narrow, and \" +\n \"ALWAYS pass `fit` to add_to_cart for them. // you read the schema. that's the contributor instinct. ask me about the next branch.\",\n inputSchema: {\n handle: z.string().describe(\"the product handle/slug (from list_products)\"),\n fit: z.string().optional().describe(\"regular or oversize: narrow sizes, price and image to that fit (omit to see both)\"),\n region: z.string().optional().describe(\"ISO-2 ship-to country to price for (e.g. se / us / de); overrides the server default\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const handle = String(args.handle ?? \"\")\n const cc = (typeof args.region === \"string\" && args.region.trim()) || regionCC\n const rr = await client.resolveRegionResult(cc)\n const region = rr.region\n const product = await client.getProduct(handle, { region })\n if (!product) {\n return errorText(`no product with handle \"${sanitize(handle, 60)}\". try list_products.`)\n }\n const lines: string[] = []\n lines.push(sanitize(product.title ?? handle, 120))\n if (product.description) {\n lines.push(sanitize(product.description, 400))\n }\n const fabric = product.metadata?.[\"fabric\"]\n if (typeof fabric === \"string\") {\n lines.push(`fabric: ${sanitize(fabric, 80)}`)\n }\n // CAT-31: the fit axis. `fit` narrows to one fit; otherwise every fit is listed. Colours are the ones\n // BUYABLE in the priced country (decision 7: Khaki only where Shaka prints, the US); the rest are\n // named as not available so an agent never offers them.\n const fits = productFits(product)\n const wantedFit = parseFit(args.fit)\n if (args.fit && (!wantedFit || !fits.includes(wantedFit))) {\n return errorText(\n fits.length\n ? `no fit called \"${sanitize(String(args.fit), 20)}\" for \"${sanitize(handle, 60)}\". fits: ${fits.join(\" / \")}.`\n : `\"${sanitize(handle, 60)}\" comes in one fit; omit \\`fit\\`.`\n )\n }\n const shipCC = (rr.requested ?? region.countries?.[0]?.iso_2 ?? \"\").toLowerCase() || null\n const shownFits: Array<import(\"@binaryandthread/store-client\").Fit | null> = fits.length ? (wantedFit ? [wantedFit] : fits) : [null]\n if (fits.length > 1) {\n lines.push(`fit: ${fits.map((f) => `${f} (${fmtPrice(priceFor(product, f).amount, priceFor(product, f).currency)})`).join(\" · \")}${wantedFit ? ` (showing ${wantedFit})` : \"\"}`)\n }\n // A3 — available colours, DERIVED from the variants (never hardcoded). Sizes are listed per\n // colour (and per fit) when there's a colour axis, else a flat list (legacy Size-only product).\n const colours = productColours(product)\n const availability: Record<string, Record<string, string[]>> = {}\n const unavailableColours: Record<string, string[]> = {}\n if (colours.length > 0) {\n for (const f of shownFits) {\n const buyable = coloursFor(product, f, shipCC)\n const notBuyable = coloursFor(product, f).filter((c) => !buyable.includes(c))\n const indent = f ? \" \" : \"\"\n if (f) lines.push(`${fitLabel(f).toLowerCase()}:`)\n if (!f) lines.push(`available colours: ${buyable.map((c) => sanitize(colourLabel(c), 20)).join(\", \")}`)\n const perColour: Record<string, string[]> = {}\n for (const c of buyable) {\n // CAT-37 — only the sizes the ship-to blank can PRINT (Shaka, the US oversize blank, has\n // no XS; SOL'S does). Listing one we cannot make invites add_to_cart to charge for it.\n const cs = sizesFor(product, f, c, shipCC)\n perColour[c] = cs\n lines.push(`${indent} ${sanitize(colourLabel(c), 20)}: ${cs.map((x) => sanitize(x, 20)).join(\" \")}`)\n }\n for (const c of notBuyable) {\n lines.push(`${indent} ${sanitize(colourLabel(c), 20)}: not available where you ship`)\n }\n if (f) {\n availability[f] = perColour\n if (notBuyable.length) unavailableColours[f] = notBuyable\n }\n }\n } else {\n const sizes = (product.variants ?? []).map((v) => sanitize(v.title ?? \"?\", 20))\n lines.push(`sizes: ${sizes.join(\" \")}`)\n }\n const v0 = (product.variants ?? [])[0]\n const shownPrice = wantedFit ? { ...priceFor(product, wantedFit), from: false } : priceFrom(product)\n if (shownPrice.amount != null) {\n lines.push(`price: ${shownPrice.from ? \"from \" : \"\"}${fmtPrice(shownPrice.amount, shownPrice.currency)}${taxSuffix(shownPrice.taxInclusive)}`)\n }\n // The full spec sheet (the PDP's technical block, per fit) + the lore — so the agent knows the piece,\n // not just its price. Single-sourced from store-client (matches the storefront).\n const specs = productSpecs(product, wantedFit ?? (fits.length ? fits[0] : null))\n lines.push(\"specs:\")\n for (const s of specs) {\n lines.push(` ${sanitize(s.key, 20)}: ${sanitize(s.value, 120)}`)\n }\n const lore = productLore(product)\n if (lore.text) {\n lines.push(`lore: ${sanitize(lore.text, 600)}`)\n for (const src of lore.sources) {\n lines.push(` source: ${sanitize(src.label, 80)} (${sanitize(src.url, 300)})`)\n }\n }\n const content: ImageBlock[] = [{ type: \"text\", text: lines.join(\"\\n\") }]\n // The image: the shown fit's first buyable colour by image METADATA (fit-neutral plp when the fit has\n // no shot of its own), else the thumbnail. A colour with no shot yet (Khaki) never picks another colour's.\n const imgFit = wantedFit ?? (fits.length ? fits[0]! : null)\n const imgColour = colours.length ? (coloursFor(product, imgFit, shipCC)[0] ?? null) : null\n const imageUrl = (imgColour ? imageFor(product, { fit: imgFit, colour: imgColour }) : null) ?? product.thumbnail ?? null\n if (imageUrl) {\n const img = await buildImageContent(imageUrl)\n content.push(...img)\n }\n // A machine variant map so an agent gets an authoritative variant_id + fit/size/colour + price for\n // add_to_cart, instead of guessing from free-text (D2). `size` is the REAL size (never the title).\n const variantMap = (product.variants ?? []).map((v) => {\n const vFit = variantFit(v)\n const vColour = variantColour(v) ?? null\n const vSize = sizeOfVariant(v)\n // The gates judge by the BLANK, so a legacy product (no Fit option — the live catalog today)\n // is judged by its implied fit; otherwise `buyable` would read true for everything.\n const gFit = gateFit(product, vFit)\n return {\n variant_id: v.id,\n fit: vFit,\n size: vSize,\n colour: vColour,\n price: v.calculated_price?.calculated_amount ?? null,\n // Whether add_to_cart will ACCEPT this id for the priced region (money-path review 2026-09-04):\n // the map used to hand out ids for colours the ship-to blank can't print, with the caveat buried\n // in a separate unavailable_colours field. Same derived answer, on the row an agent actually uses.\n // CAT-37 added the SIZE half — the US and EU oversize blanks have different size ranges.\n buyable:\n // TB129 review: judged by the product's OWN garment (a 4711 is not a 5082).\n (!gFit || !vColour || colourBuyable(gFit, vColour, shipCC, supplierOf(product), printfulBlankOf(product))) &&\n (!gFit || !vSize || sizeBuyable(gFit, vSize, shipCC, supplierOf(product), printfulBlankOf(product))),\n }\n })\n const sizeList = colours.length > 0\n ? Object.fromEntries(colours.map((c) => [c, sizesForColour(product, c)]))\n : (product.variants ?? []).map((v) => v.title ?? \"?\")\n return {\n content,\n // G8/D3 — the untrusted backend TEXT (title/readme/specs/lore) is sanitize()'d before it\n // enters the machine-readable path too (the header's promise), not just the text block;\n // ids/urls/numbers pass through redactForOutput unchanged.\n structuredContent: redactForOutput({\n handle: product.handle ?? handle,\n title: sanitize(product.title ?? \"\", 120),\n price: shownPrice.amount ?? v0?.calculated_price?.calculated_amount ?? null,\n price_from: shownPrice.from,\n prices_by_fit: Object.fromEntries(fits.map((f) => [f, priceFor(product, f).amount])),\n currency: shownPrice.currency ?? v0?.calculated_price?.currency_code ?? null,\n tax_inclusive: shownPrice.taxInclusive ?? v0?.calculated_price?.is_calculated_price_tax_inclusive ?? null,\n region: rr.requested ?? null,\n fits,\n fit: wantedFit ?? null,\n colours,\n sizes: sizeList,\n ...(fits.length ? { availability } : {}),\n ...(Object.keys(unavailableColours).length ? { unavailable_colours: unavailableColours } : {}),\n variants: variantMap,\n image_url: imageUrl,\n // the PDP content, machine-readable: readme + the spec sheet + the lore (+ sources).\n readme: sanitize(productReadme(product), 2000),\n specs: specs.map((s) => ({ key: sanitize(s.key, 40), value: sanitize(s.value, 200) })),\n lore: {\n text: sanitize(lore.text ?? \"\", 1200),\n sources: lore.sources.map((s) => ({ label: sanitize(s.label, 120), url: s.url })),\n },\n }) as Record<string, unknown>,\n }\n },\n }\n\n // add_to_cart — create-or-reuse a cart; return the cart_id handle + the server cart.\n const add_to_cart: ToolDef = {\n name: \"add_to_cart\",\n description:\n \"Add a piece to a cart. Pass cart_id to reuse an existing cart (the durable handle), or omit it \" +\n \"to start a fresh one. Call get_product first to get valid sizes/colours (or an exact variant_id); \" +\n \"for a design sold in two fits (regular / oversize, priced apart) you MUST pass `fit`. \" +\n \"size is required when the colour comes in more than one size: ask the buyer, never choose one for them. \" +\n \"qty is capped at 20. Returns the cart_id and the server-side cart summary (VAT included where applicable). \" +\n \"`region` (ISO-2) sets the ship-to country + currency of a NEW cart. A reused cart ships where it already ships: \" +\n \"a `region` that disagrees with it is refused (start a new cart instead). If a reused cart has expired or was \" +\n \"already checked out, a fresh one is made in the same region and the reply says cart_replaced: true.\",\n inputSchema: {\n handle: z.string().optional().describe(\"product handle (resolves a variant by size + colour; call get_product for valid values)\"),\n variant_id: z.string().optional().describe(\"a specific variant id from get_product (overrides handle/size/colour)\"),\n fit: z.string().optional().describe(\"regular or oversize; REQUIRED when the design comes in both (get_product lists them)\"),\n size: z.string().optional().describe(\"size label from get_product (it lists the sizes you can buy where the cart ships); required when the colour comes in more than one size, ask the buyer\"),\n colour: z.string().optional().describe(\"piece colour from get_product, e.g. black / white, or the name shown, e.g. Faded Black (default: the first colour you can buy where the cart ships)\"),\n qty: z.number().int().positive().max(20).optional().describe(\"quantity, 1–20 (default 1)\"),\n cart_id: z.string().optional().describe(\"reuse this cart; omit to create a new one\"),\n region: z.string().optional().describe(\"ISO-2 ship-to country for a NEW cart (e.g. se / us / de); with a cart_id it must match where that cart ships, or nothing is added\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const reuse = typeof args.cart_id === \"string\" && args.cart_id.trim() ? args.cart_id.trim() : undefined\n const explicitCC = (typeof args.region === \"string\" && args.region.trim().toLowerCase()) || undefined\n\n // D1 review (HIGH): a REUSED cart is gated by where THAT cart ships, never by the `region` arg or the server\n // default. Read it first: its shipping address, else its region's first country. A cart we cannot read (404)\n // is replaced, and then the replacement's region is the gate, like a new cart.\n let existing: CartLike | null = null\n if (reuse) {\n try {\n existing = await client.getCart(reuse)\n } catch (e) {\n if (!(e instanceof RecoverableCartError)) throw e\n }\n // TB252 (plan-check 4): a cart that became an order still reads 200. Treat it like a 404: replaced,\n // gated by the region it is recreated in, never written to.\n if (existing?.completed_at) existing = null\n }\n\n let region: RegionLike\n let shipCC: string | null\n if (existing) {\n const to = await cartShipTo(existing, () => client.getRegions())\n if (!to) {\n // fail closed: a cart whose destination we cannot read cannot be gated\n return errorText(`can't tell where cart ${sanitize(reuse!, 60)} ships, so nothing was added. omit cart_id to start a new cart.`)\n }\n if (shipToDisagrees(explicitCC, to)) {\n return errorText(\n `cart ${sanitize(reuse!, 60)} ships to ${sanitize(to.country, 8)}, not ${sanitize(explicitCC!, 8)}. nothing was added. ` +\n `omit region to keep adding to this cart, or omit cart_id to start a new cart for ${sanitize(explicitCC!, 8)}.`\n )\n }\n region = to.region\n shipCC = to.country\n } else {\n // Fail CLOSED on a named ship-to country we don't have a region for — never create a cart in a\n // substituted currency (A1).\n const rr = await client.resolveRegionResult(explicitCC || regionCC)\n if (rr.requested && !rr.matched) {\n return errorText(`we don't ship to ${sanitize(rr.requested, 8)} yet — nothing was added. (list_products with a supported region to see prices.)`)\n }\n region = rr.region\n shipCC = (rr.requested ?? region.countries?.[0]?.iso_2 ?? \"\").toLowerCase() || null\n }\n const qty = typeof args.qty === \"number\" ? args.qty : 1\n\n // resolve the variant id\n let variantId = typeof args.variant_id === \"string\" ? args.variant_id : undefined\n if (variantId) {\n // A caller-supplied variant_id must clear the SAME region/fit gate as a resolved one (money-path\n // review 2026-09-04: this fast-path skipped resolveVariant entirely, so an agent could add a\n // colour the ship-to blank can't print — captured payment, then a held order at fulfillment).\n const owner = (await client.listProducts({ region })).find((p) =>\n (p.variants ?? []).some((v) => v.id === variantId)\n )\n const v = owner?.variants?.find((x) => x.id === variantId)\n if (!owner || !v) {\n return errorText(`no such variant \"${sanitize(variantId, 60)}\" in the catalog. call get_product for a current variant_id. nothing was added.`)\n }\n const vColour = variantColour(v)\n const vSize = sizeOfVariant(v)\n // Judge by the BLANK: a legacy product (no Fit option) contributes its implied fit, or this\n // whole gate no-ops on the catalog that is actually live today.\n const gFit = gateFit(owner, variantFit(v))\n const shipCC2 = shipCC\n // D1: judged by the product's OWN supplier's blank (a Printful product is not a Gildan one).\n const supplier = supplierOf(owner)\n // TB129 review: and by the product's OWN garment (a 4711 judged by the 5082 row refused every valid jet line).\n const blank = printfulBlankOf(owner)\n if (gFit && vColour && !colourBuyable(gFit, vColour, shipCC2, supplier, blank)) {\n return errorText(\n `${sanitize(colourLabel(vColour), 20)} is not available in ${gFit} for ${sanitize(shipCC2 ?? \"this region\", 8)}. nothing was added.`\n )\n }\n // CAT-37 — same gate for the SIZE. A caller-supplied variant_id must clear it too, or an agent\n // could add an Oversize XS for a US cart that Shaka cannot print (paid, then held at fulfillment).\n if (gFit && vSize && !sizeBuyable(gFit, vSize, shipCC2, supplier, blank)) {\n return errorText(\n `size ${sanitize(vSize, 20)} is not made in ${gFit} for ${sanitize(shipCC2 ?? \"this region\", 8)}. nothing was added.`\n )\n }\n }\n if (!variantId) {\n const handle = typeof args.handle === \"string\" ? args.handle : \"\"\n if (!handle) {\n return errorText(\"add_to_cart needs a variant_id or a handle (+ optional size/colour).\")\n }\n const product = await client.getProduct(handle, { region })\n if (!product) {\n return errorText(`no product with handle \"${sanitize(handle, 60)}\".`)\n }\n // A3/CAT-31 — resolve by fit AND size AND colour (DERIVED, never hardcoded), FAIL CLOSED: a two-fit\n // design without `fit` is an error listing the fits + prices (never pick a garment for the buyer);\n // a colour the ship-to country's blank does not offer is an error; a missing combo is a CLEAR\n // error (no substitution). colour defaults to the first colour buyable where the cart ships.\n const size = typeof args.size === \"string\" ? args.size : undefined\n const r = resolveVariant(product, {\n fit: typeof args.fit === \"string\" ? args.fit : undefined,\n size,\n colour: typeof args.colour === \"string\" ? args.colour : undefined,\n countryCode: shipCC,\n })\n if (!r.ok) {\n if (r.reason === \"ambiguous_fit\") {\n const fitList = r.fits.map((f) => `${f.fit} (${fmtPrice(f.amount, f.currency)})`).join(\" · \")\n // CAT-40: state the CHOICE and who makes it. The CLI's equivalent message once named a\n // single fit as the \"fix\" and a live agent staged it without ever asking the buyer.\n return errorText(\n `fit required for \"${sanitize(handle, 60)}\": ${fitList}. these are different garments at ` +\n `different prices — ASK the buyer which fit they want, don't choose for them, then pass ` +\n `fit: \"regular\" or fit: \"oversize\". nothing was added.`\n )\n }\n if (r.reason === \"fit_not_offered\") {\n return errorText(\n `\"${sanitize(handle, 60)}\" isn't sold in fit \"${sanitize(r.requested, 20)}\"` +\n `${r.fits.length ? ` — it comes in ${r.fits.join(\" / \")}` : \"\"}. nothing was added.`\n )\n }\n // M1 (TB252) — a colour the design is not made in AT ALL is its own answer, in the buyer's own word\n // with the real colours as they are passed. Checked before the region gate, which also fails an\n // unknown colour and would call it \"not available\" here (the CLI does the same).\n const colourAsked = \"colour\" in r ? r.colour : null\n const madeIn = coloursFor(product, productFits(product).length > 0 ? (r.fit ?? null) : null)\n if (colourAsked && (r.reason === \"fit_unavailable\" || r.reason === \"not_found\") && !madeIn.includes(colourAsked)) {\n return errorText(\n `\"${sanitize(handle, 60)}\" isn't made in ${sanitize(colourAsked, 20)}. colours: ${madeIn.map((c) => sanitize(c, 20)).join(\" · \")}` +\n ` (pass one as colour). nothing was added.`\n )\n }\n if (r.reason === \"fit_unavailable\") {\n return errorText(\n `${sanitize(colourLabel(r.colour ?? \"\"), 20)} is not available in ${r.fit} for ${sanitize(shipCC ?? \"this region\", 8)}. nothing was added.`\n )\n }\n // CAT-37 — the size exists on the design but the blank that ships HERE isn't made in it.\n if (r.reason === \"size_unavailable\") {\n return errorText(\n `size ${sanitize(r.size, 20)} is not made in ${r.fit} for ${sanitize(shipCC ?? \"this region\", 8)}` +\n `${r.sizes.length ? ` — sizes here: ${r.sizes.map((x) => sanitize(x, 20)).join(\" \")}` : \"\"}. nothing was added.`\n )\n }\n // M1 — the colour exists, the size is what it is not cut in.\n if (size && r.colour && r.sizes.length) {\n return errorText(\n `\"${sanitize(handle, 60)}\" doesn't come in size ${sanitize(size, 20)} in ${sanitize(r.colour, 20)}. ` +\n `sizes: ${sortSizes(r.sizes).map((x) => sanitize(x, 20)).join(\" \")}. nothing was added.`\n )\n }\n const have = r.colours.length ? ` available colours: ${r.colours.map((c) => sanitize(colourLabel(c), 20)).join(\", \")}.` : \"\"\n const haveSizes = r.sizes.length ? ` sizes: ${r.sizes.map((x) => sanitize(x, 20)).join(\" \")}.` : \"\"\n return errorText(\n `no variant for \"${sanitize(handle, 60)}\"${r.fit ? ` fit ${r.fit}` : \"\"}${size ? ` size ${sanitize(size, 20)}` : \"\"}` +\n `${r.colour ? ` (${sanitize(colourLabel(r.colour), 20)})` : \"\"}.${have}${haveSizes}`\n )\n }\n // M2 (TB252) — no size: refuse, never pick one (the first variant was S, silently). After every !r.ok\n // answer, so a fit/colour problem is named first; handle path only, a variant_id names its size.\n // A colour cut in one size has nothing to choose.\n if (!size?.trim()) {\n const sizes = sortSizes(\n r.colour\n ? sizesFor(product, r.fit, r.colour, shipCC)\n : [...new Set((product.variants ?? []).map((v) => sizeOfVariant(v)).filter((s): s is string => !!s))]\n )\n if (sizes.length > 1) {\n return errorText(\n `size required for \"${sanitize(handle, 60)}\": ${sizes.map((x) => sanitize(x, 20)).join(\" \")}` +\n `${r.colour ? ` in ${sanitize(r.colour, 20)}` : \"\"}. ask the buyer which size, don't choose for them, then pass size. nothing was added.`\n )\n }\n }\n variantId = r.variant.id\n }\n\n // create-or-reuse the cart; a stale/completed cart → make a fresh one IN THE SAME REGION (F-B2), and say so.\n const addTo = async (cartId: string): Promise<CartLike> => client.addLineItem(cartId, variantId!, qty)\n let cart: CartLike\n let replaced = !!reuse && !existing // the reused cart could not be read: it is replaced below\n if (existing) {\n try {\n cart = await addTo(reuse!)\n } catch (e) {\n if (!(e instanceof RecoverableCartError)) throw e\n replaced = true\n cart = await addTo((await client.createCart(region)).id)\n }\n } else {\n cart = await addTo((await client.createCart(region)).id)\n }\n const itemCount = (cart.items ?? []).reduce((n, li) => n + (li.quantity ?? 1), 0)\n const replacedNote = replaced\n ? `\\ncart ${sanitize(reuse!, 60)} had expired or was already checked out, so this is a NEW cart${existing ? \" (same region)\" : \"\"}. use the new cart_id from now on.`\n : \"\"\n return result(\n `added.\\ncart_id: ${cart.id} (reuse this handle on the next add / checkout)${replacedNote}\\n${cartSummary(cart, region)}`,\n {\n cart_id: cart.id,\n // only when a cart_id was passed, so a fresh-cart reply is unchanged\n ...(reuse ? { cart_replaced: replaced, ...(replaced ? { replaced_cart_id: reuse } : {}) } : {}),\n total: cart.total ?? null,\n currency: cart.currency_code ?? region.currency_code ?? null,\n item_count: itemCount,\n items: cartItems(cart),\n // A2 — `total` is pre-shipping until checkout attaches a method; flag it for the agent.\n total_excludes_shipping: (cart.shipping_methods ?? []).length === 0,\n }\n )\n },\n }\n\n // get_cart — the authoritative server cart summary.\n const get_cart: ToolDef = {\n name: \"get_cart\",\n description: \"Show a cart's current contents and total (VAT included where applicable), straight from the server.\",\n inputSchema: { cart_id: z.string().describe(\"the cart handle from add_to_cart\") },\n handler: async (args): Promise<ToolResult> => {\n const cartId = String(args.cart_id ?? \"\")\n const region = await client.resolveRegion(regionCC)\n try {\n const cart = await client.getCart(cartId)\n // M3 (TB252): a completed cart still reads 200; listing its items read as \"still in your cart\".\n if (cart.completed_at) {\n return {\n ...errorText(\n \"that cart was already checked out and became an order, so it cannot be edited. omit cart_id on the next add_to_cart to start a new one.\"\n ),\n structuredContent: { completed: true },\n }\n }\n const itemCount = (cart.items ?? []).reduce((n, li) => n + (li.quantity ?? 1), 0)\n return result(cartSummary(cart, region), {\n cart_id: cart.id,\n total: cart.total ?? null,\n currency: cart.currency_code ?? region.currency_code ?? null,\n item_count: itemCount,\n items: cartItems(cart),\n // A2 — signal that `total` is pre-shipping so a chaining agent doesn't quote it as final.\n total_excludes_shipping: (cart.shipping_methods ?? []).length === 0,\n })\n } catch (e) {\n if (e instanceof RecoverableCartError) {\n return errorText(\"that cart is no longer valid (expired or already committed). add an item to start a fresh one.\")\n }\n throw e\n }\n },\n }\n\n // checkout — async handle-ID hand-off. Returns INSTANTLY. NEVER polls / opens a browser /\n // sets an address. Payment details NEVER enter the chat (G8 confused-deputy / F-I4).\n const checkout: ToolDef = {\n name: \"checkout\",\n description:\n \"Mint a checkout hand-off for a cart. Returns a checkout_url + a session_id INSTANTLY — it does \" +\n \"NOT poll and does NOT complete the purchase. The human must open the link in their own browser \" +\n \"to enter their payment + ship-to details on the storefront (those details never enter this chat). \" +\n \"After they pay, call check_payment with the session_id to see the status.\",\n inputSchema: {\n cart_id: z.string().describe(\"the cart handle to check out\"),\n email: z.string().optional().describe(\"the buyer's email for the receipt (optional)\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const cartId = String(args.cart_id ?? \"\")\n const email = typeof args.email === \"string\" ? args.email : undefined\n // BUG 1: the headless checkout route requires the cart to carry a shipping method. Attach\n // one first (idempotent in the client; auto-picks the only/cheapest region option — it\n // sets a SHIPPING method, never a payment/address, so the G8 boundary is untouched). Fail\n // CLOSED: if no option is available we return an error and NEVER mint a checkout link.\n let shipped: CartLike\n try {\n shipped = await client.ensureCartShippingMethod(cartId)\n } catch (e) {\n if (e instanceof RecoverableCartError) {\n return errorText(\"that cart is no longer valid (expired or already committed). add an item to start a fresh one.\")\n }\n return errorText(\"couldn't attach a shipping method to this cart — checkout isn't available for it right now.\")\n }\n // A cart that already became an order (completed_at, or the mint's 409 when it completed between the\n // read and the mint) is never re-minted: say so, and point at check_payment / a fresh cart.\n const completedAnswer = (): ToolResult => ({\n ...errorText(\n \"that cart was already checked out and became an order. call check_payment with its session_id, or omit cart_id on add_to_cart to start a new one.\"\n ),\n structuredContent: { completed: true },\n })\n if (shipped.completed_at) {\n return completedAnswer()\n }\n // mintCheckout is idempotent in the client AND applies the dev-channel 10% server-side. We\n // return its result DIRECTLY — no poll, no browser, no address (the G8 boundary).\n let mint: Awaited<ReturnType<StoreClient[\"mintCheckout\"]>>\n try {\n mint = await client.mintCheckout(cartId, email, \"mcp\")\n } catch (e) {\n if (e instanceof StoreApiError && e.status === 409) {\n return completedAnswer()\n }\n throw e\n }\n const { checkout_url, session_id } = mint\n // A2 — quote the DISCOUNTED, shipping-inclusive total the mint computed (dev-channel 10% off),\n // not the pre-discount cart figure. Fall back to the shipped cart total if an older backend\n // didn't echo one.\n const finalTotal = mint.total ?? shipped.total ?? null\n const finalCurrency = mint.currency ?? shipped.currency_code ?? null\n const totalStr = `${fmtPrice(finalTotal, finalCurrency)}${taxSuffix(isCartTaxInclusive(shipped))}`\n // CAT-44 — itemise what makes up the total instead of asserting one number. The mint total is\n // shipping-inclusive and discount-applied, so the rows have to come from the SHIPPED cart plus\n // the mint's own discount (the dev-channel 10% is applied server-side at mint, never on the cart).\n const moneyLines = cartMoneyRows({\n itemTotal: shipped.item_total,\n discountTotal: mint.discount?.amount,\n discountLabel: \"dev channel\",\n discountNote: mint.discount ? `${mint.discount.pct}% off — for buying from the terminal/agent` : null,\n shippingTotal: shipped.shipping_total,\n hasShippingMethod: (shipped.shipping_methods ?? []).length > 0,\n total: finalTotal,\n })\n .filter((r) => r.kind !== \"total\") // the total is printed on its own line below\n .map((r) => {\n const value = r.display ?? `${r.kind === \"discount\" ? \"−\" : \"\"}${fmtPrice(r.amount, finalCurrency)}`\n return ` ${r.label}: ${value}${r.note ? ` (${r.note})` : \"\"}`\n })\n const body = [\n \"checkout ready. open this link in your browser to pay:\",\n ` ${checkout_url}`,\n ...moneyLines,\n ` total: ${totalStr}`,\n \"\",\n \"payment details are entered on the storefront (Stripe) — they never enter this chat, and\",\n \"I can't complete this for you. once you've paid, call check_payment with:\",\n ` session_id: ${session_id}`,\n \"\",\n // brand-delight #9 — the deadpan flourish. The boundary stays honest (the line above), then\n // one dry true line: an agent can do everything up to the card. The card is the human's.\n \"I staged it, priced it, and walked you to the door. the card swipe is yours — by design.\",\n ].join(\"\\n\")\n // redact defensively (the url/session id carry no secret, but the redactor is the rule).\n return result(redactForOutput(body) as string, {\n checkout_url,\n session_id,\n total: finalTotal,\n currency: finalCurrency,\n discount: mint.discount ?? null,\n // CAT-44 — an agent should be able to itemise the total without re-deriving it.\n item_total: shipped.item_total ?? null,\n shipping_total: shipped.shipping_total ?? null,\n })\n },\n }\n\n // check_payment — poll ONCE; the agent re-calls at its own cadence (async handle-ID, F-I4).\n const check_payment: ToolDef = {\n name: \"check_payment\",\n description:\n \"Check a checkout's payment status ONCE (call again later to re-check — this never blocks/loops). \" +\n \"Returns the status and, once placed, the order_id.\",\n inputSchema: { session_id: z.string().describe(\"the session_id from checkout\") },\n handler: async (args): Promise<ToolResult> => {\n const sessionId = String(args.session_id ?? \"\")\n let res\n try {\n res = await client.poll(sessionId)\n } catch (e) {\n // A bad/expired session id 404s — map it to an actionable line, not a raw \"HTTP 404\" (D5).\n if (e instanceof StoreApiError && e.status === 404) {\n return errorText(\"that checkout session is unknown or expired — mint a fresh one with checkout.\")\n }\n throw e\n }\n // ENG-39: once placed, surface the SETTLED order summary (real total + discount line) so the\n // agent reports what the buyer ACTUALLY paid — never the pre-payment cart estimate.\n const s = res.summary\n const out = redactForOutput({ status: res.status, order_id: res.order_id, summary: s ?? null })\n let human: string\n if (res.status === \"placed\") {\n // \"placed\": the order exists. Say what the status means and no more (D1 review: not \"paid\").\n const lines = [`placed — order ${s?.display_id ? `#${s.display_id}` : \"created\"}. order_id: ${res.order_id ?? \"—\"}`]\n if (s) {\n const cur = s.currency_code\n const vat = taxSuffix(s.is_tax_inclusive === true)\n if (s.item_total != null) lines.push(` subtotal ${fmtPrice(s.item_total, cur)}`)\n if (s.discount_total) {\n const codes = s.discount_codes.length ? ` (${s.discount_codes.join(\", \")})` : \"\"\n lines.push(` discount${codes} −${fmtPrice(s.discount_total, cur)}`)\n }\n if (s.shipping_total != null) lines.push(` shipping ${fmtPrice(s.shipping_total, cur)}`)\n lines.push(` total ${fmtPrice(s.total, cur)}${vat}`)\n }\n human = lines.join(\"\\n\")\n } else {\n human = `status: ${out.status} (call check_payment again in a moment if still pending)`\n }\n return { content: [{ type: \"text\", text: human }], structuredContent: out as Record<string, unknown> }\n },\n }\n\n // The customer's order stage in plain words (store-client orderStage). No production stage exists (Sam 2026-10-02).\n const STAGE_WORDS = {\n committed: \"placed, not shipped yet\",\n shipped: \"shipped\",\n delivered: \"delivered\",\n reverted: \"canceled or refunded\",\n } as const\n\n // get_order_status — status/timeline TEXT only; NO address (F-H3); id + exact-email, 404 on miss (F-H1).\n const get_order_status: ToolDef = {\n name: \"get_order_status\",\n description:\n \"Look up an order's status by its display id + the EXACT email on the order. Returns status and stage \" +\n \"(placed, shipped or delivered; no shipping address). A wrong id/email returns not-found (no enumeration).\",\n inputSchema: {\n display_id: z.string().describe(\"the order's display id (e.g. 1042)\"),\n email: z.string().describe(\"the exact email on the order\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const displayId = String(args.display_id ?? \"\")\n const email = String(args.email ?? \"\")\n // AUD-8: the shared client now POSTs to /store/order-lookup with the email in the BODY\n // (no longer a query string); getOrder still surfaces the 404→null (no-enumeration) contract.\n let looked: unknown | null\n try {\n looked = await client.getOrder(displayId, email)\n } catch (e) {\n if (e instanceof StoreApiError && e.status === 404) {\n looked = null\n } else {\n throw e\n }\n }\n if (!looked) {\n return errorText(`no order matches that id + email.`)\n }\n // Extract ONLY status/timeline fields — NEVER the shipping address (F-H3).\n const order = (looked as { order?: Record<string, unknown> }).order ?? (looked as Record<string, unknown>)\n // Brand rule (Sam 2026-10-02, memory customer-sees-shipped-or-not): the customer learns placed, shipped or\n // delivered, never a production stage. The fulfilment word goes through customerFulfillmentStatus, so an older\n // backend's raw \"fulfilled\" (made, not shipped) reads not_fulfilled; `stage` is the plain-English answer.\n const rawFulfillment = order[\"fulfillment_status\"]\n const fulfillment = customerFulfillmentStatus(typeof rawFulfillment === \"string\" ? rawFulfillment : null)\n const status = sanitize(String(order[\"status\"] ?? fulfillment ?? \"unknown\"), 60)\n const display = sanitize(String(order[\"display_id\"] ?? displayId), 30)\n const stage = STAGE_WORDS[orderStage({ status: String(order[\"status\"] ?? \"\"), fulfillment_status: fulfillment })]\n const lines = [`order #${display}`, `status: ${status}`, `stage: ${stage}`]\n if (fulfillment) {\n lines.push(`fulfillment: ${sanitize(fulfillment, 60)}`)\n }\n // The output is redacted — and we never selected the address, so no PII can leak (G10/F-H3).\n return result(redactForOutput(lines.join(\"\\n\")) as string, {\n display_id: order[\"display_id\"] ?? display,\n status,\n stage,\n fulfillment_status: fulfillment,\n })\n },\n }\n\n // report_issue — file a bug report / feedback to the maintainers (d14). ADDITIVE: it sends NO\n // payment/PII and cannot touch any order — the one way an agent can flag a problem. The message +\n // optional context are redacted SERVER-SIDE before they're emailed (G10); we return a reference.\n const report_issue: ToolDef = {\n name: \"report_issue\",\n description:\n \"File a bug report or feedback to the maintainers. Pass a short message (and optional \" +\n \"structured context like versions/ids). Returns a reference id. It sends no payment details \" +\n \"and cannot change any order — it just files the report. Tokens/emails are redacted server-side.\",\n inputSchema: {\n message: z.string().describe(\"what went wrong / the feedback (one or two sentences)\"),\n context: z\n // zod v4 requires an explicit key type: record(keyType, valueType). (Was record(value) —\n // a pre-existing TS2554 that tsup transpiled past; fixed so `npm run typecheck` is clean.)\n .record(z.string(), z.unknown())\n .optional()\n .describe(\"optional structured context (versions, ids) — tokens/emails are redacted server-side\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const message = String(args.message ?? \"\").trim()\n if (!message) {\n return errorText(\"report_issue needs a non-empty message describing the problem.\")\n }\n const context =\n args.context && typeof args.context === \"object\" && !Array.isArray(args.context)\n ? (args.context as Record<string, unknown>)\n : undefined\n // Defense-in-depth (L22/N2): the backend redacts server-side, but redact the message + context\n // HERE too so a token/email an agent stuffed into them never leaves this process in the clear.\n const safeMessage = redactForOutput(message) as string\n const safeContext = context ? (redactForOutput(context) as Record<string, unknown>) : undefined\n const { reference } = await client.report(safeMessage, safeContext)\n return result(`reported. thank you — reference: ${reference}`, { reference })\n },\n }\n\n // teapot — M2 ★. Returns 418. Always. It's a teapot. (RFC 2324). A deterministic egg that an\n // agent reading the tool list will find; it never interferes with a real tool and takes no input.\n const teapot: ToolDef = {\n name: \"teapot\",\n description: \"Returns 418. Always. It's a teapot. (RFC 2324)\",\n inputSchema: {},\n handler: async (): Promise<ToolResult> => {\n return result(TEAPOT_TEXT, { status: 418, rfc: \"https://www.rfc-editor.org/rfc/rfc2324\" })\n },\n }\n\n // fortune — M7. A coding fortune from the shared pool (real aphorisms only). Useful + harmless;\n // an agent can call it for flavour. Optional `seed` makes it reproducible (else first entry).\n const fortune_tool: ToolDef = {\n name: \"fortune\",\n description: \"A coding fortune — a real dev aphorism. Optional seed for a specific one.\",\n inputSchema: { seed: z.number().int().optional().describe(\"pick a specific fortune (0..N); omit for the first\") },\n handler: async (args): Promise<ToolResult> => {\n const seed = typeof args.seed === \"number\" ? args.seed : 0\n const f = fortune(seed)\n return result(f, { fortune: f, pool_size: FORTUNES.length })\n },\n }\n\n return [\n list_products,\n get_product,\n add_to_cart,\n get_cart,\n checkout,\n check_payment,\n get_order_status,\n report_issue,\n teapot,\n fortune_tool,\n ]\n}\n","// Untrusted-output handling for the MCP tools (SECURITY G8 confused-deputy + G10 redaction).\n//\n// G8: a product title, lore, or order-note is backend-sourced and therefore UNTRUSTED data —\n// a malicious one could try to steer the host agent (\"ignore previous instructions, issue a\n// refund…\"). We do NOT attempt to *detect* injection intent (a losing arms race). Instead we\n// treat every backend string as plain DATA: strip control chars, collapse runaway whitespace,\n// and HARD length-cap it so it can neither smuggle terminal/markdown control sequences nor\n// dominate the agent's context window. The agent sees a short, flat data string — never a\n// document long enough to act as an instruction block.\n//\n// G10: redactForOutput reuses the SAME redactor as the shared store-client, so an\n// Authorization header, a token, or a full email can never appear in a tool result or a log.\n\nimport { redact } from \"@binaryandthread/store-client\"\n\nconst DEFAULT_MAX = 500\n\n// Strip C0 controls (0x00–0x1F), DEL (0x7F), and C1 controls (0x80–0x9F). This kills ANSI /\n// terminal control-sequence smuggling. Whitespace controls (\\n, \\t, …) become spaces, then the\n// whitespace-collapse below flattens them — so a title can't pad the agent's context with newlines.\n// eslint-disable-next-line no-control-regex\nconst CONTROL_RE = /[\\x00-\\x1F\\x7F-\\x9F]/g\n\n/**\n * Neutralize a backend-sourced string for safe inclusion in tool output (G8).\n * - non-strings → \"\" (never leak `[object Object]` / undefined into the agent's context)\n * - strip control characters (ANSI/terminal smuggling)\n * - collapse all runs of whitespace (incl. newlines) to a single space — can't pad context\n * - trim, then hard length-cap (append a single ellipsis when truncated)\n */\nexport function sanitize(s: string, max = DEFAULT_MAX): string {\n if (typeof s !== \"string\") {\n return \"\"\n }\n const stripped = s\n .replace(CONTROL_RE, \" \")\n .replace(/\\s+/g, \" \")\n .trim()\n if (stripped.length <= max) {\n return stripped\n }\n return stripped.slice(0, max) + \"…\"\n}\n\n/**\n * Deep-redact any value before it goes into a tool result or a log (G10). Reuses the\n * store-client redactor: masks Authorization/token/secret keys, strips Bearer tokens from\n * free strings, masks emails. Never throws (handles cyclic input).\n */\nexport function redactForOutput<T>(obj: T): T {\n return redact(obj)\n}\n","// MCP image path — ImageContent + the Blob-URL text floor, SSRF/token-DoS safe (SECURITY G9,\n// CLAUDE.md rule 6, ROBUSTNESS §6 / F-A3).\n//\n// Why BOTH an image block AND a URL text block: Claude renders ImageContent natively on\n// claude.ai / the API, but Claude Code has a base64-bloat bug (#31208, 15–25k tokens/image) and\n// can fail to render it. So the Blob URL is ALWAYS emitted as a text block too — the reliable\n// floor (the buy is never blocked on rendering).\n//\n// SECURITY G9 — the fetch is the dangerous part, so it is gated HARD:\n// 1. host allowlist (reused verbatim from @binaryandthread/store-client — the SAME constant the\n// CLI uses) BEFORE any network call. A non-allowlisted host / look-alike subdomain → no fetch.\n// 2. scheme MUST be https — a `data:` or `http:` URL → no fetch (no inline payload, no plaintext).\n// 3. a byte cap (≤2MB) + a fetch timeout — over-cap or slow → URL-text-only (token-DoS guard).\n// 4. content-type must be an image — refuse to base64 a non-image payload.\n// On ANY of those (or any failure) we degrade to URL-text-only and fetch NOTHING. It NEVER throws.\n\nimport { isAllowedImageHost } from \"@binaryandthread/store-client\"\n\n/** ≤2MB — over this we return the URL only (token-DoS guard, G9). */\nexport const IMAGE_BYTE_CAP = 2 * 1024 * 1024\n/** Fetch timeout in ms — a slow/hung host must not stall the tool (a list/view is read-mostly). */\nexport const IMAGE_FETCH_TIMEOUT_MS = 5000\n\n/** An MCP content block — either an inline image or a text line. */\nexport type ImageBlock =\n | { type: \"image\"; data: string; mimeType: string }\n | { type: \"text\"; text: string }\n\nexport interface BuildImageOpts {\n /** Inject a fetch impl (tests). Defaults to global fetch. */\n fetchImpl?: typeof fetch\n /** Override the byte cap (tests). */\n byteCap?: number\n /** Override the timeout (tests). */\n timeoutMs?: number\n}\n\n/** The URL-text-only floor — always carries the Blob URL so the agent can hand it to the human. */\nfunction urlOnly(blobUrl: string): ImageBlock[] {\n return [{ type: \"text\", text: blobUrl }]\n}\n\n/** The \"we couldn't render it\" floor — still carries the URL (never blocks the buy). */\nfunction unavailable(blobUrl: string): ImageBlock[] {\n return [{ type: \"text\", text: `(image unavailable) ${blobUrl}` }]\n}\n\n/**\n * Build the content blocks for a single product image. Returns the Blob URL text floor in EVERY\n * case; adds an ImageContent block ONLY when the URL is an allowlisted-host https Blob URL that\n * fetches successfully within the byte cap + timeout as a real image. NEVER throws, NEVER fetches\n * a non-allowlisted / data: / non-https URL.\n */\nexport async function buildImageContent(\n blobUrl: string,\n opts: BuildImageOpts = {}\n): Promise<ImageBlock[]> {\n if (typeof blobUrl !== \"string\" || blobUrl.trim().length === 0) {\n return urlOnly(String(blobUrl ?? \"\"))\n }\n\n // (1) + (2): host allowlist + https-only, BEFORE any network call (G9 / rule 6).\n // isAllowedImageHost already returns false for an unparseable URL; we also require https.\n let scheme: string\n try {\n scheme = new URL(blobUrl).protocol\n } catch {\n return urlOnly(blobUrl)\n }\n if (scheme !== \"https:\") {\n return urlOnly(blobUrl) // data:, http:, etc. — never fetch (no SSRF, no plaintext, no inline payload)\n }\n if (!isAllowedImageHost(blobUrl)) {\n return urlOnly(blobUrl) // non-allowlisted host or look-alike subdomain — never fetch\n }\n\n const fetchImpl = opts.fetchImpl ?? (globalThis.fetch as typeof fetch)\n const byteCap = opts.byteCap ?? IMAGE_BYTE_CAP\n const timeoutMs = opts.timeoutMs ?? IMAGE_FETCH_TIMEOUT_MS\n\n // (3) + (4): the gated fetch. Any failure → URL-text floor (never throws — F-A3).\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), timeoutMs)\n try {\n const res = await fetchImpl(blobUrl as unknown as string, { signal: controller.signal })\n if (!res.ok) {\n return urlOnly(blobUrl)\n }\n const mimeType = (res.headers.get(\"content-type\") ?? \"\").split(\";\")[0]!.trim().toLowerCase()\n if (!mimeType.startsWith(\"image/\")) {\n return urlOnly(blobUrl) // refuse to base64 a non-image payload\n }\n // (M4) Reject an over-cap body BEFORE buffering it into memory, when the server declares its size.\n // The post-buffer check below still applies (Content-Length can be absent or wrong) — this just\n // avoids reading a known-too-large body at all.\n const declaredLen = Number.parseInt(res.headers.get(\"content-length\") ?? \"\", 10)\n if (Number.isFinite(declaredLen) && declaredLen > byteCap) {\n return urlOnly(blobUrl)\n }\n const buf = Buffer.from(await res.arrayBuffer())\n if (buf.byteLength === 0 || buf.byteLength > byteCap) {\n return urlOnly(blobUrl) // empty or over-cap → token-DoS guard\n }\n return [\n { type: \"image\", data: buf.toString(\"base64\"), mimeType },\n { type: \"text\", text: blobUrl }, // the reliable floor (bug #31208)\n ]\n } catch {\n return unavailable(blobUrl)\n } finally {\n clearTimeout(timer)\n }\n}\n","// MCP egg copy (VOICE §3.2). The MCP surface is read by AGENTS and the humans driving them, so its\n// eggs live in tool descriptions (devs read schemas) and in deterministic tool payloads. They NEVER\n// interfere with a real tool and never touch money.\n//\n// This mirrors the CLI's governed copy (packages/cli/src/eggs.ts). It is intentionally duplicated:\n// the MCP package composes over store-client only (not the CLI), so each channel owns its copy.\n// Brand voice (memory brand-voice-no-pod-no-tshirt): never \"t-shirt\"/\"tee\"/\"print\"/Gelato.\n\n/** M2 ★ — the bt.teapot tool body. Returns 418. Always. It's a teapot. (RFC 2324) */\nexport const TEAPOT_TEXT = \"418 i'm a teapot. we make clothing, not coffee. (RFC 2324)\"\n\n/** C10 / M7 — the shared fortune pool. REAL dev aphorisms only (gate 1: never invented). */\nexport const FORTUNES: ReadonlyArray<string> = [\n '// \"there are only two hard things in computer science: cache invalidation and naming things.\" — phil karlton',\n '// \"premature optimization is the root of all evil.\" — donald knuth',\n '// \"weeks of programming can save you hours of planning.\"',\n '// \"make it work, make it right, make it fast.\" — kent beck',\n '// \"talk is cheap. show me the code.\" — linus torvalds',\n '// \"given enough eyeballs, all bugs are shallow.\" — linus\\'s law',\n '// \"always code as if the person maintaining it is a violent psychopath who knows where you live.\" — john woods',\n '// \"it works on my machine.\"',\n]\n\n/** Deterministic fortune by seed (reproducible for tests + screenshots). */\nexport function fortune(seed = 0): string {\n const n = Number.isFinite(seed) ? Math.abs(Math.floor(seed)) : 0\n return FORTUNES[n % FORTUNES.length]!\n}\n","// Decode a JWT's `exp` claim LOCALLY (no signature verification — display/preflight only). Mirror\n// of packages/cli/src/jwt.ts (ENG-22 reuses the CLI pattern; the two packages don't import each\n// other — store-client is their only shared dep). Purely informational: never an authorization\n// decision (that's the backend's job).\n\nexport interface JwtExpiry {\n /** ISO timestamp the token expires, or null if no/invalid token or no exp claim. */\n expiresAt: string | null\n /** true if expired, false if still valid, null if unknown (no/invalid token / no exp). */\n expired: boolean | null\n}\n\nexport function jwtExpiry(jwt: string | null | undefined, now: number = Date.now()): JwtExpiry {\n if (!jwt) {\n return { expiresAt: null, expired: null }\n }\n const parts = jwt.split(\".\")\n if (parts.length < 2 || !parts[1]) {\n return { expiresAt: null, expired: null }\n }\n try {\n const b64 = parts[1].replace(/-/g, \"+\").replace(/_/g, \"/\")\n const payload = JSON.parse(Buffer.from(b64, \"base64\").toString(\"utf8\")) as { exp?: unknown }\n const exp = typeof payload.exp === \"number\" ? payload.exp : null\n if (exp == null) {\n return { expiresAt: null, expired: null }\n }\n const expMs = exp * 1000\n return { expiresAt: new Date(expMs).toISOString(), expired: expMs <= now }\n } catch {\n return { expiresAt: null, expired: null }\n }\n}\n","// Resolve the MCP server's transport config from the environment, falling back to the baked-in\n// PUBLIC production defaults so a host MCP config needs ZERO env to reach the live store\n// (d2 / P4.5-7b). Env ALWAYS wins (set BT_STORE_URL / BT_PUBLISHABLE_KEY for local dev / staging).\n// The JWT (BT_TOKEN) and region (BT_REGION) stay env-only — never baked, never read from chat (T8).\n\nimport { BT_DEFAULT_STORE_URL, BT_DEFAULT_PUBLISHABLE_KEY } from \"@binaryandthread/store-client\"\nimport { jwtExpiry } from \"./jwt\"\n\nexport interface ResolvedServerConfig {\n baseUrl: string\n publishableKey: string\n jwt?: string\n region?: string\n}\n\nexport function resolveServerConfig(\n env: NodeJS.ProcessEnv = process.env,\n // stderr by default — MCP stdio purity: stdout carries protocol frames only.\n warn: (message: string) => void = (m) => console.error(m)\n): ResolvedServerConfig {\n let jwt = env.BT_TOKEN || undefined\n if (jwt) {\n // ENG-22: preflight the token's exp at boot — a stale BT_TOKEN in a host's MCP config otherwise\n // fails opaque on the first authed call. Expired → warn + start unauthenticated (browsing and\n // guest checkout still work). Undecodable exp → KEEP it (the backend stays the authority).\n const { expired, expiresAt } = jwtExpiry(jwt)\n if (expired === true) {\n warn(\n `bt-mcp: BT_TOKEN is expired (since ${expiresAt}) — starting unauthenticated. ` +\n \"re-run `bt login` and update the token in your MCP config.\"\n )\n jwt = undefined\n }\n }\n return {\n baseUrl: env.BT_STORE_URL || BT_DEFAULT_STORE_URL,\n publishableKey: env.BT_PUBLISHABLE_KEY || BT_DEFAULT_PUBLISHABLE_KEY,\n jwt,\n region: env.BT_REGION || undefined,\n }\n}\n","// npx @binaryandthread/mcp — the stdio entrypoint.\n//\n// Config comes from the ENVIRONMENT only (SECURITY T8/§4); all of it has a baked-in default so\n// the server reaches the live store with ZERO env (d2 / P4.5-7b):\n// - BT_STORE_URL the Medusa Store API base URL (default: prod; override for dev)\n// - BT_PUBLISHABLE_KEY the publishable API key — PUBLIC by design (default: prod; T6)\n// - BT_TOKEN the customer JWT (OPTIONAL; from the OS keychain / a configured env)\n// - BT_REGION an ISO-2 country code to resolve pricing (OPTIONAL; never hardcoded)\n//\n// The JWT is read from the env the host MCP config injects (which a keychain helper can populate)\n// — it is NEVER read from the chat (T8). A secret key is rejected by the StoreClient (T6). On\n// stdio, NOTHING is written to stdout except MCP protocol frames — diagnostics go to stderr.\n\nimport { StoreClient } from \"@binaryandthread/store-client\"\nimport { redact } from \"@binaryandthread/store-client\"\nimport { startServer } from \"./server\"\nimport { makeTools } from \"./tools\"\nimport { resolveServerConfig } from \"./config\"\n\nasync function main(): Promise<void> {\n const { baseUrl, publishableKey, jwt, region } = resolveServerConfig()\n\n const client = new StoreClient({\n baseUrl,\n publishableKey,\n jwt,\n // All client logs route to stderr through redact() — stdout is reserved for the transport,\n // and no token/PII ever reaches a log line (G10).\n logger: {\n info: (...a) => process.stderr.write(JSON.stringify(redact(a)) + \"\\n\"),\n warn: (...a) => process.stderr.write(JSON.stringify(redact(a)) + \"\\n\"),\n error: (...a) => process.stderr.write(JSON.stringify(redact(a)) + \"\\n\"),\n },\n })\n\n const tools = makeTools(client, { region })\n await startServer(tools)\n process.stderr.write(`bt-mcp: ready on stdio — ${tools.length} tools registered.\\n`)\n}\n\nmain().catch((e) => {\n // Redact before printing — never leak a token in a crash trace.\n process.stderr.write(`bt-mcp: fatal: ${JSON.stringify(redact(String(e?.message ?? e)))}\\n`)\n process.exit(1)\n})\n"],"mappings":";;;AAOA,IAAM,iBAAiB,oBAAI,IAAI;EAC7B;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;AACF,CAAC;AAED,IAAM,O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","../../store-client/src/client.ts","../../store-client/src/defaults.ts","../../store-client/src/host-allowlist.ts","../../store-client/src/fit.ts","../../store-client/src/colour.ts","../../store-client/src/order-stage.ts","../../store-client/src/cart-ship-to.ts","../../store-client/src/product.ts","../../store-client/src/tax.ts","../../store-client/src/money-rows.ts","../src/server.ts","../src/version.ts","../src/onboarding.ts","../src/tools.ts","../src/sanitize.ts","../src/images.ts","../src/eggs.ts","../src/jwt.ts","../src/config.ts","../src/bin.ts"],"sourcesContent":["// Secret/PII redaction for logs and error envelopes (SECURITY G10).\n//\n// The single rule for the whole channel: Authorization headers, tokens, and full\n// emails NEVER reach a log line, an error message, or an MCP/CLI transcript. This\n// function is applied by the StoreClient's logger and error mapper before anything\n// is emitted. It is consumed by BOTH the CLI and (Wave 4) the MCP — one redactor.\n\nconst SENSITIVE_KEYS = new Set([\n \"authorization\",\n \"token\",\n \"access_token\",\n \"refresh_token\",\n \"jwt\",\n \"idempotency-key\",\n \"x-publishable-api-key\",\n \"password\",\n \"secret\",\n \"cookie\",\n \"set-cookie\",\n])\n\nconst MASK = \"[REDACTED]\"\n\n// Bearer-token-in-a-string (e.g. \"Authorization: Bearer abc.def.ghi\" or a stray \"Bearer xyz\").\nconst BEARER_RE = /Bearer\\s+[A-Za-z0-9._\\-]+/gi\n// A plausible email anywhere in a string.\nconst EMAIL_RE = /([^\\s@]+)@([^\\s@]+\\.[^\\s@]+)/g\n\n/** Mask an email to `d***@x` form (first char + stars, host first char). */\nexport function maskEmail(email: string): string {\n const at = email.indexOf(\"@\")\n if (at <= 0) {\n return email\n }\n const local = email.slice(0, at)\n const host = email.slice(at + 1)\n const localMasked = local[0] + \"*\".repeat(Math.max(2, local.length - 1))\n const hostMasked = host.length > 0 ? host[0] : \"x\"\n return `${localMasked}@${hostMasked}`\n}\n\nfunction redactString(s: string): string {\n return s.replace(BEARER_RE, \"Bearer [REDACTED]\").replace(EMAIL_RE, (_m, _l, _h) =>\n maskEmail(`${_l}@${_h}`)\n )\n}\n\n/**\n * Deep-redact an object/array/string. Returns a structurally-similar value with\n * sensitive keys masked, Bearer tokens stripped, and emails masked. Never throws.\n *\n * `_seen` memoises input-object → its REDACTED output. This does double duty: it makes the\n * function cycle-safe (a revisited object resolves to its own — possibly still-filling — output\n * container) AND ensures a REPEATED reference is redacted every time. The previous WeakSet guard\n * returned the *original* object on revisit, leaking any token/email reachable via a 2nd path (G10).\n * The output container is registered BEFORE recursing so a cycle points at the redacted copy.\n */\nexport function redact<T>(input: T, _seen?: Map<object, unknown>): T {\n if (input == null) {\n return input\n }\n if (typeof input === \"string\") {\n return redactString(input) as unknown as T\n }\n if (typeof input !== \"object\") {\n return input\n }\n\n const seen = _seen ?? new Map<object, unknown>()\n const existing = seen.get(input as object)\n if (existing !== undefined) {\n return existing as T\n }\n\n if (Array.isArray(input)) {\n const out: unknown[] = []\n seen.set(input as object, out) // register before recursing (cycle-safe)\n for (const v of input) {\n out.push(redact(v, seen))\n }\n return out as unknown as T\n }\n\n const out: Record<string, unknown> = {}\n seen.set(input as object, out) // register before recursing (cycle-safe)\n for (const [key, value] of Object.entries(input as Record<string, unknown>)) {\n if (SENSITIVE_KEYS.has(key.toLowerCase())) {\n out[key] = MASK\n } else if (key.toLowerCase() === \"email\" && typeof value === \"string\") {\n out[key] = maskEmail(value)\n } else {\n out[key] = redact(value, seen)\n }\n }\n return out as unknown as T\n}\n","// StoreClient — the ONE shared HTTP client over the B&T Medusa Store API.\n//\n// Design (ROBUSTNESS §0): the clients are DUMB. Every dangerous property lives in the\n// backend. This module is the single place the channel's HTTP-client invariants live so\n// they are written once and contract-tested once (the CLI and the Wave-4 MCP both use it):\n//\n// - publishable key on EVERY request (public by design, T6); customer JWT when present\n// - Idempotency-Key (crypto.randomUUID) on EVERY mutation, STABLE across a retry — honoured by\n// the checkout MINT (per-cart session reuse) and by the /store/carts idempotency middleware,\n// which answers 409 IDEMPOTENCY_CONFLICT + Retry-After while the first request is in flight (§2)\n// - retry 5xx, network errors, 429 and 409 IDEMPOTENCY_CONFLICT; NEVER retry another 4xx (F-A1/F-I2)\n// - region resolved before pricing — never a bare currency literal (F-A2/INV-11)\n// - 404, or a COMPLETED-cart answer, on a cart mutation → RecoverableCartError (CLI makes a fresh\n// cart). Any other 409 is a plain StoreApiError, never a reason to throw a live cart away (F-B2)\n// - all logs + error envelopes pass through redact() — no token/email ever leaks (G10)\n// - NO secret key is ever stored or sent — publishable key only (T6)\n\nimport { redact } from \"./redact\"\nimport type {\n CartLike,\n ClientLogger,\n DropRow,\n HeadlessCheckout,\n OrderSummary,\n PollResult,\n ProductLike,\n RegionLike,\n RegionResolution,\n ReportResult,\n RequestLinkResult,\n RetryPolicy,\n ShippingOptionLike,\n StoreClientOptions,\n VerifyLinkResult,\n WhoAmI,\n} from \"./types\"\n\nconst DEFAULT_RETRY: RetryPolicy = {\n attempts: 3,\n baseDelayMs: 200,\n maxDelayMs: 2000,\n}\n\n/** Cap on how long we'll honor a server's Retry-After, so a hostile/huge value can't hang us. */\nconst RETRY_AFTER_MAX_MS = 10_000\n\n/**\n * Per-attempt request timeout. A black-holed connection (socket accepted, no response) would\n * otherwise never settle, hanging every command/tool AND defeating the checkout poll's 20-min\n * deadline (F-I3) — the deadline can't be re-checked while stuck awaiting a dead fetch. 15s is\n * generous for the Store API while still failing closed on a hang.\n */\nconst DEFAULT_TIMEOUT_MS = 15_000\n\n/**\n * Extra fields to request on product reads. `+metadata` ADDS metadata to Medusa's default field set\n * (it doesn't replace it — variants + calculated_price still come through). metadata is where lore /\n * lore_sources / per-product spec fields live, and the Store API omits it unless asked. The `+` is\n * URL-encoded to %2B by URLSearchParams, which Medusa decodes back to `+metadata`.\n */\nconst PRODUCT_FIELDS = \"+metadata\"\n\n/**\n * Parse an HTTP Retry-After header → ms, or null if absent/unparseable. Accepts both forms:\n * delta-seconds (\"2\") and an HTTP-date. Capped at RETRY_AFTER_MAX_MS (d12).\n */\nfunction parseRetryAfterMs(res: Response): number | null {\n let raw: string | null = null\n try {\n raw = res.headers?.get?.(\"retry-after\") ?? null\n } catch {\n return null\n }\n if (!raw) {\n return null\n }\n const secs = Number(raw)\n let ms: number\n if (Number.isFinite(secs)) {\n ms = secs * 1000\n } else {\n const dateMs = Date.parse(raw)\n if (!Number.isFinite(dateMs)) {\n return null\n }\n ms = dateMs - Date.now()\n }\n return Math.min(Math.max(0, ms), RETRY_AFTER_MAX_MS)\n}\n\n/** A cart error the CLI maps to \"discard local cart_id, create a fresh cart, re-add\" (F-B2). */\nexport class RecoverableCartError extends Error {\n readonly recoverable = true as const\n readonly status: number\n constructor(message: string, status: number) {\n super(message)\n this.name = \"RecoverableCartError\"\n this.status = status\n }\n}\n\n/** A non-recoverable API error. Its message/envelope is already redacted (G10). */\nexport class StoreApiError extends Error {\n readonly status: number\n readonly path: string\n constructor(message: string, status: number, path: string) {\n super(message)\n this.name = \"StoreApiError\"\n this.status = status\n this.path = path\n }\n}\n\ntype HttpMethod = \"GET\" | \"POST\" | \"DELETE\"\n\ninterface RequestOpts {\n body?: unknown\n /** Mutations carry an Idempotency-Key. */\n idempotent?: boolean\n /** Treat a 404 or a completed-cart answer as a RecoverableCartError (cart mutations). */\n cartRecovery?: boolean\n query?: Record<string, string | number | undefined>\n /**\n * Statuses for which a 4xx is EXPECTED, not an error — the client_error envelope is NOT\n * logged (BUG 3: a guest whoami 401 is the normal unauthenticated path, not noise to dump).\n * The error is still thrown so the caller can branch on it.\n */\n expected4xx?: number[]\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n\n/** crypto.randomUUID with a defensive fallback (older runtimes). */\nfunction newUuid(): string {\n const g = globalThis as unknown as { crypto?: { randomUUID?: () => string } }\n if (g.crypto?.randomUUID) {\n return g.crypto.randomUUID()\n }\n // RFC4122-ish fallback — never the happy path, just defensive.\n return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0\n const v = c === \"x\" ? r : (r & 0x3) | 0x8\n return v.toString(16)\n })\n}\n\nexport class StoreClient {\n private readonly baseUrl: string\n private readonly publishableKey: string\n private readonly jwt?: string\n private readonly fetchImpl: typeof fetch\n private readonly logger: ClientLogger\n private readonly retry: RetryPolicy\n private readonly sleepImpl: (ms: number) => Promise<void>\n private readonly timeoutMs: number\n\n /** Cached resolved regions (read-only catalog optimisation). */\n private regionsCache: RegionLike[] | null = null\n\n constructor(opts: StoreClientOptions) {\n if (!opts.baseUrl) {\n throw new Error(\"StoreClient: baseUrl is required\")\n }\n if (!opts.publishableKey) {\n throw new Error(\"StoreClient: publishableKey is required\")\n }\n // Guard against accidentally shipping a secret key (T6 — defensive).\n if (/^sk_/.test(opts.publishableKey)) {\n throw new Error(\"StoreClient: refusing a secret key — the client carries only the publishable key (T6)\")\n }\n this.baseUrl = opts.baseUrl.replace(/\\/+$/, \"\")\n this.publishableKey = opts.publishableKey\n this.jwt = opts.jwt\n this.fetchImpl = opts.fetchImpl ?? (globalThis.fetch as typeof fetch)\n this.logger = opts.logger ?? console\n this.retry = { ...DEFAULT_RETRY, ...(opts.retry ?? {}) }\n this.sleepImpl = opts.sleepImpl ?? sleep\n this.timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS\n }\n\n // ----- the request engine --------------------------------------------------\n\n private buildUrl(path: string, query?: RequestOpts[\"query\"]): string {\n const url = new URL(this.baseUrl + path)\n if (query) {\n for (const [k, v] of Object.entries(query)) {\n if (v !== undefined && v !== null && v !== \"\") {\n url.searchParams.set(k, String(v))\n }\n }\n }\n return url.toString()\n }\n\n private baseHeaders(): Record<string, string> {\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n \"x-publishable-api-key\": this.publishableKey,\n }\n if (this.jwt) {\n headers[\"Authorization\"] = `Bearer ${this.jwt}`\n }\n return headers\n }\n\n private backoffMs(attemptIndex: number): number {\n // exponential base with full jitter, capped.\n const exp = this.retry.baseDelayMs * 2 ** attemptIndex\n const capped = Math.min(exp, this.retry.maxDelayMs)\n return Math.floor(Math.random() * (capped + 1))\n }\n\n private async request<T>(method: HttpMethod, path: string, opts: RequestOpts = {}): Promise<T> {\n const headers = this.baseHeaders()\n // Generate the idempotency key ONCE and reuse it across retries (ROBUSTNESS §2). The server\n // honours it: the checkout MINT reuses the open per-cart session, and the /store/carts\n // idempotency middleware replays a finished request's response for the same key, or answers\n // 409 IDEMPOTENCY_CONFLICT + Retry-After while the first is still in flight. That 409 is retried\n // below like a 429, with this same key, so a retried createCart/addLineItem never double-applies.\n if (opts.idempotent) {\n headers[\"Idempotency-Key\"] = newUuid()\n }\n const url = this.buildUrl(path, opts.query)\n const init: RequestInit = {\n method,\n headers,\n ...(opts.body !== undefined ? { body: JSON.stringify(opts.body) } : {}),\n }\n\n let lastErr: unknown\n for (let attempt = 0; attempt < this.retry.attempts; attempt++) {\n // A server-dictated delay for THIS attempt's backoff (429 Retry-After); null → jittered backoff.\n let retryAfterMs: number | null = null\n // Bound THIS attempt: a hung connection is aborted after timeoutMs. The AbortError lands in\n // the catch below and is treated as a retryable network error (so the backoff + retry, and\n // ultimately the exhausted-retries StoreApiError, all apply). Fresh controller per attempt.\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), this.timeoutMs)\n try {\n const res = await this.fetchImpl(url as unknown as string, { ...init, signal: controller.signal })\n if (res.ok) {\n // 204 / empty body tolerance\n const text = await res.text()\n return (text ? JSON.parse(text) : {}) as T\n }\n\n // Read the error body defensively (may not be JSON).\n let parsed: Record<string, unknown> = {}\n try {\n const t = await res.text()\n parsed = t ? (JSON.parse(t) as Record<string, unknown>) : {}\n } catch {\n parsed = {}\n }\n\n // The same Idempotency-Key is still in flight server-side (the /store/carts middleware).\n // NOT a dead cart: wait Retry-After and replay with the same key.\n const idempotencyConflict = res.status === 409 && parsed.code === \"IDEMPOTENCY_CONFLICT\"\n\n // Cart recovery: a stale/completed cart on a cart mutation (F-B2). A 404, or an answer that SAYS\n // the cart completed (the backend guard's 409 { type: \"cart_completed\" }, or Medusa's own words).\n // A bare 409 is not enough: read as a dead cart, a live one would be thrown away and replaced.\n const parsedType = String(parsed.type ?? \"\")\n if (\n opts.cartRecovery &&\n (res.status === 404 ||\n parsedType === \"cart_completed\" ||\n parsedType.includes(\"cart.completed\") ||\n /cart.*complete/i.test(String(parsed.message ?? \"\")))\n ) {\n throw new RecoverableCartError(\n `cart is stale or completed (HTTP ${res.status})`,\n res.status\n )\n }\n\n // 429 Too Many Requests, or 409 IDEMPOTENCY_CONFLICT — RETRYABLE, honoring Retry-After (d12).\n // Must be handled BEFORE the generic 4xx-no-retry rule below, or either becomes a hard failure.\n if (idempotencyConflict) {\n retryAfterMs = parseRetryAfterMs(res)\n lastErr = new StoreApiError(`request in flight (HTTP 409)`, 409, path)\n this.logWarn(\"store.request.idempotency_conflict\", { path, attempt: attempt + 1, retryAfterMs })\n } else if (res.status === 429) {\n retryAfterMs = parseRetryAfterMs(res)\n lastErr = new StoreApiError(`rate limited (HTTP 429)`, 429, path)\n this.logWarn(\"store.request.rate_limited\", {\n path,\n attempt: attempt + 1,\n retryAfterMs,\n })\n } else if (res.status >= 400 && res.status < 500) {\n // other 4xx is a client error — do NOT retry.\n // Suppress the envelope for EXPECTED 4xx (e.g. a guest whoami 401 — BUG 3): it's a\n // normal branch the caller handles, not noise to dump to the user's terminal.\n if (!(opts.expected4xx ?? []).includes(res.status)) {\n this.logError(\"store.request.client_error\", { path, status: res.status, body: parsed })\n }\n throw new StoreApiError(\n `request failed (HTTP ${res.status})`,\n res.status,\n path\n )\n } else {\n // 5xx — retryable. Fall through to the retry loop.\n lastErr = new StoreApiError(`upstream error (HTTP ${res.status})`, res.status, path)\n this.logWarn(\"store.request.retryable\", { path, status: res.status, attempt: attempt + 1 })\n }\n } catch (e) {\n // RecoverableCartError / StoreApiError(4xx) must propagate immediately.\n if (e instanceof RecoverableCartError) {\n throw e\n }\n if (e instanceof StoreApiError && e.status >= 400 && e.status < 500) {\n throw e\n }\n // Network error, an aborted (timed-out) request, or a 5xx StoreApiError → retryable.\n lastErr = e\n this.logWarn(\"store.request.network_retry\", { path, attempt: attempt + 1 })\n } finally {\n // Always clear the per-attempt timeout — on success-return, on a rethrow, or before a retry.\n clearTimeout(timer)\n }\n\n // Backoff before the next attempt (skip after the final attempt). A 429 Retry-After, when\n // present, dictates the delay; otherwise jittered exponential backoff (d12).\n if (attempt < this.retry.attempts - 1) {\n await this.sleepImpl(retryAfterMs ?? this.backoffMs(attempt))\n }\n }\n\n // Exhausted retries. The thrown envelope is already free of secrets (we only ever\n // log/throw redacted, derived data — never the request headers/body).\n this.logError(\"store.request.exhausted\", { path, attempts: this.retry.attempts })\n if (lastErr instanceof StoreApiError) {\n throw lastErr\n }\n throw new StoreApiError(\n `request failed after ${this.retry.attempts} attempts: ${path}`,\n 0,\n path\n )\n }\n\n private logWarn(event: string, ctx: Record<string, unknown>): void {\n this.logger.warn(event, redact(ctx))\n }\n\n private logError(event: string, ctx: Record<string, unknown>): void {\n this.logger.error(event, redact(ctx))\n }\n\n // ----- regions (F-A2 / INV-11) ---------------------------------------------\n\n async getRegions(): Promise<RegionLike[]> {\n const data = await this.request<{ regions: RegionLike[] }>(\"GET\", \"/store/regions\")\n this.regionsCache = data.regions ?? []\n return this.regionsCache\n }\n\n /**\n * Resolve a region by country code, WITH provenance (matched / home / requested), so callers on\n * the money path can fail closed on an unmatched explicit country instead of silently pricing in\n * the wrong currency (F-A2). Region #1 is the launch/home default — SEK today, derived, never a\n * hardcoded literal.\n *\n * Semantics:\n * - country given AND a region ships there → { matched: true, home: false }.\n * - country given, none ships there → { matched: false, home: false } (money surfaces MUST\n * refuse; discovery may show the home region + a warning). We still return a region so a\n * discovery command can render *something*, but `matched:false` is the signal to fail closed.\n * - no country given → { matched: false, home: true } (the legitimate\n * launch default — not a \"wrong\" currency).\n */\n async resolveRegionResult(countryCode?: string): Promise<RegionResolution> {\n const regions = this.regionsCache ?? (await this.getRegions())\n if (regions.length === 0) {\n // Fail closed: refuse to price without a resolved region (F-A2).\n throw new Error(\"no regions configured — cannot resolve a region for pricing\")\n }\n const home = regions[0]! // region #1 — the launch region (SEK today, derived not hardcoded)\n if (countryCode) {\n const cc = countryCode.toLowerCase()\n const match = regions.find((r) =>\n (r.countries ?? []).some((c) => (c.iso_2 ?? \"\").toLowerCase() === cc)\n )\n if (match) {\n return { region: match, matched: true, requested: cc, home: false }\n }\n // A country code was given but no region ships there. Warn (stderr, redacted) so the\n // mis-resolution is observable, and report matched:false so money surfaces fail closed.\n this.logWarn(\"store.region.unmatched\", { countryCode: cc, fallback: home.id })\n return { region: home, matched: false, requested: cc, home: false }\n }\n return { region: home, matched: false, requested: undefined, home: true }\n }\n\n /**\n * Resolve a region by country code, falling back to region #1. A thin wrapper over\n * resolveRegionResult for callers that only need the region (back-compat).\n * SEK is region #1 by configuration — NEVER a hardcoded literal.\n */\n async resolveRegion(countryCode?: string): Promise<RegionLike> {\n return (await this.resolveRegionResult(countryCode)).region\n }\n\n // ----- products (region resolved BEFORE pricing) ---------------------------\n\n async listProducts(opts?: { region?: RegionLike; countryCode?: string }): Promise<ProductLike[]> {\n const region = opts?.region ?? (await this.resolveRegion(opts?.countryCode))\n // Paginate (L12): a single limit:100 call silently drops the 101st product. Page through until a\n // short page, with a hard cap so a misbehaving API can never spin this unbounded.\n const PAGE = 100\n const HARD_CAP = 1000\n const all: ProductLike[] = []\n for (let offset = 0; offset < HARD_CAP; offset += PAGE) {\n const data = await this.request<{ products: ProductLike[] }>(\"GET\", \"/store/products\", {\n // +metadata: Medusa omits product.metadata by default, and that's where lore/lore_sources\n // (and any per-product spec fields) live — without this the CLI/MCP never see them.\n query: { region_id: region.id, limit: PAGE, offset, fields: PRODUCT_FIELDS },\n })\n const page = data.products ?? []\n all.push(...page)\n if (page.length < PAGE) {\n break\n }\n }\n return all\n }\n\n async getProduct(handle: string, opts?: { region?: RegionLike; countryCode?: string }): Promise<ProductLike | null> {\n const region = opts?.region ?? (await this.resolveRegion(opts?.countryCode))\n const data = await this.request<{ products: ProductLike[] }>(\"GET\", \"/store/products\", {\n query: { handle, region_id: region.id, limit: 1, fields: PRODUCT_FIELDS },\n })\n return (data.products ?? [])[0] ?? null\n }\n\n // ----- cart (mutations carry Idempotency-Key) ------------------------------\n\n async createCart(region: RegionLike): Promise<CartLike> {\n const data = await this.request<{ cart: CartLike }>(\"POST\", \"/store/carts\", {\n idempotent: true,\n body: { region_id: region.id },\n })\n return data.cart\n }\n\n async addLineItem(cartId: string, variantId: string, quantity: number): Promise<CartLike> {\n const data = await this.request<{ cart: CartLike }>(\n \"POST\",\n `/store/carts/${encodeURIComponent(cartId)}/line-items`,\n {\n idempotent: true,\n cartRecovery: true,\n body: { variant_id: variantId, quantity },\n }\n )\n return data.cart\n }\n\n async getCart(cartId: string): Promise<CartLike> {\n const data = await this.request<{ cart: CartLike }>(\n \"GET\",\n `/store/carts/${encodeURIComponent(cartId)}`,\n { cartRecovery: true }\n )\n return data.cart\n }\n\n /** Remove a staged line item (bt unstage). Returns the updated cart (Medusa's `parent`). */\n async removeLineItem(cartId: string, itemId: string): Promise<CartLike> {\n const data = await this.request<{ parent?: CartLike; cart?: CartLike }>(\n \"DELETE\",\n `/store/carts/${encodeURIComponent(cartId)}/line-items/${encodeURIComponent(itemId)}`,\n { idempotent: true, cartRecovery: true }\n )\n return (data.parent ?? data.cart) as CartLike\n }\n\n /**\n * Ensure the cart has a shipping method before checkout (BUG 1 — the headless\n * /store/headless/checkout route guards on it, and neither client ever set one).\n *\n * - Idempotent: if the cart already carries a shipping method, no-op (no POST).\n * - Lists options via the STANDARD store route GET /store/shipping-options?cart_id=…\n * (NOT a /headless route — standard routes are always available, no kill-switch).\n * - Picks deterministically: exactly one → use it; several → the cheapest by amount.\n * - The SE launch option is region-scoped (price_type:\"flat\"), so listing by cart_id\n * returns it without a shipping address being set first (confirmed: seed-shipping.ts).\n *\n * TODO(04.5): buyer-choice / intl multiple shipping options — today we auto-pick the\n * cheapest; an explicit buyer chooser belongs to the Phase-6 international pass.\n */\n async ensureCartShippingMethod(cartId: string): Promise<CartLike> {\n // 1. read the authoritative cart; a stale/completed cart → RecoverableCartError (F-B2).\n const cart = await this.getCart(cartId)\n if (cart.shipping_methods && cart.shipping_methods.length > 0) {\n // idempotent: already satisfied.\n return cart\n }\n\n // 2. list options via the STANDARD store route (always available, no kill-switch).\n const data = await this.request<{ shipping_options: ShippingOptionLike[] }>(\n \"GET\",\n \"/store/shipping-options\",\n { query: { cart_id: cartId } }\n )\n const options = data.shipping_options ?? []\n if (options.length === 0) {\n // fail closed: never mint a checkout we know the backend will reject (BUG 1).\n throw new Error(\n \"no shipping option is available for this cart — cannot attach a shipping method\"\n )\n }\n\n // 3. pick deterministically: one → it; several → the cheapest by amount.\n const chosen =\n options.length === 1\n ? options[0]!\n : [...options].sort(\n (a, b) => (a.amount ?? Number.POSITIVE_INFINITY) - (b.amount ?? Number.POSITIVE_INFINITY)\n )[0]!\n\n // 4. attach it — a mutation, so it carries an Idempotency-Key + cart-recovery.\n const res = await this.request<{ cart: CartLike }>(\n \"POST\",\n `/store/carts/${encodeURIComponent(cartId)}/shipping-methods`,\n {\n idempotent: true,\n cartRecovery: true,\n body: { option_id: chosen.id },\n }\n )\n return res.cart\n }\n\n // ----- headless contract (04.5-01) -----------------------------------------\n\n // `channel` is REQUIRED (L11): a default silently mis-attributes analytics for a future caller that\n // forgets it. Both current callers (cli, mcp) pass it explicitly.\n async mintCheckout(cartId: string, email: string | undefined, channel: string): Promise<HeadlessCheckout> {\n return this.request<HeadlessCheckout>(\"POST\", \"/store/headless/checkout\", {\n idempotent: true,\n body: { cart_id: cartId, email, channel },\n })\n }\n\n async poll(sessionId: string): Promise<PollResult> {\n return this.request<PollResult>(\n \"GET\",\n `/store/headless/poll/${encodeURIComponent(sessionId)}`\n )\n }\n\n /** JWT-bound: returns the authenticated customer's own profile (G2). */\n async whoami(): Promise<WhoAmI> {\n // A guest (no/expired JWT) gets a 401/403 — that's the EXPECTED unauthenticated branch the\n // CLI turns into the friendly \"browsing as a guest\" line, NOT an error to dump (BUG 3).\n return this.request<WhoAmI>(\"GET\", \"/store/headless/whoami\", { expected4xx: [401, 403] })\n }\n\n // ----- magic-link auth (04.5-05) -------------------------------------------\n // Passwordless login — magic-link is the only grant (Medusa has no OAuth pairing-grant, G1).\n // request-link emails a single-use link; verify exchanges the link's token for a customer-scoped\n // JWT. The JWT is stored in the OS keychain by the CLI — never here, never in plaintext config (G7/G8).\n\n /** Start a magic-link login. Returns a UNIFORM ack regardless of whether the email exists (G12). */\n async requestMagicLink(email: string): Promise<RequestLinkResult> {\n return this.request<RequestLinkResult>(\"POST\", \"/store/headless/auth/request-link\", {\n idempotent: true,\n body: { email },\n })\n }\n\n /** Exchange a magic-link token for a customer-scoped JWT (the customer is materialized server-side). */\n async verifyMagicLink(token: string): Promise<VerifyLinkResult> {\n return this.request<VerifyLinkResult>(\"POST\", \"/store/headless/auth/verify\", {\n idempotent: true,\n body: { token },\n })\n }\n\n async listDrops(): Promise<DropRow[]> {\n const data = await this.request<{ drops: DropRow[]; count: number }>(\n \"GET\",\n \"/store/headless/drops\"\n )\n return data.drops ?? []\n }\n\n /**\n * File a developer bug report (d14 — `bt report` / MCP report_issue). Returns a correlation\n * reference. Additive: no money/cart path. The backend redacts authoritatively; callers SHOULD\n * pass an already-redacted context (the doctor snapshot). A mutation → carries an Idempotency-Key\n * so a retry can't email the report twice.\n */\n async report(message: string, context?: Record<string, unknown>): Promise<ReportResult> {\n return this.request<ReportResult>(\"POST\", \"/store/headless/report\", {\n idempotent: true,\n body: { message, ...(context !== undefined ? { context } : {}) },\n })\n }\n\n /**\n * The authenticated customer's order history (d13 — `bt log`). JWT-bound; the backend derives the\n * customer from the bearer token and returns MINIMAL fields (status/timeline only — never\n * email/address, F-H3). A guest (no/expired JWT) gets a 401 — the EXPECTED unauthenticated branch\n * we turn into the honest empty history (`[]`), NOT an error to dump (BUG 3).\n */\n async listOrders(): Promise<OrderSummary[]> {\n try {\n const data = await this.request<{ orders: OrderSummary[] }>(\"GET\", \"/store/headless/orders\", {\n expected4xx: [401, 403],\n })\n return data.orders ?? []\n } catch (e) {\n if (e instanceof StoreApiError && (e.status === 401 || e.status === 403)) {\n return []\n }\n throw e\n }\n }\n\n /**\n * Guest order lookup (AUD-8) — POST /store/order-lookup with the order id (display id OR internal\n * id) + the EXACT email in the BODY (never the query string, so the email is not access-logged).\n * The backend returns 404 on any mismatch (no enumeration) → the client surfaces null. Rate-limited\n * server-side; returns minimal status only, never PII (F-H1 / F-H3).\n */\n async getOrder(idOrDisplayId: string | number, email: string): Promise<unknown | null> {\n try {\n return await this.request<unknown>(\"POST\", \"/store/order-lookup\", {\n body: { id: String(idOrDisplayId), email },\n })\n } catch (e) {\n if (e instanceof StoreApiError && e.status === 404) {\n return null\n }\n throw e\n }\n }\n}\n","// Baked-in PUBLIC defaults for the Binary & Thread production channel.\n//\n// These let `npx @binaryandthread/cli` and the MCP server reach the live store with ZERO\n// configuration — the point of the soft-launch tarball (P4.5-7b) and decision #79 (d2).\n// store-client is bundled into both the CLI and the MCP server (tsup `noExternal`), so this\n// is the single source of truth for \"which store\" both clients talk to by default.\n//\n// SAFETY (CLAUDE.md rule 1 / SECURITY T6): the publishable key is PUBLIC BY DESIGN. It is the\n// SAME key embedded in the public storefront's client bundle (binaryandthread.com) and carries\n// no privilege beyond read + cart over the Store API — every dangerous operation lives behind\n// the backend. It is therefore NOT a secret; rule 1 governs secrets, and this is deliberately\n// published. Any env var (BT_API_URL / BT_STORE_URL / BT_PUBLISHABLE_KEY / MEDUSA_*) OVERRIDES\n// these for local dev or staging.\n\n/** The Binary & Thread production Medusa Store API base URL. */\nexport const BT_DEFAULT_STORE_URL = \"https://backend-production-511f5.up.railway.app\"\n\n// The PUBLIC Medusa publishable key (read + cart only; safe to ship — T6). Identical to the key\n// served in the public storefront bundle. NOT a secret (CLAUDE.md rule 1); the inline allow keeps\n// the gitleaks pre-commit hook from flagging a deliberately-published value.\nexport const BT_DEFAULT_PUBLISHABLE_KEY = \"pk_0d7d706860568a0414dff0436aadb9713a2609de3a1b153ef2a6f9694d23f907\" // gitleaks:allow\n","// Image-host allowlist — the canonical source for the WHOLE channel (CLAUDE.md rule 6 / SECURITY G9).\n//\n// We fetch product images ONLY from our own Vercel Blob store. Both the bt CLI (its terminal\n// image ladder) and the Wave-4 MCP (its server-side ImageContent fetch) gate every outbound\n// image fetch on this allowlist. It lives HERE in the shared client — not in the CLI — so the\n// MCP does not take a runtime dependency on the CLI package; the CLI re-exports it from here.\n//\n// HISTORY: a bare `endsWith(allowed)` suffix match was a CRITICAL allowlist-bypass bug fixed in\n// Wave 2 (it would admit `evilblob.vercel-storage.com`). The dot-delimited-subdomain check below\n// is load-bearing security — `host === allowed || host.endsWith(\".\" + allowed)`, never a bare\n// suffix. Do NOT \"simplify\" it.\n\n/**\n * Allowlisted image hosts. We fetch product images ONLY from our own Vercel Blob store\n * (public). NEVER a public temp host (uguu/0x0/catbox/transfer.sh) and NEVER an arbitrary\n * URL — that would be SSRF + a brand-IP leak (CLAUDE.md rule 6).\n */\nexport const BLOB_HOST_ALLOWLIST: readonly string[] = [\n \"blob.vercel-storage.com\", // matches the exact host + *.blob.vercel-storage.com (dot-delimited subdomains only)\n]\n\n/**\n * True if the URL's host is on the Blob allowlist.\n *\n * Exact host OR a true dot-delimited subdomain. NOT a bare suffix: a bare\n * `endsWith(allowed)` would admit `evilblob.vercel-storage.com` (allowlist bypass →\n * SSRF / brand-IP leak, CLAUDE.md rule 6). The dot guard is load-bearing.\n */\nexport function isAllowedImageHost(url: string): boolean {\n let host: string\n try {\n host = new URL(url).hostname.toLowerCase()\n } catch {\n return false\n }\n return BLOB_HOST_ALLOWLIST.some(\n (allowed) => host === allowed || host.endsWith(\".\" + allowed)\n )\n}\n","// The Fit axis (CAT-31 / D10). One product per design with options Fit × Colour × Size; only printable\n// combinations exist as variants. Written once here so the CLI and the MCP resolve fit the SAME way\n// (memory: never parse the variant title positionally; a wrong garment is a money-path defect).\n//\n// FAIL CLOSED: a product with a fit axis and no fit chosen is an error that lists the fits with their\n// prices. We never pick a fit for the buyer.\nimport type { ProductLike, VariantLike } from \"./types\"\nimport { variantColour, sizeOfVariant, colourKey, productColours } from \"./colour\"\n\nexport type Fit = \"regular\" | \"oversize\"\nconst FITS: readonly Fit[] = [\"regular\", \"oversize\"]\n\nexport function parseFit(v: unknown): Fit | null {\n const s = typeof v === \"string\" ? v.trim().toLowerCase() : \"\"\n return s === \"regular\" || s === \"oversize\" ? s : null\n}\n\nexport function fitLabel(f: Fit): \"Regular\" | \"Oversize\" {\n return f === \"oversize\" ? \"Oversize\" : \"Regular\"\n}\n\n/** The variant's Fit option value, or null when the product has no fit axis (legacy Size × Colour). */\nexport function variantFit(v: VariantLike): Fit | null {\n const opt = (v.options ?? []).find((o) => (o.option?.title ?? \"\").toLowerCase() === \"fit\")\n return opt ? parseFit(opt.value) : null\n}\n\nexport function hasFitAxis(p: ProductLike): boolean {\n return (p.variants ?? []).some((v) => variantFit(v) !== null)\n}\n\n/** The fits this product is sold in, regular first; [] for a legacy product. Derived from VARIANTS. */\nexport function productFits(p: ProductLike): Fit[] {\n const seen = new Set<Fit>()\n for (const v of p.variants ?? []) {\n const f = variantFit(v)\n if (f) seen.add(f)\n }\n return FITS.filter((f) => seen.has(f))\n}\n\n// ----- region rule (mirrors backend geography.ts + storefront availability.ts; hand-kept in sync) -----\n\nexport type Blank = \"gildan-5000\" | \"sols-03996\" | \"shaka-shmhss\"\n\n/** Colours we sell per blank, lower-cased. Khaki exists ONLY on Shaka (US oversize). */\nexport const BLANK_COLOURS: Record<Blank, readonly string[]> = {\n \"gildan-5000\": [\"black\", \"white\"],\n \"sols-03996\": [\"black\", \"white\"],\n \"shaka-shmhss\": [\"black\", \"white\", \"khaki\"],\n}\n\n/**\n * ISO-3166-1 alpha-2 (lower-case) → the Gelato STOCK REGION that country produces in. Mirrors the\n * backend `geography.ts` STOCK_REGION_BY_COUNTRY, which is canonical; kept in sync by hand and\n * asserted by `docs/verify-blank-rules.mjs`, because these files cannot import each other.\n *\n * SAR-435: this replaced \"is the country the US? then Shaka, otherwise SOL'S\" — a shortcut that\n * returned a printable blank for every country on earth. The backend resolver fails closed at\n * FULFILMENT, which is after the money has moved, so the buyer-facing answer has to fail closed too.\n * A country that is missing here is a country we cannot print for: say no before the card, not after.\n */\nexport const STOCK_REGION_BY_COUNTRY: Record<string, string> = {\n us: \"US-CA\",\n se: \"EU\",\n at: \"EU\", be: \"EU\", bg: \"EU\", hr: \"EU\", cy: \"EU\", cz: \"EU\", dk: \"EU\", ee: \"EU\",\n fi: \"EU\", fr: \"EU\", de: \"EU\", gr: \"EU\", hu: \"EU\", ie: \"EU\", it: \"EU\", lv: \"EU\",\n lt: \"EU\", lu: \"EU\", mt: \"EU\", nl: \"EU\", pl: \"EU\", pt: \"EU\", ro: \"EU\", sk: \"EU\",\n si: \"EU\", es: \"EU\",\n}\n\n/**\n * fit × stock region → the blank that prints it. `\"*\"` = produced in every region. A (fit, region)\n * with no entry is not produced there: OC (AU/NZ) and UK have NEITHER oversize blank, which is the\n * gap this exists to express. Mirrors the backend GARMENT_BY_REGION.\n */\nconst BLANK_BY_REGION: Record<Fit, Record<string, Blank>> = {\n regular: { \"*\": \"gildan-5000\" },\n oversize: { EU: \"sols-03996\", \"US-CA\": \"shaka-shmhss\" },\n}\n\n/**\n * The blank that prints (fit, ship-to country) — or **null when nothing we can print ships there**.\n * Null is the honest answer, and every caller treats it as \"not buyable\" rather than guessing.\n */\nexport function blankFor(fit: Fit, countryCode: string | null | undefined): Blank | null {\n const region = STOCK_REGION_BY_COUNTRY[(countryCode ?? \"\").trim().toLowerCase()]\n if (!region) return null\n const map = BLANK_BY_REGION[fit]\n return map[\"*\"] ?? map[region] ?? null\n}\n\n/**\n * The sizes each blank can PRINT, lower-cased, in OUR canonical option values (not Gelato's tokens —\n * SOL'S `xxl` is our \"2XL\"). Read off the LIVE Gelato catalog 2026-09-04 (`products:search`, the\n * `_gsi_<size>_` token per blank). Like BLANK_COLOURS this is the BLANK's capability, not our range:\n * a variant must ALSO exist, so listing 4XL/5XL here is harmless.\n *\n * CAT-37: the two oversize blanks differ — SOL'S (EU) prints XS and stops at 3XL, Shaka (US) starts at\n * S and runs to 5XL. Gating colour but not size let a US buyer stage and PAY for an Oversize XS that\n * Gelato cannot print; the order then fails closed at fulfillment, i.e. after the money moved.\n * Mirrored in the backend GARMENT_BLANKS (`catalog-variants.ts`, canonical) + storefront availability.ts.\n */\nexport const BLANK_SIZES: Record<Blank, readonly string[]> = {\n \"gildan-5000\": [\"s\", \"m\", \"l\", \"xl\", \"2xl\", \"3xl\", \"4xl\", \"5xl\"],\n \"shaka-shmhss\": [\"s\", \"m\", \"l\", \"xl\", \"2xl\", \"3xl\", \"4xl\", \"5xl\"],\n \"sols-03996\": [\"xs\", \"s\", \"m\", \"l\", \"xl\", \"2xl\", \"3xl\"],\n}\n\n// ----- the Printful blank (Phase 7 D1, TC1) -----\n//\n// A product is fulfilled by ONE supplier, named on the product (`metadata.bt_supplier`, written by the\n// backend when it clones a design onto Printful). A Printful product must be judged by the Printful\n// blank: judged by the Gildan row above it would offer 3XL-5XL, sizes we do not sell it in.\n// Mirrors the backend `printful-blanks.ts` (PRINTFUL_BLANKS colours + PRINTFUL_SALE_SIZES, canonical);\n// hand-kept in sync and asserted by `docs/verify-blank-rules.mjs`. The sizes are what we SELL (S-2XL,\n// Gelato parity), not the blank's capability (Black XS-3XL, White S-3XL): selling S-2XL also keeps the\n// colour-dependent White-has-no-XS rule out of this table.\n\nexport type Supplier = \"gelato\" | \"printful\"\n\n/**\n * The supplier that fulfils this product, matched EXACTLY: absent = the Gelato path (every product that\n * predates Phase 7 carries no `bt_supplier`), \"gelato\" / \"printful\" = that supplier, anything else = null.\n * Null is refused by every gate (`blankRule`): an unrecognised value judged by the Gelato blank is the\n * wrong-blank bug this exists to prevent.\n */\nexport function supplierOf(p: ProductLike): Supplier | null {\n const v = (p.metadata ?? {})[\"bt_supplier\"]\n if (v === undefined || v === null) return \"gelato\"\n return v === \"gelato\" || v === \"printful\" ? v : null\n}\n\nexport type PrintfulBlank = \"ascolour-5082\" | \"bellacanvas-4711\" | \"yupoong-6606\"\n\nexport const PRINTFUL_BLANK_COLOURS: Record<PrintfulBlank, readonly string[]> = {\n \"ascolour-5082\": [\"black\", \"white\", \"coal\", \"eucalyptus\", \"bone\", \"grey\", \"khaki\", \"indigo\"], // TB124, TB126\n \"bellacanvas-4711\": [\"asphalt\", \"dust\", \"navy\", \"maroon\", \"forest\", \"jet\"], // TB128: the sweatshirt; TB129: + jet (its Black)\n \"yupoong-6606\": [\"blackmesh\"], // TB268: the cap\n}\n\nexport const PRINTFUL_BLANK_SIZES: Record<PrintfulBlank, readonly string[]> = {\n \"ascolour-5082\": [\"s\", \"m\", \"l\", \"xl\", \"2xl\"],\n \"bellacanvas-4711\": [\"s\", \"m\", \"l\", \"xl\", \"2xl\"],\n \"yupoong-6606\": [\"os\"], // TB268: the cap is one size\n}\n\n/**\n * TB128: the garment a Printful product is made on, from `metadata.bt_blank`: absent (or null) = the 5082 (every\n * product before TB128), an exact known key = that garment, anything else = null (refused by every gate). Mirrors the\n * backend printful-blanks.ts printfulBlankOfProduct.\n */\nexport function printfulBlankOf(p: ProductLike): PrintfulBlank | null {\n const v = (p.metadata ?? {})[\"bt_blank\"]\n if (v === undefined || v === null) return \"ascolour-5082\"\n return typeof v === \"string\" && Object.prototype.hasOwnProperty.call(PRINTFUL_BLANK_COLOURS, v) ? (v as PrintfulBlank) : null\n}\n\n/**\n * The countries a Printful product ships to: every country of every backend market (`markets.ts`\n * MARKETS[*].countries, canonical; each market carries a Printful selling region). Its own list, not\n * STOCK_REGION_BY_COUNTRY (that is Gelato's geography); asserted equal to markets.ts by\n * `docs/verify-blank-rules.mjs`.\n */\nexport const PRINTFUL_SERVED_COUNTRIES: readonly string[] = [\n \"se\",\n \"us\",\n \"at\", \"be\", \"bg\", \"hr\", \"cy\", \"cz\", \"dk\", \"ee\", \"fi\", \"fr\", \"de\", \"gr\", \"hu\",\n \"ie\", \"it\", \"lv\", \"lt\", \"lu\", \"mt\", \"nl\", \"pl\", \"pt\", \"ro\", \"sk\", \"si\", \"es\",\n]\n\n/**\n * The Printful blank for (fit, ship-to country), or null (refuse). Mirrors the backend\n * `printfulBlankKeyFor`: a country no market serves gets null, regular -> the 5082, oversize -> null.\n */\nexport function printfulBlankFor(\n fit: Fit,\n countryCode: string | null | undefined,\n /** TB128: the product's garment (printfulBlankOf); null = unknown = refused. */\n productBlank: PrintfulBlank | null = \"ascolour-5082\"\n): PrintfulBlank | null {\n if (!PRINTFUL_SERVED_COUNTRIES.includes((countryCode ?? \"\").trim().toLowerCase())) return null\n if (productBlank === null) return null\n return fit === \"regular\" ? productBlank : null\n}\n\n/** Canonical size key. \"XXL\" and \"2XL\" name the same size; BLANK_SIZES is keyed on the \"2xl\" form. */\nfunction sizeKey(size: string): string {\n const s = size.trim().toLowerCase()\n return s === \"xxl\" ? \"2xl\" : s === \"xxxl\" ? \"3xl\" : s\n}\n\n/**\n * The colour + size rule of the blank that prints (fit, country) for this SUPPLIER, or null when nothing\n * we can print ships there. The one place the supplier decides which table judges the buyer.\n */\nfunction blankRule(\n fit: Fit,\n countryCode: string | null | undefined,\n supplier: Supplier | null,\n productBlank: PrintfulBlank | null\n): { colours: readonly string[]; sizes: readonly string[] } | null {\n if (supplier === null) return null // an unrecognised supplier: no table can judge it, refuse\n if (supplier === \"printful\") {\n const pb = printfulBlankFor(fit, countryCode, productBlank)\n return pb ? { colours: PRINTFUL_BLANK_COLOURS[pb], sizes: PRINTFUL_BLANK_SIZES[pb] } : null\n }\n const blank = blankFor(fit, countryCode)\n return blank ? { colours: BLANK_COLOURS[blank], sizes: BLANK_SIZES[blank] } : null\n}\n\nexport function colourBuyable(\n fit: Fit,\n colour: string,\n countryCode: string | null | undefined,\n supplier: Supplier | null = \"gelato\",\n /** TB128: a Printful product's garment (printfulBlankOf). Omitted = the 5082 (its colours are disjoint from the 4711's). */\n printfulBlank: PrintfulBlank | null = \"ascolour-5082\"\n): boolean {\n const rule = blankRule(fit, countryCode, supplier, printfulBlank)\n return !!rule && rule.colours.includes(colour.trim().toLowerCase())\n}\n\n/** Can the blank that prints this fit HERE make this size? (CAT-37 — the size half of colourBuyable.) */\nexport function sizeBuyable(\n fit: Fit,\n size: string,\n countryCode: string | null | undefined,\n supplier: Supplier | null = \"gelato\",\n printfulBlank: PrintfulBlank | null = \"ascolour-5082\"\n): boolean {\n const rule = blankRule(fit, countryCode, supplier, printfulBlank)\n return !!rule && rule.sizes.includes(sizeKey(size))\n}\n\n/**\n * The fit of a LEGACY product — one with no Fit option, which is every product in the live catalog\n * until CAT-31 Phase E migrates it. Read from metadata.fit → title → handle, the same pre-merge rule\n * the storefront uses (`detectLegacyFit` / `isOversizeText`), so both channels agree.\n */\nexport function legacyFit(p: ProductLike): Fit {\n const md = (p.metadata ?? {}) as Record<string, unknown>\n const hay = [typeof md.fit === \"string\" ? md.fit : \"\", p.title ?? \"\", p.handle ?? \"\"].join(\" \")\n return /oversiz|drop.?shoulder/i.test(hay) ? \"oversize\" : \"regular\"\n}\n\n/**\n * The fit the REGION gates should judge by — which is not always the fit axis. The gates ask \"what\n * blank prints this, where you ship?\", and the blank follows the fit, so on a legacy product (no Fit\n * option) they must use its implied fit. Without this every `colourBuyable`/`sizeBuyable` check behind\n * a `fit &&` guard silently no-ops on the ENTIRE live catalog — the CAT-37 review found exactly that.\n * Returns null only when the product HAS a fit axis and no fit has been chosen: then there is no single\n * blank to judge by, and the caller is failing closed on the missing fit anyway.\n */\nexport function gateFit(p: ProductLike, fit: Fit | null): Fit | null {\n if (fit) return fit\n return hasFitAxis(p) ? null : legacyFit(p)\n}\n\n/** The variants of one fit; `null` = all variants (legacy product). */\nexport function variantsOfFit(p: ProductLike, fit: Fit | null): VariantLike[] {\n const all = p.variants ?? []\n return fit === null ? all : all.filter((v) => variantFit(v) === fit)\n}\n\n/**\n * Distinct colours of a fit (lower-cased, first-seen order). When `countryCode` is given (even null),\n * only the colours BUYABLE there are returned; `undefined` = no region filter.\n */\nexport function coloursFor(p: ProductLike, fit: Fit | null, countryCode?: string | null): string[] {\n const gate = gateFit(p, fit) // legacy product: its implied fit, so the gate is not skipped\n const supplier = supplierOf(p)\n const pb = printfulBlankOf(p)\n const out: string[] = []\n for (const v of variantsOfFit(p, fit)) {\n const c = variantColour(v)\n if (!c || out.includes(c)) continue\n if (gate && countryCode !== undefined && !colourBuyable(gate, c, countryCode, supplier, pb)) continue\n out.push(c)\n }\n return out\n}\n\n/**\n * The sizes of a (fit, colour), in variant order (callers sort canonically for display). When\n * `countryCode` is given (even null), only the sizes the ship-to blank can PRINT are returned;\n * `undefined` = no region filter — same convention as `coloursFor`.\n */\nexport function sizesFor(p: ProductLike, fit: Fit | null, colour: string, countryCode?: string | null): string[] {\n const gate = gateFit(p, fit) // legacy product: its implied fit, so the gate is not skipped\n const supplier = supplierOf(p)\n const pb = printfulBlankOf(p)\n const target = colour.trim().toLowerCase()\n const out: string[] = []\n for (const v of variantsOfFit(p, fit)) {\n if (variantColour(v) !== target) continue\n const s = sizeOfVariant(v)\n if (!s || out.includes(s)) continue\n if (gate && countryCode !== undefined && !sizeBuyable(gate, s, countryCode, supplier, pb)) continue\n out.push(s)\n }\n return out\n}\n\nexport type PriceTriple = { amount: number | null; currency: string | null; taxInclusive: boolean | null }\n\nfunction cheapest(vs: VariantLike[]): PriceTriple {\n const priced = vs\n .filter((v) => typeof v.calculated_price?.calculated_amount === \"number\")\n .sort(\n (a, b) => (a.calculated_price!.calculated_amount as number) - (b.calculated_price!.calculated_amount as number)\n )\n const v = priced[0]\n return {\n amount: v?.calculated_price?.calculated_amount ?? null,\n currency: v?.calculated_price?.currency_code ?? null,\n taxInclusive: v?.calculated_price?.is_calculated_price_tax_inclusive ?? null,\n }\n}\n\n/** The cheapest price across the product; `from` is true when the fits are priced differently. */\nexport function priceFrom(p: ProductLike): PriceTriple & { from: boolean } {\n const fits = productFits(p)\n const base = cheapest(p.variants ?? [])\n const amounts = new Set(fits.map((f) => cheapest(variantsOfFit(p, f)).amount))\n return { ...base, from: fits.length > 1 && amounts.size > 1 }\n}\n\nexport function priceFor(p: ProductLike, fit: Fit): PriceTriple {\n return cheapest(variantsOfFit(p, fit))\n}\n\nexport type ResolveVariantResult =\n | { ok: true; variant: VariantLike; fit: Fit | null; colour: string | null; size: string | null }\n | { ok: false; reason: \"ambiguous_fit\"; fits: Array<{ fit: Fit } & PriceTriple> }\n | { ok: false; reason: \"fit_not_offered\"; requested: string; fits: Fit[] }\n | { ok: false; reason: \"fit_unavailable\"; fit: Fit; colour: string | null }\n | { ok: false; reason: \"size_unavailable\"; fit: Fit; size: string; sizes: string[] }\n | {\n ok: false\n reason: \"not_found\"\n fit: Fit | null\n colour: string | null\n size: string | null\n colours: string[]\n sizes: string[]\n }\n\n/**\n * Resolve (fit, colour, size) → a variant, or a typed reason the caller turns into ONE clear message.\n * - a fit product with no (or an unknown) fit → `ambiguous_fit` with the fits + prices (fail closed)\n * - a colour the blank for this country does not offer → `fit_unavailable`\n * - a SIZE the blank for this country cannot print → `size_unavailable` (CAT-37; the two oversize\n * blanks have different size ranges, so this is region-dependent, not product-dependent)\n * - otherwise `not_found`, with the colours/sizes that DO exist\n * The colour defaults to the first colour buyable in the fit (black first by variant order). A legacy\n * product (no fit axis) ignores `fit` and behaves exactly as before.\n */\nexport function resolveVariant(\n p: ProductLike,\n opts: { fit?: string | null; size?: string | null; colour?: string | null; countryCode?: string | null }\n): ResolveVariantResult {\n const fits = productFits(p)\n const requested = parseFit(opts.fit)\n const askedFor = typeof opts.fit === \"string\" ? opts.fit.trim() : \"\"\n let fit: Fit | null = null\n if (fits.length > 0) {\n if (requested && fits.includes(requested)) fit = requested\n // An EXPLICIT fit the design isn't sold in is never substituted — on a single-fit design that would\n // silently stage/charge the other garment (review 2026-09-03). Only an ABSENT fit may be implied.\n else if (askedFor) return { ok: false, reason: \"fit_not_offered\", requested: askedFor, fits }\n else if (fits.length === 1) fit = fits[0]!\n else return { ok: false, reason: \"ambiguous_fit\", fits: fits.map((f) => ({ fit: f, ...priceFor(p, f) })) }\n }\n const pool = variantsOfFit(p, fit)\n // The gates below judge by the BLANK, so on a legacy product (no Fit option — the whole live catalog\n // today) they use its implied fit. `fit` stays the fit-axis answer, which is what we report back.\n const gate = gateFit(p, fit)\n const supplier = supplierOf(p)\n const pb = printfulBlankOf(p)\n const colours = coloursFor(p, fit, opts.countryCode)\n // TB129: key first, a label only among the product's own colours (\"Black\" on a jet product = jet; on a 5082, black).\n const colour = colourKey(opts.colour ?? \"\", productColours(p)) || (colours[0] ?? null)\n if (gate && colour && opts.countryCode !== undefined && !colourBuyable(gate, colour, opts.countryCode, supplier, pb)) {\n return { ok: false, reason: \"fit_unavailable\", fit: gate, colour }\n }\n const size = (opts.size ?? \"\").trim() || null\n const sizeLc = size?.toLowerCase()\n const buyableSizes = colour ? sizesFor(p, fit, colour, opts.countryCode) : []\n // CAT-37 — an EXPLICIT size the ship-to blank cannot print is its own answer, not \"no such variant\":\n // the variant usually DOES exist (it is printable elsewhere), so the buyer needs the region reason.\n // Only when the size is real on this product though: a typo is \"no such variant\", not a region rule.\n const sizeExists =\n !!size && pool.some((x) => (sizeOfVariant(x) ?? x.title ?? \"\").toLowerCase() === size.toLowerCase())\n if (gate && size && sizeExists && opts.countryCode !== undefined && !sizeBuyable(gate, size, opts.countryCode, supplier, pb)) {\n return { ok: false, reason: \"size_unavailable\", fit: gate, size, sizes: buyableSizes }\n }\n // ...and with NO size given we must not fall onto an unprintable one (XS is FIRST in variant order\n // on an oversize design, so the US default would have been the one size Shaka cannot make).\n const sellable =\n gate && opts.countryCode !== undefined\n ? pool.filter((x) => {\n const s = sizeOfVariant(x)\n return !s || sizeBuyable(gate, s, opts.countryCode, supplier, pb)\n })\n : pool\n const v = sellable.find((x) => {\n const colourOk = !colour || variantColour(x) === colour\n const sizeOk =\n !sizeLc || (sizeOfVariant(x) ?? \"\").toLowerCase() === sizeLc || (x.title ?? \"\").toLowerCase() === sizeLc\n return colourOk && sizeOk\n })\n if (!v) {\n return { ok: false, reason: \"not_found\", fit, colour, size, colours, sizes: buyableSizes }\n }\n return { ok: true, variant: v, fit, colour: variantColour(v), size: sizeOfVariant(v) }\n}\n","// Colour resolution — DERIVED from the variant matrix, never hardcoded (CAT-3 forward-compat).\n//\n// The dual-colour catalog (memory: dual-colour-catalog) gives each design ONE product with a\n// Colour × Size variant matrix. A variant's colour comes from, in order of trust:\n// 1. the variant option titled \"Colour\" (Model A: options {Size, Colour}, value title-cased)\n// 2. the variant title, format \"<size> / <Colour>\" (e.g. \"S / White\", \"M / Black\")\n// We NEVER hardcode white/black — future colours (CAT-3) fall out of the data for free.\n//\n// This lives in @binaryandthread/store-client (the canonical types home) so the CLI and the MCP\n// resolve colour the SAME way — written once, tested once.\n\nimport type { ProductLike, VariantLike, ImageLike } from \"./types\"\nimport { productFits, parseFit, variantFit, type Fit } from \"./fit\"\n\n// Title-token helpers for the LEGACY fallback path (a payload without expanded options). Tokens, never\n// positions: with a Fit option the title is \"Oversize / White / M\", so \"last segment = colour\" is wrong.\nconst SIZE_TOKEN = /^(xxs|xs|s|m|l|xl|xxl|2xl|3xl|xxxl|4xl|5xl)$/i\nconst FIT_TOKEN = /^(regular|oversize)$/i\nfunction titleSegments(title: string | null | undefined): string[] {\n return (title ?? \"\")\n .split(\"/\")\n .map((s) => s.trim())\n .filter(Boolean)\n}\n\n/**\n * The CUSTOMER-FACING name of a colour (BNT-256). Display only: the stored Colour option value\n * (\"Black\"/\"White\") stays the key for image matching, availability tables and ?color= URLs.\n * black → \"Faded Black\", white → \"Faded White\", anything else is title-cased unchanged.\n * (Copies, because the storefront and backend do not import this package:\n * apps/storefront/src/lib/util/colour-label.ts and apps/backend/src/lib/colour-label.ts. Keep in sync.)\n */\n// TB124: + coal, eucalyptus, bone; TB126: + grey, khaki, indigo (the 5082 colours wired for sale). TB129: + jet, the 4711's\n// Black (its own key, so the 5082's black keeps \"Faded Black\"). Keep the three copies in sync.\nconst COLOUR_LABELS: Record<string, string> = {\n black: \"Faded Black\",\n white: \"Faded White\",\n coal: \"Faded Coal\",\n eucalyptus: \"Faded Eucalyptus\",\n bone: \"Faded Bone\",\n grey: \"Faded Grey\",\n khaki: \"Faded Khaki\",\n indigo: \"Faded Indigo\",\n jet: \"Black\",\n // TB268: the cap (Printful 252 \"Black/ White\"): black front, white mesh back.\n blackmesh: \"Black with white mesh\",\n}\nexport function colourLabel(colour: string): string {\n const c = colour.trim()\n if (!c) return c\n return COLOUR_LABELS[c.toLowerCase()] ?? c.charAt(0).toUpperCase() + c.slice(1).toLowerCase()\n}\n\n/**\n * The stored key for a typed colour, lowercased. TB129 (plan-check binding 1): an exact stored KEY wins first, then a\n * display LABEL (\"Faded Black\" → \"black\", \"Black\" → \"jet\"). With `available` (the PRODUCT's own colour keys, e.g.\n * productColours(p)) both steps look only among them, so `black` on a 5082 product stays `black` while `Black` on a\n * product sold in jet (the 4711's Black, displayed \"Black\") is `jet`. Without it, a key wins over any label (`black` is\n * never read as jet). Anything unmatched comes back lowercased, for the caller's own \"no such colour\".\n */\nexport function colourKey(colour: string, available?: readonly string[] | null): string {\n const c = colour.trim().toLowerCase()\n if (available) {\n const keys = available.map((k) => k.trim().toLowerCase())\n if (keys.includes(c)) return c\n return keys.find((k) => colourLabel(k).toLowerCase() === c) ?? c\n }\n if (Object.prototype.hasOwnProperty.call(COLOUR_LABELS, c)) return c\n const key = Object.entries(COLOUR_LABELS).find(([, label]) => label.toLowerCase() === c)?.[0]\n return key ?? c\n}\n\n/** A variant title (\"Regular / White / M\") with its colour segment relabelled for display. */\nexport function labelVariantTitle(title: string | null | undefined): string {\n return (title ?? \"\")\n .split(\"/\")\n .map((seg) => {\n const t = seg.trim()\n const k = t.toLowerCase()\n if (k === \"os\") return \"One size\" // TB268: the cap's stored size, displayed\n return Object.prototype.hasOwnProperty.call(COLOUR_LABELS, k) ? colourLabel(t) : t\n })\n .join(\" / \")\n}\n\n/** Title-case a colour token for display (\"white\" → \"Faded White\"); see colourLabel. */\nexport function displayColour(colour: string): string {\n return colourLabel(colour)\n}\n\n/**\n * The colour of a single variant, lowercased — or null if it carries no colour axis (a\n * legacy Size-only product). Reads the \"Colour\" option first, then parses the \"<size> / <Colour>\"\n * title. NEVER assumes a fixed palette.\n */\nexport function variantColour(v: VariantLike): string | null {\n // 1) the explicit Colour option (the authoritative source — memory: never parse the title).\n const opt = (v.options ?? []).find(\n (o) => (o.option?.title ?? \"\").toLowerCase() === \"colour\" || (o.option?.title ?? \"\").toLowerCase() === \"color\"\n )\n if (opt && typeof opt.value === \"string\" && opt.value.trim()) {\n return opt.value.trim().toLowerCase()\n }\n // 2) fall back to the title: the segment that is neither a size nor a fit token (≥2 segments only).\n const segs = titleSegments(v.title)\n if (segs.length >= 2) {\n const seg = segs.find((s) => !SIZE_TOKEN.test(s) && !FIT_TOKEN.test(s))\n if (seg) return seg.toLowerCase()\n }\n return null\n}\n\n/**\n * The DISTINCT colours offered by a product, in first-seen order, lowercased. Empty for a\n * legacy Size-only product (no Colour axis) — callers treat that as \"no colour to show\".\n */\nexport function productColours(p: ProductLike): string[] {\n const seen: string[] = []\n for (const v of p.variants ?? []) {\n const c = variantColour(v)\n if (c && !seen.includes(c)) {\n seen.push(c)\n }\n }\n return seen\n}\n\n/** The distinct sizes offered FOR a given colour (lowercased colour match), preserving order. */\nexport function sizesForColour(p: ProductLike, colour: string): string[] {\n const target = colour.trim().toLowerCase()\n const sizes: string[] = []\n for (const v of p.variants ?? []) {\n if (variantColour(v) !== target) continue\n const size = sizeOfVariant(v)\n if (size && !sizes.includes(size)) {\n sizes.push(size)\n }\n }\n return sizes\n}\n\n/** The size label of a variant — the Size option, else the title's leading \"<size>\" segment. */\nexport function sizeOfVariant(v: VariantLike): string | null {\n const opt = (v.options ?? []).find((o) => (o.option?.title ?? \"\").toLowerCase() === \"size\")\n if (opt && typeof opt.value === \"string\" && opt.value.trim()) {\n return opt.value.trim()\n }\n const segs = titleSegments(v.title)\n const token = segs.find((s) => SIZE_TOKEN.test(s))\n if (token) return token\n // No colour axis → the whole title IS the size (legacy Size-only product); a multi-segment title with\n // no recognisable size token yields its first segment (old behaviour).\n return segs.length === 1 ? segs[0]! : segs[0] ?? null\n}\n\n/**\n * Resolve a variant by size AND colour (the real buyability gap — bt stage / MCP add_to_cart).\n * - size + colour given → exact match on both (case-insensitive).\n * - size only → first variant of that size.\n * - colour only → first variant of that colour.\n * - neither → first variant.\n * Returns undefined when the requested size+colour combination does not exist (the caller MUST\n * surface a clear error — never silently substitute another colour the buyer didn't ask for).\n */\nexport function findVariant(\n p: ProductLike,\n opts: { fit?: string; size?: string; colour?: string } = {}\n): VariantLike | undefined {\n // Fit axis (CAT-31): a product sold in several fits needs the fit named; none → undefined (fail\n // closed, the caller reports the fits via resolveVariant). A single-fit product implies its fit.\n const fits = productFits(p)\n let fit: Fit | null = null\n if (fits.length > 0) {\n const wanted = parseFit(opts.fit)\n const askedFor = typeof opts.fit === \"string\" ? opts.fit.trim() : \"\"\n if (wanted && fits.includes(wanted)) fit = wanted\n // An explicit fit the design isn't sold in never falls back to the other garment (see resolveVariant).\n else if (askedFor) return undefined\n else if (fits.length === 1) fit = fits[0]!\n else return undefined\n }\n const variants = (p.variants ?? []).filter((v) => fit === null || variantFit(v) === fit)\n const size = opts.size?.trim().toLowerCase()\n const colour = opts.colour ? colourKey(opts.colour, productColours(p)) : undefined\n\n const sizeMatches = (v: VariantLike): boolean => {\n if (!size) return true\n return (sizeOfVariant(v) ?? \"\").toLowerCase() === size || (v.title ?? \"\").toLowerCase() === size\n }\n const colourMatches = (v: VariantLike): boolean => {\n if (!colour) return true\n return variantColour(v) === colour\n }\n\n if (size && colour) {\n // both specified → require BOTH; no fallback to a different colour (a wrong colour is worse\n // than a clear \"that combo isn't available\").\n return variants.find((v) => sizeMatches(v) && colourMatches(v))\n }\n if (colour && !size) {\n return variants.find((v) => colourMatches(v))\n }\n if (size && !colour) {\n // size specified → require it; NO fallback to variants[0] (L10) — a silently-wrong size is worse\n // than undefined, which lets the caller (stage.ts) surface a clear \"no matching variant\" error.\n return variants.find((v) => sizeMatches(v))\n }\n return variants[0]\n}\n\n// The gallery scene we surface in the terminal: the flat-lay \"plp\" shot (the tee laid flat on the\n// photographic/concrete plate) — the SAME scene as the storefront thumbnail. Pinning to one scene is\n// what makes every tee read CONSISTENTLY (never a mix of on-model here / hero there); the product\n// image order alone is unreliable (D9). Filenames are `<slug>-<scene>-<colour>.webp`.\nconst PREFERRED_SCENE = \"plp\"\n\n/** The URL of a specific SCENE for a colour (or any colour when `colour` is null) — matches\n * `-<scene>-` in the filename + the colour token. null if no such image. */\nfunction sceneImage(\n images: Array<ImageLike> | null | undefined,\n scene: string,\n colour: string | null\n): string | null {\n for (const img of images ?? []) {\n const u = (img?.url ?? \"\").trim()\n if (!u || !u.toLowerCase().includes(`-${scene}-`)) continue\n if (colour === null || new RegExp(`(^|[^a-z])${escapeRegex(colour)}([^a-z]|$)`, \"i\").test(u.toLowerCase())) {\n return u\n }\n }\n return null\n}\n\n/**\n * The image URL to render for a given colour. Resolves, in order:\n * 1. the flat-lay \"plp\" scene for the colour — the CONSISTENT, thumbnail-matching choice (D9), so\n * every tee shows the same flat-on-concrete shot rather than an order-dependent on-model/hero.\n * 2. the matching variant's own images[]\n * 3. a product image whose url/metadata names the colour\n * 4. null → the caller falls back to product.thumbnail + a \"black available\" note.\n * Never throws; a missing colour just yields null.\n */\nexport function imageForColour(p: ProductLike, colour: string): string | null {\n const target = colour.trim().toLowerCase()\n if (!target) {\n return sceneImage(p.images, PREFERRED_SCENE, null) ?? p.thumbnail ?? firstImageUrl(p.images) ?? null\n }\n\n // 1) the flat-lay plp scene for this colour — consistent across every product (matches thumbnail).\n const plp = sceneImage(p.images, PREFERRED_SCENE, target)\n if (plp) return plp\n\n // 2) the colour's variant images.\n for (const v of p.variants ?? []) {\n if (variantColour(v) !== target) continue\n const url = firstImageUrl(v.images)\n if (url) return url\n }\n\n // 3) a product image that names the colour (url substring or metadata.colour tag).\n for (const img of p.images ?? []) {\n const tag = (img.metadata?.[\"colour\"] ?? img.metadata?.[\"color\"] ?? \"\")\n .toString()\n .toLowerCase()\n if (tag === target) {\n const u = (img.url ?? \"\").trim()\n if (u) return u\n }\n }\n for (const img of p.images ?? []) {\n const u = (img.url ?? \"\").trim()\n if (u && new RegExp(`(^|[^a-z])${escapeRegex(target)}([^a-z]|$)`, \"i\").test(u)) {\n return u\n }\n }\n return null\n}\n\n/**\n * The image for a (fit, colour): the `plp` scene by image METADATA first (fit-specific, then fit-neutral),\n * then the legacy filename resolution (`imageForColour`). null when the colour has no shot at all (e.g. a\n * colour sold on one blank only) so the caller can fall back to the thumbnail + a note.\n */\nexport function imageFor(p: ProductLike, sel: { fit: Fit | null; colour: string }): string | null {\n const colour = sel.colour.trim().toLowerCase()\n if (!colour) return null\n const metas = (p.images ?? []).map((img) => ({\n url: (img?.url ?? \"\").trim(),\n scene: typeof img?.metadata?.[\"scene\"] === \"string\" ? String(img.metadata[\"scene\"]) : null,\n fit: parseFit(img?.metadata?.[\"fit\"]),\n colour: typeof img?.metadata?.[\"colour\"] === \"string\" ? String(img.metadata[\"colour\"]).toLowerCase() : null,\n }))\n const tagged = metas.filter((m) => m.url && m.colour === colour && m.scene === PREFERRED_SCENE)\n const exact = tagged.find((m) => sel.fit !== null && m.fit === sel.fit)\n if (exact) return exact.url\n const neutral = tagged.find((m) => m.fit === null)\n if (neutral) return neutral.url\n // A plp shot tagged for the OTHER fit only → this colour has no shot in THIS fit, so show nothing rather\n // than the wrong garment (the legacy filename resolution below would match the other fit's `-plp-` file).\n const otherFitOnly = tagged.length > 0 && sel.fit !== null\n if (otherFitOnly) return null\n return imageForColour(p, colour)\n}\n\nfunction firstImageUrl(images: Array<ImageLike> | null | undefined): string | null {\n for (const img of images ?? []) {\n const u = (img?.url ?? \"\").trim()\n if (u) return u\n }\n return null\n}\n\nfunction escapeRegex(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")\n}\n\n/** The segment of a variant title that IS a size (\"Regular / Coal / M\" → \"M\"), or null. Unlike\n * sizeOfVariant, never falls back to another segment: \"Regular / Coal\" has no size (TB252). */\nexport function sizeTokenOf(title: string | null | undefined): string | null {\n return titleSegments(title).find((s) => SIZE_TOKEN.test(s)) ?? null\n}\n\n// Canonical garment size order — so a size list always reads S · M · L · XL · 2XL, never the\n// arbitrary variant order the API happens to return (E2). Unknown sizes sort to the end, alpha.\nconst SIZE_ORDER = [\"xxs\", \"xs\", \"s\", \"m\", \"l\", \"xl\", \"2xl\", \"xxl\", \"3xl\", \"xxxl\", \"4xl\", \"5xl\"]\n\n/** Sort garment sizes into the canonical XS→5XL order (case-insensitive; unknowns last). */\nexport function sortSizes(sizes: string[]): string[] {\n const rank = (s: string): number => {\n const i = SIZE_ORDER.indexOf(s.trim().toLowerCase())\n return i === -1 ? SIZE_ORDER.length : i\n }\n return [...sizes].sort((a, b) => rank(a) - rank(b) || a.localeCompare(b))\n}\n\n/** A cart line's variant options (present when the cart was fetched with `*items.variant.options.option`). */\nexport type LineLike = {\n variant_title?: string | null\n title?: string | null\n variant?: { options?: Array<{ value?: string | null; option?: { title?: string | null } | null }> | null } | null\n}\n\n/** A cart line's value for the first variant option whose title (lowercased) is in `titles`; null when absent. */\nexport function lineOption(item: LineLike, titles: string[]): string | null {\n const o = item.variant?.options?.find((x) => titles.includes((x.option?.title ?? \"\").toLowerCase()))\n const v = typeof o?.value === \"string\" ? o.value.trim() : \"\"\n return v ? v : null\n}\n\n/**\n * The colour carried by a cart line (A3): the variant's Colour OPTION first, else a colour token in the\n * variant title (never a positional split: with a Fit option the title is \"Oversize / Black / M\").\n * Returns null when the line has no colour axis. Shared by the CLI and the MCP.\n */\nexport function lineColour(item: LineLike): string | null {\n const opt = lineOption(item, [\"colour\", \"color\"])\n if (opt) return opt.toLowerCase()\n const vt = (item.variant_title ?? \"\").toString()\n return vt ? variantColour({ id: \"x\", title: vt }) : null\n}\n\n/** The size carried by a cart line (TB252): the Size OPTION, else a real size token in the title; never another segment. */\nexport function lineSize(item: LineLike): string | null {\n return lineOption(item, [\"size\"]) ?? sizeTokenOf(item.variant_title)\n}\n","// The lifecycle stage of an order, for every channel (bt order, bt log; D1 review). ONE mapping, an\n// EXACT switch over Medusa's FulfillmentStatus: the substring version read \"not_fulfilled\" as a later\n// stage, because it contains \"fulfilled\". Medusa's own words, the same for every supplier.\n//\n// Brand rule (Sam 2026-10-02, memory customer-sees-shipped-or-not): a customer only learns placed\n// (committed), shipped, delivered or reverted, never a production stage. The backend already answers\n// not_fulfilled for made-not-shipped; Medusa's raw fulfilled / partially_fulfilled still read committed\n// here, so an older backend (or any route that forwards Medusa's word) cannot bring the stage back.\n\nexport type OrderStage = \"committed\" | \"shipped\" | \"delivered\" | \"reverted\"\n\nexport interface OrderStageInput {\n status?: string | null\n fulfillment_status?: string | null\n}\n\nexport function orderStage(o: OrderStageInput): OrderStage {\n const status = (o.status ?? \"\").trim().toLowerCase()\n if (status === \"canceled\" || status === \"refunded\") return \"reverted\"\n switch ((o.fulfillment_status ?? \"\").trim().toLowerCase()) {\n case \"canceled\":\n return \"reverted\"\n case \"delivered\":\n case \"partially_delivered\":\n return \"delivered\"\n case \"shipped\":\n case \"partially_shipped\":\n return \"shipped\"\n case \"fulfilled\": // made, not shipped: to the customer it is simply not shipped yet\n case \"partially_fulfilled\":\n case \"not_fulfilled\":\n default:\n // placed, not shipped yet (not a payment signal).\n return \"committed\"\n }\n}\n\n/**\n * The fulfilment word a channel shows a customer (MCP get_order_status). The same rule as the backend's\n * customerFulfillmentStatus (apps/backend/src/lib/order-fulfillment-status.ts, the packages cannot import it):\n * made-not-shipped (fulfilled / partially_fulfilled) reads not_fulfilled; everything else passes through.\n */\nexport function customerFulfillmentStatus(word: string | null | undefined): string | null {\n const w = (word ?? \"\").trim().toLowerCase()\n if (!w) return null\n return w === \"fulfilled\" || w === \"partially_fulfilled\" ? \"not_fulfilled\" : w\n}\n","// Where a REUSED cart ships, for the colour/size gates (D1 money-path review, HIGH). One rule for the CLI\n// (`bt stage` onto config.cart_id) and the MCP (`add_to_cart` with a cart_id): a reused cart is judged by\n// where IT ships, never by the caller's region argument or a server/config default. Before this, a US cart\n// reused under an SE default took an Oversize XS (SOL'S makes it, Shaka does not): paid, then held at\n// fulfilment.\nimport type { CartLike, RegionLike } from \"./types\"\n\nexport interface CartShipTo {\n /** The cart's own region (with its countries). */\n region: RegionLike\n /** The gate country: the cart's shipping address, else its region's first country. Lower-case ISO-2. */\n country: string\n /** True when the country came from the shipping address (an exact destination, not a region default). */\n fromAddress: boolean\n /** Every country of the cart's region, lower-case. */\n regionCountries: string[]\n}\n\n/**\n * Where `cart` ships, or null when that cannot be read (no region we know, no country): the caller must\n * refuse, because a cart whose destination is unknown cannot be gated. `regions` is looked up by the cart's\n * region_id only when the cart did not carry its region's countries.\n */\nexport async function cartShipTo(cart: CartLike, regions: () => Promise<RegionLike[]>): Promise<CartShipTo | null> {\n const embedded = cart.region?.countries?.length ? cart.region : null\n const id = cart.region_id ?? cart.region?.id ?? null\n const region = embedded ?? (id ? (await regions()).find((r) => r.id === id) ?? null : null)\n if (!region) return null\n const regionCountries = (region.countries ?? []).map((c) => (c.iso_2 ?? \"\").trim().toLowerCase()).filter(Boolean)\n const address = (cart.shipping_address?.country_code ?? \"\").trim().toLowerCase() || null\n const country = address ?? regionCountries[0] ?? null\n if (!country) return null\n return { region, country, fromAddress: address !== null, regionCountries }\n}\n\n/**\n * Does an explicitly requested ship-to country disagree with where the cart ships? Same country: no. A\n * country inside the cart's region when the cart has no address yet: no (same region, same currency, and\n * every country of a region produces in the same place). Anything else: yes, refuse.\n */\nexport function shipToDisagrees(requested: string | null | undefined, shipTo: CartShipTo): boolean {\n const cc = (requested ?? \"\").trim().toLowerCase()\n if (!cc || cc === shipTo.country) return false\n return shipTo.fromAddress || !shipTo.regionCountries.includes(cc)\n}\n","// Canonical product-content accessors — the ONE source for a piece's README, spec sheet, and lore,\n// shared by the CLI (`bt view` / `bt blame`), the MCP (`get_product`), and (aspirationally) the\n// storefront PDP. Keeping these here means the three channels can't drift on what a piece \"says\".\n//\n// The spec fields + fallbacks mirror the storefront's buildSpecs (apps/storefront .../templates).\n\nimport type { ProductLike } from \"./types\"\nimport { supplierOf } from \"./fit\"\n\n/** One row of the spec sheet. */\nexport interface SpecEntry {\n key: string\n value: string\n}\n\n/** A lore link (a real source behind the pun/idea). */\nexport interface LoreSource {\n url: string\n label: string\n}\n\n/** A piece's lore: the story text + any sources. Empty text = no lore on record. */\nexport interface ProductLore {\n text: string\n sources: LoreSource[]\n}\n\n/** A non-empty string, or the fallback. */\nfunction str(v: unknown, fallback: string): string {\n return typeof v === \"string\" && v.length > 0 ? v : fallback\n}\n\n/** The piece's README — the human description shown on the PDP's README toggle. */\nexport function productReadme(p: ProductLike): string {\n return typeof p.description === \"string\" ? p.description : \"\"\n}\n\n// Fallback spec rows per supplier. MIRRORS apps/storefront/src/lib/util/specs.ts + delivery.ts (packages cannot import\n// each other); change a sentence in both. Printful wording is a DRAFT until Sam signs it off; the delivery line is\n// identical for both on purpose (Sam 2026-10-01: today's wording everywhere).\nconst GELATO_FALLBACK = {\n material: \"100% cotton\",\n weight: \"180 g/m²\",\n fit: \"Unisex heavyweight crewneck\",\n delivery: \"Ships in 2–5 business days + transit\",\n care: \"Cold wash, line dry\",\n}\nconst PRINTFUL_FALLBACK = {\n material: \"100% cotton\",\n weight: \"240 g/m²\",\n fit: \"Unisex heavyweight crewneck, garment-dyed\",\n delivery: \"Ships in 2–5 business days + transit\",\n care: \"Machine wash cold, inside out. Do not tumble dry or iron the print. Line dry in shade.\",\n}\n\n/**\n * The spec sheet — the 6 canonical rows, in PDP order, with the same fallbacks as the storefront's\n * buildSpecs (the single source of truth for what a piece is made of / how it ships).\n */\nexport function productSpecs(p: ProductLike, fit?: \"regular\" | \"oversize\" | null): SpecEntry[] {\n const md = (p.metadata ?? {}) as Record<string, unknown>\n // CAT-31: a merged product carries `specs_by_fit` (one block per fit); a legacy product the flat keys.\n const byFit = (md.specs_by_fit ?? null) as Record<string, Record<string, unknown>> | null\n const fitKey = fit ?? (byFit && Object.keys(byFit).length === 1 ? Object.keys(byFit)[0] : undefined)\n const src: Record<string, unknown> = (fitKey && byFit?.[fitKey]) || md\n const pick = (k: string, fallback: string) => str(src[k] ?? md[k], fallback)\n const d = supplierOf(p) === \"printful\" ? PRINTFUL_FALLBACK : GELATO_FALLBACK\n return [\n { key: \"material\", value: pick(\"material\", d.material) },\n { key: \"weight\", value: pick(\"weight\", d.weight) },\n { key: \"fit\", value: pick(\"fit\", d.fit) },\n { key: \"print\", value: pick(\"print\", \"Front print\") },\n // Delivery: the product's own line first (Sam 2026-10-01, as the storefront since U1), else the same\n // fallback wording for both suppliers. No per-region times.\n { key: \"delivery\", value: pick(\"delivery\", d.delivery) },\n { key: \"care\", value: pick(\"care\", d.care) },\n ]\n}\n\n/** The piece's lore — the story behind the pun + any real sources. Empty text = none on record. */\nexport function productLore(p: ProductLike): ProductLore {\n const md = (p.metadata ?? {}) as Record<string, unknown>\n const text = typeof md.lore === \"string\" ? md.lore : \"\"\n const raw = Array.isArray(md.lore_sources) ? md.lore_sources : []\n const sources: LoreSource[] = raw\n .filter(\n (s): s is { url: string; label: string } =>\n !!s &&\n typeof s === \"object\" &&\n typeof (s as { url?: unknown }).url === \"string\" &&\n typeof (s as { label?: unknown }).label === \"string\"\n )\n .map((s) => ({ url: s.url, label: s.label }))\n return { text, sources }\n}\n","// Tax-inclusivity — the single source of truth for the \"incl. VAT\" price label across EVERY\n// channel (storefront, the bt CLI, the MCP server). Kept here (in the shared client) so the CLI\n// and MCP agree with each other and with the backend on how the flag is read.\n\nimport type { CartLike } from \"./types\"\n\n/**\n * Whether a cart's prices are VAT-inclusive (EU/SE) vs tax-exclusive (US).\n *\n * Medusa exposes tax-inclusivity PER LINE ITEM (and per shipping method), never on the cart root —\n * there is no `cart.is_tax_inclusive` in @medusajs/types 2.15.5. So we DERIVE it from the items.\n * It is uniform per region (every line shares the region's tax setting), so `.some` is exact and a\n * mixed cart cannot occur. A US (tax-exclusive) cart → false → NO \"incl. VAT\" label; an SE cart →\n * true → \" incl. VAT\". This is NEVER keyed off a currency or country literal (INV-11): the label\n * follows the market's own tax flag, so it stays correct as new regions come online.\n *\n * Fails closed: an empty/absent cart or items array → false (no phantom VAT label on nothing).\n */\nexport function isCartTaxInclusive(cart: Pick<CartLike, \"items\"> | null | undefined): boolean {\n return (cart?.items ?? []).some((i) => i.is_tax_inclusive === true)\n}\n","/**\n * ONE definition of what a money block contains, shared by every channel (CAT-44).\n *\n * Sam, 2026-09-07, looking at `bt commit`: *\"terminal is missing to display shipping costs\"* and\n * *\"actually not showing discount either\"*. The terminal printed a single `total` with the shipping\n * silently folded in, so a buyer could not see what they were paying for. The same week, the\n * order-confirmation email was found printing a total that ignored discounts entirely (CAT-42).\n * Both are the same failure: a surface inventing its own arithmetic and showing only the answer.\n *\n * So the ROWS live here, once, and each surface only decides how to paint them. The order is the\n * ordinary receipt order, matching the confirmation email and the storefront checkout:\n *\n * subtotal → discount(s) → shipping → total\n *\n * This module is pure and formats nothing: amounts stay numbers in the cart's own currency\n * (data-price format, never cents), so callers keep using their own fmtPrice + VAT suffix.\n */\n\nexport type MoneyRowKind = \"subtotal\" | \"discount\" | \"shipping\" | \"total\"\n\nexport type MoneyRow = {\n kind: MoneyRowKind\n /** Lowercase label, e.g. \"subtotal\" / \"dev channel\" / \"shipping\" / \"total\". */\n label: string\n /** Always POSITIVE. A discount row is rendered with a leading minus by the caller. */\n amount: number\n /** Rendered instead of the amount when set, e.g. \"free\" or \"added at checkout\". */\n display?: string\n /** A trailing parenthetical, e.g. \"10% — for shopping the terminal\". */\n note?: string\n}\n\nexport type MoneyRowsInput = {\n /**\n * Optional. NOT used for the subtotal — see cartMoneyRows for why the subtotal is DERIVED. Kept\n * only to decide whether a cart has items at all.\n */\n itemTotal?: number | null\n /** Discount to show. On a terminal cart this is 0 until checkout mints it (see discountNote). */\n discountTotal?: number | null\n /** What the discount is called, e.g. \"dev channel\". Defaults to \"discount\". */\n discountLabel?: string | null\n /** e.g. \"10% — for shopping the terminal\". */\n discountNote?: string | null\n /** What shipping costs. 0 can mean \"free\" or \"not priced yet\" — hasShippingMethod decides which. */\n shippingTotal?: number | null\n /** False before checkout attaches a method, when shipping is simply not known yet. */\n hasShippingMethod?: boolean\n /** The final amount. Always shown, even when everything above is absent. */\n total?: number | null\n}\n\nconst num = (v: number | null | undefined) => {\n const n = Number(v ?? 0)\n return Number.isFinite(n) ? n : 0\n}\n\n/**\n * Build the rows for a cart or checkout review.\n *\n * The block is CONSISTENT: with items present you always get subtotal, shipping and total, whether\n * or not shipping has been priced yet — \"shipping: added at checkout\" is itself the information a\n * buyer needs, because it says the total is not final. Only a zero discount is omitted; the first\n * draft of this also collapsed a one-line cart to a bare total, and that conditional immediately\n * produced two tests contradicting each other, which is a good sign a money block should not be\n * clever. A zero discount is the one thing worth hiding, since \"−0\" tells nobody anything.\n */\nexport function cartMoneyRows(input: MoneyRowsInput): MoneyRow[] {\n const itemTotal = num(input.itemTotal)\n const discount = Math.abs(num(input.discountTotal))\n const shipping = num(input.shippingTotal)\n const total = num(input.total)\n const hasMethod = Boolean(input.hasShippingMethod)\n\n const rows: MoneyRow[] = []\n // A cart with money in it gets a shipping row even when we cannot see its item subtotal (an older\n // backend, or a caller that only has a total). Dropping the row there would silently re-open A2:\n // the buyer would read a pre-shipping figure with nothing saying shipping is still to come.\n const hasMoney = itemTotal > 0 || total > 0\n\n // The subtotal is DERIVED from the total, never read off the cart. Medusa's `item_total` is\n // NET of promotions once one is applied, so printing it above a discount line double-counts the\n // discount and the column stops adding up: shipped 0.4.3 briefly showed\n // subtotal 359.10 · discount −39.90 · shipping 59 · total 418.10\n // where the first three make 378.20. Deriving guarantees the block reconciles by construction —\n // the total is the authoritative number (it is what the buyer pays), so everything else is\n // expressed relative to it.\n const subtotal = Math.round((total - shipping + discount) * 100) / 100\n\n if (subtotal > 0) {\n rows.push({ kind: \"subtotal\", label: \"subtotal\", amount: subtotal })\n }\n if (discount > 0) {\n rows.push({\n kind: \"discount\",\n label: input.discountLabel || \"discount\",\n amount: discount,\n ...(input.discountNote ? { note: input.discountNote } : {}),\n })\n }\n if (hasMoney) {\n rows.push(\n hasMethod\n ? // A method is attached, so the number is real: 0 here means a promotion covered it.\n { kind: \"shipping\", label: \"shipping\", amount: shipping, ...(shipping === 0 ? { display: \"free\" } : {}) }\n : // No method yet — say so rather than implying shipping is free (A2: a pre-shipping figure\n // must never read as the final price).\n { kind: \"shipping\", label: \"shipping\", amount: 0, display: \"excl. shipping — added at checkout\" }\n )\n }\n rows.push({ kind: \"total\", label: \"total\", amount: total })\n return rows\n}\n","// The B&T MCP server — stdio transport, registering exactly the channel's deterministic tools.\n//\n// The server is a THIN registrar: each tool is a deterministic wrapper over the shared\n// StoreClient (no free-form execution, no admin op — SECURITY T10/G8). The tool DEFINITIONS\n// (names, input schemas, handlers) live in tools.ts; this module just builds the McpServer and\n// wires the stdio transport so a developer's AI agent can call them over stdin/stdout.\n\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\"\nimport type { ZodRawShape } from \"zod\"\nimport type { ImageBlock } from \"./images\"\nimport { VERSION } from \"./version\"\nimport { GETTING_STARTED, GETTING_STARTED_URI, buyPromptText } from \"./onboarding\"\n\n/** What a tool handler returns — MCP content blocks (image and/or text). */\nexport interface ToolResult {\n content: ImageBlock[]\n /** Set when the tool reports a user-facing error (e.g. a 404 lookup) — not a transport error. */\n isError?: boolean\n /**\n * Machine-readable result (ids/handles like cart_id/session_id/order_id) so an agent can chain\n * tools WITHOUT regexing the prose (d9). Already redacted (G10). Mirrors the human text block.\n */\n structuredContent?: Record<string, unknown>\n}\n\n/**\n * A deterministic tool definition. `inputSchema` is a Zod raw shape (the SDK turns it into a JSON\n * schema for the agent). The handler receives the parsed args and returns content blocks. Eggs\n * (VOICE §3.2 M2/M3) may live in the `description` text but NEVER alter behavior.\n */\nexport interface ToolDef<S extends ZodRawShape = ZodRawShape> {\n name: string\n description: string\n inputSchema: S\n handler: (args: Record<string, unknown>) => Promise<ToolResult>\n}\n\n// Advertised to the host in the initialize handshake. Version is single-sourced from version.ts\n// (mirrors package.json; version.test.ts locks them together) — no more silent drift.\nexport const SERVER_INFO = {\n name: \"binary-and-thread\",\n version: VERSION,\n} as const\n\n/**\n * Build an McpServer and register every tool. Returns the server plus the registered tool names\n * (so callers/tests can assert exactly the 10 tools are present). Pure — no transport, no I/O.\n */\nexport function buildServer(tools: ToolDef[]): {\n server: McpServer\n toolNames: string[]\n resourceNames: string[]\n promptNames: string[]\n} {\n const server = new McpServer(SERVER_INFO)\n const toolNames: string[] = []\n // The SDK's registerTool generic over a ZodRawShape input schema infers excessively deeply\n // (TS2589); we register through a structurally-typed view of the method to keep the call site\n // simple. The runtime contract is unchanged — name, { description, inputSchema }, handler.\n const register = server.registerTool.bind(server) as unknown as (\n name: string,\n config: { description: string; inputSchema: ZodRawShape },\n cb: (args: Record<string, unknown>) => Promise<{\n content: ImageBlock[]\n isError?: boolean\n structuredContent?: Record<string, unknown>\n }>\n ) => void\n for (const t of tools) {\n register(\n t.name,\n { description: t.description, inputSchema: t.inputSchema },\n // The SDK validates args against inputSchema before calling us. We hand the parsed args to\n // the deterministic handler and return its content blocks (+ optional structuredContent, d9).\n async (args: Record<string, unknown>) => {\n const res = await t.handler(args ?? {})\n return {\n content: res.content,\n ...(res.isError ? { isError: true } : {}),\n ...(res.structuredContent ? { structuredContent: res.structuredContent } : {}),\n }\n }\n )\n toolNames.push(t.name)\n }\n\n // Agent onboarding: a getting-started RESOURCE + a `buy` PROMPT (neither can touch money — they're\n // read-only orientation). Registered through structurally-typed views of the SDK methods to avoid\n // the deep generic inference the tool registration also side-steps.\n const registerResource = server.registerResource.bind(server) as unknown as (\n name: string,\n uri: string,\n metadata: { title?: string; description?: string; mimeType?: string },\n cb: (uri: URL) => { contents: Array<{ uri: string; text: string; mimeType?: string }> }\n ) => void\n registerResource(\n \"getting-started\",\n GETTING_STARTED_URI,\n { title: \"binary & thread — getting started\", description: \"the buy flow + the money boundary, for a cold agent\", mimeType: \"text/plain\" },\n () => ({ contents: [{ uri: GETTING_STARTED_URI, text: GETTING_STARTED, mimeType: \"text/plain\" }] })\n )\n const resourceNames = [\"getting-started\"]\n\n const registerPrompt = server.registerPrompt.bind(server) as unknown as (\n name: string,\n config: { title?: string; description?: string; argsSchema?: ZodRawShape },\n cb: (args: Record<string, unknown>) => { messages: Array<{ role: \"user\"; content: { type: \"text\"; text: string } }> }\n ) => void\n registerPrompt(\n \"buy\",\n { title: \"buy a piece\", description: \"walk through buying a piece (respecting the card-stays-with-the-human boundary)\" },\n (args) => ({\n messages: [\n {\n role: \"user\",\n content: {\n type: \"text\",\n text: buyPromptText(\n typeof args?.handle === \"string\" ? args.handle : undefined,\n typeof args?.region === \"string\" ? args.region : undefined\n ),\n },\n },\n ],\n })\n )\n const promptNames = [\"buy\"]\n\n return { server, toolNames, resourceNames, promptNames }\n}\n\n/** Build the server with the real tools and start it on stdio. Used by bin.ts. */\nexport async function startServer(tools: ToolDef[]): Promise<McpServer> {\n const { server } = buildServer(tools)\n const transport = new StdioServerTransport()\n await server.connect(transport)\n return server\n}\n","// The single source of truth for the MCP server version string.\n//\n// Mirrored into mcp/package.json's \"version\" field; version.test.ts asserts the two never drift.\n// We don't import package.json directly (JSON imports complicate the ESM dist build), so this\n// literal is the canonical value the server advertises to a host in the `initialize` handshake\n// (SERVER_INFO.version). Before this, SERVER_INFO hardcoded \"0.1.0\" and drifted from the published\n// package — a host inspecting the server saw a stale version, and bug reports mis-attributed it.\nexport const VERSION = \"0.4.15\"\n","// Agent onboarding surfaces — a getting-started RESOURCE + a `buy` PROMPT. The MCP parallel to the\n// CLI's provenance line + `bt init`: a cold agent shouldn't have to infer the flow (or the money\n// boundary) from ten tool descriptions. Deadpan-competent dev voice; brand-voice clean (never\n// \"tee\"/\"t-shirt\"/print-on-demand/Gelato — enforced by brand-voice.test.ts scanning src/).\n\nexport const GETTING_STARTED_URI = \"bt://getting-started\"\n\nexport const GETTING_STARTED = `binary & thread — buying from your agent.\n\nthe flow (each tool returns machine-readable structuredContent, so you can chain without regexing prose):\n 1. list_products [region] what's in stock, priced in the ship-to market's currency\n 2. get_product <handle> full detail + an authoritative variant_id + sizes + colours\n 3. add_to_cart … start or reuse a cart (returns a durable cart_id handle)\n 4. get_cart <cart_id> the authoritative cart — note: the total is PRE-shipping\n 5. checkout <cart_id> mint a pay link + the SHIPPING-INCLUSIVE total the buyer pays\n 6. check_payment <session_id> poll once for the result (call again if still pending)\n\nthe dev-channel discount: every purchase through this MCP (or the bt CLI) is 10% off the web —\napplied automatically at checkout, no code. checkout returns the discounted total; quote that.\n\nthe boundary (by design): payment + ship-to details are entered by the human on the storefront.\nthey never enter this chat, and you cannot complete a purchase yourself — you walk them to the door.\n\nregion + currency: prices and shipping follow the ship-to country. pass region (ISO-2, e.g. se / us /\nde) to list_products / get_product / add_to_cart, or the host can set BT_REGION. a country we don't\nship to is refused rather than priced in the wrong currency.`\n\n/** The `buy` prompt body — a templated walk-through of the flow + the boundary, optionally seeded\n * with a handle/region the user named. Returned as a single user-turn message. */\nexport function buyPromptText(handle?: string, region?: string): string {\n const target = handle ? `\"${handle}\"` : \"the piece the user wants\"\n const reg = region ? ` Price it for region \"${region}\".` : \"\"\n return `Help me buy ${target} from binary & thread through the MCP tools.${reg}\n\nSteps:\n1. If you don't have an exact variant_id, call get_product to get valid sizes/colours + the variant_id.\n2. add_to_cart with the variant_id (or handle + size + colour). Keep the returned cart_id.\n3. Show me the cart total, and remind me it excludes shipping until checkout (where a 10%\n dev-channel discount also applies — the terminal's cut).\n4. When I confirm, call checkout and give me the pay link + the shipping-inclusive, discounted total.\n5. Do NOT ask me for card or address details — I enter those in the browser. After I pay, use\n check_payment with the session_id to confirm the order.`\n}\n","// The 10 deterministic MCP tools over the SHARED StoreClient (SECURITY G8/T10; ROBUSTNESS §4.2/4.3).\n//\n// These are thin wrappers — no free-form execution, no admin op, no money self-completion. They\n// reuse the same idempotency / region-resolution / retry / redaction that the CLI uses (it all\n// lives inside the StoreClient — written once, contract-tested once).\n//\n// The hard invariants this file enforces (the MCP red-team gates):\n// - checkout (G8 confused-deputy / F-I4): returns {checkout_url, session_id} INSTANTLY. It NEVER\n// polls, NEVER opens a browser, NEVER sets a shipping address. Its result text tells the human\n// to open the link and explicitly says \"I can't complete this for you\". The human pays on the\n// storefront (Stripe Elements) — payment details NEVER enter the chat. check_payment polls; the\n// agent re-calls at its own cadence (async handle-ID).\n// (NB: there is deliberately NO payment-detail field anywhere in this package; the security\n// verify greps for those field tokens and must come back clean.)\n// - list views are TEXT-ONLY (no per-item images — token-bloat guard, ROBUSTNESS §4.3).\n// - every backend string is sanitize()'d (G8); every output object is redactForOutput()'d (G10).\n// - get_order_status returns status/timeline only — NO address (F-H3); id + exact-email, 404 on\n// miss (F-H1).\n\nimport { z } from \"zod\"\nimport {\n RecoverableCartError,\n StoreApiError,\n productColours,\n colourLabel,\n variantColour,\n sizesForColour,\n sizeOfVariant,\n productSpecs,\n productFits,\n priceFrom,\n priceFor,\n coloursFor,\n sizesFor,\n resolveVariant,\n variantFit,\n colourBuyable,\n sizeBuyable,\n gateFit,\n supplierOf,\n printfulBlankOf,\n parseFit,\n fitLabel,\n imageFor,\n productLore,\n productReadme,\n isCartTaxInclusive,\n cartMoneyRows,\n cartShipTo,\n shipToDisagrees,\n orderStage,\n customerFulfillmentStatus,\n lineColour,\n lineSize,\n sortSizes,\n} from \"@binaryandthread/store-client\"\nimport type {\n StoreClient,\n CartLike,\n RegionLike,\n} from \"@binaryandthread/store-client\"\nimport { sanitize, redactForOutput } from \"./sanitize\"\nimport { buildImageContent } from \"./images\"\nimport type { ImageBlock } from \"./images\"\nimport type { ToolDef, ToolResult } from \"./server\"\nimport { TEAPOT_TEXT, FORTUNES, fortune } from \"./eggs\"\n\nexport interface ToolsOpts {\n /** ISO-2 country code to resolve pricing (never a hardcoded currency literal). */\n region?: string\n}\n\n// ----- result helpers ---------------------------------------------------------\n\nfunction text(s: string): ToolResult {\n return { content: [{ type: \"text\", text: s }] }\n}\n\nfunction errorText(s: string): ToolResult {\n return { content: [{ type: \"text\", text: s }], isError: true }\n}\n\n/** A text block PLUS machine-readable structuredContent (ids/handles) for agent chaining (d9). */\nfunction result(textBody: string, structured: Record<string, unknown>): ToolResult {\n return {\n content: [{ type: \"text\", text: textBody }],\n structuredContent: redactForOutput(structured) as Record<string, unknown>,\n }\n}\n\n/** Price line — currency comes from the variant/region, NEVER a literal (F-A2/INV-11). */\nfunction fmtPrice(amount: number | null | undefined, currency: string | null | undefined): string {\n if (amount == null) {\n return \"—\"\n }\n const cur = (currency ?? \"\").toUpperCase()\n return cur ? `${amount} ${cur}` : `${amount}`\n}\n\n/**\n * The tax suffix for a price line: \" incl. VAT\" ONLY for a tax-inclusive market (EU/SE), \"\" for a\n * tax-exclusive one (US). Driven off the cart's / variant's OWN is_tax_inclusive flag, NEVER a\n * currency literal (INV-11) — mirrors the CLI + storefront. Pass isCartTaxInclusive(cart) for a cart\n * total, or a variant's calculated_price.is_calculated_price_tax_inclusive for a product price.\n */\nfunction taxSuffix(isTaxInclusive?: boolean | null): string {\n return isTaxInclusive ? \" incl. VAT\" : \"\"\n}\n\n// Machine-readable cart line items so an agent can itemize the cart from structuredContent instead\n// of re-parsing the prose (D2). Untrusted titles sanitize()'d; ids/numbers pass through.\n// TB252: + size and colour (the stored key, e.g. \"coal\"), so an agent can read back what it added.\nfunction cartItems(cart: CartLike): Array<{\n variant_id: string | null\n title: string\n size: string | null\n colour: string | null\n qty: number\n total: number | null\n}> {\n return (cart.items ?? []).map((li) => {\n const size = lineSize(li)\n const colour = lineColour(li)\n return {\n variant_id: li.variant_id ?? null,\n title: sanitize(li.product_title ?? li.title ?? \"item\", 80),\n size: size ? sanitize(size, 20) : null,\n colour: colour ? sanitize(colour, 20) : null,\n qty: li.quantity ?? 1,\n total: li.total ?? null,\n }\n })\n}\n\nfunction cartSummary(cart: CartLike, region?: RegionLike): string {\n const lines: string[] = []\n lines.push(`cart ${cart.id}`)\n const items = cart.items ?? []\n if (items.length === 0) {\n lines.push(\" (empty)\")\n } else {\n for (const li of items) {\n const title = sanitize(li.product_title ?? li.title ?? \"item\", 80)\n // TB129 review: the colour as the buyer sees it (\"Black\", \"Faded Black\"), never the stored key (\"jet\").\n const rawColour = lineColour(li)\n const colour = rawColour ? colourLabel(rawColour) : null\n const size = lineSize(li)\n const tags = [colour, size].filter((x): x is string => !!x).map((x) => ` · ${sanitize(x, 20)}`).join(\"\")\n lines.push(` ${li.quantity}x ${title}${tags} — ${fmtPrice(li.total, cart.currency_code ?? region?.currency_code)}`)\n }\n }\n // CAT-44 — the full money block (subtotal → discount → shipping → total), identical to what the\n // CLI and the confirmation email print. A single `total` with shipping folded into it left both a\n // buyer and an agent unable to see what they were paying for. Shipping stays honestly labelled\n // \"added at checkout\" until a method is attached, so an agent never quotes a pre-shipping figure\n // as final (A2).\n const cur = cart.currency_code ?? region?.currency_code\n for (const row of cartMoneyRows({\n itemTotal: cart.item_total,\n discountTotal: cart.discount_total,\n shippingTotal: cart.shipping_total,\n hasShippingMethod: (cart.shipping_methods ?? []).length > 0,\n total: cart.total,\n })) {\n const value = row.display ?? `${row.kind === \"discount\" ? \"−\" : \"\"}${fmtPrice(row.amount, cur)}`\n const suffix = row.kind === \"total\" ? taxSuffix(isCartTaxInclusive(cart)) : \"\"\n lines.push(` ${row.label} ${value}${suffix}${row.note ? ` (${row.note})` : \"\"}`)\n }\n return lines.join(\"\\n\")\n}\n\n// ----- the 10 tools ------------------------------------------------------------\n\n/** Build the 10 tool definitions bound to a StoreClient. */\nexport function makeTools(client: StoreClient, opts: ToolsOpts = {}): ToolDef[] {\n const regionCC = opts.region\n\n // list_products — TEXT-ONLY catalog (no per-item images — token-bloat guard, §4.3).\n const list_products: ToolDef = {\n name: \"list_products\",\n description:\n \"List the pieces in stock (text-only — no images, to keep the agent's context lean). \" +\n \"Returns each product's handle, price (VAT included where applicable), and stage. Use get_product for one item's image. \" +\n \"Prices are in the region's currency — pass `region` (an ISO-2 country) to price for a specific market, else the server default.\",\n inputSchema: {\n stage: z.string().optional().describe(\"filter by channel stage: 'main' or 'next'\"),\n region: z.string().optional().describe(\"ISO-2 ship-to country to price for (e.g. se / us / de); overrides the server default\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const cc = (typeof args.region === \"string\" && args.region.trim()) || regionCC\n const rr = await client.resolveRegionResult(cc)\n const region = rr.region\n const products = await client.listProducts({ region })\n const stage = typeof args.stage === \"string\" ? args.stage.toLowerCase() : undefined\n const rows = products\n .map((p) => ({ p, stage: String(p.metadata?.[\"channel_stage\"] ?? \"main\") }))\n .filter((r) => (stage ? r.stage.toLowerCase() === stage : true))\n if (rows.length === 0) {\n return result(\"nothing in stock on this branch yet.\", { products: [], currency: region.currency_code, region: rr.requested ?? null })\n }\n // CAT-31: a design sold in two fits (Regular 349 / Oversize 399) is ONE row priced \"from <cheapest>\",\n // with its fits listed so an agent knows to pass `fit` to get_product / add_to_cart.\n const lines = rows.map(({ p, stage }) => {\n const { amount, currency, taxInclusive, from } = priceFrom(p)\n const fits = productFits(p)\n const handle = sanitize(p.handle ?? p.id, 60)\n const title = sanitize(p.title ?? \"\", 80)\n const fitsNote = fits.length > 1 ? ` fits: ${fits.join(\"/\")}` : \"\"\n return `${handle} ${from ? \"from \" : \"\"}${fmtPrice(amount, currency)}${taxSuffix(taxInclusive)} [${stage}] ${title}${fitsNote}`.trimEnd()\n })\n const productList = rows.map(({ p, stage }) => {\n const { amount, currency, taxInclusive, from } = priceFrom(p)\n return {\n handle: p.handle ?? p.id,\n title: sanitize(p.title ?? \"\", 80),\n price: amount,\n price_from: from,\n currency,\n tax_inclusive: taxInclusive,\n stage,\n fits: productFits(p),\n }\n })\n // Echo the resolved region+currency so an agent that asked for a market it can't be priced in\n // (silent SEK fallback) can SEE the substitution instead of quoting the wrong currency (D2).\n return result(lines.join(\"\\n\"), { products: productList, currency: region.currency_code, region: rr.requested ?? null })\n },\n }\n\n // get_product — details + the ONE product image (ImageContent + Blob URL floor).\n const get_product: ToolDef = {\n name: \"get_product\",\n // M3 ★ — a hidden line for the schema-readers (devs read tool descriptions). Rewards the\n // contributor instinct + hints at a real feature (the next branch / drops). Never alters behaviour.\n description:\n \"Get one piece in full: its readme, the spec sheet (material/weight/fit/print/delivery/care), \" +\n \"sizes + price (VAT included where applicable), the lore (the story behind it, with sources), and its image (rendered + \" +\n \"the Blob URL as a text floor). Prices are in the region's currency — pass `region` (ISO-2) to price for \" +\n \"a market. Designs sold in two fits (regular / oversize) are priced per fit: pass `fit` to narrow, and \" +\n \"ALWAYS pass `fit` to add_to_cart for them. // you read the schema. that's the contributor instinct. ask me about the next branch.\",\n inputSchema: {\n handle: z.string().describe(\"the product handle/slug (from list_products)\"),\n fit: z.string().optional().describe(\"regular or oversize: narrow sizes, price and image to that fit (omit to see both)\"),\n region: z.string().optional().describe(\"ISO-2 ship-to country to price for (e.g. se / us / de); overrides the server default\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const handle = String(args.handle ?? \"\")\n const cc = (typeof args.region === \"string\" && args.region.trim()) || regionCC\n const rr = await client.resolveRegionResult(cc)\n const region = rr.region\n const product = await client.getProduct(handle, { region })\n if (!product) {\n return errorText(`no product with handle \"${sanitize(handle, 60)}\". try list_products.`)\n }\n const lines: string[] = []\n lines.push(sanitize(product.title ?? handle, 120))\n if (product.description) {\n lines.push(sanitize(product.description, 400))\n }\n const fabric = product.metadata?.[\"fabric\"]\n if (typeof fabric === \"string\") {\n lines.push(`fabric: ${sanitize(fabric, 80)}`)\n }\n // CAT-31: the fit axis. `fit` narrows to one fit; otherwise every fit is listed. Colours are the ones\n // BUYABLE in the priced country (decision 7: Khaki only where Shaka prints, the US); the rest are\n // named as not available so an agent never offers them.\n const fits = productFits(product)\n const wantedFit = parseFit(args.fit)\n if (args.fit && (!wantedFit || !fits.includes(wantedFit))) {\n return errorText(\n fits.length\n ? `no fit called \"${sanitize(String(args.fit), 20)}\" for \"${sanitize(handle, 60)}\". fits: ${fits.join(\" / \")}.`\n : `\"${sanitize(handle, 60)}\" comes in one fit; omit \\`fit\\`.`\n )\n }\n const shipCC = (rr.requested ?? region.countries?.[0]?.iso_2 ?? \"\").toLowerCase() || null\n const shownFits: Array<import(\"@binaryandthread/store-client\").Fit | null> = fits.length ? (wantedFit ? [wantedFit] : fits) : [null]\n if (fits.length > 1) {\n lines.push(`fit: ${fits.map((f) => `${f} (${fmtPrice(priceFor(product, f).amount, priceFor(product, f).currency)})`).join(\" · \")}${wantedFit ? ` (showing ${wantedFit})` : \"\"}`)\n }\n // A3 — available colours, DERIVED from the variants (never hardcoded). Sizes are listed per\n // colour (and per fit) when there's a colour axis, else a flat list (legacy Size-only product).\n const colours = productColours(product)\n const availability: Record<string, Record<string, string[]>> = {}\n const unavailableColours: Record<string, string[]> = {}\n if (colours.length > 0) {\n for (const f of shownFits) {\n const buyable = coloursFor(product, f, shipCC)\n const notBuyable = coloursFor(product, f).filter((c) => !buyable.includes(c))\n const indent = f ? \" \" : \"\"\n if (f) lines.push(`${fitLabel(f).toLowerCase()}:`)\n if (!f) lines.push(`available colours: ${buyable.map((c) => sanitize(colourLabel(c), 20)).join(\", \")}`)\n const perColour: Record<string, string[]> = {}\n for (const c of buyable) {\n // CAT-37 — only the sizes the ship-to blank can PRINT (Shaka, the US oversize blank, has\n // no XS; SOL'S does). Listing one we cannot make invites add_to_cart to charge for it.\n const cs = sizesFor(product, f, c, shipCC)\n perColour[c] = cs\n lines.push(`${indent} ${sanitize(colourLabel(c), 20)}: ${cs.map((x) => sanitize(x, 20)).join(\" \")}`)\n }\n for (const c of notBuyable) {\n lines.push(`${indent} ${sanitize(colourLabel(c), 20)}: not available where you ship`)\n }\n if (f) {\n availability[f] = perColour\n if (notBuyable.length) unavailableColours[f] = notBuyable\n }\n }\n } else {\n const sizes = (product.variants ?? []).map((v) => sanitize(v.title ?? \"?\", 20))\n lines.push(`sizes: ${sizes.join(\" \")}`)\n }\n const v0 = (product.variants ?? [])[0]\n const shownPrice = wantedFit ? { ...priceFor(product, wantedFit), from: false } : priceFrom(product)\n if (shownPrice.amount != null) {\n lines.push(`price: ${shownPrice.from ? \"from \" : \"\"}${fmtPrice(shownPrice.amount, shownPrice.currency)}${taxSuffix(shownPrice.taxInclusive)}`)\n }\n // The full spec sheet (the PDP's technical block, per fit) + the lore — so the agent knows the piece,\n // not just its price. Single-sourced from store-client (matches the storefront).\n const specs = productSpecs(product, wantedFit ?? (fits.length ? fits[0] : null))\n lines.push(\"specs:\")\n for (const s of specs) {\n lines.push(` ${sanitize(s.key, 20)}: ${sanitize(s.value, 120)}`)\n }\n const lore = productLore(product)\n if (lore.text) {\n lines.push(`lore: ${sanitize(lore.text, 600)}`)\n for (const src of lore.sources) {\n lines.push(` source: ${sanitize(src.label, 80)} (${sanitize(src.url, 300)})`)\n }\n }\n const content: ImageBlock[] = [{ type: \"text\", text: lines.join(\"\\n\") }]\n // The image: the shown fit's first buyable colour by image METADATA (fit-neutral plp when the fit has\n // no shot of its own), else the thumbnail. A colour with no shot yet (Khaki) never picks another colour's.\n const imgFit = wantedFit ?? (fits.length ? fits[0]! : null)\n const imgColour = colours.length ? (coloursFor(product, imgFit, shipCC)[0] ?? null) : null\n const imageUrl = (imgColour ? imageFor(product, { fit: imgFit, colour: imgColour }) : null) ?? product.thumbnail ?? null\n if (imageUrl) {\n const img = await buildImageContent(imageUrl)\n content.push(...img)\n }\n // A machine variant map so an agent gets an authoritative variant_id + fit/size/colour + price for\n // add_to_cart, instead of guessing from free-text (D2). `size` is the REAL size (never the title).\n const variantMap = (product.variants ?? []).map((v) => {\n const vFit = variantFit(v)\n const vColour = variantColour(v) ?? null\n const vSize = sizeOfVariant(v)\n // The gates judge by the BLANK, so a legacy product (no Fit option — the live catalog today)\n // is judged by its implied fit; otherwise `buyable` would read true for everything.\n const gFit = gateFit(product, vFit)\n return {\n variant_id: v.id,\n fit: vFit,\n size: vSize,\n colour: vColour,\n price: v.calculated_price?.calculated_amount ?? null,\n // Whether add_to_cart will ACCEPT this id for the priced region (money-path review 2026-09-04):\n // the map used to hand out ids for colours the ship-to blank can't print, with the caveat buried\n // in a separate unavailable_colours field. Same derived answer, on the row an agent actually uses.\n // CAT-37 added the SIZE half — the US and EU oversize blanks have different size ranges.\n buyable:\n // TB129 review: judged by the product's OWN garment (a 4711 is not a 5082).\n (!gFit || !vColour || colourBuyable(gFit, vColour, shipCC, supplierOf(product), printfulBlankOf(product))) &&\n (!gFit || !vSize || sizeBuyable(gFit, vSize, shipCC, supplierOf(product), printfulBlankOf(product))),\n }\n })\n const sizeList = colours.length > 0\n ? Object.fromEntries(colours.map((c) => [c, sizesForColour(product, c)]))\n : (product.variants ?? []).map((v) => v.title ?? \"?\")\n return {\n content,\n // G8/D3 — the untrusted backend TEXT (title/readme/specs/lore) is sanitize()'d before it\n // enters the machine-readable path too (the header's promise), not just the text block;\n // ids/urls/numbers pass through redactForOutput unchanged.\n structuredContent: redactForOutput({\n handle: product.handle ?? handle,\n title: sanitize(product.title ?? \"\", 120),\n price: shownPrice.amount ?? v0?.calculated_price?.calculated_amount ?? null,\n price_from: shownPrice.from,\n prices_by_fit: Object.fromEntries(fits.map((f) => [f, priceFor(product, f).amount])),\n currency: shownPrice.currency ?? v0?.calculated_price?.currency_code ?? null,\n tax_inclusive: shownPrice.taxInclusive ?? v0?.calculated_price?.is_calculated_price_tax_inclusive ?? null,\n region: rr.requested ?? null,\n fits,\n fit: wantedFit ?? null,\n colours,\n sizes: sizeList,\n ...(fits.length ? { availability } : {}),\n ...(Object.keys(unavailableColours).length ? { unavailable_colours: unavailableColours } : {}),\n variants: variantMap,\n image_url: imageUrl,\n // the PDP content, machine-readable: readme + the spec sheet + the lore (+ sources).\n readme: sanitize(productReadme(product), 2000),\n specs: specs.map((s) => ({ key: sanitize(s.key, 40), value: sanitize(s.value, 200) })),\n lore: {\n text: sanitize(lore.text ?? \"\", 1200),\n sources: lore.sources.map((s) => ({ label: sanitize(s.label, 120), url: s.url })),\n },\n }) as Record<string, unknown>,\n }\n },\n }\n\n // add_to_cart — create-or-reuse a cart; return the cart_id handle + the server cart.\n const add_to_cart: ToolDef = {\n name: \"add_to_cart\",\n description:\n \"Add a piece to a cart. Pass cart_id to reuse an existing cart (the durable handle), or omit it \" +\n \"to start a fresh one. Call get_product first to get valid sizes/colours (or an exact variant_id); \" +\n \"for a design sold in two fits (regular / oversize, priced apart) you MUST pass `fit`. \" +\n \"size is required when the colour comes in more than one size: ask the buyer, never choose one for them. \" +\n \"qty is capped at 20. Returns the cart_id and the server-side cart summary (VAT included where applicable). \" +\n \"`region` (ISO-2) sets the ship-to country + currency of a NEW cart. A reused cart ships where it already ships: \" +\n \"a `region` that disagrees with it is refused (start a new cart instead). If a reused cart has expired or was \" +\n \"already checked out, a fresh one is made in the same region and the reply says cart_replaced: true.\",\n inputSchema: {\n handle: z.string().optional().describe(\"product handle (resolves a variant by size + colour; call get_product for valid values)\"),\n variant_id: z.string().optional().describe(\"a specific variant id from get_product (overrides handle/size/colour)\"),\n fit: z.string().optional().describe(\"regular or oversize; REQUIRED when the design comes in both (get_product lists them)\"),\n size: z.string().optional().describe(\"size label from get_product (it lists the sizes you can buy where the cart ships); required when the colour comes in more than one size, ask the buyer\"),\n colour: z.string().optional().describe(\"piece colour from get_product, e.g. black / white, or the name shown, e.g. Faded Black (default: the first colour you can buy where the cart ships)\"),\n qty: z.number().int().positive().max(20).optional().describe(\"quantity, 1–20 (default 1)\"),\n cart_id: z.string().optional().describe(\"reuse this cart; omit to create a new one\"),\n region: z.string().optional().describe(\"ISO-2 ship-to country for a NEW cart (e.g. se / us / de); with a cart_id it must match where that cart ships, or nothing is added\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const reuse = typeof args.cart_id === \"string\" && args.cart_id.trim() ? args.cart_id.trim() : undefined\n const explicitCC = (typeof args.region === \"string\" && args.region.trim().toLowerCase()) || undefined\n\n // D1 review (HIGH): a REUSED cart is gated by where THAT cart ships, never by the `region` arg or the server\n // default. Read it first: its shipping address, else its region's first country. A cart we cannot read (404)\n // is replaced, and then the replacement's region is the gate, like a new cart.\n let existing: CartLike | null = null\n if (reuse) {\n try {\n existing = await client.getCart(reuse)\n } catch (e) {\n if (!(e instanceof RecoverableCartError)) throw e\n }\n // TB252 (plan-check 4): a cart that became an order still reads 200. Treat it like a 404: replaced,\n // gated by the region it is recreated in, never written to.\n if (existing?.completed_at) existing = null\n }\n\n let region: RegionLike\n let shipCC: string | null\n if (existing) {\n const to = await cartShipTo(existing, () => client.getRegions())\n if (!to) {\n // fail closed: a cart whose destination we cannot read cannot be gated\n return errorText(`can't tell where cart ${sanitize(reuse!, 60)} ships, so nothing was added. omit cart_id to start a new cart.`)\n }\n if (shipToDisagrees(explicitCC, to)) {\n return errorText(\n `cart ${sanitize(reuse!, 60)} ships to ${sanitize(to.country, 8)}, not ${sanitize(explicitCC!, 8)}. nothing was added. ` +\n `omit region to keep adding to this cart, or omit cart_id to start a new cart for ${sanitize(explicitCC!, 8)}.`\n )\n }\n region = to.region\n shipCC = to.country\n } else {\n // Fail CLOSED on a named ship-to country we don't have a region for — never create a cart in a\n // substituted currency (A1).\n const rr = await client.resolveRegionResult(explicitCC || regionCC)\n if (rr.requested && !rr.matched) {\n return errorText(`we don't ship to ${sanitize(rr.requested, 8)} yet — nothing was added. (list_products with a supported region to see prices.)`)\n }\n region = rr.region\n shipCC = (rr.requested ?? region.countries?.[0]?.iso_2 ?? \"\").toLowerCase() || null\n }\n const qty = typeof args.qty === \"number\" ? args.qty : 1\n\n // resolve the variant id\n let variantId = typeof args.variant_id === \"string\" ? args.variant_id : undefined\n if (variantId) {\n // A caller-supplied variant_id must clear the SAME region/fit gate as a resolved one (money-path\n // review 2026-09-04: this fast-path skipped resolveVariant entirely, so an agent could add a\n // colour the ship-to blank can't print — captured payment, then a held order at fulfillment).\n const owner = (await client.listProducts({ region })).find((p) =>\n (p.variants ?? []).some((v) => v.id === variantId)\n )\n const v = owner?.variants?.find((x) => x.id === variantId)\n if (!owner || !v) {\n return errorText(`no such variant \"${sanitize(variantId, 60)}\" in the catalog. call get_product for a current variant_id. nothing was added.`)\n }\n const vColour = variantColour(v)\n const vSize = sizeOfVariant(v)\n // Judge by the BLANK: a legacy product (no Fit option) contributes its implied fit, or this\n // whole gate no-ops on the catalog that is actually live today.\n const gFit = gateFit(owner, variantFit(v))\n const shipCC2 = shipCC\n // D1: judged by the product's OWN supplier's blank (a Printful product is not a Gildan one).\n const supplier = supplierOf(owner)\n // TB129 review: and by the product's OWN garment (a 4711 judged by the 5082 row refused every valid jet line).\n const blank = printfulBlankOf(owner)\n if (gFit && vColour && !colourBuyable(gFit, vColour, shipCC2, supplier, blank)) {\n return errorText(\n `${sanitize(colourLabel(vColour), 20)} is not available in ${gFit} for ${sanitize(shipCC2 ?? \"this region\", 8)}. nothing was added.`\n )\n }\n // CAT-37 — same gate for the SIZE. A caller-supplied variant_id must clear it too, or an agent\n // could add an Oversize XS for a US cart that Shaka cannot print (paid, then held at fulfillment).\n if (gFit && vSize && !sizeBuyable(gFit, vSize, shipCC2, supplier, blank)) {\n return errorText(\n `size ${sanitize(vSize, 20)} is not made in ${gFit} for ${sanitize(shipCC2 ?? \"this region\", 8)}. nothing was added.`\n )\n }\n }\n if (!variantId) {\n const handle = typeof args.handle === \"string\" ? args.handle : \"\"\n if (!handle) {\n return errorText(\"add_to_cart needs a variant_id or a handle (+ optional size/colour).\")\n }\n const product = await client.getProduct(handle, { region })\n if (!product) {\n return errorText(`no product with handle \"${sanitize(handle, 60)}\".`)\n }\n // A3/CAT-31 — resolve by fit AND size AND colour (DERIVED, never hardcoded), FAIL CLOSED: a two-fit\n // design without `fit` is an error listing the fits + prices (never pick a garment for the buyer);\n // a colour the ship-to country's blank does not offer is an error; a missing combo is a CLEAR\n // error (no substitution). colour defaults to the first colour buyable where the cart ships.\n const size = typeof args.size === \"string\" ? args.size : undefined\n const r = resolveVariant(product, {\n fit: typeof args.fit === \"string\" ? args.fit : undefined,\n size,\n colour: typeof args.colour === \"string\" ? args.colour : undefined,\n countryCode: shipCC,\n })\n if (!r.ok) {\n if (r.reason === \"ambiguous_fit\") {\n const fitList = r.fits.map((f) => `${f.fit} (${fmtPrice(f.amount, f.currency)})`).join(\" · \")\n // CAT-40: state the CHOICE and who makes it. The CLI's equivalent message once named a\n // single fit as the \"fix\" and a live agent staged it without ever asking the buyer.\n return errorText(\n `fit required for \"${sanitize(handle, 60)}\": ${fitList}. these are different garments at ` +\n `different prices — ASK the buyer which fit they want, don't choose for them, then pass ` +\n `fit: \"regular\" or fit: \"oversize\". nothing was added.`\n )\n }\n if (r.reason === \"fit_not_offered\") {\n return errorText(\n `\"${sanitize(handle, 60)}\" isn't sold in fit \"${sanitize(r.requested, 20)}\"` +\n `${r.fits.length ? ` — it comes in ${r.fits.join(\" / \")}` : \"\"}. nothing was added.`\n )\n }\n // M1 (TB252) — a colour the design is not made in AT ALL is its own answer, in the buyer's own word\n // with the real colours as they are passed. Checked before the region gate, which also fails an\n // unknown colour and would call it \"not available\" here (the CLI does the same).\n const colourAsked = \"colour\" in r ? r.colour : null\n const madeIn = coloursFor(product, productFits(product).length > 0 ? (r.fit ?? null) : null)\n if (colourAsked && (r.reason === \"fit_unavailable\" || r.reason === \"not_found\") && !madeIn.includes(colourAsked)) {\n return errorText(\n `\"${sanitize(handle, 60)}\" isn't made in ${sanitize(colourAsked, 20)}. colours: ${madeIn.map((c) => sanitize(c, 20)).join(\" · \")}` +\n ` (pass one as colour). nothing was added.`\n )\n }\n if (r.reason === \"fit_unavailable\") {\n return errorText(\n `${sanitize(colourLabel(r.colour ?? \"\"), 20)} is not available in ${r.fit} for ${sanitize(shipCC ?? \"this region\", 8)}. nothing was added.`\n )\n }\n // CAT-37 — the size exists on the design but the blank that ships HERE isn't made in it.\n if (r.reason === \"size_unavailable\") {\n return errorText(\n `size ${sanitize(r.size, 20)} is not made in ${r.fit} for ${sanitize(shipCC ?? \"this region\", 8)}` +\n `${r.sizes.length ? ` — sizes here: ${r.sizes.map((x) => sanitize(x, 20)).join(\" \")}` : \"\"}. nothing was added.`\n )\n }\n // M1 — the colour exists, the size is what it is not cut in.\n if (size && r.colour && r.sizes.length) {\n return errorText(\n `\"${sanitize(handle, 60)}\" doesn't come in size ${sanitize(size, 20)} in ${sanitize(r.colour, 20)}. ` +\n `sizes: ${sortSizes(r.sizes).map((x) => sanitize(x, 20)).join(\" \")}. nothing was added.`\n )\n }\n const have = r.colours.length ? ` available colours: ${r.colours.map((c) => sanitize(colourLabel(c), 20)).join(\", \")}.` : \"\"\n const haveSizes = r.sizes.length ? ` sizes: ${r.sizes.map((x) => sanitize(x, 20)).join(\" \")}.` : \"\"\n return errorText(\n `no variant for \"${sanitize(handle, 60)}\"${r.fit ? ` fit ${r.fit}` : \"\"}${size ? ` size ${sanitize(size, 20)}` : \"\"}` +\n `${r.colour ? ` (${sanitize(colourLabel(r.colour), 20)})` : \"\"}.${have}${haveSizes}`\n )\n }\n // M2 (TB252) — no size: refuse, never pick one (the first variant was S, silently). After every !r.ok\n // answer, so a fit/colour problem is named first; handle path only, a variant_id names its size.\n // A colour cut in one size has nothing to choose.\n if (!size?.trim()) {\n const sizes = sortSizes(\n r.colour\n ? sizesFor(product, r.fit, r.colour, shipCC)\n : [...new Set((product.variants ?? []).map((v) => sizeOfVariant(v)).filter((s): s is string => !!s))]\n )\n if (sizes.length > 1) {\n return errorText(\n `size required for \"${sanitize(handle, 60)}\": ${sizes.map((x) => sanitize(x, 20)).join(\" \")}` +\n `${r.colour ? ` in ${sanitize(r.colour, 20)}` : \"\"}. ask the buyer which size, don't choose for them, then pass size. nothing was added.`\n )\n }\n }\n variantId = r.variant.id\n }\n\n // create-or-reuse the cart; a stale/completed cart → make a fresh one IN THE SAME REGION (F-B2), and say so.\n const addTo = async (cartId: string): Promise<CartLike> => client.addLineItem(cartId, variantId!, qty)\n let cart: CartLike\n let replaced = !!reuse && !existing // the reused cart could not be read: it is replaced below\n if (existing) {\n try {\n cart = await addTo(reuse!)\n } catch (e) {\n if (!(e instanceof RecoverableCartError)) throw e\n replaced = true\n cart = await addTo((await client.createCart(region)).id)\n }\n } else {\n cart = await addTo((await client.createCart(region)).id)\n }\n const itemCount = (cart.items ?? []).reduce((n, li) => n + (li.quantity ?? 1), 0)\n const replacedNote = replaced\n ? `\\ncart ${sanitize(reuse!, 60)} had expired or was already checked out, so this is a NEW cart${existing ? \" (same region)\" : \"\"}. use the new cart_id from now on.`\n : \"\"\n return result(\n `added.\\ncart_id: ${cart.id} (reuse this handle on the next add / checkout)${replacedNote}\\n${cartSummary(cart, region)}`,\n {\n cart_id: cart.id,\n // only when a cart_id was passed, so a fresh-cart reply is unchanged\n ...(reuse ? { cart_replaced: replaced, ...(replaced ? { replaced_cart_id: reuse } : {}) } : {}),\n total: cart.total ?? null,\n currency: cart.currency_code ?? region.currency_code ?? null,\n item_count: itemCount,\n items: cartItems(cart),\n // A2 — `total` is pre-shipping until checkout attaches a method; flag it for the agent.\n total_excludes_shipping: (cart.shipping_methods ?? []).length === 0,\n }\n )\n },\n }\n\n // get_cart — the authoritative server cart summary.\n const get_cart: ToolDef = {\n name: \"get_cart\",\n description: \"Show a cart's current contents and total (VAT included where applicable), straight from the server.\",\n inputSchema: { cart_id: z.string().describe(\"the cart handle from add_to_cart\") },\n handler: async (args): Promise<ToolResult> => {\n const cartId = String(args.cart_id ?? \"\")\n const region = await client.resolveRegion(regionCC)\n try {\n const cart = await client.getCart(cartId)\n // M3 (TB252): a completed cart still reads 200; listing its items read as \"still in your cart\".\n if (cart.completed_at) {\n return {\n ...errorText(\n \"that cart was already checked out and became an order, so it cannot be edited. omit cart_id on the next add_to_cart to start a new one.\"\n ),\n structuredContent: { completed: true },\n }\n }\n const itemCount = (cart.items ?? []).reduce((n, li) => n + (li.quantity ?? 1), 0)\n return result(cartSummary(cart, region), {\n cart_id: cart.id,\n total: cart.total ?? null,\n currency: cart.currency_code ?? region.currency_code ?? null,\n item_count: itemCount,\n items: cartItems(cart),\n // A2 — signal that `total` is pre-shipping so a chaining agent doesn't quote it as final.\n total_excludes_shipping: (cart.shipping_methods ?? []).length === 0,\n })\n } catch (e) {\n if (e instanceof RecoverableCartError) {\n return errorText(\"that cart is no longer valid (expired or already committed). add an item to start a fresh one.\")\n }\n throw e\n }\n },\n }\n\n // checkout — async handle-ID hand-off. Returns INSTANTLY. NEVER polls / opens a browser /\n // sets an address. Payment details NEVER enter the chat (G8 confused-deputy / F-I4).\n const checkout: ToolDef = {\n name: \"checkout\",\n description:\n \"Mint a checkout hand-off for a cart. Returns a checkout_url + a session_id INSTANTLY — it does \" +\n \"NOT poll and does NOT complete the purchase. The human must open the link in their own browser \" +\n \"to enter their payment + ship-to details on the storefront (those details never enter this chat). \" +\n \"After they pay, call check_payment with the session_id to see the status.\",\n inputSchema: {\n cart_id: z.string().describe(\"the cart handle to check out\"),\n email: z.string().optional().describe(\"the buyer's email for the receipt (optional)\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const cartId = String(args.cart_id ?? \"\")\n const email = typeof args.email === \"string\" ? args.email : undefined\n // BUG 1: the headless checkout route requires the cart to carry a shipping method. Attach\n // one first (idempotent in the client; auto-picks the only/cheapest region option — it\n // sets a SHIPPING method, never a payment/address, so the G8 boundary is untouched). Fail\n // CLOSED: if no option is available we return an error and NEVER mint a checkout link.\n let shipped: CartLike\n try {\n shipped = await client.ensureCartShippingMethod(cartId)\n } catch (e) {\n if (e instanceof RecoverableCartError) {\n return errorText(\"that cart is no longer valid (expired or already committed). add an item to start a fresh one.\")\n }\n return errorText(\"couldn't attach a shipping method to this cart — checkout isn't available for it right now.\")\n }\n // A cart that already became an order (completed_at, or the mint's 409 when it completed between the\n // read and the mint) is never re-minted: say so, and point at check_payment / a fresh cart.\n const completedAnswer = (): ToolResult => ({\n ...errorText(\n \"that cart was already checked out and became an order. call check_payment with its session_id, or omit cart_id on add_to_cart to start a new one.\"\n ),\n structuredContent: { completed: true },\n })\n if (shipped.completed_at) {\n return completedAnswer()\n }\n // mintCheckout is idempotent in the client AND applies the dev-channel 10% server-side. We\n // return its result DIRECTLY — no poll, no browser, no address (the G8 boundary).\n let mint: Awaited<ReturnType<StoreClient[\"mintCheckout\"]>>\n try {\n mint = await client.mintCheckout(cartId, email, \"mcp\")\n } catch (e) {\n if (e instanceof StoreApiError && e.status === 409) {\n return completedAnswer()\n }\n throw e\n }\n const { checkout_url, session_id } = mint\n // A2 — quote the DISCOUNTED, shipping-inclusive total the mint computed (dev-channel 10% off),\n // not the pre-discount cart figure. Fall back to the shipped cart total if an older backend\n // didn't echo one.\n const finalTotal = mint.total ?? shipped.total ?? null\n const finalCurrency = mint.currency ?? shipped.currency_code ?? null\n const totalStr = `${fmtPrice(finalTotal, finalCurrency)}${taxSuffix(isCartTaxInclusive(shipped))}`\n // CAT-44 — itemise what makes up the total instead of asserting one number. The mint total is\n // shipping-inclusive and discount-applied, so the rows have to come from the SHIPPED cart plus\n // the mint's own discount (the dev-channel 10% is applied server-side at mint, never on the cart).\n const moneyLines = cartMoneyRows({\n itemTotal: shipped.item_total,\n discountTotal: mint.discount?.amount,\n discountLabel: \"dev channel\",\n discountNote: mint.discount ? `${mint.discount.pct}% off — for buying from the terminal/agent` : null,\n shippingTotal: shipped.shipping_total,\n hasShippingMethod: (shipped.shipping_methods ?? []).length > 0,\n total: finalTotal,\n })\n .filter((r) => r.kind !== \"total\") // the total is printed on its own line below\n .map((r) => {\n const value = r.display ?? `${r.kind === \"discount\" ? \"−\" : \"\"}${fmtPrice(r.amount, finalCurrency)}`\n return ` ${r.label}: ${value}${r.note ? ` (${r.note})` : \"\"}`\n })\n const body = [\n \"checkout ready. open this link in your browser to pay:\",\n ` ${checkout_url}`,\n ...moneyLines,\n ` total: ${totalStr}`,\n \"\",\n \"payment details are entered on the storefront (Stripe) — they never enter this chat, and\",\n \"I can't complete this for you. once you've paid, call check_payment with:\",\n ` session_id: ${session_id}`,\n \"\",\n // brand-delight #9 — the deadpan flourish. The boundary stays honest (the line above), then\n // one dry true line: an agent can do everything up to the card. The card is the human's.\n \"I staged it, priced it, and walked you to the door. the card swipe is yours — by design.\",\n ].join(\"\\n\")\n // redact defensively (the url/session id carry no secret, but the redactor is the rule).\n return result(redactForOutput(body) as string, {\n checkout_url,\n session_id,\n total: finalTotal,\n currency: finalCurrency,\n discount: mint.discount ?? null,\n // CAT-44 — an agent should be able to itemise the total without re-deriving it.\n item_total: shipped.item_total ?? null,\n shipping_total: shipped.shipping_total ?? null,\n })\n },\n }\n\n // check_payment — poll ONCE; the agent re-calls at its own cadence (async handle-ID, F-I4).\n const check_payment: ToolDef = {\n name: \"check_payment\",\n description:\n \"Check a checkout's payment status ONCE (call again later to re-check — this never blocks/loops). \" +\n \"Returns the status and, once placed, the order_id.\",\n inputSchema: { session_id: z.string().describe(\"the session_id from checkout\") },\n handler: async (args): Promise<ToolResult> => {\n const sessionId = String(args.session_id ?? \"\")\n let res\n try {\n res = await client.poll(sessionId)\n } catch (e) {\n // A bad/expired session id 404s — map it to an actionable line, not a raw \"HTTP 404\" (D5).\n if (e instanceof StoreApiError && e.status === 404) {\n return errorText(\"that checkout session is unknown or expired — mint a fresh one with checkout.\")\n }\n throw e\n }\n // ENG-39: once placed, surface the SETTLED order summary (real total + discount line) so the\n // agent reports what the buyer ACTUALLY paid — never the pre-payment cart estimate.\n const s = res.summary\n const out = redactForOutput({ status: res.status, order_id: res.order_id, summary: s ?? null })\n let human: string\n if (res.status === \"placed\") {\n // \"placed\": the order exists. Say what the status means and no more (D1 review: not \"paid\").\n const lines = [`placed — order ${s?.display_id ? `#${s.display_id}` : \"created\"}. order_id: ${res.order_id ?? \"—\"}`]\n if (s) {\n const cur = s.currency_code\n const vat = taxSuffix(s.is_tax_inclusive === true)\n if (s.item_total != null) lines.push(` subtotal ${fmtPrice(s.item_total, cur)}`)\n if (s.discount_total) {\n const codes = s.discount_codes.length ? ` (${s.discount_codes.join(\", \")})` : \"\"\n lines.push(` discount${codes} −${fmtPrice(s.discount_total, cur)}`)\n }\n if (s.shipping_total != null) lines.push(` shipping ${fmtPrice(s.shipping_total, cur)}`)\n lines.push(` total ${fmtPrice(s.total, cur)}${vat}`)\n }\n human = lines.join(\"\\n\")\n } else {\n human = `status: ${out.status} (call check_payment again in a moment if still pending)`\n }\n return { content: [{ type: \"text\", text: human }], structuredContent: out as Record<string, unknown> }\n },\n }\n\n // The customer's order stage in plain words (store-client orderStage). No production stage exists (Sam 2026-10-02).\n const STAGE_WORDS = {\n committed: \"placed, not shipped yet\",\n shipped: \"shipped\",\n delivered: \"delivered\",\n reverted: \"canceled or refunded\",\n } as const\n\n // get_order_status — status/timeline TEXT only; NO address (F-H3); id + exact-email, 404 on miss (F-H1).\n const get_order_status: ToolDef = {\n name: \"get_order_status\",\n description:\n \"Look up an order's status by its display id + the EXACT email on the order. Returns status and stage \" +\n \"(placed, shipped or delivered; no shipping address). A wrong id/email returns not-found (no enumeration).\",\n inputSchema: {\n display_id: z.string().describe(\"the order's display id (e.g. 1042)\"),\n email: z.string().describe(\"the exact email on the order\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const displayId = String(args.display_id ?? \"\")\n const email = String(args.email ?? \"\")\n // AUD-8: the shared client now POSTs to /store/order-lookup with the email in the BODY\n // (no longer a query string); getOrder still surfaces the 404→null (no-enumeration) contract.\n let looked: unknown | null\n try {\n looked = await client.getOrder(displayId, email)\n } catch (e) {\n if (e instanceof StoreApiError && e.status === 404) {\n looked = null\n } else {\n throw e\n }\n }\n if (!looked) {\n return errorText(`no order matches that id + email.`)\n }\n // Extract ONLY status/timeline fields — NEVER the shipping address (F-H3).\n const order = (looked as { order?: Record<string, unknown> }).order ?? (looked as Record<string, unknown>)\n // Brand rule (Sam 2026-10-02, memory customer-sees-shipped-or-not): the customer learns placed, shipped or\n // delivered, never a production stage. The fulfilment word goes through customerFulfillmentStatus, so an older\n // backend's raw \"fulfilled\" (made, not shipped) reads not_fulfilled; `stage` is the plain-English answer.\n const rawFulfillment = order[\"fulfillment_status\"]\n const fulfillment = customerFulfillmentStatus(typeof rawFulfillment === \"string\" ? rawFulfillment : null)\n const status = sanitize(String(order[\"status\"] ?? fulfillment ?? \"unknown\"), 60)\n const display = sanitize(String(order[\"display_id\"] ?? displayId), 30)\n const stage = STAGE_WORDS[orderStage({ status: String(order[\"status\"] ?? \"\"), fulfillment_status: fulfillment })]\n const lines = [`order #${display}`, `status: ${status}`, `stage: ${stage}`]\n if (fulfillment) {\n lines.push(`fulfillment: ${sanitize(fulfillment, 60)}`)\n }\n // The output is redacted — and we never selected the address, so no PII can leak (G10/F-H3).\n return result(redactForOutput(lines.join(\"\\n\")) as string, {\n display_id: order[\"display_id\"] ?? display,\n status,\n stage,\n fulfillment_status: fulfillment,\n })\n },\n }\n\n // report_issue — file a bug report / feedback to the maintainers (d14). ADDITIVE: it sends NO\n // payment/PII and cannot touch any order — the one way an agent can flag a problem. The message +\n // optional context are redacted SERVER-SIDE before they're emailed (G10); we return a reference.\n const report_issue: ToolDef = {\n name: \"report_issue\",\n description:\n \"File a bug report or feedback to the maintainers. Pass a short message (and optional \" +\n \"structured context like versions/ids). Returns a reference id. It sends no payment details \" +\n \"and cannot change any order — it just files the report. Tokens/emails are redacted server-side.\",\n inputSchema: {\n message: z.string().describe(\"what went wrong / the feedback (one or two sentences)\"),\n context: z\n // zod v4 requires an explicit key type: record(keyType, valueType). (Was record(value) —\n // a pre-existing TS2554 that tsup transpiled past; fixed so `npm run typecheck` is clean.)\n .record(z.string(), z.unknown())\n .optional()\n .describe(\"optional structured context (versions, ids) — tokens/emails are redacted server-side\"),\n },\n handler: async (args): Promise<ToolResult> => {\n const message = String(args.message ?? \"\").trim()\n if (!message) {\n return errorText(\"report_issue needs a non-empty message describing the problem.\")\n }\n const context =\n args.context && typeof args.context === \"object\" && !Array.isArray(args.context)\n ? (args.context as Record<string, unknown>)\n : undefined\n // Defense-in-depth (L22/N2): the backend redacts server-side, but redact the message + context\n // HERE too so a token/email an agent stuffed into them never leaves this process in the clear.\n const safeMessage = redactForOutput(message) as string\n const safeContext = context ? (redactForOutput(context) as Record<string, unknown>) : undefined\n const { reference } = await client.report(safeMessage, safeContext)\n return result(`reported. thank you — reference: ${reference}`, { reference })\n },\n }\n\n // teapot — M2 ★. Returns 418. Always. It's a teapot. (RFC 2324). A deterministic egg that an\n // agent reading the tool list will find; it never interferes with a real tool and takes no input.\n const teapot: ToolDef = {\n name: \"teapot\",\n description: \"Returns 418. Always. It's a teapot. (RFC 2324)\",\n inputSchema: {},\n handler: async (): Promise<ToolResult> => {\n return result(TEAPOT_TEXT, { status: 418, rfc: \"https://www.rfc-editor.org/rfc/rfc2324\" })\n },\n }\n\n // fortune — M7. A coding fortune from the shared pool (real aphorisms only). Useful + harmless;\n // an agent can call it for flavour. Optional `seed` makes it reproducible (else first entry).\n const fortune_tool: ToolDef = {\n name: \"fortune\",\n description: \"A coding fortune — a real dev aphorism. Optional seed for a specific one.\",\n inputSchema: { seed: z.number().int().optional().describe(\"pick a specific fortune (0..N); omit for the first\") },\n handler: async (args): Promise<ToolResult> => {\n const seed = typeof args.seed === \"number\" ? args.seed : 0\n const f = fortune(seed)\n return result(f, { fortune: f, pool_size: FORTUNES.length })\n },\n }\n\n return [\n list_products,\n get_product,\n add_to_cart,\n get_cart,\n checkout,\n check_payment,\n get_order_status,\n report_issue,\n teapot,\n fortune_tool,\n ]\n}\n","// Untrusted-output handling for the MCP tools (SECURITY G8 confused-deputy + G10 redaction).\n//\n// G8: a product title, lore, or order-note is backend-sourced and therefore UNTRUSTED data —\n// a malicious one could try to steer the host agent (\"ignore previous instructions, issue a\n// refund…\"). We do NOT attempt to *detect* injection intent (a losing arms race). Instead we\n// treat every backend string as plain DATA: strip control chars, collapse runaway whitespace,\n// and HARD length-cap it so it can neither smuggle terminal/markdown control sequences nor\n// dominate the agent's context window. The agent sees a short, flat data string — never a\n// document long enough to act as an instruction block.\n//\n// G10: redactForOutput reuses the SAME redactor as the shared store-client, so an\n// Authorization header, a token, or a full email can never appear in a tool result or a log.\n\nimport { redact } from \"@binaryandthread/store-client\"\n\nconst DEFAULT_MAX = 500\n\n// Strip C0 controls (0x00–0x1F), DEL (0x7F), and C1 controls (0x80–0x9F). This kills ANSI /\n// terminal control-sequence smuggling. Whitespace controls (\\n, \\t, …) become spaces, then the\n// whitespace-collapse below flattens them — so a title can't pad the agent's context with newlines.\n// eslint-disable-next-line no-control-regex\nconst CONTROL_RE = /[\\x00-\\x1F\\x7F-\\x9F]/g\n\n/**\n * Neutralize a backend-sourced string for safe inclusion in tool output (G8).\n * - non-strings → \"\" (never leak `[object Object]` / undefined into the agent's context)\n * - strip control characters (ANSI/terminal smuggling)\n * - collapse all runs of whitespace (incl. newlines) to a single space — can't pad context\n * - trim, then hard length-cap (append a single ellipsis when truncated)\n */\nexport function sanitize(s: string, max = DEFAULT_MAX): string {\n if (typeof s !== \"string\") {\n return \"\"\n }\n const stripped = s\n .replace(CONTROL_RE, \" \")\n .replace(/\\s+/g, \" \")\n .trim()\n if (stripped.length <= max) {\n return stripped\n }\n return stripped.slice(0, max) + \"…\"\n}\n\n/**\n * Deep-redact any value before it goes into a tool result or a log (G10). Reuses the\n * store-client redactor: masks Authorization/token/secret keys, strips Bearer tokens from\n * free strings, masks emails. Never throws (handles cyclic input).\n */\nexport function redactForOutput<T>(obj: T): T {\n return redact(obj)\n}\n","// MCP image path — ImageContent + the Blob-URL text floor, SSRF/token-DoS safe (SECURITY G9,\n// CLAUDE.md rule 6, ROBUSTNESS §6 / F-A3).\n//\n// Why BOTH an image block AND a URL text block: Claude renders ImageContent natively on\n// claude.ai / the API, but Claude Code has a base64-bloat bug (#31208, 15–25k tokens/image) and\n// can fail to render it. So the Blob URL is ALWAYS emitted as a text block too — the reliable\n// floor (the buy is never blocked on rendering).\n//\n// SECURITY G9 — the fetch is the dangerous part, so it is gated HARD:\n// 1. host allowlist (reused verbatim from @binaryandthread/store-client — the SAME constant the\n// CLI uses) BEFORE any network call. A non-allowlisted host / look-alike subdomain → no fetch.\n// 2. scheme MUST be https — a `data:` or `http:` URL → no fetch (no inline payload, no plaintext).\n// 3. a byte cap (≤2MB) + a fetch timeout — over-cap or slow → URL-text-only (token-DoS guard).\n// 4. content-type must be an image — refuse to base64 a non-image payload.\n// On ANY of those (or any failure) we degrade to URL-text-only and fetch NOTHING. It NEVER throws.\n\nimport { isAllowedImageHost } from \"@binaryandthread/store-client\"\n\n/** ≤2MB — over this we return the URL only (token-DoS guard, G9). */\nexport const IMAGE_BYTE_CAP = 2 * 1024 * 1024\n/** Fetch timeout in ms — a slow/hung host must not stall the tool (a list/view is read-mostly). */\nexport const IMAGE_FETCH_TIMEOUT_MS = 5000\n\n/** An MCP content block — either an inline image or a text line. */\nexport type ImageBlock =\n | { type: \"image\"; data: string; mimeType: string }\n | { type: \"text\"; text: string }\n\nexport interface BuildImageOpts {\n /** Inject a fetch impl (tests). Defaults to global fetch. */\n fetchImpl?: typeof fetch\n /** Override the byte cap (tests). */\n byteCap?: number\n /** Override the timeout (tests). */\n timeoutMs?: number\n}\n\n/** The URL-text-only floor — always carries the Blob URL so the agent can hand it to the human. */\nfunction urlOnly(blobUrl: string): ImageBlock[] {\n return [{ type: \"text\", text: blobUrl }]\n}\n\n/** The \"we couldn't render it\" floor — still carries the URL (never blocks the buy). */\nfunction unavailable(blobUrl: string): ImageBlock[] {\n return [{ type: \"text\", text: `(image unavailable) ${blobUrl}` }]\n}\n\n/**\n * Build the content blocks for a single product image. Returns the Blob URL text floor in EVERY\n * case; adds an ImageContent block ONLY when the URL is an allowlisted-host https Blob URL that\n * fetches successfully within the byte cap + timeout as a real image. NEVER throws, NEVER fetches\n * a non-allowlisted / data: / non-https URL.\n */\nexport async function buildImageContent(\n blobUrl: string,\n opts: BuildImageOpts = {}\n): Promise<ImageBlock[]> {\n if (typeof blobUrl !== \"string\" || blobUrl.trim().length === 0) {\n return urlOnly(String(blobUrl ?? \"\"))\n }\n\n // (1) + (2): host allowlist + https-only, BEFORE any network call (G9 / rule 6).\n // isAllowedImageHost already returns false for an unparseable URL; we also require https.\n let scheme: string\n try {\n scheme = new URL(blobUrl).protocol\n } catch {\n return urlOnly(blobUrl)\n }\n if (scheme !== \"https:\") {\n return urlOnly(blobUrl) // data:, http:, etc. — never fetch (no SSRF, no plaintext, no inline payload)\n }\n if (!isAllowedImageHost(blobUrl)) {\n return urlOnly(blobUrl) // non-allowlisted host or look-alike subdomain — never fetch\n }\n\n const fetchImpl = opts.fetchImpl ?? (globalThis.fetch as typeof fetch)\n const byteCap = opts.byteCap ?? IMAGE_BYTE_CAP\n const timeoutMs = opts.timeoutMs ?? IMAGE_FETCH_TIMEOUT_MS\n\n // (3) + (4): the gated fetch. Any failure → URL-text floor (never throws — F-A3).\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), timeoutMs)\n try {\n const res = await fetchImpl(blobUrl as unknown as string, { signal: controller.signal })\n if (!res.ok) {\n return urlOnly(blobUrl)\n }\n const mimeType = (res.headers.get(\"content-type\") ?? \"\").split(\";\")[0]!.trim().toLowerCase()\n if (!mimeType.startsWith(\"image/\")) {\n return urlOnly(blobUrl) // refuse to base64 a non-image payload\n }\n // (M4) Reject an over-cap body BEFORE buffering it into memory, when the server declares its size.\n // The post-buffer check below still applies (Content-Length can be absent or wrong) — this just\n // avoids reading a known-too-large body at all.\n const declaredLen = Number.parseInt(res.headers.get(\"content-length\") ?? \"\", 10)\n if (Number.isFinite(declaredLen) && declaredLen > byteCap) {\n return urlOnly(blobUrl)\n }\n const buf = Buffer.from(await res.arrayBuffer())\n if (buf.byteLength === 0 || buf.byteLength > byteCap) {\n return urlOnly(blobUrl) // empty or over-cap → token-DoS guard\n }\n return [\n { type: \"image\", data: buf.toString(\"base64\"), mimeType },\n { type: \"text\", text: blobUrl }, // the reliable floor (bug #31208)\n ]\n } catch {\n return unavailable(blobUrl)\n } finally {\n clearTimeout(timer)\n }\n}\n","// MCP egg copy (VOICE §3.2). The MCP surface is read by AGENTS and the humans driving them, so its\n// eggs live in tool descriptions (devs read schemas) and in deterministic tool payloads. They NEVER\n// interfere with a real tool and never touch money.\n//\n// This mirrors the CLI's governed copy (packages/cli/src/eggs.ts). It is intentionally duplicated:\n// the MCP package composes over store-client only (not the CLI), so each channel owns its copy.\n// Brand voice (memory brand-voice-no-pod-no-tshirt): never \"t-shirt\"/\"tee\"/\"print\"/Gelato.\n\n/** M2 ★ — the bt.teapot tool body. Returns 418. Always. It's a teapot. (RFC 2324) */\nexport const TEAPOT_TEXT = \"418 i'm a teapot. we make clothing, not coffee. (RFC 2324)\"\n\n/** C10 / M7 — the shared fortune pool. REAL dev aphorisms only (gate 1: never invented). */\nexport const FORTUNES: ReadonlyArray<string> = [\n '// \"there are only two hard things in computer science: cache invalidation and naming things.\" — phil karlton',\n '// \"premature optimization is the root of all evil.\" — donald knuth',\n '// \"weeks of programming can save you hours of planning.\"',\n '// \"make it work, make it right, make it fast.\" — kent beck',\n '// \"talk is cheap. show me the code.\" — linus torvalds',\n '// \"given enough eyeballs, all bugs are shallow.\" — linus\\'s law',\n '// \"always code as if the person maintaining it is a violent psychopath who knows where you live.\" — john woods',\n '// \"it works on my machine.\"',\n]\n\n/** Deterministic fortune by seed (reproducible for tests + screenshots). */\nexport function fortune(seed = 0): string {\n const n = Number.isFinite(seed) ? Math.abs(Math.floor(seed)) : 0\n return FORTUNES[n % FORTUNES.length]!\n}\n","// Decode a JWT's `exp` claim LOCALLY (no signature verification — display/preflight only). Mirror\n// of packages/cli/src/jwt.ts (ENG-22 reuses the CLI pattern; the two packages don't import each\n// other — store-client is their only shared dep). Purely informational: never an authorization\n// decision (that's the backend's job).\n\nexport interface JwtExpiry {\n /** ISO timestamp the token expires, or null if no/invalid token or no exp claim. */\n expiresAt: string | null\n /** true if expired, false if still valid, null if unknown (no/invalid token / no exp). */\n expired: boolean | null\n}\n\nexport function jwtExpiry(jwt: string | null | undefined, now: number = Date.now()): JwtExpiry {\n if (!jwt) {\n return { expiresAt: null, expired: null }\n }\n const parts = jwt.split(\".\")\n if (parts.length < 2 || !parts[1]) {\n return { expiresAt: null, expired: null }\n }\n try {\n const b64 = parts[1].replace(/-/g, \"+\").replace(/_/g, \"/\")\n const payload = JSON.parse(Buffer.from(b64, \"base64\").toString(\"utf8\")) as { exp?: unknown }\n const exp = typeof payload.exp === \"number\" ? payload.exp : null\n if (exp == null) {\n return { expiresAt: null, expired: null }\n }\n const expMs = exp * 1000\n return { expiresAt: new Date(expMs).toISOString(), expired: expMs <= now }\n } catch {\n return { expiresAt: null, expired: null }\n }\n}\n","// Resolve the MCP server's transport config from the environment, falling back to the baked-in\n// PUBLIC production defaults so a host MCP config needs ZERO env to reach the live store\n// (d2 / P4.5-7b). Env ALWAYS wins (set BT_STORE_URL / BT_PUBLISHABLE_KEY for local dev / staging).\n// The JWT (BT_TOKEN) and region (BT_REGION) stay env-only — never baked, never read from chat (T8).\n\nimport { BT_DEFAULT_STORE_URL, BT_DEFAULT_PUBLISHABLE_KEY } from \"@binaryandthread/store-client\"\nimport { jwtExpiry } from \"./jwt\"\n\nexport interface ResolvedServerConfig {\n baseUrl: string\n publishableKey: string\n jwt?: string\n region?: string\n}\n\nexport function resolveServerConfig(\n env: NodeJS.ProcessEnv = process.env,\n // stderr by default — MCP stdio purity: stdout carries protocol frames only.\n warn: (message: string) => void = (m) => console.error(m)\n): ResolvedServerConfig {\n let jwt = env.BT_TOKEN || undefined\n if (jwt) {\n // ENG-22: preflight the token's exp at boot — a stale BT_TOKEN in a host's MCP config otherwise\n // fails opaque on the first authed call. Expired → warn + start unauthenticated (browsing and\n // guest checkout still work). Undecodable exp → KEEP it (the backend stays the authority).\n const { expired, expiresAt } = jwtExpiry(jwt)\n if (expired === true) {\n warn(\n `bt-mcp: BT_TOKEN is expired (since ${expiresAt}) — starting unauthenticated. ` +\n \"re-run `bt login` and update the token in your MCP config.\"\n )\n jwt = undefined\n }\n }\n return {\n baseUrl: env.BT_STORE_URL || BT_DEFAULT_STORE_URL,\n publishableKey: env.BT_PUBLISHABLE_KEY || BT_DEFAULT_PUBLISHABLE_KEY,\n jwt,\n region: env.BT_REGION || undefined,\n }\n}\n","// npx @binaryandthread/mcp — the stdio entrypoint.\n//\n// Config comes from the ENVIRONMENT only (SECURITY T8/§4); all of it has a baked-in default so\n// the server reaches the live store with ZERO env (d2 / P4.5-7b):\n// - BT_STORE_URL the Medusa Store API base URL (default: prod; override for dev)\n// - BT_PUBLISHABLE_KEY the publishable API key — PUBLIC by design (default: prod; T6)\n// - BT_TOKEN the customer JWT (OPTIONAL; from the OS keychain / a configured env)\n// - BT_REGION an ISO-2 country code to resolve pricing (OPTIONAL; never hardcoded)\n//\n// The JWT is read from the env the host MCP config injects (which a keychain helper can populate)\n// — it is NEVER read from the chat (T8). A secret key is rejected by the StoreClient (T6). On\n// stdio, NOTHING is written to stdout except MCP protocol frames — diagnostics go to stderr.\n\nimport { StoreClient } from \"@binaryandthread/store-client\"\nimport { redact } from \"@binaryandthread/store-client\"\nimport { startServer } from \"./server\"\nimport { makeTools } from \"./tools\"\nimport { resolveServerConfig } from \"./config\"\n\nasync function main(): Promise<void> {\n const { baseUrl, publishableKey, jwt, region } = resolveServerConfig()\n\n const client = new StoreClient({\n baseUrl,\n publishableKey,\n jwt,\n // All client logs route to stderr through redact() — stdout is reserved for the transport,\n // and no token/PII ever reaches a log line (G10).\n logger: {\n info: (...a) => process.stderr.write(JSON.stringify(redact(a)) + \"\\n\"),\n warn: (...a) => process.stderr.write(JSON.stringify(redact(a)) + \"\\n\"),\n error: (...a) => process.stderr.write(JSON.stringify(redact(a)) + \"\\n\"),\n },\n })\n\n const tools = makeTools(client, { region })\n await startServer(tools)\n process.stderr.write(`bt-mcp: ready on stdio — ${tools.length} tools registered.\\n`)\n}\n\nmain().catch((e) => {\n // Redact before printing — never leak a token in a crash trace.\n process.stderr.write(`bt-mcp: fatal: ${JSON.stringify(redact(String(e?.message ?? e)))}\\n`)\n process.exit(1)\n})\n"],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AAQ,OAAO,MAAM,kBAAkB,KAAK,UAAU,OAAO,OAAO,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;AAAA,CAAI;AAC1F,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":["out","v","stage","v"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@binaryandthread/mcp",
3
3
  "mcpName": "com.binaryandthread/store",
4
- "version": "0.4.14",
4
+ "version": "0.4.15",
5
5
  "description": "Binary & Thread MCP server — buy a piece from inside your AI agent. 10 deterministic, read-mostly tools over the shared Store API client; the card never enters the chat and the agent can never self-complete a purchase.",
6
6
  "private": false,
7
7
  "type": "module",