Skip to content

Commit 6509a09

Browse files
authored
fix(media): discover whisper.cpp models instead of a test fixture path (#124699)
The auto local-STT default hardcoded /opt/homebrew/share/whisper-cpp/for-tests-ggml-tiny.bin — a CI provisioning artifact — as the production whisper model. Real users with brew-installed whisper-cpp and downloaded models got ready: false ('model file not found'), silently skipping local transcription; test- provisioned machines transcribed real audio with the worst-quality tiny fixture with nothing recording that a test artifact was chosen. Discover ggml-*.bin models across standard install dirs, preferring non-tiny models; WHISPER_CPP_MODEL stays the explicit override. Not found now genuinely means no model is installed.
1 parent 2d6f8a7 commit 6509a09

2 files changed

Lines changed: 85 additions & 4 deletions

File tree

‎src/media-understanding/local-audio.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,47 @@ describe("local audio selection", () => {
4343
});
4444
});
4545

46+
it("discovers installed whisper models and prefers non-tiny over the tiny fixture", async () => {
47+
const tempDir = tempDirs.make("openclaw-local-audio-");
48+
const commandPath = path.join(tempDir, "whisper-cli");
49+
await fs.writeFile(commandPath, "#!/bin/sh\n");
50+
await fs.chmod(commandPath, 0o755);
51+
52+
const selection = await inspectLocalAudioSelection({
53+
env: { PATH: tempDir },
54+
platform: process.platform,
55+
arch: process.arch,
56+
inspectLinkedLibraries: async () => null,
57+
listDirectory: async (dirPath) =>
58+
dirPath === "/opt/homebrew/share/whisper-cpp"
59+
? ["for-tests-ggml-tiny.bin", "ggml-tiny.bin", "ggml-base.en.bin", "notes.txt"]
60+
: [],
61+
});
62+
63+
const whisper = selection.candidates.find((candidate) => candidate.id === "whisper-cli");
64+
expect(whisper?.ready).toBe(true);
65+
expect(whisper?.entry?.args).toContain("/opt/homebrew/share/whisper-cpp/ggml-base.en.bin");
66+
});
67+
68+
it("reports whisper as not ready when no ggml model is installed", async () => {
69+
const tempDir = tempDirs.make("openclaw-local-audio-");
70+
const commandPath = path.join(tempDir, "whisper-cli");
71+
await fs.writeFile(commandPath, "#!/bin/sh\n");
72+
await fs.chmod(commandPath, 0o755);
73+
74+
const selection = await inspectLocalAudioSelection({
75+
env: { PATH: tempDir },
76+
platform: process.platform,
77+
arch: process.arch,
78+
inspectLinkedLibraries: async () => null,
79+
listDirectory: async () => [],
80+
});
81+
82+
const whisper = selection.candidates.find((candidate) => candidate.id === "whisper-cli");
83+
expect(whisper?.ready).toBe(false);
84+
expect(whisper?.reason).toBe("model file not found");
85+
});
86+
4687
it("does not resolve auto-detected commands from empty PATH entries", async () => {
4788
const tempDir = tempDirs.make("openclaw-local-audio-");
4889
const modelPath = path.join(tempDir, "whisper.bin");

‎src/media-understanding/local-audio.ts‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,46 @@ type InspectionOptions = {
3535
checkExecutable?: (filePath: string, platform: NodeJS.Platform) => Promise<boolean>;
3636
resolveRealpath?: (filePath: string) => Promise<string>;
3737
inspectLinkedLibraries?: (filePath: string, platform: NodeJS.Platform) => Promise<string | null>;
38+
listDirectory?: (dirPath: string) => Promise<string[]>;
3839
};
3940

41+
const WHISPER_CPP_MODEL_DIRS = [
42+
"/opt/homebrew/share/whisper-cpp",
43+
"/usr/local/share/whisper-cpp",
44+
"/usr/share/whisper-cpp",
45+
];
46+
47+
async function listDirectoryEntries(dirPath: string): Promise<string[]> {
48+
try {
49+
return await fs.readdir(dirPath);
50+
} catch {
51+
return [];
52+
}
53+
}
54+
55+
/**
56+
* Picks an installed ggml whisper.cpp model without hardcoding a filename.
57+
* Larger models transcribe better, so prefer non-tiny when several exist;
58+
* WHISPER_CPP_MODEL remains the explicit override.
59+
*/
60+
async function discoverWhisperCppModel(
61+
listDirectory: (dirPath: string) => Promise<string[]>,
62+
): Promise<string | null> {
63+
for (const dir of WHISPER_CPP_MODEL_DIRS) {
64+
const models = (await listDirectory(dir))
65+
.filter((name) => name.startsWith("ggml-") && name.endsWith(".bin"))
66+
.toSorted((a, b) => {
67+
const tinyA = a.includes("tiny") ? 1 : 0;
68+
const tinyB = b.includes("tiny") ? 1 : 0;
69+
return tinyA - tinyB || a.localeCompare(b);
70+
});
71+
if (models[0]) {
72+
return path.join(dir, models[0]);
73+
}
74+
}
75+
return null;
76+
}
77+
4078
const binaryCache = new Map<string, Promise<string | null>>();
4179
const libraryCache = new Map<string, Promise<string | null>>();
4280
const observedBackendCache = new Map<string, "cpu" | "cuda" | "metal">();
@@ -282,10 +320,11 @@ export async function inspectLocalAudioSelection(
282320
);
283321

284322
const envModel = env.WHISPER_CPP_MODEL?.trim();
285-
const defaultWhisperModel = "/opt/homebrew/share/whisper-cpp/for-tests-ggml-tiny.bin";
286323
const whisperModel =
287-
envModel && (await optionalPathExists(envModel)) ? envModel : defaultWhisperModel;
288-
const whisperReady = Boolean(whisperCommand) && (await optionalPathExists(whisperModel));
324+
envModel && (await optionalPathExists(envModel))
325+
? envModel
326+
: await discoverWhisperCppModel(options.listDirectory ?? listDirectoryEntries);
327+
const whisperReady = Boolean(whisperCommand) && Boolean(whisperModel);
289328
const whisperBackend = whisperCommand
290329
? await inspectWhisperBackend({
291330
command: whisperCommand,
@@ -320,7 +359,8 @@ export async function inspectLocalAudioSelection(
320359
];
321360
const whisperArgs = [
322361
"-m",
323-
whisperModel,
362+
// Args are only executed when whisperReady, which requires a model.
363+
whisperModel ?? "",
324364
"-otxt",
325365
"-of",
326366
"{{OutputBase}}",

0 commit comments

Comments
 (0)