The best way to control Claude Code, Codex and more from any computer or phone

main
ts 113 lines 4.57 KB
Raw
1 import { createChildThreadApprovalsAtom } from "@t3tools/client-runtime/state/thread-requests";
2 import { useAtomValue } from "@effect/atom-react";
3 import {
4 createEnvironmentThreadDetailAtoms,
5 createEnvironmentThreadShellAtoms,
6 createEnvironmentThreadStateAtoms,
7 EMPTY_ENVIRONMENT_THREAD_STATE,
8 type EnvironmentThreadState,
9 createOlderSettledThreadAtoms,
10 createThreadEnvironmentAtoms,
11 createThreadFamilyAtoms,
12 } from "@t3tools/client-runtime/state/threads";
13 import { createSteadyShellAtoms } from "@t3tools/client-runtime/state/shell";
14 import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
15 import { AsyncResult, Atom } from "effect/reactivity";
16
17 import { environmentCatalog } from "../connection/catalog";
18 import { connectionAtomRuntime } from "../connection/runtime";
19 import { scopedThreadKey } from "../lib/scopedEntities";
20 import { appAtomRegistry } from "./atom-registry";
21 import {
22 pendingThreadCreationOutcomesAtom,
23 selectThreadCreationDetail,
24 } from "./pending-thread-creation";
25 import { serverEnvironment } from "./server";
26 import { environmentShell } from "./shell";
27 import { threadOutboxManager } from "./thread-outbox";
28
29 export const threadFamilies = createThreadFamilyAtoms(
30 connectionAtomRuntime,
31 environmentShell.stateAtom,
32 );
33 export const olderSettledThreads = createOlderSettledThreadAtoms(
34 connectionAtomRuntime,
35 environmentShell.stateAtom,
36 );
37 export const threadEnvironment = createThreadEnvironmentAtoms(
38 connectionAtomRuntime,
39 threadFamilies.snapshotAtom,
40 );
41 export const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
42 export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
43 environmentThreads.stateAtom,
44 );
45 export const environmentThreadShells = createEnvironmentThreadShellAtoms({
46 catalogValueAtom: environmentCatalog.catalogValueAtom,
47 snapshotAtom: threadEnvironment.snapshotAtom,
48 hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
49 });
50 const steadyShells = createSteadyShellAtoms({
51 catalogValueAtom: environmentCatalog.catalogValueAtom,
52 snapshotAtom: threadEnvironment.snapshotAtom,
53 shellStateAtom: environmentShell.stateValueAtom,
54 rowActionsAtom: threadEnvironment.rowActionsAtom,
55 connectionStateAtom: environmentCatalog.stateAtom,
56 });
57 /**
58 * The threads that lists arrange. Rows wait while their environment synchronizes, so a list
59 * changes once when environments connect, not once per environment.
60 */
61 export const steadyShellsHoldingAtom = steadyShells.holdingAtom;
62 export const steadyNavigationThreadShellsAtom = createEnvironmentThreadShellAtoms({
63 catalogValueAtom: environmentCatalog.catalogValueAtom,
64 snapshotAtom: steadyShells.snapshotAtom,
65 hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
66 }).navigationThreadShellsAtom;
67 export const childThreadApprovalsAtom = createChildThreadApprovalsAtom({
68 childThreadInputsAtom: environmentThreadShells.childThreadInputsAtom,
69 pendingRequestsAtom: environmentThreadDetails.pendingRequestsAtom,
70 });
71
72 // Long enough for the opened screen to subscribe; its subscription keeps the thread after this.
73 const OPENING_THREAD_HOLD_MS = 3_000;
74
75 /**
76 * Starts loading a thread being opened before its screen renders and subscribes, which
77 * otherwise waits for navigation to commit.
78 */
79 export function startOpeningEnvironmentThread(environmentId: EnvironmentId, threadId: ThreadId) {
80 // A thread whose creation is still queued would load as not found and stay deleted;
81 // the opened screen loads it once creation is delivered.
82 const { detailRef } = selectThreadCreationDetail({
83 threadRef: { environmentId, threadId },
84 threadKey: scopedThreadKey(environmentId, threadId),
85 queuedMessagesByThreadKey: appAtomRegistry.get(
86 threadOutboxManager.queuedMessagesByThreadKeyAtom,
87 ),
88 outcomes: appAtomRegistry.get(pendingThreadCreationOutcomesAtom),
89 });
90 if (detailRef === null) return;
91 const release = appAtomRegistry.mount(environmentThreads.stateAtom(environmentId, threadId));
92 setTimeout(release, OPENING_THREAD_HOLD_MS);
93 }
94
95 const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
96 Atom.withLabel("mobile-environment-thread:empty"),
97 );
98
99 export function useEnvironmentThread(
100 environmentId: EnvironmentId | null,
101 threadId: ThreadId | null,
102 ): EnvironmentThreadState {
103 const result = useAtomValue(
104 environmentId !== null && threadId !== null
105 ? environmentThreads.stateAtom(environmentId, threadId)
106 : EMPTY_THREAD_STATE_ATOM,
107 );
108 const state = AsyncResult.getOrElse(
109 result,
110 () => EMPTY_ENVIRONMENT_THREAD_STATE,
111 ) as EnvironmentThreadState;
112 return state;
113 }