From 2e7a88c4625943a12a938f4d5cb57cbe806e1fdf Mon Sep 17 00:00:00 2001 From: lotherk Date: Thu, 12 Nov 2026 03:22:49 +0000 Subject: [PATCH] usable: opencode-go session header, embedded catalogs, locale fixes, calmer UI, --doctor - openai transport sends x-opencode-session (opencode-go refused every request without it) and a splinter user agent - the message catalogues are compiled in: an installed binary no longer shows raw keys (ui.ready ...); LANG=de_DE now selects German (lang was emptied by an overlapping snprintf), pt_BR is matched - sidebar rewritten (agent, provider/model, usage, goal/plan, last turn); idle widgets say nothing; status line shows only what matters - tool calls shown readable instead of JSON with \u escapes - providers without their key variable are skipped by pools and refused by -m with a clear message; --doctor checks every provider Co-Authored-By: Claude Sonnet 5.5 --- .gitignore | 1 + Makefile | 7 +- include/splinter.h | 6 + plugins/focus/focus.c | 11 +- plugins/planner/planner.c | 14 +- plugins/scheduler/scheduler.c | 4 +- plugins/status/status.c | 12 +- plugins/tasks/tasks.c | 4 +- plugins/tmux/tmux.c | 2 +- share/locale/de.msg | 37 +++- share/locale/en.msg | 37 +++- share/locale/pt_BR.msg | 37 +++- src/delegate.c | 2 + src/i18n.c | 57 ++++- src/i18n.h | 7 + src/main.c | 138 +++++++++++- src/providers.c | 14 ++ src/providers.h | 3 + src/swarm.c | 6 +- src/transport_openai.c | 41 +++- src/ui.c | 405 +++++++++++++++++++++++++--------- src/usage.c | 11 +- tests/test_agents_ui.sh | 10 + tests/test_i18n.c | 2 +- tests/test_jobs.c | 3 +- tests/test_usage.c | 15 ++ tools/check_i18n.py | 4 +- tools/embed_catalogs.sh | 22 ++ 28 files changed, 724 insertions(+), 188 deletions(-) create mode 100755 tools/embed_catalogs.sh diff --git a/.gitignore b/.gitignore index 88a9ba3..ca994cf 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ tests/test_* # python bytecode from the test helpers __pycache__/ +src/catalogs.c diff --git a/Makefile b/Makefile index 265af30..60fd8cd 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ CURSES_A = $(CURSES_DIR)/libcurses/libcurses.a \ LIB = libsplinter.so LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \ src/json.o src/sse.o src/openai.o src/transport_openai.o src/builtin.o \ - src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/folder.o src/interview.o src/ack.o src/mcpserver.o src/skills.o src/memory.o src/prompt.o src/config.o src/plugin.o src/curl.o src/swarm.o src/provstate.o src/md.o src/tgmd.o src/b64.o src/a2aserver.o src/providers.o src/usage.o src/confblock.o src/agents.o + src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/folder.o src/interview.o src/ack.o src/mcpserver.o src/skills.o src/memory.o src/prompt.o src/config.o src/plugin.o src/curl.o src/swarm.o src/provstate.o src/md.o src/tgmd.o src/b64.o src/a2aserver.o src/providers.o src/usage.o src/confblock.o src/agents.o src/catalogs.o BIN = splinter BINOBJ = src/main.o src/ui.o PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \ @@ -53,6 +53,9 @@ $(CURSES_A): curses: $(CURSES_A) +src/catalogs.c: share/locale/de.msg share/locale/en.msg share/locale/pt_BR.msg tools/embed_catalogs.sh + sh tools/embed_catalogs.sh share/locale/*.msg > $@ + $(LIB): $(LIBOBJ) $(CURSES_A) $(CC) -shared -o $@ $(LIBOBJ) $(CURSES_A) $(LIBS) @@ -272,7 +275,7 @@ install: all @echo "installed $(DESTDIR)$(PREFIX)/bin/splinter" clean: - rm -f $(BIN) $(LIB) $(LIBOBJ) $(BINOBJ) $(PLUGIN) $(TESTS) \ + rm -f src/catalogs.c $(BIN) $(LIB) $(LIBOBJ) $(BINOBJ) $(PLUGIN) $(TESTS) \ tests/plugin_lifecycle.so .PHONY: all check curses install clean diff --git a/include/splinter.h b/include/splinter.h index f9a5194..5041b74 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -366,6 +366,12 @@ int sp_delegate_choose(const struct sp_config *, const char *task, /* transport_openai.c -- OpenAI-compatible chat completions, via curl(1) */ /* The turn's cancellation flag; set by the UI before a turn, cleared after. */ void sp_openai_set_cancel(const volatile int *flag); +/* + * The session whose id goes to providers that want one (opencode-go routes + * and caches by "x-opencode-session"). A pointer, so /new is seen. + */ +void sp_openai_set_session(const char *id); +size_t sp_openai_session_header(const char *url, char *buf, size_t cap); struct sp_transport sp_openai_transport(const char *url, const char *model, const char *api_key, const struct sp_registry *, long timeout_s); diff --git a/plugins/focus/focus.c b/plugins/focus/focus.c index 78023c0..b60cd59 100644 --- a/plugins/focus/focus.c +++ b/plugins/focus/focus.c @@ -136,8 +136,8 @@ focus_command(struct sp_host *h, const char *args, struct sp_buf *out, { char line[192]; - snprintf(line, sizeof(line), "%.60s%s", g, - strlen(g) > 60 ? "..." : ""); + snprintf(line, sizeof(line), "%.160s%s", g, + strlen(g) > 160 ? "..." : ""); (void)sp_host_widget_text(h, "focus", line); } return sp_buf_printf(out, sp_td("focus.set", "goal set: %s"), g); @@ -169,11 +169,10 @@ focus_tick(struct sp_host *h, const char *name, void *ctx) (void)ctx; pthread_mutex_lock(&focus_lock); if (goal[0] == '\0') - snprintf(line, sizeof(line), "%s", sp_td("focus.widget_none", - "no goal (/goal )")); + line[0] = '\0'; /* no goal: the sidebar says nothing */ else - snprintf(line, sizeof(line), "%.44s%s | %u", - goal, strlen(goal) > 44 ? "..." : "", reminders); + snprintf(line, sizeof(line), "%.160s%s", + goal, strlen(goal) > 160 ? "..." : ""); pthread_mutex_unlock(&focus_lock); return sp_host_widget_text(h, name, line); } diff --git a/plugins/planner/planner.c b/plugins/planner/planner.c index 6d63590..e0360d6 100644 --- a/plugins/planner/planner.c +++ b/plugins/planner/planner.c @@ -31,6 +31,7 @@ #include "splinter.h" #include "mcp.h" #include "splinter_plugin.h" +#include "i18n.h" #define MAX_TASKS 128 #define DESC_LEN 1024 @@ -246,12 +247,15 @@ widget(void) else if (strcmp(tasks[i].status, "done") == 0) done++; } - if (current_id > 0) - snprintf(b, sizeof(b), "plan: #%d active | todo %d doing %d " - "done %d", current_id, todo, doing, done); + if (ntasks == 0) + b[0] = '\0'; /* no plan: nothing on the sidebar */ + else if (current_id > 0) + snprintf(b, sizeof(b), sp_td("plan.widget_active", + "#%d active - todo %d, doing %d, done %d"), current_id, + todo, doing, done); else - snprintf(b, sizeof(b), "plan: %zu tasks | todo %d doing %d " - "done %d", ntasks, todo, doing, done); + snprintf(b, sizeof(b), sp_td("plan.widget", + "todo %d, doing %d, done %d"), todo, doing, done); sp_host_widget_text(host_ref, "plan", b); } diff --git a/plugins/scheduler/scheduler.c b/plugins/scheduler/scheduler.c index c99d6fe..0e54c53 100644 --- a/plugins/scheduler/scheduler.c +++ b/plugins/scheduler/scheduler.c @@ -23,9 +23,9 @@ publish(struct sp_host *h) char buf[320]; if (!h) return; if (last_status[0]) - snprintf(buf, sizeof(buf), "sched: %s", last_status); + snprintf(buf, sizeof(buf), "%s", last_status); else - snprintf(buf, sizeof(buf), "sched: idle"); + buf[0] = '\0'; sp_host_widget_text(h, "scheduler", buf); } diff --git a/plugins/status/status.c b/plugins/status/status.c index 894ed3a..bf4962b 100644 --- a/plugins/status/status.c +++ b/plugins/status/status.c @@ -50,7 +50,7 @@ render_clock(struct sp_host *h, char *buf, size_t len) (void)h; (void)localtime_r(&now, &tm); - strftime(buf, len, "clock: %H:%M:%S", &tm); + strftime(buf, len, "%H:%M", &tm); } static void @@ -63,6 +63,10 @@ render_ctx(struct sp_host *h, char *buf, size_t len) sp_host_meter_get(h, &mm); human(m->ctx_used, used, sizeof(used)); + if (m->ctx_used == 0) { + buf[0] = '\0'; /* nothing sent yet: nothing to show */ + return; + } if (m->ctx_max == 0) { snprintf(buf, len, "ctx %s", used); return; @@ -101,6 +105,10 @@ render_tokens(struct sp_host *h, char *buf, size_t len) char in[24], out[24]; sp_host_meter_get(h, &mm); + if (m->tok_in == 0 && m->tok_out == 0) { + buf[0] = '\0'; + return; + } human(m->tok_in, in, sizeof(in)); human(m->tok_out, out, sizeof(out)); snprintf(buf, len, "%s in / %s out", in, out); @@ -151,7 +159,7 @@ render_tasks(struct sp_host *h, char *buf, size_t len) const struct sp_meter *m = &mm; sp_host_meter_get(h, &mm); - if (m->tasks_max == 0) { + if (m->tasks_max == 0 || m->tasks_running == 0) { buf[0] = '\0'; return; } diff --git a/plugins/tasks/tasks.c b/plugins/tasks/tasks.c index e92f51c..ab5f428 100644 --- a/plugins/tasks/tasks.c +++ b/plugins/tasks/tasks.c @@ -120,7 +120,7 @@ refresh_widget(struct sp_host *h) } pthread_mutex_unlock(&tasks_lock); if (used == 0) - (void)sp_host_widget_text(h, "tasks", "keine"); + (void)sp_host_widget_text(h, "tasks", ""); else (void)sp_host_widget_text(h, "tasks", text); } @@ -537,7 +537,7 @@ tasks_init(struct sp_host *h) (void)sp_host_add_tool(h, &t_result); (void)sp_host_add_tool(h, &t_kill); (void)sp_host_add_widget(h, &widget); - (void)sp_host_widget_text(h, "tasks", "keine"); + (void)sp_host_widget_text(h, "tasks", ""); sp_host_set_task_ops(h, &task_ops); publish_meter(h); return 0; diff --git a/plugins/tmux/tmux.c b/plugins/tmux/tmux.c index ffaad36..e57311e 100644 --- a/plugins/tmux/tmux.c +++ b/plugins/tmux/tmux.c @@ -728,7 +728,7 @@ tick(struct sp_host *h, const char *name, void *ctx) (void)name; (void)ctx; host_ref = h; - snprintf(b, sizeof(b), "tmux: %.100s", target[0] != '\0' ? target : "-"); + snprintf(b, sizeof(b), "%.100s", target); sp_host_widget_text(h, "tmux", b); return 0; } diff --git a/share/locale/de.msg b/share/locale/de.msg index 1cdf9e3..ba9ed74 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -9,16 +9,8 @@ ui.working = laeuft... ui.steering_queued = Steuerung eingereiht sidebar.session = Sitzung -sidebar.transport = Transport -sidebar.theme = Farbschema sidebar.last_turn = letzter Turn -sidebar.output = Ausgabe -sidebar.widgets = Widgets -sidebar.steps = %zu Schritte -sidebar.tool_calls = %zu Werkzeuge sidebar.tokens = %zu Tok (ein %zu) -sidebar.chars = %zu Zeichen (~%zu Tok) -sidebar.no_widgets = keine geladen status.title = splinter %s @@ -40,7 +32,6 @@ err.plugin_failed = Plugin fehlgeschlagen role.tool = werkzeug err.thread = Turn-Thread laesst sich nicht starten err.turn_failed = Turn fehlgeschlagen -sidebar.script = Skript focus.reminder = Pruefe vor der Antwort, ob der letzte Schritt naeher am Ziel war; wenn nicht, sage das und nenne den naechsten Schritt. Ziel: %s focus.set = Ziel gesetzt: %s focus.none = kein Ziel gesetzt (/goal ) @@ -50,12 +41,9 @@ cps.plan = Plan bereit: schnitt %zu/%zu, %zu -> %zu B cli.no_output = cli: '%s' lieferte keine Ausgabe cli.timeout = cli: '%s' lief nach %s s in das Zeitlimit cli.failed = cli: '%s' scheiterte (exit %d): %.400s -focus.widget_none = kein Ziel (/goal ) ui.prompt_mark = > ui.steer_mark = steuern> ui.swarm_locked = [Schwarm gesperrt] -sidebar.ms = %ld ms -ui.empty = - ui.approve = %s ausfuehren (%s) %.60s? y/N auth.mutate = aendert Dateien auth.destructive = zerstoerend @@ -175,3 +163,28 @@ connect.later = Spaeter reply.empty = (leer) + +# Agent-Profile, Kontingent, Seitenleiste (mit den Profilen dazugekommen) +sidebar.model = Modell +sidebar.usage = Kontingent +sidebar.turn_line = %.1f s, %zu Schritte, %zu Tools +widget.focus = Ziel +widget.plan = Plan +widget.tasks = Aufgaben +widget.tmux = tmux +ui.running = laeuft %lds%s |%s Enter = steuern, Strg-C = abbrechen +ui.limit_wait = Nutzungslimit: weiter um %s (Strg-C stoppt, /resume macht weiter) +agent.switched = Agent: %s - %s %s +agent.readonly = [nur lesen] +agent.none = kein Agent '%s' +agent.help = Tab wechselt, /agent NAME waehlt +agent.desc.build = voller Zugriff: lesen, aendern, bauen, testen +agent.desc.plan = nur lesen: analysieren, abwaegen, planen; aendert nichts +usage.benched = am Limit, zurueck in %s +usage.calls = %lu Aufrufe +usage.room = , Platz in %s +usage.of_calls = (%lu/%lu Aufrufe) +usage.of_tokens = (%lu/%lu Tokens) +plan.widget = %d offen, %d aktiv, %d erledigt +plan.widget_active = #%d aktiv - %d offen, %d aktiv, %d erledigt +ui.thinking = denkt diff --git a/share/locale/en.msg b/share/locale/en.msg index 3398a24..2586db2 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -8,16 +8,8 @@ ui.working = working... ui.steering_queued = steering queued sidebar.session = session -sidebar.transport = transport -sidebar.theme = theme sidebar.last_turn = last turn -sidebar.output = output -sidebar.widgets = widgets -sidebar.steps = %zu steps -sidebar.tool_calls = %zu tool calls sidebar.tokens = %zu tok (in %zu) -sidebar.chars = %zu chars (~%zu tok) -sidebar.no_widgets = none loaded status.title = splinter %s @@ -39,7 +31,6 @@ err.plugin_failed = plugin failed role.tool = tool err.thread = cannot start the turn thread err.turn_failed = turn failed -sidebar.script = script focus.reminder = Before answering, check whether the last step moved closer to the goal. If not, say so and name the next step. Goal: %s focus.set = goal set: %s focus.none = no goal set (/goal ) @@ -49,12 +40,9 @@ cps.plan = plan ready: cut %zu/%zu, %zu -> %zu B cli.no_output = cli: '%s' produced no output cli.timeout = cli: '%s' timed out after %s s cli.failed = cli: '%s' failed (exit %d): %.400s -focus.widget_none = no goal (/goal ) ui.prompt_mark = > ui.steer_mark = steer> ui.swarm_locked = [swarm locked] -sidebar.ms = %ld ms -ui.empty = - ui.approve = run %s (%s) %.60s? y/N auth.mutate = changes files auth.destructive = destructive @@ -174,3 +162,28 @@ connect.later = Later reply.empty = (empty) + +# agent profiles, usage, sidebar (added with the profiles) +sidebar.model = model +sidebar.usage = usage +sidebar.turn_line = %.1f s, %zu steps, %zu tools +widget.focus = goal +widget.plan = plan +widget.tasks = tasks +widget.tmux = tmux +ui.running = running %lds%s |%s Enter = steer, Ctrl-C = cancel +ui.limit_wait = usage limit: continuing at %s (Ctrl-C stops, /resume goes on) +agent.switched = agent: %s - %s %s +agent.readonly = [read-only] +agent.none = no agent '%s' +agent.help = Tab switches, /agent NAME picks +agent.desc.build = full access: reads, changes, builds, tests +agent.desc.plan = read-only: analyse, weigh, plan; changes nothing +usage.benched = at its limit, back in %s +usage.calls = %lu calls +usage.room = , room in %s +usage.of_calls = (%lu/%lu calls) +usage.of_tokens = (%lu/%lu tokens) +plan.widget = todo %d, doing %d, done %d +plan.widget_active = #%d active - todo %d, doing %d, done %d +ui.thinking = thinking diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index 4b8cb01..a9b0637 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -13,18 +13,8 @@ ui.swarm_locked = [enxame bloqueado] ui.approve = executar %s (%s) %.60s? y/N sidebar.session = sessao -sidebar.transport = transporte -sidebar.theme = tema sidebar.last_turn = ultimo turno -sidebar.output = saida -sidebar.widgets = widgets -sidebar.no_widgets = nenhum carregado -sidebar.steps = %zu passos -sidebar.tool_calls = %zu ferramentas sidebar.tokens = %zu tok (entrada %zu) -sidebar.chars = %zu caracteres (~%zu tok) -sidebar.ms = %ld ms -sidebar.script = script status.title = splinter %s @@ -46,7 +36,6 @@ err.session = sessao err.plugin_failed = plugin falhou err.thread = nao foi possivel iniciar a thread do turno err.turn_failed = turno falhou -ui.empty = - auth.mutate = altera arquivos auth.destructive = destrutivo @@ -55,7 +44,6 @@ focus.reminder = Antes de responder, verifique se o ultimo passo aproximou do ob focus.set = objetivo definido: %s focus.none = nenhum objetivo definido (/goal ) focus.status = objetivo: %s (%u lembretes, a cada %u turnos) -focus.widget_none = nenhum objetivo (/goal ) cps.idle = ocioso (aplicado %lu, preparado %lu) cps.plan = plano pronto: corte %zu/%zu, %zu -> %zu B @@ -178,3 +166,28 @@ connect.later = Depois reply.empty = (vazio) + +# perfis de agente, uso, barra lateral (junto com os perfis) +sidebar.model = modelo +sidebar.usage = uso +sidebar.turn_line = %.1f s, %zu passos, %zu ferramentas +widget.focus = meta +widget.plan = plano +widget.tasks = tarefas +widget.tmux = tmux +ui.running = executando %lds%s |%s Enter = guiar, Ctrl-C = cancelar +ui.limit_wait = limite de uso: continua as %s (Ctrl-C para, /resume continua) +agent.switched = agente: %s - %s %s +agent.readonly = [somente leitura] +agent.none = nenhum agente '%s' +agent.help = Tab alterna, /agent NOME escolhe +agent.desc.build = acesso total: ler, alterar, compilar, testar +agent.desc.plan = somente leitura: analisar, ponderar, planejar; nao altera nada +usage.benched = no limite, volta em %s +usage.calls = %lu chamadas +usage.room = , vaga em %s +usage.of_calls = (%lu/%lu chamadas) +usage.of_tokens = (%lu/%lu tokens) +plan.widget = %d a fazer, %d em curso, %d feitas +plan.widget_active = #%d ativa - %d a fazer, %d em curso, %d feitas +ui.thinking = pensando diff --git a/src/delegate.c b/src/delegate.c index 1cb68bc..0156849 100644 --- a/src/delegate.c +++ b/src/delegate.c @@ -413,6 +413,8 @@ choose(const struct sp_config *cfg, const char *task, const char *wanted, if (sp_cooldown_until(d->id) != 0) continue; /* benched: at its limit until the reset */ + if (!sp_provider_has_key(d)) + continue; /* its key variable is not set here */ sp_usage_get(d->id, d->req_limit, d->req_window, d->tok_limit, d->tok_window, &u); if (u.frac >= 1.0) diff --git a/src/i18n.c b/src/i18n.c index f98da8c..b6e195d 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -89,7 +89,12 @@ detect_language(const char *override_lang) i < sizeof(lang) - 1) { char c = cand[i]; - lang[i] = (c == '-') ? '_' : (char)tolower((unsigned char)c); + /* de_DE -> de_DE as pt_BR.msg is written: language lower, region upper */ + if (c == '-') + c = '_'; + lang[i] = strchr(lang, '_') != NULL ? + (char)toupper((unsigned char)c) : + (char)tolower((unsigned char)c); i++; } lang[i] = '\0'; @@ -99,15 +104,11 @@ detect_language(const char *override_lang) } static size_t -parse_file(const char *path, struct entry *cat, size_t max) +parse_stream(FILE *f, struct entry *cat, size_t max) { - FILE *f; char line[1400]; size_t n = 0; - f = fopen(path, "r"); - if (f == NULL) - return 0; while (n < max && fgets(line, sizeof(line), f) != NULL) { char *eq, *key, *val; @@ -127,10 +128,46 @@ parse_file(const char *path, struct entry *cat, size_t max) snprintf(cat[n].text, sizeof(cat[n].text), "%s", val); n++; } + return n; +} + +static size_t +parse_file(const char *path, struct entry *cat, size_t max) +{ + FILE *f = fopen(path, "r"); + size_t n; + + if (f == NULL) + return 0; + n = parse_stream(f, cat, max); fclose(f); return n; } +/* the catalogue compiled into the program, for languages without a file */ +static size_t +parse_embedded(const char *lang, struct entry *cat, size_t max) +{ + const struct sp_embedded *e; + + /* "pt" is served by pt_BR when there is no plain pt */ + size_t ll = strlen(lang); + + for (e = sp_embedded_catalogs; e->lang != NULL; e++) + if (strcmp(e->lang, lang) == 0 || + (strncmp(e->lang, lang, ll) == 0 && e->lang[ll] == '_')) { + FILE *f = fmemopen((void *)e->text, strlen(e->text), "r"); + size_t n; + + if (f == NULL) + return 0; + n = parse_stream(f, cat, max); + fclose(f); + return n; + } + return 0; +} + int sp_i18n_load(struct sp_buf *err, const char *want) { @@ -148,6 +185,8 @@ sp_i18n_load(struct sp_buf *err, const char *want) if (main_n > 0) break; } + if (main_n == 0) + main_n = parse_embedded(want, main_cat, MAX_KEYS); /* English is the fallback for every key, always loaded */ for (i = 0; i < ndirs; i++) { if (strcmp(want, "en") == 0 && main_n > 0) @@ -158,13 +197,17 @@ sp_i18n_load(struct sp_buf *err, const char *want) break; } + if (fallback_n == 0) + fallback_n = parse_embedded("en", fallback, MAX_KEYS); if (main_n == 0 && fallback_n == 0) { if (err != NULL) sp_buf_printf(err, "i18n: no catalogue for '%s' in %zu " "directories", want, tried); return -1; } - snprintf(lang, sizeof(lang), "%s", want); + /* "want" may be the very buffer "lang": copying it onto itself empties it */ + if (want != lang) + snprintf(lang, sizeof(lang), "%s", want); return 0; } diff --git a/src/i18n.h b/src/i18n.h index 725be65..73b2394 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -47,4 +47,11 @@ int sp_i18n_fallback_count(void); const char *sp_i18n_fallback_key(size_t index); void sp_i18n_add_dir(const char *dir); +/* the catalogues compiled in (src/catalogs.c): used when no file is found */ +struct sp_embedded { + const char *lang; + const char *text; +}; +extern const struct sp_embedded sp_embedded_catalogs[]; + #endif /* SPLINTER_I18N_H */ diff --git a/src/main.c b/src/main.c index f8f7b12..1eaaeb3 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,8 @@ usage(FILE *f) " file in ~/.splinter/agents, .agents/); Tab\n" " switches in the chat\n" " --agent-list list the agent profiles and exit\n" + " --doctor check every provider: key, reachability, one\n" + " tiny turn (--live: also the cli ones)\n" " --model-list list what -m accepts (asks each endpoint\n" " which models it serves; --no-probe skips)\n" " --api-key-env VAR read the API key from VAR (default\n" @@ -222,7 +225,7 @@ struct opts { int daemon, no_stdin, continue_here, interview; int no_stream; const char *select; - int model_list, no_probe, agent_list; + int model_list, no_probe, agent_list, doctor, live; const char *agent; int acklist, ack_id, ack_yes, mcp_port; const char *a2a_ip; @@ -290,6 +293,10 @@ parse_args(int argc, char **argv, struct opts *o, struct sp_agent_config *cfg) VALUE(o->select); else if (!strcmp(a, "--agent")) VALUE(o->agent); + else if (!strcmp(a, "--doctor")) + o->doctor = 1; + else if (!strcmp(a, "--live")) + o->live = 1; else if (!strcmp(a, "--agent-list")) o->agent_list = 1; else if (!strcmp(a, "--model-list")) @@ -766,14 +773,127 @@ load_plugins(const struct opts *o, struct sp_registry *reg, struct sp_agent *ag, if (sp_plugin_load(host, o->plugins[k], err) != 0) fprintf(stderr, "splinter: %s\n", err->len > 0 ? err->p : sp_t("err.plugin_failed")); - else - fprintf(stderr, "splinter: plugin %s loaded\n", - o->plugins[k]); } *hostp = host; return 0; } +/* is the first word of 'cmd' a program on PATH? */ +static int +program_found(const char *cmd) +{ + char word[128], *path, *dir, *save = NULL; + size_t n = strcspn(cmd, " \t"); + + if (n == 0 || n >= sizeof(word)) + return 0; + memcpy(word, cmd, n); + word[n] = '\0'; + if (strchr(word, '/') != NULL) + return access(word, X_OK) == 0; + path = getenv("PATH") != NULL ? strdup(getenv("PATH")) : NULL; + if (path == NULL) + return 0; + for (dir = strtok_r(path, ":", &save); dir != NULL; + dir = strtok_r(NULL, ":", &save)) { + char full[600]; + + snprintf(full, sizeof(full), "%s/%s", dir, word); + if (access(full, X_OK) == 0) { + free(path); + return 1; + } + } + free(path); + return 0; +} + +/* --doctor: does each provider work, said in one line each */ +static int +run_doctor(const struct sp_config *conf, const struct opts *o) +{ + size_t i; + int bad = 0; + + for (i = 0; i < conf->ndelegates; i++) { + const struct sp_delegate *d = &conf->delegate[i]; + struct sp_usage u; + char use[128], what[300]; + int ok = 0; + + sp_usage_for(conf, d->id, &u); + sp_usage_brief(&u, use, sizeof(use)); + what[0] = '\0'; + if (strcmp(d->kind, "cli") == 0) { + if (!program_found(d->cmd)) { + snprintf(what, sizeof(what), "%s: program not found", + d->cmd); + } else if (d->nenv == 0 && d->key_env[0] != '\0' && + getenv(d->key_env) == NULL) { + snprintf(what, sizeof(what), "%s not set", d->key_env); + } else if (!o->live) { + snprintf(what, sizeof(what), "program found (--live " + "asks it a question)"); + ok = 1; + } else { + const char *home = getenv("HOME"); + char cmd[700]; + FILE *p; + char line[200]; + size_t k; + + (void)home; + (void)k; + sp_config_apply_env((const char (*)[SP_CONFIG_ENV_LEN])d->env, + d->nenv); + snprintf(cmd, sizeof(cmd), + "%s 'Answer with the single word: pong' 2>&1 cmd); + p = popen(cmd, "r"); + if (p != NULL && fgets(line, sizeof(line), p) != NULL) { + line[strcspn(line, "\n")] = '\0'; + snprintf(what, sizeof(what), "answered: %.60s", line); + ok = strncasecmp(line, "pong", 4) == 0; + } else + snprintf(what, sizeof(what), "no answer"); + if (p != NULL) + pclose(p); + } + } else if (!sp_provider_has_key(d)) { + snprintf(what, sizeof(what), "%s is not set", d->key_env); + } else { + struct sp_provider pv; + struct sp_buf ans, perr; + time_t t0 = time(NULL); + + memset(&pv, 0, sizeof(pv)); + snprintf(pv.id, sizeof(pv.id), "%s", d->id); + snprintf(pv.endpoint, sizeof(pv.endpoint), "%s", d->endpoint); + snprintf(pv.model, sizeof(pv.model), "%s", d->model); + snprintf(pv.key_env, sizeof(pv.key_env), "%s", d->key_env); + if (sp_buf_init(&ans) == 0 && sp_buf_init(&perr) == 0) { + ok = sp_provider_test(&pv, &ans, &perr) == 0; + if (ok) + snprintf(what, sizeof(what), "%lds, %.40s", + (long)(time(NULL) - t0), d->model); + else + snprintf(what, sizeof(what), "%.150s", + perr.len > 0 && strstr(perr.p, "transfer failed") == NULL ? + perr.p : "no answer within 30 s (down, or busy)"); + } + sp_buf_free(&ans); + sp_buf_free(&perr); + } + printf("%-10.10s %-5s %s [%s]\n", d->id, ok ? "ok" : "FAIL", what, use); + fflush(stdout); + if (!ok) + bad++; + } + if (conf->ndelegates == 0) + printf("no providers in the config\n"); + return bad == 0 ? 0 : 1; +} + /* -m: the provider (or the config's default) becomes the run's transport */ static int apply_selection(struct opts *o, struct sp_config *conf) @@ -794,6 +914,11 @@ apply_selection(struct opts *o, struct sp_config *conf) return 2; } d = ch.d; + if (!sp_provider_has_key(d)) { + fprintf(stderr, "splinter: %s needs the variable %s, and it is " + "not set\n", d->id, d->key_env); + return 2; + } sp_usage_set_active(d->id); if (strcmp(d->kind, "cli") == 0) { const struct sp_pool *pool = sp_pool_of(conf, d->id); @@ -1153,8 +1278,12 @@ main(int argc, char **argv) "environment\n", auto_added); } hide_secrets(&o, &conf); + /* the catalogue is chosen now: a config file may set the locale */ + (void)sp_i18n_init(NULL); if (o.model_list) return run_model_list(&conf, &o); + if (o.doctor) + return run_doctor(&conf, &o); if (o.agent_list) { struct sp_agents al; char cwd[700]; @@ -1191,6 +1320,7 @@ main(int argc, char **argv) if ((rc = open_session(&o, &sess, &err)) != 0) return rc; ag.sess = &sess; + sp_openai_set_session(sess.id); /* * Answering is not running an agent: any process may drop the answer, diff --git a/src/providers.c b/src/providers.c index 12a0e6a..6247f82 100644 --- a/src/providers.c +++ b/src/providers.c @@ -701,6 +701,18 @@ sp_provider_add(struct sp_config *cfg, const char *dir, const char *id, /* ---------------------------------------------------------------- pools */ +int +sp_provider_has_key(const struct sp_delegate *d) +{ + const char *v; + + if (d->key_env[0] == '\0' || strcmp(d->kind, "cli") == 0 || + d->key[0] != '\0') + return 1; + v = getenv(d->key_env); + return v != NULL && *v != '\0'; +} + const struct sp_pool * sp_pool_find(const struct sp_config *cfg, const char *id) { @@ -784,6 +796,8 @@ sp_pool_pick(const struct sp_config *cfg, const struct sp_pool *p, continue; if (!has_all_tags(d->tags, need_tags)) continue; + if (!sp_provider_has_key(d)) + continue; /* no key in this environment */ sp_usage_for(cfg, d->id, &u); if (u.benched_until > 0 || u.frac >= 1.0) { back = u.benched_until > 0 ? u.benched_until : u.reset_at; diff --git a/src/providers.h b/src/providers.h index d97d4dc..0a1d58f 100644 --- a/src/providers.h +++ b/src/providers.h @@ -92,6 +92,9 @@ int sp_provider_save_choice(const struct sp_config *, int sp_provider_line(char *buf, size_t len, const char *id, const char *endpoint, const char *model, const char *key_env); +/* 1 when the provider can be called: its key variable (if it names one) is set */ +int sp_provider_has_key(const struct sp_delegate *); + /* * Pools. A pool is a name for providers that stand in for each other. * sp_pool_pick gives the member to use now -- first with room (failover) or diff --git a/src/swarm.c b/src/swarm.c index aec64a9..08e1865 100644 --- a/src/swarm.c +++ b/src/swarm.c @@ -402,7 +402,11 @@ swarm_status(struct swarm *s, char *buf, size_t cap) if (!s || s->role == SWARM_NONE) { if (cap) buf[0] = '\0'; return; } if (s->role == SWARM_MASTER) { for (i=0;inpeers;i++) if (s->peers[i].alive) alive++; - snprintf(buf, cap, "swarm: master 0 (%d slaves)", alive); + /* nobody joined: nothing to say */ + if (alive == 0 && cap > 0) + buf[0] = '\0'; + else + snprintf(buf, cap, "swarm: master 0 (%d slaves)", alive); } else { snprintf(buf, cap, "swarm: slave %d%s", s->id, s->locked?" [locked]":""); } diff --git a/src/transport_openai.c b/src/transport_openai.c index 686543f..f6fcb7e 100644 --- a/src/transport_openai.c +++ b/src/transport_openai.c @@ -45,6 +45,29 @@ struct openai_ctx { * for the model to finish talking to nobody. */ static const volatile int *g_cancel; +static const char *g_session; + +void +sp_openai_set_session(const char *id) +{ + g_session = id; +} + +/* + * The header that carries the session id for this endpoint, or "" when the + * endpoint does not want one. opencode-go refuses a request without it + * ("Request is missing x-opencode-session"): it routes by it and caches the + * prompt for it. + */ +size_t +sp_openai_session_header(const char *url, char *buf, size_t cap) +{ + buf[0] = '\0'; + if (url != NULL && strstr(url, "opencode.ai") != NULL && + g_session != NULL && *g_session != '\0') + snprintf(buf, cap, "x-opencode-session: %s", g_session); + return strlen(buf); +} void sp_openai_set_cancel(const volatile int *flag) @@ -89,13 +112,17 @@ openai_curl(const struct openai_ctx *c, const char *body_path, int stream, { char tmo[32], data[600], auth[sizeof(c->key) + 32]; const char *args[20]; - const char *hdrs[2] = { NULL, NULL }; + const char *hdrs[4] = { NULL, NULL, NULL, NULL }; + char sess[160], ua[64]; + size_t nh = 0; int n = 0, fd; FILE *f; snprintf(tmo, sizeof(tmo), "%ld", c->timeout_s); snprintf(data, sizeof(data), "@%s", body_path); args[n++] = "-sS"; + args[n++] = "-A"; + args[n++] = ua; if (stream) args[n++] = "-N"; args[n++] = "-m"; @@ -115,10 +142,20 @@ openai_curl(const struct openai_ctx *c, const char *body_path, int stream, args[n++] = "\n%{http_code}"; } args[n] = NULL; + /* a client says who it is, and some providers insist on it */ + snprintf(ua, sizeof(ua), "splinter/%s", SP_VERSION); + hdrs[nh++] = "Accept: text/event-stream, application/json"; if (c->key[0] != '\0') { snprintf(auth, sizeof(auth), "Authorization: Bearer %s", c->key); - hdrs[0] = auth; + hdrs[nh++] = auth; } + /* + * opencode-go refuses a request without a stable session id ("Request is + * missing x-opencode-session"): it routes by it and caches the prompt. + */ + if (sp_openai_session_header(c->url, sess, sizeof(sess)) > 0) + hdrs[nh++] = sess; + hdrs[nh] = NULL; fd = sp_curl_start(args, c->url, hdrs, 0, pid, err); memset(auth, 0, sizeof(auth)); if (fd < 0) diff --git a/src/ui.c b/src/ui.c index 4cdd466..caac866 100644 --- a/src/ui.c +++ b/src/ui.c @@ -224,6 +224,107 @@ tool_end_cb(void *ctx, const char *name, const char *obs) pthread_mutex_unlock(&t->lock); } +/* + * A tool call as a person reads it. The model sends {"command":"ls 2\u003e\u00261"}; + * what belongs on the screen is command: ls 2>&1 . Strings are unescaped, + * numbers and booleans kept, anything nested is left as it came. + */ +static void +pretty_args(const char *json, char *out, size_t cap) +{ + const char *p = json; + size_t n = 0; + int first = 1; + + out[0] = '\0'; + while (*p == ' ') + p++; + if (*p != '{') { + snprintf(out, cap, "%s", json); + return; + } + p++; + while (*p != '\0' && *p != '}' && n + 8 < cap) { + char key[48]; + size_t k = 0; + + while (*p == ' ' || *p == ',') + p++; + if (*p != '"') + break; + p++; + while (*p != '\0' && *p != '"' && k + 1 < sizeof(key)) + key[k++] = *p++; + key[k] = '\0'; + if (*p == '"') + p++; + while (*p == ' ' || *p == ':') + p++; + if (!first) + n += (size_t)snprintf(out + n, cap - n, " "); + first = 0; + n += (size_t)snprintf(out + n, cap - n, "%s: ", key); + if (*p == '"') { + p++; + while (*p != '\0' && *p != '"' && n + 4 < cap) { + if (*p == '\\' && p[1] != '\0') { + p++; + if (*p == 'n') { + out[n++] = ' '; + } else if (*p == 't') { + out[n++] = ' '; + } else if (*p == 'u' && strlen(p) >= 5) { + unsigned cp = 0; + int i; + + for (i = 1; i <= 4; i++) { + char c = p[i]; + + cp = cp * 16 + (unsigned)(c >= 'a' ? + c - 'a' + 10 : c >= 'A' ? + c - 'A' + 10 : c - '0'); + } + p += 4; + if (cp < 0x80) + out[n++] = (char)cp; + else if (cp < 0x800) { + out[n++] = (char)(0xc0 | (cp >> 6)); + out[n++] = (char)(0x80 | (cp & 0x3f)); + } else { + out[n++] = (char)(0xe0 | (cp >> 12)); + out[n++] = (char)(0x80 | ((cp >> 6) & 0x3f)); + out[n++] = (char)(0x80 | (cp & 0x3f)); + } + } else + out[n++] = *p; + p++; + continue; + } + out[n++] = *p++; + } + if (*p == '"') + p++; + } else { + /* a number, a boolean, an object: as it is, up to the next key */ + int depth = 0; + + while (*p != '\0' && n + 4 < cap) { + if (*p == '{' || *p == '[') + depth++; + else if (*p == '}' || *p == ']') { + if (depth == 0) + break; + depth--; + } else if (*p == ',' && depth == 0) + break; + out[n++] = *p++; + } + } + out[n] = '\0'; + } + out[n < cap ? n : cap - 1] = '\0'; +} + static void tool_begin_cb(void *ctx, const char *name, const char *args) { @@ -244,7 +345,12 @@ tool_begin_cb(void *ctx, const char *name, const char *args) (void)sp_buf_puts(&t->live, "\n\n"); if (t->flow.len > 0 && t->flow.p[t->flow.len - 1] != '\n') (void)sp_buf_puts(&t->flow, "\n"); - snprintf(mark, sizeof(mark), "\n› %s %.480s\n\n", name, a); + { + char shown[512]; + + pretty_args(a, shown, sizeof(shown)); + snprintf(mark, sizeof(mark), "\n› %s %.480s\n\n", name, shown); + } (void)sp_buf_puts(&t->flow, mark); pthread_mutex_unlock(&t->lock); } @@ -288,9 +394,9 @@ ui_limit_wait(void *ctx, long until, const char *why) return 0; strftime(when, sizeof(when), "%a %H:%M", localtime_r(&tt, &tm)); (void)why; - snprintf(line, sizeof(line), "usage limit: continuing at %s", when); + snprintf(line, sizeof(line), sp_t("ui.limit_wait"), when); pthread_mutex_lock(&t->lock); - snprintf(t->tool_line, sizeof(t->tool_line), "%s (Ctrl-C stops, /resume goes on)", line); + snprintf(t->tool_line, sizeof(t->tool_line), "%s", line); if (t->flow.len > 0 && t->flow.p[t->flow.len - 1] != '\n') (void)sp_buf_puts(&t->flow, "\n"); (void)sp_buf_puts(&t->flow, "\xe2\x80\xba "); @@ -607,7 +713,7 @@ entry_prefix(const struct entry *e, char *buf, size_t len) strftime(clock, sizeof(clock), "%H:%M:%S", &tm); if (strcmp(e->role, "thinking") == 0) { /* no '*': the markdown pass would eat it as emphasis */ - snprintf(buf, len, "[%s] ~ thinking: ", clock); + snprintf(buf, len, "[%s] ~ %s: ", clock, sp_t("ui.thinking")); } else if (strcmp(e->role, "you") == 0) { snprintf(who, sizeof(who), "%.40s", g_who); snprintf(buf, len, "[%s] <%s> ", clock, who); @@ -1081,86 +1187,161 @@ sidebar_heading(int *y, int cols, const char *text) wattron(ui_side, COLOR_PAIR(PAIR_MAIN)); } +/* wrap 'text' into the sidebar from row *y, at most 'maxrows' rows */ +static void +side_text(int *y, int cols, const char *text, int maxrows) +{ + int rows = 0; + + while (*text != '\0' && rows < maxrows && *y < getmaxy(ui_side)) { + size_t n = strlen(text), take = n; + + if (take > (size_t)cols - 1) { + take = (size_t)cols - 1; + while (take > 0 && text[take] != ' ') + take--; + if (take == 0) + take = (size_t)cols - 1; + } + mvwaddnstr(ui_side, (*y)++, 0, text, (int)take); + rows++; + text += take; + while (*text == ' ') + text++; + } +} + +/* names of widgets the sidebar does not repeat: the status line has them */ +static int +widget_hidden(const char *name) +{ + return !strcmp(name, "clock") || !strcmp(name, "cps") || + !strcmp(name, "scheduler"); +} + static void draw_sidebar(struct ui_state *s, const char *id, const char *dir, struct sp_host *host) { - int cols, y = 0; + int cols, y = 0; + char key[64]; + const char *act = sp_usage_active(); + (void)dir; cols = getmaxx(ui_side); werase(ui_side); - { - time_t now = time(NULL); - struct tm tm; - char stamp[64]; - - (void)localtime_r(&now, &tm); - strftime(stamp, sizeof(stamp), "%A, %d %B %Y", &tm); + /* who is answering: the profile, the provider, the model */ + sidebar_heading(&y, cols, sp_t("sidebar.model")); + if (s->agents.n > 0) { wattron(ui_side, A_BOLD); - mvwaddnstr(ui_side, y++, 0, stamp, cols - 1); - wattrset(ui_side, A_NORMAL); + { + char tag[64]; + + snprintf(tag, sizeof(tag), "[%s]", + s->agents.v[s->agent_cur].name); + mvwaddnstr(ui_side, y++, 0, tag, cols - 1); + } + wattroff(ui_side, A_BOLD); + } + { + char line[300]; + + snprintf(line, sizeof(line), "%s%s%s", act, + g_model[0] != '\0' ? " / " : "", g_model); + side_text(&y, cols, line, 2); + } + y++; + + /* how much of each allowance is used: the provider in use, and any that + * has a limit or is benched */ + { + const struct sp_config *cfg = s->conf; + struct sp_usage u; + size_t i; + int printed = 0; + char brief[96], line[200]; + + for (i = 0; i <= (cfg != NULL ? cfg->ndelegates : 0); i++) { + const char *pid = i == 0 ? act : cfg->delegate[i - 1].id; + + if (i > 0 && strcmp(pid, act) == 0) + continue; + sp_usage_for(cfg, pid, &u); + if (i > 0 && u.frac < 0 && u.benched_until == 0) + continue; + if (!printed) { + sidebar_heading(&y, cols, sp_t("sidebar.usage")); + printed = 1; + } + sp_usage_brief(&u, brief, sizeof(brief)); + snprintf(line, sizeof(line), "%-9.9s %s", pid, brief); + if (u.frac >= 0.9 || u.benched_until > 0) + wattron(ui_side, COLOR_PAIR(PAIR_ACCENT) | A_BOLD); + if (y < getmaxy(ui_side)) + mvwaddnstr(ui_side, y++, 0, line, cols - 1); + wattrset(ui_side, COLOR_PAIR(PAIR_MAIN)); + } + if (printed) + y++; + } + + /* what the plugins want to show: the goal, the plan, tasks ... */ + if (host != NULL && sp_host_widget_count(host) > 0) { + char snap[1024], *line, *save = NULL; + + sp_host_widget_snapshot(host, snap, sizeof(snap)); + for (line = strtok_r(snap, "\n", &save); line != NULL; + line = strtok_r(NULL, "\n", &save)) { + char name[48], *colon = strchr(line, ':'); + char *text; + size_t nl; + + if (colon == NULL) + continue; + nl = (size_t)(colon - line); + if (nl >= sizeof(name)) + continue; + memcpy(name, line, nl); + name[nl] = '\0'; + text = colon + 1; + while (*text == ' ') + text++; + /* a text that repeats its own name ("plan: 0 tasks") */ + if (strncmp(text, name, nl) == 0 && text[nl] == ':') { + text += nl + 1; + while (*text == ' ') + text++; + } + if (*text == '\0' || widget_hidden(name) || y >= getmaxy(ui_side) - 2) + continue; + snprintf(key, sizeof(key), "widget.%s", name); + sidebar_heading(&y, cols, sp_td(key, name)); + side_text(&y, cols, text, 4); + y++; + } + } + + /* the last turn, in one line */ + if (s->last_ms > 0 && y < getmaxy(ui_side) - 2) { + char line[160]; + + sidebar_heading(&y, cols, sp_t("sidebar.last_turn")); + snprintf(line, sizeof(line), sp_t("sidebar.turn_line"), + (double)s->last_ms / 1000.0, s->last_steps, s->last_calls); + side_text(&y, cols, line, 1); + if (s->tokens_out > 0) { + snprintf(line, sizeof(line), sp_t("sidebar.tokens"), + s->tokens_out, s->tokens_in); + side_text(&y, cols, line, 1); + } y++; } - sidebar_heading(&y, cols, sp_t("sidebar.session")); - mvwaddnstr(ui_side, y++, 0, id != NULL ? id : sp_t("ui.empty"), - cols - 1); - if (dir != NULL) - mvwaddnstr(ui_side, y++, 0, dir, cols - 1); - y++; - - sidebar_heading(&y, cols, sp_t("sidebar.transport")); - mvwaddnstr(ui_side, y++, 0, s->endpoint != NULL ? s->endpoint : sp_t("sidebar.script"), - cols - 1); - y++; - - sidebar_heading(&y, cols, sp_t("sidebar.theme")); - wattron(ui_side, COLOR_PAIR(PAIR_ACCENT)); - mvwaddnstr(ui_side, y++, 0, s->theme.name, cols - 1); - wattron(ui_side, COLOR_PAIR(PAIR_MAIN)); - y++; - - sidebar_heading(&y, cols, sp_t("sidebar.last_turn")); - mvwprintw(ui_side, y++, 0, sp_t("sidebar.ms"), s->last_ms); - mvwprintw(ui_side, y++, 0, sp_t("sidebar.steps"), s->last_steps); - mvwprintw(ui_side, y++, 0, sp_t("sidebar.tool_calls"), s->last_calls); - y++; - - sidebar_heading(&y, cols, sp_t("sidebar.output")); - if (s->tokens_out > 0) - mvwprintw(ui_side, y++, 0, sp_t("sidebar.tokens"), - s->tokens_out, s->tokens_in); - else { - size_t chars = s->total_out_chars + s->out_chars; - - mvwprintw(ui_side, y++, 0, sp_t("sidebar.chars"), - chars, chars / 4); - } - y++; - - sidebar_heading(&y, cols, sp_t("sidebar.widgets")); - if (host == NULL || sp_host_widget_count(host) == 0) { + if (id != NULL && y < getmaxy(ui_side) - 1) { + sidebar_heading(&y, cols, sp_t("sidebar.session")); wattron(ui_side, COLOR_PAIR(PAIR_DIM)); - mvwaddnstr(ui_side, y++, 0, sp_t("sidebar.no_widgets"), cols - 1); - } else { - static char cache[1024]; - char snap[1024]; - char *line, *save = NULL; - - /* - * The sidebar is independent from the turn: a widget (clock, status) - * must not freeze while the model thinks or streams. Snapshot is a - * mutexed copy; only drawing happens on the UI thread. - */ - sp_host_widget_snapshot(host, snap, sizeof(snap)); - snprintf(cache, sizeof(cache), "%s", snap); - for (line = strtok_r(snap, "\n", &save); line != NULL; - line = strtok_r(NULL, "\n", &save)) { - if (y >= getmaxy(ui_side)) - break; - mvwaddnstr(ui_side, y++, 0, line, cols - 1); - } + side_text(&y, cols, id, 1); } wattrset(ui_side, A_NORMAL); @@ -1205,37 +1386,22 @@ update_meter(struct ui_state *s, struct sp_host *host) static void usage_line(struct ui_state *s, char *buf, size_t cap) { - static char cache[256]; + static char cache[160]; static time_t at; time_t now = time(NULL); if (now - at >= 5 || cache[0] == '\0') { - const struct sp_config *cfg = s->conf; - const char *act = sp_usage_active(); struct sp_usage u; - size_t i, n = 0; char b[96]; at = now; cache[0] = '\0'; - sp_usage_for(cfg, act, &u); - if (u.frac >= 0 || u.benched_until > 0) { + sp_usage_for(s->conf, sp_usage_active(), &u); + /* the sidebar has the figures; here only a warning */ + if (u.benched_until > 0 || u.frac >= 0.8) { sp_usage_brief(&u, b, sizeof(b)); - n += (size_t)snprintf(cache + n, sizeof(cache) - n, - "%s %s", act, b); - } - for (i = 0; cfg != NULL && i < cfg->ndelegates && - n + 24 < sizeof(cache); i++) { - const struct sp_delegate *d = &cfg->delegate[i]; - - if (strcmp(d->id, act) == 0) - continue; - sp_usage_for(cfg, d->id, &u); - if (u.frac < 0 && u.benched_until == 0) - continue; - sp_usage_brief(&u, b, sizeof(b)); - n += (size_t)snprintf(cache + n, sizeof(cache) - n, - "%s%s %s", n > 0 ? " | " : "", d->id, b); + snprintf(cache, sizeof(cache), "%s: %s", + sp_usage_active(), b); } } snprintf(buf, cap, "%s", cache); @@ -1287,14 +1453,17 @@ draw_status(struct ui_state *s, const char *running) char right[96], use[256]; int ulen, pos; - snprintf(right, sizeof(right), "%s %ldms ", s->theme.name, - s->last_ms); + if (s->last_ms > 0) + snprintf(right, sizeof(right), "%.1fs ", + (double)s->last_ms / 1000.0); + else + right[0] = '\0'; usage_line(s, use, sizeof(use)); ulen = (int)strlen(use); - pos = cols - (int)strlen(right) - 3 - ulen; + pos = cols - (int)strlen(right) - 2 - ulen; if (ulen > 0 && pos > at + 1) mvwaddnstr(ui_status, 0, pos, use, ulen); - if ((int)strlen(right) + 1 < cols) + if (right[0] != '\0' && (int)strlen(right) + 1 < cols) mvwaddnstr(ui_status, 0, cols - (int)strlen(right) - 1, right, cols - 1); } @@ -1465,6 +1634,19 @@ add_entry(struct ui_state *s, const char *role, const char *text) * on is remembered the first time a profile moves it and comes back with the * next profile that names no model. */ +/* a built-in profile's description in the user's language */ +static const char * +agent_desc(const struct sp_agent_def *d) +{ + if (strcmp(d->source, "(built in)") == 0) { + if (strcmp(d->name, "build") == 0) + return sp_t("agent.desc.build"); + if (strcmp(d->name, "plan") == 0) + return sp_t("agent.desc.plan"); + } + return d->description; +} + static void agent_switch(struct ui_state *s, struct sp_agent *ag, int idx, int announce) { @@ -1508,9 +1690,8 @@ agent_switch(struct ui_state *s, struct sp_agent *ag, int idx, int announce) } } if (announce) { - snprintf(msg, sizeof(msg), "agent: %s%s%s%s", d->name, - d->description[0] != '\0' ? " - " : "", d->description, - d->max_auth == 0 ? " [read-only]" : ""); + snprintf(msg, sizeof(msg), sp_t("agent.switched"), d->name, + agent_desc(d), d->max_auth == 0 ? sp_t("agent.readonly") : ""); add_entry(s, "splinter", msg); } } @@ -2084,25 +2265,30 @@ ui_slash(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, int idx = sp_agents_find(&s->agents, args); if (idx < 0) - sp_buf_printf(&out, "no agent '%s'\n", args); + { + sp_buf_printf(&out, sp_t("agent.none"), args); + sp_buf_puts(&out, "\n"); + } else { - agent_switch(s, ag, idx, 0); - sp_buf_printf(&out, "agent: %s\n", args); + agent_switch(s, ag, idx, 1); + snprintf(outbuf, sizeof(outbuf), "%s", ""); + sp_buf_free(&out); + return 1; } } - if (args[0] == '\0' || - (out.len > 0 && strncmp(out.p, "no agent", 8) == 0)) + { for (i = 0; i < s->agents.n; i++) { const struct sp_agent_def *d = &s->agents.v[i]; - sp_buf_printf(&out, "%c %-12s %s%s\n", + sp_buf_printf(&out, "%c %-12s %s %s\n", (int)i == s->agent_cur ? '*' : ' ', d->name, - d->description, d->max_auth == 0 ? - " [read-only]" : ""); + agent_desc(d), d->max_auth == 0 ? + sp_t("agent.readonly") : ""); if (strcmp(d->source, "(built in)") != 0) sp_buf_printf(&out, " %s\n", d->source); } - sp_buf_puts(&out, "Tab switches, /agent NAME picks\n"); + } + sp_buf_puts(&out, sp_t("agent.help")); snprintf(outbuf, sizeof(outbuf), "%s", out.p ? out.p : ""); sp_buf_free(&out); add_entry(s, "splinter", outbuf); @@ -2567,8 +2753,7 @@ turn_paint(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, " ctx %zu tok |", m.ctx_used); } snprintf(run, sizeof(run), - "running %lds%s |%s Enter=steer Ctrl-C=cancel", - ms / 1000, tname, ctx); + sp_t("ui.running"), ms / 1000, tname, ctx); draw_status(s, run); } werase(ui_input); diff --git a/src/usage.c b/src/usage.c index 9feb190..71a65cb 100644 --- a/src/usage.c +++ b/src/usage.c @@ -11,6 +11,7 @@ #include "config.h" #include "usage.h" +#include "i18n.h" #define KEEP_SECONDS (32L * 86400) /* the longest window anyone asks for */ #define PRUNE_BYTES (256L * 1024) @@ -273,10 +274,10 @@ sp_usage_brief(const struct sp_usage *u, char *buf, size_t cap) char w[48], t[48]; if (u->benched_until > 0) { - snprintf(buf, cap, "limit, back in %s", + snprintf(buf, cap, sp_t("usage.benched"), sp_usage_until(u->benched_until, t, sizeof(t))); } else if (u->frac < 0) { - snprintf(buf, cap, "%lu calls", u->req_used); + snprintf(buf, cap, sp_t("usage.calls"), u->req_used); } else { window_name(u->req_limit > 0 && (u->tok_limit == 0 || (double)u->req_used / (double)u->req_limit >= @@ -286,7 +287,7 @@ sp_usage_brief(const struct sp_usage *u, char *buf, size_t cap) if (u->frac >= 1.0 && u->reset_at > 0) { size_t n = strlen(buf); - snprintf(buf + n, cap - n, ", room in %s", + snprintf(buf + n, cap - n, sp_t("usage.room"), sp_usage_until(u->reset_at, t, sizeof(t))); } } @@ -333,10 +334,10 @@ sp_usage_report(const struct sp_config *cfg, struct sp_buf *out) (void)wr; sp_buf_puts(out, line); if (u.req_limit > 0) - sp_buf_printf(out, " (%lu/%lu calls)", u.req_used, + sp_buf_printf(out, sp_t("usage.of_calls"), u.req_used, u.req_limit); if (u.tok_limit > 0) - sp_buf_printf(out, " (%lu/%lu tokens)", u.tok_used, + sp_buf_printf(out, sp_t("usage.of_tokens"), u.tok_used, u.tok_limit); sp_buf_puts(out, "\n"); } diff --git a/tests/test_agents_ui.sh b/tests/test_agents_ui.sh index b2b70f9..2d2953f 100644 --- a/tests/test_agents_ui.sh +++ b/tests/test_agents_ui.sh @@ -64,6 +64,16 @@ tmux send-keys -t $S -l 'nochmal' tmux send-keys -t $S Enter check "build asks for consent" shows 'y/n\|allow\|approve\|Allow\|Approve' +# the language follows LANG (de_DE -> de), and no catalogue key shows through +tmux kill-session -t $S 2>/dev/null +tmux new-session -d -s $S -x 120 -y 30 -c "$D/proj" \ + "env -u SPLINTER_LOCALE LANG=de_DE.UTF-8 HOME=$D/home SPLINTER_USAGE_DIR=$D/u \ + SPLINTER_COOLDOWN_DIR=$D/c $PWD/splinter --no-config \ + --endpoint http://127.0.0.1:$P/v1/chat/completions --model m --chat 2>/dev/null" +check "German UI from LANG=de_DE" shows 'bereit' +check "no raw catalogue keys on the screen" sh -c \ + "! tmux capture-pane -t $S -p | grep -Eq '(^|[ >])(ui|sidebar|status|widget|agent|usage|role)\.[a-z_]+'" + if [ "$fails" -eq 0 ]; then echo "test_agents_ui: ok"; exit 0; fi tmux capture-pane -t $S -p | tail -15 exit 1 diff --git a/tests/test_i18n.c b/tests/test_i18n.c index df4b8be..4f750ce 100644 --- a/tests/test_i18n.c +++ b/tests/test_i18n.c @@ -48,7 +48,7 @@ main(void) CHECK(strcmp(sp_t("sidebar.last_turn"), "letzter Turn") == 0); /* format strings keep their placeholders, so printf still works */ - CHECK(strstr(sp_t("sidebar.steps"), "%zu") != NULL); + CHECK(strstr(sp_t("sidebar.turn_line"), "%zu") != NULL); /* ---- completeness: every English key has a German one ---- */ n_en = (size_t)sp_i18n_fallback_count(); diff --git a/tests/test_jobs.c b/tests/test_jobs.c index 1b70bfe..e409217 100644 --- a/tests/test_jobs.c +++ b/tests/test_jobs.c @@ -211,7 +211,8 @@ main(void) CHECK(sh != NULL); CHECK(sp_plugin_load(sh, "plugins/scheduler/scheduler.so", &err) == 0); CHECK(sp_host_widget_snapshot(sh, snap, sizeof(snap)) > 0); - CHECK(strstr(snap, "sched: idle") != NULL); + /* idle says nothing: the widget exists, its text is empty */ + CHECK(strstr(snap, "scheduler") != NULL); sp_host_free(sh); sp_registry_free(&r); } diff --git a/tests/test_usage.c b/tests/test_usage.c index 301b43f..b3d8b82 100644 --- a/tests/test_usage.c +++ b/tests/test_usage.c @@ -92,6 +92,21 @@ main(void) CHECK(strstr(b, "1h 40%") != NULL); } + /* opencode-go wants the session id in a header; others get none */ + { + char h[200]; + + sp_openai_set_session("sess-42"); + CHECK(sp_openai_session_header( + "https://opencode.ai/zen/go/v1/chat/completions", h, + sizeof(h)) > 0); + CHECK(strcmp(h, "x-opencode-session: sess-42") == 0); + CHECK(sp_openai_session_header("https://ollama.com/v1/chat/completions", + h, sizeof(h)) == 0); + CHECK(sp_openai_session_header("http://127.0.0.1:8080/v1/chat", h, + sizeof(h)) == 0); + } + snprintf(path, sizeof(path), "rm -rf %s", dir); if (system(path) != 0) failures++; diff --git a/tools/check_i18n.py b/tools/check_i18n.py index 1effd68..0e65723 100755 --- a/tools/check_i18n.py +++ b/tools/check_i18n.py @@ -86,7 +86,9 @@ def main(): + ", ".join(missing), file=sys.stderr) # the English catalogue is the reference: everything in it must be used en = catalogue(ROOT / "share/locale/en.msg") - unused = sorted(k for k in en if k not in used) + # keys built at run time from a name: interview.q., widget. + dynamic = ("interview.q.", "interview.h.", "widget.") + unused = sorted(k for k in en if k not in used and not k.startswith(dynamic)) if unused: print("en.msg: keys nothing uses: " + ", ".join(unused), file=sys.stderr) roles = unknown_roles() diff --git a/tools/embed_catalogs.sh b/tools/embed_catalogs.sh new file mode 100755 index 0000000..a9aa99a --- /dev/null +++ b/tools/embed_catalogs.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Writes a C file holding the message catalogues, so a splinter that was copied +# somewhere (make install, a tarball) never shows raw keys like "ui.ready". +# usage: embed_catalogs.sh share/locale/*.msg > src/catalogs.c +echo '/* generated by tools/embed_catalogs.sh from share/locale -- do not edit */' +echo '#include "splinter.h"'; echo '#include "i18n.h"' +echo +for f in "$@"; do + n=$(basename "$f" .msg | tr -c 'A-Za-z0-9\n' '_') + echo "static const char cat_$n[] =" + sed 's/\\/\\\\/g; s/"/\\"/g; s/^/\t"/; s/$/\\n"/' "$f" + echo " ;" +done +echo +echo 'const struct sp_embedded sp_embedded_catalogs[] = {' +for f in "$@"; do + l=$(basename "$f" .msg) + n=$(echo "$l" | tr -c 'A-Za-z0-9\n' '_') + echo " { \"$l\", cat_$n }," +done +echo ' { 0, 0 }' +echo '};'