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/unstable/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_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
35 Atom.withLabel("web-thread-shell:empty"),
36);
37const EMPTY_THREAD_PROJECTION_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
38 Atom.withLabel("web-thread-projection:empty"),
39);
40const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe(
41 Atom.withLabel("web-thread-status:empty"),
42);
43const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
44 Atom.withLabel("web-thread-visible-turn-items:empty"),
45);
46const EMPTY_THREAD_HISTORY_ATOM = Atom.make<ThreadHistoryMeta>(EMPTY_THREAD_HISTORY_META).pipe(
47 Atom.withLabel("web-thread-history:empty"),
48);
49
50const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
51 Atom.keepAlive,
52 Atom.withLabel("web-active-environment-id"),
53);
54
55export function useActiveEnvironmentId(): EnvironmentId | null {
56 return useAtomValue(activeEnvironmentIdAtom);
57}
58
59export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
60 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
61}
62
63export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
64 return useAtomValue(environmentThreadShells.threadRefsAtom);
65}
66
67export function useEnvironmentThreadRefs(
68 environmentId: EnvironmentId | null,
69): ReadonlyArray<ScopedThreadRef> {
70 return useAtomValue(
71 environmentId === null
72 ? EMPTY_THREAD_REFS_ATOM
73 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
74 );
75}
76
77export function useProjects(): ReadonlyArray<EnvironmentProject> {
78 return useAtomValue(environmentProjects.projectsAtom);
79}
80
81export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
82 return useAtomValue(environmentServerConfigsAtom);
83}
84
85export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
86 return useAtomValue(environmentThreadShells.threadShellsAtom);
87}
88
89export function useAllEnvironmentShellsBootstrapped(): boolean {
90 return useAtomValue(allEnvironmentShellsBootstrappedAtom);
91}
92
93export function useAllEnvironmentProjectSnapshotsReady(): boolean {
94 return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom);
95}
96
97export function useThreadShellsForProjectRefs(
98 refs: ReadonlyArray<ScopedProjectRef>,
99): ReadonlyArray<EnvironmentThreadShell> {
100 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
101}
102
103export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
104 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
105}
106
107export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
108 return useAtomValue(
109 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
110 );
111}
112
113export function useThreadProjection(ref: ScopedThreadRef | null): EnvironmentThread | null {
114 return useAtomValue(
115 ref === null ? EMPTY_THREAD_PROJECTION_ATOM : environmentThreadDetails.threadAtom(ref),
116 );
117}
118
119export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
120 return useAtomValue(
121 ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),
122 );
123}
124
125export function useThreadHistory(ref: ScopedThreadRef | null): ThreadHistoryMeta {
126 return useAtomValue(
127 ref === null ? EMPTY_THREAD_HISTORY_ATOM : environmentThreadDetails.historyAtom(ref),
128 );
129}
130
131export function resolveThreadDetailRef(
132 ref: ScopedThreadRef | null,
133 options: {
134 shellExists: boolean;
135 waitForShell: boolean;
136 },
137): ScopedThreadRef | null {
138 return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
139}
140
141export function useThreadVisibleTurnItems(
142 ref: ScopedThreadRef | null,
143): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
144 return useAtomValue(
145 ref === null
146 ? EMPTY_VISIBLE_TURN_ITEMS_ATOM
147 : environmentThreadDetails.visibleTurnItemsAtom(ref),
148 );
149}
150
151export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
152 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
153}
154
155export function readProjects(): ReadonlyArray<EnvironmentProject> {
156 return appAtomRegistry.get(environmentProjects.projectsAtom);
157}
158
159/** Resolves when the project event reaches the live client store. */
160export function waitForProject(
161 ref: ScopedProjectRef,
162 timeoutMs = 10_000,
163): Promise<EnvironmentProject> {
164 const current = readProject(ref);
165 if (current !== null) return Promise.resolve(current);
166
167 return new Promise((resolve, reject) => {
168 let unsubscribe: (() => void) | null = null;
169 const timeout = setTimeout(() => {
170 unsubscribe?.();
171 reject(new Error("The project did not appear in the desktop app."));
172 }, timeoutMs);
173 const finish = (project: EnvironmentProject | null) => {
174 if (project === null) return;
175 clearTimeout(timeout);
176 unsubscribe?.();
177 resolve(project);
178 };
179 unsubscribe = appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), finish);
180 finish(readProject(ref));
181 });
182}
183
184export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
185 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
186}
187
188export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> {
189 return waitForAtomValue({
190 registry: appAtomRegistry,
191 atom: environmentThreadShells.threadShellAtom(ref),
192 predicate: (thread) => thread !== null,
193 timeoutMs,
194 });
195}
196
197export function readEnvironmentSupportsTitleRegeneration(environmentId: EnvironmentId): boolean {
198 return (
199 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
200 .threadTitleRegeneration === true
201 );
202}
203
204/** Whether the environment's server understands thread.pin/unpin.
205 Same version-skew contract as settlement. */
206export function readEnvironmentSupportsPinning(environmentId: EnvironmentId): boolean {
207 return (
208 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
209 .threadPinning === true
210 );
211}
212
213/** Whether the environment's server understands thread.pin.reorder (and
214 orderKey on thread.pin). Same version-skew contract as settlement. */
215export function readEnvironmentSupportsPinReorder(environmentId: EnvironmentId): boolean {
216 return (
217 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
218 .threadPinReorder === true
219 );
220}
221
222/** Whether the environment's server understands thread.auto-settle.set.
223 Same version-skew contract as settlement. */
224export function readEnvironmentSupportsAutoSettleOptOut(environmentId: EnvironmentId): boolean {
225 return (
226 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
227 .threadAutoSettleOptOut === true
228 );
229}
230
231export function readEnvironmentSupportsActiveReorder(environmentId: EnvironmentId): boolean {
232 return (
233 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
234 .threadActiveReorder === true
235 );
236}
237
238/** Whether the environment's server understands thread.settle/unsettle.
239 False for pre-settlement servers (capability defaults false on decode),
240 so clients under version skew fall back instead of erroring. */
241export function readEnvironmentSupportsSettlement(environmentId: EnvironmentId): boolean {
242 return (
243 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
244 .threadSettlement === true
245 );
246}
247
248/** Whether the environment's server understands thread.snooze/unsnooze.
249 Same version-skew contract as settlement. */
250export function readEnvironmentSupportsSnooze(environmentId: EnvironmentId): boolean {
251 return (
252 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
253 .threadSnooze === true
254 );
255}
256
257/** Whether the environment's server understands thread.visit/mark-unread and
258 projects lastVisitedAt on thread shells. Same version-skew contract as
259 settlement: against older servers, clients keep the browser-local visited
260 state instead. */
261export function readEnvironmentSupportsVisitedTracking(environmentId: EnvironmentId): boolean {
262 return (
263 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
264 .threadVisitedTracking === true
265 );
266}
267
268export function readEnvironmentThreadRefs(
269 environmentId: EnvironmentId,
270): ReadonlyArray<ScopedThreadRef> {
271 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
272}
273
274export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
275 return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
276}