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 { AuthClientMetadata, AuthPairingLink } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react-native"; |
| 3 | |
| 4 | import { m } from "../../i18n/messages"; |
| 5 | |
| 6 | export interface ConnectDeviceApprovalPrompt { |
| 7 | readonly pairingLinkId: string; |
| 8 | readonly title: string; |
| 9 | readonly message: string; |
| 10 | } |
| 11 | |
| 12 | export function describeConnectDevice(device: AuthClientMetadata | undefined): string { |
| 13 | const { label, browser, os } = device ?? {}; |
| 14 | if (label) return label; |
| 15 | if (browser && os) return m(msg("{browser} on {os}", { browser, os })); |
| 16 | return browser ?? os ?? m(msg("A new device")); |
| 17 | } |
| 18 | |
| 19 | /** Requests waiting on approval that have not been shown yet, oldest first. */ |
| 20 | export function connectDeviceApprovalPrompts(input: { |
| 21 | readonly environmentLabel: string; |
| 22 | readonly pairingLinks: ReadonlyArray<AuthPairingLink>; |
| 23 | readonly shownIds: ReadonlySet<string>; |
| 24 | }): ReadonlyArray<ConnectDeviceApprovalPrompt> { |
| 25 | const waiting = input.pairingLinks.flatMap((link) => |
| 26 | link.connectDeviceRequest !== undefined && !input.shownIds.has(link.id) |
| 27 | ? [{ link, ...link.connectDeviceRequest }] |
| 28 | : [], |
| 29 | ); |
| 30 | return [...waiting] |
| 31 | .sort( |
| 32 | (left, right) => |
| 33 | left.link.createdAt.epochMilliseconds - right.link.createdAt.epochMilliseconds, |
| 34 | ) |
| 35 | .map(({ link, verificationCode, client: device }) => ({ |
| 36 | pairingLinkId: link.id, |
| 37 | title: m(msg("Approve this device?")), |
| 38 | message: m( |
| 39 | msg( |
| 40 | "{device} wants to connect to {environment} through {product}. It shows code {code}. Approve it only if that code matches.", |
| 41 | { |
| 42 | device: describeConnectDevice(device), |
| 43 | environment: input.environmentLabel, |
| 44 | product: "T3 Connect", |
| 45 | code: verificationCode, |
| 46 | }, |
| 47 | ), |
| 48 | ), |
| 49 | })); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Shows approval prompts one at a time: an Android alert replaces the one on screen, which |
| 54 | * would drop an unanswered request. A prompt answered with "retry" goes back to the front; |
| 55 | * "later" puts it back without showing anything until the next offer. |
| 56 | */ |
| 57 | export function createApprovalPromptQueue<P extends { readonly pairingLinkId: string }>( |
| 58 | present: (prompt: P, answer: (outcome: "done" | "retry" | "later") => void) => void, |
| 59 | ) { |
| 60 | const known = new Set<string>(); |
| 61 | const waiting: P[] = []; |
| 62 | let showing: { readonly prompt: P; dropped: boolean } | null = null; |
| 63 | const next = () => { |
| 64 | if (showing) return; |
| 65 | const prompt = waiting.shift(); |
| 66 | if (prompt === undefined) return; |
| 67 | const current = { prompt, dropped: false }; |
| 68 | showing = current; |
| 69 | let answered = false; |
| 70 | present(prompt, (outcome) => { |
| 71 | if (answered) return; |
| 72 | answered = true; |
| 73 | showing = null; |
| 74 | // A dropped prompt is forgotten, so its host can offer it again when it comes back. |
| 75 | if (outcome !== "done" && !current.dropped) waiting.unshift(prompt); |
| 76 | if (outcome !== "later") next(); |
| 77 | }); |
| 78 | }; |
| 79 | return { |
| 80 | known: known as ReadonlySet<string>, |
| 81 | /** Forgets waiting prompts that match, such as a host that went away. */ |
| 82 | drop(matches: (prompt: P) => boolean) { |
| 83 | if (showing && matches(showing.prompt)) { |
| 84 | showing.dropped = true; |
| 85 | known.delete(showing.prompt.pairingLinkId); |
| 86 | } |
| 87 | for (let index = waiting.length - 1; index >= 0; index--) { |
| 88 | const prompt = waiting[index]!; |
| 89 | if (!matches(prompt)) continue; |
| 90 | waiting.splice(index, 1); |
| 91 | known.delete(prompt.pairingLinkId); |
| 92 | } |
| 93 | }, |
| 94 | offer(prompts: ReadonlyArray<P>) { |
| 95 | // Also re-shows a prompt deferred because another dialog took the screen. |
| 96 | for (const prompt of prompts) { |
| 97 | if (known.has(prompt.pairingLinkId)) continue; |
| 98 | known.add(prompt.pairingLinkId); |
| 99 | waiting.push(prompt); |
| 100 | } |
| 101 | next(); |
| 102 | }, |
| 103 | }; |
| 104 | } |