Files
adolf/extensions/volcengine/speech-provider.ts
alvis bedb527145
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
Vendor OpenClaw source as Adolf fork baseline
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
2026-07-05 09:36:54 +00:00

239 lines
7.7 KiB
TypeScript

// Volcengine provider module implements model/runtime integration.
import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-input";
import type {
SpeechDirectiveTokenParseContext,
SpeechProviderConfig,
SpeechProviderOverrides,
SpeechProviderPlugin,
} from "openclaw/plugin-sdk/speech-core";
import {
asObject,
parseSpeechDirectiveNumberOverride,
trimToUndefined,
} from "openclaw/plugin-sdk/speech-core";
import { asFiniteNumberInRange } from "openclaw/plugin-sdk/string-coerce-runtime";
import { volcengineTTS, type VolcengineTtsEncoding } from "./tts.js";
const DEFAULT_VOICE = "en_female_anna_mars_bigtts";
const DEFAULT_CLUSTER = "volcano_tts";
const DEFAULT_RESOURCE_ID = "seed-tts-1.0";
const DEFAULT_APP_KEY = "aGjiRDfUWi";
const VOLCENGINE_VOICES: readonly string[] = [
"en_female_anna_mars_bigtts",
"en_male_adam_mars_bigtts",
"en_female_sarah_mars_bigtts",
"en_male_smith_mars_bigtts",
"zh_female_cancan_mars_bigtts",
"zh_female_qingxinnvsheng_mars_bigtts",
"zh_female_linjia_mars_bigtts",
"zh_male_wennuanahu_moon_bigtts",
"zh_male_shaonianzixin_moon_bigtts",
"zh_female_shuangkuaisisi_moon_bigtts",
];
type VolcengineTtsProviderConfig = {
apiKey?: string;
appId?: string;
token?: string;
voice: string;
cluster: string;
resourceId: string;
appKey: string;
baseUrl?: string;
speedRatio?: number;
emotion?: string;
};
type VolcengineTtsProviderOverrides = {
voice?: string;
speedRatio?: number;
emotion?: string;
};
function normalizeSpeedRatio(value: unknown): number | undefined {
return asFiniteNumberInRange(value, { min: 0.2, max: 3 });
}
function normalizeVolcengineProviderConfig(
rawConfig: Record<string, unknown>,
): VolcengineTtsProviderConfig {
const providers = asObject(rawConfig.providers);
const raw = asObject(providers?.volcengine) ?? asObject(rawConfig.volcengine);
return {
apiKey: normalizeResolvedSecretInputString({
value: raw?.apiKey,
path: "messages.tts.providers.volcengine.apiKey",
}),
appId: trimToUndefined(raw?.appId),
token: normalizeResolvedSecretInputString({
value: raw?.token,
path: "messages.tts.providers.volcengine.token",
}),
voice:
trimToUndefined(raw?.voice) ??
trimToUndefined(process.env.VOLCENGINE_TTS_VOICE) ??
DEFAULT_VOICE,
cluster:
trimToUndefined(raw?.cluster) ??
trimToUndefined(process.env.VOLCENGINE_TTS_CLUSTER) ??
DEFAULT_CLUSTER,
resourceId:
trimToUndefined(raw?.resourceId) ??
trimToUndefined(process.env.VOLCENGINE_TTS_RESOURCE_ID) ??
DEFAULT_RESOURCE_ID,
appKey:
trimToUndefined(raw?.appKey) ??
trimToUndefined(process.env.VOLCENGINE_TTS_APP_KEY) ??
DEFAULT_APP_KEY,
baseUrl: trimToUndefined(raw?.baseUrl) ?? trimToUndefined(process.env.VOLCENGINE_TTS_BASE_URL),
speedRatio: normalizeSpeedRatio(raw?.speedRatio),
emotion: trimToUndefined(raw?.emotion),
};
}
function resolveSeedSpeechApiKey(configApiKey?: string): string | undefined {
return (
configApiKey ??
trimToUndefined(process.env.VOLCENGINE_TTS_API_KEY) ??
trimToUndefined(process.env.BYTEPLUS_SEED_SPEECH_API_KEY)
);
}
function readProviderConfig(config: SpeechProviderConfig): VolcengineTtsProviderConfig {
const normalized = normalizeVolcengineProviderConfig({});
return {
apiKey:
normalizeResolvedSecretInputString({
value: config.apiKey,
path: "messages.tts.providers.volcengine.apiKey",
}) ?? normalized.apiKey,
appId: trimToUndefined(config.appId) ?? normalized.appId,
token: trimToUndefined(config.token) ?? normalized.token,
voice: trimToUndefined(config.voice) ?? normalized.voice,
cluster: trimToUndefined(config.cluster) ?? normalized.cluster,
resourceId: trimToUndefined(config.resourceId) ?? normalized.resourceId,
appKey: trimToUndefined(config.appKey) ?? normalized.appKey,
baseUrl: trimToUndefined(config.baseUrl) ?? normalized.baseUrl,
speedRatio: normalizeSpeedRatio(config.speedRatio) ?? normalized.speedRatio,
emotion: trimToUndefined(config.emotion) ?? normalized.emotion,
};
}
function readVolcengineOverrides(
overrides: SpeechProviderOverrides | undefined,
): VolcengineTtsProviderOverrides {
if (!overrides) {
return {};
}
return {
voice: trimToUndefined(overrides.voice),
speedRatio: normalizeSpeedRatio(overrides.speedRatio),
emotion: trimToUndefined(overrides.emotion),
};
}
function parseDirectiveToken(ctx: SpeechDirectiveTokenParseContext): {
handled: boolean;
overrides?: SpeechProviderOverrides;
warnings?: string[];
} {
switch (ctx.key) {
case "voice":
case "volcengine_voice":
case "volcenginevoice":
if (!ctx.policy.allowVoice) {
return { handled: true };
}
return { handled: true, overrides: { ...ctx.currentOverrides, voice: ctx.value } };
case "speed":
case "speedratio":
case "speed_ratio": {
return parseSpeechDirectiveNumberOverride({
ctx,
overrideKey: "speedRatio",
range: { min: 0.2, max: 3 },
warning: (value) => `invalid Volcengine speedRatio "${value}"`,
mergeCurrentOverrides: true,
});
}
case "emotion":
if (!ctx.policy.allowVoiceSettings) {
return { handled: true };
}
return { handled: true, overrides: { ...ctx.currentOverrides, emotion: ctx.value } };
default:
return { handled: false };
}
}
export function buildVolcengineSpeechProvider(): SpeechProviderPlugin {
return {
id: "volcengine",
label: "Volcengine",
autoSelectOrder: 90,
aliases: ["bytedance", "doubao"],
voices: VOLCENGINE_VOICES,
resolveConfig: ({ rawConfig }) => normalizeVolcengineProviderConfig(rawConfig),
parseDirectiveToken,
listVoices: async () =>
VOLCENGINE_VOICES.map((v) => ({
id: v,
name: v.replace(/^(?:en|zh)_(female|male)_/, "").replace(/_.*$/, ""),
locale: v.startsWith("en_") ? "en-US" : "zh-CN",
gender: v.includes("_female_") ? "female" : "male",
})),
isConfigured: ({ providerConfig }) => {
const cfg = readProviderConfig(providerConfig);
return Boolean(
resolveSeedSpeechApiKey(cfg.apiKey) ||
((cfg.appId || process.env.VOLCENGINE_TTS_APPID) &&
(cfg.token || process.env.VOLCENGINE_TTS_TOKEN)),
);
},
synthesize: async (req) => {
const cfg = readProviderConfig(req.providerConfig);
const overrides = readVolcengineOverrides(req.providerOverrides);
const apiKey = resolveSeedSpeechApiKey(cfg.apiKey);
const appId = cfg.appId || process.env.VOLCENGINE_TTS_APPID;
const token = cfg.token || process.env.VOLCENGINE_TTS_TOKEN;
if (!apiKey && (!appId || !token)) {
throw new Error(
"Volcengine TTS credentials missing. Set VOLCENGINE_TTS_API_KEY, " +
"BYTEPLUS_SEED_SPEECH_API_KEY, or legacy VOLCENGINE_TTS_APPID and VOLCENGINE_TTS_TOKEN.",
);
}
const isVoiceNote = req.target === "voice-note";
const encoding: VolcengineTtsEncoding = isVoiceNote ? "ogg_opus" : "mp3";
const audioBuffer = await volcengineTTS({
text: req.text,
apiKey,
appId,
token,
voice: overrides.voice ?? cfg.voice,
cluster: cfg.cluster,
resourceId: cfg.resourceId,
appKey: cfg.appKey,
baseUrl: cfg.baseUrl,
speedRatio: overrides.speedRatio ?? cfg.speedRatio,
emotion: overrides.emotion ?? cfg.emotion,
encoding,
timeoutMs: req.timeoutMs,
});
return {
audioBuffer,
outputFormat: encoding === "ogg_opus" ? "opus" : "mp3",
fileExtension: encoding === "ogg_opus" ? ".opus" : ".mp3",
voiceCompatible: isVoiceNote,
};
},
};
}