cli_fallback = claude1, claude0 names cli delegates whose cmd and env= take over when cli_cmd answers with its limit. The main command is benched as "cli", a fallback under its own id, on the same bench the delegation uses (sp_limit_parse, sp_cooldown_until, sp_cooldown_set are now exported from libsplinter). The session key stays, so claude resumes the same conversation under the next account. With everyone benched the turn fails with the first reset, as WAIT_QUOTA ready_at=. A fallback's env= goes through a 0600 file the shell sources, not a command line. A delegate's child is benched under the delegate's id (SPLINTER_CLI_ID), never as "cli". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
754 lines
21 KiB
C
754 lines
21 KiB
C
/*
|
|
* splinter -- configuration file.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include "config.h"
|
|
|
|
void
|
|
sp_config_init(struct sp_config *c)
|
|
{
|
|
memset(c, 0, sizeof(*c));
|
|
c->autonomous = -1;
|
|
snprintf(c->key_env, sizeof(c->key_env), "SPLINTER_API_KEY");
|
|
}
|
|
|
|
int
|
|
sp_config_path(char *buf, size_t len)
|
|
{
|
|
const char *p = getenv("SPLINTER_CONFIG");
|
|
const char *xdg = getenv("XDG_CONFIG_HOME");
|
|
const char *home = getenv("HOME");
|
|
|
|
if (p != NULL && *p != '\0') {
|
|
snprintf(buf, len, "%s", p);
|
|
return 0;
|
|
}
|
|
if (home == NULL || *home == '\0')
|
|
return -1;
|
|
/*
|
|
* The config lives next to the sessions: ~/.splinter is where this thing
|
|
* keeps its state, and a second directory for one file was a stray.
|
|
*/
|
|
snprintf(buf, len, "%s/.splinter/splinter.conf", home);
|
|
{
|
|
char newpath[512];
|
|
FILE *f;
|
|
|
|
snprintf(newpath, sizeof(newpath), "%s", buf);
|
|
f = fopen(newpath, "r");
|
|
if (f != NULL) {
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
/* still nothing there? an old install may have used XDG */
|
|
if (xdg != NULL && *xdg != '\0')
|
|
snprintf(buf, len, "%s/splinter/config", xdg);
|
|
else
|
|
snprintf(buf, len, "%s/.config/splinter/config", home);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static char *
|
|
trim(char *s)
|
|
{
|
|
char *end;
|
|
|
|
while (*s == ' ' || *s == '\t')
|
|
s++;
|
|
end = s + strlen(s);
|
|
while (end > s && (end[-1] == ' ' || end[-1] == '\t' ||
|
|
end[-1] == '\n' || end[-1] == '\r'))
|
|
*--end = '\0';
|
|
return s;
|
|
}
|
|
|
|
static void
|
|
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.
|
|
*/
|
|
/* exit = id | label | endpoint=... | model=... | key_env=... */
|
|
static int
|
|
parse_exit(struct sp_exit *e, const char *val, struct sp_buf *err,
|
|
const char *path, unsigned lineno)
|
|
{
|
|
char *copy, *field, *save = NULL;
|
|
int first = 1;
|
|
|
|
memset(e, 0, sizeof(*e));
|
|
copy = strdup(val);
|
|
if (copy == NULL)
|
|
return -1;
|
|
for (field = strtok_r(copy, "|", &save); field != NULL;
|
|
field = strtok_r(NULL, "|", &save)) {
|
|
char *s = field, *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(e->id, sizeof(e->id), "%s", s);
|
|
first = 0;
|
|
continue;
|
|
}
|
|
if (!strncmp(s, "endpoint=", 9))
|
|
snprintf(e->endpoint, sizeof(e->endpoint), "%s", s + 9);
|
|
else if (!strncmp(s, "model=", 6))
|
|
snprintf(e->model, sizeof(e->model), "%s", s + 6);
|
|
else if (!strncmp(s, "key_env=", 8))
|
|
snprintf(e->key_env, sizeof(e->key_env), "%s", s + 8);
|
|
else if (strchr(s, '=') == NULL)
|
|
snprintf(e->label, sizeof(e->label), "%s", s);
|
|
else {
|
|
sp_buf_printf(err, "config: %s:%u: unknown exit field "
|
|
"'%s'", path, lineno, s);
|
|
free(copy);
|
|
return -1;
|
|
}
|
|
}
|
|
free(copy);
|
|
if (e->id[0] == '\0' || e->endpoint[0] == '\0' ||
|
|
e->model[0] == '\0') {
|
|
sp_buf_printf(err, "config: %s:%u: exit needs id, endpoint and "
|
|
"model", path, lineno);
|
|
return -1;
|
|
}
|
|
if (e->label[0] == '\0') {
|
|
char tmp[sizeof(e->id)];
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s", e->id);
|
|
snprintf(e->label, sizeof(e->label), "%s", tmp);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* NAME=VALUE with a name the shell would accept. A bad name is an error, not
|
|
* a silently dropped variable: the whole point is that the program sees it.
|
|
*/
|
|
static int
|
|
valid_env(const char *s)
|
|
{
|
|
const char *p = s;
|
|
|
|
if (!((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || *p == '_'))
|
|
return 0;
|
|
for (p++; *p != '\0' && *p != '='; p++)
|
|
if (!((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') ||
|
|
(*p >= '0' && *p <= '9') || *p == '_'))
|
|
return 0;
|
|
return *p == '=';
|
|
}
|
|
|
|
static int
|
|
add_env(char (*env)[SP_CONFIG_ENV_LEN], size_t *n, const char *val,
|
|
struct sp_buf *err, const char *path, unsigned lineno)
|
|
{
|
|
if (!valid_env(val)) {
|
|
sp_buf_printf(err, "config: %s:%u: env wants NAME=VALUE, got "
|
|
"'%.40s'", path, lineno, val);
|
|
return -1;
|
|
}
|
|
if (*n == SP_CONFIG_MAX_ENV) {
|
|
sp_buf_printf(err, "config: %s:%u: more than %d env entries",
|
|
path, lineno, SP_CONFIG_MAX_ENV);
|
|
return -1;
|
|
}
|
|
if (strlen(val) >= SP_CONFIG_ENV_LEN) {
|
|
sp_buf_printf(err, "config: %s:%u: env entry longer than %d",
|
|
path, lineno, SP_CONFIG_ENV_LEN - 1);
|
|
return -1;
|
|
}
|
|
snprintf(env[(*n)++], SP_CONFIG_ENV_LEN, "%s", val);
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
sp_config_apply_env(const char (*env)[SP_CONFIG_ENV_LEN], size_t n)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
char name[128];
|
|
const char *eq = strchr(env[i], '=');
|
|
size_t len;
|
|
|
|
if (eq == NULL)
|
|
continue;
|
|
len = (size_t)(eq - env[i]);
|
|
if (len >= sizeof(name))
|
|
continue;
|
|
memcpy(name, env[i], len);
|
|
name[len] = '\0';
|
|
(void)setenv(name, eq + 1, 1);
|
|
}
|
|
}
|
|
|
|
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));
|
|
d->max_parallel = 1;
|
|
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, "host=", 5))
|
|
snprintf(d->host, sizeof(d->host), "%s", s + 5);
|
|
else if (!strncmp(s, "prio=", 5))
|
|
d->prio = (int)strtol(s + 5, NULL, 10);
|
|
else if (!strncmp(s, "max_parallel=", 13)) {
|
|
d->max_parallel = (int)strtol(s + 13, NULL, 10);
|
|
if (d->max_parallel < 1)
|
|
d->max_parallel = 1;
|
|
}
|
|
else if (!strncmp(s, "kind=", 5))
|
|
snprintf(d->kind, sizeof(d->kind), "%s", s + 5);
|
|
else if (!strncmp(s, "cmd=", 4))
|
|
snprintf(d->cmd, sizeof(d->cmd), "%s", s + 4);
|
|
else 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 (!strncmp(s, "env=", 4)) {
|
|
if (add_env(d->env, &d->nenv, s + 4, err, path,
|
|
lineno) != 0) {
|
|
free(copy);
|
|
return -1;
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
/*
|
|
* A cli delegate runs a program, not an endpoint: claude and opencode bring
|
|
* their own model choice. Requiring endpoint and model there would force a
|
|
* fake value into the file, which is worse than no value.
|
|
*/
|
|
if (strcmp(d->kind, "cli") != 0 &&
|
|
(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;
|
|
}
|
|
|
|
/*
|
|
* Every file is parsed by the same function, and every error carries the file
|
|
* it came from: with drop-ins a line number without a name is a riddle.
|
|
*/
|
|
static int
|
|
load_one(struct sp_config *c, const char *path, struct sp_buf *err);
|
|
|
|
static int
|
|
cmp_name(const void *a, const void *b)
|
|
{
|
|
return strcmp(*(const char *const *)a, *(const char *const *)b);
|
|
}
|
|
|
|
/*
|
|
* Drop-ins: every .conf file in the directory named after the config file,
|
|
* read in name order, so 10-local beats 90-remote. A file that fails to parse
|
|
* names itself in the error.
|
|
*/
|
|
static int
|
|
load_dropins(struct sp_config *c, const char *mainpath, struct sp_buf *err)
|
|
{
|
|
char dir[600], *names[64];
|
|
size_t n = 0, i;
|
|
DIR *d;
|
|
struct dirent *e;
|
|
|
|
snprintf(dir, sizeof(dir), "%s.d", mainpath);
|
|
d = opendir(dir);
|
|
if (d == NULL)
|
|
return 0;
|
|
while ((e = readdir(d)) != NULL && n < 64) {
|
|
size_t len = strlen(e->d_name);
|
|
|
|
if (len < 6 || strcmp(e->d_name + len - 5, ".conf") != 0)
|
|
continue;
|
|
names[n] = strdup(e->d_name);
|
|
if (names[n] != NULL)
|
|
n++;
|
|
}
|
|
closedir(d);
|
|
qsort(names, n, sizeof(names[0]), cmp_name);
|
|
for (i = 0; i < n; i++) {
|
|
char full[700];
|
|
|
|
snprintf(full, sizeof(full), "%s/%s", dir, names[i]);
|
|
free(names[i]);
|
|
if (load_one(c, full, err) != 0) {
|
|
while (++i < n)
|
|
free(names[i]);
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
sp_config_load(struct sp_config *c, const char *path, struct sp_buf *err)
|
|
{
|
|
if (load_one(c, path, err) != 0)
|
|
return -1;
|
|
return load_dropins(c, path, err);
|
|
}
|
|
|
|
static int
|
|
load_one(struct sp_config *c, const char *path, struct sp_buf *err)
|
|
{
|
|
FILE *f;
|
|
char line[2048];
|
|
unsigned lineno = 0;
|
|
|
|
if (c == NULL || path == NULL)
|
|
return -1;
|
|
f = fopen(path, "r");
|
|
if (f == NULL) {
|
|
if (errno == ENOENT)
|
|
sp_buf_printf(err, "config: %s: not found", path);
|
|
else
|
|
sp_buf_printf(err, "config: %s: %s", path,
|
|
strerror(errno));
|
|
return -1;
|
|
}
|
|
|
|
while (fgets(line, sizeof(line), f) != NULL) {
|
|
char *eq, *key, *val;
|
|
|
|
lineno++;
|
|
key = trim(line);
|
|
if (*key == '\0' || *key == '#')
|
|
continue;
|
|
eq = strchr(key, '=');
|
|
if (eq == NULL) {
|
|
sp_buf_printf(err, "config: %s:%u: no '=' in: %s",
|
|
path, lineno, key);
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
*eq = '\0';
|
|
val = trim(eq + 1);
|
|
key = trim(key);
|
|
|
|
if (!strcmp(key, "endpoint"))
|
|
set_str(c->endpoint, sizeof(c->endpoint), val);
|
|
else if (!strcmp(key, "model"))
|
|
set_str(c->model, sizeof(c->model), val);
|
|
else if (!strcmp(key, "api_key_env"))
|
|
set_str(c->key_env, sizeof(c->key_env), val);
|
|
else if (!strcmp(key, "transport"))
|
|
set_str(c->transport, sizeof(c->transport), val);
|
|
else if (!strcmp(key, "memory_url"))
|
|
set_str(c->memory_url, sizeof(c->memory_url), val);
|
|
else if (!strcmp(key, "memory_key_env"))
|
|
set_str(c->memory_key_env, sizeof(c->memory_key_env), val);
|
|
else if (!strcmp(key, "telegram_allow"))
|
|
set_str(c->telegram_allow, sizeof(c->telegram_allow), val);
|
|
else if (!strcmp(key, "locale"))
|
|
set_str(c->locale, sizeof(c->locale), val);
|
|
else if (!strcmp(key, "theme"))
|
|
set_str(c->theme, sizeof(c->theme), val);
|
|
else if (!strcmp(key, "system_prompt"))
|
|
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, "cli_cmd"))
|
|
set_str(c->cli_cmd, sizeof(c->cli_cmd), val);
|
|
else if (!strcmp(key, "cli_fallback"))
|
|
set_str(c->cli_fallback, sizeof(c->cli_fallback), val);
|
|
else if (!strcmp(key, "cli_env")) {
|
|
if (add_env(c->cli_env, &c->ncli_env, val, err, path,
|
|
lineno) != 0) {
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
}
|
|
else if (!strcmp(key, "steps"))
|
|
c->steps = (size_t)strtoul(val, NULL, 10);
|
|
else if (!strcmp(key, "max_bytes"))
|
|
c->max_bytes = (size_t)strtoul(val, NULL, 10);
|
|
else if (!strcmp(key, "http_timeout") ||
|
|
!strcmp(key, "request_timeout"))
|
|
c->http_timeout = strtol(val, NULL, 10);
|
|
else if (!strcmp(key, "cli_timeout"))
|
|
c->cli_timeout = strtol(val, NULL, 10);
|
|
else if (!strcmp(key, "delegate_timeout"))
|
|
c->delegate_timeout = strtol(val, NULL, 10);
|
|
else if (!strcmp(key, "run_timeout"))
|
|
c->run_timeout = strtol(val, NULL, 10);
|
|
else if (!strcmp(key, "env_delegates"))
|
|
c->env_delegates = (!strcmp(val, "1") ||
|
|
!strcmp(val, "true") || !strcmp(val, "yes")) ? 1 : 0;
|
|
else if (!strcmp(key, "autonomous"))
|
|
c->autonomous = (!strcmp(val, "1") ||
|
|
!strcmp(val, "true") || !strcmp(val, "yes")) ? 1 : 0;
|
|
else if (!strcmp(key, "skills_dir")) {
|
|
if (c->nskills_dir == SP_CONFIG_MAX_DELEGATES) {
|
|
sp_buf_printf(err, "config: %s:%u: more than %d "
|
|
"skills_dir", path, lineno,
|
|
SP_CONFIG_MAX_DELEGATES);
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
snprintf(c->skills_dir[c->nskills_dir++],
|
|
sizeof(c->skills_dir[0]), "%s", val);
|
|
}
|
|
else if (!strcmp(key, "exit")) {
|
|
if (c->nexits == SP_CONFIG_MAX_DELEGATES) {
|
|
sp_buf_printf(err, "config: %s:%u: more than %d "
|
|
"exits", path, lineno,
|
|
SP_CONFIG_MAX_DELEGATES);
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
if (parse_exit(&c->exit[c->nexits], val, err, path,
|
|
lineno) != 0) {
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
c->nexits++;
|
|
}
|
|
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 "
|
|
"plugins", path, lineno, SP_CONFIG_MAX_PLUGINS);
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
set_str(c->plugins[c->nplugins++], 512, val);
|
|
} else {
|
|
sp_buf_printf(err, "config: %s:%u: unknown key '%s'",
|
|
path, lineno, key);
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
}
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
sp_config_write_example(const char *path, struct sp_buf *err)
|
|
{
|
|
FILE *f;
|
|
char dir[1024];
|
|
char *slash;
|
|
|
|
if (path == NULL)
|
|
return -1;
|
|
snprintf(dir, sizeof(dir), "%s", path);
|
|
slash = strrchr(dir, '/');
|
|
if (slash != NULL) {
|
|
*slash = '\0';
|
|
if (mkdir(dir, 0700) != 0 && errno != EEXIST) {
|
|
sp_buf_printf(err, "config: %s: %s", dir, strerror(errno));
|
|
return -1;
|
|
}
|
|
}
|
|
f = fopen(path, "w");
|
|
if (f == NULL) {
|
|
sp_buf_printf(err, "config: %s: %s", path, strerror(errno));
|
|
return -1;
|
|
}
|
|
fprintf(f,
|
|
"# splinter configuration\n"
|
|
"#\n"
|
|
"# Plain key = value. '#' starts a comment. Command line wins over\n"
|
|
"# this file, this file wins over the built-in default. An unknown\n"
|
|
"# key is an error, so a typo cannot quietly do nothing.\n"
|
|
"\n"
|
|
"# Where the answers come from. Either an OpenAI-compatible endpoint\n"
|
|
"# (endpoint + model), or a transport a plugin provides (transport).\n"
|
|
"endpoint = http://127.0.0.1:8080/v1/chat/completions\n"
|
|
"model = \n"
|
|
"\n"
|
|
"# Name of the environment variable holding the API key.\n"
|
|
"# Empty/missing means no Authorization header is sent.\n"
|
|
"api_key_env = SPLINTER_API_KEY\n"
|
|
"\n"
|
|
"# A transport provided by a plugin, e.g. \"cli\" for any command as the\n"
|
|
"# model. Use either this or endpoint/model, not both.\n"
|
|
"#transport = cli\n"
|
|
"#cli_cmd = claude -p\n"
|
|
"#cli_env = CLAUDE_CODE_OAUTH_TOKEN=sk-ant-...\n"
|
|
"#cli_fallback = claude1, claude0\n"
|
|
"\n"
|
|
"# Plugins to load, one per line. Paths are relative to the working\n"
|
|
"# directory unless absolute.\n"
|
|
"#plugin = plugins/lcm/lcm.so\n"
|
|
"#plugin = plugins/cps/cps.so\n"
|
|
"\n"
|
|
"# Language: two-letter code, e.g. de or en. Catalogues live in\n"
|
|
"# share/locale. Without this, the environment decides (LANG).\n"
|
|
"#locale = de\n"
|
|
"\n"
|
|
"# CDE palette for the UI. `splinter --list-themes` prints them all.\n"
|
|
"theme = Delphinium\n"
|
|
"\n"
|
|
"# Run mutating and destructive tools without asking. 0 or 1.\n"
|
|
"autonomous = 0\n"
|
|
"\n"
|
|
"# Tool rounds per turn, and the conversation budget in bytes\n"
|
|
"# (0 = no limit).\n"
|
|
"steps = 150\n"
|
|
"max_bytes = 0\n"
|
|
"\n"
|
|
"# Timeouts in seconds. Local models can take long to answer: raise\n"
|
|
"# these instead of failing mid-turn. 0 means the built-in default.\n"
|
|
"#http_timeout = 900\n"
|
|
"#cli_timeout = 1800\n"
|
|
"#delegate_timeout = 900\n"
|
|
"#run_timeout = 600\n"
|
|
"\n"
|
|
"# Where session logs go (default ~/.splinter).\n"
|
|
"#session_dir = ~/.splinter\n"
|
|
"\n"
|
|
"# Sent once at the start of a conversation.\n"
|
|
"#system_prompt = Du bist knapp. Keine Floskeln.\n");
|
|
if (ferror(f)) {
|
|
sp_buf_printf(err, "config: %s: write failed", path);
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
* Read the provider keys out of the environment.
|
|
*
|
|
* Two families, both with an optional numeric suffix: OLLAMA_KEY, OLLAMA_KEY_0..;
|
|
* CLAUDE_CODE_OAUTH_TOKEN, CLAUDE_CODE_OAUTH_TOKEN_0... The suffix exists so a
|
|
* second and third account can be used without inventing config entries - the
|
|
* environment already knows them.
|
|
*
|
|
* An Ollama key becomes an exit and a delegate; a Claude token becomes a cli
|
|
* delegate. Nothing is logged: the keys are copied into memory and used, never
|
|
* printed.
|
|
*/
|
|
static size_t
|
|
scan_family(struct sp_config *c, const char *base, int is_ollama, size_t base_idx)
|
|
{
|
|
size_t added = 0;
|
|
int i;
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
char name[128], token[512], id[48];
|
|
const char *val;
|
|
int exit_added;
|
|
|
|
if (i == 0)
|
|
snprintf(name, sizeof(name), "%s", base);
|
|
else
|
|
snprintf(name, sizeof(name), "%s_%d", base, i - 1);
|
|
val = getenv(name);
|
|
if (val == NULL || *val == '\0')
|
|
continue;
|
|
snprintf(token, sizeof(token), "%s", val);
|
|
|
|
if (is_ollama) {
|
|
char ep[512];
|
|
const char *host = getenv("OLLAMA_ENDPOINT");
|
|
const char *model = getenv("OLLAMA_MODEL");
|
|
|
|
if (host == NULL || *host == '\0')
|
|
host = "https://ollama.com/v1/chat/completions";
|
|
snprintf(ep, sizeof(ep), "%s", host);
|
|
|
|
if (c->ndelegates < SP_CONFIG_MAX_DELEGATES) {
|
|
struct sp_delegate *d =
|
|
&c->delegate[c->ndelegates++];
|
|
|
|
memset(d, 0, sizeof(*d));
|
|
snprintf(id, sizeof(id), "ollama-%zu",
|
|
c->ndelegates - 1);
|
|
snprintf(d->id, sizeof(d->id), "%.31s", id);
|
|
snprintf(d->kind, sizeof(d->kind), "openai");
|
|
snprintf(d->about, sizeof(d->about),
|
|
"Ollama, lokales Modell, setzt um statt zu planen");
|
|
snprintf(d->tags, sizeof(d->tags),
|
|
"implement,local,cheap,ollama");
|
|
snprintf(d->endpoint, sizeof(d->endpoint), "%s", ep);
|
|
snprintf(d->model, sizeof(d->model), "%s",
|
|
model != NULL && *model != '\0' ? model :
|
|
"gpt-oss:120b");
|
|
snprintf(d->key, sizeof(d->key), "%.255s", token);
|
|
d->prio = 0;
|
|
d->max_parallel = 1;
|
|
added++;
|
|
}
|
|
exit_added = 0;
|
|
if (c->nexits < SP_CONFIG_MAX_DELEGATES) {
|
|
struct sp_exit *e = &c->exit[c->nexits++];
|
|
|
|
exit_added = 1;
|
|
|
|
memset(e, 0, sizeof(*e));
|
|
snprintf(e->id, sizeof(e->id), "ollama-%zu",
|
|
c->nexits - 1);
|
|
snprintf(e->label, sizeof(e->label),
|
|
"Ollama %zu", c->nexits);
|
|
snprintf(e->endpoint, sizeof(e->endpoint), "%s", ep);
|
|
snprintf(e->model, sizeof(e->model), "%s",
|
|
model != NULL && *model != '\0' ? model :
|
|
"gpt-oss:120b");
|
|
}
|
|
/* the key must be reachable by the child process */
|
|
{
|
|
char envname[64];
|
|
|
|
snprintf(envname, sizeof(envname),
|
|
"SPLINTER_OLLAMA_KEY_%zu", base_idx + added);
|
|
(void)setenv(envname, token, 1);
|
|
/*
|
|
* Only the exit made just now. With the table full,
|
|
* exit[nexits - 1] was the operator's own last exit, and
|
|
* this key replaced its key - sent to an unrelated endpoint.
|
|
*/
|
|
if (exit_added) {
|
|
snprintf(c->exit[c->nexits - 1].key_env,
|
|
sizeof(c->exit[0].key_env), "%s",
|
|
envname);
|
|
}
|
|
}
|
|
} else {
|
|
if (c->ndelegates < SP_CONFIG_MAX_DELEGATES) {
|
|
struct sp_delegate *d =
|
|
&c->delegate[c->ndelegates++];
|
|
|
|
memset(d, 0, sizeof(*d));
|
|
/*
|
|
* The name counts claude delegates, not the length of the
|
|
* whole delegate array. Numbering by array index meant this
|
|
* one was called 'claude-5' once five lines were configured
|
|
* - a name that changes when an unrelated delegate is added
|
|
* is a name nobody can refer to.
|
|
*/
|
|
if (base_idx + added == 0)
|
|
snprintf(id, sizeof(id), "claude");
|
|
else
|
|
snprintf(id, sizeof(id), "claude-%zu",
|
|
base_idx + added);
|
|
snprintf(d->id, sizeof(d->id), "%.31s", id);
|
|
snprintf(d->kind, sizeof(d->kind), "cli");
|
|
snprintf(d->cmd, sizeof(d->cmd), "claude -p");
|
|
snprintf(d->about, sizeof(d->about),
|
|
"Claude Code (aus CLAUDE_CODE_OAUTH_TOKEN), "
|
|
"plant und definiert Arbeit");
|
|
/*
|
|
* Its own tags, not the planner's. A delegate that appears
|
|
* because an environment variable is set must not silently
|
|
* win every planning task; it is chosen when someone asks
|
|
* for it by name or tag.
|
|
*/
|
|
snprintf(d->tags, sizeof(d->tags),
|
|
"claude,cli,big,review");
|
|
snprintf(d->key, sizeof(d->key), "%.255s", token);
|
|
/*
|
|
* Lower than the configured cloud delegates: an automatic
|
|
* entry gets no precedence over what was written down on
|
|
* purpose.
|
|
*/
|
|
d->prio = 5;
|
|
d->max_parallel = 1;
|
|
added++;
|
|
}
|
|
}
|
|
}
|
|
return added;
|
|
}
|
|
|
|
size_t
|
|
sp_config_scan_env(struct sp_config *c)
|
|
{
|
|
size_t n = 0;
|
|
|
|
if (c == NULL)
|
|
return 0;
|
|
n += scan_family(c, "OLLAMA_KEY", 1, 0);
|
|
if (c->env_delegates)
|
|
n += scan_family(c, "CLAUDE_CODE_OAUTH_TOKEN", 0, 0);
|
|
return n;
|
|
}
|