Files
splinter/plugins/focus/focus.c
T
Konrad 554b4c97b8 splinter: tool calls with arguments, newline-aware rows, complete i18n
Four things, three of them reported by the operator.

1. Rows are counted from the real line structure. The old arithmetic
   (strlen/cols) assumed one long line; a model's reasoning is full of '\n', so
   the true height was far larger than the estimate and the text painted over
   the answer below it - 'it just goes anywhere on the screen'. text_rows() and
   row_offset() split on newlines first, then wrap. entry_lines() now measures
   the string that is actually drawn ('role: text'), which also fixes the last
   character of an entry being cut off (a truncated code fence).

2. Tool calls are visible: the agent's tool_begin hook fires before each call,
   the UI drains the list under its lock and writes 'werkzeug: run
   {"command":"echo a"}' into the transcript; the one at the top of the live
   area shows the call while it runs. The reasoning entry is inserted before
   those lines, so the transcript reads du, denkt, werkzeug, modell.

3. The chat pane has a black background, and the status line sits between the
   pane and the prompt, the way Hermes shows it.

4. i18n is complete: every remaining literal in ui.c, main.c and the plugins
   goes through the catalogue, including widget texts and format strings.
   sp_td() carries a compiled-in English default, so a missing catalogue can
   never drop a printf placeholder. tools/check_i18n.py runs in make check and
   fails when a key used in the code is missing from any catalogue, or when a
   catalogue key is used nowhere.

Seventeen test binaries green.
2026-11-08 08:11:09 +00:00

188 lines
5.3 KiB
C

/*
* splinter -- focus: keep the goal in front of the model.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Long conversations drift. Ten turns in, the model is deep in a detail and the
* thing the session was started for is somewhere behind it. This plugin puts
* the goal back on the table every five turns - not as a new instruction that
* overrides the user, but as one system message right before the turn, so it
* can be read as a reminder and dismissed if the user has changed direction.
*
* The goal comes from the operator, never from the model:
*
* SPLINTER_FOCUS_GOAL text, exported from the config key `goal`
* SPLINTER_FOCUS_EVERY turns between reminders, default 5
*
* A goal longer than 1000 characters is truncated with a marker: a reminder
* that eats the budget it is meant to protect would be self-defeating.
*
* Position matters. The reminder is inserted before the newest user message,
* not appended after it: the model then reads goal -> request and answers the
* request, instead of reading the request and treating the goal as a reply.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "i18n.h"
#define GOAL_MAX 1000
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];
static int
focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
unsigned turn)
{
char text[GOAL_MAX + 512];
struct sp_msg m;
size_t at;
(void)budget;
(void)h;
if (goal[0] == '\0' || conv == NULL || conv->n == 0)
return 0;
if (!force && (turn == 0 || (turn % every) != 0))
return 0;
if (turn == last_reminder)
return 0; /* one reminder per turn, always */
{
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";
m.content = text;
/* before the newest message, so the goal is read first */
at = conv->n - 1;
while (at > 0 && conv->v[at].role != NULL &&
strcmp(conv->v[at].role, "user") != 0)
at--;
if (sp_conv_insert(conv, at, &m) != 0)
return -1;
last_reminder = turn;
force = 0;
reminders++;
snprintf(widget_text, sizeof(widget_text), "%u reminders, every %u turns",
reminders, every);
return 1;
}
/*
* /goal <text> 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, sp_td("focus.none", "no goal set (/goal <text>)"));
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)
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", goal,
strlen(goal) > 60 ? "..." : "");
(void)sp_host_widget_text(h, "focus", line);
}
return sp_buf_printf(out, sp_td("focus.set", "goal set: %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, sp_td("focus.none", "no goal set (/goal <text>)"));
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)
{
char line[192];
(void)ctx;
if (goal[0] == '\0')
snprintf(line, sizeof(line), "%s", sp_td("focus.widget_none",
"no goal (/goal <text>)"));
else
snprintf(line, sizeof(line), "%.44s%s | %u",
goal, strlen(goal) > 44 ? "..." : "", reminders);
return sp_host_widget_text(h, name, line);
}
static const struct sp_compactor focus = { "focus", focus_compact };
static int
focus_init(struct sp_host *h)
{
static const struct sp_widget w = { "focus", focus_tick, NULL, NULL, 1000 };
const char *g = getenv("SPLINTER_FOCUS_GOAL");
const char *e = getenv("SPLINTER_FOCUS_EVERY");
memset(goal, 0, sizeof(goal));
if (g != NULL) {
snprintf(goal, sizeof(goal), "%.*s", GOAL_MAX, g);
if (strlen(g) > GOAL_MAX)
snprintf(goal + GOAL_MAX - 24, 25, "...[gekuerzt]");
}
if (e != NULL && atoi(e) > 0 && atoi(e) < 1000)
every = (unsigned)atoi(e);
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;
}
static const struct sp_plugin focus_plugin = {
SP_PLUGIN_ABI, "focus", "1.0.0", focus_init, NULL, NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &focus_plugin;
}