Open History to see the last commit
1import type {
2 EnvironmentId,
3 OrchestrationShellSnapshot,
4 OrchestrationThreadShell,
5 ProjectId,
6 ScopedProjectRef,
7 ScopedThreadRef,
8 ThreadId,
9} from "@v12/contracts";
10import { Atom } from "effect/unstable/reactivity";
11
12import type { EnvironmentThreadShell } from "./models.ts";
13import { scopeThreadShell } from "./models.ts";
14import type { EnvironmentCatalogState } from "./connections.ts";
15import {
16 arrayElementsEqual,
17 parseProjectRefCollectionKey,
18 parseThreadKey,
19 projectRefCollectionKey,
20 threadKey,
21 threadRefsEqual,
22} from "./entities.ts";
23
24const EMPTY_THREADS: ReadonlyArray<OrchestrationThreadShell> = Object.freeze([]);
25const EMPTY_SCOPED_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
26const EMPTY_THREAD_INDEX: ReadonlyMap<ThreadId, OrchestrationThreadShell> = new Map();
27const EMPTY_THREAD_REFS_BY_PROJECT: ReadonlyMap<
28 ProjectId,
29 ReadonlyArray<ScopedThreadRef>
30> = new Map();
31
32export function createEnvironmentThreadShellAtoms(input: {
33 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
34 readonly snapshotAtom: (
35 environmentId: EnvironmentId,
36 ) => Atom.Atom<OrchestrationShellSnapshot | null>;
37}) {
38 const environmentThreadsAtom = Atom.family((environmentId: EnvironmentId) =>
39 Atom.make(
40 (get): ReadonlyArray<OrchestrationThreadShell> =>
41 get(input.snapshotAtom(environmentId))?.threads ?? EMPTY_THREADS,
42 ).pipe(Atom.withLabel(`environment-threads:${environmentId}`)),
43 );
44
45 const environmentThreadIndexAtom = Atom.family((environmentId: EnvironmentId) =>
46 Atom.make((get): ReadonlyMap<ThreadId, OrchestrationThreadShell> => {
47 const threads = get(environmentThreadsAtom(environmentId));
48 if (threads.length === 0) {
49 return EMPTY_THREAD_INDEX;
50 }
51 return new Map(threads.map((thread) => [thread.id, thread] as const));
52 }).pipe(Atom.withLabel(`environment-thread-index:${environmentId}`)),
53 );
54
55 const environmentThreadRefsAtom = Atom.family((environmentId: EnvironmentId) => {
56 let previous: ReadonlyArray<ScopedThreadRef> = [];
57 return Atom.make((get) => {
58 const next = get(environmentThreadsAtom(environmentId)).map((thread) => ({
59 environmentId,
60 threadId: thread.id,
61 }));
62 if (threadRefsEqual(previous, next)) {
63 return previous;
64 }
65 previous = next;
66 return next;
67 }).pipe(Atom.withLabel(`environment-thread-refs:${environmentId}`));
68 });
69
70 const environmentThreadRefsByProjectAtom = Atom.family((environmentId: EnvironmentId) => {
71 let previous: ReadonlyMap<
72 ProjectId,
73 ReadonlyArray<ScopedThreadRef>
74 > = EMPTY_THREAD_REFS_BY_PROJECT;
75 return Atom.make((get) => {
76 const grouped = new Map<ProjectId, ScopedThreadRef[]>();
77 for (const thread of get(environmentThreadsAtom(environmentId))) {
78 const refs = grouped.get(thread.projectId);
79 const ref = { environmentId, threadId: thread.id };
80 if (refs === undefined) {
81 grouped.set(thread.projectId, [ref]);
82 } else {
83 refs.push(ref);
84 }
85 }
86 if (grouped.size === 0) {
87 previous = EMPTY_THREAD_REFS_BY_PROJECT;
88 return previous;
89 }
90 const next = new Map<ProjectId, ReadonlyArray<ScopedThreadRef>>();
91 for (const [projectId, refs] of grouped) {
92 const previousRefs = previous.get(projectId);
93 next.set(
94 projectId,
95 previousRefs !== undefined && threadRefsEqual(previousRefs, refs) ? previousRefs : refs,
96 );
97 }
98 previous = next;
99 return previous;
100 }).pipe(Atom.withLabel(`environment-thread-refs-by-project:${environmentId}`));
101 });
102
103 const threadShellAtomFamily = Atom.family((key: string) => {
104 const ref = parseThreadKey(key);
105 let previousSource: OrchestrationThreadShell | null = null;
106 let previousValue: EnvironmentThreadShell | null = null;
107 return Atom.make((get) => {
108 const source = get(environmentThreadIndexAtom(ref.environmentId)).get(ref.threadId) ?? null;
109 if (source === previousSource) {
110 return previousValue;
111 }
112 previousSource = source;
113 previousValue = source === null ? null : scopeThreadShell(ref.environmentId, source);
114 return previousValue;
115 }).pipe(Atom.withLabel(`environment-thread-shell:${key}`));
116 });
117
118 const threadShellsForProjectRefsAtomFamily = Atom.family((key: string) => {
119 const projectRefs = parseProjectRefCollectionKey(key);
120 let previous: ReadonlyArray<EnvironmentThreadShell> = [];
121 return Atom.make((get) => {
122 const next: EnvironmentThreadShell[] = [];
123 const seen = new Set<string>();
124 for (const projectRef of projectRefs) {
125 const refs =
126 get(environmentThreadRefsByProjectAtom(projectRef.environmentId)).get(
127 projectRef.projectId,
128 ) ?? EMPTY_SCOPED_THREAD_REFS;
129 for (const ref of refs) {
130 const key = threadKey(ref);
131 if (seen.has(key)) {
132 continue;
133 }
134 seen.add(key);
135 const thread = get(threadShellAtomFamily(key));
136 if (thread !== null) {
137 next.push(thread);
138 }
139 }
140 }
141 if (arrayElementsEqual(previous, next)) {
142 return previous;
143 }
144 previous = next;
145 return previous;
146 }).pipe(Atom.withLabel(`environment-thread-shells-for-projects:${key}`));
147 });
148
149 let previousThreadRefs: ReadonlyArray<ScopedThreadRef> = [];
150 const threadRefsAtom = Atom.make((get) => {
151 const refs: ScopedThreadRef[] = [];
152 for (const environmentId of get(input.catalogValueAtom).entries.keys()) {
153 refs.push(...get(environmentThreadRefsAtom(environmentId)));
154 }
155 if (threadRefsEqual(previousThreadRefs, refs)) {
156 return previousThreadRefs;
157 }
158 previousThreadRefs = refs;
159 return refs;
160 }).pipe(Atom.withLabel("environment-thread-refs"));
161
162 let previousThreadShells: ReadonlyArray<EnvironmentThreadShell> = [];
163 const threadShellsAtom = Atom.make((get) => {
164 const next = get(threadRefsAtom).flatMap((ref) => {
165 const thread = get(threadShellAtomFamily(threadKey(ref)));
166 return thread === null ? [] : [thread];
167 });
168 if (arrayElementsEqual(previousThreadShells, next)) {
169 return previousThreadShells;
170 }
171 previousThreadShells = next;
172 return previousThreadShells;
173 }).pipe(Atom.withLabel("environment-thread-shell-list"));
174
175 return {
176 environmentThreadsAtom,
177 environmentThreadIndexAtom,
178 environmentThreadRefsAtom,
179 environmentThreadRefsByProjectAtom,
180 threadRefsAtom,
181 threadShellsAtom,
182 threadShellsForProjectRefsAtom: (refs: ReadonlyArray<ScopedProjectRef>) =>
183 threadShellsForProjectRefsAtomFamily(projectRefCollectionKey(refs)),
184 threadShellAtom: (ref: ScopedThreadRef) => threadShellAtomFamily(threadKey(ref)),
185 };
186}