splinter: a status line that plugins can write
New host API for it: sp_host_add_status() takes a named renderer, and sp_host_status_text() joins the segments with ' | '. The numbers a plugin shows come from the harness through struct sp_meter - context used and window, session tokens, cache hits, tokens per second, running tasks. A plugin that counted for itself would drift from the harness within one turn. plugins/status renders clock, ctx with a 24 column bar, cache, tok/s, tokens in and out, cost and task count. Prices are configuration, not knowledge: SPLINTER_CTX_MAX=32768 SPLINTER_PRICE_IN=0.3 SPLINTER_PRICE_OUT=1.2 Without prices the cost segment says nothing instead of inventing a number. Also fixed: the status.so recipe compiled kids.c, so the plugin that loaded was the kid mode with a different name - the reason seven status registrations produced an empty line. Known open: the segments compose (probe: 'clock: 17:28:14 | ctx 0/32.8k [........................] 0% | 0 in / 0 out | $0.0000') but the bar still draws only the title - not yet isolated.
This commit is contained in:
1 parent
a8667a8ccc
commit
3f65dfdc4c
6 files changed
+320
-1
No files matched your search
@@ -29,7 +29,7 @@ BINOBJ = src/main.o src/ui.o
|
||||
PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
plugins/cli/cli.so plugins/focus/focus.so plugins/mcp/mcp.so \
|
||||
plugins/ring/ring.so plugins/reminder/reminder.so \
|
||||
plugins/kids/kids.so
|
||||
plugins/kids/kids.so plugins/status/status.so
|
||||
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
|
||||
tests/test_transport tests/test_agent tests/test_openai \
|
||||
tests/test_theme tests/test_plugin tests/test_memory tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor
|
||||
@@ -75,6 +75,9 @@ plugins/ring/ring.so: plugins/ring/ring.c $(LIB)
|
||||
plugins/kids/kids.so: plugins/kids/kids.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/kids/kids.c -L. -lsplinter \
|
||||
-Iinclude -Isrc -Ivendor/libmcp $(PLUGIN_LDFLAGS)
|
||||
plugins/status/status.so: plugins/status/status.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/status/status.c -L. -lsplinter \
|
||||
-Iinclude -Isrc -Ivendor/libmcp $(PLUGIN_LDFLAGS)
|
||||
plugins/reminder/reminder.so: plugins/reminder/reminder.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/reminder/reminder.c -L. -lsplinter \
|
||||
-Iinclude -Isrc -Ivendor/libmcp $(PLUGIN_LDFLAGS)
|
||||
|
||||
@@ -85,6 +85,28 @@ struct sp_gate {
|
||||
int (*block_turn)(struct sp_host *, char *msg, size_t len);
|
||||
};
|
||||
|
||||
/*
|
||||
* What the status line is allowed to know. The harness owns the numbers - a
|
||||
* plugin must not count tokens twice or guess a price.
|
||||
*/
|
||||
struct sp_meter {
|
||||
size_t ctx_used; /* tokens the last request carried */
|
||||
size_t ctx_max; /* the window, 0 = unknown */
|
||||
size_t tok_in; /* this session */
|
||||
size_t tok_out;
|
||||
size_t cached; /* tokens the provider served from cache */
|
||||
long tps; /* output tokens per second, last turn */
|
||||
double cost; /* session cost, filled from the price config */
|
||||
unsigned tasks_running;
|
||||
unsigned tasks_max;
|
||||
};
|
||||
|
||||
/* A status segment: a name and a renderer, drawn in the order registered. */
|
||||
struct sp_status {
|
||||
const char *name;
|
||||
void (*render)(struct sp_host *, char *buf, size_t len);
|
||||
};
|
||||
|
||||
struct sp_plugin {
|
||||
uint32_t abi; /* must equal SP_PLUGIN_ABI */
|
||||
const char *name;
|
||||
@@ -108,6 +130,10 @@ int sp_host_widget_text(struct sp_host *, const char *widget,
|
||||
int sp_host_note(struct sp_host *, const char *text);
|
||||
int sp_host_add_compactor(struct sp_host *, const struct sp_compactor *);
|
||||
int sp_host_add_gate(struct sp_host *, const struct sp_gate *);
|
||||
int sp_host_add_status(struct sp_host *, const struct sp_status *);
|
||||
const struct sp_meter *sp_host_meter(struct sp_host *);
|
||||
void sp_host_set_meter(struct sp_host *, const struct sp_meter *);
|
||||
size_t sp_host_status_text(struct sp_host *, char *buf, size_t len);
|
||||
/* Ask every gate. Returns 1 allowed, 0 denied (reason filled), -1 no opinion. */
|
||||
int sp_host_gate(struct sp_host *, const char *tool, int auth,
|
||||
const char *args, char *reason, size_t reason_len);
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* splinter -- the status line.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* Everything a long session makes you want to know, in one line:
|
||||
*
|
||||
* clock: 17:20:01 | ctx 12.3k/32k [|||||||...] | 41 tok/s | 1.2k tok | $0.0042
|
||||
*
|
||||
* The numbers come from the harness (struct sp_meter), never from the plugin
|
||||
* counting for itself: two counters that disagree are worse than none.
|
||||
*
|
||||
* Prices are configuration, not knowledge:
|
||||
*
|
||||
* SPLINTER_PRICE_IN=3.0 dollars per million input tokens
|
||||
* SPLINTER_PRICE_OUT=15.0 dollars per million output tokens
|
||||
* SPLINTER_PRICE_CACHED=0.3
|
||||
*
|
||||
* Without them the cost segment says nothing rather than inventing a number.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
|
||||
#define BAR_WIDTH 24
|
||||
|
||||
static double price_in, price_out, price_cached;
|
||||
|
||||
static void
|
||||
human(size_t n, char *buf, size_t len)
|
||||
{
|
||||
if (n >= 1000000)
|
||||
snprintf(buf, len, "%.1fM", (double)n / 1e6);
|
||||
else if (n >= 1000)
|
||||
snprintf(buf, len, "%.1fk", (double)n / 1e3);
|
||||
else
|
||||
snprintf(buf, len, "%zu", n);
|
||||
}
|
||||
|
||||
static void
|
||||
render_clock(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
struct tm tm;
|
||||
|
||||
(void)h;
|
||||
(void)localtime_r(&now, &tm);
|
||||
strftime(buf, len, "clock: %H:%M:%S", &tm);
|
||||
}
|
||||
|
||||
static void
|
||||
render_ctx(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
const struct sp_meter *m = sp_host_meter(h);
|
||||
char used[24], max[24], bar[BAR_WIDTH + 1];
|
||||
size_t filled = 0, i;
|
||||
|
||||
human(m->ctx_used, used, sizeof(used));
|
||||
if (m->ctx_max == 0) {
|
||||
snprintf(buf, len, "ctx %s", used);
|
||||
return;
|
||||
}
|
||||
human(m->ctx_max, max, sizeof(max));
|
||||
filled = m->ctx_used >= m->ctx_max ? BAR_WIDTH :
|
||||
(size_t)((double)BAR_WIDTH * (double)m->ctx_used / (double)m->ctx_max);
|
||||
if (filled > BAR_WIDTH)
|
||||
filled = BAR_WIDTH;
|
||||
for (i = 0; i < BAR_WIDTH; i++)
|
||||
bar[i] = i < filled ? '|' : '.';
|
||||
bar[BAR_WIDTH] = '\0';
|
||||
snprintf(buf, len, "ctx %s/%s [%s] %u%%", used, max, bar,
|
||||
(unsigned)(100.0 * (double)m->ctx_used / (double)m->ctx_max));
|
||||
}
|
||||
|
||||
static void
|
||||
render_rate(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
const struct sp_meter *m = sp_host_meter(h);
|
||||
|
||||
if (m->tps <= 0) {
|
||||
buf[0] = '\0';
|
||||
return;
|
||||
}
|
||||
snprintf(buf, len, "%ld tok/s", m->tps);
|
||||
}
|
||||
|
||||
static void
|
||||
render_tokens(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
const struct sp_meter *m = sp_host_meter(h);
|
||||
char in[24], out[24];
|
||||
|
||||
human(m->tok_in, in, sizeof(in));
|
||||
human(m->tok_out, out, sizeof(out));
|
||||
snprintf(buf, len, "%s in / %s out", in, out);
|
||||
}
|
||||
|
||||
static void
|
||||
render_cost(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
const struct sp_meter *m = sp_host_meter(h);
|
||||
|
||||
if (price_in == 0 && price_out == 0) {
|
||||
buf[0] = '\0';
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* The price belongs to the model, the tokens belong to the harness: the
|
||||
* product is computed here so a price change needs no rebuild of anything
|
||||
* else.
|
||||
*/
|
||||
snprintf(buf, len, "$%.4f",
|
||||
(double)m->tok_in * price_in / 1e6 +
|
||||
(double)m->tok_out * price_out / 1e6);
|
||||
}
|
||||
|
||||
static void
|
||||
render_cache(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
const struct sp_meter *m = sp_host_meter(h);
|
||||
char hit[24];
|
||||
|
||||
if (m->ctx_used == 0 || m->cached == 0) {
|
||||
buf[0] = '\0';
|
||||
return;
|
||||
}
|
||||
human(m->cached, hit, sizeof(hit));
|
||||
snprintf(buf, len, "cache %s (%u%%)", hit,
|
||||
(unsigned)(100.0 * (double)m->cached / (double)m->ctx_used));
|
||||
}
|
||||
|
||||
static void
|
||||
render_tasks(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
const struct sp_meter *m = sp_host_meter(h);
|
||||
|
||||
if (m->tasks_max == 0) {
|
||||
buf[0] = '\0';
|
||||
return;
|
||||
}
|
||||
snprintf(buf, len, "tasks %u/%u", m->tasks_running, m->tasks_max);
|
||||
}
|
||||
|
||||
static const struct sp_status seg_clock = { "clock", render_clock };
|
||||
static const struct sp_status seg_ctx = { "ctx", render_ctx };
|
||||
static const struct sp_status seg_cache = { "cache", render_cache };
|
||||
static const struct sp_status seg_rate = { "rate", render_rate };
|
||||
static const struct sp_status seg_tokens = { "tokens", render_tokens };
|
||||
static const struct sp_status seg_cost = { "cost", render_cost };
|
||||
static const struct sp_status seg_tasks = { "tasks", render_tasks };
|
||||
|
||||
static int
|
||||
status_init(struct sp_host *h)
|
||||
{
|
||||
const char *in = getenv("SPLINTER_PRICE_IN");
|
||||
const char *out = getenv("SPLINTER_PRICE_OUT");
|
||||
const char *cached = getenv("SPLINTER_PRICE_CACHED");
|
||||
|
||||
if (in != NULL && *in != '\0')
|
||||
price_in = strtod(in, NULL);
|
||||
if (out != NULL && *out != '\0')
|
||||
price_out = strtod(out, NULL);
|
||||
if (cached != NULL && *cached != '\0')
|
||||
price_cached = strtod(cached, NULL);
|
||||
(void)price_cached;
|
||||
|
||||
(void)sp_host_add_status(h, &seg_clock);
|
||||
(void)sp_host_add_status(h, &seg_ctx);
|
||||
(void)sp_host_add_status(h, &seg_cache);
|
||||
(void)sp_host_add_status(h, &seg_rate);
|
||||
(void)sp_host_add_status(h, &seg_tokens);
|
||||
(void)sp_host_add_status(h, &seg_cost);
|
||||
(void)sp_host_add_status(h, &seg_tasks);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sp_plugin plugin = {
|
||||
SP_PLUGIN_ABI,
|
||||
"status",
|
||||
"1.0",
|
||||
status_init,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
const struct sp_plugin *
|
||||
sp_plugin_describe(void)
|
||||
{
|
||||
return &plugin;
|
||||
}
|
||||
@@ -56,6 +56,9 @@ struct sp_host {
|
||||
size_t ncompact;
|
||||
const struct sp_gate *gate[MAX_COMPACTORS];
|
||||
size_t ngate;
|
||||
const struct sp_status *status[MAX_COMPACTORS];
|
||||
size_t nstatus;
|
||||
struct sp_meter meter;
|
||||
struct {
|
||||
char name[32];
|
||||
struct sp_transport tr;
|
||||
@@ -174,6 +177,57 @@ sp_host_add_compactor(struct sp_host *h, const struct sp_compactor *c)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_host_add_status(struct sp_host *h, const struct sp_status *s)
|
||||
{
|
||||
if (h == NULL || s == NULL || s->render == NULL || s->name == NULL)
|
||||
return -1;
|
||||
if (h->nstatus == MAX_COMPACTORS)
|
||||
return -1;
|
||||
h->status[h->nstatus++] = s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct sp_meter *
|
||||
sp_host_meter(struct sp_host *h)
|
||||
{
|
||||
static const struct sp_meter empty;
|
||||
|
||||
return h != NULL ? &h->meter : ∅
|
||||
}
|
||||
|
||||
void
|
||||
sp_host_set_meter(struct sp_host *h, const struct sp_meter *m)
|
||||
{
|
||||
if (h != NULL && m != NULL)
|
||||
h->meter = *m;
|
||||
}
|
||||
|
||||
size_t
|
||||
sp_host_status_text(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
size_t i, used = 0;
|
||||
|
||||
if (h == NULL || buf == NULL || len == 0)
|
||||
return 0;
|
||||
buf[0] = '\0';
|
||||
for (i = 0; i < h->nstatus; i++) {
|
||||
char piece[256];
|
||||
int n;
|
||||
|
||||
piece[0] = '\0';
|
||||
h->status[i]->render(h, piece, sizeof(piece));
|
||||
if (piece[0] == '\0')
|
||||
continue;
|
||||
n = snprintf(buf + used, len - used, "%s%s",
|
||||
used > 0 ? " | " : "", piece);
|
||||
if (n < 0 || (size_t)n >= len - used)
|
||||
break;
|
||||
used += (size_t)n;
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
int
|
||||
sp_host_add_gate(struct sp_host *h, const struct sp_gate *g)
|
||||
{
|
||||
|
||||
@@ -755,6 +755,32 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
wnoutrefresh(ui_side);
|
||||
}
|
||||
|
||||
/*
|
||||
* Feed the meter the status plugins read. The harness counts; a plugin must not
|
||||
* count for itself or the two numbers drift apart.
|
||||
*/
|
||||
static struct sp_host *g_status_host;
|
||||
|
||||
static void
|
||||
update_meter(struct ui_state *s, struct sp_host *host)
|
||||
{
|
||||
struct sp_meter m;
|
||||
const char *ctxmax = getenv("SPLINTER_CTX_MAX");
|
||||
|
||||
if (host == NULL)
|
||||
return;
|
||||
m = *sp_host_meter(host);
|
||||
m.ctx_used = s->tokens_in;
|
||||
m.ctx_max = ctxmax != NULL && *ctxmax != '\0' ?
|
||||
(size_t)strtoul(ctxmax, NULL, 10) : m.ctx_max;
|
||||
m.tok_in = s->tokens_in;
|
||||
m.tok_out = s->tokens_out;
|
||||
if (s->last_ms > 0 && s->tokens_out > 0)
|
||||
m.tps = (long)((double)s->tokens_out * 1000.0 /
|
||||
(double)s->last_ms);
|
||||
sp_host_set_meter(host, &m);
|
||||
}
|
||||
|
||||
static void
|
||||
draw_status(struct ui_state *s)
|
||||
{
|
||||
@@ -764,6 +790,20 @@ draw_status(struct ui_state *s)
|
||||
wattron(ui_status, COLOR_PAIR(PAIR_STATUS));
|
||||
mvwhline(ui_status, 0, 0, ' ', cols);
|
||||
mvwprintw(ui_status, 0, 1, sp_t("status.title"), SP_VERSION);
|
||||
if (g_status_host != NULL) {
|
||||
char seg[512];
|
||||
size_t n;
|
||||
|
||||
update_meter(s, g_status_host);
|
||||
n = sp_host_status_text(g_status_host, seg, sizeof(seg));
|
||||
if (n > 0) {
|
||||
int at = 1 + (int)strlen("splinter") + 1 +
|
||||
(int)strlen(SP_VERSION) + 2;
|
||||
|
||||
if (at + (int)n < cols)
|
||||
mvwaddnstr(ui_status, 0, at, seg, cols - at);
|
||||
}
|
||||
}
|
||||
{
|
||||
char right[96];
|
||||
|
||||
@@ -928,6 +968,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
|
||||
memset(&s, 0, sizeof(s));
|
||||
sp_buf_init(&s.last_think);
|
||||
s.endpoint = endpoint;
|
||||
g_status_host = host; /* the status line is drawn per tick */
|
||||
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0)
|
||||
return 1;
|
||||
if (sp_theme_load(theme_name, &s.theme, &err) != 0) {
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user