xonecas.io
64875
Mc

mini-coder / commit 389cf10

feat: tools in the UI with update in place

xonecas2026-04-22 389cf103aa1ee815c348afe95aa41307f91691b8 parent 4b17f53

2 files changed

src/headless.ts+4−2
@@ -1,8 +1,8 @@
11 import {
22 type Context,
3- type Tool,
43 completeSimple,
54 streamSimple,
5+ type Tool,
66 } from "@mariozechner/pi-ai";
77 import { TASK_PROMPT } from "./agent";
88 import { getApiKey } from "./oauth";
@@ -79,7 +79,9 @@ export async function streamHeadless(
7979 if (toolCalls.length > 0) {
8080 // TODO: Investigate why we get an error: `No output for tool call id XXXX...` when
8181 // we add { apiKey } in this call. And how does it work without it?
82- const cont = await completeSimple(options.model, context, {reasoning: options.effort});
82+ const cont = await completeSimple(options.model, context, {
83+ reasoning: options.effort,
84+ });
8385 context.messages.push(cont);
8486 }
8587
src/tui.ts+71−15
@@ -158,27 +158,76 @@ export async function streamTUI(state: TUIState) {
158158 state.activeState = "answering";
159159 cel.render();
160160 break;
161+
161162 case "thinking_start":
162163 state.activeState = "thinking";
163164 cel.render();
164165 break;
165- case "toolcall_start":
166+
167+ case "toolcall_start": {
166168 state.activeState = "calling_tool";
169+
170+ // Collect tool calls state
171+ const newToolCalls = ev.partial.content.filter(
172+ (c) => c.type === "toolCall",
173+ );
174+
175+ for (const call of newToolCalls) {
176+ const toolMessage: TUIMessage = {
177+ role: "tool",
178+ content: `${call.name}: ${JSON.stringify(call.arguments, null, 4)}`,
179+ id: call.id,
180+ timestamp: Date.now(),
181+ };
182+ state.messages = state.messages.map((msg) => {
183+ if (call.id === msg.id) {
184+ return toolMessage;
185+ }
186+ return msg;
187+ });
188+ }
189+
167190 cel.render();
168- // TODO: tool call id? So I can update the right state.
169- // TUImessages.push({
170- // role: "tool",
171- // content: "Calling tool...",
172- // });
173- // cel.render()
174191 break;
175- case "toolcall_end":
176- // TODO: find pending tool call, update it's entry in TUImessages.
192+ }
193+
194+ case "toolcall_end": {
195+ state.activeState = "idle";
196+
197+ // Collect tool calls state
198+ const finishedToolCalls = ev.partial.content.filter(
199+ (c) => c.type === "toolCall",
200+ );
201+
202+ for (const call of finishedToolCalls) {
203+ const toolMessage: TUIMessage = {
204+ role: "tool",
205+ content: `${call.name}: ${JSON.stringify(call.arguments, null, 4)}`,
206+ id: call.id,
207+ timestamp: Date.now(),
208+ };
209+ let updated = false
210+ state.messages = state.messages.map((msg) => {
211+ if (call.id === msg.id) {
212+ updated = true
213+ return toolMessage;
214+ }
215+ return msg;
216+ });
217+ if (!updated) {
218+ state.messages.push(toolMessage)
219+ }
220+ }
221+
222+ cel.render();
177223 break;
224+ }
225+
178226 case "done":
179227 state.activeState = "idle";
180228 cel.render();
181229 break;
230+
182231 case "error":
183232 state.messages.push({
184233 role: "tool",
@@ -188,7 +237,6 @@ export async function streamTUI(state: TUIState) {
188237 });
189238 state.activeState = "idle";
190239 cel.render();
191- // TODO: find matching tool call if any and update that, or push the new error message.
192240 break;
193241 }
194242 }
@@ -220,12 +268,20 @@ export async function streamTUI(state: TUIState) {
220268 isError: false,
221269 timestamp: Date.now(),
222270 });
223- state.messages.push({
224- role: "tool",
225- content: result.substring(0, result.length > 6000 ? 6000 : undefined),
226- timestamp: Date.now(),
271+
272+ state.messages = state.messages.map((msg) => {
273+ if (call.id === msg.id) {
274+ let truncated = result.length > 6000 ? `${result.substring(0, 6000)}...\n\nTruncated at 6000 chars` : result
275+
276+ return {
277+ ...msg,
278+ content: `${call.name}\n${JSON.stringify(call.arguments, null, 4)}\n\n${truncated}`,
279+ timestamp: Date.now(),
280+ };
281+ }
282+ return msg;
227283 });
228- cel.render()
284+ cel.render();
229285 }
230286
231287 if (toolCalls.length > 0) {