Repository navigation
Expand file tree
/
Copy pathvoice-input.ts
More file actions
492 lines (461 loc) · 18 KB
/
Copy pathvoice-input.ts
File metadata and controls
492 lines (461 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import { G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
import { refineDictation, type AnthropicStreamHandle } from "../../native/anthropic";
import { anthropicApiKeySetting } from "../dashboard-settings";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, gestureHints } from "../gestures";
import { drawSelectionHighlight } from "../menu";
import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../layers";
import { MIN_WINDOW_HEIGHT, minWindowTop } from "../shell/geometry";
const DIALOG_X = 40;
const DIALOG_W = G2_LENS_WIDTH - 80;
// The dialog fits inside the min-height window band (like the other shell
// overlays), wherever the vertical position setting puts it.
const DIALOG_MARGIN_Y = 24;
const DIALOG_H = MIN_WINDOW_HEIGHT - 2 * DIALOG_MARGIN_Y;
const TEXT_MAX_WIDTH = DIALOG_W - 32;
/** Dialog top edge; band-relative, so computed per paint. */
function dialogY(): number {
return minWindowTop() + DIALOG_MARGIN_Y;
}
const MENU_ROW_H = 20;
// After the mic stops, the provider's final transcript can trail in (cloud
// commit round-trip); wait this long for it before refining with what we have.
const FOLLOWUP_FINALIZE_TIMEOUT_MS = 1200;
/**
* The dialog's lifecycle. "capturing" is the first utterance (push-to-talk or
* click-to-finish); "menu" is the send / Continue / Discard menu; "continuing"
* records a follow-up utterance (always click-to-finish); "refining" streams
* the LLM-merged text, then returns to "menu".
*/
type VoicePhase = "capturing" | "menu" | "continuing" | "refining";
/**
* A destination the captured text can be sent to. The shell supplies these per
* entry point: "Send to Assistant" (hand off to the voice assistant) and/or
* "Type Into App" (deliver to the foreground window's text input).
*/
export type VoiceSendTarget = {
id: string;
label: string;
onSend: (text: string) => void;
};
export type VoiceInputLayerOptions = {
actions: LayerActions;
/** Post-removal cleanup (also fires when the screen turns off). */
onClosed: () => void;
/** Pop this layer off the shell stack. */
dismiss: () => void;
/** Ordered send destinations shown as the first menu rows. */
sendTargets: VoiceSendTarget[];
/** Which send target is highlighted by default (entry-point dependent). */
defaultTargetIndex?: number;
/** A single click ends the utterance (opened from a menu, no held button). */
finishOnClick?: boolean;
/** Wakeword flow: mic starts immediately and ends on detected silence. */
handsFree?: boolean;
/** Skip the menu: on capture end, send straight to the default target. */
autoSend?: boolean;
};
/**
* Push-to-talk voice dialog, drawn on top of whatever is already on screen.
* The mic runs while the button is held (long-press); releasing it stops the
* mic and shows a Send / Continue / Discard menu. Send delivers the transcript
* to the foreground window (e.g. the terminal); Discard (or double-click)
* closes. Continue records another utterance — extra content or a spoken edit
* ("change X to Y", "insert ... after ...") — and merges it into the message
* with an LLM, landing back on the same menu.
*
* When opened from a menu instead of a held button (finishOnClick), there is
* no release event to end the capture, so a single click stops the mic.
*
* When opened by the "Hey Even" wakeword (handsFree), there is likewise no
* button involved: the mic starts immediately and stops when the speaker does,
* via endpoint detection on the decoded PCM. A click still ends it early.
*/
export class VoiceInputLayer implements Layer {
private phase: VoicePhase = "capturing";
private status = "Listening...";
// The active utterance. displayText() is what the dialog shows and what
// Send delivers; the refine flow also writes the merged result here.
private finalizedText = "";
private liveText = "";
// Snapshot of the message when Continue starts, so a failed or cancelled
// continuation can fall back to it.
private baseText = "";
/** Whether the mic is running (capture bookkeeping, not UI state). */
private capturing = false;
/** Continuation stopped; waiting for the trailing final transcript. */
private followupFinalizeTimer: ReturnType<typeof setTimeout> | null = null;
private refineHandle: AnthropicStreamHandle | null = null;
private menuIndex = 0;
/** Auto-send (wakeword skip-confirmation) is waiting to fire. */
private pendingAutoSend = false;
private autoSendTimer: ReturnType<typeof setTimeout> | null = null;
private unsubscribeTranscript: (() => void) | null = null;
private unsubscribeStatus: (() => void) | null = null;
private unsubscribeSpeechEnd: (() => void) | null = null;
private readonly actions: LayerActions;
private readonly onClosed: () => void;
private readonly dismiss: () => void;
private readonly sendTargets: VoiceSendTarget[];
private readonly defaultTargetIndex: number;
private readonly finishOnClick: boolean;
private readonly handsFree: boolean;
private readonly autoSend: boolean;
constructor(options: VoiceInputLayerOptions) {
this.actions = options.actions;
this.onClosed = options.onClosed;
this.dismiss = options.dismiss;
this.sendTargets = options.sendTargets;
this.finishOnClick = options.finishOnClick ?? false;
this.handsFree = options.handsFree ?? false;
this.autoSend = options.autoSend ?? false;
const defaultIndex = options.defaultTargetIndex ?? 0;
this.defaultTargetIndex = Math.min(Math.max(0, defaultIndex), Math.max(0, this.sendTargets.length - 1));
this.menuIndex = this.defaultTargetIndex;
}
startCapture(): void {
if (this.capturing) return;
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event));
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
// The refine stage owns the status line ("Refining...", error text).
if (this.phase === "refining") return;
this.status = state.status;
this.actions.requestRender();
});
if (this.handsFree) {
// No button is held, so the mic has to stop itself. endCapture() is
// idempotent, and a click still ends the utterance early.
this.unsubscribeSpeechEnd = voiceControlBridge.onSpeechEnd(() => {
if (this.phase === "capturing") {
this.endCapture();
}
});
}
this.capturing = true;
void this.actions.startVoiceCapture(this.handsFree);
this.actions.requestRender();
}
/**
* Whether a single click ends the utterance. True when there is no held
* button to release: opened from the overlay menu (finishOnClick), or
* hands-free from the wakeword — where it also lets the user cut the
* silence-detection wait short.
*/
private get clickEndsCapture(): boolean {
return this.finishOnClick || this.handsFree;
}
/** Button released (or click when clickEndsCapture): stop the mic. */
endCapture(): void {
if (!this.capturing) return;
if (this.phase === "continuing") {
this.endContinuationCapture();
return;
}
this.capturing = false;
this.phase = "menu";
void this.actions.stopVoiceCapture();
if (this.autoSend && this.sendTargets.length) {
// Skip-confirmation (wakeword): send to the default target as soon as the
// transcript finalizes, or after a short wait for the trailing final.
this.pendingAutoSend = true;
this.status = "Sending...";
this.autoSendTimer = setTimeout(() => this.performAutoSend(), FOLLOWUP_FINALIZE_TIMEOUT_MS);
} else if (this.status.startsWith("Listening")) {
this.status = "Send, continue, or discard?";
}
this.actions.requestRender();
}
/** Fire the queued skip-confirmation send (or fall back to the menu). */
private performAutoSend(): void {
if (this.autoSendTimer !== null) {
clearTimeout(this.autoSendTimer);
this.autoSendTimer = null;
}
if (!this.pendingAutoSend) return;
this.pendingAutoSend = false;
const text = this.displayText().trim();
const target = this.sendTargets[this.defaultTargetIndex];
if (text && target) {
this.dismiss();
target.onSend(text);
} else {
// Nothing heard; fall back to the menu so the user can retry or discard.
this.status = "Send, continue, or discard?";
this.actions.requestRender();
}
}
/** The menu rows: one per send target, then Continue, then Discard. */
private menuRows(): Array<{ label: string; dim: boolean; onSelect: () => void }> {
const text = this.displayText().trim();
const hasText = text.length > 0;
const hasLlmKey = anthropicApiKeySetting.get().trim().length > 0;
const rows: Array<{ label: string; dim: boolean; onSelect: () => void }> = [];
for (const target of this.sendTargets) {
rows.push({
label: target.label,
dim: !hasText,
onSelect: () => {
this.dismiss();
if (hasText) target.onSend(text);
},
});
}
rows.push({
label: hasLlmKey ? "Continue" : "Continue (Needs LLM API key)",
dim: !hasLlmKey,
onSelect: () => {
if (hasLlmKey) this.startContinuation();
},
});
rows.push({ label: "Discard", dim: false, onSelect: () => this.dismiss() });
return rows;
}
paint(_ctx: LayerContext, paintBelow: () => GrayImage): GrayImage {
const font = getDefaultSmallFont();
const image = paintBelow();
const top = dialogY();
// Solid dialog box over the underlying UI. Fill 1, not 0: identical after
// 4bpp quantization, but 0 is transparent on the color-key shell surface.
image.fillRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1, 10);
image.drawRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90, 10);
const left = DIALOG_X + 16;
image.drawText(font, left, top + 12, this.capturing ? "Voice ●" : "Voice", 220);
image.drawText(font, left, top + 30, truncateText(font, this.status, TEXT_MAX_WIDTH), 130);
const inMenu = this.phase === "menu";
const rows = inMenu ? this.menuRows() : [];
// Reserve space for the actual number of rows this menu has.
const textBottom = inMenu ? top + DIALOG_H - rows.length * MENU_ROW_H - 8 : top + DIALOG_H - 8;
const textTop = top + 56;
const maxLines = Math.max(1, ((textBottom - textTop) / 16) | 0);
const text = this.displayText() || this.placeholderText();
const wrapped = wrapText(font, text, TEXT_MAX_WIDTH);
const firstLine = Math.max(0, wrapped.length - maxLines);
for (let index = firstLine; index < wrapped.length; index++) {
image.drawText(font, left, textTop + (index - firstLine) * 16, wrapped[index]!, 235);
}
if (inMenu) {
const menuTop = top + DIALOG_H - rows.length * MENU_ROW_H - 2;
for (let i = 0; i < rows.length; i++) {
const rowY = menuTop + i * MENU_ROW_H;
const selected = i === this.menuIndex;
if (selected) {
drawSelectionHighlight(image, left - 4, rowY - 2, DIALOG_W - 24, MENU_ROW_H - 2, true, 6);
}
const row = rows[i]!;
image.drawText(font, left + 4, rowY + 2, row.label, row.dim ? 90 : selected ? 255 : 200);
}
} else {
image.drawText(font, left, top + DIALOG_H - 14, this.hintText(), 110);
}
return image;
}
handleInput(event: DashboardInputEvent, _ctx: LayerContext): void {
switch (this.phase) {
case "capturing":
if (event.type === "double-click") {
this.dismiss();
} else if (this.clickEndsCapture && event.type === "click") {
this.endCapture();
}
return;
case "continuing":
if (event.type === "click") {
this.endContinuationCapture();
} else if (event.type === "double-click") {
this.cancelContinuation("Continuation cancelled");
}
return;
case "refining":
if (event.type === "double-click") {
this.cancelContinuation("Refinement cancelled");
}
return;
case "menu":
this.handleMenuInput(event);
return;
}
}
private handleMenuInput(event: DashboardInputEvent): void {
const rowCount = this.menuRows().length;
switch (event.type) {
case "scroll-up":
this.menuIndex = (this.menuIndex + rowCount - 1) % rowCount;
this.actions.requestRender();
return;
case "scroll-down":
this.menuIndex = (this.menuIndex + 1) % rowCount;
this.actions.requestRender();
return;
case "click": {
const row = this.menuRows()[this.menuIndex];
row?.onSelect();
return;
}
case "double-click":
this.dismiss();
return;
default:
return;
}
}
/** Continue selected: keep the message aside and record a follow-up. */
private startContinuation(): void {
this.baseText = this.displayText().trim();
this.finalizedText = "";
this.liveText = "";
this.phase = "continuing";
this.status = "Listening...";
this.capturing = true;
void this.actions.startVoiceCapture();
this.actions.requestRender();
}
/** Follow-up tap: stop the mic, then refine once the transcript finalizes. */
private endContinuationCapture(): void {
if (!this.capturing) return;
this.capturing = false;
void this.actions.stopVoiceCapture();
this.status = "Refining...";
this.actions.requestRender();
// The provider's committed transcript arrives shortly after stop; refine
// when it does, or after a timeout with whatever partials we have.
this.followupFinalizeTimer = setTimeout(() => this.beginRefine(), FOLLOWUP_FINALIZE_TIMEOUT_MS);
}
private beginRefine(): void {
if (this.followupFinalizeTimer !== null) {
clearTimeout(this.followupFinalizeTimer);
this.followupFinalizeTimer = null;
}
if (this.phase !== "continuing") return;
const followup = this.displayText().trim();
if (!followup) {
this.backToMenu(this.baseText, "No follow-up heard");
return;
}
this.phase = "refining";
this.finalizedText = "";
this.liveText = "";
this.status = "Refining...";
this.actions.requestRender();
this.refineHandle = refineDictation({
apiKey: anthropicApiKeySetting.get(),
original: this.baseText,
followup,
onTextDelta: (_delta, textSoFar) => {
this.finalizedText = textSoFar;
this.actions.requestRender();
},
onDone: (text) => {
this.refineHandle = null;
this.backToMenu(text, "Send, continue, or discard?");
},
onError: (message) => {
this.refineHandle = null;
this.backToMenu(this.baseText, message);
},
});
}
/** Abort a continuation (mic or LLM stage) and restore the prior message. */
private cancelContinuation(status: string): void {
if (this.capturing) {
this.capturing = false;
void this.actions.stopVoiceCapture();
}
this.refineHandle?.cancel();
this.refineHandle = null;
this.backToMenu(this.baseText, status);
}
private backToMenu(text: string, status: string): void {
if (this.followupFinalizeTimer !== null) {
clearTimeout(this.followupFinalizeTimer);
this.followupFinalizeTimer = null;
}
this.finalizedText = text;
this.liveText = "";
this.phase = "menu";
this.menuIndex = 0;
this.status = status;
this.actions.requestRender();
}
onRemoved(): void {
this.unsubscribeTranscript?.();
this.unsubscribeTranscript = null;
this.unsubscribeStatus?.();
this.unsubscribeStatus = null;
this.unsubscribeSpeechEnd?.();
this.unsubscribeSpeechEnd = null;
if (this.followupFinalizeTimer !== null) {
clearTimeout(this.followupFinalizeTimer);
this.followupFinalizeTimer = null;
}
if (this.autoSendTimer !== null) {
clearTimeout(this.autoSendTimer);
this.autoSendTimer = null;
}
this.pendingAutoSend = false;
this.refineHandle?.cancel();
this.refineHandle = null;
if (this.capturing) {
this.capturing = false;
void this.actions.stopVoiceCapture();
}
this.onClosed();
}
private displayText(): string {
if (!this.finalizedText) return this.liveText;
if (!this.liveText) return this.finalizedText;
return `${this.finalizedText} ${this.liveText}`;
}
private placeholderText(): string {
switch (this.phase) {
case "capturing":
return "Listening...";
case "continuing":
return "Say more, or describe an edit...";
case "refining":
return "Refining...";
case "menu":
return "(no speech detected)";
}
}
private hintText(): string {
switch (this.phase) {
case "capturing":
return this.clickEndsCapture
? gestureHints([[GESTURE_CLICK, "done"], [GESTURE_DOUBLE_CLICK, "close"]])
: `${GESTURE_DOUBLE_CLICK} close`;
case "continuing":
return gestureHints([[GESTURE_CLICK, "done"], [GESTURE_DOUBLE_CLICK, "cancel"]]);
case "refining":
default:
return `${GESTURE_DOUBLE_CLICK} cancel`;
}
}
private onTranscript(event: VoiceTranscriptEvent): void {
// The refine stream owns the text buffers once it starts; a transcript
// that trails in after that point is stale.
if (this.phase === "refining") return;
if (event.isFinal) {
const finalText = event.text.trim() || this.liveText.trim();
if (finalText) {
this.finalizedText = this.finalizedText ? `${this.finalizedText} ${finalText}` : finalText;
}
this.liveText = "";
if (this.phase === "continuing" && this.followupFinalizeTimer !== null) {
// The follow-up finalized; no need to keep waiting.
this.beginRefine();
return;
}
if (this.pendingAutoSend) {
// The utterance finalized; skip-confirmation can fire without waiting
// out the fallback timer.
this.performAutoSend();
return;
}
} else {
this.liveText = event.text.trim();
}
this.actions.requestRender();
}
}