xonecas.io
7814
Ta

tiny-c-agent / files

tiny-c-agent / src / api / google_generative_ai.c

14.0 KB
1// Google Generative AI (Gemini) streaming adapter.
2
3#include "api.h"
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include <yyjson.h>
10
11#include "http.h"
12#include "util.h"
13
14// ---- request --------------------------------------------------------------
15
16static void add_text_part(yyjson_mut_doc *doc, yyjson_mut_val *parts, const char *text) {
17    yyjson_mut_val *p = yyjson_mut_obj(doc);
18    yyjson_mut_arr_add_val(parts, p);
19    yyjson_mut_obj_add_strcpy(doc, p, "text", text ? text : "");
20}
21
22static void add_function_call(yyjson_mut_doc *doc, yyjson_mut_val *parts, const tiny_block *b) {
23    yyjson_mut_val *p = yyjson_mut_obj(doc);
24    yyjson_mut_arr_add_val(parts, p);
25    yyjson_mut_val *fc = yyjson_mut_obj(doc);
26    yyjson_mut_obj_add_val(doc, p, "functionCall", fc);
27    yyjson_mut_obj_add_strcpy(doc, fc, "name", b->tool_name ? b->tool_name : "");
28    yyjson_doc *adoc = b->arguments ? yyjson_read(b->arguments, strlen(b->arguments), 0) : NULL;
29    if (adoc && yyjson_is_obj(yyjson_doc_get_root(adoc))) {
30        yyjson_mut_obj_add_val(doc, fc, "args", yyjson_val_mut_copy(doc, yyjson_doc_get_root(adoc)));
31    } else {
32        yyjson_mut_obj_add_val(doc, fc, "args", yyjson_mut_obj(doc));
33    }
34    if (adoc) yyjson_doc_free(adoc);
35}
36
37static void add_message(yyjson_mut_doc *doc, yyjson_mut_val *contents, const tiny_message *m) {
38    yyjson_mut_val *o = yyjson_mut_obj(doc);
39    yyjson_mut_arr_add_val(contents, o);
40    yyjson_mut_obj_add_strcpy(doc, o, "role", m->role == TINY_ROLE_ASSISTANT ? "model" : "user");
41    yyjson_mut_val *parts = yyjson_mut_arr(doc);
42    yyjson_mut_obj_add_val(doc, o, "parts", parts);
43    for (size_t i = 0; i < m->blocks_len; i++) {
44        const tiny_block *b = &m->blocks[i];
45        if (b->type == TINY_BLOCK_TEXT) {
46            add_text_part(doc, parts, b->text);
47        } else if (b->type == TINY_BLOCK_IMAGE) {
48            yyjson_mut_val *p = yyjson_mut_obj(doc);
49            yyjson_mut_arr_add_val(parts, p);
50            yyjson_mut_val *inl = yyjson_mut_obj(doc);
51            yyjson_mut_obj_add_val(doc, p, "inlineData", inl);
52            yyjson_mut_obj_add_strcpy(doc, inl, "mimeType", b->media_type ? b->media_type : "");
53            yyjson_mut_obj_add_strcpy(doc, inl, "data", b->data ? b->data : "");
54        } else if (b->type == TINY_BLOCK_TOOL_CALL) {
55            add_function_call(doc, parts, b);
56        }
57    }
58}
59
60// A run of tool results becomes one user turn of functionResponse parts.
61static const char *tool_name_for(const tiny_context *ctx, const char *id) {
62    for (size_t i = 0; i < ctx->messages_len; i++) {
63        const tiny_message *m = &ctx->messages[i];
64        for (size_t k = 0; k < m->blocks_len; k++) {
65            const tiny_block *b = &m->blocks[k];
66            if (b->type == TINY_BLOCK_TOOL_CALL && b->tool_call_id && id &&
67                strcmp(b->tool_call_id, id) == 0)
68                return b->tool_name ? b->tool_name : "";
69        }
70    }
71    return "";
72}
73
74static void add_tool_results(yyjson_mut_doc *doc, yyjson_mut_val *contents,
75                             const tiny_context *ctx, size_t from, size_t to) {
76    yyjson_mut_val *o = yyjson_mut_obj(doc);
77    yyjson_mut_arr_add_val(contents, o);
78    yyjson_mut_obj_add_strcpy(doc, o, "role", "user");
79    yyjson_mut_val *parts = yyjson_mut_arr(doc);
80    yyjson_mut_obj_add_val(doc, o, "parts", parts);
81    for (size_t i = from; i < to; i++) {
82        const tiny_message *m = &ctx->messages[i];
83        char *t = tiny_message_text(m);
84        yyjson_mut_val *p = yyjson_mut_obj(doc);
85        yyjson_mut_arr_add_val(parts, p);
86        yyjson_mut_val *fr = yyjson_mut_obj(doc);
87        yyjson_mut_obj_add_val(doc, p, "functionResponse", fr);
88        yyjson_mut_obj_add_strcpy(doc, fr, "name", tool_name_for(ctx, m->tool_call_id));
89        yyjson_mut_val *resp = yyjson_mut_obj(doc);
90        yyjson_mut_obj_add_val(doc, fr, "response", resp);
91        yyjson_mut_obj_add_strcpy(doc, resp, m->is_error ? "error" : "output", t);
92        free(t);
93    }
94}
95
96static void add_tools(yyjson_mut_doc *doc, yyjson_mut_val *root, const tiny_context *ctx) {
97    if (ctx->tools_len == 0) return;
98    yyjson_mut_val *tools = yyjson_mut_arr(doc);
99    yyjson_mut_obj_add_val(doc, root, "tools", tools);
100    yyjson_mut_val *group = yyjson_mut_obj(doc);
101    yyjson_mut_arr_add_val(tools, group);
102    yyjson_mut_val *decls = yyjson_mut_arr(doc);
103    yyjson_mut_obj_add_val(doc, group, "functionDeclarations", decls);
104    for (size_t i = 0; i < ctx->tools_len; i++) {
105        yyjson_mut_val *d = yyjson_mut_obj(doc);
106        yyjson_mut_arr_add_val(decls, d);
107        tiny_json_tool(doc, d, &ctx->tools[i], "parametersJsonSchema");
108    }
109}
110
111static long budget_for(const char *effort) {
112    if (!effort) return -1;
113    if (strcmp(effort, "off") == 0) return 0;
114    if (strcmp(effort, "minimal") == 0) return 512;
115    if (strcmp(effort, "low") == 0) return 1024;
116    if (strcmp(effort, "medium") == 0) return 4096;
117    if (strcmp(effort, "high") == 0) return 8192;
118    if (strcmp(effort, "xhigh") == 0) return 16384;
119    if (strcmp(effort, "max") == 0) return 24576;
120    return -1;
121}
122
123static char *build_request(const tiny_context *ctx, const tiny_stream_opts *opts) {
124    yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
125    yyjson_mut_val *root = yyjson_mut_obj(doc);
126    yyjson_mut_doc_set_root(doc, root);
127
128    yyjson_mut_val *contents = yyjson_mut_arr(doc);
129    yyjson_mut_obj_add_val(doc, root, "contents", contents);
130    for (size_t i = 0; i < ctx->messages_len;) {
131        if (ctx->messages[i].role == TINY_ROLE_TOOL) {
132            size_t j = i;
133            for (; j < ctx->messages_len && ctx->messages[j].role == TINY_ROLE_TOOL; j++) {}
134            add_tool_results(doc, contents, ctx, i, j);
135            i = j;
136        } else {
137            add_message(doc, contents, &ctx->messages[i]);
138            i++;
139        }
140    }
141
142    if (ctx->system_prompt && ctx->system_prompt[0]) {
143        yyjson_mut_val *si = yyjson_mut_obj(doc);
144        yyjson_mut_obj_add_val(doc, root, "systemInstruction", si);
145        yyjson_mut_val *parts = yyjson_mut_arr(doc);
146        yyjson_mut_obj_add_val(doc, si, "parts", parts);
147        add_text_part(doc, parts, ctx->system_prompt);
148    }
149
150    add_tools(doc, root, ctx);
151
152    yyjson_mut_val *gen = NULL;
153    if (opts && opts->max_tokens > 0) {
154        gen = yyjson_mut_obj(doc);
155        yyjson_mut_obj_add_int(doc, gen, "maxOutputTokens", opts->max_tokens);
156    }
157    long budget = opts ? budget_for(opts->effort) : -1;
158    if (budget >= 0) {
159        if (!gen) gen = yyjson_mut_obj(doc);
160        yyjson_mut_val *tc = yyjson_mut_obj(doc);
161        yyjson_mut_obj_add_val(doc, gen, "thinkingConfig", tc);
162        if (budget > 0) yyjson_mut_obj_add_bool(doc, tc, "includeThoughts", true);
163        yyjson_mut_obj_add_int(doc, tc, "thinkingBudget", budget);
164    }
165    if (gen) yyjson_mut_obj_add_val(doc, root, "generationConfig", gen);
166
167    size_t len = 0;
168    char *body = yyjson_mut_write(doc, 0, &len);
169    yyjson_mut_doc_free(doc);
170    return body;
171}
172
173// ---- streaming state ------------------------------------------------------
174
175typedef struct {
176    char *id;
177    char *name;
178    tiny_buf args;
179} gcall;
180
181typedef struct {
182    tiny_buf text;
183    tiny_buf reasoning;
184    gcall *calls;
185    size_t calls_len;
186    long tokens_in;
187    long tokens_out;
188    long tokens_total;
189    char *finish;
190    char *error;
191    tiny_stream_fn emit;
192    void *user;
193} google_state;
194
195static void add_call(google_state *st, const char *name, yyjson_val *args) {
196    st->calls = realloc(st->calls, (st->calls_len + 1) * sizeof *st->calls);
197    if (!st->calls) abort();
198    gcall *c = &st->calls[st->calls_len++];
199    memset(c, 0, sizeof *c);
200    c->name = xstrdup(name ? name : "");
201    char id[64];
202    snprintf(id, sizeof id, "%s_%zu", c->name, st->calls_len);
203    c->id = xstrdup(id);
204    if (c->name[0]) tiny_emit_tool_start(st->emit, st->user, c->id, c->name);
205    if (args) {
206        size_t len = 0;
207        char *txt = yyjson_val_write(args, 0, &len);
208        if (txt) {
209            buf_add(&c->args, txt, len);
210            free(txt);
211        }
212    }
213}
214
215static void on_data(const char *json, size_t len, void *user) {
216    google_state *st = user;
217    yyjson_doc *doc = yyjson_read(json, len, 0);
218    if (!doc) {
219        if (!st->error) st->error = xstrdup("stream: invalid JSON chunk");
220        return;
221    }
222    yyjson_val *root = yyjson_doc_get_root(doc);
223
224    yyjson_val *err = yyjson_obj_get(root, "error");
225    if (yyjson_is_obj(err)) {
226        yyjson_val *m = yyjson_obj_get(err, "message");
227        if (!st->error) st->error = xstrdup(yyjson_is_str(m) ? yyjson_get_str(m) : "stream: provider error");
228        yyjson_doc_free(doc);
229        return;
230    }
231
232    yyjson_val *cands = yyjson_obj_get(root, "candidates");
233    if (yyjson_is_arr(cands) && yyjson_arr_size(cands) > 0) {
234        yyjson_val *cand = yyjson_arr_get_first(cands);
235        yyjson_val *content = yyjson_obj_get(cand, "content");
236        yyjson_val *parts = yyjson_is_obj(content) ? yyjson_obj_get(content, "parts") : NULL;
237        if (yyjson_is_arr(parts)) {
238            size_t idx, max;
239            yyjson_val *part;
240            yyjson_arr_foreach(parts, idx, max, part) {
241                yyjson_val *fc = yyjson_obj_get(part, "functionCall");
242                if (yyjson_is_obj(fc)) {
243                    yyjson_val *name = yyjson_obj_get(fc, "name");
244                    yyjson_val *args = yyjson_obj_get(fc, "args");
245                    add_call(st, yyjson_is_str(name) ? yyjson_get_str(name) : "", args);
246                    continue;
247                }
248                yyjson_val *txt = yyjson_obj_get(part, "text");
249                if (!yyjson_is_str(txt) || !yyjson_get_str(txt)[0]) continue;
250                yyjson_val *thought = yyjson_obj_get(part, "thought");
251                const char *s = yyjson_get_str(txt);
252                if (yyjson_is_bool(thought) && yyjson_get_bool(thought)) {
253                    buf_add(&st->reasoning, s, strlen(s));
254                    tiny_emit_delta(st->emit, st->user, TINY_STREAM_REASONING_DELTA, s);
255                } else {
256                    buf_add(&st->text, s, strlen(s));
257                    tiny_emit_delta(st->emit, st->user, TINY_STREAM_TEXT_DELTA, s);
258                }
259            }
260        }
261        yyjson_val *fr = yyjson_obj_get(cand, "finishReason");
262        if (yyjson_is_str(fr)) {
263            free(st->finish);
264            st->finish = xstrdup(yyjson_get_str(fr));
265        }
266    }
267
268    yyjson_val *usage = yyjson_obj_get(root, "usageMetadata");
269    if (yyjson_is_obj(usage)) {
270        yyjson_val *v;
271        if (yyjson_is_num(v = yyjson_obj_get(usage, "promptTokenCount"))) st->tokens_in = (long)yyjson_get_num(v);
272        if (yyjson_is_num(v = yyjson_obj_get(usage, "candidatesTokenCount"))) st->tokens_out = (long)yyjson_get_num(v);
273        if (yyjson_is_num(v = yyjson_obj_get(usage, "thoughtsTokenCount"))) st->tokens_out += (long)yyjson_get_num(v);
274        if (yyjson_is_num(v = yyjson_obj_get(usage, "totalTokenCount"))) st->tokens_total = (long)yyjson_get_num(v);
275    }
276    yyjson_doc_free(doc);
277}
278
279static void free_state(google_state *st) {
280    free(st->text.data);
281    free(st->reasoning.data);
282    for (size_t i = 0; i < st->calls_len; i++) {
283        free(st->calls[i].id);
284        free(st->calls[i].name);
285        free(st->calls[i].args.data);
286    }
287    free(st->calls);
288    free(st->finish);
289    free(st->error);
290}
291
292static void finalize(google_state *st, tiny_message *out) {
293    tiny_add_text_block(out, TINY_BLOCK_THINKING, st->reasoning.data);
294    tiny_add_text_block(out, TINY_BLOCK_TEXT, st->text.data);
295    for (size_t i = 0; i < st->calls_len; i++) {
296        gcall *c = &st->calls[i];
297        tiny_add_tool_block(out, c->id, c->name, c->args.len ? c->args.data : NULL, st->emit, st->user);
298    }
299    out->tokens_in = st->tokens_in;
300    out->tokens_out = st->tokens_out;
301    out->tokens_total = st->tokens_total ? st->tokens_total : st->tokens_in + st->tokens_out;
302    if (st->finish && strcmp(st->finish, "MAX_TOKENS") == 0) out->stop_reason = xstrdup("length");
303    else if (st->calls_len > 0) out->stop_reason = xstrdup("toolUse");
304    else out->stop_reason = xstrdup("stop");
305}
306
307// ---- transport ------------------------------------------------------------
308
309static char *endpoint_url(const char *base, const char *model) {
310    if (!base || !base[0] || !model || !model[0]) return NULL;
311    const char *id = model;
312    const char *prefix = "models/";
313    size_t n = strlen(base);
314    bool slash = base[n - 1] == '/';
315    const char *sep = slash ? "" : "/";
316    if (strncmp(id, prefix, 6) == 0) {
317        size_t size = n + strlen(sep) + strlen(id) + 32;
318        char *url = xmalloc(size);
319        snprintf(url, size, "%s%s%s:streamGenerateContent?alt=sse", base, sep, id);
320        return url;
321    }
322    size_t size = n + strlen(sep) + strlen(prefix) + strlen(id) + 40;
323    char *url = xmalloc(size);
324    snprintf(url, size, "%s%s%s%s:streamGenerateContent?alt=sse", base, sep, prefix, id);
325    return url;
326}
327
328int tiny_google_generative_ai_stream(const tiny_model *model, const tiny_context *ctx,
329                                     const tiny_stream_opts *opts,
330                                     tiny_stream_fn emit, void *user,
331                                     const volatile bool *cancel,
332                                     tiny_message *out, char **err) {
333    if (err) *err = NULL;
334    memset(out, 0, sizeof *out);
335    out->role = TINY_ROLE_ASSISTANT;
336
337    char *url = endpoint_url(model->base_url, model->model);
338    if (!url) return tiny_fail(err, "provider: no base url");
339    char *body = build_request(ctx, opts);
340    if (!body) {
341        free(url);
342        return tiny_fail(err, "provider: cannot build request");
343    }
344
345    google_state st = {0};
346    st.emit = emit;
347    st.user = user;
348
349    tiny_api_result r = tiny_api_post(url, body, "x-goog-api-key: ", model->api_key,
350                                      NULL, cancel, on_data, &st, st.error, err);
351
352    int rc = 0;
353    if (r == TINY_API_ABORTED) {
354        out->stop_reason = xstrdup("aborted");
355    } else if (r == TINY_API_TRANSPORT_ERROR) {
356        rc = -1;
357    } else if (r == TINY_API_STREAM_ERROR) {
358        out->stop_reason = xstrdup("error");
359        out->error_message = xstrdup(st.error);
360    } else {
361        finalize(&st, out);
362    }
363    free_state(&st);
364    return rc;
365}