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

codexCliVersion.ts

141 lines3.9 KB

1const CODEX_VERSION_PATTERN = /\bv?(\d+\.\d+(?:\.\d+)?(?:-[0-9A-Za-z.-]+)?)\b/;
2​
3export const MINIMUM_CODEX_CLI_VERSION = "0.37.0";
4​
5interface ParsedSemver {
6 readonly major: number;
7 readonly minor: number;
8 readonly patch: number;
9 readonly prerelease: ReadonlyArray<string>;
11​
12function normalizeCodexVersion(version: string): string {
13 const [main, prerelease] = version.trim().split("-", 2);
14 const segments = (main ?? "")
15 .split(".")
16 .map((segment) => segment.trim())
17 .filter((segment) => segment.length > 0);
18​
19 if (segments.length === 2) {
20 segments.push("0");
21 }
22​
23 return prerelease ? `${segments.join(".")}-${prerelease}` : segments.join(".");
25​
26function parseSemver(version: string): ParsedSemver | null {
27 const normalized = normalizeCodexVersion(version);
28 const [main = "", prerelease] = normalized.split("-", 2);
29 const segments = main.split(".");
30 if (segments.length !== 3) {
31 return null;
32 }
33​
34 const [majorSegment, minorSegment, patchSegment] = segments;
35 if (majorSegment === undefined || minorSegment === undefined || patchSegment === undefined) {
36 return null;
37 }
38​
39 const major = Number.parseInt(majorSegment, 10);
40 const minor = Number.parseInt(minorSegment, 10);
41 const patch = Number.parseInt(patchSegment, 10);
42 if (![major, minor, patch].every(Number.isInteger)) {
43 return null;
44 }
45​
46 return {
47 major,
48 minor,
49 patch,
50 prerelease:
51 prerelease
52 ?.split(".")
53 .map((segment) => segment.trim())
54 .filter((segment) => segment.length > 0) ?? [],
55 };
57​
58function comparePrereleaseIdentifier(left: string, right: string): number {
59 const leftNumeric = /^\d+$/.test(left);
60 const rightNumeric = /^\d+$/.test(right);
61​
62 if (leftNumeric && rightNumeric) {
63 return Number.parseInt(left, 10) - Number.parseInt(right, 10);
64 }
65 if (leftNumeric) {
66 return -1;
67 }
68 if (rightNumeric) {
69 return 1;
70 }
71 return left.localeCompare(right);
73​
74export function compareCodexCliVersions(left: string, right: string): number {
75 const parsedLeft = parseSemver(left);
76 const parsedRight = parseSemver(right);
77 if (!parsedLeft || !parsedRight) {
78 return left.localeCompare(right);
79 }
80​
81 if (parsedLeft.major !== parsedRight.major) {
82 return parsedLeft.major - parsedRight.major;
83 }
84 if (parsedLeft.minor !== parsedRight.minor) {
85 return parsedLeft.minor - parsedRight.minor;
86 }
87 if (parsedLeft.patch !== parsedRight.patch) {
88 return parsedLeft.patch - parsedRight.patch;
89 }
90​
91 if (parsedLeft.prerelease.length === 0 && parsedRight.prerelease.length === 0) {
92 return 0;
93 }
94 if (parsedLeft.prerelease.length === 0) {
95 return 1;
96 }
97 if (parsedRight.prerelease.length === 0) {
98 return -1;
99 }
100​
101 const length = Math.max(parsedLeft.prerelease.length, parsedRight.prerelease.length);
102 for (let index = 0; index < length; index += 1) {
103 const leftIdentifier = parsedLeft.prerelease[index];
104 const rightIdentifier = parsedRight.prerelease[index];
105 if (leftIdentifier === undefined) {
106 return -1;
107 }
108 if (rightIdentifier === undefined) {
109 return 1;
110 }
111 const comparison = comparePrereleaseIdentifier(leftIdentifier, rightIdentifier);
112 if (comparison !== 0) {
113 return comparison;
114 }
115 }
116​
117 return 0;
119​
120export function parseCodexCliVersion(output: string): string | null {
121 const match = CODEX_VERSION_PATTERN.exec(output);
122 if (!match?.[1]) {
123 return null;
124 }
125​
126 const parsed = parseSemver(match[1]);
127 if (!parsed) {
128 return null;
129 }
130​
131 return normalizeCodexVersion(match[1]);
133​
134export function isCodexCliVersionSupported(version: string): boolean {
135 return compareCodexCliVersions(version, MINIMUM_CODEX_CLI_VERSION) >= 0;
137​
138export function formatCodexCliUpgradeMessage(version: string | null): string {
139 const versionLabel = version ? `v${version}` : "the installed version";
140 return `Codex CLI ${versionLabel} is too old for T3 Code. Upgrade to v${MINIMUM_CODEX_CLI_VERSION} or newer and restart T3 Code.`;