Open History to see the last commit
1import type {
2 ServerProvider,
3 ServerProviderVersionAdvisory,
4 ServerProviderCompatibilityAdvisory,
5} from "@t3tools/contracts";
6
7/**
8 * Visual treatment for each server-reported provider status. Centralized so
9 * the default-driver card and per-instance cards share the same language.
10 */
11export const PROVIDER_STATUS_STYLES = {
12 disabled: {
13 dot: "bg-muted-foreground/50",
14 },
15 error: {
16 dot: "bg-destructive",
17 },
18 ready: {
19 dot: "bg-success",
20 },
21 warning: {
22 dot: "bg-warning",
23 },
24} as const;
25
26export type ProviderStatusKey = keyof typeof PROVIDER_STATUS_STYLES;
27
28/**
29 * Derive the headline + detail copy shown under a provider's name in the
30 * settings page. Prefers `provider.message` for server-supplied detail and
31 * falls back to generic phrasing when the server has not yet reported any
32 * state — which happens before the first probe or when an instance names a
33 * driver this build does not ship. A ready provider without account metadata
34 * remains available and does not imply an authentication failure.
35 */
36export function getProviderSummary(provider: ServerProvider | undefined) {
37 if (!provider) {
38 return {
39 headline: "Checking provider status",
40 detail: "Waiting for the server to report installation and authentication details.",
41 };
42 }
43 if (!provider.enabled || provider.status === "disabled") {
44 return {
45 headline: "Disabled",
46 detail:
47 provider.message ?? "This provider is installed but disabled for new sessions in T3 Code.",
48 };
49 }
50 if (!provider.installed) {
51 return {
52 headline: "Not found",
53 detail: provider.message ?? "CLI not detected on PATH.",
54 };
55 }
56 if (provider.auth.status === "unauthenticated") {
57 return {
58 headline: "Not authenticated",
59 detail: provider.message ?? null,
60 };
61 }
62 if (provider.status === "warning") {
63 return {
64 headline: "Needs attention",
65 detail:
66 provider.message ?? "The provider is installed, but the server could not fully verify it.",
67 };
68 }
69 if (provider.status === "error") {
70 return {
71 headline: "Unavailable",
72 detail: provider.message ?? "The provider failed its startup checks.",
73 };
74 }
75 if (provider.auth.status === "authenticated") {
76 const authLabel = provider.auth.label ?? provider.auth.type;
77 return {
78 headline: authLabel ? `Authenticated · ${authLabel}` : "Authenticated",
79 detail: provider.message ?? null,
80 };
81 }
82 return {
83 headline: "Available",
84 detail: provider.message ?? null,
85 };
86}
87
88/**
89 * Normalize a version string for display. Adds the `v` prefix when the
90 * driver reported a bare version (e.g. `1.2.3`) so cards render
91 * consistently regardless of driver.
92 */
93export function getProviderVersionLabel(version: string | null | undefined) {
94 if (!version) return null;
95 // Antigravity reports a release tag such as `agy_acp_server_20260818_01_RC01`.
96 // Show the date and candidate so the row title keeps room for the name.
97 const antigravity = /^agy_acp_server_(\d{4})(\d{2})(\d{2})_\d+(?:_(\w+))?$/.exec(version);
98 if (antigravity) {
99 const [, year, month, day, candidate] = antigravity;
100 return `${year}-${month}-${day}${candidate ? ` ${candidate}` : ""}`;
101 }
102 // Only bare semver-like versions get a `v` prefix. Other tags are shown as-is.
103 return /^\d/.test(version) ? `v${version}` : version;
104}
105
106const COMPATIBILITY_TITLES = {
107 graceful: "Limited support",
108 unsupported: "Unsupported version",
109 broken: "Known broken version",
110} as const;
111
112/** Compatibility guidance shares the version popover, with safe install actions. */
113export function getProviderVersionAdvisoryPresentation(
114 advisory: ServerProviderVersionAdvisory | undefined,
115 compatibility?: ServerProviderCompatibilityAdvisory | undefined,
116 showCompatibility = true,
117): {
118 readonly title: string;
119 readonly detail: string;
120 readonly updateCommand: string | null;
121 readonly emphasis: "normal" | "strong";
122 readonly targetVersion: string | null;
123} | null {
124 const latestIsIncompatible =
125 compatibility?.latestVersionStatus === "broken" ||
126 compatibility?.latestVersionStatus === "unsupported";
127 if (
128 showCompatibility &&
129 compatibility &&
130 (compatibility.status === "graceful" ||
131 compatibility.status === "unsupported" ||
132 compatibility.status === "broken")
133 ) {
134 const targetVersion = compatibility.recommendedVersion;
135 const recommendation = getProviderVersionLabel(targetVersion) ?? compatibility.recommendedRange;
136 return {
137 title: COMPATIBILITY_TITLES[compatibility.status],
138 detail:
139 compatibility.message ??
140 (recommendation ? `Use ${recommendation} for full support.` : "Update for full support."),
141 updateCommand:
142 targetVersion || latestIsIncompatible ? null : (advisory?.updateCommand ?? null),
143 emphasis: compatibility.status === "graceful" ? "normal" : "strong",
144 targetVersion,
145 };
146 }
147 if (
148 !advisory ||
149 advisory.status === "current" ||
150 advisory.status === "unknown" ||
151 latestIsIncompatible
152 ) {
153 return null;
154 }
155
156 const label = "Update available";
157 const version = advisory.latestVersion;
158 const versionLabel = getProviderVersionLabel(version);
159
160 return {
161 title: label,
162 detail:
163 advisory.message ??
164 (versionLabel
165 ? `${label}: install ${versionLabel}.`
166 : `${label}: install the latest provider version.`),
167 updateCommand: advisory.updateCommand,
168 emphasis: "normal" as const,
169 targetVersion: null,
170 };
171}