xonecas.io
79113
Ma

mini-coding-agent / commit 59bdc7a

fix: better JSON handling across the whole code.

xonecas2026-10-05 59bdc7a4353263b374afb9ad0a871c3653334a49 parent 97d6f77

11 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.

notes.md+4−0
@@ -65,3 +65,7 @@ src/config.zig
6565 `src/tui/tui.zig` - This file has a lot of responsibilities, making it very big. Token estimation and commands at least, could be extracted.
6666
6767 `src/tui/theme.zig` - will eventually grow too much, each theme we add will make that file worse. A refactor for later, since it's ok right now.
68+
69+# Actions post review
70+
71+- Address the JSON issues, and it's helpers. Zig does have a good JSON support, we just ignored it and hand rolled ours. We loose some data to show on error messages.
src/api.zig+89−98
@@ -4,7 +4,6 @@
44 const std = @import("std");
55 const types = @import("types.zig");
66 const http = @import("http.zig");
7-const json = @import("json.zig");
87 const util = @import("util.zig");
98
109 pub const Event = union(enum) {
@@ -28,6 +27,53 @@ pub const Sink = struct {
2827 }
2928 };
3029
30+/// JSON written byte-for-byte as it stands, never re-encoded. The struct field
31+/// that holds it supplies the key.
32+pub const Raw = struct {
33+ bytes: []const u8 = "",
34+
35+ pub fn jsonStringify(self: Raw, jws: anytype) !void {
36+ try jws.beginWriteRaw();
37+ try jws.writer.writeAll(self.bytes);
38+ jws.endWriteRaw();
39+ }
40+};
41+
42+/// A provider error value: a message string, or an object carrying one under
43+/// "message". An empty string is no error at all.
44+pub const ErrField = struct {
45+ message: ?[]const u8 = null,
46+
47+ const Body = struct { message: ?[]const u8 = null };
48+
49+ pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !@This() {
50+ if (try source.peekNextTokenType() != .object_begin) {
51+ const token = try source.nextAlloc(allocator, options.allocate.?);
52+ return .{ .message = switch (token) {
53+ .string, .allocated_string => |v| v,
54+ else => null,
55+ } };
56+ }
57+ const body = try std.json.innerParse(Body, allocator, source, options);
58+ return .{ .message = if (body.message) |m| if (m.len > 0) m else null else null };
59+ }
60+};
61+
62+/// A token count a provider may send as an integer or a float.
63+pub const Count = struct {
64+ value: u64 = 0,
65+
66+ pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !@This() {
67+ _ = options;
68+ const token = try source.nextAlloc(allocator, .alloc_if_needed);
69+ return switch (token) {
70+ .number, .allocated_number => |n| .{ .value = @intFromFloat(@max(std.fmt.parseFloat(f64, n) catch 0, 0)) },
71+ .null => .{},
72+ else => error.UnexpectedToken,
73+ };
74+ }
75+};
76+
3177 pub const Request = struct {
3278 /// Long-lived allocator: the returned message's content outlives the step.
3379 pers: std.mem.Allocator,
@@ -74,6 +120,7 @@ pub const Wire = struct {
74120 /// finished is not emitted, so a partial call is never handed to a tool.
75121 pub fn run(
76122 comptime State: type,
123+ comptime Chunk: type,
77124 comptime wire: Wire,
78125 req: Request,
79126 sink: Sink,
@@ -97,7 +144,7 @@ pub fn run(
97144 // but truncated arguments. Such a call is not a call the model finished
98145 // making, so `finalize` drops it rather than emit arguments to run; the
99146 // 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);
147+ const complete = try post(req, url, hdrs, body, .{ .ctx = &st, .onEvent = onData(State, Chunk) }, &msg);
101148 st.failed = !complete;
102149 const reason = msg.stop_reason;
103150 const message = msg.error_message;
@@ -114,10 +161,10 @@ pub fn run(
114161 return msg;
115162 }
116163
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 {
164+/// The SSE hook every wire shares: parse one payload into the wire's `Chunk`
165+/// type and hand it to `State.handle`. The arena is reset before the next
166+/// payload, so nothing may keep a pointer into the chunk.
167+fn onData(comptime State: type, comptime Chunk: type) *const fn (ctx: *anyopaque, data: []const u8) void {
121168 return struct {
122169 fn hook(ctx: *anyopaque, data: []const u8) void {
123170 const st: *State = @ptrCast(@alignCast(ctx));
@@ -125,12 +172,11 @@ fn onData(comptime State: type) *const fn (ctx: *anyopaque, data: []const u8) vo
125172 // The sentinel OpenAI-compatible streams send in place of a final
126173 // event. It is not JSON, and it is not a failure.
127174 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";
175+ const chunk = std.json.parseFromSliceLeaky(Chunk, st.arena.allocator(), data, .{ .ignore_unknown_fields = true }) catch |e| {
176+ if (e != error.OutOfMemory and st.stream_error == null) st.stream_error = "stream: invalid JSON chunk";
130177 return;
131178 };
132- if (root != .object) return;
133- State.handle(st, root.object) catch {
179+ State.handle(st, chunk) catch {
134180 if (st.stream_error == null) st.stream_error = "stream: out of memory";
135181 };
136182 }
@@ -304,20 +350,6 @@ pub fn finishUsage(msg: *types.AssistantMessage, model: *const types.Model, usag
304350 msg.usage = u;
305351 }
306352
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) {
310- .integer => |n| @intCast(@max(n, 0)),
311- .float => |f| @intFromFloat(@max(f, 0)),
312- else => null,
313- };
314-}
315-
316-/// An integer field, or 0 when it is missing or not a number.
317-pub fn numField(obj: std.json.ObjectMap, key: []const u8) u64 {
318- return optNumField(obj, key) orelse 0;
319-}
320-
321353 /// A non-empty string field, or null.
322354 pub fn stringField(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
323355 const v = obj.get(key) orelse return null;
@@ -327,73 +359,11 @@ pub fn stringField(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
327359 };
328360 }
329361
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"),
358- .string => |s| s,
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,
369- };
370-}
371-
372-/// An integer field, or `default` when the field is missing or not an integer.
373-pub fn intOr(obj: std.json.ObjectMap, key: []const u8, default: i64) i64 {
374- return switch (obj.get(key) orelse .null) {
375- .integer => |n| n,
376- else => default,
377- };
378-}
379-
380-/// `base_url` plus one path segment, with a trailing slash on the base ignored.
381-fn buildUrl(a: std.mem.Allocator, base: []const u8, path: []const u8) ![]u8 {
382- return std.fmt.allocPrint(a, "{s}{s}", .{ std.mem.trimEnd(u8, base, "/"), path });
383-}
384-
385-/// The tool schemas parsed from the session's flat form, or null when there
386-/// are none. Borrowed from `a`.
387-pub fn parseTools(a: std.mem.Allocator, tools_json: []const u8) ?[]const std.json.Value {
388- if (tools_json.len == 0) return null;
389- const root = std.json.parseFromSliceLeaky(std.json.Value, a, tools_json, .{}) catch return null;
390- return if (root == .array) root.array.items else null;
391-}
392-
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.
362+/// How one provider frames its `tools` array value: `open` and `close` bound
363+/// it, and each element is `entry` wrapped around the call's schema under
364+/// `params_key`. The key `"tools"` is supplied by the struct field that holds
365+/// the value. A provider with no tools writes nothing at all, which is also how
366+/// an unparsable `tools_json` is treated.
397367 pub const ToolsForm = struct {
398368 open: []const u8,
399369 entry: []const u8,
@@ -402,14 +372,30 @@ pub const ToolsForm = struct {
402372 close: []const u8,
403373 };
404374
375+/// A request's `tools` array, rendered verbatim from the flat `tools_json` the
376+/// session recorded, in the wire form `form` describes. It is written as an
377+/// array value, so the caller's struct field supplies the key.
378+pub const Tools = struct {
379+ a: std.mem.Allocator,
380+ json: []const u8,
381+ form: ToolsForm,
382+
383+ pub fn jsonStringify(self: Tools, jws: anytype) !void {
384+ try jws.beginWriteRaw();
385+ try writeTools(jws.writer, self.a, self.json, self.form);
386+ jws.endWriteRaw();
387+ }
388+};
389+
405390 /// 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.
391+/// `tools_json` array the session recorded. Nothing at all is written when
392+/// there is no tool, which is also how an unparsable `tools_json` is treated.
409393 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;
394+ if (tools_json.len == 0) return;
395+ const root = std.json.parseFromSliceLeaky(std.json.Value, a, tools_json, .{}) catch return;
396+ if (root != .array) return;
411397 var first = true;
412- for (tools) |tool| {
398+ for (root.array.items) |tool| {
413399 if (tool != .object) continue;
414400 if (first) {
415401 try w.writeAll(form.open);
@@ -430,9 +416,9 @@ pub fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u
430416 /// as `{}` rather than panicking on a session log written by another version.
431417 fn writeToolBody(w: *std.Io.Writer, obj: std.json.ObjectMap, params_key: []const u8) !void {
432418 try w.writeAll("\"name\":");
433- try json.writeString(w, stringField(obj, "name") orelse "");
419+ try std.json.Stringify.encodeJsonString(stringField(obj, "name") orelse "", .{}, w);
434420 try w.writeAll(",\"description\":");
435- try json.writeString(w, stringField(obj, "description") orelse "");
421+ try std.json.Stringify.encodeJsonString(stringField(obj, "description") orelse "", .{}, w);
436422 try w.writeAll(",\"");
437423 try w.writeAll(params_key);
438424 try w.writeAll("\":");
@@ -442,3 +428,8 @@ fn writeToolBody(w: *std.Io.Writer, obj: std.json.ObjectMap, params_key: []const
442428 try w.writeAll("{}");
443429 }
444430 }
431+
432+/// `base_url` plus one path segment, with a trailing slash on the base ignored.
433+fn buildUrl(a: std.mem.Allocator, base: []const u8, path: []const u8) ![]u8 {
434+ return std.fmt.allocPrint(a, "{s}{s}", .{ std.mem.trimEnd(u8, base, "/"), path });
435+}
src/api/anthropic_messages.zig+251−158
@@ -4,93 +4,121 @@
44 const std = @import("std");
55 const api = @import("../api.zig");
66 const types = @import("../types.zig");
7-const json = @import("../json.zig");
87
98 const anthropic_version = "2023-06-01";
109
1110 // ---- request building -----------------------------------------------------
1211
13-fn writeImage(w: *std.Io.Writer, img: types.ImageContent) !void {
14- try w.writeAll("{\"type\":\"image\",\"source\":{\"type\":\"base64\",\"media_type\":");
15- try json.writeString(w, img.mime_type);
16- try w.writeAll(",\"data\":");
17- try json.writeString(w, img.data);
18- try w.writeAll("}}");
19-}
2012
21-/// A tool result is one block of a user message. Its content is a string when
22-/// there is only text, and an array when images must ride along.
23-fn writeToolResult(w: *std.Io.Writer, t: types.ToolResultMessage) !void {
24- try w.writeAll("{\"type\":\"tool_result\",\"tool_use_id\":");
25- try json.writeString(w, t.tool_call_id);
26- if (t.is_error) try w.writeAll(",\"is_error\":true");
27- if (t.images.len == 0) {
28- try w.writeAll(",\"content\":");
29- try json.writeString(w, t.text);
30- } else {
31- try w.writeAll(",\"content\":[{\"type\":\"text\",\"text\":");
32- try json.writeString(w, t.text);
33- try w.writeByte('}');
34- for (t.images) |img| {
35- try w.writeByte(',');
36- try writeImage(w, img);
13+const ImageSource = struct {
14+ @"type": []const u8 = "base64",
15+ @"media_type": []const u8,
16+ data: []const u8,
17+};
18+
19+const ImageBlock = struct {
20+ @"type": []const u8 = "image",
21+ source: ImageSource,
22+};
23+
24+const TextBlock = struct { @"type": []const u8 = "text", text: []const u8 };
25+
26+const ThinkingBlock = struct {
27+ @"type": []const u8 = "thinking",
28+ thinking: []const u8,
29+ signature: []const u8,
30+};
31+
32+const ToolUseBlock = struct {
33+ @"type": []const u8 = "tool_use",
34+ id: []const u8,
35+ name: []const u8,
36+ input: api.Raw,
37+};
38+
39+/// One block of a tool_result's content: text or an image.
40+const ResultContent = union(enum) {
41+ text: TextBlock,
42+ image: ImageBlock,
43+
44+ pub fn jsonStringify(self: ResultContent, jws: anytype) !void {
45+ switch (self) {
46+ inline else => |v| try jws.write(v),
3747 }
38- try w.writeByte(']');
3948 }
40- try w.writeByte('}');
41-}
49+};
4250
43-/// An assistant turn as content blocks. Thinking blocks carry their signature
44-/// back so the provider's continuation data survives the round trip; a
45-/// thinking block without a signature becomes plain text.
46-fn writeAssistant(w: *std.Io.Writer, am: *const types.AssistantMessage) !void {
47- try w.writeAll("{\"role\":\"assistant\",\"content\":[");
48- var first = true;
49- for (am.content.items) |block| switch (block) {
50- .text => |t| {
51- if (!first) try w.writeByte(',');
52- first = false;
53- try w.writeAll("{\"type\":\"text\",\"text\":");
54- try json.writeString(w, t);
55- try w.writeByte('}');
56- },
57- .thinking => |t| {
58- if (!first) try w.writeByte(',');
59- first = false;
60- if (t.signature) |sig| {
61- if (sig.len > 0) {
62- try w.writeAll("{\"type\":\"thinking\",\"thinking\":");
63- try json.writeString(w, t.text);
64- try w.writeAll(",\"signature\":");
65- try json.writeString(w, sig);
66- try w.writeByte('}');
67- continue;
68- }
51+/// A tool result as one block of a user message. Its content is a string when
52+/// there is only text, and an array when images must ride along.
53+const ToolResultBlock = struct {
54+ @"type": []const u8 = "tool_result",
55+ @"tool_use_id": []const u8,
56+ @"is_error": ?bool = null,
57+ content: Content,
58+ const Content = union(enum) {
59+ text: []const u8,
60+ parts: []const ResultContent,
61+
62+ pub fn jsonStringify(self: Content, jws: anytype) !void {
63+ switch (self) {
64+ .text => |v| try jws.write(v),
65+ .parts => |v| try jws.write(v),
6966 }
70- try w.writeAll("{\"type\":\"text\",\"text\":");
71- try json.writeString(w, t.text);
72- try w.writeByte('}');
73- },
74- .tool_call => |c| {
75- if (!first) try w.writeByte(',');
76- first = false;
77- try w.writeAll("{\"type\":\"tool_use\",\"id\":");
78- try json.writeString(w, c.id);
79- try w.writeAll(",\"name\":");
80- try json.writeString(w, c.name);
81- try w.writeAll(",\"input\":");
82- try w.writeAll(api.argumentsOrObject(c.arguments));
83- try w.writeByte('}');
84- },
67+ }
8568 };
86- try w.writeAll("]}");
87-}
69+};
8870
89-fn writeUser(w: *std.Io.Writer, u: types.Message) !void {
90- try w.writeAll("{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":");
91- try json.writeString(w, u.user.content);
92- try w.writeAll("}]}");
93-}
71+/// A content block of an assistant turn. A thinking block carries its
72+/// signature back so the provider's continuation data survives the round trip;
73+/// a thinking block without one becomes plain text.
74+const AssistantBlock = union(enum) {
75+ text: TextBlock,
76+ thinking: ThinkingBlock,
77+ plain: TextBlock,
78+ tool_use: ToolUseBlock,
79+
80+ pub fn jsonStringify(self: AssistantBlock, jws: anytype) !void {
81+ switch (self) {
82+ inline else => |v| try jws.write(v),
83+ }
84+ }
85+};
86+
87+const UserMessage = struct {
88+ role: []const u8 = "user",
89+ content: []const ResultContent,
90+};
91+
92+const AssistantMessage = struct {
93+ role: []const u8 = "assistant",
94+ content: []const AssistantBlock,
95+};
96+
97+/// One entry of the `messages` array: a user turn (with tool results) or an
98+/// assistant turn.
99+const Message = union(enum) {
100+ user_text: struct { role: []const u8 = "user", content: []const TextBlock },
101+ user_results: struct { role: []const u8 = "user", content: []const ToolResultBlock },
102+ assistant: AssistantMessage,
103+
104+ pub fn jsonStringify(self: Message, jws: anytype) !void {
105+ switch (self) {
106+ inline else => |v| try jws.write(v),
107+ }
108+ }
109+};
110+
111+const Thinking = struct { @"type": []const u8 = "enabled", @"budget_tokens": u64 };
112+
113+const Body = struct {
114+ model: []const u8,
115+ @"max_tokens": u64,
116+ stream: bool = true,
117+ system: ?[]const u8 = null,
118+ thinking: ?Thinking = null,
119+ messages: []const Message,
120+ tools: ?api.Tools = null,
121+};
94122
95123 /// Anthropic thinking budget per effort. Anthropic requires max_tokens to
96124 /// exceed the budget, so the caller raises max_tokens when it does not.
@@ -109,67 +137,92 @@ fn budgetFor(effort: []const u8) u64 {
109137 return 0;
110138 }
111139
112-fn buildBody(req: api.Request) ![]u8 {
113- var out: std.Io.Writer.Allocating = .init(req.scratch);
114- const w = &out.writer;
115-
116- const budget = budgetFor(req.effort);
117- var max_tokens: u64 = if (req.model.max_tokens > 0) req.model.max_tokens else 8192;
118- if (budget > 0 and max_tokens <= budget) max_tokens = budget + 4096;
119-
120- try w.writeAll("{\"model\":");
121- try json.writeString(w, req.model.id);
122- try w.writeAll(",\"max_tokens\":");
123- try json.writeNum(w, max_tokens);
124- try w.writeAll(",\"stream\":true");
125- if (req.system_prompt.len > 0) {
126- try w.writeAll(",\"system\":");
127- try json.writeString(w, req.system_prompt);
128- }
129- if (budget > 0) {
130- try w.writeAll(",\"thinking\":{\"type\":\"enabled\",\"budget_tokens\":");
131- try json.writeNum(w, budget);
132- try w.writeByte('}');
140+fn resultContent(a: std.mem.Allocator, t: types.ToolResultMessage) !ToolResultBlock.Content {
141+ if (t.images.len == 0) return .{ .text = t.text };
142+ var parts: std.ArrayList(ResultContent) = .empty;
143+ try parts.append(a, .{ .text = .{ .text = t.text } });
144+ for (t.images) |img| {
145+ try parts.append(a, .{ .image = .{ .source = .{ .@"media_type" = img.mime_type, .data = img.data } } });
133146 }
147+ return .{ .parts = parts.items };
148+}
149+
150+fn assistantBlocks(a: std.mem.Allocator, am: *const types.AssistantMessage) ![]const AssistantBlock {
151+ var blocks: std.ArrayList(AssistantBlock) = .empty;
152+ for (am.content.items) |block| switch (block) {
153+ .text => |t| try blocks.append(a, .{ .text = .{ .text = t } }),
154+ .thinking => |t| {
155+ if (t.signature) |sig| {
156+ if (sig.len > 0) {
157+ try blocks.append(a, .{ .thinking = .{ .thinking = t.text, .signature = sig } });
158+ continue;
159+ }
160+ }
161+ try blocks.append(a, .{ .plain = .{ .text = t.text } });
162+ },
163+ .tool_call => |c| try blocks.append(a, .{ .tool_use = .{
164+ .id = c.id,
165+ .name = c.name,
166+ .input = .{ .bytes = api.argumentsOrObject(c.arguments) },
167+ } }),
168+ };
169+ return blocks.items;
170+}
134171
135- try w.writeAll(",\"messages\":[");
136- var first = true;
172+fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message {
173+ var out: std.ArrayList(Message) = .empty;
137174 var i: usize = 0;
138175 while (i < req.messages.len) {
139176 if (req.messages[i] == .tool_result) {
140177 // Every result of one assistant turn shares a single user message.
141- if (!first) try w.writeByte(',');
142- first = false;
143- try w.writeAll("{\"role\":\"user\",\"content\":[");
144- var first_block = true;
178+ var blocks: std.ArrayList(ToolResultBlock) = .empty;
145179 while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {
146- if (!first_block) try w.writeByte(',');
147- first_block = false;
148- try writeToolResult(w, req.messages[i].tool_result);
180+ const t = req.messages[i].tool_result;
181+ try blocks.append(a, .{
182+ .@"tool_use_id" = t.tool_call_id,
183+ .@"is_error" = if (t.is_error) true else null,
184+ .content = try resultContent(a, t),
185+ });
149186 }
150- try w.writeAll("]}");
187+ try out.append(a, .{ .user_results = .{ .content = blocks.items } });
151188 continue;
152189 }
153- if (!first) try w.writeByte(',');
154- first = false;
155190 switch (req.messages[i]) {
156- .user => try writeUser(w, req.messages[i]),
157- .assistant => |am| try writeAssistant(w, am),
191+ .user => |u| {
192+ const blocks = try a.alloc(TextBlock, 1);
193+ blocks[0] = .{ .text = u.content };
194+ try out.append(a, .{ .user_text = .{ .content = blocks } });
195+ },
196+ .assistant => |am| try out.append(a, .{ .assistant = .{
197+ .content = try assistantBlocks(a, am),
198+ } }),
158199 .tool_result => unreachable,
159200 }
160201 i += 1;
161202 }
162- try w.writeByte(']');
163-
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- });
171- try w.writeByte('}');
172- return out.written();
203+ return out.items;
204+}
205+
206+fn buildBody(req: api.Request) ![]u8 {
207+ const a = req.scratch;
208+ const budget = budgetFor(req.effort);
209+ var max_tokens: u64 = if (req.model.max_tokens > 0) req.model.max_tokens else 8192;
210+ if (budget > 0 and max_tokens <= budget) max_tokens = budget + 4096;
211+ const body = Body{
212+ .model = req.model.id,
213+ .@"max_tokens" = max_tokens,
214+ .system = if (req.system_prompt.len > 0) req.system_prompt else null,
215+ .thinking = if (budget > 0) .{ .@"budget_tokens" = budget } else null,
216+ .messages = try buildMessages(a, req),
217+ .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
218+ .open = "[",
219+ .entry = "{",
220+ .params_key = "input_schema",
221+ .entry_close = "}",
222+ .close = "]",
223+ } } else null,
224+ };
225+ return std.json.Stringify.valueAlloc(a, body, .{ .emit_null_optional_fields = false });
173226 }
174227
175228 // ---- streaming state ------------------------------------------------------
@@ -184,6 +237,45 @@ const Block = struct {
184237 call: usize = 0,
185238 };
186239
240+const Chunk = struct {
241+ @"type": ?[]const u8 = null,
242+ @"error": ?api.ErrField = null,
243+ index: ?i64 = null,
244+ message: ?MessageStart = null,
245+ @"content_block": ?BlockStart = null,
246+ delta: ?Delta = null,
247+ usage: ?Usage = null,
248+
249+ const MessageStart = struct {
250+ id: ?[]const u8 = null,
251+ usage: ?Usage = null,
252+ };
253+
254+ const BlockStart = struct {
255+ @"type": ?[]const u8 = null,
256+ id: ?[]const u8 = null,
257+ name: ?[]const u8 = null,
258+ data: ?[]const u8 = null,
259+ };
260+
261+ const Delta = struct {
262+ @"type": ?[]const u8 = null,
263+ text: ?[]const u8 = null,
264+ thinking: ?[]const u8 = null,
265+ signature: ?[]const u8 = null,
266+ @"partial_json": ?[]const u8 = null,
267+ @"stop_reason": ?[]const u8 = null,
268+ };
269+
270+ const Usage = struct {
271+ @"input_tokens": api.Count = .{},
272+ @"output_tokens": api.Count = .{},
273+ @"cache_read_input_tokens": api.Count = .{},
274+ @"cache_creation_input_tokens": api.Count = .{},
275+ @"output_tokens_details": ?struct { @"thinking_tokens": api.Count = .{} } = null,
276+ };
277+};
278+
187279 const State = struct {
188280 req: api.Request,
189281 sink: api.Sink,
@@ -197,43 +289,48 @@ const State = struct {
197289 /// Set by `run` when the stream did not run to completion.
198290 failed: bool = false,
199291
200- pub fn handle(st: *State, obj: std.json.ObjectMap) !void {
201- const event_type = api.stringField(obj, "type") orelse "";
292+ pub fn handle(st: *State, chunk: Chunk) !void {
293+ const event_type = chunk.@"type" orelse "";
202294
203295 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");
296+ const message = if (chunk.@"error") |e| e.message orelse "stream: provider error" else "stream: provider error";
297+ if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, message);
205298 return;
206299 }
207300
208301 if (std.mem.eql(u8, event_type, "message_start")) {
209- const message = api.objField(obj, "message") orelse return;
302+ const message = chunk.message orelse return;
210303 if (st.msg.response_id == null) {
211- if (api.stringField(message, "id")) |id| st.msg.response_id = try st.req.pers.dupe(u8, id);
304+ if (message.id) |id| {
305+ if (id.len > 0) st.msg.response_id = try st.req.pers.dupe(u8, id);
306+ }
212307 }
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");
308+ if (message.usage) |u| {
309+ st.usage.input = u.@"input_tokens".value;
310+ st.usage.output = u.@"output_tokens".value;
311+ st.usage.cache_read = u.@"cache_read_input_tokens".value;
312+ st.usage.cache_write = u.@"cache_creation_input_tokens".value;
218313 }
219314 return;
220315 }
221316
222317 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 "";
318+ const block = try api.blockAt(Block, &st.blocks, st.req.pers, chunk.index orelse -1);
319+ const cb = chunk.@"content_block" orelse return;
320+ const bt = cb.@"type" orelse "";
226321 if (std.mem.eql(u8, bt, "tool_use")) {
227322 block.kind = .tool;
228323 block.call = st.calls.items.len;
229324 const call = try st.calls.addOne(st.req.pers);
230325 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 "");
326+ if (cb.id) |id| {
327+ if (id.len > 0) call.id = try st.req.pers.dupe(u8, id);
328+ }
329+ try call.announce(st.req.pers, st.sink, cb.name orelse "");
233330 } else if (std.mem.eql(u8, bt, "thinking") or std.mem.eql(u8, bt, "redacted_thinking")) {
234331 block.kind = .thinking;
235332 if (std.mem.eql(u8, bt, "redacted_thinking")) {
236- if (api.stringField(cb, "data")) |d| try block.signature.appendSlice(st.req.pers, d);
333+ if (cb.data) |d| try block.signature.appendSlice(st.req.pers, d);
237334 }
238335 } else {
239336 block.kind = .text;
@@ -242,48 +339,44 @@ const State = struct {
242339 }
243340
244341 if (std.mem.eql(u8, event_type, "content_block_delta")) {
245- const idx = api.intOr(obj, "index", -1);
342+ const idx = chunk.index orelse -1;
246343 if (idx < 0 or idx >= st.blocks.items.len) return;
247- const delta = api.objField(obj, "delta") orelse return;
344+ const delta = chunk.delta orelse return;
248345 const block = &st.blocks.items[@intCast(idx)];
249346 const call = if (block.kind == .tool) &st.calls.items[block.call] else null;
250- const dt = api.stringField(delta, "type") orelse "";
347+ const dt = delta.@"type" orelse "";
251348 if (std.mem.eql(u8, dt, "text_delta")) {
252- if (api.stringField(delta, "text")) |s| {
349+ if (delta.text) |s| {
253350 try block.text.appendSlice(st.req.pers, s);
254351 st.sink.emit(.{ .text = s });
255352 }
256353 } else if (std.mem.eql(u8, dt, "thinking_delta")) {
257- if (api.stringField(delta, "thinking")) |s| {
354+ if (delta.thinking) |s| {
258355 try block.text.appendSlice(st.req.pers, s);
259356 st.sink.emit(.{ .reasoning = s });
260357 }
261358 } 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- }
359+ if (delta.signature) |s| try block.signature.appendSlice(st.req.pers, s);
265360 } else if (std.mem.eql(u8, dt, "input_json_delta")) {
266361 if (call) |c| {
267- if (api.stringField(delta, "partial_json")) |s| try c.args.appendSlice(st.req.pers, s);
362+ if (delta.@"partial_json") |s| try c.args.appendSlice(st.req.pers, s);
268363 }
269364 }
270365 return;
271366 }
272367
273368 if (std.mem.eql(u8, event_type, "message_delta")) {
274- if (api.objField(obj, "delta")) |delta| {
275- if (api.stringField(delta, "stop_reason")) |s| {
276- st.stop_reason = try st.req.pers.dupe(u8, s);
369+ if (chunk.delta) |delta| {
370+ if (delta.@"stop_reason") |s| {
371+ if (s.len > 0) st.stop_reason = try st.req.pers.dupe(u8, s);
277372 }
278373 }
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");
286- }
374+ if (chunk.usage) |u| {
375+ st.usage.input = u.@"input_tokens".value;
376+ st.usage.output = u.@"output_tokens".value;
377+ st.usage.cache_read = u.@"cache_read_input_tokens".value;
378+ st.usage.cache_write = u.@"cache_creation_input_tokens".value;
379+ if (u.@"output_tokens_details") |d| st.usage.reasoning = d.@"thinking_tokens".value;
287380 }
288381 return;
289382 }
@@ -346,5 +439,5 @@ const wire = api.Wire{
346439 };
347440
348441 pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!types.AssistantMessage {
349- return api.run(State, wire, req, sink, finalize);
442+ return api.run(State, Chunk, wire, req, sink, finalize);
350443 }
src/api/google_generative_ai.zig+157−145
@@ -4,62 +4,64 @@
44 const std = @import("std");
55 const api = @import("../api.zig");
66 const types = @import("../types.zig");
7-const json = @import("../json.zig");
87
98 // ---- request building -----------------------------------------------------
109
11-fn writeText(w: *std.Io.Writer, text: []const u8) !void {
12- try w.writeAll("{\"text\":");
13- try json.writeString(w, text);
14- try w.writeByte('}');
15-}
10+/// A part of a content object. Exactly one member is set; the rest are null
11+/// and are left out.
12+const Part = struct {
13+ text: ?[]const u8 = null,
14+ @"inlineData": ?struct { @"mimeType": []const u8, data: []const u8 } = null,
15+ @"functionCall": ?struct { name: []const u8, args: api.Raw, @"thoughtSignature": ?[]const u8 = null } = null,
16+ @"functionResponse": ?struct {
17+ name: []const u8,
18+ response: Response,
19+ const Response = struct { output: ?[]const u8 = null, @"error": ?[]const u8 = null };
20+ } = null,
21+};
1622
17-fn writeImage(w: *std.Io.Writer, img: types.ImageContent) !void {
18- try w.writeAll("{\"inlineData\":{\"mimeType\":");
19- try json.writeString(w, img.mime_type);
20- try w.writeAll(",\"data\":");
21- try json.writeString(w, img.data);
22- try w.writeAll("}}");
23-}
23+const Content = struct {
24+ role: []const u8,
25+ parts: []const Part,
26+};
2427
25-fn writeFunctionCall(w: *std.Io.Writer, c: types.ToolCall) !void {
26- try w.writeAll("{\"functionCall\":{\"name\":");
27- try json.writeString(w, c.name);
28- try w.writeAll(",\"args\":");
29- try w.writeAll(api.argumentsOrObject(c.arguments));
30- try w.writeByte('}');
31- if (c.thought_signature) |sig| {
32- try w.writeAll(",\"thoughtSignature\":");
33- try json.writeString(w, sig);
34- }
35- try w.writeByte('}');
28+const SystemInstruction = struct { parts: []const Part };
29+
30+const GenerationConfig = struct {
31+ @"maxOutputTokens": ?u64 = null,
32+ @"thinkingConfig": ?struct { @"includeThoughts": bool = true, @"thinkingBudget": u64 } = null,
33+};
34+
35+const Body = struct {
36+ contents: []const Content,
37+ @"systemInstruction": ?SystemInstruction = null,
38+ tools: ?api.Tools = null,
39+ @"generationConfig": ?GenerationConfig = null,
40+};
41+
42+fn textPart(text: []const u8) Part {
43+ return .{ .text = text };
3644 }
3745
38-/// A user or assistant message as a content object.
39-fn writeContent(w: *std.Io.Writer, msg: types.Message) !void {
46+fn contentParts(a: std.mem.Allocator, msg: types.Message) ![]const Part {
4047 switch (msg) {
4148 .user => |u| {
42- try w.writeAll("{\"role\":\"user\",\"parts\":[");
43- try writeText(w, u.content);
44- try w.writeAll("]}");
49+ const parts = try a.alloc(Part, 1);
50+ parts[0] = textPart(u.content);
51+ return parts;
4552 },
4653 .assistant => |am| {
47- try w.writeAll("{\"role\":\"model\",\"parts\":[");
48- var first = true;
54+ var parts: std.ArrayList(Part) = .empty;
4955 for (am.content.items) |block| switch (block) {
50- .text => |t| {
51- if (!first) try w.writeByte(',');
52- first = false;
53- try writeText(w, t);
54- },
55- .tool_call => |c| {
56- if (!first) try w.writeByte(',');
57- first = false;
58- try writeFunctionCall(w, c);
59- },
56+ .text => |t| try parts.append(a, textPart(t)),
57+ .tool_call => |c| try parts.append(a, .{ .@"functionCall" = .{
58+ .name = c.name,
59+ .args = .{ .bytes = api.argumentsOrObject(c.arguments) },
60+ .@"thoughtSignature" = c.thought_signature,
61+ } }),
6062 .thinking => {},
6163 };
62- try w.writeAll("]}");
64+ return parts.items;
6365 },
6466 .tool_result => unreachable,
6567 }
@@ -67,20 +69,36 @@ fn writeContent(w: *std.Io.Writer, msg: types.Message) !void {
6769
6870 /// A run of tool results becomes one user turn of functionResponse parts. The
6971 /// response key is "error" when the tool failed and "output" otherwise.
70-fn writeToolResults(w: *std.Io.Writer, run: []const types.Message) !void {
71- try w.writeAll("{\"role\":\"user\",\"parts\":[");
72- for (run, 0..) |msg, i| {
73- if (i > 0) try w.writeByte(',');
72+fn toolResultContent(a: std.mem.Allocator, run: []const types.Message) !Content {
73+ const parts = try a.alloc(Part, run.len);
74+ for (run, parts) |msg, *part| {
7475 const t = msg.tool_result;
75- try w.writeAll("{\"functionResponse\":{\"name\":");
76- try json.writeString(w, t.tool_name);
77- try w.writeAll(",\"response\":{\"");
78- try w.writeAll(if (t.is_error) "error" else "output");
79- try w.writeAll("\":");
80- try json.writeString(w, t.text);
81- try w.writeAll("}}}");
76+ part.* = .{ .@"functionResponse" = .{ .name = t.tool_name, .response = if (t.is_error)
77+ .{ .@"error" = t.text }
78+ else
79+ .{ .output = t.text } } };
80+ }
81+ return .{ .role = "user", .parts = parts };
82+}
83+
84+fn buildContents(a: std.mem.Allocator, req: api.Request) ![]const Content {
85+ var out: std.ArrayList(Content) = .empty;
86+ var i: usize = 0;
87+ while (i < req.messages.len) {
88+ if (req.messages[i] == .tool_result) {
89+ const start = i;
90+ while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {}
91+ try out.append(a, try toolResultContent(a, req.messages[start..i]));
92+ continue;
93+ }
94+ const msg = req.messages[i];
95+ try out.append(a, .{
96+ .role = if (msg == .assistant) "model" else "user",
97+ .parts = try contentParts(a, msg),
98+ });
99+ i += 1;
82100 }
83- try w.writeAll("]}");
101+ return out.items;
84102 }
85103
86104 /// Thinking budget per effort. `off` disables thinking entirely.
@@ -101,62 +119,30 @@ fn budgetFor(effort: []const u8) ?u64 {
101119 }
102120
103121 fn buildBody(req: api.Request) ![]u8 {
104- var out: std.Io.Writer.Allocating = .init(req.scratch);
105- const w = &out.writer;
106-
107- try w.writeAll("{\"contents\":[");
108- var first = true;
109- var i: usize = 0;
110- while (i < req.messages.len) {
111- if (req.messages[i] == .tool_result) {
112- const start = i;
113- while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {}
114- if (!first) try w.writeByte(',');
115- first = false;
116- try writeToolResults(w, req.messages[start..i]);
117- continue;
118- }
119- if (!first) try w.writeByte(',');
120- first = false;
121- try writeContent(w, req.messages[i]);
122- i += 1;
123- }
124- try w.writeByte(']');
125-
126- if (req.system_prompt.len > 0) {
127- try w.writeAll(",\"systemInstruction\":{\"parts\":[");
128- try writeText(w, req.system_prompt);
129- try w.writeAll("]}");
130- }
131-
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(',');
Cut short: this commit's diff is over 1000 lines.
src/api/openai_completions.zig+215−145
Cut short: this commit's diff is over 1000 lines.
src/api/openai_responses.zig+164−121
Cut short: this commit's diff is over 1000 lines.
src/config.zig+37−144
Cut short: this commit's diff is over 1000 lines.
src/json.zig+0−40
Cut short: this commit's diff is over 1000 lines.
src/models.zig+14−5
Cut short: this commit's diff is over 1000 lines.
src/session.zig+264−147
Cut short: this commit's diff is over 1000 lines.
src/tools/index.zig+2−3
Cut short: this commit's diff is over 1000 lines.