@senad-d/micme 0.2.0 → 0.3.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 +3 -0
- package/README.md +33 -11
- package/SECURITY.md +3 -3
- package/micme.example.json +5 -2
- package/micme.schema.json +48 -4
- package/package.json +17 -6
- package/scripts/check-package-contents.mjs +1 -0
- package/scripts/doctor.mjs +92 -0
- package/src/audio.ts +64 -21
- package/src/backends.ts +249 -0
- package/src/config.ts +80 -12
- package/src/constants.ts +3 -1
- package/src/extension.ts +27 -28
- package/src/models.ts +100 -9
- package/src/recording-dir.ts +54 -0
- package/src/settings.ts +237 -69
- package/src/transcription.ts +32 -48
- package/src/types.ts +27 -0
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,
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
DEFAULT_STREAM_STEP_MS,
|
|
15
15
|
DEFAULT_STREAM_VAD_THRESHOLD,
|
|
16
16
|
DEFAULT_STREAM_FLUSH_MS,
|
|
17
|
+
DEFAULT_TRANSCRIBE_BACKEND,
|
|
17
18
|
DEFAULT_TRANSCRIBE_SAMPLE_RATE,
|
|
18
19
|
DEFAULT_TRANSCRIBE_TIMEOUT_MS,
|
|
19
20
|
DEFAULT_MIN_MAX_VOLUME_DB,
|
|
@@ -74,7 +75,7 @@ export async function writeMicmeConfigValue(key: string, value: string) {
|
|
|
74
75
|
await writeMicmeConfigValues({ [key]: value });
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
export async function writeMicmeConfigValues(values: Record<string, string>) {
|
|
78
|
+
export async function writeMicmeConfigValues(values: Record<string, string | undefined>) {
|
|
78
79
|
for (const key of Object.keys(values)) {
|
|
79
80
|
if (!key.startsWith("MICME_")) throw new Error(`Micme config keys must start with MICME_: ${key}`);
|
|
80
81
|
}
|
|
@@ -84,7 +85,13 @@ export async function writeMicmeConfigValues(values: Record<string, string>) {
|
|
|
84
85
|
const existing = readMicmeJsonObjectForWrite(configPath);
|
|
85
86
|
const next: JsonObject = { ...existing };
|
|
86
87
|
|
|
87
|
-
for (const [key, value] of Object.entries(values))
|
|
88
|
+
for (const [key, value] of Object.entries(values)) {
|
|
89
|
+
if (value === undefined) {
|
|
90
|
+
delete next[key];
|
|
91
|
+
} else {
|
|
92
|
+
next[key] = String(value);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
88
95
|
|
|
89
96
|
await mkdir(configDir, { recursive: true });
|
|
90
97
|
const tempPath = join(configDir, `.micme.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`);
|
|
@@ -146,22 +153,47 @@ export function env(name: string) {
|
|
|
146
153
|
return process.env[name] ?? micmeConfigState.values[name];
|
|
147
154
|
}
|
|
148
155
|
|
|
156
|
+
export function getShortcutSettingValue() {
|
|
157
|
+
const shortcut = env("MICME_SHORTCUT");
|
|
158
|
+
if (shortcut !== undefined) return shortcut;
|
|
159
|
+
|
|
160
|
+
const legacyPrintableShortcut = firstShortcutValue(env("MICME_PRINTABLE_SHORTCUTS"));
|
|
161
|
+
return legacyPrintableShortcut || DEFAULT_SHORTCUT;
|
|
162
|
+
}
|
|
163
|
+
|
|
149
164
|
export function getShortcut() {
|
|
150
|
-
return
|
|
165
|
+
return getShortcutSettingValue() || "not set";
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function getTerminalShortcut() {
|
|
169
|
+
const shortcut = env("MICME_SHORTCUT");
|
|
170
|
+
if (shortcut !== undefined) return isTerminalShortcut(shortcut) ? shortcut.trim() : undefined;
|
|
171
|
+
return DEFAULT_SHORTCUT;
|
|
151
172
|
}
|
|
152
173
|
|
|
153
174
|
export function getTranscriptionMode() {
|
|
154
175
|
return env("MICME_TRANSCRIPTION_MODE") === "stream" ? "stream" : "clip";
|
|
155
176
|
}
|
|
156
177
|
|
|
178
|
+
export function getTranscribeBackend(): TranscribeBackend {
|
|
179
|
+
const value = env("MICME_TRANSCRIBE_BACKEND")?.trim();
|
|
180
|
+
return isTranscribeBackend(value) ? value : DEFAULT_TRANSCRIBE_BACKEND;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function isTranscribeBackend(value: string | undefined): value is TranscribeBackend {
|
|
184
|
+
return value === "auto" || value === "whisper.cpp" || value === "python" || value === "custom";
|
|
185
|
+
}
|
|
186
|
+
|
|
157
187
|
export function getPrintableShortcuts() {
|
|
158
|
-
const
|
|
159
|
-
if (
|
|
160
|
-
return
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
.filter(Boolean);
|
|
188
|
+
const shortcut = env("MICME_SHORTCUT");
|
|
189
|
+
if (shortcut !== undefined) {
|
|
190
|
+
if (shortcut && !isTerminalShortcut(shortcut)) return splitShortcutValues(shortcut);
|
|
191
|
+
const legacyConfigured = env("MICME_PRINTABLE_SHORTCUTS");
|
|
192
|
+
return legacyConfigured !== undefined ? splitShortcutValues(legacyConfigured) : [];
|
|
164
193
|
}
|
|
194
|
+
|
|
195
|
+
const legacyConfigured = env("MICME_PRINTABLE_SHORTCUTS");
|
|
196
|
+
if (legacyConfigured !== undefined) return splitShortcutValues(legacyConfigured);
|
|
165
197
|
return process.platform === "darwin" ? [DEFAULT_MACOS_PRINTABLE_SHORTCUT] : [];
|
|
166
198
|
}
|
|
167
199
|
|
|
@@ -169,6 +201,22 @@ export function matchesPrintableMicmeShortcut(data: string) {
|
|
|
169
201
|
return getPrintableShortcuts().includes(data);
|
|
170
202
|
}
|
|
171
203
|
|
|
204
|
+
export function isTerminalShortcut(value: string) {
|
|
205
|
+
const normalized = value.trim();
|
|
206
|
+
return /^[\x20-\x7E]$/.test(normalized) || /^(?:escape|esc|enter|return|tab|space|backspace|delete|insert|clear|home|end|pageUp|pageDown|up|down|left|right|f\d{1,2}|(?:(?:ctrl|control|alt|option|meta|cmd|command|shift|super)\+)+.+)$/i.test(normalized);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function firstShortcutValue(value: string | undefined) {
|
|
210
|
+
return splitShortcutValues(value ?? "")[0] ?? "";
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function splitShortcutValues(value: string) {
|
|
214
|
+
return value
|
|
215
|
+
.split(",")
|
|
216
|
+
.map((candidate) => candidate.trim())
|
|
217
|
+
.filter(Boolean);
|
|
218
|
+
}
|
|
219
|
+
|
|
172
220
|
export function getTranscribeTimeoutMs() {
|
|
173
221
|
const value = Number(env("MICME_TRANSCRIBE_TIMEOUT_MS"));
|
|
174
222
|
return Number.isFinite(value) && value > 0 ? value : DEFAULT_TRANSCRIBE_TIMEOUT_MS;
|
|
@@ -220,8 +268,10 @@ export function getStreamWordsPerChunk() {
|
|
|
220
268
|
}
|
|
221
269
|
|
|
222
270
|
export function getRecordSampleRate() {
|
|
223
|
-
const
|
|
224
|
-
|
|
271
|
+
const configured = env("MICME_RECORD_SAMPLE_RATE")?.trim();
|
|
272
|
+
if (!configured || /^auto$/i.test(configured)) return undefined;
|
|
273
|
+
const value = Number(configured);
|
|
274
|
+
return Number.isFinite(value) && value > 0 ? Math.round(value) : undefined;
|
|
225
275
|
}
|
|
226
276
|
|
|
227
277
|
export function getTranscribeSampleRate() {
|
|
@@ -229,6 +279,24 @@ export function getTranscribeSampleRate() {
|
|
|
229
279
|
return Number.isFinite(value) && value > 0 ? Math.round(value) : DEFAULT_TRANSCRIBE_SAMPLE_RATE;
|
|
230
280
|
}
|
|
231
281
|
|
|
282
|
+
export function getRecordMeter() {
|
|
283
|
+
return envFlag("MICME_RECORD_METER");
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function getRecordSync() {
|
|
287
|
+
const value = env("MICME_RECORD_SYNC");
|
|
288
|
+
return value === undefined ? true : /^(1|true|yes|on)$/i.test(value);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function getAvfoundationDropLateFrames() {
|
|
292
|
+
return envFlag("MICME_AVFOUNDATION_DROP_LATE_FRAMES");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function getAvfoundationInputSampleRate() {
|
|
296
|
+
const value = Number(env("MICME_AVFOUNDATION_INPUT_SAMPLE_RATE"));
|
|
297
|
+
return Number.isFinite(value) && value > 0 ? Math.round(value) : undefined;
|
|
298
|
+
}
|
|
299
|
+
|
|
232
300
|
export function getAudioFilter() {
|
|
233
301
|
if (env("MICME_AUDIO_FILTER") !== undefined) return env("MICME_AUDIO_FILTER")!.trim();
|
|
234
302
|
return "highpass=f=80,lowpass=f=7600";
|
package/src/constants.ts
CHANGED
|
@@ -3,7 +3,7 @@ export const RECORDING_WIDGET_KEY = "micme-recording";
|
|
|
3
3
|
export const DEFAULT_SHORTCUT = "alt+m";
|
|
4
4
|
export const DEFAULT_MACOS_PRINTABLE_SHORTCUT = "§";
|
|
5
5
|
export const DEFAULT_TRANSCRIBE_TIMEOUT_MS = 120_000;
|
|
6
|
-
export const DEFAULT_RECORD_SAMPLE_RATE =
|
|
6
|
+
export const DEFAULT_RECORD_SAMPLE_RATE = "auto";
|
|
7
7
|
export const DEFAULT_TRANSCRIBE_SAMPLE_RATE = 16_000;
|
|
8
8
|
export const RECORDER_STARTUP_GRACE_MS = 700;
|
|
9
9
|
export const RECORDER_STOP_GRACE_MS = 3_000;
|
|
@@ -25,7 +25,9 @@ export const STREAM_PROFILE_VAD_THRESHOLD = 0.35;
|
|
|
25
25
|
export const STREAM_PROFILE_FLUSH_MS = 650;
|
|
26
26
|
export const DEFAULT_MIN_MAX_VOLUME_DB = -50;
|
|
27
27
|
export const AUDIO_VALIDATION_TIMEOUT_MS = 30_000;
|
|
28
|
+
export const DEFAULT_TRANSCRIBE_BACKEND = "auto";
|
|
28
29
|
export const DEFAULT_WHISPER_CPP_MODEL_NAME = "small.en";
|
|
30
|
+
export const DEFAULT_PYTHON_WHISPER_MODEL_NAME = "base.en";
|
|
29
31
|
export const WHISPER_CPP_MODEL_BASE_URL = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main";
|
|
30
32
|
export const WHISPER_CPP_MODEL_NAMES = [
|
|
31
33
|
"tiny.en",
|
package/src/extension.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { mkdtemp } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
2
|
import { join } from "node:path";
|
|
5
3
|
import { RECORDER_STARTUP_GRACE_MS, STATUS_KEY } from "./constants.ts";
|
|
6
4
|
import {
|
|
7
|
-
env,
|
|
8
5
|
envFlag,
|
|
9
|
-
expandConfigPath,
|
|
10
6
|
getShortcut,
|
|
11
7
|
getStreamFinalizeWithClip,
|
|
8
|
+
getTerminalShortcut,
|
|
12
9
|
getTranscriptionMode,
|
|
13
10
|
reloadMicmeConfig,
|
|
14
11
|
} from "./config.ts";
|
|
15
12
|
import { buildRecorderCommand, listAudioDevices, prepareAudioForTranscription, registerDeviceMessageRenderer, validateRecordedAudio } from "./audio.ts";
|
|
16
13
|
import { installMicmeEditorFallback, type MicmeEditorInputHandlers } from "./editor.ts";
|
|
17
|
-
import {
|
|
14
|
+
import { resolveTranscriptionPlan, formatTranscriptionPlan } from "./backends.ts";
|
|
15
|
+
import { ensureWhisperCppModel } from "./models.ts";
|
|
18
16
|
import {
|
|
19
17
|
cleanup,
|
|
20
18
|
formatExit,
|
|
@@ -25,6 +23,7 @@ import {
|
|
|
25
23
|
stopRecorder,
|
|
26
24
|
} from "./processes.ts";
|
|
27
25
|
import { clearRecordingWidget, startRecordingWidget } from "./recording-widget.ts";
|
|
26
|
+
import { createRecordingDirectory } from "./recording-dir.ts";
|
|
28
27
|
import { showConfiguration } from "./settings.ts";
|
|
29
28
|
import {
|
|
30
29
|
buildWhisperStreamCommand,
|
|
@@ -38,7 +37,7 @@ import {
|
|
|
38
37
|
showStreamingDiagnostics,
|
|
39
38
|
} from "./streaming.ts";
|
|
40
39
|
import { pasteOrSubmitTranscript } from "./transcript-delivery.ts";
|
|
41
|
-
import {
|
|
40
|
+
import { transcribe } from "./transcription.ts";
|
|
42
41
|
import type { Recording } from "./types.ts";
|
|
43
42
|
|
|
44
43
|
const MICME_ACTIONS = ["devices", "conf", "last", "audio", "help"] as const;
|
|
@@ -101,17 +100,20 @@ export default function micmeExtension(pi: ExtensionAPI) {
|
|
|
101
100
|
},
|
|
102
101
|
});
|
|
103
102
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
103
|
+
const terminalShortcut = getTerminalShortcut();
|
|
104
|
+
if (terminalShortcut) {
|
|
105
|
+
pi.registerShortcut(terminalShortcut as Parameters<ExtensionAPI["registerShortcut"]>[0], {
|
|
106
|
+
description: "Micme: toggle local voice recording",
|
|
107
|
+
handler: async (ctx) => {
|
|
108
|
+
try {
|
|
109
|
+
if (isShortcutAutoRepeat()) return;
|
|
110
|
+
await toggle(ctx);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
handleExtensionError(ctx, error);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}
|
|
115
117
|
|
|
116
118
|
pi.on("session_start", async (_event, ctx) => {
|
|
117
119
|
reloadMicmeConfig();
|
|
@@ -163,13 +165,14 @@ function getHelpText() {
|
|
|
163
165
|
|
|
164
166
|
async function startRecording(ctx: ExtensionContext, stopHint = getShortcut()) {
|
|
165
167
|
if (recording) return;
|
|
168
|
+
reloadMicmeConfig();
|
|
166
169
|
|
|
167
170
|
if (getTranscriptionMode() === "stream") {
|
|
168
171
|
await startStreamingTranscription(ctx, stopHint);
|
|
169
172
|
return;
|
|
170
173
|
}
|
|
171
174
|
|
|
172
|
-
const tempDir = await
|
|
175
|
+
const tempDir = await createRecordingDirectory(ctx.cwd, envFlag("MICME_KEEP_AUDIO"), "micme-");
|
|
173
176
|
const audioPath = join(tempDir, "raw.wav");
|
|
174
177
|
const command = buildRecorderCommand(audioPath);
|
|
175
178
|
const active = spawnRecording(command, audioPath, tempDir);
|
|
@@ -240,16 +243,15 @@ async function stopAndTranscribe(ctx: ExtensionContext, pi: ExtensionAPI) {
|
|
|
240
243
|
}
|
|
241
244
|
|
|
242
245
|
async function startStreamingTranscription(ctx: ExtensionContext, stopHint = getShortcut()) {
|
|
243
|
-
const
|
|
244
|
-
if (!
|
|
245
|
-
throw new Error(
|
|
246
|
+
const plan = resolveTranscriptionPlan({ transcriptionMode: "stream" });
|
|
247
|
+
if (plan.effectiveBackend !== "whisper.cpp" || !plan.binary || !plan.modelPath) {
|
|
248
|
+
throw new Error(formatTranscriptionPlan(plan));
|
|
246
249
|
}
|
|
247
250
|
|
|
248
|
-
|
|
249
|
-
await ensureWhisperCppModel(whisperCppModel, ctx);
|
|
251
|
+
await ensureWhisperCppModel(plan.modelPath, ctx, { allowDownload: plan.modelDownloadable !== false });
|
|
250
252
|
|
|
251
|
-
const tempDir = await
|
|
252
|
-
const command = buildWhisperStreamCommand(
|
|
253
|
+
const tempDir = await createRecordingDirectory(ctx.cwd, envFlag("MICME_KEEP_AUDIO"), "micme-stream-");
|
|
254
|
+
const command = buildWhisperStreamCommand(plan.binary, plan.modelPath, tempDir);
|
|
253
255
|
const active = spawnRecording(command, "", tempDir);
|
|
254
256
|
const baseText = ctx.ui.getEditorText();
|
|
255
257
|
active.streaming = {
|
|
@@ -358,7 +360,4 @@ async function stopStreamingTranscription(ctx: ExtensionContext, pi: ExtensionAP
|
|
|
358
360
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
359
361
|
}
|
|
360
362
|
|
|
361
|
-
function getConfiguredWhisperCppModel() {
|
|
362
|
-
return env("MICME_WHISPER_CPP_MODEL") || getDefaultWhisperCppModelPath();
|
|
363
|
-
}
|
|
364
363
|
|