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.
297 lines
6.9 KiB
C
297 lines
6.9 KiB
C
/*
|
|
* splinter -- SSE accumulation for streaming chat completions.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* OpenAI streams a turn as `data: {json}` lines. Text arrives in
|
|
* `choices[0].delta.content` chunks; a tool call arrives in pieces: the name
|
|
* once, the arguments as a growing string, keyed by an index. This file turns
|
|
* that into one reply, and it does it one line at a time so the whole thing is
|
|
* testable with recorded lines and no socket.
|
|
*
|
|
* Anything it does not understand is reported with the line quoted. A silent
|
|
* skip here would mean a turn that ends with no answer and no reason.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "mcp.h"
|
|
|
|
#define MAX_CALLS 8
|
|
|
|
struct sse_call {
|
|
char *id;
|
|
char *name;
|
|
struct sp_buf args;
|
|
int seen;
|
|
};
|
|
|
|
struct sp_sse {
|
|
struct sp_buf content;
|
|
struct sp_buf think;
|
|
struct sse_call calls[MAX_CALLS];
|
|
size_t ncalls;
|
|
int done;
|
|
};
|
|
|
|
struct sp_sse *
|
|
sp_sse_new(void)
|
|
{
|
|
struct sp_sse *s = calloc(1, sizeof(*s));
|
|
|
|
if (s == NULL)
|
|
return NULL;
|
|
if (sp_buf_init(&s->content) != 0 || sp_buf_init(&s->think) != 0) {
|
|
free(s);
|
|
return NULL;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
void
|
|
sp_sse_free(struct sp_sse *s)
|
|
{
|
|
size_t i;
|
|
|
|
if (s == NULL)
|
|
return;
|
|
for (i = 0; i < s->ncalls; i++) {
|
|
free(s->calls[i].id);
|
|
free(s->calls[i].name);
|
|
sp_buf_free(&s->calls[i].args);
|
|
}
|
|
sp_buf_free(&s->content);
|
|
sp_buf_free(&s->think);
|
|
free(s);
|
|
}
|
|
|
|
int
|
|
sp_sse_done(const struct sp_sse *s)
|
|
{
|
|
return s != NULL ? s->done : 0;
|
|
}
|
|
|
|
const char *
|
|
sp_sse_tool_start(const struct sp_sse *s, size_t index)
|
|
{
|
|
if (s == NULL || index >= s->ncalls)
|
|
return NULL;
|
|
return s->calls[index].name;
|
|
}
|
|
|
|
static struct sse_call *
|
|
call_slot(struct sp_sse *s, size_t index)
|
|
{
|
|
if (index >= MAX_CALLS)
|
|
return NULL;
|
|
if (index >= s->ncalls) {
|
|
size_t i;
|
|
|
|
for (i = s->ncalls; i <= index; i++) {
|
|
memset(&s->calls[i], 0, sizeof(s->calls[i]));
|
|
if (sp_buf_init(&s->calls[i].args) != 0)
|
|
return NULL;
|
|
}
|
|
s->ncalls = index + 1;
|
|
}
|
|
return &s->calls[index];
|
|
}
|
|
|
|
int
|
|
sp_sse_line(struct sp_sse *s, const char *line, struct sp_buf *out,
|
|
struct sp_buf *err)
|
|
{
|
|
mcp_json *v;
|
|
const mcp_json *choices, *choice, *delta, *content, *tcs;
|
|
int produced = 0;
|
|
const char *payload;
|
|
|
|
if (s == NULL)
|
|
return -1;
|
|
while (*line == ' ' || *line == '\t')
|
|
line++;
|
|
if (*line == '\0' || *line == ':' || *line == '\n')
|
|
return 0;
|
|
if (strncmp(line, "data:", 5) != 0) {
|
|
/* event:/id:/retry: carry nothing we need */
|
|
return 0;
|
|
}
|
|
payload = line + 5;
|
|
while (*payload == ' ')
|
|
payload++;
|
|
if (strncmp(payload, "[DONE]", 6) == 0) {
|
|
s->done = 1;
|
|
return 0;
|
|
}
|
|
|
|
v = mcp_parse(payload);
|
|
if (v == NULL) {
|
|
sp_buf_printf(err, "sse: unparseable data line: %.200s", payload);
|
|
return -1;
|
|
}
|
|
|
|
choices = mcp_arg(v, "choices");
|
|
if (choices == NULL || mcp_array_len(choices) == 0) {
|
|
/* keepalive and usage-only events land here */
|
|
mcp_json_free(v);
|
|
return 0;
|
|
}
|
|
choice = mcp_array_at(choices, 0);
|
|
delta = mcp_arg(choice, "delta");
|
|
if (delta == NULL) {
|
|
mcp_json_free(v);
|
|
return 0;
|
|
}
|
|
|
|
content = mcp_arg(delta, "content");
|
|
if (content != NULL && mcp_json_string(content) != NULL) {
|
|
const char *text = mcp_json_string(content);
|
|
|
|
if (sp_buf_puts(&s->content, text) != 0 ||
|
|
sp_buf_puts(out, text) != 0) {
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
produced = 1;
|
|
}
|
|
|
|
/* reasoning models report their thinking in a separate field */
|
|
{
|
|
const mcp_json *think = mcp_arg(delta, "reasoning_content");
|
|
|
|
if (think != NULL && mcp_json_string(think) != NULL) {
|
|
if (sp_buf_puts(&s->think, mcp_json_string(think)) != 0 ||
|
|
sp_buf_puts(out, mcp_json_string(think)) != 0) {
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
produced = 1;
|
|
}
|
|
}
|
|
|
|
tcs = mcp_arg(delta, "tool_calls");
|
|
if (tcs != NULL) {
|
|
size_t i, n = mcp_array_len(tcs);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
const mcp_json *tc = mcp_array_at(tcs, i);
|
|
const mcp_json *fn = mcp_arg(tc, "function");
|
|
const mcp_json *idx = mcp_arg(tc, "index");
|
|
const mcp_json *id = mcp_arg(tc, "id");
|
|
struct sse_call *slot;
|
|
int index = (int)mcp_arg_int(tcs, "index", -1);
|
|
|
|
if (idx != NULL)
|
|
index = (int)mcp_arg_int(delta, "index", index);
|
|
/* the index lives on the tool call itself in OpenAI's shape */
|
|
index = (int)mcp_arg_int(tc, "index", index);
|
|
if (index < 0)
|
|
index = (int)(s->ncalls == 0 ? 0 : s->ncalls - 1);
|
|
slot = call_slot(s, (size_t)index);
|
|
if (slot == NULL) {
|
|
mcp_json_free(v);
|
|
sp_buf_puts(err, "sse: too many tool calls");
|
|
return -1;
|
|
}
|
|
if (id != NULL && mcp_json_string(id) != NULL &&
|
|
slot->id == NULL) {
|
|
slot->id = strdup(mcp_json_string(id));
|
|
slot->seen = 1;
|
|
}
|
|
if (fn != NULL) {
|
|
const mcp_json *name = mcp_arg(fn, "name");
|
|
const mcp_json *args = mcp_arg(fn, "arguments");
|
|
|
|
if (name != NULL && mcp_json_string(name) != NULL &&
|
|
slot->name == NULL) {
|
|
slot->name =
|
|
strdup(mcp_json_string(name));
|
|
slot->seen = 1;
|
|
if (sp_buf_puts(out, slot->name) != 0) {
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
produced = 1;
|
|
}
|
|
if (args != NULL && mcp_json_string(args) != NULL &&
|
|
sp_buf_puts(&slot->args,
|
|
mcp_json_string(args)) != 0) {
|
|
mcp_json_free(v);
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
mcp_json_free(v);
|
|
return produced;
|
|
}
|
|
|
|
int
|
|
sp_sse_finish(struct sp_sse *s, struct sp_reply *out, struct sp_buf *err)
|
|
{
|
|
size_t i;
|
|
int any = 0;
|
|
|
|
if (s == NULL || out == NULL)
|
|
return -1;
|
|
out->status = 200;
|
|
out->content = strdup(s->content.p != NULL ? s->content.p : "");
|
|
if (out->content == NULL)
|
|
return -1;
|
|
for (i = 0; i < s->ncalls; i++)
|
|
if (s->calls[i].seen)
|
|
any = 1;
|
|
if (!any)
|
|
return 0;
|
|
|
|
out->calls = calloc(s->ncalls, sizeof(*out->calls));
|
|
if (out->calls == NULL)
|
|
return -1;
|
|
out->ncalls = s->ncalls;
|
|
for (i = 0; i < s->ncalls; i++) {
|
|
out->calls[i].id = strdup(s->calls[i].id != NULL ?
|
|
s->calls[i].id : "call-0");
|
|
out->calls[i].name = strdup(s->calls[i].name != NULL ?
|
|
s->calls[i].name : "");
|
|
out->calls[i].args = strdup(s->calls[i].args.p != NULL ?
|
|
s->calls[i].args.p : "{}");
|
|
if (out->calls[i].id == NULL || out->calls[i].name == NULL ||
|
|
out->calls[i].args == NULL) {
|
|
sp_reply_free(out);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/* hand the provider's structure back verbatim next round */
|
|
{
|
|
struct sp_buf json;
|
|
|
|
if (sp_buf_init(&json) != 0)
|
|
return -1;
|
|
sp_buf_puts(&json, "[");
|
|
for (i = 0; i < s->ncalls; i++) {
|
|
if (i > 0)
|
|
sp_buf_puts(&json, ",");
|
|
sp_buf_puts(&json, "{\"id\":\"");
|
|
sp_json_escape(&json, out->calls[i].id);
|
|
sp_buf_puts(&json, "\",\"type\":\"function\","
|
|
"\"function\":{\"name\":\"");
|
|
sp_json_escape(&json, out->calls[i].name);
|
|
sp_buf_puts(&json, "\",\"arguments\":\"");
|
|
sp_json_escape(&json, out->calls[i].args);
|
|
sp_buf_puts(&json, "\"}}");
|
|
}
|
|
sp_buf_puts(&json, "]");
|
|
out->calls_json = strdup(json.p);
|
|
sp_buf_free(&json);
|
|
if (out->calls_json == NULL)
|
|
return -1;
|
|
}
|
|
(void)err;
|
|
return 0;
|
|
}
|