openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
152 lines (151 loc) • 5.66 kB
JavaScript
import { t as XAI_BASE_URL } from "./model-definitions-DO0IXz00.mjs";
import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-input";
import { asFiniteNumberInRange, asOptionalObjectRecord, normalizeLowercaseStringOrEmpty, normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
import { createSpeechDirectiveParser } from "openclaw/plugin-sdk/speech-provider";
//#region extensions/xai/speech-provider-metadata-factory.ts
const XAI_SPEECH_RESPONSE_FORMATS = [
"mp3",
"wav",
"pcm",
"mulaw",
"alaw"
];
const XAI_TTS_FALLBACK_VOICES = [
"ara",
"eve",
"leo",
"rex",
"sal"
];
function normalizeXaiTtsBaseUrl(e) {
return normalizeOptionalString(e)?.replace(/\/+$/, "") ?? XAI_BASE_URL;
}
function isValidXaiTtsVoice(e) {
return normalizeOptionalString(e) !== void 0;
}
function normalizeXaiLanguageCode(e) {
const t = normalizeOptionalString(e)?.toLowerCase();
if (!t) return;
if (t === "auto" || /^[a-z]{2,3}(?:-[a-z]{2,4})?$/.test(t)) return t;
throw new Error(`xAI language must be "auto" or a BCP-47 tag (e.g. "en", "pt-br", "zh-cn"); got: ${t}`);
}
function normalizeXaiSpeechSpeed(e) {
return asFiniteNumberInRange(e, {
min: .7,
max: 1.5
});
}
function normalizeXaiSpeechResponseFormat(e) {
const t = normalizeLowercaseStringOrEmpty(e);
if (!t) return;
const n = XAI_SPEECH_RESPONSE_FORMATS.find((e) => e === t);
if (n) return n;
throw new Error(`Invalid xAI speech responseFormat: ${t}`);
}
function resolveXaiSpeechResponseFormat(e, t) {
return e === "voice-note" ? "mp3" : t ?? "mp3";
}
function xaiSpeechResponseFormatToFileExtension(e) {
return e === "wav" || e === "pcm" || e === "mulaw" || e === "alaw" ? `.${e}` : ".mp3";
}
function normalizeXaiSpeechProviderConfig(e) {
const t = asOptionalObjectRecord(e.providers);
const n = asOptionalObjectRecord(t?.xai) ?? {};
return {
apiKey: normalizeResolvedSecretInputString({
value: n.apiKey,
path: "tts.providers.xai.apiKey"
}),
baseUrl: normalizeXaiTtsBaseUrl(normalizeOptionalString(n.baseUrl) ?? normalizeOptionalString(process.env.XAI_BASE_URL) ?? XAI_BASE_URL),
voiceId: normalizeOptionalString(n.voiceId ?? n.voice) ?? "eve",
language: normalizeXaiLanguageCode(n.language ?? n.languageCode),
speed: normalizeXaiSpeechSpeed(n.speed),
responseFormat: normalizeXaiSpeechResponseFormat(n.responseFormat)
};
}
function readXaiSpeechProviderConfig(e) {
return {
apiKey: normalizeOptionalString(e.apiKey),
baseUrl: normalizeOptionalString(e.baseUrl) ?? normalizeXaiTtsBaseUrl(process.env.XAI_BASE_URL),
voiceId: normalizeOptionalString(e.voiceId ?? e.voice) ?? "eve",
language: normalizeXaiLanguageCode(e.language ?? e.languageCode),
speed: normalizeXaiSpeechSpeed(e.speed),
responseFormat: normalizeXaiSpeechResponseFormat(e.responseFormat)
};
}
function readXaiSpeechOverrides(e) {
if (!e) return {};
return {
voiceId: normalizeOptionalString(e.voiceId ?? e.voice),
language: normalizeXaiLanguageCode(e.language),
speed: normalizeXaiSpeechSpeed(e.speed)
};
}
function resolveDirectXaiAudioApiKey(e) {
return normalizeOptionalString(e) ?? normalizeOptionalString(process.env.XAI_API_KEY);
}
const parseXaiSpeechDirectiveToken = createSpeechDirectiveParser({ voice: [
"allowVoice",
[
"voice",
"voice_id",
"voiceid",
"xai_voice",
"xaivoice"
],
{ parse: (e) => isValidXaiTtsVoice(e.value) ? {
handled: true,
overrides: { voiceId: e.value }
} : {
handled: true,
warnings: [`invalid xAI voice "${e.value}"`]
} }
] });
function createXaiSpeechProviderMetadata(e) {
return {
id: "xai",
label: "xAI",
autoSelectOrder: 25,
models: [],
voices: XAI_TTS_FALLBACK_VOICES,
resolveConfig: ({ rawConfig: e }) => normalizeXaiSpeechProviderConfig(e),
parseDirectiveToken: parseXaiSpeechDirectiveToken,
resolveTalkConfig: ({ baseTtsConfig: e, talkProviderConfig: t }) => {
const n = normalizeXaiSpeechProviderConfig(e);
const r = normalizeXaiSpeechResponseFormat(t.responseFormat);
const i = normalizeOptionalString(t.baseUrl);
return {
...n,
apiKey: t.apiKey === void 0 ? n.apiKey : normalizeResolvedSecretInputString({
value: t.apiKey,
path: "talk.providers.xai.apiKey"
}),
baseUrl: i === void 0 ? n.baseUrl : normalizeXaiTtsBaseUrl(i),
voiceId: normalizeOptionalString(t.voiceId) ?? n.voiceId,
language: normalizeXaiLanguageCode(t.language ?? t.languageCode) ?? n.language,
speed: normalizeXaiSpeechSpeed(t.speed) ?? n.speed,
responseFormat: r ?? n.responseFormat
};
},
resolveTalkOverrides: ({ params: e }) => {
const t = normalizeOptionalString(e.voiceId ?? e.voice);
const n = normalizeXaiLanguageCode(e.language ?? e.languageCode);
const r = normalizeXaiSpeechSpeed(e.speed);
return {
...t === void 0 ? {} : { voiceId: t },
...n === void 0 ? {} : { language: n },
...r === void 0 ? {} : { speed: r }
};
},
isConfigured: ({ providerConfig: t, cfg: n }) => Boolean(resolveDirectXaiAudioApiKey(readXaiSpeechProviderConfig(t).apiKey)) || e.isProviderAuthProfileConfigured({
provider: "xai",
cfg: n
}),
isConfiguredAsync: async ({ providerConfig: t, cfg: n }) => Boolean(resolveDirectXaiAudioApiKey(readXaiSpeechProviderConfig(t).apiKey)) || await e.isProviderAuthProfileConfiguredAsync({
provider: "xai",
cfg: n
})
};
}
//#endregion
export { readXaiSpeechOverrides as a, resolveXaiSpeechResponseFormat as c, normalizeXaiTtsBaseUrl as i, xaiSpeechResponseFormatToFileExtension as l, createXaiSpeechProviderMetadata as n, readXaiSpeechProviderConfig as o, normalizeXaiLanguageCode as r, resolveDirectXaiAudioApiKey as s, XAI_TTS_FALLBACK_VOICES as t };