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

main
ts 77 lines 2.52 KB
Raw
1 import { msg } from "gt-react-native";
2
3 import { m } from "../../i18n/messages";
4 import {
5 VoiceTranscriptionError,
6 VOICE_RECORDING_LIMIT_SECONDS,
7 throwIfVoiceTranscriptionAborted,
8 type VoiceTranscriber,
9 } from "@t3tools/client-runtime/voice-input";
10
11 const pcmBytesPerSecond = 16_000 * 2;
12 const maxPcmBytes = pcmBytesPerSecond * VOICE_RECORDING_LIMIT_SECONDS;
13
14 export function createEnvironmentTranscriber(input: {
15 readonly canSend: () => boolean;
16 readonly prepare: (signal: AbortSignal) => Promise<string>;
17 readonly decode: (uri: string) => Promise<Uint8Array>;
18 readonly upload: (pcm: Uint8Array, signal: AbortSignal) => Promise<string>;
19 }): VoiceTranscriber {
20 function check(signal: AbortSignal) {
21 throwIfVoiceTranscriptionAborted(signal);
22 if (!input.canSend()) {
23 throw new VoiceTranscriptionError({
24 code: "cancelled",
25 message: m(msg("Your transcription choice changed.")),
26 });
27 }
28 }
29 return {
30 async prepare({ signal }) {
31 check(signal);
32 let locale = "auto";
33 const ready = input.prepare(signal).then((resolved) => {
34 check(signal);
35 locale = resolved;
36 });
37 void ready.catch(() => {});
38 return {
39 get locale() {
40 return locale;
41 },
42 async start(_uri, { signal }) {
43 await ready;
44 check(signal);
45 return null;
46 },
47 async transcribe(uri, { signal }) {
48 await ready;
49 check(signal);
50 const decoded = await input.decode(uri);
51 check(signal);
52 if (
53 !decoded.length ||
54 decoded.length % 2 ||
55 decoded.length > maxPcmBytes + pcmBytesPerSecond
56 ) {
57 throw new VoiceTranscriptionError({
58 code: "transcription-failed",
59 message: m(msg("This recording couldn’t be read. Try recording again.")),
60 });
61 }
62 // A timed native stop can include a final encoder packet beyond the recording limit.
63 const bytes = decoded.subarray(0, maxPcmBytes);
64 const samples =
65 bytes.byteOffset % 2 === 0
66 ? new Int16Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 2)
67 : new Int16Array(Uint8Array.from(bytes).buffer);
68 const pcm = new Float32Array(samples.length);
69 for (let i = 0; i < samples.length; i++) pcm[i] = samples[i]! / 32768;
70 const text = await input.upload(new Uint8Array(pcm.buffer), signal);
71 check(signal);
72 return text;
73 },
74 };
75 },
76 };
77 }