Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

timelineHeight.ts

103 lines4.2 KB

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