|
| 1 | +import type { ImageGenerationProvider } from "openclaw/plugin-sdk/image-generation"; |
| 2 | +import type { MediaUnderstandingProvider } from "openclaw/plugin-sdk/media-understanding"; |
| 3 | +import { |
| 4 | + isProviderApiKeyConfigured, |
| 5 | + isProviderAuthProfileConfigured, |
| 6 | +} from "openclaw/plugin-sdk/provider-auth"; |
| 7 | +import type { |
| 8 | + RealtimeTranscriptionProviderConfig, |
| 9 | + RealtimeTranscriptionProviderPlugin, |
| 10 | +} from "openclaw/plugin-sdk/realtime-transcription"; |
| 11 | +import type { |
| 12 | + RealtimeVoiceAudioFormat, |
| 13 | + RealtimeVoiceBridgeCreateRequest, |
| 14 | + RealtimeVoiceProviderPlugin, |
| 15 | +} from "openclaw/plugin-sdk/realtime-voice"; |
| 16 | +import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-input"; |
| 17 | +import { |
| 18 | + isRecord, |
| 19 | + normalizeOptionalString, |
| 20 | + parseBooleanValue as readBoolean, |
| 21 | + parseFiniteNumber as readFiniteNumber, |
| 22 | +} from "openclaw/plugin-sdk/string-coerce-runtime"; |
| 23 | +import type { |
| 24 | + VideoGenerationProvider, |
| 25 | + VideoGenerationProviderCapabilities, |
| 26 | +} from "openclaw/plugin-sdk/video-generation"; |
| 27 | +import { XAI_DEFAULT_IMAGE_MODEL, XAI_IMAGE_MODELS } from "./model-definitions.js"; |
| 28 | +import { |
| 29 | + XAI_REALTIME_DEFAULT_MODEL, |
| 30 | + XAI_REALTIME_VOICES, |
| 31 | + hasXaiRealtimeApiKeyInput, |
| 32 | + normalizeXaiRealtimeProviderConfig, |
| 33 | +} from "./realtime-voice-config.js"; |
| 34 | + |
| 35 | +export const XAI_IMAGE_DEFAULT_TIMEOUT_MS = 600_000; |
| 36 | +export const XAI_SUPPORTED_IMAGE_ASPECT_RATIOS = [ |
| 37 | + "1:1", |
| 38 | + "16:9", |
| 39 | + "9:16", |
| 40 | + "4:3", |
| 41 | + "3:4", |
| 42 | + "3:2", |
| 43 | + "2:3", |
| 44 | + "2:1", |
| 45 | + "1:2", |
| 46 | + "19.5:9", |
| 47 | + "9:19.5", |
| 48 | + "20:9", |
| 49 | + "9:20", |
| 50 | +] as const; |
| 51 | + |
| 52 | +export function createXaiImageGenerationProviderMetadata() { |
| 53 | + return { |
| 54 | + id: "xai", |
| 55 | + label: "xAI", |
| 56 | + defaultModel: XAI_DEFAULT_IMAGE_MODEL, |
| 57 | + defaultTimeoutMs: XAI_IMAGE_DEFAULT_TIMEOUT_MS, |
| 58 | + models: [...XAI_IMAGE_MODELS], |
| 59 | + capabilities: { |
| 60 | + generate: { |
| 61 | + maxCount: 4, |
| 62 | + supportsAspectRatio: true, |
| 63 | + supportsResolution: true, |
| 64 | + supportsSize: false, |
| 65 | + }, |
| 66 | + edit: { |
| 67 | + enabled: true, |
| 68 | + maxCount: 4, |
| 69 | + maxInputImages: 3, |
| 70 | + supportsAspectRatio: true, |
| 71 | + supportsResolution: true, |
| 72 | + supportsSize: false, |
| 73 | + }, |
| 74 | + geometry: { |
| 75 | + aspectRatios: [...XAI_SUPPORTED_IMAGE_ASPECT_RATIOS], |
| 76 | + resolutions: ["1K", "2K"], |
| 77 | + }, |
| 78 | + }, |
| 79 | + } satisfies Omit<ImageGenerationProvider, "generateImage">; |
| 80 | +} |
| 81 | + |
| 82 | +export function createXaiMediaUnderstandingProviderMetadata() { |
| 83 | + return { |
| 84 | + id: "xai", |
| 85 | + capabilities: ["audio"], |
| 86 | + autoPriority: { audio: 25 }, |
| 87 | + } satisfies Omit<MediaUnderstandingProvider, "transcribeAudio">; |
| 88 | +} |
| 89 | + |
| 90 | +export const DEFAULT_XAI_VIDEO_BASE_URL = "https://api.x.ai/v1"; |
| 91 | +export const DEFAULT_XAI_VIDEO_MODEL = "grok-imagine-video"; |
| 92 | +const XAI_VIDEO_15_MODEL = "grok-imagine-video-1.5"; |
| 93 | +export const XAI_VIDEO_DEFAULT_TIMEOUT_MS = 600_000; |
| 94 | +export const XAI_VIDEO_ASPECT_RATIOS = new Set(["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3"]); |
| 95 | +const XAI_VIDEO_15_CAPABILITIES = { |
| 96 | + imageToVideo: { |
| 97 | + enabled: true, |
| 98 | + maxVideos: 1, |
| 99 | + maxInputImages: 1, |
| 100 | + maxDurationSeconds: 15, |
| 101 | + aspectRatios: [...XAI_VIDEO_ASPECT_RATIOS], |
| 102 | + resolutions: ["480P", "720P", "1080P"], |
| 103 | + supportsAspectRatio: true, |
| 104 | + supportsResolution: true, |
| 105 | + }, |
| 106 | + videoToVideo: { |
| 107 | + enabled: false, |
| 108 | + }, |
| 109 | +} satisfies VideoGenerationProviderCapabilities; |
| 110 | + |
| 111 | +const XAI_VIDEO_15_MODEL_IDS = new Set([ |
| 112 | + XAI_VIDEO_15_MODEL, |
| 113 | + "grok-imagine-video-1.5-preview", |
| 114 | + "grok-imagine-video-1.5-2026-05-30", |
| 115 | +]); |
| 116 | + |
| 117 | +export function isXaiVideo15Model(model: string | undefined): boolean { |
| 118 | + const normalized = normalizeOptionalString(model); |
| 119 | + return normalized ? XAI_VIDEO_15_MODEL_IDS.has(normalized) : false; |
| 120 | +} |
| 121 | + |
| 122 | +export function createXaiVideoGenerationProviderMetadata() { |
| 123 | + return { |
| 124 | + id: "xai", |
| 125 | + label: "xAI", |
| 126 | + defaultModel: DEFAULT_XAI_VIDEO_MODEL, |
| 127 | + defaultTimeoutMs: XAI_VIDEO_DEFAULT_TIMEOUT_MS, |
| 128 | + models: [DEFAULT_XAI_VIDEO_MODEL, XAI_VIDEO_15_MODEL], |
| 129 | + catalogByModel: { |
| 130 | + [XAI_VIDEO_15_MODEL]: { |
| 131 | + capabilities: XAI_VIDEO_15_CAPABILITIES, |
| 132 | + modes: ["imageToVideo"], |
| 133 | + }, |
| 134 | + }, |
| 135 | + isConfigured: (ctx) => isProviderApiKeyConfigured({ provider: "xai", ...ctx }), |
| 136 | + capabilities: { |
| 137 | + generate: { |
| 138 | + maxVideos: 1, |
| 139 | + maxDurationSeconds: 15, |
| 140 | + aspectRatios: [...XAI_VIDEO_ASPECT_RATIOS], |
| 141 | + resolutions: ["480P", "720P"], |
| 142 | + supportsAspectRatio: true, |
| 143 | + supportsResolution: true, |
| 144 | + }, |
| 145 | + imageToVideo: { |
| 146 | + enabled: true, |
| 147 | + maxVideos: 1, |
| 148 | + maxInputImages: 7, |
| 149 | + maxDurationSeconds: 15, |
| 150 | + aspectRatios: [...XAI_VIDEO_ASPECT_RATIOS], |
| 151 | + resolutions: ["480P", "720P"], |
| 152 | + supportsAspectRatio: true, |
| 153 | + supportsResolution: true, |
| 154 | + }, |
| 155 | + videoToVideo: { |
| 156 | + enabled: true, |
| 157 | + maxVideos: 1, |
| 158 | + maxInputVideos: 1, |
| 159 | + maxDurationSeconds: 10, |
| 160 | + supportsAspectRatio: false, |
| 161 | + supportsResolution: false, |
| 162 | + }, |
| 163 | + }, |
| 164 | + resolveModelCapabilities: ({ model }) => |
| 165 | + isXaiVideo15Model(model) ? XAI_VIDEO_15_CAPABILITIES : undefined, |
| 166 | + } satisfies Omit<VideoGenerationProvider, "generateVideo">; |
| 167 | +} |
| 168 | + |
| 169 | +export type XaiRealtimeTranscriptionEncoding = "pcm" | "mulaw" | "alaw"; |
| 170 | + |
| 171 | +type XaiRealtimeTranscriptionProviderConfig = { |
| 172 | + apiKey?: string; |
| 173 | + baseUrl?: string; |
| 174 | + sampleRate?: number; |
| 175 | + encoding?: XaiRealtimeTranscriptionEncoding; |
| 176 | + interimResults?: boolean; |
| 177 | + endpointingMs?: number; |
| 178 | + language?: string; |
| 179 | +}; |
| 180 | + |
| 181 | +function normalizeRealtimeTranscriptionEncoding( |
| 182 | + value: unknown, |
| 183 | +): XaiRealtimeTranscriptionEncoding | undefined { |
| 184 | + const normalized = normalizeOptionalString(value)?.toLowerCase(); |
| 185 | + if (!normalized) { |
| 186 | + return undefined; |
| 187 | + } |
| 188 | + if (normalized === "ulaw" || normalized === "g711_ulaw" || normalized === "g711-mulaw") { |
| 189 | + return "mulaw"; |
| 190 | + } |
| 191 | + if (normalized === "g711_alaw" || normalized === "g711-alaw") { |
| 192 | + return "alaw"; |
| 193 | + } |
| 194 | + if (normalized === "pcm" || normalized === "mulaw" || normalized === "alaw") { |
| 195 | + return normalized; |
| 196 | + } |
| 197 | + throw new Error(`Invalid xAI realtime transcription encoding: ${normalized}`); |
| 198 | +} |
| 199 | + |
| 200 | +export function normalizeXaiRealtimeTranscriptionProviderConfig( |
| 201 | + config: RealtimeTranscriptionProviderConfig, |
| 202 | +): XaiRealtimeTranscriptionProviderConfig { |
| 203 | + const raw = isRecord(config) ? config : undefined; |
| 204 | + const providers = isRecord(raw?.providers) ? raw.providers : undefined; |
| 205 | + const nested = providers?.xai ?? raw?.xai ?? raw; |
| 206 | + const xai = isRecord(nested) ? nested : {}; |
| 207 | + return { |
| 208 | + apiKey: normalizeResolvedSecretInputString({ |
| 209 | + value: xai.apiKey, |
| 210 | + path: "plugins.entries.voice-call.config.streaming.providers.xai.apiKey", |
| 211 | + }), |
| 212 | + baseUrl: normalizeOptionalString(xai.baseUrl), |
| 213 | + sampleRate: readFiniteNumber(xai.sampleRate ?? xai.sample_rate), |
| 214 | + encoding: normalizeRealtimeTranscriptionEncoding(xai.encoding), |
| 215 | + interimResults: readBoolean(xai.interimResults ?? xai.interim_results), |
| 216 | + endpointingMs: readFiniteNumber(xai.endpointingMs ?? xai.endpointing ?? xai.silenceDurationMs), |
| 217 | + language: normalizeOptionalString(xai.language), |
| 218 | + }; |
| 219 | +} |
| 220 | + |
| 221 | +export function createXaiRealtimeTranscriptionProviderMetadata() { |
| 222 | + return { |
| 223 | + id: "xai", |
| 224 | + label: "xAI Realtime Transcription", |
| 225 | + aliases: ["xai-realtime", "grok-stt-streaming"], |
| 226 | + autoSelectOrder: 25, |
| 227 | + resolveConfig: ({ rawConfig }) => normalizeXaiRealtimeTranscriptionProviderConfig(rawConfig), |
| 228 | + isConfigured: ({ providerConfig, cfg }) => |
| 229 | + Boolean( |
| 230 | + normalizeXaiRealtimeTranscriptionProviderConfig(providerConfig).apiKey ?? |
| 231 | + normalizeOptionalString(process.env.XAI_API_KEY), |
| 232 | + ) || isProviderAuthProfileConfigured({ provider: "xai", cfg }), |
| 233 | + } satisfies Omit<RealtimeTranscriptionProviderPlugin, "createSession">; |
| 234 | +} |
| 235 | + |
| 236 | +const XAI_REALTIME_AUDIO_FORMAT_G711_ULAW_8KHZ = { |
| 237 | + encoding: "g711_ulaw", |
| 238 | + sampleRateHz: 8000, |
| 239 | + channels: 1, |
| 240 | +} satisfies RealtimeVoiceAudioFormat; |
| 241 | +const XAI_REALTIME_AUDIO_FORMAT_PCM16_24KHZ = { |
| 242 | + encoding: "pcm16", |
| 243 | + sampleRateHz: 24000, |
| 244 | + channels: 1, |
| 245 | +} satisfies RealtimeVoiceAudioFormat; |
| 246 | + |
| 247 | +export function createXaiRealtimeVoiceProviderMetadata() { |
| 248 | + return { |
| 249 | + id: "xai", |
| 250 | + label: "xAI Grok Voice", |
| 251 | + aliases: ["xai-realtime-voice", "grok-voice"], |
| 252 | + defaultModel: XAI_REALTIME_DEFAULT_MODEL, |
| 253 | + voices: XAI_REALTIME_VOICES, |
| 254 | + autoSelectOrder: 25, |
| 255 | + capabilities: { |
| 256 | + transports: ["gateway-relay"], |
| 257 | + inputAudioFormats: [ |
| 258 | + XAI_REALTIME_AUDIO_FORMAT_G711_ULAW_8KHZ, |
| 259 | + XAI_REALTIME_AUDIO_FORMAT_PCM16_24KHZ, |
| 260 | + ], |
| 261 | + outputAudioFormats: [ |
| 262 | + XAI_REALTIME_AUDIO_FORMAT_G711_ULAW_8KHZ, |
| 263 | + XAI_REALTIME_AUDIO_FORMAT_PCM16_24KHZ, |
| 264 | + ], |
| 265 | + supportsBargeIn: true, |
| 266 | + handlesInputAudioBargeIn: true, |
| 267 | + supportsToolCalls: true, |
| 268 | + supportsSessionResumption: true, |
| 269 | + }, |
| 270 | + resolveConfig: ({ rawConfig }) => normalizeXaiRealtimeProviderConfig(rawConfig), |
| 271 | + isConfigured: ({ providerConfig, cfg }) => |
| 272 | + hasXaiRealtimeApiKeyInput(normalizeXaiRealtimeProviderConfig(providerConfig).apiKey, cfg), |
| 273 | + } satisfies Omit<RealtimeVoiceProviderPlugin, "createBridge" | "createBrowserSession">; |
| 274 | +} |
| 275 | + |
| 276 | +export function assertXaiRealtimeVoiceRequestSupported( |
| 277 | + req: RealtimeVoiceBridgeCreateRequest, |
| 278 | +): void { |
| 279 | + const config = normalizeXaiRealtimeProviderConfig(req.providerConfig); |
| 280 | + if (req.autoRespondToAudio === false) { |
| 281 | + throw new Error( |
| 282 | + 'xAI realtime voice requires automatic server-VAD responses; use consultRouting: "provider-direct"', |
| 283 | + ); |
| 284 | + } |
| 285 | + if ((req.interruptResponseOnInputAudio ?? config.interruptResponseOnInputAudio) === false) { |
| 286 | + throw new Error("xAI realtime voice requires automatic server-VAD interruption handling"); |
| 287 | + } |
| 288 | +} |
0 commit comments