Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threads.ts

101 lines3.9 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 createEnvironmentThreadDetailAtoms,
4 createEnvironmentThreadShellAtoms,
5 createEnvironmentThreadStateAtoms,
6 EMPTY_ENVIRONMENT_THREAD_STATE,
7 type EnvironmentThreadState,
8 createOlderSettledThreadAtoms,
9 createThreadEnvironmentAtoms,
10 createThreadFamilyAtoms,
11} from "@t3tools/client-runtime/state/threads";
12import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
13import { AsyncResult, Atom } from "effect/reactivity";
14import { useEffect } from "react";
15​
16import { environmentCatalog } from "../connection/catalog";
17import { connectionAtomRuntime } from "../connection/runtime";
18import { scopedThreadKey } from "../lib/scopedEntities";
19import { appAtomRegistry } from "./atom-registry";
20import {
21 pendingThreadCreationOutcomesAtom,
22 selectThreadCreationDetail,
23} from "./pending-thread-creation";
24import { environmentShell } from "./shell";
25import { threadOutboxManager } from "./thread-outbox";
26​
27export const threadFamilies = createThreadFamilyAtoms(
28 connectionAtomRuntime,
29 environmentShell.stateAtom,
30);
31export const olderSettledThreads = createOlderSettledThreadAtoms(
32 connectionAtomRuntime,
33 environmentShell.stateAtom,
34);
35export const threadEnvironment = createThreadEnvironmentAtoms(
36 connectionAtomRuntime,
37 threadFamilies.snapshotAtom,
38);
39export const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
40export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
41 environmentThreads.stateAtom,
42);
43export const environmentThreadShells = createEnvironmentThreadShellAtoms({
44 catalogValueAtom: environmentCatalog.catalogValueAtom,
45 snapshotAtom: threadEnvironment.snapshotAtom,
46});
47​
48/** Keeps the subagent tree under `rootThreadId` in the thread list while the caller shows it. */
49export function useThreadFamily(
50 environmentId: EnvironmentId | null,
51 rootThreadId: ThreadId | null,
52): void {
53 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mounts the subagent tree under the root thread into the shared thread registry while the caller shows it
54 useEffect(() => {
55 if (environmentId === null || rootThreadId === null) return;
56 return threadFamilies.mount(appAtomRegistry, environmentId, rootThreadId);
57 }, [environmentId, rootThreadId]);
59​
60// Long enough for the opened screen to subscribe; its subscription keeps the thread after this.
61const OPENING_THREAD_HOLD_MS = 3_000;
62​
63/**
64 * Starts loading a thread being opened before its screen renders and subscribes, which
65 * otherwise waits for navigation to commit.
66 */
67export function startOpeningEnvironmentThread(environmentId: EnvironmentId, threadId: ThreadId) {
68 // A thread whose creation is still queued would load as not found and stay deleted;
69 // the opened screen loads it once creation is delivered.
70 const { detailRef } = selectThreadCreationDetail({
71 threadRef: { environmentId, threadId },
72 threadKey: scopedThreadKey(environmentId, threadId),
73 queuedMessagesByThreadKey: appAtomRegistry.get(
74 threadOutboxManager.queuedMessagesByThreadKeyAtom,
75 ),
76 outcomes: appAtomRegistry.get(pendingThreadCreationOutcomesAtom),
77 });
78 if (detailRef === null) return;
79 const release = appAtomRegistry.mount(environmentThreads.stateAtom(environmentId, threadId));
80 setTimeout(release, OPENING_THREAD_HOLD_MS);
82​
83const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
84 Atom.withLabel("mobile-environment-thread:empty"),
85);
86​
87export function useEnvironmentThread(
88 environmentId: EnvironmentId | null,
89 threadId: ThreadId | null,
90): EnvironmentThreadState {
91 const result = useAtomValue(
92 environmentId !== null && threadId !== null
93 ? environmentThreads.stateAtom(environmentId, threadId)
94 : EMPTY_THREAD_STATE_ATOM,
95 );
96 const state = AsyncResult.getOrElse(
97 result,
98 () => EMPTY_ENVIRONMENT_THREAD_STATE,
99 ) as EnvironmentThreadState;
100 return state;