ui, model, cost and idle timeouts
- lcm: the sidebar widget and /status segment show the context, the number of
compactions, messages and bytes of the last cut, and how many old tool
outputs were trimmed. The counters are the compactor's own, not an estimate.
- cost: a provider-reported USD cost (usage.cost/cost_usd) is carried through
sse/openai, the agent and the engine. /cost reports it with the cache share
and splits judged from plain turns; the notes say that an upstream judge is
not visible in the provider's usage. The status plugin and junction prefer
the reported number over list-price arithmetic.
- curses: the running turn's seconds moved from the input line to the status
bar. Notes and errors are transcript rows below the cause, never text in the
input line or the status bar; a failed backend request is a status row and is
retried by the agent. The steer prompt is only "steer> " and what was typed.
- model: /model probe works in the chat. The picker lists the configuration at
once, then asks the endpoint the session is on what it serves and adds those
models, so a router's whole menu is selectable. provider/model selectors now
apply the right provider instead of becoming a model name. The choice is
written to <session>.model and restored when that session is reopened; /new
carries the current model into the new session.
- sidebar: plugin titles are bold and underlined (this curses has no italic
attribute), with a rule between the plugin blocks.
- run/cli: a command timeout is an idle window. Every chunk on stdout or
stderr resets it, and the process is asked with waitpid(WNOHANG) whether it
is still there before the window is extended. A long build that keeps
reporting progress is no longer killed for being long; a silent hang is.
- config: ';' separates settings on one line, so
plugin lcm { keep = 6; tool_trim = 8192 } parses as two keys.
- tests: lcm widget/status counters, one-line config blocks, session model
restore and /model probe, cli idle timeout with a talkative and a silent
command, run output reset, and the UI status/retry rows.
make check: all tests passed.
This commit is contained in:
1 parent
dc9a1c62ec
commit
af2001bfee
33 files changed
+1035
-184
No files matched your search
@@ -722,6 +722,8 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
|
||||
reply.tokens_in : 0);
|
||||
a->stats.tokens_out += (size_t)(reply.tokens_out > 0 ?
|
||||
reply.tokens_out : 0);
|
||||
if (reply.cost > 0)
|
||||
a->stats.cost += reply.cost;
|
||||
/*
|
||||
* Some providers do not report usage on a tool-call step. A zero
|
||||
* there is "unknown", not "the context became empty": keep the
|
||||
|
||||
+27
-7
@@ -133,9 +133,11 @@ extern char **environ;
|
||||
* Run a shell command for the model.
|
||||
*
|
||||
* fork/exec instead of popen(), for three reasons popen cannot give:
|
||||
* - a timeout: the child gets its own process group, and when it outlives
|
||||
* SPLINTER_RUN_TIMEOUT (default 300 s) the whole group is killed - a
|
||||
* 'sleep 1000' or an editor waiting for input no longer holds the agent;
|
||||
* - a timeout: the child gets its own process group, and when it stays
|
||||
* silent for SPLINTER_RUN_TIMEOUT (default 600 s) the whole group is
|
||||
* killed - a 'sleep 1000' or an editor waiting for input no longer holds
|
||||
* the agent. Every chunk the command prints moves the deadline: a build
|
||||
* that reports progress is working, not hung;
|
||||
* - stdin is /dev/null: a command that reads input used to read the curses
|
||||
* terminal, stealing the operator's keystrokes;
|
||||
* - the environment is filtered: see secret_env above.
|
||||
@@ -150,6 +152,7 @@ tool_run(const char *args, struct sp_buf *out, void *ctx)
|
||||
char **envp;
|
||||
size_t total = 0, n, i, j;
|
||||
int fds[2], status = 0, timeout_s, timed_out = 0, cut = 0;
|
||||
int reaped = 0;
|
||||
pid_t child;
|
||||
time_t end;
|
||||
|
||||
@@ -243,18 +246,35 @@ tool_run(const char *args, struct sp_buf *out, void *ctx)
|
||||
if (sp_buf_add(out, chunk, (size_t)got) != 0)
|
||||
break;
|
||||
total += (size_t)got;
|
||||
/*
|
||||
* The command said something, so it is working: give it
|
||||
* another full window. Ask the process, not the pipe - a
|
||||
* closed child can still have bytes buffered here.
|
||||
*/
|
||||
if (!reaped) {
|
||||
pid_t r;
|
||||
|
||||
do {
|
||||
r = waitpid(child, &status, WNOHANG);
|
||||
} while (r < 0 && errno == EINTR);
|
||||
if (r == child)
|
||||
reaped = 1;
|
||||
}
|
||||
if (!reaped)
|
||||
end = time(NULL) + timeout_s;
|
||||
}
|
||||
if (timed_out || cut)
|
||||
(void)kill(-child, SIGKILL);
|
||||
close(fds[0]);
|
||||
while (waitpid(child, &status, 0) < 0 && errno == EINTR)
|
||||
;
|
||||
if (!reaped)
|
||||
while (waitpid(child, &status, 0) < 0 && errno == EINTR)
|
||||
;
|
||||
sp_buf_free(&keep);
|
||||
if (cut)
|
||||
sp_buf_puts(out, "\n[output truncated at 1 MiB]");
|
||||
if (timed_out)
|
||||
return sp_buf_printf(out, "\n[timed out after %d s, killed]",
|
||||
timeout_s);
|
||||
return sp_buf_printf(out,
|
||||
"\n[timed out: no output for %d s, killed]", timeout_s);
|
||||
if (WIFEXITED(status))
|
||||
return sp_buf_printf(out, "\n[exit status %d]",
|
||||
WEXITSTATUS(status));
|
||||
|
||||
@@ -74,7 +74,8 @@ static void
|
||||
skip(struct cb *c, int nl)
|
||||
{
|
||||
for (;;) {
|
||||
if (*c->p == ' ' || *c->p == '\t' || *c->p == '\r')
|
||||
if (*c->p == ' ' || *c->p == '\t' || *c->p == '\r' ||
|
||||
*c->p == ';')
|
||||
c->p++;
|
||||
else if (*c->p == '\n' && nl) {
|
||||
c->line++;
|
||||
@@ -152,7 +153,8 @@ scalar(struct cb *c)
|
||||
} else {
|
||||
while (*c->p != '\0' && !isspace((unsigned char)*c->p) &&
|
||||
*c->p != '#' && *c->p != ',' && *c->p != ']' &&
|
||||
*c->p != '}' && *c->p != '[' && *c->p != '{') {
|
||||
*c->p != '}' && *c->p != '[' && *c->p != '{' &&
|
||||
*c->p != ';') {
|
||||
tmp[n] = *c->p++;
|
||||
if (n + 1 < sizeof(tmp))
|
||||
n++;
|
||||
|
||||
+415
-37
@@ -121,6 +121,14 @@ struct sp_engine {
|
||||
/* the last turn, for the sidebar and /status */
|
||||
size_t last_ms, last_steps, last_calls;
|
||||
size_t turn_out0; /* tokens_out when the turn began */
|
||||
size_t turn_in0; /* tokens_in when the turn began */
|
||||
double turn_cost0; /* cost when the turn began */
|
||||
struct {
|
||||
unsigned turns;
|
||||
unsigned long ms;
|
||||
size_t in, out;
|
||||
double cost;
|
||||
} judged, plain; /* the two kinds, for /cost */
|
||||
size_t last_turn_out; /* output tokens it produced */
|
||||
struct timespec turn_t0;
|
||||
/*
|
||||
@@ -223,6 +231,7 @@ sp_engine_state(struct sp_engine *e, struct sp_engine_state *st)
|
||||
pthread_mutex_lock(&e->lock);
|
||||
st->tokens_in = e->snapshot.tokens_in;
|
||||
st->tokens_out = e->snapshot.tokens_out;
|
||||
st->cost = e->snapshot.cost;
|
||||
st->ctx_used = e->snapshot.last_in;
|
||||
st->cached = e->snapshot.last_cached;
|
||||
st->turn_tokens_out = e->running && e->snapshot.tokens_out >=
|
||||
@@ -291,6 +300,78 @@ sp_engine_models(struct sp_engine *e, struct sp_model_choice *v, size_t max)
|
||||
return sp_provider_choices(e->conf, e->cur_model, v, max);
|
||||
}
|
||||
|
||||
/*
|
||||
* The endpoint says what it really serves; the configured model may be one of
|
||||
* several aliases or not listed at all. Only the provider the session is on is
|
||||
* asked: /model with no argument must open at once, and one GET is enough to
|
||||
* show the rest of that endpoint's menu.
|
||||
*/
|
||||
size_t
|
||||
sp_engine_served_models(struct sp_engine *e, struct sp_model_choice *v,
|
||||
size_t max)
|
||||
{
|
||||
struct sp_provider p;
|
||||
struct sp_buf ids, perr;
|
||||
const char *act;
|
||||
char *m, *save = NULL;
|
||||
size_t i, n = 0, np;
|
||||
|
||||
if (e == NULL || e->conf == NULL || v == NULL || max == 0 ||
|
||||
e->cur_endpoint[0] == '\0')
|
||||
return 0;
|
||||
memset(&p, 0, sizeof(p));
|
||||
act = sp_usage_active();
|
||||
np = sp_provider_count(e->conf);
|
||||
for (i = 0; i < np; i++) {
|
||||
struct sp_provider q;
|
||||
|
||||
if (sp_provider_at(e->conf, i, &q) != 0)
|
||||
continue;
|
||||
if ((act != NULL && *act != '\0' &&
|
||||
strcmp(q.id, act) == 0) ||
|
||||
strcmp(q.endpoint, e->cur_endpoint) == 0) {
|
||||
p = q;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (p.id[0] == '\0') {
|
||||
/* --endpoint/--model without a provider block */
|
||||
snprintf(p.id, sizeof(p.id), "%s",
|
||||
act != NULL && *act != '\0' ? act : "main");
|
||||
snprintf(p.kind, sizeof(p.kind), "%s", e->conf->kind);
|
||||
snprintf(p.endpoint, sizeof(p.endpoint), "%s", e->cur_endpoint);
|
||||
snprintf(p.model, sizeof(p.model), "%s", e->cur_model);
|
||||
snprintf(p.key_env, sizeof(p.key_env), "%s", e->conf->key_env);
|
||||
}
|
||||
if (p.endpoint[0] == '\0' || strcmp(p.kind, "cli") == 0)
|
||||
return 0;
|
||||
if (sp_buf_init(&ids) != 0)
|
||||
return 0;
|
||||
if (sp_buf_init(&perr) != 0) {
|
||||
sp_buf_free(&ids);
|
||||
return 0;
|
||||
}
|
||||
if (sp_provider_served(&p, &ids, &perr) == 0 && ids.p != NULL) {
|
||||
for (m = strtok_r(ids.p, "\n", &save);
|
||||
m != NULL && n < max; m = strtok_r(NULL, "\n", &save)) {
|
||||
if (*m == '\0' || strcmp(m, p.model) == 0)
|
||||
continue;
|
||||
if (p.main || strcmp(p.id, "main") == 0)
|
||||
snprintf(v[n].sel, sizeof(v[n].sel), "%s", m);
|
||||
else
|
||||
snprintf(v[n].sel, sizeof(v[n].sel), "%s/%s",
|
||||
p.id, m);
|
||||
snprintf(v[n].label, sizeof(v[n].label), "%s %s",
|
||||
p.id, m);
|
||||
v[n].current = strcmp(m, e->cur_model) == 0;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
sp_buf_free(&ids);
|
||||
sp_buf_free(&perr);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* <session dir>/<id>.agent: the profile a session was left in */
|
||||
static void
|
||||
profile_path(struct sp_engine *e, char *buf, size_t cap)
|
||||
@@ -315,6 +396,94 @@ profile_save(struct sp_engine *e)
|
||||
(void)chmod(path, 0600);
|
||||
}
|
||||
|
||||
/* <session dir>/<id>.model: the model /model chose in this session */
|
||||
static void
|
||||
session_model_path(struct sp_engine *e, char *buf, size_t cap)
|
||||
{
|
||||
snprintf(buf, cap, "%s/%s.model", e->sess.dir, e->sess.id);
|
||||
}
|
||||
|
||||
static int
|
||||
session_model_save(struct sp_engine *e, const char *sel, char *err,
|
||||
size_t errcap)
|
||||
{
|
||||
char path[800], tmp[820];
|
||||
FILE *f;
|
||||
|
||||
if (e == NULL || sel == NULL || *sel == '\0')
|
||||
return -1;
|
||||
session_model_path(e, path, sizeof(path));
|
||||
snprintf(tmp, sizeof(tmp), "%s.tmp", path);
|
||||
f = fopen(tmp, "w");
|
||||
if (f == NULL) {
|
||||
if (err != NULL && errcap > 0)
|
||||
snprintf(err, errcap, "%s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
(void)fchmod(fileno(f), 0600);
|
||||
fprintf(f, "%s\n", sel);
|
||||
if (fclose(f) != 0 || rename(tmp, path) != 0) {
|
||||
if (err != NULL && errcap > 0)
|
||||
snprintf(err, errcap, "%s", strerror(errno));
|
||||
(void)unlink(tmp);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
session_model_read(struct sp_engine *e, char *buf, size_t cap)
|
||||
{
|
||||
char path[800], line[320];
|
||||
FILE *f;
|
||||
size_t n;
|
||||
|
||||
if (e == NULL || e->sess.dir[0] == '\0' || buf == NULL || cap == 0)
|
||||
return -1;
|
||||
session_model_path(e, path, sizeof(path));
|
||||
f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
return -1;
|
||||
if (fgets(line, sizeof(line), f) == NULL) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
line[strcspn(line, "\r\n")] = '\0';
|
||||
n = strlen(line);
|
||||
if (n == 0 || n >= cap)
|
||||
return -1;
|
||||
memcpy(buf, line, n + 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* the selector that puts this engine back on the model it is on now */
|
||||
static void
|
||||
current_model_selector(struct sp_engine *e, char *buf, size_t cap)
|
||||
{
|
||||
const char *act = sp_usage_active();
|
||||
|
||||
if (act != NULL && *act != '\0' && strcmp(act, "main") != 0)
|
||||
snprintf(buf, cap, "%s/%s", act, e->cur_model);
|
||||
else
|
||||
snprintf(buf, cap, "%s", e->cur_model);
|
||||
}
|
||||
|
||||
/* the transcript note after a successful /model: saved in this session */
|
||||
static void
|
||||
session_model_note(struct sp_engine *e, const char *sel, struct sp_buf *out,
|
||||
const char *model, const char *provider)
|
||||
{
|
||||
char err[128];
|
||||
|
||||
err[0] = '\0';
|
||||
if (session_model_save(e, sel, err, sizeof(err)) != 0)
|
||||
sp_buf_printf(out, sp_t("model.save_failed"), model, provider,
|
||||
err[0] != '\0' ? err : "?");
|
||||
else
|
||||
sp_buf_printf(out, sp_t("model.switched"), model, provider);
|
||||
}
|
||||
|
||||
static int
|
||||
profile_load(struct sp_engine *e, char *buf, size_t cap)
|
||||
{
|
||||
@@ -932,6 +1101,8 @@ turn_thread(void *arg)
|
||||
sp_buf_clear(&e->err);
|
||||
pthread_mutex_lock(&e->lock);
|
||||
e->turn_out0 = e->ag.stats.tokens_out;
|
||||
e->turn_in0 = e->ag.stats.tokens_in;
|
||||
e->turn_cost0 = e->ag.stats.cost;
|
||||
pthread_mutex_unlock(&e->lock);
|
||||
rc = sp_agent_run(&e->ag, text, &e->answer, &e->err);
|
||||
if (e->have_swarm)
|
||||
@@ -1026,6 +1197,28 @@ turn_thread(void *arg)
|
||||
e->last_ms = ms;
|
||||
e->last_turn_out = e->ag.stats.tokens_out >= e->turn_out0 ?
|
||||
e->ag.stats.tokens_out - e->turn_out0 : 0;
|
||||
{
|
||||
struct sp_engine *ce = e;
|
||||
int is_judged = strstr(ce->cur_model, "judged") != NULL;
|
||||
size_t tin = ce->ag.stats.tokens_in >= ce->turn_in0 ?
|
||||
ce->ag.stats.tokens_in - ce->turn_in0 : 0;
|
||||
double c = ce->ag.stats.cost >= ce->turn_cost0 ?
|
||||
ce->ag.stats.cost - ce->turn_cost0 : 0;
|
||||
|
||||
if (is_judged) {
|
||||
ce->judged.turns++;
|
||||
ce->judged.ms += (unsigned long)ms;
|
||||
ce->judged.in += tin;
|
||||
ce->judged.out += ce->last_turn_out;
|
||||
ce->judged.cost += c;
|
||||
} else {
|
||||
ce->plain.turns++;
|
||||
ce->plain.ms += (unsigned long)ms;
|
||||
ce->plain.in += tin;
|
||||
ce->plain.out += ce->last_turn_out;
|
||||
ce->plain.cost += c;
|
||||
}
|
||||
}
|
||||
e->last_steps = e->ag.stats.steps;
|
||||
e->last_calls = e->ag.stats.tool_calls;
|
||||
pthread_mutex_unlock(&e->lock);
|
||||
@@ -1522,7 +1715,7 @@ int
|
||||
sp_engine_command_names(struct sp_engine *e, char (*names)[64], int max)
|
||||
{
|
||||
static const char *builtin[] = {
|
||||
"agent", "agents", "compact", "connect", "continue", "exit", "model",
|
||||
"agent", "agents", "compact", "connect", "continue", "cost", "exit", "model",
|
||||
"new", "q", "quit", "rename", "reset", "resume", "rm", "status",
|
||||
"swarm", "usage", "widgets"
|
||||
};
|
||||
@@ -1564,12 +1757,17 @@ sp_engine_command_names(struct sp_engine *e, char (*names)[64], int max)
|
||||
return n;
|
||||
}
|
||||
|
||||
/* /model: list, switch provider, pool or model; remember the choice */
|
||||
/*
|
||||
* /model: list, switch provider, pool or model. The choice is written to the
|
||||
* session's own sidecar, so reopening that conversation brings it back without
|
||||
* changing what a new conversation starts on. 'probe' asks the configured
|
||||
* endpoints which models they really serve.
|
||||
*/
|
||||
static void
|
||||
cmd_model(struct sp_engine *e, const char *args, struct sp_buf *out)
|
||||
{
|
||||
struct sp_provider p;
|
||||
struct sp_buf perr, serr;
|
||||
struct sp_buf perr;
|
||||
size_t idx = 0, i, n;
|
||||
char poolname[40];
|
||||
int is_pool = 0;
|
||||
@@ -1607,6 +1805,45 @@ cmd_model(struct sp_engine *e, const char *args, struct sp_buf *out)
|
||||
sp_buf_printf(out, " %s",
|
||||
e->conf->pool[i].member[j]);
|
||||
}
|
||||
} else if (strcmp(args, "probe") == 0) {
|
||||
size_t total = 0;
|
||||
|
||||
n = sp_provider_count(e->conf);
|
||||
for (i = 0; i < n; i++) {
|
||||
struct sp_buf ids, perr2;
|
||||
char *m, *save = NULL;
|
||||
|
||||
if (sp_provider_at(e->conf, i, &p) != 0 ||
|
||||
p.endpoint[0] == '\0' ||
|
||||
strcmp(p.kind, "cli") == 0)
|
||||
continue;
|
||||
if (sp_buf_init(&ids) != 0)
|
||||
break;
|
||||
if (sp_buf_init(&perr2) != 0) {
|
||||
sp_buf_free(&ids);
|
||||
break;
|
||||
}
|
||||
if (sp_provider_served(&p, &ids, &perr2) == 0 &&
|
||||
ids.p != NULL) {
|
||||
for (m = strtok_r(ids.p, "\n", &save);
|
||||
m != NULL; m = strtok_r(NULL, "\n", &save)) {
|
||||
if (*m == '\0')
|
||||
continue;
|
||||
if (p.main ||
|
||||
strcmp(p.id, "main") == 0)
|
||||
sp_buf_printf(out,
|
||||
"\n %s", m);
|
||||
else
|
||||
sp_buf_printf(out,
|
||||
"\n %s/%s", p.id, m);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
sp_buf_free(&ids);
|
||||
sp_buf_free(&perr2);
|
||||
}
|
||||
if (total == 0)
|
||||
sp_buf_puts(out, sp_t("model.probe_none"));
|
||||
} else if (is_pool) {
|
||||
struct sp_choice ch;
|
||||
|
||||
@@ -1625,12 +1862,13 @@ cmd_model(struct sp_engine *e, const char *args, struct sp_buf *out)
|
||||
"%s", ch.d->endpoint);
|
||||
snprintf(e->cur_model, sizeof(e->cur_model), "%s",
|
||||
ch.model[0] != '\0' ? ch.model : ch.d->model);
|
||||
sp_buf_printf(out, sp_t("model.pool"), poolname,
|
||||
session_model_note(e, poolname, out,
|
||||
e->cur_model, ch.d->id);
|
||||
}
|
||||
sp_buf_free(&perr);
|
||||
}
|
||||
} else if (sp_provider_find(e->conf, args, &idx)) {
|
||||
/* a provider id: its configured model, main included */
|
||||
sp_buf_init(&perr);
|
||||
if (sp_provider_use(&e->ag, e->conf, idx, &perr) != 0) {
|
||||
sp_buf_printf(out, sp_t("model.failed"),
|
||||
@@ -1640,43 +1878,79 @@ cmd_model(struct sp_engine *e, const char *args, struct sp_buf *out)
|
||||
p.endpoint);
|
||||
snprintf(e->cur_model, sizeof(e->cur_model), "%s",
|
||||
p.model);
|
||||
sp_buf_init(&serr);
|
||||
sp_buf_printf(out, sp_provider_save_choice(e->conf, &p,
|
||||
&serr) == 0 ? sp_t("model.saved") :
|
||||
sp_t("model.switched"), p.model, p.id);
|
||||
sp_buf_free(&serr);
|
||||
session_model_note(e, p.id, out, p.model, p.id);
|
||||
}
|
||||
sp_buf_free(&perr);
|
||||
} else {
|
||||
struct sp_provider chosen;
|
||||
int ok;
|
||||
struct sp_choice ch;
|
||||
|
||||
memset(&p, 0, sizeof(p));
|
||||
if (!sp_provider_find(e->conf, e->cur_model, &idx) ||
|
||||
sp_provider_at(e->conf, idx, &p) != 0)
|
||||
(void)sp_provider_at(e->conf, 0, &p);
|
||||
sp_buf_init(&perr);
|
||||
ok = sp_provider_apply(&e->ag, p.kind, p.endpoint, args,
|
||||
p.key_env, &perr) == 0;
|
||||
if (ok) {
|
||||
memset(&chosen, 0, sizeof(chosen));
|
||||
snprintf(chosen.id, sizeof(chosen.id), "%s", p.id);
|
||||
snprintf(chosen.endpoint, sizeof(chosen.endpoint), "%s",
|
||||
p.endpoint);
|
||||
snprintf(chosen.model, sizeof(chosen.model), "%s", args);
|
||||
snprintf(chosen.key_env, sizeof(chosen.key_env), "%s",
|
||||
p.key_env);
|
||||
snprintf(e->cur_model, sizeof(e->cur_model), "%s", args);
|
||||
sp_buf_init(&serr);
|
||||
sp_buf_printf(out, sp_provider_save_choice(e->conf,
|
||||
&chosen, &serr) == 0 ? sp_t("model.saved") :
|
||||
sp_t("model.switched"), args, p.id);
|
||||
sp_buf_free(&serr);
|
||||
memset(&ch, 0, sizeof(ch));
|
||||
if (sp_provider_choose(e->conf, args, &ch) == 0 &&
|
||||
ch.d != NULL) {
|
||||
/* provider/model, or a model one provider owns */
|
||||
const char *model = ch.model[0] != '\0' ?
|
||||
ch.model : ch.d->model;
|
||||
char sel[320];
|
||||
|
||||
sp_buf_init(&perr);
|
||||
if (sp_provider_apply(&e->ag, ch.d->kind,
|
||||
ch.d->endpoint, model, ch.d->key_env, &perr) != 0) {
|
||||
sp_buf_printf(out, sp_t("model.failed"),
|
||||
perr.p != NULL ? perr.p : "?");
|
||||
} else {
|
||||
sp_usage_set_active(ch.d->id);
|
||||
snprintf(e->cur_endpoint, sizeof(e->cur_endpoint),
|
||||
"%s", ch.d->endpoint);
|
||||
snprintf(e->cur_model, sizeof(e->cur_model), "%s",
|
||||
model);
|
||||
if (strcmp(ch.d->id, "main") == 0)
|
||||
snprintf(sel, sizeof(sel), "%s", model);
|
||||
else
|
||||
snprintf(sel, sizeof(sel), "%s/%s",
|
||||
ch.d->id, model);
|
||||
session_model_note(e, sel, out, e->cur_model,
|
||||
ch.d->id);
|
||||
}
|
||||
sp_buf_free(&perr);
|
||||
} else {
|
||||
sp_buf_printf(out, sp_t("model.failed"),
|
||||
perr.p != NULL ? perr.p : "?");
|
||||
/*
|
||||
* A bare model the config does not own: it goes on the
|
||||
* endpoint this session is already talking to.
|
||||
*/
|
||||
const char *act = sp_usage_active();
|
||||
char sel[320];
|
||||
int ok;
|
||||
|
||||
memset(&p, 0, sizeof(p));
|
||||
if (act == NULL || *act == '\0' ||
|
||||
!sp_provider_find(e->conf, act, &idx))
|
||||
idx = 0;
|
||||
if (sp_provider_at(e->conf, idx, &p) != 0) {
|
||||
sp_buf_printf(out, sp_t("model.unknown"),
|
||||
args);
|
||||
} else {
|
||||
sp_buf_init(&perr);
|
||||
ok = sp_provider_apply(&e->ag, p.kind,
|
||||
p.endpoint, args, p.key_env, &perr) == 0;
|
||||
if (ok) {
|
||||
snprintf(e->cur_model,
|
||||
sizeof(e->cur_model), "%s", args);
|
||||
if (p.main ||
|
||||
strcmp(p.id, "main") == 0)
|
||||
snprintf(sel, sizeof(sel), "%s",
|
||||
args);
|
||||
else
|
||||
snprintf(sel, sizeof(sel),
|
||||
"%s/%s", p.id, args);
|
||||
session_model_note(e, sel, out, args,
|
||||
p.id);
|
||||
} else {
|
||||
sp_buf_printf(out, sp_t("model.failed"),
|
||||
perr.p != NULL ? perr.p : "?");
|
||||
}
|
||||
sp_buf_free(&perr);
|
||||
}
|
||||
}
|
||||
sp_buf_free(&perr);
|
||||
}
|
||||
if (e->host != NULL)
|
||||
sp_host_set_model(e->host, e->cur_model);
|
||||
@@ -1717,6 +1991,13 @@ cmd_new(struct sp_engine *e, struct sp_buf *out)
|
||||
(void)sp_log_open(e->sess.dir, e->sess.id);
|
||||
sp_logf("session", "started: %s", e->sess.id);
|
||||
profile_save(e);
|
||||
{
|
||||
char sel[320];
|
||||
|
||||
current_model_selector(e, sel, sizeof(sel));
|
||||
if (sel[0] != '\0')
|
||||
(void)session_model_save(e, sel, NULL, 0);
|
||||
}
|
||||
ev_push(e, SP_EV_CLEAR, 0, NULL, NULL);
|
||||
sp_buf_printf(out, sp_t("session.new"), e->sess.id);
|
||||
}
|
||||
@@ -1968,6 +2249,61 @@ cmd_agents(struct sp_engine *e, struct sp_buf *out)
|
||||
sp_buf_puts(out, sp_t("agent.help"));
|
||||
}
|
||||
|
||||
/*
|
||||
* What the session has cost so far and what the provider told us about it.
|
||||
* judge calls happen upstream; the usage object only carries the final answer,
|
||||
* so this reports what is measurable and says where it stops.
|
||||
*/
|
||||
static void
|
||||
cmd_cost(struct sp_engine *e, struct sp_buf *out)
|
||||
{
|
||||
struct sp_agent_stats st = e->snapshot;
|
||||
unsigned cachepct = 0;
|
||||
|
||||
if (st.last_in > 0 && st.last_cached > 0)
|
||||
cachepct = (unsigned)(100.0 * (double)st.last_cached /
|
||||
(double)st.last_in);
|
||||
sp_buf_printf(out, "cost: $%.4f reported, %zu in / %zu out, "
|
||||
"last ctx %u%% cached\n", st.cost, st.tokens_in, st.tokens_out,
|
||||
cachepct);
|
||||
sp_buf_printf(out, "average: $%.5f/turn, %zu ms/turn, %zu calls\n",
|
||||
e->judged.turns + e->plain.turns > 0 ?
|
||||
st.cost / (double)(e->judged.turns + e->plain.turns) : 0.0,
|
||||
e->judged.turns + e->plain.turns > 0 ?
|
||||
(e->judged.ms + e->plain.ms) / (e->judged.turns + e->plain.turns) : 0,
|
||||
st.tool_calls);
|
||||
if (e->judged.turns > 0 || e->plain.turns > 0) {
|
||||
sp_buf_printf(out, "judged: %u turns, %.0f ms/turn, "
|
||||
"%.0f tok/turn, $%.5f/turn\n", e->judged.turns,
|
||||
e->judged.turns ? (double)e->judged.ms / e->judged.turns : 0,
|
||||
e->judged.turns ? (double)(e->judged.in + e->judged.out) /
|
||||
e->judged.turns : 0,
|
||||
e->judged.turns ? e->judged.cost / e->judged.turns : 0);
|
||||
sp_buf_printf(out, "plain: %u turns, %.0f ms/turn, "
|
||||
"%.0f tok/turn, $%.5f/turn\n", e->plain.turns,
|
||||
e->plain.turns ? (double)e->plain.ms / e->plain.turns : 0,
|
||||
e->plain.turns ? (double)(e->plain.in + e->plain.out) /
|
||||
e->plain.turns : 0,
|
||||
e->plain.turns ? e->plain.cost / e->plain.turns : 0);
|
||||
if (e->judged.turns > 0 && e->plain.turns > 0)
|
||||
sp_buf_printf(out, "delta: %+.0f ms, %+.0f tok, "
|
||||
"%+.5f $/turn (judged - plain)\n",
|
||||
(double)e->judged.ms / e->judged.turns -
|
||||
(double)e->plain.ms / e->plain.turns,
|
||||
(double)(e->judged.in + e->judged.out) /
|
||||
e->judged.turns -
|
||||
(double)(e->plain.in + e->plain.out) /
|
||||
e->plain.turns,
|
||||
(e->judged.cost / e->judged.turns) -
|
||||
(e->plain.cost / e->plain.turns));
|
||||
}
|
||||
if (strstr(e->cur_model, "judged") != NULL)
|
||||
sp_buf_puts(out, "note: judge calls happen upstream; the "
|
||||
"provider's usage carries only the final answer, so they "
|
||||
"are not in $ or tokens above - only latency and the "
|
||||
"reported tokens are measurable\n");
|
||||
}
|
||||
|
||||
int
|
||||
sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out)
|
||||
{
|
||||
@@ -2058,6 +2394,10 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out)
|
||||
ev_push(e, SP_EV_STATE, 0, NULL, NULL);
|
||||
return SP_INPUT_HANDLED;
|
||||
}
|
||||
if (strcmp(name, "cost") == 0) {
|
||||
cmd_cost(e, out);
|
||||
return SP_INPUT_HANDLED;
|
||||
}
|
||||
if (strcmp(name, "status") == 0) {
|
||||
cmd_status(e, out);
|
||||
return SP_INPUT_HANDLED;
|
||||
@@ -2402,13 +2742,21 @@ int
|
||||
sp_engine_open(struct sp_engine **out, const struct sp_engine_opts *o,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
struct sp_engine_opts local;
|
||||
struct sp_engine *e;
|
||||
char cwd[700];
|
||||
char cwd[700], saved[320], saved_model[320];
|
||||
int start = -1, code = -1;
|
||||
|
||||
if (out == NULL || o == NULL || o->conf == NULL || o->base == NULL)
|
||||
return -1;
|
||||
*out = NULL;
|
||||
/*
|
||||
* The model a session was left on wins over the configuration when the
|
||||
* session comes back: a choice made with /model belongs to the
|
||||
* conversation, not to every later start.
|
||||
*/
|
||||
local = *o;
|
||||
o = &local;
|
||||
e = calloc(1, sizeof(*e));
|
||||
if (e == NULL) {
|
||||
sp_buf_puts(err, sp_t("err.oom"));
|
||||
@@ -2455,6 +2803,35 @@ sp_engine_open(struct sp_engine **out, const struct sp_engine_opts *o,
|
||||
}
|
||||
if (open_session(e, o, err, &code) != 0)
|
||||
goto fail;
|
||||
if (session_model_read(e, saved, sizeof(saved)) == 0) {
|
||||
struct sp_choice ch;
|
||||
|
||||
memset(&ch, 0, sizeof(ch));
|
||||
if (sp_provider_choose(e->conf, saved, &ch) == 0 &&
|
||||
ch.d != NULL) {
|
||||
local.endpoint = ch.d->endpoint;
|
||||
snprintf(saved_model, sizeof(saved_model), "%s",
|
||||
ch.model[0] != '\0' ? ch.model : ch.d->model);
|
||||
local.model = saved_model;
|
||||
local.kind = ch.d->kind;
|
||||
local.key_env = ch.d->key_env;
|
||||
sp_usage_set_active(ch.d->id);
|
||||
} else if (strchr(saved, '/') == NULL) {
|
||||
/* a model of the endpoint the run was configured with */
|
||||
snprintf(saved_model, sizeof(saved_model), "%s", saved);
|
||||
local.model = saved_model;
|
||||
} else {
|
||||
sp_logf("session", "saved model '%s' is gone; "
|
||||
"using the default", saved);
|
||||
saved[0] = '\0';
|
||||
}
|
||||
if (saved[0] != '\0') {
|
||||
snprintf(e->cur_endpoint, sizeof(e->cur_endpoint), "%s",
|
||||
local.endpoint != NULL ? local.endpoint : "");
|
||||
snprintf(e->cur_model, sizeof(e->cur_model), "%.255s",
|
||||
local.model != NULL ? local.model : "");
|
||||
}
|
||||
}
|
||||
e->ag.sess = &e->sess;
|
||||
sp_logf("session", "started: %s", e->sess.id);
|
||||
if (load_plugins(e, o, err) != 0)
|
||||
@@ -2481,7 +2858,8 @@ sp_engine_open(struct sp_engine **out, const struct sp_engine_opts *o,
|
||||
snprintf(e->conf->endpoint, sizeof(e->conf->endpoint), "%s",
|
||||
o->endpoint);
|
||||
if (e->conf->model[0] == '\0' && o->model != NULL)
|
||||
snprintf(e->conf->model, sizeof(e->conf->model), "%s", o->model);
|
||||
snprintf(e->conf->model, sizeof(e->conf->model), "%.255s",
|
||||
o->model);
|
||||
if (setup_conversation(e, o, err) != 0)
|
||||
goto fail;
|
||||
e->ag.reg = &e->reg;
|
||||
|
||||
@@ -270,6 +270,14 @@ sp_openai_reply(const char *json, struct sp_reply *out, struct sp_buf *err)
|
||||
out->tokens_out = pout;
|
||||
if (cached >= 0)
|
||||
out->tokens_cached = cached;
|
||||
{
|
||||
double c = mcp_arg_num(usage, "cost", -1);
|
||||
|
||||
if (c < 0)
|
||||
c = mcp_arg_num(usage, "cost_usd", -1);
|
||||
if (c >= 0)
|
||||
out->cost = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -303,7 +303,8 @@ http_get(const char *url, const char *const *headers, struct sp_buf *out,
|
||||
|
||||
snprintf(tbuf, sizeof(tbuf), "%d", PROVIDER_GET_TIMEOUT_S);
|
||||
snprintf(cbuf, sizeof(cbuf), "%d", PROVIDER_CONNECT_TIMEOUT_S);
|
||||
args[n++] = "-sS";
|
||||
/* -s, not -sS: a probe can fail while curses owns the terminal */
|
||||
args[n++] = "-s";
|
||||
args[n++] = "--connect-timeout";
|
||||
args[n++] = cbuf;
|
||||
args[n++] = "-m";
|
||||
|
||||
@@ -40,6 +40,7 @@ struct sp_sse {
|
||||
long tokens_in;
|
||||
long tokens_out;
|
||||
long tokens_cached;
|
||||
double cost;
|
||||
};
|
||||
|
||||
#define PENDING_MAX (1u << 20) /* a huge "object" is not a kept promise */
|
||||
@@ -256,6 +257,14 @@ sp_sse_line(struct sp_sse *s, const char *line, struct sp_buf *out,
|
||||
s->tokens_out = out;
|
||||
if (cached >= 0)
|
||||
s->tokens_cached = cached;
|
||||
{
|
||||
double c = mcp_arg_num(usage, "cost", -1);
|
||||
|
||||
if (c < 0)
|
||||
c = mcp_arg_num(usage, "cost_usd", -1);
|
||||
if (c >= 0)
|
||||
s->cost = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,6 +388,7 @@ sp_sse_finish(struct sp_sse *s, struct sp_reply *out, struct sp_buf *err)
|
||||
out->tokens_in = s->tokens_in;
|
||||
out->tokens_out = s->tokens_out;
|
||||
out->tokens_cached = s->tokens_cached;
|
||||
out->cost = s->cost;
|
||||
out->content = strdup(s->content.p != NULL ? s->content.p : "");
|
||||
if (out->content == NULL)
|
||||
return -1;
|
||||
|
||||
+108
-70
@@ -19,9 +19,9 @@
|
||||
* | | session |
|
||||
* | | directory |
|
||||
* +---------------------------------+------------+
|
||||
* | ctx 1290/131072 [|| ] 1% | cache 62% | t/s |
|
||||
* | 12s | ctx 1290/131072 [|| ] 1% | cache 62% | t/s |
|
||||
* +----------------------------------------------+
|
||||
* | build> prompt, or "12s tool steer>" |
|
||||
* | build> prompt, or "steer> " while a turn runs|
|
||||
* +----------------------------------------------+
|
||||
*
|
||||
* Only the UI thread ever calls into curses. The engine runs a turn on a
|
||||
@@ -113,8 +113,11 @@ struct ui_state {
|
||||
size_t gen; /* history changed since painted */
|
||||
size_t painted; /* the generation last drawn */
|
||||
long last_ms;
|
||||
long turn_ms; /* the running turn, for the status bar */
|
||||
int turn_running;
|
||||
size_t tokens_in;
|
||||
size_t tokens_out;
|
||||
double cost;
|
||||
size_t ctx_used; /* prompt tokens of the last request */
|
||||
size_t ctx_max; /* its context window, 0 = unknown */
|
||||
size_t ctx_cached; /* of ctx_used, served from cache */
|
||||
@@ -198,6 +201,7 @@ sync_state(struct ui_state *s)
|
||||
return;
|
||||
s->tokens_in = st.tokens_in;
|
||||
s->tokens_out = st.tokens_out;
|
||||
s->cost = st.cost;
|
||||
s->ctx_used = st.ctx_used;
|
||||
s->ctx_max = st.ctx_max;
|
||||
if (ctxmax != NULL && *ctxmax != '\0')
|
||||
@@ -1042,6 +1046,36 @@ sidebar_heading(int *y, int cols, const char *text)
|
||||
wattron(ui_side, COLOR_PAIR(PAIR_SIDE));
|
||||
}
|
||||
|
||||
/* a plugin block's title: bold, underlined, italic where the terminal has it */
|
||||
static void
|
||||
sidebar_widget_heading(int *y, int cols, const char *text)
|
||||
{
|
||||
int attr = COLOR_PAIR(PAIR_SIDE) | A_BOLD | A_UNDERLINE;
|
||||
|
||||
#ifdef A_ITALIC
|
||||
attr |= A_ITALIC;
|
||||
#endif
|
||||
wattron(ui_side, attr);
|
||||
mvwaddnstr(ui_side, *y, 0, text, cols - 1);
|
||||
wattrset(ui_side, COLOR_PAIR(PAIR_SIDE));
|
||||
(*y)++;
|
||||
}
|
||||
|
||||
/* the line between two plugin blocks; '-' because this curses' ACS is broken */
|
||||
static void
|
||||
sidebar_rule(int *y, int cols)
|
||||
{
|
||||
int i, w = cols > 1 ? cols - 1 : 1;
|
||||
|
||||
if (*y >= getmaxy(ui_side) - 2)
|
||||
return;
|
||||
wattron(ui_side, COLOR_PAIR(PAIR_DIM));
|
||||
for (i = 0; i < w; i++)
|
||||
mvwaddch(ui_side, *y, i, '-');
|
||||
wattrset(ui_side, COLOR_PAIR(PAIR_SIDE));
|
||||
(*y)++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Wrap 'text' into the sidebar from row *y, at most 'maxrows' rows. The
|
||||
* first row of a widget is its title line: bold, so a plugin block reads as a
|
||||
@@ -1224,6 +1258,7 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
if (host != NULL && sp_host_widget_count(host) > 0) {
|
||||
char snap[1024], *line, *save = NULL;
|
||||
char cont_name[48] = "";
|
||||
int first_widget = 1;
|
||||
|
||||
sp_host_widget_snapshot(host, snap, sizeof(snap));
|
||||
for (line = strtok_r(snap, "\n", &save); line != NULL;
|
||||
@@ -1260,11 +1295,16 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
}
|
||||
if (*text == '\0' || y >= getmaxy(ui_side) - 2)
|
||||
continue;
|
||||
/* one blank row between widgets, not between their rows */
|
||||
if (cont_name[0] != '\0' && y < getmaxy(ui_side) - 2)
|
||||
y++;
|
||||
/*
|
||||
* A rule between the plugin blocks, not between their
|
||||
* rows: the sidebar has to read as separate reports.
|
||||
*/
|
||||
if (!first_widget && cont_name[0] != '\0' &&
|
||||
y < getmaxy(ui_side) - 2)
|
||||
sidebar_rule(&y, cols);
|
||||
first_widget = 0;
|
||||
snprintf(key, sizeof(key), "widget.%s", name);
|
||||
sidebar_heading(&y, cols, sp_td(key, name));
|
||||
sidebar_widget_heading(&y, cols, sp_td(key, name));
|
||||
side_text(&y, cols, text, 8, 1);
|
||||
snprintf(cont_name, sizeof(cont_name), "%s", name);
|
||||
}
|
||||
@@ -1337,6 +1377,7 @@ set_ui_fields(struct sp_meter *m, void *ctx)
|
||||
m->cached = s->ctx_cached;
|
||||
m->tok_in = s->tokens_in;
|
||||
m->tok_out = s->tokens_out;
|
||||
m->cost = s->cost;
|
||||
if (s->last_ms > 0 && s->turn_out > 0)
|
||||
m->tps = (long)((double)s->turn_out * 1000.0 /
|
||||
(double)s->last_ms);
|
||||
@@ -1368,6 +1409,11 @@ draw_statusbar(struct ui_state *s)
|
||||
return;
|
||||
werase(ui_status);
|
||||
wattrset(ui_status, COLOR_PAIR(PAIR_STATUS));
|
||||
if (s->turn_running) {
|
||||
n += (size_t)snprintf(line + n, sizeof(line) - n,
|
||||
sp_td("ui.status_turn", "%lds"), s->turn_ms / 1000);
|
||||
n += (size_t)snprintf(line + n, sizeof(line) - n, " | ");
|
||||
}
|
||||
if (max > 0) {
|
||||
int bw = 16, filled, pct;
|
||||
|
||||
@@ -1852,11 +1898,23 @@ model_picker(const struct ui_backend *be, char *sel, size_t cap)
|
||||
return -1;
|
||||
memset(&list, 0, sizeof(list));
|
||||
filter[0] = '\0';
|
||||
be->models(be->ctx, pick_add, &list);
|
||||
nmatch = pick_match(&list, filter, match);
|
||||
win = pick_window(&w, &h);
|
||||
if (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) {
|
||||
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);
|
||||
wattrset(win, COLOR_PAIR(PAIR_MAIN));
|
||||
mvwaddnstr(win, 3, 2, sp_t("ui.model_probing"), w - 4);
|
||||
wnoutrefresh(win);
|
||||
doupdate();
|
||||
be->served(be->ctx, pick_add, &list);
|
||||
}
|
||||
nmatch = pick_match(&list, filter, match);
|
||||
for (;;) {
|
||||
int changed = 0;
|
||||
int listh = h - 4;
|
||||
@@ -2400,9 +2458,6 @@ struct turn_view {
|
||||
struct sp_buf live; /* the step's answer so far */
|
||||
char line[1024]; /* steering text being typed */
|
||||
size_t llen;
|
||||
char note[600]; /* a usage-limit wait, or "" */
|
||||
char steer[160]; /* "steering queued (n)", or "" */
|
||||
unsigned steers; /* how many went out this turn */
|
||||
int rc;
|
||||
int got_end;
|
||||
};
|
||||
@@ -2488,8 +2543,6 @@ finalize_turn(struct ui_state *s, struct turn_view *v, int rc, const char *answe
|
||||
v->ans.id = v->ans.off = v->ans.shown = 0;
|
||||
sp_buf_clear(&v->thinkbuf);
|
||||
sp_buf_clear(&v->live);
|
||||
v->note[0] = '\0';
|
||||
v->steer[0] = '\0';
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -2536,7 +2589,6 @@ drain_events(struct ui_state *s, struct turn_view *v)
|
||||
seg_show(s, &v->ans, "agent", ev.b);
|
||||
v->think.id = v->think.off = v->think.shown = 0;
|
||||
v->ans.id = v->ans.off = v->ans.shown = 0;
|
||||
v->steer[0] = '\0';
|
||||
sp_buf_clear(&v->thinkbuf);
|
||||
sp_buf_clear(&v->live);
|
||||
break;
|
||||
@@ -2556,8 +2608,14 @@ drain_events(struct ui_state *s, struct turn_view *v)
|
||||
add_entry(s, "observation", ev.b);
|
||||
break;
|
||||
case SP_EV_NOTE:
|
||||
snprintf(v->note, sizeof(v->note), "%s",
|
||||
ev.a != NULL ? ev.a : "");
|
||||
/*
|
||||
* A backend note (a retry, a usage-limit wait) is a
|
||||
* status message and belongs below the row that caused
|
||||
* it, not in the input line or the status bar. The
|
||||
* retry itself is the agent's job.
|
||||
*/
|
||||
if (ev.a != NULL && ev.a[0] != '\0')
|
||||
add_entry(s, "splinter", ev.a);
|
||||
break;
|
||||
case SP_EV_ENTRY:
|
||||
add_entry(s, ev.a, ev.b);
|
||||
@@ -2590,57 +2648,35 @@ drain_events(struct ui_state *s, struct turn_view *v)
|
||||
}
|
||||
|
||||
/*
|
||||
* The steering line: elapsed seconds, an engine note (a usage-limit wait), the
|
||||
* steer confirmation, then "steer>" and what is typed. One row, never wrapped.
|
||||
* Called every tick and, without a history repaint, after every key: typing
|
||||
* must not wait for the next 100 ms frame.
|
||||
* The steering line: the steer mark and what is typed, nothing else. It is one
|
||||
* row and never wraps. Called every tick and, without a history repaint, after
|
||||
* every key: typing must not wait for the next 100 ms frame. The running time
|
||||
* is a meter and lives in the status bar; notes from the backend are transcript
|
||||
* rows, so nothing but the input appears here.
|
||||
*/
|
||||
static void
|
||||
turn_draw_input(struct ui_state *s, struct turn_view *v)
|
||||
{
|
||||
struct timespec now;
|
||||
long ms;
|
||||
int w, used;
|
||||
int w, used;
|
||||
char steer[32];
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
ms = (long)((now.tv_sec - v->t0.tv_sec) * 1000 +
|
||||
(now.tv_nsec - v->t0.tv_nsec) / 1000000);
|
||||
s->last_ms = ms;
|
||||
(void)s;
|
||||
werase(ui_input);
|
||||
w = getmaxx(ui_input) - 1;
|
||||
snprintf(steer, sizeof(steer), "%s",
|
||||
sp_td("ui.steer_mark", "steer> "));
|
||||
/* a catalogue entry without the trailing space must not glue the
|
||||
* typed text to the mark: steer>x reads like a syntax error */
|
||||
{
|
||||
/*
|
||||
* No status bar: the input line leads with how long the turn
|
||||
* has been running and the steer prompt. The notes are the
|
||||
* exception - a usage-limit wait and a queued steer say so
|
||||
* here, because that is where the operator is looking.
|
||||
*/
|
||||
char note[900], mark[1000], steer[32], extra[200];
|
||||
size_t sl = strlen(steer);
|
||||
|
||||
snprintf(steer, sizeof(steer), "%s",
|
||||
sp_td("ui.steer_mark", "steer> "));
|
||||
/* a catalogue entry without the trailing space must not glue the
|
||||
* typed text to the mark: steer>x reads like a syntax error */
|
||||
{
|
||||
size_t sl = strlen(steer);
|
||||
|
||||
if (sl > 0 && steer[sl - 1] != ' ' &&
|
||||
sl + 1 < sizeof(steer)) {
|
||||
steer[sl] = ' ';
|
||||
steer[sl + 1] = '\0';
|
||||
}
|
||||
if (sl > 0 && steer[sl - 1] != ' ' && sl + 1 < sizeof(steer)) {
|
||||
steer[sl] = ' ';
|
||||
steer[sl + 1] = '\0';
|
||||
}
|
||||
extra[0] = '\0';
|
||||
if (v->steer[0] != '\0')
|
||||
snprintf(extra, sizeof(extra), " %s", v->steer);
|
||||
snprintf(note, sizeof(note), "%lds%s%.500s%s", ms / 1000,
|
||||
v->note[0] != '\0' ? " " : "", v->note, extra);
|
||||
cut_line(note, w > (int)strlen(steer) + 2 ?
|
||||
(size_t)w - strlen(steer) - 2 : 0);
|
||||
snprintf(mark, sizeof(mark), "%s %s", note, steer);
|
||||
mvwaddnstr(ui_input, 0, 0, mark, w);
|
||||
used = (int)strlen(mark);
|
||||
}
|
||||
mvwaddnstr(ui_input, 0, 0, steer, w);
|
||||
used = (int)strlen(steer);
|
||||
if (used < w)
|
||||
ui_add_text(ui_input, v->line, w - used - 1);
|
||||
wclrtoeol(ui_input);
|
||||
@@ -2675,6 +2711,13 @@ turn_paint(struct ui_state *s, struct turn_view *v)
|
||||
swarm_poll(sw);
|
||||
|
||||
update_meter(s, s->be->host(s->be->ctx));
|
||||
{
|
||||
struct timespec now;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
s->turn_ms = (long)((now.tv_sec - v->t0.tv_sec) * 1000 +
|
||||
(now.tv_nsec - v->t0.tv_nsec) / 1000000);
|
||||
}
|
||||
draw_statusbar(s);
|
||||
turn_draw_input(s, v);
|
||||
}
|
||||
@@ -2766,16 +2809,9 @@ turn_key(struct ui_state *s, struct turn_view *v, int c)
|
||||
char line[600];
|
||||
|
||||
if (s->be->steer(s->be->ctx, v->line) == 0) {
|
||||
/*
|
||||
* Say it twice: in the transcript, where the
|
||||
* steer is a message to the model, and in the
|
||||
* input line, which stays visible while the
|
||||
* answer streams.
|
||||
*/
|
||||
v->steers++;
|
||||
snprintf(v->steer, sizeof(v->steer),
|
||||
"%s (%u)", sp_t("ui.steering_queued"),
|
||||
v->steers);
|
||||
/* the transcript is where a steer is a
|
||||
* message to the model, and where it stays
|
||||
* readable while the answer streams */
|
||||
snprintf(line, sizeof(line),
|
||||
sp_t("ui.steer_queued_line"), v->line);
|
||||
cut_line(line, 240);
|
||||
@@ -2821,6 +2857,8 @@ ui_turn(struct ui_state *s, const char *text)
|
||||
return -1;
|
||||
}
|
||||
wtimeout(ui_input, 100);
|
||||
s->turn_running = 1;
|
||||
s->turn_ms = 0;
|
||||
for (;;) {
|
||||
int c;
|
||||
|
||||
@@ -2848,14 +2886,13 @@ ui_turn(struct ui_state *s, const char *text)
|
||||
*/
|
||||
if (cancelled) {
|
||||
wtimeout(ui_input, 100);
|
||||
/* the operator sees why the screen stays, in the transcript */
|
||||
add_entry(s, "splinter", sp_t("ui.cancelling"));
|
||||
while (s->be->busy(s->be->ctx)) {
|
||||
int c2;
|
||||
|
||||
drain_events(s, &v);
|
||||
werase(ui_input);
|
||||
mvwaddstr(ui_input, 0, 0, sp_t("ui.cancelling"));
|
||||
wclrtoeol(ui_input);
|
||||
wrefresh(ui_input);
|
||||
turn_draw_input(s, &v);
|
||||
c2 = wgetch(ui_input);
|
||||
if (c2 == 3 || c2 == 4) {
|
||||
endwin();
|
||||
@@ -2868,6 +2905,7 @@ ui_turn(struct ui_state *s, const char *text)
|
||||
drain_events(s, &v);
|
||||
(void)s->be->wait(s->be->ctx);
|
||||
wtimeout(ui_input, -1);
|
||||
s->turn_running = 0;
|
||||
sync_state(s);
|
||||
/*
|
||||
* The answers were added when their turn events arrived. What is left
|
||||
|
||||
+10
-3
@@ -485,9 +485,13 @@ send_sessions(struct client *c, struct proto *p)
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* what a /model picker may offer; no network, the configuration is the list */
|
||||
/*
|
||||
* 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;
|
||||
* the picker asks for the configured list first and this after it is visible.
|
||||
*/
|
||||
static int
|
||||
send_models(struct client *c, struct proto *p)
|
||||
send_models(struct client *c, struct proto *p, int probe)
|
||||
{
|
||||
struct sp_model_choice v[SP_MODEL_CHOICES];
|
||||
size_t n, i;
|
||||
@@ -495,6 +499,9 @@ send_models(struct client *c, struct proto *p)
|
||||
int rc;
|
||||
|
||||
n = sp_engine_models(p->eng, v, SP_MODEL_CHOICES);
|
||||
if (probe)
|
||||
n += sp_engine_served_models(p->eng, v + n,
|
||||
SP_MODEL_CHOICES - n);
|
||||
if (sp_buf_init(&b) != 0)
|
||||
return -1;
|
||||
ev_start(&b, "models");
|
||||
@@ -705,7 +712,7 @@ handle_line(struct client *c, struct proto *p, const char *line)
|
||||
} else if (strcmp(cmd, "sessions") == 0) {
|
||||
(void)send_sessions(c, p);
|
||||
} else if (strcmp(cmd, "models") == 0) {
|
||||
(void)send_models(c, p);
|
||||
(void)send_models(c, p, mcp_arg_bool(v, "probe", 0));
|
||||
} else if (strcmp(cmd, "open") == 0) {
|
||||
(void)proto_open(c, p, mcp_arg_string(v, "session"),
|
||||
mcp_arg_bool(v, "new", 0));
|
||||
|
||||
@@ -63,6 +63,13 @@ struct ui_backend {
|
||||
int (*models)(void *ctx,
|
||||
void (*cb)(void *cbctx, const char *sel, const char *label,
|
||||
int current), void *cbctx);
|
||||
/*
|
||||
* The same for the models the serving endpoint reports it has. Network;
|
||||
* NULL when the backend cannot ask (then the picker stays config-only).
|
||||
*/
|
||||
int (*served)(void *ctx,
|
||||
void (*cb)(void *cbctx, const char *sel, const char *label,
|
||||
int current), void *cbctx);
|
||||
/* the sessions in this backend's directory; cb per entry */
|
||||
int (*sessions)(void *ctx,
|
||||
void (*cb)(void *cbctx, const char *id, const char *title,
|
||||
|
||||
@@ -157,6 +157,19 @@ loc_models(void *ctx, void (*cb)(void *, const char *, const char *, int),
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
static int
|
||||
loc_served(void *ctx, void (*cb)(void *, const char *, const char *, int),
|
||||
void *cbctx)
|
||||
{
|
||||
struct sp_model_choice v[SP_MODEL_CHOICES];
|
||||
size_t n, i;
|
||||
|
||||
n = sp_engine_served_models(ENG(ctx), v, SP_MODEL_CHOICES);
|
||||
for (i = 0; i < n; i++)
|
||||
cb(cbctx, v[i].sel, v[i].label, v[i].current);
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
static int
|
||||
loc_history(void *ctx, void (*cb)(void *, const char *, const char *),
|
||||
void *cbctx)
|
||||
@@ -257,6 +270,7 @@ static const struct ui_backend local = {
|
||||
.agent_switch = loc_agent_switch,
|
||||
.command_names = loc_command_names,
|
||||
.models = loc_models,
|
||||
.served = loc_served,
|
||||
.sessions = loc_sessions,
|
||||
.open_session = loc_open_session,
|
||||
.history = loc_history,
|
||||
|
||||
@@ -712,20 +712,21 @@ rem_command_names(void *ctx, char (*names)[64], int max)
|
||||
|
||||
/*
|
||||
* The picker's list from the server: `models` answers one event with every
|
||||
* selector. Other events that arrive while we wait (state, busy) still go
|
||||
* through dispatch, so nothing is lost.
|
||||
* 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(void *ctx, void (*cb)(void *, const char *, const char *, int),
|
||||
void *cbctx)
|
||||
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", NULL, NULL) != 0)
|
||||
if (r->fd < 0 || send_cmd(r, "models", probe ? "probe" : NULL,
|
||||
probe ? "true" : NULL) != 0)
|
||||
return 0;
|
||||
deadline = now_ms() + 5000;
|
||||
deadline = now_ms() + (probe ? 15000 : 5000);
|
||||
while (now_ms() < deadline) {
|
||||
mcp_json *v;
|
||||
const char *ev;
|
||||
@@ -760,6 +761,20 @@ rem_models(void *ctx, void (*cb)(void *, const char *, const char *, int),
|
||||
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)
|
||||
{
|
||||
@@ -1104,6 +1119,7 @@ sp_ui_backend_remote(const char *socket_path, struct sp_buf *err)
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user