@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/streaming.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
3
|
-
MAX_CAPTURED_OUTPUT_CHARS,
|
|
4
|
-
STREAM_MIN_INITIAL_WORDS,
|
|
5
|
-
} from "./constants.ts";
|
|
2
|
+
import { MAX_CAPTURED_OUTPUT_CHARS } from "./constants.ts";
|
|
6
3
|
import {
|
|
7
4
|
env,
|
|
8
5
|
envFlag,
|
|
6
|
+
getStreamFlushMs,
|
|
9
7
|
getStreamKeepContext,
|
|
10
8
|
getStreamKeepMs,
|
|
11
9
|
getStreamLengthMs,
|
|
@@ -18,6 +16,22 @@ import { pasteOrSubmitTranscript } from "./transcript-delivery.ts";
|
|
|
18
16
|
import { shellQuote } from "./processes.ts";
|
|
19
17
|
import type { CommandSpec, Recording, StreamingState } from "./types.ts";
|
|
20
18
|
|
|
19
|
+
export type StreamingFrameMode = "cumulative" | "incremental" | "rolling" | "duplicate" | "reset";
|
|
20
|
+
|
|
21
|
+
export type StreamingExtraction = {
|
|
22
|
+
mode: StreamingFrameMode;
|
|
23
|
+
newWords: string[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type StreamingDiagnosticDetails = {
|
|
27
|
+
rawFrame?: string;
|
|
28
|
+
sanitizedText?: string;
|
|
29
|
+
frameWords?: string[];
|
|
30
|
+
extractionMode: StreamingFrameMode | "ignored" | "flush";
|
|
31
|
+
newWords?: string[];
|
|
32
|
+
reason?: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
21
35
|
export function buildWhisperStreamCommand(binary: string, modelPath: string, tempDir: string): CommandSpec {
|
|
22
36
|
const args = [
|
|
23
37
|
"-m",
|
|
@@ -52,25 +66,54 @@ export function handleStreamingOutput(ctx: ExtensionContext, active: Recording,
|
|
|
52
66
|
|
|
53
67
|
export function drainStreamingOutput(ctx: ExtensionContext, active: Recording, force: boolean, chunk = "") {
|
|
54
68
|
if (!active.streaming) return;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
69
|
+
const state = active.streaming;
|
|
70
|
+
|
|
71
|
+
for (const rawFrame of readStreamingOutputFrames(state, chunk, force)) {
|
|
72
|
+
const sanitizedText = sanitizeStreamingText(rawFrame);
|
|
73
|
+
const frameWords = splitStreamingWords(sanitizedText);
|
|
74
|
+
|
|
75
|
+
if (!sanitizedText) {
|
|
76
|
+
if (shouldResetStreamingPending(rawFrame)) {
|
|
77
|
+
clearStreamingFlush(state);
|
|
78
|
+
state.candidateWords = [];
|
|
79
|
+
state.lastHypothesisWords = [];
|
|
80
|
+
state.lastText = "";
|
|
81
|
+
renderStreamingPreview(ctx, state, false);
|
|
82
|
+
showStreamingFrameDiagnostics(ctx, state, {
|
|
83
|
+
rawFrame,
|
|
84
|
+
sanitizedText,
|
|
85
|
+
frameWords,
|
|
86
|
+
extractionMode: "reset",
|
|
87
|
+
newWords: [],
|
|
88
|
+
reason: "reset/hallucination frame",
|
|
89
|
+
});
|
|
90
|
+
} else {
|
|
91
|
+
showStreamingFrameDiagnostics(ctx, state, {
|
|
92
|
+
rawFrame,
|
|
93
|
+
sanitizedText,
|
|
94
|
+
frameWords,
|
|
95
|
+
extractionMode: "ignored",
|
|
96
|
+
newWords: [],
|
|
97
|
+
reason: "empty frame",
|
|
98
|
+
});
|
|
61
99
|
}
|
|
62
100
|
continue;
|
|
63
101
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
renderStreamingPreview(ctx, active.streaming, false);
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
102
|
+
|
|
103
|
+
clearStreamingFlush(state);
|
|
104
|
+
if (!state.firstOutputAt) state.firstOutputAt = Date.now();
|
|
105
|
+
const extraction = extractStreamingCandidate(state, frameWords);
|
|
106
|
+
state.lastText = sanitizedText;
|
|
72
107
|
active.audioLevel = () => 0.55;
|
|
73
|
-
queueStableStreamingWords(ctx,
|
|
108
|
+
queueStableStreamingWords(ctx, state, extraction.newWords, extraction.mode);
|
|
109
|
+
state.lastHypothesisWords = frameWords;
|
|
110
|
+
showStreamingFrameDiagnostics(ctx, state, {
|
|
111
|
+
rawFrame,
|
|
112
|
+
sanitizedText,
|
|
113
|
+
frameWords,
|
|
114
|
+
extractionMode: extraction.mode,
|
|
115
|
+
newWords: extraction.newWords,
|
|
116
|
+
});
|
|
74
117
|
}
|
|
75
118
|
}
|
|
76
119
|
|
|
@@ -117,6 +160,32 @@ export function diffStreamingText(previous: string, current: string) {
|
|
|
117
160
|
return currentWords.slice(overlap).join(" ");
|
|
118
161
|
}
|
|
119
162
|
|
|
163
|
+
export function extractStreamingCandidate(state: StreamingState, frameWords: string[]): StreamingExtraction {
|
|
164
|
+
const words = frameWords.filter(Boolean);
|
|
165
|
+
if (words.length === 0) return { mode: "reset", newWords: [] };
|
|
166
|
+
|
|
167
|
+
const emittedOverlap = streamingWordOverlap(state.emittedWords.slice(-160), words);
|
|
168
|
+
if (emittedOverlap === words.length) return { mode: "duplicate", newWords: [] };
|
|
169
|
+
if (emittedOverlap > 0) return { mode: "cumulative", newWords: words.slice(emittedOverlap) };
|
|
170
|
+
|
|
171
|
+
if (state.candidateWords.length > 0) {
|
|
172
|
+
if (streamingWordsEqual(state.candidateWords, words)) return { mode: "duplicate", newWords: words };
|
|
173
|
+
if (isStreamingWordsPrefix(state.candidateWords, words)) return { mode: "cumulative", newWords: words };
|
|
174
|
+
if (streamingWordOverlap(state.candidateWords, words) > 0) return { mode: "rolling", newWords: words };
|
|
175
|
+
if (looksLikeStreamingCorrection(state.candidateWords, words)) return { mode: "reset", newWords: words };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (state.lastHypothesisWords.length > 0) {
|
|
179
|
+
if (streamingWordsEqual(state.lastHypothesisWords, words)) return { mode: "duplicate", newWords: words };
|
|
180
|
+
if (isStreamingWordsPrefix(state.lastHypothesisWords, words)) return { mode: "cumulative", newWords: trimCommittedStreamingPrefix(state, words) };
|
|
181
|
+
if (streamingWordOverlap(state.lastHypothesisWords, words) > 0) return { mode: "rolling", newWords: words };
|
|
182
|
+
if (looksLikeStreamingCorrection(state.lastHypothesisWords, words)) return { mode: "reset", newWords: words };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const isFirstHypothesis = state.emittedWords.length === 0 && state.candidateWords.length === 0 && state.lastHypothesisWords.length === 0;
|
|
186
|
+
return { mode: isFirstHypothesis ? "cumulative" : "incremental", newWords: words };
|
|
187
|
+
}
|
|
188
|
+
|
|
120
189
|
export function isLikelyStreamingHallucination(text: string) {
|
|
121
190
|
if (!text) return true;
|
|
122
191
|
const normalized = text.toLowerCase().replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, "");
|
|
@@ -133,28 +202,74 @@ export function shouldResetStreamingPending(text: string) {
|
|
|
133
202
|
return /^\[[^\]]+\]$/.test(cleaned) || /^\([^)]*\)$/.test(cleaned);
|
|
134
203
|
}
|
|
135
204
|
|
|
136
|
-
export function queueStableStreamingWords(ctx: ExtensionContext, state: StreamingState, currentWords: string[]) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
205
|
+
export function queueStableStreamingWords(ctx: ExtensionContext, state: StreamingState, currentWords: string[], mode: StreamingFrameMode = "cumulative") {
|
|
206
|
+
const words = currentWords.filter(Boolean);
|
|
207
|
+
|
|
208
|
+
if (mode === "reset") {
|
|
209
|
+
state.candidateWords = trimCommittedStreamingPrefix(state, words);
|
|
210
|
+
renderStreamingPreview(ctx, state, false);
|
|
211
|
+
scheduleStreamingFlush(ctx, state);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (mode === "incremental") {
|
|
216
|
+
commitStreamingCandidate(state);
|
|
217
|
+
state.candidateWords = trimCommittedStreamingPrefix(state, words);
|
|
218
|
+
renderStreamingPreview(ctx, state, false);
|
|
219
|
+
scheduleStreamingFlush(ctx, state);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (mode === "rolling") {
|
|
224
|
+
queueRollingStreamingWords(state, words);
|
|
225
|
+
renderStreamingPreview(ctx, state, false);
|
|
226
|
+
scheduleStreamingFlush(ctx, state);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (mode === "duplicate" && words.length === 0) {
|
|
231
|
+
renderStreamingPreview(ctx, state, false);
|
|
232
|
+
scheduleStreamingFlush(ctx, state);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const stableCount = commonStreamingPrefixLength(state.candidateWords, words);
|
|
237
|
+
const confirmedCount = Math.min(stableCount, getStreamWordsPerChunk());
|
|
238
|
+
if (confirmedCount > 0) queueStreamingWords(state, words.slice(0, confirmedCount));
|
|
239
|
+
state.candidateWords = words.slice(confirmedCount);
|
|
143
240
|
renderStreamingPreview(ctx, state, false);
|
|
241
|
+
scheduleStreamingFlush(ctx, state);
|
|
144
242
|
}
|
|
145
243
|
|
|
146
|
-
export function flushPendingStreamingWords(ctx: ExtensionContext, state: StreamingState) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
244
|
+
export function flushPendingStreamingWords(ctx: ExtensionContext, state: StreamingState, trailingSpace = true) {
|
|
245
|
+
clearStreamingFlush(state);
|
|
246
|
+
commitStreamingCandidate(state);
|
|
247
|
+
renderStreamingPreview(ctx, state, trailingSpace);
|
|
248
|
+
showStreamingFrameDiagnostics(ctx, state, {
|
|
249
|
+
extractionMode: "flush",
|
|
250
|
+
newWords: [],
|
|
251
|
+
reason: trailingSpace ? "stop flush" : "pause flush",
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function scheduleStreamingFlush(ctx: ExtensionContext, state: StreamingState) {
|
|
256
|
+
clearStreamingFlush(state);
|
|
257
|
+
if (state.candidateWords.length === 0) return;
|
|
258
|
+
const delayMs = getStreamFlushMs();
|
|
259
|
+
state.flushTimer = setTimeout(() => {
|
|
260
|
+
state.flushTimer = undefined;
|
|
261
|
+
flushPendingStreamingWords(ctx, state, false);
|
|
262
|
+
}, delayMs);
|
|
263
|
+
unrefStreamingTimer(state.flushTimer);
|
|
152
264
|
}
|
|
153
265
|
|
|
154
266
|
export function queueStreamingWords(state: StreamingState, words: string[]) {
|
|
155
267
|
const nextWords = words.filter(Boolean);
|
|
156
268
|
if (nextWords.length === 0) return;
|
|
157
|
-
state.emittedWords
|
|
269
|
+
const overlap = streamingWordOverlap(state.emittedWords, nextWords);
|
|
270
|
+
const appendWords = nextWords.slice(overlap);
|
|
271
|
+
if (appendWords.length === 0) return;
|
|
272
|
+
state.emittedWords.push(...appendWords);
|
|
158
273
|
}
|
|
159
274
|
|
|
160
275
|
export function splitStreamingWords(text: string) {
|
|
@@ -181,6 +296,15 @@ export function commonStreamingPrefixLength(previousWords: string[], currentWord
|
|
|
181
296
|
return count;
|
|
182
297
|
}
|
|
183
298
|
|
|
299
|
+
export function commonStreamingSuffixLength(previousWords: string[], currentWords: string[]) {
|
|
300
|
+
const maxSuffix = Math.min(previousWords.length, currentWords.length);
|
|
301
|
+
let count = 0;
|
|
302
|
+
while (count < maxSuffix && normalizeStreamingWord(previousWords[previousWords.length - 1 - count] ?? "") === normalizeStreamingWord(currentWords[currentWords.length - 1 - count] ?? "")) {
|
|
303
|
+
count++;
|
|
304
|
+
}
|
|
305
|
+
return count;
|
|
306
|
+
}
|
|
307
|
+
|
|
184
308
|
export function streamingWordsEqual(left: string[], right: string[]) {
|
|
185
309
|
if (left.length !== right.length) return false;
|
|
186
310
|
return left.every((word, index) => normalizeStreamingWord(word) === normalizeStreamingWord(right[index] ?? ""));
|
|
@@ -192,7 +316,7 @@ export function normalizeStreamingWord(word: string) {
|
|
|
192
316
|
}
|
|
193
317
|
|
|
194
318
|
export function getStreamingTranscript(state: StreamingState) {
|
|
195
|
-
return
|
|
319
|
+
return state.emittedWords.join(" ");
|
|
196
320
|
}
|
|
197
321
|
|
|
198
322
|
export function renderStreamingPreview(ctx: ExtensionContext, state: StreamingState, trailingSpace: boolean) {
|
|
@@ -235,3 +359,74 @@ export function clearStreamingFlush(state: StreamingState) {
|
|
|
235
359
|
clearTimeout(state.flushTimer);
|
|
236
360
|
state.flushTimer = undefined;
|
|
237
361
|
}
|
|
362
|
+
|
|
363
|
+
function queueRollingStreamingWords(state: StreamingState, words: string[]) {
|
|
364
|
+
const overlap = streamingWordOverlap(state.candidateWords, words);
|
|
365
|
+
if (overlap <= 0) {
|
|
366
|
+
commitStreamingCandidate(state);
|
|
367
|
+
state.candidateWords = trimCommittedStreamingPrefix(state, words);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const previousCandidatePrefix = state.candidateWords.slice(0, state.candidateWords.length - overlap);
|
|
372
|
+
queueStreamingWords(state, previousCandidatePrefix);
|
|
373
|
+
const confirmedOverlap = Math.min(overlap, getStreamWordsPerChunk());
|
|
374
|
+
queueStreamingWords(state, words.slice(0, confirmedOverlap));
|
|
375
|
+
state.candidateWords = trimCommittedStreamingPrefix(state, words.slice(confirmedOverlap));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function commitStreamingCandidate(state: StreamingState) {
|
|
379
|
+
if (state.candidateWords.length === 0) return;
|
|
380
|
+
queueStreamingWords(state, state.candidateWords);
|
|
381
|
+
state.candidateWords = [];
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function trimCommittedStreamingPrefix(state: StreamingState, words: string[]) {
|
|
385
|
+
const overlap = streamingWordOverlap(state.emittedWords, words);
|
|
386
|
+
return words.slice(overlap);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function isStreamingWordsPrefix(prefixWords: string[], words: string[]) {
|
|
390
|
+
if (prefixWords.length === 0 || prefixWords.length > words.length) return false;
|
|
391
|
+
return streamingWordsEqual(prefixWords, words.slice(0, prefixWords.length));
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function looksLikeStreamingCorrection(previousWords: string[], currentWords: string[]) {
|
|
395
|
+
if (previousWords.length === 0 || currentWords.length === 0) return false;
|
|
396
|
+
if (previousWords.length === 1 && currentWords.length === 1) return false;
|
|
397
|
+
if (commonStreamingPrefixLength(previousWords, currentWords) > 0) return true;
|
|
398
|
+
if (commonStreamingSuffixLength(previousWords, currentWords) > 0) return true;
|
|
399
|
+
const previousNormalized = new Set(previousWords.map(normalizeStreamingWord));
|
|
400
|
+
const sharedCount = currentWords.filter((word) => previousNormalized.has(normalizeStreamingWord(word))).length;
|
|
401
|
+
return previousWords.length > 1 && currentWords.length > 1 && sharedCount >= Math.ceil(Math.min(previousWords.length, currentWords.length) / 2);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function showStreamingFrameDiagnostics(ctx: ExtensionContext, state: StreamingState, details: StreamingDiagnosticDetails) {
|
|
405
|
+
if (!envFlag("MICME_STREAM_DIAGNOSTICS")) return;
|
|
406
|
+
const payload = {
|
|
407
|
+
rawFrame: details.rawFrame === undefined ? undefined : truncateStreamingDiagnostic(stripStreamingControls(details.rawFrame)),
|
|
408
|
+
sanitizedText: details.sanitizedText === undefined ? undefined : truncateStreamingDiagnostic(details.sanitizedText),
|
|
409
|
+
frameWords: limitStreamingDiagnosticWords(details.frameWords ?? []),
|
|
410
|
+
emittedWords: limitStreamingDiagnosticWords(state.emittedWords),
|
|
411
|
+
candidateWords: limitStreamingDiagnosticWords(state.candidateWords),
|
|
412
|
+
previewText: truncateStreamingDiagnostic(getStreamingTranscript(state)),
|
|
413
|
+
extractionMode: details.extractionMode,
|
|
414
|
+
newWords: limitStreamingDiagnosticWords(details.newWords ?? []),
|
|
415
|
+
reason: details.reason,
|
|
416
|
+
};
|
|
417
|
+
ctx.ui.notify(`Micme stream frame: ${JSON.stringify(payload)}`, "info");
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function limitStreamingDiagnosticWords(words: string[]) {
|
|
421
|
+
return words.slice(-16);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function truncateStreamingDiagnostic(value: string) {
|
|
425
|
+
return value.length > 160 ? `${value.slice(0, 157)}...` : value;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function unrefStreamingTimer(timer: ReturnType<typeof setTimeout>) {
|
|
429
|
+
if (typeof timer !== "object" || timer === null || !("unref" in timer)) return;
|
|
430
|
+
const unref = timer.unref;
|
|
431
|
+
if (typeof unref === "function") unref.call(timer);
|
|
432
|
+
}
|
package/src/transcription.ts
CHANGED
|
@@ -2,31 +2,31 @@ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { readFile } from "node:fs/promises";
|
|
4
4
|
import { basename, join } from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
5
|
+
import { resolveTranscriptionPlan, formatTranscriptionPlan } from "./backends.ts";
|
|
6
|
+
import { env, getTranscribeTimeoutMs } from "./config.ts";
|
|
7
|
+
import { ensureWhisperCppModel } from "./models.ts";
|
|
8
|
+
import { formatRunExit, replacePlaceholders, runProcess, runShell } from "./processes.ts";
|
|
8
9
|
|
|
9
|
-
export
|
|
10
|
-
const custom = env("MICME_TRANSCRIBE_COMMAND");
|
|
11
|
-
if (custom?.trim()) {
|
|
12
|
-
return transcribeWithCustomCommand(custom, audioPath, tempDir);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const whisperCppBinary = getWhisperCppBinary();
|
|
16
|
-
const whisperCppModel = env("MICME_WHISPER_CPP_MODEL") || (whisperCppBinary ? getDefaultWhisperCppModelPath() : undefined);
|
|
17
|
-
if (whisperCppModel && whisperCppBinary) {
|
|
18
|
-
return transcribeWithWhisperCpp(whisperCppBinary, expandConfigPath(whisperCppModel), audioPath, tempDir, ctx);
|
|
19
|
-
}
|
|
10
|
+
export { getWhisperCppBinary, getWhisperStreamBinary, resolveExecutableConfig } from "./backends.ts";
|
|
20
11
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
export async function transcribe(audioPath: string, tempDir: string, ctx?: ExtensionContext): Promise<string> {
|
|
13
|
+
const plan = resolveTranscriptionPlan({ transcriptionMode: "clip" });
|
|
14
|
+
|
|
15
|
+
switch (plan.effectiveBackend) {
|
|
16
|
+
case "custom":
|
|
17
|
+
if (!plan.command) break;
|
|
18
|
+
return transcribeWithCustomCommand(plan.command, audioPath, tempDir);
|
|
19
|
+
case "whisper.cpp":
|
|
20
|
+
if (!plan.binary || !plan.modelPath) break;
|
|
21
|
+
return transcribeWithWhisperCpp(plan.binary, plan.modelPath, audioPath, tempDir, ctx, { allowDownload: plan.modelDownloadable !== false });
|
|
22
|
+
case "python":
|
|
23
|
+
if (!plan.binary) break;
|
|
24
|
+
return transcribeWithOpenAiWhisper(plan.binary, audioPath, tempDir, plan.modelName);
|
|
25
|
+
case "none":
|
|
26
|
+
break;
|
|
24
27
|
}
|
|
25
28
|
|
|
26
|
-
throw new Error(
|
|
27
|
-
"No Micme transcription backend found. Install whisper-cpp and set MICME_WHISPER_CPP_MODEL, " +
|
|
28
|
-
"install openai-whisper, or set MICME_TRANSCRIBE_COMMAND.",
|
|
29
|
-
);
|
|
29
|
+
throw new Error(formatTranscriptionPlan(plan));
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export async function transcribeWithCustomCommand(template: string, audioPath: string, tempDir: string) {
|
|
@@ -45,8 +45,15 @@ export async function transcribeWithCustomCommand(template: string, audioPath: s
|
|
|
45
45
|
return result.stdout;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export async function transcribeWithWhisperCpp(
|
|
49
|
-
|
|
48
|
+
export async function transcribeWithWhisperCpp(
|
|
49
|
+
binary: string,
|
|
50
|
+
modelPath: string,
|
|
51
|
+
audioPath: string,
|
|
52
|
+
tempDir: string,
|
|
53
|
+
ctx?: ExtensionContext,
|
|
54
|
+
options: { allowDownload?: boolean } = {},
|
|
55
|
+
) {
|
|
56
|
+
await ensureWhisperCppModel(modelPath, ctx, options);
|
|
50
57
|
const outputBase = join(tempDir, "whisper-cpp");
|
|
51
58
|
const args = ["-m", modelPath, "-f", audioPath, "-otxt", "-of", outputBase, "-nt", "-np", "-nf", "-sns"];
|
|
52
59
|
const language = env("MICME_LANGUAGE");
|
|
@@ -66,8 +73,8 @@ export async function transcribeWithWhisperCpp(binary: string, modelPath: string
|
|
|
66
73
|
return result.stdout;
|
|
67
74
|
}
|
|
68
75
|
|
|
69
|
-
export async function transcribeWithOpenAiWhisper(binary: string, audioPath: string, tempDir: string) {
|
|
70
|
-
const model = env("MICME_WHISPER_MODEL") || "base.en";
|
|
76
|
+
export async function transcribeWithOpenAiWhisper(binary: string, audioPath: string, tempDir: string, modelName?: string) {
|
|
77
|
+
const model = modelName || env("MICME_WHISPER_MODEL") || "base.en";
|
|
71
78
|
const args = [
|
|
72
79
|
audioPath,
|
|
73
80
|
"--model",
|
|
@@ -106,26 +113,3 @@ export async function transcribeWithOpenAiWhisper(binary: string, audioPath: str
|
|
|
106
113
|
|
|
107
114
|
return result.stdout;
|
|
108
115
|
}
|
|
109
|
-
|
|
110
|
-
export function getWhisperCppBinary() {
|
|
111
|
-
const configured = env("MICME_WHISPER_CPP_BIN")?.trim();
|
|
112
|
-
if (configured) return resolveExecutableConfig(configured, "MICME_WHISPER_CPP_BIN");
|
|
113
|
-
return findExecutable(["whisper-cli", "whisper-cpp"]);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function getWhisperStreamBinary() {
|
|
117
|
-
const configured = env("MICME_WHISPER_STREAM_BIN")?.trim();
|
|
118
|
-
if (configured) return resolveExecutableConfig(configured, "MICME_WHISPER_STREAM_BIN");
|
|
119
|
-
return findExecutable(["whisper-stream"]);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function resolveExecutableConfig(value: string, configName: string) {
|
|
123
|
-
const expanded = expandConfigPath(value);
|
|
124
|
-
if (/[/\\]/.test(expanded)) {
|
|
125
|
-
if (!existsSync(expanded)) throw new Error(`${configName} is set but not found: ${expanded}`);
|
|
126
|
-
return expanded;
|
|
127
|
-
}
|
|
128
|
-
const executable = findExecutable([expanded]);
|
|
129
|
-
if (!executable) throw new Error(`${configName} is set but not found on PATH: ${expanded}`);
|
|
130
|
-
return executable;
|
|
131
|
-
}
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
import type { ChildProcess } from "node:child_process";
|
|
2
2
|
|
|
3
3
|
export type TranscriptionMode = "clip" | "stream";
|
|
4
|
+
export type TranscribeBackend = "auto" | "whisper.cpp" | "python" | "custom";
|
|
5
|
+
export type EffectiveBackend = "whisper.cpp" | "python" | "custom" | "none";
|
|
6
|
+
export type WhisperCppModelSource = "explicit-path" | "configured-name" | "default-name";
|
|
7
|
+
export type TranscriptionModelSource = WhisperCppModelSource | "python-name" | "custom-command";
|
|
8
|
+
|
|
9
|
+
export type ResolvedWhisperCppModel = {
|
|
10
|
+
path: string;
|
|
11
|
+
modelName?: string;
|
|
12
|
+
source: WhisperCppModelSource;
|
|
13
|
+
configuredValue?: string;
|
|
14
|
+
exists: boolean;
|
|
15
|
+
downloadable: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type ResolvedTranscriptionPlan = {
|
|
19
|
+
requestedBackend: TranscribeBackend;
|
|
20
|
+
effectiveBackend: EffectiveBackend;
|
|
21
|
+
reason: string;
|
|
22
|
+
command?: string;
|
|
23
|
+
binary?: string;
|
|
24
|
+
modelName?: string;
|
|
25
|
+
modelPath?: string;
|
|
26
|
+
modelSource?: TranscriptionModelSource;
|
|
27
|
+
modelDownloadable?: boolean;
|
|
28
|
+
warnings: string[];
|
|
29
|
+
};
|
|
4
30
|
|
|
5
31
|
export type CommandSpec = {
|
|
6
32
|
command: string;
|
|
@@ -35,8 +61,9 @@ export type StreamingState = {
|
|
|
35
61
|
previewText: string;
|
|
36
62
|
outputBuffer: string;
|
|
37
63
|
lastText: string;
|
|
38
|
-
pendingWords: string[];
|
|
39
64
|
emittedWords: string[];
|
|
65
|
+
candidateWords: string[];
|
|
66
|
+
lastHypothesisWords: string[];
|
|
40
67
|
startedAt: number;
|
|
41
68
|
firstOutputAt?: number;
|
|
42
69
|
firstPreviewAt?: number;
|
|
@@ -70,6 +97,7 @@ export type ModelCandidate = {
|
|
|
70
97
|
value: string;
|
|
71
98
|
description: string;
|
|
72
99
|
installed: boolean;
|
|
100
|
+
kind?: "model-name" | "path";
|
|
73
101
|
};
|
|
74
102
|
|
|
75
103
|
export type AudioDeviceCandidate = {
|