Files
splinter/plugins/recap/recap.c
T
lotherkandClaude Sonnet 5.5 4af38ef91b recap: /recap <text> anchors the operator's note into the conversation every N turns
A system message before the newest user message, on the next request and
then every N turns (default 5; plugin recap { every }, SPLINTER_RECAP_EVERY,
/recap every N). Stored per session in <id>.recap, back on resume.
make check passes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 09:49:09 +00:00

229 lines
5.5 KiB
C

/*
* splinter -- the recap.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* /recap <text> set the recap; it goes into the conversation on the
* next request and then every N turns
* /recap show it
* /recap every N change the interval (0 = never repeat)
* /recap off forget it
*
* The recap is the operator's own text, put into the conversation as a system
* message right before the newest user message. It is a real message: it stays
* in the history, so a long session keeps meeting it and not only the
* system prompt that sank under the conversation.
*
* Interval: plugin recap { every = 5 }, SPLINTER_RECAP_EVERY, or /recap every.
* Default 5. The recap is kept per session in <session dir>/<id>.recap and
* comes back on resume.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "splinter_plugin.h"
#define RECAP_MAX 1500
#define EVERY_MAX 1000
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static char text[RECAP_MAX + 1];
static unsigned every = 5;
static unsigned last_turn = (unsigned)-1;
static int fresh; /* set, and not yet in the conversation */
static unsigned sent;
static char session_id[64];
static int
recap_path(struct sp_host *h, char *path, size_t n)
{
const char *dir = sp_host_session_dir(h);
const char *id = sp_host_session_id(h);
if (dir == NULL || id == NULL || *id == '\0' || strchr(id, '/') != NULL)
return -1;
return snprintf(path, n, "%s/%s.recap", dir, id) < (int)n ? 0 : -1;
}
static void
recap_save(struct sp_host *h)
{
char path[1024];
FILE *f;
if (recap_path(h, path, sizeof(path)) != 0)
return;
if (text[0] == '\0') {
(void)remove(path);
return;
}
if ((f = fopen(path, "w")) == NULL)
return;
(void)fputs(text, f);
(void)fclose(f);
}
/* The statics serve every session; reload when the session changes. */
static void
recap_sync(struct sp_host *h)
{
const char *id = sp_host_session_id(h);
char path[1024];
FILE *f;
size_t n;
if (id == NULL || *id == '\0' || strcmp(session_id, id) == 0)
return;
snprintf(session_id, sizeof(session_id), "%s", id);
text[0] = '\0';
fresh = 0;
last_turn = (unsigned)-1;
if (recap_path(h, path, sizeof(path)) != 0 ||
(f = fopen(path, "r")) == NULL)
return;
n = fread(text, 1, RECAP_MAX, f);
text[n] = '\0';
(void)fclose(f);
}
static int
recap_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
unsigned turn)
{
char msg[RECAP_MAX + 64];
struct sp_msg m;
size_t at, i;
int due;
(void)budget;
pthread_mutex_lock(&lock);
recap_sync(h);
if (conv == NULL || conv->n == 0 || text[0] == '\0') {
pthread_mutex_unlock(&lock);
return 0;
}
due = fresh || (every != 0 && turn != 0 && turn % every == 0);
if (!due || turn == last_turn) {
pthread_mutex_unlock(&lock);
return 0;
}
last_turn = turn;
fresh = 0;
snprintf(msg, sizeof(msg), "[recap]\n%s\n", text);
pthread_mutex_unlock(&lock);
at = conv->n;
for (i = conv->n; i > 0; i--) {
if (conv->v[i - 1].role != NULL &&
strcmp(conv->v[i - 1].role, "user") == 0) {
at = i - 1;
break;
}
}
memset(&m, 0, sizeof(m));
m.role = "system";
m.content = msg;
if (sp_conv_add(conv, &m) != 0) {
(void)sp_host_note(h, "recap: cannot add the message");
return -1;
}
if (at < conv->n - 1) {
struct sp_msg moved = conv->v[conv->n - 1];
memmove(&conv->v[at + 1], &conv->v[at],
sizeof(conv->v[0]) * (conv->n - 1 - at));
conv->v[at] = moved;
}
pthread_mutex_lock(&lock);
sent++;
pthread_mutex_unlock(&lock);
return 1;
}
static int
recap_command(struct sp_host *h, const char *args, struct sp_buf *out,
void *ctx)
{
int rc;
(void)ctx;
pthread_mutex_lock(&lock);
recap_sync(h);
while (args != NULL && (*args == ' ' || *args == '\t'))
args++;
if (args == NULL || *args == '\0') {
if (text[0] == '\0')
rc = sp_buf_puts(out, "no recap set (/recap <text>)");
else
rc = sp_buf_printf(out,
"recap: %s (every %u turns, %u sent)", text, every,
sent);
} else if (strcmp(args, "off") == 0) {
text[0] = '\0';
fresh = 0;
recap_save(h);
rc = sp_buf_puts(out, "recap removed");
} else if (strncmp(args, "every ", 6) == 0) {
char *end;
unsigned long v = strtoul(args + 6, &end, 10);
while (*end == ' ')
end++;
if (end == args + 6 || *end != '\0' || v > EVERY_MAX)
rc = sp_buf_printf(out, "every: 0..%d", EVERY_MAX);
else {
every = (unsigned)v;
rc = sp_buf_printf(out, "recap every %u turns", every);
}
} else {
snprintf(text, sizeof(text), "%.*s", RECAP_MAX, args);
fresh = 1;
sent = 0;
recap_save(h);
rc = sp_buf_printf(out, "recap set (every %u turns)", every);
}
pthread_mutex_unlock(&lock);
return rc;
}
static const struct sp_compactor compactor = { "recap", recap_compact };
static int
recap_init(struct sp_host *h)
{
const char *v = sp_host_plugin_config(h, "recap", "every");
const char *env = getenv("SPLINTER_RECAP_EVERY");
if ((v == NULL || *v == '\0') && env != NULL)
v = env;
text[0] = '\0';
session_id[0] = '\0';
every = 5;
if (v != NULL && *v != '\0') {
unsigned long n = strtoul(v, NULL, 10);
if (n <= EVERY_MAX)
every = (unsigned)n;
}
if (sp_host_add_compactor(h, &compactor) != 0 ||
sp_host_add_command(h, "recap", recap_command, NULL) != 0) {
(void)sp_host_note(h, "recap: cannot register");
return -1;
}
return 0;
}
static const struct sp_plugin plugin = {
SP_PLUGIN_ABI, "recap", "1.0", recap_init, NULL, NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &plugin;
}