Artifact c4b85dbb2ae3cc7dc6806ccc96e0e58efb743267f122bc1f605dc370f3e4a0f5:
- File
test/pic/decode_stream.c
— part of check-in
[55cff08c2c]
at
2026-11-19 00:56:53
on branch stage-4
— Add the Chat Completions adapter for OpenAI, OpenRouter and compatibles
A chunk decoder following Pi: text, reasoning from reasoning_content, reasoning or reasoning_text kept under its field, OpenRouter reasoning_details verbatim, tool calls by index, usage, finish_reason, and in-stream errors. Requests follow Pi too: system or developer role, tools as functions, tool messages, stream_options, store and reasoning by dialect. The retry loop and header assembly move into a shared module both adapters use. Recorded streams, a fuzz target and fake server cases cover it, including a conversation moving between the two APIs in both directions. (user: dmestas, size: 4388) [annotate] [blame] [check-ins using]
/* Decodes recorded SSE streams and prints a canonical summary. Each stream is * decoded whole, byte by byte and in 7-byte chunks; the three must agree. * Usage: decode_stream [--chat] FILE... (--chat: Chat Completions streams) */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "anthropic.h" #include "openai_chat.h" #include "sse.h" static int chat_mode; static char *summarize_chat(sqlite3 *db, const char *bytes, size_t n, size_t chunk) { pic_chat c; pic_sse p; char *out, *native = NULL, *neutral = NULL; size_t cap = 1u << 16; int rc = 0; if (pic_chat_init(&c, db) != 0 || pic_sse_init(&p, 1u << 20, 1u << 20, pic_chat_event, &c) != 0) { exit(2); } for (size_t i = 0; i < n && rc == 0; i += chunk) { rc = pic_sse_feed(&p, bytes + i, n - i < chunk ? n - i : chunk); } if (rc == 0 && pic_sse_pending(&p)) { rc = -1; (void)snprintf(c.err, sizeof c.err, "stream ended inside an event"); } else if (rc == 0) { rc = pic_chat_finish(&c); } else if (c.err[0] == '\0') { (void)snprintf(c.err, sizeof c.err, "SSE framing limit exceeded"); } if (rc == 0) { native = pic_chat_native(&c); neutral = pic_chat_neutral(&c); } out = malloc(cap); if (!out) { exit(2); } (void)snprintf(out, cap, "result: %s%s\nstop: %s\nusage: %s\nnative: %s\nneutral: %s\n", rc == 0 ? "ok" : "fail: ", rc == 0 ? "" : c.err, rc == 0 ? pic_chat_stop(&c) : "-", c.usage ? c.usage : "-", native ? native : "-", neutral ? neutral : "-"); free(native); free(neutral); pic_sse_free(&p); pic_chat_free(&c); return out; } static char *summarize(sqlite3 *db, const char *bytes, size_t n, size_t chunk) { if (chat_mode) { return summarize_chat(db, bytes, n, chunk); } pic_anthropic d; pic_sse p; char *out; size_t cap = 1u << 16; int rc = 0; if (pic_anthropic_init(&d, db) != 0 || pic_sse_init(&p, 1u << 20, 1u << 20, pic_anthropic_event, &d) != 0) { fprintf(stderr, "init failed\n"); exit(2); } for (size_t i = 0; i < n && rc == 0; i += chunk) { rc = pic_sse_feed(&p, bytes + i, n - i < chunk ? n - i : chunk); } if (rc == 0) { rc = pic_anthropic_finish(&d); } else if (d.err[0] == '\0') { (void)snprintf(d.err, sizeof d.err, "SSE framing limit exceeded"); } out = malloc(cap); if (!out) { exit(2); } (void)snprintf(out, cap, "result: %s%s\nid: %s\nmodel: %s\nstop: %s\nusage: %s\nerror: %s\ncontent: %s\n", rc == 0 ? "ok" : "fail: ", rc == 0 ? "" : d.err, d.msg.id ? d.msg.id : "-", d.msg.model ? d.msg.model : "-", d.msg.stop_reason ? d.msg.stop_reason : "-", d.msg.usage ? d.msg.usage : "-", d.msg.error ? d.msg.error : "-", d.msg.content ? d.msg.content : "-"); pic_sse_free(&p); pic_anthropic_free(&d); return out; } int main(int argc, char **argv) { sqlite3 *db; int first = 1; if (sqlite3_open(":memory:", &db) != SQLITE_OK) { return 2; } if (argc > 1 && strcmp(argv[1], "--chat") == 0) { chat_mode = 1; first = 2; } for (int i = first; i < argc; i++) { FILE *f = fopen(argv[i], "rb"); char *bytes, *whole, *bytewise, *chunked; long n; if (!f || fseek(f, 0, SEEK_END) != 0 || (n = ftell(f)) < 0 || fseek(f, 0, SEEK_SET) != 0) { fprintf(stderr, "cannot read %s\n", argv[i]); return 2; } bytes = malloc((size_t)n + 1); if (!bytes || fread(bytes, 1, (size_t)n, f) != (size_t)n) { return 2; } fclose(f); whole = summarize(db, bytes, (size_t)n, (size_t)n + 1); bytewise = summarize(db, bytes, (size_t)n, 1); chunked = summarize(db, bytes, (size_t)n, 7); if (strcmp(whole, bytewise) != 0 || strcmp(whole, chunked) != 0) { fprintf(stderr, "%s: result depends on chunking\n--- whole\n%s--- bytewise\n%s--- chunked\n%s", argv[i], whole, bytewise, chunked); return 1; } fputs(whole, stdout); free(bytes); free(whole); free(bytewise); free(chunked); } sqlite3_close(db); return 0; }