sessions: /sessions is a picker over sources, foreign sessions included
`/sessions` printed a list of ids; the operator then typed the id into
/open. The list was the wrong shape for a thing you pick from, and the
foreign stores (claude, hermes, opencode) were not in it at all - their
sessions were only reachable through the model's import tool.
Now it is a picker in two steps: the sources first (`splinter` and every
foreign store that has something), then the rows of the chosen one. A
foreign row's id is "claude:<id>", which sp_engine_switch() already reads
as an import - so picking it loads and continues that session, exactly
like `/open claude:<id>` typed by hand.
The picker got one shared implementation while it was open: pick_run()
with a value/label list, a configurable title and two hint lines. /model
and /sessions draw the same way now instead of two copies that could
diverge on Esc, resize and the filter. One window is held across both
steps, so a quick double Esc leaves; with a window per step the second Esc
landed in a window that was already gone and the picker stayed open.
Over the socket the same two steps exist: `{"cmd":"sources"}` and
`{"cmd":"sessions","source":"NAME"}`.
Tested: make check, all suites green (test_engine, test_protocol,
test_ui against a real tmux pane, 229 i18n keys in three catalogues).
This commit is contained in:
1 parent
2175263236
commit
f6211362cb
18 files changed
+1048
-73
No files matched your search
@@ -182,10 +182,13 @@ The engine keeps more than one session: `sessions` lists the directory, `open`
|
||||
switches to an id (or `new`/`last`) and sends the state and history of that
|
||||
session. The screen offers the same as `/sessions` and `/open ID`, locally and
|
||||
over the socket; a local switch closes the engine and reopens it from the same
|
||||
options, because plugins keep process globals. `/sessions` shows a title - the
|
||||
first user line until `/rename [ID] TITLE` says otherwise - and `/rm ID`
|
||||
removes a session that is not open. One session is active at a time; over the
|
||||
socket, one client at a time.
|
||||
options, because plugins keep process globals. `/sessions` without an argument
|
||||
is a picker in two steps: the sources first (`splinter` and every foreign store
|
||||
that has sessions), then the sessions of the chosen one. A foreign row carries
|
||||
its source in the id (`claude:<id>`), and choosing it imports that session and
|
||||
continues it here. A session shows a title - the first user line until
|
||||
`/rename [ID] TITLE` says otherwise - and `/rm ID` removes a session that is not
|
||||
open. One session is active at a time; over the socket, one client at a time.
|
||||
|
||||
`--attach` reconnects: if the server dies, the screen says so, keeps running,
|
||||
and attaches again when a server is back on the socket - asking for the session
|
||||
|
||||
+5
-2
@@ -68,8 +68,11 @@ switches to another id (or `new`/`last`), sending the state and history of that
|
||||
session. The curses screen exposes that as `/sessions` and `/open ID`, in both
|
||||
modes: `--serve` switches its engine, and the in-process `--chat` closes its
|
||||
engine and opens the next one from the same options (plugins keep process
|
||||
globals, so the old one goes first). One session is active at a time; two
|
||||
clients still take turns.
|
||||
globals, so the old one goes first). `/sessions` without an argument is a
|
||||
two-step picker over the same ops (`sources`, then `source_sessions`): the
|
||||
foreign stores appear next to this folder's sessions, and picking one of their
|
||||
rows runs the same `/open <source>:<id>` that imports and continues it. One
|
||||
session is active at a time; two clients still take turns.
|
||||
|
||||
`--attach` keeps running when the backend disappears: it says so, retries every
|
||||
second, and on the next server asks for the session it was on by id. The history
|
||||
|
||||
@@ -156,6 +156,21 @@ List the sessions in the server's session directory, newest first:
|
||||
`title` is the first user line or what `/rename` set (empty when there is no
|
||||
message yet), `current` marks the session the connection is on.
|
||||
|
||||
### `{"cmd":"sources"}`
|
||||
|
||||
What the `/sessions` picker offers as a first step: `"ev":"sources"`,
|
||||
`"items":[{"name","count"}]` — `splinter` first (this session directory), then
|
||||
every foreign store that has sessions. A store the plugin cannot read is left
|
||||
out instead of shown empty.
|
||||
|
||||
### `{"cmd":"sessions","source":"NAME"}`
|
||||
|
||||
The second step: the sessions of one source, same `"ev":"sessions"` shape as
|
||||
above. For `splinter` the rows are plain session ids. For `claude`, `hermes`
|
||||
and `opencode` a row's `id` is `"<source>:<id>"`, which is exactly what
|
||||
`{"cmd":"open","session":"claude:<id>"}` takes to import that session and
|
||||
continue it here.
|
||||
|
||||
### `{"cmd":"open","session":"ID"}`
|
||||
|
||||
Switch this connection to another session. `"new"` starts a fresh one,
|
||||
|
||||
@@ -70,8 +70,11 @@ struct sp_engine_opts {
|
||||
/*
|
||||
* --claude/--opencode/--hermes: import the newest session that tool
|
||||
* ran in this directory and continue that instead of a splinter one.
|
||||
* from_foreign_id names one session instead of "the newest" - that is
|
||||
* how the /sessions picker continues what the operator pointed at.
|
||||
*/
|
||||
const char *from_foreign;
|
||||
const char *from_foreign_id;
|
||||
int no_transport; /* tools only (MCP server, delegate) */
|
||||
int interview; /* the model interviews the user */
|
||||
int swarm; /* join the local fleet */
|
||||
@@ -179,6 +182,29 @@ 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 *);
|
||||
|
||||
/*
|
||||
* What the /sessions picker offers: the sources that have sessions at all,
|
||||
* its own first. "splinter" is always there (this folder's session directory
|
||||
* is what the run is); the other three only when their store answers with at
|
||||
* least one session, so the picker never offers an empty source.
|
||||
*/
|
||||
struct sp_session_source {
|
||||
char name[16]; /* "splinter", "claude", "hermes", "opencode" */
|
||||
int count; /* 0 when the source could not be listed */
|
||||
};
|
||||
int sp_engine_sources(struct sp_engine *,
|
||||
struct sp_session_source *v, size_t max);
|
||||
|
||||
/*
|
||||
* The sessions of one source. For "splinter" that is the session directory;
|
||||
* for the others the foreign plugin's list, and a row's `id` is then
|
||||
* "claude:<id>" - what sp_engine_switch() takes to import and continue it.
|
||||
* `title` is a label for the screen, not something to parse.
|
||||
*/
|
||||
int sp_engine_source_sessions(struct sp_engine *, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *title,
|
||||
int current, long mtime), void *cbctx);
|
||||
|
||||
/* ------------------------------------------------------------------ turns */
|
||||
|
||||
/*
|
||||
|
||||
@@ -13,6 +13,13 @@ ui.reconnecting = Verbindung weg - verbinde neu ...
|
||||
ui.no_sessions = noch keine Sitzungen
|
||||
ui.untitled = (ohne Titel)
|
||||
ui.session_opened = Sitzung geoeffnet
|
||||
|
||||
# /sessions: the two-stage picker (source, then session)
|
||||
ui.sessions_title_source = Sitzungen: Quelle
|
||||
ui.sessions_title_session = Sitzungen
|
||||
ui.sessions_no_source = keine Quelle hat Sitzungen
|
||||
ui.sessions_hint_source = Enter oeffnet eine Quelle Esc bricht ab
|
||||
ui.sessions_hint_session = Enter oeffnet die Sitzung Esc zurueck
|
||||
ui.session_gone = Sitzung %s ist weg - jetzt auf %s
|
||||
ui.compacted = Kontext kompaktiert: %zu->%zu Nachrichten, %s->%s (%s->%s)
|
||||
ui.compact_nohost = compact: kein Plugin-Host, nichts zu kompaktieren
|
||||
|
||||
@@ -12,6 +12,13 @@ ui.reconnecting = connection lost - reconnecting ...
|
||||
ui.no_sessions = no sessions yet
|
||||
ui.untitled = (untitled)
|
||||
ui.session_opened = session opened
|
||||
|
||||
# /sessions: the two-stage picker (source, then session)
|
||||
ui.sessions_title_source = sessions: source
|
||||
ui.sessions_title_session = sessions
|
||||
ui.sessions_no_source = no source has sessions
|
||||
ui.sessions_hint_source = Enter opens a source Esc cancels
|
||||
ui.sessions_hint_session = Enter opens the session Esc back
|
||||
ui.session_gone = session %s is gone - now on %s
|
||||
ui.compacted = context compacted: %zu->%zu msgs, %s->%s (%s->%s)
|
||||
ui.compact_nohost = compact: no plugin host, nothing to compact
|
||||
|
||||
@@ -13,6 +13,13 @@ ui.reconnecting = conexao perdida - reconectando ...
|
||||
ui.no_sessions = nenhuma sessao ainda
|
||||
ui.untitled = (sem titulo)
|
||||
ui.session_opened = sessao aberta
|
||||
|
||||
# /sessions: the two-stage picker (source, then session)
|
||||
ui.sessions_title_source = sessoes: fonte
|
||||
ui.sessions_title_session = sessoes
|
||||
ui.sessions_no_source = nenhuma fonte tem sessoes
|
||||
ui.sessions_hint_source = Enter abre uma fonte Esc cancela
|
||||
ui.sessions_hint_session = Enter abre a sessao Esc volta
|
||||
ui.session_gone = sessao %s sumiu - agora em %s
|
||||
ui.compacted = contexto compactado: %zu->%zu msgs, %s->%s (%s->%s)
|
||||
ui.compact_nohost = compact: sem host de plugin, nada a compactar
|
||||
|
||||
+155
-2
@@ -581,6 +581,112 @@ profile_load(struct sp_engine *e, char *buf, size_t cap)
|
||||
const char *
|
||||
sp_engine_session_id(struct sp_engine *e) { return e->sess.id; }
|
||||
|
||||
/* ------------------------------------------------- the /sessions sources */
|
||||
|
||||
/*
|
||||
* The foreign side of the picker: which stores answer, and their rows. Both
|
||||
* ask the plugin's own foreign_list, so the three formats stay in the plugin
|
||||
* and a store that appeared since the start shows up without a restart.
|
||||
*/
|
||||
static void
|
||||
foreign_target_of(struct sp_engine *e, struct sp_foreign_target *ft)
|
||||
{
|
||||
ft->sess = &e->sess;
|
||||
ft->reg = &e->reg;
|
||||
ft->host = e->host;
|
||||
ft->emb = e->emb;
|
||||
}
|
||||
|
||||
int
|
||||
sp_engine_sources(struct sp_engine *e, struct sp_session_source *v, size_t max)
|
||||
{
|
||||
struct sp_foreign_target ft;
|
||||
char names[4][16];
|
||||
struct sp_session_brief sb[100];
|
||||
size_t i, n = 0;
|
||||
int nf;
|
||||
|
||||
if (e == NULL || v == NULL || max == 0)
|
||||
return 0;
|
||||
/* its own sessions first: they are what this run is */
|
||||
if (n < max) {
|
||||
snprintf(v[n].name, sizeof(v[n].name), "splinter");
|
||||
v[n].count = sp_session_list(e->sess.dir, sb, 100);
|
||||
if (v[n].count < 0)
|
||||
v[n].count = 0;
|
||||
n++;
|
||||
}
|
||||
foreign_target_of(e, &ft);
|
||||
nf = sp_foreign_sources(&ft, names, sizeof(names) / sizeof(names[0]));
|
||||
for (i = 0; i < (size_t)nf && n < max; i++, n++) {
|
||||
snprintf(v[n].name, sizeof(v[n].name), "%.15s", names[i]);
|
||||
v[n].count = 0;
|
||||
}
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
/* the callback the foreign list fills: id gets the source prefix */
|
||||
struct src_rows {
|
||||
const char *source;
|
||||
void (*cb)(void *, const char *, const char *, int, long);
|
||||
void *cbctx;
|
||||
int n;
|
||||
};
|
||||
|
||||
static void
|
||||
src_row(void *cbctx, const char *id, const char *label)
|
||||
{
|
||||
struct src_rows *r = cbctx;
|
||||
char prefixed[128];
|
||||
|
||||
/*
|
||||
* The prefix is what sp_engine_switch() reads to know this is an
|
||||
* import; a plain id would be looked for in this folder's directory
|
||||
* and not be found.
|
||||
*/
|
||||
snprintf(prefixed, sizeof(prefixed), "%s:%s", r->source, id);
|
||||
r->cb(r->cbctx, prefixed, label, 0, 0);
|
||||
r->n++;
|
||||
}
|
||||
|
||||
int
|
||||
sp_engine_source_sessions(struct sp_engine *e, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *title, int current,
|
||||
long mtime), void *cbctx)
|
||||
{
|
||||
struct sp_session_brief v[100];
|
||||
struct sp_foreign_target ft;
|
||||
struct sp_buf err;
|
||||
int n, i;
|
||||
|
||||
if (e == NULL || source == NULL || cb == NULL)
|
||||
return 0;
|
||||
if (strcmp(source, "splinter") == 0) {
|
||||
n = sp_session_list(e->sess.dir, v, 100);
|
||||
for (i = 0; i < n; i++)
|
||||
cb(cbctx, v[i].id, v[i].title,
|
||||
strcmp(v[i].id, e->sess.id) == 0, v[i].mtime);
|
||||
return n > 0 ? n : 0;
|
||||
}
|
||||
foreign_target_of(e, &ft);
|
||||
if (sp_buf_init(&err) != 0)
|
||||
return 0;
|
||||
{
|
||||
struct src_rows r;
|
||||
|
||||
r.source = source;
|
||||
r.cb = cb;
|
||||
r.cbctx = cbctx;
|
||||
r.n = 0;
|
||||
n = sp_foreign_sessions(&ft, source, src_row, &r, &err);
|
||||
if (n < 0 && err.p != NULL && err.len > 0)
|
||||
sp_logf("session", "cannot list %s sessions: %s",
|
||||
source, err.p);
|
||||
sp_buf_free(&err);
|
||||
return r.n;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
sp_engine_agents(struct sp_engine *e) { return (int)e->agents.n; }
|
||||
|
||||
@@ -3751,7 +3857,11 @@ sp_engine_open(struct sp_engine **out, const struct sp_engine_opts *o,
|
||||
ft.reg = &e->reg;
|
||||
ft.host = e->host;
|
||||
ft.emb = e->emb;
|
||||
frc = sp_foreign_open(&ft, o->from_foreign, here, err);
|
||||
if (o->from_foreign_id != NULL && *o->from_foreign_id != '\0')
|
||||
frc = sp_foreign_import(&ft, o->from_foreign,
|
||||
o->from_foreign_id, err);
|
||||
else
|
||||
frc = sp_foreign_open(&ft, o->from_foreign, here, err);
|
||||
/*
|
||||
* A run that started without a session to resume opened an empty
|
||||
* placeholder, and the import replaced it (or never happened,
|
||||
@@ -3953,6 +4063,33 @@ valid_session_id(const char *id)
|
||||
return strstr(id, "..") == NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* A picker row from another harness ("claude:<id>") split into its two parts.
|
||||
* Returns 1 when `spec` is one, 0 when it is an ordinary session id or a
|
||||
* keyword - the two are told apart by the colon, which no session id has.
|
||||
*/
|
||||
static int
|
||||
foreign_spec(const char *spec, char *src, char *id, size_t cap)
|
||||
{
|
||||
static const char *const known[] = { "claude", "hermes", "opencode" };
|
||||
const char *c;
|
||||
size_t i, n;
|
||||
|
||||
if (spec == NULL || (c = strchr(spec, ':')) == NULL || c == spec)
|
||||
return 0;
|
||||
for (i = 0; i < sizeof(known) / sizeof(known[0]); i++) {
|
||||
n = strlen(known[i]);
|
||||
if ((size_t)(c - spec) != n || strncmp(spec, known[i], n) != 0)
|
||||
continue;
|
||||
if (*(c + 1) == '\0' || strlen(c + 1) + 1 > cap)
|
||||
return 0;
|
||||
snprintf(src, 16, "%s", known[i]);
|
||||
snprintf(id, cap, "%s", c + 1);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_engine_switch(struct sp_engine **ep, const struct sp_engine_opts *tmpl,
|
||||
const char *id, struct sp_buf *err)
|
||||
@@ -3961,6 +4098,7 @@ sp_engine_switch(struct sp_engine **ep, const struct sp_engine_opts *tmpl,
|
||||
struct sp_engine *old, *ne = NULL;
|
||||
struct sp_buf oerr;
|
||||
char keep[64], path[800], picked[64];
|
||||
char src[16] = "", fid[128] = "";
|
||||
const char *dir;
|
||||
int rc;
|
||||
|
||||
@@ -3978,14 +4116,29 @@ sp_engine_switch(struct sp_engine **ep, const struct sp_engine_opts *tmpl,
|
||||
snprintf(keep, sizeof(keep), "%s", sp_engine_session_id(old));
|
||||
dir = sp_engine_state_dir(old);
|
||||
o = *tmpl;
|
||||
if (id == NULL || strcmp(id, "last") == 0) {
|
||||
o.from_foreign_id = NULL;
|
||||
if (foreign_spec(id, src, fid, sizeof(fid))) {
|
||||
/*
|
||||
* A picker row from another harness: "claude:<id>". The import
|
||||
* writes a splinter session and the engine opens that one, so
|
||||
* the id is not known until the engine exists - hence
|
||||
* from_foreign on the options instead of a session id.
|
||||
*/
|
||||
o.session_id = NULL;
|
||||
o.resume = 0;
|
||||
o.continue_last = 0;
|
||||
o.from_foreign = src;
|
||||
o.from_foreign_id = fid;
|
||||
} else if (id == NULL || strcmp(id, "last") == 0) {
|
||||
o.session_id = NULL;
|
||||
o.resume = 1;
|
||||
o.continue_last = 0;
|
||||
o.from_foreign = NULL;
|
||||
} else if (strcmp(id, "new") == 0) {
|
||||
o.session_id = NULL;
|
||||
o.resume = 0;
|
||||
o.continue_last = 0;
|
||||
o.from_foreign = NULL;
|
||||
} else {
|
||||
if (!valid_session_id(id)) {
|
||||
if (err != NULL)
|
||||
|
||||
+203
-14
@@ -209,9 +209,9 @@ int
|
||||
sp_foreign_open(struct sp_foreign_target *t, const char *source,
|
||||
const char *cwd, struct sp_buf *err)
|
||||
{
|
||||
const struct sp_tool *list, *import;
|
||||
const struct sp_tool *list;
|
||||
struct sp_buf args, out, tmp;
|
||||
char id[96], newid[96];
|
||||
char id[96];
|
||||
int rc = -1;
|
||||
|
||||
if (source == NULL || *source == '\0')
|
||||
@@ -223,8 +223,7 @@ sp_foreign_open(struct sp_foreign_target *t, const char *source,
|
||||
return -1;
|
||||
}
|
||||
list = tool_of(t->reg, "foreign_list");
|
||||
import = tool_of(t->reg, "foreign_import");
|
||||
if (list == NULL || import == NULL) {
|
||||
if (list == NULL || tool_of(t->reg, "foreign_import") == NULL) {
|
||||
sp_buf_printf(err, "--%s needs the foreign plugin: add "
|
||||
"'plugin foreign' to the config", source);
|
||||
return -1;
|
||||
@@ -259,34 +258,82 @@ sp_foreign_open(struct sp_foreign_target *t, const char *source,
|
||||
memcpy(id, idp, idn);
|
||||
id[idn] = '\0';
|
||||
}
|
||||
return sp_foreign_import(t, source, id, err);
|
||||
failed:
|
||||
/* keep what the plugin said: it knows why a store was unreadable */
|
||||
if (err->len == 0 && out.p != NULL && out.len > 0)
|
||||
sp_buf_add(err, out.p, out.len);
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&tmp);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* Import one named session and make it the session of this run. The picker
|
||||
* behind /sessions uses this: the operator saw the row, so the id is the
|
||||
* choice, and sc_foreign_open is only the "newest in this folder" shortcut.
|
||||
*
|
||||
* An earlier import of the same foreign session is continued instead of being
|
||||
* imported again: the plugin replaces a file of the same name, which would
|
||||
* throw away everything the conversation did in splinter since.
|
||||
*/
|
||||
int
|
||||
sp_foreign_import(struct sp_foreign_target *t, const char *source,
|
||||
const char *id, struct sp_buf *err)
|
||||
{
|
||||
const struct sp_tool *import;
|
||||
struct sp_buf args, out, tmp;
|
||||
char newid[96];
|
||||
int rc = -1;
|
||||
|
||||
if (source == NULL || id == NULL || *id == '\0')
|
||||
return -1;
|
||||
if (strcmp(source, "claude") != 0 && strcmp(source, "hermes") != 0 &&
|
||||
strcmp(source, "opencode") != 0) {
|
||||
sp_buf_printf(err, "%s is not a source: claude, hermes or "
|
||||
"opencode", source);
|
||||
return -1;
|
||||
}
|
||||
import = tool_of(t->reg, "foreign_import");
|
||||
if (import == NULL) {
|
||||
sp_buf_puts(err, "the foreign plugin is not loaded");
|
||||
return -1;
|
||||
}
|
||||
if (sp_buf_init(&args) != 0 || sp_buf_init(&out) != 0 ||
|
||||
sp_buf_init(&tmp) != 0) {
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&tmp);
|
||||
return -1;
|
||||
}
|
||||
if (imported_before(t->sess->dir, source, id, newid, sizeof(newid))) {
|
||||
if (take_session(t, newid, err) != 0)
|
||||
goto failed;
|
||||
goto out;
|
||||
sp_logf("session", "continuing %s session %s, imported before "
|
||||
"as %s", source, id, newid);
|
||||
rc = 0;
|
||||
goto out;
|
||||
}
|
||||
sp_buf_clear(&args);
|
||||
(void)sp_buf_printf(&args, "{\"source\":\"%s\",\"id\":\"%s\"}", source,
|
||||
id);
|
||||
if (run_tool(import, args.p, &out, &tmp) != 0 ||
|
||||
out.p == NULL || strncmp(out.p, "imported ", 9) != 0) {
|
||||
sp_buf_printf(err, "cannot import %s %s: %s", source, id,
|
||||
out.len > 0 && out.p != NULL ? out.p : "the import failed");
|
||||
goto failed;
|
||||
goto out;
|
||||
}
|
||||
if (id_from_answer(out.p, newid, sizeof(newid)) != 0) {
|
||||
sp_buf_printf(err, "imported, but the new session id is not in "
|
||||
"the answer: %s", out.p);
|
||||
goto failed;
|
||||
goto out;
|
||||
}
|
||||
/*
|
||||
* The import wrote a new file, and the log, the embed index and the
|
||||
* agent still point at the empty session this run started with.
|
||||
*/
|
||||
if (take_session(t, newid, err) != 0)
|
||||
goto failed;
|
||||
goto out;
|
||||
sp_logf("session", "continuing %s session %s as %s", source, id, newid);
|
||||
rc = 0;
|
||||
out:
|
||||
@@ -294,9 +341,151 @@ out:
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&tmp);
|
||||
return rc;
|
||||
failed:
|
||||
/* keep what the plugin said: it knows why a store was unreadable */
|
||||
if (err->len == 0 && out.p != NULL && out.len > 0)
|
||||
sp_buf_add(err, out.p, out.len);
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*
|
||||
* The three sources this file knows and the plugin's list tool. Shared by the
|
||||
* two picker helpers below; -1 and `err` filled when the plugin is not loaded.
|
||||
*/
|
||||
static int
|
||||
foreign_list_tool(struct sp_foreign_target *t, const struct sp_tool **tool,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
if (t == NULL || t->reg == NULL) {
|
||||
sp_buf_puts(err, "no registry");
|
||||
return -1;
|
||||
}
|
||||
*tool = tool_of(t->reg, "foreign_list");
|
||||
if (*tool == NULL) {
|
||||
sp_buf_puts(err, "the foreign plugin is not loaded");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Which foreign stores have sessions. The plugin's own listing is the
|
||||
* authority: a source that lists nothing (no store, an unreadable one, a
|
||||
* tracedb that sqlite3 cannot open) is not offered by the picker, and a store
|
||||
* that gained sessions since the last start shows up without a restart.
|
||||
*/
|
||||
int
|
||||
sp_foreign_sources(struct sp_foreign_target *t, char (*names)[16], size_t max)
|
||||
{
|
||||
static const char *const src[] = { "claude", "hermes", "opencode" };
|
||||
const struct sp_tool *list;
|
||||
struct sp_buf args, out, tmp;
|
||||
size_t i, n = 0;
|
||||
|
||||
if (max == 0 || foreign_list_tool(t, &list, &tmp) != 0)
|
||||
return 0;
|
||||
if (sp_buf_init(&args) != 0)
|
||||
return 0;
|
||||
if (sp_buf_init(&out) != 0) {
|
||||
sp_buf_free(&args);
|
||||
return 0;
|
||||
}
|
||||
if (sp_buf_init(&tmp) != 0) {
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < sizeof(src) / sizeof(src[0]) && n < max; i++) {
|
||||
const char *p;
|
||||
|
||||
sp_buf_clear(&args);
|
||||
(void)sp_buf_printf(&args, "{\"source\":\"%s\",\"cwd\":\"*\","
|
||||
"\"limit\":1}", src[i]);
|
||||
if (run_tool(list, args.p, &out, &tmp) != 0)
|
||||
continue;
|
||||
if (out.p == NULL || strncmp(out.p, "no foreign sessions", 19) == 0)
|
||||
continue;
|
||||
/* a store that answers with an error is not a source either */
|
||||
p = out.p;
|
||||
if (strchr(p, '\n') == NULL && strchr(p, ' ') == NULL)
|
||||
continue;
|
||||
if (p[0] != '\0' && p[1] != '\0' && p[2] == '-' &&
|
||||
strncmp(p, "error:", 6) == 0)
|
||||
continue;
|
||||
snprintf(names[n], 16, "%s", src[i]);
|
||||
n++;
|
||||
}
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&tmp);
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
/*
|
||||
* Every session of one foreign source, as the plugin lists it. One row is
|
||||
* "DATE SOURCE ID TITLE CWD"; the picker gets the id and the rest of the
|
||||
* row, so a title with an odd character in it cannot break the id.
|
||||
*/
|
||||
int
|
||||
sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *label), void *cbctx,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
const struct sp_tool *list;
|
||||
struct sp_buf args, out, tmp;
|
||||
char *line, *save;
|
||||
int n = 0;
|
||||
|
||||
if (foreign_list_tool(t, &list, err) != 0)
|
||||
return -1;
|
||||
if (sp_buf_init(&args) != 0)
|
||||
return -1;
|
||||
if (sp_buf_init(&out) != 0) {
|
||||
sp_buf_free(&args);
|
||||
return -1;
|
||||
}
|
||||
if (sp_buf_init(&tmp) != 0) {
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
return -1;
|
||||
}
|
||||
(void)sp_buf_printf(&args, "{\"source\":\"%s\",\"cwd\":\"*\","
|
||||
"\"limit\":100}", source);
|
||||
if (run_tool(list, args.p, &out, &tmp) != 0 ||
|
||||
out.p == NULL || strncmp(out.p, "error:", 6) == 0) {
|
||||
sp_buf_printf(err, "%s", out.len > 0 && out.p != NULL ?
|
||||
out.p : "the foreign list failed");
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&tmp);
|
||||
return -1;
|
||||
}
|
||||
for (line = strtok_r(out.p, "\n", &save); line != NULL;
|
||||
line = strtok_r(NULL, "\n", &save)) {
|
||||
const char *idp = NULL, *rest;
|
||||
size_t idn;
|
||||
char id[96], label[160];
|
||||
|
||||
/* the plugin's own notes are not sessions */
|
||||
if (strncmp(line, "error:", 6) == 0 ||
|
||||
strncmp(line, "...", 3) == 0 ||
|
||||
strncmp(line, "no foreign sessions", 19) == 0)
|
||||
continue;
|
||||
idn = column(line, 2, &idp);
|
||||
if (idn == 0 || idn + 1 > sizeof(id))
|
||||
continue;
|
||||
memcpy(id, idp, idn);
|
||||
id[idn] = '\0';
|
||||
/*
|
||||
* An id is what foreign_import accepts, so anything that would
|
||||
* not survive as a JSON string or a file name is not one.
|
||||
*/
|
||||
if (strpbrk(id, "():\"'\\/") != NULL)
|
||||
continue;
|
||||
rest = idp + idn;
|
||||
while (*rest == ' ')
|
||||
rest++;
|
||||
snprintf(label, sizeof(label), "%.31s %s", id, rest);
|
||||
cb(cbctx, id, label);
|
||||
n++;
|
||||
}
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&tmp);
|
||||
return n;
|
||||
}
|
||||
@@ -38,4 +38,30 @@ struct sp_foreign_target {
|
||||
int sp_foreign_open(struct sp_foreign_target *t, const char *source,
|
||||
const char *cwd, struct sp_buf *err);
|
||||
|
||||
/* the same for one named session: the picker's choice */
|
||||
int sp_foreign_import(struct sp_foreign_target *t, const char *source,
|
||||
const char *id, struct sp_buf *err);
|
||||
|
||||
/*
|
||||
* The picker behind /sessions needs the other side of that: which foreign
|
||||
* stores have sessions at all, and every session of one store. Both ask the
|
||||
* foreign plugin's own list tool, so the three formats stay in the plugin and
|
||||
* this file only reads the rows it prints.
|
||||
*
|
||||
* sp_foreign_sources fills up to `max` source names ("claude", "hermes",
|
||||
* "opencode") and returns how many it found; a source whose store is missing
|
||||
* or unreadable is not one of them - the picker offers what is there.
|
||||
*/
|
||||
int sp_foreign_sources(struct sp_foreign_target *t, char (*names)[16],
|
||||
size_t max);
|
||||
|
||||
/*
|
||||
* Every session of `source`, newest first, as the plugin lists it. `label` is
|
||||
* the row without the date and source columns: what a picker can show as is.
|
||||
* Returns the number of rows, or -1 with `err` filled when the store failed.
|
||||
*/
|
||||
int sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *label), void *cbctx,
|
||||
struct sp_buf *err);
|
||||
|
||||
#endif
|
||||
+295
-50
@@ -2398,13 +2398,33 @@ draw_prompt(struct ui_state *s, const char *input)
|
||||
draw_prompt_cur(s, input, input != NULL ? strlen(input) : 0);
|
||||
}
|
||||
|
||||
/* ---- /model: a picker, not a printed list ------------------------------ */
|
||||
/* ---- the picker behind /model and /sessions ---------------------------- */
|
||||
|
||||
/*
|
||||
* One list for both pickers. /model had its own struct and its own draw; a
|
||||
* second copy of the same 120 lines for /sessions would have meant two places
|
||||
* that handle Esc, resize and the filter differently. What differs is the
|
||||
* title, the two "nothing here" lines and what Enter does with the choice.
|
||||
*/
|
||||
struct pick_item {
|
||||
char sel[320]; /* what Enter hands back */
|
||||
char label[512]; /* what the row shows */
|
||||
int current;
|
||||
};
|
||||
|
||||
struct pick_list {
|
||||
struct sp_model_choice v[SP_MODEL_CHOICES];
|
||||
struct pick_item v[SP_MODEL_CHOICES];
|
||||
size_t n;
|
||||
};
|
||||
|
||||
struct pick_text {
|
||||
const char *title; /* the window's title line */
|
||||
const char *empty; /* nothing in the list at all */
|
||||
const char *none; /* nothing matches the filter */
|
||||
const char *filter; /* label in front of the filter line */
|
||||
const char *hint; /* the footer */
|
||||
};
|
||||
|
||||
static void
|
||||
pick_add(void *ctx, const char *sel, const char *label, int current)
|
||||
{
|
||||
@@ -2419,6 +2439,16 @@ pick_add(void *ctx, const char *sel, const char *label, int current)
|
||||
l->n++;
|
||||
}
|
||||
|
||||
/* a row with a label that is not the selector: the /sessions sources */
|
||||
static void
|
||||
pick_add_count(void *ctx, const char *name, int count)
|
||||
{
|
||||
char label[64];
|
||||
|
||||
snprintf(label, sizeof(label), "%s (%d)", name, count);
|
||||
pick_add(ctx, name, label, 0);
|
||||
}
|
||||
|
||||
/* case-blind substring; an empty filter matches every entry */
|
||||
static int
|
||||
ci_contains(const char *hay, const char *needle)
|
||||
@@ -2482,10 +2512,11 @@ pick_window(int *wp, int *hp)
|
||||
|
||||
static void
|
||||
pick_draw(WINDOW *win, const struct pick_list *l, const size_t *match,
|
||||
size_t nmatch, size_t cur, size_t top, const char *filter)
|
||||
size_t nmatch, size_t cur, size_t top, const char *filter,
|
||||
const struct pick_text *t)
|
||||
{
|
||||
int h = getmaxy(win), w = getmaxx(win), listh = h - 4, y;
|
||||
const char *label = sp_t("ui.model_filter");
|
||||
const char *label = t->filter != NULL ? t->filter : "";
|
||||
int fw = 2 + (int)strlen(label) + 1;
|
||||
char line[1100];
|
||||
|
||||
@@ -2497,15 +2528,15 @@ pick_draw(WINDOW *win, const struct pick_list *l, const size_t *match,
|
||||
*/
|
||||
wattrset(win, COLOR_PAIR(PAIR_STATUS) | A_BOLD);
|
||||
mvwhline(win, 0, 0, ' ', w);
|
||||
mvwaddnstr(win, 0, 1, sp_t("ui.model_title"), w - 2);
|
||||
mvwaddnstr(win, 0, 1, t->title != NULL ? t->title : "", w - 2);
|
||||
wattrset(win, COLOR_PAIR(PAIR_MAIN));
|
||||
mvwaddnstr(win, 1, 2, label, w - 4);
|
||||
if (fw < w - 2)
|
||||
mvwaddnstr(win, 1, fw, filter, w - 2 - fw);
|
||||
if (l->n == 0)
|
||||
mvwaddnstr(win, 3, 2, sp_t("ui.model_empty"), w - 4);
|
||||
mvwaddnstr(win, 3, 2, t->empty != NULL ? t->empty : "", w - 4);
|
||||
else if (nmatch == 0)
|
||||
mvwaddnstr(win, 3, 2, sp_t("ui.model_none"), w - 4);
|
||||
mvwaddnstr(win, 3, 2, t->none != NULL ? t->none : "", w - 4);
|
||||
for (y = 0; y < listh; y++) {
|
||||
size_t pos = top + (size_t)y, idx;
|
||||
int row = 2 + y;
|
||||
@@ -2525,7 +2556,8 @@ pick_draw(WINDOW *win, const struct pick_list *l, const size_t *match,
|
||||
}
|
||||
wattrset(win, COLOR_PAIR(PAIR_DIM));
|
||||
mvwhline(win, h - 2, 0, ' ', w);
|
||||
mvwaddnstr(win, h - 2, 1, sp_t("ui.model_hint"), w - 2);
|
||||
if (t->hint != NULL)
|
||||
mvwaddnstr(win, h - 2, 1, t->hint, w - 2);
|
||||
wattrset(win, COLOR_PAIR(PAIR_MAIN));
|
||||
wmove(win, 1, fw + (int)strlen(filter) < w - 1 ?
|
||||
fw + (int)strlen(filter) : w - 2);
|
||||
@@ -2534,42 +2566,75 @@ pick_draw(WINDOW *win, const struct pick_list *l, const size_t *match,
|
||||
}
|
||||
|
||||
/*
|
||||
* The picker behind /model: Up/Down move, typing filters by selector or model,
|
||||
* Enter takes the highlighted one, Esc cancels. It never talks to the network;
|
||||
* the list is the configuration (be->models), and the switch itself is the
|
||||
* ordinary "/model <selector>" command, so local and --attach behave the same.
|
||||
* The window a picker draws in, held by the caller so that a picker with more
|
||||
* than one stage keeps ONE window. Esc arrives twice when it is typed twice
|
||||
* quickly and curses holds the second one in the window's own key queue; with
|
||||
* a window per stage that second Esc landed in a window that was already gone
|
||||
* and the picker stayed open (seen 2026-11-22 in tests/test_ui.sh). One window
|
||||
* for both stages makes the second Esc mean what it looks like: leave.
|
||||
*/
|
||||
struct pick_view {
|
||||
WINDOW *win;
|
||||
int w;
|
||||
int h;
|
||||
};
|
||||
|
||||
static int
|
||||
pick_view_open(struct pick_view *v)
|
||||
{
|
||||
v->win = pick_window(&v->w, &v->h);
|
||||
return v->win != NULL ? 0 : -1;
|
||||
}
|
||||
|
||||
static void
|
||||
pick_view_close(struct pick_view *v)
|
||||
{
|
||||
if (v->win != NULL)
|
||||
delwin(v->win);
|
||||
v->win = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* The loop both pickers share: Up/Down move, typing filters, Enter takes the
|
||||
* highlighted row, Esc cancels. `fill` puts the rows in before the first draw;
|
||||
* `pending` paints one line while a slow fill runs (the /model probe). Returns
|
||||
* 0 and the selector in `sel`, or -1 when nothing was taken. The window belongs
|
||||
* to the caller (see struct pick_view) and stays open across stages.
|
||||
*/
|
||||
static int
|
||||
model_picker(const struct ui_backend *be, char *sel, size_t cap)
|
||||
pick_run(struct pick_view *view, struct pick_list *list,
|
||||
const struct pick_text *t,
|
||||
void (*fill)(void *ctx, struct pick_list *l), void *fillctx,
|
||||
const char *pending_text, void (*pending)(void *ctx, struct pick_list *l),
|
||||
void *pendingctx, char *sel, size_t cap)
|
||||
{
|
||||
struct pick_list list;
|
||||
char filter[64];
|
||||
size_t flen = 0, match[SP_MODEL_CHOICES], nmatch;
|
||||
size_t cur = 0, top = 0;
|
||||
WINDOW *win;
|
||||
int w = 0, h = 0, rc = -1, c;
|
||||
char filter[64];
|
||||
size_t flen = 0, match[SP_MODEL_CHOICES], nmatch;
|
||||
size_t cur = 0, top = 0;
|
||||
WINDOW *win;
|
||||
int w, h, rc = -1, c;
|
||||
|
||||
if (be == NULL || be->models == NULL)
|
||||
return -1;
|
||||
memset(&list, 0, sizeof(list));
|
||||
filter[0] = '\0';
|
||||
win = pick_window(&w, &h);
|
||||
if (win == NULL)
|
||||
if (view == NULL || view->win == NULL)
|
||||
return -1;
|
||||
/* the configured list is instant; the endpoint may take a moment */
|
||||
be->models(be->ctx, pick_add, &list);
|
||||
if (be->served != NULL) {
|
||||
win = view->win;
|
||||
w = view->w;
|
||||
h = view->h;
|
||||
if (fill != NULL)
|
||||
fill(fillctx, list);
|
||||
if (pending != NULL) {
|
||||
werase(win);
|
||||
wattrset(win, COLOR_PAIR(PAIR_STATUS) | A_BOLD);
|
||||
mvwhline(win, 0, 0, ' ', w);
|
||||
mvwaddnstr(win, 0, 1, sp_t("ui.model_title"), w - 2);
|
||||
mvwaddnstr(win, 0, 1, t->title != NULL ? t->title : "", w - 2);
|
||||
wattrset(win, COLOR_PAIR(PAIR_MAIN));
|
||||
mvwaddnstr(win, 3, 2, sp_t("ui.model_probing"), w - 4);
|
||||
mvwaddnstr(win, 3, 2, pending_text != NULL ? pending_text : "",
|
||||
w - 4);
|
||||
wnoutrefresh(win);
|
||||
doupdate();
|
||||
be->served(be->ctx, pick_add, &list);
|
||||
pending(pendingctx, list);
|
||||
}
|
||||
nmatch = pick_match(&list, filter, match);
|
||||
nmatch = pick_match(list, filter, match);
|
||||
for (;;) {
|
||||
int changed = 0;
|
||||
int listh = h - 4;
|
||||
@@ -2582,24 +2647,26 @@ model_picker(const struct ui_backend *be, char *sel, size_t cap)
|
||||
top = cur;
|
||||
if (listh > 0 && cur >= top + (size_t)listh)
|
||||
top = cur - (size_t)listh + 1;
|
||||
pick_draw(win, &list, match, nmatch, cur, top, filter);
|
||||
werase(win);
|
||||
pick_draw(win, list, match, nmatch, cur, top, filter, t);
|
||||
c = ui_getkey(win, -1);
|
||||
if (c == 27 || c == 3 || c == 4)
|
||||
break;
|
||||
if (c == '\n' || c == '\r' || c == KEY_ENTER) {
|
||||
if (nmatch > 0) {
|
||||
snprintf(sel, cap, "%s",
|
||||
list.v[match[cur]].sel);
|
||||
snprintf(sel, cap, "%s", list->v[match[cur]].sel);
|
||||
rc = 0;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == KEY_RESIZE) {
|
||||
delwin(win);
|
||||
win = pick_window(&w, &h);
|
||||
if (win == NULL)
|
||||
pick_view_close(view);
|
||||
if (pick_view_open(view) != 0)
|
||||
break;
|
||||
win = view->win;
|
||||
w = view->w;
|
||||
h = view->h;
|
||||
changed = 1;
|
||||
} else if (c == KEY_UP || c == 16 || c == UK_WHEEL_UP) {
|
||||
if (cur > 0)
|
||||
@@ -2632,13 +2699,177 @@ model_picker(const struct ui_backend *be, char *sel, size_t cap)
|
||||
changed = 1;
|
||||
}
|
||||
if (changed) {
|
||||
nmatch = pick_match(&list, filter, match);
|
||||
nmatch = pick_match(list, filter, match);
|
||||
cur = 0;
|
||||
top = 0;
|
||||
}
|
||||
}
|
||||
if (win != NULL)
|
||||
delwin(win);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/* the fill callbacks the pickers hand to pick_run */
|
||||
struct model_fill {
|
||||
const struct ui_backend *be;
|
||||
void (*add)(void *cbctx, const char *sel, const char *label, int current);
|
||||
void *addctx;
|
||||
};
|
||||
|
||||
static void
|
||||
model_fill_all(void *ctx, struct pick_list *l)
|
||||
{
|
||||
struct model_fill *f = ctx;
|
||||
|
||||
(void)l;
|
||||
if (f->be->models != NULL)
|
||||
f->be->models(f->be->ctx, f->add, f->addctx);
|
||||
}
|
||||
|
||||
static void
|
||||
model_fill_served(void *ctx, struct pick_list *l)
|
||||
{
|
||||
struct model_fill *f = ctx;
|
||||
|
||||
(void)l;
|
||||
if (f->be->served != NULL)
|
||||
f->be->served(f->be->ctx, f->add, f->addctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* The picker behind /model: Up/Down move, typing filters by selector or model,
|
||||
* Enter takes the highlighted one, Esc cancels. It never talks to the network
|
||||
* on its own; the list is the configuration (be->models), and the switch itself
|
||||
* is the ordinary "/model <selector>" command, so local and --attach behave the
|
||||
* same. The served list is fetched while the window says it is asking.
|
||||
*/
|
||||
static int
|
||||
model_picker(const struct ui_backend *be, char *sel, size_t cap)
|
||||
{
|
||||
struct pick_list list;
|
||||
struct pick_text t;
|
||||
struct model_fill f;
|
||||
struct pick_view view;
|
||||
int rc;
|
||||
|
||||
if (be == NULL || be->models == NULL)
|
||||
return -1;
|
||||
memset(&list, 0, sizeof(list));
|
||||
memset(&t, 0, sizeof(t));
|
||||
memset(&view, 0, sizeof(view));
|
||||
t.title = sp_t("ui.model_title");
|
||||
t.empty = sp_t("ui.model_empty");
|
||||
t.none = sp_t("ui.model_none");
|
||||
t.filter = sp_t("ui.model_filter");
|
||||
t.hint = sp_t("ui.model_hint");
|
||||
f.be = be;
|
||||
f.add = pick_add;
|
||||
f.addctx = &list;
|
||||
if (pick_view_open(&view) != 0)
|
||||
return -1;
|
||||
rc = pick_run(&view, &list, &t, model_fill_all, &f,
|
||||
sp_t("ui.model_probing"),
|
||||
be->served != NULL ? model_fill_served : NULL, &f, sel, cap);
|
||||
pick_view_close(&view);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* ---- /sessions: the source, then the sessions of that source ---------- */
|
||||
|
||||
struct src_fill {
|
||||
const struct ui_backend *be;
|
||||
const char *source;
|
||||
};
|
||||
|
||||
static void
|
||||
src_fill(void *ctx, struct pick_list *l)
|
||||
{
|
||||
struct src_fill *f = ctx;
|
||||
|
||||
if (f->be->sources != NULL)
|
||||
f->be->sources(f->be->ctx, pick_add_count, l);
|
||||
}
|
||||
|
||||
static void
|
||||
source_line(void *cbctx, const char *id, const char *title, int current,
|
||||
long mtime)
|
||||
{
|
||||
struct pick_list *l = cbctx;
|
||||
struct tm tm;
|
||||
char when[32] = "";
|
||||
char label[600];
|
||||
time_t t = (time_t)mtime;
|
||||
|
||||
if (localtime_r(&t, &tm) != NULL)
|
||||
strftime(when, sizeof(when), "%Y-%m-%d %H:%M", &tm);
|
||||
snprintf(label, sizeof(label), "%-16.16s %-22s %-40.40s",
|
||||
when, id, title != NULL && *title != '\0' ? title :
|
||||
sp_t("ui.untitled"));
|
||||
pick_add(l, id, label, current);
|
||||
}
|
||||
|
||||
static void
|
||||
sess_fill(void *ctx, struct pick_list *l)
|
||||
{
|
||||
struct src_fill *f = ctx;
|
||||
|
||||
if (f->be->source_sessions != NULL)
|
||||
f->be->source_sessions(f->be->ctx, f->source, source_line, l);
|
||||
}
|
||||
|
||||
/*
|
||||
* /sessions as two steps: the sources first (this folder, then every foreign
|
||||
* store that has something), then the sessions of the chosen source. A row
|
||||
* from a foreign store carries its source as a prefix ("claude:<id>") and /open
|
||||
* reads that as an import. Esc in the first step ends the whole thing; Esc in
|
||||
* the second goes back to the sources, because a wrong pick there should not
|
||||
* cost the whole picker. Both steps draw in one window: two windows lost the
|
||||
* second Esc of a quick double press (see struct pick_view).
|
||||
*/
|
||||
static int
|
||||
sessions_picker(const struct ui_backend *be, char *sel, size_t cap)
|
||||
{
|
||||
struct pick_list sources, rows;
|
||||
struct pick_text st, rt;
|
||||
struct src_fill f;
|
||||
struct pick_view view;
|
||||
char src[64] = "";
|
||||
int rc = -1;
|
||||
|
||||
if (be == NULL || be->sources == NULL || be->source_sessions == NULL)
|
||||
return -1;
|
||||
memset(&sources, 0, sizeof(sources));
|
||||
memset(&st, 0, sizeof(st));
|
||||
memset(&view, 0, sizeof(view));
|
||||
st.title = sp_t("ui.sessions_title_source");
|
||||
st.empty = sp_t("ui.sessions_no_source");
|
||||
st.none = sp_t("ui.model_none");
|
||||
st.filter = sp_t("ui.model_filter");
|
||||
st.hint = sp_t("ui.sessions_hint_source");
|
||||
f.be = be;
|
||||
f.source = src;
|
||||
if (pick_view_open(&view) != 0)
|
||||
return -1;
|
||||
for (;;) {
|
||||
sources.n = 0;
|
||||
if (pick_run(&view, &sources, &st, src_fill, &f, NULL, NULL,
|
||||
NULL, src, sizeof(src)) != 0)
|
||||
break;
|
||||
memset(&rows, 0, sizeof(rows));
|
||||
memset(&rt, 0, sizeof(rt));
|
||||
rt.title = sp_t("ui.sessions_title_session");
|
||||
rt.empty = sp_t("ui.no_sessions");
|
||||
rt.none = sp_t("ui.model_none");
|
||||
rt.filter = sp_t("ui.model_filter");
|
||||
rt.hint = sp_t("ui.sessions_hint_session");
|
||||
f.source = src;
|
||||
if (pick_run(&view, &rows, &rt, sess_fill, &f, NULL, NULL,
|
||||
NULL, sel, cap) == 0) {
|
||||
rc = 0;
|
||||
break;
|
||||
}
|
||||
/* Esc in the second step: back to the sources */
|
||||
}
|
||||
pick_view_close(&view);
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -3571,6 +3802,12 @@ turn_run_command(struct ui_state *s, const char *line)
|
||||
if (strcmp(cmd, "/sessions") == 0) {
|
||||
struct sp_buf list;
|
||||
|
||||
/*
|
||||
* While a turn runs the picker is refused for the same reason
|
||||
* /open is: the engine cannot be replaced under it. The list
|
||||
* still prints, so the operator can read the ids and open one
|
||||
* after the turn.
|
||||
*/
|
||||
if (sp_buf_init(&list) != 0)
|
||||
return;
|
||||
if (s->be->sessions(s->be->ctx, session_line, &list) == 0)
|
||||
@@ -4211,19 +4448,27 @@ sp_ui_run(const struct ui_backend *be, const char *theme_name,
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* /sessions without an argument picks a session: sources
|
||||
* first, then the rows of the chosen one. The choice turns
|
||||
* into the same "/open <id>" the operator could have typed,
|
||||
* so the import path is one code path, not two.
|
||||
*/
|
||||
if (strcmp(input, "/sessions") == 0) {
|
||||
struct sp_buf list;
|
||||
char sel[128];
|
||||
|
||||
if (sp_buf_init(&list) == 0) {
|
||||
if (be->sessions(be->ctx, session_line,
|
||||
&list) == 0)
|
||||
sp_buf_puts(&list,
|
||||
sp_t("ui.no_sessions"));
|
||||
add_entry(&s, "splinter",
|
||||
list.p != NULL ? list.p : "");
|
||||
sp_buf_free(&list);
|
||||
if (sessions_picker(be, sel, sizeof(sel)) == 0)
|
||||
snprintf(input, sizeof(input), "/open %s", sel);
|
||||
else {
|
||||
clearok(curscr, TRUE);
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
clearok(curscr, TRUE);
|
||||
redraw(&s, be);
|
||||
werase(ui_input);
|
||||
mvwaddstr(ui_input, 0, 0, prompt_mark());
|
||||
wnoutrefresh(ui_input);
|
||||
doupdate();
|
||||
}
|
||||
if (strncmp(input, "/open ", 6) == 0 && input[6] != '\0') {
|
||||
struct sp_buf err;
|
||||
|
||||
+77
-1
@@ -487,6 +487,75 @@ send_sessions(struct client *c, struct proto *p)
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* the sessions of one source, for the second stage of the picker */
|
||||
struct srv_sess {
|
||||
struct sp_buf *b;
|
||||
int n;
|
||||
};
|
||||
|
||||
static void
|
||||
srv_sess_cb(void *cbctx, const char *id, const char *title, int current,
|
||||
long mtime)
|
||||
{
|
||||
struct srv_sess *s = cbctx;
|
||||
|
||||
if (s->n > 0)
|
||||
sp_buf_puts(s->b, ",");
|
||||
s->n++;
|
||||
sp_buf_puts(s->b, "{\"id\":\"");
|
||||
(void)sp_json_escape(s->b, id != NULL ? id : "");
|
||||
sp_buf_puts(s->b, "\",\"title\":\"");
|
||||
(void)sp_json_escape(s->b, title != NULL ? title : "");
|
||||
sp_buf_printf(s->b, "\",\"mtime\":%ld,\"current\":%s}", mtime,
|
||||
current ? "true" : "false");
|
||||
}
|
||||
|
||||
static int
|
||||
send_source_sessions(struct client *c, struct proto *p, const char *source)
|
||||
{
|
||||
struct sp_buf b;
|
||||
struct srv_sess s;
|
||||
int rc;
|
||||
|
||||
if (sp_buf_init(&b) != 0)
|
||||
return -1;
|
||||
ev_start(&b, "sessions");
|
||||
sp_buf_puts(&b, ",\"items\":[");
|
||||
s.b = &b;
|
||||
s.n = 0;
|
||||
(void)sp_engine_source_sessions(p->eng, source, srv_sess_cb, &s);
|
||||
sp_buf_puts(&b, "]}");
|
||||
rc = send_line(c, &b);
|
||||
sp_buf_free(&b);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* which sources the /sessions picker offers */
|
||||
static int
|
||||
send_sources(struct client *c, struct proto *p)
|
||||
{
|
||||
struct sp_session_source v[4];
|
||||
struct sp_buf b;
|
||||
int n, i, rc;
|
||||
|
||||
n = sp_engine_sources(p->eng, v, 4);
|
||||
if (sp_buf_init(&b) != 0)
|
||||
return -1;
|
||||
ev_start(&b, "sources");
|
||||
sp_buf_puts(&b, ",\"items\":[");
|
||||
for (i = 0; i < n; i++) {
|
||||
if (i > 0)
|
||||
sp_buf_puts(&b, ",");
|
||||
sp_buf_puts(&b, "{\"name\":\"");
|
||||
(void)sp_json_escape(&b, v[i].name);
|
||||
sp_buf_printf(&b, "\",\"count\":%d}", v[i].count);
|
||||
}
|
||||
sp_buf_puts(&b, "]}");
|
||||
rc = send_line(c, &b);
|
||||
sp_buf_free(&b);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* What a /model picker may offer: the configuration, and with probe the
|
||||
* endpoint this session is on says it serves as well. Probe is a network call;
|
||||
@@ -723,7 +792,14 @@ handle_line(struct client *c, struct proto *p, const char *line)
|
||||
} else if (strcmp(cmd, "state") == 0) {
|
||||
(void)send_state(c, eng, "state");
|
||||
} else if (strcmp(cmd, "sessions") == 0) {
|
||||
(void)send_sessions(c, p);
|
||||
const char *src = mcp_arg_string(v, "source");
|
||||
|
||||
if (src != NULL && *src != '\0')
|
||||
(void)send_source_sessions(c, p, src);
|
||||
else
|
||||
(void)send_sessions(c, p);
|
||||
} else if (strcmp(cmd, "sources") == 0) {
|
||||
(void)send_sources(c, p);
|
||||
} else if (strcmp(cmd, "models") == 0) {
|
||||
(void)send_models(c, p, mcp_arg_bool(v, "probe", 0));
|
||||
} else if (strcmp(cmd, "open") == 0) {
|
||||
|
||||
@@ -74,6 +74,22 @@ struct ui_backend {
|
||||
int (*sessions)(void *ctx,
|
||||
void (*cb)(void *cbctx, const char *id, const char *title,
|
||||
int current, long mtime), void *cbctx);
|
||||
/*
|
||||
* The sources the picker offers: the backend's own sessions plus every
|
||||
* foreign store that has some. cb gets a name ("splinter", "claude", ...)
|
||||
* and the number of sessions in it.
|
||||
*/
|
||||
int (*sources)(void *ctx,
|
||||
void (*cb)(void *cbctx, const char *name, int count),
|
||||
void *cbctx);
|
||||
/*
|
||||
* The sessions of one source, for the second stage of the picker. A
|
||||
* row from a foreign source has an id of the form "claude:<id>", which
|
||||
* open_session imports and continues; a plain id is this backend's own.
|
||||
*/
|
||||
int (*source_sessions)(void *ctx, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *title,
|
||||
int current, long mtime), void *cbctx);
|
||||
/* switch to another session; the backend replaces its engine */
|
||||
int (*open_session)(void *ctx, const char *id, struct sp_buf *err);
|
||||
/* the transcript so far, one callback per message (no system prompt) */
|
||||
|
||||
@@ -131,6 +131,25 @@ loc_sessions(void *ctx, void (*cb)(void *, const char *, const char *, int,
|
||||
return n;
|
||||
}
|
||||
|
||||
static int
|
||||
loc_sources(void *ctx, void (*cb)(void *, const char *, int), void *cbctx)
|
||||
{
|
||||
struct sp_session_source v[4];
|
||||
int n, i;
|
||||
|
||||
n = sp_engine_sources(ENG(ctx), v, 4);
|
||||
for (i = 0; i < n; i++)
|
||||
cb(cbctx, v[i].name, v[i].count);
|
||||
return n > 0 ? n : 0;
|
||||
}
|
||||
|
||||
static int
|
||||
loc_source_sessions(void *ctx, const char *source, void (*cb)(void *,
|
||||
const char *, const char *, int, long), void *cbctx)
|
||||
{
|
||||
return sp_engine_source_sessions(ENG(ctx), source, cb, cbctx);
|
||||
}
|
||||
|
||||
static int
|
||||
loc_open_session(void *ctx, const char *id, struct sp_buf *err)
|
||||
{
|
||||
@@ -272,6 +291,8 @@ static const struct ui_backend local = {
|
||||
.models = loc_models,
|
||||
.served = loc_served,
|
||||
.sessions = loc_sessions,
|
||||
.sources = loc_sources,
|
||||
.source_sessions = loc_source_sessions,
|
||||
.open_session = loc_open_session,
|
||||
.history = loc_history,
|
||||
.host = loc_host,
|
||||
|
||||
@@ -51,6 +51,13 @@ struct rem_sess {
|
||||
int current;
|
||||
};
|
||||
|
||||
/* a source of the /sessions picker, and the rows of the one asked for */
|
||||
#define REM_MAX_SOURCES 8
|
||||
struct rem_source {
|
||||
char name[16];
|
||||
int count;
|
||||
};
|
||||
|
||||
struct rem {
|
||||
int fd;
|
||||
struct sp_buf in; /* bytes not yet cut into lines */
|
||||
@@ -67,6 +74,9 @@ struct rem {
|
||||
struct rem_sess sess[100];
|
||||
int nsess;
|
||||
int got_sessions;
|
||||
struct rem_source src[REM_MAX_SOURCES];
|
||||
int nsrc;
|
||||
int got_sources;
|
||||
int got_opened;
|
||||
int sess_gone; /* the restored session was not there */
|
||||
char gone_old[64], gone_new[64];
|
||||
@@ -362,6 +372,24 @@ dispatch(struct rem *r, const char *line)
|
||||
}
|
||||
}
|
||||
r->got_history = 1;
|
||||
} else if (strcmp(ev, "sources") == 0) {
|
||||
const mcp_json *items = mcp_arg(v, "items");
|
||||
size_t i, n = items != NULL ? mcp_array_len(items) : 0;
|
||||
|
||||
r->nsrc = 0;
|
||||
for (i = 0; i < n && r->nsrc < REM_MAX_SOURCES; i++) {
|
||||
const mcp_json *it = mcp_array_at(items, i);
|
||||
const char *name = mcp_arg_string(it, "name");
|
||||
|
||||
if (name == NULL)
|
||||
continue;
|
||||
snprintf(r->src[r->nsrc].name,
|
||||
sizeof(r->src[0].name), "%s", name);
|
||||
r->src[r->nsrc].count =
|
||||
(int)mcp_arg_int(it, "count", 0);
|
||||
r->nsrc++;
|
||||
}
|
||||
r->got_sources = 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;
|
||||
@@ -833,6 +861,55 @@ rem_sessions(void *ctx, void (*cb)(void *, const char *, const char *, int,
|
||||
return r->nsess;
|
||||
}
|
||||
|
||||
static int
|
||||
rem_source_sessions(void *ctx, const char *source, 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", "source", source != NULL ? source : "") != 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_sources(void *ctx, void (*cb)(void *, const char *, int), void *cbctx)
|
||||
{
|
||||
struct rem *r = ctx;
|
||||
long deadline;
|
||||
int i;
|
||||
|
||||
if (r->fd < 0 || send_cmd(r, "sources", NULL, NULL) != 0)
|
||||
return 0;
|
||||
r->got_sources = 0;
|
||||
deadline = now_ms() + 5000;
|
||||
while (!r->got_sources && now_ms() < deadline) {
|
||||
int rc = next_line(r, 5000);
|
||||
|
||||
if (rc <= 0)
|
||||
break;
|
||||
(void)dispatch(r, r->line.p);
|
||||
}
|
||||
for (i = 0; i < r->nsrc; i++)
|
||||
cb(cbctx, r->src[i].name, r->src[i].count);
|
||||
return r->nsrc;
|
||||
}
|
||||
|
||||
static int
|
||||
rem_open_session(void *ctx, const char *id, struct sp_buf *err)
|
||||
{
|
||||
@@ -1145,6 +1222,8 @@ sp_ui_backend_remote(const char *socket_path, struct sp_buf *err)
|
||||
be.models = rem_models;
|
||||
be.served = rem_served;
|
||||
be.sessions = rem_sessions;
|
||||
be.sources = rem_sources;
|
||||
be.source_sessions = rem_source_sessions;
|
||||
be.open_session = rem_open_session;
|
||||
be.take_steer = rem_take_steer;
|
||||
be.history = rem_history;
|
||||
|
||||
@@ -40,6 +40,27 @@ contains(const char *hay, const char *needle)
|
||||
return hay != NULL && strstr(hay, needle) != NULL;
|
||||
}
|
||||
|
||||
/* collects the picker rows the engine offers for one source */
|
||||
struct rowset {
|
||||
char id[8][128];
|
||||
int cur[8];
|
||||
int n;
|
||||
};
|
||||
|
||||
static void
|
||||
row_cb(void *cbctx, const char *id, const char *title, int current, long mtime)
|
||||
{
|
||||
struct rowset *r = cbctx;
|
||||
|
||||
(void)title;
|
||||
(void)mtime;
|
||||
if (r->n >= 8)
|
||||
return;
|
||||
snprintf(r->id[r->n], sizeof(r->id[r->n]), "%s", id);
|
||||
r->cur[r->n] = current;
|
||||
r->n++;
|
||||
}
|
||||
|
||||
/* the whole turn, by draining the queue until the engine says idle */
|
||||
static void
|
||||
run_turn(struct sp_engine *eng)
|
||||
@@ -460,6 +481,46 @@ main(void)
|
||||
CHECK(seen);
|
||||
}
|
||||
|
||||
/*
|
||||
* The picker's two halves. Sources first: this folder is always one, and
|
||||
* a foreign store that has nothing is not offered. Then the rows of one
|
||||
* source: an own session keeps its bare id (the picker turns it into
|
||||
* /open <id>), and the current one is marked.
|
||||
*/
|
||||
{
|
||||
struct sp_session_source src[4];
|
||||
static struct rowset rows;
|
||||
int n, i, saw_prev = 0, saw_cur = 0;
|
||||
|
||||
memset(&rows, 0, sizeof(rows));
|
||||
|
||||
n = sp_engine_sources(eng, src, 4);
|
||||
CHECK(n >= 1);
|
||||
CHECK(n >= 1 && strcmp(src[0].name, "splinter") == 0);
|
||||
CHECK(n >= 1 && src[0].count >= 2);
|
||||
for (i = 1; i < n; i++)
|
||||
CHECK(strcmp(src[i].name, "splinter") != 0);
|
||||
/*
|
||||
* A source this build cannot reach is empty, not a crash: the
|
||||
* engine logs why and the picker shows an empty list.
|
||||
*/
|
||||
CHECK(sp_engine_source_sessions(eng, "claude", row_cb, &rows) == 0);
|
||||
|
||||
rows.n = 0;
|
||||
CHECK(sp_engine_source_sessions(eng, "splinter", row_cb,
|
||||
&rows) == 2);
|
||||
for (i = 0; i < rows.n; i++) {
|
||||
CHECK(strchr(rows.id[i], ':') == NULL);
|
||||
if (strcmp(rows.id[i], "sess-prev") == 0)
|
||||
saw_prev = 1;
|
||||
if (strcmp(rows.id[i],
|
||||
sp_engine_session_id(eng)) == 0 && rows.cur[i])
|
||||
saw_cur = 1;
|
||||
}
|
||||
CHECK(saw_prev);
|
||||
CHECK(saw_cur);
|
||||
}
|
||||
|
||||
/*
|
||||
* /new drops the conversation, not the way of working: the active
|
||||
* profile's prompt goes back on top, and the new session gets an id of
|
||||
|
||||
@@ -241,6 +241,27 @@ while True:
|
||||
if ev.get("ev") == "state":
|
||||
print("still_a" if ev.get("session") == sessA else "not_a")
|
||||
break
|
||||
# ---- the picker's two steps: sources, then the rows of one source ----
|
||||
f.write(json.dumps({"cmd": "sources"}) + "\n")
|
||||
f.flush()
|
||||
while True:
|
||||
ev = json.loads(f.readline())
|
||||
if ev.get("ev") == "sources":
|
||||
names = [i.get("name") for i in ev.get("items", [])]
|
||||
counts = {i.get("name"): i.get("count") for i in ev.get("items", [])}
|
||||
print("sources_ok" if names and names[0] == "splinter" and
|
||||
counts.get("splinter", 0) >= 2 else "sources_bad")
|
||||
break
|
||||
f.write(json.dumps({"cmd": "sessions", "source": "splinter"}) + "\n")
|
||||
f.flush()
|
||||
while True:
|
||||
ev = json.loads(f.readline())
|
||||
if ev.get("ev") == "sessions":
|
||||
ids = [i.get("id") for i in ev.get("items", [])]
|
||||
cur = [i.get("id") for i in ev.get("items", []) if i.get("current")]
|
||||
print("source_rows_ok" if sessA in ids and cur == [sessA] else
|
||||
"source_rows_bad")
|
||||
break
|
||||
reply = command("/rm does-not-exist")
|
||||
print("rm_missing_ok" if "No such session" in reply else "rm_missing_bad")
|
||||
reply = command("/rm " + sessB)
|
||||
@@ -274,6 +295,8 @@ checkgrep 'open_back_ok' "$D/events.out"
|
||||
checkgrep 'history_restored' "$D/events.out"
|
||||
checkgrep 'open_missing$' "$D/events.out"
|
||||
checkgrep 'still_a' "$D/events.out"
|
||||
checkgrep 'sources_ok' "$D/events.out"
|
||||
checkgrep 'source_rows_ok' "$D/events.out"
|
||||
checkgrep 'title_ok' "$D/events.out"
|
||||
checkgrep 'rename_ok' "$D/events.out"
|
||||
checkgrep 'rename_listed' "$D/events.out"
|
||||
|
||||
@@ -220,13 +220,31 @@ start 2
|
||||
typed 'single word ok'; key Enter
|
||||
check shows '<m> ok'
|
||||
old=$(basename "$(ls "$D/home/.splinter/"*.jsonl | head -1)" .jsonl)
|
||||
# /sessions is a picker, not a printout: the sources first (this folder is
|
||||
# always one), Enter opens that source's rows, Enter again opens the session,
|
||||
# Esc steps back and a second Esc cancels.
|
||||
typed '/sessions'; key Enter
|
||||
check shows 'sessions: source'
|
||||
check shows 'Enter opens a source' # the picker is open, not just drawn
|
||||
key Enter
|
||||
check shows 'Enter opens the session'
|
||||
check shows "$old"
|
||||
check shows 'single word ok' # the first user line is the title
|
||||
key Escape # back to the sources
|
||||
sleep 0.3
|
||||
key Escape # and out
|
||||
sleep 0.5
|
||||
check not_shows 'Enter opens the session'
|
||||
typed '/rename Lokale Sitzung'; key Enter
|
||||
check shows 'Lokale Sitzung'
|
||||
typed '/sessions'; key Enter
|
||||
check shows 'Enter opens a source'
|
||||
key Enter
|
||||
check shows 'Enter opens the session'
|
||||
check shows 'Lokale Sitzung'
|
||||
key Escape; sleep 0.3; key Escape
|
||||
sleep 0.5
|
||||
check not_shows 'Enter opens the session'
|
||||
typed '/open new'; key Enter
|
||||
check shows 'session opened'
|
||||
sleep 1
|
||||
|
||||
Reference in new issue
Block a user