openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
172 lines (171 loc) • 6.59 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "../../string-coerce-CCIiB4H7.mjs";
import { D as parseStrictPositiveInteger } from "../../number-coercion-D6u-4YD8.mjs";
import { t as formatErrorMessage } from "../../errors-BKzCeTYb.mjs";
import { r as createLazyRuntimeModule } from "../../lazy-runtime-zGjszCtt.mjs";
import { a as resolveActiveTalkProviderConfig } from "../../talk-CqEDzucG.mjs";
import "../../string-coerce-runtime-BwAZZDf5.mjs";
import "../../number-runtime-W4chuk8B.mjs";
import "../../error-runtime-BM740gJj.mjs";
import { t as definePluginEntry } from "../../plugin-entry-CiJdaHtx.mjs";
import "../../talk-config-runtime-DiolyUdx.mjs";
import "../../api-0Q8AdG-R.mjs";
//#region extensions/talk-voice/index.ts
function mask(e) {
const t = e.trim();
if (t.length <= 6) return "***";
return `${t.slice(0, 6)}…`;
}
function isLikelyVoiceId(e) {
const t = e.trim();
if (t.length < 10 || t.length > 64) return false;
return /^[a-zA-Z0-9_-]+$/.test(t);
}
function resolveProviderLabel(e) {
switch (e) {
case "openai": return "OpenAI";
case "microsoft": return "Microsoft";
case "elevenlabs": return "ElevenLabs";
default: return e;
}
}
const TALK_ADMIN_SCOPE = "operator.admin";
const TALK_VOICE_PARAMETERS = {
type: "object",
properties: {
action: {
type: "string",
enum: ["list", "set"]
},
voice: {
type: "string",
description: "Voice ID from list; required for set."
}
},
required: ["action"],
additionalProperties: false
};
var talk_voice_default = definePluginEntry({
id: "talk-voice",
name: "Talk Voice",
description: "Select the active Talk voice and manage Talk voice configuration",
register(e) {
const t = createLazyRuntimeModule(() => import("./tool.js"));
e.registerTool((e) => {
const n = e.sessionKey;
return {
name: "talk_voice",
label: "Talk Voice",
description: "List or change the voice of the active realtime Talk call (browser, iOS, or Android) or Discord voice call in this conversation. Use list to see its provider, model, current voice, available voice IDs, and whether it can change. Use set with an available voice ID to reconnect the active call, preserving conversation and ongoing agent work. Success means the replacement call is ready. Saved voice defaults stay unchanged.",
parameters: TALK_VOICE_PARAMETERS,
execute: async (...e) => await (await t()).executeTalkVoiceTool(n, ...e)
};
}, { name: "talk_voice" });
e.registerCommand({
name: "voice",
nativeNames: { discord: "talkvoice" },
description: "List/set Talk provider voices (affects iOS Talk playback).",
acceptsArgs: true,
exposeSenderIsOwner: true,
handler: async (t) => {
const n = t.assertOwnerCurrent;
const r = t.channel === "discord" ? "/talkvoice" : "/voice";
const i = (t.args?.trim() ?? "").split(/\s+/).filter(Boolean);
const a = normalizeLowercaseStringOrEmpty(i[0] ?? "status");
const o = e.runtime.config.current();
const s = resolveActiveTalkProviderConfig(o.talk);
if (!s) return { text: "Talk voice is not configured.\n\nMissing: talk.provider and talk.providers.<provider>.\nSet it on the gateway, then retry." };
const c = s.provider;
const l = resolveProviderLabel(c);
const u = normalizeOptionalString(s.config.apiKey);
const d = normalizeOptionalString(s.config.baseUrl);
const f = normalizeOptionalString(s.config.voiceId);
if (a === "status") return { text: `Talk voice status:
- provider: ${c}\n- talk.providers.${c}.voiceId: ${f ? f : "(unset)"}\n- ${c}.apiKey: ${u ? mask(u) : "(unset)"}` };
if (a === "list") {
const t = parseStrictPositiveInteger(i[1]) ?? 12;
try {
const n = await e.runtime.tts.listVoices({
provider: c,
cfg: o,
apiKey: u || void 0,
baseUrl: d
});
const r = n.slice(0, Math.max(1, Math.min(t, 50)));
const i = [];
i.push(`${l} voices: ${n.length}`);
i.push("");
for (const e of r) {
const t = (e.name ?? "").trim() || "(unnamed)";
const n = (e.category ?? "").trim();
const r = n ? ` · ${n}` : "";
i.push(`- ${t}${r}`);
i.push(` id: ${e.id}`);
const a = [e.locale, e.gender];
const o = e.personalities?.filter((e) => e.trim().length > 0) ?? [];
if (o.length > 0) a.push(o.join(", "));
const s = a.filter((e) => Boolean(e?.trim()));
const c = s.length > 0 ? s.join(" · ") : void 0;
if (c) i.push(` meta: ${c}`);
const l = (e.description ?? "").trim();
if (l) i.push(` note: ${l}`);
}
if (n.length > r.length) {
i.push("");
i.push(`(showing first ${r.length})`);
}
return { text: i.join("\n") };
} catch (e) {
return { text: `${l} voice list failed: ${formatErrorMessage(e)}` };
}
}
if (a === "set") {
const { senderIsOwner: a, gatewayClientScopes: s } = t;
if (Array.isArray(s) ? !s.includes(TALK_ADMIN_SCOPE) : a !== true) return { text: `⚠️ ${r} set requires operator.admin.` };
const f = i.slice(1).join(" ").trim();
if (!f) return { text: `Usage: ${r} set <voiceId|name>` };
let p;
try {
p = await e.runtime.tts.listVoices({
provider: c,
cfg: o,
apiKey: u || void 0,
baseUrl: d
});
} catch (e) {
return { text: `${l} voice lookup failed: ${formatErrorMessage(e)}` };
}
const m = normalizeLowercaseStringOrEmpty(f);
const h = p.find((e) => e.id === f) ?? p.find((e) => normalizeOptionalLowercaseString(e.name) === m) ?? p.find((e) => normalizeLowercaseStringOrEmpty(e.name).includes(m)) ?? null;
if (!h) return { text: `No voice found for ${isLikelyVoiceId(f) ? f : `"${f}"`}. Try: ${r} list` };
await e.runtime.config.mutateConfigFile({
afterWrite: { mode: "auto" },
writeOptions: { assertCurrent: Array.isArray(t.gatewayClientScopes) ? void 0 : n },
mutate: (e) => {
e.talk = {
...e.talk,
provider: c,
providers: {
...e.talk?.providers,
[c]: {
...e.talk?.providers?.[c],
voiceId: h.id
}
}
};
}
});
return { text: `✅ ${l} Talk voice set to ${(h.name ?? "").trim() || "(unnamed)"}\n${h.id}` };
}
return { text: [
"Voice commands:",
"",
`${r} status`,
`${r} list [limit]`,
`${r} set <voiceId|name>`
].join("\n") };
}
});
}
});
//#endregion
export { talk_voice_default as default };