Files
splinter/plugins/focus/focus.c
T
Konrad 8fa3e9d7bb splinter: /goal as a command, and widgets that are actually alive
The goal is not configuration: it belongs to the conversation in front of you.
So it moved out of the config file and became a command.

- commands are an ABI now: sp_host_add_command / sp_host_command, so the UI
  never needs to know a plugin by name. A leading '/' is never sent to the
  model; an unknown command lists what exists.
- works in both modes: '/goal <text>' in the TUI and
  'splinter -p "/goal ..."' (exit 0 on success, 2 on an unknown command).
- focus registers /goal (set, show when empty of arguments) and /recap (force
  a reminder into the next turn, regardless of the five-turn cadence).

Fixed while testing it: the UI only repainted on a keypress, so a plugin widget
that ticks was frozen on screen - the clock showed the time from when the window
opened. The input read now times out after 500 ms and that timeout repaints the
sidebar and status line: clock, focus and cps are alive while you do nothing.

Fifteen test binaries green; test_focus covers the command path too: set, show,
force a recap outside the cadence, refuse an unknown command.
2026-11-07 20:50:58 +00:00

181 lines
5.1 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"
#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 */
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);
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, "kein Goal gesetzt "
"(/goal <text>)");
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 <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), "no goal (/goal <text>)");
else
snprintf(line, sizeof(line), "%.44s%s | %u rem",
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;
}