Open History to see the last commit
1import {
2 type OrchestrationV2LimitRecovery,
3 type OrchestrationV2LimitRecoveryUpdate,
4 type RunId,
5} from "@t3tools/contracts";
6import { type TimestampFormat } from "@t3tools/contracts/settings";
7import { deadlineStore } from "@t3tools/client-runtime/clock";
8import { GaugeIcon } from "lucide-react";
9import { useState } from "react";
10import { useDeadlinePassed } from "../../hooks/useDeadline";
11import { Button } from "../ui/button";
12import { formatUpcomingTimestamp } from "../../timestampFormat";
13import { toastManager } from "../ui/toast";
14import type { ComposerBannerStackItem } from "./ComposerBannerStack";
15
16type RecoveryProps = {
17 runId: RunId;
18 resetAt: string | null;
19 stoppedAt: string;
20 snoozedUntil: string | null;
21 recovery: OrchestrationV2LimitRecovery | null;
22 timestampFormat: TimestampFormat;
23 onChange: (recovery: OrchestrationV2LimitRecoveryUpdate) => Promise<void>;
24};
25
26export function usageLimitRecoveryBannerItem(props: RecoveryProps): ComposerBannerStackItem {
27 const { runId, resetAt, stoppedAt, timestampFormat } = props;
28 const canSchedule = resetAt !== null && Date.parse(resetAt) > Date.parse(stoppedAt);
29 return {
30 id: `usage-limit-recovery:${runId}`,
31 variant: "warning",
32 icon: <GaugeIcon />,
33 title: "Usage limit reached",
34 description: resetAt
35 ? `Resets ${formatUpcomingTimestamp(resetAt, timestampFormat)}`
36 : "Reset time unavailable; retry manually",
37 actions: canSchedule ? <RecoveryActions key={`${runId}:${resetAt}`} {...props} /> : null,
38 };
39}
40
41function RecoveryActions({ runId, resetAt, recovery, snoozedUntil, onChange }: RecoveryProps) {
42 const [pending, setPending] = useState(false);
43
44 const scheduled =
45 recovery?.runId === runId && recovery.resetAt === resetAt && recovery.autoResume;
46 // The server leaves snoozedUntil in place after the timer wakes the thread,
47 // so a snooze whose reset has passed is no longer active.
48 const resetPassed = useDeadlinePassed(resetAt === null ? null : Date.parse(resetAt));
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 !resetPassed;
57 async function toggle(action: "resume" | "snooze") {
58 if (resetAt === null) return;
59 if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) {
60 toastManager.add({
61 type: "error",
62 title: "Could not snooze thread",
63 description: "The reset time has passed. Retry the thread manually.",
64 });
65 // The deadline timer can lag after a throttled tab or sleep; disable now.
66 deadlineStore(Date.parse(resetAt)).refresh();
67 return;
68 }
69 setPending(true);
70 try {
71 await onChange({
72 runId,
73 resetAt,
74 ...(action === "resume" ? { autoResume: !scheduled } : { snooze: !snoozed }),
75 });
76 } catch (cause) {
77 toastManager.add({
78 type: "error",
79 title: action === "resume" ? "Could not change auto-resume" : "Could not snooze thread",
80 description: cause instanceof Error ? cause.message : "An error occurred.",
81 });
82 }
83 setPending(false);
84 }
85 // Inline errors would widen the actions column and crush the title.
86 return (
87 <>
88 <Button size="xs" variant="ghost" disabled={pending} onClick={() => void toggle("resume")}>
89 {pending ? "Saving..." : scheduled ? "Cancel auto-resume" : "Resume at reset"}
90 </Button>
91 <Button
92 size="xs"
93 variant="ghost"
94 disabled={pending || (!snoozed && resetPassed)}
95 onClick={() => void toggle("snooze")}
96 >
97 {pending ? "Saving..." : snoozed ? "Wake now" : "Snooze until reset"}
98 </Button>
99 </>
100 );
101}