The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { Atom } from "effect/reactivity"; |
| 3 | |
| 4 | import { appAtomRegistry } from "../../rpc/atomRegistry"; |
| 5 | |
| 6 | /** Threads whose next message starts a fresh provider session, keyed by thread, valued by when it was armed. */ |
| 7 | const freshSessionArmsAtom = Atom.make<Readonly<Record<string, number>>>({}).pipe(Atom.keepAlive); |
| 8 | |
| 9 | export function armFreshSession(threadKey: string): void { |
| 10 | appAtomRegistry.set(freshSessionArmsAtom, { |
| 11 | ...appAtomRegistry.get(freshSessionArmsAtom), |
| 12 | [threadKey]: Date.now(), |
| 13 | }); |
| 14 | } |
| 15 | |
| 16 | /** With `arm`, clears only that arm, so a send never consumes one made while it was in flight. */ |
| 17 | export function disarmFreshSession(threadKey: string, arm?: number): void { |
| 18 | const current = appAtomRegistry.get(freshSessionArmsAtom); |
| 19 | if (current[threadKey] === undefined || (arm !== undefined && current[threadKey] !== arm)) return; |
| 20 | const { [threadKey]: _removed, ...rest } = current; |
| 21 | appAtomRegistry.set(freshSessionArmsAtom, rest); |
| 22 | } |
| 23 | |
| 24 | export function readFreshSessionArm(threadKey: string): number | undefined { |
| 25 | return appAtomRegistry.get(freshSessionArmsAtom)[threadKey]; |
| 26 | } |
| 27 | |
| 28 | export function useFreshSessionArmed(threadKey: string): boolean { |
| 29 | return useAtomValue(freshSessionArmsAtom)[threadKey] !== undefined; |
| 30 | } |