File size: 20,386 Bytes
0ef8bc0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | /**
* callSocket.ts β typed transport for the voice_call WebSocket.
*
* Owns the connection, the envelope codec, monotonic sequence
* tracking, the heartbeat, graceful shutdown, and reconnect-with-
* backoff semantics. Exposes a narrow event surface that the React
* hook (useCallSession) binds to; callers never touch the raw socket.
*
* Explicit state machine β the only transitions allowed are:
*
* idle ββconnect()βββΆ connecting ββopenββββββββΆ live
* β β
* β (handshake failure) β (transient drop w/ resume window)
* βΌ βΌ
* closed reconnecting ββ(give up / expired)βββΆ closed
* β
* ββββββ(socket open)ββββββββΆ live
*
* live ββclose('user_ended' | 'max_duration' | 'idle')ββββΆ closed
*
* Industry choices baked in:
* β’ Exponential backoff with full jitter (AWS / Cloudflare canonical
* implementation) β stops thundering herds on a shared-backend
* brown-out.
* β’ Heartbeat treats silence as liveness failure β we send a
* client-originated `call.control ping` every N seconds AND
* watch for server-side `ping` events; if BOTH are silent for
* 2Γ the interval, the connection is considered dead and the
* backoff loop fires.
* β’ Outbound envelopes are queued while reconnecting. When the
* socket reopens within the resume window the queue is flushed
* in-order; otherwise it's dropped on shutdown.
* β’ Resume semantics honoured: one reconnect attempt carries the
* same session_id + resume_token; if the server returns
* 1008 resume-expired we give up and surface 'resume_expired'.
* β’ Every event emitted by this module is typed; no `any`, no
* ad-hoc payload parsing inside listeners.
*/
// ββ Typed envelope contracts βββββββββββββββββββββββββββββββββββββββ
// Mirror the backend ws.py contract. Keep in sync with
// docs/analysis/voice-call-human-simulation-design.md Β§ 6.
export type CallLifecycleStatus =
| 'idle'
| 'connecting'
| 'live'
| 'reconnecting'
| 'draining'
| 'closed'
export type CallCloseReason =
| 'user_ended'
| 'max_duration'
| 'idle_timeout'
| 'resume_expired'
| 'bad_resume_token'
| 'session_not_found'
| 'session_ended'
| 'websocket_disabled'
| 'network_error'
| 'unmounted'
| 'unknown'
export interface ServerEnvelope<
T extends string = string,
P extends Record<string, unknown> = Record<string, unknown>,
> {
type: T
seq: number
ts: number
payload: P
}
// Server β client event payloads. One type per server `type`; if
// the backend ever adds a new event, it lands in `unknown` and is
// dropped β not a runtime crash.
export interface CallStatePayload {
status: 'live' | 'ending' | 'ended'
reason?: 'user_ended' | 'idle' | 'max_duration'
since?: number
}
export interface AssistantTranscriptPayload {
role: 'assistant'
text: string
}
export interface AssistantFillerPayload {
token: string
ts?: number
session_id?: string
}
export interface AssistantBackchannelPayload {
token: string
volume_db?: number
ts?: number
}
export interface ServerErrorPayload {
code: string
message: string
}
// Phase 2/3 payloads (Β§ 3.2 of the streaming design doc).
export interface AssistantPartialPayload {
turn_id: string
/** The NEW text only. Callers concatenate deltas per turn_id. */
delta: string
/** Monotonic per-turn index starting at 0. Advisory (the WS
* already enforces monotonic seq); useful for replay logs. */
index: number
}
export interface AssistantTurnEndPayload {
turn_id: string
reason: 'complete' | 'cancelled' | 'error'
/** Authoritative concatenation of this turn's deltas (server-side).
* Clients can use this directly instead of maintaining their own
* buffer. Optional in principle β but our server always emits it. */
full_text?: string
}
export interface AssistantCancelPayload {
turn_id: string
cause?: 'user_barge_in' | 'user_partial' | string
}
// Client β server. Keep the types literal so the compiler catches
// typos at the send() call site.
export interface UiStatePayload {
muted?: boolean
speaker_on?: boolean
backgrounded?: boolean
}
export interface TranscriptFinalPayload {
text: string
model?: string
lang?: string
}
export type CallControlAction = 'end' | 'ping'
// ββ Typed event bus for callers ββββββββββββββββββββββββββββββββββββ
export interface CallSocketEventMap {
statusChange: CallLifecycleStatus
callState: CallStatePayload
/** Still fires for turns that ran in unary mode (flag off OR
* capability mismatch). Streaming turns use the three events
* below instead. */
assistantTranscript: AssistantTranscriptPayload
/** Phase 2: one delta per server emission. */
assistantPartial: AssistantPartialPayload
/** Phase 2: exactly once per streamed turn, with a final reason. */
assistantTurnEnd: AssistantTurnEndPayload
/** Phase 3: server acks a barge-in; client stops TTS on receipt. */
assistantCancel: AssistantCancelPayload
assistantFiller: AssistantFillerPayload
assistantBackchannel: AssistantBackchannelPayload
serverError: ServerErrorPayload
safetyNotice: Record<string, unknown>
pong: void
closed: { reason: CallCloseReason; code?: number; detail?: string }
}
type Listener<E extends keyof CallSocketEventMap> = (
payload: CallSocketEventMap[E],
) => void
// ββ Backoff helper (AWS-style full jitter) βββββββββββββββββββββββββ
function backoffMs(attempt: number, baseMs = 500, capMs = 10_000): number {
const exp = Math.min(capMs, baseMs * 2 ** attempt)
return Math.floor(Math.random() * exp)
}
// ββ Close-code β reason mapping ββββββββββββββββββββββββββββββββββββ
const POLICY_VIOLATION = 1008
const NORMAL_CLOSURE = 1000
function closeReasonFrom(code: number | undefined, text: string): CallCloseReason {
const reason = (text || '').toLowerCase()
if (code === POLICY_VIOLATION) {
if (reason.includes('resume-expired')) return 'resume_expired'
if (reason.includes('bad-resume-token')) return 'bad_resume_token'
if (reason.includes('session-not-found')) return 'session_not_found'
if (reason.includes('session-ended')) return 'session_ended'
if (reason.includes('websocket-disabled')) return 'websocket_disabled'
}
if (code === NORMAL_CLOSURE && reason.includes('max-duration')) return 'max_duration'
if (reason.includes('idle')) return 'idle_timeout'
return 'unknown'
}
// ββ Construction options βββββββββββββββββββββββββββββββββββββββββββ
export interface CallSocketOptions {
/** Fully-resolved WS URL including ?resume_token=β¦ and ?token=β¦. */
url: string
/** How long the backend will honour a resume. Used to decide when to
* give up reconnecting. Default 20 s (matches VOICE_CALL_RESUME_WINDOW_SEC). */
resumeWindowSec?: number
/** How often we send a client-originated ping. Default 15 s. */
heartbeatIntervalMs?: number
/** Dependency injection for tests β defaults to global WebSocket. */
webSocketImpl?: typeof WebSocket
/** Structured log sink. Defaults to console.{info,warn,error} with
* any `resume_token`, `token`, or `jwt` fields redacted by the
* caller. Keep string-typed; don't pass raw envelopes in. */
log?: {
info: (msg: string, extra?: Record<string, unknown>) => void
warn: (msg: string, extra?: Record<string, unknown>) => void
error: (msg: string, extra?: Record<string, unknown>) => void
}
}
// ββ CallSocket class βββββββββββββββββββββββββββββββββββββββββββββββ
export class CallSocket {
private ws: WebSocket | null = null
private status: CallLifecycleStatus = 'idle'
private lastServerSeq = 0
private outboundQueue: string[] = []
private heartbeatTimer: number | null = null
private lastServerActivityMs = 0
private deadPeerTimer: number | null = null
private reconnectAttempt = 0
private reconnectTimer: number | null = null
private connectedAtMs = 0
private shuttingDown = false
private readonly listeners: {
[K in keyof CallSocketEventMap]?: Set<Listener<K>>
} = {}
private readonly url: string
private readonly resumeWindowMs: number
private readonly heartbeatIntervalMs: number
private readonly WebSocketImpl: typeof WebSocket
private readonly log: NonNullable<CallSocketOptions['log']>
constructor(opts: CallSocketOptions) {
this.url = opts.url
this.resumeWindowMs = (opts.resumeWindowSec ?? 20) * 1000
this.heartbeatIntervalMs = opts.heartbeatIntervalMs ?? 15_000
this.WebSocketImpl = opts.webSocketImpl ?? WebSocket
this.log = opts.log ?? {
info: (m, e) => console.info(`[callSocket] ${m}`, e ?? ''),
warn: (m, e) => console.warn(`[callSocket] ${m}`, e ?? ''),
error: (m, e) => console.error(`[callSocket] ${m}`, e ?? ''),
}
}
// Public API ----------------------------------------------------
getStatus(): CallLifecycleStatus { return this.status }
on<E extends keyof CallSocketEventMap>(evt: E, fn: Listener<E>): () => void {
let set = this.listeners[evt] as Set<Listener<E>> | undefined
if (!set) {
set = new Set<Listener<E>>()
// Narrow through `as unknown` β TS can't track the generic
// discriminant through the indexed property set.
;(this.listeners as Record<string, unknown>)[evt] = set
}
set.add(fn)
return () => set!.delete(fn)
}
/** Open the socket. Safe to call exactly once per instance. */
connect(): void {
if (this.status !== 'idle') {
this.log.warn('connect() called in non-idle state', { status: this.status })
return
}
this.openSocket()
}
/** Send the captured user transcript as a chat turn. Queued if the
* socket is currently mid-reconnect. */
sendTranscript(p: TranscriptFinalPayload): void {
this.enqueueLine({ type: 'transcript.final', ts: Date.now(), payload: p })
}
/** Bookkeeping β muted, speaker on/off, app backgrounded. */
sendUiState(p: UiStatePayload): void {
this.enqueueLine({ type: 'ui.state', ts: Date.now(), payload: p })
}
/** Control channel β 'ping' requests a pong; 'end' terminates. */
sendControl(action: CallControlAction): void {
this.enqueueLine({ type: 'call.control', ts: Date.now(), payload: { action } })
}
/** Phase 2 β interim STT output while the user is still speaking.
* Server uses it as a secondary barge-in trigger and, in a
* future phase, for semantic endpointing. Safe no-op against a
* non-streaming server (unknown type is dropped). */
sendTranscriptPartial(p: { text: string; stable_prefix_len?: number }): void {
this.enqueueLine({
type: 'transcript.partial',
ts: Date.now(),
payload: p,
})
}
/** Phase 3 β explicit interrupt. Fired the instant the client's
* VAD trips above threshold while an assistant turn is in-flight.
* ``turn_id`` disambiguates against a stale signal racing a new
* turn; the server compares ids and silently drops mismatches. */
sendBargeIn(turn_id: string): void {
this.enqueueLine({
type: 'user.barge_in',
ts: Date.now(),
payload: { turn_id },
})
}
/** Graceful user-initiated end. Sends control 'end', then closes. */
end(): void {
if (this.shuttingDown) return
this.shuttingDown = true
this.transition('draining')
this.sendControl('end')
// Give the server ~400 ms to echo call.state {status:'ended'}
// before we force-close; prevents the socket from looking
// orphaned in backend logs.
window.setTimeout(() => this.dispose('user_ended'), 400)
}
/** Teardown without a control 'end' β used when the component
* unmounts, the user navigates away, or a terminal error occurs. */
dispose(reason: CallCloseReason = 'unmounted', code = NORMAL_CLOSURE): void {
if (this.status === 'closed') return
this.shuttingDown = true
this.clearTimers()
try { this.ws?.close(code, reason) } catch { /* already closed */ }
this.ws = null
this.outboundQueue = []
this.transition('closed')
this.emit('closed', { reason, code })
}
// Socket lifecycle ----------------------------------------------
private openSocket(): void {
this.transition(this.reconnectAttempt === 0 ? 'connecting' : 'reconnecting')
let ws: WebSocket
try {
ws = new this.WebSocketImpl(this.url)
} catch (err) {
this.log.error('WebSocket constructor threw', { err: String(err) })
this.scheduleReconnect()
return
}
this.ws = ws
this.lastServerActivityMs = Date.now()
ws.addEventListener('open', () => {
this.reconnectAttempt = 0
this.connectedAtMs = Date.now()
this.transition('live')
this.startHeartbeat()
this.flushQueue()
})
ws.addEventListener('message', (ev) => this.onMessage(ev))
ws.addEventListener('close', (ev) => this.onClose(ev))
ws.addEventListener('error', () => {
// The browser fires `error` right before `close` on network
// failures. We don't transition here β onClose() decides
// reconnect vs terminal based on the code.
this.log.warn('socket error event')
})
}
private onMessage(ev: MessageEvent): void {
this.lastServerActivityMs = Date.now()
if (typeof ev.data !== 'string') return
let env: ServerEnvelope | null = null
try {
env = JSON.parse(ev.data) as ServerEnvelope
} catch {
this.log.warn('non-JSON frame dropped')
return
}
if (!env || typeof env.type !== 'string') return
// Monotonic seq validation. If the server ever ships out-of-order
// frames we log once and keep going β correctness trumps strictness.
if (typeof env.seq === 'number' && env.seq <= this.lastServerSeq) {
this.log.warn('non-monotonic seq', {
got: env.seq, last: this.lastServerSeq, type: env.type,
})
} else if (typeof env.seq === 'number') {
this.lastServerSeq = env.seq
}
this.dispatch(env)
}
private dispatch(env: ServerEnvelope): void {
const raw = env.payload as unknown
switch (env.type) {
case 'call.state': {
const p = raw as CallStatePayload
this.emit('callState', p)
if (p.status === 'ended') this.dispose('user_ended')
return
}
case 'transcript.final':
this.emit('assistantTranscript', raw as AssistantTranscriptPayload)
return
case 'assistant.partial':
this.emit('assistantPartial', raw as AssistantPartialPayload)
return
case 'assistant.turn_end':
this.emit('assistantTurnEnd', raw as AssistantTurnEndPayload)
return
case 'assistant.cancel':
this.emit('assistantCancel', raw as AssistantCancelPayload)
return
case 'assistant.filler':
this.emit('assistantFiller', raw as AssistantFillerPayload)
return
case 'assistant.backchannel':
this.emit('assistantBackchannel', raw as AssistantBackchannelPayload)
return
case 'safety.notice':
this.emit('safetyNotice', env.payload)
return
case 'error':
this.emit('serverError', raw as ServerErrorPayload)
return
case 'pong':
this.emit('pong', undefined as void)
return
case 'ping':
// Server heartbeat β immediately echo to keep the liveness
// counters tight on both ends.
this.sendControl('ping')
return
default:
// Forward-compat: unknown event types are no-ops.
this.log.info(`unknown server event: ${env.type}`)
}
}
private onClose(ev: CloseEvent): void {
const reason = closeReasonFrom(ev.code, ev.reason)
this.clearTimers()
// Terminal close codes: don't reconnect.
const terminal: ReadonlySet<CallCloseReason> = new Set([
'user_ended',
'max_duration',
'resume_expired',
'bad_resume_token',
'session_not_found',
'session_ended',
'websocket_disabled',
])
if (this.shuttingDown || terminal.has(reason)) {
this.ws = null
this.transition('closed')
this.emit('closed', { reason, code: ev.code, detail: ev.reason })
return
}
// Transient β try to reconnect within the resume window.
this.log.warn('socket dropped; scheduling reconnect', {
code: ev.code, reason: ev.reason,
})
this.ws = null
this.scheduleReconnect()
}
private scheduleReconnect(): void {
if (this.shuttingDown) return
const sinceLiveMs = this.connectedAtMs > 0 ? Date.now() - this.connectedAtMs : 0
if (sinceLiveMs > this.resumeWindowMs) {
this.log.warn('resume window elapsed; giving up')
this.dispose('resume_expired', POLICY_VIOLATION)
return
}
const delay = backoffMs(this.reconnectAttempt)
this.reconnectAttempt += 1
this.transition('reconnecting')
this.reconnectTimer = window.setTimeout(() => {
this.reconnectTimer = null
this.openSocket()
}, delay)
}
// Queue + heartbeat ---------------------------------------------
private enqueueLine(env: { type: string; ts: number; payload: unknown }): void {
const line = JSON.stringify(env)
if (this.status === 'live' && this.ws?.readyState === this.WebSocketImpl.OPEN) {
try { this.ws.send(line) } catch (err) {
this.log.warn('send failed; queueing', { err: String(err) })
this.outboundQueue.push(line)
}
} else {
this.outboundQueue.push(line)
}
}
private flushQueue(): void {
if (!this.ws || this.ws.readyState !== this.WebSocketImpl.OPEN) return
const q = this.outboundQueue
this.outboundQueue = []
for (const line of q) {
try { this.ws.send(line) } catch (err) {
this.log.error('flush failed; re-queueing remaining', { err: String(err) })
this.outboundQueue.push(line)
return
}
}
}
private startHeartbeat(): void {
this.clearHeartbeat()
this.heartbeatTimer = window.setInterval(() => {
this.sendControl('ping')
}, this.heartbeatIntervalMs)
// Dead-peer watchdog β if we haven't heard from the server in
// 2Γ the heartbeat interval, force a reconnect. Avoids hanging
// forever on half-open TCP sockets (mobile NAT rebinds).
this.deadPeerTimer = window.setInterval(() => {
if (Date.now() - this.lastServerActivityMs > this.heartbeatIntervalMs * 2) {
this.log.warn('peer silent; forcing reconnect')
try { this.ws?.close() } catch { /* ignore */ }
}
}, this.heartbeatIntervalMs)
}
private clearHeartbeat(): void {
if (this.heartbeatTimer !== null) {
window.clearInterval(this.heartbeatTimer)
this.heartbeatTimer = null
}
if (this.deadPeerTimer !== null) {
window.clearInterval(this.deadPeerTimer)
this.deadPeerTimer = null
}
}
private clearTimers(): void {
this.clearHeartbeat()
if (this.reconnectTimer !== null) {
window.clearTimeout(this.reconnectTimer)
this.reconnectTimer = null
}
}
// Emit + state transition ---------------------------------------
private emit<E extends keyof CallSocketEventMap>(
evt: E, payload: CallSocketEventMap[E],
): void {
const set = this.listeners[evt] as Set<Listener<E>> | undefined
if (!set) return
for (const fn of set) {
try { fn(payload) } catch (err) {
this.log.error('listener threw', { evt, err: String(err) })
}
}
}
private transition(next: CallLifecycleStatus): void {
if (this.status === next) return
this.status = next
this.emit('statusChange', next)
}
}
|