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