Open History to see the last commit
1import { deriveDisplayedUserMessageState } from "../lib/terminalContext";
2import { buildInlineTerminalContextText } from "./chat/userMessageTerminalContexts";
3
4const ASSISTANT_CHARS_PER_LINE_FALLBACK = 72;
5const USER_CHARS_PER_LINE_FALLBACK = 56;
6const USER_LINE_HEIGHT_PX = 22;
7const ASSISTANT_LINE_HEIGHT_PX = 22.75;
8// Assistant rows render as markdown content plus a compact timestamp meta line.
9// The DOM baseline is much smaller than the user bubble chrome, so model it
10// separately instead of reusing the old shared constant.
11const ASSISTANT_BASE_HEIGHT_PX = 41;
12const USER_BASE_HEIGHT_PX = 96;
13const ATTACHMENTS_PER_ROW = 2;
14// Full-app browser measurements land closer to a ~116px attachment row once
15// the bubble shrinks to content width, so calibrate the estimate to that DOM.
16const USER_ATTACHMENT_ROW_HEIGHT_PX = 116;
17const USER_BUBBLE_WIDTH_RATIO = 0.8;
18const USER_BUBBLE_HORIZONTAL_PADDING_PX = 32;
19const ASSISTANT_MESSAGE_HORIZONTAL_PADDING_PX = 8;
20const USER_MONO_AVG_CHAR_WIDTH_PX = 8.4;
21const ASSISTANT_AVG_CHAR_WIDTH_PX = 7.2;
22const MIN_USER_CHARS_PER_LINE = 4;
23const MIN_ASSISTANT_CHARS_PER_LINE = 20;
24
25interface TimelineMessageHeightInput {
26 role: "user" | "assistant" | "system";
27 text: string;
28 attachments?: ReadonlyArray<{ id: string }>;
29}
30
31interface TimelineHeightEstimateLayout {
32 timelineWidthPx: number | null;
33}
34
35function estimateWrappedLineCount(text: string, charsPerLine: number): number {
36 if (text.length === 0) return 1;
37
38 // Avoid allocating via split for long logs; iterate once and count wrapped lines.
39 let lines = 0;
40 let currentLineLength = 0;
41 for (let index = 0; index < text.length; index += 1) {
42 if (text.charCodeAt(index) === 10) {
43 lines += Math.max(1, Math.ceil(currentLineLength / charsPerLine));
44 currentLineLength = 0;
45 continue;
46 }
47 currentLineLength += 1;
48 }
49
50 lines += Math.max(1, Math.ceil(currentLineLength / charsPerLine));
51 return lines;
52}
53
54function isFinitePositiveNumber(value: number | null | undefined): value is number {
55 return typeof value === "number" && Number.isFinite(value) && value > 0;
56}
57
58function estimateCharsPerLineForUser(timelineWidthPx: number | null): number {
59 if (!isFinitePositiveNumber(timelineWidthPx)) return USER_CHARS_PER_LINE_FALLBACK;
60 const bubbleWidthPx = timelineWidthPx * USER_BUBBLE_WIDTH_RATIO;
61 const textWidthPx = Math.max(bubbleWidthPx - USER_BUBBLE_HORIZONTAL_PADDING_PX, 0);
62 return Math.max(MIN_USER_CHARS_PER_LINE, Math.floor(textWidthPx / USER_MONO_AVG_CHAR_WIDTH_PX));
63}
64
65function estimateCharsPerLineForAssistant(timelineWidthPx: number | null): number {
66 if (!isFinitePositiveNumber(timelineWidthPx)) return ASSISTANT_CHARS_PER_LINE_FALLBACK;
67 const textWidthPx = Math.max(timelineWidthPx - ASSISTANT_MESSAGE_HORIZONTAL_PADDING_PX, 0);
68 return Math.max(
69 MIN_ASSISTANT_CHARS_PER_LINE,
70 Math.floor(textWidthPx / ASSISTANT_AVG_CHAR_WIDTH_PX),
71 );
72}
73
74export function estimateTimelineMessageHeight(
75 message: TimelineMessageHeightInput,
76 layout: TimelineHeightEstimateLayout = { timelineWidthPx: null },
77): number {
78 if (message.role === "assistant") {
79 const charsPerLine = estimateCharsPerLineForAssistant(layout.timelineWidthPx);
80 const estimatedLines = estimateWrappedLineCount(message.text, charsPerLine);
81 return ASSISTANT_BASE_HEIGHT_PX + estimatedLines * ASSISTANT_LINE_HEIGHT_PX;
82 }
83
84 if (message.role === "user") {
85 const charsPerLine = estimateCharsPerLineForUser(layout.timelineWidthPx);
86 const displayedUserMessage = deriveDisplayedUserMessageState(message.text);
87 const renderedText =
88 displayedUserMessage.contexts.length > 0
89 ? [
90 buildInlineTerminalContextText(displayedUserMessage.contexts),
91 displayedUserMessage.visibleText,
92 ]
93 .filter((part) => part.length > 0)
94 .join(" ")
95 : displayedUserMessage.visibleText;
96 const estimatedLines = estimateWrappedLineCount(renderedText, charsPerLine);
97 const attachmentCount = message.attachments?.length ?? 0;
98 const attachmentRows = Math.ceil(attachmentCount / ATTACHMENTS_PER_ROW);
99 const attachmentHeight = attachmentRows * USER_ATTACHMENT_ROW_HEIGHT_PX;
100 return USER_BASE_HEIGHT_PX + estimatedLines * USER_LINE_HEIGHT_PX + attachmentHeight;
101 }
102
103 // `system` messages are not rendered in the chat timeline, but keep a stable
104 // explicit branch in case they are present in timeline data.
105 const charsPerLine = estimateCharsPerLineForAssistant(layout.timelineWidthPx);
106 const estimatedLines = estimateWrappedLineCount(message.text, charsPerLine);
107 return ASSISTANT_BASE_HEIGHT_PX + estimatedLines * ASSISTANT_LINE_HEIGHT_PX;
108}