Open History to see the last commit
1import { PROVIDER_DISPLAY_NAMES, type ServerProvider } from "@t3tools/contracts";
2import { compareSemverVersions, parseSemver } from "@t3tools/shared/semver";
3import { mFallback, msg, select, part } from "gt-i18n";
4import { formatConjunctionList } from "./listFormat.ts";
5import type { M } from "gt-i18n";
6
7const CLAUDE_CODE_NAME = "Claude Code";
8
9function withV(version: string): string {
10 return version.startsWith("v") ? version : `v${version}`;
11}
12
13// The stable cask lags npm; the conflicting claude-code@latest cask tracks it.
14const HOMEBREW_STABLE_CASK_UPDATE_COMMAND = "brew upgrade --cask claude-code";
15const HOMEBREW_LATEST_CASK_SWITCH_COMMAND =
16 "brew uninstall --cask claude-code && brew install --cask claude-code@latest";
17
18type UpdateRequiredProvider = Pick<
19 ServerProvider,
20 "driver" | "updateRequiredModels" | "versionAdvisory"
21>;
22
23function matchingUpdateRequiredModels(provider: UpdateRequiredProvider, searchQuery: string) {
24 const query = searchQuery.trim().toLocaleLowerCase();
25 return (provider.updateRequiredModels ?? []).filter(
26 (model) =>
27 query.length === 0 ||
28 model.name.toLocaleLowerCase().includes(query) ||
29 model.slug.toLocaleLowerCase().includes(query),
30 );
31}
32
33function highestMinVersion(models: ReadonlyArray<{ readonly minVersion: string }>): string {
34 return models
35 .map((model) => model.minVersion)
36 .reduce((highest, version) =>
37 compareSemverVersions(version, highest) > 0 ? version : highest,
38 );
39}
40
41function channelLatestVersionBelow(
42 provider: UpdateRequiredProvider,
43 minVersion: string,
44): string | null {
45 const latestVersion = provider.versionAdvisory?.latestVersion;
46 return latestVersion &&
47 parseSemver(latestVersion) &&
48 parseSemver(minVersion) &&
49 compareSemverVersions(latestVersion, minVersion) < 0
50 ? latestVersion
51 : null;
52}
53
54/** True when the provider's own update cannot reach the version its update-required models need. */
55export function providerUpdateChannelLags(
56 provider: UpdateRequiredProvider,
57 searchQuery = "",
58): boolean {
59 const models = matchingUpdateRequiredModels(provider, searchQuery);
60 return (
61 models.length > 0 && channelLatestVersionBelow(provider, highestMinVersion(models)) !== null
62 );
63}
64
65export function formatProviderUpdateRequiredNotice(
66 provider: UpdateRequiredProvider,
67 searchQuery = "",
68 resolve: M = mFallback,
69 locale = "en",
70): string | null {
71 const models = matchingUpdateRequiredModels(provider, searchQuery);
72 if (models.length === 0) return null;
73 const minVersion = highestMinVersion(models);
74 const providerName =
75 provider.driver === "claudeAgent"
76 ? CLAUDE_CODE_NAME
77 : (PROVIDER_DISPLAY_NAMES[provider.driver] ?? provider.driver);
78 const version = withV(minVersion);
79 const names = formatConjunctionList(
80 models.map((model) => model.name),
81 locale,
82 resolve,
83 );
84 const runtime = provider.driver === "codex" ? "codex" : "other";
85 const latestVersion = channelLatestVersionBelow(provider, minVersion);
86 if (
87 latestVersion &&
88 provider.driver === "claudeAgent" &&
89 provider.versionAdvisory?.updateCommand === HOMEBREW_STABLE_CASK_UPDATE_COMMAND
90 ) {
91 return resolve(
92 msg`Using ${{ names }} requires ${{ providerName }} ${{ version }} or newer, but the claude-code Homebrew cask only has ${{ latestVersion: withV(latestVersion) }}. Switch to the claude-code@latest cask: ${{ command: HOMEBREW_LATEST_CASK_SWITCH_COMMAND }}`,
93 );
94 }
95 if (latestVersion) {
96 return resolve(
97 msg`${select(
98 { runtime },
99 {
100 codex: part`Using ${{ names }} requires the Codex CLI ${{ version }} or newer, but the newest this install can update to is ${{ latestVersion: withV(latestVersion) }}. Install the Codex CLI another way or wait for a newer release.`,
101 other: part`Using ${{ names }} requires ${{ providerName }} ${{ version }} or newer, but the newest this install can update to is ${{ latestVersion: withV(latestVersion) }}. Install ${{ providerName }} another way or wait for a newer release.`,
102 },
103 )}`,
104 );
105 }
106 return resolve(
107 msg`${select(
108 { runtime },
109 {
110 codex: part`Update the Codex CLI to ${{ version }} or newer to use ${{ names }}.`,
111 other: part`Update ${{ providerName }} to ${{ version }} or newer to use ${{ names }}.`,
112 },
113 )}`,
114 );
115}