- Migrate web and mobile state, commands, and persistence to V2 projections - Render conversations from authoritative visible turn items - Harden runtime policy, launch handling, attachments, and cache invalidation
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import type {
|
|
OrchestrationV2ShellSnapshot,
|
|
OrchestrationV2ShellStreamItem,
|
|
} from "@t3tools/contracts";
|
|
|
|
function upsertById<T extends { readonly id: unknown }>(
|
|
items: ReadonlyArray<T>,
|
|
item: T,
|
|
): ReadonlyArray<T> {
|
|
const index = items.findIndex((candidate) => candidate.id === item.id);
|
|
if (index === -1) return [...items, item];
|
|
return items.map((candidate, candidateIndex) => (candidateIndex === index ? item : candidate));
|
|
}
|
|
|
|
/** Applies one committed V2 shell delta while preserving active/archive exclusivity. */
|
|
export function applyShellStreamEvent(
|
|
snapshot: OrchestrationV2ShellSnapshot,
|
|
event: Exclude<OrchestrationV2ShellStreamItem, { readonly kind: "snapshot" }>,
|
|
): OrchestrationV2ShellSnapshot {
|
|
if (event.sequence <= snapshot.snapshotSequence) return snapshot;
|
|
|
|
switch (event.kind) {
|
|
case "project.updated":
|
|
return {
|
|
...snapshot,
|
|
projects: upsertById(snapshot.projects, event.project),
|
|
snapshotSequence: event.sequence,
|
|
};
|
|
case "project.removed":
|
|
return {
|
|
...snapshot,
|
|
projects: snapshot.projects.filter((project) => project.id !== event.projectId),
|
|
snapshotSequence: event.sequence,
|
|
};
|
|
case "thread.updated": {
|
|
const withoutThread = (threads: OrchestrationV2ShellSnapshot["threads"]) =>
|
|
threads.filter((thread) => thread.id !== event.thread.id);
|
|
return {
|
|
...snapshot,
|
|
threads:
|
|
event.location === "active"
|
|
? upsertById(withoutThread(snapshot.threads), event.thread)
|
|
: withoutThread(snapshot.threads),
|
|
archivedThreads:
|
|
event.location === "archive"
|
|
? upsertById(withoutThread(snapshot.archivedThreads), event.thread)
|
|
: withoutThread(snapshot.archivedThreads),
|
|
snapshotSequence: event.sequence,
|
|
};
|
|
}
|
|
case "thread.removed":
|
|
return {
|
|
...snapshot,
|
|
threads: snapshot.threads.filter((thread) => thread.id !== event.threadId),
|
|
archivedThreads: snapshot.archivedThreads.filter((thread) => thread.id !== event.threadId),
|
|
snapshotSequence: event.sequence,
|
|
};
|
|
default:
|
|
return snapshot;
|
|
}
|
|
}
|