The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { VoiceTranscriptionDestination } from "@t3tools/contracts"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import { useSyncExternalStore } from "react"; |
| 4 | import { randomUUID } from "../lib/utils"; |
| 5 | import { |
| 6 | getLocalStorageItem, |
| 7 | readLocalStorageValue, |
| 8 | subscribeLocalStorageKey, |
| 9 | useLocalStorage, |
| 10 | writeLocalStorageValue, |
| 11 | } from "../hooks/useLocalStorage"; |
| 12 | |
| 13 | // Separate keys: a stale window saving ordinary preferences must not restore either grant. |
| 14 | const AUDIO_KEY = "t3code:voice-audio-grant:v1"; |
| 15 | const CLEANUP_KEY = "t3code:voice-cleanup-grant:v1"; |
| 16 | // "device" is Apple Speech in the macOS desktop app. Audio stays on this device, so it stays out of the shared contract. |
| 17 | const AudioDestination = Schema.Union([ |
| 18 | VoiceTranscriptionDestination, |
| 19 | Schema.Struct({ kind: Schema.Literal("device") }), |
| 20 | ]); |
| 21 | export type AudioDestination = typeof AudioDestination.Type; |
| 22 | const Grant = Schema.Struct({ id: Schema.String, destination: AudioDestination }); |
| 23 | const AudioGrant = Schema.NullOr(Grant); |
| 24 | const sameGrant = Schema.toEquivalence(AudioGrant); |
| 25 | const SavedChoice = Schema.Union([AudioGrant, Schema.Literal("off")]); |
| 26 | const AudioChoice = Schema.Union([ |
| 27 | SavedChoice, |
| 28 | Schema.Struct({ pending: Grant, previous: SavedChoice }), |
| 29 | ]); |
| 30 | type AudioChoice = typeof AudioChoice.Type; |
| 31 | const savedKey = (id: string) => `${AUDIO_KEY}:saved:${id}`; |
| 32 | const pendingGrant = (choice: AudioChoice) => |
| 33 | choice && choice !== "off" && "pending" in choice ? choice.pending : null; |
| 34 | const CleanupGrant = Schema.NullOr(Schema.String); |
| 35 | const decodeChoice = Schema.decodeSync(Schema.fromJsonString(AudioChoice)); |
| 36 | const readChoice = () => readLocalStorageValue(AUDIO_KEY, null, AudioChoice); |
| 37 | |
| 38 | function readSaveMarker(id: string | undefined) { |
| 39 | try { |
| 40 | return id ? window.localStorage.getItem(savedKey(id)) : null; |
| 41 | } catch { |
| 42 | return undefined; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | function activeChoice(choice: AudioChoice, marker: string | null | undefined) { |
| 47 | if (!choice || choice === "off" || !("pending" in choice)) return choice; |
| 48 | // Only a confirmed missing marker means the previous grant still applies. |
| 49 | if (marker === null) return choice.previous; |
| 50 | return marker === "true" ? choice.pending : ("off" as const); |
| 51 | } |
| 52 | |
| 53 | export function useAudioChoice() { |
| 54 | const [choice] = useLocalStorage(AUDIO_KEY, null, AudioChoice); |
| 55 | const id = pendingGrant(choice)?.id; |
| 56 | const marker = useSyncExternalStore( |
| 57 | (notify) => (id ? subscribeLocalStorageKey(savedKey(id), notify) : () => {}), |
| 58 | () => readSaveMarker(id), |
| 59 | () => undefined, |
| 60 | ); |
| 61 | return activeChoice(choice, marker); |
| 62 | } |
| 63 | |
| 64 | function replaceChoice(choice: AudioChoice, previous = readChoice()) { |
| 65 | const old = pendingGrant(previous); |
| 66 | writeLocalStorageValue(AUDIO_KEY, choice, null, AudioChoice); |
| 67 | if (old && pendingGrant(getLocalStorageItem(AUDIO_KEY, AudioChoice))?.id !== old.id) |
| 68 | window.localStorage.removeItem(savedKey(old.id)); |
| 69 | } |
| 70 | |
| 71 | export function chooseAudioDestination(destination: AudioDestination | null) { |
| 72 | replaceChoice(destination ? { id: randomUUID(), destination } : "off"); |
| 73 | } |
| 74 | |
| 75 | export function pendingCloudAudioChoice( |
| 76 | destination: Extract<VoiceTranscriptionDestination, { kind: "cloud" }>, |
| 77 | ) { |
| 78 | const choice = readChoice(); |
| 79 | const pending = { id: randomUUID(), destination }; |
| 80 | replaceChoice( |
| 81 | { pending, previous: activeChoice(choice, readSaveMarker(pendingGrant(choice)?.id)) }, |
| 82 | choice, |
| 83 | ); |
| 84 | // A late response can acknowledge only its own choice, never write the destination another tab picked. |
| 85 | return () => { |
| 86 | if (pendingGrant(readChoice())?.id === pending.id) |
| 87 | writeLocalStorageValue(savedKey(pending.id), true, false, Schema.Boolean); |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | // Bind this recording to this act of consent, even if the user later reselects the same destination. |
| 92 | export function bindAudioGrant(id: string) { |
| 93 | try { |
| 94 | let cachedRaw: string | null = null; |
| 95 | let choice: AudioChoice = null; |
| 96 | const current = () => { |
| 97 | const raw = window.localStorage.getItem(AUDIO_KEY); |
| 98 | if (raw !== cachedRaw) { |
| 99 | choice = raw ? decodeChoice(raw) : null; |
| 100 | cachedRaw = raw; |
| 101 | } |
| 102 | const active = activeChoice(choice, readSaveMarker(pendingGrant(choice)?.id)); |
| 103 | return active === "off" ? null : active; |
| 104 | }; |
| 105 | const bound = current(); |
| 106 | if (bound?.id !== id) return () => false; |
| 107 | return () => { |
| 108 | try { |
| 109 | return sameGrant(current(), bound); |
| 110 | } catch { |
| 111 | return false; |
| 112 | } |
| 113 | }; |
| 114 | } catch { |
| 115 | return () => false; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | export function useCleanupGrant() { |
| 120 | return useLocalStorage(CLEANUP_KEY, null, CleanupGrant)[0]; |
| 121 | } |
| 122 | export function readCleanupGrant() { |
| 123 | return readLocalStorageValue(CLEANUP_KEY, null, CleanupGrant); |
| 124 | } |
| 125 | export function chooseTranscriptCleanup(enabled: boolean) { |
| 126 | writeLocalStorageValue(CLEANUP_KEY, enabled ? randomUUID() : null, null, CleanupGrant); |
| 127 | } |