79113
Ma
mini-coding-agent / files
mini-coding-agent / src / api / openai_completions.zig
10.1 KB
1const std = @import("std"); 2const api = @import("../api.zig"); 3const message_mod = @import("../message.zig"); 4 5const ToolCall = struct { 6 id: []const u8, 7 type: []const u8 = "function", 8 function: struct { name: []const u8, arguments: []const u8 }, 9}; 10 11const Assistant = struct { 12 role: []const u8 = "assistant", 13 content: []const u8, 14 reasoning_content: ?[]const u8 = null, 15 tool_calls: ?[]const ToolCall = null, 16}; 17 18const Tool = struct { 19 role: []const u8 = "tool", 20 tool_call_id: []const u8, 21 content: []const u8, 22}; 23 24const TextPart = struct { type: []const u8 = "text", text: []const u8 }; 25const UrlImagePart = struct { 26 type: []const u8 = "image_url", 27 image_url: struct { url: []const u8 }, 28}; 29 30const Part = union(enum) { 31 text: TextPart, 32 image: UrlImagePart, 33 34 pub fn jsonStringify(self: Part, jws: anytype) !void { 35 switch (self) { 36 inline else => |v| try jws.write(v), 37 } 38 } 39}; 40 41const Images = struct { 42 role: []const u8 = "user", 43 content: []const Part, 44}; 45 46const Message = union(enum) { 47 system: struct { role: []const u8 = "system", content: []const u8 }, 48 user: struct { role: []const u8 = "user", content: []const u8 }, 49 assistant: Assistant, 50 tool: Tool, 51 images: Images, 52 53 pub fn jsonStringify(self: Message, jws: anytype) !void { 54 switch (self) { 55 inline else => |v| try jws.write(v), 56 } 57 } 58}; 59 60const Body = struct { 61 model: []const u8, 62 stream: bool = true, 63 stream_options: struct { include_usage: bool = true } = .{}, 64 reasoning_effort: ?[]const u8 = null, 65 messages: []const Message, 66 tools: ?api.Tools = null, 67}; 68 69fn toolCall(block: message_mod.ContentBlock) ?ToolCall { 70 if (block != .tool_call) return null; 71 const c = block.tool_call; 72 return .{ .id = c.id, .function = .{ .name = c.name, .arguments = api.argumentsOrObject(c.arguments) } }; 73} 74 75fn imageParts(a: std.mem.Allocator, run: []const message_mod.Message) ![]const Part { 76 var parts: std.ArrayList(Part) = .empty; 77 try parts.append(a, .{ .text = .{ .text = "Attached image(s) from tool result:" } }); 78 for (run) |msg| { 79 for (msg.tool_result.images) |img| { 80 try parts.append(a, .{ .image = .{ .image_url = .{ 81 .url = try std.fmt.allocPrint(a, "data:{s};base64,{s}", .{ img.mime_type, img.data }), 82 } } }); 83 } 84 } 85 return parts.items; 86} 87 88fn buildMessages(a: std.mem.Allocator, req: api.Request) ![]const Message { 89 var out: std.ArrayList(Message) = .empty; 90 const reasoning_on = api.effortOn(req) != null; 91 if (req.system_prompt.len > 0) { 92 try out.append(a, .{ .system = .{ .content = req.system_prompt } }); 93 } 94 var i: usize = 0; 95 while (i < req.messages.len) { 96 if (req.messages[i] == .tool_result) { 97 const start = i; 98 var images = false; 99 while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) { 100 const t = req.messages[i].tool_result; 101 try out.append(a, .{ .tool = .{ 102 .tool_call_id = t.tool_call_id, 103 .content = if (t.text.len > 0 or t.images.len == 0) t.text else "(see attached image)", 104 } }); 105 if (t.images.len > 0) images = true; 106 } 107 if (images) try out.append(a, .{ .images = .{ .content = try imageParts(a, req.messages[start..i]) } }); 108 continue; 109 } 110 switch (req.messages[i]) { 111 .user => |u| try out.append(a, .{ .user = .{ .content = u.content } }), 112 .assistant => |am| { 113 var calls: std.ArrayList(ToolCall) = .empty; 114 var thinking: std.ArrayList(u8) = .empty; 115 for (am.content.items) |block| { 116 if (toolCall(block)) |c| try calls.append(a, c); 117 if (block == .thinking) try thinking.appendSlice(a, block.thinking.text); 118 } 119 try out.append(a, .{ .assistant = .{ 120 .content = try am.text(a), 121 .reasoning_content = if (thinking.items.len > 0 or reasoning_on) thinking.items else null, 122 .tool_calls = if (calls.items.len > 0) calls.items else null, 123 } }); 124 }, 125 .tool_result => unreachable, 126 } 127 i += 1; 128 } 129 return out.items; 130} 131 132fn buildBody(req: api.Request) ![]u8 { 133 const a = req.scratch; 134 const body = Body{ 135 .model = req.model.id, 136 .reasoning_effort = api.effortOn(req), 137 .messages = try buildMessages(a, req), 138 .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{ 139 .open = "[", 140 .entry = "{\"type\":\"function\",\"function\":{", 141 .params_key = "parameters", 142 .entry_close = "}}", 143 .close = "]", 144 } } else null, 145 }; 146 return std.json.Stringify.valueAlloc(a, body, .{ .emit_null_optional_fields = false }); 147} 148 149const Chunk = struct { 150 id: ?[]const u8 = null, 151 model: ?[]const u8 = null, 152 @"error": ?api.ErrField = null, 153 usage: ?Usage = null, 154 choices: []const Choice = &.{}, 155 156 const Choice = struct { 157 delta: ?Delta = null, 158 finish_reason: ?[]const u8 = null, 159 }; 160 161 const Delta = struct { 162 content: ?[]const u8 = null, 163 reasoning_content: ?[]const u8 = null, 164 reasoning: ?[]const u8 = null, 165 reasoning_text: ?[]const u8 = null, 166 tool_calls: ?[]const ToolCallDelta = null, 167 }; 168 169 const ToolCallDelta = struct { 170 index: ?i64 = null, 171 id: ?[]const u8 = null, 172 function: ?Function = null, 173 const Function = struct { name: ?[]const u8 = null, arguments: ?[]const u8 = null }; 174 }; 175 176 const Usage = struct { 177 prompt_tokens: api.Count = .{}, 178 completion_tokens: api.Count = .{}, 179 prompt_tokens_details: ?Detail = null, 180 cached_tokens: api.Count = .{}, 181 completion_tokens_details: ?Reasoning = null, 182 183 const Detail = struct { 184 cached_tokens: api.Count = .{}, 185 cache_write_tokens: api.Count = .{}, 186 }; 187 const Reasoning = struct { reasoning_tokens: ?api.Count = null }; 188 }; 189}; 190 191const reasoning_fields = [_][]const u8{ "reasoning_content", "reasoning", "reasoning_text" }; 192 193const State = struct { 194 req: api.Request, 195 sink: api.Sink, 196 arena: *std.heap.ArenaAllocator, 197 msg: *message_mod.AssistantMessage, 198 text: std.ArrayList(u8) = .empty, 199 thinking: std.ArrayList(u8) = .empty, 200 signature: ?[]const u8 = null, 201 calls: std.ArrayList(api.Call) = .empty, 202 usage: message_mod.Usage = .{}, 203 finish_reason: ?[]const u8 = null, 204 stream_error: ?[]const u8 = null, 205 failed: bool = false, 206 207 pub fn handle(st: *State, chunk: Chunk) !void { 208 if (chunk.@"error") |err| { 209 if (err.message) |m| { 210 try api.keepString(&st.stream_error, st.req.pers, m); 211 return; 212 } 213 } 214 215 try api.keepString(&st.msg.response_id, st.req.pers, chunk.id); 216 if (st.msg.response_model == null) { 217 if (chunk.model) |v| { 218 if (v.len > 0 and !std.mem.eql(u8, v, st.req.model.id)) { 219 st.msg.response_model = try st.req.pers.dupe(u8, v); 220 } 221 } 222 } 223 224 if (chunk.choices.len > 0) { 225 const choice = chunk.choices[0]; 226 if (choice.delta) |delta| try handleDelta(st, delta); 227 if (choice.finish_reason) |fr| { 228 if (fr.len > 0) st.finish_reason = try st.req.pers.dupe(u8, fr); 229 } 230 } 231 232 if (chunk.usage) |u| st.usage = usageOf(u); 233 } 234}; 235 236fn handleDelta(st: *State, delta: Chunk.Delta) !void { 237 if (delta.content) |s| { 238 if (s.len > 0) { 239 try st.text.appendSlice(st.req.pers, s); 240 st.sink.emit(.{ .text = s }); 241 } 242 } 243 244 inline for (reasoning_fields) |field| { 245 if (@field(delta, field)) |s| { 246 if (s.len > 0) { 247 if (st.signature == null) st.signature = field; 248 try st.thinking.appendSlice(st.req.pers, s); 249 st.sink.emit(.{ .reasoning = s }); 250 break; 251 } 252 } 253 } 254 255 for (delta.tool_calls orelse &.{}) |tc| { 256 const call = try api.blockAt(api.Call, &st.calls, st.req.pers, tc.index orelse -1); 257 if (tc.id) |id| { 258 if (id.len > 0 and call.id.len == 0) call.id = try st.req.pers.dupe(u8, id); 259 } 260 const fn_ = tc.function orelse continue; 261 if (fn_.name) |name| { 262 if (name.len > 0) try call.announce(st.req.pers, st.sink, name); 263 } 264 if (fn_.arguments) |args| try call.grow(st.req.pers, st.sink, args); 265 } 266} 267 268fn usageOf(u: Chunk.Usage) message_mod.Usage { 269 var cache_read = if (u.prompt_tokens_details) |d| d.cached_tokens.value else 0; 270 if (cache_read == 0) cache_read = u.cached_tokens.value; 271 const cache_write = if (u.prompt_tokens_details) |d| d.cache_write_tokens.value else 0; 272 var usage = message_mod.Usage{ 273 .input = u.prompt_tokens.value -| cache_read -| cache_write, 274 .output = u.completion_tokens.value, 275 .cache_read = cache_read, 276 .cache_write = cache_write, 277 }; 278 if (u.completion_tokens_details) |d| { 279 if (d.reasoning_tokens) |r| usage.reasoning = r.value; 280 } 281 return usage; 282} 283 284fn finalize(st: *State) !void { 285 const a = st.req.pers; 286 try api.appendParts(st.msg, a, st.thinking.items, st.signature, st.text.items); 287 const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed); 288 api.finishUsage(st.msg, st.req.model, st.usage); 289 st.msg.raw_stop_reason = st.finish_reason; 290 if (has_calls) { 291 st.msg.stop_reason = .tool_use; 292 } else if (!api.stopReason(st.msg, st.finish_reason)) { 293 st.msg.stop_reason = .stop; 294 } 295} 296 297const wire = api.Wire{ 298 .key_header = "Authorization", 299 .key_prefix = "Bearer ", 300 .path = "/chat/completions", 301 .build_body = buildBody, 302}; 303 304pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!message_mod.AssistantMessage { 305 return api.run(State, Chunk, wire, req, sink, finalize); 306}