UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

96 lines (95 loc) • 3.05 kB
import { asNullableRecord, isRecord } from "openclaw/plugin-sdk/string-coerce-runtime"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { ensureRecord } from "openclaw/plugin-sdk/collection-input-runtime"; import { mergeMissing } from "openclaw/plugin-sdk/runtime-doctor-migrations"; //#region extensions/elevenlabs/config-compat.ts const ELEVENLABS_API_KEY_ENV = "ELEVENLABS_API_KEY"; const PROFILE_CANDIDATES = [ ".profile", ".zprofile", ".zshrc", ".bashrc" ]; const LEGACY_TALK_FIELD_KEYS = [ "voiceId", "voiceAliases", "modelId", "outputFormat", "apiKey" ]; const ELEVENLABS_TALK_PROVIDER_ID = "elevenlabs"; function isBlockedObjectKey(e) { return e === "__proto__" || e === "prototype" || e === "constructor"; } function hasLegacyTalkFields(e) { const t = asNullableRecord(e); if (!t) return false; return LEGACY_TALK_FIELD_KEYS.some((e) => Object.hasOwn(t, e)); } function resolveTalkMigrationTargetProviderId(e) { const t = typeof e.provider === "string" && e.provider.trim() ? e.provider.trim() : null; const n = asNullableRecord(e.providers); if (t) { if (isBlockedObjectKey(t)) return null; return t; } if (!n) return ELEVENLABS_TALK_PROVIDER_ID; const r = Object.keys(n).filter((e) => !isBlockedObjectKey(e)); if (r.length === 0) return ELEVENLABS_TALK_PROVIDER_ID; if (r.length === 1) return r[0] ?? null; return null; } function migrateElevenLabsLegacyTalkConfig(e) { if (!isRecord(e)) return { config: e, changes: [] }; const t = asNullableRecord(e.talk); if (!t || !hasLegacyTalkFields(t)) return { config: e, changes: [] }; const n = resolveTalkMigrationTargetProviderId(t); if (!n) return { config: e, changes: ["Skipped talk legacy field migration because talk.providers defines multiple providers and talk.provider is unset; move talk.voiceId/talk.voiceAliases/talk.modelId/talk.outputFormat/talk.apiKey under the intended provider manually."] }; const r = structuredClone(e); const i = ensureRecord(r, "talk"); const a = ensureRecord(i, "providers"); const o = asNullableRecord(a[n]) ?? {}; const s = {}; const c = []; for (const e of LEGACY_TALK_FIELD_KEYS) { if (!Object.hasOwn(i, e)) continue; s[e] = i[e]; delete i[e]; c.push(e); } mergeMissing(o, s); a[n] = o; return { config: r, changes: [`Moved talk legacy fields (${c.join(", ")}) → talk.providers.${n} (filled missing provider fields only).`] }; } function readApiKeyFromProfile() { const e = os.homedir(); for (const t of PROFILE_CANDIDATES) { const n = path.join(e, t); try { const e = fs.readFileSync(n, "utf-8").match(/(?:^|\n)\s*(?:export\s+)?ELEVENLABS_API_KEY\s*=\s*["']?([^\n"']+)["']?/)?.[1]?.trim(); if (e) return e; } catch {} } return null; } function resolveElevenLabsApiKeyWithProfileFallback() { const e = (process.env[ELEVENLABS_API_KEY_ENV] ?? "").trim(); if (e) return e; return readApiKeyFromProfile(); } //#endregion export { resolveElevenLabsApiKeyWithProfileFallback as n, migrateElevenLabsLegacyTalkConfig as t };