xonecas.io
79113
Ma

mini-coding-agent / commit 8133a70

chore: remove dead code, duplication. Consolidate code paths

xonecas2026-10-04 8133a701599730baf5e5f91653696879fc53725d parent cb624f6

22 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

src/agent.zig+13−27
@@ -37,8 +37,8 @@ pub const Listener = struct {
3737 };
3838
3939 /// How the agent reaches the projection while a turn is in flight. Headless
40-/// uses `NO_INTERACTION`; the TUI supplies its own. `requestSteering` blocks
41-/// the turn thread until the user submits a line or the turn is cancelled.
40+/// passes `null`; the TUI supplies its own. `request_steering` blocks the turn
41+/// thread until the user submits a line or the turn is cancelled.
4242 pub const Interaction = struct {
4343 ctx: *anyopaque,
4444 is_pause_requested: *const fn (ctx: *anyopaque) bool,
@@ -56,21 +56,6 @@ pub const Interaction = struct {
5656 }
5757 };
5858
59-pub const NO_INTERACTION = Interaction{
60- .ctx = undefined,
61- .is_pause_requested = noPause,
62- .clear_pause = noop,
63- .request_steering = noSteering,
64-};
65-
66-fn noPause(_: *anyopaque) bool {
67- return false;
68-}
69-fn noop(_: *anyopaque) void {}
70-fn noSteering(_: *anyopaque) []const u8 {
71- return "";
72-}
73-
7459 pub const Options = struct {
7560 /// Long-lived allocator for the transcript and its messages.
7661 a: std.mem.Allocator,
@@ -115,13 +100,14 @@ fn steer(opts: Options, messages: *std.ArrayList(types.Message), content: []cons
115100
116101 /// Returns the steering typed while paused, or "" when there was no pause;
117102 /// null once the turn has been cancelled.
118-fn pauseStep(run: Run, listener: Listener) ?[]const u8 {
103+fn pauseStep(run: Run) ?[]const u8 {
119104 if (run.opts.cancel.load(.acquire)) return null;
120- if (!run.interaction.pauseRequested()) return "";
121- run.interaction.clearPause();
122- listener.emit(.{ .phase = .{ .phase = .pausing } });
123- const steering = run.interaction.requestSteering();
124- listener.emit(.{ .phase = .{ .phase = .idle } });
105+ const interaction = run.interaction orelse return "";
106+ if (!interaction.pauseRequested()) return "";
107+ interaction.clearPause();
108+ run.listener.emit(.{ .phase = .{ .phase = .pausing } });
109+ const steering = interaction.requestSteering();
110+ run.listener.emit(.{ .phase = .{ .phase = .idle } });
125111 if (run.opts.cancel.load(.acquire)) return null;
126112 return steering;
127113 }
@@ -129,14 +115,14 @@ fn pauseStep(run: Run, listener: Listener) ?[]const u8 {
129115 const Run = struct {
130116 opts: Options,
131117 messages: *std.ArrayList(types.Message),
132- interaction: Interaction,
118+ interaction: ?Interaction,
133119 listener: Listener,
134120 };
135121
136122 /// Runs one turn to completion, appending to `messages`. Returns after the
137123 /// model stops without tool calls, after a cancellation, or after an error;
138124 /// every outcome is reported through the listener.
139-pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interaction: Interaction, listener: Listener) void {
125+pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interaction: ?Interaction, listener: Listener) void {
140126 const run = Run{ .opts = opts, .messages = messages, .interaction = interaction, .listener = listener };
141127
142128 const model = opts.model orelse {
@@ -153,7 +139,7 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
153139
154140 // Cancellation and steering land at the step boundary, never between an
155141 // assistant turn and its tool results.
156- const steering = pauseStep(run, listener) orelse {
142+ const steering = pauseStep(run) orelse {
157143 listener.emit(.cancelled);
158144 return;
159145 };
@@ -232,7 +218,7 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
232218 if (block != .tool_call) continue;
233219 const call = block.tool_call;
234220
235- const step = pauseStep(run, listener) orelse {
221+ const step = pauseStep(run) orelse {
236222 listener.emit(.cancelled);
237223 return;
238224 };
src/api.zig+281−31
@@ -4,6 +4,8 @@
44 const std = @import("std");
55 const types = @import("types.zig");
66 const http = @import("http.zig");
7+const json = @import("json.zig");
8+const util = @import("util.zig");
79
810 pub const Event = union(enum) {
911 /// Assistant text delta.
@@ -41,6 +43,112 @@ pub const Request = struct {
4143 cancel: *const std.atomic.Value(bool),
4244 };
4345
46+/// An untrusted stream index is a hint, never a size. Anything past this is
47+/// treated like a missing index so a hostile chunk cannot force an allocation.
48+pub const max_stream_index = 10_000;
49+
50+/// How one wire reaches its provider: the URL, the key header, and the body
51+/// encoder. Everything else about the request is the same for all four.
52+pub const Wire = struct {
53+ /// The header the api key travels in, and the prefix its value carries.
54+ /// An empty key sends no header at all.
55+ key_header: []const u8,
56+ key_prefix: []const u8 = "",
57+ /// A protocol version header, sent verbatim when its name is not empty.
58+ version: [2][]const u8 = .{ "", "" },
59+ /// The path appended to the model's base URL, after any trailing slash.
60+ /// `build_url` overrides it for a provider whose URL is not base plus path.
61+ path: []const u8 = "",
62+ build_url: ?*const fn (std.mem.Allocator, Request) anyerror![]u8 = null,
63+ build_body: *const fn (Request) anyerror![]u8,
64+};
65+
66+/// The request life cycle every wire shares, over a `State` that carries
67+/// `req`, `sink`, `arena`, `msg`, `stream_error` and `failed`, and handles each
68+/// payload through `State.handle`. `finalize` moves the accumulated state into
69+/// `msg`.
70+///
71+/// A stream that fails or is cancelled still finalizes first: the text the
72+/// model did stream belongs in the transcript, and dropping it would lose a
73+/// turn the user can see and cannot recover. A tool call whose arguments never
74+/// finished is not emitted, so a partial call is never handed to a tool.
75+pub fn run(
76+ comptime State: type,
77+ comptime wire: Wire,
78+ req: Request,
79+ sink: Sink,
80+ finalize: *const fn (st: *State) std.mem.Allocator.Error!void,
81+) std.mem.Allocator.Error!types.AssistantMessage {
82+ var msg = newAssistant(req);
83+
84+ var arena = std.heap.ArenaAllocator.init(req.scratch);
85+ defer arena.deinit();
86+
87+ const url = if (wire.build_url) |build|
88+ build(req.scratch, req) catch return error.OutOfMemory
89+ else
90+ buildUrl(req.scratch, req.model.base_url, wire.path) catch return error.OutOfMemory;
91+ const hdrs = try wireHeaders(req.scratch, req, wire);
92+ const body = wire.build_body(req) catch return error.OutOfMemory;
93+
94+ var st = State{ .req = req, .sink = sink, .arena = &arena, .msg = &msg };
95+
96+ // A stream that did not run to the end can leave a tool call with a name
97+ // but truncated arguments. Such a call is not a call the model finished
98+ // making, so `finalize` drops it rather than emit arguments to run; the
99+ // transport's own stop reason is the one the message keeps.
100+ const complete = try post(req, url, hdrs, body, .{ .ctx = &st, .onEvent = onData(State) }, &msg);
101+ st.failed = !complete;
102+ const reason = msg.stop_reason;
103+ const message = msg.error_message;
104+ try finalize(&st);
105+ if (!complete) {
106+ msg.stop_reason = reason;
107+ msg.error_message = message;
108+ }
109+
110+ if (st.stream_error) |text| {
111+ msg.stop_reason = .err;
112+ msg.error_message = try req.pers.dupe(u8, text);
113+ }
114+ return msg;
115+}
116+
117+/// The SSE hook every wire shares: parse one payload into `st.arena` and hand
118+/// the object to `State.handle`. The arena is reset before the next payload, so
119+/// nothing may keep a pointer into it.
120+fn onData(comptime State: type) *const fn (ctx: *anyopaque, data: []const u8) void {
121+ return struct {
122+ fn hook(ctx: *anyopaque, data: []const u8) void {
123+ const st: *State = @ptrCast(@alignCast(ctx));
124+ defer _ = st.arena.reset(.retain_capacity);
125+ // The sentinel OpenAI-compatible streams send in place of a final
126+ // event. It is not JSON, and it is not a failure.
127+ if (std.mem.eql(u8, std.mem.trim(u8, data, " \r\n"), "[DONE]")) return;
128+ const root = std.json.parseFromSliceLeaky(std.json.Value, st.arena.allocator(), data, .{}) catch {
129+ if (st.stream_error == null) st.stream_error = "stream: invalid JSON chunk";
130+ return;
131+ };
132+ if (root != .object) return;
133+ State.handle(st, root.object) catch {
134+ if (st.stream_error == null) st.stream_error = "stream: out of memory";
135+ };
136+ }
137+ }.hook;
138+}
139+
140+/// Grows `list` so `index` exists, and returns the element there. An untrusted
141+/// stream index is a hint, never a size: anything outside the sane range
142+/// appends instead, so a hostile chunk cannot force an allocation.
143+pub fn blockAt(comptime T: type, list: *std.ArrayList(T), a: std.mem.Allocator, index: i64) !*T {
144+ const idx: usize = if (index >= 0 and index <= max_stream_index)
145+ @intCast(index)
146+ else
147+ list.items.len;
148+ while (list.items.len <= idx) try list.append(a, .{});
149+ return &list.items[idx];
150+}
151+
44152 /// Runs one streaming request. Transport and protocol failures are recorded in
45153 /// the returned message's `stop_reason`/`error_message`; only a failure to
46154 /// allocate is an error.
@@ -63,26 +171,36 @@ pub fn stream(req: Request, sink: Sink) std.mem.Allocator.Error!types.AssistantM
63171 return msg;
64172 }
65173
66-pub fn newAssistant(req: Request) types.AssistantMessage {
174+fn newAssistant(req: Request) types.AssistantMessage {
67175 return .{
68176 .content = .empty,
69177 .api = req.model.api,
70178 .provider = req.model.provider,
71179 .model = req.model.id,
72- .timestamp = @import("util.zig").nowMs(),
180+ .timestamp = util.nowMs(),
73181 };
74182 }
75183
76-/// The header list every provider shares: the content negotiation pair, the
77-/// provider's own extra pairs (skipped when their value is empty), and then
78-/// the caller's custom and session headers.
79-pub fn headers(a: std.mem.Allocator, req: Request, extra: []const [2][]const u8) ![]http.Header {
184+/// The header list every request shares: content negotiation, the wire's own
185+/// key and version headers, then the caller's custom and session headers. A
186+/// header with an empty value is left out entirely.
187+fn wireHeaders(a: std.mem.Allocator, req: Request, wire: Wire) ![]http.Header {
80188 var list: std.ArrayList(http.Header) = .empty;
81189 errdefer list.deinit(a);
82190 try list.append(a, .{ .name = "Content-Type", .value = "application/json" });
83191 try list.append(a, .{ .name = "Accept", .value = "text/event-stream" });
84- for (extra) |kv| {
85- if (kv[1].len > 0) try list.append(a, .{ .name = kv[0], .value = kv[1] });
192+ if (wire.version[0].len > 0) {
193+ try list.append(a, .{ .name = wire.version[0], .value = wire.version[1] });
194+ }
195+ if (wire.key_header.len > 0) {
196+ if (req.model.api_key) |key| {
197+ if (key.len > 0) {
198+ try list.append(a, .{
199+ .name = wire.key_header,
200+ .value = try std.fmt.allocPrint(a, "{s}{s}", .{ wire.key_prefix, key }),
201+ });
202+ }
203+ }
86204 }
87205 for (req.model.headers) |kv| try list.append(a, .{ .name = kv[0], .value = kv[1] });
88206 if (req.session_id) |sid| {
@@ -96,7 +214,7 @@ pub fn headers(a: std.mem.Allocator, req: Request, extra: []const [2][]const u8)
96214 /// Sends the request and maps a transport failure onto `msg`. Returns false
97215 /// once the caller must return `msg` as-is; true when the stream completed and
98216 /// the caller should proceed to finalize.
99-pub fn post(req: Request, url: []const u8, hdrs: []const http.Header, body: []const u8, handler: http.SseHandler, msg: *types.AssistantMessage) std.mem.Allocator.Error!bool {
217+fn post(req: Request, url: []const u8, hdrs: []const http.Header, body: []const u8, handler: http.SseHandler, msg: *types.AssistantMessage) std.mem.Allocator.Error!bool {
100218 var err_body: ?[]const u8 = null;
101219 http.postSse(req.scratch, url, hdrs, body, handler, req.cancel, &err_body) catch |e| {
102220 if (e == error.OutOfMemory) return error.OutOfMemory;
@@ -120,26 +238,84 @@ pub fn post(req: Request, url: []const u8, hdrs: []const http.Header, body: []co
120238 return true;
121239 }
122240
123-/// The `{d}` cost fields, shared by every provider's usage accounting.
124-pub fn applyCost(model: *const types.Model, u: *types.Usage) void {
125- const m = 1_000_000.0;
126- u.cost_input = @as(f64, @floatFromInt(u.input)) * model.cost_input / m;
127- u.cost_output = @as(f64, @floatFromInt(u.output)) * model.cost_output / m;
128- u.cost_cache_read = @as(f64, @floatFromInt(u.cache_read)) * model.cost_cache_read / m;
129- u.cost_cache_write = 0;
130- u.cost_total = u.cost_input + u.cost_output + u.cost_cache_read + u.cost_cache_write;
241+/// One tool call as the provider streams it. The wires differ only in where
242+/// the pieces arrive; the shape and the accumulation are the same.
243+pub const Call = struct {
244+ id: []const u8 = "",
245+ name: []const u8 = "",
246+ args: std.ArrayList(u8) = .empty,
247+ started: bool = false,
248+ /// Google's continuation data, replayed on the next request.
249+ thought_signature: ?[]const u8 = null,
250+
251+ /// Records the name and announces the call the first time one arrives.
252+ pub fn announce(self: *Call, a: std.mem.Allocator, sink: Sink, name: []const u8) !void {
253+ if (self.name.len == 0) self.name = try a.dupe(u8, name);
254+ if (!self.started and self.name.len > 0) {
255+ self.started = true;
256+ sink.emit(.{ .tool_start = self.name });
257+ }
258+ }
259+};
260+
261+/// Appends every call the stream finished with, and says whether there was
262+/// one. A stream that failed left arguments truncated, so its calls are
263+/// dropped: a partial call is not a call the model finished making.
264+pub fn appendCalls(msg: *types.AssistantMessage, sink: Sink, a: std.mem.Allocator, calls: []const Call, failed: bool) !bool {
265+ if (failed) return false;
266+ var any = false;
267+ for (calls) |call| {
268+ if (call.name.len == 0) continue;
269+ any = true;
270+ try msg.content.append(a, .{ .tool_call = .{
271+ .id = call.id,
272+ .name = call.name,
273+ .arguments = argumentsOrObject(call.args.items),
274+ .thought_signature = call.thought_signature,
275+ } });
276+ sink.emit(.{ .tool_call = .{
277+ .id = call.id,
278+ .name = call.name,
279+ .arguments = argumentsOrObject(call.args.items),
280+ } });
281+ }
282+ return any;
131283 }
132284
133-pub fn num(v: std.json.Value) u64 {
134- return switch (v) {
285+/// A call's arguments, or an empty object when the provider streamed none.
286+pub fn argumentsOrObject(args: []const u8) []const u8 {
287+ return if (args.len > 0) args else "{}";
288+}
289+
290+/// Records the tokens a response cost and prices them. The total is the
291+/// provider's own when it reported one, and the sum of the parts otherwise.
292+pub fn finishUsage(msg: *types.AssistantMessage, model: *const types.Model, usage: types.Usage) void {
293+ var u = usage;
294+ if (u.total_tokens == 0) u.total_tokens = u.input + u.output + u.cache_read + u.cache_write;
295+ const per = struct {
296+ fn cost(tokens: u64, rate: f64) f64 {
297+ return @as(f64, @floatFromInt(tokens)) * rate / 1_000_000.0;
298+ }
299+ };
300+ u.cost_input = per.cost(u.input, model.cost_input);
301+ u.cost_output = per.cost(u.output, model.cost_output);
302+ u.cost_cache_read = per.cost(u.cache_read, model.cost_cache_read);
303+ u.cost_total = u.cost_input + u.cost_output + u.cost_cache_read;
304+ msg.usage = u;
305+}
306+
307+/// An integer field, or null when it is missing or not a number.
308+pub fn optNumField(obj: std.json.ObjectMap, key: []const u8) ?u64 {
309+ return switch (obj.get(key) orelse return null) {
135310 .integer => |n| @intCast(@max(n, 0)),
136311 .float => |f| @intFromFloat(@max(f, 0)),
137- else => 0,
312+ else => null,
138313 };
139314 }
140315
316+/// An integer field, or 0 when it is missing or not a number.
141317 pub fn numField(obj: std.json.ObjectMap, key: []const u8) u64 {
142- return num(obj.get(key) orelse return 0);
318+ return optNumField(obj, key) orelse 0;
143319 }
144320
145321 /// A non-empty string field, or null.
@@ -151,11 +327,45 @@ pub fn stringField(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
151327 };
152328 }
153329
154-/// A string field, or `""` when the field is missing or not a string.
155-pub fn stringOr(obj: std.json.ObjectMap, key: []const u8) []const u8 {
156- return switch (obj.get(key) orelse .null) {
330+/// A nested object field, or null when it is missing or not an object.
331+pub fn objField(obj: std.json.ObjectMap, key: []const u8) ?std.json.ObjectMap {
332+ return switch (obj.get(key) orelse return null) {
333+ .object => |o| o,
334+ else => null,
335+ };
336+}
337+
338+/// The first object element of an array field, or null when the field is
339+/// missing, not an array, empty, or its first element is not an object.
340+pub fn firstObjField(obj: std.json.ObjectMap, key: []const u8) ?std.json.ObjectMap {
341+ const arr = switch (obj.get(key) orelse return null) {
342+ .array => |x| x,
343+ else => return null,
344+ };
345+ if (arr.items.len == 0) return null;
346+ return switch (arr.items[0]) {
347+ .object => |o| o,
348+ else => null,
349+ };
350+}
351+
352+/// A provider error message out of `key`, or null. The field may be a message
353+/// string itself or an object carrying one under "message"; an empty value is
354+/// no error at all.
355+pub fn errorMessage(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
356+ const err = switch (obj.get(key) orelse return null) {
357+ .object => |o| return stringField(o, "message"),
157358 .string => |s| s,
158- else => "",
359+ else => return null,
360+ };
361+ return if (err.len > 0) err else null;
362+}
363+
364+/// A boolean field, or `default` when the field is missing or not a boolean.
365+pub fn boolField(obj: std.json.ObjectMap, key: []const u8, default: bool) bool {
366+ return switch (obj.get(key) orelse return default) {
367+ .bool => |b| b,
368+ else => default,
159369 };
160370 }
161371
@@ -168,7 +378,7 @@ pub fn intOr(obj: std.json.ObjectMap, key: []const u8, default: i64) i64 {
168378 }
169379
170380 /// `base_url` plus one path segment, with a trailing slash on the base ignored.
171-pub fn buildUrl(a: std.mem.Allocator, base: []const u8, path: []const u8) ![]u8 {
381+fn buildUrl(a: std.mem.Allocator, base: []const u8, path: []const u8) ![]u8 {
172382 return std.fmt.allocPrint(a, "{s}{s}", .{ std.mem.trimEnd(u8, base, "/"), path });
173383 }
174384
@@ -180,15 +390,55 @@ pub fn parseTools(a: std.mem.Allocator, tools_json: []const u8) ?[]const std.jso
180390 return if (root == .array) root.array.items else null;
181391 }
182392
393+/// How one provider frames its `tools` array: `open` and `close` bound the
394+/// whole thing, and each element is `entry` wrapped around the call's schema
395+/// under `params_key`. A provider with no tools writes nothing at all, which is
396+/// also how an unparsable `tools_json` is treated.
397+pub const ToolsForm = struct {
398+ open: []const u8,
399+ entry: []const u8,
400+ params_key: []const u8,
401+ entry_close: []const u8 = "",
402+ close: []const u8,
403+};
404+
405+/// Writes the `tools` array in the wire form `form` describes, from the flat
406+/// `tools_json` array the session recorded. `open` carries the key and its
407+/// leading comma, so nothing at all is written when there is no tool, which is
408+/// also how an unparsable `tools_json` is treated.
409+pub fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8, form: ToolsForm) !void {
410+ const tools = parseTools(a, tools_json) orelse return;
411+ var first = true;
412+ for (tools) |tool| {
413+ if (tool != .object) continue;
414+ if (first) {
415+ try w.writeAll(form.open);
416+ first = false;
417+ } else {
418+ try w.writeByte(',');
419+ }
420+ try w.writeAll(form.entry);
421+ try writeToolBody(w, tool.object, form.params_key);
422+ try w.writeAll(form.entry_close);
423+ }
424+ if (first) return;
425+ try w.writeAll(form.close);
426+}
427+
183428 /// `"name":"…","description":"…","<params_key>":<schema>` — the body every
184-/// provider's tool encoding shares. The caller supplies the wrapper.
185-pub fn writeToolBody(w: *std.Io.Writer, obj: std.json.ObjectMap, params_key: []const u8) !void {
429+/// provider's tool encoding shares. A schema that names no parameters is sent
430+/// as `{}` rather than panicking on a session log written by another version.
431+fn writeToolBody(w: *std.Io.Writer, obj: std.json.ObjectMap, params_key: []const u8) !void {
186432 try w.writeAll("\"name\":");
187- try @import("json.zig").writeString(w, obj.get("name").?.string);
433+ try json.writeString(w, stringField(obj, "name") orelse "");
188434 try w.writeAll(",\"description\":");
189- try @import("json.zig").writeString(w, obj.get("description").?.string);
435+ try json.writeString(w, stringField(obj, "description") orelse "");
190436 try w.writeAll(",\"");
191437 try w.writeAll(params_key);
192438 try w.writeAll("\":");
193- try std.json.Stringify.value(obj.get("parameters").?, .{}, w);
439+ if (obj.get("parameters")) |params| {
440+ try std.json.Stringify.value(params, .{}, w);
441+ } else {
442+ try w.writeAll("{}");
443+ }
194444 }
src/api/anthropic_messages.zig+102−225
@@ -4,16 +4,10 @@
44 const std = @import("std");
55 const api = @import("../api.zig");
66 const types = @import("../types.zig");
7-const http = @import("../http.zig");
87 const json = @import("../json.zig");
98
10-const Value = std.json.Value;
119 const anthropic_version = "2023-06-01";
1210
13-/// An untrusted stream index is a hint, never a size. Anything past this is
14-/// treated like a missing index so a hostile chunk cannot force an allocation.
15-const max_stream_index = 10_000;
16-
1711 // ---- request building -----------------------------------------------------
1812
1913 fn writeImage(w: *std.Io.Writer, img: types.ImageContent) !void {
@@ -77,11 +71,6 @@ fn writeAssistant(w: *std.Io.Writer, am: *const types.AssistantMessage) !void {
7771 try json.writeString(w, t.text);
7872 try w.writeByte('}');
7973 },
80- .image => |img| {
81- if (!first) try w.writeByte(',');
82- first = false;
83- try writeImage(w, img);
84- },
8574 .tool_call => |c| {
8675 if (!first) try w.writeByte(',');
8776 first = false;
@@ -90,7 +79,7 @@ fn writeAssistant(w: *std.Io.Writer, am: *const types.AssistantMessage) !void {
9079 try w.writeAll(",\"name\":");
9180 try json.writeString(w, c.name);
9281 try w.writeAll(",\"input\":");
93- try w.writeAll(if (c.arguments.len > 0) c.arguments else "{}");
82+ try w.writeAll(api.argumentsOrObject(c.arguments));
9483 try w.writeByte('}');
9584 },
9685 };
@@ -103,18 +92,6 @@ fn writeUser(w: *std.Io.Writer, u: types.Message) !void {
10392 try w.writeAll("}]}");
10493 }
10594
106-fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8, params_key: []const u8) !void {
107- const tools = api.parseTools(a, tools_json) orelse return;
108- try w.writeAll(",\"tools\":[");
109- for (tools, 0..) |tool, i| {
110- if (i > 0) try w.writeByte(',');
111- try w.writeByte('{');
112- try api.writeToolBody(w, tool.object, params_key);
113- try w.writeByte('}');
114- }
115- try w.writeByte(']');
116-}
117-
11895 /// Anthropic thinking budget per effort. Anthropic requires max_tokens to
11996 /// exceed the budget, so the caller raises max_tokens when it does not.
12097 fn budgetFor(effort: []const u8) u64 {
@@ -143,7 +120,7 @@ fn buildBody(req: api.Request) ![]u8 {
143120 try w.writeAll("{\"model\":");
144121 try json.writeString(w, req.model.id);
145122 try w.writeAll(",\"max_tokens\":");
146- try json.writeUint(w, max_tokens);
123+ try json.writeNum(w, max_tokens);
147124 try w.writeAll(",\"stream\":true");
148125 if (req.system_prompt.len > 0) {
149126 try w.writeAll(",\"system\":");
@@ -151,7 +128,7 @@ fn buildBody(req: api.Request) ![]u8 {
151128 }
152129 if (budget > 0) {
153130 try w.writeAll(",\"thinking\":{\"type\":\"enabled\",\"budget_tokens\":");
154- try json.writeUint(w, budget);
131+ try json.writeNum(w, budget);
155132 try w.writeByte('}');
156133 }
157134
@@ -184,22 +161,17 @@ fn buildBody(req: api.Request) ![]u8 {
184161 }
185162 try w.writeByte(']');
186163
187- try writeTools(w, req.scratch, req.tools_json, "input_schema");
164+ try api.writeTools(w, req.scratch, req.tools_json, .{
165+ .open = ",\"tools\":[",
166+ .entry = "{",
167+ .params_key = "input_schema",
168+ .entry_close = "}",
169+ .close = "]",
170+ });
188171 try w.writeByte('}');
189172 return out.written();
190173 }
191174
192-fn buildUrl(a: std.mem.Allocator, base: []const u8) ![]u8 {
193- return api.buildUrl(a, base, "/messages");
194-}
195-
196-fn headers(a: std.mem.Allocator, req: api.Request, key: []const u8) ![]http.Header {
197- return api.headers(a, req, &.{
198- .{ "anthropic-version", anthropic_version },
199- .{ "x-api-key", key },
200- });
201-}
202-
203175 // ---- streaming state ------------------------------------------------------
204176
205177 const BlockKind = enum { text, thinking, tool };
@@ -208,10 +180,8 @@ const Block = struct {
208180 kind: BlockKind = .text,
209181 text: std.ArrayList(u8) = .empty,
210182 signature: std.ArrayList(u8) = .empty,
211- id: []const u8 = "",
212- name: []const u8 = "",
213- args: std.ArrayList(u8) = .empty,
214- started: bool = false,
183+ /// The call this block's arguments belong to, when `kind` is `.tool`.
184+ call: usize = 0,
215185 };
216186
217187 const State = struct {
@@ -220,166 +190,109 @@ const State = struct {
220190 arena: *std.heap.ArenaAllocator,
221191 msg: *types.AssistantMessage,
222192 blocks: std.ArrayList(Block) = .empty,
223- tokens_in: u64 = 0,
224- tokens_out: u64 = 0,
225- cache_read: u64 = 0,
226- cache_write: u64 = 0,
227- reasoning_tokens: ?u64 = null,
193+ calls: std.ArrayList(api.Call) = .empty,
194+ usage: types.Usage = .{},
228195 stop_reason: ?[]const u8 = null,
229196 stream_error: ?[]const u8 = null,
230-};
197+ /// Set by `run` when the stream did not run to completion.
198+ failed: bool = false,
231199
232-fn blockAt(st: *State, index: i64) !*Block {
233- const idx: usize = if (index >= 0 and index <= max_stream_index)
234- @intCast(index)
235- else
236- st.blocks.items.len;
237- while (st.blocks.items.len <= idx) try st.blocks.append(st.req.pers, .{});
238- return &st.blocks.items[idx];
239-}
240-
241-fn handle(st: *State, obj: std.json.ObjectMap) !void {
242- const event_type = api.stringOr(obj, "type");
200+ pub fn handle(st: *State, obj: std.json.ObjectMap) !void {
201+ const event_type = api.stringField(obj, "type") orelse "";
243202
244- if (std.mem.eql(u8, event_type, "error")) {
245- if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, errorMessage(obj));
246- return;
247- }
203+ if (std.mem.eql(u8, event_type, "error")) {
204+ if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, api.errorMessage(obj, "error") orelse "stream: provider error");
205+ return;
206+ }
248207
249- if (std.mem.eql(u8, event_type, "message_start")) {
250- const message = obj.get("message") orelse return;
251- if (message != .object) return;
252- const message_obj = message.object;
253- if (st.msg.response_id == null) {
254- if (message_obj.get("id")) |v| {
255- if (v == .string) st.msg.response_id = try st.req.pers.dupe(u8, v.string);
208+ if (std.mem.eql(u8, event_type, "message_start")) {
209+ const message = api.objField(obj, "message") orelse return;
210+ if (st.msg.response_id == null) {
211+ if (api.stringField(message, "id")) |id| st.msg.response_id = try st.req.pers.dupe(u8, id);
256212 }
257- }
258- if (message_obj.get("usage")) |usage| {
259- if (usage == .object) {
260- st.tokens_in = api.numField(usage.object, "input_tokens");
261- st.tokens_out = api.numField(usage.object, "output_tokens");
262- st.cache_read = api.numField(usage.object, "cache_read_input_tokens");
263- st.cache_write = api.numField(usage.object, "cache_creation_input_tokens");
213+ if (api.objField(message, "usage")) |u| {
214+ st.usage.input = api.numField(u, "input_tokens");
215+ st.usage.output = api.numField(u, "output_tokens");
216+ st.usage.cache_read = api.numField(u, "cache_read_input_tokens");
217+ st.usage.cache_write = api.numField(u, "cache_creation_input_tokens");
264218 }
219+ return;
265220 }
266- return;
267- }
268221
269- if (std.mem.eql(u8, event_type, "content_block_start")) {
270- const raw = api.intOr(obj, "index", -1);
271- if (raw < 0 or raw > max_stream_index) return;
272- const block = try blockAt(st, raw);
273- const cb = obj.get("content_block") orelse return;
274- if (cb != .object) return;
275- const bt = api.stringOr(cb.object, "type");
276- if (std.mem.eql(u8, bt, "tool_use")) {
277- block.kind = .tool;
278- if (cb.object.get("id")) |id| {
279- if (id == .string) block.id = try st.req.pers.dupe(u8, id.string);
280- }
281- if (cb.object.get("name")) |name| {
282- if (name == .string) block.name = try st.req.pers.dupe(u8, name.string);
283- }
284- if (!block.started and block.name.len > 0) {
285- block.started = true;
286- st.sink.emit(.{ .tool_start = block.name });
287- }
288- } else if (std.mem.eql(u8, bt, "thinking") or std.mem.eql(u8, bt, "redacted_thinking")) {
289- block.kind = .thinking;
290- if (std.mem.eql(u8, bt, "redacted_thinking")) {
291- if (cb.object.get("data")) |d| {
292- if (d == .string) try block.signature.appendSlice(st.req.pers, d.string);
222+ if (std.mem.eql(u8, event_type, "content_block_start")) {
223+ const block = try api.blockAt(Block, &st.blocks, st.req.pers, api.intOr(obj, "index", -1));
224+ const cb = api.objField(obj, "content_block") orelse return;
225+ const bt = api.stringField(cb, "type") orelse "";
226+ if (std.mem.eql(u8, bt, "tool_use")) {
227+ block.kind = .tool;
228+ block.call = st.calls.items.len;
229+ const call = try st.calls.addOne(st.req.pers);
230+ call.* = .{};
231+ if (api.stringField(cb, "id")) |id| call.id = try st.req.pers.dupe(u8, id);
232+ try call.announce(st.req.pers, st.sink, api.stringField(cb, "name") orelse "");
233+ } else if (std.mem.eql(u8, bt, "thinking") or std.mem.eql(u8, bt, "redacted_thinking")) {
234+ block.kind = .thinking;
235+ if (std.mem.eql(u8, bt, "redacted_thinking")) {
236+ if (api.stringField(cb, "data")) |d| try block.signature.appendSlice(st.req.pers, d);
293237 }
238+ } else {
239+ block.kind = .text;
294240 }
295- } else {
296- block.kind = .text;
241+ return;
297242 }
298- return;
299- }
300243
301- if (std.mem.eql(u8, event_type, "content_block_delta")) {
302- const idx = api.intOr(obj, "index", -1);
303- if (idx < 0 or idx >= st.blocks.items.len) return;
304- const delta = obj.get("delta") orelse return;
305- if (delta != .object) return;
306- const block = &st.blocks.items[@intCast(idx)];
307- const dt = api.stringOr(delta.object, "type");
308- if (std.mem.eql(u8, dt, "text_delta")) {
309- if (api.stringField(delta.object, "text")) |s| {
310- try block.text.appendSlice(st.req.pers, s);
311- st.sink.emit(.{ .text = s });
312- }
313- } else if (std.mem.eql(u8, dt, "thinking_delta")) {
314- if (api.stringField(delta.object, "thinking")) |s| {
315- try block.text.appendSlice(st.req.pers, s);
316- st.sink.emit(.{ .reasoning = s });
317- }
318- } else if (std.mem.eql(u8, dt, "signature_delta")) {
319- if (api.stringField(delta.object, "signature")) |s| {
320- try block.signature.appendSlice(st.req.pers, s);
321- }
322- } else if (std.mem.eql(u8, dt, "input_json_delta")) {
323- if (api.stringField(delta.object, "partial_json")) |s| {
324- try block.args.appendSlice(st.req.pers, s);
244+ if (std.mem.eql(u8, event_type, "content_block_delta")) {
245+ const idx = api.intOr(obj, "index", -1);
246+ if (idx < 0 or idx >= st.blocks.items.len) return;
247+ const delta = api.objField(obj, "delta") orelse return;
248+ const block = &st.blocks.items[@intCast(idx)];
249+ const call = if (block.kind == .tool) &st.calls.items[block.call] else null;
250+ const dt = api.stringField(delta, "type") orelse "";
251+ if (std.mem.eql(u8, dt, "text_delta")) {
252+ if (api.stringField(delta, "text")) |s| {
253+ try block.text.appendSlice(st.req.pers, s);
254+ st.sink.emit(.{ .text = s });
255+ }
256+ } else if (std.mem.eql(u8, dt, "thinking_delta")) {
257+ if (api.stringField(delta, "thinking")) |s| {
258+ try block.text.appendSlice(st.req.pers, s);
259+ st.sink.emit(.{ .reasoning = s });
260+ }
261+ } else if (std.mem.eql(u8, dt, "signature_delta")) {
262+ if (api.stringField(delta, "signature")) |s| {
263+ try block.signature.appendSlice(st.req.pers, s);
264+ }
265+ } else if (std.mem.eql(u8, dt, "input_json_delta")) {
266+ if (call) |c| {
267+ if (api.stringField(delta, "partial_json")) |s| try c.args.appendSlice(st.req.pers, s);
268+ }
325269 }
270+ return;
326271 }
327- return;
328- }
329272
330- if (std.mem.eql(u8, event_type, "message_delta")) {
331- if (obj.get("delta")) |delta| {
332- if (delta == .object) {
333- if (api.stringField(delta.object, "stop_reason")) |s| {
273+ if (std.mem.eql(u8, event_type, "message_delta")) {
274+ if (api.objField(obj, "delta")) |delta| {
275+ if (api.stringField(delta, "stop_reason")) |s| {
334276 st.stop_reason = try st.req.pers.dupe(u8, s);
335277 }
336278 }
337- }
338- if (obj.get("usage")) |usage| {
339- if (usage == .object) {
340- if (usage.object.get("input_tokens") != null) st.tokens_in = api.numField(usage.object, "input_tokens");
341- if (usage.object.get("output_tokens") != null) st.tokens_out = api.numField(usage.object, "output_tokens");
342- if (usage.object.get("cache_read_input_tokens") != null) st.cache_read = api.numField(usage.object, "cache_read_input_tokens");
343- if (usage.object.get("cache_creation_input_tokens") != null) st.cache_write = api.numField(usage.object, "cache_creation_input_tokens");
344- if (usage.object.get("output_tokens_details")) |details| {
345- if (details == .object and details.object.get("thinking_tokens") != null) {
346- st.reasoning_tokens = api.numField(details.object, "thinking_tokens");
347- }
279+ if (api.objField(obj, "usage")) |u| {
280+ if (api.optNumField(u, "input_tokens")) |v| st.usage.input = v;
281+ if (api.optNumField(u, "output_tokens")) |v| st.usage.output = v;
282+ if (api.optNumField(u, "cache_read_input_tokens")) |v| st.usage.cache_read = v;
283+ if (api.optNumField(u, "cache_creation_input_tokens")) |v| st.usage.cache_write = v;
284+ if (api.objField(u, "output_tokens_details")) |d| {
285+ st.usage.reasoning = api.optNumField(d, "thinking_tokens");
348286 }
349287 }
288+ return;
350289 }
351- return;
352290 }
353-}
354-
355-fn onData(ctx: *anyopaque, data: []const u8) void {
356- const st: *State = @ptrCast(@alignCast(ctx));
357- const a = st.arena.allocator();
358- defer _ = st.arena.reset(.retain_capacity);
359-
360- const root = std.json.parseFromSliceLeaky(Value, a, data, .{}) catch {
361- if (st.stream_error == null) st.stream_error = "stream: invalid JSON chunk";
362- return;
363- };
364- if (root != .object) return;
365- handle(st, root.object) catch {
366- if (st.stream_error == null) st.stream_error = "stream: out of memory";
367- };
368-}
369-
370-fn errorMessage(obj: std.json.ObjectMap) []const u8 {
371- const err = obj.get("error") orelse return "stream: provider error";
372- return switch (err) {
373- .object => |o| api.stringField(o, "message") orelse "stream: provider error",
374- else => "stream: provider error",
375- };
376-}
291+};
377292
378-fn mapStopReason(st: *State) void {
379- var has_calls = false;
380- for (st.blocks.items) |block| {
381- if (block.kind == .tool and block.name.len > 0) has_calls = true;
382- }
293+/// A stream that produced finished calls is a tool turn, not a stop. A broken
294+/// stream reports none, and `run` restores the transport's own reason.
295+fn mapStopReason(st: *State, has_calls: bool) void {
383296 if (has_calls) {
384297 st.msg.stop_reason = .tool_use;
385298 return;
@@ -418,56 +331,20 @@ fn finalize(st: *State) !void {
418331 if (block.text.items.len == 0) continue;
419332 try st.msg.content.append(a, .{ .text = block.text.items });
420333 }
421- for (st.blocks.items) |block| {
422- if (block.kind != .tool or block.name.len == 0) continue;
423- const arguments = if (block.args.items.len > 0) block.args.items else "{}";
424- try st.msg.content.append(a, .{ .tool_call = .{
425- .id = block.id,
426- .name = block.name,
427- .arguments = arguments,
428- } });
429- st.sink.emit(.{ .tool_call = .{
430- .id = block.id,
431- .name = block.name,
432- .arguments = arguments,
433- } });
434- }
435-
436- var usage = types.Usage{};
437- usage.input = st.tokens_in;
438- usage.output = st.tokens_out;
439- usage.cache_read = st.cache_read;
440- usage.cache_write = st.cache_write;
441- usage.reasoning = st.reasoning_tokens;
442- usage.total_tokens = st.tokens_in + st.tokens_out + st.cache_read + st.cache_write;
443- api.applyCost(st.req.model, &usage);
444- st.msg.usage = usage;
334+ const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);
335+ api.finishUsage(st.msg, st.req.model, st.usage);
445336
446337 st.msg.raw_stop_reason = st.stop_reason;
447- mapStopReason(st);
338+ mapStopReason(st, has_calls);
448339 }
449340
450-pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.AssistantMessage {
451- var msg = api.newAssistant(req);
452-
453- var arena = std.heap.ArenaAllocator.init(req.scratch);
454- defer arena.deinit();
455-
456- const body = buildBody(req) catch return error.OutOfMemory;
457- const url = buildUrl(req.scratch, req.model.base_url) catch return error.OutOfMemory;
458- const key = req.model.api_key orelse "";
459- const hdrs = headers(req.scratch, req, key) catch return error.OutOfMemory;
460-
461- var st = State{ .req = req, .sink = sink, .arena = &arena, .msg = &msg };
462-
463- if (!try api.post(req, url, hdrs, body, .{ .ctx = &st, .onEvent = onData }, &msg)) return msg;
464-
465- if (st.stream_error) |message| {
466- msg.stop_reason = .err;
467- msg.error_message = try req.pers.dupe(u8, message);
468- return msg;
469- }
341+const wire = api.Wire{
342+ .key_header = "x-api-key",
343+ .version = .{ "anthropic-version", anthropic_version },
344+ .path = "/messages",
345+ .build_body = buildBody,
346+};
470347
471- try finalize(&st);
472- return msg;
348+pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.AssistantMessage {
349+ return api.run(State, wire, req, sink, finalize);
473350 }
src/api/google_generative_ai.zig+93−174
@@ -4,11 +4,8 @@
44 const std = @import("std");
55 const api = @import("../api.zig");
66 const types = @import("../types.zig");
7-const http = @import("../http.zig");
87 const json = @import("../json.zig");
98
10-const Value = std.json.Value;
11-
129 // ---- request building -----------------------------------------------------
1310
1411 fn writeText(w: *std.Io.Writer, text: []const u8) !void {
@@ -29,7 +26,7 @@ fn writeFunctionCall(w: *std.Io.Writer, c: types.ToolCall) !void {
2926 try w.writeAll("{\"functionCall\":{\"name\":");
3027 try json.writeString(w, c.name);
3128 try w.writeAll(",\"args\":");
32- try w.writeAll(if (c.arguments.len > 0) c.arguments else "{}");
29+ try w.writeAll(api.argumentsOrObject(c.arguments));
3330 try w.writeByte('}');
3431 if (c.thought_signature) |sig| {
3532 try w.writeAll(",\"thoughtSignature\":");
@@ -60,7 +57,7 @@ fn writeContent(w: *std.Io.Writer, msg: types.Message) !void {
6057 first = false;
6158 try writeFunctionCall(w, c);
6259 },
63- .thinking, .image => {},
60+ .thinking => {},
6461 };
6562 try w.writeAll("]}");
6663 },
@@ -86,18 +83,6 @@ fn writeToolResults(w: *std.Io.Writer, run: []const types.Message) !void {
8683 try w.writeAll("]}");
8784 }
8885
89-fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8) !void {
90- const tools = api.parseTools(a, tools_json) orelse return;
91- try w.writeAll(",\"tools\":[{\"functionDeclarations\":[");
92- for (tools, 0..) |tool, i| {
93- if (i > 0) try w.writeByte(',');
94- try w.writeByte('{');
95- try api.writeToolBody(w, tool.object, "parametersJsonSchema");
96- try w.writeByte('}');
97- }
98- try w.writeAll("]}]");
99-}
100-
10186 /// Thinking budget per effort. `off` disables thinking entirely.
10287 fn budgetFor(effort: []const u8) ?u64 {
10388 const map = .{
@@ -144,51 +129,52 @@ fn buildBody(req: api.Request) ![]u8 {
144129 try w.writeAll("]}");
145130 }
146131
147- try writeTools(w, req.scratch, req.tools_json);
148-
149- var generation = false;
150- if (req.model.max_tokens > 0) {
151- try w.writeAll(",\"generationConfig\":{\"maxOutputTokens\":");
152- try json.writeUint(w, req.model.max_tokens);
153- generation = true;
154- }
155- if (budgetFor(req.effort)) |budget| {
156- if (!generation) {
157- try w.writeAll(",\"generationConfig\":{");
158- } else {
159- try w.writeByte(',');
132+ try api.writeTools(w, req.scratch, req.tools_json, .{
133+ .open = ",\"tools\":[{\"functionDeclarations\":[",
134+ .entry = "{",
135+ .params_key = "parametersJsonSchema",
136+ .entry_close = "}",
137+ .close = "]}]",
138+ });
139+
140+ // `generationConfig` holds max tokens and, when the model thinks, the
141+ // thinking budget. It is written only when it has at least one field.
142+ if (req.model.max_tokens > 0 or budgetFor(req.effort) != null) {
143+ try w.writeAll(",\"generationConfig\":{");
144+ if (req.model.max_tokens > 0) {
145+ try w.writeAll("\"maxOutputTokens\":");
146+ try json.writeNum(w, req.model.max_tokens);
147+ }
148+ if (budgetFor(req.effort)) |budget| {
149+ if (req.model.max_tokens > 0) try w.writeByte(',');
150+ try w.writeAll("\"thinkingConfig\":{");
151+ if (budget > 0) try w.writeAll("\"includeThoughts\":true,");
Cut short: this commit's diff is over 1000 lines.
src/api/openai_completions.zig+76−197
Cut short: this commit's diff is over 1000 lines.
src/api/openai_responses.zig+125−230
Cut short: this commit's diff is over 1000 lines.
src/config.zig+1−9
Cut short: this commit's diff is over 1000 lines.
src/http.zig+2−2
Cut short: this commit's diff is over 1000 lines.
src/json.zig+1−5
Cut short: this commit's diff is over 1000 lines.
src/main.zig+25−28
Cut short: this commit's diff is over 1000 lines.
src/models.zig+57−48
Cut short: this commit's diff is over 1000 lines.
src/prompt.zig+7−17
Cut short: this commit's diff is over 1000 lines.
src/session.zig+17−20
Cut short: this commit's diff is over 1000 lines.
src/tools/bash.zig+22−7
Cut short: this commit's diff is over 1000 lines.
src/tools/index.zig+31−43
Cut short: this commit's diff is over 1000 lines.
src/tools/read.zig+21−18
Cut short: this commit's diff is over 1000 lines.
src/tui/editor.zig+16−10
Cut short: this commit's diff is over 1000 lines.
src/tui/render.zig+36−39
Cut short: this commit's diff is over 1000 lines.
src/tui/theme.zig+1−1
Cut short: this commit's diff is over 1000 lines.
src/tui/tui.zig+170−212
Cut short: this commit's diff is over 1000 lines.
src/types.zig+0−4
Cut short: this commit's diff is over 1000 lines.
src/util.zig+32−27
Cut short: this commit's diff is over 1000 lines.