The best way to control Claude Code, Codex and more from any computer or phone

main
ts 213 lines 6.91 KB
Raw
1 import {
2 VOICE_RECORDING_LIMIT_SECONDS,
3 VoiceInputController,
4 voiceInputBlocksSubmission,
5 type VoiceDraftSnapshot,
6 type VoiceInputControllerDependencies,
7 type VoiceInputPhase,
8 type VoiceInputState,
9 } from "@t3tools/client-runtime/voice-input";
10
11 import { m } from "../../i18n/messages";
12 import { normalizeVoiceInputDecibels, VOICE_WAVEFORM_SAMPLE_COUNT } from "./voiceInputMetering";
13
14 const VOICE_METERING_INTERVAL_MS = 80;
15 const IDLE_STATE: VoiceInputState = { phase: "idle", error: null, errorAction: null };
16 const SILENT_LEVELS: ReadonlyArray<number> = Array<number>(VOICE_WAVEFORM_SAMPLE_COUNT).fill(0);
17
18 export type VoiceInputTarget = {
19 readonly ownerKey: string;
20 readonly label?: string;
21 readonly readDraft: () => VoiceDraftSnapshot | null;
22 readonly commitDraft: VoiceInputControllerDependencies["commitDraft"];
23 };
24
25 export type VoiceInputSnapshot = {
26 readonly state: VoiceInputState;
27 readonly ownerKey: string | null;
28 readonly label: string | null;
29 readonly elapsedSeconds: number;
30 readonly focusedOwners: ReadonlySet<string>;
31 };
32
33 export type VoiceRecordingSample = {
34 readonly durationMillis: number;
35 readonly metering: number | undefined;
36 };
37
38 export type VoiceInputSessionDependencies = Omit<
39 VoiceInputControllerDependencies,
40 "readDraft" | "commitDraft" | "onStateChange"
41 > & {
42 /** Reads the live recorder, or null when it is not capturing. */
43 readonly sampleRecording: () => VoiceRecordingSample | null;
44 /** Levels change every metering tick, so they bypass React state. */
45 readonly publishAudioLevels: (levels: ReadonlyArray<number>) => void;
46 /** Holds the screen awake while recording; returns the release. */
47 readonly keepAwake: () => () => void;
48 };
49
50 export function createVoiceInputTarget(
51 ownerKey: string,
52 readText: () => string | null,
53 commitDraft: VoiceInputTarget["commitDraft"],
54 selection: VoiceDraftSnapshot["selection"],
55 ): VoiceInputTarget {
56 return {
57 ownerKey,
58 readDraft: () => {
59 const text = readText();
60 return text === null ? null : { ownerKey, text, selection };
61 },
62 commitDraft,
63 };
64 }
65
66 /**
67 * The app's single voice input: it owns the controller, the recording-only
68 * side effects (keep-awake, metering) and publishes what the UI renders.
69 */
70 export class VoiceInputSession {
71 readonly controller: VoiceInputController;
72 private readonly dependencies: VoiceInputSessionDependencies;
73 private target: VoiceInputTarget | null = null;
74 private startText: string | null = null;
75 private snapshot: VoiceInputSnapshot = {
76 state: IDLE_STATE,
77 ownerKey: null,
78 label: null,
79 elapsedSeconds: 0,
80 focusedOwners: new Set(),
81 };
82 private readonly listeners = new Set<() => void>();
83 private audioLevels = SILENT_LEVELS;
84 private releaseKeepAwake: (() => void) | null = null;
85 private meteringInterval: ReturnType<typeof setInterval> | null = null;
86
87 constructor(dependencies: VoiceInputSessionDependencies) {
88 this.dependencies = dependencies;
89 this.controller = new VoiceInputController({
90 resolve: m,
91 ...dependencies,
92 onStateChange: (state) => this.handleStateChange(state),
93 // The target's selection is fixed at start, so a draft changed since refuses the transcript.
94 readDraft: () => {
95 const draft = this.target?.readDraft() ?? null;
96 return draft?.text === this.startText ? draft : null;
97 },
98 commitDraft: (text, selection) => this.target?.commitDraft(text, selection),
99 });
100 }
101
102 get ownerKey(): string | null {
103 return this.target?.ownerKey ?? null;
104 }
105
106 get label(): string | null {
107 return this.target?.label ?? null;
108 }
109
110 readonly subscribe = (listener: () => void): (() => void) => {
111 this.listeners.add(listener);
112 return () => this.listeners.delete(listener);
113 };
114
115 readonly getSnapshot = (): VoiceInputSnapshot => this.snapshot;
116
117 setOwnerFocused(ownerKey: string, focused: boolean): void {
118 if (this.snapshot.focusedOwners.has(ownerKey) === focused) return;
119 const focusedOwners = new Set(this.snapshot.focusedOwners);
120 if (focused) focusedOwners.add(ownerKey);
121 else focusedOwners.delete(ownerKey);
122 this.publish({ ...this.snapshot, focusedOwners });
123 }
124
125 cancel(ownerKey: string | null): void {
126 if (ownerKey !== null && this.ownerKey === ownerKey) this.controller.cancel();
127 }
128
129 retry(): Promise<void> {
130 return this.target ? this.start(this.target) : Promise.resolve();
131 }
132
133 start(target: VoiceInputTarget): Promise<void> {
134 if (voiceInputBlocksSubmission(this.controller.currentState)) return Promise.resolve();
135 this.target = target;
136 this.startText = target.readDraft()?.text ?? null;
137 return this.controller.start();
138 }
139
140 dispose(): void {
141 this.controller.dispose();
142 this.stopRecordingActivity();
143 this.listeners.clear();
144 }
145
146 private handleStateChange(state: VoiceInputState): void {
147 const previousPhase = this.snapshot.state.phase;
148 let elapsedSeconds = this.snapshot.elapsedSeconds;
149 if (state.phase !== previousPhase) {
150 if (previousPhase === "recording") this.stopRecordingActivity();
151 if (capturesAudio(state.phase)) {
152 if (this.audioLevels.some((level) => level !== 0)) this.setAudioLevels(SILENT_LEVELS);
153 elapsedSeconds = 0;
154 }
155 }
156 this.publish({
157 ...this.snapshot,
158 state,
159 ownerKey: this.ownerKey,
160 label: this.label,
161 elapsedSeconds,
162 });
163 if (state.phase === "recording" && previousPhase !== "recording") {
164 this.startRecordingActivity();
165 }
166 }
167
168 private startRecordingActivity(): void {
169 this.releaseKeepAwake = this.dependencies.keepAwake();
170 this.sampleRecording();
171 this.meteringInterval = setInterval(() => this.sampleRecording(), VOICE_METERING_INTERVAL_MS);
172 }
173
174 private stopRecordingActivity(): void {
175 if (this.meteringInterval !== null) clearInterval(this.meteringInterval);
176 this.meteringInterval = null;
177 this.releaseKeepAwake?.();
178 this.releaseKeepAwake = null;
179 }
180
181 private sampleRecording(): void {
182 if (this.controller.currentState.phase !== "recording") return;
183 const sample = this.dependencies.sampleRecording();
184 if (!sample) return;
185
186 const level = normalizeVoiceInputDecibels(sample.metering);
187 if (level !== 0 || this.audioLevels.some((sample) => sample !== 0)) {
188 this.setAudioLevels([...this.audioLevels.slice(1), level]);
189 }
190
191 const elapsedSeconds = Math.min(
192 VOICE_RECORDING_LIMIT_SECONDS,
193 Math.max(0, Math.floor(sample.durationMillis / 1_000)),
194 );
195 if (elapsedSeconds !== this.snapshot.elapsedSeconds) {
196 this.publish({ ...this.snapshot, elapsedSeconds });
197 }
198 }
199
200 private setAudioLevels(levels: ReadonlyArray<number>): void {
201 this.audioLevels = levels;
202 this.dependencies.publishAudioLevels(levels);
203 }
204
205 private publish(snapshot: VoiceInputSnapshot): void {
206 this.snapshot = snapshot;
207 for (const listener of this.listeners) listener();
208 }
209 }
210
211 function capturesAudio(phase: VoiceInputPhase): boolean {
212 return phase === "preparing" || phase === "recording";
213 }