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