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

versionSkew.ts

177 lines6.4 KB

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