The screen had no room for the numbers that say how a turn is doing, and two older behaviours still hurt: scrolling to the top made PgDn jump back to the top, and a catalogue's steer mark without its trailing space glued the typed text to ">". - draw_statusbar() in curses.c: one row above the prompt with ctx used/max + [|||| ] gauge, cache share and tok/s. The history and sidebar lose one row; the prompt stays the bottom row. The bar is drawn on every idle tick, during a turn and in --attach. - ctx: optional field on a provider (and in core) in tokens; the engine reports it in struct sp_engine_state. SPLINTER_CTX_MAX still overrides it. - cache: sp_reply gained tokens_cached. OpenAI reads prompt_tokens_details.cached_tokens / prompt_cache_hit_tokens, Anthropic reads cache_read_input_tokens and counts cache reads+writes in the prompt total. sp_agent_stats keeps the last request's prompt and cache share; the engine keeps the turn's output tokens for a real tok/s. - scroll: grow scroll by the growth of maxscroll, not by the distance from the first visible line. At the top that distance was 0, so every frame re-added the whole scrollback and PgDn could not leave the top. - steer mark: append the missing space if a catalogue omits it. - protocol state carries ctx_used/ctx_max/cached/turn_tokens_out, so the attached UI shows the same bar. - tests: cache parsing in test_sse/test_openai/test_anthropic, ctx parsing in test_confblock, and test_ui checks the bar, the cache share and the PgUp-to-top/PgDn-down path. make check: all tests passed.
157 lines
4.9 KiB
C
157 lines
4.9 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: 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;
|
|
}
|