ui: a status bar above the prompt, a scroll fix, a steer space

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.
This commit is contained in:
lotherk committed 2026-11-16 16:59:49 +00:00
1 parent 76c9007c02
commit 1726bc1967
27 files changed
+374 -35

No files matched your search

+6 -4
View File
@@ -5,8 +5,9 @@ them, and what happened — and trains the ones that do the fighting.
A C99/POSIX agent harness with a curses interface: the conversation on three
quarters, a sidebar on the last one that says which agent, provider, model and
allowance you are on and where the tools will act, the prompt below it. The
conversation starts clear at the top and only ever grows.
allowance you are on and where the tools will act, a status line above the
prompt with the context gauge (`ctx used/max`, cache share, tok/s), and the
prompt below it. The conversation starts clear at the top and only ever grows.
make # libsplinter.so, the binaries, the plugins
make check # the tests, no network, no model
@@ -358,8 +359,9 @@ one row in the transcript, in the words of the call itself - `>> Run echo a` -
because "it did something" is not information. The reasoning is inserted before
those rows: it came first, and the log should read in the order things
happened. While it runs, the input line is only `12s steer> ` and what is
typed: the call belongs in the transcript, and in the prompt it was a second
copy that pushed the steer hint off the right edge. A steer is picked up at the
typed, and the status line above it stays visible: the call belongs in the
transcript, and in the prompt it was a second copy that pushed the steer hint
off the right edge. A steer is picked up at the
next step boundary; in a single-step turn it becomes the next message instead of
being dropped. Curses is only ever touched by the UI thread.
+1
View File
@@ -231,6 +231,7 @@ lies about what it does.
|-----|---------|
| `endpoint` | the API base, e.g. `http://127.0.0.1:8080/v1`; splinter appends `/chat/completions` (or `/messages` for `kind = anthropic`), `/models`, and the usage routes. A full route still works: it is cut back to the base |
| `model` | model name the endpoint expects |
| `ctx` | context window in tokens, optional; the curses status bar uses it for the `ctx used/max` gauge. `SPLINTER_CTX_MAX` overrides it for one run |
| `api_key_env` | name of the env var holding the key (default `SPLINTER_API_KEY`) |
| `transport` | a transport a plugin provides, e.g. `cli` |
| `cli_cmd` | command for the `cli` transport, e.g. `claude -p` |
+1 -1
View File
@@ -166,7 +166,7 @@ with 0.
| `ev` | fields | meaning |
| --- | --- | --- |
| `ready` | `protocol`, `version` | the greeting, always first |
| `state` | `session`, `dir`, `model`, `endpoint`, `agent`, `agents[]`, `agent_cur`, `tokens_in`, `tokens_out`, `steps`, `calls`, `compactions`, `last_ms`, `last_steps`, `last_calls`, `busy`, `waiting`, `quit` | everything a sidebar shows |
| `state` | `session`, `dir`, `model`, `endpoint`, `agent`, `agents[]`, `agent_cur`, `tokens_in`, `tokens_out`, `ctx_used`, `ctx_max`, `cached`, `turn_tokens_out`, `steps`, `calls`, `compactions`, `last_ms`, `last_steps`, `last_calls`, `busy`, `waiting`, `quit` | everything a sidebar or the status bar shows |
| `submitted` | `text` | the prompt actually run (a command may have rewritten it) |
| `busy` | `on` | a turn started or ended |
| `stream` | `kind` (`text` or `think`), `text` | a delta of the answer or the reasoning |
+4
View File
@@ -133,6 +133,8 @@ struct sp_reply {
long status;
long tokens_in; /* provider-reported, 0 when unknown */
long tokens_out;
long tokens_cached; /* prompt tokens the provider served
* from its cache, 0 unknown */
long retry_after_s; /* parsed Retry-After header, 0 unknown */
char *content; /* assistant text, may be "" */
struct sp_toolcall *calls;
@@ -323,6 +325,8 @@ struct sp_agent_stats {
size_t steps;
size_t tokens_in;
size_t tokens_out;
size_t last_in; /* the last request's prompt, for the meter */
size_t last_cached; /* of which the provider had cached */
size_t compactions;
size_t tool_calls;
size_t denied;
+4
View File
@@ -111,6 +111,10 @@ struct sp_engine_state {
char dir[512];
char theme[48]; /* set by the front end, for /status */
size_t tokens_in, tokens_out;
size_t ctx_used; /* prompt tokens of the last request */
size_t ctx_max; /* context window, 0 = unknown */
size_t cached; /* of ctx_used, served from the provider cache */
size_t turn_tokens_out; /* output tokens of this/last turn */
size_t steps, calls, compactions; /* the session's counters */
size_t last_ms, last_steps, last_calls; /* the last turn */
int busy;
+6
View File
@@ -54,6 +54,12 @@ cli.failed = cli: '%s' scheiterte (exit %d): %.400s
ui.prompt_mark = >
ui.steer_mark = steuern>
ui.swarm_locked = [Schwarm gesperrt]
ui.status_ctx_bar = ctx %zu/%zu %s %d%%
ui.status_ctx = ctx %zu
ui.status_cache = cache %d%%
ui.status_cache_none = cache -
ui.status_tps = %d tok/s
ui.status_tps_none = - tok/s
ui.approve = %s (%s)? y/N
auth.mutate = aendert Dateien
auth.destructive = zerstoerend
+6
View File
@@ -53,6 +53,12 @@ cli.failed = cli: '%s' failed (exit %d): %.400s
ui.prompt_mark = >
ui.steer_mark = steer>
ui.swarm_locked = [swarm locked]
ui.status_ctx_bar = ctx %zu/%zu %s %d%%
ui.status_ctx = ctx %zu
ui.status_cache = cache %d%%
ui.status_cache_none = cache -
ui.status_tps = %d tok/s
ui.status_tps_none = - tok/s
ui.approve = %s (%s)? y/N
auth.mutate = changes files
auth.destructive = destructive
+6
View File
@@ -18,6 +18,12 @@ ui.steering_queued = direcionamento na fila
ui.prompt_mark = >
ui.steer_mark = direcionar>
ui.swarm_locked = [enxame bloqueado]
ui.status_ctx_bar = ctx %zu/%zu %s %d%%
ui.status_ctx = ctx %zu
ui.status_cache = cache %d%%
ui.status_cache_none = cache -
ui.status_tps = %d tok/s
ui.status_tps_none = - tok/s
ui.approve = %s (%s)? y/N
sidebar.session = sessao
+4
View File
@@ -585,6 +585,10 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
reply.tokens_in : 0);
a->stats.tokens_out += (size_t)(reply.tokens_out > 0 ?
reply.tokens_out : 0);
a->stats.last_in = (size_t)(reply.tokens_in > 0 ?
reply.tokens_in : 0);
a->stats.last_cached = (size_t)(reply.tokens_cached > 0 ?
reply.tokens_cached : 0);
sp_host_provider_account(a->host, a->tr.provider, a->tr.key_id,
reply.status,
(size_t)(reply.tokens_in > 0 ? reply.tokens_in : 0),
+38 -13
View File
@@ -489,6 +489,8 @@ calls_json(struct sp_reply *out, const char *const *ids,
return out->calls_json != NULL ? 0 : -1;
}
static void as_usage_set(long *in, long *cached, const mcp_json *usage);
int
sp_anthropic_reply(const char *json, struct sp_reply *out, struct sp_buf *err)
{
@@ -570,11 +572,12 @@ sp_anthropic_reply(const char *json, struct sp_reply *out, struct sp_buf *err)
goto oom;
usage = mcp_arg(v, "usage");
if (usage != NULL) {
int pin = mcp_arg_int(usage, "input_tokens", -1);
int pout = mcp_arg_int(usage, "output_tokens", -1);
long in = 0, cached = 0;
int pout = mcp_arg_int(usage, "output_tokens", -1);
if (pin >= 0)
out->tokens_in = pin;
as_usage_set(&in, &cached, usage);
out->tokens_in = in;
out->tokens_cached = cached;
if (pout >= 0)
out->tokens_out = pout;
}
@@ -634,9 +637,36 @@ struct sp_as_sse {
size_t nblocks;
long tokens_in;
long tokens_out;
long tokens_cached;
int done;
};
/*
* Anthropic reports the prompt in pieces: input_tokens excludes the cache
* reads and writes. The context the request carried is the sum, and the cache
* share is the read part. A later usage block with no cache fields (the
* message_delta event) must not throw that away.
*/
static void
as_usage_set(long *in, long *cached, const mcp_json *usage)
{
int pin = mcp_arg_int(usage, "input_tokens", -1);
int cr = mcp_arg_int(usage, "cache_read_input_tokens", -1);
int cc = mcp_arg_int(usage, "cache_creation_input_tokens", -1);
if (pin >= 0) {
if (*in == 0 || cr >= 0 || cc >= 0) {
*in = (pin > 0 ? pin : 0) +
(cr > 0 ? cr : 0) + (cc > 0 ? cc : 0);
if (cr >= 0)
*cached = cr > 0 ? cr : 0;
}
} else if (cr >= 0) {
*in += cr > 0 ? cr : 0;
*cached = cr > 0 ? cr : 0;
}
}
struct sp_as_sse *
sp_as_sse_new(void)
{
@@ -758,22 +788,16 @@ sp_as_sse_line(struct sp_as_sse *s, const char *line, struct sp_buf *out,
if (strcmp(type, "message_start") == 0) {
msg = mcp_arg(v, "message");
usage = msg != NULL ? mcp_arg(msg, "usage") : NULL;
if (usage != NULL) {
int in = mcp_arg_int(usage, "input_tokens", -1);
if (in >= 0)
s->tokens_in = in;
}
if (usage != NULL)
as_usage_set(&s->tokens_in, &s->tokens_cached, usage);
} else if (strcmp(type, "message_delta") == 0) {
usage = mcp_arg(v, "usage");
if (usage != NULL) {
int outt = mcp_arg_int(usage, "output_tokens", -1);
int in = mcp_arg_int(usage, "input_tokens", -1);
if (outt >= 0)
s->tokens_out = outt;
if (in >= 0)
s->tokens_in = in;
as_usage_set(&s->tokens_in, &s->tokens_cached, usage);
}
} else if (strcmp(type, "content_block_start") == 0) {
const char *bt;
@@ -878,6 +902,7 @@ sp_as_sse_finish(struct sp_as_sse *s, struct sp_reply *out, struct sp_buf *err)
out->status = 200;
out->tokens_in = s->tokens_in;
out->tokens_out = s->tokens_out;
out->tokens_cached = s->tokens_cached;
out->content = strdup(s->content.p != NULL ? s->content.p : "");
if (out->content == NULL)
return -1;
+2 -1
View File
@@ -324,7 +324,8 @@ do_provider(struct cb *c, const char *name, unsigned line, struct ent *e,
size_t n)
{
static const char *const plain[] = { "kind", "cmd", "provider", "host",
"endpoint", "model", "key_env", "prio", "max_parallel", "limit", "tokens", NULL };
"endpoint", "model", "key_env", "ctx", "prio", "max_parallel",
"limit", "tokens", NULL };
struct sp_buf l;
const char *about = "";
size_t i, j;
+19
View File
@@ -291,6 +291,15 @@ parse_delegate(struct sp_delegate *d, const char *val, struct sp_buf *err,
snprintf(d->model, sizeof(d->model), "%s", s + 6);
else if (!strncmp(s, "key_env=", 8))
snprintf(d->key_env, sizeof(d->key_env), "%s", s + 8);
else if (!strncmp(s, "ctx=", 4)) {
d->ctx = strtoul(s + 4, &end, 10);
if (*end != '\0') {
sp_buf_printf(err, "config: %s:%u: ctx= wants "
"a number of tokens", path, lineno);
free(copy);
return -1;
}
}
else if (!strncmp(s, "env=", 4)) {
if (add_env(d->env, &d->nenv, s + 4, err, path,
lineno) != 0) {
@@ -639,6 +648,16 @@ load_stream(struct sp_config *c, FILE *f, const char *path,
set_str(c->endpoint, sizeof(c->endpoint), val);
else if (!strcmp(key, "model"))
set_str(c->model, sizeof(c->model), val);
else if (!strcmp(key, "ctx")) {
char *end;
c->ctx = strtoul(val, &end, 10);
if (*end != '\0') {
sp_buf_printf(err, "config: %s:%u: ctx wants "
"a number of tokens", path, lineno);
return -1;
}
}
else if (!strcmp(key, "api_key_env"))
set_str(c->key_env, sizeof(c->key_env), val);
else if (!strcmp(key, "kind")) {
+2
View File
@@ -78,6 +78,7 @@ struct sp_delegate {
char endpoint[512];
char model[256];
char key_env[64];
unsigned long ctx; /* context window in tokens, 0 = unknown */
char env[SP_CONFIG_MAX_ENV][SP_CONFIG_ENV_LEN]; /* NAME=VALUE */
size_t nenv;
/* the allowance, 0: none declared (see usage.h) */
@@ -131,6 +132,7 @@ struct sp_config {
char endpoint[512];
char model[256];
char key_env[64];
unsigned long ctx; /* of the main provider, 0 = unknown */
char kind[16]; /* of the main provider: "" (openai) or anthropic */
unsigned long req_limit, tok_limit; /* of the main provider */
long req_window, tok_window;
+23
View File
@@ -120,6 +120,8 @@ struct sp_engine {
/* the last turn, for the sidebar and /status */
size_t last_ms, last_steps, last_calls;
size_t turn_out0; /* tokens_out when the turn began */
size_t last_turn_out; /* output tokens it produced */
struct timespec turn_t0;
/*
* The agent's counters, copied on the thread that owns them (the turn
@@ -218,6 +220,11 @@ sp_engine_state(struct sp_engine *e, struct sp_engine_state *st)
pthread_mutex_lock(&e->lock);
st->tokens_in = e->snapshot.tokens_in;
st->tokens_out = e->snapshot.tokens_out;
st->ctx_used = e->snapshot.last_in;
st->cached = e->snapshot.last_cached;
st->turn_tokens_out = e->running && e->snapshot.tokens_out >=
e->turn_out0 ? e->snapshot.tokens_out - e->turn_out0 :
(e->running ? 0 : e->last_turn_out);
st->steps = e->snapshot.steps;
st->calls = e->snapshot.tool_calls;
st->compactions = e->snapshot.compactions;
@@ -230,6 +237,17 @@ sp_engine_state(struct sp_engine *e, struct sp_engine_state *st)
st->agents_n = (int)e->agents.n;
st->agent_cur = e->agent_cur;
pthread_mutex_unlock(&e->lock);
if (e->conf != NULL) {
const char *active = sp_usage_active();
struct sp_provider p;
size_t idx;
st->ctx_max = e->conf->ctx;
if (active != NULL && *active != '\0' &&
sp_provider_find(e->conf, active, &idx) &&
sp_provider_at(e->conf, idx, &p) == 0 && p.ctx > 0)
st->ctx_max = p.ctx;
}
return 0;
}
@@ -894,6 +912,9 @@ turn_thread(void *arg)
pthread_mutex_unlock(&e->lock);
sp_buf_clear(&e->answer);
sp_buf_clear(&e->err);
pthread_mutex_lock(&e->lock);
e->turn_out0 = e->ag.stats.tokens_out;
pthread_mutex_unlock(&e->lock);
rc = sp_agent_run(&e->ag, text, &e->answer, &e->err);
if (e->have_swarm)
swarm_report_busy(&e->swarm, 0);
@@ -978,6 +999,8 @@ turn_thread(void *arg)
(now.tv_nsec - e->turn_t0.tv_nsec) / 1000000);
pthread_mutex_lock(&e->lock);
e->last_ms = ms;
e->last_turn_out = e->ag.stats.tokens_out >= e->turn_out0 ?
e->ag.stats.tokens_out - e->turn_out0 : 0;
e->last_steps = e->ag.stats.steps;
e->last_calls = e->ag.stats.tool_calls;
pthread_mutex_unlock(&e->lock);
+14
View File
@@ -244,18 +244,32 @@ sp_openai_reply(const char *json, struct sp_reply *out, struct sp_buf *err)
const mcp_json *usage = mcp_arg(v, "usage");
if (usage != NULL) {
const mcp_json *details;
long pin = (long)mcp_arg_int(usage, "prompt_tokens", -1);
long pout = (long)mcp_arg_int(usage, "completion_tokens",
-1);
long cached = -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);
details = mcp_arg(usage, "prompt_tokens_details");
if (details != NULL)
cached = (long)mcp_arg_int(details,
"cached_tokens", -1);
if (cached < 0)
cached = (long)mcp_arg_int(usage,
"prompt_cache_hit_tokens", -1);
if (cached < 0)
cached = (long)mcp_arg_int(usage,
"cache_read_input_tokens", -1);
if (pin >= 0)
out->tokens_in = pin;
if (pout >= 0)
out->tokens_out = pout;
if (cached >= 0)
out->tokens_cached = cached;
}
}
+2
View File
@@ -78,6 +78,7 @@ sp_provider_at(const struct sp_config *cfg, size_t idx, struct sp_provider *out)
cfg->endpoint);
snprintf(out->model, sizeof(out->model), "%s", cfg->model);
snprintf(out->key_env, sizeof(out->key_env), "%s", cfg->key_env);
out->ctx = cfg->ctx;
out->main = 1;
return 0;
}
@@ -96,6 +97,7 @@ delegates:
snprintf(out->model, sizeof(out->model), "%s", d->model);
snprintf(out->key_env, sizeof(out->key_env), "%s", d->key_env);
snprintf(out->about, sizeof(out->about), "%s", d->about);
out->ctx = d->ctx;
return 0;
}
for (i = 0; i < cfg->nexits; i++) {
+1
View File
@@ -23,6 +23,7 @@ struct sp_provider {
char model[256];
char key_env[64];
char about[128];
unsigned long ctx; /* context window, 0 = unknown */
int main; /* the endpoint/model the run started with */
};
+16
View File
@@ -38,6 +38,7 @@ struct sp_sse {
int last; /* what the last line put into out */
long tokens_in;
long tokens_out;
long tokens_cached;
};
struct sp_sse *
@@ -174,17 +175,31 @@ sp_sse_line(struct sp_sse *s, const char *line, struct sp_buf *out,
const mcp_json *usage = mcp_arg(v, "usage");
if (usage != NULL) {
const mcp_json *details;
long in = (long)mcp_arg_int(usage, "prompt_tokens", -1);
long out = (long)mcp_arg_int(usage, "completion_tokens", -1);
long cached = -1;
if (in < 0)
in = (long)mcp_arg_int(usage, "input_tokens", -1);
if (out < 0)
out = (long)mcp_arg_int(usage, "output_tokens", -1);
details = mcp_arg(usage, "prompt_tokens_details");
if (details != NULL)
cached = (long)mcp_arg_int(details,
"cached_tokens", -1);
if (cached < 0)
cached = (long)mcp_arg_int(usage,
"prompt_cache_hit_tokens", -1);
if (cached < 0)
cached = (long)mcp_arg_int(usage,
"cache_read_input_tokens", -1);
if (in >= 0)
s->tokens_in = in;
if (out >= 0)
s->tokens_out = out;
if (cached >= 0)
s->tokens_cached = cached;
}
}
@@ -299,6 +314,7 @@ sp_sse_finish(struct sp_sse *s, struct sp_reply *out, struct sp_buf *err)
out->status = 200;
out->tokens_in = s->tokens_in;
out->tokens_out = s->tokens_out;
out->tokens_cached = s->tokens_cached;
out->content = strdup(s->content.p != NULL ? s->content.p : "");
if (out->content == NULL)
return -1;
+138 -13
View File
@@ -19,6 +19,8 @@
* | | session |
* | | directory |
* +---------------------------------+------------+
* | ctx 1290/131072 [|| ] 1% | cache 62% | t/s |
* +----------------------------------------------+
* | build> prompt, or "12s tool steer>" |
* +----------------------------------------------+
*
@@ -107,10 +109,13 @@ struct ui_state {
size_t next_id; /* ids start at 1 */
size_t scroll; /* lines scrolled back from the bottom */
size_t scroll_max; /* maxscroll of the last frame */
size_t scroll_base; /* maxscroll when the reader scrolled */
long last_ms;
size_t tokens_in;
size_t tokens_out;
size_t ctx_used; /* prompt tokens of the last request */
size_t ctx_max; /* its context window, 0 = unknown */
size_t ctx_cached; /* of ctx_used, served from cache */
size_t turn_out; /* output tokens of this/last turn */
size_t last_steps;
size_t last_calls;
char hist[128][1024]; /* what was typed before */
@@ -125,6 +130,7 @@ struct ui_state {
static WINDOW *ui_hist;
static WINDOW *ui_side;
static WINDOW *ui_status;
static WINDOW *ui_input;
/*
* The agent whose profile is active. The sidebar does not show it; with one
@@ -148,6 +154,7 @@ enum {
static void draw_history(struct ui_state *);
static void draw_sidebar(struct ui_state *, const char *, const char *,
struct sp_host *);
static void draw_statusbar(struct ui_state *);
static size_t add_entry(struct ui_state *, const char *, const char *);
static void drain_idle(struct ui_state *);
@@ -175,12 +182,19 @@ sync_state(struct ui_state *s)
{
struct sp_engine_state st;
const char *slash;
const char *ctxmax = getenv("SPLINTER_CTX_MAX");
size_t n;
if (s->be->state(s->be->ctx, &st) != 0)
return;
s->tokens_in = st.tokens_in;
s->tokens_out = st.tokens_out;
s->ctx_used = st.ctx_used;
s->ctx_max = st.ctx_max;
if (ctxmax != NULL && *ctxmax != '\0')
s->ctx_max = (size_t)strtoul(ctxmax, NULL, 10);
s->ctx_cached = st.cached;
s->turn_out = st.turn_tokens_out;
s->last_ms = st.last_ms;
s->last_steps = st.last_steps;
s->last_calls = st.last_calls;
@@ -919,14 +933,19 @@ draw_history(struct ui_state *s)
total += l->n + (size_t)blank[idx];
}
maxscroll = total > (size_t)rows ? total - (size_t)rows : 0;
/* scrolled up and text arrived below: keep the reader where he is */
if (s->scroll > 0 && s->scroll <= s->scroll_max &&
maxscroll > s->scroll_base)
s->scroll += maxscroll - s->scroll_base;
/*
* Scrolled up and text arrived below: grow 'scroll' by exactly the
* growth of maxscroll, so the first visible line stays the same. The
* old code compared maxscroll with the first visible line instead of
* the previous maxscroll; once the reader reached the top that line was
* 0, the condition was true on every later frame, and PgDn jumped
* straight back to the top.
*/
if (s->scroll > 0 && maxscroll > s->scroll_max)
s->scroll += maxscroll - s->scroll_max;
if (s->scroll > maxscroll)
s->scroll = maxscroll;
s->scroll_max = maxscroll;
s->scroll_base = maxscroll - s->scroll;
first = maxscroll - s->scroll; /* index of the top visible line */
for (idx = 0; idx < s->n && y < rows; idx++) {
@@ -1218,13 +1237,15 @@ set_ui_fields(struct sp_meter *m, void *ctx)
const struct ui_state *s = ctx;
const char *ctxmax = getenv("SPLINTER_CTX_MAX");
m->ctx_used = s->tokens_in;
m->ctx_used = s->ctx_used;
m->ctx_max = s->ctx_max;
if (ctxmax != NULL && *ctxmax != '\0')
m->ctx_max = (size_t)strtoul(ctxmax, NULL, 10);
m->cached = s->ctx_cached;
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 /
if (s->last_ms > 0 && s->turn_out > 0)
m->tps = (long)((double)s->turn_out * 1000.0 /
(double)s->last_ms);
}
@@ -1235,6 +1256,84 @@ update_meter(struct ui_state *s, struct sp_host *host)
sp_host_meter_update(host, set_ui_fields, s);
}
/*
* The line above the prompt: how full the context window is, how much of the
* prompt the provider had cached, and how fast the last turn wrote. It is a
* status bar, not a transcript row: one line, cut at the window edge.
*/
static void
draw_statusbar(struct ui_state *s)
{
char line[1024], bar[64];
size_t n = 0, used = s->ctx_used, max = s->ctx_max;
int w, tps = 0;
if (ui_status == NULL)
return;
w = getmaxx(ui_status);
if (w <= 1)
return;
werase(ui_status);
wattrset(ui_status, COLOR_PAIR(PAIR_STATUS));
if (max > 0) {
int bw = 16, filled, pct;
if (bw > w / 4)
bw = w / 4;
if (bw < 4)
bw = 4;
pct = (int)((used * 100 + max / 2) / max);
if (pct > 100)
pct = 100;
filled = (int)((used * bw + max / 2) / max);
if (filled < 0)
filled = 0;
if (filled > bw)
filled = bw;
{
int i;
bar[0] = '[';
for (i = 0; i < bw; i++)
bar[i + 1] = i < filled ? '|' : ' ';
bar[bw + 1] = ']';
bar[bw + 2] = '\0';
}
n += (size_t)snprintf(line + n, sizeof(line) - n,
sp_td("ui.status_ctx_bar", "ctx %zu/%zu %s %d%%"),
used, max, bar, pct);
} else {
n += (size_t)snprintf(line + n, sizeof(line) - n,
sp_td("ui.status_ctx", "ctx %zu"), used);
}
if (s->ctx_used > 0) {
int pct = (int)((s->ctx_cached * 100 + s->ctx_used / 2) /
s->ctx_used);
if (pct > 100)
pct = 100;
n += (size_t)snprintf(line + n, sizeof(line) - n, " | ");
n += (size_t)snprintf(line + n, sizeof(line) - n,
sp_td("ui.status_cache", "cache %d%%"), pct);
} else {
n += (size_t)snprintf(line + n, sizeof(line) - n, " | %s",
sp_td("ui.status_cache_none", "cache -"));
}
if (s->last_ms > 0 && s->turn_out > 0)
tps = (int)((s->turn_out * 1000 + s->last_ms / 2) / s->last_ms);
n += (size_t)snprintf(line + n, sizeof(line) - n, " | ");
if (tps > 0)
n += (size_t)snprintf(line + n, sizeof(line) - n,
sp_td("ui.status_tps", "%d tok/s"), tps);
else
n += (size_t)snprintf(line + n, sizeof(line) - n, "%s",
sp_td("ui.status_tps_none", "- tok/s"));
line[sizeof(line) - 1] = '\0';
mvwaddnstr(ui_status, 0, 0, line, w - 1);
wattrset(ui_status, A_NORMAL);
wnoutrefresh(ui_status);
}
static void
hist_load(struct ui_state *s, const char *dir)
@@ -1405,11 +1504,14 @@ layout(void)
side_w = cols > 50 ? 18 : 0;
if (side_w == 0)
chat_w = cols;
ui_hist = newwin(rows - 1, chat_w, 0, 0);
ui_hist = newwin(rows - 2, chat_w, 0, 0);
ui_side = side_w > 0 ?
newwin(rows - 1, side_w, 0, chat_w + 1) : NULL;
newwin(rows - 2, side_w, 0, chat_w + 1) : NULL;
}
wbkgd(ui_hist, COLOR_PAIR(PAIR_MAIN));
/* the meter line sits above the input, like a status bar */
ui_status = newwin(1, cols, rows - 2, 0);
wbkgd(ui_status, COLOR_PAIR(PAIR_STATUS));
ui_input = newwin(1, cols, rows - 1, 0);
wbkgd(ui_input, COLOR_PAIR(PAIR_INPUT));
keypad(ui_input, TRUE);
@@ -1429,9 +1531,11 @@ relayout(int input_timeout_ms)
delwin(ui_hist);
if (ui_side != NULL)
delwin(ui_side);
if (ui_status != NULL)
delwin(ui_status);
if (ui_input != NULL)
delwin(ui_input);
ui_hist = ui_side = ui_input = NULL;
ui_hist = ui_side = ui_status = ui_input = NULL;
layout();
wtimeout(ui_input, input_timeout_ms);
clearok(curscr, TRUE);
@@ -1443,6 +1547,7 @@ redraw(struct ui_state *s, const struct ui_backend *be)
{
draw_history(s);
draw_sidebar(s, SESS_ID(be), SESS_DIR(be), be->host(be->ctx));
draw_statusbar(s);
doupdate();
}
@@ -1538,6 +1643,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
draw_history(s);
}
draw_sidebar(s, SESS_ID(be), SESS_DIR(be), host);
draw_statusbar(s);
if (locked) {
werase(ui_input);
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
@@ -1567,6 +1673,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
clearok(curscr, TRUE);
draw_history(s);
draw_sidebar(s, SESS_ID(be), SESS_DIR(be), host);
draw_statusbar(s);
if (locked) {
werase(ui_input);
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
@@ -1613,6 +1720,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
s->scroll += (ch == KEY_SR ? 1 : 10);
draw_history(s);
draw_sidebar(s, SESS_ID(be), SESS_DIR(be), host);
draw_statusbar(s);
if (locked) {
werase(ui_input);
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
@@ -1628,6 +1736,7 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
s->scroll = s->scroll > step ? s->scroll - step : 0;
draw_history(s);
draw_sidebar(s, SESS_ID(be), SESS_DIR(be), host);
draw_statusbar(s);
if (locked) {
werase(ui_input);
mvwaddstr(ui_input, 0, 0, sp_td("ui.swarm_locked", "[swarm locked] "));
@@ -1666,8 +1775,8 @@ ui_read_line(struct ui_state *s, const struct ui_backend *be, char *input,
sync_state(s);
drain_idle(s);
draw_history(s);
draw_history(s);
draw_sidebar(s, SESS_ID(be), SESS_DIR(be), host);
draw_statusbar(s);
draw_prompt(input);
wnoutrefresh(ui_input);
doupdate();
@@ -1975,6 +2084,8 @@ turn_paint(struct ui_state *s, struct turn_view *v)
int w, used;
struct swarm *sw = s->be->swarm(s->be->ctx);
/* the meter lives in the engine; ask it before drawing, not after */
sync_state(s);
draw_history(s);
draw_sidebar(s, SESS_ID(s->be), SESS_DIR(s->be),
s->be->host(s->be->ctx));
@@ -1990,6 +2101,7 @@ turn_paint(struct ui_state *s, struct turn_view *v)
(now.tv_nsec - v->t0.tv_nsec) / 1000000);
s->last_ms = ms;
update_meter(s, s->be->host(s->be->ctx));
draw_statusbar(s);
werase(ui_input);
w = getmaxx(ui_input) - 1;
{
@@ -2002,6 +2114,17 @@ turn_paint(struct ui_state *s, struct turn_view *v)
snprintf(steer, sizeof(steer), "%s",
sp_td("ui.steer_mark", "steer> "));
/* a catalogue entry without the trailing space must not glue the
* typed text to the mark: steer>x reads like a syntax error */
{
size_t sl = strlen(steer);
if (sl > 0 && steer[sl - 1] != ' ' &&
sl + 1 < sizeof(steer)) {
steer[sl] = ' ';
steer[sl + 1] = '\0';
}
}
snprintf(note, sizeof(note), "%lds%s%.500s", ms / 1000,
v->note[0] != '\0' ? " " : "", v->note);
cut_line(note, w > (int)strlen(steer) + 2 ?
@@ -2305,6 +2428,7 @@ sp_ui_run(const struct ui_backend *be, const char *theme_name)
draw_history(&s);
draw_sidebar(&s, SESS_ID(be), SESS_DIR(be),
be->host(be->ctx));
draw_statusbar(&s);
werase(ui_input);
mvwaddstr(ui_input, 0, 0, prompt_mark());
wnoutrefresh(ui_input);
@@ -2393,6 +2517,7 @@ sp_ui_run(const struct ui_backend *be, const char *theme_name)
draw_history(&s);
draw_sidebar(&s, SESS_ID(be), SESS_DIR(be),
be->host(be->ctx));
draw_statusbar(&s);
werase(ui_input);
mvwaddstr(ui_input, 0, 0, sp_t("ui.working"));
wrefresh(ui_input);
+4
View File
@@ -163,6 +163,10 @@ send_state(struct client *c, struct sp_engine *eng, const char *name)
ev_str(&b, "agent", st.agent);
ev_num(&b, "tokens_in", (long)st.tokens_in);
ev_num(&b, "tokens_out", (long)st.tokens_out);
ev_num(&b, "ctx_used", (long)st.ctx_used);
ev_num(&b, "ctx_max", (long)st.ctx_max);
ev_num(&b, "cached", (long)st.cached);
ev_num(&b, "turn_tokens_out", (long)st.turn_tokens_out);
ev_num(&b, "steps", (long)st.steps);
ev_num(&b, "calls", (long)st.calls);
ev_num(&b, "compactions", (long)st.compactions);
+4
View File
@@ -257,6 +257,10 @@ fill_state(struct rem *r, const mcp_json *v)
r->st.tokens_in = (size_t)mcp_arg_int(v, "tokens_in", 0);
r->st.tokens_out = (size_t)mcp_arg_int(v, "tokens_out", 0);
r->st.ctx_used = (size_t)mcp_arg_int(v, "ctx_used", 0);
r->st.ctx_max = (size_t)mcp_arg_int(v, "ctx_max", 0);
r->st.cached = (size_t)mcp_arg_int(v, "cached", 0);
r->st.turn_tokens_out = (size_t)mcp_arg_int(v, "turn_tokens_out", 0);
r->st.steps = (size_t)mcp_arg_int(v, "steps", 0);
r->st.calls = (size_t)mcp_arg_int(v, "calls", 0);
r->st.compactions = (size_t)mcp_arg_int(v, "compactions", 0);
+8
View File
@@ -175,6 +175,10 @@ class H(http.server.BaseHTTPRequestHandler):
answer = "Datei:\n\n![Kein Bild](%s)\n" % p
chunks = [{"choices": [{"delta": {"content": answer}}]},
{"choices": [{"delta": {}, "finish_reason": "stop"}]}]
elif "viele zeilen" in last_user:
answer = "".join("Zeile %03d\n" % i for i in range(1, 121))
chunks = [{"choices": [{"delta": {"content": answer}}]},
{"choices": [{"delta": {}, "finish_reason": "stop"}]}]
elif "bild" in last_user:
answer = "Hier:\n\n![Ein Bild](https://example.org/b.png)\n\nEnde\n"
chunks = [{"choices": [{"delta": {"content": answer}}]},
@@ -201,6 +205,10 @@ class H(http.server.BaseHTTPRequestHandler):
return self.reply({"choices": [{"index": 0, "message": {
"role": "assistant", "content": answer},
"finish_reason": "stop"}]})
# the meter: context, cache share and tok/s in the status bar
chunks.append({"choices": [], "usage": {"prompt_tokens": 1290,
"completion_tokens": 50,
"prompt_tokens_details": {"cached_tokens": 800}}})
b = "".join("data: " + json.dumps(c) + "\n\n" for c in chunks) + "data: [DONE]\n\n"
self.reply(b.encode(), "text/event-stream")
+18 -3
View File
@@ -173,6 +173,19 @@ main(void)
CHECK(rep.tokens_in == 11 && rep.tokens_out == 7);
sp_reply_free(&rep);
/*
* input_tokens excludes the cache reads and writes; the context is the
* sum, the cache share is the read part.
*/
CHECK(sp_anthropic_reply("{\"type\":\"message\",\"content\":["
"{\"type\":\"text\",\"text\":\"ok\"}],"
"\"usage\":{\"input_tokens\":10,\"cache_read_input_tokens\":100,"
"\"cache_creation_input_tokens\":20,\"output_tokens\":3}}",
&rep, &err) == 0);
CHECK(rep.tokens_in == 130 && rep.tokens_out == 3);
CHECK(rep.tokens_cached == 100);
sp_reply_free(&rep);
/* a provider error is quoted, not turned into an empty answer */
CHECK(sp_anthropic_reply("{\"type\":\"error\",\"error\":"
"{\"type\":\"rate_limit_error\",\"message\":\"slow down\"}}",
@@ -184,8 +197,9 @@ main(void)
CHECK(sse != NULL);
CHECK(sp_as_sse_line(sse, "event: message_start", &delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"message_start\","
"\"message\":{\"usage\":{\"input_tokens\":21}}}", &delta,
&err) == 0);
"\"message\":{\"usage\":{\"input_tokens\":21,"
"\"cache_read_input_tokens\":100,"
"\"cache_creation_input_tokens\":20}}}", &delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_start\","
"\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}",
&delta, &err) == 0);
@@ -219,7 +233,8 @@ main(void)
CHECK(strcmp(rep.content, "Hello") == 0);
CHECK(rep.ncalls == 1);
CHECK(strcmp(rep.calls[0].args, "{\"path\":\"/tmp\"}") == 0);
CHECK(rep.tokens_in == 21 && rep.tokens_out == 9);
CHECK(rep.tokens_in == 141 && rep.tokens_out == 9);
CHECK(rep.tokens_cached == 100);
sp_reply_free(&rep);
sp_as_sse_free(sse);
+4
View File
@@ -56,6 +56,7 @@ main(void)
" plugins = [a.so, \"b c.so\"]\n"
" on_limit = wait\n"
" max_wait = 6h\n"
" ctx = 200000\n"
"}\n"
"telegram { allow = [1, 2] }\n"
"provider local {\n"
@@ -64,6 +65,7 @@ main(void)
" model = m\n"
" tags = [local, code]\n"
" prio = 5\n"
" ctx = 131072\n"
" limit = 10/5h\n"
"}\n"
"provider claude-a {\n"
@@ -90,11 +92,13 @@ main(void)
CHECK(strcmp(c->default_id, "claude") == 0);
CHECK(c->nplugins == 2 && strcmp(c->plugins[1], "b c.so") == 0);
CHECK(c->max_wait == 6 * 3600);
CHECK(c->ctx == 200000);
CHECK(strcmp(c->telegram_allow, "1,2") == 0);
CHECK(c->ndelegates == 2);
CHECK(strcmp(c->delegate[0].id, "local") == 0);
CHECK(strcmp(c->delegate[0].about, "the local one") == 0);
CHECK(strcmp(c->delegate[0].tags, "local,code") == 0);
CHECK(c->delegate[0].ctx == 131072);
CHECK(c->delegate[0].req_limit == 10 && c->delegate[0].req_window == 18000);
CHECK(c->delegate[1].nenv == 1);
CHECK(strstr(c->delegate[1].env[0], "=sekrit") != NULL);
+9
View File
@@ -97,6 +97,15 @@ main(void)
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\":"
+14
View File
@@ -117,6 +117,20 @@ main(void)
sp_reply_free(&r);
sp_sse_free(s);
/* ---- usage carries the cache share of the prompt ---- */
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":"
"100,\"completion_tokens\":5,\"prompt_tokens_details\":"
"{\"cached_tokens\":80}}}\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [DONE]\n", &out, &err) == 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.tokens_in == 100 && r.tokens_out == 5);
CHECK(r.tokens_cached == 80);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- a broken data line is an error that quotes itself ---- */
s = sp_sse_new();
sp_buf_clear(&err);
+20
View File
@@ -81,6 +81,7 @@ start() { # a fresh fake model and a fresh UI
exit 1
fi
tmux new-session -d -s $S -x 110 -y 30 "HOME=$D/home SPLINTER_LOCALE=en \
SPLINTER_CTX_MAX=131072 \
./splinter --no-config --endpoint http://127.0.0.1:$P/v1/chat/completions \
--model m --chat 2>/dev/null; echo \$? > $D/rc"
shows 'Enter sends'
@@ -116,6 +117,10 @@ check not_shows '{"path"'
key y
check shows 'fertig'
check shows '>> Write '
# the status bar above the prompt: context gauge, cache share, speed
check shows 'ctx 1290/131072'
check shows 'cache 62%'
check shows 'tok/s'
check grep -q 'vom gateway' "$D/out.txt"
typed 'halb'
check shows 'halb' # let the UI consume the keys
@@ -194,5 +199,20 @@ check shows 'removed'
check test ! -e "$D/home/.splinter/$new.jsonl"
check test "$(quit_code)" = 0
# ---- scrolling: reaching the top must not break PgDn ----
# The old keep-position check compared maxscroll with the first visible line;
# at the top that line was 0, so every later frame added the whole scrollback
# again and one PgDn jumped straight back. PgDn after the top must move down.
start 3
typed 'viele zeilen'; key Enter
check shows 'Zeile 120'
for i in $(seq 1 30); do key PPage; done
sleep 1
check shows 'ready. Enter sends' # at the very top
key NPage
sleep 1
check not_shows 'ready. Enter sends' # and PgDn leaves it again
check test "$(quit_code)" = 0
if [ "$fails" -eq 0 ]; then echo "test_ui: ok"; exit 0; fi
exit 1