xonecas.io
79113
Ma

mini-coding-agent / commit ce94817

feat(tui): show incoming tokens on tool call args

xonecas2026-10-08 ce948178ea56fa47106956852d6944de3d835bdb parent 51ff1d1

8 files changed

src/agent.zig+2−0
@@ -15,6 +15,7 @@ pub const Event = union(enum) {
1515 text: []const u8,
1616 reasoning: []const u8,
1717 tool_call_start: []const u8,
18+ tool_args: usize,
1819 tool_call: struct { name: []const u8, arguments: []const u8 },
1920 tool_output: []const u8,
2021 tool_result: struct { name: []const u8, text: []const u8, is_error: bool, diffs: []const common.FileDiff, body: ?[]const u8 },
@@ -85,6 +86,7 @@ fn onApiEvent(ctx: *anyopaque, event: api.Event) void {
8586 .text => |d| listener.emit(.{ .text = d }),
8687 .reasoning => |d| listener.emit(.{ .reasoning = d }),
8788 .tool_start => |name| listener.emit(.{ .tool_call_start = name }),
89+ .tool_args => |n| listener.emit(.{ .tool_args = n }),
8890 .tool_call => |call| listener.emit(.{ .tool_call = .{ .name = call.name, .arguments = call.arguments } }),
8991 }
9092 }
src/api.zig+6−0
@@ -7,6 +7,7 @@ pub const Event = union(enum) {
77 text: []const u8,
88 reasoning: []const u8,
99 tool_start: []const u8,
10+ tool_args: usize,
1011 tool_call: types.ToolCall,
1112 };
1213
@@ -235,6 +236,11 @@ pub const Call = struct {
235236 sink.emit(.{ .tool_start = self.name });
236237 }
237238 }
239+
240+ pub fn grow(self: *Call, a: std.mem.Allocator, sink: Sink, bytes: []const u8) !void {
241+ try self.args.appendSlice(a, bytes);
242+ sink.emit(.{ .tool_args = self.args.items.len });
243+ }
238244 };
239245
240246 pub fn appendCalls(msg: *types.AssistantMessage, sink: Sink, a: std.mem.Allocator, calls: []const Call, failed: bool) !bool {
src/api/anthropic_messages.zig+1−1
@@ -329,7 +329,7 @@ const State = struct {
329329 if (delta.signature) |s| try block.signature.appendSlice(st.req.pers, s);
330330 } else if (std.mem.eql(u8, dt, "input_json_delta")) {
331331 if (call) |c| {
332- if (delta.partial_json) |s| try c.args.appendSlice(st.req.pers, s);
332+ if (delta.partial_json) |s| try c.grow(st.req.pers, st.sink, s);
333333 }
334334 }
335335 return;
src/api/google_generative_ai.zig+1−1
@@ -246,7 +246,7 @@ fn addCall(st: *State, part: Chunk.Piece) !void {
246246 if (args != .null) {
247247 var out: std.Io.Writer.Allocating = .init(st.req.pers);
248248 try std.json.Stringify.value(args, .{}, &out.writer);
249- try call.args.appendSlice(st.req.pers, out.written());
249+ try call.grow(st.req.pers, st.sink, out.written());
250250 }
251251 }
252252 }
src/api/openai_completions.zig+1−1
@@ -261,7 +261,7 @@ fn handleDelta(st: *State, delta: Chunk.Delta) !void {
261261 if (fn_.name) |name| {
262262 if (name.len > 0) try call.announce(st.req.pers, st.sink, name);
263263 }
264- if (fn_.arguments) |args| try call.args.appendSlice(st.req.pers, args);
264+ if (fn_.arguments) |args| try call.grow(st.req.pers, st.sink, args);
265265 }
266266 }
267267
src/api/openai_responses.zig+3−3
@@ -200,7 +200,7 @@ const State = struct {
200200 if (std.mem.eql(u8, event_type, "response.function_call_arguments.delta")) {
201201 const index = streamIndex(chunk, st.calls.items.len);
202202 if (index < st.calls.items.len) {
203- if (chunk.delta) |s| try st.calls.items[index].args.appendSlice(st.req.pers, s);
203+ if (chunk.delta) |s| try st.calls.items[index].grow(st.req.pers, st.sink, s);
204204 }
205205 return;
206206 }
@@ -210,7 +210,7 @@ const State = struct {
210210 if (index < st.calls.items.len) {
211211 if (chunk.arguments) |s| {
212212 st.calls.items[index].args.clearRetainingCapacity();
213- try st.calls.items[index].args.appendSlice(st.req.pers, s);
213+ try st.calls.items[index].grow(st.req.pers, st.sink, s);
214214 }
215215 }
216216 return;
@@ -233,7 +233,7 @@ const State = struct {
233233 st.sink.emit(.{ .tool_start = call.name });
234234 }
235235 if (call.args.items.len == 0) {
236- if (item.arguments) |s| try call.args.appendSlice(st.req.pers, s);
236+ if (item.arguments) |s| try call.grow(st.req.pers, st.sink, s);
237237 }
238238 return;
239239 }
src/tui/tui.zig+11−2
@@ -79,6 +79,7 @@ const Tui = struct {
7979 phase: agent.Phase = .idle,
8080 detail: ?[]const u8 = null,
8181 writing_tool: ?[]const u8 = null,
82+ writing_bytes: usize = 0,
8283 active: bool = false,
8384 paused: bool = false,
8485 pause_requested: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
@@ -209,7 +210,11 @@ const Tui = struct {
209210 const label = switch (self.phase) {
210211 .preparing => "preparing",
211212 .waiting_model => "waiting for provider",
212- .streaming => if (self.writing_tool) |w| std.fmt.allocPrint(self.s, "writing {s}", .{w}) catch "streaming" else "streaming",
213+ .streaming => if (self.writing_tool) |w| blk: {
214+ if (self.writing_bytes == 0) break :blk std.fmt.allocPrint(self.s, "writing {s}", .{w}) catch "streaming";
215+ const tokenest = (self.writing_bytes + 3) / 4;
216+ break :blk std.fmt.allocPrint(self.s, "writing {s} · ~{s} tok", .{ w, usage_mod.formatTokens(self.s, tokenest) }) catch "streaming";
217+ } else "streaming",
213218 .running_tool => std.fmt.allocPrint(self.s, "running {s}", .{self.detail orelse "tool"}) catch "running tool",
214219 else => "idle",
215220 };
@@ -320,7 +325,11 @@ const Tui = struct {
320325 .summary = tool_view.callSummary(self.a, tc.name, tc.arguments),
321326 }) catch {};
322327 },
323- .tool_call_start => |name| self.keepName(&self.writing_tool, name),
328+ .tool_call_start => |name| {
329+ self.keepName(&self.writing_tool, name);
330+ self.writing_bytes = 0;
331+ },
332+ .tool_args => |n| self.writing_bytes = n,
324333 .tool_output => |chunk| self.activity.feed(chunk),
325334 .message => |am| self.commitMessage(am),
326335 .tool_result => |tr| {
src/tui/usage.zig+1−1
@@ -43,7 +43,7 @@ fn estimateMessageTokens(m: types.Message) u64 {
4343 };
4444 }
4545
46-fn formatTokens(a: std.mem.Allocator, n: u64) []const u8 {
46+pub fn formatTokens(a: std.mem.Allocator, n: u64) []const u8 {
4747 if (n < 1000) return std.fmt.allocPrint(a, "{d}", .{n}) catch "";
4848 const millions = n >= 1_000_000;
4949 const div: f64 = if (millions) 1_000_000.0 else 1000.0;