Open History to see the last commit
1const ASSISTANT_CHARS_PER_LINE_FALLBACK = 72;
2const USER_CHARS_PER_LINE_FALLBACK = 56;
3const LINE_HEIGHT_PX = 22;
4const ASSISTANT_BASE_HEIGHT_PX = 78;
5const USER_BASE_HEIGHT_PX = 96;
6const ATTACHMENTS_PER_ROW = 2;
7// Attachment thumbnails render with `max-h-[220px]` plus ~8px row gap.
8const USER_ATTACHMENT_ROW_HEIGHT_PX = 228;
9const USER_BUBBLE_WIDTH_RATIO = 0.8;
10const USER_BUBBLE_HORIZONTAL_PADDING_PX = 32;
11const ASSISTANT_MESSAGE_HORIZONTAL_PADDING_PX = 8;
12const USER_MONO_AVG_CHAR_WIDTH_PX = 8.4;
13const ASSISTANT_AVG_CHAR_WIDTH_PX = 7.2;
14const MIN_USER_CHARS_PER_LINE = 4;
15const MIN_ASSISTANT_CHARS_PER_LINE = 20;
16
17interface TimelineMessageHeightInput {
18 role: "user" | "assistant" | "system";
19 text: string;
20 attachments?: ReadonlyArray<{ id: string }>;
21}
22
23interface TimelineHeightEstimateLayout {
24 timelineWidthPx: number | null;
25}
26
27function estimateWrappedLineCount(text: string, charsPerLine: number): number {
28 if (text.length === 0) return 1;
29
30 // Avoid allocating via split for long logs; iterate once and count wrapped lines.
31 let lines = 0;
32 let currentLineLength = 0;
33 for (let index = 0; index < text.length; index += 1) {
34 if (text.charCodeAt(index) === 10) {
35 lines += Math.max(1, Math.ceil(currentLineLength / charsPerLine));
36 currentLineLength = 0;
37 continue;
38 }
39 currentLineLength += 1;
40 }
41
42 lines += Math.max(1, Math.ceil(currentLineLength / charsPerLine));
43 return lines;
44}
45
46function isFinitePositiveNumber(value: number | null | undefined): value is number {
47 return typeof value === "number" && Number.isFinite(value) && value > 0;
48}
49
50function estimateCharsPerLineForUser(timelineWidthPx: number | null): number {
51 if (!isFinitePositiveNumber(timelineWidthPx)) return USER_CHARS_PER_LINE_FALLBACK;
52 const bubbleWidthPx = timelineWidthPx * USER_BUBBLE_WIDTH_RATIO;
53 const textWidthPx = Math.max(bubbleWidthPx - USER_BUBBLE_HORIZONTAL_PADDING_PX, 0);
54 return Math.max(MIN_USER_CHARS_PER_LINE, Math.floor(textWidthPx / USER_MONO_AVG_CHAR_WIDTH_PX));
55}
56
57function estimateCharsPerLineForAssistant(timelineWidthPx: number | null): number {
58 if (!isFinitePositiveNumber(timelineWidthPx)) return ASSISTANT_CHARS_PER_LINE_FALLBACK;
59 const textWidthPx = Math.max(timelineWidthPx - ASSISTANT_MESSAGE_HORIZONTAL_PADDING_PX, 0);
60 return Math.max(
61 MIN_ASSISTANT_CHARS_PER_LINE,
62 Math.floor(textWidthPx / ASSISTANT_AVG_CHAR_WIDTH_PX),
63 );
64}
65
66export function estimateTimelineMessageHeight(
67 message: TimelineMessageHeightInput,
68 layout: TimelineHeightEstimateLayout = { timelineWidthPx: null },
69): number {
70 if (message.role === "assistant") {
71 const charsPerLine = estimateCharsPerLineForAssistant(layout.timelineWidthPx);
72 const estimatedLines = estimateWrappedLineCount(message.text, charsPerLine);
73 return ASSISTANT_BASE_HEIGHT_PX + estimatedLines * LINE_HEIGHT_PX;
74 }
75
76 if (message.role === "user") {
77 const charsPerLine = estimateCharsPerLineForUser(layout.timelineWidthPx);
78 const estimatedLines = estimateWrappedLineCount(message.text, charsPerLine);
79 const attachmentCount = message.attachments?.length ?? 0;
80 const attachmentRows = Math.ceil(attachmentCount / ATTACHMENTS_PER_ROW);
81 const attachmentHeight = attachmentRows * USER_ATTACHMENT_ROW_HEIGHT_PX;
82 return USER_BASE_HEIGHT_PX + estimatedLines * LINE_HEIGHT_PX + attachmentHeight;
83 }
84
85 // `system` messages are not rendered in the chat timeline, but keep a stable
86 // explicit branch in case they are present in timeline data.
87 const charsPerLine = estimateCharsPerLineForAssistant(layout.timelineWidthPx);
88 const estimatedLines = estimateWrappedLineCount(message.text, charsPerLine);
89 return ASSISTANT_BASE_HEIGHT_PX + estimatedLines * LINE_HEIGHT_PX;
90}