Open History to see the last commit
1import type {
2 ApplicationStoredEvent,
3 OrchestrationProjectShell,
4 OrchestrationV2ArchivedShellStreamItem,
5 OrchestrationV2ShellSnapshot,
6 OrchestrationV2ThreadShell,
7 OrchestrationV2ThreadShellSnapshot,
8 OrchestrationV2ShellStreamItem,
9 OrchestrationV2StoredEvent,
10} from "@t3tools/contracts";
11import * as Stream from "effect/Stream";
12
13/** Build the regular navigation shell without duplicating the archive dataset. */
14export function buildActiveShellSnapshot(input: {
15 readonly projects: ReadonlyArray<OrchestrationProjectShell>;
16 readonly threads: OrchestrationV2ThreadShellSnapshot;
17 readonly snapshotSequence: number;
18}): OrchestrationV2ShellSnapshot {
19 return {
20 schemaVersion: input.threads.schemaVersion,
21 snapshotSequence: input.snapshotSequence,
22 projects: input.projects,
23 threads: input.threads.threads,
24 archivedThreads: [],
25 };
26}
27
28export type ShellApplicationEvent =
29 | Pick<
30 Extract<ApplicationStoredEvent, { readonly aggregateKind: "project" }>,
31 "aggregateKind" | "aggregateId" | "type" | "sequence"
32 >
33 | {
34 readonly sequence: number;
35 readonly event: Pick<OrchestrationV2StoredEvent["event"], "threadId">;
36 };
37
38/** Shell updates refetch an aggregate; drop transcript bodies before retaining an event. */
39export function toShellApplicationEvent(stored: ApplicationStoredEvent): ShellApplicationEvent {
40 return "aggregateKind" in stored
41 ? {
42 aggregateKind: stored.aggregateKind,
43 aggregateId: stored.aggregateId,
44 type: stored.type,
45 sequence: stored.sequence,
46 }
47 : { sequence: stored.sequence, event: { threadId: stored.event.threadId } };
48}
49
50/** Keep only the newest shell-relevant event per project/thread aggregate. */
51export function coalesceShellApplicationEvents<A extends ShellApplicationEvent>(
52 events: ReadonlyArray<A>,
53): ReadonlyArray<A> {
54 const latestByAggregate = new Map<string, A>();
55 for (const stored of events) {
56 const key =
57 "aggregateKind" in stored
58 ? `project:${stored.aggregateId}`
59 : `thread:${stored.event.threadId}`;
60 latestByAggregate.set(key, stored);
61 }
62 return Array.from(latestByAggregate.values()).sort(
63 (left, right) => left.sequence - right.sequence,
64 );
65}
66
67/**
68 * Emit the initial shell prefix strictly first, then merge the post-prefix
69 * tail with enrichment refreshes. Prevents a newer marked enrichment from
70 * landing before the unmarked authoritative initial snapshot.
71 */
72export function composeShellStreamWithEnrichment<A, E, R, A2, E2, R2, A3, E3, R3>(input: {
73 readonly initial: Stream.Stream<A, E, R>;
74 readonly tail: Stream.Stream<A2, E2, R2>;
75 readonly enrichment: Stream.Stream<A3, E3, R3>;
76}): Stream.Stream<A | A2 | A3, E | E2 | E3, R | R2 | R3> {
77 return Stream.concat(input.initial, Stream.merge(input.tail, input.enrichment));
78}
79
80/** Build a shell snapshot stream item for a batched enrichment completion. */
81export function shellStreamItemFromEnrichmentRefresh(input: {
82 readonly snapshot: OrchestrationV2ShellSnapshot;
83 readonly changes: ReadonlyArray<{ readonly workspaceRoot: string }>;
84}): Extract<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }> {
85 const resolvedRepositoryIdentityRoots = [
86 ...new Set(input.changes.map((change) => change.workspaceRoot)),
87 ];
88 return {
89 kind: "snapshot",
90 snapshot: {
91 ...input.snapshot,
92 projects: input.snapshot.projects.filter((project) =>
93 resolvedRepositoryIdentityRoots.includes(project.workspaceRoot),
94 ),
95 threads: [],
96 archivedThreads: [],
97 },
98 resolvedRepositoryIdentityRoots,
99 };
100}
101
102/**
103 * Initial subscribe frames: always emit the unmarked authoritative snapshot,
104 * then a same-sequence enrichment frame only when some roots already resolved.
105 */
106export function shellStreamItemsFromInitialSnapshot(input: {
107 readonly snapshot: OrchestrationV2ShellSnapshot;
108 readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
109}): ReadonlyArray<Extract<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }>> {
110 const authoritative = {
111 kind: "snapshot" as const,
112 snapshot: input.snapshot,
113 };
114 if (input.resolvedRepositoryIdentityRoots.length === 0) {
115 return [authoritative];
116 }
117 return [
118 authoritative,
119 {
120 kind: "snapshot" as const,
121 snapshot: {
122 ...input.snapshot,
123 projects: input.snapshot.projects.filter((project) =>
124 input.resolvedRepositoryIdentityRoots.includes(project.workspaceRoot),
125 ),
126 threads: [],
127 archivedThreads: [],
128 },
129 resolvedRepositoryIdentityRoots: [...new Set(input.resolvedRepositoryIdentityRoots)],
130 },
131 ];
132}
133
134/** Resume after HTTP/cache hydration with metadata only, never another full shell body. */
135export function shellStreamItemsFromResumeSnapshot(input: {
136 readonly snapshot: OrchestrationV2ShellSnapshot;
137 readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
138}): ReadonlyArray<Extract<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }>> {
139 return shellStreamItemsFromInitialSnapshot(input).filter(
140 (item) => item.resolvedRepositoryIdentityRoots !== undefined,
141 );
142}
143
144/** Keep only the newest stored event per thread within a coalescing window. */
145export function coalesceStoredThreadEvents(
146 events: ReadonlyArray<OrchestrationV2StoredEvent>,
147): ReadonlyArray<OrchestrationV2StoredEvent> {
148 const latestByThreadId = new Map<string, OrchestrationV2StoredEvent>();
149 for (const stored of events) {
150 latestByThreadId.set(stored.event.threadId, stored);
151 }
152 return Array.from(latestByThreadId.values()).sort(
153 (left, right) => left.sequence - right.sequence,
154 );
155}
156
157/**
158 * Converts a committed event and the affected thread's current shell into one
159 * delta. `shell` is null when the thread is deleted or unknown.
160 */
161export function shellStreamItemFromThreadShell(input: {
162 readonly stored: Extract<ShellApplicationEvent, { readonly event: unknown }>;
163 readonly shell: OrchestrationV2ThreadShell | null;
164}): Exclude<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }> {
165 if (input.shell !== null) {
166 if (input.shell.archivedAt !== null) {
167 return {
168 kind: "thread.removed",
169 sequence: input.stored.sequence,
170 location: "active",
171 threadId: input.shell.id,
172 };
173 }
174 return {
175 kind: "thread.updated",
176 sequence: input.stored.sequence,
177 location: "active",
178 thread: input.shell,
179 };
180 }
181
182 return {
183 kind: "thread.removed",
184 sequence: input.stored.sequence,
185 location: "active",
186 threadId: input.stored.event.threadId,
187 };
188}
189
190/** Converts a committed event into an archive-only delta when it changes archive membership. */
191export function archivedShellStreamItemFromThreadShell(input: {
192 readonly stored: OrchestrationV2StoredEvent;
193 readonly shell: OrchestrationV2ThreadShell | null;
194}): Exclude<OrchestrationV2ArchivedShellStreamItem, { readonly kind: "snapshot" }> | null {
195 if (input.shell !== null && input.shell.archivedAt !== null) {
196 return {
197 kind: "thread.updated",
198 sequence: input.stored.sequence,
199 thread: input.shell,
200 };
201 }
202 if (
203 input.stored.event.type === "thread.unarchived" ||
204 (input.stored.event.type === "thread.deleted" && input.stored.event.payload.archivedAt !== null)
205 ) {
206 return {
207 kind: "thread.removed",
208 sequence: input.stored.sequence,
209 threadId: input.stored.event.threadId,
210 };
211 }
212 return null;
213}