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 { mergeEnvironmentThread } from "@t3tools/client-runtime/state/threads";
8import type {
9 OrchestrationMessage,
10 OrchestrationProposedPlan,
11 OrchestrationSession,
12 OrchestrationThreadActivity,
13 ScopedProjectRef,
14 ScopedThreadRef,
15 ServerConfig,
16} from "@t3tools/contracts";
17import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
18import { Atom } from "effect/unstable/reactivity";
19import { useMemo } from "react";
20import { appAtomRegistry } from "../rpc/atomRegistry";
21import { environmentProjects } from "./projects";
22import { environmentServerConfigsAtom } from "./server";
23import { environmentThreadDetails, environmentThreadShells } from "./threads";
24
25const EMPTY_PROJECT_REFS: ReadonlyArray<ScopedProjectRef> = Object.freeze([]);
26const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
27const EMPTY_MESSAGES: ReadonlyArray<OrchestrationMessage> = Object.freeze([]);
28const EMPTY_ACTIVITIES: ReadonlyArray<OrchestrationThreadActivity> = Object.freeze([]);
29const EMPTY_PROPOSED_PLANS: ReadonlyArray<OrchestrationProposedPlan> = Object.freeze([]);
30
31const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
32 Atom.withLabel("web-project:empty"),
33);
34const EMPTY_PROJECT_REFS_ATOM = Atom.make(EMPTY_PROJECT_REFS).pipe(
35 Atom.withLabel("web-project-refs:empty"),
36);
37const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
38 Atom.withLabel("web-thread-refs:empty"),
39);
40const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
41 Atom.withLabel("web-thread-shell:empty"),
42);
43const EMPTY_THREAD_DETAIL_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
44 Atom.withLabel("web-thread-detail:empty"),
45);
46const EMPTY_MESSAGES_ATOM = Atom.make(EMPTY_MESSAGES).pipe(
47 Atom.withLabel("web-thread-messages:empty"),
48);
49const EMPTY_ACTIVITIES_ATOM = Atom.make(EMPTY_ACTIVITIES).pipe(
50 Atom.withLabel("web-thread-activities:empty"),
51);
52const EMPTY_PROPOSED_PLANS_ATOM = Atom.make(EMPTY_PROPOSED_PLANS).pipe(
53 Atom.withLabel("web-thread-proposed-plans:empty"),
54);
55const EMPTY_SESSION_ATOM = Atom.make<OrchestrationSession | null>(null).pipe(
56 Atom.withLabel("web-thread-session:empty"),
57);
58
59export const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
60 Atom.keepAlive,
61 Atom.withLabel("web-active-environment-id"),
62);
63
64export function useActiveEnvironmentId(): EnvironmentId | null {
65 return useAtomValue(activeEnvironmentIdAtom);
66}
67
68export function readActiveEnvironmentId(): EnvironmentId | null {
69 return appAtomRegistry.get(activeEnvironmentIdAtom);
70}
71
72export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
73 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
74}
75
76export function useProjectRefs(): ReadonlyArray<ScopedProjectRef> {
77 return useAtomValue(environmentProjects.projectRefsAtom);
78}
79
80export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
81 return useAtomValue(environmentThreadShells.threadRefsAtom);
82}
83
84export function useEnvironmentProjectRefs(
85 environmentId: EnvironmentId | null,
86): ReadonlyArray<ScopedProjectRef> {
87 return useAtomValue(
88 environmentId === null
89 ? EMPTY_PROJECT_REFS_ATOM
90 : environmentProjects.environmentProjectRefsAtom(environmentId),
91 );
92}
93
94export function useEnvironmentThreadRefs(
95 environmentId: EnvironmentId | null,
96): ReadonlyArray<ScopedThreadRef> {
97 return useAtomValue(
98 environmentId === null
99 ? EMPTY_THREAD_REFS_ATOM
100 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
101 );
102}
103
104export function useProjects(): ReadonlyArray<EnvironmentProject> {
105 return useAtomValue(environmentProjects.projectsAtom);
106}
107
108export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
109 return useAtomValue(environmentServerConfigsAtom);
110}
111
112export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
113 return useAtomValue(environmentThreadShells.threadShellsAtom);
114}
115
116export function useThreadShellsForProjectRefs(
117 refs: ReadonlyArray<ScopedProjectRef>,
118): ReadonlyArray<EnvironmentThreadShell> {
119 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
120}
121
122export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
123 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
124}
125
126export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
127 return useAtomValue(
128 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
129 );
130}
131
132export function useThreadDetail(ref: ScopedThreadRef | null): EnvironmentThread | null {
133 return useAtomValue(
134 ref === null ? EMPTY_THREAD_DETAIL_ATOM : environmentThreadDetails.detailAtom(ref),
135 );
136}
137
138/** Detail collections composed with shell-authoritative thread/workspace metadata. */
139export function useThread(ref: ScopedThreadRef | null): EnvironmentThread | null {
140 const shell = useThreadShell(ref);
141 const detail = useThreadDetail(ref);
142 return useMemo(() => mergeEnvironmentThread(detail, shell), [detail, shell]);
143}
144
145export function useThreadMessages(
146 ref: ScopedThreadRef | null,
147): ReadonlyArray<OrchestrationMessage> {
148 return useAtomValue(
149 ref === null ? EMPTY_MESSAGES_ATOM : environmentThreadDetails.messagesAtom(ref),
150 );
151}
152
153export function useThreadActivities(
154 ref: ScopedThreadRef | null,
155): ReadonlyArray<OrchestrationThreadActivity> {
156 return useAtomValue(
157 ref === null ? EMPTY_ACTIVITIES_ATOM : environmentThreadDetails.activitiesAtom(ref),
158 );
159}
160
161export function useThreadProposedPlans(
162 ref: ScopedThreadRef | null,
163): ReadonlyArray<OrchestrationProposedPlan> {
164 return useAtomValue(
165 ref === null ? EMPTY_PROPOSED_PLANS_ATOM : environmentThreadDetails.proposedPlansAtom(ref),
166 );
167}
168
169export function useThreadSession(ref: ScopedThreadRef | null): OrchestrationSession | null {
170 return useAtomValue(
171 ref === null ? EMPTY_SESSION_ATOM : environmentThreadDetails.sessionAtom(ref),
172 );
173}
174
175export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
176 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
177}
178
179export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
180 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
181}
182
183export function readThreadDetail(ref: ScopedThreadRef): EnvironmentThread | null {
184 return appAtomRegistry.get(environmentThreadDetails.detailAtom(ref));
185}
186
187export function readEnvironmentThreadRefs(
188 environmentId: EnvironmentId,
189): ReadonlyArray<ScopedThreadRef> {
190 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
191}
192
193export function readThreadRefs(): ReadonlyArray<ScopedThreadRef> {
194 return appAtomRegistry.get(environmentThreadShells.threadRefsAtom);
195}
196
197export function findThreadRef(threadId: ThreadId): ScopedThreadRef | null {
198 return (
199 appAtomRegistry
200 .get(environmentThreadShells.threadRefsAtom)
201 .find((ref) => ref.threadId === threadId) ?? null
202 );
203}