Files
splinter/src/config.c
T
lotherkandClaude Opus 5.5 05c8d936e7 anthropic transport, provider usage APIs, endpoints as /v1 base
- kind = anthropic: the Messages API over curl, OAuth tokens (sk-ant-oat)
  as Bearer with the oauth beta, tool_use/tool_result blocks.
- --doctor spends no tokens: it asks /models and the provider's own usage
  route (ollama /api/balance, opencode go /v1/usage) instead of a tiny turn;
  cli providers are only checked for the program. /usage shows the same
  provider figures, cached for a minute.
- endpoint names the API base (https://host/v1); sp_endpoint_url() builds
  chat/completions (default), messages, models and usage from it. A full
  route is still accepted and cut back to the base.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:35:41 +00:00

1051 lines
29 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"
#include "usage.h"
#include "confblock.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, "limit=", 6)) {
if (!sp_usage_limit_parse(s + 6, &d->req_limit,
&d->req_window)) {
sp_buf_printf(err, "config: %s:%u: limit= wants "
"N/window, e.g. 45/5h", path, lineno);
free(copy);
return -1;
}
}
else if (!strncmp(s, "tokens=", 7)) {
if (!sp_usage_limit_parse(s + 7, &d->tok_limit,
&d->tok_window)) {
sp_buf_printf(err, "config: %s:%u: tokens= wants "
"N/window, e.g. 2000000/7d", path, lineno);
free(copy);
return -1;
}
}
else if (!strncmp(s, "kind=", 5)) {
if (strcmp(s + 5, "cli") != 0 &&
strcmp(s + 5, "openai") != 0 &&
strcmp(s + 5, "anthropic") != 0) {
sp_buf_printf(err, "config: %s:%u: kind is "
"openai, anthropic or cli, not '%s'",
path, lineno, s + 5);
free(copy);
return -1;
}
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;
}
/*
* The file this process actually read. --config names a path that need not be
* the default one, and a command that writes a drop-in has to write next to
* the file that is in use, not next to a path guessed from the environment.
*/
static char g_loaded_path[700];
const char *
sp_config_loaded_path(void)
{
return g_loaded_path[0] != '\0' ? g_loaded_path : NULL;
}
int
sp_config_load(struct sp_config *c, const char *path, struct sp_buf *err)
{
snprintf(g_loaded_path, sizeof(g_loaded_path), "%s",
path != NULL ? path : "");
if (load_one(c, path, err) != 0) {
/* nothing was read: a drop-in next to it would never be seen */
g_loaded_path[0] = '\0';
return -1;
}
return load_dropins(c, path, err);
}
/* mcp = id | url=... | cmd=... | header=... | enabled=0 */
static int
parse_mcp(struct sp_mcpserver *m, const char *val, struct sp_buf *err,
const char *path, unsigned lineno)
{
char *copy, *field, *save = NULL;
int first = 1;
memset(m, 0, sizeof(*m));
m->enabled = 1;
copy = strdup(val);
if (copy == NULL)
return -1;
for (field = strtok_r(copy, "|", &save); field != NULL;
field = strtok_r(NULL, "|", &save)) {
char *s = trim(field);
if (*s == '\0')
continue;
if (first && strchr(s, '=') == NULL) {
snprintf(m->id, sizeof(m->id), "%s", s);
first = 0;
} else if (!strncmp(s, "url=", 4))
snprintf(m->url, sizeof(m->url), "%s", s + 4);
else if (!strncmp(s, "cmd=", 4))
snprintf(m->cmd, sizeof(m->cmd), "%s", s + 4);
else if (!strncmp(s, "header=", 7))
snprintf(m->header, sizeof(m->header), "%s", s + 7);
else if (!strncmp(s, "enabled=", 8))
m->enabled = strcmp(s + 8, "0") != 0 &&
strcmp(s + 8, "false") != 0 &&
strcmp(s + 8, "no") != 0;
else {
sp_buf_printf(err, "config: %s:%u: unknown mcp field "
"'%s'", path, lineno, s);
free(copy);
return -1;
}
}
free(copy);
if (m->id[0] == '\0' || (m->url[0] == '\0' && m->cmd[0] == '\0')) {
sp_buf_printf(err, "config: %s:%u: an mcp server needs a name "
"and a url or a cmd", path, lineno);
return -1;
}
return 0;
}
/* pool = id | strategy=failover|balance | members=a,b,c */
static int
parse_pool(struct sp_pool *p, const char *val, struct sp_buf *err,
const char *path, unsigned lineno)
{
char *copy, *field, *save = NULL;
int first = 1;
memset(p, 0, sizeof(*p));
snprintf(p->strategy, sizeof(p->strategy), "failover");
copy = strdup(val);
if (copy == NULL)
return -1;
for (field = strtok_r(copy, "|", &save); field != NULL;
field = strtok_r(NULL, "|", &save)) {
char *s = trim(field);
if (*s == '\0')
continue;
if (first) {
snprintf(p->id, sizeof(p->id), "%s", s);
first = 0;
} else if (!strncmp(s, "strategy=", 9)) {
if (strcmp(s + 9, "failover") && strcmp(s + 9, "balance")) {
sp_buf_printf(err, "config: %s:%u: strategy is "
"failover or balance", path, lineno);
free(copy);
return -1;
}
snprintf(p->strategy, sizeof(p->strategy), "%s", s + 9);
} else if (!strncmp(s, "members=", 8)) {
char *m, *msave = NULL;
for (m = strtok_r(s + 8, ",", &msave); m != NULL;
m = strtok_r(NULL, ",", &msave)) {
m = trim(m);
if (*m == '\0')
continue;
if (p->nmembers == SP_CONFIG_POOL_MEMBERS) {
sp_buf_printf(err, "config: %s:%u: more than "
"%d members", path, lineno,
SP_CONFIG_POOL_MEMBERS);
free(copy);
return -1;
}
snprintf(p->member[p->nmembers++],
sizeof(p->member[0]), "%s", m);
}
} else {
sp_buf_printf(err, "config: %s:%u: unknown pool field "
"'%s'", path, lineno, s);
free(copy);
return -1;
}
}
free(copy);
if (p->id[0] == '\0' || p->nmembers == 0) {
sp_buf_printf(err, "config: %s:%u: a pool needs an id and "
"members", path, lineno);
return -1;
}
return 0;
}
static int
load_stream(struct sp_config *c, FILE *f, const char *path,
const unsigned *linemap, size_t nmap, struct sp_buf *err);
static int
load_one(struct sp_config *c, const char *path, struct sp_buf *err)
{
FILE *f;
char *text = NULL;
size_t len = 0, cap = 0;
int rc;
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;
}
for (;;) {
char *nt;
size_t n;
if (len + 4096 + 1 > cap) {
cap = cap != 0 ? cap * 2 : 8192;
nt = realloc(text, cap);
if (nt == NULL) {
free(text);
fclose(f);
return -1;
}
text = nt;
}
n = fread(text + len, 1, cap - len - 1, f);
if (n == 0)
break;
len += n;
}
fclose(f);
if (text == NULL)
text = calloc(1, 1);
else
text[len] = '\0';
if (text != NULL && sp_confblock_is(text)) {
struct sp_buf flat;
unsigned *map = NULL;
size_t nmap = 0;
FILE *mf;
if (sp_buf_init(&flat) != 0) {
free(text);
return -1;
}
if (sp_confblock_translate(text, path, &flat, &map, &nmap,
err) != 0) {
sp_buf_free(&flat);
free(text);
return -1;
}
mf = fmemopen(flat.p != NULL ? flat.p : (char *)"", flat.len, "r");
if (mf == NULL && flat.len == 0) {
rc = 0;
} else if (mf == NULL) {
rc = -1;
} else {
rc = load_stream(c, mf, path, map, nmap, err);
fclose(mf);
}
free(map);
sp_buf_free(&flat);
free(text);
return rc;
}
f = fmemopen(text, len, "r");
if (f == NULL) {
rc = len == 0 ? 0 : -1;
} else {
rc = load_stream(c, f, path, NULL, 0, err);
fclose(f);
}
free(text);
return rc;
}
static int
load_stream(struct sp_config *c, FILE *f, const char *path,
const unsigned *linemap, size_t nmap, struct sp_buf *err)
{
char line[4096];
unsigned lineno = 0, phys = 0;
while (fgets(line, sizeof(line), f) != NULL) {
char *eq, *key, *val;
phys++;
lineno = linemap != NULL && phys <= nmap ? linemap[phys - 1] : phys;
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);
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, "kind")) {
if (strcmp(val, "openai") != 0 &&
strcmp(val, "anthropic") != 0) {
sp_buf_printf(err, "config: %s:%u: kind is "
"openai or anthropic (cli is a provider "
"field)", path, lineno);
return -1;
}
set_str(c->kind, sizeof(c->kind), 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) {
return -1;
}
}
else if (!strcmp(key, "limit") || !strcmp(key, "tokens")) {
int lim = key[0] == 'l';
if (!sp_usage_limit_parse(val,
lim ? &c->req_limit : &c->tok_limit,
lim ? &c->req_window : &c->tok_window)) {
sp_buf_printf(err, "config: %s:%u: %s wants "
"N/window, e.g. %s", path, lineno, key,
lim ? "45/5h" : "2000000/7d");
return -1;
}
}
else if (!strcmp(key, "on_limit")) {
if (!strcmp(val, "wait"))
c->on_limit_fail = 0;
else if (!strcmp(val, "fail"))
c->on_limit_fail = 1;
else {
sp_buf_printf(err, "config: %s:%u: on_limit is "
"wait or fail", path, lineno);
return -1;
}
}
else if (!strcmp(key, "max_wait")) {
c->max_wait = sp_usage_window(val);
if (c->max_wait == 0) {
sp_buf_printf(err, "config: %s:%u: max_wait is a "
"duration like 6h or 2d", path, lineno);
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);
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);
return -1;
}
if (parse_exit(&c->exit[c->nexits], val, err, path,
lineno) != 0) {
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);
return -1;
}
if (parse_delegate(&c->delegate[c->ndelegates], val,
err, path, lineno) != 0) {
return -1;
}
c->ndelegates++;
}
else if (!strcmp(key, "pool")) {
if (c->npools == SP_CONFIG_MAX_POOLS) {
sp_buf_printf(err, "config: %s:%u: more than %d "
"pools", path, lineno, SP_CONFIG_MAX_POOLS);
return -1;
}
if (parse_pool(&c->pool[c->npools], val, err, path,
lineno) != 0)
return -1;
c->npools++;
}
else if (!strcmp(key, "default"))
set_str(c->default_id, sizeof(c->default_id), val);
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);
return -1;
}
set_str(c->plugins[c->nplugins++], 512, val);
}
else if (!strcmp(key, "mcp")) {
if (c->nmcp == SP_CONFIG_MAX_MCP) {
sp_buf_printf(err, "config: %s:%u: more than %d "
"mcp servers", path, lineno, SP_CONFIG_MAX_MCP);
return -1;
}
if (parse_mcp(&c->mcp[c->nmcp], val, err, path,
lineno) != 0)
return -1;
c->nmcp++;
} else {
sp_buf_printf(err, "config: %s:%u: unknown key '%s'",
path, lineno, key);
return -1;
}
}
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;
}
/* the file may end up holding a key: 0600 from the first instant */
(void)fchmod(fileno(f), 0600);
fprintf(f,
"# splinter configuration\n"
"#\n"
"# Blocks with { }, settings as name = value, lists in [ ].\n"
"# \"${NAME}\" inserts an environment variable. '#' starts a comment.\n"
"# An unknown name is an error, so a typo cannot quietly do nothing.\n"
"# Command line wins over this file, this file over the built-in default.\n"
"\n"
"core {\n"
" default = local # a provider or pool below; -m overrides\n"
" theme = Delphinium # splinter --list-themes\n"
" autonomous = false # true: run mutating tools without asking\n"
" #locale = de\n"
" steps = 150 # tool rounds per turn\n"
" #max_bytes = 0 # conversation budget, 0 = none\n"
" #session_dir = ~/.splinter\n"
"\n"
" # A usage limit is waited out: splinter finds the reset and goes on.\n"
" #on_limit = wait # or fail\n"
" #max_wait = 12h\n"
"\n"
" #plugins = [plugins/lcm/lcm.so, plugins/cps/cps.so]\n"
"}\n"
"\n"
"#telegram { allow = [123456789] }\n"
"\n"
"provider local {\n"
" about = \"my local model\"\n"
" endpoint = http://127.0.0.1:8080/v1 # the API base; routes follow\n"
" model = my-model # the name the endpoint serves\n"
" tags = [local, code]\n"
" #key_env = SPLINTER_API_KEY # name of the variable holding the key\n"
"}\n"
"\n"
"# A subscription with an allowance splinter should watch:\n"
"#provider claude-a {\n"
"# kind = cli\n"
"# cmd = \"claude -p\"\n"
"# tags = [cli, big]\n"
"# limit = 40/5h # calls per window; tokens = N/window too\n"
"# env { CLAUDE_CODE_OAUTH_TOKEN = \"${CLAUDE_A_TOKEN}\" }\n"
"#}\n"
"\n"
"# Claude over the API instead of the CLI: the Messages API has its own\n"
"# shape, and an sk-ant-oat token (Claude Code) or an sk-ant-api key\n"
"# both work. One provider per account, one pool over them.\n"
"#provider claude-api {\n"
"# kind = anthropic\n"
"# endpoint = https://api.anthropic.com/v1\n"
"# model = claude-sonnet-5-5\n"
"# tags = [analyze, decide, review]\n"
"# limit = 40/5h\n"
"# key_env = CLAUDE_CODE_OAUTH_TOKEN\n"
"#}\n"
"\n"
"# Providers that stand in for each other:\n"
"#pool claude { members = [claude-a, claude-b] strategy = balance }\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";
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;
}