Skip to content

Commit e93bc7d

Browse files
committed
fix(cli): publish TTS output atomically
1 parent 9045484 commit e93bc7d

3 files changed

Lines changed: 81 additions & 4 deletions

File tree

‎docs/cli/infer.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ openclaw infer tts status --json
224224
Notes:
225225

226226
- `tts status` only supports `--gateway` (it reflects gateway-managed TTS state).
227+
- Local and loopback-Gateway `tts convert --output` copies stage beside the destination and replace it only after success; a failed copy leaves an existing file unchanged.
227228
- Use `tts convert --provider <id>` when selecting a provider without overriding its model.
228229
- Use `tts providers`, `tts voices`, `tts personas`, `tts set-provider`, and `tts set-persona` to inspect and configure TTS behavior.
229230

‎src/cli/capability-cli.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,6 +2904,74 @@ describe("capability cli", () => {
29042904
expectRuntimeErrorContains("--output is not supported for remote gateway TTS yet");
29052905
});
29062906

2907+
it.each(["local", "gateway"] as const)(
2908+
"preserves an existing %s TTS --output when the final copy fails",
2909+
async (transport) => {
2910+
const tempDir = tempDirs.make(`openclaw-tts-${transport}-copy-fail-`);
2911+
const sourcePath = path.join(tempDir, "source.mp3");
2912+
const outputDir = path.join(tempDir, "output");
2913+
const outputPath = path.join(outputDir, "speech.mp3");
2914+
await fs.mkdir(outputDir);
2915+
await fs.writeFile(sourcePath, Buffer.alloc(2_048, 0x41));
2916+
await fs.writeFile(outputPath, "existing-speech");
2917+
await fs.chmod(outputPath, 0o640);
2918+
2919+
if (transport === "gateway") {
2920+
mocks.callGateway.mockResolvedValueOnce({
2921+
audioPath: sourcePath,
2922+
provider: "openai",
2923+
outputFormat: "mp3",
2924+
voiceCompatible: false,
2925+
} as never);
2926+
} else {
2927+
mocks.textToSpeech.mockResolvedValueOnce({
2928+
success: true,
2929+
audioPath: sourcePath,
2930+
provider: "openai",
2931+
outputFormat: "mp3",
2932+
voiceCompatible: false,
2933+
attempts: [],
2934+
});
2935+
}
2936+
2937+
const copyFile = fs.copyFile.bind(fs);
2938+
const copyFileSpy = vi.spyOn(fs, "copyFile").mockImplementation(async (...args) => {
2939+
const [source, destination] = args;
2940+
if (typeof destination === "string" && path.dirname(destination) === outputDir) {
2941+
const bytes = await fs.readFile(source);
2942+
await fs.writeFile(destination, bytes.subarray(0, 17));
2943+
throw new Error("injected TTS copy failure");
2944+
}
2945+
await copyFile(...args);
2946+
});
2947+
2948+
try {
2949+
await expect(
2950+
runCapability(
2951+
"tts",
2952+
"convert",
2953+
`--${transport}`,
2954+
"--text",
2955+
"hello",
2956+
"--output",
2957+
outputPath,
2958+
"--json",
2959+
),
2960+
).rejects.toThrow("exit 1");
2961+
2962+
expectRuntimeErrorContains("injected TTS copy failure");
2963+
expect(mocks.runtime.writeJson).not.toHaveBeenCalled();
2964+
expect(await fs.readFile(outputPath, "utf8")).toBe("existing-speech");
2965+
if (process.platform !== "win32") {
2966+
expect((await fs.stat(outputPath)).mode & 0o777).toBe(0o640);
2967+
}
2968+
expect(await fs.readdir(outputDir)).toEqual(["speech.mp3"]);
2969+
} finally {
2970+
copyFileSpy.mockRestore();
2971+
}
2972+
},
2973+
);
2974+
29072975
it("uses only embedding providers for embedding creation", async () => {
29082976
await runCapability("embedding", "create", "--text", "hello", "--json");
29092977

‎src/cli/capability-cli/tts-runtime.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
textToSpeech,
2626
} from "../../tts/tts.js";
2727
import { getTtsCommandSecretTargetIds } from "../command-secret-targets.js";
28+
import { publishOutputFileAtomically } from "./media-output.js";
2829
import type { CapabilityEnvelope, CapabilityTransport } from "./metadata.js";
2930
import {
3031
pinRuntimeConfigSnapshot,
@@ -33,6 +34,15 @@ import {
3334
resolveSelectedProviderFromModelRef,
3435
} from "./shared.js";
3536

37+
async function copyTtsOutputAtomically(sourcePath: string, targetPath: string): Promise<void> {
38+
await publishOutputFileAtomically({
39+
filePath: targetPath,
40+
writeTemp: async (tempPath) => {
41+
await fs.copyFile(sourcePath, tempPath);
42+
},
43+
});
44+
}
45+
3646
export async function runTtsConvert(params: {
3747
text: string;
3848
channel?: string;
@@ -71,8 +81,7 @@ export async function runTtsConvert(params: {
7181
);
7282
}
7383
const target = path.resolve(params.output);
74-
await fs.mkdir(path.dirname(target), { recursive: true });
75-
await fs.copyFile(result.audioPath, target);
84+
await copyTtsOutputAtomically(result.audioPath, target);
7685
outputPath = target;
7786
}
7887
return {
@@ -134,8 +143,7 @@ export async function runTtsConvert(params: {
134143
let outputPath = result.audioPath;
135144
if (params.output) {
136145
const target = path.resolve(params.output);
137-
await fs.mkdir(path.dirname(target), { recursive: true });
138-
await fs.copyFile(result.audioPath, target);
146+
await copyTtsOutputAtomically(result.audioPath, target);
139147
outputPath = target;
140148
}
141149
return {

0 commit comments

Comments
 (0)