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

threads.ts

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