refactor: share tts request setup

This commit is contained in:
Peter Steinberger 2026-03-14 00:54:47 +00:00
parent f7f5c24786
commit d1fda7b8f2
1 changed files with 48 additions and 24 deletions

View File

@ -559,6 +559,35 @@ function buildTtsFailureResult(errors: string[]): { success: false; error: strin
};
}
function resolveTtsRequestSetup(params: {
text: string;
cfg: OpenClawConfig;
prefsPath?: string;
providerOverride?: TtsProvider;
}):
| {
config: ResolvedTtsConfig;
providers: TtsProvider[];
}
| {
error: string;
} {
const config = resolveTtsConfig(params.cfg);
const prefsPath = params.prefsPath ?? resolveTtsPrefsPath(config);
if (params.text.length > config.maxTextLength) {
return {
error: `Text too long (${params.text.length} chars, max ${config.maxTextLength})`,
};
}
const userProvider = getTtsProvider(config, prefsPath);
const provider = params.providerOverride ?? userProvider;
return {
config,
providers: resolveTtsProviderOrder(provider),
};
}
export async function textToSpeech(params: {
text: string;
cfg: OpenClawConfig;
@ -566,22 +595,19 @@ export async function textToSpeech(params: {
channel?: string;
overrides?: TtsDirectiveOverrides;
}): Promise<TtsResult> {
const config = resolveTtsConfig(params.cfg);
const prefsPath = params.prefsPath ?? resolveTtsPrefsPath(config);
const channelId = resolveChannelId(params.channel);
const output = resolveOutputFormat(channelId);
if (params.text.length > config.maxTextLength) {
return {
success: false,
error: `Text too long (${params.text.length} chars, max ${config.maxTextLength})`,
};
const setup = resolveTtsRequestSetup({
text: params.text,
cfg: params.cfg,
prefsPath: params.prefsPath,
providerOverride: params.overrides?.provider,
});
if ("error" in setup) {
return { success: false, error: setup.error };
}
const userProvider = getTtsProvider(config, prefsPath);
const overrideProvider = params.overrides?.provider;
const provider = overrideProvider ?? userProvider;
const providers = resolveTtsProviderOrder(provider);
const { config, providers } = setup;
const channelId = resolveChannelId(params.channel);
const output = resolveOutputFormat(channelId);
const errors: string[] = [];
@ -734,18 +760,16 @@ export async function textToSpeechTelephony(params: {
cfg: OpenClawConfig;
prefsPath?: string;
}): Promise<TtsTelephonyResult> {
const config = resolveTtsConfig(params.cfg);
const prefsPath = params.prefsPath ?? resolveTtsPrefsPath(config);
if (params.text.length > config.maxTextLength) {
return {
success: false,
error: `Text too long (${params.text.length} chars, max ${config.maxTextLength})`,
};
const setup = resolveTtsRequestSetup({
text: params.text,
cfg: params.cfg,
prefsPath: params.prefsPath,
});
if ("error" in setup) {
return { success: false, error: setup.error };
}
const userProvider = getTtsProvider(config, prefsPath);
const providers = resolveTtsProviderOrder(userProvider);
const { config, providers } = setup;
const errors: string[] = [];