@hxnnxs/opencode-voice 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/PUBLISHING.md +3 -3
- package/README.md +75 -35
- package/bin/opencode-voice.js +26 -5
- package/docs/README.es.md +5 -2
- package/docs/README.ru.md +5 -2
- package/docs/README.zh.md +10 -7
- package/index.js +265 -150
- package/lib/engine.js +44 -17
- package/lib/engines.js +130 -60
- package/lib/handy-model-catalog.js +820 -0
- package/lib/models.js +183 -24
- package/package.json +3 -2
package/lib/engine.js
CHANGED
|
@@ -466,8 +466,8 @@ function platformKey(options = {}) {
|
|
|
466
466
|
}
|
|
467
467
|
|
|
468
468
|
export function getBundledEngineDir(command, options = {}) {
|
|
469
|
-
|
|
470
|
-
return path.join(getEnginesDir(options, options),
|
|
469
|
+
const engineId = command === "whisper-cli" ? "whisper.cpp" : command === "opencode-voice-transcribe" ? "transcribe-cpp" : "";
|
|
470
|
+
return engineId ? path.join(getEnginesDir(options, options), engineId, platformKey(options)) : "";
|
|
471
471
|
}
|
|
472
472
|
|
|
473
473
|
function looksLikePath(value) {
|
|
@@ -482,6 +482,11 @@ function candidateCommands(command, options = {}) {
|
|
|
482
482
|
if (process.env.OPENCODE_VOICE_WHISPER_CLI) candidates.push(process.env.OPENCODE_VOICE_WHISPER_CLI);
|
|
483
483
|
}
|
|
484
484
|
|
|
485
|
+
if (command === "opencode-voice-transcribe") {
|
|
486
|
+
if (options.transcribeCpp) candidates.push(options.transcribeCpp);
|
|
487
|
+
if (process.env.OPENCODE_VOICE_TRANSCRIBE_CPP) candidates.push(process.env.OPENCODE_VOICE_TRANSCRIBE_CPP);
|
|
488
|
+
}
|
|
489
|
+
|
|
485
490
|
if (command === "ffmpeg") {
|
|
486
491
|
if (options.ffmpeg) candidates.push(options.ffmpeg);
|
|
487
492
|
if (process.env.OPENCODE_VOICE_FFMPEG) candidates.push(process.env.OPENCODE_VOICE_FFMPEG);
|
|
@@ -562,8 +567,8 @@ export function commandExists(command, options = {}) {
|
|
|
562
567
|
function childEnv(options = {}) {
|
|
563
568
|
const localLib = path.join(os.homedir(), ".local", "lib");
|
|
564
569
|
const localBin = path.join(os.homedir(), ".local", "bin");
|
|
565
|
-
const
|
|
566
|
-
const pathEntries = [
|
|
570
|
+
const bundledDirs = [getBundledEngineDir("whisper-cli", options), getBundledEngineDir("opencode-voice-transcribe", options)];
|
|
571
|
+
const pathEntries = [...bundledDirs, localBin, localLib, process.env.PATH].filter(Boolean);
|
|
567
572
|
return {
|
|
568
573
|
...process.env,
|
|
569
574
|
PATH: pathEntries.join(path.delimiter),
|
|
@@ -579,6 +584,15 @@ function normalizeWindowsAudioInput(device = "") {
|
|
|
579
584
|
return `audio=${value}`;
|
|
580
585
|
}
|
|
581
586
|
|
|
587
|
+
function formatCommand(command, args) {
|
|
588
|
+
const quote = (value) => {
|
|
589
|
+
const text = String(value);
|
|
590
|
+
if (/^[^\s"\\]+$/.test(text)) return text;
|
|
591
|
+
return `"${text.replace(/(\\*)"/g, "$1$1\\\"").replace(/(\\*)$/g, "$1$1")}"`;
|
|
592
|
+
};
|
|
593
|
+
return [command, ...args].map(quote).join(" ");
|
|
594
|
+
}
|
|
595
|
+
|
|
582
596
|
function parseWindowsMicrophones(stderr) {
|
|
583
597
|
const devices = new Set();
|
|
584
598
|
for (const line of stderr.split(/\r?\n/)) {
|
|
@@ -668,7 +682,13 @@ function buildRecorders(file, settings = {}) {
|
|
|
668
682
|
if (process.platform === "win32" && ffmpegCommand) {
|
|
669
683
|
const ffmpegCmd = ffmpegCommand;
|
|
670
684
|
if (ffmpegCmd) {
|
|
671
|
-
const
|
|
685
|
+
const configuredInput = mic && mic !== "default" ? normalizeWindowsAudioInput(mic) : "";
|
|
686
|
+
// DirectShow does not provide a reliable "audio=default" alias. Try each
|
|
687
|
+
// enumerated device before retaining it as a final fallback for custom setups.
|
|
688
|
+
const detectedInputs = listMicrophones(settings)
|
|
689
|
+
.filter((device) => device !== "default")
|
|
690
|
+
.map(normalizeWindowsAudioInput);
|
|
691
|
+
const inputs = [...new Set([configuredInput, ...detectedInputs, "audio=default"].filter(Boolean))];
|
|
672
692
|
for (const input of inputs) {
|
|
673
693
|
recorders.push({
|
|
674
694
|
label: `ffmpeg dshow (${input.replace(/^audio=/, "")})`,
|
|
@@ -734,14 +754,22 @@ function startRecorder(recorder, file) {
|
|
|
734
754
|
let stderr = "";
|
|
735
755
|
let exited = false;
|
|
736
756
|
let exitCode = null;
|
|
757
|
+
const commandLine = formatCommand(recorder.command, recorder.args);
|
|
758
|
+
const failure = (detail) => new Error(`${recorder.label}: ${detail}\nCommand: ${commandLine}${stderr.trim() ? `\nffmpeg stderr:\n${stderr.trim()}` : ""}`);
|
|
737
759
|
|
|
738
|
-
|
|
760
|
+
let proc;
|
|
761
|
+
try {
|
|
762
|
+
proc = spawn(recorder.command, recorder.args, { stdio: ["ignore", "ignore", "pipe"] });
|
|
763
|
+
} catch (error) {
|
|
764
|
+
reject(failure(error instanceof Error ? error.message : String(error)));
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
739
767
|
proc.stderr.on("data", (chunk) => {
|
|
740
768
|
stderr += chunk.toString();
|
|
741
769
|
});
|
|
742
770
|
proc.once("error", (error) => {
|
|
743
771
|
exited = true;
|
|
744
|
-
reject(
|
|
772
|
+
reject(failure(error.message));
|
|
745
773
|
});
|
|
746
774
|
proc.once("exit", (code) => {
|
|
747
775
|
exited = true;
|
|
@@ -750,7 +778,7 @@ function startRecorder(recorder, file) {
|
|
|
750
778
|
|
|
751
779
|
setTimeout(() => {
|
|
752
780
|
if (exited) {
|
|
753
|
-
reject(
|
|
781
|
+
reject(failure(lastLine(stderr) || `exited with code ${exitCode}`));
|
|
754
782
|
return;
|
|
755
783
|
}
|
|
756
784
|
|
|
@@ -758,7 +786,7 @@ function startRecorder(recorder, file) {
|
|
|
758
786
|
try {
|
|
759
787
|
proc.kill("SIGKILL");
|
|
760
788
|
} catch {}
|
|
761
|
-
reject(
|
|
789
|
+
reject(failure("did not create an audio file"));
|
|
762
790
|
return;
|
|
763
791
|
}
|
|
764
792
|
|
|
@@ -859,23 +887,22 @@ export class VoiceRuntime {
|
|
|
859
887
|
|
|
860
888
|
async transcribe(audioFile, model, settings = {}) {
|
|
861
889
|
const commandOptions = { ...this.options, downloadDir: settings.downloadDir };
|
|
862
|
-
const
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
}
|
|
890
|
+
const command = model.engine === "transcribe-cpp" ? "opencode-voice-transcribe" : "whisper-cli";
|
|
891
|
+
const binary = resolveCommand(command, commandOptions);
|
|
892
|
+
if (!binary) throw new Error(`${command} not found. Install the managed ${model.engine} engine or set its OPENCODE_VOICE_* binary path.`);
|
|
866
893
|
|
|
867
894
|
const modelFile = getModelPath(model, this.options, settings);
|
|
868
895
|
if (!fs.existsSync(modelFile)) {
|
|
869
896
|
throw new Error(`Model is not downloaded: ${model.name}`);
|
|
870
897
|
}
|
|
871
898
|
|
|
872
|
-
const args = ["-m", modelFile, "-f", audioFile, "-np", "-nt"];
|
|
873
|
-
if (settings.language && settings.language !== "auto") args.push("-l", settings.language);
|
|
899
|
+
const args = model.engine === "transcribe-cpp" ? ["--model", modelFile, "--file", audioFile] : ["-m", modelFile, "-f", audioFile, "-np", "-nt"];
|
|
900
|
+
if (settings.language && settings.language !== "auto") args.push(model.engine === "transcribe-cpp" ? "--language" : "-l", settings.language);
|
|
874
901
|
|
|
875
902
|
return new Promise((resolve, reject) => {
|
|
876
903
|
let stdout = "";
|
|
877
904
|
let stderr = "";
|
|
878
|
-
const proc = spawn(
|
|
905
|
+
const proc = spawn(binary, args, { env: childEnv(commandOptions), stdio: ["ignore", "pipe", "pipe"] });
|
|
879
906
|
this.transcription = proc;
|
|
880
907
|
|
|
881
908
|
const timer = setTimeout(() => {
|
|
@@ -901,7 +928,7 @@ export class VoiceRuntime {
|
|
|
901
928
|
clearTimeout(timer);
|
|
902
929
|
this.transcription = null;
|
|
903
930
|
if (code !== 0) {
|
|
904
|
-
reject(new Error(stderr.trim().split("\n").pop() ||
|
|
931
|
+
reject(new Error(stderr.trim().split("\n").pop() || `${command} exited with code ${code}`));
|
|
905
932
|
return;
|
|
906
933
|
}
|
|
907
934
|
|
package/lib/engines.js
CHANGED
|
@@ -9,23 +9,39 @@ import { sha256, ensureDir, replaceFile } from "./download.js";
|
|
|
9
9
|
import { getCacheDir } from "./models.js";
|
|
10
10
|
import { getBundledEngineDir, resolveCommand } from "./engine.js";
|
|
11
11
|
|
|
12
|
-
export const DEFAULT_ENGINE_REGISTRY_URL = "https://github.com/ihxnnxs/opencode-voice/releases/download/v0.
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
export const DEFAULT_ENGINE_REGISTRY_URL = "https://github.com/ihxnnxs/opencode-voice/releases/download/v0.2.0/registry.json";
|
|
13
|
+
|
|
14
|
+
export const ENGINES = {
|
|
15
|
+
"whisper.cpp": {
|
|
16
|
+
id: "whisper.cpp",
|
|
17
|
+
command: "whisper-cli",
|
|
18
|
+
probeArgs: ["--help"],
|
|
19
|
+
probeContains: ["--model", "--file"],
|
|
20
|
+
},
|
|
21
|
+
"transcribe-cpp": {
|
|
22
|
+
id: "transcribe-cpp",
|
|
23
|
+
command: "opencode-voice-transcribe",
|
|
24
|
+
probeArgs: ["--help"],
|
|
25
|
+
probeContains: ["--model", "--file"],
|
|
26
|
+
},
|
|
19
27
|
};
|
|
20
28
|
|
|
21
29
|
const DEFAULT_ENGINE_DOWNLOAD_RETRIES = 5;
|
|
30
|
+
const DEFAULT_ENGINE_DOWNLOAD_TIMEOUT_MS = 120000;
|
|
22
31
|
|
|
23
32
|
function sleep(ms) {
|
|
24
33
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
25
34
|
}
|
|
26
35
|
|
|
27
|
-
function
|
|
28
|
-
|
|
36
|
+
function getEngine(engineId) {
|
|
37
|
+
const engine = ENGINES[engineId];
|
|
38
|
+
if (!engine) throw new Error(`Unsupported engine: ${engineId}`);
|
|
39
|
+
return engine;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function executableName(engineId, options = {}) {
|
|
43
|
+
const command = getEngine(engineId).command;
|
|
44
|
+
return (options.platform || process.platform) === "win32" ? `${command}.exe` : command;
|
|
29
45
|
}
|
|
30
46
|
|
|
31
47
|
function temporaryBinaryPath(binaryPath) {
|
|
@@ -45,20 +61,23 @@ export function getEnginePlatformKey(options = {}) {
|
|
|
45
61
|
return `${options.platform || process.platform}-${normalizePlatform(options.arch || process.arch)}`;
|
|
46
62
|
}
|
|
47
63
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return getBundledEngineDir(ENGINE.command, { ...options, downloadDir: settings.downloadDir });
|
|
64
|
+
function withDownloadDir(options = {}, settings = {}) {
|
|
65
|
+
return { ...options, downloadDir: settings.downloadDir || options.downloadDir };
|
|
51
66
|
}
|
|
52
67
|
|
|
53
|
-
export function
|
|
54
|
-
return
|
|
68
|
+
export function getManagedEngineDir(engineId = "whisper.cpp", options = {}, settings = {}) {
|
|
69
|
+
return getBundledEngineDir(getEngine(engineId).command, withDownloadDir(options, settings));
|
|
55
70
|
}
|
|
56
71
|
|
|
57
|
-
export function
|
|
72
|
+
export function getManagedEngineBinary(engineId = "whisper.cpp", options = {}, settings = {}) {
|
|
73
|
+
return path.join(getManagedEngineDir(engineId, options, settings), executableName(engineId, options));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function getEngineManifestPath(engineId = "whisper.cpp", options = {}, settings = {}) {
|
|
58
77
|
return path.join(getManagedEngineDir(engineId, options, settings), "manifest.json");
|
|
59
78
|
}
|
|
60
79
|
|
|
61
|
-
export function readEngineManifest(engineId =
|
|
80
|
+
export function readEngineManifest(engineId = "whisper.cpp", options = {}, settings = {}) {
|
|
62
81
|
const file = getEngineManifestPath(engineId, options, settings);
|
|
63
82
|
if (!fs.existsSync(file)) return null;
|
|
64
83
|
try {
|
|
@@ -68,41 +87,64 @@ export function readEngineManifest(engineId = ENGINE.id, options = {}, settings
|
|
|
68
87
|
}
|
|
69
88
|
}
|
|
70
89
|
|
|
71
|
-
function engineSource(resolved, managedBinary, options = {}) {
|
|
90
|
+
function engineSource(engine, resolved, managedBinary, options = {}) {
|
|
72
91
|
if (!resolved) return "missing";
|
|
73
92
|
if (path.resolve(resolved) === path.resolve(managedBinary)) return "managed";
|
|
74
|
-
|
|
75
|
-
|
|
93
|
+
const optionKey = engine.id === "whisper.cpp" ? "whisperCli" : "transcribeCpp";
|
|
94
|
+
const envKey = engine.id === "whisper.cpp" ? "OPENCODE_VOICE_WHISPER_CLI" : "OPENCODE_VOICE_TRANSCRIBE_CPP";
|
|
95
|
+
if (options[optionKey] && path.resolve(resolved) === path.resolve(options[optionKey])) return "option";
|
|
96
|
+
if (process.env[envKey] && path.resolve(resolved) === path.resolve(process.env[envKey])) return "env";
|
|
76
97
|
return "system";
|
|
77
98
|
}
|
|
78
99
|
|
|
79
|
-
export function getEngineStatus(engineId =
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
const
|
|
100
|
+
export function getEngineStatus(engineId = "whisper.cpp", options = {}, settings = {}) {
|
|
101
|
+
const engine = getEngine(engineId);
|
|
102
|
+
const commandOptions = withDownloadDir(options, settings);
|
|
103
|
+
const managedDir = getManagedEngineDir(engineId, commandOptions);
|
|
104
|
+
const managedBinary = getManagedEngineBinary(engineId, commandOptions);
|
|
105
|
+
const manifest = readEngineManifest(engineId, commandOptions);
|
|
106
|
+
const resolvedBinary = resolveCommand(engine.command, commandOptions);
|
|
85
107
|
|
|
86
108
|
return {
|
|
87
109
|
id: engineId,
|
|
88
|
-
command:
|
|
89
|
-
platform: getEnginePlatformKey(
|
|
90
|
-
cacheDir: getCacheDir(
|
|
110
|
+
command: engine.command,
|
|
111
|
+
platform: getEnginePlatformKey(commandOptions),
|
|
112
|
+
cacheDir: getCacheDir(commandOptions),
|
|
91
113
|
managedDir,
|
|
92
114
|
managedBinary,
|
|
93
115
|
managedInstalled: fs.existsSync(managedBinary),
|
|
94
116
|
manifest,
|
|
95
117
|
resolvedBinary,
|
|
96
|
-
source: engineSource(resolvedBinary, managedBinary,
|
|
118
|
+
source: engineSource(engine, resolvedBinary, managedBinary, commandOptions),
|
|
97
119
|
};
|
|
98
120
|
}
|
|
99
121
|
|
|
100
|
-
|
|
101
|
-
|
|
122
|
+
function summarizeProbeOutput(output) {
|
|
123
|
+
return String(output || "").replace(/\s+/g, " ").trim().slice(0, 240);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function engineProbeMessage(message) {
|
|
127
|
+
const text = summarizeProbeOutput(message);
|
|
128
|
+
if (/\b(MSVCP140|VCOMP140|VCRUNTIME140|VCRUNTIME140_1|api-ms-win-crt)\b/i.test(text)) {
|
|
129
|
+
return `${text}; Windows VC++ runtime dependency is missing`;
|
|
130
|
+
}
|
|
131
|
+
return text || "unknown probe failure";
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function probeEngine(engineId = "whisper.cpp", binaryPath, options = {}) {
|
|
135
|
+
const engine = getEngine(engineId);
|
|
102
136
|
if (!binaryPath) return { ok: false, message: "missing binary" };
|
|
103
137
|
|
|
104
138
|
return new Promise((resolve) => {
|
|
105
139
|
let output = "";
|
|
140
|
+
let settled = false;
|
|
141
|
+
let timer;
|
|
142
|
+
const finish = (result) => {
|
|
143
|
+
if (settled) return;
|
|
144
|
+
settled = true;
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
resolve(result);
|
|
147
|
+
};
|
|
106
148
|
const localLib = path.join(os.homedir(), ".local", "lib");
|
|
107
149
|
const env = {
|
|
108
150
|
...process.env,
|
|
@@ -111,12 +153,12 @@ export async function probeEngine(engineId = ENGINE.id, binaryPath, options = {}
|
|
|
111
153
|
DYLD_LIBRARY_PATH: [localLib, process.env.DYLD_LIBRARY_PATH].filter(Boolean).join(path.delimiter),
|
|
112
154
|
...options.env,
|
|
113
155
|
};
|
|
114
|
-
const proc = spawn(binaryPath,
|
|
115
|
-
|
|
156
|
+
const proc = spawn(binaryPath, engine.probeArgs, { stdio: ["ignore", "pipe", "pipe"], env });
|
|
157
|
+
timer = setTimeout(() => {
|
|
116
158
|
try {
|
|
117
159
|
proc.kill("SIGKILL");
|
|
118
160
|
} catch {}
|
|
119
|
-
|
|
161
|
+
finish({ ok: false, message: "probe timed out" });
|
|
120
162
|
}, options.timeoutMs || 10000);
|
|
121
163
|
|
|
122
164
|
proc.stdout.on("data", (chunk) => {
|
|
@@ -126,13 +168,12 @@ export async function probeEngine(engineId = ENGINE.id, binaryPath, options = {}
|
|
|
126
168
|
output += chunk.toString();
|
|
127
169
|
});
|
|
128
170
|
proc.on("error", (error) => {
|
|
129
|
-
|
|
130
|
-
resolve({ ok: false, message: error.message });
|
|
171
|
+
finish({ ok: false, message: engineProbeMessage(error.message) });
|
|
131
172
|
});
|
|
132
|
-
proc.on("exit", () => {
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
173
|
+
proc.on("exit", (code, signal) => {
|
|
174
|
+
const ok = engine.probeContains.every((item) => output.includes(item));
|
|
175
|
+
const detail = summarizeProbeOutput(output) || `exit ${code ?? signal ?? "unknown"}`;
|
|
176
|
+
finish({ ok, message: ok ? "ok" : engineProbeMessage(`unexpected ${engine.command} help output: ${detail}`) });
|
|
136
177
|
});
|
|
137
178
|
});
|
|
138
179
|
}
|
|
@@ -150,6 +191,14 @@ async function fetchWithTimeout(url, init = {}, timeoutMs = 60000) {
|
|
|
150
191
|
}
|
|
151
192
|
}
|
|
152
193
|
|
|
194
|
+
function engineDownloadTimeoutMs(options = {}, hooks = {}) {
|
|
195
|
+
return Number(options.engineDownloadTimeoutMs || options.downloadTimeoutMs || hooks.timeoutMs || DEFAULT_ENGINE_DOWNLOAD_TIMEOUT_MS);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function engineDownloadTimeoutError(url, timeoutMs) {
|
|
199
|
+
return new Error(`Engine download stalled after ${Math.round(timeoutMs / 1000)}s: ${url}`);
|
|
200
|
+
}
|
|
201
|
+
|
|
153
202
|
async function readJsonUrl(url, options = {}) {
|
|
154
203
|
if (url.startsWith("file://")) return JSON.parse(await fs.promises.readFile(new URL(url), "utf8"));
|
|
155
204
|
if (!/^https?:\/\//.test(url)) return JSON.parse(await fs.promises.readFile(path.resolve(url), "utf8"));
|
|
@@ -201,7 +250,8 @@ async function downloadAsset(asset, compressedFile, hooks = {}, attempt = 1, att
|
|
|
201
250
|
return;
|
|
202
251
|
}
|
|
203
252
|
|
|
204
|
-
const
|
|
253
|
+
const timeoutMs = engineDownloadTimeoutMs(options, hooks);
|
|
254
|
+
const response = await fetchWithTimeout(asset.url, {}, timeoutMs);
|
|
205
255
|
if (!response.ok) throw new Error(`Engine download failed: HTTP ${response.status}`);
|
|
206
256
|
|
|
207
257
|
const contentLength = Number(response.headers.get("content-length") || asset.size || 0);
|
|
@@ -211,25 +261,42 @@ async function downloadAsset(asset, compressedFile, hooks = {}, attempt = 1, att
|
|
|
211
261
|
const body = response.body?.getReader ? Readable.fromWeb(response.body) : response.body;
|
|
212
262
|
if (!body) throw new Error("Engine download failed: empty response body");
|
|
213
263
|
|
|
264
|
+
const controller = new AbortController();
|
|
265
|
+
let stallTimer;
|
|
266
|
+
const resetStallTimer = () => {
|
|
267
|
+
clearTimeout(stallTimer);
|
|
268
|
+
stallTimer = setTimeout(() => controller.abort(), timeoutMs);
|
|
269
|
+
};
|
|
270
|
+
|
|
214
271
|
const progress = new Transform({
|
|
215
272
|
transform(chunk, _encoding, callback) {
|
|
273
|
+
resetStallTimer();
|
|
216
274
|
downloaded += chunk.length ?? chunk.byteLength ?? 0;
|
|
217
275
|
hooks.onProgress?.({ state: "downloading", downloaded, total: contentLength, percent: contentLength ? (downloaded / contentLength) * 100 : 0, attempt, attempts });
|
|
218
276
|
callback(null, chunk);
|
|
219
277
|
},
|
|
220
278
|
});
|
|
221
279
|
|
|
222
|
-
|
|
280
|
+
resetStallTimer();
|
|
281
|
+
try {
|
|
282
|
+
await pipeline(body, progress, fs.createWriteStream(compressedFile), { signal: controller.signal });
|
|
283
|
+
} catch (error) {
|
|
284
|
+
if (error?.name === "AbortError") throw engineDownloadTimeoutError(asset.url, timeoutMs);
|
|
285
|
+
throw error;
|
|
286
|
+
} finally {
|
|
287
|
+
clearTimeout(stallTimer);
|
|
288
|
+
}
|
|
223
289
|
}
|
|
224
290
|
|
|
225
291
|
async function writeManifest(engineId, managedBinary, source, options = {}) {
|
|
292
|
+
const engine = getEngine(engineId);
|
|
226
293
|
const hash = await sha256(managedBinary);
|
|
227
294
|
const stat = await fs.promises.stat(managedBinary);
|
|
228
295
|
const manifest = {
|
|
229
296
|
schema: "opencode-voice.engine-install.v1",
|
|
230
297
|
id: engineId,
|
|
231
298
|
kind: "cli",
|
|
232
|
-
command:
|
|
299
|
+
command: engine.command,
|
|
233
300
|
platform: getEnginePlatformKey(options),
|
|
234
301
|
version: source.version || "local-import",
|
|
235
302
|
source,
|
|
@@ -241,32 +308,33 @@ async function writeManifest(engineId, managedBinary, source, options = {}) {
|
|
|
241
308
|
return manifest;
|
|
242
309
|
}
|
|
243
310
|
|
|
244
|
-
export async function installManagedEngine(engineId =
|
|
245
|
-
|
|
311
|
+
export async function installManagedEngine(engineId = "whisper.cpp", options = {}, settings = {}, hooks = {}) {
|
|
312
|
+
getEngine(engineId);
|
|
246
313
|
|
|
247
|
-
const
|
|
248
|
-
const
|
|
314
|
+
const commandOptions = withDownloadDir(options, settings);
|
|
315
|
+
const managedDir = getManagedEngineDir(engineId, commandOptions);
|
|
316
|
+
const managedBinary = getManagedEngineBinary(engineId, commandOptions);
|
|
249
317
|
if (!hooks.force && fs.existsSync(managedBinary)) {
|
|
250
318
|
const probe = await probeEngine(engineId, managedBinary);
|
|
251
|
-
if (probe.ok) return { manifest: readEngineManifest(engineId,
|
|
319
|
+
if (probe.ok) return { manifest: readEngineManifest(engineId, commandOptions), managedBinary, skipped: true };
|
|
252
320
|
}
|
|
253
321
|
|
|
254
322
|
hooks.onProgress?.({ state: "registry", downloaded: 0, total: 0, percent: 0, attempt: 1, attempts: 1 });
|
|
255
|
-
const { url: registryUrl, registry } = await loadEngineRegistry(
|
|
256
|
-
const platform = getEnginePlatformKey(
|
|
323
|
+
const { url: registryUrl, registry } = await loadEngineRegistry(commandOptions);
|
|
324
|
+
const platform = getEnginePlatformKey(commandOptions);
|
|
257
325
|
const { engine, asset } = selectEngineAsset(registry, engineId, platform);
|
|
258
326
|
|
|
259
327
|
await ensureDir(managedDir);
|
|
260
328
|
const compressedFile = path.join(managedDir, `${path.basename(asset.url || `${platform}.gz`)}.download`);
|
|
261
329
|
const tmpBinary = temporaryBinaryPath(managedBinary);
|
|
262
|
-
const attempts = Number(
|
|
330
|
+
const attempts = Number(commandOptions.engineDownloadRetries || hooks.retries || DEFAULT_ENGINE_DOWNLOAD_RETRIES);
|
|
263
331
|
let lastError;
|
|
264
332
|
|
|
265
333
|
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
266
334
|
try {
|
|
267
335
|
await fs.promises.unlink(compressedFile).catch(() => {});
|
|
268
336
|
await fs.promises.unlink(tmpBinary).catch(() => {});
|
|
269
|
-
await downloadAsset(asset, compressedFile, hooks, attempt, attempts,
|
|
337
|
+
await downloadAsset(asset, compressedFile, hooks, attempt, attempts, commandOptions);
|
|
270
338
|
|
|
271
339
|
hooks.onProgress?.({ state: "verifying", downloaded: asset.size || 0, total: asset.size || 0, percent: 100, attempt, attempts });
|
|
272
340
|
if (asset.sha256 && (await sha256(compressedFile)) !== asset.sha256) throw new Error("Engine archive SHA256 mismatch");
|
|
@@ -294,7 +362,7 @@ export async function installManagedEngine(engineId = ENGINE.id, options = {}, s
|
|
|
294
362
|
version: engine.version || registry.version || "registry",
|
|
295
363
|
upstream: engine.upstream,
|
|
296
364
|
},
|
|
297
|
-
|
|
365
|
+
commandOptions,
|
|
298
366
|
);
|
|
299
367
|
hooks.onProgress?.({ state: "done", downloaded: asset.binary?.size || asset.size || 0, total: asset.binary?.size || asset.size || 0, percent: 100, attempt, attempts });
|
|
300
368
|
return { manifest, managedBinary, skipped: false };
|
|
@@ -310,16 +378,17 @@ export async function installManagedEngine(engineId = ENGINE.id, options = {}, s
|
|
|
310
378
|
throw lastError;
|
|
311
379
|
}
|
|
312
380
|
|
|
313
|
-
export async function importManagedEngine(engineId =
|
|
314
|
-
|
|
315
|
-
if (
|
|
381
|
+
export async function importManagedEngine(engineId = "whisper.cpp", sourcePath, options = {}, settings = {}) {
|
|
382
|
+
const engine = getEngine(engineId);
|
|
383
|
+
if (!sourcePath) throw new Error(`Pass an ${engine.command} path to import`);
|
|
316
384
|
|
|
317
385
|
const source = path.resolve(sourcePath.replace(/^~(?=$|\/)/, os.homedir()));
|
|
318
386
|
const stat = await fs.promises.stat(source).catch(() => null);
|
|
319
387
|
if (!stat?.isFile()) throw new Error(`Engine binary not found: ${source}`);
|
|
320
388
|
|
|
321
|
-
const
|
|
322
|
-
const
|
|
389
|
+
const commandOptions = withDownloadDir(options, settings);
|
|
390
|
+
const managedDir = getManagedEngineDir(engineId, commandOptions);
|
|
391
|
+
const managedBinary = getManagedEngineBinary(engineId, commandOptions);
|
|
323
392
|
const tmp = temporaryBinaryPath(managedBinary);
|
|
324
393
|
await ensureDir(managedDir);
|
|
325
394
|
await fs.promises.copyFile(source, tmp);
|
|
@@ -332,11 +401,12 @@ export async function importManagedEngine(engineId = ENGINE.id, sourcePath, opti
|
|
|
332
401
|
}
|
|
333
402
|
|
|
334
403
|
await replaceFile(tmp, managedBinary);
|
|
335
|
-
const manifest = await writeManifest(engineId, managedBinary, { type: "local-file", path: source },
|
|
404
|
+
const manifest = await writeManifest(engineId, managedBinary, { type: "local-file", path: source }, commandOptions);
|
|
336
405
|
return { manifest, managedBinary };
|
|
337
406
|
}
|
|
338
407
|
|
|
339
|
-
export async function removeManagedEngine(engineId =
|
|
408
|
+
export async function removeManagedEngine(engineId = "whisper.cpp", options = {}, settings = {}) {
|
|
409
|
+
getEngine(engineId);
|
|
340
410
|
const dir = getManagedEngineDir(engineId, options, settings);
|
|
341
411
|
await fs.promises.rm(dir, { recursive: true, force: true });
|
|
342
412
|
return dir;
|