splinter: delegation to configured models, chosen by splinter
A delegate is a line in the config - id, what it is good at, tags, endpoint, model. No profiles: a profile is a directory of files, and this needs a line you can read at 2 a.m. The delegate tool takes a task and optional tags. The pick is code, not a guess: requested tag 5, tag in the task 3, description word 1, ties to the earlier entry. The observation opens with the choice and the reason, and --choose prints the same decision without running anything: coder 7 task mentions 'code' quick 5 task mentions 'chat' coder 3 task mentions 'refactor' The child is splinter itself in one-shot mode - same tools, own context, own endpoint and model. Classed destructive, because it runs a whole agent; the operator approves the delegation once. test_delegate pins the decisions, including the two cases that must not pick anything: no matching tag and no delegates at all.
This commit is contained in:
1 parent
e128d679e4
commit
781e7eb514
11 files changed
+592
-3
No files matched your search
@@ -23,14 +23,14 @@ CURSES_A = $(CURSES_DIR)/libcurses/libcurses.a \
|
||||
LIB = libsplinter.so
|
||||
LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \
|
||||
src/json.o src/sse.o src/openai.o src/transport_openai.o src/builtin.o \
|
||||
src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/config.o src/plugin.o
|
||||
src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/config.o src/plugin.o
|
||||
BIN = splinter
|
||||
BINOBJ = src/main.o src/ui.o
|
||||
PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
plugins/cli/cli.so plugins/focus/focus.so plugins/mcp/mcp.so
|
||||
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
|
||||
tests/test_transport tests/test_agent tests/test_openai \
|
||||
tests/test_theme tests/test_plugin tests/test_builtin tests/test_ascii tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor
|
||||
tests/test_theme tests/test_plugin tests/test_delegate tests/test_builtin tests/test_ascii tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor
|
||||
|
||||
RPATH = -Wl,-rpath,'$$ORIGIN'
|
||||
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
|
||||
@@ -96,6 +96,7 @@ src/theme.o: src/theme.h src/palettes.h include/splinter.h
|
||||
src/palettes.o: src/palettes.h
|
||||
src/decomp.o: src/decomp.h
|
||||
src/i18n.o: src/i18n.h include/splinter.h
|
||||
src/delegate.o: src/config.h include/splinter.h
|
||||
src/config.o: src/config.h include/splinter.h
|
||||
src/plugin.o: include/splinter.h include/splinter_plugin.h
|
||||
src/ui.o: src/ui.h src/theme.h src/decomp.h src/i18n.h include/splinter.h include/splinter_plugin.h
|
||||
@@ -119,6 +120,8 @@ tests/test_theme: tests/test_theme.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_theme.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_focus: tests/test_focus.c $(LIB) $(PLUGIN)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_focus.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_delegate: tests/test_delegate.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_delegate.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_builtin: tests/test_builtin.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_builtin.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_i18n: tests/test_i18n.c $(LIB)
|
||||
|
||||
@@ -184,6 +184,29 @@ key that `en.msg` has.
|
||||
|
||||
Ships with `en` and `de`. Plugins translate their own strings with `sp_t()`.
|
||||
|
||||
## Delegation
|
||||
|
||||
Work can be handed to another model. A delegate is one line in the config - no
|
||||
profiles, no directories, nothing to keep in sync:
|
||||
|
||||
delegate = coder | C99, POSIX, kennt den Quellbaum | tags=code,c99,review |
|
||||
endpoint=http://kontron.local:8080/v1/chat/completions |
|
||||
model=Qwen3.6-35B
|
||||
delegate = quick | kurze Antworten, Chat | tags=chat,cheap |
|
||||
endpoint=... | model=...
|
||||
|
||||
The `delegate` tool takes a task and, optionally, tags. splinter picks the model
|
||||
itself: a requested tag counts 5, a tag found in the task 3, a word of the
|
||||
description 1. The observation starts with the choice and the reason, so a wrong
|
||||
pick is visible instead of mysterious:
|
||||
|
||||
[delegate coder, score 7: task mentions 'code']
|
||||
|
||||
`--choose "text"` prints that decision without running anything:
|
||||
`coder 7 task mentions 'code'`. The child is splinter in one-shot mode with its
|
||||
own context; the tool is classed destructive, so the operator approves the
|
||||
delegation once.
|
||||
|
||||
## Resuming
|
||||
|
||||
`--resume` loads the newest session in the session directory back into the
|
||||
|
||||
@@ -250,6 +250,11 @@ int sp_agent_run(struct sp_agent *, const char *user_text,
|
||||
|
||||
/* builtin.c */
|
||||
int sp_builtin_register(struct sp_registry *);
|
||||
struct sp_config;
|
||||
int sp_delegate_register(struct sp_registry *, const struct sp_config *);
|
||||
int sp_delegate_choose(const struct sp_config *, const char *task,
|
||||
const char *tags, char *id, size_t cap, char *why, size_t whylen,
|
||||
int *score);
|
||||
|
||||
/* transport_openai.c -- OpenAI-compatible chat completions, via curl(1) */
|
||||
/* The turn's cancellation flag; set by the UI before a turn, cleared after. */
|
||||
|
||||
Binary file not shown.
@@ -63,6 +63,72 @@ set_str(char *dst, size_t len, const char *src)
|
||||
snprintf(dst, len, "%s", src);
|
||||
}
|
||||
|
||||
/*
|
||||
* delegate = id | what it is good at | tags=a,b | endpoint=... | model=...
|
||||
*
|
||||
* The bar is that every field is named: a positional format with five slots is
|
||||
* how a config file becomes a guessing game.
|
||||
*/
|
||||
static int
|
||||
parse_delegate(struct sp_delegate *d, const char *val, struct sp_buf *err,
|
||||
const char *path, unsigned lineno)
|
||||
{
|
||||
char *copy, *field, *save = NULL;
|
||||
int first = 1;
|
||||
|
||||
memset(d, 0, sizeof(*d));
|
||||
copy = strdup(val);
|
||||
if (copy == NULL)
|
||||
return -1;
|
||||
for (field = strtok_r(copy, "|", &save); field != NULL;
|
||||
field = strtok_r(NULL, "|", &save)) {
|
||||
char *s = field;
|
||||
char *end;
|
||||
|
||||
while (*s == ' ' || *s == '\t')
|
||||
s++;
|
||||
end = s + strlen(s);
|
||||
while (end > s && (end[-1] == ' ' || end[-1] == '\t'))
|
||||
*--end = '\0';
|
||||
if (*s == '\0')
|
||||
continue;
|
||||
if (first && strchr(s, '=') == NULL) {
|
||||
snprintf(d->id, sizeof(d->id), "%s", s);
|
||||
first = 0;
|
||||
continue;
|
||||
}
|
||||
if (!strncmp(s, "tags=", 5))
|
||||
snprintf(d->tags, sizeof(d->tags), "%s", s + 5);
|
||||
else if (!strncmp(s, "endpoint=", 9))
|
||||
snprintf(d->endpoint, sizeof(d->endpoint), "%s", s + 9);
|
||||
else if (!strncmp(s, "model=", 6))
|
||||
snprintf(d->model, sizeof(d->model), "%s", s + 6);
|
||||
else if (!strncmp(s, "key_env=", 8))
|
||||
snprintf(d->key_env, sizeof(d->key_env), "%s", s + 8);
|
||||
else if (strchr(s, '=') == NULL) {
|
||||
snprintf(d->about, sizeof(d->about), "%s", s);
|
||||
first = 0;
|
||||
} else {
|
||||
sp_buf_printf(err, "config: %s:%u: unknown delegate "
|
||||
"field '%s'", path, lineno, s);
|
||||
free(copy);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
free(copy);
|
||||
if (d->id[0] == '\0') {
|
||||
sp_buf_printf(err, "config: %s:%u: delegate without an id",
|
||||
path, lineno);
|
||||
return -1;
|
||||
}
|
||||
if (d->endpoint[0] == '\0' || d->model[0] == '\0') {
|
||||
sp_buf_printf(err, "config: %s:%u: delegate '%s' needs endpoint "
|
||||
"and model", path, lineno, d->id);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_config_load(struct sp_config *c, const char *path, struct sp_buf *err)
|
||||
{
|
||||
@@ -125,6 +191,21 @@ sp_config_load(struct sp_config *c, const char *path, struct sp_buf *err)
|
||||
else if (!strcmp(key, "autonomous"))
|
||||
c->autonomous = (!strcmp(val, "1") ||
|
||||
!strcmp(val, "true") || !strcmp(val, "yes")) ? 1 : 0;
|
||||
else if (!strcmp(key, "delegate")) {
|
||||
if (c->ndelegates == SP_CONFIG_MAX_DELEGATES) {
|
||||
sp_buf_printf(err, "config: %s:%u: more than %d "
|
||||
"delegates", path, lineno,
|
||||
SP_CONFIG_MAX_DELEGATES);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
if (parse_delegate(&c->delegate[c->ndelegates], val,
|
||||
err, path, lineno) != 0) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
c->ndelegates++;
|
||||
}
|
||||
else if (!strcmp(key, "plugin")) {
|
||||
if (c->nplugins == SP_CONFIG_MAX_PLUGINS) {
|
||||
sp_buf_printf(err, "config: %s:%u: more than %d "
|
||||
|
||||
@@ -23,7 +23,25 @@
|
||||
|
||||
#define SP_CONFIG_MAX_PLUGINS 8
|
||||
|
||||
/*
|
||||
* A delegate is another model splinter may hand work to: a name, what it is
|
||||
* good at, and where it lives. No profiles - a profile is a directory with a
|
||||
* dozen files, and all this needs is a line you can read at 2 a.m.
|
||||
*/
|
||||
#define SP_CONFIG_MAX_DELEGATES 8
|
||||
|
||||
struct sp_delegate {
|
||||
char id[32];
|
||||
char about[128]; /* what it is good at, in words */
|
||||
char tags[128]; /* comma separated, matched against the task */
|
||||
char endpoint[512];
|
||||
char model[256];
|
||||
char key_env[64];
|
||||
};
|
||||
|
||||
struct sp_config {
|
||||
struct sp_delegate delegate[SP_CONFIG_MAX_DELEGATES];
|
||||
size_t ndelegates;
|
||||
char endpoint[512];
|
||||
char model[256];
|
||||
char key_env[64];
|
||||
|
||||
+347
@@ -0,0 +1,347 @@
|
||||
/*
|
||||
* splinter -- delegation to other models.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* The harness can hand work to another model. Which one is decided here, in
|
||||
* code, from the tags and the description configured for each delegate - not by
|
||||
* guessing and not by asking a model to pick itself a helper.
|
||||
*
|
||||
* delegate = coder | C99 und POSIX, kennt den Baum | tags=code,c99 |
|
||||
* endpoint=http://kontron.local:8080/v1/chat/completions |
|
||||
* model=Qwen3.6-35B
|
||||
*
|
||||
* The child is splinter itself in one-shot mode: same tools, same rules, own
|
||||
* context. Its answer comes back as the observation, with the choice and the
|
||||
* reason in front of it, so a wrong pick is visible instead of mysterious.
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "mcp.h"
|
||||
#include "config.h"
|
||||
|
||||
#define DELEGATE_TIMEOUT_MS 300000
|
||||
#define DELEGATE_MAX_BYTES 65536
|
||||
|
||||
struct del_ctx {
|
||||
const struct sp_config *cfg;
|
||||
char self[512];
|
||||
};
|
||||
|
||||
static void
|
||||
lower(char *dst, size_t cap, const char *src)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; src[i] != '\0' && i + 1 < cap; i++)
|
||||
dst[i] = (char)tolower((unsigned char)src[i]);
|
||||
dst[i] = '\0';
|
||||
}
|
||||
|
||||
/*
|
||||
* Pick the delegate that fits the task.
|
||||
*
|
||||
* A requested tag outweighs a tag found in the task, which outweighs a word of
|
||||
* the description. Ties go to the earlier entry: predictable beats clever when
|
||||
* someone is reading the log at night.
|
||||
*/
|
||||
static const struct sp_delegate *
|
||||
choose(const struct sp_config *cfg, const char *task, const char *wanted,
|
||||
int *score_out, char *why, size_t whylen)
|
||||
{
|
||||
const struct sp_delegate *best = NULL;
|
||||
char task_lc[1024], want_lc[256];
|
||||
int best_score = 0;
|
||||
size_t i;
|
||||
|
||||
lower(task_lc, sizeof(task_lc), task != NULL ? task : "");
|
||||
lower(want_lc, sizeof(want_lc), wanted != NULL ? wanted : "");
|
||||
|
||||
for (i = 0; i < cfg->ndelegates; i++) {
|
||||
const struct sp_delegate *d = &cfg->delegate[i];
|
||||
char tags[160], about_lc[160], *t, *save = NULL;
|
||||
int score = 0;
|
||||
char reason[192] = "";
|
||||
|
||||
snprintf(tags, sizeof(tags), "%s", d->tags);
|
||||
for (t = strtok_r(tags, ",", &save); t != NULL;
|
||||
t = strtok_r(NULL, ",", &save)) {
|
||||
char tag[64];
|
||||
|
||||
lower(tag, sizeof(tag), t);
|
||||
while (*tag == ' ')
|
||||
memmove(tag, tag + 1, strlen(tag));
|
||||
if (tag[0] == '\0')
|
||||
continue;
|
||||
if (want_lc[0] != '\0' && strstr(want_lc, tag) != NULL) {
|
||||
score += 5;
|
||||
snprintf(reason, sizeof(reason),
|
||||
"requested tag '%s'", tag);
|
||||
} else if (strstr(task_lc, tag) != NULL) {
|
||||
score += 3;
|
||||
if (reason[0] == '\0')
|
||||
snprintf(reason, sizeof(reason),
|
||||
"task mentions '%s'", tag);
|
||||
}
|
||||
}
|
||||
lower(about_lc, sizeof(about_lc), d->about);
|
||||
{
|
||||
char *w, *wsave = NULL;
|
||||
|
||||
for (w = strtok_r(about_lc, " ,.;", &wsave); w != NULL;
|
||||
w = strtok_r(NULL, " ,.;", &wsave)) {
|
||||
if (strlen(w) < 4)
|
||||
continue;
|
||||
if (strstr(task_lc, w) == NULL)
|
||||
continue;
|
||||
score += 1;
|
||||
if (reason[0] == '\0')
|
||||
snprintf(reason, sizeof(reason),
|
||||
"description matches '%s'", w);
|
||||
}
|
||||
}
|
||||
if (score > best_score) {
|
||||
best_score = score;
|
||||
best = d;
|
||||
snprintf(why, whylen, "%s", reason);
|
||||
}
|
||||
}
|
||||
if (score_out != NULL)
|
||||
*score_out = best_score;
|
||||
return best;
|
||||
}
|
||||
|
||||
/* Run the child and collect its answer. */
|
||||
static int
|
||||
run_child(const struct del_ctx *c, const struct sp_delegate *d,
|
||||
const char *task, struct sp_buf *out, struct sp_buf *err)
|
||||
{
|
||||
int fds[2];
|
||||
pid_t child;
|
||||
const char *key_env = d->key_env[0] != '\0' ? d->key_env : NULL;
|
||||
char maxbuf[32];
|
||||
|
||||
if (pipe(fds) != 0) {
|
||||
sp_buf_printf(err, "delegate: pipe: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
child = fork();
|
||||
if (child < 0) {
|
||||
sp_buf_printf(err, "delegate: fork: %s", strerror(errno));
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
return -1;
|
||||
}
|
||||
if (child == 0) {
|
||||
char *argv[16];
|
||||
int n = 0;
|
||||
|
||||
(void)dup2(fds[1], STDOUT_FILENO);
|
||||
(void)dup2(fds[1], STDERR_FILENO);
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
snprintf(maxbuf, sizeof(maxbuf), "%d", DELEGATE_MAX_BYTES);
|
||||
argv[n++] = (char *)c->self;
|
||||
argv[n++] = "--no-config";
|
||||
argv[n++] = "-p";
|
||||
argv[n++] = (char *)task;
|
||||
argv[n++] = "--endpoint";
|
||||
argv[n++] = (char *)d->endpoint;
|
||||
argv[n++] = "--model";
|
||||
argv[n++] = (char *)d->model;
|
||||
argv[n++] = "--max-bytes";
|
||||
argv[n++] = maxbuf;
|
||||
argv[n++] = "--autonomous";
|
||||
if (key_env != NULL) {
|
||||
argv[n++] = "--api-key-env";
|
||||
argv[n++] = (char *)key_env;
|
||||
}
|
||||
argv[n] = NULL;
|
||||
execv(c->self, argv);
|
||||
_exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
{
|
||||
struct pollfd pfd;
|
||||
char chunk[4096];
|
||||
ssize_t got;
|
||||
int done = 0;
|
||||
|
||||
while (!done) {
|
||||
pfd.fd = fds[0];
|
||||
pfd.events = POLLIN;
|
||||
if (poll(&pfd, 1, DELEGATE_TIMEOUT_MS) <= 0) {
|
||||
(void)kill(child, SIGKILL);
|
||||
(void)waitpid(child, NULL, 0);
|
||||
close(fds[0]);
|
||||
sp_buf_printf(err, "delegate: '%s' timed out",
|
||||
d->id);
|
||||
return -1;
|
||||
}
|
||||
got = read(fds[0], chunk, sizeof(chunk));
|
||||
if (got <= 0)
|
||||
break;
|
||||
if (out->len + (size_t)got > DELEGATE_MAX_BYTES) {
|
||||
size_t room = DELEGATE_MAX_BYTES - out->len;
|
||||
|
||||
if (room > 0)
|
||||
(void)sp_buf_add(out, chunk, room);
|
||||
sp_buf_puts(out, "\n[delegate output truncated]\n");
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
if (sp_buf_add(out, chunk, (size_t)got) != 0)
|
||||
break;
|
||||
}
|
||||
close(fds[0]);
|
||||
{
|
||||
int st = 0;
|
||||
|
||||
(void)waitpid(child, &st, 0);
|
||||
if (done || (WIFEXITED(st) && WEXITSTATUS(st) != 0)) {
|
||||
sp_buf_printf(err, "delegate: '%s' exited %d",
|
||||
d->id, WIFEXITED(st) ? WEXITSTATUS(st) : -1);
|
||||
return done ? 0 : -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_delegate(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
struct del_ctx *c = ctx;
|
||||
struct sp_buf keep, tbuf, reply, err;
|
||||
const char *task, *wanted;
|
||||
const struct sp_delegate *d;
|
||||
char why[192] = "";
|
||||
int score = 0;
|
||||
|
||||
if (sp_buf_init(&keep) != 0 || sp_buf_init(&tbuf) != 0)
|
||||
return -1;
|
||||
task = NULL;
|
||||
wanted = NULL;
|
||||
if (args != NULL && args[0] == '{') {
|
||||
mcp_json *v = mcp_parse(args);
|
||||
|
||||
if (v != NULL) {
|
||||
const char *t = mcp_arg_string(v, "task");
|
||||
const char *g = mcp_arg_string(v, "tags");
|
||||
|
||||
if (t != NULL)
|
||||
(void)sp_buf_puts(&keep, t);
|
||||
if (g != NULL)
|
||||
(void)sp_buf_puts(&tbuf, g);
|
||||
mcp_json_free(v);
|
||||
task = keep.p;
|
||||
wanted = tbuf.p;
|
||||
}
|
||||
} else
|
||||
task = args;
|
||||
|
||||
if (task == NULL || *task == '\0') {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&tbuf);
|
||||
return sp_buf_puts(out, "error: 'task' is required");
|
||||
}
|
||||
if (c->cfg->ndelegates == 0) {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&tbuf);
|
||||
return sp_buf_puts(out, "error: no delegate configured "
|
||||
"(add a 'delegate = ...' line to the config)");
|
||||
}
|
||||
d = choose(c->cfg, task, wanted, &score, why, sizeof(why));
|
||||
if (d == NULL) {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&tbuf);
|
||||
return sp_buf_puts(out, "error: no delegate matches this task");
|
||||
}
|
||||
|
||||
if (sp_buf_init(&reply) != 0 || sp_buf_init(&err) != 0) {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&tbuf);
|
||||
return -1;
|
||||
}
|
||||
sp_buf_printf(out, "[delegate %s, score %d%s%s]\n", d->id, score,
|
||||
why[0] != '\0' ? ": " : "", why);
|
||||
if (run_child(c, d, task, &reply, &err) != 0) {
|
||||
sp_buf_printf(out, "error: %s\n", err.p != NULL ? err.p : "failed");
|
||||
sp_buf_free(&reply);
|
||||
sp_buf_free(&err);
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&tbuf);
|
||||
return 0;
|
||||
}
|
||||
sp_buf_printf(out, "%s", reply.p != NULL ? reply.p : "(no answer)");
|
||||
sp_buf_free(&reply);
|
||||
sp_buf_free(&err);
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&tbuf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The decision on its own, so it can be tested and shown: which delegate fits a
|
||||
* task, and why. A pick nobody can explain is a pick nobody can correct.
|
||||
*/
|
||||
int
|
||||
sp_delegate_choose(const struct sp_config *cfg, const char *task,
|
||||
const char *tags, char *id, size_t cap, char *why, size_t whylen,
|
||||
int *score)
|
||||
{
|
||||
const struct sp_delegate *d;
|
||||
|
||||
if (id != NULL && cap > 0)
|
||||
id[0] = '\0';
|
||||
if (why != NULL && whylen > 0)
|
||||
why[0] = '\0';
|
||||
d = choose(cfg, task, tags, score, why, whylen);
|
||||
if (d == NULL)
|
||||
return -1;
|
||||
if (id != NULL)
|
||||
snprintf(id, cap, "%s", d->id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_delegate_register(struct sp_registry *r, const struct sp_config *cfg)
|
||||
{
|
||||
static struct del_ctx ctx;
|
||||
struct sp_tool t;
|
||||
ssize_t n;
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
ctx.cfg = cfg;
|
||||
n = readlink("/proc/self/exe", ctx.self, sizeof(ctx.self) - 1);
|
||||
if (n <= 0)
|
||||
snprintf(ctx.self, sizeof(ctx.self), "splinter");
|
||||
else
|
||||
ctx.self[n] = '\0';
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "delegate";
|
||||
t.description = "Hand a task to another configured model and return its "
|
||||
"answer. Use it for work another model does better or cheaper.";
|
||||
t.params = "{\"type\":\"object\",\"properties\":{"
|
||||
"\"task\":{\"type\":\"string\",\"description\":\"what to do, "
|
||||
"self-contained\"},"
|
||||
"\"tags\":{\"type\":\"string\",\"description\":\"what kind of work, "
|
||||
"e.g. code or chat\"}},"
|
||||
"\"required\":[\"task\"]}";
|
||||
t.auth = SP_AUTH_DESTRUCTIVE; /* it runs a whole agent of its own */
|
||||
t.run = tool_delegate;
|
||||
t.ctx = &ctx;
|
||||
return sp_registry_add(r, &t);
|
||||
}
|
||||
+32
-1
@@ -42,6 +42,7 @@ usage(FILE *f)
|
||||
" --theme NAME CDE palette for the UI (default Delphinium)\n"
|
||||
" --list-themes print the palette names and exit\n"
|
||||
" --resume load the session's history into the conversation\n"
|
||||
" --choose TEXT print which delegate fits TEXT, then exit\n"
|
||||
" --autonomous run mutating tools without asking\n"
|
||||
" --steps N tool rounds per turn (default 8)\n"
|
||||
" --session DIR session directory (default ~/.splinter)\n"
|
||||
@@ -123,6 +124,8 @@ main(int argc, char **argv)
|
||||
struct sp_mock mock;
|
||||
int have_mock = 0;
|
||||
int i, rc, chatmode = 0, resume = 0;
|
||||
struct sp_config conf;
|
||||
const char *choose_text = NULL;
|
||||
const char *plugins[16];
|
||||
size_t nplugins = 0;
|
||||
struct sp_host *host = NULL;
|
||||
@@ -132,6 +135,8 @@ main(int argc, char **argv)
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.max_steps = 8;
|
||||
|
||||
sp_config_init(&conf);
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
|
||||
usage(stdout);
|
||||
@@ -183,6 +188,12 @@ main(int argc, char **argv)
|
||||
plugins[nplugins++] = argv[++i];
|
||||
} else if (!strcmp(argv[i], "--chat")) {
|
||||
chatmode = 1;
|
||||
} else if (!strcmp(argv[i], "--choose")) {
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "splinter: --choose needs text\n");
|
||||
return 2;
|
||||
}
|
||||
choose_text = argv[++i];
|
||||
} else if (!strcmp(argv[i], "--resume")) {
|
||||
resume = 1;
|
||||
} else if (!strcmp(argv[i], "--autonomous")) {
|
||||
@@ -227,7 +238,6 @@ main(int argc, char **argv)
|
||||
|
||||
/* ---- configuration file: defaults, the command line still wins ---- */
|
||||
{
|
||||
struct sp_config conf;
|
||||
char cpath[512];
|
||||
int have_conf = 0;
|
||||
|
||||
@@ -299,6 +309,23 @@ main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
if (choose_text != NULL) {
|
||||
char id[64], why[192];
|
||||
int score = 0;
|
||||
|
||||
if (conf.ndelegates == 0) {
|
||||
fprintf(stderr, "splinter: no delegate configured\n");
|
||||
return 2;
|
||||
}
|
||||
if (sp_delegate_choose(&conf, choose_text, NULL, id, sizeof(id),
|
||||
why, sizeof(why), &score) != 0) {
|
||||
fprintf(stderr, "splinter: no delegate matches\n");
|
||||
return 1;
|
||||
}
|
||||
printf("%s\t%d\t%s\n", id, score, why);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* the catalogue is chosen now: a config file may set the locale */
|
||||
(void)sp_i18n_init(NULL);
|
||||
|
||||
@@ -325,6 +352,10 @@ main(int argc, char **argv)
|
||||
fprintf(stderr, "splinter: cannot register builtins\n");
|
||||
return 3;
|
||||
}
|
||||
if (sp_delegate_register(®, &conf) != 0) {
|
||||
fprintf(stderr, "splinter: cannot register delegate\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (sessdir == NULL) {
|
||||
const char *home = getenv("HOME");
|
||||
|
||||
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* splinter -- delegate selection.
|
||||
*
|
||||
* The choice is code, so it is tested like code: given a task and a set of
|
||||
* delegates, the same one must win every time, and the reason must name what
|
||||
* decided it.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "config.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static void
|
||||
add(struct sp_config *c, const char *id, const char *about, const char *tags)
|
||||
{
|
||||
struct sp_delegate *d = &c->delegate[c->ndelegates++];
|
||||
|
||||
memset(d, 0, sizeof(*d));
|
||||
snprintf(d->id, sizeof(d->id), "%s", id);
|
||||
snprintf(d->about, sizeof(d->about), "%s", about);
|
||||
snprintf(d->tags, sizeof(d->tags), "%s", tags);
|
||||
snprintf(d->endpoint, sizeof(d->endpoint), "http://example/v1");
|
||||
snprintf(d->model, sizeof(d->model), "m");
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct sp_config c;
|
||||
char id[64], why[192];
|
||||
int score;
|
||||
|
||||
memset(&c, 0, sizeof(c));
|
||||
add(&c, "coder", "C99, POSIX, kennt den Quellbaum", "code,c99,review");
|
||||
add(&c, "quick", "kurze Antworten, Chat", "chat,cheap,summary");
|
||||
|
||||
/* a tag in the task wins by tag */
|
||||
CHECK(sp_delegate_choose(&c, "pruefe den code auf fehler", NULL, id, sizeof(id),
|
||||
why, sizeof(why), &score) == 0);
|
||||
CHECK(strcmp(id, "coder") == 0);
|
||||
CHECK(score >= 3);
|
||||
CHECK(strstr(why, "code") != NULL);
|
||||
|
||||
CHECK(sp_delegate_choose(&c, "mach eine kurze summary", NULL, id, sizeof(id),
|
||||
why, sizeof(why), &score) == 0);
|
||||
CHECK(strcmp(id, "quick") == 0);
|
||||
|
||||
/* a requested tag outweighs the wording */
|
||||
CHECK(sp_delegate_choose(&c, "irgendwas mit code", "chat", id, sizeof(id),
|
||||
why, sizeof(why), &score) == 0);
|
||||
CHECK(strcmp(id, "quick") == 0);
|
||||
CHECK(strstr(why, "requested") != NULL);
|
||||
|
||||
/* a description word is weaker than a tag but still counts */
|
||||
CHECK(sp_delegate_choose(&c, "kennt jemand den quellbaum", NULL, id,
|
||||
sizeof(id), why, sizeof(why), &score) == 0);
|
||||
CHECK(strcmp(id, "coder") == 0);
|
||||
|
||||
/* nothing matches: no pick rather than a random one */
|
||||
CHECK(sp_delegate_choose(&c, "xyzzy", NULL, id, sizeof(id), why,
|
||||
sizeof(why), &score) != 0);
|
||||
|
||||
/* an empty set is not a match either */
|
||||
c.ndelegates = 0;
|
||||
CHECK(sp_delegate_choose(&c, "code", NULL, id, sizeof(id), why,
|
||||
sizeof(why), &score) != 0);
|
||||
|
||||
if (failures == 0)
|
||||
printf("test_delegate: ok\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user