xonecas.io
79113
Ma

mini-coding-agent / commit 2382098

fix: remove duplication, consolidate code, minor fixes.

xonecas2026-10-07 23820988c6db92e9f8525aaf39735ad02e94b251 parent d602c3f

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

AGENTS.md+7−5
@@ -19,10 +19,12 @@ Verify against the real provider. Use the `OPENCODE_API_KEY` environment
1919 variable and a scratch `XDG_CONFIG_HOME` with a `mini-coding-agent/config.json`
2020 that declares the provider under test.
2121
22-Every Zen/Go request needs the `x-opencode-session` header; set it via the custom
23-provider's `headers`. Not every catalog model is enabled for the account (for
24-example `claude-opus-5` is disabled and returns `Model access is disabled`).
25-Run a real tool call, not just a text turn.
22+Use these models:
23+
24+- `openai-completions` -> `opnecode-go/deepseek-v4.1-flash`
25+- `openai-responses` -> `opencode-go/gpt-6-luna`
26+- `anthropic-messages` -> `opencode-go/minimax-m3`
27+- `google-generative-ai` -> `opencode/gemini-3.8-flash`
2628
2729 ### Driving the TUI with kitty
2830
@@ -31,7 +33,7 @@ window over remote control on a private socket, one window per run: it launches
3133 the app in a scratch cwd, waits for the banner, types the prompt, and
3234 screenshots the window at 15fps until the capture window ends or the app exits.
3335
34- tools/stress.sh -C <scratch cwd> -o /tmp/mini-runs/<name> -d 300 -p '<prompt>'
36+ tools/stress.sh -C <scratch cwd> -o /tmp/mini-runs/<name> -d 60 -p '<prompt>'
3537
3638 Frames land in `frame-NNNNN.png`. At the end of a run the pane is written with
3739 its truecolor SGR to `final.txt` (scrollback) and `final-screen.txt` (live
src/api.zig+13−9
@@ -19,15 +19,7 @@ pub const Sink = struct {
1919 }
2020 };
2121
22-pub const Raw = struct {
23- bytes: []const u8 = "",
24-
25- pub fn jsonStringify(self: Raw, jws: anytype) !void {
26- try jws.beginWriteRaw();
27- try jws.writer.writeAll(self.bytes);
28- jws.endWriteRaw();
29- }
30-};
22+pub const Raw = types.Raw;
3123
3224 pub const ErrField = struct {
3325 message: ?[]const u8 = null,
@@ -270,6 +262,18 @@ pub fn argumentsOrObject(args: []const u8) []const u8 {
270262 return if (args.len > 0) args else "{}";
271263 }
272264
265+pub fn keepString(slot: *?[]const u8, a: std.mem.Allocator, value: ?[]const u8) !void {
266+ if (slot.* != null) return;
267+ const v = value orelse return;
268+ if (v.len == 0) return;
269+ slot.* = try a.dupe(u8, v);
270+}
271+
272+pub fn appendParts(msg: *types.AssistantMessage, a: std.mem.Allocator, thinking: []const u8, signature: ?[]const u8, text: []const u8) !void {
273+ if (thinking.len > 0) try msg.content.append(a, .{ .thinking = .{ .text = thinking, .signature = signature } });
274+ if (text.len > 0) try msg.content.append(a, .{ .text = text });
275+}
276+
273277 pub fn finishUsage(msg: *types.AssistantMessage, model: *const types.Model, usage: types.Usage) void {
274278 var u = usage;
275279 if (u.total_tokens == 0) u.total_tokens = u.input + u.output + u.cache_read + u.cache_write;
src/api/anthropic_messages.zig+44−53
@@ -5,26 +5,26 @@ const types = @import("../types.zig");
55 const anthropic_version = "2023-06-01";
66
77 const ImageSource = struct {
8- @"type": []const u8 = "base64",
9- @"media_type": []const u8,
8+ type: []const u8 = "base64",
9+ media_type: []const u8,
1010 data: []const u8,
1111 };
1212
1313 const ImageBlock = struct {
14- @"type": []const u8 = "image",
14+ type: []const u8 = "image",
1515 source: ImageSource,
1616 };
1717
18-const TextBlock = struct { @"type": []const u8 = "text", text: []const u8 };
18+const TextBlock = struct { type: []const u8 = "text", text: []const u8 };
1919
2020 const ThinkingBlock = struct {
21- @"type": []const u8 = "thinking",
21+ type: []const u8 = "thinking",
2222 thinking: []const u8,
2323 signature: []const u8,
2424 };
2525
2626 const ToolUseBlock = struct {
27- @"type": []const u8 = "tool_use",
27+ type: []const u8 = "tool_use",
2828 id: []const u8,
2929 name: []const u8,
3030 input: api.Raw,
@@ -42,9 +42,9 @@ const ResultContent = union(enum) {
4242 };
4343
4444 const ToolResultBlock = struct {
45- @"type": []const u8 = "tool_result",
46- @"tool_use_id": []const u8,
47- @"is_error": ?bool = null,
45+ type: []const u8 = "tool_result",
46+ tool_use_id: []const u8,
47+ is_error: ?bool = null,
4848 content: Content,
4949 const Content = union(enum) {
5050 text: []const u8,
@@ -72,11 +72,6 @@ const AssistantBlock = union(enum) {
7272 }
7373 };
7474
75-const UserMessage = struct {
76- role: []const u8 = "user",
77- content: []const ResultContent,
78-};
79-
8075 const AssistantMessage = struct {
8176 role: []const u8 = "assistant",
8277 content: []const AssistantBlock,
@@ -94,11 +89,11 @@ const Message = union(enum) {
9489 }
9590 };
9691
97-const Thinking = struct { @"type": []const u8 = "enabled", @"budget_tokens": u64 };
92+const Thinking = struct { type: []const u8 = "enabled", budget_tokens: u64 };
9893
9994 const Body = struct {
10095 model: []const u8,
101- @"max_tokens": u64,
96+ max_tokens: u64,
10297 stream: bool = true,
10398 system: ?[]const u8 = null,
10499 thinking: ?Thinking = null,
@@ -126,7 +121,7 @@ fn resultContent(a: std.mem.Allocator, t: types.ToolResultMessage) !ToolResultBl
126121 var parts: std.ArrayList(ResultContent) = .empty;
127122 try parts.append(a, .{ .text = .{ .text = t.text } });
128123 for (t.images) |img| {
129- try parts.append(a, .{ .image = .{ .source = .{ .@"media_type" = img.mime_type, .data = img.data } } });
124+ try parts.append(a, .{ .image = .{ .source = .{ .media_type = img.mime_type, .data = img.data } } });
130125 }
131126 return .{ .parts = parts.items };
132127 }
@@ -162,8 +157,8 @@ fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message {
162157 while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {
163158 const t = req.messages[i].tool_result;
164159 try blocks.append(a, .{
165- .@"tool_use_id" = t.tool_call_id,
166- .@"is_error" = if (t.is_error) true else null,
160+ .tool_use_id = t.tool_call_id,
161+ .is_error = if (t.is_error) true else null,
167162 .content = try resultContent(a, t),
168163 });
169164 }
@@ -193,9 +188,9 @@ fn buildBody(req: api.Request) ![]u8 {
193188 if (budget > 0 and max_tokens <= budget) max_tokens = budget + 4096;
194189 const body = Body{
195190 .model = req.model.id,
196- .@"max_tokens" = max_tokens,
191+ .max_tokens = max_tokens,
197192 .system = if (req.system_prompt.len > 0) req.system_prompt else null,
198- .thinking = if (budget > 0) .{ .@"budget_tokens" = budget } else null,
193+ .thinking = if (budget > 0) .{ .budget_tokens = budget } else null,
199194 .messages = try buildMessages(a, req),
200195 .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
201196 .open = "[",
@@ -218,11 +213,11 @@ const Block = struct {
218213 };
219214
220215 const Chunk = struct {
221- @"type": ?[]const u8 = null,
216+ type: ?[]const u8 = null,
222217 @"error": ?api.ErrField = null,
223218 index: ?i64 = null,
224219 message: ?MessageStart = null,
225- @"content_block": ?BlockStart = null,
220+ content_block: ?BlockStart = null,
226221 delta: ?Delta = null,
227222 usage: ?Usage = null,
228223
@@ -232,27 +227,27 @@ const Chunk = struct {
232227 };
233228
234229 const BlockStart = struct {
235- @"type": ?[]const u8 = null,
230+ type: ?[]const u8 = null,
236231 id: ?[]const u8 = null,
237232 name: ?[]const u8 = null,
238233 data: ?[]const u8 = null,
239234 };
240235
241236 const Delta = struct {
242- @"type": ?[]const u8 = null,
237+ type: ?[]const u8 = null,
243238 text: ?[]const u8 = null,
244239 thinking: ?[]const u8 = null,
245240 signature: ?[]const u8 = null,
246- @"partial_json": ?[]const u8 = null,
247- @"stop_reason": ?[]const u8 = null,
241+ partial_json: ?[]const u8 = null,
242+ stop_reason: ?[]const u8 = null,
248243 };
249244
250245 const Usage = struct {
251- @"input_tokens": api.Count = .{},
252- @"output_tokens": api.Count = .{},
253- @"cache_read_input_tokens": api.Count = .{},
254- @"cache_creation_input_tokens": api.Count = .{},
255- @"output_tokens_details": ?struct { @"thinking_tokens": api.Count = .{} } = null,
246+ input_tokens: api.Count = .{},
247+ output_tokens: api.Count = .{},
248+ cache_read_input_tokens: api.Count = .{},
249+ cache_creation_input_tokens: api.Count = .{},
250+ output_tokens_details: ?struct { thinking_tokens: api.Count = .{} } = null,
256251 };
257252 };
258253
@@ -269,34 +264,30 @@ const State = struct {
269264 failed: bool = false,
270265
271266 pub fn handle(st: *State, chunk: Chunk) !void {
272- const event_type = chunk.@"type" orelse "";
267+ const event_type = chunk.type orelse "";
273268
274269 if (std.mem.eql(u8, event_type, "error")) {
275270 const message = if (chunk.@"error") |e| e.message orelse "stream: provider error" else "stream: provider error";
276- if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, message);
271+ try api.keepString(&st.stream_error, st.req.pers, message);
277272 return;
278273 }
279274
280275 if (std.mem.eql(u8, event_type, "message_start")) {
281276 const message = chunk.message orelse return;
282- if (st.msg.response_id == null) {
283- if (message.id) |id| {
284- if (id.len > 0) st.msg.response_id = try st.req.pers.dupe(u8, id);
285- }
286- }
277+ try api.keepString(&st.msg.response_id, st.req.pers, message.id);
287278 if (message.usage) |u| {
288- st.usage.input = u.@"input_tokens".value;
289- st.usage.output = u.@"output_tokens".value;
290- st.usage.cache_read = u.@"cache_read_input_tokens".value;
291- st.usage.cache_write = u.@"cache_creation_input_tokens".value;
279+ st.usage.input = u.input_tokens.value;
280+ st.usage.output = u.output_tokens.value;
281+ st.usage.cache_read = u.cache_read_input_tokens.value;
282+ st.usage.cache_write = u.cache_creation_input_tokens.value;
292283 }
293284 return;
294285 }
295286
296287 if (std.mem.eql(u8, event_type, "content_block_start")) {
297288 const block = try api.blockAt(Block, &st.blocks, st.req.pers, chunk.index orelse -1);
298- const cb = chunk.@"content_block" orelse return;
299- const bt = cb.@"type" orelse "";
289+ const cb = chunk.content_block orelse return;
290+ const bt = cb.type orelse "";
300291 if (std.mem.eql(u8, bt, "tool_use")) {
301292 block.kind = .tool;
302293 block.call = st.calls.items.len;
@@ -323,7 +314,7 @@ const State = struct {
323314 const delta = chunk.delta orelse return;
324315 const block = &st.blocks.items[@intCast(idx)];
325316 const call = if (block.kind == .tool) &st.calls.items[block.call] else null;
326- const dt = delta.@"type" orelse "";
317+ const dt = delta.type orelse "";
327318 if (std.mem.eql(u8, dt, "text_delta")) {
328319 if (delta.text) |s| {
329320 try block.text.appendSlice(st.req.pers, s);
@@ -338,7 +329,7 @@ const State = struct {
338329 if (delta.signature) |s| try block.signature.appendSlice(st.req.pers, s);
339330 } else if (std.mem.eql(u8, dt, "input_json_delta")) {
340331 if (call) |c| {
341- if (delta.@"partial_json") |s| try c.args.appendSlice(st.req.pers, s);
332+ if (delta.partial_json) |s| try c.args.appendSlice(st.req.pers, s);
342333 }
343334 }
344335 return;
@@ -346,16 +337,16 @@ const State = struct {
346337
347338 if (std.mem.eql(u8, event_type, "message_delta")) {
348339 if (chunk.delta) |delta| {
349- if (delta.@"stop_reason") |s| {
340+ if (delta.stop_reason) |s| {
350341 if (s.len > 0) st.stop_reason = try st.req.pers.dupe(u8, s);
351342 }
352343 }
353344 if (chunk.usage) |u| {
354- st.usage.input = u.@"input_tokens".value;
355- st.usage.output = u.@"output_tokens".value;
356- st.usage.cache_read = u.@"cache_read_input_tokens".value;
357- st.usage.cache_write = u.@"cache_creation_input_tokens".value;
358- if (u.@"output_tokens_details") |d| st.usage.reasoning = d.@"thinking_tokens".value;
345+ st.usage.input = u.input_tokens.value;
346+ st.usage.output = u.output_tokens.value;
347+ st.usage.cache_read = u.cache_read_input_tokens.value;
348+ st.usage.cache_write = u.cache_creation_input_tokens.value;
349+ if (u.output_tokens_details) |d| st.usage.reasoning = d.thinking_tokens.value;
359350 }
360351 return;
361352 }
src/api/google_generative_ai.zig+32−33
@@ -4,9 +4,9 @@ const types = @import("../types.zig");
44
55 const Part = struct {
66 text: ?[]const u8 = null,
7- @"inlineData": ?struct { @"mimeType": []const u8, data: []const u8 } = null,
8- @"functionCall": ?struct { name: []const u8, args: api.Raw, @"thoughtSignature": ?[]const u8 = null } = null,
9- @"functionResponse": ?struct {
7+ inlineData: ?struct { mimeType: []const u8, data: []const u8 } = null,
8+ functionCall: ?struct { name: []const u8, args: api.Raw, thoughtSignature: ?[]const u8 = null } = null,
9+ functionResponse: ?struct {
1010 name: []const u8,
1111 response: Response,
1212 const Response = struct { output: ?[]const u8 = null, @"error": ?[]const u8 = null };
@@ -21,15 +21,15 @@ const Content = struct {
2121 const SystemInstruction = struct { parts: []const Part };
2222
2323 const GenerationConfig = struct {
24- @"maxOutputTokens": ?u64 = null,
25- @"thinkingConfig": ?struct { @"includeThoughts": bool = true, @"thinkingBudget": u64 } = null,
24+ maxOutputTokens: ?u64 = null,
25+ thinkingConfig: ?struct { includeThoughts: bool = true, thinkingBudget: u64 } = null,
2626 };
2727
2828 const Body = struct {
2929 contents: []const Content,
30- @"systemInstruction": ?SystemInstruction = null,
30+ systemInstruction: ?SystemInstruction = null,
3131 tools: ?api.Tools = null,
32- @"generationConfig": ?GenerationConfig = null,
32+ generationConfig: ?GenerationConfig = null,
3333 };
3434
3535 fn textPart(text: []const u8) Part {
@@ -47,10 +47,10 @@ fn contentParts(a: std.mem.Allocator, msg: types.Message) ![]const Part {
4747 var parts: std.ArrayList(Part) = .empty;
4848 for (am.content.items) |block| switch (block) {
4949 .text => |t| try parts.append(a, textPart(t)),
50- .tool_call => |c| try parts.append(a, .{ .@"functionCall" = .{
50+ .tool_call => |c| try parts.append(a, .{ .functionCall = .{
5151 .name = c.name,
5252 .args = .{ .bytes = api.argumentsOrObject(c.arguments) },
53- .@"thoughtSignature" = c.thought_signature,
53+ .thoughtSignature = c.thought_signature,
5454 } }),
5555 .thinking => {},
5656 };
@@ -64,7 +64,7 @@ fn toolResultContent(a: std.mem.Allocator, run: []const types.Message) !Content
6464 const parts = try a.alloc(Part, run.len);
6565 for (run, parts) |msg, *part| {
6666 const t = msg.tool_result;
67- part.* = .{ .@"functionResponse" = .{ .name = t.tool_name, .response = if (t.is_error)
67+ part.* = .{ .functionResponse = .{ .name = t.tool_name, .response = if (t.is_error)
6868 .{ .@"error" = t.text }
6969 else
7070 .{ .output = t.text } } };
@@ -112,8 +112,8 @@ fn buildBody(req: api.Request) ![]u8 {
112112 const a = req.scratch;
113113 const budget = budgetFor(req.effort);
114114 const config: ?GenerationConfig = if (req.model.max_tokens > 0 or budget != null) .{
115- .@"maxOutputTokens" = if (req.model.max_tokens > 0) req.model.max_tokens else null,
116- .@"thinkingConfig" = if (budget) |b| .{ .@"includeThoughts" = b > 0, .@"thinkingBudget" = b } else null,
115+ .maxOutputTokens = if (req.model.max_tokens > 0) req.model.max_tokens else null,
116+ .thinkingConfig = if (budget) |b| .{ .includeThoughts = b > 0, .thinkingBudget = b } else null,
117117 } else null;
118118 const sys = if (req.system_prompt.len > 0) blk: {
119119 const parts = try a.alloc(Part, 1);
@@ -122,7 +122,7 @@ fn buildBody(req: api.Request) ![]u8 {
122122 } else null;
123123 const body = Body{
124124 .contents = try buildContents(a, req),
125- .@"systemInstruction" = sys,
125+ .systemInstruction = sys,
126126 .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
127127 .open = "[{\"functionDeclarations\":[",
128128 .entry = "{",
@@ -130,7 +130,7 @@ fn buildBody(req: api.Request) ![]u8 {
130130 .entry_close = "}",
131131 .close = "]}]",
132132 } } else null,
133- .@"generationConfig" = config,
133+ .generationConfig = config,
134134 };
135135 return std.json.Stringify.valueAlloc(a, body, .{ .emit_null_optional_fields = false });
136136 }
@@ -149,26 +149,26 @@ fn buildUrl(a: std.mem.Allocator, req: api.Request) ![]u8 {
149149 const Chunk = struct {
150150 @"error": ?api.ErrField = null,
151151 candidates: []const Candidate = &.{},
152- @"usageMetadata": ?Usage = null,
152+ usageMetadata: ?Usage = null,
153153
154154 const Candidate = struct {
155155 content: ?struct { parts: []const Piece = &.{} } = null,
156- @"finishReason": ?[]const u8 = null,
156+ finishReason: ?[]const u8 = null,
157157 };
158158
159159 const Piece = struct {
160160 text: ?[]const u8 = null,
161161 thought: ?bool = null,
162- @"functionCall": ?Function = null,
163- @"thoughtSignature": ?[]const u8 = null,
162+ functionCall: ?Function = null,
163+ thoughtSignature: ?[]const u8 = null,
164164 const Function = struct { name: ?[]const u8 = null, args: ?std.json.Value = null };
165165 };
166166
167167 const Usage = struct {
168- @"promptTokenCount": api.Count = .{},
169- @"candidatesTokenCount": api.Count = .{},
170- @"thoughtsTokenCount": api.Count = .{},
171- @"totalTokenCount": api.Count = .{},
168+ promptTokenCount: api.Count = .{},
169+ candidatesTokenCount: api.Count = .{},
170+ thoughtsTokenCount: api.Count = .{},
171+ totalTokenCount: api.Count = .{},
172172 };
173173 };
174174
@@ -188,7 +188,7 @@ const State = struct {
188188 pub fn handle(st: *State, chunk: Chunk) !void {
189189 if (chunk.@"error") |err| {
190190 if (err.message) |m| {
191- if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, m);
191+ try api.keepString(&st.stream_error, st.req.pers, m);
192192 return;
193193 }
194194 }
@@ -197,7 +197,7 @@ const State = struct {
197197 const candidate = chunk.candidates[0];
198198 if (candidate.content) |content| {
199199 for (content.parts) |part| {
200- if (part.@"functionCall" != null) {
200+ if (part.functionCall != null) {
201201 try addCall(st, part);
202202 continue;
203203 }
@@ -211,24 +211,24 @@ const State = struct {
211211 }
212212 }
213213 }
214- if (candidate.@"finishReason") |fr| {
214+ if (candidate.finishReason) |fr| {
215215 if (fr.len > 0) st.finish = try st.req.pers.dupe(u8, fr);
216216 }
217217 }
218218
219- if (chunk.@"usageMetadata") |u| {
220- st.usage.input = u.@"promptTokenCount".value;
221- st.usage.output = u.@"candidatesTokenCount".value + u.@"thoughtsTokenCount".value;
222- st.usage.total_tokens = u.@"totalTokenCount".value;
219+ if (chunk.usageMetadata) |u| {
220+ st.usage.input = u.promptTokenCount.value;
221+ st.usage.output = u.candidatesTokenCount.value + u.thoughtsTokenCount.value;
222+ st.usage.total_tokens = u.totalTokenCount.value;
223223 }
224224 }
225225 };
226226
227227 fn addCall(st: *State, part: Chunk.Piece) !void {
228- const fc = part.@"functionCall" orelse return;
228+ const fc = part.functionCall orelse return;
229229 const call = try st.calls.addOne(st.req.pers);
230230 call.* = .{};
231- if (part.@"thoughtSignature") |sig| {
231+ if (part.thoughtSignature) |sig| {
232232 if (sig.len > 0) call.thought_signature = try st.req.pers.dupe(u8, sig);
233233 }
234234 try call.announce(st.req.pers, st.sink, fc.name orelse "");
@@ -243,8 +243,7 @@ fn addCall(st: *State, part: Chunk.Piece) !void {
243243
244244 fn finalize(st: *State) !void {
245245 const a = st.req.pers;
246- if (st.reasoning.items.len > 0) try st.msg.content.append(a, .{ .thinking = .{ .text = st.reasoning.items } });
247- if (st.text.items.len > 0) try st.msg.content.append(a, .{ .text = st.text.items });
246+ try api.appendParts(st.msg, a, st.reasoning.items, null, st.text.items);
248247
249248 const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);
250249
src/api/openai_completions.zig+36−47
@@ -4,7 +4,7 @@ const types = @import("../types.zig");
44
55 const ToolCall = struct {
66 id: []const u8,
7- @"type": []const u8 = "function",
7+ type: []const u8 = "function",
88 function: struct { name: []const u8, arguments: []const u8 },
99 };
1010
@@ -16,14 +16,14 @@ const Assistant = struct {
1616
1717 const Tool = struct {
1818 role: []const u8 = "tool",
19- @"tool_call_id": []const u8,
19+ tool_call_id: []const u8,
2020 content: []const u8,
2121 };
2222
23-const TextPart = struct { @"type": []const u8 = "text", text: []const u8 };
23+const TextPart = struct { type: []const u8 = "text", text: []const u8 };
2424 const UrlImagePart = struct {
25- @"type": []const u8 = "image_url",
26- @"image_url": struct { url: []const u8 },
25+ type: []const u8 = "image_url",
26+ image_url: struct { url: []const u8 },
2727 };
2828
2929 const Part = union(enum) {
@@ -59,16 +59,15 @@ const Message = union(enum) {
5959 const Body = struct {
6060 model: []const u8,
6161 stream: bool = true,
62- @"stream_options": struct { @"include_usage": bool = true } = .{},
63- @"reasoning_effort": ?[]const u8 = null,
62+ stream_options: struct { include_usage: bool = true } = .{},
63+ reasoning_effort: ?[]const u8 = null,
6464 messages: []const Message,
6565 tools: ?api.Tools = null,
6666 };
6767
68-fn toolCall(block: types.ContentBlock, a: std.mem.Allocator) !?ToolCall {
68+fn toolCall(block: types.ContentBlock) ?ToolCall {
6969 if (block != .tool_call) return null;
7070 const c = block.tool_call;
71- _ = a;
7271 return .{ .id = c.id, .function = .{ .name = c.name, .arguments = api.argumentsOrObject(c.arguments) } };
7372 }
7473
@@ -77,7 +76,7 @@ fn imageParts(a: std.mem.Allocator, run: []const types.Message) ![]const Part {
7776 try parts.append(a, .{ .text = .{ .text = "Attached image(s) from tool result:" } });
7877 for (run) |msg| {
7978 for (msg.tool_result.images) |img| {
80- try parts.append(a, .{ .image = .{ .@"image_url" = .{
79+ try parts.append(a, .{ .image = .{ .image_url = .{
8180 .url = try std.fmt.allocPrint(a, "data:{s};base64,{s}", .{ img.mime_type, img.data }),
8281 } } });
8382 }
@@ -98,7 +97,7 @@ fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message {
9897 while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {
9998 const t = req.messages[i].tool_result;
10099 try out.append(a, .{ .tool = .{
101- .@"tool_call_id" = t.tool_call_id,
100+ .tool_call_id = t.tool_call_id,
102101 .content = if (t.text.len > 0 or t.images.len == 0) t.text else "(see attached image)",
103102 } });
104103 if (t.images.len > 0) images = true;
@@ -111,7 +110,7 @@ fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message {
111110 .assistant => |am| {
112111 var calls: std.ArrayList(ToolCall) = .empty;
113112 for (am.content.items) |block| {
114- if (try toolCall(block, a)) |c| try calls.append(a, c);
113+ if (toolCall(block)) |c| try calls.append(a, c);
115114 }
116115 try out.append(a, .{ .assistant = .{
117116 .content = try types.assistantText(a, am),
@@ -130,7 +129,7 @@ fn buildBody(req: api.Request) ![]u8 {
130129 const effort = if (req.effort.len > 0 and !std.mem.eql(u8, req.effort, "off")) req.effort else null;
131130 const body = Body{
132131 .model = req.model.id,
133- .@"reasoning_effort" = effort,
132+ .reasoning_effort = effort,
134133 .messages = try buildMessages(a, req),
135134 .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
136135 .open = "[",
@@ -152,15 +151,15 @@ const Chunk = struct {
152151
153152 const Choice = struct {
154153 delta: ?Delta = null,
155- @"finish_reason": ?[]const u8 = null,
154+ finish_reason: ?[]const u8 = null,
156155 };
157156
158157 const Delta = struct {
159158 content: ?[]const u8 = null,
160- @"reasoning_content": ?[]const u8 = null,
159+ reasoning_content: ?[]const u8 = null,
161160 reasoning: ?[]const u8 = null,
162- @"reasoning_text": ?[]const u8 = null,
163- @"tool_calls": ?[]const ToolCallDelta = null,
161+ reasoning_text: ?[]const u8 = null,
162+ tool_calls: ?[]const ToolCallDelta = null,
164163 };
165164
166165 const ToolCallDelta = struct {
@@ -171,17 +170,17 @@ const Chunk = struct {
171170 };
172171
173172 const Usage = struct {
174- @"prompt_tokens": api.Count = .{},
175- @"completion_tokens": api.Count = .{},
176- @"prompt_tokens_details": ?Detail = null,
177- @"cached_tokens": api.Count = .{},
178- @"completion_tokens_details": ?Reasoning = null,
173+ prompt_tokens: api.Count = .{},
174+ completion_tokens: api.Count = .{},
175+ prompt_tokens_details: ?Detail = null,
176+ cached_tokens: api.Count = .{},
177+ completion_tokens_details: ?Reasoning = null,
179178
180179 const Detail = struct {
181- @"cached_tokens": api.Count = .{},
182- @"cache_write_tokens": api.Count = .{},
180+ cached_tokens: api.Count = .{},
181+ cache_write_tokens: api.Count = .{},
183182 };
184- const Reasoning = struct { @"reasoning_tokens": ?api.Count = null };
183+ const Reasoning = struct { reasoning_tokens: ?api.Count = null };
185184 };
186185 };
187186
@@ -204,16 +203,12 @@ const State = struct {
204203 pub fn handle(st: *State, chunk: Chunk) !void {
205204 if (chunk.@"error") |err| {
206205 if (err.message) |m| {
207- if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, m);
206+ try api.keepString(&st.stream_error, st.req.pers, m);
208207 return;
209208 }
210209 }
211210
212- if (st.msg.response_id == null) {
213- if (chunk.id) |v| {
214- if (v.len > 0) st.msg.response_id = try st.req.pers.dupe(u8, v);
215- }
216- }
211+ try api.keepString(&st.msg.response_id, st.req.pers, chunk.id);
217212 if (st.msg.response_model == null) {
218213 if (chunk.model) |v| {
219214 if (v.len > 0 and !std.mem.eql(u8, v, st.req.model.id)) {
@@ -225,7 +220,7 @@ const State = struct {
225220 if (chunk.choices.len > 0) {
226221 const choice = chunk.choices[0];
227222 if (choice.delta) |delta| try handleDelta(st, delta);
228- if (choice.@"finish_reason") |fr| {
223+ if (choice.finish_reason) |fr| {
229224 if (fr.len > 0) st.finish_reason = try st.req.pers.dupe(u8, fr);
230225 }
231226 }
@@ -253,7 +248,7 @@ fn handleDelta(st: *State, delta: Chunk.Delta) !void {
253248 }
254249 }
255250
256- for (delta.@"tool_calls" orelse &.{}) |tc| {
251+ for (delta.tool_calls orelse &.{}) |tc| {
257252 const call = try api.blockAt(api.Call, &st.calls, st.req.pers, tc.index orelse -1);
258253 if (tc.id) |id| {
259254 if (id.len > 0 and call.id.len == 0) call.id = try st.req.pers.dupe(u8, id);
@@ -267,17 +262,17 @@ fn handleDelta(st: *State, delta: Chunk.Delta) !void {
267262 }
268263
269264 fn usageOf(u: Chunk.Usage) types.Usage {
270- var cache_read = if (u.@"prompt_tokens_details") |d| d.@"cached_tokens".value else 0;
271- if (cache_read == 0) cache_read = u.@"cached_tokens".value;
272- const cache_write = if (u.@"prompt_tokens_details") |d| d.@"cache_write_tokens".value else 0;
265+ var cache_read = if (u.prompt_tokens_details) |d| d.cached_tokens.value else 0;
266+ if (cache_read == 0) cache_read = u.cached_tokens.value;
267+ const cache_write = if (u.prompt_tokens_details) |d| d.cache_write_tokens.value else 0;
273268 var usage = types.Usage{
274- .input = u.@"prompt_tokens".value -| cache_read -| cache_write,
275- .output = u.@"completion_tokens".value,
269+ .input = u.prompt_tokens.value -| cache_read -| cache_write,
270+ .output = u.completion_tokens.value,
276271 .cache_read = cache_read,
277272 .cache_write = cache_write,
278273 };
279- if (u.@"completion_tokens_details") |d| {
280- if (d.@"reasoning_tokens") |r| usage.reasoning = r.value;
274+ if (u.completion_tokens_details) |d| {
275+ if (d.reasoning_tokens) |r| usage.reasoning = r.value;
281276 }
282277 return usage;
283278 }
@@ -305,13 +300,7 @@ fn mapStopReason(st: *State, has_calls: bool) void {
305300
306301 fn finalize(st: *State) !void {
307302 const a = st.req.pers;
308- if (st.thinking.items.len > 0) {
309- try st.msg.content.append(a, .{ .thinking = .{
310- .text = st.thinking.items,
311- .signature = st.signature,
312- } });
313- }
314- if (st.text.items.len > 0) try st.msg.content.append(a, .{ .text = st.text.items });
303+ try api.appendParts(st.msg, a, st.thinking.items, st.signature, st.text.items);
315304 const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);
316305 api.finishUsage(st.msg, st.req.model, st.usage);
317306 st.msg.raw_stop_reason = st.finish_reason;
src/api/openai_responses.zig+35−49
@@ -2,25 +2,16 @@ const std = @import("std");
22 const api = @import("../api.zig");
33 const types = @import("../types.zig");
44
5-const InputPart = struct {
6- @"type": []const u8,
7- text: ?[]const u8 = null,
8- role: ?[]const u8 = null,
9- content: ?[]const OutputPart = null,
10-
11- const OutputPart = struct { @"type": []const u8, text: []const u8 };
12-};
13-
145 const InputItem = struct {
15- @"type": []const u8,
6+ type: []const u8,
167 role: ?[]const u8 = null,
178 content: ?[]const ContentPart = null,
18- @"call_id": ?[]const u8 = null,
9+ call_id: ?[]const u8 = null,
1910 name: ?[]const u8 = null,
2011 arguments: ?[]const u8 = null,
2112 output: ?[]const u8 = null,
2213
23- const ContentPart = struct { @"type": []const u8 = "input_text", text: []const u8 };
14+ const ContentPart = struct { type: []const u8 = "input_text", text: []const u8 };
2415 };
2516
2617 const Body = struct {
@@ -28,7 +19,7 @@ const Body = struct {
2819 stream: bool = true,
2920 store: bool = false,
3021 instructions: ?[]const u8 = null,
31- @"max_output_tokens": ?u64 = null,
22+ max_output_tokens: ?u64 = null,
3223 reasoning: ?struct { effort: []const u8 } = null,
3324 input: []const InputItem,
3425 tools: ?api.Tools = null,
@@ -39,25 +30,25 @@ fn buildInput(a: std.mem.Allocator, req: api.Request) ![]const InputItem {
3930 for (req.messages) |msg| {
4031 switch (msg) {
4132 .tool_result => |t| try out.append(a, .{
42- .@"type" = "function_call_output",
43- .@"call_id" = t.tool_call_id,
33+ .type = "function_call_output",
34+ .call_id = t.tool_call_id,
4435 .output = t.text,
4536 }),
4637 .user => |u| {
4738 const content = try a.alloc(InputItem.ContentPart, 1);
4839 content[0] = .{ .text = u.content };
49- try out.append(a, .{ .@"type" = "message", .role = "user", .content = content });
40+ try out.append(a, .{ .type = "message", .role = "user", .content = content });
5041 },
5142 .assistant => |am| {
5243 for (am.content.items) |block| switch (block) {
5344 .text => |t| {
5445 const content = try a.alloc(InputItem.ContentPart, 1);
55- content[0] = .{ .@"type" = "output_text", .text = t };
56- try out.append(a, .{ .@"type" = "message", .role = "assistant", .content = content });
46+ content[0] = .{ .type = "output_text", .text = t };
47+ try out.append(a, .{ .type = "message", .role = "assistant", .content = content });
5748 },
5849 .tool_call => |c| try out.append(a, .{
59- .@"type" = "function_call",
60- .@"call_id" = c.id,
50+ .type = "function_call",
51+ .call_id = c.id,
6152 .name = c.name,
6253 .arguments = api.argumentsOrObject(c.arguments),
6354 }),
@@ -79,7 +70,7 @@ fn buildBody(req: api.Request) ![]u8 {
7970 const body = Body{
8071 .model = req.model.id,
8172 .instructions = if (req.system_prompt.len > 0) req.system_prompt else null,
82- .@"max_output_tokens" = max_tokens,
73+ .max_output_tokens = max_tokens,
8374 .reasoning = if (effort) |e| .{ .effort = e } else null,
8475 .input = try buildInput(a, req),
8576 .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
@@ -94,18 +85,18 @@ fn buildBody(req: api.Request) ![]u8 {
9485 }
9586
9687 const Chunk = struct {
97- @"type": ?[]const u8 = null,
88+ type: ?[]const u8 = null,
9889 message: ?[]const u8 = null,
9990 @"error": ?api.ErrField = null,
100- @"output_index": ?i64 = null,
91+ output_index: ?i64 = null,
10192 delta: ?[]const u8 = null,
10293 arguments: ?[]const u8 = null,
10394 item: ?Item = null,
10495 response: ?Response = null,
10596
10697 const Item = struct {
107- @"type": ?[]const u8 = null,
108- @"call_id": ?[]const u8 = null,
98+ type: ?[]const u8 = null,
99+ call_id: ?[]const u8 = null,
109100 name: ?[]const u8 = null,
110101 arguments: ?[]const u8 = null,
111102 };
@@ -113,21 +104,21 @@ const Chunk = struct {
113104 const Response = struct {
114105 id: ?[]const u8 = null,
115106 status: ?[]const u8 = null,
116- @"incomplete_details": ?struct { reason: ?[]const u8 = null } = null,
107+ incomplete_details: ?struct { reason: ?[]const u8 = null } = null,
117108 usage: ?Usage = null,
118109 @"error": ?api.ErrField = null,
119110 };
120111
121112 const Usage = struct {
122- @"input_tokens": api.Count = .{},
123- @"output_tokens": api.Count = .{},
124- @"total_tokens": api.Count = .{},
125- @"input_tokens_details": ?struct { @"cached_tokens": api.Count = .{} } = null,
113+ input_tokens: api.Count = .{},
114+ output_tokens: api.Count = .{},
115+ total_tokens: api.Count = .{},
116+ input_tokens_details: ?struct { cached_tokens: api.Count = .{} } = null,
126117 };
127118 };
128119
129120 fn streamIndex(chunk: Chunk, fallback: usize) usize {
130- const v = chunk.@"output_index" orelse return fallback;
121+ const v = chunk.output_index orelse return fallback;
131122 return if (v < 0 or v > api.max_stream_index) fallback else @intCast(v);
132123 }
133124
@@ -146,32 +137,28 @@ const State = struct {
146137 failed: bool = false,
147138
148139 pub fn handle(st: *State, chunk: Chunk) !void {
149- const event_type = chunk.@"type" orelse "";
140+ const event_type = chunk.type orelse "";
150141
151142 if (std.mem.eql(u8, event_type, "error")) {
152143 const message = chunk.message orelse
153144 (if (chunk.@"error") |e| e.message orelse "stream: provider error" else "stream: provider error");
154- if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, message);
145+ try api.keepString(&st.stream_error, st.req.pers, message);
155146 return;
156147 }
157148
158149 if (std.mem.eql(u8, event_type, "response.created")) {
159150 if (chunk.response) |resp| {
160- if (st.msg.response_id == null) {
161- if (resp.id) |id| {
162- if (id.len > 0) st.msg.response_id = try st.req.pers.dupe(u8, id);
163- }
164- }
151+ try api.keepString(&st.msg.response_id, st.req.pers, resp.id);
165152 }
166153 return;
167154 }
168155
169156 if (std.mem.eql(u8, event_type, "response.output_item.added")) {
170157 const item = chunk.item orelse return;
171- if (!std.mem.eql(u8, item.@"type" orelse "", "function_call")) return;
158+ if (!std.mem.eql(u8, item.type orelse "", "function_call")) return;
172159 const index = streamIndex(chunk, st.calls.items.len);
173160 const call = try api.blockAt(api.Call, &st.calls, st.req.pers, @intCast(index));
174- if (item.@"call_id") |id| call.id = try st.req.pers.dupe(u8, id);
161+ if (item.call_id) |id| call.id = try st.req.pers.dupe(u8, id);
175162 try call.announce(st.req.pers, st.sink, item.name orelse "");
176163 return;
177164 }
@@ -217,11 +204,11 @@ const State = struct {
217204
218205 if (std.mem.eql(u8, event_type, "response.output_item.done")) {
219206 const item = chunk.item orelse return;
220- if (!std.mem.eql(u8, item.@"type" orelse "", "function_call")) return;
207+ if (!std.mem.eql(u8, item.type orelse "", "function_call")) return;
221208 const index = streamIndex(chunk, st.calls.items.len);
222209 if (index >= st.calls.items.len) return;
223210 const call = &st.calls.items[index];
224- if (item.@"call_id") |id| call.id = try st.req.pers.dupe(u8, id);
211+ if (item.call_id) |id| call.id = try st.req.pers.dupe(u8, id);
225212 if (item.name) |name| call.name = try st.req.pers.dupe(u8, name);
226213 if (!call.started and call.name.len > 0) {
227214 call.started = true;
@@ -240,14 +227,14 @@ const State = struct {
240227 if (resp.status) |s| {
241228 if (s.len > 0) st.status = try st.req.pers.dupe(u8, s);
242229 }
243- if (resp.@"incomplete_details") |details| {
230+ if (resp.incomplete_details) |details| {
244231 if (details.reason) |r| st.incomplete = try st.req.pers.dupe(u8, r);
245232 }
246233 if (resp.usage) |u| {
247- st.usage.input = u.@"input_tokens".value;
248- st.usage.output = u.@"output_tokens".value;
249- st.usage.total_tokens = u.@"total_tokens".value;
250- if (u.@"input_tokens_details") |d| st.usage.cache_read = d.@"cached_tokens".value;
234+ st.usage.input = u.input_tokens.value;
235+ st.usage.output = u.output_tokens.value;
236+ st.usage.total_tokens = u.total_tokens.value;
237+ if (u.input_tokens_details) |d| st.usage.cache_read = d.cached_tokens.value;
251238 }
252239 return;
253240 }
@@ -263,8 +250,7 @@ const State = struct {
263250
264251 fn finalize(st: *State) !void {
265252 const a = st.req.pers;
266- if (st.reasoning.items.len > 0) try st.msg.content.append(a, .{ .thinking = .{ .text = st.reasoning.items } });
267- if (st.text.items.len > 0) try st.msg.content.append(a, .{ .text = st.text.items });
253+ try api.appendParts(st.msg, a, st.reasoning.items, null, st.text.items);
268254
269255 const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);
270256
src/config.zig+18−18
@@ -15,10 +15,10 @@ pub const Api = enum {
1515 pub const CustomProvider = struct {
1616 id: []const u8,
1717 name: ?[]const u8 = null,
18- @"baseUrl": []const u8,
18+ baseUrl: []const u8,
1919 api: Api,
2020 models: []const []const u8,
21- @"envKeys": []const []const u8 = &.{},
21+ envKeys: []const []const u8 = &.{},
2222 headers: ?std.json.ArrayHashMap([]const u8) = null,
2323 };
2424
@@ -39,18 +39,18 @@ pub const Config = struct {
3939 };
4040
4141 const File = struct {
42- @"sessionsDir": ?[]const u8 = null,
43- @"systemPrompt": ?[]const u8 = null,
44- @"discoverAgentFiles": ?bool = null,
45- @"skillsDirs": ?[]const []const u8 = null,
42+ sessionsDir: ?[]const u8 = null,
43+ systemPrompt: ?[]const u8 = null,
44+ discoverAgentFiles: ?bool = null,
45+ skillsDirs: ?[]const []const u8 = null,
4646 tools: ?[]const ToolName = null,
4747 provider: ?[]const u8 = null,
4848 model: ?[]const u8 = null,
49- @"thinkingEffort": ?[]const u8 = null,
50- @"customProviders": ?[]const CustomProvider = null,
49+ thinkingEffort: ?[]const u8 = null,
50+ customProviders: ?[]const CustomProvider = null,
5151 theme: ?[]const u8 = null,
52- @"snapshotIgnoreDirs": ?[]const []const u8 = null,
53- @"snapshotUsesGitignore": ?bool = null,
52+ snapshotIgnoreDirs: ?[]const []const u8 = null,
53+ snapshotUsesGitignore: ?bool = null,
5454 };
5555
5656 fn configDir(a: std.mem.Allocator) []const u8 {
@@ -99,18 +99,18 @@ fn readInto(a: std.mem.Allocator, path: []const u8, required: bool, cfg: *Config
9999 return error.InvalidConfig;
100100 };
101101
102- if (file.@"sessionsDir") |v| cfg.sessions_dir = v;
103- if (file.@"systemPrompt") |v| cfg.system_prompt = v;
102+ if (file.sessionsDir) |v| cfg.sessions_dir = v;
103+ if (file.systemPrompt) |v| cfg.system_prompt = v;
104104 if (file.provider) |v| cfg.provider = v;
105105 if (file.model) |v| cfg.model = v;
106- if (file.@"discoverAgentFiles") |v| cfg.discover_agent_files = v;
107- if (file.@"skillsDirs") |v| cfg.skills_dirs = v;
106+ if (file.discoverAgentFiles) |v| cfg.discover_agent_files = v;
107+ if (file.skillsDirs) |v| cfg.skills_dirs = v;
108108 if (file.tools) |v| cfg.tools = v;
109- if (file.@"customProviders") |v| cfg.custom_providers = v;
110- if (file.@"thinkingEffort") |v| cfg.thinking_effort = v;
109+ if (file.customProviders) |v| cfg.custom_providers = v;
110+ if (file.thinkingEffort) |v| cfg.thinking_effort = v;
111111 if (file.theme) |v| cfg.theme = v;
112- if (file.@"snapshotIgnoreDirs") |v| cfg.snapshot_ignore_dirs = v;
113- if (file.@"snapshotUsesGitignore") |v| cfg.snapshot_uses_gitignore = v;
112+ if (file.snapshotIgnoreDirs) |v| cfg.snapshot_ignore_dirs = v;
113+ if (file.snapshotUsesGitignore) |v| cfg.snapshot_uses_gitignore = v;
114114 }
115115
116116 pub const Update = struct {
src/models.zig+3−3
@@ -128,8 +128,8 @@ pub fn resolveNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id
128128 .name = model_id,
129129 .api = @tagName(p.api),
130130 .provider = provider_id,
131- .base_url = p.@"baseUrl",
132- .api_key = firstEnv(p.@"envKeys"),
131+ .base_url = p.baseUrl,
132+ .api_key = firstEnv(p.envKeys),
133133 .effort = cfg.thinking_effort orelse "",
134134 .supports_images = true,
135135 .context_window = 200_000,
@@ -162,7 +162,7 @@ pub fn providers(a: std.mem.Allocator, cfg: *const config.Config) []ProviderEntr
162162 out.append(a, .{ .id = b.id, .name = b.id, .key_present = firstEnv(b.env_keys) != null }) catch {};
163163 }
164164 for (cfg.custom_providers) |p| {
165- out.append(a, .{ .id = p.id, .name = p.name orelse p.id, .key_present = p.@"envKeys".len == 0 or firstEnv(p.@"envKeys") != null }) catch {};
165+ out.append(a, .{ .id = p.id, .name = p.name orelse p.id, .key_present = p.envKeys.len == 0 or firstEnv(p.envKeys) != null }) catch {};
166166 }
167167 return out.toOwnedSlice(a) catch &.{};
168168 }
src/session.zig+3−40
@@ -5,45 +5,8 @@ const types = @import("types.zig");
55
66 const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
77
8-const Raw = struct {
9- bytes: []const u8 = "",
10- pub fn jsonStringify(self: Raw, jws: anytype) !void {
11- try jws.beginWriteRaw();
12- try jws.writer.writeAll(self.bytes);
13- jws.endWriteRaw();
14- }
15-};
Cut short: this commit's diff is over 1000 lines.
src/tools/bash.zig+1−4
Cut short: this commit's diff is over 1000 lines.
src/tools/common.zig+7−0
Cut short: this commit's diff is over 1000 lines.
src/tools/edit.zig+1−4
Cut short: this commit's diff is over 1000 lines.
src/tools/read.zig+1−4
Cut short: this commit's diff is over 1000 lines.
src/tui/editor.zig+1−5
Cut short: this commit's diff is over 1000 lines.
src/tui/highlight.zig+3−7
Cut short: this commit's diff is over 1000 lines.
src/tui/render.zig+10−2
Cut short: this commit's diff is over 1000 lines.
src/tui/tui.zig+4−15
Cut short: this commit's diff is over 1000 lines.
src/tui/width.zig+0−10
Cut short: this commit's diff is over 1000 lines.
src/types.zig+14−0
Cut short: this commit's diff is over 1000 lines.