Open History to see the last commit
1import type {
2 OrchestrationV2DomainEvent,
3 OrchestrationV2ThreadProjection,
4 OrchestrationV2TurnItem,
5} from "@t3tools/contracts";
6import { isOrchestrationV2TurnItemVisible } from "@t3tools/shared/orchestrationV2Timeline";
7
8function upsertEntity<T extends { readonly id: unknown }>(
9 items: ReadonlyArray<T>,
10 item: T,
11): ReadonlyArray<T> {
12 const index = items.findIndex((candidate) => candidate.id === item.id);
13 if (index === -1) return [...items, item];
14 const next = [...items];
15 next[index] = item;
16 return next;
17}
18
19function removeVisibleItem(
20 rows: OrchestrationV2ThreadProjection["visibleTurnItems"],
21 sourceItemId: OrchestrationV2TurnItem["id"],
22): OrchestrationV2ThreadProjection["visibleTurnItems"] {
23 const index = rows.findIndex((row) => row.sourceItemId === sourceItemId);
24 if (index === -1) return rows;
25 return renumberVisibleItems([...rows.slice(0, index), ...rows.slice(index + 1)]);
26}
27
28function renumberVisibleItems(
29 rows: OrchestrationV2ThreadProjection["visibleTurnItems"],
30): OrchestrationV2ThreadProjection["visibleTurnItems"] {
31 return rows.map((row, position) => (row.position === position ? row : { ...row, position }));
32}
33
34function shouldShowLocalTurnItem(
35 projection: OrchestrationV2ThreadProjection,
36 item: OrchestrationV2TurnItem,
37): boolean {
38 return isOrchestrationV2TurnItemVisible({
39 item,
40 runs: projection.runs,
41 attempts: projection.attempts,
42 });
43}
44
45function activeVisibleTurnItems(
46 projection: OrchestrationV2ThreadProjection,
47): OrchestrationV2ThreadProjection["visibleTurnItems"] {
48 const rows = projection.visibleTurnItems;
49 let next: Array<(typeof rows)[number]> | null = null;
50 for (let index = 0; index < rows.length; index += 1) {
51 const row = rows[index]!;
52 const keep = row.visibility !== "local" || shouldShowLocalTurnItem(projection, row.item);
53 if (!keep) {
54 next ??= rows.slice(0, index);
55 continue;
56 }
57 next?.push(row);
58 }
59 return next === null ? rows : renumberVisibleItems(next);
60}
61
62function upsertVisibleTurnItem(
63 projection: OrchestrationV2ThreadProjection,
64 item: OrchestrationV2TurnItem,
65): OrchestrationV2ThreadProjection["visibleTurnItems"] {
66 const rows = projection.visibleTurnItems;
67 const index = rows.findIndex((row) => row.sourceItemId === item.id);
68 const next = {
69 position: index === -1 ? rows.length : rows[index]!.position,
70 visibility: "local" as const,
71 sourceThreadId: item.threadId,
72 sourceItemId: item.id,
73 item,
74 };
75 if (index === -1) return [...rows, next];
76 const previous = rows[index]!;
77 if (
78 previous.visibility === next.visibility &&
79 previous.sourceThreadId === next.sourceThreadId &&
80 previous.sourceItemId === next.sourceItemId &&
81 previous.item === item
82 ) {
83 return rows;
84 }
85 const updated = [...rows];
86 updated[index] = next;
87 return updated;
88}
89
90/** Applies one committed event to a matching thread projection. */
91export function applyOrchestrationV2ProjectionEvent(
92 projection: OrchestrationV2ThreadProjection | null,
93 event: OrchestrationV2DomainEvent,
94): OrchestrationV2ThreadProjection | null {
95 if (projection === null || event.threadId !== projection.thread.id) return projection;
96
97 const base = { ...projection, updatedAt: event.occurredAt };
98 switch (event.type) {
99 case "thread.created":
100 case "thread.archived":
101 case "thread.unarchived":
102 case "thread.deleted":
103 case "thread.metadata-updated":
104 case "thread.runtime-mode-updated":
105 case "thread.interaction-mode-updated":
106 case "thread.model-selection-updated":
107 case "thread.provider-switched":
108 return { ...base, thread: event.payload };
109 case "run.created":
110 case "run.updated": {
111 const next = { ...base, runs: upsertEntity(base.runs, event.payload) };
112 return { ...next, visibleTurnItems: activeVisibleTurnItems(next) };
113 }
114 case "run-attempt.created":
115 case "run-attempt.updated": {
116 const next = { ...base, attempts: upsertEntity(base.attempts, event.payload) };
117 return { ...next, visibleTurnItems: activeVisibleTurnItems(next) };
118 }
119 case "node.updated":
120 return { ...base, nodes: upsertEntity(base.nodes, event.payload) };
121 case "subagent.updated":
122 return { ...base, subagents: upsertEntity(base.subagents, event.payload) };
123 case "provider-session.attached":
124 case "provider-session.updated":
125 return {
126 ...base,
127 providerSessions: upsertEntity(base.providerSessions, event.payload),
128 };
129 case "provider-session.detached":
130 return {
131 ...base,
132 providerSessions: base.providerSessions.filter(
133 (session) => session.id !== event.payload.providerSessionId,
134 ),
135 };
136 case "provider-thread.updated":
137 return { ...base, providerThreads: upsertEntity(base.providerThreads, event.payload) };
138 case "provider-turn.updated":
139 return { ...base, providerTurns: upsertEntity(base.providerTurns, event.payload) };
140 case "runtime-request.updated":
141 return { ...base, runtimeRequests: upsertEntity(base.runtimeRequests, event.payload) };
142 case "message.updated":
143 return { ...base, messages: upsertEntity(base.messages, event.payload) };
144 case "plan.updated":
145 return { ...base, plans: upsertEntity(base.plans, event.payload) };
146 case "turn-item.updated": {
147 const next = { ...base, turnItems: upsertEntity(base.turnItems, event.payload) };
148 const visible = { ...next, visibleTurnItems: activeVisibleTurnItems(next) };
149 return {
150 ...next,
151 visibleTurnItems: shouldShowLocalTurnItem(next, event.payload)
152 ? upsertVisibleTurnItem(visible, event.payload)
153 : removeVisibleItem(visible.visibleTurnItems, event.payload.id),
154 };
155 }
156 case "checkpoint-scope.created":
157 return { ...base, checkpointScopes: upsertEntity(base.checkpointScopes, event.payload) };
158 case "checkpoint.captured":
159 return { ...base, checkpoints: upsertEntity(base.checkpoints, event.payload) };
160 case "checkpoint.rollback-requested":
161 return base;
162 case "context-handoff.updated":
163 return { ...base, contextHandoffs: upsertEntity(base.contextHandoffs, event.payload) };
164 case "context-transfer.created":
165 case "context-transfer.updated":
166 return { ...base, contextTransfers: upsertEntity(base.contextTransfers, event.payload) };
167 }
168}