@senad-d/micme 0.2.1 → 0.3.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 +4 -0
- package/README.md +17 -12
- package/SECURITY.md +1 -1
- package/micme.example.json +5 -2
- package/micme.schema.json +34 -3
- package/package.json +16 -5
- package/scripts/check-package-contents.mjs +1 -0
- package/scripts/doctor.mjs +42 -5
- package/src/audio.ts +64 -21
- package/src/backends.ts +14 -3
- package/src/config.ts +75 -11
- package/src/constants.ts +1 -1
- package/src/extension.ts +19 -16
- package/src/models.ts +76 -18
- package/src/recording-dir.ts +54 -0
- package/src/settings.ts +171 -34
- package/src/streaming.ts +8 -2
- package/src/transcription.ts +17 -7
- package/src/types.ts +1 -0
package/src/config.ts
CHANGED
|
@@ -75,7 +75,7 @@ export async function writeMicmeConfigValue(key: string, value: string) {
|
|
|
75
75
|
await writeMicmeConfigValues({ [key]: value });
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
export async function writeMicmeConfigValues(values: Record<string, string>) {
|
|
78
|
+
export async function writeMicmeConfigValues(values: Record<string, string | undefined>) {
|
|
79
79
|
for (const key of Object.keys(values)) {
|
|
80
80
|
if (!key.startsWith("MICME_")) throw new Error(`Micme config keys must start with MICME_: ${key}`);
|
|
81
81
|
}
|
|
@@ -85,7 +85,13 @@ export async function writeMicmeConfigValues(values: Record<string, string>) {
|
|
|
85
85
|
const existing = readMicmeJsonObjectForWrite(configPath);
|
|
86
86
|
const next: JsonObject = { ...existing };
|
|
87
87
|
|
|
88
|
-
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
|
+
}
|
|
89
95
|
|
|
90
96
|
await mkdir(configDir, { recursive: true });
|
|
91
97
|
const tempPath = join(configDir, `.micme.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`);
|
|
@@ -147,14 +153,34 @@ export function env(name: string) {
|
|
|
147
153
|
return process.env[name] ?? micmeConfigState.values[name];
|
|
148
154
|
}
|
|
149
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
|
+
|
|
150
164
|
export function getShortcut() {
|
|
151
|
-
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;
|
|
152
172
|
}
|
|
153
173
|
|
|
154
174
|
export function getTranscriptionMode() {
|
|
155
175
|
return env("MICME_TRANSCRIPTION_MODE") === "stream" ? "stream" : "clip";
|
|
156
176
|
}
|
|
157
177
|
|
|
178
|
+
export function getTranslateToEnglishLanguage() {
|
|
179
|
+
const value = env("MICME_TRANSLATE_TO_ENGLISH")?.trim();
|
|
180
|
+
if (!value || /^(0|false|no|off)$/i.test(value)) return undefined;
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
|
|
158
184
|
export function getTranscribeBackend(): TranscribeBackend {
|
|
159
185
|
const value = env("MICME_TRANSCRIBE_BACKEND")?.trim();
|
|
160
186
|
return isTranscribeBackend(value) ? value : DEFAULT_TRANSCRIBE_BACKEND;
|
|
@@ -165,13 +191,15 @@ export function isTranscribeBackend(value: string | undefined): value is Transcr
|
|
|
165
191
|
}
|
|
166
192
|
|
|
167
193
|
export function getPrintableShortcuts() {
|
|
168
|
-
const
|
|
169
|
-
if (
|
|
170
|
-
return
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
.filter(Boolean);
|
|
194
|
+
const shortcut = env("MICME_SHORTCUT");
|
|
195
|
+
if (shortcut !== undefined) {
|
|
196
|
+
if (shortcut && !isTerminalShortcut(shortcut)) return splitShortcutValues(shortcut);
|
|
197
|
+
const legacyConfigured = env("MICME_PRINTABLE_SHORTCUTS");
|
|
198
|
+
return legacyConfigured !== undefined ? splitShortcutValues(legacyConfigured) : [];
|
|
174
199
|
}
|
|
200
|
+
|
|
201
|
+
const legacyConfigured = env("MICME_PRINTABLE_SHORTCUTS");
|
|
202
|
+
if (legacyConfigured !== undefined) return splitShortcutValues(legacyConfigured);
|
|
175
203
|
return process.platform === "darwin" ? [DEFAULT_MACOS_PRINTABLE_SHORTCUT] : [];
|
|
176
204
|
}
|
|
177
205
|
|
|
@@ -179,6 +207,22 @@ export function matchesPrintableMicmeShortcut(data: string) {
|
|
|
179
207
|
return getPrintableShortcuts().includes(data);
|
|
180
208
|
}
|
|
181
209
|
|
|
210
|
+
export function isTerminalShortcut(value: string) {
|
|
211
|
+
const normalized = value.trim();
|
|
212
|
+
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);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function firstShortcutValue(value: string | undefined) {
|
|
216
|
+
return splitShortcutValues(value ?? "")[0] ?? "";
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function splitShortcutValues(value: string) {
|
|
220
|
+
return value
|
|
221
|
+
.split(",")
|
|
222
|
+
.map((candidate) => candidate.trim())
|
|
223
|
+
.filter(Boolean);
|
|
224
|
+
}
|
|
225
|
+
|
|
182
226
|
export function getTranscribeTimeoutMs() {
|
|
183
227
|
const value = Number(env("MICME_TRANSCRIBE_TIMEOUT_MS"));
|
|
184
228
|
return Number.isFinite(value) && value > 0 ? value : DEFAULT_TRANSCRIBE_TIMEOUT_MS;
|
|
@@ -230,8 +274,10 @@ export function getStreamWordsPerChunk() {
|
|
|
230
274
|
}
|
|
231
275
|
|
|
232
276
|
export function getRecordSampleRate() {
|
|
233
|
-
const
|
|
234
|
-
|
|
277
|
+
const configured = env("MICME_RECORD_SAMPLE_RATE")?.trim();
|
|
278
|
+
if (!configured || /^auto$/i.test(configured)) return undefined;
|
|
279
|
+
const value = Number(configured);
|
|
280
|
+
return Number.isFinite(value) && value > 0 ? Math.round(value) : undefined;
|
|
235
281
|
}
|
|
236
282
|
|
|
237
283
|
export function getTranscribeSampleRate() {
|
|
@@ -239,6 +285,24 @@ export function getTranscribeSampleRate() {
|
|
|
239
285
|
return Number.isFinite(value) && value > 0 ? Math.round(value) : DEFAULT_TRANSCRIBE_SAMPLE_RATE;
|
|
240
286
|
}
|
|
241
287
|
|
|
288
|
+
export function getRecordMeter() {
|
|
289
|
+
return envFlag("MICME_RECORD_METER");
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function getRecordSync() {
|
|
293
|
+
const value = env("MICME_RECORD_SYNC");
|
|
294
|
+
return value === undefined ? true : /^(1|true|yes|on)$/i.test(value);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export function getAvfoundationDropLateFrames() {
|
|
298
|
+
return envFlag("MICME_AVFOUNDATION_DROP_LATE_FRAMES");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function getAvfoundationInputSampleRate() {
|
|
302
|
+
const value = Number(env("MICME_AVFOUNDATION_INPUT_SAMPLE_RATE"));
|
|
303
|
+
return Number.isFinite(value) && value > 0 ? Math.round(value) : undefined;
|
|
304
|
+
}
|
|
305
|
+
|
|
242
306
|
export function getAudioFilter() {
|
|
243
307
|
if (env("MICME_AUDIO_FILTER") !== undefined) return env("MICME_AUDIO_FILTER")!.trim();
|
|
244
308
|
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;
|
package/src/extension.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
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
6
|
getShortcut,
|
|
10
7
|
getStreamFinalizeWithClip,
|
|
8
|
+
getTerminalShortcut,
|
|
11
9
|
getTranscriptionMode,
|
|
12
10
|
reloadMicmeConfig,
|
|
13
11
|
} from "./config.ts";
|
|
@@ -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,
|
|
@@ -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);
|
|
@@ -247,7 +250,7 @@ async function startStreamingTranscription(ctx: ExtensionContext, stopHint = get
|
|
|
247
250
|
|
|
248
251
|
await ensureWhisperCppModel(plan.modelPath, ctx, { allowDownload: plan.modelDownloadable !== false });
|
|
249
252
|
|
|
250
|
-
const tempDir = await
|
|
253
|
+
const tempDir = await createRecordingDirectory(ctx.cwd, envFlag("MICME_KEEP_AUDIO"), "micme-stream-");
|
|
251
254
|
const command = buildWhisperStreamCommand(plan.binary, plan.modelPath, tempDir);
|
|
252
255
|
const active = spawnRecording(command, "", tempDir);
|
|
253
256
|
const baseText = ctx.ui.getEditorText();
|
package/src/models.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { once } from "node:events";
|
|
|
3
3
|
import { createWriteStream, existsSync, readdirSync, statSync } from "node:fs";
|
|
4
4
|
import { mkdir, rename, unlink } from "node:fs/promises";
|
|
5
5
|
import { homedir } from "node:os";
|
|
6
|
-
import { basename, dirname, join } from "node:path";
|
|
6
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
7
7
|
import { finished } from "node:stream/promises";
|
|
8
8
|
import {
|
|
9
9
|
DEFAULT_PYTHON_WHISPER_MODEL_NAME,
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
WHISPER_CPP_MODEL_BASE_URL,
|
|
14
14
|
WHISPER_CPP_MODEL_NAMES,
|
|
15
15
|
} from "./constants.ts";
|
|
16
|
-
import { env, expandConfigPath } from "./config.ts";
|
|
16
|
+
import { env, expandConfigPath, getTranslateToEnglishLanguage } from "./config.ts";
|
|
17
17
|
import { findExecutable, runProcess } from "./processes.ts";
|
|
18
18
|
import type { ModelCandidate, ResolvedWhisperCppModel } from "./types.ts";
|
|
19
19
|
|
|
@@ -118,33 +118,90 @@ export function describeWhisperModel(name: string) {
|
|
|
118
118
|
|
|
119
119
|
export function resolveWhisperCppModel(): ResolvedWhisperCppModel {
|
|
120
120
|
const explicitPath = env("MICME_WHISPER_CPP_MODEL")?.trim();
|
|
121
|
-
if (explicitPath)
|
|
122
|
-
const path = expandConfigPath(explicitPath);
|
|
123
|
-
return {
|
|
124
|
-
path,
|
|
125
|
-
modelName: getWhisperCppModelNameFromPath(path),
|
|
126
|
-
source: "explicit-path",
|
|
127
|
-
configuredValue: explicitPath,
|
|
128
|
-
exists: existsSync(path),
|
|
129
|
-
downloadable: isDownloadableWhisperCppModelPath(path),
|
|
130
|
-
};
|
|
131
|
-
}
|
|
121
|
+
if (explicitPath) return resolveExplicitWhisperCppModel(expandConfigPath(explicitPath), explicitPath);
|
|
132
122
|
|
|
133
123
|
const configuredName = env("MICME_DEFAULT_WHISPER_CPP_MODEL")?.trim();
|
|
134
|
-
const
|
|
124
|
+
const rawModelName = configuredName || DEFAULT_WHISPER_CPP_MODEL_NAME;
|
|
125
|
+
const modelName = getTranslationAwareWhisperModelName(rawModelName);
|
|
135
126
|
const path = join(getWhisperCppModelCacheDir(), `ggml-${modelName}.bin`);
|
|
136
|
-
return {
|
|
137
|
-
path,
|
|
127
|
+
return buildResolvedWhisperCppModel(path, {
|
|
138
128
|
modelName,
|
|
139
129
|
source: configuredName ? "configured-name" : "default-name",
|
|
140
130
|
configuredValue: configuredName,
|
|
131
|
+
translationFallbackFrom: getTranslationFallbackSource(rawModelName, modelName),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function resolveExplicitWhisperCppModel(path: string, configuredValue: string): ResolvedWhisperCppModel {
|
|
136
|
+
const modelName = getWhisperCppModelNameFromPath(path);
|
|
137
|
+
const translationModelName = modelName ? getTranslationAwareWhisperModelName(modelName) : undefined;
|
|
138
|
+
if (modelName && translationModelName && translationModelName !== modelName) {
|
|
139
|
+
const fallbackPath = getSiblingWhisperCppModelPath(path, translationModelName);
|
|
140
|
+
return buildResolvedWhisperCppModel(fallbackPath, {
|
|
141
|
+
modelName: translationModelName,
|
|
142
|
+
source: "explicit-path",
|
|
143
|
+
configuredValue,
|
|
144
|
+
translationFallbackFrom: modelName,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return buildResolvedWhisperCppModel(path, {
|
|
149
|
+
modelName,
|
|
150
|
+
source: "explicit-path",
|
|
151
|
+
configuredValue,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function buildResolvedWhisperCppModel(
|
|
156
|
+
path: string,
|
|
157
|
+
metadata: Pick<ResolvedWhisperCppModel, "source"> & Pick<Partial<ResolvedWhisperCppModel>, "modelName" | "configuredValue" | "translationFallbackFrom">,
|
|
158
|
+
): ResolvedWhisperCppModel {
|
|
159
|
+
return {
|
|
160
|
+
path,
|
|
161
|
+
modelName: metadata.modelName,
|
|
162
|
+
source: metadata.source,
|
|
163
|
+
configuredValue: metadata.configuredValue,
|
|
141
164
|
exists: existsSync(path),
|
|
142
165
|
downloadable: isDownloadableWhisperCppModelPath(path),
|
|
166
|
+
translationFallbackFrom: metadata.translationFallbackFrom,
|
|
143
167
|
};
|
|
144
168
|
}
|
|
145
169
|
|
|
170
|
+
function getSiblingWhisperCppModelPath(modelPath: string, modelName: string) {
|
|
171
|
+
const extension = extname(modelPath) || ".bin";
|
|
172
|
+
return join(dirname(modelPath), `ggml-${modelName}${extension}`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function getTranslationFallbackSource(rawModelName: string, modelName: string) {
|
|
176
|
+
return rawModelName !== modelName ? rawModelName : undefined;
|
|
177
|
+
}
|
|
178
|
+
|
|
146
179
|
export function getPythonWhisperModelName() {
|
|
147
|
-
|
|
180
|
+
const modelName = env("MICME_WHISPER_MODEL")?.trim() || DEFAULT_PYTHON_WHISPER_MODEL_NAME;
|
|
181
|
+
return getTranslationAwareWhisperModelName(modelName);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function getTranslationAwareWhisperModelName(modelName: string) {
|
|
185
|
+
return getTranslateToEnglishLanguage() ? toTranslationCapableWhisperModelName(modelName) : modelName;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function toTranslationCapableWhisperModelName(modelName: string) {
|
|
189
|
+
const multilingualName = toMultilingualWhisperModelName(modelName);
|
|
190
|
+
return isTranslationUnsupportedWhisperModelName(multilingualName) ? "large-v3" : multilingualName;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function toMultilingualWhisperModelName(modelName: string) {
|
|
194
|
+
return modelName.replace(/\.en$/i, "");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function isEnglishOnlyWhisperModelName(modelName: string | undefined) {
|
|
198
|
+
return Boolean(modelName && /\.en$/i.test(modelName));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function isTranslationUnsupportedWhisperModelName(modelName: string | undefined) {
|
|
202
|
+
if (!modelName) return false;
|
|
203
|
+
const normalized = modelName.toLowerCase();
|
|
204
|
+
return normalized === "turbo" || normalized === "large-v3-turbo" || normalized.startsWith("large-v3-turbo-");
|
|
148
205
|
}
|
|
149
206
|
|
|
150
207
|
export async function discoverPythonWhisperModels(): Promise<ModelCandidate[]> {
|
|
@@ -304,7 +361,8 @@ export function getWhisperCppModelCacheDir() {
|
|
|
304
361
|
}
|
|
305
362
|
|
|
306
363
|
export function getDefaultWhisperCppModelPath() {
|
|
307
|
-
const
|
|
364
|
+
const rawModelName = env("MICME_DEFAULT_WHISPER_CPP_MODEL")?.trim() || DEFAULT_WHISPER_CPP_MODEL_NAME;
|
|
365
|
+
const modelName = getTranslationAwareWhisperModelName(rawModelName);
|
|
308
366
|
return join(getWhisperCppModelCacheDir(), `ggml-${modelName}.bin`);
|
|
309
367
|
}
|
|
310
368
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, readdir } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
const PROJECT_RECORDINGS_DIR = "micme-rec";
|
|
6
|
+
const RECORDING_DIR_PREFIX = "rec-";
|
|
7
|
+
const MAX_SEQUENTIAL_RECORDING_DIRS = 999_999;
|
|
8
|
+
|
|
9
|
+
export async function createRecordingDirectory(cwd: string, keepAudio: boolean, tempPrefix = "micme-") {
|
|
10
|
+
if (!keepAudio) return mkdtemp(join(tmpdir(), tempPrefix));
|
|
11
|
+
return createProjectRecordingDirectory(cwd);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function createProjectRecordingDirectory(cwd: string) {
|
|
15
|
+
const recordingsRoot = join(cwd, PROJECT_RECORDINGS_DIR);
|
|
16
|
+
await mkdir(recordingsRoot, { recursive: true });
|
|
17
|
+
|
|
18
|
+
const nextIndex = await getNextRecordingIndex(recordingsRoot);
|
|
19
|
+
|
|
20
|
+
for (let index = nextIndex; index <= MAX_SEQUENTIAL_RECORDING_DIRS; index += 1) {
|
|
21
|
+
const candidate = join(recordingsRoot, formatRecordingDirectoryName(index));
|
|
22
|
+
try {
|
|
23
|
+
await mkdir(candidate);
|
|
24
|
+
return candidate;
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (isAlreadyExistsError(error)) continue;
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
throw new Error(`Unable to create a Micme recording directory under ${recordingsRoot}.`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function getNextRecordingIndex(recordingsRoot: string) {
|
|
35
|
+
const entries = await readdir(recordingsRoot, { withFileTypes: true });
|
|
36
|
+
let maxIndex = 0;
|
|
37
|
+
for (const entry of entries) {
|
|
38
|
+
const match = /^rec-(\d+)$/.exec(entry.name);
|
|
39
|
+
if (!match) continue;
|
|
40
|
+
const index = Number(match[1]);
|
|
41
|
+
if (Number.isInteger(index) && index > maxIndex) maxIndex = index;
|
|
42
|
+
}
|
|
43
|
+
return Math.max(1, maxIndex + 1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function formatRecordingDirectoryName(index: number) {
|
|
47
|
+
const safeIndex = Math.max(1, Math.trunc(index));
|
|
48
|
+
const text = String(safeIndex);
|
|
49
|
+
return `${RECORDING_DIR_PREFIX}${text.padStart(Math.max(3, text.length), "0")}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isAlreadyExistsError(error: unknown) {
|
|
53
|
+
return typeof error === "object" && error !== null && "code" in error && (error as NodeJS.ErrnoException).code === "EEXIST";
|
|
54
|
+
}
|