Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderStatusBanner.tsx

138 lines5.2 KB

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