UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

272 lines (271 loc) • 8.45 kB
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime"; import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-input"; import { createSpeechDirectiveParser, resolveSpeechProviderApiKey } from "openclaw/plugin-sdk/speech-provider"; import { asFiniteNumberInRange, asOptionalRecord, normalizeOptionalString, readStringField } from "openclaw/plugin-sdk/string-coerce-runtime"; //#region extensions/minimax/speech-provider-factory.ts const DEFAULT_MINIMAX_TTS_BASE_URL = "https://api.minimax.io"; const MINIMAX_TTS_MODELS = [ "speech-2.8-hd", "speech-2.8-turbo", "speech-2.6-hd", "speech-2.6-turbo", "speech-02-hd", "speech-02-turbo", "speech-01-hd", "speech-01-turbo" ]; const MINIMAX_TTS_VOICES = [ "English_expressive_narrator", "Chinese (Mandarin)_Warm_Girl", "Chinese (Mandarin)_Lively_Girl", "Chinese (Mandarin)_Gentle_Boy", "Chinese (Mandarin)_Steady_Boy" ]; function normalizeMinimaxTtsBaseUrl(e) { const t = e?.trim(); return t ? t.replace(/\/+$/, "").replace(/\/(?:anthropic|v1)$/i, "") : DEFAULT_MINIMAX_TTS_BASE_URL; } const MINIMAX_PORTAL_PROVIDER_ID = "minimax-portal"; const MINIMAX_TOKEN_PLAN_ENV_VARS = [ "MINIMAX_OAUTH_TOKEN", "MINIMAX_CODE_PLAN_KEY", "MINIMAX_CODING_API_KEY" ]; function resolveConfiguredPortalTtsBaseUrl(e) { const t = normalizeOptionalString(e?.models?.providers?.[MINIMAX_PORTAL_PROVIDER_ID]?.baseUrl); return t ? normalizeMinimaxTtsBaseUrl(t) : void 0; } function resolveMinimaxDirectTtsApiKey(e) { return resolveSpeechProviderApiKey(e, ...MINIMAX_TOKEN_PLAN_ENV_VARS.map((e) => process.env[e]), process.env.MINIMAX_API_KEY); } function normalizeMinimaxProviderConfig(e, t) { const n = asOptionalRecord(e.providers); const r = asOptionalRecord(n?.minimax); return { apiKey: normalizeResolvedSecretInputString({ value: r?.apiKey, path: "tts.providers.minimax.apiKey" }), baseUrl: normalizeMinimaxTtsBaseUrl(normalizeOptionalString(r?.baseUrl) ?? normalizeOptionalString(process.env.MINIMAX_API_HOST) ?? resolveConfiguredPortalTtsBaseUrl(t) ?? DEFAULT_MINIMAX_TTS_BASE_URL), model: normalizeOptionalString(r?.model) ?? normalizeOptionalString(process.env.MINIMAX_TTS_MODEL) ?? "speech-2.8-hd", voiceId: normalizeOptionalString(r?.voiceId) ?? normalizeOptionalString(process.env.MINIMAX_TTS_VOICE_ID) ?? "English_expressive_narrator", speed: normalizeMinimaxSpeed(r?.speed), vol: normalizeMinimaxVolume(r?.vol), pitch: normalizeMinimaxPitch(r?.pitch) }; } function normalizeMinimaxSpeed(e) { return asFiniteNumberInRange(e, { min: .5, max: 2 }); } function normalizeMinimaxVolume(e) { return asFiniteNumberInRange(e, { min: 0, max: 10, minExclusive: true }); } function normalizeMinimaxPitch(e) { const t = asFiniteNumberInRange(e, { min: -12, max: 12 }); return t !== void 0 ? Math.trunc(t) : void 0; } function readMinimaxProviderConfig(e, t) { return normalizeMinimaxProviderConfig({ providers: { minimax: { ...e, apiKey: normalizeOptionalString(e.apiKey) } } }, t); } function readMinimaxOverrides(e) { if (!e) return {}; const t = { model: normalizeOptionalString(e.model), voiceId: normalizeOptionalString(e.voiceId), speed: normalizeMinimaxSpeed(e.speed), vol: normalizeMinimaxVolume(e.vol), pitch: normalizeMinimaxPitch(e.pitch) }; for (const e of [ "model", "voiceId", "speed", "vol", "pitch" ]) if (t[e] === void 0) delete t[e]; return t; } const parseDirectiveToken = createSpeechDirectiveParser({ voiceId: ["allowVoice", [ "voice", "voiceid", "voice_id", "minimax_voice", "minimaxvoice" ]], model: ["allowModelId", [ "model", "minimax_model", "minimaxmodel" ]], speed: [ "allowVoiceSettings", ["speed"], { number: { range: { min: .5, max: 2 }, warning: (e) => `invalid MiniMax speed "${e}" (0.5-2.0)` } } ], vol: [ "allowVoiceSettings", ["vol", "volume"], { number: { range: { min: 0, minExclusive: true, max: 10 }, warning: (e) => `invalid MiniMax volume "${e}" (must be greater than 0 and at most 10)` } } ], pitch: [ "allowVoiceSettings", ["pitch"], { number: { range: { min: -12, max: 12 }, warning: (e) => `invalid MiniMax pitch "${e}" (-12 to 12)` } } ] }); function buildMinimaxSpeechProvider({ isProviderAuthProfileConfigured: e, isProviderAuthProfileConfiguredAsync: t }) { return { id: "minimax", label: "MiniMax", autoSelectOrder: 40, defaultModel: MINIMAX_TTS_MODELS[0], models: MINIMAX_TTS_MODELS, voices: MINIMAX_TTS_VOICES, resolveConfig: ({ rawConfig: e, cfg: t }) => normalizeMinimaxProviderConfig(e, t), parseDirectiveToken, resolveTalkConfig: ({ baseTtsConfig: e, talkProviderConfig: t }) => { const n = normalizeMinimaxProviderConfig(e); const r = normalizeOptionalString(t.baseUrl); return { ...n, ...t.apiKey === void 0 ? {} : { apiKey: normalizeResolvedSecretInputString({ value: t.apiKey, path: "talk.providers.minimax.apiKey" }) }, ...r ? { baseUrl: normalizeMinimaxTtsBaseUrl(r) } : {}, ...readMinimaxOverrides({ ...t, model: t.modelId }) }; }, resolveTalkOverrides: ({ params: e }) => readMinimaxOverrides({ ...e, model: e.modelId }), listVoices: async () => MINIMAX_TTS_VOICES.map((e) => ({ id: e, name: e })), isConfigured: ({ cfg: t, providerConfig: n }) => Boolean(resolveMinimaxDirectTtsApiKey(readMinimaxProviderConfig(n, t).apiKey) || e({ cfg: t, provider: MINIMAX_PORTAL_PROVIDER_ID })), isConfiguredAsync: async ({ cfg: e, providerConfig: n }) => Boolean(resolveMinimaxDirectTtsApiKey(readMinimaxProviderConfig(n, e).apiKey) || await t({ cfg: e, provider: MINIMAX_PORTAL_PROVIDER_ID })), synthesize: async (e) => { const t = readMinimaxProviderConfig(e.providerConfig, e.cfg); const n = readMinimaxOverrides(e.providerOverrides); const { resolveProviderAuthProfileApiKey: r } = await import("openclaw/plugin-sdk/provider-auth"); const i = resolveSpeechProviderApiKey(t.apiKey, t.apiKey ? void 0 : await r({ cfg: e.cfg, provider: MINIMAX_PORTAL_PROVIDER_ID }), resolveMinimaxDirectTtsApiKey()); if (!i) throw new Error("MiniMax TTS auth missing"); const a = resolveTimerTimeoutMs(e.timeoutMs, 1); const { assertOkOrThrowProviderError: o, readProviderJsonObjectResponse: s } = await import("openclaw/plugin-sdk/provider-http"); const { fetchWithSsrFGuard: c, ssrfPolicyFromHttpBaseUrlAllowedHostname: l } = await import("openclaw/plugin-sdk/ssrf-runtime"); const { assertMinimaxBaseResp: u, normalizeMinimaxHexAudio: d } = await import("./.setup/media-provider-runtime-BThOjZc7.mjs"); let f; const { response: p, release: m } = await c({ url: `${t.baseUrl}/v1/t2a_v2`, init: { method: "POST", headers: { Authorization: `Bearer ${i}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: n.model ?? t.model, text: e.text, stream: false, output_format: "hex", voice_setting: { voice_id: n.voiceId ?? t.voiceId, speed: n.speed ?? t.speed ?? 1, vol: n.vol ?? t.vol ?? 1, pitch: n.pitch ?? t.pitch ?? 0 }, audio_setting: { format: "mp3", sample_rate: 32e3 } }) }, timeoutMs: a, policy: l(t.baseUrl), auditContext: "minimax.tts" }); try { await o(p, "MiniMax TTS API error"); const e = await s(p, "minimax.tts"); u(e.base_resp, "MiniMax TTS API error"); const t = readStringField(asOptionalRecord(e.data), "audio"); if (!t) throw new Error("MiniMax TTS API returned no audio data"); f = Buffer.from(d(t, "MiniMax TTS API"), "hex"); } finally { await m(); } if (e.target === "voice-note") { const { transcodeAudioBufferToOpus: t } = await import("openclaw/plugin-sdk/media-runtime"); return { audioBuffer: await t({ audioBuffer: f, inputExtension: "mp3", tempPrefix: "tts-minimax-", timeoutMs: e.timeoutMs }), outputFormat: "opus", fileExtension: ".opus", voiceCompatible: true }; } return { audioBuffer: f, outputFormat: "mp3", fileExtension: ".mp3", voiceCompatible: false }; } }; } //#endregion //#region extensions/minimax/capability-catalog.ts const catalog = (e) => ({ speechProviders: [buildMinimaxSpeechProvider(e)] }); //#endregion export { catalog as default };