@@ -0,0 +1,534 @@
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 <stdarg.h>
8+#include <stdio.h>
9+#include <stdlib.h>
10+#include <string.h>
11+
12+#include <yyjson.h>
13+
14+static void *xmalloc(size_t n) {
15+ void *p = malloc(n);
16+ if (!p) abort();
17+ return p;
18+}
19+
20+static char *xstrdup(const char *s) {
21+ size_t n = strlen(s) + 1;
22+ char *p = xmalloc(n);
23+ memcpy(p, s, n);
24+ return p;
25+}
26+
27+static int fail(char **err, const char *fmt, ...) {
28+ char detail[512];
29+ va_list ap;
30+ va_start(ap, fmt);
31+ vsnprintf(detail, sizeof detail, fmt, ap);
32+ va_end(ap);
33+ *err = xstrdup(detail);
34+ return -1;
35+}
36+
37+// ---- growable byte buffer -------------------------------------------------
38+
39+typedef struct {
40+ char *data;
41+ size_t len;
42+ size_t cap;
43+} buf;
44+
45+static void buf_add(buf *b, const void *p, size_t n) {
46+ if (b->len + n + 1 > b->cap) {
47+ size_t cap = b->cap ? b->cap : 128;
48+ while (cap < b->len + n + 1) cap *= 2;
49+ b->data = realloc(b->data, cap);
50+ if (!b->data) abort();
51+ b->cap = cap;
52+ }
53+ memcpy(b->data + b->len, p, n);
54+ b->len += n;
55+ b->data[b->len] = '\0';
56+}
57+
58+static void buf_free(buf *b) {
59+ free(b->data);
60+ b->data = NULL;
61+ b->len = b->cap = 0;
62+}
63+
64+// ---- request building -----------------------------------------------------
65+
66+// Concatenates a message's text blocks into one string.
67+static char *join_text(const cage_message *m) {
68+ buf b = {0};
69+ for (size_t i = 0; i < m->blocks_len; i++)
70+ if (m->blocks[i].type == CAGE_BLOCK_TEXT && m->blocks[i].text)
71+ buf_add(&b, m->blocks[i].text, strlen(m->blocks[i].text));
72+ buf_add(&b, "", 0);
73+ return b.data;
74+}
75+
76+static void add_message(yyjson_mut_doc *doc, yyjson_mut_val *arr, const cage_message *m) {
77+ yyjson_mut_val *o = yyjson_mut_obj(doc);
78+ yyjson_mut_arr_add_val(arr, o);
79+
80+ if (m->role == CAGE_ROLE_TOOL) {
81+ yyjson_mut_obj_add_strcpy(doc, o, "role", "tool");
82+ yyjson_mut_obj_add_strcpy(doc, o, "tool_call_id", m->tool_call_id ? m->tool_call_id : "");
83+ char *t = join_text(m);
84+ yyjson_mut_obj_add_strcpy(doc, o, "content", t);
85+ free(t);
86+ return;
87+ }
88+ if (m->role == CAGE_ROLE_USER) {
89+ yyjson_mut_obj_add_strcpy(doc, o, "role", "user");
90+ char *t = join_text(m);
91+ yyjson_mut_obj_add_strcpy(doc, o, "content", t);
92+ free(t);
93+ return;
94+ }
95+
96+ yyjson_mut_obj_add_strcpy(doc, o, "role", "assistant");
97+ char *t = join_text(m);
98+ yyjson_mut_obj_add_strcpy(doc, o, "content", t);
99+ free(t);
100+
101+ bool has_calls = false;
102+ yyjson_mut_val *calls = yyjson_mut_arr(doc);
103+ for (size_t i = 0; i < m->blocks_len; i++) {
104+ const cage_block *b = &m->blocks[i];
105+ if (b->type != CAGE_BLOCK_TOOL_CALL) continue;
106+ has_calls = true;
107+ yyjson_mut_val *tc = yyjson_mut_obj(doc);
108+ yyjson_mut_arr_add_val(calls, tc);
109+ yyjson_mut_obj_add_strcpy(doc, tc, "id", b->tool_call_id ? b->tool_call_id : "");
110+ yyjson_mut_obj_add_strcpy(doc, tc, "type", "function");
111+ yyjson_mut_val *fn = yyjson_mut_obj(doc);
112+ yyjson_mut_obj_add_val(doc, tc, "function", fn);
113+ yyjson_mut_obj_add_strcpy(doc, fn, "name", b->tool_name ? b->tool_name : "");
114+ yyjson_mut_obj_add_strcpy(doc, fn, "arguments", b->arguments ? b->arguments : "{}");
115+ }
116+ if (has_calls) yyjson_mut_obj_add_val(doc, o, "tool_calls", calls);
117+}
118+
119+static void add_tools(yyjson_mut_doc *doc, yyjson_mut_val *root, const cage_context *ctx) {
120+ if (ctx->tools_len == 0) return;
121+ yyjson_mut_val *tools = yyjson_mut_arr(doc);
122+ yyjson_mut_obj_add_val(doc, root, "tools", tools);
123+ for (size_t i = 0; i < ctx->tools_len; i++) {
124+ const cage_tool *t = &ctx->tools[i];
125+ yyjson_mut_val *tool = yyjson_mut_obj(doc);
126+ yyjson_mut_arr_add_val(tools, tool);
127+ yyjson_mut_obj_add_strcpy(doc, tool, "type", "function");
128+ yyjson_mut_val *fn = yyjson_mut_obj(doc);
129+ yyjson_mut_obj_add_val(doc, tool, "function", fn);
130+ yyjson_mut_obj_add_strcpy(doc, fn, "name", t->name);
131+ yyjson_mut_obj_add_strcpy(doc, fn, "description", t->description ? t->description : "");
132+ const char *params = t->parameters ? t->parameters : "{}";
133+ yyjson_doc *pdoc = yyjson_read(params, strlen(params), 0);
134+ if (pdoc) {
135+ yyjson_mut_val *copy = yyjson_val_mut_copy(doc, yyjson_doc_get_root(pdoc));
136+ yyjson_mut_obj_add_val(doc, fn, "parameters", copy);
137+ yyjson_doc_free(pdoc);
138+ }
139+ }
140+}
141+
142+static char *build_request(const cage_model *model, const cage_context *ctx,
143+ const cage_stream_opts *opts) {
144+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
145+ yyjson_mut_val *root = yyjson_mut_obj(doc);
146+ yyjson_mut_doc_set_root(doc, root);
147+
148+ yyjson_mut_obj_add_strcpy(doc, root, "model", model->model ? model->model : "");
149+ yyjson_mut_obj_add_bool(doc, root, "stream", true);
150+ yyjson_mut_val *so = yyjson_mut_obj(doc);
151+ yyjson_mut_obj_add_bool(doc, so, "include_usage", true);
152+ yyjson_mut_obj_add_val(doc, root, "stream_options", so);
153+
154+ if (opts && opts->effort && strcmp(opts->effort, "off") != 0)
155+ yyjson_mut_obj_add_strcpy(doc, root, "reasoning_effort", opts->effort);
156+ if (opts && opts->max_tokens > 0)
157+ yyjson_mut_obj_add_int(doc, root, "max_tokens", opts->max_tokens);
158+
159+ yyjson_mut_val *messages = yyjson_mut_arr(doc);
160+ yyjson_mut_obj_add_val(doc, root, "messages", messages);
161+ if (ctx->system_prompt && ctx->system_prompt[0]) {
162+ yyjson_mut_val *sys = yyjson_mut_obj(doc);
163+ yyjson_mut_arr_add_val(messages, sys);
164+ yyjson_mut_obj_add_strcpy(doc, sys, "role", "system");
165+ yyjson_mut_obj_add_strcpy(doc, sys, "content", ctx->system_prompt);
166+ }
167+ for (size_t i = 0; i < ctx->messages_len; i++)
168+ add_message(doc, messages, &ctx->messages[i]);
169+
170+ add_tools(doc, root, ctx);
171+
172+ size_t len = 0;
173+ char *body = yyjson_mut_write(doc, 0, &len);
174+ yyjson_mut_doc_free(doc);
175+ return body;
176+}
177+
178+// ---- streaming state ------------------------------------------------------
179+
180+typedef struct {
181+ char *id;
182+ char *name;
183+ buf args;
184+} tool_acc;
185+
186+typedef struct {
187+ buf pending; // bytes not yet split into SSE lines
188+ buf raw; // response body, kept only for error messages
189+ buf text;
190+ buf reasoning;
191+ tool_acc *calls;
192+ size_t calls_len;
193+ char *finish_reason;
194+ long tokens_in;
195+ long tokens_out;
196+ long tokens_total;
197+ char *error;
198+ int http_status;
199+ const volatile bool *cancel;
200+ cage_stream_fn emit;
201+ void *user;
202+} sse_state;
203+
204+static void emit_delta(sse_state *st, cage_stream_type type, const char *delta) {
205+ if (!st->emit) return;
206+ cage_stream_event ev = {.type = type, .delta = delta};
207+ st->emit(&ev, st->user);
208+}
209+
210+static void grow_calls(sse_state *st, size_t count) {
211+ if (count <= st->calls_len) return;
212+ st->calls = realloc(st->calls, count * sizeof *st->calls);
213+ if (!st->calls) abort();
214+ memset(&st->calls[st->calls_len], 0, (count - st->calls_len) * sizeof *st->calls);
215+ st->calls_len = count;
216+}
217+
218+static void handle_delta(sse_state *st, yyjson_val *delta) {
219+ yyjson_val *content = yyjson_obj_get(delta, "content");
220+ if (yyjson_is_str(content) && yyjson_get_str(content)[0]) {
221+ const char *s = yyjson_get_str(content);
222+ buf_add(&st->text, s, strlen(s));
223+ emit_delta(st, CAGE_STREAM_TEXT_DELTA, s);
224+ }
225+
226+ static const char *const reasoning_fields[] = {"reasoning_content", "reasoning", "reasoning_text"};
227+ for (size_t i = 0; i < sizeof reasoning_fields / sizeof reasoning_fields[0]; i++) {
228+ yyjson_val *r = yyjson_obj_get(delta, reasoning_fields[i]);
229+ if (!yyjson_is_str(r) || !yyjson_get_str(r)[0]) continue;
230+ const char *s = yyjson_get_str(r);
231+ buf_add(&st->reasoning, s, strlen(s));
232+ emit_delta(st, CAGE_STREAM_REASONING_DELTA, s);
233+ break;
234+ }
235+
236+ yyjson_val *tcs = yyjson_obj_get(delta, "tool_calls");
237+ if (!yyjson_is_arr(tcs)) return;
238+ size_t idx, max;
239+ yyjson_val *tc;
240+ yyjson_arr_foreach(tcs, idx, max, tc) {
241+ yyjson_val *iv = yyjson_obj_get(tc, "index");
242+ size_t index = yyjson_is_int(iv) ? (size_t)yyjson_get_int(iv) : st->calls_len;
243+ grow_calls(st, index + 1);
244+ tool_acc *a = &st->calls[index];
245+ yyjson_val *id = yyjson_obj_get(tc, "id");
246+ if (yyjson_is_str(id)) {
247+ free(a->id);
248+ a->id = xstrdup(yyjson_get_str(id));
249+ }
250+ yyjson_val *fn = yyjson_obj_get(tc, "function");
251+ if (!yyjson_is_obj(fn)) continue;
252+ yyjson_val *name = yyjson_obj_get(fn, "name");
253+ if (yyjson_is_str(name)) {
254+ free(a->name);
255+ a->name = xstrdup(yyjson_get_str(name));
256+ }
257+ yyjson_val *args = yyjson_obj_get(fn, "arguments");
258+ if (yyjson_is_str(args)) {
259+ const char *s = yyjson_get_str(args);
260+ buf_add(&a->args, s, strlen(s));
261+ }
262+ }
263+}
264+
265+static void handle_data(sse_state *st, const char *json, size_t len) {
266+ yyjson_doc *doc = yyjson_read(json, len, 0);
267+ if (!doc) {
268+ if (!st->error) st->error = xstrdup("stream: invalid JSON chunk");
269+ return;
270+ }
271+ yyjson_val *root = yyjson_doc_get_root(doc);
272+
273+ yyjson_val *err = yyjson_obj_get(root, "error");
274+ if (err) {
275+ if (!st->error) {
276+ const char *msg = NULL;
277+ if (yyjson_is_obj(err)) {
278+ yyjson_val *m = yyjson_obj_get(err, "message");
279+ if (yyjson_is_str(m)) msg = yyjson_get_str(m);
280+ } else if (yyjson_is_str(err)) {
281+ msg = yyjson_get_str(err);
282+ }
283+ st->error = xstrdup(msg ? msg : "stream: provider error");
284+ }
285+ yyjson_doc_free(doc);
286+ return;
287+ }
288+
289+ yyjson_val *choices = yyjson_obj_get(root, "choices");
290+ if (yyjson_is_arr(choices) && yyjson_arr_size(choices) > 0) {
291+ yyjson_val *choice = yyjson_arr_get_first(choices);
292+ yyjson_val *delta = yyjson_obj_get(choice, "delta");
293+ if (yyjson_is_obj(delta)) handle_delta(st, delta);
294+ yyjson_val *fr = yyjson_obj_get(choice, "finish_reason");
295+ if (yyjson_is_str(fr)) {
296+ free(st->finish_reason);
297+ st->finish_reason = xstrdup(yyjson_get_str(fr));
298+ }
299+ }
300+
301+ yyjson_val *usage = yyjson_obj_get(root, "usage");
302+ if (yyjson_is_obj(usage)) {
303+ yyjson_val *v;
304+ if (yyjson_is_num(v = yyjson_obj_get(usage, "prompt_tokens"))) st->tokens_in = (long)yyjson_get_num(v);
305+ if (yyjson_is_num(v = yyjson_obj_get(usage, "completion_tokens"))) st->tokens_out = (long)yyjson_get_num(v);
306+ if (yyjson_is_num(v = yyjson_obj_get(usage, "total_tokens"))) st->tokens_total = (long)yyjson_get_num(v);
307+ }
308+ yyjson_doc_free(doc);
309+}
310+
311+static void handle_line(sse_state *st, const char *line, size_t len) {
312+ if (len < 5 || memcmp(line, "data:", 5) != 0) return;
313+ const char *p = line + 5;
314+ const char *end = line + len;
315+ while (p < end && *p == ' ') p++;
316+ if (end - p == 6 && memcmp(p, "[DONE]", 6) == 0) return;
317+ if (end > p) handle_data(st, p, (size_t)(end - p));
318+}
319+
320+static void sse_feed(sse_state *st, const char *data, size_t n) {
321+ buf_add(&st->pending, data, n);
322+ size_t start = 0;
323+ for (size_t i = 0; i < st->pending.len; i++) {
324+ if (st->pending.data[i] != '\n') continue;
325+ size_t end = i;
326+ if (end > start && st->pending.data[end - 1] == '\r') end--;
327+ handle_line(st, st->pending.data + start, end - start);
328+ start = i + 1;
329+ }
330+ if (start > 0) {
331+ memmove(st->pending.data, st->pending.data + start, st->pending.len - start);
332+ st->pending.len -= start;
333+ st->pending.data[st->pending.len] = '\0';
334+ }
335+}
336+
337+static void sse_free(sse_state *st) {
338+ buf_free(&st->pending);
339+ buf_free(&st->raw);
340+ buf_free(&st->text);
341+ buf_free(&st->reasoning);
342+ for (size_t i = 0; i < st->calls_len; i++) {
343+ free(st->calls[i].id);
344+ free(st->calls[i].name);
345+ buf_free(&st->calls[i].args);
346+ }
347+ free(st->calls);
348+ free(st->finish_reason);
349+ free(st->error);
350+}
351+
352+static void finalize(sse_state *st, cage_message *out) {
353+ if (st->reasoning.len > 0) {
354+ cage_block *b = cage_message_add_block(out, CAGE_BLOCK_THINKING);
355+ b->text = xstrdup(st->reasoning.data);
356+ }
357+ if (st->text.len > 0) {
358+ cage_block *b = cage_message_add_block(out, CAGE_BLOCK_TEXT);
359+ b->text = xstrdup(st->text.data);
360+ }
361+ for (size_t i = 0; i < st->calls_len; i++) {
362+ tool_acc *a = &st->calls[i];
363+ if (!a->name && a->args.len == 0) continue;
364+ cage_block *b = cage_message_add_block(out, CAGE_BLOCK_TOOL_CALL);
365+ b->tool_call_id = xstrdup(a->id ? a->id : "");
366+ b->tool_name = xstrdup(a->name ? a->name : "");
367+ b->arguments = xstrdup(a->args.len ? a->args.data : "{}");
368+ if (st->emit) {
369+ cage_stream_event ev = {
370+ .type = CAGE_STREAM_TOOL_CALL,
371+ .tool_call_id = b->tool_call_id,
372+ .tool_name = b->tool_name,
373+ .tool_arguments = b->arguments,
374+ };
375+ st->emit(&ev, st->user);
376+ }
377+ }
378+ out->tokens_in = st->tokens_in;
379+ out->tokens_out = st->tokens_out;
380+ out->tokens_total = st->tokens_total;
Cut short: this commit's diff is over 1000 lines.