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, useSyncExternalStore } 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 isDismissed: boolean,
55): boolean {
56 return getProviderStatusBannerKey(status) !== null && !isDismissed;
57}
58
59// Built-in instance ids such as `codex` repeat across environments.
60export function getProviderStatusDismissalKey(
61 environmentId: string | undefined,
62 status: ServerProvider | null,
63): string | null {
64 const bannerKey = getProviderStatusBannerKey(status);
65 return bannerKey === null ? null : `${environmentId ?? ""}\u0000${bannerKey}`;
66}
67
68// A dismissal lasts the session, not one view, so opening another thread cannot bring it back.
69const sessionDismissedProviderStatusBannerKeys = new Set<string>();
70const dismissalListeners = new Set<() => void>();
71
72export function dismissProviderStatusBannerForSession(bannerKey: string | null): void {
73 if (bannerKey === null || sessionDismissedProviderStatusBannerKeys.has(bannerKey)) return;
74 sessionDismissedProviderStatusBannerKeys.add(bannerKey);
75 for (const listener of dismissalListeners) listener();
76}
77
78export function isProviderStatusBannerDismissedForSession(bannerKey: string | null): boolean {
79 return bannerKey !== null && sessionDismissedProviderStatusBannerKeys.has(bannerKey);
80}
81
82function subscribeToDismissals(listener: () => void) {
83 dismissalListeners.add(listener);
84 return () => dismissalListeners.delete(listener);
85}
86
87export function useProviderStatusBannerDismissed(bannerKey: string | null): boolean {
88 return useSyncExternalStore(subscribeToDismissals, () =>
89 isProviderStatusBannerDismissedForSession(bannerKey),
90 );
91}
92
93export function hasProviderSetup(status: ServerProvider): boolean {
94 return (
95 status.driver === "antigravity" ||
96 status.setup?.canAuthenticate === true ||
97 status.setup?.canInstall === true
98 );
99}
100
101export function getProviderStatusMessage(status: ServerProvider): string {
102 if (
103 status.auth.status !== "unauthenticated" &&
104 status.compatibilityAdvisory?.status === "broken" &&
105 status.compatibilityAdvisory.message
106 ) {
107 return status.compatibilityAdvisory.message;
108 }
109 if (status.message) return status.message;
110 const providerName = status.displayName?.trim() || formatProviderDriverKindLabel(status.driver);
111 if (!status.installed && hasProviderSetup(status)) {
112 return `Open provider setup to install ${formatProviderDriverKindLabel(status.driver)} on this environment.`;
113 }
114 if (status.auth.status === "unauthenticated") {
115 if (hasProviderSetup(status)) {
116 return status.driver === "antigravity"
117 ? "Open provider setup to sign in with Google."
118 : "Open provider setup to sign in.";
119 }
120 return "Sign in via the CLI to authenticate again.";
121 }
122 return status.status === "ready"
123 ? "No models are available for this provider."
124 : status.status === "error"
125 ? `${providerName} provider is unavailable.`
126 : `${providerName} provider has limited availability.`;
127}
128
129export const ProviderStatusBanner = memo(function ProviderStatusBanner({
130 environmentId,
131 onDismiss,
132 onOpenProviderSetup,
133 status,
134}: {
135 environmentId: EnvironmentId;
136 onDismiss: () => void;
137 onOpenProviderSetup?: (instanceId: ProviderInstanceId) => void;
138 status: ServerProvider | null;
139}) {
140 const canRefresh = useAtomValue(serverEnvironment.refreshProviders.permissionAtom(environmentId));
141 const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
142 label: "Refresh provider status",
143 });
144 const refreshingRef = useRef(false);
145 const [isRefreshing, setIsRefreshing] = useState(false);
146
147 const refresh = async () => {
148 if (!status || !canRefresh || refreshingRef.current) return;
149 refreshingRef.current = true;
150 setIsRefreshing(true);
151 await refreshProviders({
152 environmentId,
153 input: { instanceId: status.instanceId, refreshModels: true },
154 });
155 refreshingRef.current = false;
156 setIsRefreshing(false);
157 };
158
159 if (!status || getProviderStatusBannerKey(status) === null) {
160 return null;
161 }
162
163 const providerName = status.displayName?.trim() || formatProviderDriverKindLabel(status.driver);
164 const isUnauthenticated = status.status === "error" && status.auth.status === "unauthenticated";
165 const incompatible = getIncompatibleVersion(status);
166 const title = isUnauthenticated
167 ? `${providerName} is unauthenticated`
168 : incompatible
169 ? `${providerName} ${status.version ?? ""} is ${incompatible.status === "broken" ? "known to be broken" : "unsupported"}`
170 : `${providerName} provider status`;
171 const message = incompatible?.message ?? getProviderStatusMessage(status);
172 const isWarning =
173 incompatible?.status !== "broken" && (status.status === "warning" || incompatible !== null);
174
175 return (
176 <div className="pointer-events-auto mx-auto w-fit max-w-[calc(100%-2rem)] pt-3">
177 <Alert
178 variant={isWarning ? "warning" : "error"}
179 role={incompatible && incompatible.status !== "broken" ? "status" : "alert"}
180 surface="glass"
181 controlAlignment="first-line"
182 >
183 <InfoIcon />
184 <AlertTitle>{title}</AlertTitle>
185 <AlertDescription>
186 <div className="flex flex-col gap-2">
187 <Tooltip>
188 <TooltipTrigger render={<div className="line-clamp-3" />}>{message}</TooltipTrigger>
189 <TooltipPopup side="top" className="whitespace-pre-wrap">
190 {message}
191 </TooltipPopup>
192 </Tooltip>
193 <div className="flex flex-wrap items-center justify-end gap-3">
194 {onOpenProviderSetup && hasProviderSetup(status) ? (
195 <InlineButton onClick={() => onOpenProviderSetup(status.instanceId)}>
196 Open provider setup
197 </InlineButton>
198 ) : null}
199 <Button
200 aria-label={`Refresh ${providerName} provider status`}
201 disabled={!canRefresh || isRefreshing}
202 onClick={() => void refresh()}
203 size="xs"
204 variant="outline"
205 >
206 <RefreshIcon />
207 {isRefreshing ? "Refreshing..." : "Refresh"}
208 </Button>
209 </div>
210 </div>
211 </AlertDescription>
212 <AlertAction>
213 <Button
214 aria-label={`Dismiss ${providerName} provider ${status.status}`}
215 onClick={onDismiss}
216 size="icon-xs"
217 variant="ghost-muted"
218 >
219 <XIcon />
220 </Button>
221 </AlertAction>
222 </Alert>
223 </div>
224 );
225});