The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { type DeviceToolVersions, WS_METHODS } from "@t3tools/contracts"; |
| 2 | import { Atom } from "effect/reactivity"; |
| 3 | |
| 4 | import type { EnvironmentRegistry } from "../connection/registry.ts"; |
| 5 | import { |
| 6 | createAtomCommandScheduler, |
| 7 | createEnvironmentRpcCommand, |
| 8 | createEnvironmentRpcSubscriptionAtomFamily, |
| 9 | } from "./runtime.ts"; |
| 10 | import { mFallback, msg } from "gt-i18n"; |
| 11 | import type { MFunctionType } from "gt-i18n/types"; |
| 12 | |
| 13 | export function createDeviceEnvironmentAtoms<R, E>( |
| 14 | runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>, |
| 15 | ) { |
| 16 | const scheduler = createAtomCommandScheduler(); |
| 17 | const concurrency = { |
| 18 | mode: "serial" as const, |
| 19 | key: ({ environmentId }: { environmentId: string }) => environmentId, |
| 20 | }; |
| 21 | // Starting an Expo Simulator can take minutes; it must not hold up powering off |
| 22 | // another device or turning device support off, so each device opens in its |
| 23 | // own lane. Every Expo start opens the same catalog device, so the thread is |
| 24 | // part of the lane too. |
| 25 | const openConcurrency = { |
| 26 | mode: "serial" as const, |
| 27 | key: ({ |
| 28 | environmentId, |
| 29 | input, |
| 30 | }: { |
| 31 | environmentId: string; |
| 32 | input: { |
| 33 | readonly threadId: string; |
| 34 | readonly hostId?: string | undefined; |
| 35 | readonly deviceId: string; |
| 36 | }; |
| 37 | }) => |
| 38 | JSON.stringify([environmentId, "open", input.threadId, input.hostId ?? null, input.deviceId]), |
| 39 | }; |
| 40 | return { |
| 41 | state: createEnvironmentRpcSubscriptionAtomFamily(runtime, { |
| 42 | label: "environment-data:device:state", |
| 43 | tag: WS_METHODS.subscribeDeviceState, |
| 44 | }), |
| 45 | configure: createEnvironmentRpcCommand(runtime, { |
| 46 | label: "environment-data:device:configure", |
| 47 | tag: WS_METHODS.deviceConfigure, |
| 48 | scheduler, |
| 49 | concurrency, |
| 50 | }), |
| 51 | testHost: createEnvironmentRpcCommand(runtime, { |
| 52 | label: "environment-data:device:test-host", |
| 53 | tag: WS_METHODS.deviceTestHost, |
| 54 | }), |
| 55 | easAccount: createEnvironmentRpcCommand(runtime, { |
| 56 | label: "environment-data:device:eas-account", |
| 57 | tag: WS_METHODS.deviceEasAccount, |
| 58 | }), |
| 59 | list: createEnvironmentRpcCommand(runtime, { |
| 60 | label: "environment-data:device:list", |
| 61 | tag: WS_METHODS.deviceList, |
| 62 | scheduler, |
| 63 | concurrency, |
| 64 | }), |
| 65 | open: createEnvironmentRpcCommand(runtime, { |
| 66 | label: "environment-data:device:open", |
| 67 | tag: WS_METHODS.deviceOpen, |
| 68 | scheduler, |
| 69 | concurrency: openConcurrency, |
| 70 | }), |
| 71 | close: createEnvironmentRpcCommand(runtime, { |
| 72 | label: "environment-data:device:close", |
| 73 | tag: WS_METHODS.deviceClose, |
| 74 | scheduler, |
| 75 | concurrency, |
| 76 | }), |
| 77 | shutdown: createEnvironmentRpcCommand(runtime, { |
| 78 | label: "environment-data:device:shutdown", |
| 79 | tag: WS_METHODS.deviceShutdown, |
| 80 | scheduler, |
| 81 | concurrency, |
| 82 | }), |
| 83 | detail: createEnvironmentRpcCommand(runtime, { |
| 84 | label: "environment-data:device:detail", |
| 85 | tag: WS_METHODS.deviceDetail, |
| 86 | scheduler, |
| 87 | concurrency, |
| 88 | }), |
| 89 | action: createEnvironmentRpcCommand(runtime, { |
| 90 | label: "environment-data:device:action", |
| 91 | tag: WS_METHODS.deviceAction, |
| 92 | scheduler, |
| 93 | concurrency, |
| 94 | }), |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | export function deviceToolVersionLabels( |
| 99 | tools: DeviceToolVersions | undefined, |
| 100 | resolve: MFunctionType = mFallback, |
| 101 | ) { |
| 102 | if (!tools) return [resolve(msg("Device tool versions have not been checked."))]; |
| 103 | return ( |
| 104 | [ |
| 105 | [resolve(msg("Device hub")), tools.hub], |
| 106 | [resolve(msg("Agent tools")), tools.agent], |
| 107 | ] as const |
| 108 | ).map(([name, tool]) => { |
| 109 | const installed = tool.installedVersions.length |
| 110 | ? tool.installedVersions.join(", ") |
| 111 | : resolve(msg("none", { $context: "no installed versions" })); |
| 112 | return tool.runningVersion |
| 113 | ? resolve( |
| 114 | msg("{name}: installed {installed}; required {required}; running {running}.", { |
| 115 | name, |
| 116 | installed, |
| 117 | required: tool.requiredVersion, |
| 118 | running: tool.runningVersion, |
| 119 | }), |
| 120 | ) |
| 121 | : resolve( |
| 122 | msg("{name}: installed {installed}; required {required}.", { |
| 123 | name, |
| 124 | installed, |
| 125 | required: tool.requiredVersion, |
| 126 | }), |
| 127 | ); |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | export function deviceToolUpdatePolicy( |
| 132 | tools: DeviceToolVersions | undefined, |
| 133 | resolve: MFunctionType = mFallback, |
| 134 | ) { |
| 135 | if (!tools) |
| 136 | return resolve(msg("Versions have not been checked. Reconnect the host and check versions.")); |
| 137 | const outdated = [tools.hub, tools.agent].filter( |
| 138 | (tool) => |
| 139 | tool.installedVersions.length > 0 && !tool.installedVersions.includes(tool.requiredVersion), |
| 140 | ); |
| 141 | return outdated.length > 0 |
| 142 | ? resolve( |
| 143 | msg( |
| 144 | "Update pending. Required tools will install automatically when next used. The host needs network access; an older install is not used as a fallback.", |
| 145 | ), |
| 146 | ) |
| 147 | : resolve( |
| 148 | msg( |
| 149 | "Required tools are installed automatically when needed. Checking versions does not install or start anything.", |
| 150 | ), |
| 151 | ); |
| 152 | } |
| 153 | export const deviceToolUpdateOwnership = msg( |
| 154 | "This environment's T3 server chooses device tool versions for itself and its SSH hosts. Update that server to receive newer tool versions; updating only your browser or mobile app does not update a remote server.", |
| 155 | ); |