From d1fda7b8f289c8533f0301c28c92164b51f796aa Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Mar 2026 00:54:47 +0000 Subject: [PATCH] refactor: share tts request setup --- src/tts/tts.ts | 72 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/src/tts/tts.ts b/src/tts/tts.ts index 5cd306f13a9..403efc10543 100644 --- a/src/tts/tts.ts +++ b/src/tts/tts.ts @@ -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 { - 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 { - 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[] = [];