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.
250 lines
10 KiB
C
250 lines
10 KiB
C
/*
|
|
* splinter -- the engine every front end talks to.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* The engine is the whole harness minus the screen: it owns the session, the
|
|
* conversation, the tool registry, the plugins, the transport and the agent,
|
|
* runs one turn at a time on its own thread, and reports what happened as a
|
|
* queue of events.
|
|
*
|
|
* Why a queue and not callbacks: a turn runs on a thread of the engine's
|
|
* making, and a front end (curses, a socket, a GUI) must not be called from
|
|
* it -- it would have to be thread-safe in every drawing routine, and a slow
|
|
* front end would stall the model. The engine only appends to a queue under a
|
|
* mutex; the front end drains it whenever it is ready. Nothing here ever calls
|
|
* into a front end.
|
|
*
|
|
* The contract, in one paragraph: sp_engine_open() builds the engine from the
|
|
* command line and the config; sp_engine_input() turns a typed line into
|
|
* either a command reply or a prompt; sp_engine_submit() starts the turn;
|
|
* sp_engine_poll() delivers its events; sp_engine_approve(), sp_engine_steer()
|
|
* and sp_engine_cancel() are what a front end may do while it runs.
|
|
*
|
|
* This is the C boundary. The same engine is served over a line-oriented JSON
|
|
* socket by `splinter --serve`, so a GUI may be written in any language; see
|
|
* docs/PROTOCOL.md. The two front ends use exactly the calls below, which is
|
|
* what keeps them from drifting apart.
|
|
*/
|
|
#ifndef SPLINTER_ENGINE_H
|
|
#define SPLINTER_ENGINE_H
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "splinter.h"
|
|
|
|
struct sp_config;
|
|
struct sp_host;
|
|
struct sp_agent;
|
|
struct sp_conv;
|
|
struct sp_engine;
|
|
struct swarm;
|
|
|
|
#define SP_ENGINE_PLUGINS 32
|
|
|
|
/* ---------------------------------------------------------------- options */
|
|
|
|
/*
|
|
* What sp_engine_open() needs. Strings are copied; `conf` and `base` are read
|
|
* and not owned (main keeps them alive for the whole run), but the engine does
|
|
* change conf the way the old binary did: /model writes a drop-in, /connect
|
|
* adds a provider, a pool failover marks one benched.
|
|
*/
|
|
struct sp_engine_opts {
|
|
struct sp_config *conf;
|
|
const struct sp_agent_config *base; /* steps, byte budget, prompt */
|
|
const char *endpoint;
|
|
const char *model;
|
|
const char *key_env; /* variable holding the API key */
|
|
const char *transport; /* plugin transport ("cli"), or NULL */
|
|
const char *kind; /* "anthropic", or NULL */
|
|
const char *script; /* mock replies, one per line */
|
|
const char *agent; /* profile to start in */
|
|
const char *session_dir;
|
|
const char *session_id;
|
|
const char *plugins[SP_ENGINE_PLUGINS];
|
|
size_t nplugins;
|
|
int resume; /* load the session's history */
|
|
int continue_last; /* -c: newest session in this cwd */
|
|
int no_transport; /* tools only (MCP server, delegate) */
|
|
int swarm; /* join the local fleet */
|
|
};
|
|
|
|
/* ----------------------------------------------------------------- events */
|
|
|
|
enum sp_ev_kind {
|
|
SP_EV_NONE = 0,
|
|
SP_EV_BUSY, /* n: 1 the turn started, 0 it ended */
|
|
SP_EV_STREAM, /* n: SP_STREAM_TEXT|SP_STREAM_THINK, a: chunk */
|
|
SP_EV_STEP, /* a: the step's reasoning, b: its answer */
|
|
SP_EV_TOOL, /* a: name, b: arguments as sent */
|
|
SP_EV_TOOL_RESULT, /* a: name, b: observation, n: 1 joins the call */
|
|
SP_EV_NOTE, /* a: the prompt's one line, "" clears it */
|
|
SP_EV_ENTRY, /* a: role, b: text */
|
|
SP_EV_APPROVAL, /* a: name, b: args, n: authorization class */
|
|
SP_EV_TURN_END, /* n: rc, a: answer, b: error */
|
|
SP_EV_STATE, /* the model, tokens or profile changed */
|
|
SP_EV_CLEAR, /* /new: the transcript is the old conversation */
|
|
SP_EV_QUIT /* /quit was typed; the front end may close */
|
|
};
|
|
|
|
struct sp_event {
|
|
int kind;
|
|
long n;
|
|
char *a;
|
|
char *b;
|
|
};
|
|
|
|
/* 1 when an event was taken, 0 when the queue is empty. Free with
|
|
* sp_engine_event_free(). */
|
|
int sp_engine_poll(struct sp_engine *, struct sp_event *);
|
|
void sp_engine_event_free(struct sp_event *);
|
|
|
|
/* --------------------------------------------------------------- snapshot */
|
|
|
|
struct sp_engine_state {
|
|
char model[256];
|
|
char endpoint[512];
|
|
char agent[48];
|
|
char session[64];
|
|
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;
|
|
int quit; /* /quit was typed */
|
|
int waiting; /* the turn waits for an approval */
|
|
int agents_n;
|
|
int agent_cur;
|
|
};
|
|
|
|
int sp_engine_state(struct sp_engine *, struct sp_engine_state *);
|
|
|
|
/* -------------------------------------------------------------- lifecycle */
|
|
|
|
int sp_engine_open(struct sp_engine **, const struct sp_engine_opts *,
|
|
struct sp_buf *err);
|
|
void sp_engine_close(struct sp_engine *);
|
|
/*
|
|
* Move an engine to another session: close the old engine, open the new one
|
|
* from the same options, replace *ep. `id` is a session id, "new" for a fresh
|
|
* one, or NULL/"last" for the newest. The old engine is closed first because
|
|
* plugins keep process globals; a failed open reopens the session the engine
|
|
* was on, so the pointer is only NULL when even that fails. Returns 0 or -1
|
|
* with `err` set.
|
|
*/
|
|
int sp_engine_switch(struct sp_engine **ep, const struct sp_engine_opts *,
|
|
const char *id, struct sp_buf *err);
|
|
|
|
/* The pieces a front end may look at. The pointers stay valid until close. */
|
|
struct sp_agent *sp_engine_agent(struct sp_engine *);
|
|
struct sp_host *sp_engine_host(struct sp_engine *);
|
|
struct sp_config *sp_engine_config(struct sp_engine *);
|
|
struct sp_registry *sp_engine_registry(struct sp_engine *);
|
|
struct swarm *sp_engine_swarm(struct sp_engine *);
|
|
const struct sp_conv *sp_engine_conv(struct sp_engine *);
|
|
void sp_engine_set_theme(struct sp_engine *, const char *);
|
|
/* The default stream callback for async turns (the gateway has nobody to
|
|
* stream to and installs a sink; -p passes its own to sp_engine_prompt). */
|
|
void sp_engine_set_stream(struct sp_engine *, sp_stream_cb, void *ctx);
|
|
const char *sp_engine_state_dir(struct sp_engine *);
|
|
const char *sp_engine_session_id(struct sp_engine *);
|
|
|
|
/* ------------------------------------------------------------------ turns */
|
|
|
|
/*
|
|
* Start a turn. Returns 0, or -1 when one is already running: the engine runs
|
|
* exactly one turn at a time, because there is one conversation and one model.
|
|
*/
|
|
int sp_engine_submit(struct sp_engine *, const char *text);
|
|
int sp_engine_busy(struct sp_engine *);
|
|
int sp_engine_quit(struct sp_engine *);
|
|
/* Join the finished turn. Call it once sp_engine_busy() said 0, before the
|
|
* next submit; closing does it too. */
|
|
int sp_engine_wait(struct sp_engine *);
|
|
|
|
/* Ask the model once without the queue: what -p and the slash prompt use. */
|
|
int sp_engine_prompt(struct sp_engine *, const char *text, sp_stream_cb,
|
|
void *stream_ctx, struct sp_buf *answer, struct sp_buf *err);
|
|
|
|
/* ------------------------------------------------------- during one turn */
|
|
|
|
int sp_engine_cancel(struct sp_engine *);
|
|
/* Text typed while the turn runs: the loop picks it up at a step boundary.
|
|
* -1 when no turn is running. */
|
|
int sp_engine_steer(struct sp_engine *, const char *text);
|
|
/*
|
|
* A steer that arrived after the turn's last step boundary: the worker has
|
|
* finished, the text is still pending. A front end takes it and runs it as the
|
|
* next prompt, so typing into the last second is not lost. 0 while a turn is
|
|
* still running (the loop will take it) or when nothing is pending.
|
|
*/
|
|
int sp_engine_take_steer(struct sp_engine *, struct sp_buf *out);
|
|
/* 1 while a tool waits for the operator, with the call in the buffers. */
|
|
int sp_engine_approval(struct sp_engine *, char *name, size_t namelen,
|
|
int *auth, char *args, size_t argslen);
|
|
/* Answer it. 0 when nothing was pending. */
|
|
int sp_engine_approve(struct sp_engine *, int allow);
|
|
|
|
/* ------------------------------------------------------------------ input */
|
|
|
|
/*
|
|
* One line from the operator. Returns
|
|
*
|
|
* SP_INPUT_PROMPT a prompt for the model; start the turn with it
|
|
* SP_INPUT_HANDLED a command; `out` is the reply to show
|
|
* SP_INPUT_SUBMIT a command that replaced the line; `out` is the prompt
|
|
* to start a turn with (a program in commands/)
|
|
*
|
|
* The engine owns /model, /agent, /new, /connect, /usage, /status, /widgets,
|
|
* /swarm, /resume, /quit, !shell and every command a plugin registered, so a
|
|
* GUI gets the same set as the curses front end without re-implementing one.
|
|
*
|
|
* Call it while the engine is idle. A command changes the conversation, the
|
|
* transport and the session; doing that under a running turn is a front-end
|
|
* bug. sp_engine_conv() is a read-only view for the same reason: the messages
|
|
* are the worker's while a turn runs.
|
|
*/
|
|
#define SP_INPUT_PROMPT 0
|
|
#define SP_INPUT_HANDLED 1
|
|
#define SP_INPUT_SUBMIT 2
|
|
|
|
int sp_engine_input(struct sp_engine *, const char *line, struct sp_buf *out);
|
|
/* The names Tab completes, without the slash, sorted. Returns the count. */
|
|
int sp_engine_command_names(struct sp_engine *, char (*names)[64], int max);
|
|
|
|
/* Agent profiles: Tab's three calls. */
|
|
int sp_engine_agents(struct sp_engine *);
|
|
const char *sp_engine_agent_name(struct sp_engine *, int idx);
|
|
int sp_engine_agent_cur(struct sp_engine *);
|
|
int sp_engine_agent_switch(struct sp_engine *, int idx);
|
|
|
|
/* ------------------------------------------------------------- formatting */
|
|
|
|
/*
|
|
* How a tool call and its observation read, shared by every front end:
|
|
*
|
|
* Run du -sh ../xsrc
|
|
* Read src/frontend/curses.c
|
|
*
|
|
* Arguments are unescaped, the one that matters (command, path, ...) is
|
|
* picked, and the rest is left out. SP_OBS_JOIN says the observation is only
|
|
* " [exit status 0]" and belongs on the call's row; SP_OBS_NONE says there is
|
|
* nothing to show; SP_OBS_SHOWN means `out` is a block for the transcript.
|
|
*/
|
|
#define SP_OBS_NONE 0
|
|
#define SP_OBS_SHOWN 1
|
|
#define SP_OBS_JOIN 2
|
|
|
|
void sp_call_summary(const char *name, const char *args, char *out,
|
|
size_t cap);
|
|
int sp_obs_short(const char *obs, struct sp_buf *out);
|
|
|
|
#endif /* SPLINTER_ENGINE_H */
|