- 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.
1143 lines
28 KiB
C
1143 lines
28 KiB
C
/*
|
|
* splinter -- the curses front end behind a socket.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* `splinter --attach` runs the same screen as `--chat`, but the engine is a
|
|
* `splinter --serve` in another process. This file turns the line protocol of
|
|
* docs/PROTOCOL.md into the operations ui_backend.h promises:
|
|
*
|
|
* - commands are sent as {"cmd":"input"} and answered with reply/submitted
|
|
* - a turn is {"cmd":"say"} (or was already started by input)
|
|
* - the event stream is decoded into struct sp_event, the same records the
|
|
* in-process backend returns, so the screen cannot tell the difference
|
|
* - state, agents and the command list are cached from the state/commands
|
|
* events
|
|
*
|
|
* Everything is synchronous on the UI thread: the screen polls, so a reader
|
|
* thread would only add a second queue and a shutdown problem. While a turn
|
|
* runs the server streams; the UI drains with a 100 ms tick.
|
|
*/
|
|
#include <errno.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_engine.h"
|
|
#include "mcp.h"
|
|
#include "protocol.h"
|
|
#include "i18n.h"
|
|
#include "ui_backend.h"
|
|
|
|
#define REM_MAX_AGENTS 32
|
|
#define REM_MAX_NAMES 64
|
|
|
|
struct rem_item {
|
|
char *role;
|
|
char *text;
|
|
};
|
|
|
|
struct rem_sess {
|
|
char id[64];
|
|
char title[96];
|
|
long mtime;
|
|
int current;
|
|
};
|
|
|
|
struct rem {
|
|
int fd;
|
|
struct sp_buf in; /* bytes not yet cut into lines */
|
|
struct sp_buf line; /* the line being handed to dispatch */
|
|
struct sp_event *q;
|
|
size_t nq, capq;
|
|
struct sp_engine_state st;
|
|
struct sp_buf last; /* the text of reply/submitted/error */
|
|
char agents[REM_MAX_AGENTS][48];
|
|
char names[REM_MAX_NAMES][64];
|
|
struct rem_item *hist;
|
|
size_t nhist;
|
|
int got_history;
|
|
struct rem_sess sess[100];
|
|
int nsess;
|
|
int got_sessions;
|
|
int got_opened;
|
|
int sess_gone; /* the restored session was not there */
|
|
char gone_old[64], gone_new[64];
|
|
int nagents, ncommands;
|
|
int pending; /* an approval question is open */
|
|
char a_name[64], a_args[256];
|
|
int a_auth;
|
|
int presubmitted; /* input started the turn already */
|
|
int proto; /* the server's protocol version */
|
|
int got_cmds; /* the commands event arrived */
|
|
char path[700]; /* the socket, for a reconnect */
|
|
long next_try; /* epoch ms of the next reconnect attempt */
|
|
int quiet_error; /* an expected error, no entry */
|
|
};
|
|
|
|
static int remote_live; /* one attached screen per process */
|
|
static int rem_connect(struct rem *r, struct sp_buf *err);
|
|
static void emit_history(struct rem *r);
|
|
|
|
static void
|
|
q_push(struct rem *r, int kind, long n, const char *a, const char *b)
|
|
{
|
|
struct sp_event *ev;
|
|
|
|
if (r->nq == r->capq) {
|
|
size_t nc = r->capq != 0 ? r->capq * 2 : 32;
|
|
struct sp_event *nq = realloc(r->q, nc * sizeof(*nq));
|
|
|
|
if (nq == NULL)
|
|
return;
|
|
r->q = nq;
|
|
r->capq = nc;
|
|
}
|
|
ev = &r->q[r->nq++];
|
|
ev->kind = kind;
|
|
ev->n = n;
|
|
ev->a = a != NULL ? strdup(a) : NULL;
|
|
ev->b = b != NULL ? strdup(b) : NULL;
|
|
}
|
|
|
|
static int
|
|
q_pop(struct rem *r, struct sp_event *out)
|
|
{
|
|
if (r->nq == 0)
|
|
return 0;
|
|
*out = r->q[0];
|
|
memmove(&r->q[0], &r->q[1], (r->nq - 1) * sizeof(r->q[0]));
|
|
r->nq--;
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
send_all(int fd, const char *p, size_t n)
|
|
{
|
|
while (n > 0) {
|
|
ssize_t w = send(fd, p, n, MSG_NOSIGNAL);
|
|
|
|
if (w < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
return -1;
|
|
}
|
|
p += w;
|
|
n -= (size_t)w;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
send_cmd(struct rem *r, const char *cmd, const char *key, const char *val)
|
|
{
|
|
struct sp_buf b;
|
|
int rc;
|
|
|
|
if (sp_buf_init(&b) != 0)
|
|
return -1;
|
|
sp_buf_printf(&b, "{\"cmd\":\"%s\"", cmd);
|
|
if (key != NULL && val != NULL) {
|
|
sp_buf_printf(&b, ",\"%s\":\"", key);
|
|
(void)sp_json_escape(&b, val);
|
|
sp_buf_puts(&b, "\"");
|
|
}
|
|
sp_buf_puts(&b, "}\n");
|
|
rc = send_all(r->fd, b.p, b.len);
|
|
sp_buf_free(&b);
|
|
return rc;
|
|
}
|
|
|
|
/* one non-blocking read into the line buffer; 1 data, 0 none, -1 gone */
|
|
static int
|
|
fill(struct rem *r, int timeout_ms)
|
|
{
|
|
struct pollfd p = { r->fd, POLLIN, 0 };
|
|
char chunk[4096];
|
|
ssize_t got;
|
|
int pr;
|
|
|
|
pr = poll(&p, 1, timeout_ms);
|
|
if (pr < 0)
|
|
return errno == EINTR ? 0 : -1;
|
|
if (pr == 0)
|
|
return 0;
|
|
if (p.revents & (POLLERR | POLLNVAL))
|
|
return -1;
|
|
if (p.revents & POLLIN) {
|
|
got = recv(r->fd, chunk, sizeof(chunk), 0);
|
|
if (got <= 0)
|
|
return -1;
|
|
if (sp_buf_add(&r->in, chunk, (size_t)got) != 0)
|
|
return -1;
|
|
/*
|
|
* A peer that sent its last events and closed reports POLLIN
|
|
* and POLLHUP together. The bytes just read are the answer;
|
|
* returning -1 here dropped them.
|
|
*/
|
|
if (p.revents & POLLHUP)
|
|
return got > 0 ? 1 : -1;
|
|
return 1;
|
|
}
|
|
if (p.revents & POLLHUP)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* 1 a complete line in r->line, 0 none buffered. The line grows with the
|
|
* event: a tool observation or a command reply may be larger than any fixed
|
|
* buffer, and a truncated JSON line is an event the front end silently loses.
|
|
*/
|
|
static int
|
|
take_line(struct rem *r)
|
|
{
|
|
char *nl = memchr(r->in.p, '\n', r->in.len);
|
|
size_t len, used;
|
|
|
|
if (nl == NULL)
|
|
return 0;
|
|
len = (size_t)(nl - r->in.p);
|
|
sp_buf_clear(&r->line);
|
|
if (sp_buf_add(&r->line, r->in.p, len) != 0)
|
|
return 0;
|
|
if (r->line.len > 0 && r->line.p[r->line.len - 1] == '\r')
|
|
r->line.p[--r->line.len] = '\0';
|
|
used = len + 1;
|
|
memmove(r->in.p, r->in.p + used, r->in.len - used);
|
|
r->in.len -= used;
|
|
r->in.p[r->in.len] = '\0';
|
|
return 1;
|
|
}
|
|
|
|
static long
|
|
now_ms(void)
|
|
{
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
return (long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
|
}
|
|
|
|
/* wait up to timeout_ms for one line; 1 line, 0 timeout, -1 connection gone */
|
|
static int
|
|
next_line(struct rem *r, int timeout_ms)
|
|
{
|
|
long deadline = now_ms() + timeout_ms;
|
|
|
|
for (;;) {
|
|
int left, rc;
|
|
|
|
if (take_line(r))
|
|
return 1;
|
|
left = (int)(deadline - now_ms());
|
|
if (left < 0)
|
|
left = 0;
|
|
rc = fill(r, left);
|
|
if (rc < 0)
|
|
return -1;
|
|
if (rc == 0 && now_ms() >= deadline)
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/* cache the state event: everything a sidebar or a Tab needs */
|
|
static void
|
|
fill_state(struct rem *r, const mcp_json *v)
|
|
{
|
|
const char *s;
|
|
const mcp_json *ag;
|
|
|
|
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);
|
|
r->st.last_ms = (size_t)mcp_arg_int(v, "last_ms", 0);
|
|
r->st.busy = mcp_arg_bool(v, "busy", 0);
|
|
r->st.waiting = mcp_arg_bool(v, "waiting", 0);
|
|
r->st.quit = mcp_arg_bool(v, "quit", 0);
|
|
r->st.agent_cur = mcp_arg_int(v, "agent_cur", 0);
|
|
if ((s = mcp_arg_string(v, "session")) != NULL)
|
|
snprintf(r->st.session, sizeof(r->st.session), "%s", s);
|
|
if ((s = mcp_arg_string(v, "dir")) != NULL)
|
|
snprintf(r->st.dir, sizeof(r->st.dir), "%s", s);
|
|
if ((s = mcp_arg_string(v, "model")) != NULL)
|
|
snprintf(r->st.model, sizeof(r->st.model), "%s", s);
|
|
if ((s = mcp_arg_string(v, "endpoint")) != NULL)
|
|
snprintf(r->st.endpoint, sizeof(r->st.endpoint), "%s", s);
|
|
if ((s = mcp_arg_string(v, "agent")) != NULL)
|
|
snprintf(r->st.agent, sizeof(r->st.agent), "%s", s);
|
|
ag = mcp_arg(v, "agents");
|
|
r->nagents = 0;
|
|
if (ag != NULL) {
|
|
size_t i, n = mcp_array_len(ag);
|
|
|
|
for (i = 0; i < n && r->nagents < REM_MAX_AGENTS; i++) {
|
|
const char *name = mcp_json_string(mcp_array_at(ag, i));
|
|
|
|
if (name != NULL)
|
|
snprintf(r->agents[r->nagents++],
|
|
sizeof(r->agents[0]), "%s", name);
|
|
}
|
|
}
|
|
r->st.agents_n = r->nagents;
|
|
}
|
|
|
|
enum { D_EVENT = 0, D_REPLY, D_SUBMITTED, D_ERROR };
|
|
|
|
static void
|
|
copy_last(struct rem *r, const char *s)
|
|
{
|
|
sp_buf_clear(&r->last);
|
|
(void)sp_buf_puts(&r->last, s != NULL ? s : "");
|
|
}
|
|
|
|
/* one protocol line into events and caches; the D_* says what it was */
|
|
static int
|
|
dispatch(struct rem *r, const char *line)
|
|
{
|
|
mcp_json *v = mcp_parse(line);
|
|
const char *ev, *s;
|
|
int rc = D_EVENT;
|
|
|
|
if (v == NULL)
|
|
return D_EVENT;
|
|
ev = mcp_arg_string(v, "ev");
|
|
if (ev == NULL) {
|
|
mcp_json_free(v);
|
|
return D_EVENT;
|
|
}
|
|
if (strcmp(ev, "state") == 0) {
|
|
fill_state(r, v);
|
|
/* the screen updates its counters when it drains this one */
|
|
q_push(r, SP_EV_STATE, 0, NULL, NULL);
|
|
} else if (strcmp(ev, "ready") == 0) {
|
|
r->proto = mcp_arg_int(v, "protocol", 0);
|
|
} else if (strcmp(ev, "history") == 0) {
|
|
const mcp_json *items = mcp_arg(v, "items");
|
|
size_t i, n = items != NULL ? mcp_array_len(items) : 0;
|
|
|
|
for (i = 0; i < r->nhist; i++) {
|
|
free(r->hist[i].role);
|
|
free(r->hist[i].text);
|
|
}
|
|
free(r->hist);
|
|
r->hist = n > 0 ? calloc(n, sizeof(*r->hist)) : NULL;
|
|
r->nhist = 0;
|
|
if (r->hist != NULL) {
|
|
for (i = 0; i < n; i++) {
|
|
const mcp_json *it = mcp_array_at(items, i);
|
|
const char *role = mcp_arg_string(it, "role");
|
|
const char *text = mcp_arg_string(it, "text");
|
|
|
|
if (role == NULL || text == NULL)
|
|
continue;
|
|
r->hist[r->nhist].role = strdup(role);
|
|
r->hist[r->nhist].text = strdup(text);
|
|
if (r->hist[r->nhist].role != NULL &&
|
|
r->hist[r->nhist].text != NULL)
|
|
r->nhist++;
|
|
else {
|
|
free(r->hist[r->nhist].role);
|
|
free(r->hist[r->nhist].text);
|
|
}
|
|
}
|
|
}
|
|
r->got_history = 1;
|
|
} else if (strcmp(ev, "sessions") == 0) {
|
|
const mcp_json *items = mcp_arg(v, "items");
|
|
size_t i, n = items != NULL ? mcp_array_len(items) : 0;
|
|
|
|
r->nsess = 0;
|
|
for (i = 0; i < n && r->nsess < 100; i++) {
|
|
const mcp_json *it = mcp_array_at(items, i);
|
|
const char *id = mcp_arg_string(it, "id");
|
|
const char *title = mcp_arg_string(it, "title");
|
|
|
|
if (id == NULL)
|
|
continue;
|
|
snprintf(r->sess[r->nsess].id,
|
|
sizeof(r->sess[0].id), "%s", id);
|
|
snprintf(r->sess[r->nsess].title,
|
|
sizeof(r->sess[0].title), "%s",
|
|
title != NULL ? title : "");
|
|
r->sess[r->nsess].mtime = mcp_arg_int(it, "mtime", 0);
|
|
r->sess[r->nsess].current =
|
|
mcp_arg_bool(it, "current", 0);
|
|
r->nsess++;
|
|
}
|
|
r->got_sessions = 1;
|
|
} else if (strcmp(ev, "opened") == 0) {
|
|
r->got_opened = 1;
|
|
} else if (strcmp(ev, "commands") == 0) {
|
|
const mcp_json *names = mcp_arg(v, "names");
|
|
|
|
r->got_cmds = 1;
|
|
r->ncommands = 0;
|
|
if (names != NULL) {
|
|
size_t i, n = mcp_array_len(names);
|
|
|
|
for (i = 0; i < n && r->ncommands < REM_MAX_NAMES; i++) {
|
|
const char *nm = mcp_json_string(
|
|
mcp_array_at(names, i));
|
|
|
|
if (nm != NULL)
|
|
snprintf(r->names[r->ncommands++],
|
|
sizeof(r->names[0]), "%s", nm);
|
|
}
|
|
}
|
|
} else if (strcmp(ev, "busy") == 0) {
|
|
r->st.busy = mcp_arg_bool(v, "on", 0);
|
|
q_push(r, SP_EV_BUSY, r->st.busy, NULL, NULL);
|
|
} else if (strcmp(ev, "stream") == 0) {
|
|
int kind = strcmp(mcp_arg_string(v, "kind") != NULL ?
|
|
mcp_arg_string(v, "kind") : "text", "think") == 0 ?
|
|
SP_STREAM_THINK : SP_STREAM_TEXT;
|
|
|
|
q_push(r, SP_EV_STREAM, kind, mcp_arg_string(v, "text"), NULL);
|
|
} else if (strcmp(ev, "step") == 0) {
|
|
q_push(r, SP_EV_STEP, 0, mcp_arg_string(v, "think"),
|
|
mcp_arg_string(v, "text"));
|
|
} else if (strcmp(ev, "tool") == 0) {
|
|
q_push(r, SP_EV_TOOL, 0, mcp_arg_string(v, "name"),
|
|
mcp_arg_string(v, "args"));
|
|
} else if (strcmp(ev, "tool_result") == 0) {
|
|
q_push(r, SP_EV_TOOL_RESULT,
|
|
mcp_arg_bool(v, "join", 0) ? 1 : 0,
|
|
mcp_arg_string(v, "name"), mcp_arg_string(v, "text"));
|
|
} else if (strcmp(ev, "note") == 0) {
|
|
q_push(r, SP_EV_NOTE, 0, mcp_arg_string(v, "text"), NULL);
|
|
} else if (strcmp(ev, "entry") == 0) {
|
|
q_push(r, SP_EV_ENTRY, 0, mcp_arg_string(v, "role"),
|
|
mcp_arg_string(v, "text"));
|
|
} else if (strcmp(ev, "approval") == 0) {
|
|
r->pending = 1;
|
|
r->a_auth = mcp_arg_int(v, "auth", SP_AUTH_MUTATE);
|
|
snprintf(r->a_name, sizeof(r->a_name), "%s",
|
|
mcp_arg_string(v, "name") != NULL ?
|
|
mcp_arg_string(v, "name") : "?");
|
|
snprintf(r->a_args, sizeof(r->a_args), "%s",
|
|
mcp_arg_string(v, "args") != NULL ?
|
|
mcp_arg_string(v, "args") : "");
|
|
q_push(r, SP_EV_APPROVAL, r->a_auth, r->a_name, r->a_args);
|
|
} else if (strcmp(ev, "turn") == 0) {
|
|
q_push(r, SP_EV_TURN_END, mcp_arg_int(v, "rc", 1),
|
|
mcp_arg_string(v, "answer"), mcp_arg_string(v, "error"));
|
|
} else if (strcmp(ev, "clear") == 0) {
|
|
q_push(r, SP_EV_CLEAR, 0, NULL, NULL);
|
|
} else if (strcmp(ev, "quit") == 0) {
|
|
r->st.quit = 1;
|
|
q_push(r, SP_EV_QUIT, 0, NULL, NULL);
|
|
} else if (strcmp(ev, "reply") == 0) {
|
|
copy_last(r, mcp_arg_string(v, "text"));
|
|
rc = D_REPLY;
|
|
} else if (strcmp(ev, "submitted") == 0) {
|
|
copy_last(r, mcp_arg_string(v, "text"));
|
|
r->presubmitted = 1;
|
|
r->st.busy = 1;
|
|
rc = D_SUBMITTED;
|
|
} else if (strcmp(ev, "error") == 0) {
|
|
s = mcp_arg_string(v, "text");
|
|
copy_last(r, s != NULL ? s : "protocol error");
|
|
if (!r->quiet_error)
|
|
q_push(r, SP_EV_ENTRY, 0, "error",
|
|
s != NULL ? s : "protocol error");
|
|
rc = D_ERROR;
|
|
}
|
|
mcp_json_free(v);
|
|
return rc;
|
|
}
|
|
|
|
/* ------------------------------------------------------------- operations */
|
|
|
|
static int
|
|
rem_input(void *ctx, const char *line, struct sp_buf *out)
|
|
{
|
|
struct rem *r = ctx;
|
|
|
|
if (r->fd < 0 || send_cmd(r, "input", "line", line) != 0) {
|
|
sp_buf_puts(out, "not connected to the backend");
|
|
return SP_INPUT_HANDLED;
|
|
}
|
|
for (;;) {
|
|
int rc = next_line(r, 30000);
|
|
|
|
if (rc < 0) {
|
|
sp_buf_puts(out, "connection to the backend lost");
|
|
return SP_INPUT_HANDLED;
|
|
}
|
|
if (rc == 0) {
|
|
sp_buf_puts(out, "the backend did not answer");
|
|
return SP_INPUT_HANDLED;
|
|
}
|
|
rc = dispatch(r, r->line.p);
|
|
if (rc == D_REPLY) {
|
|
(void)sp_buf_puts(out, r->last.p != NULL ? r->last.p : "");
|
|
return SP_INPUT_HANDLED;
|
|
}
|
|
if (rc == D_ERROR) {
|
|
/*
|
|
* dispatch queued an error entry; the screen shows it
|
|
* when it drains. Returning it here too printed it
|
|
* twice.
|
|
*/
|
|
return SP_INPUT_HANDLED;
|
|
}
|
|
if (rc == D_SUBMITTED) {
|
|
/*
|
|
* The server already started this turn. The screen adds
|
|
* the line and calls submit; that call is the no-op.
|
|
*/
|
|
(void)sp_buf_puts(out, r->last.p != NULL ? r->last.p : "");
|
|
return SP_INPUT_PROMPT;
|
|
}
|
|
/* an event (state, entry, ...): queued, keep waiting */
|
|
}
|
|
}
|
|
|
|
static int
|
|
rem_submit(void *ctx, const char *text)
|
|
{
|
|
struct rem *r = ctx;
|
|
|
|
if (r->presubmitted) {
|
|
r->presubmitted = 0;
|
|
return 0;
|
|
}
|
|
if (r->fd < 0)
|
|
return -1;
|
|
if (send_cmd(r, "say", "text", text) != 0)
|
|
return -1;
|
|
r->st.busy = 1;
|
|
return 0;
|
|
}
|
|
|
|
static int rem_busy(void *ctx) { return ((struct rem *)ctx)->st.busy; }
|
|
static int rem_wait(void *ctx) { (void)ctx; return 0; }
|
|
|
|
static int
|
|
rem_cancel(void *ctx)
|
|
{
|
|
struct rem *r = ctx;
|
|
|
|
if (r->fd < 0)
|
|
return -1;
|
|
return send_cmd(r, "cancel", NULL, NULL);
|
|
}
|
|
|
|
static int
|
|
rem_steer(void *ctx, const char *text)
|
|
{
|
|
struct rem *r = ctx;
|
|
|
|
if (r->fd < 0)
|
|
return -1;
|
|
return send_cmd(r, "steer", "text", text);
|
|
}
|
|
|
|
static int
|
|
rem_approval(void *ctx, char *name, size_t namelen, int *auth, char *args,
|
|
size_t argslen)
|
|
{
|
|
struct rem *r = ctx;
|
|
|
|
if (!r->pending)
|
|
return 0;
|
|
if (name != NULL && namelen > 0)
|
|
snprintf(name, namelen, "%s", r->a_name);
|
|
if (args != NULL && argslen > 0)
|
|
snprintf(args, argslen, "%s", r->a_args);
|
|
if (auth != NULL)
|
|
*auth = r->a_auth;
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
rem_approve(void *ctx, int allow)
|
|
{
|
|
struct rem *r = ctx;
|
|
struct sp_buf b;
|
|
int rc;
|
|
|
|
if (!r->pending)
|
|
return 0;
|
|
r->pending = 0;
|
|
if (r->fd < 0 || sp_buf_init(&b) != 0)
|
|
return 0;
|
|
sp_buf_printf(&b, "{\"cmd\":\"approve\",\"allow\":%s}\n",
|
|
allow ? "true" : "false");
|
|
rc = send_all(r->fd, b.p, b.len);
|
|
sp_buf_free(&b);
|
|
return rc == 0;
|
|
}
|
|
|
|
static int
|
|
rem_poll(void *ctx, struct sp_event *out)
|
|
{
|
|
struct rem *r = ctx;
|
|
|
|
if (q_pop(r, out))
|
|
return 1;
|
|
if (r->fd < 0) {
|
|
/* disconnected: retry once a second, keep the screen alive */
|
|
if (now_ms() < r->next_try)
|
|
return 0;
|
|
if (rem_connect(r, NULL) != 0) {
|
|
r->next_try = now_ms() + 1000;
|
|
return 0;
|
|
}
|
|
/*
|
|
* The clear in emit_history wipes the screen, so a note about a
|
|
* session that did not come back has to follow it.
|
|
*/
|
|
emit_history(r);
|
|
if (r->sess_gone) {
|
|
struct sp_buf note;
|
|
|
|
if (sp_buf_init(¬e) == 0) {
|
|
sp_buf_printf(¬e, sp_t("ui.session_gone"),
|
|
r->gone_old, r->gone_new);
|
|
q_push(r, SP_EV_ENTRY, 0, "splinter",
|
|
note.p != NULL ? note.p : "");
|
|
sp_buf_free(¬e);
|
|
}
|
|
r->sess_gone = 0;
|
|
}
|
|
q_push(r, SP_EV_ENTRY, 0, "splinter", sp_t("ui.reconnected"));
|
|
return q_pop(r, out);
|
|
}
|
|
for (;;) {
|
|
int rc = next_line(r, 0);
|
|
|
|
if (rc < 0) {
|
|
/*
|
|
* The backend went away. Not a quit: the client keeps
|
|
* running and tries to attach again, with the session
|
|
* it had. The in-flight turn is gone with the server.
|
|
*/
|
|
int was = r->fd;
|
|
|
|
close(was);
|
|
r->fd = -1;
|
|
r->st.busy = 0;
|
|
r->pending = 0;
|
|
r->next_try = now_ms() + 1000;
|
|
q_push(r, SP_EV_ENTRY, 0, "splinter",
|
|
sp_t("ui.reconnecting"));
|
|
return q_pop(r, out);
|
|
}
|
|
if (rc == 0)
|
|
return 0;
|
|
(void)dispatch(r, r->line.p);
|
|
if (q_pop(r, out))
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
static void
|
|
rem_event_free(void *ctx, struct sp_event *ev)
|
|
{
|
|
(void)ctx;
|
|
if (ev == NULL)
|
|
return;
|
|
free(ev->a);
|
|
free(ev->b);
|
|
ev->a = ev->b = NULL;
|
|
}
|
|
|
|
static int
|
|
rem_state(void *ctx, struct sp_engine_state *out)
|
|
{
|
|
struct rem *r = ctx;
|
|
|
|
if (out == NULL)
|
|
return -1;
|
|
*out = r->st;
|
|
return 0;
|
|
}
|
|
|
|
static int rem_agents(void *ctx) { return ((struct rem *)ctx)->nagents; }
|
|
|
|
static const char *
|
|
rem_agent_name(void *ctx, int idx)
|
|
{
|
|
struct rem *r = ctx;
|
|
|
|
if (idx < 0 || idx >= r->nagents)
|
|
return "";
|
|
return r->agents[idx];
|
|
}
|
|
|
|
static int rem_agent_cur(void *ctx) { return ((struct rem *)ctx)->st.agent_cur; }
|
|
|
|
static int
|
|
rem_agent_switch(void *ctx, int idx)
|
|
{
|
|
struct rem *r = ctx;
|
|
char line[96];
|
|
struct sp_buf out;
|
|
|
|
if (idx < 0 || idx >= r->nagents)
|
|
return -1;
|
|
snprintf(line, sizeof(line), "/agent %s", r->agents[idx]);
|
|
if (sp_buf_init(&out) != 0)
|
|
return -1;
|
|
(void)rem_input(r, line, &out);
|
|
sp_buf_free(&out);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
rem_command_names(void *ctx, char (*names)[64], int max)
|
|
{
|
|
struct rem *r = ctx;
|
|
int i, n = 0;
|
|
|
|
for (i = 0; i < r->ncommands && n < max; i++)
|
|
snprintf(names[n++], 64, "%s", r->names[i]);
|
|
return n;
|
|
}
|
|
|
|
/*
|
|
* The picker's list from the server: `models` answers one event with every
|
|
* selector; probe also asks the serving endpoint. Other events that arrive
|
|
* while we wait (state, busy) still go through dispatch, so nothing is lost.
|
|
*/
|
|
static int
|
|
rem_models_probe(void *ctx,
|
|
void (*cb)(void *, const char *, const char *, int), void *cbctx, int probe)
|
|
{
|
|
struct rem *r = ctx;
|
|
long deadline;
|
|
int n = 0;
|
|
|
|
if (r->fd < 0 || send_cmd(r, "models", probe ? "probe" : NULL,
|
|
probe ? "true" : NULL) != 0)
|
|
return 0;
|
|
deadline = now_ms() + (probe ? 15000 : 5000);
|
|
while (now_ms() < deadline) {
|
|
mcp_json *v;
|
|
const char *ev;
|
|
int rc = next_line(r, 5000);
|
|
|
|
if (rc <= 0)
|
|
break;
|
|
v = mcp_parse(r->line.p);
|
|
ev = v != NULL ? mcp_arg_string(v, "ev") : NULL;
|
|
if (ev != NULL && strcmp(ev, "models") == 0) {
|
|
const mcp_json *items = mcp_arg(v, "items");
|
|
size_t i, len = mcp_array_len(items);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
const mcp_json *it = mcp_array_at(items, i);
|
|
const char *sel = mcp_arg_string(it, "sel");
|
|
const char *label = mcp_arg_string(it,
|
|
"label");
|
|
|
|
if (sel == NULL)
|
|
continue;
|
|
cb(cbctx, sel, label != NULL ? label : sel,
|
|
mcp_arg_bool(it, "current", 0));
|
|
n++;
|
|
}
|
|
mcp_json_free(v);
|
|
break;
|
|
}
|
|
(void)dispatch(r, r->line.p);
|
|
mcp_json_free(v);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static int
|
|
rem_models(void *ctx, void (*cb)(void *, const char *, const char *, int),
|
|
void *cbctx)
|
|
{
|
|
return rem_models_probe(ctx, cb, cbctx, 0);
|
|
}
|
|
|
|
static int
|
|
rem_served(void *ctx, void (*cb)(void *, const char *, const char *, int),
|
|
void *cbctx)
|
|
{
|
|
return rem_models_probe(ctx, cb, cbctx, 1);
|
|
}
|
|
|
|
static void
|
|
emit_history(struct rem *r)
|
|
{
|
|
size_t i;
|
|
|
|
q_push(r, SP_EV_CLEAR, 0, NULL, NULL);
|
|
for (i = 0; i < r->nhist; i++)
|
|
q_push(r, SP_EV_ENTRY, 0,
|
|
strcmp(r->hist[i].role, "user") == 0 ? "you" : "agent",
|
|
r->hist[i].text);
|
|
}
|
|
|
|
static int rem_take_steer(void *ctx, struct sp_buf *out)
|
|
{
|
|
/* the server owns the turn and folds a late steer into the next prompt */
|
|
(void)ctx;
|
|
(void)out;
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
rem_sessions(void *ctx, void (*cb)(void *, const char *, const char *, int,
|
|
long), void *cbctx)
|
|
{
|
|
struct rem *r = ctx;
|
|
long deadline;
|
|
int i;
|
|
|
|
if (r->fd < 0 || send_cmd(r, "sessions", NULL, NULL) != 0)
|
|
return 0;
|
|
r->got_sessions = 0;
|
|
deadline = now_ms() + 5000;
|
|
while (!r->got_sessions && now_ms() < deadline) {
|
|
int rc = next_line(r, 5000);
|
|
|
|
if (rc <= 0)
|
|
break;
|
|
(void)dispatch(r, r->line.p);
|
|
}
|
|
for (i = 0; i < r->nsess; i++)
|
|
cb(cbctx, r->sess[i].id, r->sess[i].title,
|
|
r->sess[i].current, r->sess[i].mtime);
|
|
return r->nsess;
|
|
}
|
|
|
|
static int
|
|
rem_open_session(void *ctx, const char *id, struct sp_buf *err)
|
|
{
|
|
struct rem *r = ctx;
|
|
long deadline;
|
|
char why[512] = "";
|
|
|
|
if (r->fd < 0) {
|
|
sp_buf_puts(err, "not connected to the backend");
|
|
return -1;
|
|
}
|
|
if (send_cmd(r, "open", "session", id != NULL ? id : "last") != 0) {
|
|
sp_buf_puts(err, "cannot send open");
|
|
return -1;
|
|
}
|
|
r->got_opened = 0;
|
|
deadline = now_ms() + 30000;
|
|
while (!r->got_opened && now_ms() < deadline) {
|
|
mcp_json *v;
|
|
const char *ev;
|
|
int rc = next_line(r, 30000);
|
|
|
|
if (rc <= 0)
|
|
break;
|
|
v = mcp_parse(r->line.p);
|
|
ev = v != NULL ? mcp_arg_string(v, "ev") : NULL;
|
|
if (ev != NULL && strcmp(ev, "error") == 0) {
|
|
const char *t = mcp_arg_string(v, "text");
|
|
|
|
snprintf(why, sizeof(why), "%s", t != NULL ? t :
|
|
"cannot open the session");
|
|
}
|
|
(void)dispatch(r, r->line.p);
|
|
mcp_json_free(v);
|
|
if (why[0] != '\0')
|
|
break;
|
|
}
|
|
if (why[0] != '\0') {
|
|
sp_buf_puts(err, why);
|
|
return -1;
|
|
}
|
|
if (!r->got_opened) {
|
|
sp_buf_puts(err, "the backend did not confirm the session");
|
|
return -1;
|
|
}
|
|
/* the cached history is now the new session's; the screen replays it */
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
rem_history(void *ctx, void (*cb)(void *, const char *, const char *),
|
|
void *cbctx)
|
|
{
|
|
struct rem *r = ctx;
|
|
size_t i;
|
|
|
|
for (i = 0; i < r->nhist; i++)
|
|
cb(cbctx, r->hist[i].role, r->hist[i].text);
|
|
return (int)r->nhist;
|
|
}
|
|
|
|
static struct sp_host *rem_host(void *ctx) { (void)ctx; return NULL; }
|
|
static struct swarm *rem_swarm(void *ctx) { (void)ctx; return NULL; }
|
|
static struct sp_config *rem_config(void *ctx) { (void)ctx; return NULL; }
|
|
|
|
static const char *
|
|
rem_session_id(void *ctx)
|
|
{
|
|
return ((struct rem *)ctx)->st.session;
|
|
}
|
|
|
|
static const char *
|
|
rem_state_dir(void *ctx)
|
|
{
|
|
return ((struct rem *)ctx)->st.dir;
|
|
}
|
|
|
|
static void rem_set_theme(void *ctx, const char *theme) { (void)ctx; (void)theme; }
|
|
|
|
static int rem_quit(void *ctx) { return ((struct rem *)ctx)->st.quit; }
|
|
|
|
static int rem_local(void *ctx) { (void)ctx; return 0; }
|
|
|
|
static void
|
|
rem_close(void *ctx)
|
|
{
|
|
struct rem *r = ctx;
|
|
struct sp_event ev;
|
|
|
|
if (r == NULL)
|
|
return;
|
|
remote_live = 0;
|
|
while (q_pop(r, &ev))
|
|
rem_event_free(r, &ev);
|
|
for (size_t i = 0; i < r->nhist; i++) {
|
|
free(r->hist[i].role);
|
|
free(r->hist[i].text);
|
|
}
|
|
free(r->hist);
|
|
free(r->q);
|
|
sp_buf_free(&r->in);
|
|
sp_buf_free(&r->line);
|
|
sp_buf_free(&r->last);
|
|
if (r->fd >= 0)
|
|
close(r->fd);
|
|
free(r);
|
|
}
|
|
|
|
static int
|
|
connect_to(const char *path)
|
|
{
|
|
struct sockaddr_un addr;
|
|
int fd;
|
|
|
|
if (strlen(path) >= sizeof(addr.sun_path)) {
|
|
errno = ENAMETOOLONG;
|
|
return -1;
|
|
}
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
if (fd < 0)
|
|
return -1;
|
|
memset(&addr, 0, sizeof(addr));
|
|
addr.sun_family = AF_UNIX;
|
|
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
/*
|
|
* Read the greeting and, when reconnecting, ask for the session we had. The
|
|
* commands and history requests are sent first; an open replaces them with the
|
|
* restored session's state and history.
|
|
*/
|
|
static int
|
|
rem_greet(struct rem *r, struct sp_buf *err)
|
|
{
|
|
char old[64] = "";
|
|
int got_state = 0, got_cmds = 0, got_history = 0, guard = 0;
|
|
int restore;
|
|
|
|
snprintf(old, sizeof(old), "%s", r->st.session);
|
|
restore = old[0] != '\0';
|
|
r->proto = 0;
|
|
r->got_cmds = 0;
|
|
r->got_history = 0;
|
|
r->got_opened = 0;
|
|
r->got_sessions = 0;
|
|
if (send_cmd(r, "commands", NULL, NULL) != 0 ||
|
|
send_cmd(r, "history", NULL, NULL) != 0) {
|
|
if (err != NULL)
|
|
sp_buf_puts(err, "cannot ask for the greeting");
|
|
return -1;
|
|
}
|
|
while ((!got_state || !got_cmds || !got_history) && guard++ < 100) {
|
|
int rc = next_line(r, 3000);
|
|
|
|
if (rc <= 0) {
|
|
if (err != NULL)
|
|
sp_buf_puts(err, "no greeting from the backend");
|
|
return -1;
|
|
}
|
|
(void)dispatch(r, r->line.p);
|
|
if (r->st.session[0] != '\0')
|
|
got_state = 1;
|
|
if (r->got_cmds)
|
|
got_cmds = 1;
|
|
if (r->got_history)
|
|
got_history = 1;
|
|
}
|
|
if (!got_state) {
|
|
if (err != NULL)
|
|
sp_buf_puts(err, "the backend did not say which session "
|
|
"it serves");
|
|
return -1;
|
|
}
|
|
if (r->proto != SP_PROTO_VERSION) {
|
|
if (err != NULL)
|
|
sp_buf_printf(err, "speaks protocol %d, this client "
|
|
"speaks %d", r->proto, SP_PROTO_VERSION);
|
|
return -1;
|
|
}
|
|
if (restore && strcmp(old, r->st.session) != 0) {
|
|
int failed = 0;
|
|
|
|
/*
|
|
* The server came back with another (or a new) session. Ask for
|
|
* the one this screen was on. If it is gone, stay on what the
|
|
* server has and say so - the history on screen is stale either
|
|
* way, and the user can pick another with /sessions.
|
|
*/
|
|
r->quiet_error = 1;
|
|
if (send_cmd(r, "open", "session", old) != 0) {
|
|
failed = 1;
|
|
} else {
|
|
for (guard = 0; guard < 100 && !r->got_opened &&
|
|
!failed; guard++) {
|
|
mcp_json *v;
|
|
const char *ev;
|
|
int rc = next_line(r, 3000);
|
|
|
|
if (rc <= 0)
|
|
break;
|
|
v = mcp_parse(r->line.p);
|
|
ev = v != NULL ?
|
|
mcp_arg_string(v, "ev") : NULL;
|
|
if (ev != NULL &&
|
|
strcmp(ev, "error") == 0)
|
|
failed = 1;
|
|
(void)dispatch(r, r->line.p);
|
|
mcp_json_free(v);
|
|
}
|
|
if (!r->got_opened)
|
|
failed = 1;
|
|
}
|
|
r->quiet_error = 0;
|
|
if (failed) {
|
|
r->sess_gone = 1;
|
|
snprintf(r->gone_old, sizeof(r->gone_old), "%s", old);
|
|
snprintf(r->gone_new, sizeof(r->gone_new), "%s",
|
|
r->st.session);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
rem_connect(struct rem *r, struct sp_buf *err)
|
|
{
|
|
if (r->fd >= 0)
|
|
close(r->fd);
|
|
r->fd = -1;
|
|
sp_buf_clear(&r->in);
|
|
sp_buf_clear(&r->line);
|
|
r->fd = connect_to(r->path);
|
|
if (r->fd < 0) {
|
|
if (err != NULL)
|
|
sp_buf_printf(err, "%s: %s", r->path, strerror(errno));
|
|
return -1;
|
|
}
|
|
if (rem_greet(r, err) != 0) {
|
|
close(r->fd);
|
|
r->fd = -1;
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
const struct ui_backend *
|
|
sp_ui_backend_remote(const char *socket_path, struct sp_buf *err)
|
|
{
|
|
static struct ui_backend be;
|
|
static struct rem *r;
|
|
char path[700];
|
|
|
|
sp_proto_default_socket(path, sizeof(path));
|
|
if (socket_path != NULL && *socket_path != '\0')
|
|
snprintf(path, sizeof(path), "%s", socket_path);
|
|
if (remote_live) {
|
|
sp_buf_printf(err, "%s: this process is already attached", path);
|
|
return NULL;
|
|
}
|
|
r = calloc(1, sizeof(*r));
|
|
if (r == NULL) {
|
|
sp_buf_puts(err, "out of memory");
|
|
return NULL;
|
|
}
|
|
r->fd = -1;
|
|
if (sp_buf_init(&r->in) != 0 || sp_buf_init(&r->line) != 0 ||
|
|
sp_buf_init(&r->last) != 0) {
|
|
sp_buf_free(&r->in);
|
|
sp_buf_free(&r->line);
|
|
free(r);
|
|
sp_buf_puts(err, "out of memory");
|
|
return NULL;
|
|
}
|
|
snprintf(r->path, sizeof(r->path), "%s", path);
|
|
if (rem_connect(r, err) != 0)
|
|
goto fail;
|
|
be.ctx = r;
|
|
be.input = rem_input;
|
|
be.submit = rem_submit;
|
|
be.busy = rem_busy;
|
|
be.wait = rem_wait;
|
|
be.cancel = rem_cancel;
|
|
be.steer = rem_steer;
|
|
be.approval = rem_approval;
|
|
be.approve = rem_approve;
|
|
be.poll = rem_poll;
|
|
be.event_free = rem_event_free;
|
|
be.state = rem_state;
|
|
be.agents = rem_agents;
|
|
be.agent_name = rem_agent_name;
|
|
be.agent_cur = rem_agent_cur;
|
|
be.agent_switch = rem_agent_switch;
|
|
be.command_names = rem_command_names;
|
|
be.models = rem_models;
|
|
be.served = rem_served;
|
|
be.sessions = rem_sessions;
|
|
be.open_session = rem_open_session;
|
|
be.take_steer = rem_take_steer;
|
|
be.history = rem_history;
|
|
be.host = rem_host;
|
|
be.swarm = rem_swarm;
|
|
be.config = rem_config;
|
|
be.session_id = rem_session_id;
|
|
be.state_dir = rem_state_dir;
|
|
be.set_theme = rem_set_theme;
|
|
be.quit = rem_quit;
|
|
be.local = rem_local;
|
|
be.close = rem_close;
|
|
remote_live = 1;
|
|
return &be;
|
|
fail:
|
|
rem_close(r);
|
|
r = NULL;
|
|
return NULL;
|
|
}
|