@tanstack/ai-grok 0.6.7 → 0.7.0

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.
Files changed (50) hide show
  1. package/dist/esm/adapters/image.js +36 -17
  2. package/dist/esm/adapters/image.js.map +1 -1
  3. package/dist/esm/adapters/summarize.js +51 -22
  4. package/dist/esm/adapters/summarize.js.map +1 -1
  5. package/dist/esm/adapters/text.js +25 -10
  6. package/dist/esm/adapters/text.js.map +1 -1
  7. package/dist/esm/adapters/transcription.d.ts +84 -0
  8. package/dist/esm/adapters/transcription.js +109 -0
  9. package/dist/esm/adapters/transcription.js.map +1 -0
  10. package/dist/esm/adapters/tts.d.ts +70 -0
  11. package/dist/esm/adapters/tts.js +137 -0
  12. package/dist/esm/adapters/tts.js.map +1 -0
  13. package/dist/esm/audio/transcription-provider-options.d.ts +41 -0
  14. package/dist/esm/audio/tts-provider-options.d.ts +42 -0
  15. package/dist/esm/index.d.ts +8 -2
  16. package/dist/esm/index.js +17 -2
  17. package/dist/esm/index.js.map +1 -1
  18. package/dist/esm/model-meta.d.ts +6 -0
  19. package/dist/esm/model-meta.js +22 -1
  20. package/dist/esm/model-meta.js.map +1 -1
  21. package/dist/esm/realtime/adapter.d.ts +21 -0
  22. package/dist/esm/realtime/adapter.js +816 -0
  23. package/dist/esm/realtime/adapter.js.map +1 -0
  24. package/dist/esm/realtime/index.d.ts +4 -0
  25. package/dist/esm/realtime/realtime-contract.d.ts +30 -0
  26. package/dist/esm/realtime/token.d.ts +22 -0
  27. package/dist/esm/realtime/token.js +73 -0
  28. package/dist/esm/realtime/token.js.map +1 -0
  29. package/dist/esm/realtime/types.d.ts +95 -0
  30. package/dist/esm/utils/audio.d.ts +23 -0
  31. package/dist/esm/utils/audio.js +171 -0
  32. package/dist/esm/utils/audio.js.map +1 -0
  33. package/dist/esm/utils/index.d.ts +1 -0
  34. package/package.json +6 -3
  35. package/src/adapters/image.ts +41 -19
  36. package/src/adapters/summarize.ts +56 -25
  37. package/src/adapters/text.ts +26 -9
  38. package/src/adapters/transcription.ts +233 -0
  39. package/src/adapters/tts.ts +260 -0
  40. package/src/audio/transcription-provider-options.ts +54 -0
  41. package/src/audio/tts-provider-options.ts +44 -0
  42. package/src/index.ts +50 -1
  43. package/src/model-meta.ts +54 -0
  44. package/src/realtime/adapter.ts +1215 -0
  45. package/src/realtime/index.ts +18 -0
  46. package/src/realtime/realtime-contract.ts +46 -0
  47. package/src/realtime/token.ts +131 -0
  48. package/src/realtime/types.ts +105 -0
  49. package/src/utils/audio.ts +217 -0
  50. package/src/utils/index.ts +1 -0
@@ -0,0 +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 RealtimeStatus,\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 let dataChannel: RTCDataChannel | null = null\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 dataChannel = pc.createDataChannel('oai-events')\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 dataChannel!.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' as RealtimeStatus })\n resolve()\n }\n })\n\n dataChannel.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 dataChannel.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 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 dataChannel.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:\n state === 'failed'\n ? ('error' as RealtimeStatus)\n : ('idle' as RealtimeStatus),\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', { messageId: currentMessageId ?? undefined })\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 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.\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 dataChannel!.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' as RealtimeStatus })\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', { messageId: currentMessageId ?? undefined })\n },\n\n on<TEvent extends RealtimeEvent>(\n event: TEvent,\n handler: RealtimeEventHandler<TEvent>,\n ): () => void {\n if (!eventHandlers.has(event)) {\n eventHandlers.set(event, new Set())\n }\n eventHandlers.get(event)!.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,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;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;"}
@@ -0,0 +1,4 @@
1
+ export { grokRealtimeToken } from './token.js';
2
+ export { grokRealtime } from './adapter.js';
3
+ export type { GrokRealtimeVoice, GrokRealtimeTokenOptions, GrokRealtimeOptions, GrokTurnDetection, GrokSemanticVADConfig, GrokServerVADConfig, } from './types.js';
4
+ export type { GrokRealtimeModel } from '../model-meta.js';
@@ -0,0 +1,30 @@
1
+ import { AnyClientTool, AudioVisualization, RealtimeEvent, RealtimeEventHandler, RealtimeSessionConfig, RealtimeToken } from '@tanstack/ai';
2
+ /**
3
+ * Structural contract for the `RealtimeAdapter` / `RealtimeConnection` types
4
+ * from `@tanstack/ai-client`.
5
+ *
6
+ * We duplicate the shapes here (and verify structural compatibility in a
7
+ * dev-only type check — see `tests/realtime-contract.drift.test-d.ts`) so that
8
+ * `@tanstack/ai-grok` does not impose `@tanstack/ai-client` as a `peerDependency`.
9
+ * Consumers only need `@tanstack/ai-client` at the point where they actually
10
+ * construct a `RealtimeClient`, not when they import this adapter.
11
+ *
12
+ * If `@tanstack/ai-client` ever changes these interfaces, the drift check
13
+ * will fail and we must update this file in lockstep.
14
+ */
15
+ export interface RealtimeAdapter {
16
+ provider: string;
17
+ connect: (token: RealtimeToken, clientTools?: ReadonlyArray<AnyClientTool>) => Promise<RealtimeConnection>;
18
+ }
19
+ export interface RealtimeConnection {
20
+ disconnect: () => Promise<void>;
21
+ startAudioCapture: () => Promise<void>;
22
+ stopAudioCapture: () => void;
23
+ sendText: (text: string) => void;
24
+ sendImage: (imageData: string, mimeType: string) => void;
25
+ sendToolResult: (callId: string, result: string) => void;
26
+ updateSession: (config: Partial<RealtimeSessionConfig>) => void;
27
+ interrupt: () => void;
28
+ on: <TEvent extends RealtimeEvent>(event: TEvent, handler: RealtimeEventHandler<TEvent>) => () => void;
29
+ getAudioVisualization: () => AudioVisualization;
30
+ }
@@ -0,0 +1,22 @@
1
+ import { RealtimeTokenAdapter } from '@tanstack/ai';
2
+ import { GrokRealtimeTokenOptions } from './types.js';
3
+ /**
4
+ * Creates a Grok realtime token adapter.
5
+ *
6
+ * Generates ephemeral client secrets for browser-side WebRTC connections to
7
+ * the xAI Voice Agent API.
8
+ *
9
+ * @param options - Configuration options for the realtime session.
10
+ * @returns A RealtimeTokenAdapter for use with `realtimeToken()`.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { realtimeToken } from '@tanstack/ai'
15
+ * import { grokRealtimeToken } from '@tanstack/ai-grok'
16
+ *
17
+ * const token = await realtimeToken({
18
+ * adapter: grokRealtimeToken({ model: 'grok-voice-fast-1.0' }),
19
+ * })
20
+ * ```
21
+ */
22
+ export declare function grokRealtimeToken(options?: GrokRealtimeTokenOptions): RealtimeTokenAdapter;
@@ -0,0 +1,73 @@
1
+ import { resolveDebugOption } from "@tanstack/ai/adapter-internals";
2
+ import { getGrokApiKeyFromEnv } from "../utils/client.js";
3
+ const GROK_REALTIME_CLIENT_SECRETS_URL = "https://api.x.ai/v1/realtime/client_secrets";
4
+ const DEFAULT_TOKEN_FETCH_TIMEOUT_MS = 15e3;
5
+ function grokRealtimeToken(options = {}) {
6
+ const apiKey = getGrokApiKeyFromEnv();
7
+ const logger = resolveDebugOption(options.debug);
8
+ return {
9
+ provider: "grok",
10
+ async generateToken() {
11
+ const model = options.model ?? "grok-voice-fast-1.0";
12
+ logger.request(`activity=realtimeToken provider=grok model=${model}`, {
13
+ provider: "grok",
14
+ model
15
+ });
16
+ const requestBody = {
17
+ session: { model }
18
+ };
19
+ const controller = new AbortController();
20
+ const timeout = setTimeout(
21
+ () => controller.abort(new Error("Grok realtime token request timed out")),
22
+ DEFAULT_TOKEN_FETCH_TIMEOUT_MS
23
+ );
24
+ try {
25
+ const response = await fetch(GROK_REALTIME_CLIENT_SECRETS_URL, {
26
+ method: "POST",
27
+ headers: {
28
+ Authorization: `Bearer ${apiKey}`,
29
+ "Content-Type": "application/json"
30
+ },
31
+ body: JSON.stringify(requestBody),
32
+ signal: controller.signal
33
+ });
34
+ if (!response.ok) {
35
+ const errorText = await response.text();
36
+ throw new Error(
37
+ `Grok realtime session creation failed: ${response.status} ${errorText}`
38
+ );
39
+ }
40
+ const sessionData = await response.json();
41
+ const clientSecret = sessionData?.client_secret;
42
+ if (!clientSecret || typeof clientSecret.value !== "string" || typeof clientSecret.expires_at !== "number" || !Number.isFinite(clientSecret.expires_at)) {
43
+ throw new Error(
44
+ "Grok realtime session response missing or malformed `client_secret`"
45
+ );
46
+ }
47
+ const sessionModel = sessionData.model ?? model;
48
+ const raw = clientSecret.expires_at;
49
+ const expiresAt = raw > 1e12 ? raw : raw * 1e3;
50
+ return {
51
+ provider: "grok",
52
+ token: clientSecret.value,
53
+ expiresAt,
54
+ config: {
55
+ model: sessionModel
56
+ }
57
+ };
58
+ } catch (error) {
59
+ logger.errors("grok.realtimeToken fatal", {
60
+ error,
61
+ source: "grok.realtimeToken"
62
+ });
63
+ throw error;
64
+ } finally {
65
+ clearTimeout(timeout);
66
+ }
67
+ }
68
+ };
69
+ }
70
+ export {
71
+ grokRealtimeToken
72
+ };
73
+ //# sourceMappingURL=token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.js","sources":["../../../src/realtime/token.ts"],"sourcesContent":["import { resolveDebugOption } from '@tanstack/ai/adapter-internals'\nimport { getGrokApiKeyFromEnv } from '../utils'\nimport type { RealtimeToken, RealtimeTokenAdapter } from '@tanstack/ai'\nimport type { GrokRealtimeModel } from '../model-meta'\nimport type {\n GrokRealtimeSessionResponse,\n GrokRealtimeTokenOptions,\n} from './types'\n\nconst GROK_REALTIME_CLIENT_SECRETS_URL =\n 'https://api.x.ai/v1/realtime/client_secrets'\n\nconst DEFAULT_TOKEN_FETCH_TIMEOUT_MS = 15_000\n\n/**\n * Creates a Grok realtime token adapter.\n *\n * Generates ephemeral client secrets for browser-side WebRTC connections to\n * the xAI Voice Agent API.\n *\n * @param options - Configuration options for the realtime session.\n * @returns A RealtimeTokenAdapter for use with `realtimeToken()`.\n *\n * @example\n * ```typescript\n * import { realtimeToken } from '@tanstack/ai'\n * import { grokRealtimeToken } from '@tanstack/ai-grok'\n *\n * const token = await realtimeToken({\n * adapter: grokRealtimeToken({ model: 'grok-voice-fast-1.0' }),\n * })\n * ```\n */\nexport function grokRealtimeToken(\n options: GrokRealtimeTokenOptions = {},\n): RealtimeTokenAdapter {\n const apiKey = getGrokApiKeyFromEnv()\n const logger = resolveDebugOption(options.debug)\n\n return {\n provider: 'grok',\n\n async generateToken(): Promise<RealtimeToken> {\n const model: GrokRealtimeModel = options.model ?? 'grok-voice-fast-1.0'\n\n logger.request(`activity=realtimeToken provider=grok model=${model}`, {\n provider: 'grok',\n model,\n })\n\n // xAI docs (docs.x.ai/developers/rest-api-reference/inference/voice)\n // specify the body as `{ session: { model } }`. `expires_after` is\n // available to shorten the default 600s TTL but we don't expose it\n // yet — the caller can still call `generateToken()` more often if\n // they want a shorter-lived session.\n const requestBody: Record<string, unknown> = {\n session: { model },\n }\n\n // Abort the fetch if xAI never responds. Without this the whole\n // realtime connect flow hangs forever on a dead endpoint.\n const controller = new AbortController()\n const timeout = setTimeout(\n () =>\n controller.abort(new Error('Grok realtime token request timed out')),\n DEFAULT_TOKEN_FETCH_TIMEOUT_MS,\n )\n\n try {\n const response = await fetch(GROK_REALTIME_CLIENT_SECRETS_URL, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(requestBody),\n signal: controller.signal,\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new Error(\n `Grok realtime session creation failed: ${response.status} ${errorText}`,\n )\n }\n\n const sessionData = (await response.json()) as\n | Partial<GrokRealtimeSessionResponse>\n | undefined\n\n // Validate shape before dereferencing — xAI could return an error\n // envelope with 200 status, or a partial response under protocol drift.\n const clientSecret = sessionData?.client_secret\n if (\n !clientSecret ||\n typeof clientSecret.value !== 'string' ||\n typeof clientSecret.expires_at !== 'number' ||\n !Number.isFinite(clientSecret.expires_at)\n ) {\n throw new Error(\n 'Grok realtime session response missing or malformed `client_secret`',\n )\n }\n const sessionModel = sessionData.model ?? model\n\n // xAI docs describe `expires_at` as a unix timestamp in seconds, but\n // in practice different deployments have returned milliseconds. Treat\n // any value that already looks like ms (>1e12 ≈ Sep 2001 in ms) as ms.\n const raw = clientSecret.expires_at\n const expiresAt = raw > 1e12 ? raw : raw * 1000\n\n return {\n provider: 'grok',\n token: clientSecret.value,\n expiresAt,\n config: {\n model: sessionModel,\n },\n }\n } catch (error) {\n logger.errors('grok.realtimeToken fatal', {\n error,\n source: 'grok.realtimeToken',\n })\n throw error\n } finally {\n clearTimeout(timeout)\n }\n },\n }\n}\n"],"names":[],"mappings":";;AASA,MAAM,mCACJ;AAEF,MAAM,iCAAiC;AAqBhC,SAAS,kBACd,UAAoC,IACd;AACtB,QAAM,SAAS,qBAAA;AACf,QAAM,SAAS,mBAAmB,QAAQ,KAAK;AAE/C,SAAO;AAAA,IACL,UAAU;AAAA,IAEV,MAAM,gBAAwC;AAC5C,YAAM,QAA2B,QAAQ,SAAS;AAElD,aAAO,QAAQ,8CAA8C,KAAK,IAAI;AAAA,QACpE,UAAU;AAAA,QACV;AAAA,MAAA,CACD;AAOD,YAAM,cAAuC;AAAA,QAC3C,SAAS,EAAE,MAAA;AAAA,MAAM;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3
+ /**
4
+ * Grok realtime voice options (Voice Agent API).
5
+ * https://docs.x.ai/developers/model-capabilities/audio/voice-agent
6
+ */
7
+ export type GrokRealtimeVoice = 'eve' | 'ara' | 'rex' | 'sal' | 'leo';
8
+ /**
9
+ * Grok semantic VAD configuration.
10
+ */
11
+ export interface GrokSemanticVADConfig {
12
+ type: 'semantic_vad';
13
+ /** Eagerness level for turn detection */
14
+ eagerness?: 'low' | 'medium' | 'high';
15
+ }
16
+ /**
17
+ * Grok server VAD configuration.
18
+ */
19
+ export interface GrokServerVADConfig extends VADConfig {
20
+ type: 'server_vad';
21
+ }
22
+ /**
23
+ * Grok turn detection configuration.
24
+ */
25
+ export type GrokTurnDetection = GrokSemanticVADConfig | GrokServerVADConfig | null;
26
+ /**
27
+ * Options for the Grok realtime token adapter.
28
+ */
29
+ export interface GrokRealtimeTokenOptions {
30
+ /** Model to use (default: 'grok-voice-fast-1.0'). */
31
+ model?: GrokRealtimeModel;
32
+ /**
33
+ * Enable debug logging for token creation.
34
+ *
35
+ * - `true`: log all categories via the default `ConsoleLogger`
36
+ * - `false`: silence everything including errors
37
+ * - `DebugConfig`: per-category toggles plus an optional custom `logger`
38
+ * - omitted: only the `errors` category is active (default behaviour)
39
+ */
40
+ debug?: DebugOption;
41
+ }
42
+ /**
43
+ * Options for the Grok realtime client adapter.
44
+ */
45
+ export interface GrokRealtimeOptions {
46
+ /** Connection mode (default: 'webrtc' in browser). */
47
+ connectionMode?: 'webrtc' | 'websocket';
48
+ /**
49
+ * Enable debug logging for this adapter.
50
+ *
51
+ * - `true`: log all categories via the default `ConsoleLogger`
52
+ * - `false`: silence everything including errors
53
+ * - `DebugConfig`: per-category toggles plus an optional custom `logger`
54
+ * - omitted: only the `errors` category is active (default behaviour)
55
+ */
56
+ debug?: DebugOption;
57
+ }
58
+ /**
59
+ * Grok realtime session response from the `/v1/realtime/client_secrets`
60
+ * endpoint. Shape matches OpenAI's `/v1/realtime/sessions` response since
61
+ * xAI advertises its voice agent API as OpenAI-realtime-compatible.
62
+ */
63
+ export interface GrokRealtimeSessionResponse {
64
+ id: string;
65
+ object: string;
66
+ model: string;
67
+ modalities: Array<string>;
68
+ instructions: string;
69
+ voice: string;
70
+ input_audio_format: string;
71
+ output_audio_format: string;
72
+ input_audio_transcription: {
73
+ model: string;
74
+ } | null;
75
+ turn_detection: {
76
+ type: string;
77
+ threshold?: number;
78
+ prefix_padding_ms?: number;
79
+ silence_duration_ms?: number;
80
+ eagerness?: string;
81
+ } | null;
82
+ tools: Array<{
83
+ type: string;
84
+ name: string;
85
+ description: string;
86
+ parameters: Record<string, unknown>;
87
+ }>;
88
+ tool_choice: string;
89
+ temperature: number;
90
+ max_response_output_tokens: number | string;
91
+ client_secret: {
92
+ value: string;
93
+ expires_at: number;
94
+ };
95
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Coerce the various audio input shapes accepted by `TranscriptionOptions.audio`
3
+ * into a `File` suitable for `multipart/form-data` uploads.
4
+ *
5
+ * For base64 string inputs we require an explicit MIME type — either via a
6
+ * `data:<mime>;base64,<payload>` URI prefix, or via the caller-provided
7
+ * `audioFormat` parameter. Bare base64 without either is rejected, because
8
+ * silently defaulting to `audio/mpeg` misreports non-mp3 audio to the server.
9
+ *
10
+ * The same rule applies to raw `ArrayBuffer` inputs: the caller must supply
11
+ * an `audioFormat` so we know what MIME type and extension to use.
12
+ */
13
+ export declare function toAudioFile(audio: string | File | Blob | ArrayBuffer, audioFormat?: string): File;
14
+ /**
15
+ * Cross-runtime ArrayBuffer → base64 conversion.
16
+ *
17
+ * Uses Node's `Buffer` when available (fastest path on server) and falls
18
+ * back to `btoa` + chunked `String.fromCharCode` everywhere else (browser,
19
+ * Cloudflare Workers, Bun, Deno). Chunking is required because a very
20
+ * large audio buffer spread into `String.fromCharCode(...bytes)` in one
21
+ * call can hit `Maximum call stack size exceeded`.
22
+ */
23
+ export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
@@ -0,0 +1,171 @@
1
+ function toAudioFile(audio, audioFormat) {
2
+ if (typeof File !== "undefined" && audio instanceof File) {
3
+ if (audioFormat) {
4
+ const mimeType = toMimeType(audioFormat);
5
+ return new File([audio], `audio.${extensionFor(mimeType)}`, {
6
+ type: mimeType
7
+ });
8
+ }
9
+ if (audio.type) {
10
+ return audio;
11
+ }
12
+ throw new Error(
13
+ 'toAudioFile cannot infer type for File input with empty .type — pass an explicit audioFormat (e.g. "mp3", "wav", "audio/mpeg")'
14
+ );
15
+ }
16
+ if (typeof Blob !== "undefined" && audio instanceof Blob) {
17
+ const mimeType = audioFormat ? toMimeType(audioFormat) : audio.type || void 0;
18
+ if (!mimeType) {
19
+ throw new Error(
20
+ 'toAudioFile cannot infer type for Blob input with empty .type — pass an explicit audioFormat (e.g. "mp3", "wav", "audio/mpeg")'
21
+ );
22
+ }
23
+ return new File([audio], `audio.${extensionFor(mimeType)}`, {
24
+ type: mimeType
25
+ });
26
+ }
27
+ if (audio instanceof ArrayBuffer) {
28
+ if (!audioFormat) {
29
+ throw new Error(
30
+ 'toAudioFile cannot infer type for ArrayBuffer input — pass an explicit audioFormat (e.g. "mp3", "wav", "audio/mpeg")'
31
+ );
32
+ }
33
+ const mimeType = toMimeType(audioFormat);
34
+ return new File([audio], `audio.${extensionFor(mimeType)}`, {
35
+ type: mimeType
36
+ });
37
+ }
38
+ if (typeof audio === "string") {
39
+ if (audio.startsWith("data:")) {
40
+ const [header, base64Data] = audio.split(",");
41
+ const headerMatch = header?.match(/data:([^;]+)/);
42
+ const uriMimeType = headerMatch?.[1];
43
+ if (!uriMimeType) {
44
+ throw new Error(
45
+ "Malformed data: URI in toAudioFile: cannot parse MIME type — expected data:<mime>[;charset=…][;base64],<payload>"
46
+ );
47
+ }
48
+ if (base64Data === void 0 || base64Data.trim() === "") {
49
+ throw new Error(
50
+ "Malformed data: URI in toAudioFile: missing base64 payload after comma"
51
+ );
52
+ }
53
+ const mimeType2 = audioFormat ? toMimeType(audioFormat) : uriMimeType;
54
+ const buffer2 = base64ToArrayBuffer(base64Data);
55
+ return new File([buffer2], `audio.${extensionFor(mimeType2)}`, {
56
+ type: mimeType2
57
+ });
58
+ }
59
+ if (!audioFormat) {
60
+ throw new Error(
61
+ "toAudioFile requires a data: URI (e.g. data:audio/wav;base64,...) or an explicit audioFormat argument — bare base64 strings have no MIME type to infer"
62
+ );
63
+ }
64
+ const buffer = base64ToArrayBuffer(audio);
65
+ const mimeType = toMimeType(audioFormat);
66
+ return new File([buffer], `audio.${extensionFor(mimeType)}`, {
67
+ type: mimeType
68
+ });
69
+ }
70
+ throw new Error("Invalid audio input type");
71
+ }
72
+ function toMimeType(audioFormat) {
73
+ if (audioFormat.includes("/")) return audioFormat;
74
+ const ext = audioFormat.toLowerCase();
75
+ switch (ext) {
76
+ case "mp3":
77
+ return "audio/mpeg";
78
+ case "wav":
79
+ return "audio/wav";
80
+ case "ogg":
81
+ return "audio/ogg";
82
+ case "opus":
83
+ return "audio/opus";
84
+ case "flac":
85
+ return "audio/flac";
86
+ case "aac":
87
+ return "audio/aac";
88
+ case "mp4":
89
+ return "audio/mp4";
90
+ case "m4a":
91
+ return "audio/mp4";
92
+ case "webm":
93
+ return "audio/webm";
94
+ case "pcm":
95
+ return "audio/L16";
96
+ case "mulaw":
97
+ return "audio/basic";
98
+ case "alaw":
99
+ return "audio/x-alaw-basic";
100
+ default:
101
+ return `audio/${ext}`;
102
+ }
103
+ }
104
+ function extensionFor(mimeType) {
105
+ switch (mimeType) {
106
+ case "audio/mpeg":
107
+ return "mp3";
108
+ case "audio/wav":
109
+ case "audio/x-wav":
110
+ return "wav";
111
+ case "audio/ogg":
112
+ return "ogg";
113
+ case "audio/opus":
114
+ return "opus";
115
+ case "audio/flac":
116
+ return "flac";
117
+ case "audio/aac":
118
+ return "aac";
119
+ case "audio/mp4":
120
+ return "m4a";
121
+ case "audio/webm":
122
+ return "webm";
123
+ case "audio/L16":
124
+ return "pcm";
125
+ case "audio/basic":
126
+ return "mulaw";
127
+ case "audio/x-alaw-basic":
128
+ return "alaw";
129
+ default: {
130
+ const slash = mimeType.indexOf("/");
131
+ if (slash === -1) return "bin";
132
+ return mimeType.slice(slash + 1) || "bin";
133
+ }
134
+ }
135
+ }
136
+ function arrayBufferToBase64(buffer) {
137
+ if (typeof Buffer !== "undefined") {
138
+ return Buffer.from(buffer).toString("base64");
139
+ }
140
+ const bytes = new Uint8Array(buffer);
141
+ const chunkSize = 32768;
142
+ let binary = "";
143
+ for (let i = 0; i < bytes.length; i += chunkSize) {
144
+ const end = Math.min(i + chunkSize, bytes.length);
145
+ binary += String.fromCharCode.apply(
146
+ null,
147
+ bytes.subarray(i, end)
148
+ );
149
+ }
150
+ return btoa(binary);
151
+ }
152
+ function base64ToArrayBuffer(base64) {
153
+ let binary;
154
+ try {
155
+ binary = atob(base64);
156
+ } catch (err) {
157
+ const msg = err instanceof Error ? err.message : String(err);
158
+ throw new Error(`Invalid base64 input to toAudioFile: ${msg}`);
159
+ }
160
+ const buffer = new ArrayBuffer(binary.length);
161
+ const bytes = new Uint8Array(buffer);
162
+ for (let i = 0; i < binary.length; i++) {
163
+ bytes[i] = binary.charCodeAt(i);
164
+ }
165
+ return buffer;
166
+ }
167
+ export {
168
+ arrayBufferToBase64,
169
+ toAudioFile
170
+ };
171
+ //# sourceMappingURL=audio.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"audio.js","sources":["../../../src/utils/audio.ts"],"sourcesContent":["/**\n * Coerce the various audio input shapes accepted by `TranscriptionOptions.audio`\n * into a `File` suitable for `multipart/form-data` uploads.\n *\n * For base64 string inputs we require an explicit MIME type — either via a\n * `data:<mime>;base64,<payload>` URI prefix, or via the caller-provided\n * `audioFormat` parameter. Bare base64 without either is rejected, because\n * silently defaulting to `audio/mpeg` misreports non-mp3 audio to the server.\n *\n * The same rule applies to raw `ArrayBuffer` inputs: the caller must supply\n * an `audioFormat` so we know what MIME type and extension to use.\n */\nexport function toAudioFile(\n audio: string | File | Blob | ArrayBuffer,\n audioFormat?: string,\n): File {\n if (typeof File !== 'undefined' && audio instanceof File) {\n // Prefer the caller-supplied `audioFormat` over a potentially empty or\n // incorrect `File.type` — callers pass `audioFormat` precisely because\n // they have more context than the browser does about the payload. If\n // neither is set, fall through to the Blob-style error path below.\n if (audioFormat) {\n const mimeType = toMimeType(audioFormat)\n return new File([audio], `audio.${extensionFor(mimeType)}`, {\n type: mimeType,\n })\n }\n if (audio.type) {\n return audio\n }\n throw new Error(\n 'toAudioFile cannot infer type for File input with empty .type — pass an explicit audioFormat (e.g. \"mp3\", \"wav\", \"audio/mpeg\")',\n )\n }\n\n if (typeof Blob !== 'undefined' && audio instanceof Blob) {\n // Mirror the ArrayBuffer / bare-base64 paths: prefer the explicit\n // audioFormat argument over the Blob's (often empty) .type. We refuse to\n // fall back to `application/octet-stream` because that mislabels audio\n // for the server.\n const mimeType = audioFormat\n ? toMimeType(audioFormat)\n : audio.type || undefined\n if (!mimeType) {\n throw new Error(\n 'toAudioFile cannot infer type for Blob input with empty .type — pass an explicit audioFormat (e.g. \"mp3\", \"wav\", \"audio/mpeg\")',\n )\n }\n return new File([audio], `audio.${extensionFor(mimeType)}`, {\n type: mimeType,\n })\n }\n\n if (audio instanceof ArrayBuffer) {\n if (!audioFormat) {\n throw new Error(\n 'toAudioFile cannot infer type for ArrayBuffer input — pass an explicit audioFormat (e.g. \"mp3\", \"wav\", \"audio/mpeg\")',\n )\n }\n const mimeType = toMimeType(audioFormat)\n return new File([audio], `audio.${extensionFor(mimeType)}`, {\n type: mimeType,\n })\n }\n\n if (typeof audio === 'string') {\n if (audio.startsWith('data:')) {\n const [header, base64Data] = audio.split(',')\n // Fail loudly on malformed data: URIs instead of silently defaulting\n // to `audio/mpeg` — the file's contract is that we never mislabel\n // audio for the server.\n const headerMatch = header?.match(/data:([^;]+)/)\n const uriMimeType = headerMatch?.[1]\n if (!uriMimeType) {\n throw new Error(\n 'Malformed data: URI in toAudioFile: cannot parse MIME type — expected data:<mime>[;charset=…][;base64],<payload>',\n )\n }\n if (base64Data === undefined || base64Data.trim() === '') {\n throw new Error(\n 'Malformed data: URI in toAudioFile: missing base64 payload after comma',\n )\n }\n // Caller-supplied `audioFormat` wins over the URI-embedded MIME: the\n // caller has more context (the URI MIME may be wrong, or a generic\n // `application/octet-stream`).\n const mimeType = audioFormat ? toMimeType(audioFormat) : uriMimeType\n const buffer = base64ToArrayBuffer(base64Data)\n return new File([buffer], `audio.${extensionFor(mimeType)}`, {\n type: mimeType,\n })\n }\n\n if (!audioFormat) {\n throw new Error(\n 'toAudioFile requires a data: URI (e.g. data:audio/wav;base64,...) or an explicit audioFormat argument — bare base64 strings have no MIME type to infer',\n )\n }\n\n const buffer = base64ToArrayBuffer(audio)\n const mimeType = toMimeType(audioFormat)\n return new File([buffer], `audio.${extensionFor(mimeType)}`, {\n type: mimeType,\n })\n }\n\n throw new Error('Invalid audio input type')\n}\n\nfunction toMimeType(audioFormat: string): string {\n // Accept either \"audio/…\" strings or bare extensions like \"mp3\".\n if (audioFormat.includes('/')) return audioFormat\n const ext = audioFormat.toLowerCase()\n switch (ext) {\n case 'mp3':\n return 'audio/mpeg'\n case 'wav':\n return 'audio/wav'\n case 'ogg':\n return 'audio/ogg'\n case 'opus':\n return 'audio/opus'\n case 'flac':\n return 'audio/flac'\n case 'aac':\n return 'audio/aac'\n case 'mp4':\n return 'audio/mp4'\n case 'm4a':\n return 'audio/mp4'\n case 'webm':\n return 'audio/webm'\n case 'pcm':\n return 'audio/L16'\n case 'mulaw':\n return 'audio/basic'\n case 'alaw':\n return 'audio/x-alaw-basic'\n default:\n return `audio/${ext}`\n }\n}\n\nfunction extensionFor(mimeType: string): string {\n switch (mimeType) {\n case 'audio/mpeg':\n return 'mp3'\n case 'audio/wav':\n case 'audio/x-wav':\n return 'wav'\n case 'audio/ogg':\n return 'ogg'\n case 'audio/opus':\n return 'opus'\n case 'audio/flac':\n return 'flac'\n case 'audio/aac':\n return 'aac'\n case 'audio/mp4':\n return 'm4a'\n case 'audio/webm':\n return 'webm'\n case 'audio/L16':\n return 'pcm'\n case 'audio/basic':\n return 'mulaw'\n case 'audio/x-alaw-basic':\n return 'alaw'\n default: {\n const slash = mimeType.indexOf('/')\n if (slash === -1) return 'bin'\n return mimeType.slice(slash + 1) || 'bin'\n }\n }\n}\n\n/**\n * Cross-runtime ArrayBuffer → base64 conversion.\n *\n * Uses Node's `Buffer` when available (fastest path on server) and falls\n * back to `btoa` + chunked `String.fromCharCode` everywhere else (browser,\n * Cloudflare Workers, Bun, Deno). Chunking is required because a very\n * large audio buffer spread into `String.fromCharCode(...bytes)` in one\n * call can hit `Maximum call stack size exceeded`.\n */\nexport function arrayBufferToBase64(buffer: ArrayBuffer): string {\n if (typeof Buffer !== 'undefined') {\n return Buffer.from(buffer).toString('base64')\n }\n const bytes = new Uint8Array(buffer)\n const chunkSize = 0x8000\n let binary = ''\n for (let i = 0; i < bytes.length; i += chunkSize) {\n const end = Math.min(i + chunkSize, bytes.length)\n binary += String.fromCharCode.apply(\n null,\n bytes.subarray(i, end) as unknown as Array<number>,\n )\n }\n return btoa(binary)\n}\n\nfunction base64ToArrayBuffer(base64: string): ArrayBuffer {\n let binary: string\n try {\n binary = atob(base64)\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err)\n throw new Error(`Invalid base64 input to toAudioFile: ${msg}`)\n }\n const buffer = new ArrayBuffer(binary.length)\n const bytes = new Uint8Array(buffer)\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i)\n }\n return buffer\n}\n"],"names":["mimeType","buffer"],"mappings":"AAYO,SAAS,YACd,OACA,aACM;AACN,MAAI,OAAO,SAAS,eAAe,iBAAiB,MAAM;AAKxD,QAAI,aAAa;AACf,YAAM,WAAW,WAAW,WAAW;AACvC,aAAO,IAAI,KAAK,CAAC,KAAK,GAAG,SAAS,aAAa,QAAQ,CAAC,IAAI;AAAA,QAC1D,MAAM;AAAA,MAAA,CACP;AAAA,IACH;AACA,QAAI,MAAM,MAAM;AACd,aAAO;AAAA,IACT;AAC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C,KAAK;AAAA,IACtC;AAAA,EAAA;AAEJ;AAWO,SAAS,oBAAoB,QAA6B;AAC/D,MAAI,OAAO,WAAW,aAAa;AACjC,WAAO,OAAO,KAAK,MAAM,EAAE,SAAS,QAAQ;AAAA,EAC9C;AACA,QAAM,QAAQ,IAAI,WAAW,MAAM;AACnC,QAAM,YAAY;AAClB,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,WAAW;AAChD,UAAM,MAAM,KAAK,IAAI,IAAI,WAAW,MAAM,MAAM;AAChD,cAAU,OAAO,aAAa;AAAA,MAC5B;AAAA,MACA,MAAM,SAAS,GAAG,GAAG;AAAA,IAAA;AAAA,EAEzB;AACA,SAAO,KAAK,MAAM;AACpB;AAEA,SAAS,oBAAoB,QAA6B;AACxD,MAAI;AACJ,MAAI;AACF,aAAS,KAAK,MAAM;AAAA,EACtB,SAAS,KAAK;AACZ,UAAM,MAAM,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAC3D,UAAM,IAAI,MAAM,wCAAwC,GAAG,EAAE;AAAA,EAC/D;AACA,QAAM,SAAS,IAAI,YAAY,OAAO,MAAM;AAC5C,QAAM,QAAQ,IAAI,WAAW,MAAM;AACnC,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,UAAM,CAAC,IAAI,OAAO,WAAW,CAAC;AAAA,EAChC;AACA,SAAO;AACT;"}
@@ -1,2 +1,3 @@
1
1
  export { createGrokClient, getGrokApiKeyFromEnv, generateId, type GrokClientConfig, } from './client.js';
2
2
  export { makeGrokStructuredOutputCompatible, transformNullsToUndefined, } from './schema-converter.js';
3
+ export { toAudioFile, arrayBufferToBase64 } from './audio.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai-grok",
3
- "version": "0.6.7",
3
+ "version": "0.7.0",
4
4
  "description": "Grok (xAI) adapter for TanStack AI",
5
5
  "author": "",
6
6
  "license": "MIT",
@@ -38,11 +38,14 @@
38
38
  },
39
39
  "devDependencies": {
40
40
  "@vitest/coverage-v8": "4.0.14",
41
- "vite": "^7.2.7"
41
+ "vite": "^7.2.7",
42
+ "zod": "^4.2.0",
43
+ "@tanstack/ai": "0.14.0",
44
+ "@tanstack/ai-client": "0.8.0"
42
45
  },
43
46
  "peerDependencies": {
44
47
  "zod": "^4.0.0",
45
- "@tanstack/ai": "^0.12.0"
48
+ "@tanstack/ai": "^0.14.0"
46
49
  },
47
50
  "scripts": {
48
51
  "build": "vite build",