diff --git a/packages/session-ui/src/timeline/projection.test.ts b/packages/session-ui/src/timeline/projection.test.ts index 5e947ca359e..5bae029692e 100644 --- a/packages/session-ui/src/timeline/projection.test.ts +++ b/packages/session-ui/src/timeline/projection.test.ts @@ -50,6 +50,17 @@ const part = (key: string, partID: string) => const user = (userMessageID = "user-1") => new TimelineRow.UserMessage({ userMessageID }) +const diff = (files: string[]) => + new TimelineRow.TurnDiff({ userMessageID: "user-1", messageID: "idle-1", from: "user-1", files }) + +const spaced = (spacing?: "tool" | "content") => + new TimelineRow.AssistantPart({ + userMessageID: "user-1", + group: { key: "part:a", type: "part", ref: { messageID: "assistant-1", partID: "a" } }, + previousAssistantPart: true, + spacing, + }) + const keys = (rows: TimelineRow.TimelineRow[]) => rows.map(TimelineRow.key) describe("Timeline.resolveContent", () => { @@ -198,6 +209,29 @@ describe("reuseTimelineRows", () => { expected: ["user-message:user-1"], reused: [[0, 0]], }, + { + name: "reuses a row whose list field is equal", + previous: [diff(["a.ts", "b.ts"])], + rows: [diff(["a.ts", "b.ts"])], + expected: ["turn-diff:idle-1"], + reused: [[0, 0]], + }, + { + name: "replaces a row whose list field changed", + previous: [diff(["a.ts"])], + rows: [diff(["a.ts", "b.ts"])], + expected: ["turn-diff:idle-1"], + reused: [], + replaced: [[0, 0]], + }, + { + name: "replaces a row whose optional field changed", + previous: [spaced("tool")], + rows: [spaced("content")], + expected: ["assistant-part:part:part:a"], + reused: [], + replaced: [[0, 0]], + }, { name: "does not create accidental key collisions", previous: [context("context:a", ["a", "b", "c"])], @@ -209,12 +243,13 @@ describe("reuseTimelineRows", () => { ], reused: [], }, - ])("$name", ({ previous, rows, expected, reused }) => { + ])("$name", ({ previous, rows, expected, reused, replaced }) => { const result = reuseTimelineRows([...previous], [...rows]) expect(keys(result)).toEqual([...expected]) expect(new Set(keys(result)).size).toBe(result.length) reused.forEach(([resultIndex, previousIndex]) => expect(result[resultIndex]).toBe(previous[previousIndex])) + replaced?.forEach(([resultIndex, previousIndex]) => expect(result[resultIndex]).not.toBe(previous[previousIndex])) }) }) diff --git a/packages/session-ui/src/timeline/timeline-row.ts b/packages/session-ui/src/timeline/timeline-row.ts index 3baabbf498d..fca3e8c7158 100644 --- a/packages/session-ui/src/timeline/timeline-row.ts +++ b/packages/session-ui/src/timeline/timeline-row.ts @@ -1,4 +1,4 @@ -import { Data, Equal, Match } from "effect" +import { Data, Match, Predicate } from "effect" export type PartRef = { messageID: string @@ -131,11 +131,63 @@ export namespace TimelineRow { Activity: () => "activity", }) + // Every projection change compares each row with its previous version. Effect's structural `Equal.equals` hashes + // every nested group and ref of the new row first, which cost milliseconds per change on long sessions. A field added + // to a row needs a comparison here, or a change to only that field keeps the previous row. + const sameFields: (row: TimelineRow) => (other: TimelineRow) => boolean = Match.typeTags()({ + TurnGap: () => (other: TimelineRow) => Predicate.isTagged(other, "TurnGap"), + UserMessage: () => (other: TimelineRow) => Predicate.isTagged(other, "UserMessage"), + Shell: (row) => (other: TimelineRow) => Predicate.isTagged(other, "Shell") && other.messageID === row.messageID, + Notice: (row) => (other: TimelineRow) => Predicate.isTagged(other, "Notice") && other.messageID === row.messageID, + TurnDivider: () => (other: TimelineRow) => Predicate.isTagged(other, "TurnDivider"), + AssistantPart: (row) => (other: TimelineRow) => + Predicate.isTagged(other, "AssistantPart") && + other.previousAssistantPart === row.previousAssistantPart && + other.spacing === row.spacing && + sameGroup(other.group, row.group), + Thinking: (row) => (other: TimelineRow) => Predicate.isTagged(other, "Thinking") && sameRef(other.ref, row.ref), + Error: (row) => (other: TimelineRow) => + Predicate.isTagged(other, "Error") && + other.text === row.text && + other.kind === row.kind && + other.resetAt === row.resetAt, + Retry: () => (other: TimelineRow) => Predicate.isTagged(other, "Retry"), + CompactionQueued: (row) => (other: TimelineRow) => + Predicate.isTagged(other, "CompactionQueued") && other.inboxID === row.inboxID, + TurnDiff: (row) => (other: TimelineRow) => + Predicate.isTagged(other, "TurnDiff") && + other.messageID === row.messageID && + other.from === row.from && + other.files.length === row.files.length && + other.files.every((file, index) => file === row.files[index]), + Activity: () => (other: TimelineRow) => Predicate.isTagged(other, "Activity"), + }) + export function equals(a: TimelineRow, b: TimelineRow) { - return Equal.equals(a, b) + return a === b || (a.userMessageID === b.userMessageID && sameFields(a)(b)) } } +function sameGroup(a: PartGroup, b: PartGroup) { + if (a.key !== b.key) return false + + if (a.type === "part") return b.type === "part" && sameRef(a.ref, b.ref) + + return ( + b.type === a.type && + b.refs.length === a.refs.length && + a.refs.every((ref, index) => { + const other = b.refs[index] + + return other !== undefined && sameRef(ref, other) + }) + ) +} + +function sameRef(a: PartRef, b: PartRef) { + return a.messageID === b.messageID && a.partID === b.partID +} + export type TimelineRowMap = { TurnGap: { userMessageID: string } UserMessage: { userMessageID: string }