@tanstack/ai-grok 0.13.0 → 0.14.1
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/video.d.ts +128 -0
- package/dist/esm/adapters/video.js +237 -0
- package/dist/esm/adapters/video.js.map +1 -0
- package/dist/esm/index.d.ts +5 -2
- package/dist/esm/index.js +10 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/model-meta.d.ts +7 -0
- package/dist/esm/model-meta.js +12 -1
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/realtime/adapter.d.ts +1 -1
- package/dist/esm/realtime/adapter.js.map +1 -1
- package/dist/esm/video/video-provider-options.d.ts +135 -0
- package/dist/esm/video/video-provider-options.js +67 -0
- package/dist/esm/video/video-provider-options.js.map +1 -0
- package/package.json +4 -5
- package/src/adapters/video.ts +462 -0
- package/src/index.ts +23 -0
- package/src/model-meta.ts +52 -0
- package/src/realtime/adapter.ts +2 -1
- package/src/video/video-provider-options.ts +241 -0
- package/dist/esm/realtime/realtime-contract.d.ts +0 -30
- package/src/realtime/realtime-contract.ts +0 -46
|
@@ -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 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 { RealtimeAdapter, RealtimeConnection } from './realtime-contract'\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":";AAeA,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,QAAQ,KAAK;AAE/C,SAAO;AAAA,IACL,UAAU;AAAA,IAEV,MAAM,QACJ,OACA,cAC6B;AAC7B,YAAM,QAAQ,MAAM,OAAO,SAAS;AACpC,aAAO,QAAQ,yCAAyC,KAAK,IAAI;AAAA,QAC/D,UAAU;AAAA,QACV;AAAA,MAAA,CACD;AAED,UAAI,mBAAmB,UAAU;AAC/B,eAAO,uBAAuB,OAAO,MAAM;AAAA,MAC7C;AACA,YAAM,QAAQ,IAAI,MAAM,+CAA+C;AACvE,aAAO,OAAO,uBAAuB;AAAA,QACnC;AAAA,QACA,QAAQ;AAAA,MAAA,CACT;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AA,QACL,IAAI,aAAa;AACf,cAAI,CAAC,cAAe,QAAO;AAC3B,iBAAO,eAAe,aAAa;AAAA,QACrC;AAAA,QAEA,IAAI,cAAc;AAChB,cAAI,CAAC,eAAgB,QAAO;AAC5B,iBAAO,eAAe,cAAc;AAAA,QACtC;AAAA,QAEA,wBAAwB;AACtB,cAAI,CAAC;AACH,mBAAO,IAAI,WAAW,4BAA4B;AACpD,gBAAM,OAAO,IAAI,WAAW,cAAc,iBAAiB;AAC3D,wBAAc,qBAAqB,IAAI;AACvC,iBAAO;AAAA,QACT;AAAA,QAEA,yBAAyB;AACvB,cAAI,CAAC;AACH,mBAAO,IAAI,WAAW,4BAA4B;AACpD,gBAAM,OAAO,IAAI,WAAW,eAAe,iBAAiB;AAC5D,yBAAe,qBAAqB,IAAI;AACxC,iBAAO;AAAA,QACT;AAAA,QAEA,yBAAyB;AACvB,cAAI,CAAC;AACH,mBAAO,IAAI,WAAW,yBAAyB,EAAE;AAAA,cAC/C;AAAA,YAAA;AAEJ,gBAAM,OAAO,IAAI,WAAW,cAAc,OAAO;AACjD,wBAAc,sBAAsB,IAAI;AACxC,iBAAO;AAAA,QACT;AAAA,QAEA,0BAA0B;AACxB,cAAI,CAAC;AACH,mBAAO,IAAI,WAAW,yBAAyB,EAAE;AAAA,cAC/C;AAAA,YAAA;AAEJ,gBAAM,OAAO,IAAI,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","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,QAAQ,KAAK;AAE/C,SAAO;AAAA,IACL,UAAU;AAAA,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;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;"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { DurationOptions } from '@tanstack/ai/adapters';
|
|
2
|
+
import { GrokVideoModel } from '../model-meta.js';
|
|
3
|
+
/**
|
|
4
|
+
* Aspect ratios accepted by the grok-imagine video models.
|
|
5
|
+
*
|
|
6
|
+
* Note: this is a narrower set than the grok-imagine image models — the
|
|
7
|
+
* video endpoint rejects the phone-screen ratios ('9:19.5', '9:20', …) and
|
|
8
|
+
* 'auto'.
|
|
9
|
+
*
|
|
10
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
11
|
+
*/
|
|
12
|
+
export type GrokVideoAspectRatio = '1:1' | '16:9' | '9:16' | '4:3' | '3:4' | '3:2' | '2:3';
|
|
13
|
+
/**
|
|
14
|
+
* Resolution tiers for the grok-imagine video models.
|
|
15
|
+
*
|
|
16
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
17
|
+
*/
|
|
18
|
+
export type GrokVideoResolution = '480p' | '720p' | '1080p';
|
|
19
|
+
/**
|
|
20
|
+
* Size strings for grok-imagine video models. The Imagine API is
|
|
21
|
+
* aspect-ratio based rather than pixel-size based; like the grok-imagine
|
|
22
|
+
* image models, the generic `size` option uses an
|
|
23
|
+
* `aspectRatio_resolution` template ("16:9_720p") — the resolution suffix
|
|
24
|
+
* is optional ("16:9" uses the API default).
|
|
25
|
+
*
|
|
26
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
27
|
+
*/
|
|
28
|
+
export type GrokVideoSize = GrokVideoAspectRatio | `${GrokVideoAspectRatio}_${GrokVideoResolution}`;
|
|
29
|
+
/**
|
|
30
|
+
* Video duration limits enforced by the Imagine API (seconds).
|
|
31
|
+
*/
|
|
32
|
+
export declare const GROK_VIDEO_MIN_DURATION = 1;
|
|
33
|
+
export declare const GROK_VIDEO_MAX_DURATION = 15;
|
|
34
|
+
/**
|
|
35
|
+
* Parses a grok video size string into its components.
|
|
36
|
+
* Format: "aspectRatio" or "aspectRatio_resolution",
|
|
37
|
+
* e.g. "16:9_720p" → { aspectRatio: "16:9", resolution: "720p" }.
|
|
38
|
+
* Returns undefined when the string doesn't match the template.
|
|
39
|
+
*/
|
|
40
|
+
export declare function parseGrokVideoSize(size: string): {
|
|
41
|
+
aspectRatio: string;
|
|
42
|
+
resolution?: string;
|
|
43
|
+
} | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Validate the `size` template for a given grok video model.
|
|
46
|
+
*
|
|
47
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
48
|
+
*/
|
|
49
|
+
export declare function validateVideoSize(model: string, size?: string): asserts size is GrokVideoSize | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Per-model duration type. The Imagine API accepts any integer second in the
|
|
52
|
+
* 1–15 range, so this is a continuous range expressed as `number` (a literal
|
|
53
|
+
* union can't represent it). `snapDuration()` coerces a raw seconds value into
|
|
54
|
+
* the valid range at runtime.
|
|
55
|
+
*
|
|
56
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
57
|
+
*/
|
|
58
|
+
export type GrokVideoModelDurationByName = {
|
|
59
|
+
'grok-imagine-video': number;
|
|
60
|
+
'grok-imagine-video-1.5': number;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Runtime duration table backing `availableDurations()` / `snapDuration()`.
|
|
64
|
+
* Both grok-imagine video models accept the same continuous 1–15 integer-second
|
|
65
|
+
* range.
|
|
66
|
+
*
|
|
67
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
68
|
+
*/
|
|
69
|
+
export declare const GROK_VIDEO_DURATIONS: {
|
|
70
|
+
readonly [TModel in GrokVideoModel]: DurationOptions<GrokVideoModelDurationByName[TModel]>;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Look up the duration options for a grok video model.
|
|
74
|
+
*
|
|
75
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
76
|
+
*/
|
|
77
|
+
export declare function getGrokVideoDurationOptions<TModel extends GrokVideoModel>(model: TModel): DurationOptions<GrokVideoModelDurationByName[TModel]>;
|
|
78
|
+
/**
|
|
79
|
+
* Provider-specific options for grok video generation. These map directly
|
|
80
|
+
* onto the Imagine API request body and take precedence over the generic
|
|
81
|
+
* `size` / `duration` options when both are provided.
|
|
82
|
+
*
|
|
83
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
84
|
+
*/
|
|
85
|
+
export interface GrokVideoProviderOptions {
|
|
86
|
+
/**
|
|
87
|
+
* Output aspect ratio.
|
|
88
|
+
*/
|
|
89
|
+
aspect_ratio?: GrokVideoAspectRatio;
|
|
90
|
+
/**
|
|
91
|
+
* Output resolution tier.
|
|
92
|
+
*/
|
|
93
|
+
resolution?: GrokVideoResolution;
|
|
94
|
+
/**
|
|
95
|
+
* Video duration in integer seconds (1–15).
|
|
96
|
+
*/
|
|
97
|
+
duration?: number;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Type-only map from model name to its specific provider options.
|
|
101
|
+
*
|
|
102
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
103
|
+
*/
|
|
104
|
+
export type GrokVideoModelProviderOptionsByName = {
|
|
105
|
+
'grok-imagine-video': GrokVideoProviderOptions;
|
|
106
|
+
'grok-imagine-video-1.5': GrokVideoProviderOptions;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Type-only map from model name to its supported `size` strings.
|
|
110
|
+
*
|
|
111
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
112
|
+
*/
|
|
113
|
+
export type GrokVideoModelSizeByName = {
|
|
114
|
+
'grok-imagine-video': GrokVideoSize;
|
|
115
|
+
'grok-imagine-video-1.5': GrokVideoSize;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Type-only map from model name to the non-text prompt modalities it accepts.
|
|
119
|
+
* Both models accept an `image` prompt part as the starting frame:
|
|
120
|
+
* `grok-imagine-video` (v1.0) does text-to-video and image-to-video, while
|
|
121
|
+
* `grok-imagine-video-1.5` is image-to-video only (the image is required).
|
|
122
|
+
*
|
|
123
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
124
|
+
*/
|
|
125
|
+
export type GrokVideoModelInputModalitiesByName = {
|
|
126
|
+
'grok-imagine-video': readonly ['image'];
|
|
127
|
+
'grok-imagine-video-1.5': readonly ['image'];
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* True when the model only supports image-to-video (a starting frame is
|
|
131
|
+
* required).
|
|
132
|
+
*
|
|
133
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
134
|
+
*/
|
|
135
|
+
export declare function isImageToVideoOnlyModel(model: string): boolean;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const GROK_VIDEO_ASPECT_RATIOS = [
|
|
2
|
+
"1:1",
|
|
3
|
+
"16:9",
|
|
4
|
+
"9:16",
|
|
5
|
+
"4:3",
|
|
6
|
+
"3:4",
|
|
7
|
+
"3:2",
|
|
8
|
+
"2:3"
|
|
9
|
+
];
|
|
10
|
+
const GROK_VIDEO_RESOLUTIONS = ["480p", "720p", "1080p"];
|
|
11
|
+
const GROK_VIDEO_MIN_DURATION = 1;
|
|
12
|
+
const GROK_VIDEO_MAX_DURATION = 15;
|
|
13
|
+
function parseGrokVideoSize(size) {
|
|
14
|
+
const match = size.match(/^([\d.]+:[\d.]+)(?:_(.+))?$/);
|
|
15
|
+
const [, aspectRatio, resolution] = match ?? [];
|
|
16
|
+
if (aspectRatio === void 0) return void 0;
|
|
17
|
+
return { aspectRatio, ...resolution !== void 0 && { resolution } };
|
|
18
|
+
}
|
|
19
|
+
function validateVideoSize(model, size) {
|
|
20
|
+
if (size === void 0) return;
|
|
21
|
+
const parsed = parseGrokVideoSize(size);
|
|
22
|
+
if (!parsed || !GROK_VIDEO_ASPECT_RATIOS.includes(parsed.aspectRatio)) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`Size "${size}" is not supported by model "${model}". Expected "aspectRatio" or "aspectRatio_resolution" (e.g. "16:9_720p") with aspect ratio one of: ${GROK_VIDEO_ASPECT_RATIOS.join(", ")}`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
if (parsed.resolution !== void 0 && !GROK_VIDEO_RESOLUTIONS.includes(parsed.resolution)) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`Resolution "${parsed.resolution}" is not supported by model "${model}". Supported resolutions: ${GROK_VIDEO_RESOLUTIONS.join(", ")}`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const GROK_VIDEO_DURATIONS = {
|
|
34
|
+
"grok-imagine-video": {
|
|
35
|
+
kind: "range",
|
|
36
|
+
min: GROK_VIDEO_MIN_DURATION,
|
|
37
|
+
max: GROK_VIDEO_MAX_DURATION,
|
|
38
|
+
step: 1,
|
|
39
|
+
unit: "seconds"
|
|
40
|
+
},
|
|
41
|
+
"grok-imagine-video-1.5": {
|
|
42
|
+
kind: "range",
|
|
43
|
+
min: GROK_VIDEO_MIN_DURATION,
|
|
44
|
+
max: GROK_VIDEO_MAX_DURATION,
|
|
45
|
+
step: 1,
|
|
46
|
+
unit: "seconds"
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
function getGrokVideoDurationOptions(model) {
|
|
50
|
+
return GROK_VIDEO_DURATIONS[model];
|
|
51
|
+
}
|
|
52
|
+
const GROK_VIDEO_IMAGE_TO_VIDEO_ONLY = /* @__PURE__ */ new Set([
|
|
53
|
+
"grok-imagine-video-1.5"
|
|
54
|
+
]);
|
|
55
|
+
function isImageToVideoOnlyModel(model) {
|
|
56
|
+
return GROK_VIDEO_IMAGE_TO_VIDEO_ONLY.has(model);
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
GROK_VIDEO_DURATIONS,
|
|
60
|
+
GROK_VIDEO_MAX_DURATION,
|
|
61
|
+
GROK_VIDEO_MIN_DURATION,
|
|
62
|
+
getGrokVideoDurationOptions,
|
|
63
|
+
isImageToVideoOnlyModel,
|
|
64
|
+
parseGrokVideoSize,
|
|
65
|
+
validateVideoSize
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=video-provider-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"video-provider-options.js","sources":["../../../src/video/video-provider-options.ts"],"sourcesContent":["/**\n * Grok Video Generation Provider Options (xAI Imagine API)\n *\n * Based on https://docs.x.ai/docs/guides/video-generations\n *\n * @experimental Video generation is an experimental feature and may change.\n */\n\nimport type { DurationOptions } from '@tanstack/ai/adapters'\nimport type { GrokVideoModel } from '../model-meta'\n\n/**\n * Aspect ratios accepted by the grok-imagine video models.\n *\n * Note: this is a narrower set than the grok-imagine image models — the\n * video endpoint rejects the phone-screen ratios ('9:19.5', '9:20', …) and\n * 'auto'.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type GrokVideoAspectRatio =\n | '1:1'\n | '16:9'\n | '9:16'\n | '4:3'\n | '3:4'\n | '3:2'\n | '2:3'\n\n/**\n * Resolution tiers for the grok-imagine video models.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type GrokVideoResolution = '480p' | '720p' | '1080p'\n\n/**\n * Size strings for grok-imagine video models. The Imagine API is\n * aspect-ratio based rather than pixel-size based; like the grok-imagine\n * image models, the generic `size` option uses an\n * `aspectRatio_resolution` template (\"16:9_720p\") — the resolution suffix\n * is optional (\"16:9\" uses the API default).\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type GrokVideoSize =\n | GrokVideoAspectRatio\n | `${GrokVideoAspectRatio}_${GrokVideoResolution}`\n\nconst GROK_VIDEO_ASPECT_RATIOS: ReadonlyArray<string> = [\n '1:1',\n '16:9',\n '9:16',\n '4:3',\n '3:4',\n '3:2',\n '2:3',\n]\n\nconst GROK_VIDEO_RESOLUTIONS: ReadonlyArray<string> = ['480p', '720p', '1080p']\n\n/**\n * Video duration limits enforced by the Imagine API (seconds).\n */\nexport const GROK_VIDEO_MIN_DURATION = 1\nexport const GROK_VIDEO_MAX_DURATION = 15\n\n/**\n * Parses a grok video size string into its components.\n * Format: \"aspectRatio\" or \"aspectRatio_resolution\",\n * e.g. \"16:9_720p\" → { aspectRatio: \"16:9\", resolution: \"720p\" }.\n * Returns undefined when the string doesn't match the template.\n */\nexport function parseGrokVideoSize(\n size: string,\n): { aspectRatio: string; resolution?: string } | undefined {\n const match = size.match(/^([\\d.]+:[\\d.]+)(?:_(.+))?$/)\n const [, aspectRatio, resolution] = match ?? []\n if (aspectRatio === undefined) return undefined\n return { aspectRatio, ...(resolution !== undefined && { resolution }) }\n}\n\n/**\n * Validate the `size` template for a given grok video model.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport function validateVideoSize(\n model: string,\n size?: string,\n): asserts size is GrokVideoSize | undefined {\n if (size === undefined) return\n const parsed = parseGrokVideoSize(size)\n if (!parsed || !GROK_VIDEO_ASPECT_RATIOS.includes(parsed.aspectRatio)) {\n throw new Error(\n `Size \"${size}\" is not supported by model \"${model}\". Expected ` +\n `\"aspectRatio\" or \"aspectRatio_resolution\" (e.g. \"16:9_720p\") with ` +\n `aspect ratio one of: ${GROK_VIDEO_ASPECT_RATIOS.join(', ')}`,\n )\n }\n if (\n parsed.resolution !== undefined &&\n !GROK_VIDEO_RESOLUTIONS.includes(parsed.resolution)\n ) {\n throw new Error(\n `Resolution \"${parsed.resolution}\" is not supported by model \"${model}\". ` +\n `Supported resolutions: ${GROK_VIDEO_RESOLUTIONS.join(', ')}`,\n )\n }\n}\n\n/**\n * Per-model duration type. The Imagine API accepts any integer second in the\n * 1–15 range, so this is a continuous range expressed as `number` (a literal\n * union can't represent it). `snapDuration()` coerces a raw seconds value into\n * the valid range at runtime.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type GrokVideoModelDurationByName = {\n 'grok-imagine-video': number\n 'grok-imagine-video-1.5': number\n}\n\n/**\n * Runtime duration table backing `availableDurations()` / `snapDuration()`.\n * Both grok-imagine video models accept the same continuous 1–15 integer-second\n * range.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport const GROK_VIDEO_DURATIONS: {\n readonly [TModel in GrokVideoModel]: DurationOptions<\n GrokVideoModelDurationByName[TModel]\n >\n} = {\n 'grok-imagine-video': {\n kind: 'range',\n min: GROK_VIDEO_MIN_DURATION,\n max: GROK_VIDEO_MAX_DURATION,\n step: 1,\n unit: 'seconds',\n },\n 'grok-imagine-video-1.5': {\n kind: 'range',\n min: GROK_VIDEO_MIN_DURATION,\n max: GROK_VIDEO_MAX_DURATION,\n step: 1,\n unit: 'seconds',\n },\n}\n\n/**\n * Look up the duration options for a grok video model.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport function getGrokVideoDurationOptions<TModel extends GrokVideoModel>(\n model: TModel,\n): DurationOptions<GrokVideoModelDurationByName[TModel]> {\n return GROK_VIDEO_DURATIONS[model]\n}\n\n/**\n * Provider-specific options for grok video generation. These map directly\n * onto the Imagine API request body and take precedence over the generic\n * `size` / `duration` options when both are provided.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface GrokVideoProviderOptions {\n /**\n * Output aspect ratio.\n */\n aspect_ratio?: GrokVideoAspectRatio\n\n /**\n * Output resolution tier.\n */\n resolution?: GrokVideoResolution\n\n /**\n * Video duration in integer seconds (1–15).\n */\n duration?: number\n}\n\n/**\n * Type-only map from model name to its specific provider options.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type GrokVideoModelProviderOptionsByName = {\n 'grok-imagine-video': GrokVideoProviderOptions\n 'grok-imagine-video-1.5': GrokVideoProviderOptions\n}\n\n/**\n * Type-only map from model name to its supported `size` strings.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type GrokVideoModelSizeByName = {\n 'grok-imagine-video': GrokVideoSize\n 'grok-imagine-video-1.5': GrokVideoSize\n}\n\n/**\n * Type-only map from model name to the non-text prompt modalities it accepts.\n * Both models accept an `image` prompt part as the starting frame:\n * `grok-imagine-video` (v1.0) does text-to-video and image-to-video, while\n * `grok-imagine-video-1.5` is image-to-video only (the image is required).\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type GrokVideoModelInputModalitiesByName = {\n 'grok-imagine-video': readonly ['image']\n 'grok-imagine-video-1.5': readonly ['image']\n}\n\n/**\n * Models that only support image-to-video — a starting-frame image is\n * required and text-to-video is rejected by the Imagine API. Used by the\n * adapter to fail fast with a clear message instead of surfacing the raw\n * \"Text-to-video is not supported for this model\" 400.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nconst GROK_VIDEO_IMAGE_TO_VIDEO_ONLY: ReadonlySet<string> = new Set([\n 'grok-imagine-video-1.5',\n])\n\n/**\n * True when the model only supports image-to-video (a starting frame is\n * required).\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport function isImageToVideoOnlyModel(model: string): boolean {\n return GROK_VIDEO_IMAGE_TO_VIDEO_ONLY.has(model)\n}\n"],"names":[],"mappings":"AAiDA,MAAM,2BAAkD;AAAA,EACtD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,yBAAgD,CAAC,QAAQ,QAAQ,OAAO;AAKvE,MAAM,0BAA0B;AAChC,MAAM,0BAA0B;AAQhC,SAAS,mBACd,MAC0D;AAC1D,QAAM,QAAQ,KAAK,MAAM,6BAA6B;AACtD,QAAM,GAAG,aAAa,UAAU,IAAI,SAAS,CAAA;AAC7C,MAAI,gBAAgB,OAAW,QAAO;AACtC,SAAO,EAAE,aAAa,GAAI,eAAe,UAAa,EAAE,aAAW;AACrE;AAOO,SAAS,kBACd,OACA,MAC2C;AAC3C,MAAI,SAAS,OAAW;AACxB,QAAM,SAAS,mBAAmB,IAAI;AACtC,MAAI,CAAC,UAAU,CAAC,yBAAyB,SAAS,OAAO,WAAW,GAAG;AACrE,UAAM,IAAI;AAAA,MACR,SAAS,IAAI,gCAAgC,KAAK,sGAExB,yBAAyB,KAAK,IAAI,CAAC;AAAA,IAAA;AAAA,EAEjE;AACA,MACE,OAAO,eAAe,UACtB,CAAC,uBAAuB,SAAS,OAAO,UAAU,GAClD;AACA,UAAM,IAAI;AAAA,MACR,eAAe,OAAO,UAAU,gCAAgC,KAAK,6BACzC,uBAAuB,KAAK,IAAI,CAAC;AAAA,IAAA;AAAA,EAEjE;AACF;AAsBO,MAAM,uBAIT;AAAA,EACF,sBAAsB;AAAA,IACpB,MAAM;AAAA,IACN,KAAK;AAAA,IACL,KAAK;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,EAAA;AAAA,EAER,0BAA0B;AAAA,IACxB,MAAM;AAAA,IACN,KAAK;AAAA,IACL,KAAK;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,EAAA;AAEV;AAOO,SAAS,4BACd,OACuD;AACvD,SAAO,qBAAqB,KAAK;AACnC;AAmEA,MAAM,qDAA0D,IAAI;AAAA,EAClE;AACF,CAAC;AAQM,SAAS,wBAAwB,OAAwB;AAC9D,SAAO,+BAA+B,IAAI,KAAK;AACjD;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-grok",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.1",
|
|
4
4
|
"description": "xAI Grok adapter for TanStack AI chat, image generation, realtime, and structured outputs.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,17 +51,16 @@
|
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"openai": "^6.41.0",
|
|
53
53
|
"@tanstack/ai-utils": "0.3.0",
|
|
54
|
-
"@tanstack/openai-base": "0.9.
|
|
54
|
+
"@tanstack/openai-base": "0.9.1"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@vitest/coverage-v8": "4.0.14",
|
|
58
58
|
"vite": "^7.3.3",
|
|
59
|
-
"@tanstack/ai": "0.
|
|
60
|
-
"@tanstack/ai-client": "0.18.2"
|
|
59
|
+
"@tanstack/ai": "0.35.0"
|
|
61
60
|
},
|
|
62
61
|
"peerDependencies": {
|
|
63
62
|
"zod": "^4.0.0",
|
|
64
|
-
"@tanstack/ai": "^0.
|
|
63
|
+
"@tanstack/ai": "^0.35.0"
|
|
65
64
|
},
|
|
66
65
|
"scripts": {
|
|
67
66
|
"build": "vite build",
|