diff --git a/packages/commands/src/commands/speech/synthesize.ts b/packages/commands/src/commands/speech/synthesize.ts index 7b507f02..04740018 100644 --- a/packages/commands/src/commands/speech/synthesize.ts +++ b/packages/commands/src/commands/speech/synthesize.ts @@ -616,15 +616,64 @@ async function handleNonStreamMode( settings: Settings, body: DashScopeTTSRequest, flags: SynthesizeFlags, +): Promise { + const controller = new AbortController(); + const timeout = setTimeout( + () => + controller.abort( + new BailianError( + `Audio synthesis timed out after ${settings.timeout}s. / 音频合成在 ${settings.timeout} 秒后超时。`, + ExitCode.TIMEOUT, + ), + ), + settings.timeout * 1000, + ); + const pending: Promise[] = []; + const track = (operation: Promise): Promise => { + pending.push(operation); + return operation; + }; + try { + await synthesizeAndDownload( + client, + commandOutput, + settings, + body, + flags, + controller.signal, + track, + ); + } catch (error) { + // A network error cancels peers too, but keeps its original diagnostics. + const reason = controller.signal.aborted ? controller.signal.reason : error; + controller.abort(reason); + await Promise.allSettled(pending); + throw reason; + } finally { + clearTimeout(timeout); + } +} + +async function synthesizeAndDownload( + client: Client, + commandOutput: CommandOutput, + settings: Settings, + body: DashScopeTTSRequest, + flags: SynthesizeFlags, + signal: AbortSignal, + track: (operation: Promise) => Promise, ): Promise { const concurrent = getConcurrency(flags); const results = await runConcurrent(concurrent, commandOutput, () => - client.requestJson({ - path: speechSynthesizePath(), - method: "POST", - body, - }), + track( + client.requestJson({ + path: speechSynthesizePath(), + method: "POST", + body, + signal, + }), + ), ); const audioUrls = results.map((result) => result.output?.audio?.url).filter(Boolean) as string[]; @@ -650,9 +699,11 @@ async function handleNonStreamMode( return { url: audioUrl, destPath: destPath! }; }); - const saved = await downloadParallel(items, downloadFile, { - quiet: settings.quiet || settings.output === "json", - }); + const saved = await downloadParallel( + items, + (url, destPath, options) => track(downloadFile(url, destPath, { ...options, signal })), + { quiet: settings.quiet || settings.output === "json" }, + ); if (saved.length === 1) { const expiresAt = results[0]!.output?.audio?.expires_at; diff --git a/packages/commands/tests/output/speech-synthesize-output.test.ts b/packages/commands/tests/output/speech-synthesize-output.test.ts index 7d3341dd..d5f43915 100644 --- a/packages/commands/tests/output/speech-synthesize-output.test.ts +++ b/packages/commands/tests/output/speech-synthesize-output.test.ts @@ -1,12 +1,21 @@ -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { + chmodSync, + existsSync, + mkdtempSync, + readFileSync, + readdirSync, + rmSync, + statSync, + writeFileSync, +} from "node:fs"; import { spawn } from "child_process"; import { fileURLToPath } from "node:url"; -import { createServer, type Server } from "node:http"; +import { createServer, type Server, type ServerResponse } from "node:http"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, expect, test, vi } from "vite-plus/test"; import { parse as parseYaml } from "yaml"; -import { buildSettings, Client, type LocalizedText } from "bailian-cli-core"; +import { buildSettings, Client, ExitCode, type LocalizedText } from "bailian-cli-core"; import { createOutputReporter } from "bailian-cli-runtime"; import synthesize from "../../src/commands/speech/synthesize.ts"; @@ -15,23 +24,66 @@ let server: Server; let origin: string; let requests: Array<{ path: string; body: unknown; stream: boolean }>; let downloads: number; -let failure: "none" | "submit" | "empty" | "download"; +let failure: + | "none" + | "submit" + | "empty" + | "download" + | "stalled-headers" + | "stalled-body" + | "combined-budget" + | "concurrent-peer"; +let failPeer: ServerResponse | undefined; +let stalledPeer: ServerResponse | undefined; +let stalledPeerClosed: boolean; const audio = Buffer.from([0, 255, 13, 10, 1, 2, 3]); beforeEach(async () => { directory = mkdtempSync(join(tmpdir(), "bl-synthesize-output-")); requests = []; downloads = 0; failure = "none"; + failPeer = stalledPeer = undefined; + stalledPeerClosed = false; server = createServer(async (request, response) => { if (request.url?.startsWith("/audio/")) { downloads++; + if (failure === "concurrent-peer") { + if (request.url === "/audio/1.mp3") { + stalledPeer = response; + response.on("close", () => { + stalledPeerClosed = true; + }); + const watchdog = setTimeout(() => response.destroy(), 1_500); + response.on("close", () => clearTimeout(watchdog)); + response.writeHead(200, { "content-length": String(audio.length) }); + response.write(audio.subarray(0, 3)); + } else { + failPeer = response; + } + if (stalledPeer && failPeer) failPeer.writeHead(503).end(); + return; + } if (failure === "download") { response.writeHead(503).end(); return; } + if (failure === "stalled-headers" || failure === "stalled-body") { + const watchdog = setTimeout(() => response.destroy(), 1_500); + response.on("close", () => clearTimeout(watchdog)); + if (failure === "stalled-body") { + response.writeHead(200, { "content-length": String(audio.length) }); + response.write(audio.subarray(0, 3)); + } + return; + } response.writeHead(200, { "content-length": String(audio.length) }); response.write(audio.subarray(0, 3)); - response.end(audio.subarray(3)); + if (failure === "combined-budget") { + const finish = setTimeout(() => response.end(audio.subarray(3)), 600); + response.on("close", () => clearTimeout(finish)); + } else { + response.end(audio.subarray(3)); + } return; } let raw = ""; @@ -58,6 +110,9 @@ beforeEach(async () => { `data: ${JSON.stringify({ output: { audio: { data: audio.subarray(3).toString("base64"), url: `${origin}/audio/full.mp3` }, finish_reason: "stop" } })}\n\n`, ); } else { + if (failure === "combined-budget") { + await new Promise((resolve) => setTimeout(resolve, 600)); + } response.writeHead(200, { "content-type": "application/json" }).end( JSON.stringify({ output: { @@ -91,6 +146,7 @@ async function invoke( tty?: boolean; dryRun?: boolean; chinese?: boolean; + timeout?: number; }, ) { const chunks: Buffer[] = []; @@ -116,7 +172,7 @@ async function invoke( dryRun: display.dryRun, }, env: {}, - file: { output_dir: directory, timeout: 2 }, + file: { output_dir: directory, timeout: display.timeout ?? 2 }, configPath: join(directory, "config.json"), }); const localize = (text: LocalizedText) => @@ -329,6 +385,68 @@ test.each(["submit", "empty", "download"] as const)( }); }, ); +test.each(["stalled-headers", "stalled-body", "combined-budget"] as const)( + "nonstream synthesis times out and leaves no incomplete output for %s", + async (mode) => { + failure = mode; + const destination = join(directory, "timed-out.wav"); + const result = await invoke( + { ...flags, model: "cosyvoice-v3-flash", format: "wav", out: destination }, + { format: "json", quiet: true, timeout: mode === "combined-budget" ? 1 : 0.3 }, + ); + expect(downloads).toBe(1); + expect(result.exitCode, result.stderr).toBe(ExitCode.TIMEOUT); + expect(result.stdout).toBe(""); + expect(JSON.parse(result.stderr)).toMatchObject({ error: { code: ExitCode.TIMEOUT } }); + expect(existsSync(destination)).toBe(false); + }, +); +test("a timed-out retry preserves the existing WAV", async () => { + failure = "stalled-body"; + const destination = join(directory, "existing.wav"); + writeFileSync(destination, audio); + const result = await invoke( + { ...flags, format: "wav", out: destination }, + { format: "json", quiet: true, timeout: 0.3 }, + ); + expect(result.exitCode).toBe(ExitCode.TIMEOUT); + expect(readFileSync(destination)).toEqual(audio); + expect(readdirSync(directory).sort()).toEqual(["existing.wav", "speech"]); + expect(readdirSync(join(directory, "speech"))).toEqual([]); +}); +test.skipIf(process.platform === "win32").each([0o600, 0o664])( + "a successful overwrite preserves the existing WAV permissions (%i)", + async (mode) => { + const destination = join(directory, "existing.wav"); + writeFileSync(destination, "previous recording"); + chmodSync(destination, mode); + + const result = await invoke( + { ...flags, format: "wav", out: destination }, + { format: "json", quiet: true }, + ); + + expect(result.exitCode, result.stderr).toBe(0); + expect(readFileSync(destination)).toEqual(audio); + expect(statSync(destination).mode & 0o777).toBe(mode); + }, +); +test("a failed download cancels its peer and keeps the original error", async () => { + failure = "concurrent-peer"; + const result = await invoke( + { ...flags, concurrent: 2 }, + { format: "json", quiet: true, timeout: 3 }, + ); + expect(downloads).toBe(2); + expect(result.stdout).toBe(""); + expect(result.exitCode).toBe(ExitCode.GENERAL); + expect(JSON.parse(result.stderr)).toMatchObject({ + error: { message: "Download failed: HTTP 503" }, + }); + await vi.waitFor(() => expect(stalledPeerClosed).toBe(true), { timeout: 600 }); + const audioDirectory = join(directory, "speech"); + if (existsSync(audioDirectory)) expect(readdirSync(audioDirectory)).toEqual([]); +}); test("text-file parsing and explicit audio parameters remain unchanged", async () => { const textFile = join(directory, "text.txt"); writeFileSync(textFile, " File text "); @@ -399,7 +517,15 @@ test("model and voice-list process prose supports Chinese", async () => { expect(list.stdout).toContain("未提供内置音色列表"); }); -const cliModes = ["raw", "raw-quiet", "stream-file", "conflict", "download", "voices"] as const; +const cliModes = [ + "raw", + "raw-quiet", + "stream-file", + "conflict", + "download", + "voices", + "timeout", +] as const; test.each(cliModes)("real CLI preserves binary/JSON boundaries for %s", async (mode) => { writeFileSync(join(directory, "config.json"), JSON.stringify({ language: "en-US" })); const destination = join(directory, mode === "stream-file" ? "cli.pcm" : "cli.mp3"); @@ -407,7 +533,12 @@ test.each(cliModes)("real CLI preserves binary/JSON boundaries for %s", async (m if (mode === "voices") args.push("--list-voices"); else args.push("--text", "Hello", "--voice", flags.voice); if (["raw", "raw-quiet", "stream-file", "conflict"].includes(mode)) args.push("--stream"); - if (mode === "stream-file" || mode === "download") args.push("--out", destination); + if (mode === "stream-file" || mode === "download" || mode === "timeout") + args.push("--out", destination); + if (mode === "timeout") { + failure = "stalled-body"; + args.push("--timeout", "0.3"); + } const raw = mode === "raw" || mode === "raw-quiet"; args.push("--output", raw ? "text" : "json"); if (mode !== "raw") args.push("--quiet"); @@ -469,7 +600,15 @@ test.each(cliModes)("real CLI preserves binary/JSON boundaries for %s", async (m .split("\n") .map((line) => JSON.parse(line)) : []; - if (mode === "conflict") { + if (mode === "timeout") { + expect(exitCode, stderr).toBe(ExitCode.TIMEOUT); + expect(bytes.length).toBe(0); + expect(records.filter((record) => record.error)).toMatchObject([ + { error: { code: ExitCode.TIMEOUT } }, + ]); + expect(existsSync(destination)).toBe(false); + expect(downloads).toBe(1); + } else if (mode === "conflict") { expect(exitCode).toBe(2); expect(bytes.length).toBe(0); expect(records).toHaveLength(1); diff --git a/packages/runtime/src/utils/download.ts b/packages/runtime/src/utils/download.ts index 44ed978e..c9de584e 100644 --- a/packages/runtime/src/utils/download.ts +++ b/packages/runtime/src/utils/download.ts @@ -1,6 +1,9 @@ import { httpTransport } from "bailian-cli-core"; import { createWriteStream, mkdirSync, unlinkSync } from "fs"; -import { dirname } from "path"; +import { dirname, join } from "path"; +import { chmod, mkdtemp, rename, rm, stat } from "node:fs/promises"; +import { Readable, Transform } from "node:stream"; +import { pipeline } from "node:stream/promises"; import { BailianError, ExitCode } from "bailian-cli-core"; import { createProgressBar } from "../output/progress.ts"; import type { ReadableStreamReadResult } from "stream/web"; @@ -8,18 +11,25 @@ import type { ReadableStreamReadResult } from "stream/web"; export async function downloadFile( url: string, destPath: string, - opts?: { quiet?: boolean }, + opts?: { quiet?: boolean; signal?: AbortSignal }, ): Promise<{ size: number }> { - const res = await httpTransport.fetch(url); + const res = opts?.signal + ? await httpTransport.fetch(url, { signal: opts.signal }) + : await httpTransport.fetch(url); if (!res.ok) { throw new BailianError(`Download failed: HTTP ${res.status}`, ExitCode.GENERAL); } const contentLength = Number(res.headers.get("content-length") || 0); - const reader = res.body?.getReader(); - if (!reader) throw new BailianError("No response body", ExitCode.GENERAL); + if (!res.body) throw new BailianError("No response body", ExitCode.GENERAL); + if (opts?.signal) { + // Keep an existing output intact until the entire timed download succeeds. + return await downloadWithDeadline(res.body, destPath, contentLength, opts.signal, opts.quiet); + } + + const reader = res.body.getReader(); mkdirSync(dirname(destPath), { recursive: true }); const writer = createWriteStream(destPath); const progress = @@ -69,6 +79,45 @@ export async function downloadFile( return { size: received }; } +async function downloadWithDeadline( + body: NonNullable, + destPath: string, + contentLength: number, + signal: AbortSignal, + quiet: boolean | undefined, +): Promise<{ size: number }> { + const parentDirectory = dirname(destPath); + mkdirSync(parentDirectory, { recursive: true }); + const directory = await mkdtemp(join(parentDirectory, ".bailian-audio-")); + const partialPath = join(directory, "partial"); + const progress = + contentLength > 0 && !quiet ? createProgressBar(contentLength, "Downloading") : null; + let received = 0; + try { + const meter = new Transform({ + transform(chunk: Buffer, _encoding, callback) { + received += chunk.byteLength; + progress?.update(received); + callback(null, chunk); + }, + }); + await pipeline(Readable.fromWeb(body), meter, createWriteStream(partialPath), { signal }); + signal.throwIfAborted(); + // Replacing the inode would otherwise reset an existing file's permissions to the umask. + const existing = await stat(destPath).catch((error: NodeJS.ErrnoException) => { + if (error.code !== "ENOENT") throw error; + return undefined; + }); + if (existing?.isFile()) await chmod(partialPath, existing.mode & 0o777); + signal.throwIfAborted(); + await rename(partialPath, destPath); + return { size: received }; + } finally { + progress?.finish(); + await rm(directory, { recursive: true, force: true }); + } +} + export function formatBytes(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;