Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type {
3 EnvironmentProject,
4 EnvironmentThread,
5 EnvironmentThreadShell,
6} from "@t3tools/client-runtime/state/shell";
7import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/contracts";
8import type { EnvironmentId, OrchestrationV2ProjectedTurnItem, ThreadId } from "@t3tools/contracts";
9import { Atom } from "effect/unstable/reactivity";
10import { appAtomRegistry } from "../rpc/atomRegistry";
11import { environmentProjects } from "./projects";
12import { environmentServerConfigsAtom } from "./server";
13import { environmentThreadDetails, environmentThreadShells } from "./threads";
14import { waitForAtomValue } from "./waitForAtomValue";
15
16const EMPTY_PROJECT_REFS: ReadonlyArray<ScopedProjectRef> = Object.freeze([]);
17const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
18const EMPTY_VISIBLE_TURN_ITEMS: ReadonlyArray<OrchestrationV2ProjectedTurnItem> = Object.freeze([]);
19
20const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
21 Atom.withLabel("web-project:empty"),
22);
23const EMPTY_PROJECT_REFS_ATOM = Atom.make(EMPTY_PROJECT_REFS).pipe(
24 Atom.withLabel("web-project-refs:empty"),
25);
26const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
27 Atom.withLabel("web-thread-refs:empty"),
28);
29const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
30 Atom.withLabel("web-thread-shell:empty"),
31);
32const EMPTY_THREAD_PROJECTION_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
33 Atom.withLabel("web-thread-projection:empty"),
34);
35const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
36 Atom.withLabel("web-thread-visible-turn-items:empty"),
37);
38
39export const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
40 Atom.keepAlive,
41 Atom.withLabel("web-active-environment-id"),
42);
43
44export function useActiveEnvironmentId(): EnvironmentId | null {
45 return useAtomValue(activeEnvironmentIdAtom);
46}
47
48export function readActiveEnvironmentId(): EnvironmentId | null {
49 return appAtomRegistry.get(activeEnvironmentIdAtom);
50}
51
52export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
53 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
54}
55
56export function useProjectRefs(): ReadonlyArray<ScopedProjectRef> {
57 return useAtomValue(environmentProjects.projectRefsAtom);
58}
59
60export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
61 return useAtomValue(environmentThreadShells.threadRefsAtom);
62}
63
64export function useEnvironmentProjectRefs(
65 environmentId: EnvironmentId | null,
66): ReadonlyArray<ScopedProjectRef> {
67 return useAtomValue(
68 environmentId === null
69 ? EMPTY_PROJECT_REFS_ATOM
70 : environmentProjects.environmentProjectRefsAtom(environmentId),
71 );
72}
73
74export function useEnvironmentThreadRefs(
75 environmentId: EnvironmentId | null,
76): ReadonlyArray<ScopedThreadRef> {
77 return useAtomValue(
78 environmentId === null
79 ? EMPTY_THREAD_REFS_ATOM
80 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
81 );
82}
83
84export function useProjects(): ReadonlyArray<EnvironmentProject> {
85 return useAtomValue(environmentProjects.projectsAtom);
86}
87
88export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
89 return useAtomValue(environmentServerConfigsAtom);
90}
91
92export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
93 return useAtomValue(environmentThreadShells.threadShellsAtom);
94}
95
96export function useThreadShellsForProjectRefs(
97 refs: ReadonlyArray<ScopedProjectRef>,
98): ReadonlyArray<EnvironmentThreadShell> {
99 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
100}
101
102export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
103 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
104}
105
106export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
107 return useAtomValue(
108 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
109 );
110}
111
112export function useThreadProjection(ref: ScopedThreadRef | null): EnvironmentThread | null {
113 return useAtomValue(
114 ref === null ? EMPTY_THREAD_PROJECTION_ATOM : environmentThreadDetails.threadAtom(ref),
115 );
116}
117
118export function useThreadVisibleTurnItems(
119 ref: ScopedThreadRef | null,
120): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
121 return useAtomValue(
122 ref === null
123 ? EMPTY_VISIBLE_TURN_ITEMS_ATOM
124 : environmentThreadDetails.visibleTurnItemsAtom(ref),
125 );
126}
127
128export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
129 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
130}
131
132export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
133 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
134}
135
136export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> {
137 return waitForAtomValue({
138 registry: appAtomRegistry,
139 atom: environmentThreadShells.threadShellAtom(ref),
140 predicate: (thread) => thread !== null,
141 timeoutMs,
142 });
143}
144
145export function readThreadProjection(ref: ScopedThreadRef): EnvironmentThread | null {
146 return appAtomRegistry.get(environmentThreadDetails.threadAtom(ref));
147}
148
149export function readEnvironmentThreadRefs(
150 environmentId: EnvironmentId,
151): ReadonlyArray<ScopedThreadRef> {
152 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
153}
154
155export function readThreadRefs(): ReadonlyArray<ScopedThreadRef> {
156 return appAtomRegistry.get(environmentThreadShells.threadRefsAtom);
157}
158
159export function findThreadRef(threadId: ThreadId): ScopedThreadRef | null {
160 return (
161 appAtomRegistry
162 .get(environmentThreadShells.threadRefsAtom)
163 .find((ref) => ref.threadId === threadId) ?? null
164 );
165}