Open History to see the last commit
1import {
2 type OrchestrationV2LimitRecovery,
3 type OrchestrationV2LimitRecoveryUpdate,
4 type RunId,
5} from "@t3tools/contracts";
6import { GaugeIcon } from "lucide-react";
7import { useEffect, useState } from "react";
8import { Button } from "../ui/button";
9import { toastManager } from "../ui/toast";
10import type { ComposerBannerStackItem } from "./ComposerBannerStack";
11
12type RecoveryProps = {
13 runId: RunId;
14 resetAt: string | null;
15 stoppedAt: string;
16 snoozedUntil: string | null;
17 recovery: OrchestrationV2LimitRecovery | null;
18 onChange: (recovery: OrchestrationV2LimitRecoveryUpdate) => Promise<void>;
19};
20
21export function usageLimitRecoveryBannerItem(props: RecoveryProps): ComposerBannerStackItem {
22 const { runId, resetAt, stoppedAt } = props;
23 const canSchedule = resetAt !== null && Date.parse(resetAt) > Date.parse(stoppedAt);
24 return {
25 id: `usage-limit-recovery:${runId}`,
26 variant: "warning",
27 priority: "urgent",
28 icon: <GaugeIcon />,
29 title: "Usage limit reached",
30 description: resetAt
31 ? `Resets ${new Date(resetAt).toLocaleString()}`
32 : "Reset time unavailable; retry manually",
33 actions: canSchedule ? <RecoveryActions key={`${runId}:${resetAt}`} {...props} /> : null,
34 };
35}
36
37function RecoveryActions({ runId, resetAt, recovery, snoozedUntil, onChange }: RecoveryProps) {
38 const [pending, setPending] = useState(false);
39 const [nowMs, setNowMs] = useState(() => Date.now());
40 useEffect(() => {
41 const delay = Date.parse(resetAt ?? "") - Math.max(nowMs, Date.now());
42 if (!Number.isFinite(delay) || delay <= 0) return;
43 const timer = window.setTimeout(() => setNowMs(Date.now()), Math.min(delay + 1, 2_147_483_647));
44 return () => window.clearTimeout(timer);
45 }, [resetAt, nowMs]);
46
47 const scheduled =
48 recovery?.runId === runId && recovery.resetAt === resetAt && recovery.autoResume;
49 const snoozed =
50 recovery?.snooze === true &&
51 recovery.runId === runId &&
52 recovery.resetAt === resetAt &&
53 resetAt !== null &&
54 snoozedUntil !== null &&
55 Date.parse(snoozedUntil) === Date.parse(resetAt);
56 async function toggle(action: "resume" | "snooze") {
57 if (resetAt === null) return;
58 if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) {
59 toastManager.add({
60 type: "error",
61 title: "Could not snooze thread",
62 description: "The reset time has passed. Retry the thread manually.",
63 });
64 setNowMs(Date.now());
65 return;
66 }
67 setPending(true);
68 try {
69 await onChange({
70 runId,
71 resetAt,
72 ...(action === "resume" ? { autoResume: !scheduled } : { snooze: !snoozed }),
73 });
74 } catch (cause) {
75 toastManager.add({
76 type: "error",
77 title: action === "resume" ? "Could not change auto-resume" : "Could not snooze thread",
78 description: cause instanceof Error ? cause.message : "An error occurred.",
79 });
80 }
81 setPending(false);
82 }
83 // Failures go to a toast: the actions column is auto-sized, so inline error
84 // text would widen it and crush the title.
85 return (
86 <>
87 <Button size="xs" variant="ghost" disabled={pending} onClick={() => void toggle("resume")}>
88 {pending ? "Saving..." : scheduled ? "Cancel auto-resume" : "Resume at reset"}
89 </Button>
90 {!snoozed ? (
91 <Button
92 size="xs"
93 variant="ghost"
94 disabled={pending || Date.parse(resetAt!) <= nowMs}
95 onClick={() => void toggle("snooze")}
96 >
97 {pending ? "Saving..." : "Snooze until reset"}
98 </Button>
99 ) : null}
100 </>
101 );
102}