The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { ClientSettings, VoiceMicrophoneEntry } from "@t3tools/contracts"; |
| 2 | |
| 3 | import type { BrowserMicrophone } from "./browserMicrophones"; |
| 4 | |
| 5 | export type MicrophoneEntry = VoiceMicrophoneEntry; |
| 6 | |
| 7 | const SYSTEM_DEFAULT: MicrophoneEntry = { kind: "default" }; |
| 8 | |
| 9 | // Chromium lists the current default and Windows communications device under these ids too. |
| 10 | const ALIAS_DEVICE_IDS = new Set(["", "default", "communications"]); |
| 11 | |
| 12 | /** True for ids that stand for the system default, including the id-less input shown before permission. */ |
| 13 | export function isSystemDefaultDeviceId(deviceId: string): boolean { |
| 14 | return ALIAS_DEVICE_IDS.has(deviceId); |
| 15 | } |
| 16 | |
| 17 | export function microphoneEntryKey(entry: MicrophoneEntry): string { |
| 18 | return entry.kind === "default" ? "default" : `device:${entry.deviceId}`; |
| 19 | } |
| 20 | |
| 21 | /** The saved order with exactly one System default row; an empty order starts from the legacy choice. */ |
| 22 | export function microphoneOrder( |
| 23 | settings: Pick<ClientSettings, "voiceMicrophones" | "voiceMicrophone">, |
| 24 | ): ReadonlyArray<MicrophoneEntry> { |
| 25 | if (settings.voiceMicrophones.length === 0) { |
| 26 | return settings.voiceMicrophone |
| 27 | ? [{ kind: "device", deviceId: settings.voiceMicrophone, label: "" }, SYSTEM_DEFAULT] |
| 28 | : [SYSTEM_DEFAULT]; |
| 29 | } |
| 30 | const keys = new Set<string>(); |
| 31 | const order = settings.voiceMicrophones.filter((entry) => { |
| 32 | const key = microphoneEntryKey(entry); |
| 33 | if (keys.has(key)) return false; |
| 34 | keys.add(key); |
| 35 | return true; |
| 36 | }); |
| 37 | return keys.has("default") ? order : [...order, SYSTEM_DEFAULT]; |
| 38 | } |
| 39 | |
| 40 | /** Adds labelled devices seen for the first time directly below System default and refreshes labels. */ |
| 41 | export function rememberMicrophones( |
| 42 | order: ReadonlyArray<MicrophoneEntry>, |
| 43 | devices: ReadonlyArray<BrowserMicrophone>, |
| 44 | ): ReadonlyArray<MicrophoneEntry> { |
| 45 | let next: MicrophoneEntry[] | null = null; |
| 46 | let insertAt = order.findIndex((entry) => entry.kind === "default") + 1; |
| 47 | for (const device of devices) { |
| 48 | if (!device.label || ALIAS_DEVICE_IDS.has(device.deviceId)) continue; |
| 49 | const current: ReadonlyArray<MicrophoneEntry> = next ?? order; |
| 50 | const index = current.findIndex( |
| 51 | (entry) => entry.kind === "device" && entry.deviceId === device.deviceId, |
| 52 | ); |
| 53 | const existing = current[index]; |
| 54 | if (existing?.kind === "device" && existing.label === device.label) continue; |
| 55 | next ??= [...order]; |
| 56 | const entry: MicrophoneEntry = { |
| 57 | kind: "device", |
| 58 | deviceId: device.deviceId, |
| 59 | label: device.label, |
| 60 | }; |
| 61 | if (index !== -1) { |
| 62 | next[index] = entry; |
| 63 | continue; |
| 64 | } |
| 65 | next.splice(insertAt, 0, entry); |
| 66 | insertAt += 1; |
| 67 | } |
| 68 | return next ?? order; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * The first entry that is connected right now. System default always is. Before microphone |
| 73 | * permission the browser hides device ids, so the first entry is used as it was saved. |
| 74 | */ |
| 75 | export function preferredMicrophone( |
| 76 | order: ReadonlyArray<MicrophoneEntry>, |
| 77 | devices: ReadonlyArray<BrowserMicrophone>, |
| 78 | ): MicrophoneEntry { |
| 79 | if (devices.every((device) => !device.deviceId)) return order[0] ?? SYSTEM_DEFAULT; |
| 80 | return ( |
| 81 | order.find( |
| 82 | (entry) => |
| 83 | entry.kind === "default" || devices.some((device) => device.deviceId === entry.deviceId), |
| 84 | ) ?? SYSTEM_DEFAULT |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** The `deviceId` to record from, or an empty string for System default. */ |
| 89 | export function preferredMicrophoneId( |
| 90 | order: ReadonlyArray<MicrophoneEntry>, |
| 91 | devices: ReadonlyArray<BrowserMicrophone>, |
| 92 | ): string { |
| 93 | const entry = preferredMicrophone(order, devices); |
| 94 | return entry.kind === "device" ? entry.deviceId : ""; |
| 95 | } |
| 96 | |
| 97 | export function moveMicrophone( |
| 98 | order: ReadonlyArray<MicrophoneEntry>, |
| 99 | key: string, |
| 100 | steps: number, |
| 101 | ): ReadonlyArray<MicrophoneEntry> { |
| 102 | const from = order.findIndex((entry) => microphoneEntryKey(entry) === key); |
| 103 | const to = Math.max(0, Math.min(order.length - 1, from + steps)); |
| 104 | if (from === -1 || from === to) return order; |
| 105 | const next = [...order]; |
| 106 | const [entry] = next.splice(from, 1); |
| 107 | if (!entry) return order; |
| 108 | next.splice(to, 0, entry); |
| 109 | return next; |
| 110 | } |
| 111 | |
| 112 | /** Moves an entry to the top, adding it when it is not listed yet. */ |
| 113 | export function preferMicrophone( |
| 114 | order: ReadonlyArray<MicrophoneEntry>, |
| 115 | entry: MicrophoneEntry, |
| 116 | ): ReadonlyArray<MicrophoneEntry> { |
| 117 | const key = microphoneEntryKey(entry); |
| 118 | const existing = order.find((candidate) => microphoneEntryKey(candidate) === key); |
| 119 | return [existing ?? entry, ...order.filter((candidate) => microphoneEntryKey(candidate) !== key)]; |
| 120 | } |
| 121 | |
| 122 | /** Removes a named microphone. System default stays. */ |
| 123 | export function forgetMicrophone( |
| 124 | order: ReadonlyArray<MicrophoneEntry>, |
| 125 | key: string, |
| 126 | ): ReadonlyArray<MicrophoneEntry> { |
| 127 | return key === "default" ? order : order.filter((entry) => microphoneEntryKey(entry) !== key); |
| 128 | } |