@@ -1,10 +1,29 @@
1-import { cel, HStack, Text, VStack } from "@cel-tui/core";
1+import {
2+ HStack,
3+ measureContentHeight,
4+ type Node,
5+ Text,
6+ VStack,
7+} from "@cel-tui/core";
28 import { elapsedTime, relativeTime } from "./shared";
39 import { TextPill, theme } from "./tui-components";
410 import { memoizedSyntaxHighlight } from "./tui-syntax-highlight";
511 import type { TUIMessage, TUIState } from "./types";
612
7-function agentMessageNode(msg: TUIMessage) {
13+const gap = 1;
14+
15+type ConversationLayout = {
16+ messages: TUIMessage[];
17+ count: number;
18+ width: number;
19+ heights: number[];
20+ tops: number[];
21+ totalHeight: number;
22+};
23+
24+let cachedLayout: ConversationLayout | undefined;
25+
26+function agentMessageNode(msg: TUIMessage): Node {
827 return memoizedSyntaxHighlight(
928 (msg.id ? msg.id : "") + String(msg.timestamp),
1029 msg.content,
@@ -12,7 +31,7 @@ function agentMessageNode(msg: TUIMessage) {
1231 );
1332 }
1433
15-function userMessageNode(msg: TUIMessage) {
34+function userMessageNode(msg: TUIMessage): Node {
1635 return VStack(
1736 {
1837 padding: { x: 1, y: 1 },
@@ -29,7 +48,7 @@ function userMessageNode(msg: TUIMessage) {
2948 );
3049 }
3150
32-function toolMessageNode(msg: TUIMessage) {
51+function toolMessageNode(msg: TUIMessage): Node {
3352 return VStack({ gap: 1 }, [
3453 memoizedSyntaxHighlight(
3554 msg.id ?? String(msg.timestamp),
@@ -40,50 +59,135 @@ function toolMessageNode(msg: TUIMessage) {
4059 ]);
4160 }
4261
62+function conversationMessageNode(msg: TUIMessage): Node {
63+ const dur = msg.durationMs
64+ ? `Took ${elapsedTime(msg.durationMs / 1000)}`
65+ : "-";
66+
67+ const body =
68+ msg.role === "agent"
69+ ? agentMessageNode(msg)
70+ : msg.role === "user"
71+ ? userMessageNode(msg)
72+ : msg.role === "tool"
73+ ? toolMessageNode(msg)
74+ : Text(msg.content, { wrap: "word", fgColor: theme.bwhite });
75+
76+ return VStack({ gap: 1 }, [
77+ body,
78+ HStack({ gap: 1, justifyContent: "end" }, [
79+ Text(`${relativeTime(msg.timestamp)} ago.`, {
80+ fgColor: theme.bblack,
81+ italic: true,
82+ }),
83+ Text(dur, {
84+ fgColor: theme.bblack,
85+ italic: true,
86+ }),
87+ TextPill(msg.label ?? msg.role, theme.bwhite, theme.bblack),
88+ ]),
89+ ]);
90+}
91+
92+function getConversationLayout(
93+ messages: TUIMessage[],
94+ width: number,
95+): ConversationLayout {
96+ if (
97+ cachedLayout &&
98+ cachedLayout.messages === messages &&
99+ cachedLayout.count === messages.length &&
100+ cachedLayout.width === width
101+ ) {
102+ return cachedLayout;
103+ }
104+
105+ const heights = messages.map((msg) =>
106+ measureContentHeight(conversationMessageNode(msg), { width }),
107+ );
108+
109+ const tops: number[] = [];
110+ let totalHeight = 0;
111+
112+ for (let i = 0; i < heights.length; i++) {
113+ tops.push(totalHeight);
114+ totalHeight += heights[i];
115+ if (i < heights.length - 1) {
116+ totalHeight += gap;
117+ }
118+ }
119+
120+ cachedLayout = {
121+ messages,
122+ count: messages.length,
123+ width,
124+ heights,
125+ tops,
126+ totalHeight,
127+ };
128+
129+ return cachedLayout;
130+}
131+
43132 export function Conversation(state: TUIState) {
44- // TODO: use scroll callbacks to make the conversation a virtual list,
45- // only showing a 12 messages window based the scroll position.
46- // currently we just don't show earlier messages :(
133+ // The root viewport only adds padding.x = 1 on each side.
134+ const width = Math.max(20, (process.stdout.columns ?? 80) - 2);
135+ const viewportEstimate = Math.max(10, (process.stdout.rows ?? 24) - 8);
136+ const overscan = viewportEstimate;
137+ const { heights, tops, totalHeight } = getConversationLayout(
138+ state.messages,
139+ width,
140+ );
141+
142+ const scrollTop = state.stickToBottom
143+ ? Math.max(0, totalHeight - viewportEstimate)
144+ : Math.max(0, Math.min(state.scrollOffset, totalHeight));
145+
146+ const minY = Math.max(0, scrollTop - overscan);
147+ const maxY = scrollTop + viewportEstimate + overscan;
148+
149+ let start = 0;
150+ while (start < heights.length && tops[start] + heights[start] <= minY) {
151+ start++;
152+ }
153+
154+ let end = start;
155+ while (end < heights.length && tops[end] < maxY) {
156+ end++;
157+ }
158+
159+ // Always render at least one item when possible.
160+ if (end === start && end < heights.length) {
161+ end++;
162+ }
163+
164+ const children: Node[] = [];
165+
166+ // Spacer before first visible item.
167+ if (start > 0) {
168+ children.push(VStack({ height: tops[start] - gap }, []));
169+ }
170+
171+ children.push(
172+ ...state.messages.slice(start, end).map(conversationMessageNode),
173+ );
174+
175+ // Spacer after last visible item.
176+ if (end < heights.length) {
177+ children.push(VStack({ height: totalHeight - tops[end] }, []));
178+ }
179+
47180 return VStack(
48181 {
49182 flex: 1,
50- gap: 1,
183+ gap,
51184 overflow: "scroll",
52185 scrollOffset: state.stickToBottom ? Infinity : state.scrollOffset,
53186 onScroll(offset, maxOffset) {
54187 state.scrollOffset = offset;
55188 state.stickToBottom = offset >= maxOffset;
56- cel.render();
57189 },
58190 },
59- [
60- ...state.messages.slice(-12).map((msg) => {
61- const dur = msg.durationMs
62- ? `Took ${elapsedTime(msg.durationMs / 1000)}`
63- : "-";
64-
65- return VStack({ gap: 1 }, [
66- msg.role === "agent"
67- ? agentMessageNode(msg)
68- : msg.role === "user"
69- ? userMessageNode(msg)
70- : msg.role === "tool"
71- ? toolMessageNode(msg)
72- : Text(msg.content, { wrap: "word", fgColor: theme.bwhite }),
73-
74- HStack({ gap: 1, justifyContent: "end" }, [
75- Text(`${relativeTime(msg.timestamp)} ago.`, {
76- fgColor: theme.bblack,
77- italic: true,
78- }),
79- Text(dur, {
80- fgColor: theme.bblack,
81- italic: true,
82- }),
83- TextPill(msg.label ?? msg.role, theme.bwhite, theme.bblack),
84- ]),
85- ]);
86- }),
87- ],
191+ children,
88192 );
89193 }