diff --git a/README.md b/README.md index 4e8bbe9..aff400d 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,11 @@ in `/history`, so the next session starts with it. ## Steering while a turn runs -The turn runs on its own thread, so the keyboard stays live. The live area has +The turn runs on its own thread, so the keyboard stays live. Every tool call is +written into the transcript with its name and arguments - `werkzeug: run +{"command":"echo a"}` - because "it did something" is not information. The +reasoning is inserted before those lines: it came first, and the log should read +in the order things happened. The live area has a fixed height (reasoning 8 rows, answer 5, tool line 1): a region that grows with the text moves everything else around it, and text that jumps around the screen is unreadable. While it runs, the diff --git a/include/splinter.h b/include/splinter.h index cbf5a23..2abddf7 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -198,6 +198,11 @@ struct sp_agent_config { int (*steer)(void *ctx, struct sp_buf *out); void *steer_ctx; + /* Called before a tool runs, with its name and its arguments as sent. */ + void (*tool_begin)(void *ctx, const char *name, + const char *args); + void *tool_begin_ctx; + /* incremental output, if the transport and the caller both support it */ sp_stream_cb stream; void *stream_ctx; diff --git a/libsplinter.so b/libsplinter.so index 9888bd8..98b9e67 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/plugins/cli/cli.c b/plugins/cli/cli.c index a2967b4..cea0b7e 100644 --- a/plugins/cli/cli.c +++ b/plugins/cli/cli.c @@ -31,6 +31,7 @@ #include "splinter.h" #include "splinter_plugin.h" +#include "i18n.h" #define OUT_CAP (4u << 20) #define BODY_CAP (1u << 20) @@ -139,18 +140,18 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, goto fail; } if (WIFEXITED(status) && WEXITSTATUS(status) == 124) { - sp_buf_printf(err, "cli: '%s' timed out after %s s", c->cmd, + sp_buf_printf(err, sp_td("cli.timeout", "cli: '%s' timed out after %s s"), c->cmd, c->timeout); goto fail; } if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { - sp_buf_printf(err, "cli: '%s' failed (exit %d): %.400s", c->cmd, + sp_buf_printf(err, sp_td("cli.failed", "cli: '%s' failed (exit %d): %.400s"), c->cmd, WIFEXITED(status) ? WEXITSTATUS(status) : -1, raw.p != NULL ? raw.p : ""); goto fail; } if (raw.len == 0) { - sp_buf_printf(err, "cli: '%s' produced no output", c->cmd); + sp_buf_printf(err, sp_td("cli.no_output", "cli: '%s' produced no output"), c->cmd); goto fail; } out->status = 200; diff --git a/plugins/cli/cli.so b/plugins/cli/cli.so index 35dbdaf..7dc5852 100755 Binary files a/plugins/cli/cli.so and b/plugins/cli/cli.so differ diff --git a/plugins/cps/cps.c b/plugins/cps/cps.c index acba38c..81184c6 100644 --- a/plugins/cps/cps.c +++ b/plugins/cps/cps.c @@ -23,6 +23,7 @@ #include "splinter.h" #include "splinter_plugin.h" +#include "i18n.h" #define SNAP_MAX 256 #define KEEP_TAIL 6 @@ -144,7 +145,7 @@ cps_tick(struct sp_host *h, const char *name, void *ctx) pthread_mutex_lock(&cps.lock); if (!cps.requested) { cps.idle++; - snprintf(line, sizeof(line), "idle (applied %lu, prepared %lu)", + snprintf(line, sizeof(line), sp_td("cps.idle", "idle (applied %lu, prepared %lu)"), cps.applied, cps.prepared); pthread_mutex_unlock(&cps.lock); return sp_host_widget_text(h, name, line); @@ -170,7 +171,7 @@ cps_tick(struct sp_host *h, const char *name, void *ctx) cps.ready = 1; cps.prepared++; did = 1; - snprintf(line, sizeof(line), "plan ready: cut %zu/%zu, %zu -> %zu B", + snprintf(line, sizeof(line), sp_td("cps.plan", "plan ready: cut %zu/%zu, %zu -> %zu B"), cut - 1, cps.n, cps.before, cps.after); pthread_mutex_unlock(&cps.lock); (void)did; diff --git a/plugins/cps/cps.so b/plugins/cps/cps.so index b956860..043ffaa 100755 Binary files a/plugins/cps/cps.so and b/plugins/cps/cps.so differ diff --git a/plugins/focus/focus.c b/plugins/focus/focus.c index 2515933..436c4b9 100644 --- a/plugins/focus/focus.c +++ b/plugins/focus/focus.c @@ -28,6 +28,7 @@ #include "splinter.h" #include "splinter_plugin.h" +#include "i18n.h" #define GOAL_MAX 1000 @@ -55,9 +56,14 @@ focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget, if (turn == last_reminder) return 0; /* one reminder per turn, always */ - snprintf(text, sizeof(text), "[focus] Das Ziel dieser Sitzung: %s " - "Pruefe vor der Antwort, ob der letzte Schritt naeher daran war; " - "wenn nicht, sage das und schlage den naechsten Schritt vor.", goal); + { + char rem[1024]; + + snprintf(rem, sizeof(rem), sp_td("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"), goal); + snprintf(text, sizeof(text), "[focus] %s", rem); + } memset(&m, 0, sizeof(m)); m.role = "system"; @@ -91,10 +97,10 @@ focus_command(struct sp_host *h, const char *args, struct sp_buf *out, (void)ctx; if (args == NULL || *args == '\0') { if (goal[0] == '\0') - return sp_buf_puts(out, "kein Goal gesetzt " - "(/goal )"); - return sp_buf_printf(out, "goal: %s (%u Erinnerungen, alle %u " - "Turns)", goal, reminders, every); + return sp_buf_puts(out, sp_td("focus.none", "no goal set (/goal )")); + return sp_buf_printf(out, sp_td("focus.status", + "goal: %s (%u reminders, every %u turns)"), goal, + reminders, every); } snprintf(goal, sizeof(goal), "%.*s", GOAL_MAX, args); if (strlen(args) > GOAL_MAX) @@ -106,11 +112,11 @@ focus_command(struct sp_host *h, const char *args, struct sp_buf *out, { char line[192]; - snprintf(line, sizeof(line), "%.60s%s | 0 reminders", goal, + snprintf(line, sizeof(line), "%.60s%s", goal, strlen(goal) > 60 ? "..." : ""); (void)sp_host_widget_text(h, "focus", line); } - return sp_buf_printf(out, "goal gesetzt: %s", goal); + return sp_buf_printf(out, sp_td("focus.set", "goal set: %s"), goal); } static int @@ -121,7 +127,7 @@ recap_command(struct sp_host *h, const char *args, struct sp_buf *out, (void)args; (void)ctx; if (goal[0] == '\0') - return sp_buf_puts(out, "kein Goal gesetzt (/goal )"); + return sp_buf_puts(out, sp_td("focus.none", "no goal set (/goal )")); force = 1; /* the next turn gets a reminder */ return sp_buf_puts(out, "Erinnerung fuer den naechsten Turn eingeplant"); } @@ -133,9 +139,10 @@ focus_tick(struct sp_host *h, const char *name, void *ctx) (void)ctx; if (goal[0] == '\0') - snprintf(line, sizeof(line), "no goal (/goal )"); + snprintf(line, sizeof(line), "%s", sp_td("focus.widget_none", + "no goal (/goal )")); else - snprintf(line, sizeof(line), "%.44s%s | %u rem", + snprintf(line, sizeof(line), "%.44s%s | %u", goal, strlen(goal) > 44 ? "..." : "", reminders); return sp_host_widget_text(h, name, line); } diff --git a/plugins/focus/focus.so b/plugins/focus/focus.so index cade179..edc4227 100755 Binary files a/plugins/focus/focus.so and b/plugins/focus/focus.so differ diff --git a/share/locale/de.msg b/share/locale/de.msg index 699eed0..9fbc371 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -5,11 +5,8 @@ # selbst - nie als leere Zeile. ui.ready = bereit. Enter sendet, Ctrl-D beendet. -ui.prompt = > ui.working = laeuft... -ui.running = laeuft %lds | Enter steuert, Ctrl-C bricht ab ui.steering_queued = Steuerung eingereiht -ui.input_hint = Enter steuert, Ctrl-C bricht ab sidebar.session = Sitzung sidebar.transport = Transport @@ -17,7 +14,6 @@ sidebar.theme = Farbschema sidebar.last_turn = letzter Turn sidebar.output = Ausgabe sidebar.widgets = Widgets -sidebar.nothing = keine geladen sidebar.steps = %zu Schritte sidebar.tool_calls = %zu Werkzeuge sidebar.tokens = %zu Tok (ein %zu) @@ -41,3 +37,20 @@ err.oom = kein Speicher err.theme = Farbschema err.session = Sitzung err.plugin_failed = Plugin fehlgeschlagen + +role.tool = werkzeug +ui.running_steer = laeuft %lds | Enter steuert, Ctrl-C bricht ab +err.thread = Turn-Thread laesst sich nicht starten +err.turn_failed = Turn fehlgeschlagen +err.followup_failed = Nachfrage 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 ) +focus.status = Ziel: %s (%u Erinnerungen, alle %u Turns) +cps.idle = Leerlauf (angewandt %lu, vorbereitet %lu) +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 ) diff --git a/share/locale/en.msg b/share/locale/en.msg index 23da2be..2345eae 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -4,11 +4,8 @@ # key that exists nowhere is shown as itself, so nothing is silently empty. ui.ready = ready. Enter sends, Ctrl-D quits. -ui.prompt = > ui.working = working... -ui.running = running %lds | Enter steers, Ctrl-C cancels ui.steering_queued = steering queued -ui.input_hint = Enter steers, Ctrl-C cancels sidebar.session = session sidebar.transport = transport @@ -16,7 +13,6 @@ sidebar.theme = theme sidebar.last_turn = last turn sidebar.output = output sidebar.widgets = widgets -sidebar.nothing = none loaded sidebar.steps = %zu steps sidebar.tool_calls = %zu tool calls sidebar.tokens = %zu tok (in %zu) @@ -40,3 +36,20 @@ err.oom = out of memory err.theme = theme err.session = session err.plugin_failed = plugin failed + +role.tool = tool +ui.running_steer = running %lds | Enter steers, Ctrl-C cancels +err.thread = cannot start the turn thread +err.turn_failed = turn failed +err.followup_failed = follow-up 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 ) +focus.status = goal: %s (%u reminders, every %u turns) +cps.idle = idle (applied %lu, prepared %lu) +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 ) diff --git a/src/agent.c b/src/agent.c index ffee77c..c7b9f73 100644 --- a/src/agent.c +++ b/src/agent.c @@ -207,6 +207,9 @@ sp_agent_run(struct sp_agent *a, const char *user_text, struct sp_buf *answer, struct sp_msg tm; sp_buf_clear(&obs); + if (a->cfg->tool_begin != NULL) + a->cfg->tool_begin(a->cfg->tool_begin_ctx, + reply.calls[i].name, reply.calls[i].args); run_one_call(a, &reply.calls[i], &obs); memset(&tm, 0, sizeof(tm)); tm.role = "tool"; diff --git a/src/i18n.c b/src/i18n.c index 63f5f0d..b4c96aa 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -212,6 +212,22 @@ sp_i18n_fallback_key(size_t index) return index < fallback_n ? fallback[index].key : NULL; } +const char * +sp_td(const char *key, const char *dflt) +{ + const char *v; + + if (key == NULL) + return dflt != NULL ? dflt : ""; + v = lookup(main_cat, main_n, key); + if (v != NULL) + return v; + v = lookup(fallback, fallback_n, key); + if (v != NULL) + return v; + return dflt != NULL ? dflt : key; +} + const char * sp_i18n_init(const char *override_lang) { diff --git a/src/i18n.h b/src/i18n.h index 673e3df..725be65 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -30,6 +30,13 @@ const char *sp_i18n_init(const char *override_lang); /* Translate a key. Never NULL: falls back to English, then to the key. */ const char *sp_t(const char *key); +/* + * For format strings: the compiled-in English text is the last resort, so a + * missing catalogue never costs a placeholder (a printf format without its %s + * silently drops the value it was meant to carry). + */ +const char *sp_td(const char *key, const char *dflt); + /* For tests and for plugins that ship their own catalogue. */ int sp_i18n_load(struct sp_buf *err, const char *lang); const char *sp_i18n_lang(void); diff --git a/src/main.c b/src/main.c index 4b5d088..8cd2665 100644 --- a/src/main.c +++ b/src/main.c @@ -84,7 +84,7 @@ load_script(struct sp_mock *m, const char *path, struct sp_buf *err) continue; if (sp_mock_push(m, 200, line) != 0) { fclose(f); - sp_buf_puts(err, "script: out of memory"); + sp_buf_puts(err, sp_t("err.oom")); return -1; } } @@ -315,7 +315,7 @@ main(int argc, char **argv) } if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0) { - fprintf(stderr, "splinter: out of memory\n"); + fprintf(stderr, "splinter: %s\n", sp_t("err.oom")); return 3; } if (sp_builtin_register(®) != 0) { @@ -336,7 +336,7 @@ main(int argc, char **argv) sessid = default_id; } if (sp_session_open(&sess, sessdir, sessid, &err) != 0) { - fprintf(stderr, "splinter: %s\n", err.p ? err.p : "session"); + fprintf(stderr, "splinter: %s\n", err.p ? err.p : sp_t("err.session")); return 3; } ag.sess = &sess; @@ -353,7 +353,7 @@ main(int argc, char **argv) sp_buf_clear(&err); if (sp_plugin_load(host, plugins[k], &err) != 0) fprintf(stderr, "splinter: %s\n", - err.p != NULL ? err.p : "plugin failed"); + err.p != NULL ? err.p : sp_t("err.plugin_failed")); else fprintf(stderr, "splinter: plugin %s loaded\n", plugins[k]); @@ -377,7 +377,7 @@ main(int argc, char **argv) ag.tr = *tr; } else if (script != NULL) { if (sp_mock_init(&mock) != 0) { - fprintf(stderr, "splinter: out of memory\n"); + fprintf(stderr, "splinter: %s\n", sp_t("err.oom")); return 3; } have_mock = 1; @@ -405,7 +405,7 @@ main(int argc, char **argv) sp_conv_init(&conv); if (cfg.system_prompt != NULL && sp_conv_push(&conv, "system", NULL, cfg.system_prompt) != 0) { - fprintf(stderr, "splinter: out of memory\n"); + fprintf(stderr, "splinter: %s\n", sp_t("err.oom")); return 3; } ag.conv = &conv; diff --git a/src/ui.c b/src/ui.c index bc070e3..d07b787 100644 --- a/src/ui.c +++ b/src/ui.c @@ -100,10 +100,36 @@ struct turn { struct sp_buf live; /* streamed text so far */ struct sp_buf think; /* the model's reasoning so far */ char tool[64]; /* last tool the model called */ + char tool_line[256]; /* name + args, for the live area */ + char tools_done[16][256]; /* finished calls, in order */ + int ntools_done; int tool_tick; pthread_mutex_t lock; }; +/* + * Called from the turn thread right before a tool runs. The UI drains the list + * on its next tick and writes the calls into the transcript in order - so the + * operator sees which tool ran with which arguments, not just that something + * happened. + */ +static void +tool_begin_cb(void *ctx, const char *name, const char *args) +{ + struct turn *t = ctx; + + if (name == NULL) + return; + pthread_mutex_lock(&t->lock); + snprintf(t->tool_line, sizeof(t->tool_line), "%s %.120s", name, + args != NULL ? args : ""); + if (t->ntools_done < 16) + snprintf(t->tools_done[t->ntools_done++], + sizeof(t->tools_done[0]), "%s %.200s", name, + args != NULL ? args : ""); + pthread_mutex_unlock(&t->lock); +} + static void stream_cb(void *ctx, int kind, const char *text) { @@ -199,12 +225,13 @@ init_colors(const struct sp_theme *t) sp_color_separate(dim, bg, 60000); /* labels: visible, not loud */ sp_color_darken(status_bg, 60000); /* the bar is a surface, not text */ - init_pair(PAIR_MAIN, sp_color_rgb(fg[0], fg[1], fg[2], n), - sp_color_rgb(bg[0], bg[1], bg[2], n)); + /* the chat area is black: text on a palette surface reads as a block of + * colour, and the operator asked for black, not for a tinted pane */ + init_pair(PAIR_MAIN, sp_color_rgb(fg[0], fg[1], fg[2], n), COLOR_BLACK); init_pair(PAIR_ACCENT, sp_color_rgb(accent[0], accent[1], accent[2], n), - sp_color_rgb(bg[0], bg[1], bg[2], n)); + COLOR_BLACK); init_pair(PAIR_DIM, sp_color_rgb(dim[0], dim[1], dim[2], n), - sp_color_rgb(bg[0], bg[1], bg[2], n)); + COLOR_BLACK); init_pair(PAIR_STATUS, sp_color_rgb(fg[0], fg[1], fg[2], n), sp_color_rgb(status_bg[0], status_bg[1], status_bg[2], n)); @@ -367,52 +394,129 @@ ui_add_text(WINDOW *w, const char *s, int maxcols) /* - * Print the tail of `text` wrapped into a fixed region, bottom-anchored, with - * an optional scroll-back in lines. Used for the reasoning and for the answer - * while it streams: both are the same problem and should look the same. + * Row accounting for text that contains newlines. + * + * The first version counted rows as strlen()/cols, which is only true for one + * long line. A model's reasoning is full of newlines, so the real height was + * much larger than the estimate and the text painted over the answer below it - + * "it just goes anywhere on the screen". Every row is therefore computed from + * the actual line structure: split on '\n', then wrap each line. + */ +static size_t +text_rows(const char *text, int cols) +{ + size_t rows = 0; + const char *p = text; + + if (text == NULL || *text == '\0' || cols < 2) + return 0; + while (*p != '\0') { + const char *nl = strchr(p, '\n'); + size_t linelen = nl != NULL ? (size_t)(nl - p) : strlen(p); + + rows += linelen == 0 ? 1 : + (linelen + (size_t)cols - 1) / (size_t)cols; + if (nl == NULL) + break; + p = nl + 1; + } + return rows; +} + +/* Byte offset of row `want`, walking the same structure text_rows uses. */ +static size_t +row_offset(const char *text, int cols, size_t want) +{ + size_t row = 0; + const char *p = text; + + while (*p != '\0') { + const char *nl = strchr(p, '\n'); + size_t linelen = nl != NULL ? (size_t)(nl - p) : strlen(p); + size_t rows = linelen == 0 ? 1 : + (linelen + (size_t)cols - 1) / (size_t)cols; + + if (want < row + rows) + return (size_t)(p - text) + + (want - row) * (size_t)cols; + row += rows; + if (nl == NULL) + break; + p = nl + 1; + } + return strlen(text); +} + +/* + * Print the tail of `text` into a fixed region, bottom-anchored, optional + * scroll-back in rows. Newline aware, so the last row on screen really is the + * last row of the text. */ static void draw_wrapped_tail(int y0, int h, int cols, const char *text, int pair, size_t back) { - size_t len, lines, first; - int y; + size_t rows, first, off, row; - if (text == NULL || text[0] == '\0' || h < 1 || cols < 4) + if (text == NULL || *text == '\0' || h < 1 || cols < 4) return; - len = strlen(text); - lines = (len + (size_t)cols - 1) / (size_t)cols; - if ((size_t)h < lines) - first = lines - (size_t)h; - else - first = 0; + rows = text_rows(text, cols); + if (rows == 0) + return; + first = rows > (size_t)h ? rows - (size_t)h : 0; if (back > 0) { if (back > first) back = first; first -= back; } + if (pair) wattron(ui_hist, COLOR_PAIR(pair)); - for (y = 0; y < h && first < lines; y++, first++) { - size_t off = align_utf8(text, len, first * (size_t)cols); - char slice[512]; - size_t take = len - off; + off = row_offset(text, cols, first); + row = 0; + while (row < (size_t)h && off < strlen(text)) { + const char *nl = strchr(text + off, '\n'); + size_t linelen = nl != NULL ? + (size_t)(nl - (text + off)) : strlen(text + off); + size_t take = linelen > (size_t)cols ? + (size_t)cols : linelen; + char slice[512]; - if (take > (size_t)cols) - take = (size_t)cols; - take = align_utf8(text, len, off + take) - off; if (take >= sizeof(slice)) take = sizeof(slice) - 1; memcpy(slice, text + off, take); slice[take] = '\0'; - wmove(ui_hist, y0 + y, 0); - ui_add_text(ui_hist, slice, (int)take); + wmove(ui_hist, y0 + (int)row, 0); + ui_add_text(ui_hist, slice, cols - 1); + row++; + off += take; + if (linelen > (size_t)cols) { + size_t wrapped = take; + + /* the rest of this long line continues on the next row */ + while (wrapped < linelen && row < (size_t)h) { + size_t t2 = linelen - wrapped; + + if (t2 > (size_t)cols) + t2 = (size_t)cols; + if (t2 >= sizeof(slice)) + t2 = sizeof(slice) - 1; + memcpy(slice, text + off, t2); + slice[t2] = '\0'; + wmove(ui_hist, y0 + (int)row, 0); + ui_add_text(ui_hist, slice, cols - 1); + row++; + off += t2; + wrapped += t2; + } + } else if (nl != NULL) { + off++; /* skip the newline itself */ + } } if (pair) wattroff(ui_hist, COLOR_PAIR(pair)); } - /* A role is stored as its English key and displayed in the chosen language. */ static const char * role_label(const char *role) @@ -425,18 +529,34 @@ role_label(const char *role) snprintf(key, sizeof(key), "role.%s", role); t = sp_t(key); if (t == NULL || strcmp(t, key) == 0) - return role; /* untranslated: show the key */ + return role; return t; } +/* + * Rows an entry really occupies: the role prefix shortens the first row, so the + * height is computed from exactly the string that gets drawn ("role: text"), + * not from the text plus an estimate. Getting this wrong lost the last + * character of an entry - a truncated ``` closing a code block. + */ static int entry_lines(int cols, const struct entry *e) { - size_t len = strlen(e->role) + 2 + strlen(e->text); + size_t rows, len; + char *render; if (cols < 2) return 1; - return (int)((len + (size_t)cols - 1) / (size_t)cols); + len = strlen(role_label(e->role)) + 2 + strlen(e->text) + 1; + render = malloc(len); + if (render == NULL) + return 1; + snprintf(render, len, "%s: %s", role_label(e->role), e->text); + rows = text_rows(render, cols); + free(render); + if (rows == 0) + rows = 1; + return (int)rows; } /* @@ -469,6 +589,7 @@ draw_history(struct ui_state *s, int reserve) getmaxyx(ui_hist, rows, cols); werase(ui_hist); + wbkgd(ui_hist, COLOR_PAIR(PAIR_MAIN)); /* while a turn runs the bottom lines belong to it */ if (reserve > 0 && reserve < rows) rows -= reserve; @@ -596,11 +717,13 @@ draw_live(struct ui_state *s, const char *text, const char *tool, draw_wrapped_tail(y_answer, answer_rows, cols, text, PAIR_ACCENT, 0); if (tool != NULL && tool[0] != '\0') { - char head[96]; + char head[320]; - snprintf(head, sizeof(head), " tool %s", tool); + snprintf(head, sizeof(head), " %s", tool); wattron(ui_hist, COLOR_PAIR(PAIR_MAIN) | A_BOLD); - mvwaddnstr(ui_hist, rows - 1, 0, head, cols - 1); + wmove(ui_hist, rows - 1, 0); + ui_add_text(ui_hist, head, cols - 1); + wclrtoeol(ui_hist); wattroff(ui_hist, A_BOLD); } (void)s; @@ -633,7 +756,7 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir, y++; sidebar_heading(&y, cols, sp_t("sidebar.transport")); - mvwaddnstr(ui_side, y++, 0, s->endpoint != NULL ? s->endpoint : "script", + mvwaddnstr(ui_side, y++, 0, s->endpoint != NULL ? s->endpoint : sp_t("sidebar.script"), cols - 1); y++; @@ -751,6 +874,29 @@ hist_push(struct ui_state *s, const char *dir, const char *line) fclose(f); } +/* Insert at a position: the reasoning happened before the tools that followed + * it, and the transcript should read in that order. */ +static void +entry_insert(struct ui_state *s, size_t at, const char *role, const char *text) +{ + char *r, *x; + + if (at > s->n || s->n == MAX_ENTRIES) + return; + r = strdup(role != NULL ? role : ""); + x = strdup(text != NULL ? text : ""); + if (r == NULL || x == NULL) { + free(r); + free(x); + return; + } + memmove(&s->entries[at + 1], &s->entries[at], + sizeof(s->entries[0]) * (s->n - at)); + s->entries[at].role = r; + s->entries[at].text = x; + s->n++; +} + static void add_entry(struct ui_state *s, const char *role, const char *text) { @@ -778,10 +924,16 @@ layout(void) if (cols - chat_w < 12) chat_w = cols > 24 ? cols - 16 : cols; side_w = cols - chat_w; + /* + * Order, top to bottom: chat and sidebar, then the status line, then the + * input. Status above the prompt, the way Hermes shows it - the line you + * read while typing sits directly above what you type. + */ ui_hist = newwin(rows - 2, chat_w, 0, 0); + wbkgd(ui_hist, COLOR_PAIR(PAIR_MAIN)); ui_side = newwin(rows - 2, side_w, 0, chat_w); - ui_input = newwin(1, cols, rows - 2, 0); - ui_status = newwin(1, cols, rows - 1, 0); + ui_status = newwin(1, cols, rows - 2, 0); + ui_input = newwin(1, cols, rows - 1, 0); keypad(ui_input, TRUE); } @@ -801,7 +953,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, return 1; if (sp_theme_load(theme_name, &s.theme, &err) != 0) { fprintf(stderr, "splinter: %s\n", - err.p != NULL ? err.p : "theme"); + err.p != NULL ? err.p : sp_t("err.theme")); return 1; } @@ -1061,12 +1213,14 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, cfg.steer_ctx = &tu; cfg.stream = stream_cb; cfg.stream_ctx = &tu; + cfg.tool_begin = tool_begin_cb; + cfg.tool_begin_ctx = &tu; ag->cfg = &cfg; if (pthread_create(&th, NULL, turn_thread, &tu) != 0) { ag->cfg = orig; snprintf((char *)input, sizeof(input), "%s", - "cannot start the turn thread"); + sp_t("err.thread")); rc = -1; } else { /* @@ -1076,7 +1230,8 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, */ char line[1024]; size_t llen = 0; - int cancelled = 0; + int cancelled = 0, drained = 0; + size_t first_tool = (size_t)-1; wtimeout(ui_input, 100); for (;;) { @@ -1096,7 +1251,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, char live[4096]; char think[2048]; int live_reserve = 0; - char tool[64]; + char tool[256]; size_t llen2 = 0, tlen2 = 0; pthread_mutex_lock(&tu.lock); @@ -1112,8 +1267,9 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, tlen2 = sizeof(think) - 1; memcpy(think, tu.think.p, tlen2); } - snprintf(tool, sizeof(tool), "%s", - tu.tool); + snprintf(tool, sizeof(tool), "%.200s", + tu.tool_line[0] != '\0' ? + tu.tool_line : tu.tool); pthread_mutex_unlock(&tu.lock); live[llen2] = '\0'; think[tlen2] = '\0'; @@ -1126,6 +1282,21 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, think); } s.out_chars = llen2; + if (tu.ntools_done > drained) { + int k; + + for (k = drained; k < tu.ntools_done; + k++) { + char line[300]; + + snprintf(line, sizeof(line), + "%s", tu.tools_done[k]); + if (first_tool == (size_t)-1) + first_tool = s.n; + add_entry(&s, "tool", line); + } + drained = tu.ntools_done; + } draw_history(&s, live_reserve); draw_live(&s, live, tool, think); doupdate(); @@ -1136,9 +1307,10 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, t0.tv_nsec) / 1000000); werase(ui_input); mvwprintw(ui_input, 0, 0, - " running %lds | Enter steers, " - "Ctrl-C cancels | > %s", ms / 1000, - line); + sp_t("ui.running_steer"), + ms / 1000); + ui_add_text(ui_input, line, + getmaxx(ui_input) - 20); wrefresh(ui_input); continue; } @@ -1226,6 +1398,19 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, tu.steers, tu.steers == 1 ? "" : "s"); sp_buf_puts(&answer, note); } + { + int k; + + pthread_mutex_lock(&tu.lock); + for (k = drained; k < tu.ntools_done; k++) { + if (first_tool == (size_t)-1) + first_tool = s.n; + add_entry(&s, "tool", + tu.tools_done[k]); + } + drained = tu.ntools_done; + pthread_mutex_unlock(&tu.lock); + } /* the reasoning stays in the transcript, as a dim * entry, capped so a long plan cannot push the chat * out of the window */ @@ -1234,10 +1419,23 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, size_t tl = tu.think.len; if (tl > 400) { - th += tl - 400; - tl = 400; + size_t skip = tl - 400; + + /* start at a word boundary: a tail + * that begins mid-word reads as + * corruption */ + while (tu.think.p[skip] != '\0' && + tu.think.p[skip] != ' ' && + tu.think.p[skip] != '\n') + skip++; + th = tu.think.p + skip; + tl -= skip; } - add_entry(&s, "thinking", th); + if (first_tool == (size_t)-1) + add_entry(&s, "thinking", th); + else + entry_insert(&s, first_tool, + "thinking", th); } sp_buf_free(&tu.answer); sp_buf_free(&tu.err); @@ -1259,7 +1457,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, add_entry(&s, "agent", answer.p); else add_entry(&s, "error", err.p != NULL ? err.p : - "turn failed"); + sp_t("err.turn_failed")); /* * A single-step turn ends with the answer, so a steer typed during it @@ -1277,7 +1475,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, add_entry(&s, "agent", answer.p); else add_entry(&s, "error", err.p != NULL ? err.p : - "follow-up failed"); + sp_t("err.followup_failed")); pending_steer[0] = '\0'; } } diff --git a/tests/test_ascii b/tests/test_ascii index 5ce4b1c..e290c4e 100755 Binary files a/tests/test_ascii and b/tests/test_ascii differ diff --git a/tests/test_focus b/tests/test_focus index b052e53..e7c659a 100755 Binary files a/tests/test_focus and b/tests/test_focus differ diff --git a/tests/test_focus.c b/tests/test_focus.c index b897b41..88c0b21 100644 --- a/tests/test_focus.c +++ b/tests/test_focus.c @@ -11,6 +11,7 @@ #include "splinter.h" #include "splinter_plugin.h" +#include "i18n.h" static int failures; @@ -37,6 +38,15 @@ count_focus(const struct sp_conv *c) int main(void) { + struct sp_buf ierr; + + /* the plugin speaks through the catalogue; load the English one */ + if (sp_buf_init(&ierr) == 0) { + sp_i18n_add_dir("share/locale"); + (void)sp_i18n_load(&ierr, "en"); + sp_buf_free(&ierr); + } + struct sp_registry reg; struct sp_agent ag; struct sp_host *host; diff --git a/tools/check_i18n.py b/tools/check_i18n.py new file mode 100755 index 0000000..4c09d8a --- /dev/null +++ b/tools/check_i18n.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +"""Every key used in the code must exist in every catalogue. + +A UI that is half translated is worse than one that is not translated at all: +the operator cannot tell a missing string from a deliberate one. This walks the +sources for sp_t()/sp_td() keys and compares them with share/locale/*.msg. +""" +import pathlib +import re +import sys + +ROOT = pathlib.Path(__file__).resolve().parent.parent +KEY = re.compile(r'sp_td?\(\s*"([^"]+)"') + +def used_keys(): + keys = set() + for path in list(ROOT.glob("src/*.c")) + list(ROOT.glob("plugins/*/*.c")): + keys.update(KEY.findall(path.read_text(encoding="utf-8", errors="replace"))) + return keys + +def catalogue(path): + out = {} + for line in path.read_text(encoding="utf-8").splitlines(): + line = line.strip() + if not line or line.startswith("#") or "=" not in line: + continue + k, v = line.split("=", 1) + out[k.strip()] = v.strip() + return out + +def main(): + used = used_keys() + # role. is built by role_label(), never written literally + used.add("role.you"); used.add("role.agent"); used.add("role.thinking") + used.add("role.tool"); used.add("role.error"); used.add("role.shell") + used.add("role.splinter") + bad = 0 + cats = sorted(ROOT.glob("share/locale/*.msg")) + if not cats: + print("check_i18n: no catalogues in share/locale", file=sys.stderr) + return 1 + for path in cats: + cat = catalogue(path) + missing = sorted(k for k in used if k not in cat) + if missing: + bad = 1 + print(f"{path.name}: missing {len(missing)} key(s): " + + ", ".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) + if unused: + print("en.msg: keys nothing uses: " + ", ".join(unused), file=sys.stderr) + if bad == 0: + print(f"check_i18n: ok ({len(used)} keys in {len(cats)} catalogues)") + return bad + +if __name__ == "__main__": + sys.exit(main())