Streaming: the OpenAI transport reads SSE over curl -N and reports deltas as they arrive. Text goes to the chat pane, a tool-call start to the sidebar, and a reasoning model's reasoning_content is shown dimmed above the answer instead of being hidden - which is what made a 143 s turn look like a dead terminal. Measured against kontron (Qwen3.6-35B, strata-poly): first byte at 11.6 s on a short question, text growing visibly in the TUI at 7 s and 17 s on a longer one. test_sse drives the accumulator with recorded lines: plain text, a tool call split over three chunks, two calls in one turn, a broken data line, keepalives. Twelve test binaries green. Defects found while proving it, each fixed or stated: - typed umlauts were dropped: the input loop accepted ASCII only. Every byte above 0x1f is kept now and the line is redrawn from the buffer. - setlocale() was never called, so curses had no encoding at all. - the vendored NetBSD curses emits Latin-1 bytes for wide characters (ü -> 0xFC) while ncurses passes UTF-8 through; proven with a raw pty dump of both. The UI therefore transliterates for display (gruen) and the conversation and session log keep the exact UTF-8. The library defect is written down, not papered over. - an answer longer than the screen was skipped by the viewport and the chat looked empty; draw_history now shows the bottom of the content, including entries taller than the window.
148 lines
4.4 KiB
C
148 lines
4.4 KiB
C
/*
|
|
* 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 <stdio.h>
|
|
#include <string.h>
|
|
|
|
#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);
|
|
}
|
|
|
|
/* ---- 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: 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;
|
|
}
|