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 { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/threads";
8import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/contracts";
9import type { EnvironmentId, OrchestrationV2ProjectedTurnItem, ThreadId } from "@t3tools/contracts";
10import { Atom } from "effect/unstable/reactivity";
11import { appAtomRegistry } from "../rpc/atomRegistry";
12import { environmentProjects } from "./projects";
13import { environmentServerConfigsAtom } from "./server";
14import { allEnvironmentShellsBootstrappedAtom } from "./shell";
15import { environmentThreadDetails, environmentThreadShells } from "./threads";
16import { waitForAtomValue } from "./waitForAtomValue";
17
18const EMPTY_PROJECT_REFS: ReadonlyArray<ScopedProjectRef> = Object.freeze([]);
19const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
20const EMPTY_VISIBLE_TURN_ITEMS: ReadonlyArray<OrchestrationV2ProjectedTurnItem> = Object.freeze([]);
21
22const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
23 Atom.withLabel("web-project:empty"),
24);
25const EMPTY_PROJECT_REFS_ATOM = Atom.make(EMPTY_PROJECT_REFS).pipe(
26 Atom.withLabel("web-project-refs:empty"),
27);
28const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
29 Atom.withLabel("web-thread-refs:empty"),
30);
31const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
32 Atom.withLabel("web-thread-shell:empty"),
33);
34const EMPTY_THREAD_PROJECTION_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
35 Atom.withLabel("web-thread-projection:empty"),
36);
37const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe(
38 Atom.withLabel("web-thread-status:empty"),
39);
40const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
41 Atom.withLabel("web-thread-visible-turn-items:empty"),
42);
43
44export const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
45 Atom.keepAlive,
46 Atom.withLabel("web-active-environment-id"),
47);
48
49export function useActiveEnvironmentId(): EnvironmentId | null {
50 return useAtomValue(activeEnvironmentIdAtom);
51}
52
53export function readActiveEnvironmentId(): EnvironmentId | null {
54 return appAtomRegistry.get(activeEnvironmentIdAtom);
55}
56
57export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
58 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
59}
60
61export function useProjectRefs(): ReadonlyArray<ScopedProjectRef> {
62 return useAtomValue(environmentProjects.projectRefsAtom);
63}
64
65export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
66 return useAtomValue(environmentThreadShells.threadRefsAtom);
67}
68
69export function useEnvironmentProjectRefs(
70 environmentId: EnvironmentId | null,
71): ReadonlyArray<ScopedProjectRef> {
72 return useAtomValue(
73 environmentId === null
74 ? EMPTY_PROJECT_REFS_ATOM
75 : environmentProjects.environmentProjectRefsAtom(environmentId),
76 );
77}
78
79export function useEnvironmentThreadRefs(
80 environmentId: EnvironmentId | null,
81): ReadonlyArray<ScopedThreadRef> {
82 return useAtomValue(
83 environmentId === null
84 ? EMPTY_THREAD_REFS_ATOM
85 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
86 );
87}
88
89export function useProjects(): ReadonlyArray<EnvironmentProject> {
90 return useAtomValue(environmentProjects.projectsAtom);
91}
92
93export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
94 return useAtomValue(environmentServerConfigsAtom);
95}
96
97export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
98 return useAtomValue(environmentThreadShells.threadShellsAtom);
99}
100
101export function useAllEnvironmentShellsBootstrapped(): boolean {
102 return useAtomValue(allEnvironmentShellsBootstrappedAtom);
103}
104
105export function useThreadShellsForProjectRefs(
106 refs: ReadonlyArray<ScopedProjectRef>,
107): ReadonlyArray<EnvironmentThreadShell> {
108 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
109}
110
111export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
112 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
113}
114
115export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
116 return useAtomValue(
117 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
118 );
119}
120
121export function useThreadProjection(ref: ScopedThreadRef | null): EnvironmentThread | null {
122 return useAtomValue(
123 ref === null ? EMPTY_THREAD_PROJECTION_ATOM : environmentThreadDetails.threadAtom(ref),
124 );
125}
126
127export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
128 return useAtomValue(
129 ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),
130 );
131}
132
133export function resolveThreadDetailRef(
134 ref: ScopedThreadRef | null,
135 options: {
136 shellExists: boolean;
137 waitForShell: boolean;
138 },
139): ScopedThreadRef | null {
140 return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
141}
142
143export function useThreadVisibleTurnItems(
144 ref: ScopedThreadRef | null,
145): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
146 return useAtomValue(
147 ref === null
148 ? EMPTY_VISIBLE_TURN_ITEMS_ATOM
149 : environmentThreadDetails.visibleTurnItemsAtom(ref),
150 );
151}
152
153export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
154 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
155}
156
157export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
158 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
159}
160
161export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> {
162 return waitForAtomValue({
163 registry: appAtomRegistry,
164 atom: environmentThreadShells.threadShellAtom(ref),
165 predicate: (thread) => thread !== null,
166 timeoutMs,
167 });
168}
169
170export function readThreadProjection(ref: ScopedThreadRef): EnvironmentThread | null {
171 return appAtomRegistry.get(environmentThreadDetails.threadAtom(ref));
172}
173
174/** Whether the environment's server understands thread.settle/unsettle.
175 False for pre-settlement servers (capability defaults false on decode),
176 so clients under version skew fall back instead of erroring. */
177export function readEnvironmentSupportsSettlement(environmentId: EnvironmentId): boolean {
178 return (
179 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
180 .threadSettlement === true
181 );
182}
183
184/** Whether the environment's server understands thread.snooze/unsnooze.
185 Same version-skew contract as settlement. */
186export function readEnvironmentSupportsSnooze(environmentId: EnvironmentId): boolean {
187 return (
188 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
189 .threadSnooze === true
190 );
191}
192
193/** Whether the environment's server understands thread.visit/mark-unread and
194 projects lastVisitedAt on thread shells. Same version-skew contract as
195 settlement: against older servers, clients keep the browser-local visited
196 state instead. */
197export function readEnvironmentSupportsVisitedTracking(environmentId: EnvironmentId): boolean {
198 return (
199 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
200 .threadVisitedTracking === true
201 );
202}
203
204export function readEnvironmentThreadRefs(
205 environmentId: EnvironmentId,
206): ReadonlyArray<ScopedThreadRef> {
207 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
208}
209
210export function readThreadRefs(): ReadonlyArray<ScopedThreadRef> {
211 return appAtomRegistry.get(environmentThreadShells.threadRefsAtom);
212}
213
214export function findThreadRef(threadId: ThreadId): ScopedThreadRef | null {
215 return (
216 appAtomRegistry
217 .get(environmentThreadShells.threadRefsAtom)
218 .find((ref) => ref.threadId === threadId) ?? null
219 );
220}