xonecas.io
7814
Ta

tiny-c-agent / files

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

14.6 KB
1// OpenAI-compatible chat completions: the wire format behind Opencode Zen/Go
2// and every customProvider with api "openai-completions".
3
4#include "api.h"
5
6#include <curl/curl.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include <yyjson.h>
12
13#include "http.h"
14#include "sse.h"
15#include "util.h"
16
17// ---- request building -----------------------------------------------------
18
19static void add_message(yyjson_mut_doc *doc, yyjson_mut_val *arr, const tiny_message *m) {
20    yyjson_mut_val *o = yyjson_mut_obj(doc);
21    yyjson_mut_arr_add_val(arr, o);
22
23    if (m->role == TINY_ROLE_TOOL) {
24        yyjson_mut_obj_add_strcpy(doc, o, "role", "tool");
25        yyjson_mut_obj_add_strcpy(doc, o, "tool_call_id", m->tool_call_id ? m->tool_call_id : "");
26        char *t = tiny_message_text(m);
27        if (t[0] == '\0' && tiny_message_has_image(m)) {
28            free(t);
29            t = xstrdup("(see attached image)");
30        }
31        yyjson_mut_obj_add_strcpy(doc, o, "content", t);
32        free(t);
33        return;
34    }
35    if (m->role == TINY_ROLE_USER) {
36        yyjson_mut_obj_add_strcpy(doc, o, "role", "user");
37        char *t = tiny_message_text(m);
38        yyjson_mut_obj_add_strcpy(doc, o, "content", t);
39        free(t);
40        return;
41    }
42
43    yyjson_mut_obj_add_strcpy(doc, o, "role", "assistant");
44    char *t = tiny_message_text(m);
45    yyjson_mut_obj_add_strcpy(doc, o, "content", t);
46    free(t);
47
48    bool has_calls = false;
49    yyjson_mut_val *calls = yyjson_mut_arr(doc);
50    for (size_t i = 0; i < m->blocks_len; i++) {
51        const tiny_block *b = &m->blocks[i];
52        if (b->type != TINY_BLOCK_TOOL_CALL) continue;
53        has_calls = true;
54        yyjson_mut_val *tc = yyjson_mut_obj(doc);
55        yyjson_mut_arr_add_val(calls, tc);
56        yyjson_mut_obj_add_strcpy(doc, tc, "id", b->tool_call_id ? b->tool_call_id : "");
57        yyjson_mut_obj_add_strcpy(doc, tc, "type", "function");
58        yyjson_mut_val *fn = yyjson_mut_obj(doc);
59        yyjson_mut_obj_add_val(doc, tc, "function", fn);
60        yyjson_mut_obj_add_strcpy(doc, fn, "name", b->tool_name ? b->tool_name : "");
61        yyjson_mut_obj_add_strcpy(doc, fn, "arguments", b->arguments ? b->arguments : "{}");
62    }
63    if (has_calls) yyjson_mut_obj_add_val(doc, o, "tool_calls", calls);
64}
65
66static void add_tools(yyjson_mut_doc *doc, yyjson_mut_val *root, const tiny_context *ctx) {
67    if (ctx->tools_len == 0) return;
68    yyjson_mut_val *tools = yyjson_mut_arr(doc);
69    yyjson_mut_obj_add_val(doc, root, "tools", tools);
70    for (size_t i = 0; i < ctx->tools_len; i++) {
71        const tiny_tool *t = &ctx->tools[i];
72        yyjson_mut_val *tool = yyjson_mut_obj(doc);
73        yyjson_mut_arr_add_val(tools, tool);
74        yyjson_mut_obj_add_strcpy(doc, tool, "type", "function");
75        yyjson_mut_val *fn = yyjson_mut_obj(doc);
76        yyjson_mut_obj_add_val(doc, tool, "function", fn);
77        tiny_json_tool(doc, fn, t, "parameters");
78    }
79}
80
81static char *build_request(const tiny_model *model, const tiny_context *ctx,
82                           const tiny_stream_opts *opts) {
83    yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
84    yyjson_mut_val *root = yyjson_mut_obj(doc);
85    yyjson_mut_doc_set_root(doc, root);
86
87    yyjson_mut_obj_add_strcpy(doc, root, "model", model->model ? model->model : "");
88    yyjson_mut_obj_add_bool(doc, root, "stream", true);
89    yyjson_mut_val *so = yyjson_mut_obj(doc);
90    yyjson_mut_obj_add_bool(doc, so, "include_usage", true);
91    yyjson_mut_obj_add_val(doc, root, "stream_options", so);
92
93    if (opts && opts->effort && strcmp(opts->effort, "off") != 0)
94        yyjson_mut_obj_add_strcpy(doc, root, "reasoning_effort", opts->effort);
95    if (opts && opts->max_tokens > 0)
96        yyjson_mut_obj_add_int(doc, root, "max_tokens", opts->max_tokens);
97
98    yyjson_mut_val *messages = yyjson_mut_arr(doc);
99    yyjson_mut_obj_add_val(doc, root, "messages", messages);
100    if (ctx->system_prompt && ctx->system_prompt[0]) {
101        yyjson_mut_val *sys = yyjson_mut_obj(doc);
102        yyjson_mut_arr_add_val(messages, sys);
103        yyjson_mut_obj_add_strcpy(doc, sys, "role", "system");
104        yyjson_mut_obj_add_strcpy(doc, sys, "content", ctx->system_prompt);
105    }
106    for (size_t i = 0; i < ctx->messages_len; ) {
107        if (ctx->messages[i].role == TINY_ROLE_TOOL) {
108            // A run of tool results lands together. Any images among them are
109            // sent as one following user message, because a tool message's
110            // content is a string and the provider rejects a user message
111            // between a call and its results.
112            size_t j = i;
113            bool has_images = false;
114            for (; j < ctx->messages_len && ctx->messages[j].role == TINY_ROLE_TOOL; j++) {
115                add_message(doc, messages, &ctx->messages[j]);
116                if (tiny_message_has_image(&ctx->messages[j])) has_images = true;
117            }
118            if (has_images) {
119                yyjson_mut_val *um = yyjson_mut_obj(doc);
120                yyjson_mut_arr_add_val(messages, um);
121                yyjson_mut_obj_add_strcpy(doc, um, "role", "user");
122                yyjson_mut_val *content = yyjson_mut_arr(doc);
123                yyjson_mut_obj_add_val(doc, um, "content", content);
124                yyjson_mut_val *head = yyjson_mut_obj(doc);
125                yyjson_mut_arr_add_val(content, head);
126                yyjson_mut_obj_add_strcpy(doc, head, "type", "text");
127                yyjson_mut_obj_add_strcpy(doc, head, "text", "Attached image(s) from tool result:");
128                for (size_t m = i; m < j; m++) {
129                    const tiny_message *msg = &ctx->messages[m];
130                    for (size_t k = 0; k < msg->blocks_len; k++) {
131                        const tiny_block *b = &msg->blocks[k];
132                        if (b->type != TINY_BLOCK_IMAGE) continue;
133                        yyjson_mut_val *iu = yyjson_mut_obj(doc);
134                        yyjson_mut_arr_add_val(content, iu);
135                        yyjson_mut_obj_add_strcpy(doc, iu, "type", "image_url");
136                        yyjson_mut_val *url = yyjson_mut_obj(doc);
137                        yyjson_mut_obj_add_val(doc, iu, "image_url", url);
138                        const char *mt = b->media_type ? b->media_type : "";
139                        const char *bs = b->data ? b->data : "";
140                        size_t need = strlen(mt) + strlen(bs) + 32;
141                        char *u = xmalloc(need);
142                        snprintf(u, need, "data:%s;base64,%s", mt, bs);
143                        yyjson_mut_obj_add_strcpy(doc, url, "url", u);
144                        free(u);
145                    }
146                }
147            }
148            i = j;
149        } else {
150            add_message(doc, messages, &ctx->messages[i]);
151            i++;
152        }
153    }
154
155    add_tools(doc, root, ctx);
156
157    size_t len = 0;
158    char *body = yyjson_mut_write(doc, 0, &len);
159    yyjson_mut_doc_free(doc);
160    return body;
161}
162
163// ---- streaming state ------------------------------------------------------
164
165typedef struct {
166    char *id;
167    char *name;
168    tiny_buf args;
169    bool started;
170} tool_acc;
171
172typedef struct {
173    tiny_buf text;
174    tiny_buf reasoning;
175    tool_acc *calls;
176    size_t calls_len;
177    char *finish_reason;
178    long tokens_in;
179    long tokens_out;
180    long tokens_total;
181    char *error;
182    tiny_stream_fn emit;
183    void *user;
184} sse_state;
185
186// An untrusted stream index is a hint, never a size. Anything past this is
187// treated like a missing index so a hostile chunk cannot force an allocation.
188#define MAX_STREAM_INDEX 10000
189
190static void grow_calls(sse_state *st, size_t count) {
191    if (count <= st->calls_len) return;
192    st->calls = realloc(st->calls, count * sizeof *st->calls);
193    if (!st->calls) abort();
194    memset(&st->calls[st->calls_len], 0, (count - st->calls_len) * sizeof *st->calls);
195    st->calls_len = count;
196}
197
198static void handle_delta(sse_state *st, yyjson_val *delta) {
199    yyjson_val *content = yyjson_obj_get(delta, "content");
200    if (yyjson_is_str(content) && yyjson_get_str(content)[0]) {
201        const char *s = yyjson_get_str(content);
202        buf_add(&st->text, s, strlen(s));
203        tiny_emit_delta(st->emit, st->user, TINY_STREAM_TEXT_DELTA, s);
204    }
205
206    static const char *const reasoning_fields[] = {"reasoning_content", "reasoning", "reasoning_text"};
207    for (size_t i = 0; i < sizeof reasoning_fields / sizeof reasoning_fields[0]; i++) {
208        yyjson_val *r = yyjson_obj_get(delta, reasoning_fields[i]);
209        if (!yyjson_is_str(r) || !yyjson_get_str(r)[0]) continue;
210        const char *s = yyjson_get_str(r);
211        buf_add(&st->reasoning, s, strlen(s));
212        tiny_emit_delta(st->emit, st->user, TINY_STREAM_REASONING_DELTA, s);
213        break;
214    }
215
216    yyjson_val *tcs = yyjson_obj_get(delta, "tool_calls");
217    if (!yyjson_is_arr(tcs)) return;
218    size_t idx, max;
219    yyjson_val *tc;
220    yyjson_arr_foreach(tcs, idx, max, tc) {
221        yyjson_val *iv = yyjson_obj_get(tc, "index");
222        long raw = yyjson_is_int(iv) ? (long)yyjson_get_int(iv) : -1;
223        size_t index = raw >= 0 && (size_t)raw <= MAX_STREAM_INDEX ? (size_t)raw : st->calls_len;
224        grow_calls(st, index + 1);
225        tool_acc *a = &st->calls[index];
226        yyjson_val *id = yyjson_obj_get(tc, "id");
227        if (yyjson_is_str(id)) {
228            free(a->id);
229            a->id = xstrdup(yyjson_get_str(id));
230        }
231        yyjson_val *fn = yyjson_obj_get(tc, "function");
232        if (!yyjson_is_obj(fn)) continue;
233        yyjson_val *name = yyjson_obj_get(fn, "name");
234        if (yyjson_is_str(name)) {
235            free(a->name);
236            a->name = xstrdup(yyjson_get_str(name));
237        }
238        if (!a->started && a->name && a->name[0]) {
239            a->started = true;
240            tiny_emit_tool_start(st->emit, st->user, a->id, a->name);
241        }
242        yyjson_val *args = yyjson_obj_get(fn, "arguments");
243        if (yyjson_is_str(args)) {
244            const char *s = yyjson_get_str(args);
245            buf_add(&a->args, s, strlen(s));
246        }
247    }
248}
249
250static void on_data(const char *json, size_t len, void *user) {
251    sse_state *st = user;
252    yyjson_doc *doc = yyjson_read(json, len, 0);
253    if (!doc) {
254        if (!st->error) st->error = xstrdup("stream: invalid JSON chunk");
255        return;
256    }
257    yyjson_val *root = yyjson_doc_get_root(doc);
258
259    yyjson_val *err = yyjson_obj_get(root, "error");
260    if (err) {
261        if (!st->error) {
262            const char *msg = NULL;
263            if (yyjson_is_obj(err)) {
264                yyjson_val *m = yyjson_obj_get(err, "message");
265                if (yyjson_is_str(m)) msg = yyjson_get_str(m);
266            } else if (yyjson_is_str(err)) {
267                msg = yyjson_get_str(err);
268            }
269            st->error = xstrdup(msg ? msg : "stream: provider error");
270        }
271        yyjson_doc_free(doc);
272        return;
273    }
274
275    yyjson_val *choices = yyjson_obj_get(root, "choices");
276    if (yyjson_is_arr(choices) && yyjson_arr_size(choices) > 0) {
277        yyjson_val *choice = yyjson_arr_get_first(choices);
278        yyjson_val *delta = yyjson_obj_get(choice, "delta");
279        if (yyjson_is_obj(delta)) handle_delta(st, delta);
280        yyjson_val *fr = yyjson_obj_get(choice, "finish_reason");
281        if (yyjson_is_str(fr)) {
282            free(st->finish_reason);
283            st->finish_reason = xstrdup(yyjson_get_str(fr));
284        }
285    }
286
287    yyjson_val *usage = yyjson_obj_get(root, "usage");
288    if (yyjson_is_obj(usage)) {
289        yyjson_val *v;
290        if (yyjson_is_num(v = yyjson_obj_get(usage, "prompt_tokens"))) st->tokens_in = (long)yyjson_get_num(v);
291        if (yyjson_is_num(v = yyjson_obj_get(usage, "completion_tokens"))) st->tokens_out = (long)yyjson_get_num(v);
292        if (yyjson_is_num(v = yyjson_obj_get(usage, "total_tokens"))) st->tokens_total = (long)yyjson_get_num(v);
293    }
294    yyjson_doc_free(doc);
295}
296
297static void sse_free(sse_state *st) {
298    buf_free(&st->text);
299    buf_free(&st->reasoning);
300    for (size_t i = 0; i < st->calls_len; i++) {
301        free(st->calls[i].id);
302        free(st->calls[i].name);
303        buf_free(&st->calls[i].args);
304    }
305    free(st->calls);
306    free(st->finish_reason);
307    free(st->error);
308}
309
310static void finalize(sse_state *st, tiny_message *out) {
311    tiny_add_text_block(out, TINY_BLOCK_THINKING, st->reasoning.data);
312    tiny_add_text_block(out, TINY_BLOCK_TEXT, st->text.data);
313    for (size_t i = 0; i < st->calls_len; i++) {
314        tool_acc *a = &st->calls[i];
315        if (!a->name && a->args.len == 0) continue;
316        tiny_add_tool_block(out, a->id, a->name, a->args.len ? a->args.data : NULL, st->emit, st->user);
317    }
318    out->tokens_in = st->tokens_in;
319    out->tokens_out = st->tokens_out;
320    out->tokens_total = st->tokens_total;
321    if (st->calls_len > 0) out->stop_reason = xstrdup("toolUse");
322    else if (st->finish_reason && strcmp(st->finish_reason, "length") == 0) out->stop_reason = xstrdup("length");
323    else out->stop_reason = xstrdup("stop");
324}
325
326// ---- transport ------------------------------------------------------------
327
328
329int tiny_openai_completions_stream(const tiny_model *model, const tiny_context *ctx,
330                                   const tiny_stream_opts *opts,
331                                   tiny_stream_fn emit, void *user,
332                                   const volatile bool *cancel,
333                                   tiny_message *out, char **err) {
334    if (err) *err = NULL;
335    memset(out, 0, sizeof *out);
336    out->role = TINY_ROLE_ASSISTANT;
337
338    char *url = tiny_url_endpoint(model->base_url, "chat/completions");
339    if (!url) return tiny_fail(err, "provider: no base url");
340    char *body = build_request(model, ctx, opts);
341    if (!body) {
342        free(url);
343        return tiny_fail(err, "provider: cannot build request");
344    }
345
346    sse_state st = {0};
347    st.emit = emit;
348    st.user = user;
349
350    const char *extra[2] = {NULL, NULL};
351    char *session = NULL;
352    if (model->session_header && model->session_header[0] && opts && opts->session_id && opts->session_id[0]) {
353        size_t n = strlen(model->session_header) + strlen(opts->session_id) + 4;
354        session = xmalloc(n);
355        snprintf(session, n, "%s: %s", model->session_header, opts->session_id);
356        extra[0] = session;
357    }
358
359    tiny_api_result r = tiny_api_post(url, body, "Authorization: Bearer ",
360                                      model->api_key, extra, cancel, on_data, &st, st.error, err);
361    free(session);
362
363    int rc;
364    if (r == TINY_API_ABORTED) {
365        out->stop_reason = xstrdup("aborted");
366        rc = 0;
367    } else if (r == TINY_API_TRANSPORT_ERROR) {
368        rc = -1;
369    } else if (r == TINY_API_STREAM_ERROR) {
370        out->stop_reason = xstrdup("error");
371        out->error_message = xstrdup(st.error);
372        rc = 0;
373    } else {
374        finalize(&st, out);
375        rc = 0;
376    }
377    sse_free(&st);
378    return rc;
379}