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 // The server leaves snoozedUntil in place after the timer wakes the thread,
50 // so a snooze whose reset has passed is no longer active.
51 const resetPassed = Date.parse(resetAt ?? "") <= nowMs;
52 const snoozed =
53 recovery?.snooze === true &&
54 recovery.runId === runId &&
55 recovery.resetAt === resetAt &&
56 resetAt !== null &&
57 snoozedUntil !== null &&
58 Date.parse(snoozedUntil) === Date.parse(resetAt) &&
59 !resetPassed;
60 async function toggle(action: "resume" | "snooze") {
61 if (resetAt === null) return;
62 if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) {
63 toastManager.add({
64 type: "error",
65 title: "Could not snooze thread",
66 description: "The reset time has passed. Retry the thread manually.",
67 });
68 setNowMs(Date.now());
69 return;
70 }
71 setPending(true);
72 try {
73 await onChange({
74 runId,
75 resetAt,
76 ...(action === "resume" ? { autoResume: !scheduled } : { snooze: !snoozed }),
77 });
78 } catch (cause) {
79 toastManager.add({
80 type: "error",
81 title: action === "resume" ? "Could not change auto-resume" : "Could not snooze thread",
82 description: cause instanceof Error ? cause.message : "An error occurred.",
83 });
84 }
85 setPending(false);
86 }
87 // Inline errors would widen the actions column and crush the title.
88 return (
89 <>
90 <Button size="xs" variant="ghost" disabled={pending} onClick={() => void toggle("resume")}>
91 {pending ? "Saving..." : scheduled ? "Cancel auto-resume" : "Resume at reset"}
92 </Button>
93 <Button
94 size="xs"
95 variant="ghost"
96 disabled={pending || (!snoozed && resetPassed)}
97 onClick={() => void toggle("snooze")}
98 >
99 {pending ? "Saving..." : snoozed ? "Wake now" : "Snooze until reset"}
100 </Button>
101 </>
102 );
103}