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 {
8 EMPTY_THREAD_HISTORY_META,
9 type EnvironmentThreadStatus,
10 type ThreadHistoryMeta,
11} from "@t3tools/client-runtime/state/threads";
12import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/contracts";
13import type { EnvironmentId, OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts";
14import { Atom } from "effect/reactivity";
15import { appAtomRegistry } from "../rpc/atomRegistry";
16import { environmentProjects } from "./projects";
17import { environmentServerConfigsAtom } from "./server";
18import {
19 allEnvironmentProjectSnapshotsReadyAtom,
20 allEnvironmentShellsBootstrappedAtom,
21} from "./shell";
22import { environmentThreadDetails, environmentThreadShells } from "./threads";
23import { waitForAtomValue } from "./waitForAtomValue";
24
25const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
26const EMPTY_VISIBLE_TURN_ITEMS: ReadonlyArray<OrchestrationV2ProjectedTurnItem> = Object.freeze([]);
27
28const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
29 Atom.withLabel("web-project:empty"),
30);
31const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
32 Atom.withLabel("web-thread-refs:empty"),
33);
34const EMPTY_THREAD_SHELLS_ATOM = Atom.make<ReadonlyArray<EnvironmentThreadShell>>(
35 Object.freeze([]),
36).pipe(Atom.withLabel("web-thread-shells:empty"));
37const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
38 Atom.withLabel("web-thread-shell:empty"),
39);
40const EMPTY_THREAD_PROJECTION_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
41 Atom.withLabel("web-thread-projection:empty"),
42);
43const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe(
44 Atom.withLabel("web-thread-status:empty"),
45);
46const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
47 Atom.withLabel("web-thread-visible-turn-items:empty"),
48);
49const EMPTY_THREAD_HISTORY_ATOM = Atom.make<ThreadHistoryMeta>(EMPTY_THREAD_HISTORY_META).pipe(
50 Atom.withLabel("web-thread-history:empty"),
51);
52
53const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
54 Atom.keepAlive,
55 Atom.withLabel("web-active-environment-id"),
56);
57
58export function useActiveEnvironmentId(): EnvironmentId | null {
59 return useAtomValue(activeEnvironmentIdAtom);
60}
61
62export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
63 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
64}
65
66export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
67 return useAtomValue(environmentThreadShells.threadRefsAtom);
68}
69
70export function useEnvironmentThreadRefs(
71 environmentId: EnvironmentId | null,
72): ReadonlyArray<ScopedThreadRef> {
73 return useAtomValue(
74 environmentId === null
75 ? EMPTY_THREAD_REFS_ATOM
76 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
77 );
78}
79
80export function useProjects(): ReadonlyArray<EnvironmentProject> {
81 return useAtomValue(environmentProjects.projectsAtom);
82}
83
84export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
85 return useAtomValue(environmentServerConfigsAtom);
86}
87
88export function useThreadShells(enabled = true): ReadonlyArray<EnvironmentThreadShell> {
89 return useAtomValue(
90 enabled ? environmentThreadShells.threadShellsAtom : EMPTY_THREAD_SHELLS_ATOM,
91 );
92}
93
94export function useAllEnvironmentShellsBootstrapped(): boolean {
95 return useAtomValue(allEnvironmentShellsBootstrappedAtom);
96}
97
98export function useAllEnvironmentProjectSnapshotsReady(): boolean {
99 return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom);
100}
101
102export function useThreadShellsForProjectRefs(
103 refs: ReadonlyArray<ScopedProjectRef>,
104): ReadonlyArray<EnvironmentThreadShell> {
105 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
106}
107
108export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
109 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
110}
111
112export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
113 return useAtomValue(
114 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
115 );
116}
117
118export function useChildThreadInputs(ref: ScopedThreadRef | null) {
119 return useAtomValue(
120 ref === null ? EMPTY_THREAD_SHELLS_ATOM : environmentThreadShells.childThreadInputsAtom(ref),
121 );
122}
123
124export function useThreadProjection(ref: ScopedThreadRef | null): EnvironmentThread | null {
125 return useAtomValue(
126 ref === null ? EMPTY_THREAD_PROJECTION_ATOM : environmentThreadDetails.threadAtom(ref),
127 );
128}
129
130export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
131 return useAtomValue(
132 ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),
133 );
134}
135
136export function useThreadHistory(ref: ScopedThreadRef | null): ThreadHistoryMeta {
137 return useAtomValue(
138 ref === null ? EMPTY_THREAD_HISTORY_ATOM : environmentThreadDetails.historyAtom(ref),
139 );
140}
141
142export function resolveThreadDetailRef(
143 ref: ScopedThreadRef | null,
144 options: {
145 shellExists: boolean;
146 waitForShell: boolean;
147 },
148): ScopedThreadRef | null {
149 return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
150}
151
152export function useThreadVisibleTurnItems(
153 ref: ScopedThreadRef | null,
154): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
155 return useAtomValue(
156 ref === null
157 ? EMPTY_VISIBLE_TURN_ITEMS_ATOM
158 : environmentThreadDetails.visibleTurnItemsAtom(ref),
159 );
160}
161
162export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
163 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
164}
165
166export function readProjects(): ReadonlyArray<EnvironmentProject> {
167 return appAtomRegistry.get(environmentProjects.projectsAtom);
168}
169
170export function waitForProject(
171 ref: ScopedProjectRef,
172 timeoutMs = 10_000,
173): Promise<EnvironmentProject> {
174 const current = readProject(ref);
175 if (current !== null) return Promise.resolve(current);
176
177 return new Promise((resolve, reject) => {
178 let unsubscribe: (() => void) | null = null;
179 const timeout = setTimeout(() => {
180 unsubscribe?.();
181 reject(new Error("The project did not appear in the desktop app."));
182 }, timeoutMs);
183 const finish = (project: EnvironmentProject | null) => {
184 if (project === null) return;
185 clearTimeout(timeout);
186 unsubscribe?.();
187 resolve(project);
188 };
189 unsubscribe = appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), finish);
190 finish(readProject(ref));
191 });
192}
193
194/**
195 * Resolves once a deleted project leaves the live client store. The delete
196 * command returns before the shell stream does, so anything that picks a
197 * project in between (the index route's draft landing) still sees it.
198 */
199export function waitForProjectRemoval(ref: ScopedProjectRef, timeoutMs = 5_000): Promise<boolean> {
200 return waitForAtomValue({
201 registry: appAtomRegistry,
202 atom: environmentProjects.projectAtom(ref),
203 predicate: (project) => project === null,
204 timeoutMs,
205 });
206}
207
208export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
209 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
210}
211
212export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> {
213 return waitForAtomValue({
214 registry: appAtomRegistry,
215 atom: environmentThreadShells.threadShellAtom(ref),
216 predicate: (thread) => thread !== null,
217 timeoutMs,
218 });
219}
220
221export function useEnvironmentSupportsServerBrowser(environmentId: EnvironmentId | null): boolean {
222 const configs = useServerConfigs();
223 return (
224 environmentId !== null &&
225 configs.get(environmentId)?.environment.capabilities.serverBrowser === true
226 );
227}
228
229export function readEnvironmentSupportsServerBrowser(environmentId: EnvironmentId): boolean {
230 return (
231 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
232 .serverBrowser === true
233 );
234}
235
236export function readEnvironmentSupportsTitleRegeneration(environmentId: EnvironmentId): boolean {
237 return (
238 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
239 .threadTitleRegeneration === true
240 );
241}
242
243export function readEnvironmentSupportsPinning(environmentId: EnvironmentId): boolean {
244 return (
245 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
246 .threadPinning === true
247 );
248}
249
250export function readEnvironmentSupportsPinReorder(environmentId: EnvironmentId): boolean {
251 return (
252 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
253 .threadPinReorder === true
254 );
255}
256
257export function readEnvironmentSupportsAutoSettleOptOut(environmentId: EnvironmentId): boolean {
258 return (
259 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
260 .threadAutoSettleOptOut === true
261 );
262}
263
264export function readEnvironmentSupportsActiveReorder(environmentId: EnvironmentId): boolean {
265 return (
266 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
267 .threadActiveReorder === true
268 );
269}
270
271export function readEnvironmentSupportsSettlement(environmentId: EnvironmentId): boolean {
272 return (
273 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
274 .threadSettlement === true
275 );
276}
277
278export function readEnvironmentSupportsSnooze(environmentId: EnvironmentId): boolean {
279 return (
280 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
281 .threadSnooze === true
282 );
283}
284
285export function readEnvironmentSupportsVisitedTracking(environmentId: EnvironmentId): boolean {
286 return (
287 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
288 .threadVisitedTracking === true
289 );
290}
291
292export function readEnvironmentThreadRefs(
293 environmentId: EnvironmentId,
294): ReadonlyArray<ScopedThreadRef> {
295 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
296}
297
298export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
299 return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
300}