@tanstack/ai-grok 0.14.8 → 0.14.10
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/esm/adapters/image.js +228 -193
- package/dist/esm/adapters/image.js.map +1 -1
- package/dist/esm/adapters/summarize.js +49 -12
- package/dist/esm/adapters/summarize.js.map +1 -1
- package/dist/esm/adapters/text.js +82 -38
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/adapters/transcription.js +133 -114
- package/dist/esm/adapters/transcription.js.map +1 -1
- package/dist/esm/adapters/tts.js +147 -130
- package/dist/esm/adapters/tts.js.map +1 -1
- package/dist/esm/adapters/video.js +275 -226
- package/dist/esm/adapters/video.js.map +1 -1
- package/dist/esm/image/image-provider-options.js +64 -66
- package/dist/esm/image/image-provider-options.js.map +1 -1
- package/dist/esm/index.js +3 -31
- package/dist/esm/model-meta.js +149 -47
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/realtime/adapter.js +729 -812
- package/dist/esm/realtime/adapter.js.map +1 -1
- package/dist/esm/realtime/index.js +3 -0
- package/dist/esm/realtime/token.js +78 -72
- package/dist/esm/realtime/token.js.map +1 -1
- package/dist/esm/tools/index.js +57 -96
- package/dist/esm/tools/index.js.map +1 -1
- package/dist/esm/utils/audio.js +116 -162
- package/dist/esm/utils/audio.js.map +1 -1
- package/dist/esm/utils/client.js +22 -16
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/index.js +5 -0
- package/dist/esm/utils/schema-converter.js +2 -0
- package/dist/esm/video/video-provider-options.js +83 -57
- package/dist/esm/video/video-provider-options.js.map +1 -1
- package/package.json +8 -8
- package/src/realtime/adapter.ts +1 -1
- package/src/utils/audio.ts +1 -1
- package/dist/esm/index.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sources":["../../../src/realtime/adapter.ts"],"sourcesContent":["import { resolveDebugOption } from '@tanstack/ai/adapter-internals'\nimport type {\n AnyClientTool,\n AudioVisualization,\n RealtimeAdapter,\n RealtimeConnection,\n RealtimeEvent,\n RealtimeEventHandler,\n RealtimeMessage,\n RealtimeMode,\n RealtimeSessionConfig,\n RealtimeToken,\n} from '@tanstack/ai'\nimport type { InternalLogger } from '@tanstack/ai/adapter-internals'\nimport type { GrokRealtimeOptions } from './types'\n\nconst GROK_REALTIME_URL = 'https://api.x.ai/v1/realtime'\n\n/**\n * Runtime-checked field readers for untyped server events. Replace the\n * drive-by `event.X as string` / `event.X as Record<string, unknown>` casts\n * with readers that return `undefined` when the shape doesn't match, so a\n * malformed frame can't throw a TypeError inside `handleServerEvent`.\n */\nfunction readString(\n obj: Record<string, unknown>,\n key: string,\n): string | undefined {\n const value = obj[key]\n return typeof value === 'string' ? value : undefined\n}\n\nfunction readObject(\n obj: Record<string, unknown>,\n key: string,\n): Record<string, unknown> | undefined {\n const value = obj[key]\n return value && typeof value === 'object' && !Array.isArray(value)\n ? (value as Record<string, unknown>)\n : undefined\n}\n\nfunction readObjectArray(\n obj: Record<string, unknown>,\n key: string,\n): Array<Record<string, unknown>> | undefined {\n const value = obj[key]\n if (!Array.isArray(value)) return undefined\n return value.filter(\n (item): item is Record<string, unknown> =>\n item !== null && typeof item === 'object' && !Array.isArray(item),\n )\n}\n\ntype RealtimeServerError = Error & {\n code?: string\n type?: string\n param?: string\n}\n\n/**\n * Creates a Grok realtime adapter for client-side use.\n *\n * Uses WebRTC for browser connections (default). Mirrors the OpenAI realtime\n * adapter because xAI's Voice Agent API is OpenAI-realtime-compatible — the\n * only differences are the endpoint URL and default model.\n *\n * @example\n * ```typescript\n * import { RealtimeClient } from '@tanstack/ai-client'\n * import { grokRealtime } from '@tanstack/ai-grok'\n *\n * const client = new RealtimeClient({\n * getToken: () => fetch('/api/realtime-token').then(r => r.json()),\n * adapter: grokRealtime(),\n * })\n * ```\n */\nexport function grokRealtime(\n options: GrokRealtimeOptions = {},\n): RealtimeAdapter {\n const connectionMode = options.connectionMode ?? 'webrtc'\n const logger = resolveDebugOption(options.debug)\n\n return {\n provider: 'grok',\n\n async connect(\n token: RealtimeToken,\n _clientTools?: ReadonlyArray<AnyClientTool>,\n ): Promise<RealtimeConnection> {\n const model = token.config.model ?? 'grok-voice-fast-1.0'\n logger.request(`activity=realtime provider=grok model=${model}`, {\n provider: 'grok',\n model,\n })\n\n if (connectionMode === 'webrtc') {\n return createWebRTCConnection(token, logger)\n }\n const error = new Error('WebSocket connection mode not yet implemented')\n logger.errors('grok.realtime fatal', {\n error,\n source: 'grok.realtime',\n })\n throw error\n },\n }\n}\n\n/**\n * Creates a WebRTC connection to xAI's realtime API.\n */\nasync function createWebRTCConnection(\n token: RealtimeToken,\n logger: InternalLogger,\n): Promise<RealtimeConnection> {\n const model = token.config.model ?? 'grok-voice-fast-1.0'\n const eventHandlers = new Map<RealtimeEvent, Set<RealtimeEventHandler<any>>>()\n\n const pc = new RTCPeerConnection()\n\n let audioContext: AudioContext | null = null\n let inputAnalyser: AnalyserNode | null = null\n let outputAnalyser: AnalyserNode | null = null\n let inputSource: MediaStreamAudioSourceNode | null = null\n let outputSource: MediaStreamAudioSourceNode | null = null\n let localStream: MediaStream | null = null\n\n let audioElement: HTMLAudioElement | null = null\n\n // Captured into a const so closures see a non-nullable reference (teardown\n // re-points the outer `dataChannel` to null, but in-flight closures still\n // need to close their own channel).\n const channel = pc.createDataChannel('oai-events')\n let dataChannel: RTCDataChannel | null = channel\n\n let currentMode: RealtimeMode = 'idle'\n let currentMessageId: string | null = null\n\n // Flipped by `teardownConnection`. Guards `sendEvent` so post-disconnect\n // calls (e.g. a React `useEffect` cleanup flushing queued events) are\n // logged and skipped instead of silently piling up in `pendingEvents`.\n let isTornDown = false\n\n // Outbound events queued while the data channel isn't yet open. Declared\n // here (rather than next to `sendEvent`) so `teardownConnection` — which\n // lives higher up and can run from the SDP-path catch before `sendEvent`\n // is defined — can drain it without hitting the TDZ.\n const pendingEvents: Array<Record<string, unknown>> = []\n\n // Tracks whether we've sent the first session.update. On the first update\n // we attach a default input_audio_transcription so the server will emit\n // user transcripts unless the caller opts out via\n // `providerOptions.inputAudioTranscription = null | false`.\n let hasSentInitialSessionUpdate = false\n\n // Size hints for the fallback buffers returned when an analyser isn't yet\n // populated. We return a *fresh* `Uint8Array` on each call so a caller\n // that draws into it (e.g. a canvas visualiser zeroing the buffer) can't\n // mutate a shared module-level instance for every other consumer.\n const FALLBACK_FREQUENCY_BIN_COUNT = 1024\n const FALLBACK_TIME_DOMAIN_SIZE = 2048\n const FALLBACK_TIME_DOMAIN_FILL = 128\n\n function emit<TEvent extends RealtimeEvent>(\n event: TEvent,\n payload: Parameters<RealtimeEventHandler<TEvent>>[0],\n ) {\n const handlers = eventHandlers.get(event)\n if (handlers) {\n for (const handler of handlers) {\n handler(payload)\n }\n }\n }\n\n let dataChannelOpened = false\n let rejectDataChannelReady: ((reason: unknown) => void) | null = null\n let dataChannelReadyTimeout: ReturnType<typeof setTimeout> | null = null\n\n const dataChannelReady = new Promise<void>((resolve, reject) => {\n rejectDataChannelReady = (reason) => {\n if (dataChannelReadyTimeout !== null) {\n clearTimeout(dataChannelReadyTimeout)\n dataChannelReadyTimeout = null\n }\n // One-shot: null out so later state transitions don't reject twice.\n rejectDataChannelReady = null\n reject(reason)\n }\n\n dataChannelReadyTimeout = setTimeout(() => {\n if (!dataChannelOpened) {\n rejectDataChannelReady?.(\n new Error(\n 'Data channel did not open within 15000ms — aborting connection',\n ),\n )\n }\n }, 15000)\n\n channel.onopen = () => {\n dataChannelOpened = true\n if (dataChannelReadyTimeout !== null) {\n clearTimeout(dataChannelReadyTimeout)\n dataChannelReadyTimeout = null\n }\n // Once resolved, rejecting is a no-op — null out so teardown paths\n // don't attempt a redundant reject on an already-settled promise.\n rejectDataChannelReady = null\n flushPendingEvents()\n emit('status_change', { status: 'connected' })\n resolve()\n }\n })\n\n channel.onmessage = (event) => {\n try {\n const message = JSON.parse(event.data)\n const messageRecord: Record<string, unknown> =\n message !== null && typeof message === 'object' ? message : {}\n logger.provider(\n `provider=grok direction=in type=${readString(messageRecord, 'type') ?? '<unknown>'}`,\n { frame: messageRecord },\n )\n handleServerEvent(messageRecord)\n } catch (parseErr) {\n logger.errors('grok.realtime fatal', {\n error: parseErr,\n source: 'grok.realtime',\n })\n emit('error', {\n error:\n parseErr instanceof Error ? parseErr : new Error(String(parseErr)),\n })\n }\n }\n\n channel.onerror = (error) => {\n // Closing the peer connection cascades into `onerror`/`onclose` on the\n // data channel. Once teardown has started, re-surfacing those as\n // `emit('error')` is noise that confuses consumers (they just called\n // `disconnect()` — they don't want an error toast for it).\n if (isTornDown) return\n logger.errors('grok.realtime fatal', {\n error,\n source: 'grok.realtime',\n })\n // RTCErrorEvent exposes a typed `.error`; fall back to the event type\n // name, then to a string representation, so the emitted error message\n // doesn't end up as \"[object Event]\".\n // `onerror` always fires with an Event (often an RTCErrorEvent), so we\n // can read it via the untyped helpers without first proving object-ness.\n // eslint-disable-next-line no-restricted-syntax -- RTCErrorEvent is a typed DOM class that does not structurally overlap Record<string, unknown>; we duck-type it via readObject/readString\n const errorRecord = error as unknown as Record<string, unknown>\n const rtcError = readObject(errorRecord, 'error')\n const msg =\n (rtcError && readString(rtcError, 'message')) ?? (error.type || 'unknown')\n const dcErr = new Error(`Data channel error: ${msg}`)\n if (!dataChannelOpened) {\n rejectDataChannelReady?.(dcErr)\n }\n emit('error', { error: dcErr })\n }\n\n channel.onclose = () => {\n // Same rationale as `onerror` above: `pc.close()` during teardown\n // cascades to the data channel's `onclose`. If we've already started\n // teardown, there's nothing to do here.\n if (isTornDown) return\n if (!dataChannelOpened) {\n rejectDataChannelReady?.(new Error('Data channel closed before opening'))\n }\n }\n\n pc.ontrack = (event) => {\n if (event.track.kind === 'audio' && event.streams[0]) {\n setupOutputAudioAnalysis(event.streams[0])\n }\n }\n\n // `status_change` has a single source of truth: `onconnectionstatechange`\n // (the higher-level aggregate state). `oniceconnectionstatechange` is\n // responsible only for rejecting `dataChannelReady` on ICE failures so we\n // surface them without waiting for the 15s timeout.\n pc.onconnectionstatechange = () => {\n const state = pc.connectionState\n logger.provider(`provider=grok pc.connectionState=${state}`, {\n state,\n })\n if (state === 'failed' || state === 'disconnected' || state === 'closed') {\n // Suppress the `status_change` emission when teardown is in progress:\n // the user-facing `disconnect()` already emits `status_change: 'idle'`\n // and then calls `teardownConnection()` → `pc.close()`, which fires\n // `onconnectionstatechange` with state === 'closed'. Without this\n // guard listeners would see two `idle` events per disconnect.\n if (!isTornDown) {\n emit('status_change', {\n status: state === 'failed' ? 'error' : 'idle',\n })\n }\n if (!dataChannelOpened) {\n // Reject on any terminal-ish pre-open state so callers don't hang\n // for the full 15s timeout. The reject is one-shot — subsequent\n // state changes become no-ops via the null-out in\n // `rejectDataChannelReady`.\n const message =\n state === 'failed'\n ? `PeerConnection failed before data channel opened`\n : `PeerConnection entered state '${state}' before data channel opened`\n rejectDataChannelReady?.(new Error(message))\n }\n // Auto-teardown on `failed`: without this the mic track, pc, and\n // AudioContext stay allocated after a fatal connection failure, so the\n // browser's mic indicator stays on and the user sees a broken\n // \"connected mic\" state. `closed` already means pc was torn down\n // (usually by teardownConnection itself) so nothing extra to do.\n // `disconnected` is transient per the WebRTC spec and may recover, so\n // we leave resources in place. `teardownConnection` is idempotent so\n // a subsequent consumer `disconnect()` remains safe.\n if (state === 'failed' && !isTornDown) {\n void teardownConnection()\n }\n }\n }\n\n pc.oniceconnectionstatechange = () => {\n const state = pc.iceConnectionState\n logger.provider(`provider=grok pc.iceConnectionState=${state}`, {\n state,\n })\n if (\n !dataChannelOpened &&\n (state === 'failed' || state === 'closed' || state === 'disconnected')\n ) {\n const message =\n state === 'failed'\n ? `ICE connection failed before data channel opened`\n : `ICE connection entered state '${state}' before data channel opened`\n rejectDataChannelReady?.(new Error(message))\n }\n }\n\n /**\n * Tear down every resource we may have allocated so the mic/pc/audio\n * nodes/audio element don't leak on a failed connect. Safe to call from\n * any point after `new RTCPeerConnection()`; each branch null-guards and\n * swallows errors because cascading closes (e.g. `pc.close()` closing the\n * data channel implicitly) are expected.\n *\n * Shared between the SDP-path catch, the post-SDP catch, and (implicitly\n * via idempotency) the `disconnect()` entry point.\n */\n async function teardownConnection() {\n // Flip the teardown flag BEFORE any awaits so handlers that fire during\n // `await audioContext.close()` (or any other async step below) can guard\n // on it — otherwise a late `pc.onconnectionstatechange` or `pc.ontrack`\n // can allocate new resources or re-emit `status_change: idle` after the\n // user-facing `disconnect()` already emitted one.\n isTornDown = true\n\n // Drop any queued events the caller sent before the data channel opened\n // up front. Without this they'd accumulate across reconnect attempts\n // (each connect allocates a fresh closure, but a caller holding the old\n // `connection` reference could otherwise keep appending forever). Done\n // at the top — before the awaits below — so `sendEvent` calls racing\n // with teardown don't push into a list we're about to drain.\n pendingEvents.length = 0\n\n // Clear the data-channel-open timeout / reject the readiness promise\n // if it's still pending. `rejectDataChannelReady` is one-shot and nulls\n // itself on first call, so calling it from `disconnect()` after a\n // successful open is a no-op.\n rejectDataChannelReady?.(\n new Error('Connection torn down before data channel opened'),\n )\n\n if (localStream) {\n for (const track of localStream.getTracks()) {\n track.stop()\n }\n localStream = null\n }\n\n // Output audio (populated by `pc.ontrack` → setupOutputAudioAnalysis,\n // which may have fired during SDP negotiation before we threw).\n if (audioElement) {\n try {\n audioElement.pause()\n } catch {\n // ignore — element may already be unloaded\n }\n audioElement.srcObject = null\n audioElement = null\n }\n if (outputSource) {\n try {\n outputSource.disconnect()\n } catch {\n // ignore\n }\n outputSource = null\n }\n if (outputAnalyser) {\n try {\n outputAnalyser.disconnect()\n } catch {\n // ignore\n }\n outputAnalyser = null\n }\n\n // Input audio (populated by setupInputAudioAnalysis after SDP).\n if (inputSource) {\n try {\n inputSource.disconnect()\n } catch {\n // ignore\n }\n inputSource = null\n }\n if (inputAnalyser) {\n try {\n inputAnalyser.disconnect()\n } catch {\n // ignore\n }\n inputAnalyser = null\n }\n\n if (dataChannel) {\n try {\n dataChannel.close()\n } catch {\n // ignore — channel may already be closed by pc.close()\n }\n dataChannel = null\n }\n\n try {\n pc.close()\n } catch {\n // ignore — pc may already be closed\n }\n\n if (audioContext) {\n try {\n await audioContext.close()\n } catch {\n // ignore — context may already be closed\n }\n audioContext = null\n }\n }\n\n // xAI requires an audio track in the SDP offer, same as OpenAI realtime.\n //\n // This try/catch also covers `getUserMedia` failure (e.g. the user denies\n // microphone permission). `pc` + `dataChannel` are already allocated above\n // and the 15s `dataChannelReady` timeout is already armed, so we MUST\n // teardown on failure here — otherwise they leak until the tab closes.\n // `teardownConnection` is idempotent and null-safe (runs fine even if the\n // mic was never acquired).\n try {\n try {\n localStream = await navigator.mediaDevices.getUserMedia({\n audio: {\n echoCancellation: true,\n noiseSuppression: true,\n sampleRate: 24000,\n },\n })\n } catch (error) {\n logger.errors('grok.realtime fatal', {\n error,\n source: 'grok.realtime.getUserMedia',\n })\n // Re-throw with the descriptive message callers rely on. Teardown runs\n // in the outer catch below.\n throw new Error(\n `Microphone access required for realtime voice: ${error instanceof Error ? error.message : error}`,\n )\n }\n\n for (const track of localStream.getAudioTracks()) {\n pc.addTrack(track, localStream)\n }\n\n const offer = await pc.createOffer()\n await pc.setLocalDescription(offer)\n\n const sdpResponse = await fetch(`${GROK_REALTIME_URL}?model=${model}`, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${token.token}`,\n 'Content-Type': 'application/sdp',\n },\n body: offer.sdp,\n })\n\n if (!sdpResponse.ok) {\n const errorText = await sdpResponse.text()\n const error = new Error(\n `Failed to establish WebRTC connection: ${sdpResponse.status} - ${errorText}`,\n )\n logger.errors('grok.realtime fatal', {\n error,\n source: 'grok.realtime.sdp',\n status: sdpResponse.status,\n })\n throw error\n }\n\n const answerSdp = await sdpResponse.text()\n await pc.setRemoteDescription({ type: 'answer', sdp: answerSdp })\n } catch (err) {\n await teardownConnection()\n throw err\n }\n\n // Second cleanup scope: after SDP succeeds we still have to set up input\n // audio analysis and wait for the data channel to open. Both can fail\n // (AudioContext allocation, 15s timeout, ICE failure, pc.close from the\n // other end, etc.) and those failures must NOT leave the mic/pc/audio\n // nodes running.\n try {\n setupInputAudioAnalysis(localStream)\n await dataChannelReady\n } catch (err) {\n await teardownConnection()\n throw err\n }\n\n function handleServerEvent(event: Record<string, unknown>) {\n const type = readString(event, 'type')\n\n switch (type) {\n case 'session.created':\n case 'session.updated':\n break\n\n case 'input_audio_buffer.speech_started':\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n break\n\n case 'input_audio_buffer.speech_stopped':\n currentMode = 'thinking'\n emit('mode_change', { mode: 'thinking' })\n break\n\n case 'input_audio_buffer.committed':\n break\n\n case 'conversation.item.input_audio_transcription.completed': {\n const transcript = readString(event, 'transcript')\n if (transcript === undefined) break\n emit('transcript', { role: 'user', transcript, isFinal: true })\n break\n }\n\n case 'response.created':\n // Reset message id so a tool-only response (which never emits\n // response.output_item.added for a message) can't reuse the previous\n // turn's id when `response.done` later inspects this flag.\n currentMessageId = null\n currentMode = 'thinking'\n emit('mode_change', { mode: 'thinking' })\n break\n\n case 'response.output_item.added': {\n const item = readObject(event, 'item')\n if (item && readString(item, 'type') === 'message') {\n const id = readString(item, 'id')\n if (id !== undefined) currentMessageId = id\n }\n break\n }\n\n // xAI realtime per docs uses `response.output_audio_transcript.*`;\n // accept the legacy OpenAI-realtime `response.audio_transcript.*` as\n // an alias so this adapter stays compatible across protocol versions.\n case 'response.output_audio_transcript.delta':\n case 'response.audio_transcript.delta': {\n const delta = readString(event, 'delta')\n if (delta === undefined) break\n emit('transcript', {\n role: 'assistant',\n transcript: delta,\n isFinal: false,\n })\n break\n }\n\n case 'response.output_audio_transcript.done':\n case 'response.audio_transcript.done': {\n const transcript = readString(event, 'transcript')\n if (transcript === undefined) break\n emit('transcript', { role: 'assistant', transcript, isFinal: true })\n break\n }\n\n // xAI realtime per docs uses `response.text.*`; accept the legacy\n // OpenAI-realtime `response.output_text.*` as an alias.\n case 'response.text.delta':\n case 'response.output_text.delta': {\n const delta = readString(event, 'delta')\n if (delta === undefined) break\n emit('transcript', {\n role: 'assistant',\n transcript: delta,\n isFinal: false,\n })\n break\n }\n\n case 'response.text.done':\n case 'response.output_text.done': {\n const text = readString(event, 'text')\n if (text === undefined) break\n emit('transcript', {\n role: 'assistant',\n transcript: text,\n isFinal: true,\n })\n break\n }\n\n // xAI realtime per docs uses `response.output_audio.*`; accept the\n // legacy OpenAI-realtime `response.audio.*` as an alias.\n case 'response.output_audio.delta':\n case 'response.audio.delta':\n if (currentMode !== 'speaking') {\n currentMode = 'speaking'\n emit('mode_change', { mode: 'speaking' })\n }\n break\n\n case 'response.output_audio.done':\n case 'response.audio.done':\n break\n\n case 'response.function_call_arguments.done': {\n // Only `call_id` is valid for `sendToolResult` correlation. Falling\n // back to `item_id` would produce a tool-call id the server doesn't\n // recognise when the result is posted back, silently dropping the\n // tool execution. If `call_id` is missing we surface an error event\n // so the UI can react instead of pretending the tool call succeeded.\n const callId = readString(event, 'call_id')\n const name = readString(event, 'name') ?? ''\n const args = readString(event, 'arguments') ?? ''\n if (!callId) {\n logger.errors(\n 'grok.realtime tool_call missing call_id — dropping tool_call',\n {\n source: 'grok.realtime',\n event_type: 'response.function_call_arguments.done',\n item_id: event.item_id,\n },\n )\n emit('error', {\n error: new Error(\n 'Realtime tool call missing call_id; tool will not execute',\n ),\n })\n break\n }\n try {\n const input = JSON.parse(args)\n emit('tool_call', { toolCallId: callId, toolName: name, input })\n } catch {\n emit('tool_call', { toolCallId: callId, toolName: name, input: args })\n }\n break\n }\n\n case 'response.done': {\n const response = readObject(event, 'response') ?? {}\n const output = readObjectArray(response, 'output')\n\n // Only transition back to `listening` if the user hasn't already\n // stopped capture — otherwise we'd override their explicit `idle`\n // state and re-arm the mic visualisation.\n if (currentMode !== 'idle') {\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n }\n\n if (currentMessageId) {\n const message: RealtimeMessage = {\n id: currentMessageId,\n role: 'assistant',\n timestamp: Date.now(),\n parts: [],\n }\n\n for (const item of output ?? []) {\n if (readString(item, 'type') !== 'message') continue\n const content = readObjectArray(item, 'content')\n if (!content) continue\n for (const part of content) {\n const partType = readString(part, 'type')\n if (partType === 'audio') {\n const transcript = readString(part, 'transcript')\n if (transcript) {\n message.parts.push({ type: 'audio', transcript })\n }\n } else if (partType === 'text') {\n const content = readString(part, 'text')\n if (content) {\n message.parts.push({ type: 'text', content })\n }\n }\n }\n }\n\n emit('message_complete', { message })\n currentMessageId = null\n }\n break\n }\n\n case 'conversation.item.truncated':\n // Assistant playback was interrupted — flip mode back to `listening`\n // unless the user already called `stopAudioCapture()` (idle). Without\n // this the visualisation would stay stuck on `speaking` even though\n // no audio is playing.\n if (currentMode !== 'idle') {\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n }\n emit('interrupted', {\n ...(currentMessageId !== null && { messageId: currentMessageId }),\n })\n break\n\n case 'error': {\n // The realtime server's `error` envelope isn't guaranteed to carry\n // an `error` object at all (network-layer corruption, protocol\n // drift, etc.). Validate shape before dereferencing so a malformed\n // payload can't throw a TypeError inside this handler and stop the\n // switch from running for the rest of the session.\n const errorObj = readObject(event, 'error') ?? {}\n const message =\n readString(errorObj, 'message') ?? 'Unknown realtime server error'\n const err: RealtimeServerError = new Error(message)\n // Preserve `code` / `type` / `param` on the Error as extra props so\n // consumers can branch on them without re-parsing the raw event.\n const code = readString(errorObj, 'code')\n if (code !== undefined) err.code = code\n const errType = readString(errorObj, 'type')\n if (errType !== undefined) err.type = errType\n const param = readString(errorObj, 'param')\n if (param !== undefined) err.param = param\n logger.errors('grok.realtime server error', {\n ...errorObj,\n source: 'grok.realtime server',\n })\n emit('error', { error: err })\n break\n }\n\n case undefined:\n default:\n // The xAI realtime protocol is a moving target; log unhandled event\n // types at provider level so they're visible during debugging without\n // emitting a user-visible error. `undefined` shares the bucket because\n // a malformed event without a `type` field is just as unhandleable.\n logger.provider('grok.realtime unhandled server event', {\n type: event.type,\n })\n break\n }\n }\n\n function setupOutputAudioAnalysis(stream: MediaStream) {\n // Bail out if teardown has already started. `pc.ontrack` can fire\n // asynchronously after `teardownConnection()` has flipped `isTornDown`\n // (e.g. a remote track arriving mid-close); without this guard we'd\n // allocate a fresh AudioContext / audio element that nothing would ever\n // clean up.\n if (isTornDown) return\n\n // Tear down any prior output audio before allocating new resources.\n // `pc.ontrack` can fire multiple times over the lifetime of a session\n // (e.g. after renegotiation), and without this we'd leak audio elements\n // and analyser nodes.\n if (audioElement) {\n try {\n audioElement.pause()\n } catch {\n // ignore — element may already be unloaded\n }\n audioElement.srcObject = null\n audioElement = null\n }\n if (outputSource) {\n try {\n outputSource.disconnect()\n } catch {\n // ignore — may already be disconnected\n }\n outputSource = null\n }\n if (outputAnalyser) {\n try {\n outputAnalyser.disconnect()\n } catch {\n // ignore\n }\n outputAnalyser = null\n }\n\n audioElement = new Audio()\n audioElement.srcObject = stream\n audioElement.autoplay = true\n audioElement.play().catch((e) => {\n // Autoplay is commonly blocked until the user interacts with the page\n // (browser gesture requirement). Surfacing this as a fatal `error`\n // event makes the UI render a red/error state even though the\n // connection is healthy — the page just needs a click. Log at a\n // dedicated source tag so it's debuggable, but don't emit `error`.\n logger.errors('grok.realtime audio autoplay blocked', {\n error: e,\n source: 'grok.realtime.audio_permission_required',\n })\n })\n\n if (!audioContext) {\n audioContext = new AudioContext()\n }\n\n if (audioContext.state === 'suspended') {\n audioContext.resume().catch((err) => {\n // Same rationale as the autoplay catch: `resume()` failure usually\n // means the user hasn't interacted yet. Logging only — no error\n // emit — so the UI doesn't go into a fatal state for a recoverable\n // condition.\n logger.errors('grok.realtime audioContext.resume failed', {\n error: err,\n source: 'grok.realtime',\n })\n })\n }\n\n outputAnalyser = audioContext.createAnalyser()\n outputAnalyser.fftSize = 2048\n outputAnalyser.smoothingTimeConstant = 0.3\n\n outputSource = audioContext.createMediaStreamSource(stream)\n outputSource.connect(outputAnalyser)\n }\n\n function setupInputAudioAnalysis(stream: MediaStream) {\n // Defensive symmetry with `setupOutputAudioAnalysis`. Today this is\n // only called inline after SDP negotiation, but keeping the guard\n // means any future caller path (e.g. renegotiation) won't leak a fresh\n // AudioContext after teardown.\n if (isTornDown) return\n\n if (!audioContext) {\n audioContext = new AudioContext()\n }\n\n if (audioContext.state === 'suspended') {\n audioContext.resume().catch((err) => {\n // Same rationale as in setupOutputAudioAnalysis: a suspended\n // AudioContext usually resumes after a user gesture. Log only —\n // surfacing this as a fatal error makes the UI look broken for a\n // recoverable condition.\n logger.errors('grok.realtime audioContext.resume failed', {\n error: err,\n source: 'grok.realtime',\n })\n })\n }\n\n inputAnalyser = audioContext.createAnalyser()\n inputAnalyser.fftSize = 2048\n inputAnalyser.smoothingTimeConstant = 0.3\n\n inputSource = audioContext.createMediaStreamSource(stream)\n inputSource.connect(inputAnalyser)\n }\n\n function sendEvent(event: Record<string, unknown>) {\n if (isTornDown) {\n // The caller is holding onto a `connection` object after `disconnect()`\n // (or a failed connect). Silently queueing would leak memory and the\n // events would never flush. Log + drop so the misuse is visible in\n // debug mode without escalating to a throw — throwing from a React\n // useEffect cleanup path can break teardown ordering in the UI.\n logger.errors('grok.realtime sendEvent after disconnect', {\n eventType: readString(event, 'type') ?? '<unknown>',\n source: 'grok.realtime',\n })\n return\n }\n if (dataChannel?.readyState === 'open') {\n logger.provider(\n `provider=grok direction=out type=${readString(event, 'type') ?? '<unknown>'}`,\n { frame: event },\n )\n // Mirror the try/catch in `flushPendingEvents` — `dataChannel.send`\n // can synchronously throw if the channel flipped to `closing` between\n // our readyState check and this call, or if `JSON.stringify` chokes\n // on a caller-supplied payload. Log + emit error instead of letting\n // the exception propagate up through public `sendText` / `sendImage`\n // / `updateSession` call sites.\n try {\n dataChannel.send(JSON.stringify(event))\n } catch (error) {\n logger.errors('grok.realtime sendEvent failed', {\n error,\n eventType: readString(event, 'type') ?? '<unknown>',\n source: 'grok.realtime',\n })\n emit('error', {\n error: error instanceof Error ? error : new Error(String(error)),\n })\n }\n } else {\n pendingEvents.push(event)\n }\n }\n\n function flushPendingEvents() {\n try {\n for (const event of pendingEvents) {\n logger.provider(\n `provider=grok direction=out type=${readString(event, 'type') ?? '<unknown>'}`,\n { frame: event },\n )\n channel.send(JSON.stringify(event))\n }\n pendingEvents.length = 0\n } catch (error) {\n // A send failure here (e.g. dataChannel went from 'open' back to\n // 'closing' mid-flush, or JSON.stringify on a caller-provided event\n // threw) would otherwise be silently swallowed. By the time we're\n // called, `onopen` has already resolved `dataChannelReady`, so the\n // consumer-facing signal is `emit('error')` — try rejectDataChannelReady\n // as a defensive belt-and-braces in case this ever runs pre-resolve.\n logger.errors('grok.realtime flushPendingEvents failed', {\n error,\n source: 'grok.realtime',\n })\n const err = error instanceof Error ? error : new Error(String(error))\n rejectDataChannelReady?.(err)\n emit('error', { error: err })\n }\n }\n\n const connection: RealtimeConnection = {\n async disconnect() {\n // Reuse the same teardown path as the failed-connect branches so\n // every cleanup site stays in sync (input analyser, output analyser,\n // output source, audio element, etc.).\n await teardownConnection()\n emit('status_change', { status: 'idle' })\n },\n\n async startAudioCapture() {\n if (localStream) {\n for (const track of localStream.getAudioTracks()) {\n track.enabled = true\n }\n }\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n },\n\n stopAudioCapture() {\n if (localStream) {\n for (const track of localStream.getAudioTracks()) {\n track.enabled = false\n }\n }\n currentMode = 'idle'\n emit('mode_change', { mode: 'idle' })\n },\n\n sendText(text: string) {\n sendEvent({\n type: 'conversation.item.create',\n item: {\n type: 'message',\n role: 'user',\n content: [{ type: 'input_text', text }],\n },\n })\n sendEvent({ type: 'response.create' })\n },\n\n sendImage(imageData: string, mimeType: string) {\n // Accept:\n // - http(s):// URLs → forward as-is\n // - data: URIs (e.g. from FileReader.readAsDataURL) → forward as-is\n // so we don't double-wrap into `data:image/png;base64,data:image/png;base64,…`\n // - bare base64 → wrap in `data:${mimeType};base64,…`\n const isAlreadyUrlOrDataUri =\n imageData.startsWith('http://') ||\n imageData.startsWith('https://') ||\n imageData.startsWith('data:')\n const imageContent = {\n type: 'input_image',\n // The OpenAI-realtime content part (which this adapter mirrors) nests\n // the URL under an `image_url: { url: ... }` object, not a bare\n // string.\n image_url: {\n url: isAlreadyUrlOrDataUri\n ? imageData\n : `data:${mimeType};base64,${imageData}`,\n },\n }\n\n sendEvent({\n type: 'conversation.item.create',\n item: {\n type: 'message',\n role: 'user',\n content: [imageContent],\n },\n })\n sendEvent({ type: 'response.create' })\n },\n\n sendToolResult(callId: string, result: string) {\n sendEvent({\n type: 'conversation.item.create',\n item: {\n type: 'function_call_output',\n call_id: callId,\n output: result,\n },\n })\n sendEvent({ type: 'response.create' })\n },\n\n updateSession(config: Partial<RealtimeSessionConfig>) {\n const sessionUpdate: Record<string, unknown> = {}\n\n if (config.instructions) {\n sessionUpdate.instructions = config.instructions\n }\n\n if (config.voice) {\n sessionUpdate.voice = config.voice\n }\n\n if (config.vadMode) {\n if (config.vadMode === 'semantic') {\n sessionUpdate.turn_detection = {\n type: 'semantic_vad',\n eagerness: config.semanticEagerness ?? 'medium',\n }\n } else if (config.vadMode === 'server') {\n sessionUpdate.turn_detection = {\n type: 'server_vad',\n threshold: config.vadConfig?.threshold ?? 0.5,\n prefix_padding_ms: config.vadConfig?.prefixPaddingMs ?? 300,\n silence_duration_ms: config.vadConfig?.silenceDurationMs ?? 500,\n }\n } else {\n sessionUpdate.turn_detection = null\n }\n }\n\n if (config.tools !== undefined) {\n sessionUpdate.tools = config.tools.map((t) => ({\n type: 'function',\n name: t.name,\n description: t.description,\n parameters: t.inputSchema ?? { type: 'object', properties: {} },\n }))\n sessionUpdate.tool_choice = 'auto'\n }\n\n if (config.outputModalities) {\n sessionUpdate.modalities = config.outputModalities\n }\n\n if (config.temperature !== undefined) {\n sessionUpdate.temperature = config.temperature\n }\n\n if (config.maxOutputTokens !== undefined) {\n sessionUpdate.max_response_output_tokens = config.maxOutputTokens\n }\n\n // Let callers forward an explicit `input_audio_transcription` value\n // through `providerOptions` — including `null` / `false` to disable\n // the feature. Only apply our `grok-stt` default on the first\n // session.update and only if the caller hasn't set it themselves.\n const providerOptions: Record<string, unknown> =\n config.providerOptions ?? {}\n const callerTranscription =\n 'inputAudioTranscription' in providerOptions\n ? providerOptions.inputAudioTranscription\n : 'input_audio_transcription' in providerOptions\n ? providerOptions.input_audio_transcription\n : undefined\n if (callerTranscription !== undefined) {\n sessionUpdate.input_audio_transcription =\n callerTranscription === false ? null : callerTranscription\n } else if (!hasSentInitialSessionUpdate) {\n sessionUpdate.input_audio_transcription = { model: 'grok-stt' }\n }\n\n if (Object.keys(sessionUpdate).length > 0) {\n sendEvent({\n type: 'session.update',\n session: sessionUpdate,\n })\n hasSentInitialSessionUpdate = true\n }\n },\n\n interrupt() {\n sendEvent({ type: 'response.cancel' })\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n emit('interrupted', {\n ...(currentMessageId !== null && { messageId: currentMessageId }),\n })\n },\n\n on<TEvent extends RealtimeEvent>(\n event: TEvent,\n handler: RealtimeEventHandler<TEvent>,\n ): () => void {\n let handlers = eventHandlers.get(event)\n if (!handlers) {\n handlers = new Set()\n eventHandlers.set(event, handlers)\n }\n handlers.add(handler)\n\n return () => {\n eventHandlers.get(event)?.delete(handler)\n }\n },\n\n getAudioVisualization(): AudioVisualization {\n function calculateLevel(analyser: AnalyserNode): number {\n const data = new Uint8Array(analyser.fftSize)\n analyser.getByteTimeDomainData(data)\n\n let maxDeviation = 0\n for (const sample of data) {\n const deviation = Math.abs(sample - 128)\n if (deviation > maxDeviation) {\n maxDeviation = deviation\n }\n }\n\n const normalized = maxDeviation / 128\n return Math.min(1, normalized * 1.5)\n }\n\n return {\n get inputLevel() {\n if (!inputAnalyser) return 0\n return calculateLevel(inputAnalyser)\n },\n\n get outputLevel() {\n if (!outputAnalyser) return 0\n return calculateLevel(outputAnalyser)\n },\n\n getInputFrequencyData() {\n if (!inputAnalyser)\n return new Uint8Array(FALLBACK_FREQUENCY_BIN_COUNT)\n const data = new Uint8Array(inputAnalyser.frequencyBinCount)\n inputAnalyser.getByteFrequencyData(data)\n return data\n },\n\n getOutputFrequencyData() {\n if (!outputAnalyser)\n return new Uint8Array(FALLBACK_FREQUENCY_BIN_COUNT)\n const data = new Uint8Array(outputAnalyser.frequencyBinCount)\n outputAnalyser.getByteFrequencyData(data)\n return data\n },\n\n getInputTimeDomainData() {\n if (!inputAnalyser)\n return new Uint8Array(FALLBACK_TIME_DOMAIN_SIZE).fill(\n FALLBACK_TIME_DOMAIN_FILL,\n )\n const data = new Uint8Array(inputAnalyser.fftSize)\n inputAnalyser.getByteTimeDomainData(data)\n return data\n },\n\n getOutputTimeDomainData() {\n if (!outputAnalyser)\n return new Uint8Array(FALLBACK_TIME_DOMAIN_SIZE).fill(\n FALLBACK_TIME_DOMAIN_FILL,\n )\n const data = new Uint8Array(outputAnalyser.fftSize)\n outputAnalyser.getByteTimeDomainData(data)\n return data\n },\n\n get inputSampleRate() {\n return 24000\n },\n\n get outputSampleRate() {\n return 24000\n },\n }\n },\n }\n\n // `dataChannelReady` was already awaited inside the post-SDP try/catch\n // above so we can short-circuit on failures with full teardown.\n return connection\n}\n"],"names":["content"],"mappings":";AAgBA,MAAM,oBAAoB;AAQ1B,SAAS,WACP,KACA,KACoB;AACpB,QAAM,QAAQ,IAAI,GAAG;AACrB,SAAO,OAAO,UAAU,WAAW,QAAQ;AAC7C;AAEA,SAAS,WACP,KACA,KACqC;AACrC,QAAM,QAAQ,IAAI,GAAG;AACrB,SAAO,SAAS,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,IAC5D,QACD;AACN;AAEA,SAAS,gBACP,KACA,KAC4C;AAC5C,QAAM,QAAQ,IAAI,GAAG;AACrB,MAAI,CAAC,MAAM,QAAQ,KAAK,EAAG,QAAO;AAClC,SAAO,MAAM;AAAA,IACX,CAAC,SACC,SAAS,QAAQ,OAAO,SAAS,YAAY,CAAC,MAAM,QAAQ,IAAI;AAAA,EAAA;AAEtE;AA0BO,SAAS,aACd,UAA+B,IACd;AACjB,QAAM,iBAAiB,QAAQ,kBAAkB;AACjD,QAAM,SAAS,mBAAmB,QAA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,WAAW,eAAe,OAAO;AAClD,yBAAe,sBAAsB,IAAI;AACzC,iBAAO;AAAA,QACT;AAAA,QAEA,IAAI,kBAAkB;AACpB,iBAAO;AAAA,QACT;AAAA,QAEA,IAAI,mBAAmB;AACrB,iBAAO;AAAA,QACT;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAKF,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"adapter.js","names":[],"sources":["../../../src/realtime/adapter.ts"],"sourcesContent":["import { resolveDebugOption } from '@tanstack/ai/adapter-internals'\nimport type {\n AnyClientTool,\n AudioVisualization,\n RealtimeAdapter,\n RealtimeConnection,\n RealtimeEvent,\n RealtimeEventHandler,\n RealtimeMessage,\n RealtimeMode,\n RealtimeSessionConfig,\n RealtimeToken,\n} from '@tanstack/ai'\nimport type { InternalLogger } from '@tanstack/ai/adapter-internals'\nimport type { GrokRealtimeOptions } from './types'\n\nconst GROK_REALTIME_URL = 'https://api.x.ai/v1/realtime'\n\n/**\n * Runtime-checked field readers for untyped server events. Replace the\n * drive-by `event.X as string` / `event.X as Record<string, unknown>` casts\n * with readers that return `undefined` when the shape doesn't match, so a\n * malformed frame can't throw a TypeError inside `handleServerEvent`.\n */\nfunction readString(\n obj: Record<string, unknown>,\n key: string,\n): string | undefined {\n const value = obj[key]\n return typeof value === 'string' ? value : undefined\n}\n\nfunction readObject(\n obj: Record<string, unknown>,\n key: string,\n): Record<string, unknown> | undefined {\n const value = obj[key]\n return value && typeof value === 'object' && !Array.isArray(value)\n ? (value as Record<string, unknown>)\n : undefined\n}\n\nfunction readObjectArray(\n obj: Record<string, unknown>,\n key: string,\n): Array<Record<string, unknown>> | undefined {\n const value = obj[key]\n if (!Array.isArray(value)) return undefined\n return value.filter(\n (item): item is Record<string, unknown> =>\n item !== null && typeof item === 'object' && !Array.isArray(item),\n )\n}\n\ntype RealtimeServerError = Error & {\n code?: string\n type?: string\n param?: string\n}\n\n/**\n * Creates a Grok realtime adapter for client-side use.\n *\n * Uses WebRTC for browser connections (default). Mirrors the OpenAI realtime\n * adapter because xAI's Voice Agent API is OpenAI-realtime-compatible — the\n * only differences are the endpoint URL and default model.\n *\n * @example\n * ```typescript\n * import { RealtimeClient } from '@tanstack/ai-client'\n * import { grokRealtime } from '@tanstack/ai-grok'\n *\n * const client = new RealtimeClient({\n * getToken: () => fetch('/api/realtime-token').then(r => r.json()),\n * adapter: grokRealtime(),\n * })\n * ```\n */\nexport function grokRealtime(\n options: GrokRealtimeOptions = {},\n): RealtimeAdapter {\n const connectionMode = options.connectionMode ?? 'webrtc'\n const logger = resolveDebugOption(options.debug)\n\n return {\n provider: 'grok',\n\n async connect(\n token: RealtimeToken,\n _clientTools?: ReadonlyArray<AnyClientTool>,\n ): Promise<RealtimeConnection> {\n const model = token.config.model ?? 'grok-voice-fast-1.0'\n logger.request(`activity=realtime provider=grok model=${model}`, {\n provider: 'grok',\n model,\n })\n\n if (connectionMode === 'webrtc') {\n return createWebRTCConnection(token, logger)\n }\n const error = new Error('WebSocket connection mode not yet implemented')\n logger.errors('grok.realtime fatal', {\n error,\n source: 'grok.realtime',\n })\n throw error\n },\n }\n}\n\n/**\n * Creates a WebRTC connection to xAI's realtime API.\n */\nasync function createWebRTCConnection(\n token: RealtimeToken,\n logger: InternalLogger,\n): Promise<RealtimeConnection> {\n const model = token.config.model ?? 'grok-voice-fast-1.0'\n const eventHandlers = new Map<RealtimeEvent, Set<RealtimeEventHandler<any>>>()\n\n const pc = new RTCPeerConnection()\n\n let audioContext: AudioContext | null = null\n let inputAnalyser: AnalyserNode | null = null\n let outputAnalyser: AnalyserNode | null = null\n let inputSource: MediaStreamAudioSourceNode | null = null\n let outputSource: MediaStreamAudioSourceNode | null = null\n let localStream: MediaStream | null = null\n\n let audioElement: HTMLAudioElement | null = null\n\n // Captured into a const so closures see a non-nullable reference (teardown\n // re-points the outer `dataChannel` to null, but in-flight closures still\n // need to close their own channel).\n const channel = pc.createDataChannel('oai-events')\n let dataChannel: RTCDataChannel | null = channel\n\n let currentMode: RealtimeMode = 'idle'\n let currentMessageId: string | null = null\n\n // Flipped by `teardownConnection`. Guards `sendEvent` so post-disconnect\n // calls (e.g. a React `useEffect` cleanup flushing queued events) are\n // logged and skipped instead of silently piling up in `pendingEvents`.\n let isTornDown = false\n\n // Outbound events queued while the data channel isn't yet open. Declared\n // here (rather than next to `sendEvent`) so `teardownConnection` — which\n // lives higher up and can run from the SDP-path catch before `sendEvent`\n // is defined — can drain it without hitting the TDZ.\n const pendingEvents: Array<Record<string, unknown>> = []\n\n // Tracks whether we've sent the first session.update. On the first update\n // we attach a default input_audio_transcription so the server will emit\n // user transcripts unless the caller opts out via\n // `providerOptions.inputAudioTranscription = null | false`.\n let hasSentInitialSessionUpdate = false\n\n // Size hints for the fallback buffers returned when an analyser isn't yet\n // populated. We return a *fresh* `Uint8Array` on each call so a caller\n // that draws into it (e.g. a canvas visualiser zeroing the buffer) can't\n // mutate a shared module-level instance for every other consumer.\n const FALLBACK_FREQUENCY_BIN_COUNT = 1024\n const FALLBACK_TIME_DOMAIN_SIZE = 2048\n const FALLBACK_TIME_DOMAIN_FILL = 128\n\n function emit<TEvent extends RealtimeEvent>(\n event: TEvent,\n payload: Parameters<RealtimeEventHandler<TEvent>>[0],\n ) {\n const handlers = eventHandlers.get(event)\n if (handlers) {\n for (const handler of handlers) {\n handler(payload)\n }\n }\n }\n\n let dataChannelOpened = false\n let rejectDataChannelReady: ((reason: unknown) => void) | null = null\n let dataChannelReadyTimeout: ReturnType<typeof setTimeout> | null = null\n\n const dataChannelReady = new Promise<void>((resolve, reject) => {\n rejectDataChannelReady = (reason) => {\n if (dataChannelReadyTimeout !== null) {\n clearTimeout(dataChannelReadyTimeout)\n dataChannelReadyTimeout = null\n }\n // One-shot: null out so later state transitions don't reject twice.\n rejectDataChannelReady = null\n reject(reason)\n }\n\n dataChannelReadyTimeout = setTimeout(() => {\n if (!dataChannelOpened) {\n rejectDataChannelReady?.(\n new Error(\n 'Data channel did not open within 15000ms — aborting connection',\n ),\n )\n }\n }, 15000)\n\n channel.onopen = () => {\n dataChannelOpened = true\n if (dataChannelReadyTimeout !== null) {\n clearTimeout(dataChannelReadyTimeout)\n dataChannelReadyTimeout = null\n }\n // Once resolved, rejecting is a no-op — null out so teardown paths\n // don't attempt a redundant reject on an already-settled promise.\n rejectDataChannelReady = null\n flushPendingEvents()\n emit('status_change', { status: 'connected' })\n resolve()\n }\n })\n\n channel.onmessage = (event) => {\n try {\n const message = JSON.parse(event.data)\n const messageRecord: Record<string, unknown> =\n message !== null && typeof message === 'object' ? message : {}\n logger.provider(\n `provider=grok direction=in type=${readString(messageRecord, 'type') ?? '<unknown>'}`,\n { frame: messageRecord },\n )\n handleServerEvent(messageRecord)\n } catch (parseErr) {\n logger.errors('grok.realtime fatal', {\n error: parseErr,\n source: 'grok.realtime',\n })\n emit('error', {\n error:\n parseErr instanceof Error ? parseErr : new Error(String(parseErr)),\n })\n }\n }\n\n channel.onerror = (error) => {\n // Closing the peer connection cascades into `onerror`/`onclose` on the\n // data channel. Once teardown has started, re-surfacing those as\n // `emit('error')` is noise that confuses consumers (they just called\n // `disconnect()` — they don't want an error toast for it).\n if (isTornDown) return\n logger.errors('grok.realtime fatal', {\n error,\n source: 'grok.realtime',\n })\n // RTCErrorEvent exposes a typed `.error`; fall back to the event type\n // name, then to a string representation, so the emitted error message\n // doesn't end up as \"[object Event]\".\n // `onerror` always fires with an Event (often an RTCErrorEvent), so we\n // can read it via the untyped helpers without first proving object-ness.\n // oxlint-disable-next-line eslint-js/no-restricted-syntax -- RTCErrorEvent is a typed DOM class that does not structurally overlap Record<string, unknown>; we duck-type it via readObject/readString\n const errorRecord = error as unknown as Record<string, unknown>\n const rtcError = readObject(errorRecord, 'error')\n const msg =\n (rtcError && readString(rtcError, 'message')) ?? (error.type || 'unknown')\n const dcErr = new Error(`Data channel error: ${msg}`)\n if (!dataChannelOpened) {\n rejectDataChannelReady?.(dcErr)\n }\n emit('error', { error: dcErr })\n }\n\n channel.onclose = () => {\n // Same rationale as `onerror` above: `pc.close()` during teardown\n // cascades to the data channel's `onclose`. If we've already started\n // teardown, there's nothing to do here.\n if (isTornDown) return\n if (!dataChannelOpened) {\n rejectDataChannelReady?.(new Error('Data channel closed before opening'))\n }\n }\n\n pc.ontrack = (event) => {\n if (event.track.kind === 'audio' && event.streams[0]) {\n setupOutputAudioAnalysis(event.streams[0])\n }\n }\n\n // `status_change` has a single source of truth: `onconnectionstatechange`\n // (the higher-level aggregate state). `oniceconnectionstatechange` is\n // responsible only for rejecting `dataChannelReady` on ICE failures so we\n // surface them without waiting for the 15s timeout.\n pc.onconnectionstatechange = () => {\n const state = pc.connectionState\n logger.provider(`provider=grok pc.connectionState=${state}`, {\n state,\n })\n if (state === 'failed' || state === 'disconnected' || state === 'closed') {\n // Suppress the `status_change` emission when teardown is in progress:\n // the user-facing `disconnect()` already emits `status_change: 'idle'`\n // and then calls `teardownConnection()` → `pc.close()`, which fires\n // `onconnectionstatechange` with state === 'closed'. Without this\n // guard listeners would see two `idle` events per disconnect.\n if (!isTornDown) {\n emit('status_change', {\n status: state === 'failed' ? 'error' : 'idle',\n })\n }\n if (!dataChannelOpened) {\n // Reject on any terminal-ish pre-open state so callers don't hang\n // for the full 15s timeout. The reject is one-shot — subsequent\n // state changes become no-ops via the null-out in\n // `rejectDataChannelReady`.\n const message =\n state === 'failed'\n ? `PeerConnection failed before data channel opened`\n : `PeerConnection entered state '${state}' before data channel opened`\n rejectDataChannelReady?.(new Error(message))\n }\n // Auto-teardown on `failed`: without this the mic track, pc, and\n // AudioContext stay allocated after a fatal connection failure, so the\n // browser's mic indicator stays on and the user sees a broken\n // \"connected mic\" state. `closed` already means pc was torn down\n // (usually by teardownConnection itself) so nothing extra to do.\n // `disconnected` is transient per the WebRTC spec and may recover, so\n // we leave resources in place. `teardownConnection` is idempotent so\n // a subsequent consumer `disconnect()` remains safe.\n if (state === 'failed' && !isTornDown) {\n void teardownConnection()\n }\n }\n }\n\n pc.oniceconnectionstatechange = () => {\n const state = pc.iceConnectionState\n logger.provider(`provider=grok pc.iceConnectionState=${state}`, {\n state,\n })\n if (\n !dataChannelOpened &&\n (state === 'failed' || state === 'closed' || state === 'disconnected')\n ) {\n const message =\n state === 'failed'\n ? `ICE connection failed before data channel opened`\n : `ICE connection entered state '${state}' before data channel opened`\n rejectDataChannelReady?.(new Error(message))\n }\n }\n\n /**\n * Tear down every resource we may have allocated so the mic/pc/audio\n * nodes/audio element don't leak on a failed connect. Safe to call from\n * any point after `new RTCPeerConnection()`; each branch null-guards and\n * swallows errors because cascading closes (e.g. `pc.close()` closing the\n * data channel implicitly) are expected.\n *\n * Shared between the SDP-path catch, the post-SDP catch, and (implicitly\n * via idempotency) the `disconnect()` entry point.\n */\n async function teardownConnection() {\n // Flip the teardown flag BEFORE any awaits so handlers that fire during\n // `await audioContext.close()` (or any other async step below) can guard\n // on it — otherwise a late `pc.onconnectionstatechange` or `pc.ontrack`\n // can allocate new resources or re-emit `status_change: idle` after the\n // user-facing `disconnect()` already emitted one.\n isTornDown = true\n\n // Drop any queued events the caller sent before the data channel opened\n // up front. Without this they'd accumulate across reconnect attempts\n // (each connect allocates a fresh closure, but a caller holding the old\n // `connection` reference could otherwise keep appending forever). Done\n // at the top — before the awaits below — so `sendEvent` calls racing\n // with teardown don't push into a list we're about to drain.\n pendingEvents.length = 0\n\n // Clear the data-channel-open timeout / reject the readiness promise\n // if it's still pending. `rejectDataChannelReady` is one-shot and nulls\n // itself on first call, so calling it from `disconnect()` after a\n // successful open is a no-op.\n rejectDataChannelReady?.(\n new Error('Connection torn down before data channel opened'),\n )\n\n if (localStream) {\n for (const track of localStream.getTracks()) {\n track.stop()\n }\n localStream = null\n }\n\n // Output audio (populated by `pc.ontrack` → setupOutputAudioAnalysis,\n // which may have fired during SDP negotiation before we threw).\n if (audioElement) {\n try {\n audioElement.pause()\n } catch {\n // ignore — element may already be unloaded\n }\n audioElement.srcObject = null\n audioElement = null\n }\n if (outputSource) {\n try {\n outputSource.disconnect()\n } catch {\n // ignore\n }\n outputSource = null\n }\n if (outputAnalyser) {\n try {\n outputAnalyser.disconnect()\n } catch {\n // ignore\n }\n outputAnalyser = null\n }\n\n // Input audio (populated by setupInputAudioAnalysis after SDP).\n if (inputSource) {\n try {\n inputSource.disconnect()\n } catch {\n // ignore\n }\n inputSource = null\n }\n if (inputAnalyser) {\n try {\n inputAnalyser.disconnect()\n } catch {\n // ignore\n }\n inputAnalyser = null\n }\n\n if (dataChannel) {\n try {\n dataChannel.close()\n } catch {\n // ignore — channel may already be closed by pc.close()\n }\n dataChannel = null\n }\n\n try {\n pc.close()\n } catch {\n // ignore — pc may already be closed\n }\n\n if (audioContext) {\n try {\n await audioContext.close()\n } catch {\n // ignore — context may already be closed\n }\n audioContext = null\n }\n }\n\n // xAI requires an audio track in the SDP offer, same as OpenAI realtime.\n //\n // This try/catch also covers `getUserMedia` failure (e.g. the user denies\n // microphone permission). `pc` + `dataChannel` are already allocated above\n // and the 15s `dataChannelReady` timeout is already armed, so we MUST\n // teardown on failure here — otherwise they leak until the tab closes.\n // `teardownConnection` is idempotent and null-safe (runs fine even if the\n // mic was never acquired).\n try {\n try {\n localStream = await navigator.mediaDevices.getUserMedia({\n audio: {\n echoCancellation: true,\n noiseSuppression: true,\n sampleRate: 24000,\n },\n })\n } catch (error) {\n logger.errors('grok.realtime fatal', {\n error,\n source: 'grok.realtime.getUserMedia',\n })\n // Re-throw with the descriptive message callers rely on. Teardown runs\n // in the outer catch below.\n throw new Error(\n `Microphone access required for realtime voice: ${error instanceof Error ? error.message : error}`,\n )\n }\n\n for (const track of localStream.getAudioTracks()) {\n pc.addTrack(track, localStream)\n }\n\n const offer = await pc.createOffer()\n await pc.setLocalDescription(offer)\n\n const sdpResponse = await fetch(`${GROK_REALTIME_URL}?model=${model}`, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${token.token}`,\n 'Content-Type': 'application/sdp',\n },\n body: offer.sdp,\n })\n\n if (!sdpResponse.ok) {\n const errorText = await sdpResponse.text()\n const error = new Error(\n `Failed to establish WebRTC connection: ${sdpResponse.status} - ${errorText}`,\n )\n logger.errors('grok.realtime fatal', {\n error,\n source: 'grok.realtime.sdp',\n status: sdpResponse.status,\n })\n throw error\n }\n\n const answerSdp = await sdpResponse.text()\n await pc.setRemoteDescription({ type: 'answer', sdp: answerSdp })\n } catch (err) {\n await teardownConnection()\n throw err\n }\n\n // Second cleanup scope: after SDP succeeds we still have to set up input\n // audio analysis and wait for the data channel to open. Both can fail\n // (AudioContext allocation, 15s timeout, ICE failure, pc.close from the\n // other end, etc.) and those failures must NOT leave the mic/pc/audio\n // nodes running.\n try {\n setupInputAudioAnalysis(localStream)\n await dataChannelReady\n } catch (err) {\n await teardownConnection()\n throw err\n }\n\n function handleServerEvent(event: Record<string, unknown>) {\n const type = readString(event, 'type')\n\n switch (type) {\n case 'session.created':\n case 'session.updated':\n break\n\n case 'input_audio_buffer.speech_started':\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n break\n\n case 'input_audio_buffer.speech_stopped':\n currentMode = 'thinking'\n emit('mode_change', { mode: 'thinking' })\n break\n\n case 'input_audio_buffer.committed':\n break\n\n case 'conversation.item.input_audio_transcription.completed': {\n const transcript = readString(event, 'transcript')\n if (transcript === undefined) break\n emit('transcript', { role: 'user', transcript, isFinal: true })\n break\n }\n\n case 'response.created':\n // Reset message id so a tool-only response (which never emits\n // response.output_item.added for a message) can't reuse the previous\n // turn's id when `response.done` later inspects this flag.\n currentMessageId = null\n currentMode = 'thinking'\n emit('mode_change', { mode: 'thinking' })\n break\n\n case 'response.output_item.added': {\n const item = readObject(event, 'item')\n if (item && readString(item, 'type') === 'message') {\n const id = readString(item, 'id')\n if (id !== undefined) currentMessageId = id\n }\n break\n }\n\n // xAI realtime per docs uses `response.output_audio_transcript.*`;\n // accept the legacy OpenAI-realtime `response.audio_transcript.*` as\n // an alias so this adapter stays compatible across protocol versions.\n case 'response.output_audio_transcript.delta':\n case 'response.audio_transcript.delta': {\n const delta = readString(event, 'delta')\n if (delta === undefined) break\n emit('transcript', {\n role: 'assistant',\n transcript: delta,\n isFinal: false,\n })\n break\n }\n\n case 'response.output_audio_transcript.done':\n case 'response.audio_transcript.done': {\n const transcript = readString(event, 'transcript')\n if (transcript === undefined) break\n emit('transcript', { role: 'assistant', transcript, isFinal: true })\n break\n }\n\n // xAI realtime per docs uses `response.text.*`; accept the legacy\n // OpenAI-realtime `response.output_text.*` as an alias.\n case 'response.text.delta':\n case 'response.output_text.delta': {\n const delta = readString(event, 'delta')\n if (delta === undefined) break\n emit('transcript', {\n role: 'assistant',\n transcript: delta,\n isFinal: false,\n })\n break\n }\n\n case 'response.text.done':\n case 'response.output_text.done': {\n const text = readString(event, 'text')\n if (text === undefined) break\n emit('transcript', {\n role: 'assistant',\n transcript: text,\n isFinal: true,\n })\n break\n }\n\n // xAI realtime per docs uses `response.output_audio.*`; accept the\n // legacy OpenAI-realtime `response.audio.*` as an alias.\n case 'response.output_audio.delta':\n case 'response.audio.delta':\n if (currentMode !== 'speaking') {\n currentMode = 'speaking'\n emit('mode_change', { mode: 'speaking' })\n }\n break\n\n case 'response.output_audio.done':\n case 'response.audio.done':\n break\n\n case 'response.function_call_arguments.done': {\n // Only `call_id` is valid for `sendToolResult` correlation. Falling\n // back to `item_id` would produce a tool-call id the server doesn't\n // recognise when the result is posted back, silently dropping the\n // tool execution. If `call_id` is missing we surface an error event\n // so the UI can react instead of pretending the tool call succeeded.\n const callId = readString(event, 'call_id')\n const name = readString(event, 'name') ?? ''\n const args = readString(event, 'arguments') ?? ''\n if (!callId) {\n logger.errors(\n 'grok.realtime tool_call missing call_id — dropping tool_call',\n {\n source: 'grok.realtime',\n event_type: 'response.function_call_arguments.done',\n item_id: event.item_id,\n },\n )\n emit('error', {\n error: new Error(\n 'Realtime tool call missing call_id; tool will not execute',\n ),\n })\n break\n }\n try {\n const input = JSON.parse(args)\n emit('tool_call', { toolCallId: callId, toolName: name, input })\n } catch {\n emit('tool_call', { toolCallId: callId, toolName: name, input: args })\n }\n break\n }\n\n case 'response.done': {\n const response = readObject(event, 'response') ?? {}\n const output = readObjectArray(response, 'output')\n\n // Only transition back to `listening` if the user hasn't already\n // stopped capture — otherwise we'd override their explicit `idle`\n // state and re-arm the mic visualisation.\n if (currentMode !== 'idle') {\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n }\n\n if (currentMessageId) {\n const message: RealtimeMessage = {\n id: currentMessageId,\n role: 'assistant',\n timestamp: Date.now(),\n parts: [],\n }\n\n for (const item of output ?? []) {\n if (readString(item, 'type') !== 'message') continue\n const content = readObjectArray(item, 'content')\n if (!content) continue\n for (const part of content) {\n const partType = readString(part, 'type')\n if (partType === 'audio') {\n const transcript = readString(part, 'transcript')\n if (transcript) {\n message.parts.push({ type: 'audio', transcript })\n }\n } else if (partType === 'text') {\n const content = readString(part, 'text')\n if (content) {\n message.parts.push({ type: 'text', content })\n }\n }\n }\n }\n\n emit('message_complete', { message })\n currentMessageId = null\n }\n break\n }\n\n case 'conversation.item.truncated':\n // Assistant playback was interrupted — flip mode back to `listening`\n // unless the user already called `stopAudioCapture()` (idle). Without\n // this the visualisation would stay stuck on `speaking` even though\n // no audio is playing.\n if (currentMode !== 'idle') {\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n }\n emit('interrupted', {\n ...(currentMessageId !== null && { messageId: currentMessageId }),\n })\n break\n\n case 'error': {\n // The realtime server's `error` envelope isn't guaranteed to carry\n // an `error` object at all (network-layer corruption, protocol\n // drift, etc.). Validate shape before dereferencing so a malformed\n // payload can't throw a TypeError inside this handler and stop the\n // switch from running for the rest of the session.\n const errorObj = readObject(event, 'error') ?? {}\n const message =\n readString(errorObj, 'message') ?? 'Unknown realtime server error'\n const err: RealtimeServerError = new Error(message)\n // Preserve `code` / `type` / `param` on the Error as extra props so\n // consumers can branch on them without re-parsing the raw event.\n const code = readString(errorObj, 'code')\n if (code !== undefined) err.code = code\n const errType = readString(errorObj, 'type')\n if (errType !== undefined) err.type = errType\n const param = readString(errorObj, 'param')\n if (param !== undefined) err.param = param\n logger.errors('grok.realtime server error', {\n ...errorObj,\n source: 'grok.realtime server',\n })\n emit('error', { error: err })\n break\n }\n\n case undefined:\n default:\n // The xAI realtime protocol is a moving target; log unhandled event\n // types at provider level so they're visible during debugging without\n // emitting a user-visible error. `undefined` shares the bucket because\n // a malformed event without a `type` field is just as unhandleable.\n logger.provider('grok.realtime unhandled server event', {\n type: event.type,\n })\n break\n }\n }\n\n function setupOutputAudioAnalysis(stream: MediaStream) {\n // Bail out if teardown has already started. `pc.ontrack` can fire\n // asynchronously after `teardownConnection()` has flipped `isTornDown`\n // (e.g. a remote track arriving mid-close); without this guard we'd\n // allocate a fresh AudioContext / audio element that nothing would ever\n // clean up.\n if (isTornDown) return\n\n // Tear down any prior output audio before allocating new resources.\n // `pc.ontrack` can fire multiple times over the lifetime of a session\n // (e.g. after renegotiation), and without this we'd leak audio elements\n // and analyser nodes.\n if (audioElement) {\n try {\n audioElement.pause()\n } catch {\n // ignore — element may already be unloaded\n }\n audioElement.srcObject = null\n audioElement = null\n }\n if (outputSource) {\n try {\n outputSource.disconnect()\n } catch {\n // ignore — may already be disconnected\n }\n outputSource = null\n }\n if (outputAnalyser) {\n try {\n outputAnalyser.disconnect()\n } catch {\n // ignore\n }\n outputAnalyser = null\n }\n\n audioElement = new Audio()\n audioElement.srcObject = stream\n audioElement.autoplay = true\n audioElement.play().catch((e) => {\n // Autoplay is commonly blocked until the user interacts with the page\n // (browser gesture requirement). Surfacing this as a fatal `error`\n // event makes the UI render a red/error state even though the\n // connection is healthy — the page just needs a click. Log at a\n // dedicated source tag so it's debuggable, but don't emit `error`.\n logger.errors('grok.realtime audio autoplay blocked', {\n error: e,\n source: 'grok.realtime.audio_permission_required',\n })\n })\n\n if (!audioContext) {\n audioContext = new AudioContext()\n }\n\n if (audioContext.state === 'suspended') {\n audioContext.resume().catch((err) => {\n // Same rationale as the autoplay catch: `resume()` failure usually\n // means the user hasn't interacted yet. Logging only — no error\n // emit — so the UI doesn't go into a fatal state for a recoverable\n // condition.\n logger.errors('grok.realtime audioContext.resume failed', {\n error: err,\n source: 'grok.realtime',\n })\n })\n }\n\n outputAnalyser = audioContext.createAnalyser()\n outputAnalyser.fftSize = 2048\n outputAnalyser.smoothingTimeConstant = 0.3\n\n outputSource = audioContext.createMediaStreamSource(stream)\n outputSource.connect(outputAnalyser)\n }\n\n function setupInputAudioAnalysis(stream: MediaStream) {\n // Defensive symmetry with `setupOutputAudioAnalysis`. Today this is\n // only called inline after SDP negotiation, but keeping the guard\n // means any future caller path (e.g. renegotiation) won't leak a fresh\n // AudioContext after teardown.\n if (isTornDown) return\n\n if (!audioContext) {\n audioContext = new AudioContext()\n }\n\n if (audioContext.state === 'suspended') {\n audioContext.resume().catch((err) => {\n // Same rationale as in setupOutputAudioAnalysis: a suspended\n // AudioContext usually resumes after a user gesture. Log only —\n // surfacing this as a fatal error makes the UI look broken for a\n // recoverable condition.\n logger.errors('grok.realtime audioContext.resume failed', {\n error: err,\n source: 'grok.realtime',\n })\n })\n }\n\n inputAnalyser = audioContext.createAnalyser()\n inputAnalyser.fftSize = 2048\n inputAnalyser.smoothingTimeConstant = 0.3\n\n inputSource = audioContext.createMediaStreamSource(stream)\n inputSource.connect(inputAnalyser)\n }\n\n function sendEvent(event: Record<string, unknown>) {\n if (isTornDown) {\n // The caller is holding onto a `connection` object after `disconnect()`\n // (or a failed connect). Silently queueing would leak memory and the\n // events would never flush. Log + drop so the misuse is visible in\n // debug mode without escalating to a throw — throwing from a React\n // useEffect cleanup path can break teardown ordering in the UI.\n logger.errors('grok.realtime sendEvent after disconnect', {\n eventType: readString(event, 'type') ?? '<unknown>',\n source: 'grok.realtime',\n })\n return\n }\n if (dataChannel?.readyState === 'open') {\n logger.provider(\n `provider=grok direction=out type=${readString(event, 'type') ?? '<unknown>'}`,\n { frame: event },\n )\n // Mirror the try/catch in `flushPendingEvents` — `dataChannel.send`\n // can synchronously throw if the channel flipped to `closing` between\n // our readyState check and this call, or if `JSON.stringify` chokes\n // on a caller-supplied payload. Log + emit error instead of letting\n // the exception propagate up through public `sendText` / `sendImage`\n // / `updateSession` call sites.\n try {\n dataChannel.send(JSON.stringify(event))\n } catch (error) {\n logger.errors('grok.realtime sendEvent failed', {\n error,\n eventType: readString(event, 'type') ?? '<unknown>',\n source: 'grok.realtime',\n })\n emit('error', {\n error: error instanceof Error ? error : new Error(String(error)),\n })\n }\n } else {\n pendingEvents.push(event)\n }\n }\n\n function flushPendingEvents() {\n try {\n for (const event of pendingEvents) {\n logger.provider(\n `provider=grok direction=out type=${readString(event, 'type') ?? '<unknown>'}`,\n { frame: event },\n )\n channel.send(JSON.stringify(event))\n }\n pendingEvents.length = 0\n } catch (error) {\n // A send failure here (e.g. dataChannel went from 'open' back to\n // 'closing' mid-flush, or JSON.stringify on a caller-provided event\n // threw) would otherwise be silently swallowed. By the time we're\n // called, `onopen` has already resolved `dataChannelReady`, so the\n // consumer-facing signal is `emit('error')` — try rejectDataChannelReady\n // as a defensive belt-and-braces in case this ever runs pre-resolve.\n logger.errors('grok.realtime flushPendingEvents failed', {\n error,\n source: 'grok.realtime',\n })\n const err = error instanceof Error ? error : new Error(String(error))\n rejectDataChannelReady?.(err)\n emit('error', { error: err })\n }\n }\n\n const connection: RealtimeConnection = {\n async disconnect() {\n // Reuse the same teardown path as the failed-connect branches so\n // every cleanup site stays in sync (input analyser, output analyser,\n // output source, audio element, etc.).\n await teardownConnection()\n emit('status_change', { status: 'idle' })\n },\n\n async startAudioCapture() {\n if (localStream) {\n for (const track of localStream.getAudioTracks()) {\n track.enabled = true\n }\n }\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n },\n\n stopAudioCapture() {\n if (localStream) {\n for (const track of localStream.getAudioTracks()) {\n track.enabled = false\n }\n }\n currentMode = 'idle'\n emit('mode_change', { mode: 'idle' })\n },\n\n sendText(text: string) {\n sendEvent({\n type: 'conversation.item.create',\n item: {\n type: 'message',\n role: 'user',\n content: [{ type: 'input_text', text }],\n },\n })\n sendEvent({ type: 'response.create' })\n },\n\n sendImage(imageData: string, mimeType: string) {\n // Accept:\n // - http(s):// URLs → forward as-is\n // - data: URIs (e.g. from FileReader.readAsDataURL) → forward as-is\n // so we don't double-wrap into `data:image/png;base64,data:image/png;base64,…`\n // - bare base64 → wrap in `data:${mimeType};base64,…`\n const isAlreadyUrlOrDataUri =\n imageData.startsWith('http://') ||\n imageData.startsWith('https://') ||\n imageData.startsWith('data:')\n const imageContent = {\n type: 'input_image',\n // The OpenAI-realtime content part (which this adapter mirrors) nests\n // the URL under an `image_url: { url: ... }` object, not a bare\n // string.\n image_url: {\n url: isAlreadyUrlOrDataUri\n ? imageData\n : `data:${mimeType};base64,${imageData}`,\n },\n }\n\n sendEvent({\n type: 'conversation.item.create',\n item: {\n type: 'message',\n role: 'user',\n content: [imageContent],\n },\n })\n sendEvent({ type: 'response.create' })\n },\n\n sendToolResult(callId: string, result: string) {\n sendEvent({\n type: 'conversation.item.create',\n item: {\n type: 'function_call_output',\n call_id: callId,\n output: result,\n },\n })\n sendEvent({ type: 'response.create' })\n },\n\n updateSession(config: Partial<RealtimeSessionConfig>) {\n const sessionUpdate: Record<string, unknown> = {}\n\n if (config.instructions) {\n sessionUpdate.instructions = config.instructions\n }\n\n if (config.voice) {\n sessionUpdate.voice = config.voice\n }\n\n if (config.vadMode) {\n if (config.vadMode === 'semantic') {\n sessionUpdate.turn_detection = {\n type: 'semantic_vad',\n eagerness: config.semanticEagerness ?? 'medium',\n }\n } else if (config.vadMode === 'server') {\n sessionUpdate.turn_detection = {\n type: 'server_vad',\n threshold: config.vadConfig?.threshold ?? 0.5,\n prefix_padding_ms: config.vadConfig?.prefixPaddingMs ?? 300,\n silence_duration_ms: config.vadConfig?.silenceDurationMs ?? 500,\n }\n } else {\n sessionUpdate.turn_detection = null\n }\n }\n\n if (config.tools !== undefined) {\n sessionUpdate.tools = config.tools.map((t) => ({\n type: 'function',\n name: t.name,\n description: t.description,\n parameters: t.inputSchema ?? { type: 'object', properties: {} },\n }))\n sessionUpdate.tool_choice = 'auto'\n }\n\n if (config.outputModalities) {\n sessionUpdate.modalities = config.outputModalities\n }\n\n if (config.temperature !== undefined) {\n sessionUpdate.temperature = config.temperature\n }\n\n if (config.maxOutputTokens !== undefined) {\n sessionUpdate.max_response_output_tokens = config.maxOutputTokens\n }\n\n // Let callers forward an explicit `input_audio_transcription` value\n // through `providerOptions` — including `null` / `false` to disable\n // the feature. Only apply our `grok-stt` default on the first\n // session.update and only if the caller hasn't set it themselves.\n const providerOptions: Record<string, unknown> =\n config.providerOptions ?? {}\n const callerTranscription =\n 'inputAudioTranscription' in providerOptions\n ? providerOptions.inputAudioTranscription\n : 'input_audio_transcription' in providerOptions\n ? providerOptions.input_audio_transcription\n : undefined\n if (callerTranscription !== undefined) {\n sessionUpdate.input_audio_transcription =\n callerTranscription === false ? null : callerTranscription\n } else if (!hasSentInitialSessionUpdate) {\n sessionUpdate.input_audio_transcription = { model: 'grok-stt' }\n }\n\n if (Object.keys(sessionUpdate).length > 0) {\n sendEvent({\n type: 'session.update',\n session: sessionUpdate,\n })\n hasSentInitialSessionUpdate = true\n }\n },\n\n interrupt() {\n sendEvent({ type: 'response.cancel' })\n currentMode = 'listening'\n emit('mode_change', { mode: 'listening' })\n emit('interrupted', {\n ...(currentMessageId !== null && { messageId: currentMessageId }),\n })\n },\n\n on<TEvent extends RealtimeEvent>(\n event: TEvent,\n handler: RealtimeEventHandler<TEvent>,\n ): () => void {\n let handlers = eventHandlers.get(event)\n if (!handlers) {\n handlers = new Set()\n eventHandlers.set(event, handlers)\n }\n handlers.add(handler)\n\n return () => {\n eventHandlers.get(event)?.delete(handler)\n }\n },\n\n getAudioVisualization(): AudioVisualization {\n function calculateLevel(analyser: AnalyserNode): number {\n const data = new Uint8Array(analyser.fftSize)\n analyser.getByteTimeDomainData(data)\n\n let maxDeviation = 0\n for (const sample of data) {\n const deviation = Math.abs(sample - 128)\n if (deviation > maxDeviation) {\n maxDeviation = deviation\n }\n }\n\n const normalized = maxDeviation / 128\n return Math.min(1, normalized * 1.5)\n }\n\n return {\n get inputLevel() {\n if (!inputAnalyser) return 0\n return calculateLevel(inputAnalyser)\n },\n\n get outputLevel() {\n if (!outputAnalyser) return 0\n return calculateLevel(outputAnalyser)\n },\n\n getInputFrequencyData() {\n if (!inputAnalyser)\n return new Uint8Array(FALLBACK_FREQUENCY_BIN_COUNT)\n const data = new Uint8Array(inputAnalyser.frequencyBinCount)\n inputAnalyser.getByteFrequencyData(data)\n return data\n },\n\n getOutputFrequencyData() {\n if (!outputAnalyser)\n return new Uint8Array(FALLBACK_FREQUENCY_BIN_COUNT)\n const data = new Uint8Array(outputAnalyser.frequencyBinCount)\n outputAnalyser.getByteFrequencyData(data)\n return data\n },\n\n getInputTimeDomainData() {\n if (!inputAnalyser)\n return new Uint8Array(FALLBACK_TIME_DOMAIN_SIZE).fill(\n FALLBACK_TIME_DOMAIN_FILL,\n )\n const data = new Uint8Array(inputAnalyser.fftSize)\n inputAnalyser.getByteTimeDomainData(data)\n return data\n },\n\n getOutputTimeDomainData() {\n if (!outputAnalyser)\n return new Uint8Array(FALLBACK_TIME_DOMAIN_SIZE).fill(\n FALLBACK_TIME_DOMAIN_FILL,\n )\n const data = new Uint8Array(outputAnalyser.fftSize)\n outputAnalyser.getByteTimeDomainData(data)\n return data\n },\n\n get inputSampleRate() {\n return 24000\n },\n\n get outputSampleRate() {\n return 24000\n },\n }\n },\n }\n\n // `dataChannelReady` was already awaited inside the post-SDP try/catch\n // above so we can short-circuit on failures with full teardown.\n return connection\n}\n"],"mappings":";;AAgBA,IAAM,oBAAoB;;;;;;;AAQ1B,SAAS,WACP,KACA,KACoB;CACpB,MAAM,QAAQ,IAAI;CAClB,OAAO,OAAO,UAAU,WAAW,QAAQ,KAAA;AAC7C;AAEA,SAAS,WACP,KACA,KACqC;CACrC,MAAM,QAAQ,IAAI;CAClB,OAAO,SAAS,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ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IAAI;KACxC,OAAO;IACT;IAEA,0BAA0B;KACxB,IAAI,CAAC,gBACH,OAAO,IAAI,WAAW,yBAAyB,CAAC,CAAC,KAC/C,yBACF;KACF,MAAM,OAAO,IAAI,WAAW,eAAe,OAAO;KAClD,eAAe,sBAAsB,IAAI;KACzC,OAAO;IACT;IAEA,IAAI,kBAAkB;KACpB,OAAO;IACT;IAEA,IAAI,mBAAmB;KACrB,OAAO;IACT;GACF;EACF;CAKK;AACT"}
|
|
@@ -1,75 +1,81 @@
|
|
|
1
|
-
import { resolveDebugOption } from "@tanstack/ai/adapter-internals";
|
|
2
|
-
import "@tanstack/ai-utils";
|
|
3
1
|
import { getGrokApiKeyFromEnv } from "../utils/client.js";
|
|
4
|
-
import "
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import "../utils/index.js";
|
|
3
|
+
import { resolveDebugOption } from "@tanstack/ai/adapter-internals";
|
|
4
|
+
//#region src/realtime/token.ts
|
|
5
|
+
var GROK_REALTIME_CLIENT_SECRETS_URL = "https://api.x.ai/v1/realtime/client_secrets";
|
|
6
|
+
var DEFAULT_TOKEN_FETCH_TIMEOUT_MS = 15e3;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Grok realtime token adapter.
|
|
9
|
+
*
|
|
10
|
+
* Generates ephemeral client secrets for browser-side WebRTC connections to
|
|
11
|
+
* the xAI Voice Agent API.
|
|
12
|
+
*
|
|
13
|
+
* @param options - Configuration options for the realtime session.
|
|
14
|
+
* @returns A RealtimeTokenAdapter for use with `realtimeToken()`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { realtimeToken } from '@tanstack/ai'
|
|
19
|
+
* import { grokRealtimeToken } from '@tanstack/ai-grok'
|
|
20
|
+
*
|
|
21
|
+
* const token = await realtimeToken({
|
|
22
|
+
* adapter: grokRealtimeToken({ model: 'grok-voice-fast-1.0' }),
|
|
23
|
+
* })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
7
26
|
function grokRealtimeToken(options = {}) {
|
|
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
|
-
} catch (error) {
|
|
61
|
-
logger.errors("grok.realtimeToken fatal", {
|
|
62
|
-
error,
|
|
63
|
-
source: "grok.realtimeToken"
|
|
64
|
-
});
|
|
65
|
-
throw error;
|
|
66
|
-
} finally {
|
|
67
|
-
clearTimeout(timeout);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
};
|
|
27
|
+
const apiKey = getGrokApiKeyFromEnv();
|
|
28
|
+
const logger = resolveDebugOption(options.debug);
|
|
29
|
+
return {
|
|
30
|
+
provider: "grok",
|
|
31
|
+
async generateToken() {
|
|
32
|
+
const model = options.model ?? "grok-voice-fast-1.0";
|
|
33
|
+
logger.request(`activity=realtimeToken provider=grok model=${model}`, {
|
|
34
|
+
provider: "grok",
|
|
35
|
+
model
|
|
36
|
+
});
|
|
37
|
+
const requestBody = { session: { model } };
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
const timeout = setTimeout(() => controller.abort(/* @__PURE__ */ new Error("Grok realtime token request timed out")), DEFAULT_TOKEN_FETCH_TIMEOUT_MS);
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(GROK_REALTIME_CLIENT_SECRETS_URL, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
headers: {
|
|
44
|
+
Authorization: `Bearer ${apiKey}`,
|
|
45
|
+
"Content-Type": "application/json"
|
|
46
|
+
},
|
|
47
|
+
body: JSON.stringify(requestBody),
|
|
48
|
+
signal: controller.signal
|
|
49
|
+
});
|
|
50
|
+
if (!response.ok) {
|
|
51
|
+
const errorText = await response.text();
|
|
52
|
+
throw new Error(`Grok realtime session creation failed: ${response.status} ${errorText}`);
|
|
53
|
+
}
|
|
54
|
+
const sessionData = await response.json();
|
|
55
|
+
const clientSecret = sessionData?.client_secret;
|
|
56
|
+
if (!clientSecret || typeof clientSecret.value !== "string" || typeof clientSecret.expires_at !== "number" || !Number.isFinite(clientSecret.expires_at)) throw new Error("Grok realtime session response missing or malformed `client_secret`");
|
|
57
|
+
const sessionModel = sessionData.model ?? model;
|
|
58
|
+
const raw = clientSecret.expires_at;
|
|
59
|
+
const expiresAt = raw > 0xe8d4a51000 ? raw : raw * 1e3;
|
|
60
|
+
return {
|
|
61
|
+
provider: "grok",
|
|
62
|
+
token: clientSecret.value,
|
|
63
|
+
expiresAt,
|
|
64
|
+
config: { model: sessionModel }
|
|
65
|
+
};
|
|
66
|
+
} catch (error) {
|
|
67
|
+
logger.errors("grok.realtimeToken fatal", {
|
|
68
|
+
error,
|
|
69
|
+
source: "grok.realtimeToken"
|
|
70
|
+
});
|
|
71
|
+
throw error;
|
|
72
|
+
} finally {
|
|
73
|
+
clearTimeout(timeout);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
71
77
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
//# sourceMappingURL=token.js.map
|
|
78
|
+
//#endregion
|
|
79
|
+
export { grokRealtimeToken };
|
|
80
|
+
|
|
81
|
+
//# sourceMappingURL=token.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sources":["../../../src/realtime/token.ts"],"sourcesContent":["import { resolveDebugOption } from '@tanstack/ai/adapter-internals'\nimport { getGrokApiKeyFromEnv } from '../utils'\nimport type { RealtimeToken, RealtimeTokenAdapter } from '@tanstack/ai'\nimport type { GrokRealtimeModel } from '../model-meta'\nimport type {\n GrokRealtimeSessionResponse,\n GrokRealtimeTokenOptions,\n} from './types'\n\nconst GROK_REALTIME_CLIENT_SECRETS_URL =\n 'https://api.x.ai/v1/realtime/client_secrets'\n\nconst DEFAULT_TOKEN_FETCH_TIMEOUT_MS = 15_000\n\n/**\n * Creates a Grok realtime token adapter.\n *\n * Generates ephemeral client secrets for browser-side WebRTC connections to\n * the xAI Voice Agent API.\n *\n * @param options - Configuration options for the realtime session.\n * @returns A RealtimeTokenAdapter for use with `realtimeToken()`.\n *\n * @example\n * ```typescript\n * import { realtimeToken } from '@tanstack/ai'\n * import { grokRealtimeToken } from '@tanstack/ai-grok'\n *\n * const token = await realtimeToken({\n * adapter: grokRealtimeToken({ model: 'grok-voice-fast-1.0' }),\n * })\n * ```\n */\nexport function grokRealtimeToken(\n options: GrokRealtimeTokenOptions = {},\n): RealtimeTokenAdapter {\n const apiKey = getGrokApiKeyFromEnv()\n const logger = resolveDebugOption(options.debug)\n\n return {\n provider: 'grok',\n\n async generateToken(): Promise<RealtimeToken> {\n const model: GrokRealtimeModel = options.model ?? 'grok-voice-fast-1.0'\n\n logger.request(`activity=realtimeToken provider=grok model=${model}`, {\n provider: 'grok',\n model,\n })\n\n // xAI docs (docs.x.ai/developers/rest-api-reference/inference/voice)\n // specify the body as `{ session: { model } }`. `expires_after` is\n // available to shorten the default 600s TTL but we don't expose it\n // yet — the caller can still call `generateToken()` more often if\n // they want a shorter-lived session.\n const requestBody: Record<string, unknown> = {\n session: { model },\n }\n\n // Abort the fetch if xAI never responds. Without this the whole\n // realtime connect flow hangs forever on a dead endpoint.\n const controller = new AbortController()\n const timeout = setTimeout(\n () =>\n controller.abort(new Error('Grok realtime token request timed out')),\n DEFAULT_TOKEN_FETCH_TIMEOUT_MS,\n )\n\n try {\n const response = await fetch(GROK_REALTIME_CLIENT_SECRETS_URL, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(requestBody),\n signal: controller.signal,\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new Error(\n `Grok realtime session creation failed: ${response.status} ${errorText}`,\n )\n }\n\n const sessionData = (await response.json()) as\n | Partial<GrokRealtimeSessionResponse>\n | undefined\n\n // Validate shape before dereferencing — xAI could return an error\n // envelope with 200 status, or a partial response under protocol drift.\n const clientSecret = sessionData?.client_secret\n if (\n !clientSecret ||\n typeof clientSecret.value !== 'string' ||\n typeof clientSecret.expires_at !== 'number' ||\n !Number.isFinite(clientSecret.expires_at)\n ) {\n throw new Error(\n 'Grok realtime session response missing or malformed `client_secret`',\n )\n }\n const sessionModel = sessionData.model ?? model\n\n // xAI docs describe `expires_at` as a unix timestamp in seconds, but\n // in practice different deployments have returned milliseconds. Treat\n // any value that already looks like ms (>1e12 ≈ Sep 2001 in ms) as ms.\n const raw = clientSecret.expires_at\n const expiresAt = raw > 1e12 ? raw : raw * 1000\n\n return {\n provider: 'grok',\n token: clientSecret.value,\n expiresAt,\n config: {\n model: sessionModel,\n },\n }\n } catch (error) {\n logger.errors('grok.realtimeToken fatal', {\n error,\n source: 'grok.realtimeToken',\n })\n throw error\n } finally {\n clearTimeout(timeout)\n }\n },\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"token.js","names":[],"sources":["../../../src/realtime/token.ts"],"sourcesContent":["import { resolveDebugOption } from '@tanstack/ai/adapter-internals'\nimport { getGrokApiKeyFromEnv } from '../utils'\nimport type { RealtimeToken, RealtimeTokenAdapter } from '@tanstack/ai'\nimport type { GrokRealtimeModel } from '../model-meta'\nimport type {\n GrokRealtimeSessionResponse,\n GrokRealtimeTokenOptions,\n} from './types'\n\nconst GROK_REALTIME_CLIENT_SECRETS_URL =\n 'https://api.x.ai/v1/realtime/client_secrets'\n\nconst DEFAULT_TOKEN_FETCH_TIMEOUT_MS = 15_000\n\n/**\n * Creates a Grok realtime token adapter.\n *\n * Generates ephemeral client secrets for browser-side WebRTC connections to\n * the xAI Voice Agent API.\n *\n * @param options - Configuration options for the realtime session.\n * @returns A RealtimeTokenAdapter for use with `realtimeToken()`.\n *\n * @example\n * ```typescript\n * import { realtimeToken } from '@tanstack/ai'\n * import { grokRealtimeToken } from '@tanstack/ai-grok'\n *\n * const token = await realtimeToken({\n * adapter: grokRealtimeToken({ model: 'grok-voice-fast-1.0' }),\n * })\n * ```\n */\nexport function grokRealtimeToken(\n options: GrokRealtimeTokenOptions = {},\n): RealtimeTokenAdapter {\n const apiKey = getGrokApiKeyFromEnv()\n const logger = resolveDebugOption(options.debug)\n\n return {\n provider: 'grok',\n\n async generateToken(): Promise<RealtimeToken> {\n const model: GrokRealtimeModel = options.model ?? 'grok-voice-fast-1.0'\n\n logger.request(`activity=realtimeToken provider=grok model=${model}`, {\n provider: 'grok',\n model,\n })\n\n // xAI docs (docs.x.ai/developers/rest-api-reference/inference/voice)\n // specify the body as `{ session: { model } }`. `expires_after` is\n // available to shorten the default 600s TTL but we don't expose it\n // yet — the caller can still call `generateToken()` more often if\n // they want a shorter-lived session.\n const requestBody: Record<string, unknown> = {\n session: { model },\n }\n\n // Abort the fetch if xAI never responds. Without this the whole\n // realtime connect flow hangs forever on a dead endpoint.\n const controller = new AbortController()\n const timeout = setTimeout(\n () =>\n controller.abort(new Error('Grok realtime token request timed out')),\n DEFAULT_TOKEN_FETCH_TIMEOUT_MS,\n )\n\n try {\n const response = await fetch(GROK_REALTIME_CLIENT_SECRETS_URL, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(requestBody),\n signal: controller.signal,\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new Error(\n `Grok realtime session creation failed: ${response.status} ${errorText}`,\n )\n }\n\n const sessionData = (await response.json()) as\n | Partial<GrokRealtimeSessionResponse>\n | undefined\n\n // Validate shape before dereferencing — xAI could return an error\n // envelope with 200 status, or a partial response under protocol drift.\n const clientSecret = sessionData?.client_secret\n if (\n !clientSecret ||\n typeof clientSecret.value !== 'string' ||\n typeof clientSecret.expires_at !== 'number' ||\n !Number.isFinite(clientSecret.expires_at)\n ) {\n throw new Error(\n 'Grok realtime session response missing or malformed `client_secret`',\n )\n }\n const sessionModel = sessionData.model ?? model\n\n // xAI docs describe `expires_at` as a unix timestamp in seconds, but\n // in practice different deployments have returned milliseconds. Treat\n // any value that already looks like ms (>1e12 ≈ Sep 2001 in ms) as ms.\n const raw = clientSecret.expires_at\n const expiresAt = raw > 1e12 ? raw : raw * 1000\n\n return {\n provider: 'grok',\n token: clientSecret.value,\n expiresAt,\n config: {\n model: sessionModel,\n },\n }\n } catch (error) {\n logger.errors('grok.realtimeToken fatal', {\n error,\n source: 'grok.realtimeToken',\n })\n throw error\n } finally {\n clearTimeout(timeout)\n }\n },\n }\n}\n"],"mappings":";;;;AASA,IAAM,mCACJ;AAEF,IAAM,iCAAiC;;;;;;;;;;;;;;;;;;;;AAqBvC,SAAgB,kBACd,UAAoC,CAAC,GACf;CACtB,MAAM,SAAS,qBAAqB;CACpC,MAAM,SAAS,mBAAmB,QAAQ,KAAK;CAE/C,OAAO;EACL,UAAU;EAEV,MAAM,gBAAwC;GAC5C,MAAM,QAA2B,QAAQ,SAAS;GAElD,OAAO,QAAQ,8CAA8C,SAAS;IACpE,UAAU;IACV;GACF,CAAC;GAOD,MAAM,cAAuC,EAC3C,SAAS,EAAE,MAAM,EACnB;GAIA,MAAM,aAAa,IAAI,gBAAgB;GACvC,MAAM,UAAU,iBAEZ,WAAW,sBAAM,IAAI,MAAM,uCAAuC,CAAC,GACrE,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