@@ -2,7 +2,6 @@
22
33 #include "api.h"
44
5-#include <stdarg.h>
65 #include <stdio.h>
76 #include <stdlib.h>
87 #include <string.h>
@@ -10,51 +9,10 @@
109 #include <yyjson.h>
1110
1211 #include "http.h"
12+#include "util.h"
1313
1414 #define ANTHROPIC_VERSION "2023-06-01"
1515
16-static void *xmalloc(size_t n) {
17- void *p = malloc(n);
18- if (!p) abort();
19- return p;
20-}
21-
22-static char *xstrdup(const char *s) {
23- size_t n = strlen(s) + 1;
24- char *p = xmalloc(n);
25- memcpy(p, s, n);
26- return p;
27-}
28-
29-static int fail(char **err, const char *fmt, ...) {
30- char detail[512];
31- va_list ap;
32- va_start(ap, fmt);
33- vsnprintf(detail, sizeof detail, fmt, ap);
34- va_end(ap);
35- *err = xstrdup(detail);
36- return -1;
37-}
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 char *p, size_t n) {
46- if (b->len + n + 1 > b->cap) {
47- size_t cap = b->cap ? b->cap : 64;
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-
5816 // ---- request --------------------------------------------------------------
5917
6018 static void add_image_block(yyjson_mut_doc *doc, yyjson_mut_val *content,
@@ -84,7 +42,7 @@ static void add_tool_result(yyjson_mut_doc *doc, yyjson_mut_val *messages, const
8442
8543 bool has_images = cage_message_has_image(m);
8644 if (!has_images) {
87- buf t = {0};
45+ cage_buf t = {0};
8846 for (size_t i = 0; i < m->blocks_len; i++)
8947 if (m->blocks[i].type == CAGE_BLOCK_TEXT && m->blocks[i].text)
9048 buf_add(&t, m->blocks[i].text, strlen(m->blocks[i].text));
@@ -149,15 +107,7 @@ static void add_tools(yyjson_mut_doc *doc, yyjson_mut_val *root, const cage_cont
149107 const cage_tool *t = &ctx->tools[i];
150108 yyjson_mut_val *tool = yyjson_mut_obj(doc);
151109 yyjson_mut_arr_add_val(tools, tool);
152- yyjson_mut_obj_add_strcpy(doc, tool, "name", t->name);
153- yyjson_mut_obj_add_strcpy(doc, tool, "description", t->description ? t->description : "");
154- const char *params = t->parameters ? t->parameters : "{}";
155- yyjson_doc *pdoc = yyjson_read(params, strlen(params), 0);
156- if (pdoc) {
157- yyjson_mut_val *copy = yyjson_val_mut_copy(doc, yyjson_doc_get_root(pdoc));
158- yyjson_mut_obj_add_val(doc, tool, "input_schema", copy);
159- yyjson_doc_free(pdoc);
160- }
110+ cage_json_tool(doc, tool, t, "input_schema");
161111 }
162112 }
163113
@@ -224,10 +174,10 @@ enum { BLK_TEXT, BLK_THINKING, BLK_TOOL };
224174
225175 typedef struct {
226176 int type;
227- buf text;
177+ cage_buf text;
228178 char *id;
229179 char *name;
230- buf args;
180+ cage_buf args;
231181 } block_acc;
232182
233183 typedef struct {
@@ -241,12 +191,6 @@ typedef struct {
241191 void *user;
242192 } anth_state;
243193
244-static void emit_delta(anth_state *st, cage_stream_type type, const char *delta) {
245- if (!st->emit) return;
246- cage_stream_event ev = {.type = type, .delta = delta};
247- st->emit(&ev, st->user);
248-}
249-
250194 static block_acc *block_at(anth_state *st, size_t index) {
251195 if (index >= st->blocks_len) {
252196 st->blocks = realloc(st->blocks, (index + 1) * sizeof *st->blocks);
@@ -257,12 +201,8 @@ static block_acc *block_at(anth_state *st, size_t index) {
257201 return &st->blocks[index];
258202 }
259203
260-static void set_str(char **dst, const char *src) {
261- free(*dst);
262- *dst = xstrdup(src ? src : "");
263-}
264-
265-static void handle_data(anth_state *st, const char *json, size_t len) {
204+static void on_data(const char *json, size_t len, void *user) {
205+ anth_state *st = user;
266206 yyjson_doc *doc = yyjson_read(json, len, 0);
267207 if (!doc) {
268208 if (!st->error) st->error = xstrdup("stream: invalid JSON chunk");
@@ -275,7 +215,7 @@ static void handle_data(anth_state *st, const char *json, size_t len) {
275215 if (strcmp(type, "error") == 0) {
276216 yyjson_val *err = yyjson_obj_get(root, "error");
277217 yyjson_val *m = yyjson_is_obj(err) ? yyjson_obj_get(err, "message") : NULL;
278- set_str(&st->error, yyjson_is_str(m) ? yyjson_get_str(m) : "stream: provider error");
218+ cage_set_str(&st->error, yyjson_is_str(m) ? yyjson_get_str(m) : "stream: provider error");
279219 } else if (strcmp(type, "message_start") == 0) {
280220 yyjson_val *msg = yyjson_obj_get(root, "message");
281221 yyjson_val *usage = yyjson_is_obj(msg) ? yyjson_obj_get(msg, "usage") : NULL;
@@ -298,8 +238,8 @@ static void handle_data(anth_state *st, const char *json, size_t len) {
298238 b->type = BLK_TOOL;
299239 yyjson_val *id = yyjson_obj_get(cb, "id");
300240 yyjson_val *nm = yyjson_obj_get(cb, "name");
301- if (yyjson_is_str(id)) set_str(&b->id, yyjson_get_str(id));
302- if (yyjson_is_str(nm)) set_str(&b->name, yyjson_get_str(nm));
241+ if (yyjson_is_str(id)) cage_set_str(&b->id, yyjson_get_str(id));
242+ if (yyjson_is_str(nm)) cage_set_str(&b->name, yyjson_get_str(nm));
303243 } else if (strcmp(bts, "thinking") == 0 || strcmp(bts, "redacted_thinking") == 0) {
304244 b->type = BLK_THINKING;
305245 } else {
@@ -324,13 +264,13 @@ static void handle_data(anth_state *st, const char *json, size_t len) {
324264 yyjson_val *t = yyjson_obj_get(d, "text");
325265 if (yyjson_is_str(t) && yyjson_get_str(t)[0]) {
326266 buf_add(&b->text, yyjson_get_str(t), strlen(yyjson_get_str(t)));
327- emit_delta(st, CAGE_STREAM_TEXT_DELTA, yyjson_get_str(t));
267+ cage_emit_delta(st->emit, st->user, CAGE_STREAM_TEXT_DELTA, yyjson_get_str(t));
328268 }
329269 } else if (strcmp(dts, "thinking_delta") == 0) {
330270 yyjson_val *t = yyjson_obj_get(d, "thinking");
331271 if (yyjson_is_str(t) && yyjson_get_str(t)[0]) {
332272 buf_add(&b->text, yyjson_get_str(t), strlen(yyjson_get_str(t)));
333- emit_delta(st, CAGE_STREAM_REASONING_DELTA, yyjson_get_str(t));
273+ cage_emit_delta(st->emit, st->user, CAGE_STREAM_REASONING_DELTA, yyjson_get_str(t));
334274 }
335275 } else if (strcmp(dts, "input_json_delta") == 0) {
336276 yyjson_val *p = yyjson_obj_get(d, "partial_json");
@@ -339,7 +279,7 @@ static void handle_data(anth_state *st, const char *json, size_t len) {
339279 } else if (strcmp(type, "message_delta") == 0) {
340280 yyjson_val *d = yyjson_obj_get(root, "delta");
341281 yyjson_val *sr = yyjson_is_obj(d) ? yyjson_obj_get(d, "stop_reason") : NULL;
342- if (yyjson_is_str(sr)) set_str(&st->stop_reason, yyjson_get_str(sr));
282+ if (yyjson_is_str(sr)) cage_set_str(&st->stop_reason, yyjson_get_str(sr));
343283 yyjson_val *usage = yyjson_obj_get(root, "usage");
344284 if (yyjson_is_obj(usage)) {
345285 yyjson_val *v;
@@ -349,10 +289,6 @@ static void handle_data(anth_state *st, const char *json, size_t len) {
349289 yyjson_doc_free(doc);
350290 }
351291
352-static void on_data(const char *json, size_t len, void *user) {
353- handle_data(user, json, len);
354-}
355-
356292 static void free_state(anth_state *st) {
357293 for (size_t i = 0; i < st->blocks_len; i++) {
358294 free(st->blocks[i].text.data);
@@ -379,30 +315,15 @@ static void finalize(anth_state *st, cage_message *out) {
379315 }
380316 for (size_t i = 0; i < st->blocks_len; i++) {
381317 block_acc *b = &st->blocks[i];
382- if (b->type == BLK_THINKING && b->text.len > 0) {
383- cage_block *blk = cage_message_add_block(out, CAGE_BLOCK_THINKING);
384- blk->text = xstrdup(b->text.data);
385- } else if (b->type == BLK_TEXT && b->text.len > 0) {
386- cage_block *blk = cage_message_add_block(out, CAGE_BLOCK_TEXT);
387- blk->text = xstrdup(b->text.data);
388- }
318+ if (b->type == BLK_THINKING)
319+ cage_add_text_block(out, CAGE_BLOCK_THINKING, b->text.data);
320+ else if (b->type == BLK_TEXT)
321+ cage_add_text_block(out, CAGE_BLOCK_TEXT, b->text.data);
389322 }
390323 for (size_t i = 0; i < st->blocks_len; i++) {
391324 block_acc *b = &st->blocks[i];
392325 if (b->type != BLK_TOOL) continue;
393- cage_block *blk = cage_message_add_block(out, CAGE_BLOCK_TOOL_CALL);
394- blk->tool_call_id = xstrdup(b->id ? b->id : "");
395- blk->tool_name = xstrdup(b->name ? b->name : "");
396- blk->arguments = xstrdup(b->args.len ? b->args.data : "{}");
397- if (st->emit) {
398- cage_stream_event ev = {
399- .type = CAGE_STREAM_TOOL_CALL,
400- .tool_call_id = blk->tool_call_id,
401- .tool_name = blk->tool_name,
402- .tool_arguments = blk->arguments,
403- };
404- st->emit(&ev, st->user);
405- }
326+ cage_add_tool_block(out, b->id, b->name, b->args.len ? b->args.data : NULL, st->emit, st->user);
406327 }
407328 out->tokens_in = st->tokens_in;
408329 out->tokens_out = st->tokens_out;
@@ -412,16 +333,6 @@ static void finalize(anth_state *st, cage_message *out) {
412333
413334 // ---- transport ------------------------------------------------------------
414335
415-static char *endpoint_url(const char *base) {
416- if (!base || !base[0]) return NULL;
417- size_t n = strlen(base);
418- const char *path = "v1/messages";
419- bool slash = base[n - 1] == '/';
420- size_t size = n + strlen(path) + 2;
421- char *url = xmalloc(size);
422- snprintf(url, size, slash ? "%s%s" : "%s/%s", base, path);
423- return url;
424-}
425336
426337 int cage_anthropic_messages_stream(const cage_model *model, const cage_context *ctx,
427338 const cage_stream_opts *opts,
@@ -432,59 +343,33 @@ int cage_anthropic_messages_stream(const cage_model *model, const cage_context *
432343 memset(out, 0, sizeof *out);
433344 out->role = CAGE_ROLE_ASSISTANT;
434345
435- char *url = endpoint_url(model->base_url);
436- if (!url) return fail(err, "provider: no base url");
346+ char *url = cage_url_endpoint(model->base_url, "v1/messages");
347+ if (!url) return cage_fail(err, "provider: no base url");
437348 char *body = build_request(model, ctx, opts);
438349 if (!body) {
439350 free(url);
440- return fail(err, "provider: cannot build request");
351+ return cage_fail(err, "provider: cannot build request");
441352 }
442353
443354 anth_state st = {0};
444355 st.emit = emit;
445356 st.user = user;
446357
447- char *auth = NULL;
448- struct curl_slist *headers = NULL;
449- headers = curl_slist_append(headers, "Content-Type: application/json");
450- headers = curl_slist_append(headers, "Accept: text/event-stream");
451- headers = curl_slist_append(headers, "anthropic-version: " ANTHROPIC_VERSION);
452- if (model->api_key && model->api_key[0]) {
453- size_t n = strlen(model->api_key) + 12;
454- auth = xmalloc(n);
455- snprintf(auth, n, "x-api-key: %s", model->api_key);
456- headers = curl_slist_append(headers, auth);
457- }
458-
459- cage_http_request req = {
460- .url = url,
461- .body = body,
462- .headers = headers,
463- .cancel = cancel,
464- .on_data = on_data,
465- .user = &st,
466- };
467- cage_http_stream(&req);
358+ const char *extra[] = {"anthropic-version: " ANTHROPIC_VERSION, NULL};
359+ cage_api_result r = cage_api_post(url, body, "x-api-key: ", model->api_key,
360+ extra, cancel, on_data, &st, st.error, err);
468361
469362 int rc = 0;
470- if (req.aborted) {
363+ if (r == CAGE_API_ABORTED) {
471364 out->stop_reason = xstrdup("aborted");
472- } else if (req.error) {
473- *err = req.error;
474- req.error = NULL;
365+ } else if (r == CAGE_API_TRANSPORT_ERROR) {
475366 rc = -1;
476- } else if (st.error) {
367+ } else if (r == CAGE_API_STREAM_ERROR) {
477368 out->stop_reason = xstrdup("error");
478369 out->error_message = xstrdup(st.error);
479370 } else {
480371 finalize(&st, out);
481372 }
482-
483- curl_slist_free_all(headers);
484- free(auth);
485- free(req.error);
486- free(url);
487- free(body);
488373 free_state(&st);
489374 return rc;
490375 }