@senad-d/micme 0.3.3 → 0.3.4
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/package.json +11 -5
- package/scripts/check-format.mjs +1 -0
- package/scripts/publish-npm.mjs +179 -74
- package/src/audio.ts +50 -12
- package/src/backends.ts +6 -4
- package/src/config.ts +51 -10
- package/src/editor.ts +6 -1
- package/src/extension.ts +157 -95
- package/src/models.ts +11 -8
- package/src/processes.ts +41 -15
- package/src/settings.ts +92 -59
- package/src/streaming.ts +139 -29
- package/src/terminal-text.ts +34 -7
package/src/settings.ts
CHANGED
|
@@ -222,7 +222,7 @@ class ConfigurationScreen implements Component {
|
|
|
222
222
|
private readonly requestRender: () => void;
|
|
223
223
|
private focusedPane: FocusedPane = "categories";
|
|
224
224
|
private categoryIndex = 0;
|
|
225
|
-
private selectedByCategory = new Map<ConfigurationCategoryId, number>();
|
|
225
|
+
private readonly selectedByCategory = new Map<ConfigurationCategoryId, number>();
|
|
226
226
|
private searchActive = false;
|
|
227
227
|
private searchQuery = "";
|
|
228
228
|
private selectedSearchIndex = 0;
|
|
@@ -275,45 +275,60 @@ class ConfigurationScreen implements Component {
|
|
|
275
275
|
}
|
|
276
276
|
|
|
277
277
|
handleInput(data: string): void {
|
|
278
|
-
if (this.
|
|
279
|
-
|
|
278
|
+
if (this.forwardSubmenuInput(data)) return;
|
|
279
|
+
if (this.forwardShortcutCaptureInput(data)) return;
|
|
280
|
+
if (this.handleCloseInput(data)) return;
|
|
281
|
+
|
|
282
|
+
if (this.handleSearchInput(data)) {
|
|
280
283
|
this.requestRender();
|
|
281
284
|
return;
|
|
282
285
|
}
|
|
283
286
|
|
|
284
|
-
if (this.
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
287
|
+
if (this.handlePaneSwitchInput(data)) return;
|
|
288
|
+
if (this.handleSelectionMoveInput(data)) return;
|
|
289
|
+
if (this.handleSettingActivationInput(data)) return;
|
|
290
|
+
this.handleEscapeNavigation(data);
|
|
291
|
+
}
|
|
288
292
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
+
private forwardSubmenuInput(data: string) {
|
|
294
|
+
if (!this.submenuComponent) return false;
|
|
295
|
+
this.submenuComponent.handleInput?.(data);
|
|
296
|
+
this.requestRender();
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
293
299
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
300
|
+
private forwardShortcutCaptureInput(data: string) {
|
|
301
|
+
if (!this.editingShortcutItem) return false;
|
|
302
|
+
this.handleShortcutCaptureInput(data);
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
298
305
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
306
|
+
private handleCloseInput(data: string) {
|
|
307
|
+
if (!matchesKey(data, Key.ctrl("c")) && data !== "q" && data !== "Q") return false;
|
|
308
|
+
this.onDone();
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
304
311
|
|
|
312
|
+
private handlePaneSwitchInput(data: string) {
|
|
313
|
+
if (!matchesKey(data, Key.tab) && !matchesKey(data, Key.shift("tab"))) return false;
|
|
314
|
+
this.focusedPane = this.focusedPane === "categories" ? "settings" : "categories";
|
|
315
|
+
this.requestRender();
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
private handleSelectionMoveInput(data: string) {
|
|
305
320
|
if (matchesKey(data, Key.up)) {
|
|
306
321
|
this.moveSelection(-1);
|
|
307
322
|
this.requestRender();
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
if (matchesKey(data, Key.down)) {
|
|
312
|
-
this.moveSelection(1);
|
|
313
|
-
this.requestRender();
|
|
314
|
-
return;
|
|
323
|
+
return true;
|
|
315
324
|
}
|
|
325
|
+
if (!matchesKey(data, Key.down)) return false;
|
|
326
|
+
this.moveSelection(1);
|
|
327
|
+
this.requestRender();
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
316
330
|
|
|
331
|
+
private handleSettingActivationInput(data: string) {
|
|
317
332
|
if (matchesKey(data, Key.enter)) {
|
|
318
333
|
if (this.focusedPane === "categories") {
|
|
319
334
|
this.focusedPane = "settings";
|
|
@@ -321,22 +336,22 @@ class ConfigurationScreen implements Component {
|
|
|
321
336
|
this.activateSelectedSetting();
|
|
322
337
|
}
|
|
323
338
|
this.requestRender();
|
|
324
|
-
return;
|
|
339
|
+
return true;
|
|
325
340
|
}
|
|
326
341
|
|
|
327
|
-
if (matchesKey(data, Key.space)
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
342
|
+
if (!matchesKey(data, Key.space) || this.focusedPane !== "settings") return false;
|
|
343
|
+
this.activateSelectedSetting();
|
|
344
|
+
this.requestRender();
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
332
347
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
348
|
+
private handleEscapeNavigation(data: string) {
|
|
349
|
+
if (!matchesKey(data, Key.escape)) return;
|
|
350
|
+
if (this.focusedPane === "settings") {
|
|
351
|
+
this.focusedPane = "categories";
|
|
352
|
+
this.requestRender();
|
|
353
|
+
} else {
|
|
354
|
+
this.onDone();
|
|
340
355
|
}
|
|
341
356
|
}
|
|
342
357
|
|
|
@@ -350,10 +365,12 @@ class ConfigurationScreen implements Component {
|
|
|
350
365
|
const paneHeight = Math.max(categoryLines.length, settingsLines.length, 8);
|
|
351
366
|
const lines: string[] = [];
|
|
352
367
|
|
|
353
|
-
lines.push(
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
368
|
+
lines.push(
|
|
369
|
+
this.renderTitleBorder(width, this.currentHeaderLabel()),
|
|
370
|
+
this.renderFullLine(width, this.renderConfigSourceText(width)),
|
|
371
|
+
this.renderFullLine(width, "↑↓ move Tab pane Enter edit / search Esc back q quit"),
|
|
372
|
+
this.renderPaneSeparator(width, leftWidth, rightWidth, "top"),
|
|
373
|
+
);
|
|
357
374
|
|
|
358
375
|
for (let index = 0; index < paneHeight; index++) {
|
|
359
376
|
const left = categoryLines[index] ?? "";
|
|
@@ -361,9 +378,11 @@ class ConfigurationScreen implements Component {
|
|
|
361
378
|
lines.push(this.renderPaneLine(left, right, leftWidth, rightWidth));
|
|
362
379
|
}
|
|
363
380
|
|
|
364
|
-
lines.push(
|
|
365
|
-
|
|
366
|
-
|
|
381
|
+
lines.push(
|
|
382
|
+
this.renderPaneSeparator(width, leftWidth, rightWidth, "bottom"),
|
|
383
|
+
this.renderFullLine(width, this.renderFooterText()),
|
|
384
|
+
this.colorBorder(`╰${"─".repeat(Math.max(0, width - 2))}╯`, width),
|
|
385
|
+
);
|
|
367
386
|
return lines.map((line) => truncateToWidth(line, width, ""));
|
|
368
387
|
}
|
|
369
388
|
|
|
@@ -375,14 +394,18 @@ class ConfigurationScreen implements Component {
|
|
|
375
394
|
const bodyLines = showCategories ? this.renderCategoryPaneLines(innerWidth) : this.renderSettingsPaneLines(innerWidth);
|
|
376
395
|
const lines: string[] = [];
|
|
377
396
|
|
|
378
|
-
lines.push(
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
397
|
+
lines.push(
|
|
398
|
+
this.renderTitleBorder(width, this.currentHeaderLabel()),
|
|
399
|
+
this.renderFullLine(width, this.renderConfigSourceText(width)),
|
|
400
|
+
this.renderFullLine(width, showCategories ? "↑↓ category Enter open / search q quit" : "↑↓ move Enter edit Esc categories / search q quit"),
|
|
401
|
+
this.colorBorder(`├${"─".repeat(innerWidth)}┤`, width),
|
|
402
|
+
);
|
|
382
403
|
for (const line of bodyLines) lines.push(this.renderFullLine(width, line));
|
|
383
|
-
lines.push(
|
|
384
|
-
|
|
385
|
-
|
|
404
|
+
lines.push(
|
|
405
|
+
this.colorBorder(`├${"─".repeat(innerWidth)}┤`, width),
|
|
406
|
+
this.renderFullLine(width, this.renderFooterText()),
|
|
407
|
+
this.colorBorder(`╰${"─".repeat(innerWidth)}╯`, width),
|
|
408
|
+
);
|
|
386
409
|
return lines.map((line) => truncateToWidth(line, width, ""));
|
|
387
410
|
}
|
|
388
411
|
|
|
@@ -447,10 +470,12 @@ class ConfigurationScreen implements Component {
|
|
|
447
470
|
return CONFIGURATION_CATEGORIES.map((category, index) => {
|
|
448
471
|
const selected = index === this.categoryIndex;
|
|
449
472
|
const focused = this.focusedPane === "categories";
|
|
473
|
+
const selectedColor = focused ? "accent" : "muted";
|
|
450
474
|
const prefix = selected ? "▶ " : " ";
|
|
451
|
-
const styledPrefix = selected ? this.theme.fg(
|
|
475
|
+
const styledPrefix = selected ? this.theme.fg(selectedColor, prefix) : prefix;
|
|
452
476
|
const label = fitAnsi(category.label, Math.max(0, width - 2));
|
|
453
|
-
const
|
|
477
|
+
const selectedLabel = focused ? this.theme.bold(label) : label;
|
|
478
|
+
const styledLabel = selected ? this.theme.fg(selectedColor, selectedLabel) : this.theme.fg("dim", label);
|
|
454
479
|
return padRightAnsi(`${styledPrefix}${styledLabel}`, width);
|
|
455
480
|
});
|
|
456
481
|
}
|
|
@@ -480,13 +505,15 @@ class ConfigurationScreen implements Component {
|
|
|
480
505
|
|
|
481
506
|
private renderSettingLine(item: ConfigurationItem, selected: boolean, width: number) {
|
|
482
507
|
const focused = this.focusedPane === "settings";
|
|
508
|
+
const selectedColor = focused ? "accent" : "muted";
|
|
483
509
|
const prefix = selected ? "▶ " : " ";
|
|
484
|
-
const styledPrefix = selected ? this.theme.fg(
|
|
510
|
+
const styledPrefix = selected ? this.theme.fg(selectedColor, prefix) : prefix;
|
|
485
511
|
const valueWidth = Math.max(0, Math.min(28, Math.floor(width * 0.4)));
|
|
486
512
|
const labelWidth = Math.max(1, width - 2 - 1 - valueWidth);
|
|
487
513
|
const labelText = this.hasSearchQuery() ? `${item.label} (${this.getCategory(item.categoryId).label})` : item.label;
|
|
488
514
|
const label = fitAnsi(labelText, labelWidth);
|
|
489
|
-
const
|
|
515
|
+
const selectedLabel = focused ? this.theme.bold(label) : label;
|
|
516
|
+
const styledLabel = selected ? this.theme.fg(selectedColor, selectedLabel) : label;
|
|
490
517
|
const value = this.formatValue(item, valueWidth);
|
|
491
518
|
return padRightAnsi(`${styledPrefix}${padRightAnsi(styledLabel, labelWidth)} ${padLeftAnsi(value, valueWidth)}`, width);
|
|
492
519
|
}
|
|
@@ -1219,13 +1246,19 @@ function getConfigurationValuesToWrite(id: string, value: string) {
|
|
|
1219
1246
|
function formatSaveStatus(id: string, value: string) {
|
|
1220
1247
|
const writtenKeys = Object.keys(getConfigurationValuesToWrite(id, value));
|
|
1221
1248
|
const overriddenKeys = writtenKeys.filter((key) => process.env[key] !== undefined);
|
|
1222
|
-
const reloadHint = id
|
|
1249
|
+
const reloadHint = getConfigurationReloadHint(id);
|
|
1223
1250
|
|
|
1224
1251
|
if (id === "MICME_TRANSCRIPTION_MODE" && overriddenKeys.length) return `Shell env still overrides ${overriddenKeys.join(", ")}`;
|
|
1225
1252
|
if (overriddenKeys.length) return reloadHint ? `Shell env still overrides ${overriddenKeys.join(", ")} • ${reloadHint}` : `Shell env still overrides ${overriddenKeys.join(", ")}`;
|
|
1226
1253
|
return reloadHint;
|
|
1227
1254
|
}
|
|
1228
1255
|
|
|
1256
|
+
function getConfigurationReloadHint(id: string) {
|
|
1257
|
+
if (id === "MICME_SHORTCUT") return "/reload required for shortcut changes";
|
|
1258
|
+
if (id === "MICME_PRINTABLE_SHORTCUTS") return "/reload recommended for printable shortcuts";
|
|
1259
|
+
return "";
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1229
1262
|
function fitAnsi(text: string, width: number, ellipsis = "…") {
|
|
1230
1263
|
if (width <= 0) return "";
|
|
1231
1264
|
return truncateToWidth(text, width, ellipsis);
|
package/src/streaming.ts
CHANGED
|
@@ -34,7 +34,7 @@ type StreamingDiagnosticDetails = {
|
|
|
34
34
|
reason?: string;
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
-
export function buildWhisperStreamCommand(binary: string, modelPath: string,
|
|
37
|
+
export function buildWhisperStreamCommand(binary: string, modelPath: string, _tempDir: string): CommandSpec {
|
|
38
38
|
const args = [
|
|
39
39
|
"-m",
|
|
40
40
|
modelPath,
|
|
@@ -130,8 +130,8 @@ export function readStreamingOutputFrames(state: StreamingState, chunk: string,
|
|
|
130
130
|
const frames: string[] = [];
|
|
131
131
|
|
|
132
132
|
while (true) {
|
|
133
|
-
const match =
|
|
134
|
-
if (
|
|
133
|
+
const match = /[\r\n]/.exec(state.outputBuffer);
|
|
134
|
+
if (match?.index === undefined) break;
|
|
135
135
|
frames.push(state.outputBuffer.slice(0, match.index));
|
|
136
136
|
state.outputBuffer = state.outputBuffer.slice(match.index + 1).replace(/^[\r\n]+/, "");
|
|
137
137
|
}
|
|
@@ -145,20 +145,89 @@ export function readStreamingOutputFrames(state: StreamingState, chunk: string,
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
export function sanitizeStreamingText(text: string) {
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
.replace(/\s+/g, " ")
|
|
154
|
-
.trim();
|
|
148
|
+
const textWithoutTokens = stripWhisperControlTokens(stripStreamingControls(text));
|
|
149
|
+
const textWithoutHeading = removeTranscriptionEndHeading(textWithoutTokens);
|
|
150
|
+
const textWithoutBracketedSegments = stripStreamingDelimitedSegments(textWithoutHeading, "[", "]");
|
|
151
|
+
const textWithoutParenthesizedSegments = stripStreamingDelimitedSegments(textWithoutBracketedSegments, "(", ")");
|
|
152
|
+
const cleaned = textWithoutParenthesizedSegments.replace(/\s+/g, " ").trim();
|
|
155
153
|
return isLikelyStreamingHallucination(cleaned) ? "" : cleaned;
|
|
156
154
|
}
|
|
157
155
|
|
|
156
|
+
function stripStreamingDelimitedSegments(text: string, startDelimiter: string, endDelimiter: string) {
|
|
157
|
+
let cleaned = "";
|
|
158
|
+
let index = 0;
|
|
159
|
+
|
|
160
|
+
while (index < text.length) {
|
|
161
|
+
const startIndex = text.indexOf(startDelimiter, index);
|
|
162
|
+
if (startIndex === -1) return `${cleaned}${text.slice(index)}`;
|
|
163
|
+
|
|
164
|
+
const endIndex = text.indexOf(endDelimiter, startIndex + startDelimiter.length);
|
|
165
|
+
if (endIndex === -1) return `${cleaned}${text.slice(index)}`;
|
|
166
|
+
|
|
167
|
+
cleaned = `${cleaned}${text.slice(index, startIndex)} `;
|
|
168
|
+
index = endIndex + endDelimiter.length;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return cleaned;
|
|
172
|
+
}
|
|
173
|
+
|
|
158
174
|
export function stripStreamingControls(text: string) {
|
|
159
175
|
return sanitizeTerminalText(text);
|
|
160
176
|
}
|
|
161
177
|
|
|
178
|
+
function stripWhisperControlTokens(text: string) {
|
|
179
|
+
let cleaned = "";
|
|
180
|
+
let index = 0;
|
|
181
|
+
|
|
182
|
+
while (index < text.length) {
|
|
183
|
+
const startIndex = text.indexOf("<|", index);
|
|
184
|
+
if (startIndex === -1) return `${cleaned}${text.slice(index)}`;
|
|
185
|
+
|
|
186
|
+
const endIndex = text.indexOf("|>", startIndex + 2);
|
|
187
|
+
if (endIndex === -1) return `${cleaned}${text.slice(index)}`;
|
|
188
|
+
|
|
189
|
+
const tokenBody = text.slice(startIndex + 2, endIndex);
|
|
190
|
+
if (tokenBody.includes("|")) {
|
|
191
|
+
cleaned = `${cleaned}${text.slice(index, startIndex + 2)}`;
|
|
192
|
+
index = startIndex + 2;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
cleaned = `${cleaned}${text.slice(index, startIndex)} `;
|
|
197
|
+
index = endIndex + 2;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return cleaned;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function removeTranscriptionEndHeading(text: string) {
|
|
204
|
+
return isTranscriptionEndHeading(text) ? " " : text;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function isTranscriptionEndHeading(text: string) {
|
|
208
|
+
const trimmed = text.trim();
|
|
209
|
+
if (!trimmed.startsWith("#")) return false;
|
|
210
|
+
|
|
211
|
+
let index = 0;
|
|
212
|
+
while (trimmed.at(index) === "#") index++;
|
|
213
|
+
while (isWhitespaceCharacter(trimmed.at(index))) index++;
|
|
214
|
+
|
|
215
|
+
const words = trimmed.slice(index).split(/\s+/u);
|
|
216
|
+
return words.length === 3 && words[0].toLowerCase() === "transcription" && isAsciiDigits(words[1]) && words[2].toLowerCase() === "end";
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function isAsciiDigits(value: string) {
|
|
220
|
+
if (!value) return false;
|
|
221
|
+
for (const character of value) {
|
|
222
|
+
if (character < "0" || character > "9") return false;
|
|
223
|
+
}
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function isWhitespaceCharacter(character: string | undefined) {
|
|
228
|
+
return character?.trim() === "";
|
|
229
|
+
}
|
|
230
|
+
|
|
162
231
|
export function diffStreamingText(previous: string, current: string) {
|
|
163
232
|
const previousWords = splitStreamingWords(previous);
|
|
164
233
|
const currentWords = splitStreamingWords(current);
|
|
@@ -171,31 +240,41 @@ export function extractStreamingCandidate(state: StreamingState, frameWords: str
|
|
|
171
240
|
const words = frameWords.filter(Boolean);
|
|
172
241
|
if (words.length === 0) return { mode: "reset", newWords: [] };
|
|
173
242
|
|
|
243
|
+
const emittedExtraction = extractEmittedStreamingCandidate(state, words);
|
|
244
|
+
if (emittedExtraction) return emittedExtraction;
|
|
245
|
+
|
|
246
|
+
const candidateExtraction = extractReferencedStreamingCandidate(state.candidateWords, words, words);
|
|
247
|
+
if (candidateExtraction) return candidateExtraction;
|
|
248
|
+
|
|
249
|
+
const hypothesisExtraction = extractReferencedStreamingCandidate(state.lastHypothesisWords, words, trimCommittedStreamingPrefix(state, words));
|
|
250
|
+
if (hypothesisExtraction) return hypothesisExtraction;
|
|
251
|
+
|
|
252
|
+
return { mode: isFirstStreamingHypothesis(state) ? "cumulative" : "incremental", newWords: words };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function extractEmittedStreamingCandidate(state: StreamingState, words: string[]): StreamingExtraction | undefined {
|
|
174
256
|
const emittedOverlap = streamingWordOverlap(state.emittedWords.slice(-160), words);
|
|
175
257
|
if (emittedOverlap === words.length) return { mode: "duplicate", newWords: [] };
|
|
176
258
|
if (emittedOverlap > 0) return { mode: "cumulative", newWords: words.slice(emittedOverlap) };
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
177
261
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (streamingWordsEqual(state.lastHypothesisWords, words)) return { mode: "duplicate", newWords: words };
|
|
187
|
-
if (isStreamingWordsPrefix(state.lastHypothesisWords, words)) return { mode: "cumulative", newWords: trimCommittedStreamingPrefix(state, words) };
|
|
188
|
-
if (streamingWordOverlap(state.lastHypothesisWords, words) > 0) return { mode: "rolling", newWords: words };
|
|
189
|
-
if (looksLikeStreamingCorrection(state.lastHypothesisWords, words)) return { mode: "reset", newWords: words };
|
|
190
|
-
}
|
|
262
|
+
function extractReferencedStreamingCandidate(referenceWords: string[], words: string[], prefixWords: string[]): StreamingExtraction | undefined {
|
|
263
|
+
if (referenceWords.length === 0) return undefined;
|
|
264
|
+
if (streamingWordsEqual(referenceWords, words)) return { mode: "duplicate", newWords: words };
|
|
265
|
+
if (isStreamingWordsPrefix(referenceWords, words)) return { mode: "cumulative", newWords: prefixWords };
|
|
266
|
+
if (streamingWordOverlap(referenceWords, words) > 0) return { mode: "rolling", newWords: words };
|
|
267
|
+
if (looksLikeStreamingCorrection(referenceWords, words)) return { mode: "reset", newWords: words };
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
191
270
|
|
|
192
|
-
|
|
193
|
-
return
|
|
271
|
+
function isFirstStreamingHypothesis(state: StreamingState) {
|
|
272
|
+
return state.emittedWords.length === 0 && state.candidateWords.length === 0 && state.lastHypothesisWords.length === 0;
|
|
194
273
|
}
|
|
195
274
|
|
|
196
275
|
export function isLikelyStreamingHallucination(text: string) {
|
|
197
276
|
if (!text) return true;
|
|
198
|
-
const normalized = text.toLowerCase()
|
|
277
|
+
const normalized = trimStreamingWordEdges(text.toLowerCase());
|
|
199
278
|
if (/^(?:start speaking|blank audio)$/i.test(normalized)) return true;
|
|
200
279
|
if (/^(?:you|yeah|okay|ok|uh|um|hmm)$/i.test(normalized)) return true;
|
|
201
280
|
if (/^(?:thank you|thanks for watching|thank you very much)$/i.test(normalized)) return true;
|
|
@@ -206,7 +285,17 @@ export function shouldResetStreamingPending(text: string) {
|
|
|
206
285
|
const cleaned = stripStreamingControls(text);
|
|
207
286
|
if (!cleaned) return false;
|
|
208
287
|
if (isLikelyStreamingHallucination(cleaned)) return true;
|
|
209
|
-
return
|
|
288
|
+
return isBracketedStreamingResetFrame(cleaned) || isParenthesizedStreamingResetFrame(cleaned);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function isBracketedStreamingResetFrame(text: string) {
|
|
292
|
+
if (text.length <= 2) return false;
|
|
293
|
+
return text.startsWith("[") && text.endsWith("]") && !text.slice(1, -1).includes("]");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function isParenthesizedStreamingResetFrame(text: string) {
|
|
297
|
+
if (text.length < 2) return false;
|
|
298
|
+
return text.startsWith("(") && text.endsWith(")") && !text.slice(1, -1).includes(")");
|
|
210
299
|
}
|
|
211
300
|
|
|
212
301
|
export function queueStableStreamingWords(ctx: ExtensionContext, state: StreamingState, currentWords: string[], mode: StreamingFrameMode = "cumulative") {
|
|
@@ -318,10 +407,25 @@ export function streamingWordsEqual(left: string[], right: string[]) {
|
|
|
318
407
|
}
|
|
319
408
|
|
|
320
409
|
export function normalizeStreamingWord(word: string) {
|
|
321
|
-
const normalized = word.toLowerCase()
|
|
410
|
+
const normalized = trimStreamingWordEdges(word.toLowerCase());
|
|
322
411
|
return normalized || word.toLowerCase();
|
|
323
412
|
}
|
|
324
413
|
|
|
414
|
+
function trimStreamingWordEdges(text: string) {
|
|
415
|
+
const characters = Array.from(text);
|
|
416
|
+
let start = 0;
|
|
417
|
+
while (start < characters.length && !isStreamingWordCharacter(characters[start] ?? "")) start++;
|
|
418
|
+
|
|
419
|
+
let end = characters.length;
|
|
420
|
+
while (end > start && !isStreamingWordCharacter(characters[end - 1] ?? "")) end--;
|
|
421
|
+
|
|
422
|
+
return characters.slice(start, end).join("");
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function isStreamingWordCharacter(character: string) {
|
|
426
|
+
return /[\p{L}\p{N}]/u.test(character);
|
|
427
|
+
}
|
|
428
|
+
|
|
325
429
|
export function getStreamingTranscript(state: StreamingState) {
|
|
326
430
|
return state.emittedWords.join(" ");
|
|
327
431
|
}
|
|
@@ -373,7 +477,13 @@ function appendTranscriptToBaseText(baseText: string, transcript: string, traili
|
|
|
373
477
|
}
|
|
374
478
|
|
|
375
479
|
function needsStreamingSeparator(baseText: string, transcript: string) {
|
|
376
|
-
|
|
480
|
+
if (!baseText || !transcript) return false;
|
|
481
|
+
return !isWhitespaceCharacter(baseText.at(-1)) && !isStreamingLeadingSeparator(transcript.at(0));
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function isStreamingLeadingSeparator(character: string | undefined) {
|
|
485
|
+
if (isWhitespaceCharacter(character)) return true;
|
|
486
|
+
return character !== undefined && ".,!?;:)}]".includes(character);
|
|
377
487
|
}
|
|
378
488
|
|
|
379
489
|
function queueRollingStreamingWords(state: StreamingState, words: string[]) {
|
package/src/terminal-text.ts
CHANGED
|
@@ -1,10 +1,37 @@
|
|
|
1
|
+
const ESCAPE_CONTROL_CHARACTER = String.fromCodePoint(0x1B);
|
|
2
|
+
const BELL_CONTROL_CHARACTER = String.fromCodePoint(0x07);
|
|
3
|
+
const NON_PRINTABLE_CONTROL_CHARACTER_RANGES = [
|
|
4
|
+
[0x00, 0x08],
|
|
5
|
+
[0x0B, 0x0C],
|
|
6
|
+
[0x0E, 0x1F],
|
|
7
|
+
[0x7F, 0x7F],
|
|
8
|
+
] as const;
|
|
9
|
+
const OPERATING_SYSTEM_COMMAND_SEQUENCE_PATTERN = new RegExp(
|
|
10
|
+
String.raw`${ESCAPE_CONTROL_CHARACTER}\][^${BELL_CONTROL_CHARACTER}]*(?:${BELL_CONTROL_CHARACTER}|${ESCAPE_CONTROL_CHARACTER}\\)`,
|
|
11
|
+
"g",
|
|
12
|
+
);
|
|
13
|
+
const TERMINAL_STRING_CONTROL_SEQUENCE_PATTERN = new RegExp(
|
|
14
|
+
String.raw`${ESCAPE_CONTROL_CHARACTER}[PX^_][\s\S]*?(?:${BELL_CONTROL_CHARACTER}|${ESCAPE_CONTROL_CHARACTER}\\)`,
|
|
15
|
+
"g",
|
|
16
|
+
);
|
|
17
|
+
const CONTROL_SEQUENCE_INTRODUCER_PATTERN = new RegExp(String.raw`${ESCAPE_CONTROL_CHARACTER}\[[0-?]*[ -/]*[@-~]`, "g");
|
|
18
|
+
const ESCAPE_SEQUENCE_PATTERN = new RegExp(`${ESCAPE_CONTROL_CHARACTER}[ -/]*[@-~]`, "g");
|
|
19
|
+
const NON_PRINTABLE_CONTROL_CHARACTER_PATTERN = new RegExp(`[${NON_PRINTABLE_CONTROL_CHARACTER_RANGES.map(formatControlCharacterRange).join("")}]`, "g");
|
|
20
|
+
|
|
1
21
|
export function stripTerminalControlSequences(value: string) {
|
|
2
22
|
return value
|
|
3
|
-
.replace(
|
|
4
|
-
.replace(
|
|
5
|
-
.replace(
|
|
6
|
-
.replace(
|
|
7
|
-
.replace(
|
|
23
|
+
.replace(OPERATING_SYSTEM_COMMAND_SEQUENCE_PATTERN, " ")
|
|
24
|
+
.replace(TERMINAL_STRING_CONTROL_SEQUENCE_PATTERN, " ")
|
|
25
|
+
.replace(CONTROL_SEQUENCE_INTRODUCER_PATTERN, " ")
|
|
26
|
+
.replace(ESCAPE_SEQUENCE_PATTERN, " ")
|
|
27
|
+
.replace(NON_PRINTABLE_CONTROL_CHARACTER_PATTERN, " ");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function formatControlCharacterRange(range: readonly [number, number]) {
|
|
31
|
+
const [first, last] = range;
|
|
32
|
+
const firstCharacter = String.fromCodePoint(first);
|
|
33
|
+
const lastCharacter = String.fromCodePoint(last);
|
|
34
|
+
return first === last ? firstCharacter : `${firstCharacter}-${lastCharacter}`;
|
|
8
35
|
}
|
|
9
36
|
|
|
10
37
|
export function sanitizeTerminalText(value: string) {
|
|
@@ -13,8 +40,8 @@ export function sanitizeTerminalText(value: string) {
|
|
|
13
40
|
|
|
14
41
|
export function sanitizeTerminalOutput(value: string) {
|
|
15
42
|
return stripTerminalControlSequences(value)
|
|
16
|
-
.
|
|
17
|
-
.
|
|
43
|
+
.replaceAll("\r\n", "\n")
|
|
44
|
+
.replaceAll("\r", "\n")
|
|
18
45
|
.split("\n")
|
|
19
46
|
.map((line) => line.trimEnd())
|
|
20
47
|
.join("\n")
|