Open History to see the last commit
1import type { OrchestrationV2ProviderFailureClass } from "@t3tools/contracts";
2import { memo } from "react";
3import { Alert, AlertAction, AlertDescription } from "../ui/alert";
4import { Button } from "../ui/button";
5import { CircleAlertIcon, XIcon } from "lucide-react";
6import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
7import { OpenAI } from "../Icons";
8import { ChatGptUsageButton } from "../settings/ChatGptUsageButton";
9
10export function getThreadErrorBannerKey(threadKey: string, error: string | null): string | null {
11 return error === null ? null : `${threadKey}\u0000${error}`;
12}
13
14export function shouldShowThreadErrorBanner(
15 threadKey: string,
16 error: string | null,
17 isDismissed: boolean,
18): boolean {
19 return getThreadErrorBannerKey(threadKey, error) !== null && !isDismissed;
20}
21
22const sessionDismissedThreadErrorBannerKeys = new Set<string>();
23
24export function dismissThreadErrorBannerForSession(bannerKey: string | null): void {
25 if (bannerKey !== null) {
26 sessionDismissedThreadErrorBannerKeys.add(bannerKey);
27 }
28}
29
30export function isThreadErrorBannerDismissedForSession(bannerKey: string | null): boolean {
31 return bannerKey !== null && sessionDismissedThreadErrorBannerKeys.has(bannerKey);
32}
33
34export const ThreadErrorBanner = memo(function ThreadErrorBanner({
35 error,
36 onDismiss,
37 errorClass,
38 chatGptUsageLimit = false,
39}: {
40 error: string | null;
41 errorClass?: OrchestrationV2ProviderFailureClass | null;
42 onDismiss?: () => void;
43 chatGptUsageLimit?: boolean;
44}) {
45 if (!error) return null;
46 const variant = errorClass === "usage_limit" ? "warning" : "error";
47 return (
48 <div className="pointer-events-auto mx-auto w-fit max-w-[min(48rem,calc(100%-2rem))] pt-3">
49 <Alert variant={variant} surface="glass" controlAlignment="first-line" data-variant={variant}>
50 {chatGptUsageLimit ? (
51 <OpenAI className="size-4 text-foreground!" aria-hidden="true" />
52 ) : (
53 <CircleAlertIcon />
54 )}
55 <AlertDescription>
56 {chatGptUsageLimit ? (
57 <div className="space-y-1">
58 <p className="font-medium">ChatGPT usage limit reached</p>
59 <p>Review your usage settings in ChatGPT to continue.</p>
60 </div>
61 ) : (
62 <Tooltip>
63 <TooltipTrigger render={<div className="line-clamp-3" />}>{error}</TooltipTrigger>
64 <TooltipPopup side="top" className="whitespace-pre-wrap">
65 {error}
66 </TooltipPopup>
67 </Tooltip>
68 )}
69 </AlertDescription>
70 {(chatGptUsageLimit || onDismiss) && (
71 <AlertAction>
72 {chatGptUsageLimit ? <ChatGptUsageButton variant="default" size="sm" /> : null}
73 {onDismiss ? (
74 <Button variant="ghost" size="icon-xs" aria-label="Dismiss error" onClick={onDismiss}>
75 <XIcon />
76 </Button>
77 ) : null}
78 </AlertAction>
79 )}
80 </Alert>
81 </div>
82 );
83});