Open History to see the last commit
1import { type ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
2import { memo } from "react";
3import { InfoIcon, XIcon } from "lucide-react";
4import { Alert, AlertAction, AlertDescription, AlertTitle } from "../ui/alert";
5import { Button, InlineButton } from "../ui/button";
6import { formatProviderDriverKindLabel } from "../../providerModels";
7import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
8
9function getIncompatibleVersion(status: ServerProvider) {
10 const compatibility = status.compatibilityAdvisory;
11 if (status.status === "error" && status.auth.status === "unauthenticated") return null;
12 return compatibility?.status === "broken" ||
13 (status.status === "ready" && compatibility?.status === "unsupported")
14 ? compatibility
15 : null;
16}
17
18export function getProviderStatusBannerKey(status: ServerProvider | null): string | null {
19 if (!status || status.status === "disabled") return null;
20 const incompatible = getIncompatibleVersion(status);
21 if (incompatible) {
22 return [
23 status.instanceId,
24 incompatible.status,
25 status.version ?? "",
26 incompatible.message ?? "",
27 ].join("\u0000");
28 }
29 if (status.status === "ready") return null;
30 // Antigravity checks saved credentials when a session starts. Its local
31 // health check leaves auth unknown after a restart, which is not a failure.
32 if (
33 status.driver === "antigravity" &&
34 status.installed &&
35 status.status === "warning" &&
36 status.auth.status === "unknown"
37 ) {
38 return null;
39 }
40 return [status.instanceId, status.status, status.auth.status, status.message ?? ""].join(
41 "\u0000",
42 );
43}
44
45export function shouldShowProviderStatusBanner(
46 status: ServerProvider | null,
47 dismissedBannerKey: string | null,
48): boolean {
49 const bannerKey = getProviderStatusBannerKey(status);
50 return bannerKey !== null && bannerKey !== dismissedBannerKey;
51}
52
53export function hasProviderSetup(status: ServerProvider): boolean {
54 return (
55 status.driver === "antigravity" ||
56 status.setup?.canAuthenticate === true ||
57 status.setup?.canInstall === true
58 );
59}
60
61export function getProviderStatusMessage(status: ServerProvider): string {
62 if (
63 status.auth.status !== "unauthenticated" &&
64 status.compatibilityAdvisory?.status === "broken" &&
65 status.compatibilityAdvisory.message
66 ) {
67 return status.compatibilityAdvisory.message;
68 }
69 if (status.message) return status.message;
70 const providerName = status.displayName?.trim() || formatProviderDriverKindLabel(status.driver);
71 if (!status.installed && hasProviderSetup(status)) {
72 return `Open provider setup to install ${formatProviderDriverKindLabel(status.driver)} on this environment.`;
73 }
74 if (status.auth.status === "unauthenticated") {
75 if (hasProviderSetup(status)) {
76 return status.driver === "antigravity"
77 ? "Open provider setup to sign in with Google."
78 : "Open provider setup to sign in.";
79 }
80 return "Sign in via the CLI to authenticate again.";
81 }
82 return status.status === "ready"
83 ? "No models are available for this provider."
84 : status.status === "error"
85 ? `${providerName} provider is unavailable.`
86 : `${providerName} provider has limited availability.`;
87}
88
89export const ProviderStatusBanner = memo(function ProviderStatusBanner({
90 onDismiss,
91 onOpenProviderSetup,
92 status,
93}: {
94 onDismiss: () => void;
95 onOpenProviderSetup?: (instanceId: ProviderInstanceId) => void;
96 status: ServerProvider | null;
97}) {
98 if (!status || getProviderStatusBannerKey(status) === null) {
99 return null;
100 }
101
102 const providerName = status.displayName?.trim() || formatProviderDriverKindLabel(status.driver);
103 const isUnauthenticated = status.status === "error" && status.auth.status === "unauthenticated";
104 const incompatible = getIncompatibleVersion(status);
105 const title = isUnauthenticated
106 ? `${providerName} is unauthenticated`
107 : incompatible
108 ? `${providerName} ${status.version ?? ""} is ${incompatible.status === "broken" ? "known to be broken" : "unsupported"}`
109 : `${providerName} provider status`;
110 const message = incompatible?.message ?? getProviderStatusMessage(status);
111 const isWarning =
112 incompatible?.status !== "broken" && (status.status === "warning" || incompatible !== null);
113
114 return (
115 <div className="pointer-events-auto mx-auto w-fit max-w-[calc(100%-2rem)] pt-3">
116 <Alert
117 variant={isWarning ? "warning" : "error"}
118 role={incompatible && incompatible.status !== "broken" ? "status" : "alert"}
119 surface="glass"
120 controlAlignment="first-line"
121 >
122 <InfoIcon />
123 <AlertTitle>{title}</AlertTitle>
124 <AlertDescription>
125 <Tooltip>
126 <TooltipTrigger render={<div className="line-clamp-3" />}>{message}</TooltipTrigger>
127 <TooltipPopup side="top" className="whitespace-pre-wrap">
128 {message}
129 </TooltipPopup>
130 </Tooltip>
131 {onOpenProviderSetup && hasProviderSetup(status) ? (
132 <InlineButton onClick={() => onOpenProviderSetup(status.instanceId)}>
133 Open provider setup
134 </InlineButton>
135 ) : null}
136 </AlertDescription>
137 <AlertAction>
138 <Button
139 aria-label={`Dismiss ${providerName} provider ${status.status}`}
140 onClick={onDismiss}
141 size="icon-xs"
142 variant="ghost-muted"
143 >
144 <XIcon />
145 </Button>
146 </AlertAction>
147 </Alert>
148 </div>
149 );
150});