xonecas.io
79113
Ma

mini-coding-agent / commit cbe9269

fix(reasoning): ensure reasoning is handled correctly on all wires

xonecas2026-10-07 cbe9269483c461e13835b2ca2b410087696fddd8 parent 6e334ae

3 files changed

src/api/google_generative_ai.zig+9−2
@@ -4,6 +4,7 @@ const types = @import("../types.zig");
44
55 const Part = struct {
66 text: ?[]const u8 = null,
7+ thought: ?bool = null,
78 inlineData: ?struct { mimeType: []const u8, data: []const u8 } = null,
89 functionCall: ?struct { name: []const u8, args: api.Raw } = null,
910 thoughtSignature: ?[]const u8 = null,
@@ -55,7 +56,11 @@ fn contentParts(a: std.mem.Allocator, msg: types.Message) ![]const Part {
5556 },
5657 .thoughtSignature = c.thought_signature,
5758 }),
58- .thinking => {},
59+ .thinking => |t| try parts.append(a, .{
60+ .text = t.text,
61+ .thought = true,
62+ .thoughtSignature = t.signature,
63+ }),
5964 };
6065 return parts.items;
6166 },
@@ -182,6 +187,7 @@ const State = struct {
182187 msg: *types.AssistantMessage,
183188 text: std.ArrayList(u8) = .empty,
184189 reasoning: std.ArrayList(u8) = .empty,
190+ signature: ?[]const u8 = null,
185191 calls: std.ArrayList(api.Call) = .empty,
186192 usage: types.Usage = .{},
187193 finish: ?[]const u8 = null,
@@ -204,6 +210,7 @@ const State = struct {
204210 try addCall(st, part);
205211 continue;
206212 }
213+ try api.keepString(&st.signature, st.req.pers, part.thoughtSignature);
207214 const text = part.text orelse continue;
208215 if (part.thought orelse false) {
209216 try st.reasoning.appendSlice(st.req.pers, text);
@@ -246,7 +253,7 @@ fn addCall(st: *State, part: Chunk.Piece) !void {
246253
247254 fn finalize(st: *State) !void {
248255 const a = st.req.pers;
249- try api.appendParts(st.msg, a, st.reasoning.items, null, st.text.items);
256+ try api.appendParts(st.msg, a, st.reasoning.items, st.signature, st.text.items);
250257
251258 const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);
252259
src/api/openai_completions.zig+4−0
@@ -11,6 +11,7 @@ const ToolCall = struct {
1111 const Assistant = struct {
1212 role: []const u8 = "assistant",
1313 content: []const u8,
14+ reasoning_content: ?[]const u8 = null,
1415 tool_calls: ?[]const ToolCall = null,
1516 };
1617
@@ -109,11 +110,14 @@ fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message {
109110 .user => |u| try out.append(a, .{ .user = .{ .content = u.content } }),
110111 .assistant => |am| {
111112 var calls: std.ArrayList(ToolCall) = .empty;
113+ var thinking: std.ArrayList(u8) = .empty;
112114 for (am.content.items) |block| {
113115 if (toolCall(block)) |c| try calls.append(a, c);
116+ if (block == .thinking) try thinking.appendSlice(a, block.thinking.text);
114117 }
115118 try out.append(a, .{ .assistant = .{
116119 .content = try types.assistantText(a, am),
120+ .reasoning_content = if (thinking.items.len > 0) thinking.items else null,
117121 .tool_calls = if (calls.items.len > 0) calls.items else null,
118122 } });
119123 },
src/api/openai_responses.zig+26−2
@@ -10,8 +10,11 @@ const InputItem = struct {
1010 name: ?[]const u8 = null,
1111 arguments: ?[]const u8 = null,
1212 output: ?[]const u8 = null,
13+ summary: ?[]const SummaryPart = null,
14+ encrypted_content: ?[]const u8 = null,
1315
1416 const ContentPart = struct { type: []const u8 = "input_text", text: []const u8 };
17+ const SummaryPart = struct { type: []const u8 = "summary_text", text: []const u8 };
1518 };
1619
1720 const Body = struct {
@@ -52,7 +55,16 @@ fn buildInput(a: std.mem.Allocator, req: api.Request) ![]const InputItem {
5255 .name = c.name,
5356 .arguments = api.argumentsOrObject(c.arguments),
5457 }),
55- .thinking => {},
58+ .thinking => |t| {
59+ const enc = t.signature orelse continue;
60+ const summary = try a.alloc(InputItem.SummaryPart, if (t.text.len > 0) 1 else 0);
61+ if (t.text.len > 0) summary[0] = .{ .text = t.text };
62+ try out.append(a, .{
63+ .type = "reasoning",
64+ .encrypted_content = enc,
65+ .summary = summary,
66+ });
67+ },
5668 };
5769 },
5870 }
@@ -99,6 +111,7 @@ const Chunk = struct {
99111 call_id: ?[]const u8 = null,
100112 name: ?[]const u8 = null,
101113 arguments: ?[]const u8 = null,
114+ encrypted_content: ?[]const u8 = null,
102115 };
103116
104117 const Response = struct {
@@ -129,6 +142,7 @@ const State = struct {
129142 msg: *types.AssistantMessage,
130143 text: std.ArrayList(u8) = .empty,
131144 reasoning: std.ArrayList(u8) = .empty,
145+ encrypted: ?[]const u8 = null,
132146 calls: std.ArrayList(api.Call) = .empty,
133147 usage: types.Usage = .{},
134148 status: ?[]const u8 = null,
@@ -204,6 +218,10 @@ const State = struct {
204218
205219 if (std.mem.eql(u8, event_type, "response.output_item.done")) {
206220 const item = chunk.item orelse return;
221+ if (std.mem.eql(u8, item.type orelse "", "reasoning")) {
222+ try api.keepString(&st.encrypted, st.req.pers, item.encrypted_content);
223+ return;
224+ }
207225 if (!std.mem.eql(u8, item.type orelse "", "function_call")) return;
208226 const index = streamIndex(chunk, st.calls.items.len);
209227 if (index >= st.calls.items.len) return;
@@ -250,7 +268,13 @@ const State = struct {
250268
251269 fn finalize(st: *State) !void {
252270 const a = st.req.pers;
253- try api.appendParts(st.msg, a, st.reasoning.items, null, st.text.items);
271+ if (st.reasoning.items.len > 0 or st.encrypted != null) {
272+ try st.msg.content.append(a, .{ .thinking = .{
273+ .text = st.reasoning.items,
274+ .signature = st.encrypted,
275+ } });
276+ }
277+ if (st.text.items.len > 0) try st.msg.content.append(a, .{ .text = st.text.items });
254278
255279 const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);