Something went wrong. Try again.
Your thinking partner.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839import type { SDKMessage } from "@letta-ai/letta-agent-sdk/client";
import type { TranscriptItem } from "./model";
/** Apply a visual batch of wire events, then perform one transcript projection. */export function applyStreamBatch<TSnapshot>( accumulator: { apply: (message: SDKMessage) => void }, events: readonly SDKMessage[], snapshot: TSnapshot, project: (snapshot: TSnapshot) => TSnapshot,): TSnapshot { for (const event of events) accumulator.apply(event); return project(snapshot);}
/** Preserve item identity when projection produced the same flat render data. */export function stabilizeTranscriptItems( previousById: ReadonlyMap<string, TranscriptItem>, projected: readonly TranscriptItem[],): { items: TranscriptItem[]; byId: Map<string, TranscriptItem> } { const byId = new Map<string, TranscriptItem>(); const items = projected.map((item) => { const previous = previousById.get(item.id); const stable = previous && sameTranscriptItem(previous, item) ? previous : item; byId.set(stable.id, stable); return stable; }); return { items, byId };}
function sameTranscriptItem(previous: TranscriptItem, next: TranscriptItem): boolean { if (previous.kind !== next.kind) return false; const previousRecord = previous as unknown as Record<string, unknown>; const nextRecord = next as unknown as Record<string, unknown>; const keys = Object.keys(nextRecord); if (keys.length !== Object.keys(previousRecord).length) return false; return keys.every((key) => previousRecord[key] === nextRecord[key]);}