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

ShellStream.ts

144 lines5.0 KB

1import type {
2 ApplicationStoredEvent,
3 OrchestrationV2ArchivedShellStreamItem,
4 OrchestrationV2ShellSnapshot,
5 OrchestrationV2ThreadShell,
6 OrchestrationV2ShellStreamItem,
7 OrchestrationV2StoredEvent,
8} from "@t3tools/contracts";
9import * as Stream from "effect/Stream";
10​
11/** Keep only the newest shell-relevant event per project/thread aggregate. */
12export function coalesceShellApplicationEvents(
13 events: ReadonlyArray<ApplicationStoredEvent>,
14): ReadonlyArray<ApplicationStoredEvent> {
15 const latestByAggregate = new Map<string, ApplicationStoredEvent>();
16 for (const stored of events) {
17 const key =
18 "aggregateKind" in stored
19 ? `project:${stored.aggregateId}`
20 : `thread:${stored.event.threadId}`;
21 latestByAggregate.set(key, stored);
22 }
23 return Array.from(latestByAggregate.values()).sort(
24 (left, right) => left.sequence - right.sequence,
25 );
27​
28/**
29 * Emit the initial shell prefix strictly first, then merge the post-prefix
30 * tail with enrichment refreshes. Prevents a newer marked enrichment from
31 * landing before the unmarked authoritative initial snapshot.
32 */
33export function composeShellStreamWithEnrichment<A, E, R, A2, E2, R2, A3, E3, R3>(input: {
34 readonly initial: Stream.Stream<A, E, R>;
35 readonly tail: Stream.Stream<A2, E2, R2>;
36 readonly enrichment: Stream.Stream<A3, E3, R3>;
37}): Stream.Stream<A | A2 | A3, E | E2 | E3, R | R2 | R3> {
38 return Stream.concat(input.initial, Stream.merge(input.tail, input.enrichment));
40​
41/** Build a shell snapshot stream item for a batched enrichment completion. */
42export function shellStreamItemFromEnrichmentRefresh(input: {
43 readonly snapshot: OrchestrationV2ShellSnapshot;
44 readonly changes: ReadonlyArray<{ readonly workspaceRoot: string }>;
45}): Extract<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }> {
46 return {
47 kind: "snapshot",
48 snapshot: input.snapshot,
49 resolvedRepositoryIdentityRoots: [
50 ...new Set(input.changes.map((change) => change.workspaceRoot)),
51 ],
52 };
54​
55/**
56 * Initial subscribe frames: always emit the unmarked authoritative snapshot,
57 * then a same-sequence enrichment frame only when some roots already resolved.
58 */
59export function shellStreamItemsFromInitialSnapshot(input: {
60 readonly snapshot: OrchestrationV2ShellSnapshot;
61 readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
62}): ReadonlyArray<Extract<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }>> {
63 const authoritative = {
64 kind: "snapshot" as const,
65 snapshot: input.snapshot,
66 };
67 if (input.resolvedRepositoryIdentityRoots.length === 0) {
68 return [authoritative];
69 }
70 return [
71 authoritative,
72 {
73 kind: "snapshot" as const,
74 snapshot: input.snapshot,
75 resolvedRepositoryIdentityRoots: [...new Set(input.resolvedRepositoryIdentityRoots)],
76 },
77 ];
79​
80/** Keep only the newest stored event per thread within a coalescing window. */
81export function coalesceStoredThreadEvents(
82 events: ReadonlyArray<OrchestrationV2StoredEvent>,
83): ReadonlyArray<OrchestrationV2StoredEvent> {
84 const latestByThreadId = new Map<string, OrchestrationV2StoredEvent>();
85 for (const stored of events) {
86 latestByThreadId.set(stored.event.threadId, stored);
87 }
88 return Array.from(latestByThreadId.values()).sort(
89 (left, right) => left.sequence - right.sequence,
90 );
92​
93/**
94 * Converts a committed event and the affected thread's current shell into one
95 * delta. `shell` is null when the thread is deleted or unknown.
96 */
97export function shellStreamItemFromThreadShell(input: {
98 readonly stored: OrchestrationV2StoredEvent;
99 readonly shell: OrchestrationV2ThreadShell | null;
100}): Exclude<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }> {
101 if (input.shell !== null) {
102 return {
103 kind: "thread.updated",
104 sequence: input.stored.sequence,
105 location: input.shell.archivedAt === null ? "active" : "archive",
106 thread: input.shell,
107 };
108 }
109​
110 return {
111 kind: "thread.removed",
112 sequence: input.stored.sequence,
113 location:
114 input.stored.event.type === "thread.deleted" && input.stored.event.payload.archivedAt !== null
115 ? "archive"
116 : "active",
117 threadId: input.stored.event.threadId,
118 };
120​
121/** Converts a committed event into an archive-only delta when it changes archive membership. */
122export function archivedShellStreamItemFromThreadShell(input: {
123 readonly stored: OrchestrationV2StoredEvent;
124 readonly shell: OrchestrationV2ThreadShell | null;
125}): Exclude<OrchestrationV2ArchivedShellStreamItem, { readonly kind: "snapshot" }> | null {
126 if (input.shell !== null && input.shell.archivedAt !== null) {
127 return {
128 kind: "thread.updated",
129 sequence: input.stored.sequence,
130 thread: input.shell,
131 };
132 }
133 if (
134 input.stored.event.type === "thread.unarchived" ||
135 (input.stored.event.type === "thread.deleted" && input.stored.event.payload.archivedAt !== null)
136 ) {
137 return {
138 kind: "thread.removed",
139 sequence: input.stored.sequence,
140 threadId: input.stored.event.threadId,
141 };
142 }
143 return null;