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

providerStatus.ts

125 lines3.9 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.
30 */
31export function getProviderSummary(provider: ServerProvider | undefined) {
32 if (!provider) {
33 return {
34 headline: "Checking provider status",
35 detail: "Waiting for the server to report installation and authentication details.",
36 };
37 }
38 if (!provider.enabled) {
39 return {
40 headline: "Disabled",
41 detail:
42 provider.message ?? "This provider is installed but disabled for new sessions in T3 Code.",
43 };
44 }
45 if (!provider.installed) {
46 return {
47 headline: "Not found",
48 detail: provider.message ?? "CLI not detected on PATH.",
49 };
50 }
51 if (provider.auth.status === "authenticated") {
52 const authLabel = provider.auth.label ?? provider.auth.type;
53 return {
54 headline: authLabel ? `Authenticated · ${authLabel}` : "Authenticated",
55 detail: provider.message ?? null,
56 };
57 }
58 if (provider.auth.status === "unauthenticated") {
59 return {
60 headline: "Not authenticated",
61 detail: provider.message ?? null,
62 };
63 }
64 if (provider.status === "warning") {
65 return {
66 headline: "Needs attention",
67 detail:
68 provider.message ?? "The provider is installed, but the server could not fully verify it.",
69 };
70 }
71 if (provider.status === "error") {
72 return {
73 headline: "Unavailable",
74 detail: provider.message ?? "The provider failed its startup checks.",
75 };
76 }
77 return {
78 headline: "Available",
79 detail: provider.message ?? "Installed and ready, but authentication could not be verified.",
80 };
82​
83/**
84 * Normalize a version string for display. Adds the `v` prefix when the
85 * driver reported a bare version (e.g. `1.2.3`) so cards render
86 * consistently regardless of driver.
87 */
88export function getProviderVersionLabel(version: string | null | undefined) {
89 if (!version) return null;
90 // Antigravity reports a release tag such as `agy_acp_server_20260818_01_RC01`.
91 // Show the date and candidate so the row title keeps room for the name.
92 const antigravity = /^agy_acp_server_(\d{4})(\d{2})(\d{2})_\d+(?:_(\w+))?$/.exec(version);
93 if (antigravity) {
94 const [, year, month, day, candidate] = antigravity;
95 return `${year}-${month}-${day}${candidate ? ` ${candidate}` : ""}`;
96 }
97 // Only bare semver-like versions get a `v` prefix. Other tags are shown as-is.
98 return /^\d/.test(version) ? `v${version}` : version;
100​
101export function getProviderVersionAdvisoryPresentation(
102 advisory: ServerProviderVersionAdvisory | undefined,
103): {
104 readonly detail: string;
105 readonly updateCommand: string | null;
106 readonly emphasis: "normal" | "strong";
107} | null {
108 if (!advisory || advisory.status === "current" || advisory.status === "unknown") {
109 return null;
110 }
111​
112 const label = "Update available";
113 const version = advisory.latestVersion;
114 const versionLabel = getProviderVersionLabel(version);
115​
116 return {
117 detail:
118 advisory.message ??
119 (versionLabel
120 ? `${label}: install ${versionLabel}.`
121 : `${label}: install the latest provider version.`),
122 updateCommand: advisory.updateCommand,
123 emphasis: "normal" as const,
124 };