Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

entities.ts

208 lines7.3 KB

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