203 lines · 6.3 KB · TypeScript
RawDownload
import type {  ServerProvider,  ServerProviderCompatibilityAdvisory,  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-amber-400",  },  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. */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) {    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 === "authenticated") {    const authLabel = provider.auth.label ?? provider.auth.type;    return {      headline: authLabel ? `Authenticated · ${authLabel}` : "Authenticated",      detail: provider.message ?? null,    };  }  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.",    };  }  return {    headline: "Available",    detail: provider.message ?? "Installed and ready, but authentication could not be verified.",  };} /** * 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;  return version.startsWith("v") ? version : `v${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,  };} function makeTargetedUpdateCommand(input: {  readonly updateCommand: string | null | undefined;  readonly recommendedVersion: string | null | undefined;}): string | null {  if (!input.updateCommand || !input.recommendedVersion) {    return null;  }  if (!input.updateCommand.includes("@latest")) {    const packageNameMatch = input.updateCommand.match(      /(?:^|\s)(@[^\s]+\/[^\s@]+|[^\s@]+)(?=\s*$)/,    );    if (!packageNameMatch?.[1]) {      return null;    }    return input.updateCommand.replace(      packageNameMatch[1],      `${packageNameMatch[1]}@${input.recommendedVersion}`,    );  }  return input.updateCommand.replace("@latest", `@${input.recommendedVersion}`);} export function getProviderCompatibilityUpdateCommand(  provider: Pick<ServerProvider, "compatibilityAdvisory" | "versionAdvisory"> | null | undefined,): string | null {  const compatibilityAdvisory = provider?.compatibilityAdvisory;  if (!compatibilityAdvisory || compatibilityAdvisory.status === "supported") {    return null;  }  return (    compatibilityAdvisory.updateCommand ??    makeTargetedUpdateCommand({      updateCommand: provider.versionAdvisory?.updateCommand,      recommendedVersion: compatibilityAdvisory.recommendedVersion,    })  );} export function canRunProviderCompatibilityUpdate(  provider: Pick<ServerProvider, "compatibilityAdvisory" | "versionAdvisory"> | null | undefined,): boolean {  const compatibilityAdvisory = provider?.compatibilityAdvisory;  if (!compatibilityAdvisory || compatibilityAdvisory.status === "supported") {    return false;  }  return (    compatibilityAdvisory.canUpdate === true ||    (provider?.versionAdvisory?.canUpdate === true &&      getProviderCompatibilityUpdateCommand(provider) !== null)  );} export function getProviderCompatibilityAdvisoryPresentation(  advisory: ServerProviderCompatibilityAdvisory | undefined,): {  readonly title: string;  readonly detail: string;  readonly updateCommand: string | null;  readonly canUpdate: boolean;  readonly emphasis: "normal" | "strong";} | null {  if (!advisory || advisory.status === "supported") {    return null;  }   const recommendedTarget = advisory.recommendedVersion ?? advisory.recommendedRange;  const recommended = recommendedTarget ? ` Recommended: ${recommendedTarget}.` : "";  const fallback =    advisory.status === "unknown"      ? `Compatibility unknown.${recommended}`      : `This provider harness is outside the supported range.${recommended}`;   return {    title:      advisory.status === "broken" ? "Incompatible provider version" : "Provider version warning",    detail: advisory.message ?? fallback,    updateCommand: advisory.updateCommand ?? null,    canUpdate: advisory.canUpdate === true,    emphasis: advisory.severity === "error" ? "strong" : "normal",  };}