xonecas.io
79113
Ma

mini-coding-agent / files

mini-coding-agent / src / api.zig

14.2 KB
1const std = @import("std");
2const message_mod = @import("message.zig");
3const models = @import("models.zig");
4const http = @import("http.zig");
5const time = @import("time.zig");
6const json = @import("json.zig");
7
8pub const Event = union(enum) {
9    retry: struct { attempt: u32, total: u32, delay_ms: u64, reason: []const u8 },
10    text: []const u8,
11    reasoning: []const u8,
12    tool_start: []const u8,
13    tool_args: usize,
14    tool_call: message_mod.ToolCall,
15};
16
17pub const Sink = struct {
18    ctx: *anyopaque,
19    on_event: *const fn (ctx: *anyopaque, event: Event) void,
20
21    pub fn emit(self: Sink, event: Event) void {
22        self.on_event(self.ctx, event);
23    }
24};
25
26pub const ErrField = struct {
27    message: ?[]const u8 = null,
28
29    const Body = struct { message: ?[]const u8 = null };
30
31    pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !@This() {
32        if (try source.peekNextTokenType() != .object_begin) {
33            const token = try source.nextAlloc(allocator, options.allocate.?);
34            return .{ .message = switch (token) {
35                .string, .allocated_string => |v| v,
36                else => null,
37            } };
38        }
39        const body = try std.json.innerParse(Body, allocator, source, options);
40        return .{ .message = if (body.message) |m| if (m.len > 0) m else null else null };
41    }
42};
43
44pub const Count = struct {
45    value: u64 = 0,
46
47    pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !@This() {
48        _ = options;
49        const token = try source.nextAlloc(allocator, .alloc_if_needed);
50        return switch (token) {
51            .number, .allocated_number => |n| .{ .value = @intFromFloat(@max(std.fmt.parseFloat(f64, n) catch 0, 0)) },
52            .null => .{},
53            else => error.UnexpectedToken,
54        };
55    }
56};
57
58pub const Request = struct {
59    pers: std.mem.Allocator,
60    scratch: std.mem.Allocator,
61    model: *const models.Model,
62    system_prompt: []const u8,
63    tools_json: []const u8,
64    messages: []const message_mod.Message,
65    effort: []const u8,
66    session_id: ?[]const u8,
67    cancel: *const std.atomic.Value(bool),
68    retry: http.Retry = .{},
69};
70
71pub const max_stream_index = 10_000;
72
73pub const Wire = struct {
74    key_header: []const u8,
75    key_prefix: []const u8 = "",
76    version: [2][]const u8 = .{ "", "" },
77    path: []const u8 = "",
78    build_url: ?*const fn (std.mem.Allocator, Request) anyerror![]u8 = null,
79    build_body: *const fn (Request) anyerror![]u8,
80};
81
82pub fn run(
83    comptime State: type,
84    comptime Chunk: type,
85    comptime wire: Wire,
86    req: Request,
87    sink: Sink,
88    finalize: *const fn (st: *State) std.mem.Allocator.Error!void,
89) std.mem.Allocator.Error!message_mod.AssistantMessage {
90    var msg = newAssistant(req);
91
92    var arena = std.heap.ArenaAllocator.init(req.scratch);
93    defer arena.deinit();
94
95    const url = if (wire.build_url) |build|
96        build(req.scratch, req) catch return error.OutOfMemory
97    else
98        buildUrl(req.scratch, req.model.base_url, wire.path) catch return error.OutOfMemory;
99    const hdrs = try wireHeaders(req.scratch, req, wire);
100    const body = wire.build_body(req) catch return error.OutOfMemory;
101
102    var st = State{ .req = req, .sink = sink, .arena = &arena, .msg = &msg };
103
104    const complete = try post(req, url, hdrs, body, .{ .ctx = &st, .onEvent = onData(State, Chunk) }, sink, &msg);
105    st.failed = !complete;
106    const reason = msg.stop_reason;
107    const message = msg.error_message;
108    try finalize(&st);
109    if (!complete) {
110        msg.stop_reason = reason;
111        msg.error_message = message;
112    }
113
114    if (st.stream_error) |text| {
115        msg.stop_reason = .err;
116        msg.error_message = try req.pers.dupe(u8, text);
117    }
118    return msg;
119}
120
121fn onData(comptime State: type, comptime Chunk: type) *const fn (ctx: *anyopaque, data: []const u8) void {
122    return struct {
123        fn hook(ctx: *anyopaque, data: []const u8) void {
124            const st: *State = @ptrCast(@alignCast(ctx));
125            defer _ = st.arena.reset(.retain_capacity);
126            if (std.mem.eql(u8, std.mem.trim(u8, data, " \r\n"), "[DONE]")) return;
127            const chunk = std.json.parseFromSliceLeaky(Chunk, st.arena.allocator(), data, .{ .ignore_unknown_fields = true }) catch |e| {
128                if (e != error.OutOfMemory and st.stream_error == null) st.stream_error = "stream: invalid JSON chunk";
129                return;
130            };
131            State.handle(st, chunk) catch {
132                if (st.stream_error == null) st.stream_error = "stream: out of memory";
133            };
134        }
135    }.hook;
136}
137
138pub fn blockAt(comptime T: type, list: *std.ArrayList(T), a: std.mem.Allocator, index: i64) !*T {
139    const idx: usize = if (index >= 0 and index <= max_stream_index)
140        @intCast(index)
141    else
142        list.items.len;
143    while (list.items.len <= idx) try list.append(a, .{});
144    return &list.items[idx];
145}
146
147const StreamFn = *const fn (Request, Sink) std.mem.Allocator.Error!message_mod.AssistantMessage;
148
149const routes = [_]struct { api: []const u8, stream: StreamFn }{
150    .{ .api = "openai-completions", .stream = @import("api/openai_completions.zig").stream },
151    .{ .api = "openai-responses", .stream = @import("api/openai_responses.zig").stream },
152    .{ .api = "anthropic-messages", .stream = @import("api/anthropic_messages.zig").stream },
153    .{ .api = "google-generative-ai", .stream = @import("api/google_generative_ai.zig").stream },
154};
155
156pub fn stream(req: Request, sink: Sink) std.mem.Allocator.Error!message_mod.AssistantMessage {
157    for (routes) |route| {
158        if (std.mem.eql(u8, req.model.api, route.api)) return route.stream(req, sink);
159    }
160    var msg = newAssistant(req);
161    msg.stop_reason = .err;
162    msg.error_message = "provider: unsupported api";
163    return msg;
164}
165
166fn newAssistant(req: Request) message_mod.AssistantMessage {
167    return .{
168        .content = .empty,
169        .api = req.model.api,
170        .provider = req.model.provider,
171        .model = req.model.id,
172        .timestamp = time.nowMs(),
173    };
174}
175
176fn wireHeaders(a: std.mem.Allocator, req: Request, wire: Wire) ![]http.Header {
177    var list: std.ArrayList(http.Header) = .empty;
178    errdefer list.deinit(a);
179    try list.append(a, .{ .name = "Content-Type", .value = "application/json" });
180    try list.append(a, .{ .name = "Accept", .value = "text/event-stream" });
181    if (wire.version[0].len > 0) {
182        try list.append(a, .{ .name = wire.version[0], .value = wire.version[1] });
183    }
184    if (wire.key_header.len > 0) {
185        if (req.model.api_key) |key| {
186            if (key.len > 0) {
187                try list.append(a, .{
188                    .name = wire.key_header,
189                    .value = try std.fmt.allocPrint(a, "{s}{s}", .{ wire.key_prefix, key }),
190                });
191            }
192        }
193    }
194    for (req.model.headers) |kv| try list.append(a, .{ .name = kv[0], .value = kv[1] });
195    if (req.session_id) |sid| {
196        if (req.model.session_header) |name| {
197            if (sid.len > 0) try list.append(a, .{ .name = name, .value = sid });
198        }
199    }
200    return list.toOwnedSlice(a);
201}
202
203const RetryCtx = struct { sink: Sink };
204
205fn onRetry(ctx: *anyopaque, attempt: usize, total: usize, delay_ms: u64, reason: []const u8) void {
206    const rc: *RetryCtx = @ptrCast(@alignCast(ctx));
207    rc.sink.emit(.{ .retry = .{
208        .attempt = @intCast(attempt),
209        .total = @intCast(total),
210        .delay_ms = delay_ms,
211        .reason = reason,
212    } });
213}
214
215fn post(req: Request, url: []const u8, hdrs: []const http.Header, body: []const u8, handler: http.SseHandler, sink: Sink, msg: *message_mod.AssistantMessage) std.mem.Allocator.Error!bool {
216    var err_body: ?[]const u8 = null;
217    var notice_ctx = RetryCtx{ .sink = sink };
218    http.postSse(req.scratch, url, hdrs, body, handler, req.cancel, &err_body, req.retry, .{ .ctx = &notice_ctx, .on_retry = onRetry }) catch |e| {
219        if (e == error.OutOfMemory) return error.OutOfMemory;
220        if (e == error.Aborted or req.cancel.load(.acquire)) {
221            msg.stop_reason = .aborted;
222        } else {
223            msg.stop_reason = .err;
224            const message = switch (e) {
225                error.HttpStatus => err_body orelse "provider returned an error status",
226                error.ReadFailed => "stream read failed",
227                error.IdleTimeout => "provider went silent",
228                else => "request failed",
229            };
230            msg.error_message = try req.pers.dupe(u8, message);
231        }
232        return false;
233    };
234    if (req.cancel.load(.acquire)) {
235        msg.stop_reason = .aborted;
236        return false;
237    }
238    return true;
239}
240
241pub const Call = struct {
242    id: []const u8 = "",
243    name: []const u8 = "",
244    args: std.ArrayList(u8) = .empty,
245    started: bool = false,
246    thought_signature: ?[]const u8 = null,
247
248    pub fn announce(self: *Call, a: std.mem.Allocator, sink: Sink, name: []const u8) !void {
249        if (self.name.len == 0) self.name = try a.dupe(u8, name);
250        if (!self.started and self.name.len > 0) {
251            self.started = true;
252            sink.emit(.{ .tool_start = self.name });
253        }
254    }
255
256    pub fn grow(self: *Call, a: std.mem.Allocator, sink: Sink, bytes: []const u8) !void {
257        try self.args.appendSlice(a, bytes);
258        sink.emit(.{ .tool_args = self.args.items.len });
259    }
260};
261
262pub fn appendCalls(msg: *message_mod.AssistantMessage, sink: Sink, a: std.mem.Allocator, calls: []const Call, failed: bool) !bool {
263    if (failed) return false;
264    var any = false;
265    for (calls) |call| {
266        if (call.name.len == 0) continue;
267        any = true;
268        const arguments = argumentsOrObject(call.args.items);
269        try msg.content.append(a, .{ .tool_call = .{
270            .id = call.id,
271            .name = call.name,
272            .arguments = arguments,
273            .thought_signature = call.thought_signature,
274        } });
275        sink.emit(.{ .tool_call = .{
276            .id = call.id,
277            .name = call.name,
278            .arguments = arguments,
279        } });
280    }
281    return any;
282}
283
284pub fn effortOn(req: Request) ?[]const u8 {
285    if (req.effort.len == 0 or std.mem.eql(u8, req.effort, "off")) return null;
286    return req.effort;
287}
288
289const stop_reasons = [_]struct { wire: []const u8, reason: message_mod.StopReason, message: ?[]const u8 = null }{
290    .{ .wire = "length", .reason = .length },
291    .{ .wire = "max_tokens", .reason = .length },
292    .{ .wire = "MAX_TOKENS", .reason = .length },
293    .{ .wire = "tool_use", .reason = .tool_use },
294    .{ .wire = "tool_calls", .reason = .tool_use },
295    .{ .wire = "function_call", .reason = .tool_use },
296    .{ .wire = "content_filter", .reason = .err, .message = "Provider finish_reason: content_filter" },
297    .{ .wire = "refusal", .reason = .err, .message = "Provider stop_reason: refusal" },
298    .{ .wire = "sensitive", .reason = .err, .message = "Provider stop_reason: sensitive" },
299};
300
301pub fn stopReason(msg: *message_mod.AssistantMessage, raw: ?[]const u8) bool {
302    const wire = raw orelse return false;
303    for (stop_reasons) |entry| {
304        if (!std.mem.eql(u8, entry.wire, wire)) continue;
305        msg.stop_reason = entry.reason;
306        msg.error_message = entry.message;
307        return true;
308    }
309    return false;
310}
311
312pub fn argumentsOrObject(args: []const u8) []const u8 {
313    return if (args.len > 0) args else "{}";
314}
315
316pub fn keepString(slot: *?[]const u8, a: std.mem.Allocator, value: ?[]const u8) !void {
317    if (slot.* != null) return;
318    const v = value orelse return;
319    if (v.len == 0) return;
320    slot.* = try a.dupe(u8, v);
321}
322
323pub fn appendParts(msg: *message_mod.AssistantMessage, a: std.mem.Allocator, thinking: []const u8, signature: ?[]const u8, text: []const u8) !void {
324    if (thinking.len > 0) try msg.content.append(a, .{ .thinking = .{ .text = thinking, .signature = signature } });
325    if (text.len > 0) try msg.content.append(a, .{ .text = text });
326}
327
328fn cost(tokens: u64, rate: f64) f64 {
329    return @as(f64, @floatFromInt(tokens)) * rate / 1_000_000.0;
330}
331
332pub fn finishUsage(msg: *message_mod.AssistantMessage, model: *const models.Model, usage: message_mod.Usage) void {
333    var u = usage;
334    if (u.total_tokens == 0) u.total_tokens = u.input + u.output + u.cache_read + u.cache_write;
335    u.cost_input = cost(u.input, model.cost_input);
336    u.cost_output = cost(u.output, model.cost_output);
337    u.cost_cache_read = cost(u.cache_read, model.cost_cache_read);
338    u.cost_total = u.cost_input + u.cost_output + u.cost_cache_read;
339    msg.usage = u;
340}
341
342pub const ToolsForm = struct {
343    open: []const u8,
344    entry: []const u8,
345    params_key: []const u8,
346    entry_close: []const u8 = "",
347    close: []const u8,
348};
349
350pub const Tools = struct {
351    a: std.mem.Allocator,
352    json: []const u8,
353    form: ToolsForm,
354
355    pub fn jsonStringify(self: Tools, jws: anytype) !void {
356        try jws.beginWriteRaw();
357        try writeTools(jws.writer, self.a, self.json, self.form);
358        jws.endWriteRaw();
359    }
360};
361
362pub fn writeTools(w: *std.Io.Writer, a: std.mem.Allocator, tools_json: []const u8, form: ToolsForm) !void {
363    if (tools_json.len == 0) return;
364    const root = std.json.parseFromSliceLeaky(std.json.Value, a, tools_json, .{}) catch return;
365    if (root != .array) return;
366    var first = true;
367    for (root.array.items) |tool| {
368        if (tool != .object) continue;
369        if (first) {
370            try w.writeAll(form.open);
371            first = false;
372        } else {
373            try w.writeByte(',');
374        }
375        try w.writeAll(form.entry);
376        try writeToolBody(w, tool.object, form.params_key);
377        try w.writeAll(form.entry_close);
378    }
379    if (first) return;
380    try w.writeAll(form.close);
381}
382
383fn writeToolBody(w: *std.Io.Writer, obj: std.json.ObjectMap, params_key: []const u8) !void {
384    try w.writeAll("\"name\":");
385    try std.json.Stringify.encodeJsonString(json.stringField(obj, "name") orelse "", .{}, w);
386    try w.writeAll(",\"description\":");
387    try std.json.Stringify.encodeJsonString(json.stringField(obj, "description") orelse "", .{}, w);
388    try w.writeAll(",\"");
389    try w.writeAll(params_key);
390    try w.writeAll("\":");
391    if (obj.get("parameters")) |params| {
392        try std.json.Stringify.value(params, .{}, w);
393    } else {
394        try w.writeAll("{}");
395    }
396}
397
398fn buildUrl(a: std.mem.Allocator, base: []const u8, path: []const u8) ![]u8 {
399    return std.fmt.allocPrint(a, "{s}{s}", .{ std.mem.trimEnd(u8, base, "/"), path });
400}