@senad-d/micme 0.3.5 → 0.3.7
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 +20 -5
- package/micme.example.json +7 -7
- package/micme.schema.json +117 -9
- package/package.json +8 -7
- package/scripts/doctor.mjs +57 -258
- package/src/audio.ts +59 -23
- package/src/config.ts +216 -52
- package/src/constants.ts +25 -0
- package/src/editor.ts +117 -66
- package/src/extension.ts +411 -188
- package/src/models.ts +208 -51
- package/src/processes.ts +104 -28
- package/src/settings.ts +53 -35
- package/src/streaming.ts +2 -2
- package/src/transcription.ts +10 -10
- package/src/types.ts +9 -1
- package/tsconfig.json +1 -1
package/src/extension.ts
CHANGED
|
@@ -8,12 +8,14 @@ import {
|
|
|
8
8
|
getTerminalShortcut,
|
|
9
9
|
getTranscriptionMode,
|
|
10
10
|
reloadMicmeConfig,
|
|
11
|
+
requireValidMicmeConfig,
|
|
11
12
|
} from "./config.ts";
|
|
12
13
|
import { buildRecorderCommand, listAudioDevices, prepareAudioForTranscription, registerDeviceMessageRenderer, validateRecordedAudio } from "./audio.ts";
|
|
13
14
|
import { installMicmeEditorFallback, type MicmeEditorInputHandlers } from "./editor.ts";
|
|
14
15
|
import { resolveTranscriptionPlan, formatTranscriptionPlan } from "./backends.ts";
|
|
15
16
|
import { ensureWhisperCppModel } from "./models.ts";
|
|
16
17
|
import {
|
|
18
|
+
assertExpectedProcessExit,
|
|
17
19
|
cleanup,
|
|
18
20
|
formatExit,
|
|
19
21
|
formatProcessOutput,
|
|
@@ -43,106 +45,259 @@ import { transcribe } from "./transcription.ts";
|
|
|
43
45
|
import type { Recording, ResolvedTranscriptionPlan } from "./types.ts";
|
|
44
46
|
|
|
45
47
|
const MICME_ACTIONS = ["devices", "conf", "last", "audio", "help"] as const;
|
|
46
|
-
|
|
47
48
|
const SHORTCUT_REPEAT_GUARD_MS = 1_000;
|
|
49
|
+
const GUARDED_UI_METHODS = new Set(["notify", "pasteToEditor", "setEditorText", "setStatus", "setWidget"]);
|
|
48
50
|
|
|
49
51
|
type StreamingTranscriptionPlan = ResolvedTranscriptionPlan & { effectiveBackend: "whisper.cpp"; binary: string; modelPath: string };
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
export type MicmeOperationPhase = "idle" | "starting" | "recording" | "streaming" | "stopping" | "transcribing" | "finalizing" | "shutting_down";
|
|
53
|
+
|
|
54
|
+
type MicmeOperation = {
|
|
55
|
+
id: number;
|
|
56
|
+
phase: Exclude<MicmeOperationPhase, "idle">;
|
|
57
|
+
context: ExtensionContext;
|
|
58
|
+
controller: AbortController;
|
|
59
|
+
processes: Set<Recording>;
|
|
60
|
+
tempDirs: Set<string>;
|
|
61
|
+
completion?: Promise<void>;
|
|
62
|
+
};
|
|
55
63
|
|
|
56
64
|
export default function micmeExtension(pi: ExtensionAPI) {
|
|
65
|
+
registerMicmeExtension(pi);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function registerMicmeExtension(pi: ExtensionAPI) {
|
|
69
|
+
const owner = new MicmeSessionOwner(pi);
|
|
57
70
|
reloadMicmeConfig();
|
|
58
71
|
registerDeviceMessageRenderer(pi);
|
|
59
72
|
|
|
60
|
-
async function toggle(ctx: ExtensionContext) {
|
|
61
|
-
if (recording) {
|
|
62
|
-
if (isStillStarting(recording)) return;
|
|
63
|
-
await stopAndTranscribe(ctx, pi);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
await startRecording(ctx);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
73
|
pi.registerCommand("micme", {
|
|
70
74
|
description: "Toggle local voice recording and paste the transcript into the editor",
|
|
71
|
-
getArgumentCompletions:
|
|
72
|
-
|
|
73
|
-
return matches.length ? matches.map((value) => ({ value, label: value })) : null;
|
|
74
|
-
},
|
|
75
|
-
handler: async (args, ctx) => {
|
|
76
|
-
const action = args.trim().split(/\s+/, 1)[0]?.toLowerCase() ?? "";
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
switch (action) {
|
|
80
|
-
case "":
|
|
81
|
-
await toggle(ctx);
|
|
82
|
-
return;
|
|
83
|
-
case "devices":
|
|
84
|
-
await listAudioDevices(ctx, pi);
|
|
85
|
-
return;
|
|
86
|
-
case "conf":
|
|
87
|
-
await showConfiguration(ctx);
|
|
88
|
-
return;
|
|
89
|
-
case "last":
|
|
90
|
-
await pasteOrSubmitTranscript(ctx, pi, lastTranscript);
|
|
91
|
-
return;
|
|
92
|
-
case "audio":
|
|
93
|
-
ctx.ui.notify(lastAudioDir ? `Last Micme audio directory: ${sanitizeTerminalOutput(lastAudioDir)}` : "No kept Micme audio yet. Set MICME_KEEP_AUDIO=1.", "info");
|
|
94
|
-
return;
|
|
95
|
-
case "help":
|
|
96
|
-
ctx.ui.notify(getHelpText(), "info");
|
|
97
|
-
return;
|
|
98
|
-
default:
|
|
99
|
-
ctx.ui.notify(`Unknown micme action: ${sanitizeTerminalOutput(action) || "(empty)"}. Try /micme help`, "warning");
|
|
100
|
-
}
|
|
101
|
-
} catch (error) {
|
|
102
|
-
handleExtensionError(ctx, error);
|
|
103
|
-
}
|
|
104
|
-
},
|
|
75
|
+
getArgumentCompletions: getMicmeArgumentCompletions,
|
|
76
|
+
handler: async (args, ctx) => handleMicmeCommand(owner, pi, args, ctx),
|
|
105
77
|
});
|
|
106
78
|
|
|
107
79
|
const terminalShortcut = getTerminalShortcut();
|
|
108
80
|
if (terminalShortcut) {
|
|
109
81
|
pi.registerShortcut(terminalShortcut as Parameters<ExtensionAPI["registerShortcut"]>[0], {
|
|
110
82
|
description: "Micme: toggle local voice recording",
|
|
111
|
-
handler: async (ctx) =>
|
|
112
|
-
try {
|
|
113
|
-
if (isShortcutAutoRepeat()) return;
|
|
114
|
-
await toggle(ctx);
|
|
115
|
-
} catch (error) {
|
|
116
|
-
handleExtensionError(ctx, error);
|
|
117
|
-
}
|
|
118
|
-
},
|
|
83
|
+
handler: async (ctx) => handleMicmeTerminalShortcut(owner, ctx),
|
|
119
84
|
});
|
|
120
85
|
}
|
|
121
86
|
|
|
122
|
-
pi.on("session_start", async (_event, ctx) =>
|
|
87
|
+
pi.on("session_start", async (_event, ctx) => owner.startSession(ctx));
|
|
88
|
+
pi.on("session_shutdown", async (_event, ctx) => owner.shutdown(ctx));
|
|
89
|
+
return owner;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export class MicmeSessionOwner {
|
|
93
|
+
private current: MicmeOperation | undefined;
|
|
94
|
+
private closed = false;
|
|
95
|
+
private nextOperationId = 1;
|
|
96
|
+
private lastShortcutInputAt = 0;
|
|
97
|
+
private lastTranscript = "";
|
|
98
|
+
private lastAudioDir = "";
|
|
99
|
+
private readonly pi: ExtensionAPI;
|
|
100
|
+
|
|
101
|
+
constructor(pi: ExtensionAPI) {
|
|
102
|
+
this.pi = pi;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getPhase(): MicmeOperationPhase {
|
|
106
|
+
if (this.closed) return "shutting_down";
|
|
107
|
+
return this.current?.phase ?? "idle";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
startSession(ctx: ExtensionContext) {
|
|
123
111
|
reloadMicmeConfig();
|
|
124
|
-
installMicmeEditorFallback(ctx, createMicmeEditorHandlers(
|
|
125
|
-
}
|
|
112
|
+
installMicmeEditorFallback(ctx, createMicmeEditorHandlers(this, ctx));
|
|
113
|
+
}
|
|
126
114
|
|
|
127
|
-
|
|
115
|
+
async toggle(ctx: ExtensionContext) {
|
|
116
|
+
if (this.closed) return;
|
|
117
|
+
const active = this.current;
|
|
118
|
+
if (!active) {
|
|
119
|
+
await this.start(ctx);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (active.phase === "recording" || active.phase === "streaming") {
|
|
123
|
+
await this.stop(active);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
ctx.ui.notify(`Micme is ${formatOperationPhase(active.phase)}. Wait for it to finish before toggling again.`, "warning");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async handleTerminalShortcut(ctx: ExtensionContext) {
|
|
130
|
+
if (this.isShortcutAutoRepeat()) return;
|
|
131
|
+
await this.toggle(ctx);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async shutdown(ctx: ExtensionContext) {
|
|
135
|
+
if (this.closed) return;
|
|
136
|
+
this.closed = true;
|
|
137
|
+
const active = this.current;
|
|
138
|
+
this.current = undefined;
|
|
128
139
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
129
140
|
clearRecordingWidget(ctx);
|
|
130
|
-
if (!
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
141
|
+
if (!active) return;
|
|
142
|
+
|
|
143
|
+
const phaseAtShutdown = active.phase;
|
|
144
|
+
active.phase = "shutting_down";
|
|
145
|
+
active.controller.abort();
|
|
146
|
+
clearOperationTimers(active);
|
|
147
|
+
await stopOwnedProcesses(active);
|
|
148
|
+
if (phaseAtShutdown !== "starting" && active.completion) {
|
|
149
|
+
await raceWithTimeout(active.completion.catch(() => undefined), 2_000);
|
|
138
150
|
}
|
|
139
|
-
|
|
140
|
-
|
|
151
|
+
clearOperationTimers(active);
|
|
152
|
+
await stopOwnedProcesses(active);
|
|
153
|
+
await cleanupOwnedDirectories(active);
|
|
154
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
155
|
+
clearRecordingWidget(ctx);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
isCurrent(operation: MicmeOperation) {
|
|
159
|
+
return !this.closed && this.current === operation;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
transition(operation: MicmeOperation, phase: MicmeOperation["phase"]) {
|
|
163
|
+
if (!this.isCurrent(operation)) return false;
|
|
164
|
+
operation.phase = phase;
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
trackProcess(operation: MicmeOperation, active: Recording) {
|
|
169
|
+
operation.processes.add(active);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
trackDirectory(operation: MicmeOperation, tempDir: string) {
|
|
173
|
+
operation.tempDirs.add(tempDir);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
rememberTranscript(operation: MicmeOperation, transcript: string) {
|
|
177
|
+
if (this.isCurrent(operation)) this.lastTranscript = transcript;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
rememberAudioDirectory(operation: MicmeOperation, tempDir: string) {
|
|
181
|
+
if (this.isCurrent(operation)) this.lastAudioDir = tempDir;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async pasteLastTranscript(ctx: ExtensionContext) {
|
|
185
|
+
await pasteOrSubmitTranscript(ctx, this.pi, this.lastTranscript);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
notifyLastAudioDirectory(ctx: ExtensionContext) {
|
|
189
|
+
ctx.ui.notify(this.lastAudioDir ? `Last Micme audio directory: ${sanitizeTerminalOutput(this.lastAudioDir)}` : "No kept Micme audio yet. Set MICME_KEEP_AUDIO=1.", "info");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private async start(ctx: ExtensionContext) {
|
|
193
|
+
requireValidMicmeConfig();
|
|
194
|
+
const operation = this.beginOperation(ctx);
|
|
195
|
+
const work = startRecording(this, operation, operation.context);
|
|
196
|
+
operation.completion = work;
|
|
197
|
+
try {
|
|
198
|
+
await work;
|
|
199
|
+
} catch (error) {
|
|
200
|
+
if (!this.isCurrent(operation)) return;
|
|
201
|
+
handleExtensionError(operation.context, error);
|
|
202
|
+
this.finish(operation);
|
|
203
|
+
} finally {
|
|
204
|
+
if (operation.completion === work) operation.completion = undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private async stop(operation: MicmeOperation) {
|
|
209
|
+
if (!this.transition(operation, "stopping")) return;
|
|
210
|
+
operation.context.ui.setStatus(STATUS_KEY, "stopping…");
|
|
211
|
+
clearRecordingWidget(operation.context);
|
|
212
|
+
const work = stopAndTranscribe(this, operation, this.pi);
|
|
213
|
+
operation.completion = work;
|
|
214
|
+
try {
|
|
215
|
+
await work;
|
|
216
|
+
} catch (error) {
|
|
217
|
+
if (this.isCurrent(operation)) handleExtensionError(operation.context, error);
|
|
218
|
+
} finally {
|
|
219
|
+
if (operation.completion === work) operation.completion = undefined;
|
|
220
|
+
if (this.isCurrent(operation)) this.finish(operation);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private beginOperation(ctx: ExtensionContext) {
|
|
225
|
+
const operation: MicmeOperation = {
|
|
226
|
+
id: this.nextOperationId++,
|
|
227
|
+
phase: "starting",
|
|
228
|
+
context: ctx,
|
|
229
|
+
controller: new AbortController(),
|
|
230
|
+
processes: new Set(),
|
|
231
|
+
tempDirs: new Set(),
|
|
232
|
+
};
|
|
233
|
+
operation.context = createGuardedContext(this, operation, ctx);
|
|
234
|
+
this.current = operation;
|
|
235
|
+
ctx.ui.setStatus(STATUS_KEY, "starting…");
|
|
236
|
+
return operation;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
private finish(operation: MicmeOperation) {
|
|
240
|
+
if (!this.isCurrent(operation)) return;
|
|
241
|
+
this.current = undefined;
|
|
242
|
+
operation.context.ui.setStatus(STATUS_KEY, undefined);
|
|
243
|
+
clearRecordingWidget(operation.context);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private isShortcutAutoRepeat() {
|
|
247
|
+
const now = Date.now();
|
|
248
|
+
const repeated = now - this.lastShortcutInputAt < SHORTCUT_REPEAT_GUARD_MS;
|
|
249
|
+
this.lastShortcutInputAt = now;
|
|
250
|
+
return repeated;
|
|
251
|
+
}
|
|
141
252
|
}
|
|
142
253
|
|
|
143
|
-
function
|
|
254
|
+
function getMicmeArgumentCompletions(prefix: string) {
|
|
255
|
+
const matches = MICME_ACTIONS.filter((action) => action.startsWith(prefix.trim().toLowerCase()));
|
|
256
|
+
return matches.length ? matches.map((value) => ({ value, label: value })) : null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function handleMicmeCommand(owner: MicmeSessionOwner, pi: ExtensionAPI, args: string, ctx: ExtensionContext) {
|
|
260
|
+
const action = args.trim().split(/\s+/, 1)[0]?.toLowerCase() ?? "";
|
|
261
|
+
try {
|
|
262
|
+
switch (action) {
|
|
263
|
+
case "":
|
|
264
|
+
await owner.toggle(ctx);
|
|
265
|
+
return;
|
|
266
|
+
case "devices":
|
|
267
|
+
requireValidMicmeConfig();
|
|
268
|
+
await listAudioDevices(ctx, pi);
|
|
269
|
+
return;
|
|
270
|
+
case "conf":
|
|
271
|
+
await showConfiguration(ctx);
|
|
272
|
+
return;
|
|
273
|
+
case "last":
|
|
274
|
+
await owner.pasteLastTranscript(ctx);
|
|
275
|
+
return;
|
|
276
|
+
case "audio":
|
|
277
|
+
owner.notifyLastAudioDirectory(ctx);
|
|
278
|
+
return;
|
|
279
|
+
case "help":
|
|
280
|
+
ctx.ui.notify(getHelpText(), "info");
|
|
281
|
+
return;
|
|
282
|
+
default:
|
|
283
|
+
ctx.ui.notify(`Unknown micme action: ${sanitizeTerminalOutput(action) || "(empty)"}. Try /micme help`, "warning");
|
|
284
|
+
}
|
|
285
|
+
} catch (error) {
|
|
286
|
+
handleExtensionError(ctx, error);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function handleMicmeTerminalShortcut(owner: MicmeSessionOwner, ctx: ExtensionContext) {
|
|
291
|
+
try {
|
|
292
|
+
await owner.handleTerminalShortcut(ctx);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
handleExtensionError(ctx, error);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function createMicmeEditorHandlers(owner: MicmeSessionOwner, ctx: ExtensionContext): MicmeEditorInputHandlers {
|
|
144
299
|
return {
|
|
145
|
-
toggle: () => toggle(ctx).catch((error) => handleExtensionError(ctx, error)),
|
|
300
|
+
toggle: () => owner.toggle(ctx).catch((error) => handleExtensionError(ctx, error)),
|
|
146
301
|
};
|
|
147
302
|
}
|
|
148
303
|
|
|
@@ -151,135 +306,79 @@ function handleExtensionError(ctx: ExtensionContext, error: unknown) {
|
|
|
151
306
|
ctx.ui.notify(sanitizeTerminalOutput(error instanceof Error ? error.message : String(error)) || "Micme failed with an empty error message.", "error");
|
|
152
307
|
}
|
|
153
308
|
|
|
154
|
-
function
|
|
155
|
-
return
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function isShortcutAutoRepeat() {
|
|
159
|
-
const now = Date.now();
|
|
160
|
-
const repeated = now - lastShortcutInputAt < SHORTCUT_REPEAT_GUARD_MS;
|
|
161
|
-
lastShortcutInputAt = now;
|
|
162
|
-
return repeated;
|
|
309
|
+
function formatOperationPhase(phase: MicmeOperation["phase"]) {
|
|
310
|
+
return phase.replaceAll("_", " ");
|
|
163
311
|
}
|
|
164
312
|
|
|
165
313
|
function getHelpText() {
|
|
166
314
|
const shortcut = sanitizeTerminalOutput(getShortcut()) || "not set";
|
|
167
315
|
const actions = MICME_ACTIONS.filter((action) => action !== "help").join("|");
|
|
168
|
-
return `Usage: /micme [${actions}]. Shortcut: ${shortcut} toggles recording; tap once to start, tap again to stop/transcribe.`;
|
|
316
|
+
return `Usage: /micme [${actions}]. Shortcut: ${shortcut} toggles recording; tap once to start, tap again to stop/transcribe. Toggles are rejected while Micme is starting, stopping, transcribing, or finalizing.`;
|
|
169
317
|
}
|
|
170
318
|
|
|
171
|
-
async function startRecording(ctx: ExtensionContext, stopHint = getShortcut()) {
|
|
172
|
-
if (recording) return;
|
|
173
|
-
reloadMicmeConfig();
|
|
174
|
-
|
|
319
|
+
async function startRecording(owner: MicmeSessionOwner, operation: MicmeOperation, ctx: ExtensionContext, stopHint = getShortcut()) {
|
|
175
320
|
if (getTranscriptionMode() === "stream") {
|
|
176
|
-
await startStreamingTranscription(ctx, stopHint);
|
|
321
|
+
await startStreamingTranscription(owner, operation, ctx, stopHint);
|
|
177
322
|
return;
|
|
178
323
|
}
|
|
324
|
+
await startClipRecording(owner, operation, ctx, stopHint);
|
|
325
|
+
}
|
|
179
326
|
|
|
327
|
+
async function startClipRecording(owner: MicmeSessionOwner, operation: MicmeOperation, ctx: ExtensionContext, stopHint: string) {
|
|
180
328
|
const tempDir = await createRecordingDirectory(ctx.cwd, envFlag("MICME_KEEP_AUDIO"), "micme-");
|
|
181
|
-
|
|
329
|
+
owner.trackDirectory(operation, tempDir);
|
|
330
|
+
if (!owner.isCurrent(operation)) {
|
|
331
|
+
await cleanup(tempDir).catch(() => undefined);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
182
334
|
|
|
183
335
|
try {
|
|
184
336
|
const audioPath = join(tempDir, "raw.wav");
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
recording = active;
|
|
188
|
-
|
|
337
|
+
const active = spawnRecording(buildRecorderCommand(audioPath), audioPath, tempDir);
|
|
338
|
+
owner.trackProcess(operation, active);
|
|
189
339
|
const earlyExit = await raceWithTimeout(active.exitPromise, RECORDER_STARTUP_GRACE_MS);
|
|
340
|
+
if (!owner.isCurrent(operation)) return;
|
|
190
341
|
if (earlyExit) {
|
|
191
|
-
if (active.stopRequested) return;
|
|
192
342
|
const stderr = formatProcessOutput(active.stderr());
|
|
193
343
|
const suffix = stderr ? `\n${stderr}` : "";
|
|
194
344
|
throw new Error(`Micme recorder exited early (${formatExit(earlyExit)}).${suffix}`);
|
|
195
345
|
}
|
|
196
346
|
|
|
347
|
+
owner.transition(operation, "recording");
|
|
197
348
|
ctx.ui.setStatus(STATUS_KEY, `● recording (${sanitizeTerminalOutput(stopHint) || "shortcut"} or /micme)`);
|
|
198
349
|
startRecordingWidget(ctx, active);
|
|
199
350
|
} catch (error) {
|
|
200
|
-
await cleanupFailedRecordingStart(ctx,
|
|
351
|
+
await cleanupFailedRecordingStart(ctx, operation);
|
|
201
352
|
throw error;
|
|
202
353
|
}
|
|
203
354
|
}
|
|
204
355
|
|
|
205
|
-
async function
|
|
206
|
-
if (active && recording === active) recording = undefined;
|
|
207
|
-
clearRecordingWidget(ctx);
|
|
208
|
-
if (active) {
|
|
209
|
-
await stopProcess(active).catch(() => undefined);
|
|
210
|
-
if (active.clipRecording) await stopProcess(active.clipRecording).catch(() => undefined);
|
|
211
|
-
}
|
|
212
|
-
await cleanup(tempDir).catch(() => undefined);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
async function stopAndTranscribe(ctx: ExtensionContext, pi: ExtensionAPI) {
|
|
216
|
-
const active = recording;
|
|
217
|
-
if (!active) {
|
|
218
|
-
ctx.ui.notify("Micme is not recording. Use /micme to start recording.", "warning");
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (active.streaming) {
|
|
223
|
-
recording = undefined;
|
|
224
|
-
clearRecordingWidget(ctx);
|
|
225
|
-
await stopStreamingTranscription(ctx, pi, active);
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
recording = undefined;
|
|
230
|
-
clearRecordingWidget(ctx);
|
|
231
|
-
ctx.ui.setStatus(STATUS_KEY, "transcribing…");
|
|
232
|
-
|
|
233
|
-
let completed = false;
|
|
234
|
-
try {
|
|
235
|
-
await stopRecorder(active);
|
|
236
|
-
const preparedAudioPath = await prepareAudioForTranscription(active.audioPath, active.tempDir);
|
|
237
|
-
await validateRecordedAudio(preparedAudioPath);
|
|
238
|
-
const transcript = await transcribe(preparedAudioPath, active.tempDir, ctx);
|
|
239
|
-
const normalized = normalizeTranscript(transcript);
|
|
240
|
-
|
|
241
|
-
if (!normalized) {
|
|
242
|
-
throw new Error("Micme did not receive any transcript text.");
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
lastTranscript = normalized;
|
|
246
|
-
await pasteOrSubmitTranscript(ctx, pi, normalized);
|
|
247
|
-
completed = true;
|
|
248
|
-
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
249
|
-
if (envFlag("MICME_KEEP_AUDIO")) {
|
|
250
|
-
lastAudioDir = active.tempDir;
|
|
251
|
-
}
|
|
252
|
-
} catch (error) {
|
|
253
|
-
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
254
|
-
lastAudioDir = active.tempDir;
|
|
255
|
-
const message = sanitizeTerminalOutput(error instanceof Error ? error.message : String(error));
|
|
256
|
-
throw new Error(`${message}\nAudio kept for debugging: ${sanitizeTerminalOutput(active.tempDir)}`, { cause: error });
|
|
257
|
-
} finally {
|
|
258
|
-
if (completed && !envFlag("MICME_KEEP_AUDIO")) {
|
|
259
|
-
await cleanup(active.tempDir).catch(() => undefined);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
async function startStreamingTranscription(ctx: ExtensionContext, stopHint = getShortcut()) {
|
|
356
|
+
async function startStreamingTranscription(owner: MicmeSessionOwner, operation: MicmeOperation, ctx: ExtensionContext, stopHint: string) {
|
|
265
357
|
const plan = resolveStreamingTranscriptionPlan();
|
|
266
|
-
await ensureWhisperCppModel(plan.modelPath, ctx, { allowDownload: plan.modelDownloadable !== false });
|
|
358
|
+
await ensureWhisperCppModel(plan.modelPath, ctx, { allowDownload: plan.modelDownloadable !== false, signal: operation.controller.signal });
|
|
359
|
+
if (!owner.isCurrent(operation)) return;
|
|
267
360
|
|
|
268
361
|
const tempDir = await createRecordingDirectory(ctx.cwd, envFlag("MICME_KEEP_AUDIO"), "micme-stream-");
|
|
269
|
-
|
|
362
|
+
owner.trackDirectory(operation, tempDir);
|
|
363
|
+
if (!owner.isCurrent(operation)) {
|
|
364
|
+
await cleanup(tempDir).catch(() => undefined);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
270
367
|
|
|
271
368
|
try {
|
|
272
369
|
const streamRecording = spawnWhisperStreamingRecording(plan, tempDir);
|
|
273
|
-
|
|
370
|
+
owner.trackProcess(operation, streamRecording);
|
|
274
371
|
initializeStreamingRecording(ctx, streamRecording);
|
|
275
|
-
startOptionalClipRecording(streamRecording, tempDir);
|
|
276
|
-
|
|
372
|
+
const clipRecording = startOptionalClipRecording(streamRecording, tempDir);
|
|
373
|
+
if (clipRecording) owner.trackProcess(operation, clipRecording);
|
|
277
374
|
|
|
278
375
|
await ensureStreamingRecorderStarted(streamRecording);
|
|
376
|
+
if (!owner.isCurrent(operation)) return;
|
|
377
|
+
owner.transition(operation, "streaming");
|
|
279
378
|
ctx.ui.setStatus(STATUS_KEY, `● streaming (${sanitizeTerminalOutput(stopHint) || "shortcut"} or /micme)`);
|
|
280
379
|
startRecordingWidget(ctx, streamRecording);
|
|
281
380
|
} catch (error) {
|
|
282
|
-
await cleanupFailedRecordingStart(ctx,
|
|
381
|
+
await cleanupFailedRecordingStart(ctx, operation);
|
|
283
382
|
throw error;
|
|
284
383
|
}
|
|
285
384
|
}
|
|
@@ -317,8 +416,10 @@ function initializeStreamingRecording(ctx: ExtensionContext, streamRecording: Re
|
|
|
317
416
|
|
|
318
417
|
function startOptionalClipRecording(streamRecording: Recording, tempDir: string) {
|
|
319
418
|
const clipAudioPath = getStreamFinalizeWithClip() ? join(tempDir, "raw.wav") : undefined;
|
|
320
|
-
if (!clipAudioPath) return;
|
|
321
|
-
|
|
419
|
+
if (!clipAudioPath) return undefined;
|
|
420
|
+
const clipRecording = spawnRecording(buildRecorderCommand(clipAudioPath), clipAudioPath, tempDir);
|
|
421
|
+
streamRecording.clipRecording = clipRecording;
|
|
422
|
+
return clipRecording;
|
|
322
423
|
}
|
|
323
424
|
|
|
324
425
|
async function ensureStreamingRecorderStarted(streamRecording: Recording) {
|
|
@@ -335,14 +436,79 @@ async function ensureStreamingRecorderStarted(streamRecording: Recording) {
|
|
|
335
436
|
throw new Error(`Micme streaming exited early (${formatExit(failedExit)}).${suffix}`);
|
|
336
437
|
}
|
|
337
438
|
|
|
338
|
-
async function
|
|
439
|
+
async function cleanupFailedRecordingStart(ctx: ExtensionContext, operation: MicmeOperation) {
|
|
440
|
+
clearRecordingWidget(ctx);
|
|
441
|
+
clearOperationTimers(operation);
|
|
442
|
+
await stopOwnedProcesses(operation);
|
|
443
|
+
await cleanupOwnedDirectories(operation);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async function stopAndTranscribe(owner: MicmeSessionOwner, operation: MicmeOperation, pi: ExtensionAPI) {
|
|
447
|
+
const active = firstMainRecording(operation);
|
|
448
|
+
if (!active) throw new Error("Micme lost ownership of its recording process.");
|
|
449
|
+
if (active.streaming) {
|
|
450
|
+
await stopStreamingTranscription(owner, operation, pi, active);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
await stopClipAndTranscribe(owner, operation, pi, active);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function firstMainRecording(operation: MicmeOperation) {
|
|
457
|
+
return operation.processes.values().next().value;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async function stopClipAndTranscribe(owner: MicmeSessionOwner, operation: MicmeOperation, pi: ExtensionAPI, active: Recording) {
|
|
461
|
+
const ctx = operation.context;
|
|
462
|
+
let completed = false;
|
|
463
|
+
try {
|
|
464
|
+
await stopRecorder(active);
|
|
465
|
+
if (!owner.isCurrent(operation)) return;
|
|
466
|
+
owner.transition(operation, "transcribing");
|
|
467
|
+
ctx.ui.setStatus(STATUS_KEY, "transcribing…");
|
|
468
|
+
const preparedAudioPath = await prepareAudioForTranscription(active.audioPath, active.tempDir, operation.controller.signal);
|
|
469
|
+
if (!owner.isCurrent(operation)) return;
|
|
470
|
+
await validateAudioForTranscription(ctx, preparedAudioPath, operation.controller.signal);
|
|
471
|
+
if (!owner.isCurrent(operation)) return;
|
|
472
|
+
const transcript = await transcribe(preparedAudioPath, active.tempDir, ctx, operation.controller.signal);
|
|
473
|
+
if (!owner.isCurrent(operation)) return;
|
|
474
|
+
const normalized = normalizeTranscript(transcript);
|
|
475
|
+
if (!normalized) throw new Error("Micme did not receive any transcript text.");
|
|
476
|
+
|
|
477
|
+
owner.rememberTranscript(operation, normalized);
|
|
478
|
+
await pasteOrSubmitTranscript(ctx, pi, normalized);
|
|
479
|
+
if (!owner.isCurrent(operation)) return;
|
|
480
|
+
completed = true;
|
|
481
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
482
|
+
if (envFlag("MICME_KEEP_AUDIO")) owner.rememberAudioDirectory(operation, active.tempDir);
|
|
483
|
+
} catch (error) {
|
|
484
|
+
if (!owner.isCurrent(operation)) return;
|
|
485
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
486
|
+
owner.rememberAudioDirectory(operation, active.tempDir);
|
|
487
|
+
const message = sanitizeTerminalOutput(error instanceof Error ? error.message : String(error));
|
|
488
|
+
throw new Error(`${message}\nAudio kept for debugging: ${sanitizeTerminalOutput(active.tempDir)}`, { cause: error });
|
|
489
|
+
} finally {
|
|
490
|
+
if (completed && !envFlag("MICME_KEEP_AUDIO")) await cleanup(active.tempDir).catch(() => undefined);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
async function stopStreamingTranscription(owner: MicmeSessionOwner, operation: MicmeOperation, pi: ExtensionAPI, active: Recording) {
|
|
495
|
+
const ctx = operation.context;
|
|
339
496
|
const state = active.streaming;
|
|
340
497
|
const clipStopPromise = stopClipRecording(active);
|
|
341
|
-
|
|
342
498
|
prepareStreamingStop(ctx, active, state);
|
|
343
|
-
await stopProcess(active);
|
|
499
|
+
const exit = await stopProcess(active);
|
|
500
|
+
if (!owner.isCurrent(operation)) return;
|
|
501
|
+
try {
|
|
502
|
+
assertExpectedProcessExit(active, exit, "stream");
|
|
503
|
+
} catch (error) {
|
|
504
|
+
if (clipStopPromise) await clipStopPromise;
|
|
505
|
+
throw error;
|
|
506
|
+
}
|
|
344
507
|
|
|
345
|
-
|
|
508
|
+
owner.transition(operation, "finalizing");
|
|
509
|
+
ctx.ui.setStatus(STATUS_KEY, "finalizing stream…");
|
|
510
|
+
const keepTempDir = state ? await completeStreamingStop(owner, operation, pi, active, state, clipStopPromise) : false;
|
|
511
|
+
if (!owner.isCurrent(operation)) return;
|
|
346
512
|
if (!keepTempDir) await cleanup(active.tempDir).catch(() => undefined);
|
|
347
513
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
348
514
|
}
|
|
@@ -351,6 +517,12 @@ function stopClipRecording(active: Recording) {
|
|
|
351
517
|
return active.clipRecording ? stopRecorder(active.clipRecording).then(() => undefined, (error: unknown) => error) : undefined;
|
|
352
518
|
}
|
|
353
519
|
|
|
520
|
+
async function validateAudioForTranscription(ctx: ExtensionContext, audioPath: string, signal: AbortSignal) {
|
|
521
|
+
const outcome = await validateRecordedAudio(audioPath, signal);
|
|
522
|
+
if (outcome.status !== "skipped" || outcome.reason !== "ffmpeg-unavailable") return;
|
|
523
|
+
ctx.ui.notify("Micme skipped silence validation because ffmpeg was not found. Install ffmpeg or set MICME_VALIDATE_AUDIO=0 to disable the silence guard.", "warning");
|
|
524
|
+
}
|
|
525
|
+
|
|
354
526
|
function prepareStreamingStop(ctx: ExtensionContext, active: Recording, state: Recording["streaming"]) {
|
|
355
527
|
if (!state) return;
|
|
356
528
|
drainStreamingOutput(ctx, active, false);
|
|
@@ -359,88 +531,139 @@ function prepareStreamingStop(ctx: ExtensionContext, active: Recording, state: R
|
|
|
359
531
|
}
|
|
360
532
|
|
|
361
533
|
async function completeStreamingStop(
|
|
362
|
-
|
|
534
|
+
owner: MicmeSessionOwner,
|
|
535
|
+
operation: MicmeOperation,
|
|
363
536
|
pi: ExtensionAPI,
|
|
364
537
|
active: Recording,
|
|
365
538
|
state: NonNullable<Recording["streaming"]>,
|
|
366
539
|
clipStopPromise: ReturnType<typeof stopClipRecording>,
|
|
367
540
|
) {
|
|
541
|
+
const ctx = operation.context;
|
|
368
542
|
drainStreamingOutput(ctx, active, true);
|
|
369
543
|
showStreamingDiagnostics(ctx, state);
|
|
370
544
|
const clipRecording = active.clipRecording;
|
|
371
545
|
if (!clipRecording) {
|
|
372
|
-
await useLiveStreamingTranscript(
|
|
546
|
+
await useLiveStreamingTranscript(owner, operation, pi, state);
|
|
373
547
|
clearStreamingFlush(state);
|
|
374
548
|
return false;
|
|
375
549
|
}
|
|
376
550
|
|
|
377
551
|
renderStreamingPreview(ctx, state, false);
|
|
378
552
|
clearStreamingFlush(state);
|
|
379
|
-
return finalizeStreamingClip(
|
|
553
|
+
return finalizeStreamingClip(owner, operation, pi, active, clipRecording, state, clipStopPromise);
|
|
380
554
|
}
|
|
381
555
|
|
|
382
|
-
async function useLiveStreamingTranscript(
|
|
556
|
+
async function useLiveStreamingTranscript(owner: MicmeSessionOwner, operation: MicmeOperation, pi: ExtensionAPI, state: NonNullable<Recording["streaming"]>) {
|
|
557
|
+
const ctx = operation.context;
|
|
383
558
|
flushPendingStreamingWords(ctx, state);
|
|
384
559
|
const liveTranscript = normalizeTranscript(getStreamingTranscript(state));
|
|
385
|
-
if (!liveTranscript) return;
|
|
560
|
+
if (!liveTranscript || !owner.isCurrent(operation)) return;
|
|
386
561
|
|
|
387
|
-
|
|
562
|
+
owner.rememberTranscript(operation, liveTranscript);
|
|
388
563
|
if (!envFlag("MICME_AUTO_SUBMIT")) return;
|
|
389
564
|
ctx.ui.setEditorText(state.baseText);
|
|
565
|
+
if (!owner.isCurrent(operation)) return;
|
|
390
566
|
await pasteOrSubmitTranscript(ctx, pi, liveTranscript);
|
|
391
567
|
}
|
|
392
568
|
|
|
393
569
|
async function finalizeStreamingClip(
|
|
394
|
-
|
|
570
|
+
owner: MicmeSessionOwner,
|
|
571
|
+
operation: MicmeOperation,
|
|
395
572
|
pi: ExtensionAPI,
|
|
396
573
|
active: Recording,
|
|
397
574
|
clipRecording: Recording,
|
|
398
575
|
state: NonNullable<Recording["streaming"]>,
|
|
399
576
|
clipStopPromise: ReturnType<typeof stopClipRecording>,
|
|
400
577
|
) {
|
|
401
|
-
ctx.ui.setStatus(STATUS_KEY, "finalizing stream…");
|
|
402
578
|
try {
|
|
403
|
-
await transcribeFinalStreamingClip(
|
|
404
|
-
return keepCompletedStreamingClipAudio(active);
|
|
579
|
+
await transcribeFinalStreamingClip(owner, operation, pi, active, clipRecording, state, clipStopPromise);
|
|
580
|
+
return keepCompletedStreamingClipAudio(owner, operation, active);
|
|
405
581
|
} catch (error) {
|
|
406
|
-
|
|
582
|
+
if (!owner.isCurrent(operation)) return false;
|
|
583
|
+
handleFinalStreamingClipError(owner, operation, active, state, error);
|
|
407
584
|
return true;
|
|
408
585
|
}
|
|
409
586
|
}
|
|
410
587
|
|
|
411
588
|
async function transcribeFinalStreamingClip(
|
|
412
|
-
|
|
589
|
+
owner: MicmeSessionOwner,
|
|
590
|
+
operation: MicmeOperation,
|
|
413
591
|
pi: ExtensionAPI,
|
|
414
592
|
active: Recording,
|
|
415
593
|
clipRecording: Recording,
|
|
416
594
|
state: NonNullable<Recording["streaming"]>,
|
|
417
595
|
clipStopPromise: ReturnType<typeof stopClipRecording>,
|
|
418
596
|
) {
|
|
597
|
+
const ctx = operation.context;
|
|
419
598
|
const clipStopError = clipStopPromise ? await clipStopPromise : undefined;
|
|
599
|
+
if (!owner.isCurrent(operation)) return;
|
|
420
600
|
if (clipStopError) throw clipStopError;
|
|
421
|
-
const preparedAudioPath = await prepareAudioForTranscription(clipRecording.audioPath, active.tempDir);
|
|
422
|
-
|
|
423
|
-
|
|
601
|
+
const preparedAudioPath = await prepareAudioForTranscription(clipRecording.audioPath, active.tempDir, operation.controller.signal);
|
|
602
|
+
if (!owner.isCurrent(operation)) return;
|
|
603
|
+
await validateAudioForTranscription(ctx, preparedAudioPath, operation.controller.signal);
|
|
604
|
+
if (!owner.isCurrent(operation)) return;
|
|
605
|
+
const transcript = await transcribe(preparedAudioPath, active.tempDir, ctx, operation.controller.signal);
|
|
606
|
+
if (!owner.isCurrent(operation)) return;
|
|
424
607
|
const normalized = normalizeTranscript(transcript);
|
|
425
608
|
if (!normalized) throw new Error("Micme did not receive any final transcript text.");
|
|
426
609
|
|
|
427
|
-
|
|
610
|
+
owner.rememberTranscript(operation, normalized);
|
|
428
611
|
await pasteOrSubmitFinalStreamingTranscript(ctx, pi, state, normalized);
|
|
429
612
|
}
|
|
430
613
|
|
|
431
|
-
function keepCompletedStreamingClipAudio(active: Recording) {
|
|
614
|
+
function keepCompletedStreamingClipAudio(owner: MicmeSessionOwner, operation: MicmeOperation, active: Recording) {
|
|
432
615
|
if (!envFlag("MICME_KEEP_AUDIO")) return false;
|
|
433
|
-
|
|
616
|
+
owner.rememberAudioDirectory(operation, active.tempDir);
|
|
434
617
|
return true;
|
|
435
618
|
}
|
|
436
619
|
|
|
437
|
-
function handleFinalStreamingClipError(
|
|
620
|
+
function handleFinalStreamingClipError(
|
|
621
|
+
owner: MicmeSessionOwner,
|
|
622
|
+
operation: MicmeOperation,
|
|
623
|
+
active: Recording,
|
|
624
|
+
state: NonNullable<Recording["streaming"]>,
|
|
625
|
+
error: unknown,
|
|
626
|
+
) {
|
|
627
|
+
const ctx = operation.context;
|
|
438
628
|
flushPendingStreamingWords(ctx, state);
|
|
439
629
|
const liveTranscript = normalizeTranscript(getStreamingTranscript(state));
|
|
440
|
-
if (liveTranscript)
|
|
441
|
-
|
|
630
|
+
if (liveTranscript) owner.rememberTranscript(operation, liveTranscript);
|
|
631
|
+
owner.rememberAudioDirectory(operation, active.tempDir);
|
|
442
632
|
const message = sanitizeTerminalOutput(error instanceof Error ? error.message : String(error));
|
|
443
633
|
ctx.ui.notify(`Micme kept the live append-only stream transcript because final clip transcription failed: ${message}\nAudio kept for debugging: ${sanitizeTerminalOutput(active.tempDir)}`, "warning");
|
|
444
634
|
}
|
|
445
635
|
|
|
636
|
+
function clearOperationTimers(operation: MicmeOperation) {
|
|
637
|
+
for (const active of operation.processes) {
|
|
638
|
+
if (active.streaming) clearStreamingFlush(active.streaming);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
async function stopOwnedProcesses(operation: MicmeOperation) {
|
|
643
|
+
await Promise.all([...operation.processes].map((active) => stopProcess(active).catch(() => undefined)));
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
async function cleanupOwnedDirectories(operation: MicmeOperation) {
|
|
647
|
+
await Promise.all([...operation.tempDirs].map((tempDir) => cleanup(tempDir).catch(() => undefined)));
|
|
648
|
+
}
|
|
446
649
|
|
|
650
|
+
function createGuardedContext(owner: MicmeSessionOwner, operation: MicmeOperation, ctx: ExtensionContext) {
|
|
651
|
+
const guardedUi = new Proxy(ctx.ui, {
|
|
652
|
+
get(target, property, receiver) {
|
|
653
|
+
const value = Reflect.get(target, property, receiver) as unknown;
|
|
654
|
+
if (typeof value !== "function") return value;
|
|
655
|
+
if (!GUARDED_UI_METHODS.has(String(property))) return value.bind(target);
|
|
656
|
+
return (...args: unknown[]) => {
|
|
657
|
+
if (!owner.isCurrent(operation)) return undefined;
|
|
658
|
+
return Reflect.apply(value, target, args);
|
|
659
|
+
};
|
|
660
|
+
},
|
|
661
|
+
});
|
|
662
|
+
return new Proxy(ctx, {
|
|
663
|
+
get(target, property, receiver) {
|
|
664
|
+
if (property === "ui") return guardedUi;
|
|
665
|
+
const value = Reflect.get(target, property, receiver) as unknown;
|
|
666
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
667
|
+
},
|
|
668
|
+
}) as ExtensionContext;
|
|
669
|
+
}
|