Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
298 lines
10 KiB
TypeScript
298 lines
10 KiB
TypeScript
// Xai provider module implements model/runtime integration.
|
|
import {
|
|
isProviderAuthProfileConfigured,
|
|
type OpenClawConfig,
|
|
} from "openclaw/plugin-sdk/provider-auth";
|
|
import { resolveApiKeyForProvider } from "openclaw/plugin-sdk/provider-auth-runtime";
|
|
import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-input";
|
|
import {
|
|
trimToUndefined,
|
|
type SpeechDirectiveTokenParseContext,
|
|
type SpeechProviderConfig,
|
|
type SpeechProviderOverrides,
|
|
type SpeechProviderPlugin,
|
|
type SpeechSynthesisTarget,
|
|
} from "openclaw/plugin-sdk/speech";
|
|
import {
|
|
asFiniteNumberInRange,
|
|
normalizeLowercaseStringOrEmpty,
|
|
} from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
import {
|
|
isValidXaiTtsVoice,
|
|
normalizeXaiLanguageCode,
|
|
normalizeXaiTtsBaseUrl,
|
|
XAI_BASE_URL,
|
|
XAI_TTS_VOICES,
|
|
xaiTTS,
|
|
} from "./tts.js";
|
|
|
|
const XAI_SPEECH_RESPONSE_FORMATS = ["mp3", "wav", "pcm", "mulaw", "alaw"] as const;
|
|
const DEFAULT_GENERATED_AUDIO_MAX_BYTES = 16 * 1024 * 1024;
|
|
|
|
type XaiSpeechResponseFormat = (typeof XAI_SPEECH_RESPONSE_FORMATS)[number];
|
|
|
|
type XaiTtsProviderConfig = {
|
|
apiKey?: string;
|
|
baseUrl: string;
|
|
voiceId: string;
|
|
language?: string;
|
|
speed?: number;
|
|
responseFormat?: XaiSpeechResponseFormat;
|
|
};
|
|
|
|
type XaiTtsProviderOverrides = {
|
|
voiceId?: string;
|
|
language?: string;
|
|
speed?: number;
|
|
};
|
|
|
|
function normalizeXaiSpeechSpeed(value: unknown): number | undefined {
|
|
return asFiniteNumberInRange(value, { min: 0.7, max: 1.5 });
|
|
}
|
|
|
|
function normalizeXaiSpeechResponseFormat(value: unknown): XaiSpeechResponseFormat | undefined {
|
|
const next = normalizeLowercaseStringOrEmpty(value);
|
|
if (!next) {
|
|
return undefined;
|
|
}
|
|
if (XAI_SPEECH_RESPONSE_FORMATS.some((format) => format === next)) {
|
|
return next as XaiSpeechResponseFormat;
|
|
}
|
|
throw new Error(`Invalid xAI speech responseFormat: ${next}`);
|
|
}
|
|
|
|
function resolveSpeechResponseFormat(
|
|
_target: SpeechSynthesisTarget,
|
|
configuredFormat?: XaiSpeechResponseFormat,
|
|
): XaiSpeechResponseFormat {
|
|
if (configuredFormat) {
|
|
return configuredFormat;
|
|
}
|
|
return "mp3";
|
|
}
|
|
|
|
function responseFormatToFileExtension(
|
|
format: XaiSpeechResponseFormat,
|
|
): ".mp3" | ".pcm" | ".wav" | ".mulaw" | ".alaw" {
|
|
switch (format) {
|
|
case "wav":
|
|
return ".wav";
|
|
case "pcm":
|
|
return ".pcm";
|
|
case "mulaw":
|
|
return ".mulaw";
|
|
case "alaw":
|
|
return ".alaw";
|
|
default:
|
|
return ".mp3";
|
|
}
|
|
}
|
|
|
|
function normalizeXaiProviderConfig(rawConfig: Record<string, unknown>): XaiTtsProviderConfig {
|
|
const providers = rawConfig?.providers as Record<string, unknown> | undefined;
|
|
const xai = (providers?.xai ?? rawConfig?.xai ?? rawConfig) as Record<string, unknown>;
|
|
return {
|
|
apiKey: normalizeResolvedSecretInputString({
|
|
value: xai?.apiKey,
|
|
path: "messages.tts.providers.xai.apiKey",
|
|
}),
|
|
baseUrl: normalizeXaiTtsBaseUrl(
|
|
trimToUndefined(xai?.baseUrl) ?? trimToUndefined(process.env.XAI_BASE_URL) ?? XAI_BASE_URL,
|
|
),
|
|
voiceId: trimToUndefined(xai?.voiceId ?? xai?.voice) ?? "eve",
|
|
language: normalizeXaiLanguageCode(trimToUndefined(xai?.language ?? xai?.languageCode)),
|
|
speed: normalizeXaiSpeechSpeed(xai?.speed),
|
|
responseFormat: normalizeXaiSpeechResponseFormat(xai?.responseFormat),
|
|
};
|
|
}
|
|
|
|
function readXaiProviderConfig(config: SpeechProviderConfig): XaiTtsProviderConfig {
|
|
const normalized = normalizeXaiProviderConfig({});
|
|
return {
|
|
apiKey: trimToUndefined(config.apiKey) ?? normalized.apiKey,
|
|
baseUrl: trimToUndefined(config.baseUrl) ?? normalized.baseUrl,
|
|
voiceId: trimToUndefined(config.voiceId ?? config.voice) ?? normalized.voiceId,
|
|
language:
|
|
normalizeXaiLanguageCode(trimToUndefined(config.language ?? config.languageCode)) ??
|
|
normalized.language,
|
|
speed: normalizeXaiSpeechSpeed(config.speed) ?? normalized.speed,
|
|
responseFormat:
|
|
normalizeXaiSpeechResponseFormat(config.responseFormat) ?? normalized.responseFormat,
|
|
};
|
|
}
|
|
|
|
function readXaiOverrides(overrides: SpeechProviderOverrides | undefined): XaiTtsProviderOverrides {
|
|
if (!overrides) {
|
|
return {};
|
|
}
|
|
return {
|
|
voiceId: trimToUndefined(overrides.voiceId ?? overrides.voice),
|
|
language: normalizeXaiLanguageCode(trimToUndefined(overrides.language)),
|
|
speed: normalizeXaiSpeechSpeed(overrides.speed),
|
|
};
|
|
}
|
|
|
|
function resolveGeneratedAudioMaxBytes(req: {
|
|
cfg: { agents?: { defaults?: { mediaMaxMb?: number } } };
|
|
}): number {
|
|
const configured = req.cfg.agents?.defaults?.mediaMaxMb;
|
|
if (typeof configured === "number" && Number.isFinite(configured) && configured > 0) {
|
|
return Math.floor(configured * 1024 * 1024);
|
|
}
|
|
return DEFAULT_GENERATED_AUDIO_MAX_BYTES;
|
|
}
|
|
|
|
function parseDirectiveToken(ctx: SpeechDirectiveTokenParseContext): {
|
|
handled: boolean;
|
|
overrides?: SpeechProviderOverrides;
|
|
warnings?: string[];
|
|
} {
|
|
const providerConfig = ctx.providerConfig as Record<string, unknown> | undefined;
|
|
const baseUrl = trimToUndefined(providerConfig?.baseUrl);
|
|
switch (ctx.key) {
|
|
case "voice":
|
|
case "voice_id":
|
|
case "voiceid":
|
|
case "xai_voice":
|
|
case "xaivoice":
|
|
if (!ctx.policy.allowVoice) {
|
|
return { handled: true };
|
|
}
|
|
if (!isValidXaiTtsVoice(ctx.value, baseUrl)) {
|
|
return { handled: true, warnings: [`invalid xAI voice "${ctx.value}"`] };
|
|
}
|
|
return { handled: true, overrides: { voiceId: ctx.value } };
|
|
default:
|
|
return { handled: false };
|
|
}
|
|
}
|
|
|
|
export function buildXaiSpeechProvider(): SpeechProviderPlugin {
|
|
return {
|
|
id: "xai",
|
|
label: "xAI",
|
|
autoSelectOrder: 25,
|
|
models: [],
|
|
voices: XAI_TTS_VOICES,
|
|
resolveConfig: ({ rawConfig }) => normalizeXaiProviderConfig(rawConfig),
|
|
parseDirectiveToken,
|
|
resolveTalkConfig: ({ baseTtsConfig, talkProviderConfig }) => {
|
|
const base = normalizeXaiProviderConfig(baseTtsConfig);
|
|
const responseFormat = normalizeXaiSpeechResponseFormat(talkProviderConfig.responseFormat);
|
|
return {
|
|
...base,
|
|
...(talkProviderConfig.apiKey === undefined
|
|
? {}
|
|
: {
|
|
apiKey: normalizeResolvedSecretInputString({
|
|
value: talkProviderConfig.apiKey,
|
|
path: "talk.providers.xai.apiKey",
|
|
}),
|
|
}),
|
|
...(trimToUndefined(talkProviderConfig.baseUrl) == null
|
|
? {}
|
|
: { baseUrl: normalizeXaiTtsBaseUrl(trimToUndefined(talkProviderConfig.baseUrl)) }),
|
|
...(trimToUndefined(talkProviderConfig.voiceId) == null
|
|
? {}
|
|
: { voiceId: trimToUndefined(talkProviderConfig.voiceId) }),
|
|
...(normalizeXaiLanguageCode(
|
|
trimToUndefined(talkProviderConfig.language ?? talkProviderConfig.languageCode),
|
|
) == null
|
|
? {}
|
|
: {
|
|
language: normalizeXaiLanguageCode(
|
|
trimToUndefined(talkProviderConfig.language ?? talkProviderConfig.languageCode),
|
|
),
|
|
}),
|
|
...(normalizeXaiSpeechSpeed(talkProviderConfig.speed) == null
|
|
? {}
|
|
: { speed: normalizeXaiSpeechSpeed(talkProviderConfig.speed) }),
|
|
...(responseFormat == null ? {} : { responseFormat }),
|
|
};
|
|
},
|
|
resolveTalkOverrides: ({ params }) => ({
|
|
...(trimToUndefined(params.voiceId ?? params.voice) == null
|
|
? {}
|
|
: { voiceId: trimToUndefined(params.voiceId ?? params.voice) }),
|
|
...(normalizeXaiLanguageCode(trimToUndefined(params.language ?? params.languageCode)) == null
|
|
? {}
|
|
: {
|
|
language: normalizeXaiLanguageCode(
|
|
trimToUndefined(params.language ?? params.languageCode),
|
|
),
|
|
}),
|
|
...(normalizeXaiSpeechSpeed(params.speed) == null
|
|
? {}
|
|
: { speed: normalizeXaiSpeechSpeed(params.speed) }),
|
|
}),
|
|
listVoices: async () => XAI_TTS_VOICES.map((voice) => ({ id: voice, name: voice })),
|
|
isConfigured: ({ providerConfig, cfg }) =>
|
|
Boolean(readXaiProviderConfig(providerConfig).apiKey || process.env.XAI_API_KEY) ||
|
|
isProviderAuthProfileConfigured({ provider: "xai", cfg }),
|
|
synthesize: async (req) => {
|
|
const config = readXaiProviderConfig(req.providerConfig);
|
|
const overrides = readXaiOverrides(req.providerOverrides);
|
|
const apiKey = await resolveXaiAudioApiKey(config.apiKey, req.cfg);
|
|
const responseFormat = resolveSpeechResponseFormat(req.target, config.responseFormat);
|
|
const audioBuffer = await xaiTTS({
|
|
text: req.text,
|
|
apiKey,
|
|
baseUrl: config.baseUrl,
|
|
voiceId: overrides.voiceId ?? config.voiceId,
|
|
language: overrides.language ?? config.language,
|
|
speed: overrides.speed ?? config.speed,
|
|
responseFormat,
|
|
timeoutMs: req.timeoutMs,
|
|
maxBytes: resolveGeneratedAudioMaxBytes(req),
|
|
});
|
|
return {
|
|
audioBuffer,
|
|
outputFormat: responseFormat,
|
|
fileExtension: responseFormatToFileExtension(responseFormat),
|
|
voiceCompatible: false,
|
|
};
|
|
},
|
|
synthesizeTelephony: async (req) => {
|
|
const config = readXaiProviderConfig(req.providerConfig);
|
|
const overrides = readXaiOverrides(req.providerOverrides);
|
|
const apiKey = await resolveXaiAudioApiKey(config.apiKey, req.cfg);
|
|
const outputFormat = "pcm" as const;
|
|
const sampleRate = 24000;
|
|
const audioBuffer = await xaiTTS({
|
|
text: req.text,
|
|
apiKey,
|
|
baseUrl: config.baseUrl,
|
|
voiceId: overrides.voiceId ?? config.voiceId,
|
|
language: overrides.language ?? config.language,
|
|
speed: overrides.speed ?? config.speed,
|
|
responseFormat: outputFormat,
|
|
timeoutMs: req.timeoutMs,
|
|
maxBytes: resolveGeneratedAudioMaxBytes(req),
|
|
});
|
|
return { audioBuffer, outputFormat, sampleRate };
|
|
},
|
|
};
|
|
}
|
|
|
|
// Resolve an xAI bearer for `/v1/tts`:
|
|
// 1. Configured `messages.tts.providers.xai.apiKey` (or talk equivalent)
|
|
// 2. `XAI_API_KEY` env var
|
|
// 3. xAI OAuth auth profile (cfg-scoped)
|
|
async function resolveXaiAudioApiKey(
|
|
configApiKey: string | undefined,
|
|
cfg: OpenClawConfig,
|
|
): Promise<string> {
|
|
const direct = trimToUndefined(configApiKey) ?? trimToUndefined(process.env.XAI_API_KEY);
|
|
if (direct) {
|
|
return direct;
|
|
}
|
|
const auth = await resolveApiKeyForProvider({ provider: "xai", cfg });
|
|
const oauthKey = trimToUndefined(auth?.apiKey);
|
|
if (oauthKey) {
|
|
return oauthKey;
|
|
}
|
|
throw new Error(
|
|
"xAI credentials missing for TTS. Sign in with `openclaw onboard --auth-choice xai-oauth`, or run `openclaw onboard --auth-choice xai-api-key`, or set XAI_API_KEY.",
|
|
);
|
|
}
|