/* * splinter -- OpenAI request/reply shaping, without a network. * * The request is checked by looking for the fields a provider needs; the reply * parser is fed real-shaped bodies, including the failing shapes. Nothing here * touches a socket, so a red test means the JSON is wrong, not the endpoint. */ #include #include #include "splinter.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static int echo_tool(const char *args, struct sp_buf *out, void *ctx) { (void)ctx; return sp_buf_puts(out, args); } int main(void) { struct sp_conv conv; struct sp_registry reg; struct sp_buf req, err; struct sp_reply rep; struct sp_tool t; sp_conv_init(&conv); memset(®, 0, sizeof(reg)); memset(&t, 0, sizeof(t)); t.name = "echo"; t.description = "echo the argument"; t.auth = SP_AUTH_READ; t.run = echo_tool; (void)sp_registry_add(®, &t); if (sp_buf_init(&req) != 0 || sp_buf_init(&err) != 0) return 1; /* ---- request ---- */ CHECK(sp_conv_push(&conv, "system", NULL, "be brief") == 0); CHECK(sp_conv_push(&conv, "user", NULL, "say \"hi\"") == 0); CHECK(sp_openai_request(&conv, "m1", ®, 0, &req) == 0); CHECK(strstr(req.p, "\"model\":\"m1\"") != NULL); CHECK(strstr(req.p, "\"role\":\"system\"") != NULL); CHECK(strstr(req.p, "\"content\":\"say \\\"hi\\\"\"") != NULL); CHECK(strstr(req.p, "\"tools\":[") != NULL); CHECK(strstr(req.p, "\"name\":\"echo\"") != NULL); CHECK(strstr(req.p, "\"stream\":false") != NULL); /* streaming switches the flag, nothing else */ CHECK(sp_openai_request(&conv, "m1", ®, 1, &req) == 0); CHECK(strstr(req.p, "\"stream\":true") != NULL); /* tool messages carry the call id, and nothing else claims to */ { struct sp_msg m; memset(&m, 0, sizeof(m)); m.role = "tool"; m.name = "echo"; m.call_id = "call-1"; m.content = "pong"; CHECK(sp_conv_add(&conv, &m) == 0); CHECK(sp_openai_request(&conv, "m1", ®, 0, &req) == 0); CHECK(strstr(req.p, "\"tool_call_id\":\"call-1\"") != NULL); } /* an assistant message that asked for tools hands them back verbatim */ { struct sp_msg m; memset(&m, 0, sizeof(m)); m.role = "assistant"; m.content = ""; m.calls = "[{\"id\":\"call-1\",\"type\":\"function\"," "\"function\":{\"name\":\"echo\",\"arguments\":\"{}\"}}]"; CHECK(sp_conv_add(&conv, &m) == 0); CHECK(sp_openai_request(&conv, "m1", ®, 0, &req) == 0); CHECK(strstr(req.p, "\"tool_calls\":[{\"id\":\"call-1\"") != NULL); } /* * Anthropic-shaped calls in history (after /model) are converted: * OpenAI refuses {"id","name","input"} with a 400. */ { struct sp_conv c2; struct sp_msg m; struct sp_registry none; sp_conv_init(&c2); memset(&none, 0, sizeof(none)); memset(&m, 0, sizeof(m)); m.role = "assistant"; m.content = ""; m.calls = "[{\"id\":\"toolu_1\",\"name\":\"echo\"," "\"input\":{\"text\":\"x\"}}]"; CHECK(sp_conv_add(&c2, &m) == 0); CHECK(sp_openai_request(&c2, "m1", ®, 0, &req) == 0); CHECK(strstr(req.p, "\"tool_calls\":[{\"id\":\"toolu_1\"," "\"type\":\"function\",\"function\":{\"name\":\"echo\"," "\"arguments\":\"{\\\"text\\\":\\\"x\\\"}\"}}]") != NULL); CHECK(strstr(req.p, "\"input\"") == NULL); /* no tools: no "tools":[] (OpenAI answers 400) */ CHECK(sp_openai_request(&c2, "m1", &none, 0, &req) == 0); CHECK(strstr(req.p, "\"tools\"") == NULL); CHECK(req.p[req.len - 1] == '}'); sp_conv_free(&c2); } /* ---- reply: finish_reason "tool_calls" before the key ---- */ CHECK(sp_openai_reply("{\"choices\":[{\"finish_reason\":\"tool_calls\"," "\"logprobs\":[1],\"message\":{\"content\":null,\"tool_calls\":[{\"id\":\"c7\"," "\"type\":\"function\",\"function\":{\"name\":\"echo\"," "\"arguments\":\"{}\"}}]}}]}", &rep, &err) == 0); CHECK(rep.calls_json != NULL && strncmp(rep.calls_json, "[{\"id\":\"c7\"", 11) == 0); sp_reply_free(&rep); /* ---- reply: plain answer ---- */ CHECK(sp_openai_reply("{\"choices\":[{\"message\":{\"role\":\"assistant\"," "\"content\":\"fertig\"}}]}", &rep, &err) == 0); CHECK(strcmp(rep.content, "fertig") == 0); CHECK(rep.ncalls == 0); CHECK(rep.calls_json == NULL); sp_reply_free(&rep); /* ---- reply: usage, including prompt tokens the cache served ---- */ CHECK(sp_openai_reply("{\"choices\":[{\"message\":{\"role\":\"assistant\"," "\"content\":\"ok\"}}],\"usage\":{\"prompt_tokens\":120," "\"completion_tokens\":8,\"prompt_tokens_details\":" "{\"cached_tokens\":96}}}", &rep, &err) == 0); CHECK(rep.tokens_in == 120 && rep.tokens_out == 8); CHECK(rep.tokens_cached == 96); sp_reply_free(&rep); /* ---- reply: a tool call ---- */ CHECK(sp_openai_reply("{\"choices\":[{\"message\":{\"role\":\"assistant\"," "\"content\":null,\"tool_calls\":[{\"id\":\"call-9\",\"type\":" "\"function\",\"function\":{\"name\":\"echo\",\"arguments\":" "\"{\\\"text\\\":\\\"x\\\"}\"}}]}}]}", &rep, &err) == 0); CHECK(rep.ncalls == 1); CHECK(strcmp(rep.calls[0].id, "call-9") == 0); CHECK(strcmp(rep.calls[0].name, "echo") == 0); CHECK(strstr(rep.calls[0].args, "text") != NULL); CHECK(rep.calls_json != NULL); CHECK(strstr(rep.calls_json, "\"call-9\"") != NULL); sp_reply_free(&rep); /* ---- reply: content null but no calls is still an answer, empty ---- */ CHECK(sp_openai_reply("{\"choices\":[{\"message\":{\"content\":null}}]}", &rep, &err) == 0); CHECK(rep.content != NULL); CHECK(strcmp(rep.content, "") == 0); CHECK(rep.ncalls == 0); sp_reply_free(&rep); /* ---- the failing shapes say what failed ---- */ sp_buf_clear(&err); CHECK(sp_openai_reply("{\"error\":{\"message\":\"model not found\"}}", &rep, &err) == -1); CHECK(strstr(err.p, "model not found") != NULL); sp_buf_clear(&err); CHECK(sp_openai_reply("{\"choices\":[]}", &rep, &err) == -1); CHECK(strstr(err.p, "no choices") != NULL); sp_buf_clear(&err); CHECK(sp_openai_reply("not json at all", &rep, &err) == -1); CHECK(strstr(err.p, "not JSON") != NULL); sp_buf_clear(&err); CHECK(sp_openai_reply("", &rep, &err) == -1); CHECK(strstr(err.p, "empty body") != NULL); sp_buf_free(&req); sp_buf_free(&err); sp_conv_free(&conv); sp_registry_free(®); if (failures == 0) printf("test_openai: ok\n"); return failures == 0 ? 0 : 1; }