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 | ServerSelfUpdateError, |
| 3 | type DesktopUpdateState, |
| 4 | type DesktopUpdateStatusReport, |
| 5 | type ServerSelfUpdateProgressStage, |
| 6 | type ServerSelfUpdateResult, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import * as Context from "effect/Context"; |
| 9 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 10 | import * as Duration from "effect/Duration"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Layer from "effect/Layer"; |
| 13 | import * as Option from "effect/Option"; |
| 14 | import * as Ref from "effect/Ref"; |
| 15 | import * as Result from "effect/Result"; |
| 16 | import * as Stream from "effect/Stream"; |
| 17 | |
| 18 | import * as ServerConfig from "../config.ts"; |
| 19 | import * as DesktopTelemetryReceiver from "../resourceTelemetry/DesktopTelemetryReceiver.ts"; |
| 20 | |
| 21 | const DESKTOP_UPDATE_TIMEOUT = Duration.minutes(20); |
| 22 | const DESKTOP_INSTALL_TIMEOUT = Duration.minutes(2); |
| 23 | |
| 24 | function desktopUpdateProgressStage( |
| 25 | state: DesktopUpdateState, |
| 26 | ): ServerSelfUpdateProgressStage | null { |
| 27 | switch (state.status) { |
| 28 | case "checking": |
| 29 | case "available": |
| 30 | case "downloading": |
| 31 | return "downloading"; |
| 32 | case "downloaded": |
| 33 | return "installing"; |
| 34 | default: |
| 35 | return null; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export class DesktopAppUpdate extends Context.Service< |
| 40 | DesktopAppUpdate, |
| 41 | { |
| 42 | readonly available: boolean; |
| 43 | readonly run: ( |
| 44 | reportProgress: ( |
| 45 | stage: ServerSelfUpdateProgressStage, |
| 46 | ) => Effect.Effect<void, ServerSelfUpdateError>, |
| 47 | trackSwitch?: DesktopTelemetryReceiver.DesktopTrackSwitch, |
| 48 | ) => Effect.Effect<ServerSelfUpdateResult, ServerSelfUpdateError>; |
| 49 | /** Success stops the server; returns only on installation failure or timeout. */ |
| 50 | readonly commit: ( |
| 51 | requestId: string, |
| 52 | onHandoffAccepted?: () => Effect.Effect<void>, |
| 53 | ) => Effect.Effect<never, ServerSelfUpdateError>; |
| 54 | } |
| 55 | >()("t3/desktopUpdate/DesktopAppUpdate") {} |
| 56 | |
| 57 | export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* () { |
| 58 | const config = yield* ServerConfig.ServerConfig; |
| 59 | const random = yield* SecureRandom.SecureRandom; |
| 60 | const receiver = yield* DesktopTelemetryReceiver.DesktopTelemetryReceiver; |
| 61 | const inFlight = yield* Ref.make(false); |
| 62 | |
| 63 | const available = config.mode === "desktop" && config.desktopTelemetryControlFd !== undefined; |
| 64 | const failWith = (reason: string, cause?: unknown) => |
| 65 | cause === undefined |
| 66 | ? new ServerSelfUpdateError({ reason }) |
| 67 | : new ServerSelfUpdateError({ reason, cause }); |
| 68 | |
| 69 | const consumeReports = Effect.fn("DesktopAppUpdate.consumeReports")(function* ( |
| 70 | requestId: string, |
| 71 | changes: Stream.Stream<DesktopUpdateStatusReport>, |
| 72 | reportProgress: ( |
| 73 | stage: ServerSelfUpdateProgressStage, |
| 74 | ) => Effect.Effect<void, ServerSelfUpdateError>, |
| 75 | trackSwitch: boolean, |
| 76 | ) { |
| 77 | const lastStage = yield* Ref.make<ServerSelfUpdateProgressStage | null>(null); |
| 78 | const emitStage = ( |
| 79 | stage: ServerSelfUpdateProgressStage | null, |
| 80 | ): Effect.Effect<void, ServerSelfUpdateError> => |
| 81 | stage === null |
| 82 | ? Effect.void |
| 83 | : Ref.get(lastStage).pipe( |
| 84 | Effect.flatMap((previous) => |
| 85 | previous === stage |
| 86 | ? Effect.void |
| 87 | : Ref.set(lastStage, stage).pipe(Effect.andThen(reportProgress(stage))), |
| 88 | ), |
| 89 | ); |
| 90 | |
| 91 | const terminal = yield* changes.pipe( |
| 92 | Stream.filter((report) => report.requestId === requestId), |
| 93 | Stream.mapEffect( |
| 94 | (report): Effect.Effect<Option.Option<DesktopUpdateStatusReport>, ServerSelfUpdateError> => |
| 95 | report.outcome === undefined |
| 96 | ? emitStage(desktopUpdateProgressStage(report.state)).pipe( |
| 97 | Effect.as(Option.none<DesktopUpdateStatusReport>()), |
| 98 | ) |
| 99 | : Effect.succeedSome(report), |
| 100 | ), |
| 101 | Stream.filterMap( |
| 102 | Option.match({ |
| 103 | onNone: () => Result.failVoid, |
| 104 | onSome: Result.succeed, |
| 105 | }), |
| 106 | ), |
| 107 | Stream.runHead, |
| 108 | ); |
| 109 | if (Option.isNone(terminal)) { |
| 110 | return yield* failWith("The desktop app stopped reporting its update."); |
| 111 | } |
| 112 | |
| 113 | const report = terminal.value; |
| 114 | if (report.outcome === "ready-to-install") { |
| 115 | yield* emitStage("installing"); |
| 116 | const targetVersion = |
| 117 | report.state.downloadedVersion ?? |
| 118 | report.state.availableVersion ?? |
| 119 | report.state.currentVersion; |
| 120 | yield* Effect.logInfo("Desktop app update prepared for install.", { |
| 121 | targetVersion, |
| 122 | }); |
| 123 | yield* Ref.set(inFlight, false); |
| 124 | return { |
| 125 | targetVersion, |
| 126 | method: "desktop-app" as const, |
| 127 | desktopUpdateToken: report.preparedRequestId ?? requestId, |
| 128 | }; |
| 129 | } |
| 130 | if (report.outcome === "up-to-date") { |
| 131 | // A switch's host already on the newest build of its track only saved the track. |
| 132 | if (trackSwitch) { |
| 133 | yield* Ref.set(inFlight, false); |
| 134 | return { targetVersion: report.state.currentVersion, method: "desktop-app" as const }; |
| 135 | } |
| 136 | return yield* failWith( |
| 137 | `The T3 Code desktop app on this machine is already up to date on ${report.state.currentVersion}.`, |
| 138 | ); |
| 139 | } |
| 140 | const reason = report.reason ?? report.state.message ?? "The desktop app update failed."; |
| 141 | const mismatch = report.state.trackMismatch; |
| 142 | if (mismatch !== undefined && reason === report.state.message) { |
| 143 | return yield* new ServerSelfUpdateError({ |
| 144 | reason, |
| 145 | trackMismatch: { |
| 146 | buildChannel: mismatch.buildChannel, |
| 147 | savedChannel: report.state.channel, |
| 148 | }, |
| 149 | }); |
| 150 | } |
| 151 | return yield* failWith(reason); |
| 152 | }); |
| 153 | |
| 154 | const run: DesktopAppUpdate["Service"]["run"] = Effect.fn("desktopUpdate.desktopAppUpdate.run")( |
| 155 | function* (reportProgress, trackSwitch) { |
| 156 | if (!available) { |
| 157 | return yield* failWith( |
| 158 | "This server was not started by the T3 Code desktop app, so it cannot drive a desktop update.", |
| 159 | ); |
| 160 | } |
| 161 | if (yield* Ref.getAndSet(inFlight, true)) { |
| 162 | return yield* failWith("A desktop app update is already in progress."); |
| 163 | } |
| 164 | |
| 165 | return yield* Effect.scoped( |
| 166 | Effect.gen(function* () { |
| 167 | const requestId = yield* random |
| 168 | .uuidV4() |
| 169 | .pipe( |
| 170 | Effect.mapError((error) => |
| 171 | failWith("Could not generate a desktop update request id.", error), |
| 172 | ), |
| 173 | ); |
| 174 | // Subscribe before requesting to catch fast reports. |
| 175 | const { changes } = yield* receiver.desktopUpdates; |
| 176 | yield* receiver |
| 177 | .requestDesktopUpdate(requestId, trackSwitch) |
| 178 | .pipe( |
| 179 | Effect.mapError((error) => |
| 180 | failWith("Could not reach the T3 Code desktop app on this machine.", error), |
| 181 | ), |
| 182 | ); |
| 183 | return yield* consumeReports( |
| 184 | requestId, |
| 185 | changes, |
| 186 | reportProgress, |
| 187 | trackSwitch !== undefined, |
| 188 | ).pipe( |
| 189 | Effect.onInterrupt(() => receiver.cancelDesktopUpdate(requestId).pipe(Effect.ignore)), |
| 190 | ); |
| 191 | }), |
| 192 | ).pipe( |
| 193 | Effect.timeout(DESKTOP_UPDATE_TIMEOUT), |
| 194 | Effect.catchTags({ |
| 195 | TimeoutError: () => failWith("The desktop app did not finish the update in time."), |
| 196 | }), |
| 197 | Effect.onError(() => Ref.set(inFlight, false)), |
| 198 | ); |
| 199 | }, |
| 200 | ); |
| 201 | |
| 202 | const commit: DesktopAppUpdate["Service"]["commit"] = Effect.fn( |
| 203 | "desktopUpdate.desktopAppUpdate.commit", |
| 204 | )(function* (requestId, onHandoffAccepted = () => Effect.void) { |
| 205 | if (!available) { |
| 206 | return yield* failWith("This server cannot commit a desktop app update."); |
| 207 | } |
| 208 | const terminal = yield* Effect.scoped( |
| 209 | Effect.gen(function* () { |
| 210 | const { latest, changes } = yield* receiver.desktopUpdates; |
| 211 | const reports = Option.match(latest, { |
| 212 | onNone: () => changes, |
| 213 | onSome: (report) => Stream.concat(Stream.make(report), changes), |
| 214 | }); |
| 215 | yield* Effect.uninterruptible( |
| 216 | receiver.commitDesktopUpdate(requestId).pipe( |
| 217 | Effect.mapError((error) => failWith("Could not reach the T3 Code desktop app.", error)), |
| 218 | Effect.tap(() => onHandoffAccepted()), |
| 219 | ), |
| 220 | ); |
| 221 | return yield* reports.pipe( |
| 222 | Stream.filter((report) => report.requestId === requestId && report.outcome === "failed"), |
| 223 | Stream.runHead, |
| 224 | Effect.timeout(DESKTOP_INSTALL_TIMEOUT), |
| 225 | Effect.catchTags({ |
| 226 | TimeoutError: () => |
| 227 | failWith("The desktop app did not report an install result in time."), |
| 228 | }), |
| 229 | ); |
| 230 | }), |
| 231 | ); |
| 232 | if (Option.isNone(terminal)) { |
| 233 | return yield* failWith("The desktop app stopped reporting the install."); |
| 234 | } |
| 235 | return yield* failWith( |
| 236 | terminal.value.reason ?? |
| 237 | terminal.value.state.message ?? |
| 238 | "The desktop app failed to install the update.", |
| 239 | ); |
| 240 | }); |
| 241 | |
| 242 | return DesktopAppUpdate.of({ available, run, commit }); |
| 243 | }); |
| 244 | |
| 245 | export const layer = Layer.effect(DesktopAppUpdate, make()); |