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 { useAtomValue } from "@effect/atom-react"; |
| 2 | import { createDeviceEnvironmentAtoms } from "@t3tools/client-runtime/state/device"; |
| 3 | import { |
| 4 | type DeviceHubAccess, |
| 5 | resolveDeviceHubAccess, |
| 6 | } from "@t3tools/client-runtime/state/deviceHubAccess"; |
| 7 | import { createEnvironmentRpcQueryAtomFamily } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import { |
| 9 | type DeviceDetail, |
| 10 | type DeviceServiceState, |
| 11 | type EnvironmentId, |
| 12 | WS_METHODS, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import * as Effect from "effect/Effect"; |
| 15 | import { useMessages } from "gt-react"; |
| 16 | import * as Option from "effect/Option"; |
| 17 | import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; |
| 18 | |
| 19 | import { connectionAtomRuntime } from "../connection/runtime"; |
| 20 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 21 | import { environmentSession } from "./session"; |
| 22 | import { formatEnvironmentQueryError, useEnvironmentQuery } from "./query"; |
| 23 | |
| 24 | export const deviceEnvironment = createDeviceEnvironmentAtoms(connectionAtomRuntime); |
| 25 | |
| 26 | const EMPTY_DEVICE_STATE: DeviceServiceState = { |
| 27 | hosts: [], |
| 28 | hostStatus: "disabled", |
| 29 | hostStatuses: {}, |
| 30 | devices: [], |
| 31 | sessions: [], |
| 32 | onboardingCompleted: false, |
| 33 | agentAccessEnabled: false, |
| 34 | hubBasePath: "/api/device-hub", |
| 35 | revision: 0, |
| 36 | }; |
| 37 | |
| 38 | export function useDeviceState(environmentId: EnvironmentId | null): { |
| 39 | readonly state: DeviceServiceState; |
| 40 | readonly loaded: boolean; |
| 41 | readonly error: string | null; |
| 42 | readonly refresh: () => void; |
| 43 | } { |
| 44 | const resolve = useMessages(); |
| 45 | const query = useEnvironmentQuery( |
| 46 | environmentId === null ? null : deviceEnvironment.state({ environmentId, input: {} }), |
| 47 | resolve, |
| 48 | ); |
| 49 | return { |
| 50 | state: query.data ?? EMPTY_DEVICE_STATE, |
| 51 | loaded: query.data !== null, |
| 52 | error: query.error, |
| 53 | refresh: query.refresh, |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | const deviceHubAccessAtom = Atom.family((environmentId: EnvironmentId) => |
| 58 | connectionAtomRuntime |
| 59 | .atom((get) => { |
| 60 | const prepared = Option.getOrNull( |
| 61 | get(environmentSession.preparedConnectionValueAtom(environmentId)), |
| 62 | ); |
| 63 | if (prepared === null) return Effect.never; |
| 64 | return resolveDeviceHubAccess({ prepared, hubBasePath: EMPTY_DEVICE_STATE.hubBasePath }); |
| 65 | }) |
| 66 | .pipe(Atom.setIdleTTL(60_000), Atom.withLabel(`device-hub-access:${environmentId}`)), |
| 67 | ); |
| 68 | |
| 69 | export function useDeviceHubAccess( |
| 70 | environmentId: EnvironmentId | null, |
| 71 | hostId = "local", |
| 72 | ): DeviceHubAccess | null { |
| 73 | const result = useAtomValue( |
| 74 | environmentId === null ? EMPTY_ACCESS_ATOM : deviceHubAccessAtom(environmentId), |
| 75 | ); |
| 76 | return AsyncResult.isSuccess(result) |
| 77 | ? { ...result.value, query: { ...result.value.query, hostId } } |
| 78 | : null; |
| 79 | } |
| 80 | |
| 81 | const EMPTY_ACCESS_ATOM = Atom.make(AsyncResult.initial<DeviceHubAccess, never>()).pipe( |
| 82 | Atom.withLabel("device-hub-access:empty"), |
| 83 | ); |
| 84 | |
| 85 | export function refreshDeviceHubAccess(environmentId: EnvironmentId): void { |
| 86 | appAtomRegistry.refresh(deviceHubAccessAtom(environmentId)); |
| 87 | } |
| 88 | |
| 89 | /** Mints a fresh hub ticket for a one-off request that failed with 401. Resolves null on failure. */ |
| 90 | export function renewDeviceHubAccess( |
| 91 | environmentId: EnvironmentId, |
| 92 | hostId = "local", |
| 93 | ): Promise<DeviceHubAccess | null> { |
| 94 | const atom = deviceHubAccessAtom(environmentId); |
| 95 | appAtomRegistry.refresh(atom); |
| 96 | return Effect.runPromise( |
| 97 | AtomRegistry.getResult(appAtomRegistry, atom, { suspendOnWaiting: true }).pipe( |
| 98 | Effect.map((access) => ({ ...access, query: { ...access.query, hostId } })), |
| 99 | Effect.timeout("10 seconds"), |
| 100 | Effect.orElseSucceed(() => null), |
| 101 | ), |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | export interface DeviceDetailKey { |
| 106 | readonly environmentId: EnvironmentId; |
| 107 | readonly hostId: string; |
| 108 | readonly deviceId: string; |
| 109 | } |
| 110 | |
| 111 | export interface DeviceDetailView { |
| 112 | /** Settings the host last confirmed, by a read or by an action's answer. */ |
| 113 | readonly detail: DeviceDetail | null; |
| 114 | readonly error: string | null; |
| 115 | } |
| 116 | |
| 117 | export const deviceDetailAtom = Atom.family((key: DeviceDetailKey) => |
| 118 | Atom.make<DeviceDetailView>({ detail: null, error: null }).pipe( |
| 119 | Atom.withLabel(`device-detail:${key.hostId}:${key.deviceId}`), |
| 120 | ), |
| 121 | ); |
| 122 | |
| 123 | /** |
| 124 | * Reads a device's settings into its detail view while mounted. Reads and |
| 125 | * actions share one serial lane per environment, so a read never answers |
| 126 | * before an action that was sent ahead of it. |
| 127 | */ |
| 128 | export const deviceDetailReadAtom = Atom.family((key: DeviceDetailKey) => |
| 129 | Atom.make((get) => |
| 130 | deviceEnvironment.detail |
| 131 | .run(get.registry, { |
| 132 | environmentId: key.environmentId, |
| 133 | input: { hostId: key.hostId, deviceId: key.deviceId }, |
| 134 | }) |
| 135 | .pipe( |
| 136 | Effect.matchCause({ |
| 137 | onSuccess: (detail) => get.set(deviceDetailAtom(key), { detail, error: null }), |
| 138 | onFailure: (cause) => |
| 139 | get.set(deviceDetailAtom(key), { |
| 140 | detail: null, |
| 141 | error: formatEnvironmentQueryError(cause), |
| 142 | }), |
| 143 | }), |
| 144 | ), |
| 145 | ).pipe(Atom.withLabel(`device-detail-read:${key.hostId}:${key.deviceId}`)), |
| 146 | ); |
| 147 | |
| 148 | /** |
| 149 | * Probes the environment's device hosts while mounted. Remote hosts report |
| 150 | * their platforms only after a probe. Probing never installs or starts helpers. |
| 151 | */ |
| 152 | export const deviceHostProbeAtom = Atom.family((environmentId: EnvironmentId) => |
| 153 | Atom.make((get) => |
| 154 | deviceEnvironment.list.run(get.registry, { environmentId, input: {} }).pipe(Effect.ignore), |
| 155 | ).pipe(Atom.withLabel(`device-host-probe:${environmentId}`)), |
| 156 | ); |
| 157 | |
| 158 | export const NO_DEVICE_PROBE_ATOM = Atom.make(AsyncResult.initial<void, never>()).pipe( |
| 159 | Atom.withLabel("device-host-probe:none"), |
| 160 | ); |
| 161 | |
| 162 | /** |
| 163 | * The Expo account an Expo host editor shows as it opens, for the saved |
| 164 | * credential. Each open checks again; edits and manual checks use the command. |
| 165 | */ |
| 166 | export const deviceEasAccountCheck = createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, { |
| 167 | label: "environment-data:device:eas-account-check", |
| 168 | tag: WS_METHODS.deviceEasAccount, |
| 169 | staleTimeMs: 0, |
| 170 | idleTtlMs: 0, |
| 171 | }); |
| 172 | |
| 173 | /** Who a Simlock token is, checked as the host editor's URL and token fields change. */ |
| 174 | export const deviceSimlockIdentityCheck = createEnvironmentRpcQueryAtomFamily( |
| 175 | connectionAtomRuntime, |
| 176 | { |
| 177 | label: "environment-data:device:simlock-identity-check", |
| 178 | tag: WS_METHODS.deviceSimlockIdentity, |
| 179 | staleTimeMs: 0, |
| 180 | idleTtlMs: 0, |
| 181 | }, |
| 182 | ); |