The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | type EnvironmentId, |
| 3 | type ProviderInstanceId, |
| 4 | type ServerProvider, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { useAtomValue } from "@effect/atom-react"; |
| 7 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 8 | import { memo, useRef, useState, useSyncExternalStore } from "react"; |
| 9 | import { InfoIcon, XIcon } from "lucide-react"; |
| 10 | import { Alert, AlertAction, AlertDescription, AlertTitle } from "../ui/alert"; |
| 11 | import { Button, InlineButton } from "../ui/button"; |
| 12 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 13 | import { formatProviderDriverKindLabel } from "../../providerModels"; |
| 14 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 15 | import { serverEnvironment } from "../../state/server"; |
| 16 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 17 | import { RefreshIcon } from "../ui/refresh-icon"; |
| 18 | |
| 19 | function getIncompatibleVersion(status: ServerProvider) { |
| 20 | const compatibility = status.compatibilityAdvisory; |
| 21 | if (status.status === "error" && status.auth.status === "unauthenticated") return null; |
| 22 | return compatibility?.status === "broken" || |
| 23 | (status.status === "ready" && compatibility?.status === "unsupported") |
| 24 | ? compatibility |
| 25 | : null; |
| 26 | } |
| 27 | |
| 28 | export function getProviderStatusBannerKey(status: ServerProvider | null): string | null { |
| 29 | if (!status || status.status === "disabled") return null; |
| 30 | const incompatible = getIncompatibleVersion(status); |
| 31 | if (incompatible) { |
| 32 | return [ |
| 33 | status.instanceId, |
| 34 | incompatible.status, |
| 35 | status.version ?? "", |
| 36 | incompatible.message ?? "", |
| 37 | ].join("\u0000"); |
| 38 | } |
| 39 | if (status.status === "ready") return null; |
| 40 | // Antigravity validates credentials at session start; unknown restart auth is not failure. |
| 41 | if ( |
| 42 | status.driver === "antigravity" && |
| 43 | status.installed && |
| 44 | status.status === "warning" && |
| 45 | status.auth.status === "unknown" |
| 46 | ) { |
| 47 | return null; |
| 48 | } |
| 49 | return [status.instanceId, status.status, status.auth.status, status.message ?? ""].join( |
| 50 | "\u0000", |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | export function shouldShowProviderStatusBanner( |
| 55 | status: ServerProvider | null, |
| 56 | isDismissed: boolean, |
| 57 | ): boolean { |
| 58 | return getProviderStatusBannerKey(status) !== null && !isDismissed; |
| 59 | } |
| 60 | |
| 61 | // Built-in instance ids such as `codex` repeat across environments. |
| 62 | export function getProviderStatusDismissalKey( |
| 63 | environmentId: string | undefined, |
| 64 | status: ServerProvider | null, |
| 65 | ): string | null { |
| 66 | const bannerKey = getProviderStatusBannerKey(status); |
| 67 | return bannerKey === null ? null : `${environmentId ?? ""}\u0000${bannerKey}`; |
| 68 | } |
| 69 | |
| 70 | // A dismissal lasts the session, not one view, so opening another thread cannot bring it back. |
| 71 | const sessionDismissedProviderStatusBannerKeys = new Set<string>(); |
| 72 | const dismissalListeners = new Set<() => void>(); |
| 73 | |
| 74 | export function dismissProviderStatusBannerForSession(bannerKey: string | null): void { |
| 75 | if (bannerKey === null || sessionDismissedProviderStatusBannerKeys.has(bannerKey)) return; |
| 76 | sessionDismissedProviderStatusBannerKeys.add(bannerKey); |
| 77 | for (const listener of dismissalListeners) listener(); |
| 78 | } |
| 79 | |
| 80 | export function isProviderStatusBannerDismissedForSession(bannerKey: string | null): boolean { |
| 81 | return bannerKey !== null && sessionDismissedProviderStatusBannerKeys.has(bannerKey); |
| 82 | } |
| 83 | |
| 84 | function subscribeToDismissals(listener: () => void) { |
| 85 | dismissalListeners.add(listener); |
| 86 | return () => dismissalListeners.delete(listener); |
| 87 | } |
| 88 | |
| 89 | export function useProviderStatusBannerDismissed(bannerKey: string | null): boolean { |
| 90 | return useSyncExternalStore(subscribeToDismissals, () => |
| 91 | isProviderStatusBannerDismissedForSession(bannerKey), |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | export function hasProviderSetup(status: ServerProvider): boolean { |
| 96 | return ( |
| 97 | status.driver === "antigravity" || |
| 98 | status.setup?.canAuthenticate === true || |
| 99 | status.setup?.canInstall === true |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** Components pass `useMessages()` so the message follows a language switch. */ |
| 104 | export function getProviderStatusMessage( |
| 105 | status: ServerProvider, |
| 106 | resolve: MessageResolver = m, |
| 107 | ): string { |
| 108 | if ( |
| 109 | status.auth.status !== "unauthenticated" && |
| 110 | status.compatibilityAdvisory?.status === "broken" && |
| 111 | status.compatibilityAdvisory.message |
| 112 | ) { |
| 113 | return status.compatibilityAdvisory.message; |
| 114 | } |
| 115 | if (status.message) return status.message; |
| 116 | const providerName = status.displayName?.trim() || formatProviderDriverKindLabel(status.driver); |
| 117 | if (!status.installed && hasProviderSetup(status)) { |
| 118 | return resolve( |
| 119 | msg("Open provider setup to install {provider} on this environment.", { |
| 120 | provider: formatProviderDriverKindLabel(status.driver), |
| 121 | }), |
| 122 | ); |
| 123 | } |
| 124 | if (status.auth.status === "unauthenticated") { |
| 125 | if (hasProviderSetup(status)) { |
| 126 | return status.driver === "antigravity" |
| 127 | ? resolve(msg("Open provider setup to sign in with Google.")) |
| 128 | : resolve(msg("Open provider setup to sign in.")); |
| 129 | } |
| 130 | return resolve(msg("Sign in via the CLI to authenticate again.")); |
| 131 | } |
| 132 | return status.status === "ready" |
| 133 | ? resolve(msg("No models are available for this provider.")) |
| 134 | : status.status === "error" |
| 135 | ? resolve(msg("{provider} provider is unavailable.", { provider: providerName })) |
| 136 | : resolve(msg("{provider} provider has limited availability.", { provider: providerName })); |
| 137 | } |
| 138 | |
| 139 | export const ProviderStatusBanner = memo(function ProviderStatusBanner({ |
| 140 | environmentId, |
| 141 | onDismiss, |
| 142 | onOpenProviderSetup, |
| 143 | status, |
| 144 | }: { |
| 145 | environmentId: EnvironmentId; |
| 146 | onDismiss: () => void; |
| 147 | onOpenProviderSetup?: (instanceId: ProviderInstanceId) => void; |
| 148 | status: ServerProvider | null; |
| 149 | }) { |
| 150 | const gt = useGT(); |
| 151 | const messages = useMessages(); |
| 152 | const canRefresh = useAtomValue(serverEnvironment.refreshProviders.permissionAtom(environmentId)); |
| 153 | const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, { |
| 154 | label: "Refresh provider status", |
| 155 | }); |
| 156 | const refreshingRef = useRef(false); |
| 157 | const [isRefreshing, setIsRefreshing] = useState(false); |
| 158 | |
| 159 | const refresh = async () => { |
| 160 | if (!status || !canRefresh || refreshingRef.current) return; |
| 161 | refreshingRef.current = true; |
| 162 | setIsRefreshing(true); |
| 163 | await refreshProviders({ |
| 164 | environmentId, |
| 165 | input: { instanceId: status.instanceId, refreshModels: true }, |
| 166 | }); |
| 167 | refreshingRef.current = false; |
| 168 | setIsRefreshing(false); |
| 169 | }; |
| 170 | |
| 171 | if (!status || getProviderStatusBannerKey(status) === null) { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | const providerName = status.displayName?.trim() || formatProviderDriverKindLabel(status.driver); |
| 176 | const isUnauthenticated = status.status === "error" && status.auth.status === "unauthenticated"; |
| 177 | const incompatible = getIncompatibleVersion(status); |
| 178 | const title = isUnauthenticated |
| 179 | ? gt("{provider} is unauthenticated", { provider: providerName }) |
| 180 | : incompatible |
| 181 | ? incompatible.status === "broken" |
| 182 | ? gt("{provider} {version} is known to be broken", { |
| 183 | provider: providerName, |
| 184 | version: status.version ?? "", |
| 185 | }) |
| 186 | : gt("{provider} {version} is unsupported", { |
| 187 | provider: providerName, |
| 188 | version: status.version ?? "", |
| 189 | }) |
| 190 | : gt("{provider} provider status", { provider: providerName }); |
| 191 | const message = incompatible?.message ?? getProviderStatusMessage(status, messages); |
| 192 | const isWarning = |
| 193 | incompatible?.status !== "broken" && (status.status === "warning" || incompatible !== null); |
| 194 | |
| 195 | return ( |
| 196 | <div className="pointer-events-auto mx-auto w-fit max-w-[calc(100%-2rem)] pt-3"> |
| 197 | <Alert |
| 198 | variant={isWarning ? "warning" : "error"} |
| 199 | role={incompatible && incompatible.status !== "broken" ? "status" : "alert"} |
| 200 | surface="glass" |
| 201 | controlAlignment="first-line" |
| 202 | > |
| 203 | <InfoIcon /> |
| 204 | <AlertTitle>{title}</AlertTitle> |
| 205 | <AlertDescription> |
| 206 | <div className="flex flex-col gap-2"> |
| 207 | <Tooltip> |
| 208 | <TooltipTrigger render={<div className="line-clamp-3" />}>{message}</TooltipTrigger> |
| 209 | <TooltipPopup side="top" className="whitespace-pre-wrap"> |
| 210 | {message} |
| 211 | </TooltipPopup> |
| 212 | </Tooltip> |
| 213 | <div className="flex flex-wrap items-center justify-end gap-3"> |
| 214 | {onOpenProviderSetup && hasProviderSetup(status) ? ( |
| 215 | <InlineButton onClick={() => onOpenProviderSetup(status.instanceId)}> |
| 216 | <T>Open provider setup</T> |
| 217 | </InlineButton> |
| 218 | ) : null} |
| 219 | <Button |
| 220 | aria-label={gt("Refresh {provider} provider status", { provider: providerName })} |
| 221 | disabled={!canRefresh || isRefreshing} |
| 222 | onClick={() => void refresh()} |
| 223 | size="xs" |
| 224 | variant="outline" |
| 225 | > |
| 226 | <RefreshIcon /> |
| 227 | {isRefreshing ? gt("Refreshing...") : gt("Refresh")} |
| 228 | </Button> |
| 229 | </div> |
| 230 | </div> |
| 231 | </AlertDescription> |
| 232 | <AlertAction> |
| 233 | <Button |
| 234 | aria-label={ |
| 235 | status.status === "error" |
| 236 | ? gt("Dismiss {provider} provider error", { provider: providerName }) |
| 237 | : status.status === "warning" |
| 238 | ? gt("Dismiss {provider} provider warning", { provider: providerName }) |
| 239 | : status.status === "ready" |
| 240 | ? gt("Dismiss {provider} provider ready", { provider: providerName }) |
| 241 | : gt("Dismiss {provider} provider disabled", { provider: providerName }) |
| 242 | } |
| 243 | onClick={onDismiss} |
| 244 | size="icon-xs" |
| 245 | variant="ghost-muted" |
| 246 | > |
| 247 | <XIcon /> |
| 248 | </Button> |
| 249 | </AlertAction> |
| 250 | </Alert> |
| 251 | </div> |
| 252 | ); |
| 253 | }); |