@@ -1,57 +1,14 @@
11 import { basename } from "node:path";
2-import type { Color } from "@cel-tui/core";
3-import { cel, HStack, ProcessTerminal, Text, VStack } from "@cel-tui/core";
2+import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
43 import type { Message } from "@mariozechner/pi-ai";
54 import { streamAgent, TASK_PROMPT } from "./agent";
65 import { getApiKey } from "./oauth";
76 import { bash, runBashTool } from "./tool-bash";
7+import { TextPill, theme } from "./tui-components";
8+import { Conversation } from "./tui-conversation";
89 import { Editor } from "./tui-editor";
910 import type { ToolAndRunner, TUIMessage, TUIState } from "./types";
1011
11-const theme = {
12- black: "color00" as Color,
13- bblack: "color08" as Color,
14-
15- red: "color01" as Color,
16- bred: "color09" as Color,
17-
18- green: "color02" as Color,
19- bgreen: "color10" as Color,
20-
21- yellow: "color03" as Color,
22- byellow: "color11" as Color,
23-
24- blue: "color04" as Color,
25- bblue: "color12" as Color,
26-
27- magenta: "color05" as Color,
28- bmagenta: "color13" as Color,
29-
30- cyan: "color06" as Color,
31- bcyan: "color14" as Color,
32-
33- white: "color07" as Color,
34- bwhite: "color15" as Color,
35-};
36-
37-// Examples:
38-//
39-// TextPill("mini-coder", theme.bblack, theme.black),
40-// TextPill("mini-coder", theme.bred, theme.red),
41-// TextPill("mini-coder", theme.bgreen, theme.green),
42-// TextPill("mini-coder", theme.byellow, theme.yellow),
43-// TextPill("mini-coder", theme.bblue, theme.blue),
44-// TextPill("mini-coder", theme.bmagenta, theme.magenta),
45-// TextPill("mini-coder", theme.bcyan, theme.cyan),
46-// TextPill("mini-coder", theme.bwhite, theme.white),
47-function TextPill(content: string, fgColor: Color, bgColor: Color) {
48- return HStack({ gap: 1 }, [
49- HStack({ bgColor, padding: { x: 1 } }, [
50- Text(content, { bold: true, fgColor }),
51- ]),
52- ]);
53-}
54-
5512 export function initTUI(state: TUIState, leave: (s: string) => void) {
5613 const cwd = basename(process.cwd());
5714
@@ -72,14 +29,7 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
7229 },
7330 },
7431 [
75- VStack({ flex: 1, gap: 1, overflow: "scroll" }, [
76- ...state.messages.map((msg) => {
77- return VStack({ gap: 1 }, [
78- TextPill(msg.role, theme.bwhite, theme.bblack),
79- Text(msg.content, { wrap: "word" }),
80- ]);
81- }),
82- ]),
32+ Conversation(state),
8333
8434 HStack({ gap: 1 }, [
8535 TextPill(state.options.model.name, theme.bblack, theme.bwhite),
@@ -122,8 +72,11 @@ export async function streamTUI(state: TUIState) {
12272 content: state.prompt || "",
12373 timestamp: Date.now(),
12474 };
125- const messages: Message[] = [userPrompt as Message];
12675 state.messages.push(userPrompt as TUIMessage);
76+ let messages: Message[] = [userPrompt as Message];
77+ if (state.context) {
78+ messages = [...state.context.messages, ...messages];
79+ }
12780 state.prompt = "";
12881 cel.render();
12982
@@ -148,7 +101,7 @@ export async function streamTUI(state: TUIState) {
148101 case "toolcall_start": {
149102 state.activeState = "calling_tool";
150103
151- // Collect tool calls state
104+ // Create initial tool tui messages
152105 const newToolCalls = ev.partial.content.filter(
153106 (c) => c.type === "toolCall",
154107 );
@@ -160,12 +113,15 @@ export async function streamTUI(state: TUIState) {
160113 id: call.id,
161114 timestamp: Date.now(),
162115 };
116+ let found = false;
163117 state.messages = state.messages.map((msg) => {
164118 if (call.id === msg.id) {
119+ found = true;
165120 return toolMessage;
166121 }
167122 return msg;
168123 });
124+ if (!found) state.messages.push(toolMessage);
169125 }
170126
171127 cel.render();
@@ -180,6 +136,7 @@ export async function streamTUI(state: TUIState) {
180136 (c) => c.type === "toolCall",
181137 );
182138
139+ // Update tui messages with arguments
183140 for (const call of finishedToolCalls) {
184141 const toolMessage: TUIMessage = {
185142 role: "tool",
@@ -210,24 +167,31 @@ export async function streamTUI(state: TUIState) {
210167 break;
211168
212169 case "error":
170+ state.activeState = "idle";
171+ break;
213172 }
214173 },
215174
216175 (tool) => {
176+ // Append the output to the existing TUI messages
217177 state.messages = state.messages.map((msg) => {
218178 if (tool.toolCallId === msg.id) {
179+ // This truncation is TUI only. The file is also
180+ // truncated at tool level to avoid big files being
181+ // sent to the llm. We truncate that even further.
182+ const truncateLimit = 1000;
219183 const result = tool.content
220184 .map((c) => (c.type === "text" ? c.text : ""))
221185 .filter((c) => Boolean(c))
222186 .join("\n");
223187 const truncated =
224- result.length > 6000
225- ? `${result.substring(0, 6000)}...\n\nTruncated at 6000 chars`
188+ result.length > truncateLimit
189+ ? `${result.substring(0, truncateLimit)}...\n\nTruncated at ${truncateLimit} of ${result.length} chars`
226190 : result;
227191
228192 return {
229193 ...msg,
230- content: `${tool.toolName}:\n\n${truncated}`,
194+ content: `${msg.content}\n\n${truncated}`,
231195 timestamp: Date.now(),
232196 };
233197 }
@@ -237,16 +201,25 @@ export async function streamTUI(state: TUIState) {
237201 },
238202
239203 (msg, context, dur) => {
240- const text = msg.content
241- .filter((c) => c.type === "text")
242- .map((c) => c.text)
243- .join("\n");
244- state.messages.push({
245- role: "agent",
246- content: text,
247- timestamp: Date.now(),
248- durationMs: dur,
249- });
204+ if (msg.errorMessage) {
205+ state.messages.push({
206+ role: "agent",
207+ content: `Error (${msg.stopReason}): ${msg.errorMessage}`,
208+ timestamp: Date.now(),
209+ });
210+ } else {
211+ const text = msg.content
212+ .filter((c) => c.type === "text")
213+ .map((c) => c.text)
214+ .join("\n");
215+ state.messages.push({
216+ role: "agent",
217+ content: text,
218+ timestamp: Date.now(),
219+ durationMs: dur,
220+ });
221+ }
222+
250223 state.streaming = false;
251224 state.context = context;
252225 cel.render();