diff --git a/README.md b/README.md index c72e0dd..c43fcc1 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,14 @@ instead of being dropped. Curses is only ever touched by the UI thread. `focus` keeps the session's goal in front of the model every five turns, the way Claude Code's recap does — one inserted system message, never twice in a -turn, nothing when no goal is configured. +turn, nothing until a goal is set. `/goal ` sets it, `/recap` forces a +reminder, both in the TUI and with `-p`. + +## Slash commands + +A leading `/` never reaches the model: the UI asks the host, the host asks the +plugins. A plugin registers what it handles (`/goal`, `/recap` today); an +unknown command lists what exists instead of being sent as a prompt. Compaction is a plugin, not a switch in the loop. Two ship here: `lcm` (deterministic front-trim with an in-band system message saying what went) and diff --git a/docs/CONFIG.md b/docs/CONFIG.md index b48007f..03096f7 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -14,6 +14,12 @@ Format: one `key = value` per line, `#` starts a comment, blank lines are ignored. Not JSON, on purpose — this file gets edited by hand, and a missing comma must not be the reason the harness will not start. +## Not in the file: the goal + +The session goal is set at runtime with `/goal ` — in the TUI, or in a +one-shot run (`splinter -p "/goal ship the config"`). It belongs to the +conversation in front of you, not to a file that outlives it. + ## Precedence Command line, then the file, then the built-in default. An unknown key is an @@ -36,7 +42,6 @@ lies about what it does. | `max_bytes` | conversation budget in bytes, `0` = no limit | | `session_dir` | where session logs go (default `~/.splinter`) | | `system_prompt` | sent once at the start of a conversation | -| `goal` | what this session is for; exported as `SPLINTER_FOCUS_GOAL`, repeated by the `focus` plugin every five turns | ## Example: local model diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index 4cd4bcb..9b7d54d 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -170,10 +170,13 @@ reason the session exists is somewhere behind it. `focus` puts the goal back on the table. ``` -goal = splinter zu einem agent-harness machen, das hermes ersetzt +/goal splinter zu einem agent-harness machen, das hermes ersetzt ``` -The config key `goal` is exported to plugins as `SPLINTER_FOCUS_GOAL`; +`/goal ` sets it, `/goal` shows it, `/recap` forces a reminder into the +next turn. That works in the TUI and in a one-shot run +(`splinter -p "/goal ..."`); it is deliberately not a config key, because a goal +belongs to the conversation in front of you and not to a file that outlives it. `SPLINTER_FOCUS_EVERY` sets the interval (default 5 turns). It registers as a compactor and, on every fifth turn, inserts one system diff --git a/include/splinter_plugin.h b/include/splinter_plugin.h index ad7bb92..7d9c94a 100644 --- a/include/splinter_plugin.h +++ b/include/splinter_plugin.h @@ -54,6 +54,16 @@ struct sp_compactor { size_t budget_bytes, unsigned turn); }; +/* + * A command is what the operator types with a leading slash: /goal . + * The UI does not know any command by name; it asks the host, and the host asks + * the plugins. A plugin that wants one registers it here. + * + * Return 0 and put the reply in `out`; return -1 to say "not mine". + */ +typedef int (*sp_cmd_fn)(struct sp_host *, const char *args, struct sp_buf *out, + void *ctx); + struct sp_plugin { uint32_t abi; /* must equal SP_PLUGIN_ABI */ const char *name; @@ -76,6 +86,11 @@ int sp_host_widget_text(struct sp_host *, const char *widget, const char *text); int sp_host_note(struct sp_host *, const char *text); int sp_host_add_compactor(struct sp_host *, const struct sp_compactor *); +int sp_host_add_command(struct sp_host *, const char *name, sp_cmd_fn, + void *ctx); +int sp_host_command(struct sp_host *, const char *name, const char *args, + struct sp_buf *out); +size_t sp_host_command_help(struct sp_host *, struct sp_buf *out); int sp_host_add_transport(struct sp_host *, const char *name, struct sp_transport); struct sp_transport *sp_host_transport(struct sp_host *, const char *name); diff --git a/libsplinter.so b/libsplinter.so index 25c5c6d..16e68f1 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/plugins/focus/focus.c b/plugins/focus/focus.c index fd4bba2..2515933 100644 --- a/plugins/focus/focus.c +++ b/plugins/focus/focus.c @@ -34,6 +34,7 @@ static char goal[GOAL_MAX + 1]; static unsigned every = 5; static unsigned last_reminder; +static int force; /* /recap: remind on the next turn */ static unsigned reminders; static char widget_text[128]; @@ -49,7 +50,7 @@ focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget, (void)h; if (goal[0] == '\0' || conv == NULL || conv->n == 0) return 0; - if (turn == 0 || (turn % every) != 0) + if (!force && (turn == 0 || (turn % every) != 0)) return 0; if (turn == last_reminder) return 0; /* one reminder per turn, always */ @@ -71,12 +72,60 @@ focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget, return -1; last_reminder = turn; + force = 0; reminders++; snprintf(widget_text, sizeof(widget_text), "%u reminders, every %u turns", reminders, every); return 1; } +/* + * /goal set the goal at runtime, effective from the next request + * /goal show what it is now + * /recap force the reminder into the next turn + */ +static int +focus_command(struct sp_host *h, const char *args, struct sp_buf *out, + void *ctx) +{ + (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); + } + snprintf(goal, sizeof(goal), "%.*s", GOAL_MAX, args); + if (strlen(args) > GOAL_MAX) + snprintf(goal + GOAL_MAX - 24, 25, "...[gekuerzt]"); + reminders = 0; + last_reminder = 0; + force = 0; + /* publish now, so the sidebar does not wait for the next tick */ + { + char line[192]; + + snprintf(line, sizeof(line), "%.60s%s | 0 reminders", goal, + strlen(goal) > 60 ? "..." : ""); + (void)sp_host_widget_text(h, "focus", line); + } + return sp_buf_printf(out, "goal gesetzt: %s", goal); +} + +static int +recap_command(struct sp_host *h, const char *args, struct sp_buf *out, + void *ctx) +{ + (void)h; + (void)args; + (void)ctx; + if (goal[0] == '\0') + return sp_buf_puts(out, "kein Goal gesetzt (/goal )"); + force = 1; /* the next turn gets a reminder */ + return sp_buf_puts(out, "Erinnerung fuer den naechsten Turn eingeplant"); +} + static int focus_tick(struct sp_host *h, const char *name, void *ctx) { @@ -84,10 +133,10 @@ focus_tick(struct sp_host *h, const char *name, void *ctx) (void)ctx; if (goal[0] == '\0') - snprintf(line, sizeof(line), "no goal (set SPLINTER_FOCUS_GOAL)"); + snprintf(line, sizeof(line), "no goal (/goal )"); else - snprintf(line, sizeof(line), "%.60s%s | %u reminders", - goal, strlen(goal) > 60 ? "..." : "", reminders); + snprintf(line, sizeof(line), "%.44s%s | %u rem", + goal, strlen(goal) > 44 ? "..." : "", reminders); return sp_host_widget_text(h, name, line); } @@ -111,6 +160,10 @@ focus_init(struct sp_host *h) if (sp_host_add_compactor(h, &focus) != 0) return -1; + if (sp_host_add_command(h, "goal", focus_command, NULL) != 0) + return -1; + if (sp_host_add_command(h, "recap", recap_command, NULL) != 0) + return -1; if (sp_host_add_widget(h, &w) != 0) return -1; return 0; diff --git a/plugins/focus/focus.so b/plugins/focus/focus.so index 0be737e..cade179 100755 Binary files a/plugins/focus/focus.so and b/plugins/focus/focus.so differ diff --git a/src/config.c b/src/config.c index b52c73e..908db80 100644 --- a/src/config.c +++ b/src/config.c @@ -114,8 +114,6 @@ sp_config_load(struct sp_config *c, const char *path, struct sp_buf *err) set_str(c->system_prompt, sizeof(c->system_prompt), val); else if (!strcmp(key, "session_dir")) set_str(c->session_dir, sizeof(c->session_dir), val); - else if (!strcmp(key, "goal")) - set_str(c->goal, sizeof(c->goal), val); else if (!strcmp(key, "cli_cmd")) set_str(c->cli_cmd, sizeof(c->cli_cmd), val); else if (!strcmp(key, "steps")) @@ -207,10 +205,6 @@ sp_config_write_example(const char *path, struct sp_buf *err) "# Where session logs go (default ~/.splinter).\n" "#session_dir = ~/.splinter\n" "\n" - "# What this session is for. Plugins see it as SPLINTER_FOCUS_GOAL;\n" - "# the focus plugin repeats it every five turns.\n" - "#goal = \n" - "\n" "# Sent once at the start of a conversation.\n" "#system_prompt = Du bist knapp. Keine Floskeln.\n"); if (ferror(f)) { diff --git a/src/config.h b/src/config.h index acce7b4..0434f37 100644 --- a/src/config.h +++ b/src/config.h @@ -32,7 +32,6 @@ struct sp_config { char system_prompt[1024]; char session_dir[512]; char cli_cmd[512]; - char goal[1024]; /* exported to plugins as SPLINTER_FOCUS_GOAL */ char plugins[SP_CONFIG_MAX_PLUGINS][512]; size_t nplugins; size_t steps; /* 0: not set */ diff --git a/src/main.c b/src/main.c index 34776aa..34ed8ab 100644 --- a/src/main.c +++ b/src/main.c @@ -287,8 +287,6 @@ main(int argc, char **argv) /* the cli transport reads its command from the environment */ if (conf.cli_cmd[0] != '\0') setenv("SPLINTER_CLI_CMD", conf.cli_cmd, 0); - if (conf.goal[0] != '\0') - setenv("SPLINTER_FOCUS_GOAL", conf.goal, 0); /* a config means "this is my setup": start the chat */ if (prompt == NULL) chatmode = 1; @@ -429,6 +427,41 @@ main(int argc, char **argv) return rc; } + /* a leading slash is a harness command, also without a TUI */ + if (prompt != NULL && prompt[0] == '/' && host != NULL) { + char name[64], args[1536]; + struct sp_buf out; + const char *sp; + + sp = strchr(prompt, ' '); + if (sp == NULL) { + snprintf(name, sizeof(name), "%.63s", prompt + 1); + args[0] = '\0'; + } else { + size_t nlen = (size_t)(sp - (prompt + 1)); + + if (nlen >= sizeof(name)) + nlen = sizeof(name) - 1; + memcpy(name, prompt + 1, nlen); + name[nlen] = '\0'; + snprintf(args, sizeof(args), "%s", sp + 1); + } + if (sp_buf_init(&out) != 0) + return 3; + if (sp_host_command(host, name, args, &out) != 0) { + sp_buf_clear(&out); + sp_buf_printf(&out, "unbekanntes Kommando: /%s\n" + " verfuegbar: ", name); + (void)sp_host_command_help(host, &out); + printf("%s\n", out.p != NULL ? out.p : ""); + sp_buf_free(&out); + return 2; + } + printf("%s\n", out.p != NULL ? out.p : ""); + sp_buf_free(&out); + return 0; + } + cfg.stream = stdout_stream; cfg.stream_ctx = NULL; rc = sp_agent_run(&ag, prompt, &answer, &err); diff --git a/src/plugin.c b/src/plugin.c index ae9d26a..774a1c5 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -28,6 +28,7 @@ #define MAX_WIDGETS 32 #define MAX_COMPACTORS 8 #define MAX_TRANSPORTS 8 +#define MAX_COMMANDS 16 #define MAX_NOTE 2048 struct widget { @@ -57,6 +58,12 @@ struct sp_host { struct sp_transport tr; } transports[MAX_TRANSPORTS]; size_t ntransports; + struct { + char name[32]; + sp_cmd_fn fn; + void *ctx; + } commands[MAX_COMMANDS]; + size_t ncommands; pthread_mutex_t lock; /* guards note and the registry */ struct sp_session_info *sess; struct sp_agent *agent; @@ -199,6 +206,55 @@ sp_host_transport(struct sp_host *h, const char *name) return NULL; } +int +sp_host_add_command(struct sp_host *h, const char *name, sp_cmd_fn fn, + void *ctx) +{ + if (h == NULL || name == NULL || fn == NULL) + return -1; + if (h->ncommands == MAX_COMMANDS) + return -1; + snprintf(h->commands[h->ncommands].name, + sizeof(h->commands[h->ncommands].name), "%s", name); + h->commands[h->ncommands].fn = fn; + h->commands[h->ncommands].ctx = ctx; + h->ncommands++; + return 0; +} + +int +sp_host_command(struct sp_host *h, const char *name, const char *args, + struct sp_buf *out) +{ + size_t i; + int rc = -1; + + if (h == NULL || name == NULL) + return -1; + for (i = 0; i < h->ncommands; i++) { + if (strcmp(h->commands[i].name, name) != 0) + continue; + rc = h->commands[i].fn(h, args != NULL ? args : "", + out, h->commands[i].ctx); + if (rc == 0) + return 0; + } + sp_buf_printf(out, "unknown command: /%s", name); + return -1; +} + +size_t +sp_host_command_help(struct sp_host *h, struct sp_buf *out) +{ + size_t i; + + if (h == NULL) + return 0; + for (i = 0; i < h->ncommands; i++) + (void)sp_buf_printf(out, "/%s ", h->commands[i].name); + return h->ncommands; +} + int sp_host_compact(struct sp_host *h, struct sp_conv *conv, size_t budget) { diff --git a/src/ui.c b/src/ui.c index a0922b8..86bdd65 100644 --- a/src/ui.c +++ b/src/ui.c @@ -603,8 +603,25 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, wmove(ui_input, 0, 2); wrefresh(ui_input); + /* + * A widget that ticks is useless if the screen only refreshes + * when a key is pressed. The input read times out every 500 ms + * and that timeout repaints the sidebar and the status line -- + * cheap, and it makes clock/focus/cps actually alive. + */ + wtimeout(ui_input, 500); for (;;) { ch = wgetch(ui_input); + if (ch == ERR) { + draw_sidebar(&s, ag->sess != NULL ? + ag->sess->id : NULL, + ag->sess != NULL ? ag->sess->dir : NULL, + host); + draw_status(&s); + wnoutrefresh(ui_input); + doupdate(); + continue; + } if (ch == '\n' || ch == '\r' || ch == KEY_ENTER) break; if (ch == 4) { @@ -647,6 +664,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, } } input[len] = '\0'; + wtimeout(ui_input, -1); } if (s.quit) @@ -654,6 +672,44 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, if (input[0] == '\0') continue; + /* + * A leading slash is a command for the harness, not a prompt: it + * never reaches the model, and its reply is shown as the harness + * speaking. Unknown commands list what exists. + */ + if (input[0] == '/' && host != NULL) { + char name[64], args[1536], outbuf[2048]; + struct sp_buf out; + const char *sp; + + sp = strchr(input, ' '); + if (sp == NULL) { + snprintf(name, sizeof(name), "%.63s", input + 1); + args[0] = '\0'; + } else { + size_t nlen = (size_t)(sp - (input + 1)); + + if (nlen >= sizeof(name)) + nlen = sizeof(name) - 1; + memcpy(name, input + 1, nlen); + name[nlen] = '\0'; + snprintf(args, sizeof(args), "%s", sp + 1); + } + if (sp_buf_init(&out) != 0) + continue; + if (sp_host_command(host, name, args, &out) != 0) { + sp_buf_clear(&out); + sp_buf_printf(&out, "unbekanntes Kommando: /%s\n" + " verfuegbar: ", name); + (void)sp_host_command_help(host, &out); + } + snprintf(outbuf, sizeof(outbuf), "%s", + out.p != NULL ? out.p : ""); + sp_buf_free(&out); + add_entry(&s, "splinter", outbuf); + continue; + } + add_entry(&s, "you", input); draw_history(&s); draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL, diff --git a/tests/test_config b/tests/test_config index 4603ea7..0a93070 100755 Binary files a/tests/test_config and b/tests/test_config differ diff --git a/tests/test_focus b/tests/test_focus index a4349de..dbda335 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 af9ca6b..b897b41 100644 --- a/tests/test_focus.c +++ b/tests/test_focus.c @@ -109,6 +109,44 @@ main(void) CHECK(sp_host_compact(host, &conv, 1) == 1); CHECK(count_focus(&conv) == 2); + /* ---- /goal sets it at runtime, /recap forces the next reminder ---- */ + { + struct sp_buf out; + + CHECK(sp_buf_init(&out) == 0); + + sp_conv_free(&conv); + sp_conv_init(&conv); + CHECK(sp_conv_push(&conv, "user", NULL, "turn fuenf") == 0); + + sp_buf_clear(&out); + CHECK(sp_host_command(host, "goal", "", &out) == 0); + CHECK(strstr(out.p, "Ship the config file") != NULL); + + sp_buf_clear(&out); + CHECK(sp_host_command(host, "goal", "neues ziel", &out) == 0); + CHECK(strstr(out.p, "neues ziel") != NULL); + + /* a forced recap lands on the next turn regardless of the counter */ + sp_buf_clear(&out); + CHECK(sp_host_command(host, "recap", "", &out) == 0); + sp_host_turn(host); + { + int before = count_focus(&conv); + + CHECK(sp_host_compact(host, &conv, 1) == 1); + CHECK(count_focus(&conv) == before + 1); + CHECK(strstr(conv.v[conv.n - 1].content, "turn fuenf") != NULL); + } + + /* an unknown command is refused and says so */ + sp_buf_clear(&out); + CHECK(sp_host_command(host, "gibtsnicht", "", &out) == -1); + CHECK(strstr(out.p, "unknown command") != NULL); + + sp_buf_free(&out); + } + sp_conv_free(&conv); sp_host_free(host); sp_buf_free(&err);