import { Button } from "../../components/ui/Button"; import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import { AsyncResult } from "effect/reactivity"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; import type { EnvironmentId } from "@t3tools/contracts"; import { CHATGPT_USAGE_LIMIT_CODE, CHATGPT_USAGE_URL, CHATGPT_WORKSPACE_ADMIN_REQUEST, CHATGPT_WORKSPACE_USAGE_LIMIT_CODE, } from "@t3tools/shared/usageLimits"; import * as DateTime from "effect/DateTime"; import * as Clipboard from "expo-clipboard"; import { useState } from "react"; import { Linking, View } from "react-native"; import { ProviderIcon } from "../../components/ProviderIcon"; import { AppText as Text } from "../../components/ui/AppText"; import { useDeadlinePassed } from "../../lib/clock"; import { threadEnvironment } from "../../state/threads"; import { useAtomCommand } from "../../state/use-atom-command"; export function UsageLimitRecoveryCard({ thread, environmentId, }: { thread: EnvironmentThreadShell; environmentId: EnvironmentId; }) { const updateMetadata = useAtomCommand(threadEnvironment.updateMetadata); const [pending, setPending] = useState(false); const [error, setError] = useState(null); const resetAt = thread.runtime?.usageLimitResetAt ?? null; const canSchedule = resetAt !== null && Date.parse(resetAt) > Date.parse(thread.latestRun?.completedAt ?? thread.updatedAt); const resetPassed = useDeadlinePassed(resetAt === null ? null : Date.parse(resetAt)); const runId = thread.latestRun?.runId; const recovery = thread.limitRecovery; const scheduled = recovery?.runId === runId && recovery?.resetAt === resetAt && recovery?.autoResume; const [copied, setCopied] = useState(false); const code = thread.runtime?.lastErrorCode; const workspaceLimit = code === CHATGPT_WORKSPACE_USAGE_LIMIT_CODE; const chatGptLimit = workspaceLimit || code === CHATGPT_USAGE_LIMIT_CODE; if ( thread.runtime?.status !== "failed" || // Servers before #25341 report sharing limits as provider errors. (thread.runtime.lastErrorClass !== "usage_limit" && !chatGptLimit) || !runId ) return null; const resetText = resetAt ? `Usage limit resets ${DateTime.toDateUtc(DateTime.makeUnsafe(resetAt)).toLocaleString()}.` : null; const snoozed = recovery?.snooze === true && recovery.runId === runId && recovery.resetAt === resetAt && resetAt !== null && thread.snoozedUntil !== null && Date.parse(thread.snoozedUntil) === Date.parse(resetAt); async function toggle(action: "resume" | "snooze") { if (!resetAt || !runId || !canSchedule) return; if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) { setError("The reset time has passed. Retry the thread manually."); return; } setPending(true); setError(null); const result = await updateMetadata({ environmentId, input: { threadId: thread.id, limitRecovery: { runId, resetAt, ...(action === "resume" ? { autoResume: !scheduled } : { snooze: !snoozed }), }, }, }); setPending(false); if (AsyncResult.isFailure(result)) { const cause = squashAtomCommandFailure(result); setError(cause instanceof Error ? cause.message : "Could not change limit recovery."); } } return ( {chatGptLimit ? ( // OpenAI's partner guidelines specify this ChatGPT notice and its action. <> ChatGPT usage limit reached {workspaceLimit ? copied ? "Request copied. Send it to your workspace admin." : "You've reached your workspace's limit for using your ChatGPT plan in other apps. Copy a request to ask your workspace admin for increased usage limits." : (resetText ?? "Review your usage settings in ChatGPT.")} ) : ( {resetText ?? "The provider did not report a reset time. Retry manually when your limit is available."} )} {canSchedule || chatGptLimit ? ( {canSchedule ? ( <>