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 type { |
| 2 | EnvironmentId, |
| 3 | ServerConfig, |
| 4 | ServerInstallation, |
| 5 | ServerSelfUpdateCapability, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import type { ServerUpdateState } from "@t3tools/client-runtime/state/server"; |
| 8 | import { compareSemverVersions, parseSemver } from "@t3tools/shared/semver"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import { msg } from "gt-react"; |
| 11 | |
| 12 | import { APP_VERSION } from "./branding"; |
| 13 | import { getLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage"; |
| 14 | import { m, type MessageResolver } from "./i18n/messages"; |
| 15 | |
| 16 | export interface VersionMismatch { |
| 17 | readonly clientVersion: string; |
| 18 | readonly serverVersion: string; |
| 19 | readonly hint: string; |
| 20 | } |
| 21 | |
| 22 | const VERSION_MISMATCH_DISMISSALS_STORAGE_KEY = "t3code:version-mismatch-dismissals:v1"; |
| 23 | |
| 24 | const dismissedServerUpdateFailures = new WeakSet<ServerUpdateState>(); |
| 25 | |
| 26 | export function isServerUpdateFailureDismissed(state: ServerUpdateState): boolean { |
| 27 | return state.status === "failed" && dismissedServerUpdateFailures.has(state); |
| 28 | } |
| 29 | |
| 30 | export function dismissServerUpdateFailure(state: ServerUpdateState): void { |
| 31 | if (state.status === "failed") dismissedServerUpdateFailures.add(state); |
| 32 | } |
| 33 | |
| 34 | const VersionMismatchDismissalsSchema = Schema.Struct({ |
| 35 | keys: Schema.Array(Schema.String), |
| 36 | }); |
| 37 | |
| 38 | type VersionMismatchDismissals = typeof VersionMismatchDismissalsSchema.Type; |
| 39 | |
| 40 | function normalizeVersion(version: string | null | undefined): string | null { |
| 41 | const trimmed = version?.trim(); |
| 42 | return trimmed && trimmed.length > 0 ? trimmed : null; |
| 43 | } |
| 44 | |
| 45 | function versionCore(version: string): string { |
| 46 | return version.replace(/[-+].*$/, ""); |
| 47 | } |
| 48 | |
| 49 | export function resolveVersionMismatch( |
| 50 | serverVersion: string | null | undefined, |
| 51 | clientVersion: string = APP_VERSION, |
| 52 | resolve: MessageResolver = m, |
| 53 | ): VersionMismatch | null { |
| 54 | const normalizedClientVersion = normalizeVersion(clientVersion); |
| 55 | const normalizedServerVersion = normalizeVersion(serverVersion); |
| 56 | if (!normalizedClientVersion || !normalizedServerVersion) { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | const clientCore = versionCore(normalizedClientVersion); |
| 61 | const serverCore = versionCore(normalizedServerVersion); |
| 62 | const compareNightlyBuilds = |
| 63 | parseSemver(normalizedClientVersion)?.prerelease[0] === "nightly" && |
| 64 | parseSemver(normalizedServerVersion)?.prerelease[0] === "nightly"; |
| 65 | const serverIsBehind = |
| 66 | parseSemver(clientCore) && parseSemver(serverCore) |
| 67 | ? compareSemverVersions( |
| 68 | compareNightlyBuilds ? normalizedServerVersion : serverCore, |
| 69 | compareNightlyBuilds ? normalizedClientVersion : clientCore, |
| 70 | ) < 0 |
| 71 | : normalizedServerVersion !== normalizedClientVersion; |
| 72 | if (!serverIsBehind) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | return { |
| 77 | clientVersion: normalizedClientVersion, |
| 78 | serverVersion: normalizedServerVersion, |
| 79 | hint: resolve( |
| 80 | msg("Version mismatch. Try syncing the client and server to the same T3 Code version."), |
| 81 | ), |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | export function resolveServerConfigVersionMismatch( |
| 86 | serverConfig: Pick<ServerConfig, "environment"> | null | undefined, |
| 87 | resolve: MessageResolver = m, |
| 88 | ): VersionMismatch | null { |
| 89 | return resolveVersionMismatch(serverConfig?.environment.serverVersion, APP_VERSION, resolve); |
| 90 | } |
| 91 | |
| 92 | export function resolveServerSelfUpdateCapability( |
| 93 | serverConfig: Pick<ServerConfig, "environment"> | null | undefined, |
| 94 | ): ServerSelfUpdateCapability | null { |
| 95 | return serverConfig?.environment.capabilities.serverSelfUpdate ?? null; |
| 96 | } |
| 97 | |
| 98 | export function supportsDesktopAppUpdate( |
| 99 | serverConfig: Pick<ServerConfig, "environment"> | null | undefined, |
| 100 | ): boolean { |
| 101 | return serverConfig?.environment.capabilities.desktopAppUpdate === true; |
| 102 | } |
| 103 | |
| 104 | export function manualServerUpdateCommand( |
| 105 | targetVersion: string, |
| 106 | installation?: ServerInstallation, |
| 107 | ): string { |
| 108 | const quote = (value: string) => `'${value.replaceAll("'", "'\\''")}'`; |
| 109 | if (installation?.kind === "npm-global") { |
| 110 | return `npm install --global --prefix ${quote(installation.prefix)} t3@${targetVersion}`; |
| 111 | } |
| 112 | if (installation?.kind === "t3-launcher") { |
| 113 | // An inherited T3CODE_HOME would otherwise send the update to a home the launcher is not in. |
| 114 | return `${quote(installation.launcher)} update --base-dir ${quote(installation.baseDir)} ${targetVersion}`; |
| 115 | } |
| 116 | const runner = |
| 117 | installation?.kind === "pnpm-dlx" ? "pnpm dlx" : installation?.kind === "bunx" ? "bunx" : "npx"; |
| 118 | return `${runner} t3@${targetVersion}`; |
| 119 | } |
| 120 | |
| 121 | export function buildVersionMismatchDismissalKey( |
| 122 | environmentId: EnvironmentId, |
| 123 | mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">, |
| 124 | ): string { |
| 125 | return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`; |
| 126 | } |
| 127 | |
| 128 | function readVersionMismatchDismissals(): VersionMismatchDismissals { |
| 129 | try { |
| 130 | return ( |
| 131 | getLocalStorageItem( |
| 132 | VERSION_MISMATCH_DISMISSALS_STORAGE_KEY, |
| 133 | VersionMismatchDismissalsSchema, |
| 134 | ) ?? { keys: [] } |
| 135 | ); |
| 136 | } catch (error) { |
| 137 | console.error("Could not read version-mismatch dismissals.", error); |
| 138 | return { keys: [] }; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void { |
| 143 | try { |
| 144 | setLocalStorageItem( |
| 145 | VERSION_MISMATCH_DISMISSALS_STORAGE_KEY, |
| 146 | document, |
| 147 | VersionMismatchDismissalsSchema, |
| 148 | ); |
| 149 | } catch (error) { |
| 150 | console.error("Could not persist version-mismatch dismissals.", error); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean { |
| 155 | if (!dismissalKey) { |
| 156 | return false; |
| 157 | } |
| 158 | return readVersionMismatchDismissals().keys.includes(dismissalKey); |
| 159 | } |
| 160 | |
| 161 | export function dismissVersionMismatch(dismissalKey: string | null | undefined): void { |
| 162 | if (!dismissalKey) { |
| 163 | return; |
| 164 | } |
| 165 | const document = readVersionMismatchDismissals(); |
| 166 | if (document.keys.includes(dismissalKey)) { |
| 167 | return; |
| 168 | } |
| 169 | writeVersionMismatchDismissals({ |
| 170 | keys: [...document.keys, dismissalKey], |
| 171 | }); |
| 172 | } |