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 priority: "urgent",
33 icon: <GaugeIcon />,
34 title: "Usage limit reached",
35 description: resetAt
36 ? `Resets ${formatUpcomingTimestamp(resetAt, timestampFormat)}`
37 : "Reset time unavailable; retry manually",
38 actions: canSchedule ? <RecoveryActions key={`${runId}:${resetAt}`} {...props} /> : null,
39 };
40}
41
42function RecoveryActions({ runId, resetAt, recovery, snoozedUntil, onChange }: RecoveryProps) {
43 const [pending, setPending] = useState(false);
44
45 const scheduled =
46 recovery?.runId === runId && recovery.resetAt === resetAt && recovery.autoResume;
47 // The server leaves snoozedUntil in place after the timer wakes the thread,
48 // so a snooze whose reset has passed is no longer active.
49 const resetPassed = useDeadlinePassed(resetAt === null ? null : Date.parse(resetAt));
50 const snoozed =
51 recovery?.snooze === true &&
52 recovery.runId === runId &&
53 recovery.resetAt === resetAt &&
54 resetAt !== null &&
55 snoozedUntil !== null &&
56 Date.parse(snoozedUntil) === Date.parse(resetAt) &&
57 !resetPassed;
58 async function toggle(action: "resume" | "snooze") {
59 if (resetAt === null) return;
60 if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) {
61 toastManager.add({
62 type: "error",
63 title: "Could not snooze thread",
64 description: "The reset time has passed. Retry the thread manually.",
65 });
66 // The deadline timer can lag after a throttled tab or sleep; disable now.
67 deadlineStore(Date.parse(resetAt)).refresh();
68 return;
69 }
70 setPending(true);
71 try {
72 await onChange({
73 runId,
74 resetAt,
75 ...(action === "resume" ? { autoResume: !scheduled } : { snooze: !snoozed }),
76 });
77 } catch (cause) {
78 toastManager.add({
79 type: "error",
80 title: action === "resume" ? "Could not change auto-resume" : "Could not snooze thread",
81 description: cause instanceof Error ? cause.message : "An error occurred.",
82 });
83 }
84 setPending(false);
85 }
86 // Inline errors would widen the actions column and crush the title.
87 return (
88 <>
89 <Button size="xs" variant="ghost" disabled={pending} onClick={() => void toggle("resume")}>
90 {pending ? "Saving..." : scheduled ? "Cancel auto-resume" : "Resume at reset"}
91 </Button>
92 <Button
93 size="xs"
94 variant="ghost"
95 disabled={pending || (!snoozed && resetPassed)}
96 onClick={() => void toggle("snooze")}
97 >
98 {pending ? "Saving..." : snoozed ? "Wake now" : "Snooze until reset"}
99 </Button>
100 </>
101 );
102}