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 { |
| 2 | SimlockDeviceHostConfig, |
| 3 | type EnvironmentId, |
| 4 | type SettingsSecretValue, |
| 5 | type SimlockDeviceHostConfig as SimlockDeviceHostConfigType, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { msg } from "gt-react"; |
| 10 | |
| 11 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 12 | |
| 13 | const parseSimlockDeviceHostDraft = Schema.decodeUnknownOption(SimlockDeviceHostConfig); |
| 14 | |
| 15 | export const newSimlockDeviceHost = (id: string): SimlockDeviceHostConfigType => ({ |
| 16 | kind: "simlock", |
| 17 | id, |
| 18 | label: "Simlock", |
| 19 | gatewayUrl: "", |
| 20 | agentToken: "", |
| 21 | maxLeases: 2, |
| 22 | maxLeaseMinutes: 60, |
| 23 | }); |
| 24 | |
| 25 | /** Adds, replaces, or removes one Simlock host. */ |
| 26 | export function updateSimlockDeviceHosts( |
| 27 | hosts: ReadonlyArray<SimlockDeviceHostConfigType>, |
| 28 | host: SimlockDeviceHostConfigType, |
| 29 | remove: boolean, |
| 30 | ): ReadonlyArray<SimlockDeviceHostConfigType> { |
| 31 | if (remove) return hosts.filter((candidate) => candidate.id !== host.id); |
| 32 | return hosts.some((candidate) => candidate.id === host.id) |
| 33 | ? hosts.map((candidate) => (candidate.id === host.id ? host : candidate)) |
| 34 | : [...hosts, host]; |
| 35 | } |
| 36 | |
| 37 | /** Why the shared fields keep a draft from saving, or null; tokens are checked per environment. */ |
| 38 | export function simlockDeviceHostProblem( |
| 39 | host: SimlockDeviceHostConfigType, |
| 40 | resolve: MessageResolver = m, |
| 41 | ): string | null { |
| 42 | const withToken = { ...host, agentToken: "token" }; |
| 43 | if (Option.isSome(parseSimlockDeviceHostDraft(withToken))) return null; |
| 44 | if (!host.label.trim()) return resolve(msg("Give the host a label.")); |
| 45 | // With in-range limits, a draft that still fails has a bad gateway URL. |
| 46 | return Option.isNone( |
| 47 | parseSimlockDeviceHostDraft({ ...withToken, maxLeases: 1, maxLeaseMinutes: 60 }), |
| 48 | ) |
| 49 | ? resolve(msg("Enter the gateway's origin, such as https://gateway.example.ts.net:4700.")) |
| 50 | : resolve(msg("Keep the limits within the ranges shown.")); |
| 51 | } |
| 52 | |
| 53 | export function simlockLeaseLimits( |
| 54 | host: SimlockDeviceHostConfigType, |
| 55 | resolve: MessageResolver = m, |
| 56 | ) { |
| 57 | return resolve( |
| 58 | msg( |
| 59 | "{count, plural, one {Up to {count} device at once, each released after {minutes} min} other {Up to {count} devices at once, each released after {minutes} min}}", |
| 60 | { count: host.maxLeases, minutes: host.maxLeaseMinutes }, |
| 61 | ), |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | export interface SimlockHostEnvironment { |
| 66 | readonly environmentId: EnvironmentId; |
| 67 | readonly label: string; |
| 68 | readonly hosts: ReadonlyArray<SimlockDeviceHostConfigType>; |
| 69 | } |
| 70 | |
| 71 | /** A token field: the environment's saved token, a replacement or secret reference, or none. */ |
| 72 | export type SimlockTokenEntry = |
| 73 | | { kind: "saved" } |
| 74 | | { kind: "new"; value: SettingsSecretValue } |
| 75 | | { kind: "cleared" }; |
| 76 | |
| 77 | /** |
| 78 | * The host list each environment saves: the shared fields everywhere, and each environment's own |
| 79 | * token, which may stay blank until later. Saved tokens arrive redacted; sending one back keeps it. |
| 80 | */ |
| 81 | export function planSimlockDeviceHostSave( |
| 82 | environments: ReadonlyArray<SimlockHostEnvironment>, |
| 83 | draft: SimlockDeviceHostConfigType, |
| 84 | tokens: ReadonlyMap<EnvironmentId, SimlockTokenEntry>, |
| 85 | resolve: MessageResolver = m, |
| 86 | ): |
| 87 | | { readonly problem: string } |
| 88 | | { |
| 89 | readonly patches: ReadonlyArray<{ |
| 90 | environmentId: EnvironmentId; |
| 91 | hosts: ReadonlyArray<SimlockDeviceHostConfigType>; |
| 92 | }>; |
| 93 | } { |
| 94 | const shared = simlockDeviceHostProblem(draft, resolve); |
| 95 | if (shared) return { problem: shared }; |
| 96 | const patches = []; |
| 97 | for (const environment of environments) { |
| 98 | const saved = environment.hosts.find((host) => host.id === draft.id)?.agentToken ?? ""; |
| 99 | const entry = tokens.get(environment.environmentId) ?? { kind: "cleared" }; |
| 100 | const agentToken = |
| 101 | entry.kind === "saved" |
| 102 | ? saved |
| 103 | : entry.kind !== "new" |
| 104 | ? "" |
| 105 | : typeof entry.value === "string" |
| 106 | ? entry.value.trim() |
| 107 | : entry.value; |
| 108 | patches.push({ |
| 109 | environmentId: environment.environmentId, |
| 110 | hosts: updateSimlockDeviceHosts(environment.hosts, { ...draft, agentToken }, false), |
| 111 | }); |
| 112 | } |
| 113 | return { patches }; |
| 114 | } |