# Live STT WebSocket protocol -- `wss://.../v1/stream`

OpenAPI can't express WebSockets; this document is the contract for SeaMeet's
live-transcription relay on the Media/STT Worker. Today its clients are the
SeaMeet desktop app and other first-party device apps.

## Connect

```
wss://seameet-stt-proxy.seameet.workers.dev/v1/stream?lang=<iso-639-1, optional>
Authorization: Bearer <SeaMeet user session token>
```

Auth is the standard `Authorization` header on the upgrade request --
Node/Electron and React Native WebSocket clients can set it; **browser
`WebSocket` cannot set headers**, so the web library does NOT do live STT
(by design -- capture is a device feature). Personal API keys are not accepted
here. Entitlement: the same Sync Pro check as the Sync API (an active or
trialing Sync Pro plan with AI enabled).

The upgrade itself is refused with a plain HTTP status when the session cannot
start: 401 (no or invalid session), 403 (not entitled), 429 (the account
already has as many live sessions as its plan allows -- wait 120 s before
retrying) or 503 (no transcription provider is available right now).

The speech-to-text provider is chosen server-side -- clients never pick. The
`lang` query hint takes part in that choice.

## Client -> server frames

| Frame | Encoding | Meaning |
|---|---|---|
| Binary | PCM 16-bit LE, 16 kHz, mono | Audio; send in ~100-250 ms chunks |
| Text `{"type":"finalize"}` | JSON | Flush upstream + emit remaining finals; server closes when drained |

## Server -> client frames (JSON text)

| `type` | Payload | Meaning |
|---|---|---|
| `ready` | `provider` | Upstream relay connected (`provider` is informational). Audio sent before it is not lost: the server holds the most recent 10 s and replays it in order, so a client may stream from open. Time 0 is the first frame the client sent (unless the dial outlasted those 10 s) |
| `segment` | `segment: TranscriptSegment` | One transcript segment: `{text, speaker?, language?, confidence?, startMs?, endMs?, isFinal}`. `isFinal` distinguishes interim (replaces the previous interim) from committed text. `startMs`/`endMs` are ms from this connection's time 0. `startMs` is always set; on some sessions the utterance already in progress when the session opened arrives with `startMs: 0` |
| `error` | `error` (code string) | Fatal; socket closes next. Notable codes: `all_providers_unavailable` |
| `relay_roll` | -- | Planned roll; close 1012 `relay-roll` follows. Reconnect at once -- not a failure |
| `session_too_long` | `capMs` | Per-session cap reached; close 1011 follows. Terminal: don't reconnect |
| `free_exhausted` | `capMs` | Free transcription pool spent; close 1000 follows. Terminal: don't reconnect |

## Lifecycle & limits

- One session = one recording; transcription time is metered per session.
- Reconnecting: after an unexpected close, reconnect once quickly (about 1 s),
  then back off slowly while the recording is still running -- for example
  5 s / 15 s / 30 s, then every 60 s. Slow on purpose: a dropped session can
  hold its concurrency slot for up to 120 s, so fast retries make an outage
  worse; after a 429 on the upgrade, wait the full 120 s. Stop on the
  terminal states above (`session_too_long`, `free_exhausted`). A
  `relay_roll` is not a failure: reconnect at once.
- A reconnect is a new session -- no replay. Its times start again at 0, so
  shift them by the audio you sent before it to keep one timeline. At every
  seam, first commit the in-flight interim as a final; otherwise the next
  session's first interim replaces it and those words are lost.
- **Planned closes**: the control frame (`relay_roll`, `session_too_long`,
  `free_exhausted`), then the close. Rolls come on a regular interval (about
  every 30 min) and whenever the server's periodic re-check sees the plan
  change or the service needs to move sessions; the reconnect then meets the
  gate's new verdict. On a finals-only session (for example `lang=auto`) the
  server first flushes the unfinished segment for up to 4 s: segments keep
  arriving, and audio sent meanwhile is dropped. A client that hasn't
  completed the close handshake 10 s after the close is dropped anyway.
- A close 1000 with **no** control frame before it means the server ended the
  session unplanned, usually because the provider session closed or errored
  (for example after several seconds with no audio). Reconnect for a fresh
  session.
- Concurrency is capped and counted per account (a slot held on a 120 s
  lease, refreshed while the session is alive); `GET /v1/health` exposes live
  session counts. File transcription shares the same capacity, so a stuck
  live slot can also delay it.
