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, 03:37 UTC
| 1 | import { DESKTOP_UPDATE_RESTART_MARKER_FILE } from "@t3tools/contracts"; |
| 2 | import * as Clock from "effect/Clock"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | |
| 9 | import * as ServerConfig from "../config.ts"; |
| 10 | import { |
| 11 | SERVICE_STATE_FILE, |
| 12 | SERVICE_STOP_MARKER_FILE, |
| 13 | serviceStateHasPendingUpdate, |
| 14 | } from "./serviceProtocol.ts"; |
| 15 | |
| 16 | const DESKTOP_UPDATE_RESTART_MARKER_TTL = Duration.minutes(1); |
| 17 | |
| 18 | export const pendingServiceUpdateExists = Effect.gen(function* () { |
| 19 | const config = yield* ServerConfig.ServerConfig; |
| 20 | const fs = yield* FileSystem.FileSystem; |
| 21 | const path = yield* Path.Path; |
| 22 | const runtimeDir = path.join(config.baseDir, "runtime"); |
| 23 | const stateText = yield* fs |
| 24 | .readFileString(path.join(runtimeDir, SERVICE_STATE_FILE)) |
| 25 | .pipe(Effect.option); |
| 26 | return Option.isSome(stateText) && serviceStateHasPendingUpdate(stateText.value); |
| 27 | }); |
| 28 | |
| 29 | export const pendingUpdateHandoffExists = Effect.gen(function* () { |
| 30 | if (!(yield* pendingServiceUpdateExists)) { |
| 31 | return false; |
| 32 | } |
| 33 | const config = yield* ServerConfig.ServerConfig; |
| 34 | const fs = yield* FileSystem.FileSystem; |
| 35 | const path = yield* Path.Path; |
| 36 | const runtimeDir = path.join(config.baseDir, "runtime"); |
| 37 | const stopping = yield* fs |
| 38 | .exists(path.join(runtimeDir, SERVICE_STOP_MARKER_FILE)) |
| 39 | .pipe(Effect.orElseSucceed(() => false)); |
| 40 | return !stopping; |
| 41 | }); |
| 42 | |
| 43 | // Reading consumes it, so shutdown checks it first. |
| 44 | export const desktopUpdateRestartPending = Effect.gen(function* () { |
| 45 | const config = yield* ServerConfig.ServerConfig; |
| 46 | const fs = yield* FileSystem.FileSystem; |
| 47 | const path = yield* Path.Path; |
| 48 | const markerPath = path.join(config.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE); |
| 49 | const marker = yield* fs.stat(markerPath).pipe(Effect.option); |
| 50 | if (Option.isNone(marker)) { |
| 51 | return false; |
| 52 | } |
| 53 | yield* fs.remove(markerPath).pipe(Effect.ignore); |
| 54 | const now = yield* Clock.currentTimeMillis; |
| 55 | return Option.match(marker.value.mtime, { |
| 56 | onNone: () => false, |
| 57 | onSome: (writtenAt) => |
| 58 | now - writtenAt.getTime() < Duration.toMillis(DESKTOP_UPDATE_RESTART_MARKER_TTL), |
| 59 | }); |
| 60 | }); |