Files
splinter/src/frontend/ui_local.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

300 lines
6.3 KiB
C

/*
* splinter -- the in-process backend for the curses front end.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Every call here is one engine call. The file is deliberately dull: it is
* the control in the experiment. Whatever --attach can do through the socket,
* this must do through the engine, or the protocol has a hole.
*
* The context is a pointer to the caller's engine pointer, not the engine:
* a session switch closes the engine and opens another one, and the front end
* must not be left holding the old pointer. `opts` is the template the new
* engine is built from.
*/
#include <stddef.h>
#include <string.h>
#include "splinter.h"
#include "splinter_engine.h"
#include "ui_backend.h"
struct loc {
struct sp_engine **ep;
const struct sp_engine_opts *opts;
};
#define ENG(ctx) (*((struct loc *)(ctx))->ep)
static int loc_input(void *ctx, const char *line, struct sp_buf *out)
{
return sp_engine_input(ENG(ctx), line, out);
}
static int loc_submit(void *ctx, const char *text)
{
return sp_engine_submit(ENG(ctx), text);
}
static int loc_busy(void *ctx)
{
return sp_engine_busy(ENG(ctx));
}
static int loc_wait(void *ctx)
{
return sp_engine_wait(ENG(ctx));
}
static int loc_take_steer(void *ctx, struct sp_buf *out)
{
return sp_engine_take_steer(ENG(ctx), out);
}
static int loc_cancel(void *ctx)
{
return sp_engine_cancel(ENG(ctx));
}
static int loc_steer(void *ctx, const char *text)
{
return sp_engine_steer(ENG(ctx), text);
}
static int loc_approval(void *ctx, char *name, size_t namelen, int *auth,
char *args, size_t argslen)
{
return sp_engine_approval(ENG(ctx), name, namelen, auth, args,
argslen);
}
static int loc_approve(void *ctx, int allow)
{
return sp_engine_approve(ENG(ctx), allow);
}
static int loc_poll(void *ctx, struct sp_event *out)
{
return sp_engine_poll(ENG(ctx), out);
}
static void loc_event_free(void *ctx, struct sp_event *ev)
{
(void)ctx;
sp_engine_event_free(ev);
}
static int loc_state(void *ctx, struct sp_engine_state *out)
{
struct loc *c = ctx;
return c->ep != NULL && *c->ep != NULL ?
sp_engine_state(*c->ep, out) : -1;
}
static int loc_agents(void *ctx)
{
return sp_engine_agents(ENG(ctx));
}
static const char *loc_agent_name(void *ctx, int idx)
{
return sp_engine_agent_name(ENG(ctx), idx);
}
static int loc_agent_cur(void *ctx)
{
return sp_engine_agent_cur(ENG(ctx));
}
static int loc_agent_switch(void *ctx, int idx)
{
return sp_engine_agent_switch(ENG(ctx), idx);
}
static int
loc_sessions(void *ctx, void (*cb)(void *, const char *, const char *, int,
long), void *cbctx)
{
struct sp_session_brief v[100];
const char *dir = sp_engine_state_dir(ENG(ctx));
const char *cur = sp_engine_session_id(ENG(ctx));
int n, i;
n = sp_session_list(dir, v, 100);
if (n < 0)
return 0;
for (i = 0; i < n; i++)
cb(cbctx, v[i].id, v[i].title,
strcmp(v[i].id, cur) == 0, v[i].mtime);
return n;
}
static int
loc_open_session(void *ctx, const char *id, struct sp_buf *err)
{
struct loc *c = ctx;
return sp_engine_switch(c->ep, c->opts, id, err);
}
static int loc_command_names(void *ctx, char (*names)[64], int max)
{
return sp_engine_command_names(ENG(ctx), names, max);
}
static int
loc_models(void *ctx, void (*cb)(void *, const char *, const char *, int),
void *cbctx)
{
struct sp_model_choice v[SP_MODEL_CHOICES];
size_t n, i;
n = sp_engine_models(ENG(ctx), v, SP_MODEL_CHOICES);
for (i = 0; i < n; i++)
cb(cbctx, v[i].sel, v[i].label, v[i].current);
return (int)n;
}
static int
loc_served(void *ctx, void (*cb)(void *, const char *, const char *, int),
void *cbctx)
{
struct sp_model_choice v[SP_MODEL_CHOICES];
size_t n, i;
n = sp_engine_served_models(ENG(ctx), v, SP_MODEL_CHOICES);
for (i = 0; i < n; i++)
cb(cbctx, v[i].sel, v[i].label, v[i].current);
return (int)n;
}
static int
loc_history(void *ctx, void (*cb)(void *, const char *, const char *),
void *cbctx)
{
const struct sp_conv *conv = sp_engine_conv(ENG(ctx));
size_t i;
int n = 0;
if (conv == NULL)
return 0;
for (i = 0; i < conv->n; i++) {
const struct sp_msg *m = &conv->v[i];
if (m->role == NULL || m->content == NULL)
continue;
if (strcmp(m->role, "user") != 0 &&
strcmp(m->role, "assistant") != 0)
continue;
cb(cbctx, m->role, m->content);
n++;
}
return n;
}
static struct sp_host *loc_host(void *ctx)
{
struct loc *c = ctx;
return c->ep != NULL && *c->ep != NULL ?
sp_engine_host(*c->ep) : NULL;
}
static struct swarm *loc_swarm(void *ctx)
{
return sp_engine_swarm(ENG(ctx));
}
static struct sp_config *loc_config(void *ctx)
{
struct loc *c = ctx;
return c->ep != NULL && *c->ep != NULL ?
sp_engine_config(*c->ep) : NULL;
}
static const char *loc_session_id(void *ctx)
{
struct loc *c = ctx;
return c->ep != NULL && *c->ep != NULL ?
sp_engine_session_id(*c->ep) : "";
}
static const char *loc_state_dir(void *ctx)
{
struct loc *c = ctx;
return c->ep != NULL && *c->ep != NULL ?
sp_engine_state_dir(*c->ep) : "";
}
static void loc_set_theme(void *ctx, const char *theme)
{
sp_engine_set_theme(ENG(ctx), theme);
}
static int loc_quit(void *ctx)
{
struct loc *c = ctx;
return c->ep != NULL && *c->ep != NULL ?
sp_engine_quit(*c->ep) : 1;
}
static int loc_local(void *ctx) { (void)ctx; return 1; }
static void loc_close(void *ctx)
{
(void)ctx;
}
static const struct ui_backend local = {
.input = loc_input,
.submit = loc_submit,
.busy = loc_busy,
.wait = loc_wait,
.cancel = loc_cancel,
.steer = loc_steer,
.take_steer = loc_take_steer,
.approval = loc_approval,
.approve = loc_approve,
.poll = loc_poll,
.event_free = loc_event_free,
.state = loc_state,
.agents = loc_agents,
.agent_name = loc_agent_name,
.agent_cur = loc_agent_cur,
.agent_switch = loc_agent_switch,
.command_names = loc_command_names,
.models = loc_models,
.served = loc_served,
.sessions = loc_sessions,
.open_session = loc_open_session,
.history = loc_history,
.host = loc_host,
.swarm = loc_swarm,
.config = loc_config,
.session_id = loc_session_id,
.state_dir = loc_state_dir,
.set_theme = loc_set_theme,
.quit = loc_quit,
.local = loc_local,
.close = loc_close
};
const struct ui_backend *
sp_ui_backend_engine(struct sp_engine **eng, const struct sp_engine_opts *opts)
{
static struct ui_backend be;
static struct loc c;
c.ep = eng;
c.opts = opts;
be = local;
be.ctx = &c;
return &be;
}