The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | DesktopRuntimeInfo, |
| 3 | DesktopUpdateChannel, |
| 4 | DesktopUpdateReleaseNote, |
| 5 | DesktopUpdateState, |
| 6 | } from "@t3tools/contracts"; |
| 7 | |
| 8 | function nextStatusAfterDownloadFailure( |
| 9 | currentState: DesktopUpdateState, |
| 10 | ): DesktopUpdateState["status"] { |
| 11 | return currentState.availableVersion ? "available" : "error"; |
| 12 | } |
| 13 | |
| 14 | // A mismatch describes only the check that found it. |
| 15 | function withoutTrackMismatch({ |
| 16 | trackMismatch: _trackMismatch, |
| 17 | ...state |
| 18 | }: DesktopUpdateState): DesktopUpdateState { |
| 19 | return state; |
| 20 | } |
| 21 | |
| 22 | function getCanRetryAfterDownloadFailure(currentState: DesktopUpdateState): boolean { |
| 23 | return currentState.availableVersion !== null; |
| 24 | } |
| 25 | |
| 26 | export function createInitialDesktopUpdateState( |
| 27 | currentVersion: string, |
| 28 | runtimeInfo: DesktopRuntimeInfo, |
| 29 | channel: DesktopUpdateChannel, |
| 30 | ): DesktopUpdateState { |
| 31 | return { |
| 32 | enabled: false, |
| 33 | status: "disabled", |
| 34 | channel, |
| 35 | currentVersion, |
| 36 | hostArch: runtimeInfo.hostArch, |
| 37 | appArch: runtimeInfo.appArch, |
| 38 | runningUnderArm64Translation: runtimeInfo.runningUnderArm64Translation, |
| 39 | availableVersion: null, |
| 40 | downloadedVersion: null, |
| 41 | releaseNotes: [], |
| 42 | omittedReleaseCount: 0, |
| 43 | downloadPercent: null, |
| 44 | checkedAt: null, |
| 45 | message: null, |
| 46 | errorContext: null, |
| 47 | canRetry: false, |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | export function reduceDesktopUpdateStateOnCheckStart( |
| 52 | state: DesktopUpdateState, |
| 53 | checkedAt: string, |
| 54 | ): DesktopUpdateState { |
| 55 | const hasDownloadedUpdate = state.downloadedVersion !== null; |
| 56 | return { |
| 57 | ...withoutTrackMismatch(state), |
| 58 | status: "checking", |
| 59 | checkedAt, |
| 60 | releaseNotes: hasDownloadedUpdate ? state.releaseNotes : [], |
| 61 | omittedReleaseCount: hasDownloadedUpdate ? state.omittedReleaseCount : 0, |
| 62 | message: null, |
| 63 | downloadPercent: hasDownloadedUpdate ? 100 : null, |
| 64 | errorContext: null, |
| 65 | canRetry: false, |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | export function reduceDesktopUpdateStateOnCheckFailure( |
| 70 | state: DesktopUpdateState, |
| 71 | message: string, |
| 72 | checkedAt: string, |
| 73 | trackMismatch?: DesktopUpdateState["trackMismatch"], |
| 74 | ): DesktopUpdateState { |
| 75 | if (state.downloadedVersion !== null) { |
| 76 | return { |
| 77 | ...withoutTrackMismatch(state), |
| 78 | status: "downloaded", |
| 79 | message: null, |
| 80 | checkedAt, |
| 81 | downloadPercent: 100, |
| 82 | errorContext: null, |
| 83 | canRetry: true, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | return { |
| 88 | ...withoutTrackMismatch(state), |
| 89 | status: "error", |
| 90 | message, |
| 91 | checkedAt, |
| 92 | downloadPercent: null, |
| 93 | errorContext: "check", |
| 94 | canRetry: true, |
| 95 | ...(trackMismatch === undefined ? {} : { trackMismatch }), |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | export function reduceDesktopUpdateStateOnUpdateAvailable( |
| 100 | state: DesktopUpdateState, |
| 101 | version: string, |
| 102 | checkedAt: string, |
| 103 | releaseNotes: ReadonlyArray<DesktopUpdateReleaseNote> = [], |
| 104 | omittedReleaseCount = 0, |
| 105 | ): DesktopUpdateState { |
| 106 | const isDownloadedVersion = state.downloadedVersion === version; |
| 107 | const preserveReleaseNotes = isDownloadedVersion && releaseNotes.length === 0; |
| 108 | return { |
| 109 | ...withoutTrackMismatch(state), |
| 110 | status: isDownloadedVersion ? "downloaded" : "available", |
| 111 | availableVersion: version, |
| 112 | downloadedVersion: isDownloadedVersion ? version : null, |
| 113 | releaseNotes: preserveReleaseNotes ? state.releaseNotes : releaseNotes, |
| 114 | omittedReleaseCount: preserveReleaseNotes ? state.omittedReleaseCount : omittedReleaseCount, |
| 115 | downloadPercent: isDownloadedVersion ? 100 : null, |
| 116 | checkedAt, |
| 117 | message: null, |
| 118 | errorContext: null, |
| 119 | canRetry: isDownloadedVersion, |
| 120 | }; |
| 121 | } |
| 122 | |
| 123 | export function reduceDesktopUpdateStateOnNoUpdate( |
| 124 | state: DesktopUpdateState, |
| 125 | checkedAt: string, |
| 126 | ): DesktopUpdateState { |
| 127 | if (state.downloadedVersion !== null) { |
| 128 | return { |
| 129 | ...withoutTrackMismatch(state), |
| 130 | status: "downloaded", |
| 131 | availableVersion: state.downloadedVersion, |
| 132 | downloadPercent: 100, |
| 133 | checkedAt, |
| 134 | message: null, |
| 135 | errorContext: null, |
| 136 | canRetry: true, |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | return { |
| 141 | ...withoutTrackMismatch(state), |
| 142 | status: "up-to-date", |
| 143 | availableVersion: null, |
| 144 | downloadedVersion: null, |
| 145 | releaseNotes: [], |
| 146 | omittedReleaseCount: 0, |
| 147 | downloadPercent: null, |
| 148 | checkedAt, |
| 149 | message: null, |
| 150 | errorContext: null, |
| 151 | canRetry: false, |
| 152 | }; |
| 153 | } |
| 154 | |
| 155 | export function reduceDesktopUpdateStateOnDownloadStart( |
| 156 | state: DesktopUpdateState, |
| 157 | ): DesktopUpdateState { |
| 158 | return { |
| 159 | ...withoutTrackMismatch(state), |
| 160 | status: "downloading", |
| 161 | downloadPercent: 0, |
| 162 | message: null, |
| 163 | errorContext: null, |
| 164 | canRetry: false, |
| 165 | }; |
| 166 | } |
| 167 | |
| 168 | export function reduceDesktopUpdateStateOnDownloadFailure( |
| 169 | state: DesktopUpdateState, |
| 170 | message: string, |
| 171 | ): DesktopUpdateState { |
| 172 | return { |
| 173 | ...withoutTrackMismatch(state), |
| 174 | status: nextStatusAfterDownloadFailure(state), |
| 175 | message, |
| 176 | downloadPercent: null, |
| 177 | errorContext: "download", |
| 178 | canRetry: getCanRetryAfterDownloadFailure(state), |
| 179 | }; |
| 180 | } |
| 181 | |
| 182 | export function reduceDesktopUpdateStateOnDownloadProgress( |
| 183 | state: DesktopUpdateState, |
| 184 | percent: number, |
| 185 | ): DesktopUpdateState { |
| 186 | return { |
| 187 | ...withoutTrackMismatch(state), |
| 188 | status: "downloading", |
| 189 | downloadPercent: percent, |
| 190 | message: null, |
| 191 | errorContext: null, |
| 192 | canRetry: false, |
| 193 | }; |
| 194 | } |
| 195 | |
| 196 | export function reduceDesktopUpdateStateOnDownloadComplete( |
| 197 | state: DesktopUpdateState, |
| 198 | version: string, |
| 199 | ): DesktopUpdateState { |
| 200 | return { |
| 201 | ...withoutTrackMismatch(state), |
| 202 | status: "downloaded", |
| 203 | availableVersion: version, |
| 204 | downloadedVersion: version, |
| 205 | downloadPercent: 100, |
| 206 | message: null, |
| 207 | errorContext: null, |
| 208 | canRetry: true, |
| 209 | }; |
| 210 | } |
| 211 | |
| 212 | export function reduceDesktopUpdateStateOnInstallFailure( |
| 213 | state: DesktopUpdateState, |
| 214 | message: string, |
| 215 | ): DesktopUpdateState { |
| 216 | return { |
| 217 | ...withoutTrackMismatch(state), |
| 218 | status: "downloaded", |
| 219 | message, |
| 220 | errorContext: "install", |
| 221 | canRetry: true, |
| 222 | }; |
| 223 | } |
| 224 | |
| 225 | export interface DesktopUpdateInstallRecord { |
| 226 | readonly fromVersion: string; |
| 227 | readonly targetVersion: string; |
| 228 | } |
| 229 | |
| 230 | export function resolveNotAppliedInstallVersion( |
| 231 | record: DesktopUpdateInstallRecord, |
| 232 | appVersion: string, |
| 233 | ): string | null { |
| 234 | return record.fromVersion === appVersion && record.targetVersion !== appVersion |
| 235 | ? record.targetVersion |
| 236 | : null; |
| 237 | } |