7814
Ta
tiny-c-agent / files
tiny-c-agent / src / api / openai_responses.c
16.3 KB
1// OpenAI Responses API 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_input(yyjson_mut_doc *doc, yyjson_mut_val *arr, const tiny_message *m) { 17 if (m->role == TINY_ROLE_TOOL) { 18 yyjson_mut_val *o = yyjson_mut_obj(doc); 19 yyjson_mut_arr_add_val(arr, o); 20 yyjson_mut_obj_add_strcpy(doc, o, "type", "function_call_output"); 21 yyjson_mut_obj_add_strcpy(doc, o, "call_id", m->tool_call_id ? m->tool_call_id : ""); 22 char *t = tiny_message_text(m); 23 yyjson_mut_obj_add_strcpy(doc, o, "output", t); 24 free(t); 25 return; 26 } 27 if (m->role == TINY_ROLE_USER) { 28 yyjson_mut_val *o = yyjson_mut_obj(doc); 29 yyjson_mut_arr_add_val(arr, o); 30 yyjson_mut_obj_add_strcpy(doc, o, "role", "user"); 31 yyjson_mut_val *content = yyjson_mut_arr(doc); 32 yyjson_mut_obj_add_val(doc, o, "content", content); 33 for (size_t i = 0; i < m->blocks_len; i++) { 34 const tiny_block *b = &m->blocks[i]; 35 if (b->type == TINY_BLOCK_TEXT) { 36 yyjson_mut_val *p = yyjson_mut_obj(doc); 37 yyjson_mut_arr_add_val(content, p); 38 yyjson_mut_obj_add_strcpy(doc, p, "type", "input_text"); 39 yyjson_mut_obj_add_strcpy(doc, p, "text", b->text ? b->text : ""); 40 } else if (b->type == TINY_BLOCK_IMAGE) { 41 yyjson_mut_val *p = yyjson_mut_obj(doc); 42 yyjson_mut_arr_add_val(content, p); 43 yyjson_mut_obj_add_strcpy(doc, p, "type", "input_image"); 44 yyjson_mut_obj_add_strcpy(doc, p, "detail", "auto"); 45 size_t need = strlen(b->media_type ? b->media_type : "") + 46 strlen(b->data ? b->data : "") + 32; 47 char *u = xmalloc(need); 48 snprintf(u, need, "data:%s;base64,%s", b->media_type ? b->media_type : "", 49 b->data ? b->data : ""); 50 yyjson_mut_obj_add_strcpy(doc, p, "image_url", u); 51 free(u); 52 } 53 } 54 return; 55 } 56 57 // Assistant: text as a message item, each tool call as its own item. 58 for (size_t i = 0; i < m->blocks_len; i++) { 59 const tiny_block *b = &m->blocks[i]; 60 if (b->type == TINY_BLOCK_TEXT) { 61 yyjson_mut_val *o = yyjson_mut_obj(doc); 62 yyjson_mut_arr_add_val(arr, o); 63 yyjson_mut_obj_add_strcpy(doc, o, "type", "message"); 64 yyjson_mut_obj_add_strcpy(doc, o, "role", "assistant"); 65 yyjson_mut_val *content = yyjson_mut_arr(doc); 66 yyjson_mut_obj_add_val(doc, o, "content", content); 67 yyjson_mut_val *p = yyjson_mut_obj(doc); 68 yyjson_mut_arr_add_val(content, p); 69 yyjson_mut_obj_add_strcpy(doc, p, "type", "output_text"); 70 yyjson_mut_obj_add_strcpy(doc, p, "text", b->text ? b->text : ""); 71 } else if (b->type == TINY_BLOCK_TOOL_CALL) { 72 yyjson_mut_val *o = yyjson_mut_obj(doc); 73 yyjson_mut_arr_add_val(arr, o); 74 yyjson_mut_obj_add_strcpy(doc, o, "type", "function_call"); 75 yyjson_mut_obj_add_strcpy(doc, o, "call_id", b->tool_call_id ? b->tool_call_id : ""); 76 yyjson_mut_obj_add_strcpy(doc, o, "name", b->tool_name ? b->tool_name : ""); 77 yyjson_mut_obj_add_strcpy(doc, o, "arguments", b->arguments ? b->arguments : "{}"); 78 } 79 } 80} 81 82static void add_tools(yyjson_mut_doc *doc, yyjson_mut_val *root, const tiny_context *ctx) { 83 if (ctx->tools_len == 0) return; 84 yyjson_mut_val *tools = yyjson_mut_arr(doc); 85 yyjson_mut_obj_add_val(doc, root, "tools", tools); 86 for (size_t i = 0; i < ctx->tools_len; i++) { 87 const tiny_tool *t = &ctx->tools[i]; 88 yyjson_mut_val *tool = yyjson_mut_obj(doc); 89 yyjson_mut_arr_add_val(tools, tool); 90 yyjson_mut_obj_add_strcpy(doc, tool, "type", "function"); 91 tiny_json_tool(doc, tool, t, "parameters"); 92 } 93} 94 95static char *build_request(const tiny_model *model, const tiny_context *ctx, 96 const tiny_stream_opts *opts) { 97 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); 98 yyjson_mut_val *root = yyjson_mut_obj(doc); 99 yyjson_mut_doc_set_root(doc, root); 100 101 yyjson_mut_obj_add_strcpy(doc, root, "model", model->model ? model->model : ""); 102 yyjson_mut_obj_add_bool(doc, root, "stream", true); 103 yyjson_mut_obj_add_bool(doc, root, "store", false); 104 if (ctx->system_prompt && ctx->system_prompt[0]) 105 yyjson_mut_obj_add_strcpy(doc, root, "instructions", ctx->system_prompt); 106 if (opts && opts->max_tokens > 0) 107 yyjson_mut_obj_add_int(doc, root, "max_output_tokens", opts->max_tokens > 16 ? opts->max_tokens : 16); 108 if (opts && opts->effort && strcmp(opts->effort, "off") != 0) { 109 yyjson_mut_val *reasoning = yyjson_mut_obj(doc); 110 yyjson_mut_obj_add_val(doc, root, "reasoning", reasoning); 111 yyjson_mut_obj_add_strcpy(doc, reasoning, "effort", opts->effort); 112 } 113 114 yyjson_mut_val *input = yyjson_mut_arr(doc); 115 yyjson_mut_obj_add_val(doc, root, "input", input); 116 for (size_t i = 0; i < ctx->messages_len; i++) add_input(doc, input, &ctx->messages[i]); 117 118 add_tools(doc, root, ctx); 119 120 size_t len = 0; 121 char *body = yyjson_mut_write(doc, 0, &len); 122 yyjson_mut_doc_free(doc); 123 return body; 124} 125 126// ---- streaming state ------------------------------------------------------ 127 128typedef struct { 129 char *call_id; 130 char *name; 131 tiny_buf args; 132 bool started; 133} call_acc; 134 135typedef struct { 136 tiny_buf text; 137 tiny_buf reasoning; 138 call_acc *calls; 139 size_t calls_len; 140 long tokens_in; 141 long tokens_out; 142 long tokens_total; 143 char *status; 144 char *incomplete; 145 char *error; 146 tiny_stream_fn emit; 147 void *user; 148} resp_state; 149 150// An untrusted stream index is a hint, never a size. Anything past this is 151// treated like a missing index so a hostile chunk cannot force an allocation. 152#define MAX_STREAM_INDEX 10000 153 154static void grow_calls(resp_state *st, size_t count) { 155 if (count <= st->calls_len) return; 156 st->calls = realloc(st->calls, count * sizeof *st->calls); 157 if (!st->calls) abort(); 158 memset(&st->calls[st->calls_len], 0, (count - st->calls_len) * sizeof *st->calls); 159 st->calls_len = count; 160} 161 162// A stream index from the wire, or `fallback` when absent, negative, or past 163// the bound. An untrusted index is a hint, never a size, so it cannot force an 164// allocation. 165static size_t stream_index(yyjson_val *iv, size_t fallback) { 166 if (!yyjson_is_int(iv)) return fallback; 167 long i = (long)yyjson_get_int(iv); 168 return i < 0 || i > MAX_STREAM_INDEX ? fallback : (size_t)i; 169} 170 171static void on_data(const char *json, size_t len, void *user) { 172 resp_state *st = user; 173 yyjson_doc *doc = yyjson_read(json, len, 0); 174 if (!doc) { 175 if (!st->error) st->error = xstrdup("stream: invalid JSON chunk"); 176 return; 177 } 178 yyjson_val *root = yyjson_doc_get_root(doc); 179 yyjson_val *tv = yyjson_obj_get(root, "type"); 180 const char *type = yyjson_is_str(tv) ? yyjson_get_str(tv) : ""; 181 182 if (strcmp(type, "error") == 0) { 183 yyjson_val *m = yyjson_obj_get(root, "message"); 184 yyjson_val *e = yyjson_obj_get(root, "error"); 185 if (yyjson_is_str(m)) tiny_set_str(&st->error, yyjson_get_str(m)); 186 else if (yyjson_is_obj(e)) { 187 yyjson_val *em = yyjson_obj_get(e, "message"); 188 if (yyjson_is_str(em)) tiny_set_str(&st->error, yyjson_get_str(em)); 189 } 190 if (!st->error) st->error = xstrdup("stream: provider error"); 191 } else if (strcmp(type, "response.output_item.added") == 0) { 192 yyjson_val *item = yyjson_obj_get(root, "item"); 193 yyjson_val *it = yyjson_is_obj(item) ? yyjson_obj_get(item, "type") : NULL; 194 if (yyjson_is_str(it) && strcmp(yyjson_get_str(it), "function_call") == 0) { 195 yyjson_val *iv = yyjson_obj_get(root, "output_index"); 196 size_t index = stream_index(iv, st->calls_len); 197 grow_calls(st, index + 1); 198 call_acc *a = &st->calls[index]; 199 yyjson_val *id = yyjson_obj_get(item, "call_id"); 200 yyjson_val *nm = yyjson_obj_get(item, "name"); 201 if (yyjson_is_str(id)) tiny_set_str(&a->call_id, yyjson_get_str(id)); 202 if (yyjson_is_str(nm)) tiny_set_str(&a->name, yyjson_get_str(nm)); 203 if (!a->started && a->name && a->name[0]) { 204 a->started = true; 205 tiny_emit_tool_start(st->emit, st->user, a->call_id, a->name); 206 } 207 } 208 } else if (strcmp(type, "response.output_text.delta") == 0 || 209 strcmp(type, "response.refusal.delta") == 0) { 210 yyjson_val *d = yyjson_obj_get(root, "delta"); 211 if (yyjson_is_str(d) && yyjson_get_str(d)[0]) { 212 const char *s = yyjson_get_str(d); 213 buf_add(&st->text, s, strlen(s)); 214 tiny_emit_delta(st->emit, st->user, TINY_STREAM_TEXT_DELTA, s); 215 } 216 } else if (strcmp(type, "response.reasoning_summary_text.delta") == 0 || 217 strcmp(type, "response.reasoning_text.delta") == 0) { 218 yyjson_val *d = yyjson_obj_get(root, "delta"); 219 if (yyjson_is_str(d) && yyjson_get_str(d)[0]) { 220 const char *s = yyjson_get_str(d); 221 buf_add(&st->reasoning, s, strlen(s)); 222 tiny_emit_delta(st->emit, st->user, TINY_STREAM_REASONING_DELTA, s); 223 } 224 } else if (strcmp(type, "response.function_call_arguments.delta") == 0) { 225 yyjson_val *iv = yyjson_obj_get(root, "output_index"); 226 size_t index = stream_index(iv, st->calls_len); 227 if (index < st->calls_len) { 228 yyjson_val *d = yyjson_obj_get(root, "delta"); 229 if (yyjson_is_str(d)) buf_add(&st->calls[index].args, yyjson_get_str(d), strlen(yyjson_get_str(d))); 230 } 231 } else if (strcmp(type, "response.function_call_arguments.done") == 0) { 232 yyjson_val *iv = yyjson_obj_get(root, "output_index"); 233 size_t index = stream_index(iv, st->calls_len); 234 if (index < st->calls_len) { 235 yyjson_val *a = yyjson_obj_get(root, "arguments"); 236 if (yyjson_is_str(a)) { 237 st->calls[index].args.len = 0; 238 if (st->calls[index].args.data) st->calls[index].args.data[0] = '\0'; 239 buf_add(&st->calls[index].args, yyjson_get_str(a), strlen(yyjson_get_str(a))); 240 } 241 } 242 } else if (strcmp(type, "response.output_item.done") == 0) { 243 yyjson_val *item = yyjson_obj_get(root, "item"); 244 yyjson_val *it = yyjson_is_obj(item) ? yyjson_obj_get(item, "type") : NULL; 245 if (yyjson_is_str(it) && strcmp(yyjson_get_str(it), "function_call") == 0) { 246 yyjson_val *iv = yyjson_obj_get(root, "output_index"); 247 size_t index = stream_index(iv, st->calls_len); 248 if (index < st->calls_len) { 249 call_acc *a = &st->calls[index]; 250 yyjson_val *id = yyjson_obj_get(item, "call_id"); 251 yyjson_val *nm = yyjson_obj_get(item, "name"); 252 yyjson_val *ar = yyjson_obj_get(item, "arguments"); 253 if (yyjson_is_str(id)) tiny_set_str(&a->call_id, yyjson_get_str(id)); 254 if (yyjson_is_str(nm)) tiny_set_str(&a->name, yyjson_get_str(nm)); 255 if (!a->started && a->name && a->name[0]) { 256 a->started = true; 257 tiny_emit_tool_start(st->emit, st->user, a->call_id, a->name); 258 } 259 if (yyjson_is_str(ar) && !a->args.len) 260 buf_add(&a->args, yyjson_get_str(ar), strlen(yyjson_get_str(ar))); 261 } 262 } 263 } else if (strcmp(type, "response.completed") == 0 || 264 strcmp(type, "response.incomplete") == 0) { 265 yyjson_val *resp = yyjson_obj_get(root, "response"); 266 if (yyjson_is_obj(resp)) { 267 yyjson_val *sv = yyjson_obj_get(resp, "status"); 268 if (yyjson_is_str(sv)) tiny_set_str(&st->status, yyjson_get_str(sv)); 269 yyjson_val *det = yyjson_obj_get(resp, "incomplete_details"); 270 if (yyjson_is_obj(det)) { 271 yyjson_val *r = yyjson_obj_get(det, "reason"); 272 if (yyjson_is_str(r)) tiny_set_str(&st->incomplete, yyjson_get_str(r)); 273 } 274 yyjson_val *usage = yyjson_obj_get(resp, "usage"); 275 if (yyjson_is_obj(usage)) { 276 yyjson_val *v; 277 if (yyjson_is_num(v = yyjson_obj_get(usage, "input_tokens"))) st->tokens_in = (long)yyjson_get_num(v); 278 if (yyjson_is_num(v = yyjson_obj_get(usage, "output_tokens"))) st->tokens_out = (long)yyjson_get_num(v); 279 if (yyjson_is_num(v = yyjson_obj_get(usage, "total_tokens"))) st->tokens_total = (long)yyjson_get_num(v); 280 } 281 } 282 } else if (strcmp(type, "response.failed") == 0) { 283 yyjson_val *resp = yyjson_obj_get(root, "response"); 284 yyjson_val *err = yyjson_is_obj(resp) ? yyjson_obj_get(resp, "error") : NULL; 285 yyjson_val *m = yyjson_is_obj(err) ? yyjson_obj_get(err, "message") : NULL; 286 tiny_set_str(&st->error, yyjson_is_str(m) ? yyjson_get_str(m) : "response failed"); 287 } 288 yyjson_doc_free(doc); 289} 290 291static void free_state(resp_state *st) { 292 free(st->text.data); 293 free(st->reasoning.data); 294 for (size_t i = 0; i < st->calls_len; i++) { 295 free(st->calls[i].call_id); 296 free(st->calls[i].name); 297 free(st->calls[i].args.data); 298 } 299 free(st->calls); 300 free(st->status); 301 free(st->incomplete); 302 free(st->error); 303} 304 305static void finalize(resp_state *st, tiny_message *out) { 306 tiny_add_text_block(out, TINY_BLOCK_THINKING, st->reasoning.data); 307 tiny_add_text_block(out, TINY_BLOCK_TEXT, st->text.data); 308 for (size_t i = 0; i < st->calls_len; i++) { 309 call_acc *a = &st->calls[i]; 310 if (!a->name) continue; 311 tiny_add_tool_block(out, a->call_id, a->name, a->args.len ? a->args.data : NULL, st->emit, st->user); 312 } 313 out->tokens_in = st->tokens_in; 314 out->tokens_out = st->tokens_out; 315 out->tokens_total = st->tokens_total; 316 317 bool has_calls = out->blocks_len > 0 && out->blocks[out->blocks_len - 1].type == TINY_BLOCK_TOOL_CALL; 318 for (size_t i = 0; i < out->blocks_len && !has_calls; i++) 319 if (out->blocks[i].type == TINY_BLOCK_TOOL_CALL) has_calls = true; 320 321 if (st->status && strcmp(st->status, "incomplete") == 0) { 322 if (st->incomplete && strcmp(st->incomplete, "max_output_tokens") == 0) { 323 out->stop_reason = xstrdup("length"); 324 } else { 325 out->stop_reason = xstrdup("error"); 326 out->error_message = xstrdup(st->incomplete ? st->incomplete : "response incomplete"); 327 } 328 } else if (st->status && (strcmp(st->status, "failed") == 0 || strcmp(st->status, "cancelled") == 0)) { 329 out->stop_reason = xstrdup("error"); 330 out->error_message = xstrdup(st->error ? st->error : "response failed"); 331 } else if (has_calls) { 332 out->stop_reason = xstrdup("toolUse"); 333 } else { 334 out->stop_reason = xstrdup("stop"); 335 } 336} 337 338// ---- transport ------------------------------------------------------------ 339 340 341int tiny_openai_responses_stream(const tiny_model *model, const tiny_context *ctx, 342 const tiny_stream_opts *opts, 343 tiny_stream_fn emit, void *user, 344 const volatile bool *cancel, 345 tiny_message *out, char **err) { 346 if (err) *err = NULL; 347 memset(out, 0, sizeof *out); 348 out->role = TINY_ROLE_ASSISTANT; 349 350 char *url = tiny_url_endpoint(model->base_url, "responses"); 351 if (!url) return tiny_fail(err, "provider: no base url"); 352 char *body = build_request(model, ctx, opts); 353 if (!body) { 354 free(url); 355 return tiny_fail(err, "provider: cannot build request"); 356 } 357 358 resp_state st = {0}; 359 st.emit = emit; 360 st.user = user; 361 362 tiny_api_result r = tiny_api_post(url, body, "Authorization: Bearer ", 363 model->api_key, NULL, cancel, on_data, &st, st.error, err); 364 365 int rc = 0; 366 if (r == TINY_API_ABORTED) { 367 out->stop_reason = xstrdup("aborted"); 368 } else if (r == TINY_API_TRANSPORT_ERROR) { 369 rc = -1; 370 } else if (r == TINY_API_STREAM_ERROR) { 371 out->stop_reason = xstrdup("error"); 372 out->error_message = xstrdup(st.error); 373 } else { 374 finalize(&st, out); 375 } 376 free_state(&st); 377 return rc; 378}