Open History to see the last commit
1import { type MessageId } from "@t3tools/contracts";
2import { type TimelineEntry, type WorkLogEntry } from "../../session-logic";
3import { buildTurnDiffTree, type TurnDiffTreeNode } from "../../lib/turnDiffTree";
4import { type ChatMessage, type ProposedPlan, type TurnDiffSummary } from "../../types";
5import { estimateTimelineMessageHeight } from "../timelineHeight";
6
7export const MAX_VISIBLE_WORK_LOG_ENTRIES = 6;
8
9export interface TimelineDurationMessage {
10 id: string;
11 role: "user" | "assistant" | "system";
12 createdAt: string;
13 completedAt?: string | undefined;
14}
15
16export type MessagesTimelineRow =
17 | {
18 kind: "work";
19 id: string;
20 createdAt: string;
21 groupedEntries: WorkLogEntry[];
22 }
23 | {
24 kind: "message";
25 id: string;
26 createdAt: string;
27 message: ChatMessage;
28 durationStart: string;
29 showCompletionDivider: boolean;
30 }
31 | {
32 kind: "proposed-plan";
33 id: string;
34 createdAt: string;
35 proposedPlan: ProposedPlan;
36 }
37 | { kind: "working"; id: string; createdAt: string | null };
38
39export function computeMessageDurationStart(
40 messages: ReadonlyArray<TimelineDurationMessage>,
41): Map<string, string> {
42 const result = new Map<string, string>();
43 let lastBoundary: string | null = null;
44
45 for (const message of messages) {
46 if (message.role === "user") {
47 lastBoundary = message.createdAt;
48 }
49 result.set(message.id, lastBoundary ?? message.createdAt);
50 if (message.role === "assistant" && message.completedAt) {
51 lastBoundary = message.completedAt;
52 }
53 }
54
55 return result;
56}
57
58export function normalizeCompactToolLabel(value: string): string {
59 return value.replace(/\s+(?:complete|completed)\s*$/i, "").trim();
60}
61
62export function deriveMessagesTimelineRows(input: {
63 timelineEntries: ReadonlyArray<TimelineEntry>;
64 completionDividerBeforeEntryId: string | null;
65 isWorking: boolean;
66 activeTurnStartedAt: string | null;
67}): MessagesTimelineRow[] {
68 const nextRows: MessagesTimelineRow[] = [];
69 const durationStartByMessageId = computeMessageDurationStart(
70 input.timelineEntries.flatMap((entry) => (entry.kind === "message" ? [entry.message] : [])),
71 );
72
73 for (let index = 0; index < input.timelineEntries.length; index += 1) {
74 const timelineEntry = input.timelineEntries[index];
75 if (!timelineEntry) {
76 continue;
77 }
78
79 if (timelineEntry.kind === "work") {
80 const groupedEntries = [timelineEntry.entry];
81 let cursor = index + 1;
82 while (cursor < input.timelineEntries.length) {
83 const nextEntry = input.timelineEntries[cursor];
84 if (!nextEntry || nextEntry.kind !== "work") break;
85 groupedEntries.push(nextEntry.entry);
86 cursor += 1;
87 }
88 nextRows.push({
89 kind: "work",
90 id: timelineEntry.id,
91 createdAt: timelineEntry.createdAt,
92 groupedEntries,
93 });
94 index = cursor - 1;
95 continue;
96 }
97
98 if (timelineEntry.kind === "proposed-plan") {
99 nextRows.push({
100 kind: "proposed-plan",
101 id: timelineEntry.id,
102 createdAt: timelineEntry.createdAt,
103 proposedPlan: timelineEntry.proposedPlan,
104 });
105 continue;
106 }
107
108 nextRows.push({
109 kind: "message",
110 id: timelineEntry.id,
111 createdAt: timelineEntry.createdAt,
112 message: timelineEntry.message,
113 durationStart:
114 durationStartByMessageId.get(timelineEntry.message.id) ?? timelineEntry.message.createdAt,
115 showCompletionDivider:
116 timelineEntry.message.role === "assistant" &&
117 input.completionDividerBeforeEntryId === timelineEntry.id,
118 });
119 }
120
121 if (input.isWorking) {
122 nextRows.push({
123 kind: "working",
124 id: "working-indicator-row",
125 createdAt: input.activeTurnStartedAt,
126 });
127 }
128
129 return nextRows;
130}
131
132export function estimateMessagesTimelineRowHeight(
133 row: MessagesTimelineRow,
134 input: {
135 timelineWidthPx: number | null;
136 expandedWorkGroups?: Readonly<Record<string, boolean>>;
137 turnDiffSummaryByAssistantMessageId?: ReadonlyMap<MessageId, TurnDiffSummary>;
138 },
139): number {
140 switch (row.kind) {
141 case "work":
142 return estimateWorkRowHeight(row, input);
143 case "proposed-plan":
144 return estimateTimelineProposedPlanHeight(row.proposedPlan);
145 case "working":
146 return 40;
147 case "message": {
148 let estimate = estimateTimelineMessageHeight(row.message, {
149 timelineWidthPx: input.timelineWidthPx,
150 });
151 const turnDiffSummary = input.turnDiffSummaryByAssistantMessageId?.get(row.message.id);
152 if (turnDiffSummary && turnDiffSummary.files.length > 0) {
153 estimate += estimateChangedFilesCardHeight(turnDiffSummary);
154 }
155 return estimate;
156 }
157 }
158}
159
160function estimateWorkRowHeight(
161 row: Extract<MessagesTimelineRow, { kind: "work" }>,
162 input: {
163 expandedWorkGroups?: Readonly<Record<string, boolean>>;
164 },
165): number {
166 const isExpanded = input.expandedWorkGroups?.[row.id] ?? false;
167 const hasOverflow = row.groupedEntries.length > MAX_VISIBLE_WORK_LOG_ENTRIES;
168 const visibleEntries =
169 hasOverflow && !isExpanded ? MAX_VISIBLE_WORK_LOG_ENTRIES : row.groupedEntries.length;
170 const onlyToolEntries = row.groupedEntries.every((entry) => entry.tone === "tool");
171 const showHeader = hasOverflow || !onlyToolEntries;
172
173 // Card chrome, optional header, and one compact work-entry row per visible entry.
174 return 28 + (showHeader ? 26 : 0) + visibleEntries * 32;
175}
176
177function estimateTimelineProposedPlanHeight(proposedPlan: ProposedPlan): number {
178 const estimatedLines = Math.max(1, Math.ceil(proposedPlan.planMarkdown.length / 72));
179 return 120 + Math.min(estimatedLines * 22, 880);
180}
181
182function estimateChangedFilesCardHeight(turnDiffSummary: TurnDiffSummary): number {
183 const treeNodes = buildTurnDiffTree(turnDiffSummary.files);
184 const visibleNodeCount = countTurnDiffTreeNodes(treeNodes);
185
186 // Card chrome: top/bottom padding, header row, and tree spacing.
187 return 60 + visibleNodeCount * 25;
188}
189
190function countTurnDiffTreeNodes(nodes: ReadonlyArray<TurnDiffTreeNode>): number {
191 let count = 0;
192 for (const node of nodes) {
193 count += 1;
194 if (node.kind === "directory") {
195 count += countTurnDiffTreeNodes(node.children);
196 }
197 }
198 return count;
199}