@senad-d/micme 0.1.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/.env.example +75 -0
- package/CHANGELOG.md +18 -0
- package/LICENSE +21 -0
- package/README.md +259 -0
- package/SECURITY.md +78 -0
- package/img/micme_logo.png +0 -0
- package/package.json +68 -0
- package/scripts/check-package-contents.mjs +50 -0
- package/scripts/doctor.mjs +162 -0
- package/src/audio-level.ts +51 -0
- package/src/audio.ts +199 -0
- package/src/config.ts +256 -0
- package/src/constants.ts +43 -0
- package/src/editor.ts +122 -0
- package/src/extension.ts +351 -0
- package/src/models.ts +236 -0
- package/src/processes.ts +231 -0
- package/src/recording-widget.ts +61 -0
- package/src/settings.ts +948 -0
- package/src/streaming.ts +237 -0
- package/src/transcript-delivery.ts +24 -0
- package/src/transcription.ts +131 -0
- package/src/types.ts +80 -0
- package/tsconfig.json +15 -0
package/src/streaming.ts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
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";
|
|
6
|
+
import {
|
|
7
|
+
env,
|
|
8
|
+
envFlag,
|
|
9
|
+
getStreamKeepContext,
|
|
10
|
+
getStreamKeepMs,
|
|
11
|
+
getStreamLengthMs,
|
|
12
|
+
getStreamMaxTokens,
|
|
13
|
+
getStreamStepMs,
|
|
14
|
+
getStreamVadThreshold,
|
|
15
|
+
getStreamWordsPerChunk,
|
|
16
|
+
} from "./config.ts";
|
|
17
|
+
import { pasteOrSubmitTranscript } from "./transcript-delivery.ts";
|
|
18
|
+
import { shellQuote } from "./processes.ts";
|
|
19
|
+
import type { CommandSpec, Recording, StreamingState } from "./types.ts";
|
|
20
|
+
|
|
21
|
+
export function buildWhisperStreamCommand(binary: string, modelPath: string, tempDir: string): CommandSpec {
|
|
22
|
+
const args = [
|
|
23
|
+
"-m",
|
|
24
|
+
modelPath,
|
|
25
|
+
"--step",
|
|
26
|
+
String(getStreamStepMs()),
|
|
27
|
+
"--length",
|
|
28
|
+
String(getStreamLengthMs()),
|
|
29
|
+
"--keep",
|
|
30
|
+
String(getStreamKeepMs()),
|
|
31
|
+
"--max-tokens",
|
|
32
|
+
String(getStreamMaxTokens()),
|
|
33
|
+
"--vad-thold",
|
|
34
|
+
String(getStreamVadThreshold()),
|
|
35
|
+
"-nf",
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
if (getStreamKeepContext()) args.push("--keep-context");
|
|
39
|
+
|
|
40
|
+
const capture = env("MICME_STREAM_CAPTURE");
|
|
41
|
+
if (capture?.trim()) args.push("--capture", capture.trim());
|
|
42
|
+
|
|
43
|
+
const language = env("MICME_LANGUAGE");
|
|
44
|
+
if (language?.trim()) args.push("-l", language.trim());
|
|
45
|
+
|
|
46
|
+
return { command: binary, args, display: `${binary} ${args.map(shellQuote).join(" ")}` };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function handleStreamingOutput(ctx: ExtensionContext, active: Recording, chunk: string) {
|
|
50
|
+
drainStreamingOutput(ctx, active, false, chunk);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function drainStreamingOutput(ctx: ExtensionContext, active: Recording, force: boolean, chunk = "") {
|
|
54
|
+
if (!active.streaming) return;
|
|
55
|
+
for (const part of readStreamingOutputFrames(active.streaming, chunk, force)) {
|
|
56
|
+
const text = sanitizeStreamingText(part);
|
|
57
|
+
if (!text) {
|
|
58
|
+
if (shouldResetStreamingPending(part)) {
|
|
59
|
+
active.streaming.pendingWords = [];
|
|
60
|
+
renderStreamingPreview(ctx, active.streaming, false);
|
|
61
|
+
}
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (!active.streaming.firstOutputAt) active.streaming.firstOutputAt = Date.now();
|
|
65
|
+
const next = diffStreamingText(active.streaming.emittedWords.slice(-160).join(" "), text);
|
|
66
|
+
active.streaming.lastText = text;
|
|
67
|
+
if (!next) {
|
|
68
|
+
active.streaming.pendingWords = [];
|
|
69
|
+
renderStreamingPreview(ctx, active.streaming, false);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
active.audioLevel = () => 0.55;
|
|
73
|
+
queueStableStreamingWords(ctx, active.streaming, splitStreamingWords(next));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readStreamingOutputFrames(state: StreamingState, chunk: string, force: boolean) {
|
|
78
|
+
// whisper-stream rewrites one terminal line with \r; wait for delimiters so split stdout chunks do not become words.
|
|
79
|
+
state.outputBuffer = `${state.outputBuffer}${chunk}`.slice(-MAX_CAPTURED_OUTPUT_CHARS);
|
|
80
|
+
const frames: string[] = [];
|
|
81
|
+
|
|
82
|
+
while (true) {
|
|
83
|
+
const match = state.outputBuffer.match(/[\r\n]/);
|
|
84
|
+
if (!match || match.index === undefined) break;
|
|
85
|
+
frames.push(state.outputBuffer.slice(0, match.index));
|
|
86
|
+
state.outputBuffer = state.outputBuffer.slice(match.index + 1).replace(/^[\r\n]+/, "");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (force && state.outputBuffer.trim()) {
|
|
90
|
+
frames.push(state.outputBuffer);
|
|
91
|
+
state.outputBuffer = "";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return frames;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function sanitizeStreamingText(text: string) {
|
|
98
|
+
const cleaned = stripStreamingControls(text)
|
|
99
|
+
.replace(/<\|[^|]*\|>/g, " ")
|
|
100
|
+
.replace(/^\s*#+\s*Transcription\s+\d+\s+END\s*$/i, " ")
|
|
101
|
+
.replace(/\[[^\]]*\]/g, " ")
|
|
102
|
+
.replace(/\([^)]*\)/g, " ")
|
|
103
|
+
.replace(/\s+/g, " ")
|
|
104
|
+
.trim();
|
|
105
|
+
return isLikelyStreamingHallucination(cleaned) ? "" : cleaned;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function stripStreamingControls(text: string) {
|
|
109
|
+
return text.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, " ").replace(/\s+/g, " ").trim();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function diffStreamingText(previous: string, current: string) {
|
|
113
|
+
const previousWords = splitStreamingWords(previous);
|
|
114
|
+
const currentWords = splitStreamingWords(current);
|
|
115
|
+
if (currentWords.length === 0) return "";
|
|
116
|
+
const overlap = streamingWordOverlap(previousWords, currentWords);
|
|
117
|
+
return currentWords.slice(overlap).join(" ");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function isLikelyStreamingHallucination(text: string) {
|
|
121
|
+
if (!text) return true;
|
|
122
|
+
const normalized = text.toLowerCase().replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, "");
|
|
123
|
+
if (/^(?:start speaking|blank audio)$/i.test(normalized)) return true;
|
|
124
|
+
if (/^(?:you|yeah|okay|ok|uh|um|hmm)$/i.test(normalized)) return true;
|
|
125
|
+
if (/^(?:thank you|thanks for watching|thank you very much)$/i.test(normalized)) return true;
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function shouldResetStreamingPending(text: string) {
|
|
130
|
+
const cleaned = stripStreamingControls(text);
|
|
131
|
+
if (!cleaned) return false;
|
|
132
|
+
if (isLikelyStreamingHallucination(cleaned)) return true;
|
|
133
|
+
return /^\[[^\]]+\]$/.test(cleaned) || /^\([^)]*\)$/.test(cleaned);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function queueStableStreamingWords(ctx: ExtensionContext, state: StreamingState, currentWords: string[]) {
|
|
137
|
+
// Popular Whisper streaming frontends use local agreement: show/commit only the common prefix of consecutive hypotheses.
|
|
138
|
+
const stableCount = commonStreamingPrefixLength(state.pendingWords, currentWords);
|
|
139
|
+
const maxConfirmed = state.emittedWords.length === 0 && currentWords.length < STREAM_MIN_INITIAL_WORDS ? 0 : stableCount;
|
|
140
|
+
const confirmedCount = Math.min(maxConfirmed, getStreamWordsPerChunk());
|
|
141
|
+
if (confirmedCount > 0) queueStreamingWords(state, currentWords.slice(0, confirmedCount));
|
|
142
|
+
state.pendingWords = currentWords.slice(confirmedCount);
|
|
143
|
+
renderStreamingPreview(ctx, state, false);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function flushPendingStreamingWords(ctx: ExtensionContext, state: StreamingState) {
|
|
147
|
+
if (state.pendingWords.length > 0) {
|
|
148
|
+
queueStreamingWords(state, state.pendingWords);
|
|
149
|
+
state.pendingWords = [];
|
|
150
|
+
}
|
|
151
|
+
renderStreamingPreview(ctx, state, true);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function queueStreamingWords(state: StreamingState, words: string[]) {
|
|
155
|
+
const nextWords = words.filter(Boolean);
|
|
156
|
+
if (nextWords.length === 0) return;
|
|
157
|
+
state.emittedWords.push(...nextWords);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function splitStreamingWords(text: string) {
|
|
161
|
+
return text.split(/\s+/).filter(Boolean);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function streamingWordOverlap(previousWords: string[], currentWords: string[]) {
|
|
165
|
+
let overlap = 0;
|
|
166
|
+
const maxOverlap = Math.min(previousWords.length, currentWords.length);
|
|
167
|
+
for (let size = 1; size <= maxOverlap; size++) {
|
|
168
|
+
if (streamingWordsEqual(previousWords.slice(-size), currentWords.slice(0, size))) {
|
|
169
|
+
overlap = size;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return overlap;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function commonStreamingPrefixLength(previousWords: string[], currentWords: string[]) {
|
|
176
|
+
const maxPrefix = Math.min(previousWords.length, currentWords.length);
|
|
177
|
+
let count = 0;
|
|
178
|
+
while (count < maxPrefix && normalizeStreamingWord(previousWords[count] ?? "") === normalizeStreamingWord(currentWords[count] ?? "")) {
|
|
179
|
+
count++;
|
|
180
|
+
}
|
|
181
|
+
return count;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function streamingWordsEqual(left: string[], right: string[]) {
|
|
185
|
+
if (left.length !== right.length) return false;
|
|
186
|
+
return left.every((word, index) => normalizeStreamingWord(word) === normalizeStreamingWord(right[index] ?? ""));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function normalizeStreamingWord(word: string) {
|
|
190
|
+
const normalized = word.toLowerCase().replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, "");
|
|
191
|
+
return normalized || word.toLowerCase();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function getStreamingTranscript(state: StreamingState) {
|
|
195
|
+
return [...state.emittedWords, ...state.pendingWords].join(" ");
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function renderStreamingPreview(ctx: ExtensionContext, state: StreamingState, trailingSpace: boolean) {
|
|
199
|
+
const transcript = getStreamingTranscript(state);
|
|
200
|
+
const suffix = transcript ? `${transcript}${trailingSpace ? " " : ""}` : "";
|
|
201
|
+
const nextText = `${state.baseText}${suffix}`;
|
|
202
|
+
if (nextText === state.previewText) return;
|
|
203
|
+
ctx.ui.setEditorText(nextText);
|
|
204
|
+
state.previewText = nextText;
|
|
205
|
+
if (transcript && !state.firstPreviewAt) {
|
|
206
|
+
state.firstPreviewAt = Date.now();
|
|
207
|
+
if (envFlag("MICME_STREAM_DIAGNOSTICS")) {
|
|
208
|
+
ctx.ui.notify(`Micme stream first preview: ${state.firstPreviewAt - state.startedAt} ms`, "info");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function showStreamingDiagnostics(ctx: ExtensionContext, state: StreamingState) {
|
|
214
|
+
if (!envFlag("MICME_STREAM_DIAGNOSTICS")) return;
|
|
215
|
+
const firstOutput = state.firstOutputAt ? `${state.firstOutputAt - state.startedAt} ms` : "none";
|
|
216
|
+
const firstPreview = state.firstPreviewAt ? `${state.firstPreviewAt - state.startedAt} ms` : "none";
|
|
217
|
+
ctx.ui.notify(`Micme stream timings: first output ${firstOutput}, first preview ${firstPreview}`, "info");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export async function pasteOrSubmitFinalStreamingTranscript(ctx: ExtensionContext, pi: ExtensionAPI, state: StreamingState, transcript: string) {
|
|
221
|
+
if (envFlag("MICME_AUTO_SUBMIT")) {
|
|
222
|
+
ctx.ui.setEditorText(state.baseText);
|
|
223
|
+
await pasteOrSubmitTranscript(ctx, pi, transcript);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const suffix = /\s$/.test(transcript) ? transcript : `${transcript} `;
|
|
228
|
+
const nextText = `${state.baseText}${suffix}`;
|
|
229
|
+
ctx.ui.setEditorText(nextText);
|
|
230
|
+
state.previewText = nextText;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function clearStreamingFlush(state: StreamingState) {
|
|
234
|
+
if (!state.flushTimer) return;
|
|
235
|
+
clearTimeout(state.flushTimer);
|
|
236
|
+
state.flushTimer = undefined;
|
|
237
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { envFlag } from "./config.ts";
|
|
3
|
+
import { normalizeTranscript } from "./processes.ts";
|
|
4
|
+
|
|
5
|
+
export async function pasteOrSubmitTranscript(ctx: ExtensionContext, pi: ExtensionAPI, transcript: string) {
|
|
6
|
+
const text = normalizeTranscript(transcript);
|
|
7
|
+
if (!text) {
|
|
8
|
+
ctx.ui.notify("No previous Micme transcript is available.", "warning");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (envFlag("MICME_AUTO_SUBMIT")) {
|
|
13
|
+
if (ctx.isIdle()) {
|
|
14
|
+
pi.sendUserMessage(text);
|
|
15
|
+
} else {
|
|
16
|
+
pi.sendUserMessage(text, { deliverAs: "followUp" });
|
|
17
|
+
ctx.ui.notify("Micme transcript queued as a follow-up message.", "info");
|
|
18
|
+
}
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const suffix = /\s$/.test(text) ? text : `${text} `;
|
|
23
|
+
ctx.ui.pasteToEditor(suffix);
|
|
24
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { basename, join } from "node:path";
|
|
5
|
+
import { env, expandConfigPath, getTranscribeTimeoutMs } from "./config.ts";
|
|
6
|
+
import { ensureWhisperCppModel, getDefaultWhisperCppModelPath } from "./models.ts";
|
|
7
|
+
import { findExecutable, formatRunExit, replacePlaceholders, runProcess, runShell } from "./processes.ts";
|
|
8
|
+
|
|
9
|
+
export async function transcribe(audioPath: string, tempDir: string, ctx?: ExtensionContext): Promise<string> {
|
|
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
|
+
}
|
|
20
|
+
|
|
21
|
+
const openAiWhisper = findExecutable(["whisper"]);
|
|
22
|
+
if (openAiWhisper) {
|
|
23
|
+
return transcribeWithOpenAiWhisper(openAiWhisper, audioPath, tempDir);
|
|
24
|
+
}
|
|
25
|
+
|
|
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
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function transcribeWithCustomCommand(template: string, audioPath: string, tempDir: string) {
|
|
33
|
+
const transcriptPath = join(tempDir, "transcript.txt");
|
|
34
|
+
const command = replacePlaceholders(template, { audio: audioPath, tempDir, transcript: transcriptPath });
|
|
35
|
+
const result = await runShell(command, getTranscribeTimeoutMs());
|
|
36
|
+
if (result.code !== 0) {
|
|
37
|
+
throw new Error(`MICME_TRANSCRIBE_COMMAND failed (${formatRunExit(result)}):\n${result.stderr || result.stdout}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (existsSync(transcriptPath)) {
|
|
41
|
+
const fromFile = await readFile(transcriptPath, "utf8");
|
|
42
|
+
if (fromFile.trim()) return fromFile;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return result.stdout;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function transcribeWithWhisperCpp(binary: string, modelPath: string, audioPath: string, tempDir: string, ctx?: ExtensionContext) {
|
|
49
|
+
await ensureWhisperCppModel(modelPath, ctx);
|
|
50
|
+
const outputBase = join(tempDir, "whisper-cpp");
|
|
51
|
+
const args = ["-m", modelPath, "-f", audioPath, "-otxt", "-of", outputBase, "-nt", "-np", "-nf", "-sns"];
|
|
52
|
+
const language = env("MICME_LANGUAGE");
|
|
53
|
+
if (language?.trim()) args.push("-l", language.trim());
|
|
54
|
+
|
|
55
|
+
const result = await runProcess(binary, args, getTranscribeTimeoutMs());
|
|
56
|
+
if (result.code !== 0) {
|
|
57
|
+
throw new Error(`whisper.cpp failed (${formatRunExit(result)}):\n${result.stderr || result.stdout}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const outputPath = `${outputBase}.txt`;
|
|
61
|
+
if (existsSync(outputPath)) {
|
|
62
|
+
const fromFile = await readFile(outputPath, "utf8");
|
|
63
|
+
if (fromFile.trim()) return fromFile;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return result.stdout;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function transcribeWithOpenAiWhisper(binary: string, audioPath: string, tempDir: string) {
|
|
70
|
+
const model = env("MICME_WHISPER_MODEL") || "base.en";
|
|
71
|
+
const args = [
|
|
72
|
+
audioPath,
|
|
73
|
+
"--model",
|
|
74
|
+
model,
|
|
75
|
+
"--output_format",
|
|
76
|
+
"txt",
|
|
77
|
+
"--output_dir",
|
|
78
|
+
tempDir,
|
|
79
|
+
"--verbose",
|
|
80
|
+
"False",
|
|
81
|
+
"--condition_on_previous_text",
|
|
82
|
+
"False",
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
const language = env("MICME_LANGUAGE");
|
|
86
|
+
if (language?.trim() && language.trim().toLowerCase() !== "auto") args.push("--language", language.trim());
|
|
87
|
+
|
|
88
|
+
const device = env("MICME_WHISPER_DEVICE");
|
|
89
|
+
if (device?.trim()) args.push("--device", device.trim());
|
|
90
|
+
|
|
91
|
+
const fp16 = env("MICME_WHISPER_FP16");
|
|
92
|
+
if (fp16?.trim()) args.push("--fp16", fp16.trim());
|
|
93
|
+
else args.push("--fp16", "False");
|
|
94
|
+
|
|
95
|
+
const result = await runProcess(binary, args, getTranscribeTimeoutMs());
|
|
96
|
+
if (result.code !== 0) {
|
|
97
|
+
throw new Error(`openai-whisper failed (${formatRunExit(result)}):\n${result.stderr || result.stdout}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const txtName = basename(audioPath).replace(/\.[^.]+$/, ".txt");
|
|
101
|
+
const outputPath = join(tempDir, txtName);
|
|
102
|
+
if (existsSync(outputPath)) {
|
|
103
|
+
const fromFile = await readFile(outputPath, "utf8");
|
|
104
|
+
if (fromFile.trim()) return fromFile;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return result.stdout;
|
|
108
|
+
}
|
|
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
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ChildProcess } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
export type TranscriptionMode = "clip" | "stream";
|
|
4
|
+
|
|
5
|
+
export type CommandSpec = {
|
|
6
|
+
command: string;
|
|
7
|
+
args: string[];
|
|
8
|
+
display: string;
|
|
9
|
+
meterFromStdout?: boolean;
|
|
10
|
+
stopInput?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type RunResult = {
|
|
14
|
+
code: number | null;
|
|
15
|
+
signal: NodeJS.Signals | null;
|
|
16
|
+
stdout: string;
|
|
17
|
+
stderr: string;
|
|
18
|
+
timedOut: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type ExitResult = {
|
|
22
|
+
code: number | null;
|
|
23
|
+
signal: NodeJS.Signals | null;
|
|
24
|
+
error?: Error;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type AudioDiagnostics = {
|
|
28
|
+
meanVolumeDb?: number;
|
|
29
|
+
maxVolumeDb?: number;
|
|
30
|
+
raw: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type StreamingState = {
|
|
34
|
+
baseText: string;
|
|
35
|
+
previewText: string;
|
|
36
|
+
outputBuffer: string;
|
|
37
|
+
lastText: string;
|
|
38
|
+
pendingWords: string[];
|
|
39
|
+
emittedWords: string[];
|
|
40
|
+
startedAt: number;
|
|
41
|
+
firstOutputAt?: number;
|
|
42
|
+
firstPreviewAt?: number;
|
|
43
|
+
flushTimer?: ReturnType<typeof setTimeout>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type Recording = {
|
|
47
|
+
process: ChildProcess;
|
|
48
|
+
audioPath: string;
|
|
49
|
+
tempDir: string;
|
|
50
|
+
startedAt: number;
|
|
51
|
+
command: CommandSpec;
|
|
52
|
+
audioLevel: () => number;
|
|
53
|
+
streaming?: StreamingState;
|
|
54
|
+
clipRecording?: Recording;
|
|
55
|
+
stopRequested?: boolean;
|
|
56
|
+
exitPromise: Promise<ExitResult>;
|
|
57
|
+
isSettled: () => boolean;
|
|
58
|
+
stdout: () => string;
|
|
59
|
+
stderr: () => string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type DotEnvState = {
|
|
63
|
+
path: string;
|
|
64
|
+
values: Record<string, string>;
|
|
65
|
+
error?: string;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type ModelCandidate = {
|
|
69
|
+
label: string;
|
|
70
|
+
value: string;
|
|
71
|
+
description: string;
|
|
72
|
+
installed: boolean;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export type AudioDeviceCandidate = {
|
|
76
|
+
label: string;
|
|
77
|
+
value: string;
|
|
78
|
+
description: string;
|
|
79
|
+
};
|
|
80
|
+
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
|
10
|
+
"noEmit": true,
|
|
11
|
+
"allowImportingTsExtensions": true,
|
|
12
|
+
"types": ["node"]
|
|
13
|
+
},
|
|
14
|
+
"include": ["src/**/*.ts"]
|
|
15
|
+
}
|