import type { ServerProvider, ServerProviderVersionAdvisory } from "@t3tools/contracts"; /** * Visual treatment for each server-reported provider status. Centralized so * the default-driver card and per-instance cards share the same language. */ export const PROVIDER_STATUS_STYLES = { disabled: { dot: "bg-muted-foreground/50", }, error: { dot: "bg-destructive", }, ready: { dot: "bg-success", }, warning: { dot: "bg-warning", }, } as const; export type ProviderStatusKey = keyof typeof PROVIDER_STATUS_STYLES; /** * Derive the headline + detail copy shown under a provider's name in the * settings page. Prefers `provider.message` for server-supplied detail and * falls back to generic phrasing when the server has not yet reported any * state — which happens before the first probe or when an instance names a * driver this build does not ship. A ready provider without account metadata * remains available and does not imply an authentication failure. */ export function getProviderSummary(provider: ServerProvider | undefined) { if (!provider) { return { headline: "Checking provider status", detail: "Waiting for the server to report installation and authentication details.", }; } if (!provider.enabled || provider.status === "disabled") { return { headline: "Disabled", detail: provider.message ?? "This provider is installed but disabled for new sessions in T3 Code.", }; } if (!provider.installed) { return { headline: "Not found", detail: provider.message ?? "CLI not detected on PATH.", }; } if (provider.auth.status === "unauthenticated") { return { headline: "Not authenticated", detail: provider.message ?? null, }; } if (provider.status === "warning") { return { headline: "Needs attention", detail: provider.message ?? "The provider is installed, but the server could not fully verify it.", }; } if (provider.status === "error") { return { headline: "Unavailable", detail: provider.message ?? "The provider failed its startup checks.", }; } if (provider.auth.status === "authenticated") { const authLabel = provider.auth.label ?? provider.auth.type; return { headline: authLabel ? `Authenticated · ${authLabel}` : "Authenticated", detail: provider.message ?? null, }; } return { headline: "Available", detail: provider.message ?? null, }; } /** * Normalize a version string for display. Adds the `v` prefix when the * driver reported a bare version (e.g. `1.2.3`) so cards render * consistently regardless of driver. */ export function getProviderVersionLabel(version: string | null | undefined) { if (!version) return null; // Antigravity reports a release tag such as `agy_acp_server_20260818_01_RC01`. // Show the date and candidate so the row title keeps room for the name. const antigravity = /^agy_acp_server_(\d{4})(\d{2})(\d{2})_\d+(?:_(\w+))?$/.exec(version); if (antigravity) { const [, year, month, day, candidate] = antigravity; return `${year}-${month}-${day}${candidate ? ` ${candidate}` : ""}`; } // Only bare semver-like versions get a `v` prefix. Other tags are shown as-is. return /^\d/.test(version) ? `v${version}` : version; } export function getProviderVersionAdvisoryPresentation( advisory: ServerProviderVersionAdvisory | undefined, ): { readonly detail: string; readonly updateCommand: string | null; readonly emphasis: "normal" | "strong"; } | null { if (!advisory || advisory.status === "current" || advisory.status === "unknown") { return null; } const label = "Update available"; const version = advisory.latestVersion; const versionLabel = getProviderVersionLabel(version); return { detail: advisory.message ?? (versionLabel ? `${label}: install ${versionLabel}.` : `${label}: install the latest provider version.`), updateCommand: advisory.updateCommand, emphasis: "normal" as const, }; }