forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.ts
More file actions
493 lines (438 loc) · 15.4 KB
/
Copy pathcontroller.ts
File metadata and controls
493 lines (438 loc) · 15.4 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
493
import { replaceTextRange } from "@t3tools/shared/composerTrigger";
import type { PreparedVoiceTranscription, VoiceTranscriber } from "./transcription.ts";
export const VOICE_RECORDING_LIMIT_SECONDS = 5 * 60;
export type VoiceInputPhase = "idle" | "preparing" | "recording" | "transcribing" | "error";
export type VoiceInputState = {
readonly phase: VoiceInputPhase;
readonly error: string | null;
readonly errorAction: "retry" | "settings" | null;
};
export function voiceInputBlocksSubmission(state: VoiceInputState): boolean {
return (
state.phase === "preparing" || state.phase === "recording" || state.phase === "transcribing"
);
}
export function voiceInputFreezesEditor(state: VoiceInputState): boolean {
return voiceInputBlocksSubmission(state);
}
export type VoiceDraftSnapshot = {
readonly ownerKey: string;
readonly text: string;
readonly selection: { readonly start: number; readonly end: number };
readonly revision: number;
};
export type VoiceRecorderStatus = {
readonly isFinished: boolean;
readonly hasError: boolean;
readonly error: string | null;
readonly url: string | null;
};
export interface VoiceRecorder {
readonly uri: string | null;
prepareToRecordAsync(): Promise<void>;
record(options: { readonly forDuration: number }): void;
stop(): Promise<void>;
}
export type VoiceInputControllerDependencies = {
readonly recorder: VoiceRecorder;
readonly getTranscriber: () => VoiceTranscriber | null;
readonly requestPermission: () => Promise<{
readonly granted: boolean;
readonly canAskAgain: boolean;
}>;
readonly configureRecording: () => Promise<void>;
readonly releaseRecording: () => Promise<void>;
readonly deleteRecording: (uri: string) => void;
readonly readDraft: () => VoiceDraftSnapshot | null;
readonly commitDraft: (
text: string,
selection: { readonly start: number; readonly end: number },
) => void;
readonly onStateChange: (state: VoiceInputState) => void;
};
type TranscriptCommitResult =
| {
readonly kind: "commit";
readonly text: string;
readonly selection: { readonly start: number; readonly end: number };
}
| { readonly kind: "stale" }
| { readonly kind: "empty" };
export function resolveTranscriptCommit(
captured: VoiceDraftSnapshot,
current: VoiceDraftSnapshot | null,
transcript: string,
locale: string,
): TranscriptCommitResult {
if (
!current ||
current.ownerKey !== captured.ownerKey ||
current.text !== captured.text ||
current.revision !== captured.revision
) {
return { kind: "stale" };
}
const replacement = transcript.trim();
if (replacement.length === 0) {
return { kind: "empty" };
}
const isEmptySelection = captured.selection.start === captured.selection.end;
const normalizedLocale = locale.replaceAll("_", "-").toLowerCase();
const usesEnglishSpacing = normalizedLocale === "en" || normalizedLocale.startsWith("en-");
let insertion = replacement;
if (isEmptySelection && usesEnglishSpacing) {
const left = captured.text[captured.selection.start - 1];
const right = captured.text[captured.selection.start];
const leftNeedsBoundary =
left !== undefined &&
/[A-Za-z0-9.!?,:;)\]}'"]/.test(left) &&
(right === undefined || /\s/.test(right));
const rightNeedsBoundary =
right !== undefined &&
/[A-Za-z0-9([{'"]/.test(right) &&
(left === undefined || /\s/.test(left));
insertion = `${leftNeedsBoundary ? " " : ""}${replacement}${rightNeedsBoundary ? " " : ""}`;
}
const result = replaceTextRange(
captured.text,
captured.selection.start,
captured.selection.end,
insertion,
);
return {
kind: "commit",
text: result.text,
selection: { start: result.cursor, end: result.cursor },
};
}
let activeSession: symbol | null = null;
let activeTranscriptionOperation: Promise<unknown> | null = null;
function acquireSession(): symbol | null {
if (activeSession) return null;
const token = Symbol("voice-input-session");
activeSession = token;
return token;
}
function releaseSession(token: symbol | null): void {
if (token && activeSession === token) activeSession = null;
}
async function runTranscriptionOperation<T>(operation: () => Promise<T>): Promise<T> {
if (activeTranscriptionOperation) {
throw new Error("voice-operation-busy");
}
const promise = operation();
activeTranscriptionOperation = promise;
try {
return await promise;
} finally {
if (activeTranscriptionOperation === promise) activeTranscriptionOperation = null;
}
}
function errorCode(error: unknown): string | null {
if (typeof error !== "object" || error === null || !("code" in error)) return null;
return typeof error.code === "string" ? error.code : null;
}
function preparationErrorMessage(error: unknown): string {
if (error instanceof Error && error.message === "voice-operation-busy") {
return "Voice transcription is still finishing. Try again shortly.";
}
if (errorCode(error) === "unsupported-locale") {
return "Voice transcription is not available for this language.";
}
return "Could not prepare voice transcription.";
}
function transcriptionErrorMessage(error: unknown): string {
if (error instanceof Error && error.message === "voice-operation-busy") {
return "Voice transcription is still finishing. Try again shortly.";
}
return "Could not transcribe this recording.";
}
const IDLE_STATE: VoiceInputState = { phase: "idle", error: null, errorAction: null };
export class VoiceInputController {
private readonly dependencies: VoiceInputControllerDependencies;
private state: VoiceInputState = IDLE_STATE;
private operationToken = 0;
private sessionToken: symbol | null = null;
private transcription: PreparedVoiceTranscription | null = null;
private transcriptionAbortController: AbortController | null = null;
private capturedDraft: VoiceDraftSnapshot | null = null;
private recordingUri: string | null = null;
private readonly ownedRecordingUris = new Set<string>();
private recordingConfigured = false;
private finishing = false;
constructor(dependencies: VoiceInputControllerDependencies) {
this.dependencies = dependencies;
}
get currentState(): VoiceInputState {
return this.state;
}
async start(): Promise<void> {
if (this.state.phase !== "idle" && this.state.phase !== "error") return;
const initiatingDraft = this.dependencies.readDraft();
if (!initiatingDraft) {
this.setError("This draft is no longer available.", "retry");
return;
}
const sessionToken = acquireSession();
if (!sessionToken) {
this.setError("Another voice recording is already active.", "retry");
return;
}
this.sessionToken = sessionToken;
const operationToken = ++this.operationToken;
const abortController = new AbortController();
this.transcriptionAbortController = abortController;
this.setState({ phase: "preparing", error: null, errorAction: null });
try {
const transcriber = this.dependencies.getTranscriber();
if (!transcriber) {
this.setError("Voice transcription is not available.", null);
return;
}
const permission = await this.dependencies.requestPermission();
if (!this.isCurrent(operationToken)) return;
if (!permission.granted) {
this.setError(
"Microphone access is required for voice input.",
permission.canAskAgain ? "retry" : "settings",
);
return;
}
try {
this.transcription = await runTranscriptionOperation(() =>
transcriber.prepare({ signal: abortController.signal }),
);
} catch (error) {
if (this.isCurrent(operationToken)) this.setError(preparationErrorMessage(error), "retry");
return;
}
if (!this.isCurrent(operationToken)) return;
await this.dependencies.configureRecording();
this.recordingConfigured = true;
if (!this.isCurrent(operationToken)) return;
await this.dependencies.recorder.prepareToRecordAsync();
if (!this.isCurrent(operationToken)) return;
this.recordingUri = this.dependencies.recorder.uri;
this.rememberRecordingUri(this.recordingUri);
const capturedDraft = this.dependencies.readDraft();
if (!capturedDraft || capturedDraft.ownerKey !== initiatingDraft.ownerKey) {
this.setError("This draft is no longer available.", "retry");
return;
}
this.capturedDraft = capturedDraft;
this.dependencies.recorder.record({ forDuration: VOICE_RECORDING_LIMIT_SECONDS });
this.setState({ phase: "recording", error: null, errorAction: null });
} catch {
if (this.isCurrent(operationToken))
this.setError("Could not start voice recording.", "retry");
} finally {
if (this.isCurrent(operationToken) && this.state.phase === "error") {
await this.releaseResources();
} else if (!this.isCurrent(operationToken) && !this.finishing) {
await this.releaseResources();
}
}
}
stop(): Promise<void> {
if (this.state.phase !== "recording") return Promise.resolve();
return this.finishRecording(false, null);
}
cancel(): void {
switch (this.state.phase) {
case "idle":
return;
case "error":
this.setState(IDLE_STATE);
return;
case "preparing":
this.invalidateOperation();
this.setState(IDLE_STATE);
return;
case "recording":
this.discardRecording(null);
return;
case "transcribing":
this.invalidateOperation();
this.setState(IDLE_STATE);
return;
}
}
interruptRecording(
message = "Voice recording was interrupted.",
completedUri: string | null = null,
): Promise<void> | void {
if (this.state.phase !== "recording") return;
this.rememberRecordingUri(completedUri);
this.recordingUri = completedUri ?? this.recordingUri;
return this.discardRecording(message);
}
appMovedToBackground(): Promise<void> | void {
if (this.state.phase === "preparing") {
this.invalidateOperation();
this.setError("Voice input stopped when the app moved to the background.", "retry");
return;
}
return this.interruptRecording();
}
handleRecorderStatus(status: VoiceRecorderStatus): Promise<void> | void {
if (this.state.phase !== "recording") return;
if (status.hasError) {
return this.interruptRecording(
status.error ?? "Voice recording was interrupted.",
status.url,
);
}
if (status.isFinished) {
if (!status.url) {
return this.interruptRecording();
}
return this.finishRecording(true, status.url);
}
}
ownerChanged(): void {
if (this.state.phase === "idle") return;
this.cancel();
}
dispose(): void {
if (this.state.phase === "recording") {
this.discardRecording(null);
return;
}
if (this.state.phase === "preparing" || this.state.phase === "transcribing") {
this.invalidateOperation();
this.setState(IDLE_STATE);
}
}
private async finishRecording(
alreadyStopped: boolean,
completedUri: string | null,
): Promise<void> {
if (this.finishing || this.state.phase !== "recording") return;
this.finishing = true;
const operationToken = this.operationToken;
this.setState({ phase: "transcribing", error: null, errorAction: null });
try {
if (!alreadyStopped) await this.dependencies.recorder.stop();
await this.releaseAudioSession();
this.recordingUri = completedUri ?? this.dependencies.recorder.uri ?? this.recordingUri;
this.rememberRecordingUri(this.recordingUri);
if (!this.isCurrent(operationToken)) return;
if (
!this.recordingUri ||
!this.transcription ||
!this.transcriptionAbortController ||
!this.capturedDraft
) {
this.setError("Could not finish voice recording.", "retry");
return;
}
const recordingUri = this.recordingUri;
const transcription = this.transcription;
const signal = this.transcriptionAbortController.signal;
const capturedDraft = this.capturedDraft;
let transcript: string;
try {
transcript = await runTranscriptionOperation(() =>
transcription.transcribe(recordingUri, { signal }),
);
} catch (error) {
if (this.isCurrent(operationToken)) {
this.setError(transcriptionErrorMessage(error), "retry");
}
return;
}
if (!this.isCurrent(operationToken)) return;
const result = resolveTranscriptCommit(
capturedDraft,
this.dependencies.readDraft(),
transcript,
transcription.locale,
);
if (result.kind === "stale") {
this.setError(
"The draft changed while voice input was running. The transcript was not added.",
"retry",
);
return;
}
if (result.kind === "empty") {
this.setError("No speech was detected.", "retry");
return;
}
this.dependencies.commitDraft(result.text, result.selection);
this.setState(IDLE_STATE);
} catch {
if (this.isCurrent(operationToken)) {
this.setError("Could not finish voice recording.", "retry");
}
} finally {
this.finishing = false;
await this.releaseResources();
}
}
private async discardRecording(error: string | null): Promise<void> {
this.invalidateOperation();
this.setState(
error
? { phase: "error", error, errorAction: "retry" }
: { phase: "idle", error: null, errorAction: null },
);
try {
await this.dependencies.recorder.stop();
this.rememberRecordingUri(this.dependencies.recorder.uri);
} catch {
this.rememberRecordingUri(this.dependencies.recorder.uri);
} finally {
await this.releaseResources();
}
}
private async releaseResources(): Promise<void> {
this.rememberRecordingUri(this.recordingUri);
this.rememberRecordingUri(this.dependencies.recorder.uri);
this.recordingUri = null;
for (const uri of this.ownedRecordingUris) {
try {
this.dependencies.deleteRecording(uri);
} catch {
// The cache may already have removed a failed or interrupted recording.
}
}
this.ownedRecordingUris.clear();
await this.releaseAudioSession();
releaseSession(this.sessionToken);
this.sessionToken = null;
this.capturedDraft = null;
this.transcription = null;
this.transcriptionAbortController = null;
}
private rememberRecordingUri(uri: string | null): void {
if (uri) this.ownedRecordingUris.add(uri);
}
private async releaseAudioSession(): Promise<void> {
if (!this.recordingConfigured) return;
try {
await this.dependencies.releaseRecording();
this.recordingConfigured = false;
} catch {
// Final cleanup retries if the prompt release before transcription fails.
}
}
private invalidateOperation(): void {
this.operationToken += 1;
this.transcriptionAbortController?.abort();
}
private isCurrent(operationToken: number): boolean {
return operationToken === this.operationToken;
}
private setError(error: string, errorAction: VoiceInputState["errorAction"]): void {
this.setState({ phase: "error", error, errorAction });
}
private setState(state: VoiceInputState): void {
this.state = state;
this.dependencies.onStateChange(state);
}
}
export function resetVoiceInputGlobalsForTests(): void {
activeSession = null;
activeTranscriptionOperation = null;
}