@senad-d/micme 0.1.2 → 0.2.1
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 +1 -0
- package/README.md +33 -6
- package/SECURITY.md +2 -2
- package/micme.example.json +3 -1
- package/micme.schema.json +35 -2
- package/package.json +3 -2
- package/scripts/doctor.mjs +92 -0
- package/src/audio.ts +539 -34
- package/src/backends.ts +249 -0
- package/src/config.ts +25 -5
- package/src/constants.ts +5 -1
- package/src/extension.ts +25 -16
- package/src/models.ts +100 -9
- package/src/settings.ts +139 -85
- package/src/streaming.ts +229 -34
- package/src/transcription.ts +32 -48
- package/src/types.ts +29 -1
package/src/backends.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import type { TranscribeBackend, TranscriptionMode, ResolvedTranscriptionPlan, ResolvedWhisperCppModel } from "./types.ts";
|
|
3
|
+
import { DEFAULT_TRANSCRIBE_BACKEND } from "./constants.ts";
|
|
4
|
+
import { env, expandConfigPath, getTranscribeBackend, isTranscribeBackend } from "./config.ts";
|
|
5
|
+
import { getPythonWhisperModelName, resolveWhisperCppModel } from "./models.ts";
|
|
6
|
+
import { findExecutable } from "./processes.ts";
|
|
7
|
+
|
|
8
|
+
export type ResolveTranscriptionPlanOptions = {
|
|
9
|
+
requestedBackend?: TranscribeBackend | string;
|
|
10
|
+
transcriptionMode?: TranscriptionMode;
|
|
11
|
+
customCommand?: string | null;
|
|
12
|
+
whisperCppBinary?: string | null;
|
|
13
|
+
whisperCppBinaryError?: string;
|
|
14
|
+
whisperStreamBinary?: string | null;
|
|
15
|
+
whisperStreamBinaryError?: string;
|
|
16
|
+
pythonWhisperBinary?: string | null;
|
|
17
|
+
whisperCppModel?: ResolvedWhisperCppModel;
|
|
18
|
+
pythonModelName?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function resolveTranscriptionPlan(options: ResolveTranscriptionPlanOptions = {}): ResolvedTranscriptionPlan {
|
|
22
|
+
const warnings: string[] = [];
|
|
23
|
+
const requestedBackend = parseRequestedBackend(options.requestedBackend, warnings);
|
|
24
|
+
const transcriptionMode = options.transcriptionMode ?? "clip";
|
|
25
|
+
const customCommand = getOptionValue(options, "customCommand", () => env("MICME_TRANSCRIBE_COMMAND")?.trim() || undefined);
|
|
26
|
+
const whisperCppModel = options.whisperCppModel ?? resolveWhisperCppModel();
|
|
27
|
+
const pythonModelName = options.pythonModelName ?? getPythonWhisperModelName();
|
|
28
|
+
|
|
29
|
+
if (transcriptionMode === "stream") {
|
|
30
|
+
return resolveStreamingPlan({
|
|
31
|
+
requestedBackend,
|
|
32
|
+
customCommand: customCommand ?? undefined,
|
|
33
|
+
whisperCppModel,
|
|
34
|
+
warnings,
|
|
35
|
+
whisperStreamBinary: getExecutableOption(options, "whisperStreamBinary", "whisperStreamBinaryError", getWhisperStreamBinary),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const whisperCppBinary = getExecutableOption(options, "whisperCppBinary", "whisperCppBinaryError", getWhisperCppBinary);
|
|
40
|
+
const pythonWhisperBinary = getOptionValue(options, "pythonWhisperBinary", getPythonWhisperBinary);
|
|
41
|
+
|
|
42
|
+
if (requestedBackend === "custom") {
|
|
43
|
+
return customCommand
|
|
44
|
+
? customPlan(requestedBackend, customCommand, "MICME_TRANSCRIBE_BACKEND=custom")
|
|
45
|
+
: nonePlan(requestedBackend, "MICME_TRANSCRIBE_BACKEND=custom but MICME_TRANSCRIBE_COMMAND is not set.", warnings);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (requestedBackend === "whisper.cpp") {
|
|
49
|
+
return whisperCppBinary
|
|
50
|
+
? whisperCppPlan(requestedBackend, whisperCppBinary, whisperCppModel, "MICME_TRANSCRIBE_BACKEND=whisper.cpp")
|
|
51
|
+
: nonePlan(requestedBackend, "MICME_TRANSCRIBE_BACKEND=whisper.cpp but whisper.cpp was not found.", warnings);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (requestedBackend === "python") {
|
|
55
|
+
return pythonWhisperBinary
|
|
56
|
+
? pythonPlan(requestedBackend, pythonWhisperBinary, pythonModelName, "MICME_TRANSCRIBE_BACKEND=python")
|
|
57
|
+
: nonePlan(requestedBackend, "MICME_TRANSCRIBE_BACKEND=python but the `whisper` CLI was not found.", warnings);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (customCommand) return customPlan(requestedBackend, customCommand, "auto selected custom command because MICME_TRANSCRIBE_COMMAND is configured");
|
|
61
|
+
if (whisperCppBinary) return whisperCppPlan(requestedBackend, whisperCppBinary, whisperCppModel, "auto selected whisper.cpp because a whisper.cpp binary is available");
|
|
62
|
+
if (pythonWhisperBinary) return pythonPlan(requestedBackend, pythonWhisperBinary, pythonModelName, "auto selected Python Whisper because whisper.cpp is unavailable");
|
|
63
|
+
|
|
64
|
+
return nonePlan(
|
|
65
|
+
requestedBackend,
|
|
66
|
+
"No Micme transcription backend found. Install whisper-cpp, install openai-whisper, set MICME_TRANSCRIBE_COMMAND, or choose a backend in /micme conf.",
|
|
67
|
+
warnings,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function getWhisperCppBinary() {
|
|
72
|
+
const configured = env("MICME_WHISPER_CPP_BIN")?.trim();
|
|
73
|
+
if (configured) return resolveExecutableConfig(configured, "MICME_WHISPER_CPP_BIN");
|
|
74
|
+
return findExecutable(["whisper-cli", "whisper-cpp"]);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function getWhisperStreamBinary() {
|
|
78
|
+
const configured = env("MICME_WHISPER_STREAM_BIN")?.trim();
|
|
79
|
+
if (configured) return resolveExecutableConfig(configured, "MICME_WHISPER_STREAM_BIN");
|
|
80
|
+
return findExecutable(["whisper-stream"]);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function getPythonWhisperBinary() {
|
|
84
|
+
return findExecutable(["whisper"]);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function resolveExecutableConfig(value: string, configName: string) {
|
|
88
|
+
const expanded = expandConfigPath(value);
|
|
89
|
+
if (/[/\\]/.test(expanded)) {
|
|
90
|
+
if (!existsSync(expanded)) throw new Error(`${configName} is set but not found: ${expanded}`);
|
|
91
|
+
return expanded;
|
|
92
|
+
}
|
|
93
|
+
const executable = findExecutable([expanded]);
|
|
94
|
+
if (!executable) throw new Error(`${configName} is set but not found on PATH: ${expanded}`);
|
|
95
|
+
return executable;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function formatTranscriptionPlan(plan: ResolvedTranscriptionPlan) {
|
|
99
|
+
const lines = [`Requested backend: ${formatBackendLabel(plan.requestedBackend)}`, `Effective backend: ${formatEffectiveBackendLabel(plan.effectiveBackend)}`];
|
|
100
|
+
if (plan.binary) lines.push(`Binary: ${plan.binary}`);
|
|
101
|
+
if (plan.modelPath) lines.push(`Model: ${plan.modelPath}`);
|
|
102
|
+
else if (plan.modelName) lines.push(`Model: ${plan.modelName}`);
|
|
103
|
+
if (plan.modelSource) lines.push(`Source: ${plan.modelSource}`);
|
|
104
|
+
for (const warning of plan.warnings) lines.push(`Warning: ${warning}`);
|
|
105
|
+
lines.push(`Reason: ${plan.reason}`);
|
|
106
|
+
return lines.join("\n");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function formatBackendLabel(backend: TranscribeBackend) {
|
|
110
|
+
switch (backend) {
|
|
111
|
+
case "auto":
|
|
112
|
+
return "Auto";
|
|
113
|
+
case "whisper.cpp":
|
|
114
|
+
return "whisper.cpp";
|
|
115
|
+
case "python":
|
|
116
|
+
return "Python Whisper";
|
|
117
|
+
case "custom":
|
|
118
|
+
return "Custom command";
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function formatEffectiveBackendLabel(backend: ResolvedTranscriptionPlan["effectiveBackend"]) {
|
|
123
|
+
switch (backend) {
|
|
124
|
+
case "whisper.cpp":
|
|
125
|
+
return "whisper.cpp";
|
|
126
|
+
case "python":
|
|
127
|
+
return "Python Whisper";
|
|
128
|
+
case "custom":
|
|
129
|
+
return "Custom command";
|
|
130
|
+
case "none":
|
|
131
|
+
return "none";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function resolveStreamingPlan(options: {
|
|
136
|
+
requestedBackend: TranscribeBackend;
|
|
137
|
+
customCommand?: string;
|
|
138
|
+
whisperCppModel: ResolvedWhisperCppModel;
|
|
139
|
+
warnings: string[];
|
|
140
|
+
whisperStreamBinary?: string;
|
|
141
|
+
}): ResolvedTranscriptionPlan {
|
|
142
|
+
const { requestedBackend, whisperCppModel, warnings, whisperStreamBinary } = options;
|
|
143
|
+
if (requestedBackend === "python") {
|
|
144
|
+
return nonePlan(requestedBackend, "Streaming mode requires whisper.cpp; Python Whisper only supports clip transcription.", warnings);
|
|
145
|
+
}
|
|
146
|
+
if (requestedBackend === "custom") {
|
|
147
|
+
return nonePlan(requestedBackend, "Streaming mode requires whisper.cpp; custom transcribe commands only support clip transcription.", warnings);
|
|
148
|
+
}
|
|
149
|
+
if (!whisperStreamBinary) {
|
|
150
|
+
const reason = requestedBackend === "auto"
|
|
151
|
+
? "MICME_TRANSCRIPTION_MODE=stream but whisper-stream was not found. Auto stream mode requires whisper.cpp/whisper-stream."
|
|
152
|
+
: "MICME_TRANSCRIBE_BACKEND=whisper.cpp and MICME_TRANSCRIPTION_MODE=stream but whisper-stream was not found.";
|
|
153
|
+
return nonePlan(requestedBackend, reason, warnings);
|
|
154
|
+
}
|
|
155
|
+
return whisperCppPlan(requestedBackend, whisperStreamBinary, whisperCppModel, "streaming selected whisper.cpp because whisper-stream is available");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function customPlan(requestedBackend: TranscribeBackend, command: string, reason: string): ResolvedTranscriptionPlan {
|
|
159
|
+
return {
|
|
160
|
+
requestedBackend,
|
|
161
|
+
effectiveBackend: "custom",
|
|
162
|
+
reason,
|
|
163
|
+
command,
|
|
164
|
+
modelName: "unknown",
|
|
165
|
+
modelSource: "custom-command",
|
|
166
|
+
warnings: [],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function whisperCppPlan(requestedBackend: TranscribeBackend, binary: string, model: ResolvedWhisperCppModel, reason: string): ResolvedTranscriptionPlan {
|
|
171
|
+
const warnings: string[] = [];
|
|
172
|
+
if (model.source === "explicit-path" && !model.exists && !model.downloadable) {
|
|
173
|
+
warnings.push(`MICME_WHISPER_CPP_MODEL is set but not found: ${model.path}`);
|
|
174
|
+
} else if (!model.exists && !model.downloadable) {
|
|
175
|
+
warnings.push(`Whisper.cpp model is missing and is not a standard downloadable model: ${model.path}`);
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
requestedBackend,
|
|
179
|
+
effectiveBackend: "whisper.cpp",
|
|
180
|
+
reason,
|
|
181
|
+
binary,
|
|
182
|
+
modelName: model.modelName,
|
|
183
|
+
modelPath: model.path,
|
|
184
|
+
modelSource: model.source,
|
|
185
|
+
modelDownloadable: model.downloadable,
|
|
186
|
+
warnings,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function pythonPlan(requestedBackend: TranscribeBackend, binary: string, modelName: string, reason: string): ResolvedTranscriptionPlan {
|
|
191
|
+
return {
|
|
192
|
+
requestedBackend,
|
|
193
|
+
effectiveBackend: "python",
|
|
194
|
+
reason,
|
|
195
|
+
binary,
|
|
196
|
+
modelName,
|
|
197
|
+
modelSource: "python-name",
|
|
198
|
+
warnings: [],
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function nonePlan(requestedBackend: TranscribeBackend, reason: string, warnings: string[]): ResolvedTranscriptionPlan {
|
|
203
|
+
return {
|
|
204
|
+
requestedBackend,
|
|
205
|
+
effectiveBackend: "none",
|
|
206
|
+
reason,
|
|
207
|
+
warnings: [...warnings],
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function parseRequestedBackend(value: TranscribeBackend | string | undefined, warnings: string[]) {
|
|
212
|
+
const raw = value ?? env("MICME_TRANSCRIBE_BACKEND")?.trim();
|
|
213
|
+
if (!raw) return getTranscribeBackend();
|
|
214
|
+
if (isTranscribeBackend(raw)) return raw;
|
|
215
|
+
warnings.push(`Invalid MICME_TRANSCRIBE_BACKEND=${raw}; using ${DEFAULT_TRANSCRIBE_BACKEND}.`);
|
|
216
|
+
return DEFAULT_TRANSCRIBE_BACKEND;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function getOptionValue<K extends keyof ResolveTranscriptionPlanOptions>(
|
|
220
|
+
options: ResolveTranscriptionPlanOptions,
|
|
221
|
+
key: K,
|
|
222
|
+
fallback: () => Extract<ResolveTranscriptionPlanOptions[K], string | null | undefined>,
|
|
223
|
+
) {
|
|
224
|
+
if (Object.prototype.hasOwnProperty.call(options, key)) {
|
|
225
|
+
const value = options[key];
|
|
226
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
227
|
+
}
|
|
228
|
+
const value = fallback();
|
|
229
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function getExecutableOption(
|
|
233
|
+
options: ResolveTranscriptionPlanOptions,
|
|
234
|
+
key: "whisperCppBinary" | "whisperStreamBinary",
|
|
235
|
+
errorKey: "whisperCppBinaryError" | "whisperStreamBinaryError",
|
|
236
|
+
fallback: () => string | undefined,
|
|
237
|
+
) {
|
|
238
|
+
const error = options[errorKey];
|
|
239
|
+
if (error) return undefined;
|
|
240
|
+
if (Object.prototype.hasOwnProperty.call(options, key)) {
|
|
241
|
+
const value = options[key];
|
|
242
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
return fallback();
|
|
246
|
+
} catch {
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { mkdir, rename, rm, writeFile } from "node:fs/promises";
|
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { dirname, join, resolve } from "node:path";
|
|
6
|
-
import type { MicmeConfigState, TranscriptionMode } from "./types.ts";
|
|
6
|
+
import type { MicmeConfigState, TranscribeBackend, TranscriptionMode } from "./types.ts";
|
|
7
7
|
import {
|
|
8
8
|
DEFAULT_MACOS_PRINTABLE_SHORTCUT,
|
|
9
9
|
DEFAULT_RECORD_SAMPLE_RATE,
|
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
DEFAULT_STREAM_MAX_TOKENS,
|
|
14
14
|
DEFAULT_STREAM_STEP_MS,
|
|
15
15
|
DEFAULT_STREAM_VAD_THRESHOLD,
|
|
16
|
+
DEFAULT_STREAM_FLUSH_MS,
|
|
17
|
+
DEFAULT_TRANSCRIBE_BACKEND,
|
|
16
18
|
DEFAULT_TRANSCRIBE_SAMPLE_RATE,
|
|
17
19
|
DEFAULT_TRANSCRIBE_TIMEOUT_MS,
|
|
18
20
|
DEFAULT_MIN_MAX_VOLUME_DB,
|
|
@@ -22,6 +24,7 @@ import {
|
|
|
22
24
|
STREAM_PROFILE_STEP_MS,
|
|
23
25
|
STREAM_PROFILE_VAD_THRESHOLD,
|
|
24
26
|
STREAM_PROFILE_WORDS_PER_CHUNK,
|
|
27
|
+
STREAM_PROFILE_FLUSH_MS,
|
|
25
28
|
} from "./constants.ts";
|
|
26
29
|
|
|
27
30
|
const MICME_CONFIG_FILE = "micme.json";
|
|
@@ -106,7 +109,9 @@ export function getTranscriptionModeProfile(mode: TranscriptionMode): Record<str
|
|
|
106
109
|
MICME_STREAM_KEEP_MS: String(STREAM_PROFILE_KEEP_MS),
|
|
107
110
|
MICME_STREAM_MAX_TOKENS: String(STREAM_PROFILE_MAX_TOKENS),
|
|
108
111
|
MICME_STREAM_WORDS_PER_CHUNK: String(STREAM_PROFILE_WORDS_PER_CHUNK),
|
|
109
|
-
|
|
112
|
+
MICME_STREAM_FLUSH_MS: String(STREAM_PROFILE_FLUSH_MS),
|
|
113
|
+
// whisper-stream's upstream default is no prompt carry-over. Keep it disabled for Micme's append-only stream profile so raw dictation is less likely to rewrite short chunks contextually.
|
|
114
|
+
MICME_STREAM_KEEP_CONTEXT: "0",
|
|
110
115
|
MICME_STREAM_FINALIZE_WITH_CLIP: "0",
|
|
111
116
|
MICME_STREAM_VAD_THRESHOLD: String(STREAM_PROFILE_VAD_THRESHOLD),
|
|
112
117
|
};
|
|
@@ -119,7 +124,8 @@ export function getTranscriptionModeProfile(mode: TranscriptionMode): Record<str
|
|
|
119
124
|
MICME_STREAM_KEEP_MS: String(DEFAULT_STREAM_KEEP_MS),
|
|
120
125
|
MICME_STREAM_MAX_TOKENS: String(DEFAULT_STREAM_MAX_TOKENS),
|
|
121
126
|
MICME_STREAM_WORDS_PER_CHUNK: "10",
|
|
122
|
-
|
|
127
|
+
MICME_STREAM_FLUSH_MS: String(DEFAULT_STREAM_FLUSH_MS),
|
|
128
|
+
MICME_STREAM_KEEP_CONTEXT: "0",
|
|
123
129
|
MICME_STREAM_FINALIZE_WITH_CLIP: "1",
|
|
124
130
|
MICME_STREAM_VAD_THRESHOLD: String(DEFAULT_STREAM_VAD_THRESHOLD),
|
|
125
131
|
};
|
|
@@ -149,6 +155,15 @@ export function getTranscriptionMode() {
|
|
|
149
155
|
return env("MICME_TRANSCRIPTION_MODE") === "stream" ? "stream" : "clip";
|
|
150
156
|
}
|
|
151
157
|
|
|
158
|
+
export function getTranscribeBackend(): TranscribeBackend {
|
|
159
|
+
const value = env("MICME_TRANSCRIBE_BACKEND")?.trim();
|
|
160
|
+
return isTranscribeBackend(value) ? value : DEFAULT_TRANSCRIBE_BACKEND;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function isTranscribeBackend(value: string | undefined): value is TranscribeBackend {
|
|
164
|
+
return value === "auto" || value === "whisper.cpp" || value === "python" || value === "custom";
|
|
165
|
+
}
|
|
166
|
+
|
|
152
167
|
export function getPrintableShortcuts() {
|
|
153
168
|
const configured = env("MICME_PRINTABLE_SHORTCUTS");
|
|
154
169
|
if (configured !== undefined) {
|
|
@@ -196,12 +211,17 @@ export function getStreamVadThreshold() {
|
|
|
196
211
|
|
|
197
212
|
export function getStreamKeepContext() {
|
|
198
213
|
const value = env("MICME_STREAM_KEEP_CONTEXT");
|
|
199
|
-
return value === undefined ?
|
|
214
|
+
return value === undefined ? false : /^(1|true|yes|on)$/i.test(value);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function getStreamFlushMs() {
|
|
218
|
+
const value = Number(env("MICME_STREAM_FLUSH_MS"));
|
|
219
|
+
return Number.isFinite(value) && value > 0 ? Math.round(value) : DEFAULT_STREAM_FLUSH_MS;
|
|
200
220
|
}
|
|
201
221
|
|
|
202
222
|
export function getStreamFinalizeWithClip() {
|
|
203
223
|
const value = env("MICME_STREAM_FINALIZE_WITH_CLIP");
|
|
204
|
-
return value === undefined ?
|
|
224
|
+
return value === undefined ? false : /^(1|true|yes|on)$/i.test(value);
|
|
205
225
|
}
|
|
206
226
|
|
|
207
227
|
export function getStreamWordsPerChunk() {
|
package/src/constants.ts
CHANGED
|
@@ -9,21 +9,25 @@ export const RECORDER_STARTUP_GRACE_MS = 700;
|
|
|
9
9
|
export const RECORDER_STOP_GRACE_MS = 3_000;
|
|
10
10
|
export const MIN_AUDIO_BYTES = 512;
|
|
11
11
|
export const MAX_CAPTURED_OUTPUT_CHARS = 100_000;
|
|
12
|
-
export const STREAM_MIN_INITIAL_WORDS =
|
|
12
|
+
export const STREAM_MIN_INITIAL_WORDS = 1;
|
|
13
13
|
export const DEFAULT_STREAM_STEP_MS = 1_000;
|
|
14
14
|
export const DEFAULT_STREAM_LENGTH_MS = 5_000;
|
|
15
15
|
export const DEFAULT_STREAM_KEEP_MS = 500;
|
|
16
16
|
export const DEFAULT_STREAM_MAX_TOKENS = 64;
|
|
17
17
|
export const DEFAULT_STREAM_VAD_THRESHOLD = 0.45;
|
|
18
|
+
export const DEFAULT_STREAM_FLUSH_MS = 700;
|
|
18
19
|
export const STREAM_PROFILE_STEP_MS = 500;
|
|
19
20
|
export const STREAM_PROFILE_LENGTH_MS = 3_000;
|
|
20
21
|
export const STREAM_PROFILE_KEEP_MS = 300;
|
|
21
22
|
export const STREAM_PROFILE_MAX_TOKENS = 32;
|
|
22
23
|
export const STREAM_PROFILE_WORDS_PER_CHUNK = 5;
|
|
23
24
|
export const STREAM_PROFILE_VAD_THRESHOLD = 0.35;
|
|
25
|
+
export const STREAM_PROFILE_FLUSH_MS = 650;
|
|
24
26
|
export const DEFAULT_MIN_MAX_VOLUME_DB = -50;
|
|
25
27
|
export const AUDIO_VALIDATION_TIMEOUT_MS = 30_000;
|
|
28
|
+
export const DEFAULT_TRANSCRIBE_BACKEND = "auto";
|
|
26
29
|
export const DEFAULT_WHISPER_CPP_MODEL_NAME = "small.en";
|
|
30
|
+
export const DEFAULT_PYTHON_WHISPER_MODEL_NAME = "base.en";
|
|
27
31
|
export const WHISPER_CPP_MODEL_BASE_URL = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main";
|
|
28
32
|
export const WHISPER_CPP_MODEL_NAMES = [
|
|
29
33
|
"tiny.en",
|
package/src/extension.ts
CHANGED
|
@@ -6,15 +6,15 @@ import { RECORDER_STARTUP_GRACE_MS, STATUS_KEY } from "./constants.ts";
|
|
|
6
6
|
import {
|
|
7
7
|
env,
|
|
8
8
|
envFlag,
|
|
9
|
-
expandConfigPath,
|
|
10
9
|
getShortcut,
|
|
11
10
|
getStreamFinalizeWithClip,
|
|
12
11
|
getTranscriptionMode,
|
|
13
12
|
reloadMicmeConfig,
|
|
14
13
|
} from "./config.ts";
|
|
15
|
-
import { buildRecorderCommand, listAudioDevices, prepareAudioForTranscription, validateRecordedAudio } from "./audio.ts";
|
|
14
|
+
import { buildRecorderCommand, listAudioDevices, prepareAudioForTranscription, registerDeviceMessageRenderer, validateRecordedAudio } from "./audio.ts";
|
|
16
15
|
import { installMicmeEditorFallback, type MicmeEditorInputHandlers } from "./editor.ts";
|
|
17
|
-
import {
|
|
16
|
+
import { resolveTranscriptionPlan, formatTranscriptionPlan } from "./backends.ts";
|
|
17
|
+
import { ensureWhisperCppModel } from "./models.ts";
|
|
18
18
|
import {
|
|
19
19
|
cleanup,
|
|
20
20
|
formatExit,
|
|
@@ -38,7 +38,7 @@ import {
|
|
|
38
38
|
showStreamingDiagnostics,
|
|
39
39
|
} from "./streaming.ts";
|
|
40
40
|
import { pasteOrSubmitTranscript } from "./transcript-delivery.ts";
|
|
41
|
-
import {
|
|
41
|
+
import { transcribe } from "./transcription.ts";
|
|
42
42
|
import type { Recording } from "./types.ts";
|
|
43
43
|
|
|
44
44
|
const MICME_ACTIONS = ["devices", "conf", "last", "audio", "help"] as const;
|
|
@@ -52,6 +52,7 @@ let lastShortcutInputAt = 0;
|
|
|
52
52
|
|
|
53
53
|
export default function micmeExtension(pi: ExtensionAPI) {
|
|
54
54
|
reloadMicmeConfig();
|
|
55
|
+
registerDeviceMessageRenderer(pi);
|
|
55
56
|
|
|
56
57
|
async function toggle(ctx: ExtensionContext) {
|
|
57
58
|
if (recording) {
|
|
@@ -77,7 +78,7 @@ export default function micmeExtension(pi: ExtensionAPI) {
|
|
|
77
78
|
await toggle(ctx);
|
|
78
79
|
return;
|
|
79
80
|
case "devices":
|
|
80
|
-
await listAudioDevices(ctx);
|
|
81
|
+
await listAudioDevices(ctx, pi);
|
|
81
82
|
return;
|
|
82
83
|
case "conf":
|
|
83
84
|
await showConfiguration(ctx);
|
|
@@ -239,19 +240,27 @@ async function stopAndTranscribe(ctx: ExtensionContext, pi: ExtensionAPI) {
|
|
|
239
240
|
}
|
|
240
241
|
|
|
241
242
|
async function startStreamingTranscription(ctx: ExtensionContext, stopHint = getShortcut()) {
|
|
242
|
-
const
|
|
243
|
-
if (!
|
|
244
|
-
throw new Error(
|
|
243
|
+
const plan = resolveTranscriptionPlan({ transcriptionMode: "stream" });
|
|
244
|
+
if (plan.effectiveBackend !== "whisper.cpp" || !plan.binary || !plan.modelPath) {
|
|
245
|
+
throw new Error(formatTranscriptionPlan(plan));
|
|
245
246
|
}
|
|
246
247
|
|
|
247
|
-
|
|
248
|
-
await ensureWhisperCppModel(whisperCppModel, ctx);
|
|
248
|
+
await ensureWhisperCppModel(plan.modelPath, ctx, { allowDownload: plan.modelDownloadable !== false });
|
|
249
249
|
|
|
250
250
|
const tempDir = await mkdtemp(join(tmpdir(), "micme-stream-"));
|
|
251
|
-
const command = buildWhisperStreamCommand(
|
|
251
|
+
const command = buildWhisperStreamCommand(plan.binary, plan.modelPath, tempDir);
|
|
252
252
|
const active = spawnRecording(command, "", tempDir);
|
|
253
253
|
const baseText = ctx.ui.getEditorText();
|
|
254
|
-
active.streaming = {
|
|
254
|
+
active.streaming = {
|
|
255
|
+
baseText,
|
|
256
|
+
previewText: baseText,
|
|
257
|
+
outputBuffer: "",
|
|
258
|
+
lastText: "",
|
|
259
|
+
emittedWords: [],
|
|
260
|
+
candidateWords: [],
|
|
261
|
+
lastHypothesisWords: [],
|
|
262
|
+
startedAt: Date.now(),
|
|
263
|
+
};
|
|
255
264
|
if (getStreamFinalizeWithClip()) {
|
|
256
265
|
const audioPath = join(tempDir, "raw.wav");
|
|
257
266
|
active.clipRecording = spawnRecording(buildRecorderCommand(audioPath), audioPath, tempDir);
|
|
@@ -334,10 +343,13 @@ async function stopStreamingTranscription(ctx: ExtensionContext, pi: ExtensionAP
|
|
|
334
343
|
keepTempDir = true;
|
|
335
344
|
}
|
|
336
345
|
} catch (error) {
|
|
346
|
+
flushPendingStreamingWords(ctx, state);
|
|
347
|
+
const liveTranscript = normalizeTranscript(getStreamingTranscript(state));
|
|
348
|
+
if (liveTranscript) lastTranscript = liveTranscript;
|
|
337
349
|
lastAudioDir = active.tempDir;
|
|
338
350
|
keepTempDir = true;
|
|
339
351
|
const message = error instanceof Error ? error.message : String(error);
|
|
340
|
-
ctx.ui.notify(`Micme kept the live stream
|
|
352
|
+
ctx.ui.notify(`Micme kept the live append-only stream transcript because final clip transcription failed: ${message}\nAudio kept for debugging: ${active.tempDir}`, "warning");
|
|
341
353
|
}
|
|
342
354
|
}
|
|
343
355
|
|
|
@@ -345,7 +357,4 @@ async function stopStreamingTranscription(ctx: ExtensionContext, pi: ExtensionAP
|
|
|
345
357
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
346
358
|
}
|
|
347
359
|
|
|
348
|
-
function getConfiguredWhisperCppModel() {
|
|
349
|
-
return env("MICME_WHISPER_CPP_MODEL") || getDefaultWhisperCppModelPath();
|
|
350
|
-
}
|
|
351
360
|
|
package/src/models.ts
CHANGED
|
@@ -6,13 +6,16 @@ import { homedir } from "node:os";
|
|
|
6
6
|
import { basename, dirname, join } from "node:path";
|
|
7
7
|
import { finished } from "node:stream/promises";
|
|
8
8
|
import {
|
|
9
|
+
DEFAULT_PYTHON_WHISPER_MODEL_NAME,
|
|
9
10
|
DEFAULT_WHISPER_CPP_MODEL_NAME,
|
|
11
|
+
PYTHON_WHISPER_MODEL_NAMES,
|
|
10
12
|
STATUS_KEY,
|
|
11
13
|
WHISPER_CPP_MODEL_BASE_URL,
|
|
12
14
|
WHISPER_CPP_MODEL_NAMES,
|
|
13
15
|
} from "./constants.ts";
|
|
14
16
|
import { env, expandConfigPath } from "./config.ts";
|
|
15
|
-
import
|
|
17
|
+
import { findExecutable, runProcess } from "./processes.ts";
|
|
18
|
+
import type { ModelCandidate, ResolvedWhisperCppModel } from "./types.ts";
|
|
16
19
|
|
|
17
20
|
const modelDownloads = new Map<string, Promise<void>>();
|
|
18
21
|
|
|
@@ -32,23 +35,25 @@ export function discoverWhisperCppModels(cwd: string): ModelCandidate[] {
|
|
|
32
35
|
add({
|
|
33
36
|
label: basename(expanded),
|
|
34
37
|
value: expanded,
|
|
35
|
-
description: existsSync(expanded) ? `current
|
|
38
|
+
description: existsSync(expanded) ? `current explicit path: ${expanded}` : `current explicit path is missing: ${expanded}`,
|
|
36
39
|
installed: existsSync(expanded),
|
|
40
|
+
kind: "path",
|
|
37
41
|
});
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
for (const file of scanModelFiles(cwd)) {
|
|
41
|
-
add({ label: basename(file), value: file, description: file, installed: true });
|
|
45
|
+
add({ label: basename(file), value: file, description: file, installed: true, kind: "path" });
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
const cacheDir = getWhisperCppModelCacheDir();
|
|
45
49
|
for (const modelName of WHISPER_CPP_MODEL_NAMES) {
|
|
46
50
|
const path = join(cacheDir, `ggml-${modelName}.bin`);
|
|
47
51
|
add({
|
|
48
|
-
label:
|
|
52
|
+
label: modelName,
|
|
49
53
|
value: path,
|
|
50
|
-
description: `${describeWhisperModel(modelName)}${existsSync(path) ? "" : " (expected path; download model first)"}`,
|
|
54
|
+
description: `${describeWhisperModel(modelName)} • ${basename(path)}${existsSync(path) ? "" : " (expected path; download model first)"}`,
|
|
51
55
|
installed: existsSync(path),
|
|
56
|
+
kind: "model-name",
|
|
52
57
|
});
|
|
53
58
|
}
|
|
54
59
|
|
|
@@ -111,8 +116,80 @@ export function describeWhisperModel(name: string) {
|
|
|
111
116
|
return "Whisper model";
|
|
112
117
|
}
|
|
113
118
|
|
|
114
|
-
export
|
|
119
|
+
export function resolveWhisperCppModel(): ResolvedWhisperCppModel {
|
|
120
|
+
const explicitPath = env("MICME_WHISPER_CPP_MODEL")?.trim();
|
|
121
|
+
if (explicitPath) {
|
|
122
|
+
const path = expandConfigPath(explicitPath);
|
|
123
|
+
return {
|
|
124
|
+
path,
|
|
125
|
+
modelName: getWhisperCppModelNameFromPath(path),
|
|
126
|
+
source: "explicit-path",
|
|
127
|
+
configuredValue: explicitPath,
|
|
128
|
+
exists: existsSync(path),
|
|
129
|
+
downloadable: isDownloadableWhisperCppModelPath(path),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const configuredName = env("MICME_DEFAULT_WHISPER_CPP_MODEL")?.trim();
|
|
134
|
+
const modelName = configuredName || DEFAULT_WHISPER_CPP_MODEL_NAME;
|
|
135
|
+
const path = join(getWhisperCppModelCacheDir(), `ggml-${modelName}.bin`);
|
|
136
|
+
return {
|
|
137
|
+
path,
|
|
138
|
+
modelName,
|
|
139
|
+
source: configuredName ? "configured-name" : "default-name",
|
|
140
|
+
configuredValue: configuredName,
|
|
141
|
+
exists: existsSync(path),
|
|
142
|
+
downloadable: isDownloadableWhisperCppModelPath(path),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function getPythonWhisperModelName() {
|
|
147
|
+
return env("MICME_WHISPER_MODEL")?.trim() || DEFAULT_PYTHON_WHISPER_MODEL_NAME;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function discoverPythonWhisperModels(): Promise<ModelCandidate[]> {
|
|
151
|
+
const dynamicNames = await queryPythonWhisperModelNames();
|
|
152
|
+
const names = dynamicNames.length > 0 ? dynamicNames : [...PYTHON_WHISPER_MODEL_NAMES];
|
|
153
|
+
const source = dynamicNames.length > 0 ? "reported by whisper.available_models()" : "built-in fallback list";
|
|
154
|
+
return names.map((name) => ({
|
|
155
|
+
label: name,
|
|
156
|
+
value: name,
|
|
157
|
+
description: `${describeWhisperModel(name)} • ${source}`,
|
|
158
|
+
installed: dynamicNames.length > 0,
|
|
159
|
+
kind: "model-name",
|
|
160
|
+
}));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function queryPythonWhisperModelNames(): Promise<string[]> {
|
|
164
|
+
const python = findExecutable(["python3", "python"]);
|
|
165
|
+
if (!python) return [];
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
const result = await runProcess(python, ["-c", "import whisper; print('\\n'.join(whisper.available_models()))"], 2_000);
|
|
169
|
+
if (result.code !== 0 || result.timedOut) return [];
|
|
170
|
+
return uniqueModelNames(result.stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean));
|
|
171
|
+
} catch {
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function uniqueModelNames(names: string[]) {
|
|
177
|
+
const seen = new Set<string>();
|
|
178
|
+
const output: string[] = [];
|
|
179
|
+
for (const name of names) {
|
|
180
|
+
const normalized = name.trim();
|
|
181
|
+
if (!normalized || seen.has(normalized)) continue;
|
|
182
|
+
seen.add(normalized);
|
|
183
|
+
output.push(normalized);
|
|
184
|
+
}
|
|
185
|
+
return output;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export async function ensureWhisperCppModel(modelPath: string, ctx?: ExtensionContext, options: { allowDownload?: boolean } = {}) {
|
|
115
189
|
if (existsSync(modelPath)) return;
|
|
190
|
+
if (options.allowDownload === false) {
|
|
191
|
+
throw new Error(`Micme model is missing: ${modelPath}`);
|
|
192
|
+
}
|
|
116
193
|
if (env("MICME_AUTO_DOWNLOAD_MODEL") === "0") {
|
|
117
194
|
throw new Error(`Micme model is missing and auto-download is disabled: ${modelPath}`);
|
|
118
195
|
}
|
|
@@ -128,9 +205,9 @@ export async function ensureWhisperCppModel(modelPath: string, ctx?: ExtensionCo
|
|
|
128
205
|
return;
|
|
129
206
|
}
|
|
130
207
|
|
|
131
|
-
const modelName =
|
|
208
|
+
const modelName = getDownloadableWhisperCppModelName(modelPath);
|
|
132
209
|
if (!modelName) {
|
|
133
|
-
throw new Error(`Micme model is missing and cannot infer download URL: ${modelPath}`);
|
|
210
|
+
throw new Error(`Micme model is missing and cannot infer a standard download URL: ${modelPath}`);
|
|
134
211
|
}
|
|
135
212
|
|
|
136
213
|
const download = downloadWhisperCppModel(modelName, modelPath, ctx);
|
|
@@ -204,6 +281,20 @@ export function getWhisperCppModelNameFromPath(modelPath: string) {
|
|
|
204
281
|
return match?.[1];
|
|
205
282
|
}
|
|
206
283
|
|
|
284
|
+
export function getDownloadableWhisperCppModelName(modelPath: string) {
|
|
285
|
+
const match = basename(modelPath).match(/^ggml-(.+)\.bin$/i);
|
|
286
|
+
const modelName = match?.[1];
|
|
287
|
+
return modelName && isKnownWhisperCppModelName(modelName) ? modelName : undefined;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function isDownloadableWhisperCppModelPath(modelPath: string) {
|
|
291
|
+
return getDownloadableWhisperCppModelName(modelPath) !== undefined;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function isKnownWhisperCppModelName(modelName: string): modelName is (typeof WHISPER_CPP_MODEL_NAMES)[number] {
|
|
295
|
+
return WHISPER_CPP_MODEL_NAMES.includes(modelName as (typeof WHISPER_CPP_MODEL_NAMES)[number]);
|
|
296
|
+
}
|
|
297
|
+
|
|
207
298
|
export function getWhisperCppModelUrl(modelName: string) {
|
|
208
299
|
return `${WHISPER_CPP_MODEL_BASE_URL}/ggml-${modelName}.bin`;
|
|
209
300
|
}
|
|
@@ -213,7 +304,7 @@ export function getWhisperCppModelCacheDir() {
|
|
|
213
304
|
}
|
|
214
305
|
|
|
215
306
|
export function getDefaultWhisperCppModelPath() {
|
|
216
|
-
const modelName = env("MICME_DEFAULT_WHISPER_CPP_MODEL") || DEFAULT_WHISPER_CPP_MODEL_NAME;
|
|
307
|
+
const modelName = env("MICME_DEFAULT_WHISPER_CPP_MODEL")?.trim() || DEFAULT_WHISPER_CPP_MODEL_NAME;
|
|
217
308
|
return join(getWhisperCppModelCacheDir(), `ggml-${modelName}.bin`);
|
|
218
309
|
}
|
|
219
310
|
|