Files
lotherk a01bfe9df1 sessions switch locally, gateway keeps caller settings and mixed calls
- sp_engine_switch(): close the old engine and open the chosen session from
  the same options; if the new one fails, reopen the session the engine was
  on. The protocol's open and the in-process /open both use it. The curses
  screen clears the pane and replays the new history, so /open no longer says
  it needs --attach.
- gateway: temperature, top_p and max_tokens (max_completion_tokens) are
  forwarded to the provider instead of dropped; request bodies may be
  Transfer-Encoding: chunked; a mixed reply runs the gw_ calls, returns the
  caller's calls in tool_calls and puts the local observations in content
  (also in the SSE delta), so the caller can echo them back.
- reconnect: when the remembered session is gone, the screen says which one
  and stays on the session the server has instead of silently swapping the
  transcript.
- ring: the exits' transports are disposed on plugin shutdown.
- /status goes through the catalogues like the rest of the UI (the English
  labels and their alignment are unchanged, so existing parsers still work).
- the man page passes mandoc -Tlint and covers the gateway and /open.

Tests: test_ui.sh covers local /sessions and /open (new, back, history),
test_protocol.sh the shared switch, test_reconnect.sh a vanished session,
test_gateway_openai.sh forwarded parameters, a chunked body and the mixed
reply in JSON and SSE. make check green.
2026-11-16 10:18:41 +00:00

323 lines
7.7 KiB
C

/*
* splinter -- the exit ring.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* A turn should not die because one provider's free tier ran out at 03:14. The
* ring holds several ways to reach a model and walks them in order:
*
* exit = go | OpenCode Go | endpoint=... | model=... | key_env=OPENCODE_KEY
* exit = groq | Groq free tier | endpoint=... | model=... | key_env=GROQ_KEY
* exit = local | kontron, lokal | endpoint=... | model=...
*
* Only pressure moves the ring: a rate limit, an exhausted quota, or a provider
* that is down (500/502/503/504). A syntax error or a bad key stops the turn
* where it is - pretending every failure is "try the next one" would hide real
* breakage behind a random provider. Each step is reported, so the log says which
* exit served the turn.
*
* And once an exit has streamed text to the screen, its failure is the turn's
* failure: asking the next exit would show the operator a second answer after
* half of the first one.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "config.h"
#define MAX_EXITS SP_CONFIG_MAX_DELEGATES
struct ring {
struct sp_host *host;
const struct sp_config *cfg;
struct sp_transport tr[MAX_EXITS];
size_t n;
int last; /* which exit served the last turn */
int used[MAX_EXITS];
/* last and used are written by the turn thread, read by the widget */
pthread_mutex_t lock;
};
static struct ring ring = { .lock = PTHREAD_MUTEX_INITIALIZER };
/* Pressure - a limit or a provider that is down - is worth another exit. */
static int
is_pressure(int rc, long status, const struct sp_buf *err)
{
if (status == 429 || status == 503 || status == 502 ||
status == 504 || status == 500)
return 1;
if (rc == 0)
return 0;
if (err != NULL && err->p != NULL) {
const char *p = err->p;
if (strstr(p, "429") != NULL || strstr(p, "rate limit") != NULL ||
strstr(p, "rate_limit") != NULL ||
strstr(p, "quota") != NULL ||
strstr(p, "limit reached") != NULL ||
strstr(p, "insufficient") != NULL ||
strstr(p, "overloaded") != NULL)
return 1;
}
return 0;
}
static void
publish(const char *text)
{
(void)sp_host_widget_text(ring.host, "ring", text);
(void)sp_host_note(ring.host, text);
}
static int
build(void)
{
size_t i;
if (ring.n > 0)
return 0;
if (ring.cfg == NULL || ring.cfg->nexits == 0) {
publish("ring: no exit configured");
return -1;
}
for (i = 0; i < ring.cfg->nexits && i < MAX_EXITS; i++) {
const struct sp_exit *e = &ring.cfg->exit[i];
const char *env = e->key_env[0] != '\0' ?
e->key_env : "SPLINTER_API_KEY";
const char *key = getenv(env);
{
const char *hto = getenv("SPLINTER_HTTP_TIMEOUT");
long hto_s = hto != NULL && *hto != '\0' ?
strtol(hto, NULL, 10) : 0;
if (hto_s <= 0)
hto_s = 900;
ring.tr[ring.n] = sp_openai_transport(e->endpoint, e->model,
key, sp_host_registry(ring.host), hto_s);
}
if (ring.tr[ring.n].send == NULL) {
publish("ring: cannot build an exit");
return -1;
}
ring.n++;
}
return 0;
}
static int
ring_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
struct sp_buf *err)
{
struct ring *r = ctx;
size_t i;
char line[128];
if (build() != 0)
return -1;
for (i = 0; i < r->n; i++) {
struct sp_buf e;
long status;
int rc;
if (sp_buf_init(&e) != 0)
return -1;
sp_reply_free(out);
rc = r->tr[i].send(r->tr[i].ctx, conv, out, &e);
status = out->status;
if (rc == 0) {
pthread_mutex_lock(&r->lock);
r->last = (int)i;
r->used[i]++;
pthread_mutex_unlock(&r->lock);
snprintf(line, sizeof(line), "%s (%zu/%zu)",
r->cfg->exit[i].label, i + 1, r->n);
(void)sp_host_widget_text(r->host, "ring", line);
sp_buf_free(&e);
return 0;
}
if (!is_pressure(rc, status, &e)) {
/* not a limit: this is a real failure, keep it */
if (err != NULL)
(void)sp_buf_puts(err, e.p != NULL ? e.p :
"ring: exit failed");
sp_buf_free(&e);
return rc;
}
snprintf(line, sizeof(line), "ring: %s busy, next exit",
r->cfg->exit[i].label);
(void)sp_host_note(r->host, line);
sp_buf_free(&e);
}
if (err != NULL)
(void)sp_buf_puts(err, "ring: every exit is rate limited");
return -1;
}
/* passes the stream through and remembers whether anything was shown */
struct seen {
sp_stream_cb cb;
void *ctx;
int shown;
};
static void
seen_cb(void *ctx, int kind, const char *text)
{
struct seen *s = ctx;
if (text != NULL && *text != '\0')
s->shown = 1;
if (s->cb != NULL)
s->cb(s->ctx, kind, text);
}
static int
ring_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
struct sp_buf *err, sp_stream_cb cb, void *cbctx)
{
struct ring *r = ctx;
size_t i;
char line[128];
struct seen seen;
if (build() != 0)
return -1;
for (i = 0; i < r->n; i++) {
struct sp_buf e;
long status;
int rc;
if (sp_buf_init(&e) != 0)
return -1;
sp_reply_free(out);
seen.cb = cb;
seen.ctx = cbctx;
seen.shown = 0;
rc = r->tr[i].send_stream(r->tr[i].ctx, conv, out, &e, seen_cb,
&seen);
status = out->status;
if (rc == 0) {
pthread_mutex_lock(&r->lock);
r->last = (int)i;
r->used[i]++;
pthread_mutex_unlock(&r->lock);
snprintf(line, sizeof(line), "%s (%zu/%zu)",
r->cfg->exit[i].label, i + 1, r->n);
(void)sp_host_widget_text(r->host, "ring", line);
sp_buf_free(&e);
return 0;
}
if (!is_pressure(rc, status, &e) || seen.shown) {
if (err != NULL) {
(void)sp_buf_puts(err, e.p != NULL ? e.p :
"ring: exit failed");
if (seen.shown && is_pressure(rc, status, &e))
(void)sp_buf_puts(err, " (after part of "
"the answer was shown; not retried)");
}
sp_buf_free(&e);
return rc != 0 ? rc : -1;
}
snprintf(line, sizeof(line), "ring: %s busy, next exit",
r->cfg->exit[i].label);
(void)sp_host_note(r->host, line);
sp_buf_free(&e);
}
if (err != NULL)
(void)sp_buf_puts(err, "ring: every exit is rate limited");
return -1;
}
static int
ring_widget(struct sp_host *h, const char *name, void *ctx)
{
struct ring *r = ctx;
char line[160];
size_t i;
(void)h;
if (r->n == 0) {
(void)sp_host_widget_text(ring.host, name, "no exits");
return 0;
}
pthread_mutex_lock(&r->lock);
snprintf(line, sizeof(line), "%.16s |", r->cfg->exit[r->last].label);
for (i = 0; i < r->n; i++) {
char bit[16];
snprintf(bit, sizeof(bit), " %d", r->used[i]);
strncat(line, bit, sizeof(line) - strlen(line) - 1);
}
pthread_mutex_unlock(&r->lock);
(void)sp_host_widget_text(ring.host, name, line);
return 0;
}
static const struct sp_widget widget = {
"ring",
ring_widget,
NULL,
&ring,
3000
};
static int
ring_init(struct sp_host *h)
{
struct sp_transport t;
memset(&ring, 0, sizeof(ring));
(void)pthread_mutex_init(&ring.lock, NULL);
ring.host = h;
ring.cfg = sp_host_config(h);
if (ring.cfg == NULL || ring.cfg->nexits == 0) {
(void)sp_host_note(h, "ring: no exit configured");
return -1;
}
memset(&t, 0, sizeof(t));
t.send = ring_send;
t.send_stream = ring_stream;
t.ctx = &ring;
if (sp_host_add_transport(h, "ring", t) != 0) {
(void)sp_host_note(h, "ring: cannot register the transport");
return -1;
}
(void)sp_host_add_widget(h, &widget);
return 0;
}
/* the exits own their contexts; the plugin's lifetime is the process's */
static void
ring_shutdown(struct sp_host *h)
{
size_t i;
(void)h;
for (i = 0; i < ring.n; i++)
sp_transport_dispose(&ring.tr[i]);
ring.n = 0;
(void)pthread_mutex_destroy(&ring.lock);
}
static const struct sp_plugin plugin = {
SP_PLUGIN_ABI,
"ring",
"1.0",
ring_init,
NULL,
ring_shutdown
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &plugin;
}