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

codexCliVersion.ts

144 lines4.0 KB

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