improve: add empty-text guard and retry for Edge TTS

Follow-up to PR #43385

Implements reviewer suggestions:

- Add pre-check for empty or whitespace-only text
- Retry once if Edge TTS produces a zero-byte file
- Include file size in error message for debugging
This commit is contained in:
Hiago Silva 2026-03-15 08:02:49 -03:00 committed by GitHub
parent 843e3c1efb
commit e179a3291b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -703,6 +703,7 @@ export async function edgeTTS(params: {
timeoutMs: number;
}): Promise<void> {
const { text, outputPath, config, timeoutMs } = params;
const tts = new EdgeTTS({
voice: config.voice,
lang: config.lang,
@ -714,11 +715,23 @@ export async function edgeTTS(params: {
volume: config.volume,
timeout: config.timeoutMs ?? timeoutMs,
});
if (!text || text.trim().length === 0) {
throw new Error("TTS text cannot be empty");
}
await tts.ttsPromise(text, outputPath);
const { size } = statSync(outputPath);
let { size } = statSync(outputPath);
if (size === 0) {
throw new Error("Edge TTS produced empty audio file");
await tts.ttsPromise(text, outputPath);
({ size } = statSync(outputPath));
if (size === 0) {
throw new Error(`Edge TTS produced empty audio file (size=${size})`);
}
}
}