Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

versionSkew.ts

205 lines7.2 KB

1import type {
2 EnvironmentId,
3 ServerConfig,
4 ServerInstallation,
5 ServerSelfUpdateCapability,
6} from "@t3tools/contracts";
7import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
8import { compareSemverVersions, parseSemver } from "@t3tools/shared/semver";
9import * as Schema from "effect/Schema";
10​
11import { APP_VERSION } from "./branding";
12import { getLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage";
13​
14export interface VersionMismatch {
15 readonly clientVersion: string;
16 readonly serverVersion: string;
17 readonly hint: string;
19​
20const VERSION_MISMATCH_DISMISSALS_STORAGE_KEY = "t3code:version-mismatch-dismissals:v1";
21​
22// Runtime failures retain their identity until the next attempt. Dismiss only
23// that attempt, across chat remounts, without clearing the error in Settings.
24const dismissedServerUpdateFailures = new WeakSet<ServerUpdateState>();
25​
26export function isServerUpdateFailureDismissed(state: ServerUpdateState): boolean {
27 return state.status === "failed" && dismissedServerUpdateFailures.has(state);
29​
30export function dismissServerUpdateFailure(state: ServerUpdateState): void {
31 if (state.status === "failed") dismissedServerUpdateFailures.add(state);
33​
34const VersionMismatchDismissalsSchema = Schema.Struct({
35 keys: Schema.Array(Schema.String),
36});
37​
38type VersionMismatchDismissals = typeof VersionMismatchDismissalsSchema.Type;
39​
40function normalizeVersion(version: string | null | undefined): string | null {
41 const trimmed = version?.trim();
42 return trimmed && trimmed.length > 0 ? trimmed : null;
44​
45/** Core `major.minor.patch`, dropping any prerelease or build suffix. */
46function versionCore(version: string): string {
47 return version.replace(/[-+].*$/, "");
49​
50/**
51 * The skew a user can act on: the connected server runs an older T3 Code than
52 * this client, so the server is the side that needs updating.
53 *
54 * Two nightly builds compare their full versions, including the date and run.
55 * Other combinations compare their core `major.minor.patch` only, so a stable
56 * build and a nightly build with the same core do not cause an update warning.
57 * A server ahead of the client does not need an update. Versions that do not
58 * parse as semver fall back to plain string inequality.
59 */
60export function resolveVersionMismatch(
61 serverVersion: string | null | undefined,
62): VersionMismatch | null {
63 const normalizedClientVersion = normalizeVersion(APP_VERSION);
64 const normalizedServerVersion = normalizeVersion(serverVersion);
65 if (!normalizedClientVersion || !normalizedServerVersion) {
66 return null;
67 }
68​
69 const clientCore = versionCore(normalizedClientVersion);
70 const serverCore = versionCore(normalizedServerVersion);
71 const compareNightlyBuilds =
72 parseSemver(normalizedClientVersion)?.prerelease[0] === "nightly" &&
73 parseSemver(normalizedServerVersion)?.prerelease[0] === "nightly";
74 const serverIsBehind =
75 parseSemver(clientCore) && parseSemver(serverCore)
76 ? compareSemverVersions(
77 compareNightlyBuilds ? normalizedServerVersion : serverCore,
78 compareNightlyBuilds ? normalizedClientVersion : clientCore,
79 ) < 0
80 : normalizedServerVersion !== normalizedClientVersion;
81 if (!serverIsBehind) {
82 return null;
83 }
84​
85 return {
86 clientVersion: normalizedClientVersion,
87 serverVersion: normalizedServerVersion,
88 hint: "Version mismatch. Try syncing the client and server to the same T3 Code version.",
89 };
91​
92export function resolveServerConfigVersionMismatch(
93 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
94): VersionMismatch | null {
95 return resolveVersionMismatch(serverConfig?.environment.serverVersion);
97​
98/** The update path the connected server offers, or null when it only
99 supports a manual relaunch (older servers, dev checkouts, Windows). */
100export function resolveServerSelfUpdateCapability(
101 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
102): ServerSelfUpdateCapability | null {
103 return serverConfig?.environment.capabilities.serverSelfUpdate ?? null;
105​
106/** True when the desktop app supervising this server can be told to update
107 itself over RPC. Older desktop servers only get the manual instruction. */
108export function supportsDesktopAppUpdate(
109 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
110): boolean {
111 return serverConfig?.environment.capabilities.desktopAppUpdate === true;
113​
114/** True when the connected server can recover opted-in running turns after
115 its self-update restart. */
116export function supportsServerUpdateThreadContinuation(
117 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
118): boolean {
119 return serverConfig?.environment.capabilities.serverUpdateThreadContinuation === true;
121​
122/** The command to hand users whose server cannot update itself. */
123export function manualServerUpdateCommand(
124 targetVersion: string,
125 installation?: ServerInstallation,
126): string {
127 if (installation?.kind === "npm-global") {
128 const prefix = `'${installation.prefix.replaceAll("'", "'\\''")}'`;
129 return `npm install --global --prefix ${prefix} t3@${targetVersion}`;
130 }
131 const runner =
132 installation?.kind === "pnpm-dlx" ? "pnpm dlx" : installation?.kind === "bunx" ? "bunx" : "npx";
133 return `${runner} t3@${targetVersion}`;
135​
136export function serverUpdateGuidance(capability: ServerSelfUpdateCapability): string {
137 return capability === "desktop-managed" ? "Update the desktop app" : "Update to stay in sync";
139​
140export function buildVersionMismatchDismissalKey(
141 environmentId: EnvironmentId,
142 mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">,
143): string {
144 return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`;
146​
147function readVersionMismatchDismissals(): VersionMismatchDismissals {
148 try {
149 return (
150 getLocalStorageItem(
151 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
152 VersionMismatchDismissalsSchema,
153 ) ?? { keys: [] }
154 );
155 } catch (error) {
156 console.error("Could not read version-mismatch dismissals.", error);
157 return { keys: [] };
158 }
160​
161function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void {
162 try {
163 setLocalStorageItem(
164 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
165 document,
166 VersionMismatchDismissalsSchema,
167 );
168 } catch (error) {
169 console.error("Could not persist version-mismatch dismissals.", error);
170 }
172​
173export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean {
174 if (!dismissalKey) {
175 return false;
176 }
177 return readVersionMismatchDismissals().keys.includes(dismissalKey);
179​
180export function dismissVersionMismatch(dismissalKey: string | null | undefined): void {
181 if (!dismissalKey) {
182 return;
183 }
184 const document = readVersionMismatchDismissals();
185 if (document.keys.includes(dismissalKey)) {
186 return;
187 }
188 writeVersionMismatchDismissals({
189 keys: [...document.keys, dismissalKey],
190 });
192​
193export function appendVersionMismatchHint(
194 message: string | null | undefined,
195 mismatch: VersionMismatch | null | undefined,
196): string | null {
197 const normalizedMessage = normalizeVersion(message);
198 if (!normalizedMessage) {
199 return mismatch?.hint ?? null;
200 }
201 if (!mismatch) {
202 return normalizedMessage;
203 }
204 return `${normalizedMessage} Hint: ${mismatch.hint}`;