The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { T, msg, useGT } from "gt-react"; |
| 2 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 3 | import { m, type MessageResolver } from "~/i18n/messages"; |
| 4 | import { |
| 5 | type OrchestrationV2LimitRecovery, |
| 6 | type OrchestrationV2LimitRecoveryUpdate, |
| 7 | type RunId, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { type TimestampFormat } from "@t3tools/contracts/settings"; |
| 10 | import { deadlineStore } from "@t3tools/client-runtime/clock"; |
| 11 | import { limitRecoveryOwnsSnooze } from "@t3tools/client-runtime/state/thread-execution"; |
| 12 | import { |
| 13 | CHATGPT_USAGE_LIMIT_CODE, |
| 14 | CHATGPT_WORKSPACE_USAGE_LIMIT_CODE, |
| 15 | } from "@t3tools/shared/usageLimits"; |
| 16 | import { GaugeIcon } from "lucide-react"; |
| 17 | import { useState } from "react"; |
| 18 | import { useDeadlinePassed } from "../../hooks/useDeadline"; |
| 19 | import { OpenAI } from "../Icons"; |
| 20 | import { ChatGptUsageButton } from "../settings/ChatGptUsageButton"; |
| 21 | import { Button, InlineButton } from "../ui/button"; |
| 22 | import { formatUpcomingTimestamp } from "../../timestampFormat"; |
| 23 | import { toastManager } from "../ui/toast"; |
| 24 | import type { ComposerBannerStackItem } from "./ComposerBannerStack"; |
| 25 | import { copyAdminRequest } from "./ThreadErrorBanner"; |
| 26 | |
| 27 | type RecoveryProps = { |
| 28 | runId: RunId; |
| 29 | errorCode: string | null; |
| 30 | resetAt: string | null; |
| 31 | stoppedAt: string; |
| 32 | settled: boolean; |
| 33 | requestPending: boolean; |
| 34 | snoozedUntil: string | null; |
| 35 | recovery: OrchestrationV2LimitRecovery | null; |
| 36 | timestampFormat: TimestampFormat; |
| 37 | resetPassed?: boolean; |
| 38 | displayPassed?: boolean; |
| 39 | onChange: (recovery: OrchestrationV2LimitRecoveryUpdate) => Promise<void>; |
| 40 | }; |
| 41 | |
| 42 | function parseEpochMs(isoDate: string | null): number | null { |
| 43 | if (isoDate === null) return null; |
| 44 | const parsed = Date.parse(isoDate); |
| 45 | return Number.isFinite(parsed) ? parsed : null; |
| 46 | } |
| 47 | |
| 48 | function limitRecoveryScheduleState( |
| 49 | props: Pick< |
| 50 | RecoveryProps, |
| 51 | "runId" | "resetAt" | "stoppedAt" | "settled" | "requestPending" | "recovery" | "snoozedUntil" |
| 52 | >, |
| 53 | ) { |
| 54 | // The server refuses recovery changes while a request is pending. |
| 55 | const canSchedule = |
| 56 | !props.settled && |
| 57 | !props.requestPending && |
| 58 | props.resetAt !== null && |
| 59 | Date.parse(props.resetAt) > Date.parse(props.stoppedAt); |
| 60 | const resumes = |
| 61 | canSchedule && |
| 62 | props.recovery?.runId === props.runId && |
| 63 | props.recovery.resetAt === props.resetAt && |
| 64 | props.recovery.autoResume; |
| 65 | const displayAt = |
| 66 | resumes && |
| 67 | props.resetAt && |
| 68 | props.snoozedUntil && |
| 69 | Date.parse(props.snoozedUntil) > Date.parse(props.resetAt) |
| 70 | ? props.snoozedUntil |
| 71 | : props.resetAt; |
| 72 | return { canSchedule, resumes, displayAt }; |
| 73 | } |
| 74 | |
| 75 | export function useUsageLimitRecoveryBannerItem( |
| 76 | props: RecoveryProps | null, |
| 77 | resolve: MessageResolver = m, |
| 78 | ): ComposerBannerStackItem | null { |
| 79 | const displayAt = props === null ? null : limitRecoveryScheduleState(props).displayAt; |
| 80 | const resetPassed = useDeadlinePassed(props === null ? null : parseEpochMs(props.resetAt)); |
| 81 | const displayPassed = useDeadlinePassed(parseEpochMs(displayAt)); |
| 82 | if (props === null) return null; |
| 83 | return usageLimitRecoveryBannerItem({ ...props, resetPassed, displayPassed }, resolve); |
| 84 | } |
| 85 | |
| 86 | export function usageLimitRecoveryBannerItem( |
| 87 | props: RecoveryProps, |
| 88 | resolve: MessageResolver = m, |
| 89 | ): ComposerBannerStackItem { |
| 90 | const { runId, errorCode, resetAt, timestampFormat } = props; |
| 91 | const { canSchedule, resumes, displayAt } = limitRecoveryScheduleState(props); |
| 92 | const resetAtMs = parseEpochMs(resetAt); |
| 93 | const displayAtMs = parseEpochMs(displayAt); |
| 94 | const resetPassed = props.resetPassed ?? (resetAtMs !== null && resetAtMs <= Date.now()); |
| 95 | const displayPassed = props.displayPassed ?? (displayAtMs !== null && displayAtMs <= Date.now()); |
| 96 | const workspaceLimit = errorCode === CHATGPT_WORKSPACE_USAGE_LIMIT_CODE; |
| 97 | const chatGptLimit = workspaceLimit || errorCode === CHATGPT_USAGE_LIMIT_CODE; |
| 98 | const recoveryActions = canSchedule ? ( |
| 99 | <RecoveryActions key={`${runId}:${resetAt}`} {...props} /> |
| 100 | ) : null; |
| 101 | const resumesUpcoming = resumes && !displayPassed; |
| 102 | const shownAt = resumesUpcoming ? displayAt : resetAt; |
| 103 | const formattedTime = shownAt |
| 104 | ? formatUpcomingTimestamp(shownAt, timestampFormat, undefined, undefined, resolve) |
| 105 | : null; |
| 106 | const resetDescription = |
| 107 | formattedTime !== null |
| 108 | ? resumesUpcoming |
| 109 | ? resolve(msg("Resumes {time}", { time: formattedTime })) |
| 110 | : resetPassed |
| 111 | ? resolve(msg("Reset {time}", { time: formattedTime })) |
| 112 | : resolve(msg("Resets {time}", { time: formattedTime })) |
| 113 | : resolve(msg("Reset time unavailable; retry manually")); |
| 114 | if (!chatGptLimit) { |
| 115 | return { |
| 116 | id: `usage-limit-recovery:${runId}`, |
| 117 | variant: "warning", |
| 118 | group: "environment", |
| 119 | icon: <GaugeIcon />, |
| 120 | title: resolve(msg("Usage limit reached")), |
| 121 | description: resetDescription, |
| 122 | actions: recoveryActions, |
| 123 | }; |
| 124 | } |
| 125 | // OpenAI's partner guidelines specify this ChatGPT notice and its actions. |
| 126 | return { |
| 127 | id: `usage-limit-recovery:${runId}`, |
| 128 | variant: "warning", |
| 129 | group: "environment", |
| 130 | icon: <OpenAI aria-hidden="true" />, |
| 131 | title: resolve(msg("ChatGPT usage limit reached")), |
| 132 | description: workspaceLimit |
| 133 | ? resolve( |
| 134 | msg("You've reached your workspace's limit for using your ChatGPT plan in other apps."), |
| 135 | ) |
| 136 | : resetDescription, |
| 137 | actions: ( |
| 138 | <> |
| 139 | {recoveryActions} |
| 140 | {workspaceLimit ? ( |
| 141 | <Button size="xs" variant="ghost" onClick={copyAdminRequest}> |
| 142 | <T>Copy request for admin</T> |
| 143 | </Button> |
| 144 | ) : ( |
| 145 | <ChatGptUsageButton size="xs" variant="ghost" /> |
| 146 | )} |
| 147 | </> |
| 148 | ), |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | function RecoveryActions({ runId, resetAt, recovery, snoozedUntil, onChange }: RecoveryProps) { |
| 153 | const gt = useGT(); |
| 154 | const [pending, setPending] = useState(false); |
| 155 | |
| 156 | const scheduled = |
| 157 | recovery?.runId === runId && recovery.resetAt === resetAt && recovery.autoResume; |
| 158 | // The server leaves snoozedUntil in place after the timer wakes the thread, |
| 159 | // so a snooze whose reset has passed is no longer active. |
| 160 | const resetPassed = useDeadlinePassed(resetAt === null ? null : Date.parse(resetAt)); |
| 161 | const snoozed = |
| 162 | limitRecoveryOwnsSnooze({ runId, resetAt, recovery, snoozedUntil }) && !resetPassed; |
| 163 | async function toggle(action: "resume" | "snooze") { |
| 164 | if (resetAt === null) return; |
| 165 | if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) { |
| 166 | toastManager.add({ |
| 167 | type: "error", |
| 168 | title: m(msg("Could not snooze thread")), |
| 169 | description: m(msg("The reset time has passed. Retry the thread manually.")), |
| 170 | }); |
| 171 | // The deadline timer can lag after a throttled tab or sleep; disable now. |
| 172 | deadlineStore(Date.parse(resetAt)).refresh(); |
| 173 | return; |
| 174 | } |
| 175 | setPending(true); |
| 176 | try { |
| 177 | await onChange({ |
| 178 | runId, |
| 179 | resetAt, |
| 180 | ...(action === "resume" ? { autoResume: !scheduled } : { snooze: !snoozed }), |
| 181 | }); |
| 182 | } catch (cause) { |
| 183 | toastManager.add({ |
| 184 | type: "error", |
| 185 | title: |
| 186 | action === "resume" |
| 187 | ? m(msg("Could not change auto-resume")) |
| 188 | : m(msg("Could not snooze thread")), |
| 189 | description: thrownErrorMessage(cause, m) ?? m(msg("An error occurred.")), |
| 190 | }); |
| 191 | } |
| 192 | setPending(false); |
| 193 | } |
| 194 | // Inline errors would widen the actions column and crush the title. |
| 195 | return ( |
| 196 | <span className="flex flex-wrap items-center gap-x-3 gap-y-1 px-1"> |
| 197 | <InlineButton touchTarget="padded" disabled={pending} onClick={() => void toggle("resume")}> |
| 198 | {pending |
| 199 | ? gt("Saving...") |
| 200 | : scheduled |
| 201 | ? gt("Cancel auto-resume") |
| 202 | : resetPassed |
| 203 | ? gt("Resume now") |
| 204 | : gt("Resume at reset")} |
| 205 | </InlineButton> |
| 206 | <InlineButton |
| 207 | touchTarget="padded" |
| 208 | disabled={pending || (!snoozed && resetPassed)} |
| 209 | onClick={() => void toggle("snooze")} |
| 210 | > |
| 211 | {pending ? gt("Saving...") : snoozed ? gt("Wake now") : gt("Snooze until reset")} |
| 212 | </InlineButton> |
| 213 | </span> |
| 214 | ); |
| 215 | } |