Files
openclaw/extensions/azure-speech/speech-provider.ts
Peter Steinberger fa03d9b913 refactor: consolidate coercion helpers (#121366)
* refactor: consolidate coercion helpers

* fix: remove duplicate coercion imports

* fix: preserve serialized coercion guard

* chore: ratchet coercion helper carve-outs

* fix(test): keep gauntlet subprocess startup lean

* fix: preserve imported session timestamp semantics

* fix: preserve catalog timestamp string semantics

* chore: align plugin SDK surface ratchet

* fix: preserve trajectory and SDK string contracts

* fix(test): preserve QA record assertion semantics

* fix: complete standalone record guard rename

* refactor(cron): use canonical string coercion

* fix(acpx): preserve Pi timestamp parsing

* test(channels): adapt custody test harnesses

* test(telegram): classify media harness as test support

* test(acpx): split timestamp contract coverage

* test(channels): support generated custody contracts

* chore: ban the full coercion helper name set

Extends the declaration guard to all eleven consolidated helper names and
renames the cron schedule-identity readNumber wrapper to readScheduleInteger
so the banned generic name cannot regrow.

* fix(scripts): repair release-validation guard drift and lint cause

Restores the renamed isJsonRecord guard in assertTrustedWorkflowHarness after
main added isRecord call sites in parallel, and attaches the caught YAML error
as the thrown error cause (preserve-caught-error was red on main).

* fix: preserve Claude timestamp string semantics

* fix: preserve persisted timestamp string semantics

* fix: preserve date-first timestamp contracts

* fix(openai): harden delegation failure formatting

* chore: close coercion helper guard gaps

* test(openai): model non-error delegation rejection

* chore: refresh plugin SDK API contract

* fix(tasks): use canonical string field reader

* fix(ai): use canonical provider error field coercion

* fix(browser): migrate native bootstrap coercion

* docs(plugin-sdk): clarify text record export compatibility

* fix(gateway): normalize approval execution identity

* test(outbound): isolate message action poll harness
2026-08-11 00:02:18 -07:00

324 lines
11 KiB
TypeScript

/**
* Azure Speech provider descriptor. It reads config/env defaults, parses speech
* directives, lists voices, and calls the Azure TTS runtime helper.
*/
import { resolveGeneratedMediaMaxBytes } from "openclaw/plugin-sdk/media-generation-runtime";
import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-input";
import type {
SpeechDirectiveTokenParseContext,
SpeechProviderConfig,
SpeechProviderOverrides,
SpeechProviderPlugin,
} from "openclaw/plugin-sdk/speech-core";
import {
asFiniteNumber,
resolveSpeechProviderApiKey,
trimToUndefined,
} from "openclaw/plugin-sdk/speech-core";
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
import {
azureSpeechTTS,
DEFAULT_AZURE_SPEECH_AUDIO_FORMAT,
DEFAULT_AZURE_SPEECH_LANG,
DEFAULT_AZURE_SPEECH_TELEPHONY_FORMAT,
DEFAULT_AZURE_SPEECH_VOICE,
DEFAULT_AZURE_SPEECH_VOICE_NOTE_FORMAT,
inferAzureSpeechFileExtension,
isAzureSpeechVoiceCompatible,
listAzureSpeechVoices,
normalizeAzureSpeechBaseUrl,
} from "./tts.js";
type AzureSpeechProviderConfig = {
apiKey?: string;
region?: string;
endpoint?: string;
baseUrl?: string;
voice: string;
lang: string;
outputFormat: string;
voiceNoteOutputFormat: string;
timeoutMs?: number;
};
type AzureSpeechProviderOverrides = {
voice?: string;
lang?: string;
outputFormat?: string;
};
function readAzureSpeechEnvApiKey(): string | undefined {
return (
trimToUndefined(process.env.AZURE_SPEECH_KEY) ??
trimToUndefined(process.env.AZURE_SPEECH_API_KEY) ??
trimToUndefined(process.env.SPEECH_KEY)
);
}
function readAzureSpeechEnvRegion(): string | undefined {
return (
trimToUndefined(process.env.AZURE_SPEECH_REGION) ?? trimToUndefined(process.env.SPEECH_REGION)
);
}
function readAzureSpeechEnvEndpoint(): string | undefined {
return trimToUndefined(process.env.AZURE_SPEECH_ENDPOINT);
}
function resolveAzureSpeechConfigRecord(
rawConfig: Record<string, unknown>,
): Record<string, unknown> | undefined {
const providers = asOptionalRecord(rawConfig.providers);
return (
asOptionalRecord(providers?.["azure-speech"]) ??
asOptionalRecord(providers?.azure) ??
asOptionalRecord(rawConfig["azure-speech"]) ??
asOptionalRecord(rawConfig.azure)
);
}
function normalizeAzureSpeechProviderConfig(
rawConfig: Record<string, unknown>,
): AzureSpeechProviderConfig {
const raw = resolveAzureSpeechConfigRecord(rawConfig);
const region = trimToUndefined(raw?.region) ?? readAzureSpeechEnvRegion();
const endpoint = trimToUndefined(raw?.endpoint) ?? readAzureSpeechEnvEndpoint();
const baseUrl = normalizeAzureSpeechBaseUrl({
baseUrl: trimToUndefined(raw?.baseUrl),
endpoint,
region,
});
return {
apiKey: normalizeResolvedSecretInputString({
value: raw?.apiKey,
path: "tts.providers.azure-speech.apiKey",
}),
region,
endpoint,
baseUrl,
voice: trimToUndefined(raw?.voice ?? raw?.voiceId) ?? DEFAULT_AZURE_SPEECH_VOICE,
lang: trimToUndefined(raw?.lang ?? raw?.languageCode) ?? DEFAULT_AZURE_SPEECH_LANG,
outputFormat: trimToUndefined(raw?.outputFormat) ?? DEFAULT_AZURE_SPEECH_AUDIO_FORMAT,
voiceNoteOutputFormat:
trimToUndefined(raw?.voiceNoteOutputFormat) ?? DEFAULT_AZURE_SPEECH_VOICE_NOTE_FORMAT,
timeoutMs: asFiniteNumber(raw?.timeoutMs),
};
}
function readAzureSpeechProviderConfig(config: SpeechProviderConfig): AzureSpeechProviderConfig {
const defaults = normalizeAzureSpeechProviderConfig({});
const region = trimToUndefined(config.region) ?? defaults.region;
const endpoint = trimToUndefined(config.endpoint) ?? defaults.endpoint;
const baseUrl = normalizeAzureSpeechBaseUrl({
baseUrl: trimToUndefined(config.baseUrl) ?? defaults.baseUrl,
endpoint,
region,
});
return {
apiKey: trimToUndefined(config.apiKey) ?? defaults.apiKey,
region,
endpoint,
baseUrl,
voice: trimToUndefined(config.voice ?? config.voiceId) ?? defaults.voice,
lang: trimToUndefined(config.lang ?? config.languageCode) ?? defaults.lang,
outputFormat: trimToUndefined(config.outputFormat) ?? defaults.outputFormat,
voiceNoteOutputFormat:
trimToUndefined(config.voiceNoteOutputFormat) ?? defaults.voiceNoteOutputFormat,
timeoutMs: asFiniteNumber(config.timeoutMs) ?? defaults.timeoutMs,
};
}
function readAzureSpeechOverrides(
overrides: SpeechProviderOverrides | undefined,
): AzureSpeechProviderOverrides {
if (!overrides) {
return {};
}
return {
voice: trimToUndefined(overrides.voice ?? overrides.voiceId),
lang: trimToUndefined(overrides.lang ?? overrides.languageCode),
outputFormat: trimToUndefined(overrides.outputFormat),
};
}
function parseDirectiveToken(ctx: SpeechDirectiveTokenParseContext): {
handled: boolean;
overrides?: SpeechProviderOverrides;
} {
switch (ctx.key) {
case "voice":
case "voiceid":
case "voice_id":
case "azure_voice":
case "azurevoice":
case "azure_speech_voice":
if (!ctx.policy.allowVoice) {
return { handled: true };
}
return { handled: true, overrides: { ...ctx.currentOverrides, voice: ctx.value } };
case "lang":
case "language":
case "language_code":
case "languagecode":
case "azure_lang":
case "azure_language":
if (!ctx.policy.allowVoiceSettings) {
return { handled: true };
}
return { handled: true, overrides: { ...ctx.currentOverrides, lang: ctx.value } };
case "output_format":
case "outputformat":
case "azure_format":
case "azure_output_format":
if (!ctx.policy.allowVoiceSettings) {
return { handled: true };
}
return { handled: true, overrides: { ...ctx.currentOverrides, outputFormat: ctx.value } };
default:
return { handled: false };
}
}
function resolveApiKey(...candidates: Array<string | undefined>): string | undefined {
return resolveSpeechProviderApiKey(...candidates, readAzureSpeechEnvApiKey());
}
function resolveTimeoutMs(config: AzureSpeechProviderConfig, timeoutMs: number): number {
return config.timeoutMs ?? timeoutMs;
}
/** Build the Azure Speech provider descriptor for the speech-core runtime. */
export function buildAzureSpeechProvider(): SpeechProviderPlugin {
return {
id: "azure-speech",
label: "Azure Speech",
aliases: ["azure"],
autoSelectOrder: 30,
resolveConfig: ({ rawConfig }) => normalizeAzureSpeechProviderConfig(rawConfig),
parseDirectiveToken,
resolveTalkConfig: ({ baseTtsConfig, talkProviderConfig }) => {
const base = normalizeAzureSpeechProviderConfig(baseTtsConfig);
const apiKey =
talkProviderConfig.apiKey === undefined
? undefined
: normalizeResolvedSecretInputString({
value: talkProviderConfig.apiKey,
path: "talk.providers.azure-speech.apiKey",
});
const region = trimToUndefined(talkProviderConfig.region);
const endpoint = trimToUndefined(talkProviderConfig.endpoint ?? talkProviderConfig.baseUrl);
const baseUrl = normalizeAzureSpeechBaseUrl({
baseUrl: trimToUndefined(talkProviderConfig.baseUrl),
endpoint,
region: region ?? base.region,
});
return {
...base,
...(apiKey === undefined ? {} : { apiKey }),
...(region === undefined ? {} : { region }),
...(endpoint === undefined ? {} : { endpoint }),
...(baseUrl === undefined ? {} : { baseUrl }),
...(trimToUndefined(talkProviderConfig.voiceId) == null
? {}
: { voice: trimToUndefined(talkProviderConfig.voiceId) }),
...(trimToUndefined(talkProviderConfig.languageCode) == null
? {}
: { lang: trimToUndefined(talkProviderConfig.languageCode) }),
...(trimToUndefined(talkProviderConfig.outputFormat) == null
? {}
: { outputFormat: trimToUndefined(talkProviderConfig.outputFormat) }),
};
},
resolveTalkOverrides: ({ params }) => ({
...(trimToUndefined(params.voiceId) == null
? {}
: { voice: trimToUndefined(params.voiceId) }),
...(trimToUndefined(params.languageCode) == null
? {}
: { lang: trimToUndefined(params.languageCode) }),
...(trimToUndefined(params.outputFormat) == null
? {}
: { outputFormat: trimToUndefined(params.outputFormat) }),
}),
listVoices: async (req) => {
const config = req.providerConfig
? readAzureSpeechProviderConfig(req.providerConfig)
: undefined;
const requestValue = req.apiKey;
const configValue = config?.apiKey;
const apiKey = resolveApiKey(requestValue, configValue);
if (!apiKey) {
throw new Error("Azure Speech API key missing");
}
return listAzureSpeechVoices({
apiKey,
baseUrl: req.baseUrl ?? config?.baseUrl,
endpoint: config?.endpoint,
region: config?.region ?? readAzureSpeechEnvRegion(),
timeoutMs: config?.timeoutMs ?? req.timeoutMs,
});
},
isConfigured: ({ providerConfig }) => {
const config = readAzureSpeechProviderConfig(providerConfig);
return Boolean(
resolveApiKey(config.apiKey) && (config.baseUrl || config.region || config.endpoint),
);
},
synthesize: async (req) => {
const config = readAzureSpeechProviderConfig(req.providerConfig);
const overrides = readAzureSpeechOverrides(req.providerOverrides);
const apiKey = resolveApiKey(config.apiKey);
if (!apiKey) {
throw new Error("Azure Speech API key missing");
}
const outputFormat =
overrides.outputFormat ??
(req.target === "voice-note" ? config.voiceNoteOutputFormat : config.outputFormat);
const audioBuffer = await azureSpeechTTS({
text: req.text,
apiKey,
baseUrl: config.baseUrl,
endpoint: config.endpoint,
region: config.region,
voice: overrides.voice ?? config.voice,
lang: overrides.lang ?? config.lang,
outputFormat,
timeoutMs: resolveTimeoutMs(config, req.timeoutMs),
maxBytes: resolveGeneratedMediaMaxBytes(req.cfg, "audio"),
});
return {
audioBuffer,
outputFormat,
fileExtension: inferAzureSpeechFileExtension(outputFormat),
voiceCompatible: isAzureSpeechVoiceCompatible(outputFormat),
};
},
synthesizeTelephony: async (req) => {
const config = readAzureSpeechProviderConfig(req.providerConfig);
const overrides = readAzureSpeechOverrides(req.providerOverrides);
const apiKey = resolveApiKey(config.apiKey);
if (!apiKey) {
throw new Error("Azure Speech API key missing");
}
const sampleRate = 8_000;
const audioBuffer = await azureSpeechTTS({
text: req.text,
apiKey,
baseUrl: config.baseUrl,
endpoint: config.endpoint,
region: config.region,
voice: overrides.voice ?? config.voice,
lang: overrides.lang ?? config.lang,
outputFormat: DEFAULT_AZURE_SPEECH_TELEPHONY_FORMAT,
timeoutMs: resolveTimeoutMs(config, req.timeoutMs),
maxBytes: resolveGeneratedMediaMaxBytes(req.cfg, "audio"),
});
return {
audioBuffer,
outputFormat: DEFAULT_AZURE_SPEECH_TELEPHONY_FORMAT,
sampleRate,
};
},
};
}