# @sensiblestats/widget-sdk

Headless browser SDK for the SensibleStats chat widget gateway. Zero dependencies, typed.

## Install

`yarn add @sensiblestats/widget-sdk`

## Usage

```ts
import { StatsWidgetClient } from '@sensiblestats/widget-sdk'

const client = new StatsWidgetClient({ operatorId: 'sandbox1', publicKey: 'wgk_…' })

// Streaming chat — iterate
for await (const ev of client.chat({ message: 'odds for Arsenal?' })) {
  if (ev.type === 'answer') console.log(ev.data.text)
}

// Streaming chat — callbacks (await to run to completion)
await client.chat({ message: 'odds for Arsenal?' }, { onAnswer: a => render(a.text) })

// Multi-turn
const convo = client.conversation({ operatorUserId: '6f1c2a9e-4b7d-4e2a-9c3f-8a5d1e7b2c40' })
await convo.send({ message: 'and their recent form?' })

// Signals
await client.sendSignal({ signalType: 'MatchPageView', entityType: 'Match', entityName: 'Arsenal vs Chelsea', operatorUserId: '6f1c2a9e-4b7d-4e2a-9c3f-8a5d1e7b2c40' })
```

Visitor ids (`operatorUserId`, `anonymousUserId`) are passed to the SDK as before. On the wire they travel in the `X-SS-Operator-User-Id` and `X-SS-Anonymous-User-Id` request headers on every gateway call, never in the URL or the JSON body; a blank id sends no header. Ids must be printable ASCII, at most 128 characters (surrounding spaces are trimmed); an id that does not qualify is not sent, as if it were blank.

`operatorUserId` is sent from the browser: use a random, non-guessable value you map to the user on your side (a UUID works), never an account number, email, username or sequential id.

Page grounding: `ChatInput.page` is a `PageContext` (`{ sport: 'football' | 'basketball' | 'americanfootball' | 'tennis', kind?: 'section' | 'competition' | 'match', matchId?, competitionId? }`). Send it on the session-start greeting only; the server ignores it on any other request. `ChatGrounding.sport` names the sport of a tapped grounding. Greeting dashboard tiles may carry `meta` (a short line in the hero's top row where a price would go) and `imageCodes` (team abbreviations drawn as text marks when there are no images).

Errors are typed: `AuthError` (401), `ForbiddenError` (403), `RateLimitError` (429), `UpstreamError` (5xx), `NetworkError`, `ParseError`.
