The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { AuthOrchestrationOperateScope } from "@t3tools/contracts"; |
| 3 | import { Atom } from "effect/reactivity"; |
| 4 | import { useMessages } from "gt-react-native"; |
| 5 | import { useMemo } from "react"; |
| 6 | import { threadKey } from "@t3tools/client-runtime/state/entities"; |
| 7 | import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 8 | import { |
| 9 | buildPendingNewTasks, |
| 10 | isQueuedCreationSending, |
| 11 | type PendingNewTask, |
| 12 | type ThreadOutboxSendGate, |
| 13 | } from "./pending-new-tasks-model"; |
| 14 | import { pendingThreadCreationOutcomesAtom, withLaunchingThreads } from "./pending-thread-creation"; |
| 15 | import { flattenQueuedThreadMessages } from "./thread-outbox-model"; |
| 16 | import { environmentThreadShells } from "./threads"; |
| 17 | import { newTaskComposerDraftsAtom } from "./use-composer-drafts"; |
| 18 | import { |
| 19 | retryingQueuedMessageIdsAtom, |
| 20 | threadOutboxShellStatusesAtom, |
| 21 | useThreadOutboxMessages, |
| 22 | } from "./use-thread-outbox"; |
| 23 | import { environmentSession, sessionHasScope } from "./session"; |
| 24 | import { workspaceConnections } from "./workspace"; |
| 25 | |
| 26 | export type { |
| 27 | PendingDraftTask, |
| 28 | PendingNewTask, |
| 29 | PendingQueuedTask, |
| 30 | } from "./pending-new-tasks-model"; |
| 31 | |
| 32 | const threadOutboxSendGateAtom = Atom.make((get): ThreadOutboxSendGate => { |
| 33 | const connected = get(workspaceConnections.environmentsAtom).flatMap((environment) => |
| 34 | environment.connectionState === "connected" ? [environment.environmentId] : [], |
| 35 | ); |
| 36 | return { |
| 37 | connectedEnvironmentIds: new Set(connected), |
| 38 | operableEnvironmentIds: new Set( |
| 39 | connected.filter((environmentId) => |
| 40 | sessionHasScope( |
| 41 | get(environmentSession.sessionStateAtom(environmentId)), |
| 42 | AuthOrchestrationOperateScope, |
| 43 | ), |
| 44 | ), |
| 45 | ), |
| 46 | shellStatuses: get(threadOutboxShellStatusesAtom), |
| 47 | retryingMessageIds: get(retryingQueuedMessageIdsAtom), |
| 48 | }; |
| 49 | }).pipe(Atom.withLabel("mobile:pending-new-tasks:send-gate")); |
| 50 | |
| 51 | export function usePendingNewTasks(): ReadonlyArray<PendingNewTask> { |
| 52 | const resolve = useMessages(); |
| 53 | const queuedMessagesByThreadKey = useThreadOutboxMessages(); |
| 54 | const drafts = useAtomValue(newTaskComposerDraftsAtom); |
| 55 | const hiddenKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom); |
| 56 | const sendGate = useAtomValue(threadOutboxSendGateAtom); |
| 57 | // A delivered creation can stay queued after its thread is live and hidden. |
| 58 | const queuedMessages = flattenQueuedThreadMessages(queuedMessagesByThreadKey).filter( |
| 59 | (message) => !hiddenKeys.has(threadKey(message)), |
| 60 | ); |
| 61 | return buildPendingNewTasks( |
| 62 | { |
| 63 | queuedMessages, |
| 64 | drafts, |
| 65 | sendGate, |
| 66 | }, |
| 67 | resolve, |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** The list's threads, with creations the outbox is sending placed as working. */ |
| 72 | export function useLaunchingThreads( |
| 73 | threads: ReadonlyArray<EnvironmentThreadShell>, |
| 74 | ): ReadonlyArray<EnvironmentThreadShell> { |
| 75 | const queuedMessagesByThreadKey = useThreadOutboxMessages(); |
| 76 | const outcomes = useAtomValue(pendingThreadCreationOutcomesAtom); |
| 77 | const hiddenThreadKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom); |
| 78 | const sendGate = useAtomValue(threadOutboxSendGateAtom); |
| 79 | return useMemo(() => { |
| 80 | const launches = [ |
| 81 | ...flattenQueuedThreadMessages(queuedMessagesByThreadKey).filter((message) => |
| 82 | isQueuedCreationSending(message, sendGate), |
| 83 | ), |
| 84 | ...Object.values(outcomes).flatMap((outcome) => |
| 85 | outcome.kind === "delivered" ? [outcome.message] : [], |
| 86 | ), |
| 87 | ]; |
| 88 | return withLaunchingThreads({ threads, launches, hiddenThreadKeys }); |
| 89 | }, [threads, queuedMessagesByThreadKey, outcomes, hiddenThreadKeys, sendGate]); |
| 90 | } |