@@ -8,220 +8,10 @@ import type {
88 } from "@earendil-works/pi-ai";
99 import { estimateTokens, relativeTime } from "./shared";
1010 import { TextPill, theme } from "./tui-components";
11-import type { TUIState } from "./types";
12-
13-function agentMessageNode(msg: AssistantMessage): Node {
14- let thinking = "";
15- let text = "";
16- const toolCalls: Node[] = [];
17-
18- for (const block of msg.content) {
19- if (block.type === "thinking" && block.thinking.length > 0) {
20- thinking += block.thinking;
21- }
22-
23- if (block.type === "text" && block.text.length > 0) {
24- text += block.text;
25- }
26-
27- if (block.type === "toolCall" && block.arguments && block.name) {
28- let text = "";
29- let node: Node | undefined;
30- if ("path" in block.arguments) {
31- text = block.arguments.path;
32- node = Text(text);
33- } else if ("command" in block.arguments) {
34- text = block.arguments.command;
35- node = SyntaxHighlight(text, "bash");
36- } else {
37- text = JSON.stringify(block.arguments);
38- node = Text(text);
39- }
40-
41- toolCalls.push(
42- VStack({ padding: { x: 4 }, gap: 1 }, [
43- TextPill(block.name, theme.white, theme.bblack),
44- node,
45- ]),
46- );
47- }
48- }
49- const textBlocks: Node[] = [];
50- if (thinking.length > 0) {
51- const tokens = estimateTokens(thinking);
52-
53- textBlocks.push(
54- Text(`Thinking... (~${tokens} tokens)`, {
55- fgColor: theme.bblack,
56- italic: true,
57- }),
58- );
59- }
60- if (text.length > 0) {
61- textBlocks.push(SyntaxHighlight(text, "markdown"));
62- }
63- if (toolCalls.length > 0) {
64- textBlocks.push(...toolCalls);
65- }
66- const error =
67- ((msg.stopReason === "error" || msg.stopReason === "aborted") &&
68- msg.errorMessage) ??
69- "Unknown error.";
70- if (error) {
71- textBlocks.push(
72- VStack({ padding: { x: 4 }, gap: 1 }, [
73- TextPill(msg.stopReason, theme.white, theme.bblack),
74- Text(error),
75- ]),
76- );
77- }
78-
79- if (textBlocks.length === 0) {
80- textBlocks.push(
81- Text("Loading...", {
82- fgColor: theme.bblack,
83- italic: true,
84- }),
85- );
86- }
87-
88- return VStack({ gap: 1 }, textBlocks);
89-}
90-
91-function userMessageNode(msg: UserMessage): Node {
92- let text = "";
93- if (typeof msg.content === "string") {
94- text = msg.content;
95- } else {
96- text = msg.content
97- .filter((b) => b.type === "text")
98- .map((b) => b.text)
99- .join("");
100- }
101-
102- // Remove any reminders we might have attached before render
103- // Keep this fast, it runs on the render cycle.
104- text = text
105- .replaceAll(/<system-reminder>[\s\S]*?<\/system-reminder>/g, "")
106- .trimStart();
107-
108- return VStack(
109- {
110- padding: { x: 1, y: 1 },
111- bgColor: theme.bblack,
112- fgColor: theme.white,
113- },
114- [SyntaxHighlight(text, "markdown")],
115- );
116-}
117-
118-function toolMessageNode(msg: ToolResultMessage): Node {
119- // TODO: read tool image output
120- // Output only shows last 10 lines of scroll.
121- const text = msg.content
122- .filter((c) => c.type === "text")
123- .map((c) => c.text)
124- .join("")
125- .trim();
126-
127- return VStack({ padding: { x: 4 }, gap: 1 }, [
128- Text(`~${estimateTokens(text)} tokens, ${text.split("\n").length} lines.`, {
129- fgColor: theme.bblack,
130- }),
131- VStack(
132- {
133- flex: 1,
134- maxHeight: msg.toolName === "edit" ? 20 : 10,
135- overflow: "scroll",
136- scrollOffset: Infinity,
137- onScroll: () => false,
138- },
139- [
140- msg.toolName === "edit"
141- ? SyntaxHighlight(text, "diff")
142- : Text(text, { wrap: "word", fgColor: theme.white }),
143- ],
144- ),
145- ]);
146-}
147-
148-const messageBodyCache = new WeakMap<Message, { key: number; node: Node }>();
149-
150-function messageCacheKey(msg: Message): number {
151- if (msg.role === "assistant") {
152- let key = 0;
153- for (const block of msg.content) {
154- if (block.type === "thinking") key += block.thinking.length;
155- if (block.type === "text") key += block.text.length;
156- if (block.type === "toolCall" && block.arguments)
157- key += JSON.stringify(block.arguments).length;
158- }
159- if (msg.stopReason) key += msg.stopReason.length;
160- if (msg.errorMessage) key += msg.errorMessage.length;
161- return key;
162- }
163- if (msg.role === "user") {
164- if (typeof msg.content === "string") return msg.content.length;
165- return msg.content.reduce(
166- (sum, b) => (b.type === "text" ? sum + b.text.length : sum),
167- 0,
168- );
169- }
170- if (msg.role === "toolResult") {
171- return msg.content.reduce(
172- (sum, b) => (b.type === "text" ? sum + b.text.length : sum),
173- 0,
174- );
175- }
176- return 0;
177-}
178-
179-function cachedMessageBody(msg: Message): Node {
180- const key = messageCacheKey(msg);
181- const cached = messageBodyCache.get(msg);
182- if (cached && cached.key === key) {
183- return cached.node;
184- }
185- const node =
186- msg.role === "assistant"
187- ? agentMessageNode(msg)
188- : msg.role === "user"
189- ? userMessageNode(msg)
190- : msg.role === "toolResult"
191- ? toolMessageNode(msg)
192- : Text("Unknown message?", {
193- wrap: "word",
194- fgColor: theme.bwhite,
195- });
196- messageBodyCache.set(msg, { key, node });
197- return node;
198-}
199-
200-function conversationMessageNode(msg: Message): Node {
201- const label = msg.role === "toolResult" ? `${msg.toolName} result` : msg.role;
202- return VStack({ gap: 1 }, [
203- cachedMessageBody(msg),
204- HStack({ gap: 1, justifyContent: "end" }, [
205- Text(`${relativeTime(msg.timestamp)} ago.`, {
206- fgColor: theme.bblack,
207- italic: true,
208- }),
209- TextPill(label, theme.bwhite, theme.bblack),
210- ]),
211- ]);
212-}
213-
214-const colors = [
215- theme.bgreen,
216- theme.byellow,
217- theme.bblue,
218- theme.bcyan,
219- theme.bmagenta,
220- theme.bred,
221-];
222-const randColor = colors[Math.floor(Math.random() * colors.length)];
11+import type { TUIMessage, TUIState, TUIToolCall } from "./types";
22312
22413 export function emptyState(): Node {
14+ const randColor = theme.bgreen;
22515 return HStack({ flex: 1, alignItems: "center" }, [
22616 VStack({ flex: 1, alignItems: "center", gap: 1 }, [
22717 HStack({ gap: 1 }, [
@@ -254,11 +44,111 @@ export function emptyState(): Node {
25444 ]);
25545 }
25646
47+function ConversationMessageToolCall(call: TUIToolCall) {
48+ let outputNode: Node | null = null;
49+
50+ // Compress read and bash calls
51+ if (call.tool === "read") {
52+ outputNode = Text(`Read ~${estimateTokens(call.output)} tokens`, {
53+ wrap: "word",
54+ });
55+ } else if (call.tool === "bash") {
56+ const tail = call.output.trim().slice(-200);
57+ const blocks: Node[] = [];
58+
59+ blocks.push(Text(tail, { fgColor: theme.white, wrap: "word" }));
60+
61+ if (tail.length !== call.output.trim().length) {
62+ blocks.push(
63+ Text("Showing the last 200 characters", {
64+ fgColor: theme.bblack,
65+ italic: true,
66+ }),
67+ );
68+ }
69+
70+ outputNode = VStack({ width: "100%" }, blocks);
71+ } else if (call.tool === "edit") {
72+ outputNode = SyntaxHighlight(call.output, "patch");
73+ } else {
74+ outputNode = Text(call.output, { wrap: "word" });
75+ }
76+
77+ return VStack({}, [
78+ TextPill(call.tool, theme.black, theme.bwhite),
79+
80+ ...Object.entries(call.args).map(([key, value]) => {
81+ let node: Node | null = Text(String(value));
82+
83+ // Syntax highlight bash args
84+ if (call.tool === "bash") {
85+ node = SyntaxHighlight(String(value), "bash");
86+ }
87+
88+ return HStack({ gap: 1 }, [
89+ Text(`${key}`, { italic: true, fgColor: theme.white }),
90+ node,
91+ ]);
92+ }),
93+
94+ outputNode,
95+ ]);
96+}
97+
98+function ConversationMessage(message: TUIMessage) {
99+ const blocks: Node[] = [];
100+
101+ if (message.thinking) {
102+ // Compress thinking blocks
103+ const estThinkingTok = estimateTokens(message.thinking);
104+ blocks.push(
105+ Text(`Thinking... ~${estThinkingTok} tokens`, {
106+ wrap: "word",
107+ fgColor: theme.bblack,
108+ }),
109+ );
110+ }
111+
112+ if (message.text.length) {
113+ blocks.push(
114+ VStack(
115+ {
116+ width: "100%",
117+ bgColor: message.role === "user" ? theme.bblack : undefined,
118+ padding: { y: 1, x: 1 },
119+ },
120+ [SyntaxHighlight(message.text, "markdown")],
121+ ),
122+ );
123+ }
124+
125+ if (message.toolCalls?.length) {
126+ blocks.push(
127+ VStack({ gap: 1 }, message.toolCalls.map(ConversationMessageToolCall)),
128+ );
129+ }
130+
131+ // Msg footer:
132+ blocks.push(
133+ Text(`on ${message.timestamp} by ${message.role}`, {
134+ fgColor: theme.bblack,
135+ italic: true,
136+ }),
137+ );
138+
139+ return VStack(
140+ {
141+ gap: 1,
142+ },
143+ blocks,
144+ );
145+}
146+
257147 export function Conversation(state: TUIState) {
258148 return VStack(
259149 {
260150 flex: 1,
261- gap: 3,
151+ gap: 2,
262152 overflow: "scroll",
263153 scrollOffset: state.stickToBottom ? Infinity : state.scrollOffset,
264154 onScroll(offset, maxOffset) {
@@ -266,9 +156,6 @@ export function Conversation(state: TUIState) {
266156 state.stickToBottom = offset >= maxOffset;
267157 },
268158 },
269- state.messages.map(conversationMessageNode),
270- // state.tuiMessages.map((m) => {
271- // return VStack({}, []);
272- // }),
159+ state.tuiMessages.map(ConversationMessage),
273160 );
274161 }