Open History to see the last commit
1import {
2 type EnvironmentId,
3 type ProviderInstanceId,
4 type ServerProvider,
5} from "@t3tools/contracts";
6import { useAtomValue } from "@effect/atom-react";
7import { memo, useRef, useState } from "react";
8import { InfoIcon, XIcon } from "lucide-react";
9import { Alert, AlertAction, AlertDescription, AlertTitle } from "../ui/alert";
10import { Button, InlineButton } from "../ui/button";
11import { formatProviderDriverKindLabel } from "../../providerModels";
12import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
13import { serverEnvironment } from "../../state/server";
14import { useAtomCommand } from "../../state/use-atom-command";
15import { RefreshIcon } from "../ui/refresh-icon";
16
17function getIncompatibleVersion(status: ServerProvider) {
18 const compatibility = status.compatibilityAdvisory;
19 if (status.status === "error" && status.auth.status === "unauthenticated") return null;
20 return compatibility?.status === "broken" ||
21 (status.status === "ready" && compatibility?.status === "unsupported")
22 ? compatibility
23 : null;
24}
25
26export function getProviderStatusBannerKey(status: ServerProvider | null): string | null {
27 if (!status || status.status === "disabled") return null;
28 const incompatible = getIncompatibleVersion(status);
29 if (incompatible) {
30 return [
31 status.instanceId,
32 incompatible.status,
33 status.version ?? "",
34 incompatible.message ?? "",
35 ].join("\u0000");
36 }
37 if (status.status === "ready") return null;
38 // Antigravity validates credentials at session start; unknown restart auth is not failure.
39 if (
40 status.driver === "antigravity" &&
41 status.installed &&
42 status.status === "warning" &&
43 status.auth.status === "unknown"
44 ) {
45 return null;
46 }
47 return [status.instanceId, status.status, status.auth.status, status.message ?? ""].join(
48 "\u0000",
49 );
50}
51
52export function shouldShowProviderStatusBanner(
53 status: ServerProvider | null,
54 dismissedBannerKey: string | null,
55): boolean {
56 const bannerKey = getProviderStatusBannerKey(status);
57 return bannerKey !== null && bannerKey !== dismissedBannerKey;
58}
59
60export function hasProviderSetup(status: ServerProvider): boolean {
61 return (
62 status.driver === "antigravity" ||
63 status.setup?.canAuthenticate === true ||
64 status.setup?.canInstall === true
65 );
66}
67
68export function getProviderStatusMessage(status: ServerProvider): string {
69 if (
70 status.auth.status !== "unauthenticated" &&
71 status.compatibilityAdvisory?.status === "broken" &&
72 status.compatibilityAdvisory.message
73 ) {
74 return status.compatibilityAdvisory.message;
75 }
76 if (status.message) return status.message;
77 const providerName = status.displayName?.trim() || formatProviderDriverKindLabel(status.driver);
78 if (!status.installed && hasProviderSetup(status)) {
79 return `Open provider setup to install ${formatProviderDriverKindLabel(status.driver)} on this environment.`;
80 }
81 if (status.auth.status === "unauthenticated") {
82 if (hasProviderSetup(status)) {
83 return status.driver === "antigravity"
84 ? "Open provider setup to sign in with Google."
85 : "Open provider setup to sign in.";
86 }
87 return "Sign in via the CLI to authenticate again.";
88 }
89 return status.status === "ready"
90 ? "No models are available for this provider."
91 : status.status === "error"
92 ? `${providerName} provider is unavailable.`
93 : `${providerName} provider has limited availability.`;
94}
95
96export const ProviderStatusBanner = memo(function ProviderStatusBanner({
97 environmentId,
98 onDismiss,
99 onOpenProviderSetup,
100 status,
101}: {
102 environmentId: EnvironmentId;
103 onDismiss: () => void;
104 onOpenProviderSetup?: (instanceId: ProviderInstanceId) => void;
105 status: ServerProvider | null;
106}) {
107 const canRefresh = useAtomValue(serverEnvironment.refreshProviders.permissionAtom(environmentId));
108 const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
109 label: "Refresh provider status",
110 });
111 const refreshingRef = useRef(false);
112 const [isRefreshing, setIsRefreshing] = useState(false);
113
114 const refresh = async () => {
115 if (!status || !canRefresh || refreshingRef.current) return;
116 refreshingRef.current = true;
117 setIsRefreshing(true);
118 await refreshProviders({
119 environmentId,
120 input: { instanceId: status.instanceId, refreshModels: true },
121 });
122 refreshingRef.current = false;
123 setIsRefreshing(false);
124 };
125
126 if (!status || getProviderStatusBannerKey(status) === null) {
127 return null;
128 }
129
130 const providerName = status.displayName?.trim() || formatProviderDriverKindLabel(status.driver);
131 const isUnauthenticated = status.status === "error" && status.auth.status === "unauthenticated";
132 const incompatible = getIncompatibleVersion(status);
133 const title = isUnauthenticated
134 ? `${providerName} is unauthenticated`
135 : incompatible
136 ? `${providerName} ${status.version ?? ""} is ${incompatible.status === "broken" ? "known to be broken" : "unsupported"}`
137 : `${providerName} provider status`;
138 const message = incompatible?.message ?? getProviderStatusMessage(status);
139 const isWarning =
140 incompatible?.status !== "broken" && (status.status === "warning" || incompatible !== null);
141
142 return (
143 <div className="pointer-events-auto mx-auto w-fit max-w-[calc(100%-2rem)] pt-3">
144 <Alert
145 variant={isWarning ? "warning" : "error"}
146 role={incompatible && incompatible.status !== "broken" ? "status" : "alert"}
147 surface="glass"
148 controlAlignment="first-line"
149 >
150 <InfoIcon />
151 <AlertTitle>{title}</AlertTitle>
152 <AlertDescription>
153 <div className="flex flex-col gap-2">
154 <Tooltip>
155 <TooltipTrigger render={<div className="line-clamp-3" />}>{message}</TooltipTrigger>
156 <TooltipPopup side="top" className="whitespace-pre-wrap">
157 {message}
158 </TooltipPopup>
159 </Tooltip>
160 <div className="flex flex-wrap items-center justify-end gap-3">
161 {onOpenProviderSetup && hasProviderSetup(status) ? (
162 <InlineButton onClick={() => onOpenProviderSetup(status.instanceId)}>
163 Open provider setup
164 </InlineButton>
165 ) : null}
166 <Button
167 aria-label={`Refresh ${providerName} provider status`}
168 disabled={!canRefresh || isRefreshing}
169 onClick={() => void refresh()}
170 size="xs"
171 variant="outline"
172 >
173 <RefreshIcon />
174 {isRefreshing ? "Refreshing..." : "Refresh"}
175 </Button>
176 </div>
177 </div>
178 </AlertDescription>
179 <AlertAction>
180 <Button
181 aria-label={`Dismiss ${providerName} provider ${status.status}`}
182 onClick={onDismiss}
183 size="icon-xs"
184 variant="ghost-muted"
185 >
186 <XIcon />
187 </Button>
188 </AlertAction>
189 </Alert>
190 </div>
191 );
192});