# CoffeeShot SDK Contract This is the public contract for the headless CoffeeShot browser SDK. CoffeeShot provides camera framing guidance and still-photo analysis. The SDK is intentionally smaller than Dictum SDK. It does not include speech-to-text, WebSockets, identity, or Workers. ## CDN quick start The preferred host integration loads the awaitable Cloudflare Pages module: ```js const sdkOrigin = 'https://coffeeshot-sdk.pages.dev'; const { checkCompatibility, createClient } = await import( `${sdkOrigin}/coffeeshot-sdk.mjs` ); const compatibility = checkCompatibility(); if (!compatibility.supported) { throw new Error(compatibility.reason ?? 'Camera is unavailable.'); } const client = createClient({ apiUrl: 'https://api.example.com', // optional analyzePath: '/analyze', previewElement: '#preview', overlay: true, locale: 'fr', }); client.on('*', (event) => console.log(event.type, event.data)); await client.start(); await client.flip(); const still = await client.capture(); // kicks analyze; result arrives as `result` await client.stop(); client.destroy(); ``` A host can also copy `dist/coffeeshot-sdk.mjs` next to its own assets and import that file. No secrets belong in the browser bundle. The API origin is host-provided config. ## Exports The bundle entry (`src/index.ts` → `dist/coffeeshot-sdk.mjs`) exports: | Name | Kind | | --- | --- | | `checkCompatibility` | function | | `createClient` | function | | `CoffeeShotError` | class | | `FRAMING_GUIDES` | constant | TypeScript declarations are emitted next to the bundle as `dist/sdk.d.ts`. ## `checkCompatibility()` Synchronous. Does not request the camera or create a client. ```ts type CompatibilityReport = { supported: boolean; reason?: 'not_browser' | 'insecure_context' | 'no_media_devices'; cameras?: number; }; ``` - `supported: true` when `window.isSecureContext` and `navigator.mediaDevices.getUserMedia` exist. - `cameras` is omitted here (device enumeration is async). The `ready` event and `start()` result include a camera count when the browser reports one. ## `createClient(config?)` Returns a reusable `CoffeeShotClient`. Construction is synchronous and does not call `getUserMedia`. ```ts type CoffeeShotClientConfig = { apiUrl?: string; // backend origin; mock analysis when omitted analyzePath?: string; // default `/analyze` previewElement?: HTMLVideoElement | string; overlay?: boolean | HTMLElement | string; locale?: 'fr' | 'en'; // default `fr` source?: string; // multipart field; default `coffeeshot-sdk` quality?: number; // JPEG 0–1; default `0.85` hintIntervalMs?: number; // default `3200` }; ``` Invalid `locale`, `quality`, or `hintIntervalMs` throw `CoffeeShotError` with code `invalid_config`. ## Client lifecycle ``` idle → starting → previewing ⇄ flipping ↘ capturing → analyzing → previewing previewing / analyzing → stopped any live state → destroyed ``` | Method | Behavior | | --- | --- | | `start()` | Request the camera (unless `{ demo: true }`), attach the preview, start hint rotation, emit `ready`. | | `flip()` | Stop every track on the current `MediaStream`, remount the preview, open the other facing. Never leaves a dead stream. | | `capture()` | Encode a JPEG still from the live video (or a generated demo still). Returns `{ blob, uri }` and **starts** `analyze()` without blocking on it. | | `analyze(blob?)` | Multipart POST to `{apiUrl}{analyzePath}` or a local mock. Uses the last capture when no input is given. | | `stop()` | Stop tracks, detach `srcObject`, stop hints, and unmount the optional overlay. The client can `start()` again. | | `destroy()` | `stop()` plus overlay teardown, listener removal, and object-URL revoke. Further commands throw `destroyed`. | Commands are serialized on one client. A second overlapping command rejects with `busy` only if it races the in-flight lock; queued calls wait. `start()` is idempotent while already `starting` / `previewing`. ## Camera and flip rules - Preview is an existing host `