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 } from "gt-i18n";
4import { formatConjunctionList } from "./listFormat.ts";
5import type { MFunctionType } from "gt-i18n/types";
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: MFunctionType = 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(
93 "Using {names} requires {providerName} {version} or newer, but the claude-code Homebrew cask only has {latestVersion}. Switch to the claude-code@latest cask: {command}",
94 {
95 providerName,
96 version,
97 names,
98 latestVersion: withV(latestVersion),
99 command: HOMEBREW_LATEST_CASK_SWITCH_COMMAND,
100 },
101 ),
102 );
103 }
104 if (latestVersion) {
105 return resolve(
106 msg(
107 "{runtime, select, codex {Using {names} requires the Codex CLI {version} or newer, but the newest this install can update to is {latestVersion}. Install the Codex CLI another way or wait for a newer release.} other {Using {names} requires {providerName} {version} or newer, but the newest this install can update to is {latestVersion}. Install {providerName} another way or wait for a newer release.}}",
108 { runtime, providerName, version, names, latestVersion: withV(latestVersion) },
109 ),
110 );
111 }
112 return resolve(
113 msg(
114 "{runtime, select, codex {Update the Codex CLI to {version} or newer to use {names}.} other {Update {providerName} to {version} or newer to use {names}.}}",
115 {
116 runtime,
117 providerName,
118 version,
119 names,
120 },
121 ),
122 );
123}