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 type EnvironmentThreadStatus,
9 mergeEnvironmentThread,
10} from "@t3tools/client-runtime/state/threads";
11import type {
12 OrchestrationMessage,
13 OrchestrationProposedPlan,
14 OrchestrationSession,
15 OrchestrationThreadActivity,
16 ScopedProjectRef,
17 ScopedThreadRef,
18 ServerConfig,
19} from "@t3tools/contracts";
20import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
21import { Atom } from "effect/unstable/reactivity";
22import { useMemo } from "react";
23import { appAtomRegistry } from "../rpc/atomRegistry";
24import { environmentProjects } from "./projects";
25import { environmentServerConfigsAtom } from "./server";
26import { allEnvironmentShellsBootstrappedAtom } from "./shell";
27import { environmentThreadDetails, environmentThreadShells } from "./threads";
28
29const EMPTY_PROJECT_REFS: ReadonlyArray<ScopedProjectRef> = Object.freeze([]);
30const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
31const EMPTY_MESSAGES: ReadonlyArray<OrchestrationMessage> = Object.freeze([]);
32const EMPTY_ACTIVITIES: ReadonlyArray<OrchestrationThreadActivity> = Object.freeze([]);
33const EMPTY_PROPOSED_PLANS: ReadonlyArray<OrchestrationProposedPlan> = Object.freeze([]);
34
35const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
36 Atom.withLabel("web-project:empty"),
37);
38const EMPTY_PROJECT_REFS_ATOM = Atom.make(EMPTY_PROJECT_REFS).pipe(
39 Atom.withLabel("web-project-refs:empty"),
40);
41const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
42 Atom.withLabel("web-thread-refs:empty"),
43);
44const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
45 Atom.withLabel("web-thread-shell:empty"),
46);
47const EMPTY_THREAD_DETAIL_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
48 Atom.withLabel("web-thread-detail:empty"),
49);
50const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe(
51 Atom.withLabel("web-thread-status:empty"),
52);
53const EMPTY_MESSAGES_ATOM = Atom.make(EMPTY_MESSAGES).pipe(
54 Atom.withLabel("web-thread-messages:empty"),
55);
56const EMPTY_ACTIVITIES_ATOM = Atom.make(EMPTY_ACTIVITIES).pipe(
57 Atom.withLabel("web-thread-activities:empty"),
58);
59const EMPTY_PROPOSED_PLANS_ATOM = Atom.make(EMPTY_PROPOSED_PLANS).pipe(
60 Atom.withLabel("web-thread-proposed-plans:empty"),
61);
62const EMPTY_SESSION_ATOM = Atom.make<OrchestrationSession | null>(null).pipe(
63 Atom.withLabel("web-thread-session:empty"),
64);
65
66export const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
67 Atom.keepAlive,
68 Atom.withLabel("web-active-environment-id"),
69);
70
71export function useActiveEnvironmentId(): EnvironmentId | null {
72 return useAtomValue(activeEnvironmentIdAtom);
73}
74
75export function readActiveEnvironmentId(): EnvironmentId | null {
76 return appAtomRegistry.get(activeEnvironmentIdAtom);
77}
78
79export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
80 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
81}
82
83export function useProjectRefs(): ReadonlyArray<ScopedProjectRef> {
84 return useAtomValue(environmentProjects.projectRefsAtom);
85}
86
87export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
88 return useAtomValue(environmentThreadShells.threadRefsAtom);
89}
90
91export function useEnvironmentProjectRefs(
92 environmentId: EnvironmentId | null,
93): ReadonlyArray<ScopedProjectRef> {
94 return useAtomValue(
95 environmentId === null
96 ? EMPTY_PROJECT_REFS_ATOM
97 : environmentProjects.environmentProjectRefsAtom(environmentId),
98 );
99}
100
101export function useEnvironmentThreadRefs(
102 environmentId: EnvironmentId | null,
103): ReadonlyArray<ScopedThreadRef> {
104 return useAtomValue(
105 environmentId === null
106 ? EMPTY_THREAD_REFS_ATOM
107 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
108 );
109}
110
111export function useProjects(): ReadonlyArray<EnvironmentProject> {
112 return useAtomValue(environmentProjects.projectsAtom);
113}
114
115export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
116 return useAtomValue(environmentServerConfigsAtom);
117}
118
119export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
120 return useAtomValue(environmentThreadShells.threadShellsAtom);
121}
122
123export function useAllEnvironmentShellsBootstrapped(): boolean {
124 return useAtomValue(allEnvironmentShellsBootstrappedAtom);
125}
126
127export function useThreadShellsForProjectRefs(
128 refs: ReadonlyArray<ScopedProjectRef>,
129): ReadonlyArray<EnvironmentThreadShell> {
130 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
131}
132
133export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
134 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
135}
136
137export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
138 return useAtomValue(
139 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
140 );
141}
142
143export function useThreadDetail(ref: ScopedThreadRef | null): EnvironmentThread | null {
144 return useAtomValue(
145 ref === null ? EMPTY_THREAD_DETAIL_ATOM : environmentThreadDetails.detailAtom(ref),
146 );
147}
148
149export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
150 return useAtomValue(
151 ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),
152 );
153}
154
155export function resolveThreadDetailRef(
156 ref: ScopedThreadRef | null,
157 options: {
158 shellExists: boolean;
159 waitForShell: boolean;
160 },
161): ScopedThreadRef | null {
162 return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
163}
164
165/** Detail collections composed with shell-authoritative thread/workspace metadata. */
166export function useThread(
167 ref: ScopedThreadRef | null,
168 options?: {
169 /**
170 * Client-reserved draft thread ids do not exist on the server until the
171 * first send. Waiting for the shell index avoids polling the detail
172 * endpoint for an intentionally missing thread during that window.
173 */
174 waitForShell?: boolean;
175 },
176): EnvironmentThread | null {
177 const shell = useThreadShell(ref);
178 const detail = useThreadDetail(
179 resolveThreadDetailRef(ref, {
180 shellExists: shell !== null,
181 waitForShell: options?.waitForShell === true,
182 }),
183 );
184 return useMemo(() => mergeEnvironmentThread(detail, shell), [detail, shell]);
185}
186
187export function useThreadMessages(
188 ref: ScopedThreadRef | null,
189): ReadonlyArray<OrchestrationMessage> {
190 return useAtomValue(
191 ref === null ? EMPTY_MESSAGES_ATOM : environmentThreadDetails.messagesAtom(ref),
192 );
193}
194
195export function useThreadActivities(
196 ref: ScopedThreadRef | null,
197): ReadonlyArray<OrchestrationThreadActivity> {
198 return useAtomValue(
199 ref === null ? EMPTY_ACTIVITIES_ATOM : environmentThreadDetails.activitiesAtom(ref),
200 );
201}
202
203export function useThreadProposedPlans(
204 ref: ScopedThreadRef | null,
205): ReadonlyArray<OrchestrationProposedPlan> {
206 return useAtomValue(
207 ref === null ? EMPTY_PROPOSED_PLANS_ATOM : environmentThreadDetails.proposedPlansAtom(ref),
208 );
209}
210
211export function useThreadSession(ref: ScopedThreadRef | null): OrchestrationSession | null {
212 return useAtomValue(
213 ref === null ? EMPTY_SESSION_ATOM : environmentThreadDetails.sessionAtom(ref),
214 );
215}
216
217export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
218 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
219}
220
221export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
222 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
223}
224
225/** Whether the environment's server understands thread.settle/unsettle.
226 False for pre-settlement servers (capability defaults false on decode),
227 so clients under version skew fall back instead of erroring. */
228export function readEnvironmentSupportsSettlement(environmentId: EnvironmentId): boolean {
229 return (
230 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
231 .threadSettlement === true
232 );
233}
234
235/** Whether the environment's server understands thread.snooze/unsnooze.
236 Same version-skew contract as settlement. */
237export function readEnvironmentSupportsSnooze(environmentId: EnvironmentId): boolean {
238 return (
239 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
240 .threadSnooze === true
241 );
242}
243
244/** Whether the environment's server understands thread.pin/unpin.
245 Same version-skew contract as settlement. */
246export function readEnvironmentSupportsPinning(environmentId: EnvironmentId): boolean {
247 return (
248 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
249 .threadPinning === true
250 );
251}
252
253/** Whether the environment's server understands thread title regeneration.
254 Same version-skew contract as settlement. */
255export function readEnvironmentSupportsTitleRegeneration(environmentId: EnvironmentId): boolean {
256 return (
257 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
258 .threadTitleRegeneration === true
259 );
260}
261
262/** Whether the environment's server understands thread.pin.reorder (and
263 orderKey on thread.pin). Same version-skew contract as settlement. */
264export function readEnvironmentSupportsPinReorder(environmentId: EnvironmentId): boolean {
265 return (
266 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
267 .threadPinReorder === true
268 );
269}
270
271export function readThreadDetail(ref: ScopedThreadRef): EnvironmentThread | null {
272 return appAtomRegistry.get(environmentThreadDetails.detailAtom(ref));
273}
274
275export function readEnvironmentThreadRefs(
276 environmentId: EnvironmentId,
277): ReadonlyArray<ScopedThreadRef> {
278 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
279}
280
281export function readThreadRefs(): ReadonlyArray<ScopedThreadRef> {
282 return appAtomRegistry.get(environmentThreadShells.threadRefsAtom);
283}
284
285export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
286 return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
287}
288
289export function findThreadRef(threadId: ThreadId): ScopedThreadRef | null {
290 return (
291 appAtomRegistry
292 .get(environmentThreadShells.threadRefsAtom)
293 .find((ref) => ref.threadId === threadId) ?? null
294 );
295}