Files
splinter/plugins/status/status.c
T
lotherk af2001bfee ui, model, cost and idle timeouts
- lcm: the sidebar widget and /status segment show the context, the number of
  compactions, messages and bytes of the last cut, and how many old tool
  outputs were trimmed. The counters are the compactor's own, not an estimate.
- cost: a provider-reported USD cost (usage.cost/cost_usd) is carried through
  sse/openai, the agent and the engine. /cost reports it with the cache share
  and splits judged from plain turns; the notes say that an upstream judge is
  not visible in the provider's usage. The status plugin and junction prefer
  the reported number over list-price arithmetic.
- curses: the running turn's seconds moved from the input line to the status
  bar. Notes and errors are transcript rows below the cause, never text in the
  input line or the status bar; a failed backend request is a status row and is
  retried by the agent. The steer prompt is only "steer> " and what was typed.
- model: /model probe works in the chat. The picker lists the configuration at
  once, then asks the endpoint the session is on what it serves and adds those
  models, so a router's whole menu is selectable. provider/model selectors now
  apply the right provider instead of becoming a model name. The choice is
  written to <session>.model and restored when that session is reopened; /new
  carries the current model into the new session.
- sidebar: plugin titles are bold and underlined (this curses has no italic
  attribute), with a rule between the plugin blocks.
- run/cli: a command timeout is an idle window. Every chunk on stdout or
  stderr resets it, and the process is asked with waitpid(WNOHANG) whether it
  is still there before the window is extended. A long build that keeps
  reporting progress is no longer killed for being long; a silent hang is.
- config: ';' separates settings on one line, so
  plugin lcm { keep = 6; tool_trim = 8192 } parses as two keys.
- tests: lcm widget/status counters, one-line config blocks, session model
  restore and /model probe, cli idle timeout with a talkative and a silent
  command, run output reset, and the UI status/retry rows.

make check: all tests passed.
2026-11-17 23:21:56 +00:00

224 lines
5.6 KiB
C

/*
* 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 segments go to sp_host_add_status(): /status shows them in the
* transcript and the Telegram gateway sends them with its replies. The chat's
* status bar draws its own meters (context, cache, speed, the running turn);
* the segments are not squeezed onto that line.
*
* 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, "%H:%M", &tm);
}
static void
render_ctx(struct sp_host *h, char *buf, size_t len)
{
struct sp_meter mm;
const struct sp_meter *m = &mm;
char used[24], max[24], bar[BAR_WIDTH + 1];
size_t filled = 0, i;
sp_host_meter_get(h, &mm);
human(m->ctx_used, used, sizeof(used));
if (m->ctx_used == 0) {
buf[0] = '\0'; /* nothing sent yet: nothing to show */
return;
}
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)
{
struct sp_meter mm;
const struct sp_meter *m = &mm;
sp_host_meter_get(h, &mm);
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)
{
struct sp_meter mm;
const struct sp_meter *m = &mm;
char in[24], out[24];
sp_host_meter_get(h, &mm);
if (m->tok_in == 0 && m->tok_out == 0) {
buf[0] = '\0';
return;
}
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)
{
struct sp_meter mm;
const struct sp_meter *m = &mm;
sp_host_meter_get(h, &mm);
/*
* A provider that reports a cost is believed over our own arithmetic:
* a router may bill differently than its list price.
*/
if (m->cost > 0) {
snprintf(buf, len, "$%.4f", m->cost);
return;
}
if (price_in == 0 && price_out == 0) {
buf[0] = '\0';
return;
}
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)
{
struct sp_meter mm;
const struct sp_meter *m = &mm;
char hit[24];
sp_host_meter_get(h, &mm);
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)
{
struct sp_meter mm;
const struct sp_meter *m = &mm;
sp_host_meter_get(h, &mm);
if (m->tasks_max == 0 || m->tasks_running == 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;
}