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

ShellStream.ts

191 lines6.5 KB

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 };
27​
28/** Keep only the newest shell-relevant event per project/thread aggregate. */
29export function coalesceShellApplicationEvents(
30 events: ReadonlyArray<ApplicationStoredEvent>,
31): ReadonlyArray<ApplicationStoredEvent> {
32 const latestByAggregate = new Map<string, ApplicationStoredEvent>();
33 for (const stored of events) {
34 const key =
35 "aggregateKind" in stored
36 ? `project:${stored.aggregateId}`
37 : `thread:${stored.event.threadId}`;
38 latestByAggregate.set(key, stored);
39 }
40 return Array.from(latestByAggregate.values()).sort(
41 (left, right) => left.sequence - right.sequence,
42 );
44​
45/**
46 * Emit the initial shell prefix strictly first, then merge the post-prefix
47 * tail with enrichment refreshes. Prevents a newer marked enrichment from
48 * landing before the unmarked authoritative initial snapshot.
49 */
50export function composeShellStreamWithEnrichment<A, E, R, A2, E2, R2, A3, E3, R3>(input: {
51 readonly initial: Stream.Stream<A, E, R>;
52 readonly tail: Stream.Stream<A2, E2, R2>;
53 readonly enrichment: Stream.Stream<A3, E3, R3>;
54}): Stream.Stream<A | A2 | A3, E | E2 | E3, R | R2 | R3> {
55 return Stream.concat(input.initial, Stream.merge(input.tail, input.enrichment));
57​
58/** Build a shell snapshot stream item for a batched enrichment completion. */
59export function shellStreamItemFromEnrichmentRefresh(input: {
60 readonly snapshot: OrchestrationV2ShellSnapshot;
61 readonly changes: ReadonlyArray<{ readonly workspaceRoot: string }>;
62}): Extract<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }> {
63 const resolvedRepositoryIdentityRoots = [
64 ...new Set(input.changes.map((change) => change.workspaceRoot)),
65 ];
66 return {
67 kind: "snapshot",
68 snapshot: {
69 ...input.snapshot,
70 projects: input.snapshot.projects.filter((project) =>
71 resolvedRepositoryIdentityRoots.includes(project.workspaceRoot),
72 ),
73 threads: [],
74 archivedThreads: [],
75 },
76 resolvedRepositoryIdentityRoots,
77 };
79​
80/**
81 * Initial subscribe frames: always emit the unmarked authoritative snapshot,
82 * then a same-sequence enrichment frame only when some roots already resolved.
83 */
84export function shellStreamItemsFromInitialSnapshot(input: {
85 readonly snapshot: OrchestrationV2ShellSnapshot;
86 readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
87}): ReadonlyArray<Extract<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }>> {
88 const authoritative = {
89 kind: "snapshot" as const,
90 snapshot: input.snapshot,
91 };
92 if (input.resolvedRepositoryIdentityRoots.length === 0) {
93 return [authoritative];
94 }
95 return [
96 authoritative,
97 {
98 kind: "snapshot" as const,
99 snapshot: {
100 ...input.snapshot,
101 projects: input.snapshot.projects.filter((project) =>
102 input.resolvedRepositoryIdentityRoots.includes(project.workspaceRoot),
103 ),
104 threads: [],
105 archivedThreads: [],
106 },
107 resolvedRepositoryIdentityRoots: [...new Set(input.resolvedRepositoryIdentityRoots)],
108 },
109 ];
111​
112/** Resume after HTTP/cache hydration with metadata only, never another full shell body. */
113export function shellStreamItemsFromResumeSnapshot(input: {
114 readonly snapshot: OrchestrationV2ShellSnapshot;
115 readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
116}): ReadonlyArray<Extract<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }>> {
117 return shellStreamItemsFromInitialSnapshot(input).filter(
118 (item) => item.resolvedRepositoryIdentityRoots !== undefined,
119 );
121​
122/** Keep only the newest stored event per thread within a coalescing window. */
123export function coalesceStoredThreadEvents(
124 events: ReadonlyArray<OrchestrationV2StoredEvent>,
125): ReadonlyArray<OrchestrationV2StoredEvent> {
126 const latestByThreadId = new Map<string, OrchestrationV2StoredEvent>();
127 for (const stored of events) {
128 latestByThreadId.set(stored.event.threadId, stored);
129 }
130 return Array.from(latestByThreadId.values()).sort(
131 (left, right) => left.sequence - right.sequence,
132 );
134​
135/**
136 * Converts a committed event and the affected thread's current shell into one
137 * delta. `shell` is null when the thread is deleted or unknown.
138 */
139export function shellStreamItemFromThreadShell(input: {
140 readonly stored: OrchestrationV2StoredEvent;
141 readonly shell: OrchestrationV2ThreadShell | null;
142}): Exclude<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }> {
143 if (input.shell !== null) {
144 if (input.shell.archivedAt !== null) {
145 return {
146 kind: "thread.removed",
147 sequence: input.stored.sequence,
148 location: "active",
149 threadId: input.shell.id,
150 };
151 }
152 return {
153 kind: "thread.updated",
154 sequence: input.stored.sequence,
155 location: "active",
156 thread: input.shell,
157 };
158 }
159​
160 return {
161 kind: "thread.removed",
162 sequence: input.stored.sequence,
163 location: "active",
164 threadId: input.stored.event.threadId,
165 };
167​
168/** Converts a committed event into an archive-only delta when it changes archive membership. */
169export function archivedShellStreamItemFromThreadShell(input: {
170 readonly stored: OrchestrationV2StoredEvent;
171 readonly shell: OrchestrationV2ThreadShell | null;
172}): Exclude<OrchestrationV2ArchivedShellStreamItem, { readonly kind: "snapshot" }> | null {
173 if (input.shell !== null && input.shell.archivedAt !== null) {
174 return {
175 kind: "thread.updated",
176 sequence: input.stored.sequence,
177 thread: input.shell,
178 };
179 }
180 if (
181 input.stored.event.type === "thread.unarchived" ||
182 (input.stored.event.type === "thread.deleted" && input.stored.event.payload.archivedAt !== null)
183 ) {
184 return {
185 kind: "thread.removed",
186 sequence: input.stored.sequence,
187 threadId: input.stored.event.threadId,
188 };
189 }
190 return null;