@@ -3,6 +3,7 @@ import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
33 import type { Message } from "@mariozechner/pi-ai";
44 import { streamAgent, TASK_PROMPT } from "./agent";
55 import { getApiKey } from "./oauth";
6+import { onceEvery, secureRandomString } from "./shared";
67 import { bash, runBashTool } from "./tool-bash";
78 import { TextPill, theme } from "./tui-components";
89 import { Conversation } from "./tui-conversation";
@@ -12,6 +13,23 @@ import type { ToolAndRunner, TUIMessage, TUIState } from "./types";
1213 export function initTUI(state: TUIState, leave: (s: string) => void) {
1314 const cwd = basename(process.cwd());
1415
16+ // TODO: move to components, have a function return the everyFn and current spinner
17+ const spinnerFrames = ["⠤", "⠆", "⠒", "⠰"];
18+ let spinnerTick = 0;
19+ const spinnerEvery = onceEvery(5, () => spinnerTick++);
20+ const currentSpinner = () =>
21+ spinnerFrames[spinnerTick % spinnerFrames.length];
22+
23+ // Because we don't render often, the ui feels unresponsive at times.
24+ // This ensure 60fps, and excessive calls get coalesced in cel-tui.
25+ const fps = 60;
26+ const baseFramerateIntervalId = setInterval(() => {
27+ if (state.streaming) {
28+ spinnerEvery();
29+ }
30+ cel.render();
31+ }, 1000 / fps);
32+
1533 cel.init(new ProcessTerminal());
1634 cel.setTitle(`mc | ${cwd}`);
1735
@@ -23,6 +41,7 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
2341 padding: { x: 1, y: 1 },
2442 onKeyPress: (key) => {
2543 if (key === "ctrl+q" || key === "ctrl+c" || key === "ctrl+d") {
44+ clearInterval(baseFramerateIntervalId);
2645 cel.stop();
2746 leave("Done.");
2847 }
@@ -35,6 +54,9 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
3554 TextPill(state.options.model.name, theme.bblack, theme.bwhite),
3655 TextPill(`../${cwd}`, theme.bwhite, theme.bblack),
3756 VStack({ flex: 1 }, []),
57+ state.streaming
58+ ? TextPill(currentSpinner(), theme.bblack, theme.bwhite)
59+ : TextPill(currentSpinner(), theme.bwhite, theme.bblack),
3860 TextPill(state.activeState, theme.bwhite, theme.bblack),
3961 ]),
4062
@@ -71,6 +93,7 @@ export async function streamTUI(state: TUIState) {
7193 role: "user",
7294 content: state.prompt || "",
7395 timestamp: Date.now(),
96+ id: secureRandomString(8),
7497 };
7598 state.messages.push(userPrompt as TUIMessage);
7699 let messages: Message[] = [userPrompt as Message];
@@ -86,7 +109,7 @@ export async function streamTUI(state: TUIState) {
86109 TASK_PROMPT,
87110 messages,
88111 state.options,
89- (ev) => {
112+ (ev, dur) => {
90113 switch (ev.type) {
91114 case "text_start":
92115 state.activeState = "answering";
@@ -109,9 +132,14 @@ export async function streamTUI(state: TUIState) {
109132 for (const call of newToolCalls) {
110133 const toolMessage: TUIMessage = {
111134 role: "tool",
112- content: `${call.name}: ${JSON.stringify(call.arguments, null, 4)}`,
135+ label: call.name.toLocaleUpperCase(),
136+ header: call.arguments.command
137+ ? call.arguments.command
138+ : `Calling ${call.name}...`,
139+ content: "",
113140 id: call.id,
114141 timestamp: Date.now(),
142+ durationMs: dur,
115143 };
116144 let found = false;
117145 state.messages = state.messages.map((msg) => {
@@ -140,9 +168,14 @@ export async function streamTUI(state: TUIState) {
140168 for (const call of finishedToolCalls) {
141169 const toolMessage: TUIMessage = {
142170 role: "tool",
143- content: `${call.name}: ${JSON.stringify(call.arguments, null, 4)}`,
171+ label: call.name.toLocaleUpperCase(),
172+ header: call.arguments.command
173+ ? call.arguments.command
174+ : `"Calling ${call.name}..."`,
175+ content: "",
144176 id: call.id,
145177 timestamp: Date.now(),
178+ durationMs: dur,
146179 };
147180 let updated = false;
148181 state.messages = state.messages.map((msg) => {
@@ -172,7 +205,7 @@ export async function streamTUI(state: TUIState) {
172205 }
173206 },
174207
175- (tool) => {
208+ (tool, dur) => {
176209 // Append the output to the existing TUI messages
177210 state.messages = state.messages.map((msg) => {
178211 if (tool.toolCallId === msg.id) {
@@ -191,8 +224,9 @@ export async function streamTUI(state: TUIState) {
191224
192225 return {
193226 ...msg,
194- content: `${msg.content}\n\n${truncated}`,
227+ content: truncated,
195228 timestamp: Date.now(),
229+ durationMs: dur,
196230 };
197231 }
198232 return msg;
@@ -203,9 +237,11 @@ export async function streamTUI(state: TUIState) {
203237 (msg, context, dur) => {
204238 if (msg.errorMessage) {
205239 state.messages.push({
240+ id: secureRandomString(8),
206241 role: "agent",
207242 content: `Error (${msg.stopReason}): ${msg.errorMessage}`,
208243 timestamp: Date.now(),
244+ durationMs: dur,
209245 });
210246 } else {
211247 const text = msg.content
@@ -213,6 +249,7 @@ export async function streamTUI(state: TUIState) {
213249 .map((c) => c.text)
214250 .join("\n");
215251 state.messages.push({
252+ id: secureRandomString(8),
216253 role: "agent",
217254 content: text,
218255 timestamp: Date.now(),