The sidebar counted characters and guessed tokens. The provider reports the real numbers in the final chunk (prompt_tokens/completion_tokens, or input_tokens/output_tokens), so both the streaming accumulator and the one-shot reply parser now keep them, the agent sums them per turn, and the sidebar shows '79 tok (in 344)' instead of an estimate. The estimate stays as the fallback for endpoints that report nothing, and it is still labelled as one.
265 lines
6.8 KiB
C
265 lines
6.8 KiB
C
/*
|
|
* splinter -- OpenAI-compatible request/reply shaping.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* Two pure functions, so both directions can be tested without a network:
|
|
*
|
|
* sp_openai_request() conversation + tool table -> the JSON body
|
|
* sp_openai_reply() the endpoint's JSON -> sp_reply
|
|
*
|
|
* What is deliberately not done here: no retries, no streaming, no guessing.
|
|
* A reply without a usable choice is an error with the provider's own message
|
|
* in it, because "the model said nothing" and "the endpoint is broken" are
|
|
* different facts and the caller has to be able to tell them apart.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "mcp.h"
|
|
|
|
static int
|
|
put_field(struct sp_buf *b, const char *key, const char *value)
|
|
{
|
|
if (sp_buf_printf(b, "\"%s\":", key) != 0 ||
|
|
sp_buf_puts(b, "\"") != 0 ||
|
|
sp_json_escape(b, value != NULL ? value : "") != 0 ||
|
|
sp_buf_puts(b, "\"") != 0)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
sp_openai_request(const struct sp_conv *conv, const char *model,
|
|
const struct sp_registry *reg, int streaming, struct sp_buf *out)
|
|
{
|
|
size_t i;
|
|
|
|
sp_buf_clear(out);
|
|
if (sp_buf_puts(out, "{\"model\":") != 0 || sp_buf_puts(out, "\"") != 0 ||
|
|
sp_json_escape(out, model != NULL ? model : "") != 0 ||
|
|
sp_buf_puts(out, streaming ? "\",\"stream\":true,\"messages\":["
|
|
: "\",\"stream\":false,\"messages\":[") != 0)
|
|
return -1;
|
|
|
|
for (i = 0; i < conv->n; i++) {
|
|
const struct sp_msg *m = &conv->v[i];
|
|
|
|
if (i > 0 && sp_buf_puts(out, ",") != 0)
|
|
return -1;
|
|
if (sp_buf_puts(out, "{") != 0 ||
|
|
put_field(out, "role", m->role) != 0)
|
|
return -1;
|
|
if (m->call_id != NULL) {
|
|
if (sp_buf_puts(out, ",") != 0 ||
|
|
put_field(out, "tool_call_id", m->call_id) != 0)
|
|
return -1;
|
|
}
|
|
if (m->name != NULL && m->call_id == NULL) {
|
|
if (sp_buf_puts(out, ",") != 0 ||
|
|
put_field(out, "name", m->name) != 0)
|
|
return -1;
|
|
}
|
|
if (m->content != NULL) {
|
|
if (sp_buf_puts(out, ",") != 0 ||
|
|
put_field(out, "content", m->content) != 0)
|
|
return -1;
|
|
}
|
|
if (m->calls != NULL && *m->calls != '\0') {
|
|
if (sp_buf_printf(out, ",\"tool_calls\":%s", m->calls) != 0)
|
|
return -1;
|
|
}
|
|
if (sp_buf_puts(out, "}") != 0)
|
|
return -1;
|
|
}
|
|
|
|
/* tools: name, description and an open parameter object */
|
|
if (sp_buf_puts(out, "],\"tools\":[") != 0)
|
|
return -1;
|
|
for (i = 0; i < reg->len; i++) {
|
|
const struct sp_tool *t = ®->tools[i];
|
|
|
|
if (i > 0 && sp_buf_puts(out, ",") != 0)
|
|
return -1;
|
|
if (sp_buf_puts(out, "{\"type\":\"function\",\"function\":{") != 0 ||
|
|
put_field(out, "name", t->name) != 0 ||
|
|
sp_buf_puts(out, ",") != 0 ||
|
|
put_field(out, "description", t->description) != 0 ||
|
|
sp_buf_puts(out, ",\"parameters\":") != 0)
|
|
return -1;
|
|
/* a tool without a schema gets an open object, not an empty one:
|
|
* an empty properties map tells the model "no arguments exist" */
|
|
if (t->params != NULL) {
|
|
if (sp_buf_puts(out, t->params) != 0)
|
|
return -1;
|
|
} else if (sp_buf_puts(out, "{\"type\":\"object\","
|
|
"\"properties\":{},\"additionalProperties\":true}") != 0) {
|
|
return -1;
|
|
}
|
|
if (sp_buf_puts(out, "}}") != 0)
|
|
return -1;
|
|
}
|
|
return sp_buf_puts(out, "]}");
|
|
}
|
|
|
|
/*
|
|
* Extract the raw tool_calls array as text, so a later request can hand the
|
|
* provider back exactly what it sent: re-encoding parsed calls would be a
|
|
* second, silently different JSON.
|
|
*/
|
|
static char *
|
|
raw_calls(const char *json, const char *key)
|
|
{
|
|
const char *p = strstr(json, key);
|
|
const char *start;
|
|
int depth = 0, in_str = 0, esc = 0;
|
|
size_t len;
|
|
|
|
if (p == NULL)
|
|
return NULL;
|
|
start = strchr(p + strlen(key), '[');
|
|
if (start == NULL)
|
|
return NULL;
|
|
for (p = start; *p != '\0'; p++) {
|
|
if (esc) {
|
|
esc = 0;
|
|
continue;
|
|
}
|
|
if (*p == '\\') {
|
|
esc = 1;
|
|
continue;
|
|
}
|
|
if (*p == '"') {
|
|
in_str = !in_str;
|
|
continue;
|
|
}
|
|
if (in_str)
|
|
continue;
|
|
if (*p == '[')
|
|
depth++;
|
|
else if (*p == ']') {
|
|
depth--;
|
|
if (depth == 0) {
|
|
len = (size_t)(p - start) + 1;
|
|
return strndup(start, len);
|
|
}
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static const char *
|
|
jstr(const mcp_json *v)
|
|
{
|
|
return v != NULL ? mcp_json_string(v) : NULL;
|
|
}
|
|
|
|
int
|
|
sp_openai_reply(const char *json, struct sp_reply *out, struct sp_buf *err)
|
|
{
|
|
mcp_json *v;
|
|
const mcp_json *choices, *choice, *message, *calls, *content;
|
|
const mcp_json *error;
|
|
const char *text;
|
|
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
|
|
if (json == NULL || *json == '\0') {
|
|
sp_buf_puts(err, "openai: empty body");
|
|
return -1;
|
|
}
|
|
v = mcp_parse(json);
|
|
if (v == NULL) {
|
|
sp_buf_printf(err, "openai: reply is not JSON: %.120s", json);
|
|
return -1;
|
|
}
|
|
|
|
error = mcp_arg(v, "error");
|
|
if (error != NULL) {
|
|
text = mcp_arg_string(error, "message");
|
|
sp_buf_printf(err, "openai: provider error: %s",
|
|
text != NULL ? text : "(no message)");
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
|
|
choices = mcp_arg(v, "choices");
|
|
if (choices == NULL || mcp_array_len(choices) == 0) {
|
|
sp_buf_printf(err, "openai: no choices in reply: %.160s", json);
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
choice = mcp_array_at(choices, 0);
|
|
message = choice != NULL ? mcp_arg(choice, "message") : NULL;
|
|
if (message == NULL) {
|
|
sp_buf_puts(err, "openai: choice without a message");
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
|
|
content = mcp_arg(message, "content");
|
|
out->content = strdup(jstr(content) != NULL ? jstr(content) : "");
|
|
if (out->content == NULL) {
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
|
|
{
|
|
const mcp_json *usage = mcp_arg(v, "usage");
|
|
|
|
if (usage != NULL) {
|
|
long pin = (long)mcp_arg_int(usage, "prompt_tokens", -1);
|
|
long pout = (long)mcp_arg_int(usage, "completion_tokens",
|
|
-1);
|
|
|
|
if (pin < 0)
|
|
pin = (long)mcp_arg_int(usage, "input_tokens", -1);
|
|
if (pout < 0)
|
|
pout = (long)mcp_arg_int(usage, "output_tokens", -1);
|
|
if (pin >= 0)
|
|
out->tokens_in = pin;
|
|
if (pout >= 0)
|
|
out->tokens_out = pout;
|
|
}
|
|
}
|
|
|
|
calls = mcp_arg(message, "tool_calls");
|
|
if (calls != NULL && mcp_array_len(calls) > 0) {
|
|
size_t i, n = mcp_array_len(calls);
|
|
|
|
out->calls = calloc(n, sizeof(*out->calls));
|
|
if (out->calls == NULL) {
|
|
sp_reply_free(out);
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
for (i = 0; i < n; i++) {
|
|
const mcp_json *c = mcp_array_at(calls, i);
|
|
const mcp_json *fn = mcp_arg(c, "function");
|
|
|
|
out->calls[i].id = strdup(jstr(mcp_arg(c, "id")) != NULL ?
|
|
jstr(mcp_arg(c, "id")) : "");
|
|
out->calls[i].name = strdup(jstr(mcp_arg(fn, "name")) !=
|
|
NULL ? jstr(mcp_arg(fn, "name")) : "");
|
|
out->calls[i].args = strdup(jstr(mcp_arg(fn, "arguments")) !=
|
|
NULL ? jstr(mcp_arg(fn, "arguments")) : "");
|
|
if (out->calls[i].id == NULL || out->calls[i].name == NULL ||
|
|
out->calls[i].args == NULL) {
|
|
out->ncalls = i + 1;
|
|
sp_reply_free(out);
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
}
|
|
out->ncalls = n;
|
|
out->calls_json = raw_calls(json, "\"tool_calls\"");
|
|
}
|
|
|
|
mcp_json_free(v);
|
|
return 0;
|
|
}
|