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 { msg, useMessages } from "gt-react"; |
| 2 | import { useAtomValue } from "@effect/atom-react"; |
| 3 | import { PROVIDER_DISPLAY_NAMES } from "@t3tools/contracts"; |
| 4 | |
| 5 | import { APP_VERSION } from "~/branding"; |
| 6 | import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal"; |
| 7 | import { isElectron } from "~/env"; |
| 8 | import { useEnvironments } from "~/state/environments"; |
| 9 | import { environmentIdsKey } from "~/state/environmentAtomList"; |
| 10 | import { environmentUpdateStatesAtom, serverEnvironment } from "~/state/server"; |
| 11 | import { environmentSessionStatesAtom } from "~/state/session"; |
| 12 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 13 | import { getProviderUpdateRunOutcome } from "../ProviderUpdateLaunchNotification.logic"; |
| 14 | import { useProviderUpdates } from "../ProviderUpdatesAction"; |
| 15 | import { canUpdateServer, type ServerUpdateOutcome, useServerUpdate } from "../ServerUpdateAction"; |
| 16 | import { |
| 17 | type ChannelSwitchRelease, |
| 18 | closeFinishedUpdateRun, |
| 19 | collectChannelSwitchTargets, |
| 20 | collectRestartingMachines, |
| 21 | collectServerUpdateTargets, |
| 22 | collectUpdateEverythingResult, |
| 23 | describeUpdateEverything, |
| 24 | runUpdateEverything, |
| 25 | type UpdateRunMachine, |
| 26 | } from "./updateEverything.logic"; |
| 27 | import { |
| 28 | clearSavedUpdateEverythingResult, |
| 29 | saveUpdateEverythingResult, |
| 30 | setUpdateEverythingRun, |
| 31 | settleUpdateRunMachine, |
| 32 | showUpdateEverythingResult, |
| 33 | waitForServerUpdateHandoff, |
| 34 | waitForUpdateRestart, |
| 35 | } from "./updateEverythingRun"; |
| 36 | |
| 37 | const UPDATE_RESTART_DELAY_MS = 5_000; |
| 38 | |
| 39 | /** Servers this desktop app runs; they update with it. */ |
| 40 | export function isHostedByThisApp( |
| 41 | environment: ReturnType<typeof useEnvironments>["environments"][number], |
| 42 | ) { |
| 43 | return ( |
| 44 | isElectron && |
| 45 | (environment.entry.target._tag === "PrimaryConnectionTarget" || |
| 46 | isDesktopLocalConnectionTarget(environment.entry.target)) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | export type ReportLocalUpdateError = (title: string, description: string) => void; |
| 51 | |
| 52 | /** `channelSwitch` makes the run move the fleet to that release's track instead of updating it. */ |
| 53 | export function useUpdateEverything( |
| 54 | localUpdateVersion: string | null, |
| 55 | channelSwitch?: ChannelSwitchRelease, |
| 56 | ) { |
| 57 | const targetVersion = localUpdateVersion ?? APP_VERSION; |
| 58 | const { environments } = useEnvironments(); |
| 59 | const resolve = useMessages(); |
| 60 | const environmentIds = environmentIdsKey(environments.map(({ environmentId }) => environmentId)); |
| 61 | const sessionStates = useAtomValue(environmentSessionStatesAtom(environmentIds)); |
| 62 | const updateStates = useAtomValue(environmentUpdateStatesAtom(environmentIds)); |
| 63 | const machines = environments.map((environment, index) => ({ |
| 64 | environmentId: environment.environmentId, |
| 65 | label: environment.label, |
| 66 | serverConfig: environment.serverConfig, |
| 67 | connected: environment.connection.phase === "connected", |
| 68 | hostedByThisApp: isHostedByThisApp(environment), |
| 69 | canMaintain: canUpdateServer(sessionStates[index]!), |
| 70 | updateState: updateStates[index]!, |
| 71 | })); |
| 72 | const { targets: servers, skipped } = channelSwitch |
| 73 | ? collectChannelSwitchTargets(machines, channelSwitch, resolve) |
| 74 | : collectServerUpdateTargets(machines, targetVersion, resolve); |
| 75 | // Track-only hosts save a setting and restart nothing, so they are neither listed nor counted. |
| 76 | const moving = servers.filter((server) => server.trackOnly !== true); |
| 77 | const restarts = collectRestartingMachines(machines, moving, localUpdateVersion !== null); |
| 78 | const providerUpdates = useProviderUpdates(); |
| 79 | // A track switch moves app and servers only; provider updates stay a separate choice. |
| 80 | const providers = channelSwitch ? { machines: [], updateAll: async () => [] } : providerUpdates; |
| 81 | const updateServer = useServerUpdate(); |
| 82 | const stageServerUpdate = useAtomCommand(serverEnvironment.stageServerUpdate, { |
| 83 | reportFailure: false, |
| 84 | }); |
| 85 | |
| 86 | const view = describeUpdateEverything( |
| 87 | { |
| 88 | localVersion: localUpdateVersion, |
| 89 | localMachines: machines.filter((machine) => machine.hostedByThisApp), |
| 90 | servers: moving, |
| 91 | skipped, |
| 92 | providerMachines: providers.machines.map((machine) => ({ |
| 93 | environmentId: machine.environmentId, |
| 94 | label: machine.label, |
| 95 | providers: machine.candidates.map( |
| 96 | (candidate) => PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver, |
| 97 | ), |
| 98 | })), |
| 99 | }, |
| 100 | resolve, |
| 101 | ); |
| 102 | |
| 103 | return { |
| 104 | hasUpdates: servers.length > 0 || providers.machines.length > 0, |
| 105 | ...view, |
| 106 | restarts, |
| 107 | run: async ({ |
| 108 | downloadLocal, |
| 109 | installLocal, |
| 110 | }: { |
| 111 | downloadLocal?: (reportError: ReportLocalUpdateError) => Promise<boolean>; |
| 112 | installLocal?: (reportError: ReportLocalUpdateError) => Promise<boolean>; |
| 113 | }) => { |
| 114 | const localFailures: Array<string> = []; |
| 115 | const reportLocalError: ReportLocalUpdateError = (title, description) => { |
| 116 | localFailures.push(`${title}: ${description}`); |
| 117 | }; |
| 118 | const startedAt = Date.now(); |
| 119 | const machinesById = new Map<UpdateRunMachine["environmentId"], UpdateRunMachine>(); |
| 120 | for (const machine of providers.machines) { |
| 121 | machinesById.set(machine.environmentId, { |
| 122 | environmentId: machine.environmentId, |
| 123 | label: machine.label, |
| 124 | updatesProviders: true, |
| 125 | server: null, |
| 126 | handedOffVersion: null, |
| 127 | startedAt, |
| 128 | result: null, |
| 129 | failure: null, |
| 130 | providerFailure: null, |
| 131 | }); |
| 132 | } |
| 133 | for (const server of servers) { |
| 134 | const existing = machinesById.get(server.environmentId); |
| 135 | machinesById.set(server.environmentId, { |
| 136 | environmentId: server.environmentId, |
| 137 | label: server.serverLabel, |
| 138 | updatesProviders: existing?.updatesProviders ?? false, |
| 139 | server, |
| 140 | handedOffVersion: null, |
| 141 | startedAt, |
| 142 | result: null, |
| 143 | failure: null, |
| 144 | providerFailure: null, |
| 145 | }); |
| 146 | } |
| 147 | const localVersion = installLocal ? localUpdateVersion : null; |
| 148 | setUpdateEverythingRun(() => ({ |
| 149 | ...(channelSwitch ? { trackSwitch: true } : {}), |
| 150 | localVersion, |
| 151 | phase: "providers", |
| 152 | restartAt: null, |
| 153 | machines: Array.from(machinesById.values()), |
| 154 | })); |
| 155 | const providerFailures = new Map<UpdateRunMachine["environmentId"], string>(); |
| 156 | const finishedServers: Array<{ label: string; outcome: ServerUpdateOutcome }> = []; |
| 157 | try { |
| 158 | const outcome = await runUpdateEverything({ |
| 159 | ...(downloadLocal ? { downloadLocal: () => downloadLocal(reportLocalError) } : {}), |
| 160 | updateProviders: async () => { |
| 161 | const runs = await providers.updateAll({ quiet: true }); |
| 162 | const outcomes = runs.map((run) => { |
| 163 | const runOutcome = getProviderUpdateRunOutcome(run); |
| 164 | if (runOutcome.status !== "updated") { |
| 165 | providerFailures.set( |
| 166 | run.environmentId, |
| 167 | runOutcome.status === "failed" |
| 168 | ? runOutcome.line |
| 169 | : resolve(msg("Provider update interrupted")), |
| 170 | ); |
| 171 | } |
| 172 | return runOutcome; |
| 173 | }); |
| 174 | for (const machine of machinesById.values()) { |
| 175 | if (machine.server === null) { |
| 176 | settleUpdateRunMachine( |
| 177 | machine.environmentId, |
| 178 | providerFailures.get(machine.environmentId) ?? null, |
| 179 | ); |
| 180 | } |
| 181 | } |
| 182 | const serversStartedAt = Date.now(); |
| 183 | setUpdateEverythingRun( |
| 184 | (run) => |
| 185 | run && { |
| 186 | ...run, |
| 187 | phase: "servers", |
| 188 | machines: run.machines.map((machine) => |
| 189 | machine.server === null ? machine : { ...machine, startedAt: serversStartedAt }, |
| 190 | ), |
| 191 | }, |
| 192 | ); |
| 193 | return outcomes; |
| 194 | }, |
| 195 | ...(providers.machines.length > 0 |
| 196 | ? { |
| 197 | stageServers: () => |
| 198 | new Map( |
| 199 | servers |
| 200 | .filter( |
| 201 | (server) => |
| 202 | machines.find((machine) => machine.environmentId === server.environmentId) |
| 203 | ?.serverConfig?.environment.capabilities.serverUpdateStaging === true, |
| 204 | ) |
| 205 | .map((server) => [ |
| 206 | server.environmentId, |
| 207 | stageServerUpdate({ |
| 208 | environmentId: server.environmentId, |
| 209 | input: { targetVersion: server.targetVersion, stageOnly: true }, |
| 210 | }), |
| 211 | ]), |
| 212 | ), |
| 213 | } |
| 214 | : {}), |
| 215 | updateServers: async (staged) => { |
| 216 | const runs = servers.map(async (server) => { |
| 217 | await staged.get(server.environmentId); |
| 218 | const serverOutcome = await updateServer(server, { quiet: true }); |
| 219 | settleUpdateRunMachine( |
| 220 | server.environmentId, |
| 221 | serverOutcome.status === "failed" |
| 222 | ? serverOutcome.message |
| 223 | : (providerFailures.get(server.environmentId) ?? null), |
| 224 | { |
| 225 | serverUpdated: serverOutcome.status !== "failed", |
| 226 | providerFailure: providerFailures.get(server.environmentId) ?? null, |
| 227 | }, |
| 228 | ); |
| 229 | const finished = { label: server.serverLabel, outcome: serverOutcome }; |
| 230 | finishedServers.push(finished); |
| 231 | return finished; |
| 232 | }); |
| 233 | await Promise.all( |
| 234 | servers.map((server, index) => |
| 235 | waitForServerUpdateHandoff(server.environmentId, runs[index]!), |
| 236 | ), |
| 237 | ); |
| 238 | return runs; |
| 239 | }, |
| 240 | }); |
| 241 | const result = (finished: ReadonlyArray<{ label: string; outcome: ServerUpdateOutcome }>) => |
| 242 | collectUpdateEverythingResult({ |
| 243 | localVersion: null, |
| 244 | localFailures, |
| 245 | servers: finished, |
| 246 | providers: outcome.providers, |
| 247 | }); |
| 248 | if (outcome.localReady && installLocal) { |
| 249 | if (await waitForUpdateRestart(UPDATE_RESTART_DELAY_MS)) { |
| 250 | setUpdateEverythingRun( |
| 251 | (run) => run && { ...run, phase: "installing", restartAt: null }, |
| 252 | ); |
| 253 | saveUpdateEverythingResult({ ...result(finishedServers), localVersion }); |
| 254 | if (await installLocal(reportLocalError)) return; |
| 255 | clearSavedUpdateEverythingResult(); |
| 256 | } |
| 257 | } |
| 258 | setUpdateEverythingRun( |
| 259 | (run) => |
| 260 | run && |
| 261 | closeFinishedUpdateRun({ |
| 262 | ...run, |
| 263 | phase: "servers", |
| 264 | restartAt: null, |
| 265 | localVersion: null, |
| 266 | }), |
| 267 | ); |
| 268 | showUpdateEverythingResult(result(await Promise.all(outcome.servers))); |
| 269 | } catch (error) { |
| 270 | setUpdateEverythingRun(() => null); |
| 271 | throw error; |
| 272 | } |
| 273 | }, |
| 274 | }; |
| 275 | } |