@@ -1,9 +1,9 @@
11 import { basename } from "node:path";
22 import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
3-import type { Message } from "@mariozechner/pi-ai";
3+import type { Message, ToolCall, ToolResultMessage } from "@mariozechner/pi-ai";
44 import { streamAgent, TASK_PROMPT } from "./agent";
55 import { getApiKey } from "./oauth";
6-import { onceEvery, secureRandomString } from "./shared";
6+import { onceEvery, secureRandomString, takeTail } from "./shared";
77 import { bash, runBashTool } from "./tool-bash";
88 import { TextPill, theme } from "./tui-components";
99 import { Conversation } from "./tui-conversation";
@@ -83,6 +83,60 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
8383 );
8484 }
8585
86+// Create the TUIState message for context tool call and result.
87+function createTUIToolMessage(
88+ source: ToolCall | ToolResultMessage,
89+ existing?: Partial<TUIMessage>,
90+): TUIMessage {
91+ // This truncation is TUI only. The file is also
92+ // truncated at tool level to avoid big files being
93+ // sent to the llm. We truncate that even further for the user.
94+ // We only show the tail of the file, which includes if the file
95+ // was truncated at tool level to the user.
96+
97+ // Join text if there is more than one block.
98+ const showLines = 6;
99+ const content = "content" in source ? source.content : [];
100+ const text = content.length
101+ ? content
102+ .map((c) => (c.type === "text" ? c.text : ""))
103+ .filter((c) => Boolean(c))
104+ .join("\n")
105+ : (existing?.content ?? "");
106+ // Grab the tail
107+ let tail = takeTail(text.split("\n"), showLines).join("\n");
108+ // Some commands output without newlines tons of chars.
109+ // if needed to take roughly 6 lines at 100 chars worth of tail.
110+ if (tail.length > showLines * 100) {
111+ tail = tail.slice(showLines * 100 * -1);
112+ }
113+
114+ // Now the similar cut is needed in arguments, but here we care about
115+ // seeing the start of the command, like `cd bla/ && cat ...`
116+ const argsMaxLength = 600; // estimaded by 100 char line width x 10 lines.
117+ let args = existing?.header ?? "Writting...";
118+ if ("arguments" in source && "command" in source.arguments) {
119+ args =
120+ source.arguments?.command?.length > argsMaxLength
121+ ? source.arguments.command.substring(0, argsMaxLength)
122+ : source.arguments.command;
123+ }
124+
125+ const msg: TUIMessage = {
126+ id: "id" in source ? source.id : (existing?.id ?? secureRandomString(8)),
127+ timestamp:
128+ "timestamp" in source
129+ ? source.timestamp
130+ : (existing?.timestamp ?? Date.now()),
131+ role: "tool",
132+ label: "name" in source ? source.name : (existing?.label ?? ""),
133+ header: args,
134+ content: tail,
135+ durationMs: existing?.durationMs ? existing.durationMs : 0,
136+ };
137+ return msg;
138+}
139+
86140 export async function streamTUI(state: TUIState) {
87141 if (state.streaming) return;
88142 state.streaming = true;
@@ -130,17 +184,9 @@ export async function streamTUI(state: TUIState) {
130184 );
131185
132186 for (const call of newToolCalls) {
133- const toolMessage: TUIMessage = {
134- role: "tool",
135- label: call.name.toLocaleUpperCase(),
136- header: call.arguments.command
137- ? call.arguments.command
138- : `Calling ${call.name}...`,
139- content: "",
140- id: call.id,
141- timestamp: Date.now(),
187+ const toolMessage = createTUIToolMessage(call, {
142188 durationMs: dur,
143- };
189+ });
144190 let found = false;
145191 state.messages = state.messages.map((msg) => {
146192 if (call.id === msg.id) {
@@ -166,27 +212,18 @@ export async function streamTUI(state: TUIState) {
166212
167213 // Update tui messages with arguments
168214 for (const call of finishedToolCalls) {
169- const toolMessage: TUIMessage = {
170- role: "tool",
171- label: call.name.toLocaleUpperCase(),
172- header: call.arguments.command
173- ? call.arguments.command
174- : `"Calling ${call.name}..."`,
175- content: "",
176- id: call.id,
177- timestamp: Date.now(),
178- durationMs: dur,
179- };
180215 let updated = false;
181216 state.messages = state.messages.map((msg) => {
182217 if (call.id === msg.id) {
183218 updated = true;
184- return toolMessage;
219+ return createTUIToolMessage(call, { ...msg, durationMs: dur });
185220 }
186221 return msg;
187222 });
188223 if (!updated) {
189- state.messages.push(toolMessage);
224+ state.messages.push(
225+ createTUIToolMessage(call, { durationMs: dur }),
226+ );
190227 }
191228 }
192229
@@ -209,25 +246,7 @@ export async function streamTUI(state: TUIState) {
209246 // Append the output to the existing TUI messages
210247 state.messages = state.messages.map((msg) => {
211248 if (tool.toolCallId === msg.id) {
212- // This truncation is TUI only. The file is also
213- // truncated at tool level to avoid big files being
214- // sent to the llm. We truncate that even further.
215- const truncateLimit = 1000;
216- const result = tool.content
217- .map((c) => (c.type === "text" ? c.text : ""))
218- .filter((c) => Boolean(c))
219- .join("\n");
220- const truncated =
221- result.length > truncateLimit
222- ? `${result.substring(0, truncateLimit)}...\n\nTruncated at ${truncateLimit} of ${result.length} chars`
223- : result;
224-
225- return {
226- ...msg,
227- content: truncated,
228- timestamp: Date.now(),
229- durationMs: dur,
230- };
249+ return createTUIToolMessage(tool, { ...msg, durationMs: dur });
231250 }
232251 return msg;
233252 });