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

providerStatus.ts

127 lines4.1 KB

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 const authLabel = provider.auth.label ?? provider.auth.type;
54 return {
55 headline: authLabel ? `Not authenticated · ${authLabel}` : "Not authenticated",
56 detail: provider.message ?? null,
57 };
58 }
59 if (provider.status === "warning") {
60 return {
61 headline: "Needs attention",
62 detail:
63 provider.message ?? "The provider is installed, but the server could not fully verify it.",
64 };
65 }
66 if (provider.status === "error") {
67 return {
68 headline: "Unavailable",
69 detail: provider.message ?? "The provider failed its startup checks.",
70 };
71 }
72 if (provider.auth.status === "authenticated") {
73 const authLabel = provider.auth.label ?? provider.auth.type;
74 return {
75 headline: authLabel ? `Authenticated · ${authLabel}` : "Authenticated",
76 detail: provider.message ?? null,
77 };
78 }
79 return {
80 headline: "Available",
81 detail: provider.message ?? null,
82 };
84​
85/**
86 * Normalize a version string for display. Adds the `v` prefix when the
87 * driver reported a bare version (e.g. `1.2.3`) so cards render
88 * consistently regardless of driver.
89 */
90export function getProviderVersionLabel(version: string | null | undefined) {
91 if (!version) return null;
92 // Antigravity reports a release tag such as `agy_acp_server_20260818_01_RC01`.
93 // Show the date and candidate so the row title keeps room for the name.
94 const antigravity = /^agy_acp_server_(\d{4})(\d{2})(\d{2})_\d+(?:_(\w+))?$/.exec(version);
95 if (antigravity) {
96 const [, year, month, day, candidate] = antigravity;
97 return `${year}-${month}-${day}${candidate ? ` ${candidate}` : ""}`;
98 }
99 // Only bare semver-like versions get a `v` prefix. Other tags are shown as-is.
100 return /^\d/.test(version) ? `v${version}` : version;
102​
103export function getProviderVersionAdvisoryPresentation(
104 advisory: ServerProviderVersionAdvisory | undefined,
105): {
106 readonly detail: string;
107 readonly updateCommand: string | null;
108 readonly emphasis: "normal" | "strong";
109} | null {
110 if (!advisory || advisory.status === "current" || advisory.status === "unknown") {
111 return null;
112 }
113​
114 const label = "Update available";
115 const version = advisory.latestVersion;
116 const versionLabel = getProviderVersionLabel(version);
117​
118 return {
119 detail:
120 advisory.message ??
121 (versionLabel
122 ? `${label}: install ${versionLabel}.`
123 : `${label}: install the latest provider version.`),
124 updateCommand: advisory.updateCommand,
125 emphasis: "normal" as const,
126 };