xonecas.io
79113
Ma

mini-coding-agent / files

mini-coding-agent / src / api / google_generative_ai.zig

9.2 KB
1const std = @import("std");
2const api = @import("../api.zig");
3const message_mod = @import("../message.zig");
4const json = @import("../json.zig");
5
6const Part = struct {
7    text: ?[]const u8 = null,
8    thought: ?bool = null,
9    inlineData: ?struct { mimeType: []const u8, data: []const u8 } = null,
10    functionCall: ?struct { name: []const u8, args: json.Raw } = null,
11    thoughtSignature: ?[]const u8 = null,
12    functionResponse: ?struct {
13        name: []const u8,
14        response: Response,
15        const Response = struct { output: ?[]const u8 = null, @"error": ?[]const u8 = null };
16    } = null,
17};
18
19const Content = struct {
20    role: []const u8,
21    parts: []const Part,
22};
23
24const SystemInstruction = struct { parts: []const Part };
25
26const GenerationConfig = struct {
27    maxOutputTokens: ?u64 = null,
28    thinkingConfig: ?struct { includeThoughts: bool = true, thinkingBudget: u64 } = null,
29};
30
31const Body = struct {
32    contents: []const Content,
33    systemInstruction: ?SystemInstruction = null,
34    tools: ?api.Tools = null,
35    generationConfig: ?GenerationConfig = null,
36};
37
38fn textPart(text: []const u8) Part {
39    return .{ .text = text };
40}
41
42fn contentParts(a: std.mem.Allocator, msg: message_mod.Message) ![]const Part {
43    switch (msg) {
44        .user => |u| {
45            const parts = try a.alloc(Part, 1);
46            parts[0] = textPart(u.content);
47            return parts;
48        },
49        .assistant => |am| {
50            var parts: std.ArrayList(Part) = .empty;
51            for (am.content.items) |block| switch (block) {
52                .text => |t| try parts.append(a, textPart(t)),
53                .tool_call => |c| try parts.append(a, .{
54                    .functionCall = .{
55                        .name = c.name,
56                        .args = .{ .bytes = api.argumentsOrObject(c.arguments) },
57                    },
58                    .thoughtSignature = c.thought_signature,
59                }),
60                .thinking => |t| try parts.append(a, .{
61                    .text = t.text,
62                    .thought = true,
63                    .thoughtSignature = t.signature,
64                }),
65            };
66            return parts.items;
67        },
68        .tool_result => unreachable,
69    }
70}
71
72fn toolResultContent(a: std.mem.Allocator, run: []const message_mod.Message) !Content {
73    const parts = try a.alloc(Part, run.len);
74    for (run, parts) |msg, *part| {
75        const t = msg.tool_result;
76        part.* = .{ .functionResponse = .{ .name = t.tool_name, .response = if (t.is_error)
77            .{ .@"error" = t.text }
78        else
79            .{ .output = t.text } } };
80    }
81    return .{ .role = "user", .parts = parts };
82}
83
84fn buildContents(a: std.mem.Allocator, req: api.Request) ![]const Content {
85    var out: std.ArrayList(Content) = .empty;
86    var i: usize = 0;
87    while (i < req.messages.len) {
88        if (req.messages[i] == .tool_result) {
89            const start = i;
90            while (i < req.messages.len and req.messages[i] == .tool_result) : (i += 1) {}
91            try out.append(a, try toolResultContent(a, req.messages[start..i]));
92            continue;
93        }
94        const msg = req.messages[i];
95        try out.append(a, .{
96            .role = if (msg == .assistant) "model" else "user",
97            .parts = try contentParts(a, msg),
98        });
99        i += 1;
100    }
101    return out.items;
102}
103
104fn budgetFor(effort: []const u8) ?u64 {
105    const map = .{
106        .{ "off", 0 },
107        .{ "minimal", 512 },
108        .{ "low", 1024 },
109        .{ "medium", 4096 },
110        .{ "high", 8192 },
111        .{ "xhigh", 16384 },
112        .{ "max", 24576 },
113    };
114    inline for (map) |entry| {
115        if (std.mem.eql(u8, effort, entry[0])) return entry[1];
116    }
117    return null;
118}
119
120fn buildBody(req: api.Request) ![]u8 {
121    const a = req.scratch;
122    const budget = budgetFor(req.effort);
123    const config: ?GenerationConfig = if (req.model.max_tokens > 0 or budget != null) .{
124        .maxOutputTokens = if (req.model.max_tokens > 0) req.model.max_tokens else null,
125        .thinkingConfig = if (budget) |b| .{ .includeThoughts = b > 0, .thinkingBudget = b } else null,
126    } else null;
127    const sys = if (req.system_prompt.len > 0) blk: {
128        const parts = try a.alloc(Part, 1);
129        parts[0] = textPart(req.system_prompt);
130        break :blk SystemInstruction{ .parts = parts };
131    } else null;
132    const body = Body{
133        .contents = try buildContents(a, req),
134        .systemInstruction = sys,
135        .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
136            .open = "[{\"functionDeclarations\":[",
137            .entry = "{",
138            .params_key = "parametersJsonSchema",
139            .entry_close = "}",
140            .close = "]}]",
141        } } else null,
142        .generationConfig = config,
143    };
144    return std.json.Stringify.valueAlloc(a, body, .{ .emit_null_optional_fields = false });
145}
146
147fn buildUrl(a: std.mem.Allocator, req: api.Request) ![]u8 {
148    const model = if (std.mem.startsWith(u8, req.model.id, "models/"))
149        req.model.id
150    else
151        try std.fmt.allocPrint(a, "models/{s}", .{req.model.id});
152    return std.fmt.allocPrint(a, "{s}/{s}:streamGenerateContent?alt=sse", .{
153        std.mem.trimEnd(u8, req.model.base_url, "/"),
154        model,
155    });
156}
157
158const Chunk = struct {
159    @"error": ?api.ErrField = null,
160    candidates: []const Candidate = &.{},
161    usageMetadata: ?Usage = null,
162
163    const Candidate = struct {
164        content: ?struct { parts: []const Piece = &.{} } = null,
165        finishReason: ?[]const u8 = null,
166    };
167
168    const Piece = struct {
169        text: ?[]const u8 = null,
170        thought: ?bool = null,
171        functionCall: ?Function = null,
172        thoughtSignature: ?[]const u8 = null,
173        const Function = struct { name: ?[]const u8 = null, args: ?std.json.Value = null };
174    };
175
176    const Usage = struct {
177        promptTokenCount: api.Count = .{},
178        candidatesTokenCount: api.Count = .{},
179        thoughtsTokenCount: api.Count = .{},
180        totalTokenCount: api.Count = .{},
181    };
182};
183
184const State = struct {
185    req: api.Request,
186    sink: api.Sink,
187    arena: *std.heap.ArenaAllocator,
188    msg: *message_mod.AssistantMessage,
189    text: std.ArrayList(u8) = .empty,
190    reasoning: std.ArrayList(u8) = .empty,
191    signature: ?[]const u8 = null,
192    calls: std.ArrayList(api.Call) = .empty,
193    usage: message_mod.Usage = .{},
194    finish: ?[]const u8 = null,
195    stream_error: ?[]const u8 = null,
196    failed: bool = false,
197
198    pub fn handle(st: *State, chunk: Chunk) !void {
199        if (chunk.@"error") |err| {
200            if (err.message) |m| {
201                try api.keepString(&st.stream_error, st.req.pers, m);
202                return;
203            }
204        }
205
206        if (chunk.candidates.len > 0) {
207            const candidate = chunk.candidates[0];
208            if (candidate.content) |content| {
209                for (content.parts) |part| {
210                    if (part.functionCall != null) {
211                        try addCall(st, part);
212                        continue;
213                    }
214                    try api.keepString(&st.signature, st.req.pers, part.thoughtSignature);
215                    const text = part.text orelse continue;
216                    if (part.thought orelse false) {
217                        try st.reasoning.appendSlice(st.req.pers, text);
218                        st.sink.emit(.{ .reasoning = text });
219                    } else {
220                        try st.text.appendSlice(st.req.pers, text);
221                        st.sink.emit(.{ .text = text });
222                    }
223                }
224            }
225            if (candidate.finishReason) |fr| {
226                if (fr.len > 0) st.finish = try st.req.pers.dupe(u8, fr);
227            }
228        }
229
230        if (chunk.usageMetadata) |u| {
231            st.usage.input = u.promptTokenCount.value;
232            st.usage.output = u.candidatesTokenCount.value + u.thoughtsTokenCount.value;
233            st.usage.total_tokens = u.totalTokenCount.value;
234        }
235    }
236};
237
238fn addCall(st: *State, part: Chunk.Piece) !void {
239    const fc = part.functionCall orelse return;
240    const call = try st.calls.addOne(st.req.pers);
241    call.* = .{};
242    if (part.thoughtSignature) |sig| {
243        if (sig.len > 0) call.thought_signature = try st.req.pers.dupe(u8, sig);
244    }
245    try call.announce(st.req.pers, st.sink, fc.name orelse "");
246    if (fc.args) |args| {
247        if (args != .null) {
248            var out: std.Io.Writer.Allocating = .init(st.req.pers);
249            try std.json.Stringify.value(args, .{}, &out.writer);
250            try call.grow(st.req.pers, st.sink, out.written());
251        }
252    }
253}
254
255fn finalize(st: *State) !void {
256    const a = st.req.pers;
257    try api.appendParts(st.msg, a, st.reasoning.items, st.signature, st.text.items);
258
259    const has_calls = try api.appendCalls(st.msg, st.sink, a, st.calls.items, st.failed);
260
261    api.finishUsage(st.msg, st.req.model, st.usage);
262
263    st.msg.raw_stop_reason = st.finish;
264    if (!api.stopReason(st.msg, st.finish)) {
265        st.msg.stop_reason = if (has_calls) .tool_use else .stop;
266    }
267}
268
269const wire = api.Wire{
270    .key_header = "x-goog-api-key",
271    .build_url = buildUrl,
272    .build_body = buildBody,
273};
274
275pub fn stream(req: api.Request, sink: api.Sink) std.mem.Allocator.Error!message_mod.AssistantMessage {
276    return api.run(State, Chunk, wire, req, sink, finalize);
277}