79113
Ma
mini-coding-agent / files
mini-coding-agent / src / api / openai_responses.zig
11.8 KB
1const std = @import("std"); 2const api = @import("../api.zig"); 3const message_mod = @import("../message.zig"); 4 5const InputItem = struct { 6 type: []const u8, 7 role: ?[]const u8 = null, 8 content: ?[]const ContentPart = null, 9 call_id: ?[]const u8 = null, 10 name: ?[]const u8 = null, 11 arguments: ?[]const u8 = null, 12 output: ?[]const u8 = null, 13 summary: ?[]const SummaryPart = null, 14 encrypted_content: ?[]const u8 = null, 15 16 const ContentPart = struct { type: []const u8 = "input_text", text: []const u8 }; 17 const SummaryPart = struct { type: []const u8 = "summary_text", text: []const u8 }; 18}; 19 20const Body = struct { 21 model: []const u8, 22 stream: bool = true, 23 store: bool = false, 24 instructions: ?[]const u8 = null, 25 max_output_tokens: ?u64 = null, 26 reasoning: ?struct { effort: []const u8, summary: []const u8 = "concise" } = null, 27 input: []const InputItem, 28 tools: ?api.Tools = null, 29}; 30 31fn buildInput(a: std.mem.Allocator, req: api.Request) ![]const InputItem { 32 var out: std.ArrayList(InputItem) = .empty; 33 for (req.messages) |msg| { 34 switch (msg) { 35 .tool_result => |t| try out.append(a, .{ 36 .type = "function_call_output", 37 .call_id = t.tool_call_id, 38 .output = t.text, 39 }), 40 .user => |u| { 41 const content = try a.alloc(InputItem.ContentPart, 1); 42 content[0] = .{ .text = u.content }; 43 try out.append(a, .{ .type = "message", .role = "user", .content = content }); 44 }, 45 .assistant => |am| { 46 for (am.content.items) |block| switch (block) { 47 .text => |t| { 48 const content = try a.alloc(InputItem.ContentPart, 1); 49 content[0] = .{ .type = "output_text", .text = t }; 50 try out.append(a, .{ .type = "message", .role = "assistant", .content = content }); 51 }, 52 .tool_call => |c| try out.append(a, .{ 53 .type = "function_call", 54 .call_id = c.id, 55 .name = c.name, 56 .arguments = api.argumentsOrObject(c.arguments), 57 }), 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 }, 68 }; 69 }, 70 } 71 } 72 return out.items; 73} 74 75fn buildBody(req: api.Request) ![]u8 { 76 const a = req.scratch; 77 const max_tokens: ?u64 = if (req.model.max_tokens > 0) 78 (if (req.model.max_tokens > 16) req.model.max_tokens else 16) 79 else 80 null; 81 const body = Body{ 82 .model = req.model.id, 83 .instructions = if (req.system_prompt.len > 0) req.system_prompt else null, 84 .max_output_tokens = max_tokens, 85 .reasoning = if (api.effortOn(req)) |e| .{ .effort = e } else null, 86 .input = try buildInput(a, req), 87 .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{ 88 .open = "[", 89 .entry = "{\"type\":\"function\",", 90 .params_key = "parameters", 91 .entry_close = "}", 92 .close = "]", 93 } } else null, 94 }; 95 return std.json.Stringify.valueAlloc(a, body, .{ .emit_null_optional_fields = false }); 96} 97 98const Chunk = struct { 99 type: ?[]const u8 = null, 100 message: ?[]const u8 = null, 101 @"error": ?api.ErrField = null, 102 output_index: ?i64 = null, 103 delta: ?[]const u8 = null, 104 arguments: ?[]const u8 = null, 105 item: ?Item = null, 106 response: ?Response = null, 107 108 const Item = struct { 109 type: ?[]const u8 = null, 110 call_id: ?[]const u8 = null, 111 name: ?[]const u8 = null, 112 arguments: ?[]const u8 = null, 113 encrypted_content: ?[]const u8 = null, 114 }; 115 116 const Response = struct { 117 id: ?[]const u8 = null, 118 status: ?[]const u8 = null, 119 incomplete_details: ?struct { reason: ?[]const u8 = null } = null, 120 usage: ?Usage = null, 121 @"error": ?api.ErrField = null, 122 }; 123 124 const Usage = struct { 125 input_tokens: api.Count = .{}, 126 output_tokens: api.Count = .{}, 127 total_tokens: api.Count = .{}, 128 input_tokens_details: ?struct { cached_tokens: api.Count = .{} } = null, 129 }; 130}; 131 132fn streamIndex(chunk: Chunk, fallback: usize) usize { 133 const v = chunk.output_index orelse return fallback; 134 return if (v < 0 or v > api.max_stream_index) fallback else @intCast(v); 135} 136 137const State = struct { 138 req: api.Request, 139 sink: api.Sink, 140 arena: *std.heap.ArenaAllocator, 141 msg: *message_mod.AssistantMessage, 142 text: std.ArrayList(u8) = .empty, 143 reasoning: std.ArrayList(u8) = .empty, 144 encrypted: ?[]const u8 = null, 145 calls: std.ArrayList(api.Call) = .empty, 146 usage: message_mod.Usage = .{}, 147 status: ?[]const u8 = null, 148 incomplete: ?[]const u8 = null, 149 stream_error: ?[]const u8 = null, 150 failed: bool = false, 151 152 pub fn handle(st: *State, chunk: Chunk) !void { 153 const event_type = chunk.type orelse ""; 154 155 if (std.mem.eql(u8, event_type, "error")) { 156 const message = chunk.message orelse 157 (if (chunk.@"error") |e| e.message orelse "stream: provider error" else "stream: provider error"); 158 try api.keepString(&st.stream_error, st.req.pers, message); 159 return; 160 } 161 162 if (std.mem.eql(u8, event_type, "response.created")) { 163 if (chunk.response) |resp| { 164 try api.keepString(&st.msg.response_id, st.req.pers, resp.id); 165 } 166 return; 167 } 168 169 if (std.mem.eql(u8, event_type, "response.output_item.added")) { 170 const item = chunk.item orelse return; 171 if (!std.mem.eql(u8, item.type orelse "", "function_call")) return; 172 const index = streamIndex(chunk, st.calls.items.len); 173 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); 175 try call.announce(st.req.pers, st.sink, item.name orelse ""); 176 return; 177 } 178 179 if (std.mem.eql(u8, event_type, "response.output_text.delta") or 180 std.mem.eql(u8, event_type, "response.refusal.delta")) 181 { 182 if (chunk.delta) |s| { 183 try st.text.appendSlice(st.req.pers, s); 184 st.sink.emit(.{ .text = s }); 185 } 186 return; 187 } 188 189 if (std.mem.eql(u8, event_type, "response.reasoning_summary_text.delta") or 190 std.mem.eql(u8, event_type, "response.reasoning_text.delta")) 191 { 192 if (chunk.delta) |s| { 193 try st.reasoning.appendSlice(st.req.pers, s); 194 st.sink.emit(.{ .reasoning = s }); 195 } 196 return; 197 } 198 199 if (std.mem.eql(u8, event_type, "response.function_call_arguments.delta")) { 200 const index = streamIndex(chunk, st.calls.items.len); 201 if (index < st.calls.items.len) { 202 if (chunk.delta) |s| try st.calls.items[index].grow(st.req.pers, st.sink, s); 203 } 204 return; 205 } 206 207 if (std.mem.eql(u8, event_type, "response.function_call_arguments.done")) { 208 const index = streamIndex(chunk, st.calls.items.len); 209 if (index < st.calls.items.len) { 210 if (chunk.arguments) |s| { 211 st.calls.items[index].args.clearRetainingCapacity(); 212 try st.calls.items[index].grow(st.req.pers, st.sink, s); 213 } 214 } 215 return; 216 } 217 218 if (std.mem.eql(u8, event_type, "response.output_item.done")) { 219 const item = chunk.item orelse return; 220 if (std.mem.eql(u8, item.type orelse "", "reasoning")) { 221 try api.keepString(&st.encrypted, st.req.pers, item.encrypted_content); 222 return; 223 } 224 if (!std.mem.eql(u8, item.type orelse "", "function_call")) return; 225 const index = streamIndex(chunk, st.calls.items.len); 226 if (index >= st.calls.items.len) return; 227 const call = &st.calls.items[index]; 228 if (item.call_id) |id| call.id = try st.req.pers.dupe(u8, id); 229 if (item.name) |name| call.name = try st.req.pers.dupe(u8, name); 230 if (!call.started and call.name.len > 0) { 231 call.started = true; 232 st.sink.emit(.{ .tool_start = call.name }); 233 } 234 if (call.args.items.len == 0) { 235 if (item.arguments) |s| try call.grow(st.req.pers, st.sink, s); 236 } 237 return; 238 } 239 240 if (std.mem.eql(u8, event_type, "response.completed") or 241 std.mem.eql(u8, event_type, "response.incomplete")) 242 { 243 const resp = chunk.response orelse return; 244 if (resp.status) |s| { 245 if (s.len > 0) st.status = try st.req.pers.dupe(u8, s); 246 } 247 if (resp.incomplete_details) |details| { 248 if (details.reason) |r| st.incomplete = try st.req.pers.dupe(u8, r); 249 } 250 if (resp.usage) |u| { 251 st.usage.input = u.input_tokens.value; 252 st.usage.output = u.output_tokens.value; 253 st.usage.total_tokens = u.total_tokens.value; 254 if (u.input_tokens_details) |d| st.usage.cache_read = d.cached_tokens.value; 255 } 256 return; 257 } 258 259 if (std.mem.eql(u8, event_type, "response.failed")) { 260 const resp = chunk.response orelse return; 261 const message = if (resp.@"error") |e| e.message orelse "response failed" else "response failed"; 262 if (st.stream_error == null) st.stream_error = try st.req.pers.dupe(u8, message); 263 return; 264 } 265 } 266}; 267 268fn finalize(st: *State) !void { 269 const a = st.req.pers; 270 if (st.reasoning.items.len > 0 or st.encrypted != null) { 271 try st.msg.content.append(a, .{ .thinking = .{ 272 .text = st.reasoning.items, 273 .signature = st.encrypted, 274 } }); 275 } 276 if (st.text.items.len > 0) try st.msg.content.append(a, .{ .text = st.text.items }); 277 278 const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed); 279 280 var usage = st.usage; 281 usage.input = st.usage.input -| st.usage.cache_read; 282 api.finishUsage(st.msg, st.req.model, usage); 283 284 if (st.status) |status| { 285 if (std.mem.eql(u8, status, "incomplete")) { 286 if (st.incomplete) |reason| { 287 if (std.mem.eql(u8, reason, "max_output_tokens")) { 288 st.msg.stop_reason = .length; 289 } else { 290 st.msg.stop_reason = .err; 291 st.msg.error_message = try a.dupe(u8, reason); 292 } 293 } else { 294 st.msg.stop_reason = .length; 295 } 296 return; 297 } 298 if (std.mem.eql(u8, status, "failed") or std.mem.eql(u8, status, "cancelled")) { 299 st.msg.stop_reason = .err; 300 st.msg.error_message = st.stream_error orelse "response failed"; 301 return; 302 } 303 } 304 st.msg.stop_reason = if (has_calls) .tool_use else .stop; 305} 306 307const wire = api.Wire{ 308 .key_header = "Authorization", 309 .key_prefix = "Bearer ", 310 .path = "/responses", 311 .build_body = buildBody, 312}; 313 314pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!message_mod.AssistantMessage { 315 return api.run(State, Chunk, wire, req, sink, finalize); 316}