|
| 1 | +import type { EnvironmentId, OrchestrationThreadShell } from "@t3tools/contracts"; |
| 2 | +import { deriveUsageLimitRecovery, usageLimitResumeAt } from "@t3tools/shared/usageLimitRecovery"; |
| 3 | +import * as Cause from "effect/Cause"; |
| 4 | +import * as Option from "effect/Option"; |
| 5 | +import { useState } from "react"; |
| 6 | +import { Alert, Pressable, View } from "react-native"; |
| 7 | + |
| 8 | +import { AppText as Text } from "../../components/AppText"; |
| 9 | +import { useAtomValue } from "@effect/atom-react"; |
| 10 | +import { environmentServerConfigsAtom } from "../../state/server"; |
| 11 | +import { threadEnvironment } from "../../state/threads"; |
| 12 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 13 | +import { useThreadDetail } from "../../state/use-thread-detail"; |
| 14 | + |
| 15 | +const RESET_TIME_FORMATTER = new Intl.DateTimeFormat(undefined, { |
| 16 | + weekday: "short", |
| 17 | + hour: "numeric", |
| 18 | + minute: "2-digit", |
| 19 | +}); |
| 20 | + |
| 21 | +/** |
| 22 | + * Resume actions for a turn stopped on a usage limit. Continuing on another |
| 23 | + * account needs a chat fork, which mobile does not have; that stays on web and |
| 24 | + * desktop. |
| 25 | + */ |
| 26 | +export function UsageLimitRecoveryNotice({ |
| 27 | + environmentId, |
| 28 | + thread, |
| 29 | +}: { |
| 30 | + environmentId: EnvironmentId; |
| 31 | + thread: OrchestrationThreadShell; |
| 32 | +}) { |
| 33 | + const state = useThreadDetail({ environmentId, threadId: thread.id }); |
| 34 | + const detail = Option.getOrNull(state.data); |
| 35 | + const supported = |
| 36 | + useAtomValue(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities |
| 37 | + .threadUsageLimitResume === true; |
| 38 | + const resume = useAtomCommand(threadEnvironment.resumeAfterUsageLimit, { reportFailure: false }); |
| 39 | + const [busy, setBusy] = useState(false); |
| 40 | + const recovery = deriveUsageLimitRecovery({ |
| 41 | + activities: detail?.activities ?? [], |
| 42 | + latestUserMessageAt: thread.latestUserMessageAt, |
| 43 | + sessionStatus: thread.session?.status ?? null, |
| 44 | + }); |
| 45 | + if (!recovery || !supported) return null; |
| 46 | + |
| 47 | + const run = async (action: "now" | "schedule" | "cancel", resumeAt?: string) => { |
| 48 | + setBusy(true); |
| 49 | + try { |
| 50 | + const result = await resume({ |
| 51 | + environmentId, |
| 52 | + input: { |
| 53 | + threadId: thread.id, |
| 54 | + errorActivityId: recovery.errorActivityId, |
| 55 | + action, |
| 56 | + ...(resumeAt ? { resumeAt } : {}), |
| 57 | + }, |
| 58 | + }); |
| 59 | + if (result._tag === "Failure") { |
| 60 | + const error = Cause.squash(result.cause); |
| 61 | + Alert.alert( |
| 62 | + action === "cancel" ? "Could not cancel the resume" : "Could not resume", |
| 63 | + error instanceof Error && error.message.trim().length > 0 |
| 64 | + ? error.message |
| 65 | + : "Try again in a moment.", |
| 66 | + ); |
| 67 | + } |
| 68 | + } finally { |
| 69 | + setBusy(false); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + const nowMs = Date.now(); |
| 74 | + const resetInFuture = recovery.resetsAt !== null && Date.parse(recovery.resetsAt) > nowMs; |
| 75 | + const resumeAt = resetInFuture ? usageLimitResumeAt(recovery.resetsAt, nowMs) : null; |
| 76 | + const status = |
| 77 | + recovery.scheduledResumeAt !== null |
| 78 | + ? `Resumes automatically: ${RESET_TIME_FORMATTER.format(Date.parse(recovery.scheduledResumeAt))}` |
| 79 | + : resetInFuture && recovery.resetsAt !== null |
| 80 | + ? `Limit resets: ${RESET_TIME_FORMATTER.format(Date.parse(recovery.resetsAt))}` |
| 81 | + : null; |
| 82 | + |
| 83 | + return ( |
| 84 | + <View |
| 85 | + accessibilityRole="alert" |
| 86 | + className="mx-3 mb-2 gap-2 rounded-xl border border-border-subtle bg-composer-panel p-3" |
| 87 | + > |
| 88 | + <Text className="text-sm font-t3-medium text-foreground">{recovery.message}</Text> |
| 89 | + {status ? <Text className="text-xs text-foreground-muted">{status}</Text> : null} |
| 90 | + <View className="flex-row flex-wrap gap-2"> |
| 91 | + {recovery.scheduledResumeAt !== null ? ( |
| 92 | + <NoticeButton label="Cancel auto-resume" disabled={busy} onPress={() => run("cancel")} /> |
| 93 | + ) : ( |
| 94 | + <> |
| 95 | + <NoticeButton label="Resume now" disabled={busy} onPress={() => run("now")} /> |
| 96 | + {resumeAt !== null ? ( |
| 97 | + <NoticeButton |
| 98 | + primary |
| 99 | + label="Resume when available" |
| 100 | + disabled={busy} |
| 101 | + onPress={() => run("schedule", resumeAt)} |
| 102 | + /> |
| 103 | + ) : null} |
| 104 | + </> |
| 105 | + )} |
| 106 | + </View> |
| 107 | + </View> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +function NoticeButton(props: { |
| 112 | + label: string; |
| 113 | + primary?: boolean; |
| 114 | + disabled: boolean; |
| 115 | + onPress: () => void; |
| 116 | +}) { |
| 117 | + return ( |
| 118 | + <Pressable |
| 119 | + accessibilityRole="button" |
| 120 | + disabled={props.disabled} |
| 121 | + className={ |
| 122 | + props.primary |
| 123 | + ? "min-h-11 justify-center rounded-lg bg-primary px-3" |
| 124 | + : "min-h-11 justify-center rounded-lg border border-border-subtle px-3" |
| 125 | + } |
| 126 | + onPress={props.onPress} |
| 127 | + > |
| 128 | + <Text |
| 129 | + className={ |
| 130 | + props.primary |
| 131 | + ? "text-sm font-t3-medium text-primary-foreground" |
| 132 | + : "text-sm font-t3-medium text-foreground" |
| 133 | + } |
| 134 | + > |
| 135 | + {props.label} |
| 136 | + </Text> |
| 137 | + </Pressable> |
| 138 | + ); |
| 139 | +} |
0 commit comments