Files
splinter/include/splinter_engine.h
T
lotherk 1e15c91e4e interview: the model talks to the user, USER.txt rides in the prompt
The six-question form is gone. `splinter --interview` now opens the chat with an
interview system prompt: get to know the person, one question at a time, then
write a compact summary and end with INTERVIEW_COMPLETE.

- the marker is captured in the engine's turn thread and in the synchronous -p
  path: the summary before it is written to ~/.splinter/USER.txt (0600), the
  marker is stripped from the answer and the conversation, and a "interview
  saved to USER.txt" note goes into the transcript. An installation from before
  USER.txt keeps working: profile/<user>.md is the fallback until the first new
  interview.
- the USER.txt text is appended to the system prompt of every later session
  (plain "What you know about the user" block), including after /new and an
  agent switch (it rides in base_prompt).
- --interview is a chat mode with the interview prompt and no agent profiles;
  it needs a provider like any chat.
- Telegram: /interview swaps the system prompt, starts the conversation and
  restores the normal prompt once the summary is captured. The initial turn and
  every later reply are checked, so a model that summarizes immediately also
  works.
- test_interview.sh now proves capture, marker-free USER.txt and the summary in
  the next request; test_gateway drives /interview and the fake model answers
  with the marker when the interview prompt is in the request.
- docs (README, PLUGINS) describe the new flow.

make check: all tests passed.
2026-11-17 20:50:03 +00:00

259 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 interview; /* the model interviews the user */
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 *);
/*
* The choices /model takes - every provider, then every pool - with the model
* as the label and the current one marked. Configuration, not a network probe:
* a picker must open at once. Returns how many were written.
*/
size_t sp_engine_models(struct sp_engine *, struct sp_model_choice *,
size_t max);
/* -------------------------------------------------------------- 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 */