diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 2e824e0..5ca520e 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -29,9 +29,11 @@ core { steps = 60 on_limit = wait # a usage limit is waited out (or: fail) max_wait = 2d - plugins = [plugins/lcm/lcm.so, plugins/cli/cli.so] } +plugin lcm { } # one block per plugin, see "Plugins" +plugin cli { } + telegram { allow = [123456789] } provider ocgo { @@ -228,7 +230,7 @@ lies about what it does. | `api_key_env` | name of the env var holding the key (default `SPLINTER_API_KEY`) | | `transport` | a transport a plugin provides, e.g. `cli` | | `cli_cmd` | command for the `cli` transport, e.g. `claude -p` | -| `plugin` | a plugin to load, one line each; absolute paths survive a `cd` | +| `plugin` | a plugin to load, one line each: a shipped plugin's name (`lcm`) or a path; in the block form `plugin NAME { }`, see "Plugins" | | `theme` | CDE palette name (`splinter --list-themes`) | | `locale` | language code (`de`, `en`); catalogues live in `share/locale` | | `autonomous` | `1` runs mutating/destructive tools without asking | @@ -320,29 +322,66 @@ task moves to the next delegate in its own slot while the others keep running, unless that one is already at its `max_parallel`. Only when nothing else fits does the job wait (`waiting_quota`) for the reset. +## Plugins + +Every plugin has its own block. The block loads it, and everything in it +apart from `path` and `enabled` is that plugin's own configuration: + +``` +plugin lcm { + keep = 40 +} +plugin tmux { } +plugin mine { + path = ~/my/plugin.so # not shipped: say where it is + foo = bar +} +plugin status { enabled = false } # kept in the file, not loaded +``` + +| field | meaning | +|-------|---------| +| `path` | the `.so` to load; without it the shipped plugin of that name | +| `enabled` | `false` keeps the block but does not load the plugin (default on) | +| anything else | the plugin's own setting, read with `sp_host_plugin_config()` | + +Without `path`, `plugin NAME { }` finds the shipped plugin next to the +binary: `plugins/NAME/NAME.so` in the build tree, then the installed +`$(PREFIX)/lib/splinter/plugins/NAME.so`, then the build directory the binary +was compiled in. A nested block gives dotted names: `server a { url = x }` +inside `plugin foo` is `server.a.url`; a list `[a, b]` arrives as `a,b`. +`docs/PLUGINS.md` has the API. + +The old forms still load, so an existing file keeps working: +`plugins = [plugins/lcm/lcm.so, ...]` in `core` (one path each) and the flat +`plugin = /path/to/x.so`. They carry no plugin settings. + ## MCP servers -An MCP server contributes tools. One line each, or a block, and it needs the -`plugins/mcp/mcp.so` plugin: +An MCP server contributes tools. The servers are the configuration of the +`mcp` plugin, one `server` block each: ``` -plugin = /home/lotherk/src/splinter/plugins/mcp/mcp.so +plugin mcp { + # streamable HTTP: one POST per message + server junction { + url = http://172.25.206.16:8090/mcp + header = "Authorization: Bearer ${JUNCTION_TOKEN}" + } -# streamable HTTP: one POST per message -mcp junction { - url = http://172.25.206.16:8090/mcp - header = "Authorization: Bearer ${JUNCTION_TOKEN}" + # or a program that speaks MCP on its stdin/stdout + server files { + cmd = "npx -y @modelcontextprotocol/server-filesystem /home" + } + + # a server that exists but should not be asked + server old { url = http://127.0.0.1:9000/mcp enabled = false } } - -# or a program that speaks MCP on its stdin/stdout -mcp files { - cmd = "npx -y @modelcontextprotocol/server-filesystem /home" -} - -# a server that exists but should not be asked -mcp old { url = http://127.0.0.1:9000/mcp enabled = false } ``` +The top-level `mcp NAME { ... }` block and the flat `mcp = ...` line from +before still work (they need the plugin loaded as well). + | field | meaning | |-------|---------| | `url` | the endpoint, streamable-HTTP transport (`.../mcp`) | @@ -373,9 +412,9 @@ endpoint = http://127.0.0.1:8080/v1 model = /models/unsloth/Qwen3.6-35B-A3B-MTP-GGUF/Qwen3.6-35B-A3B-UD-Q8_K_XL.gguf theme = Delphinium autonomous = 1 -plugin = /home/lotherk/src/splinter/plugins/lcm/lcm.so -plugin = /home/lotherk/src/splinter/plugins/cps/cps.so -plugin = /home/lotherk/src/splinter/plugins/clock/clock.so +plugin = lcm +plugin = cps +plugin = clock session_dir = /home/lotherk/.splinter ``` @@ -385,7 +424,7 @@ session_dir = /home/lotherk/.splinter transport = cli cli_cmd = claude -p cli_timeout = 1800 -plugin = /home/lotherk/src/splinter/plugins/cli/cli.so +plugin = cli theme = Delphinium ``` diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index a0e7d7f..bd557e7 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -24,6 +24,46 @@ cc -std=c99 -shared -fPIC -o myplugin.so myplugin.c -L. -lsplinter \ plugin that fails to load is reported on stderr and skipped; the harness keeps working. +## Configuration + +In the config file every plugin has its own block (`docs/CONFIG.md`, +"Plugins"): + +``` +plugin mine { + path = ~/my/myplugin.so # omitted: the shipped plugin "mine" + depth = 3 + hosts = [a, b] + server a { url = http://a/mcp } +} +``` + +Everything but `path` and `enabled` is the plugin's own, and it reads it +through the host by the block's name: + +```c +const char *d = sp_host_plugin_config(host, "mine", "depth"); /* "3" */ +const char *h = sp_host_plugin_config(host, "mine", "hosts"); /* "a,b" */ +const char *u = sp_host_plugin_config(host, "mine", "server.a.url"); +``` + +A missing key is NULL; the value is a string, parsing is the plugin's job. +To see every setting (the servers it was given, say), walk them in file order: + +```c +const char *key, *val; +size_t i; + +for (i = 0; (val = sp_host_plugin_config_at(host, "mine", i, &key)) != NULL; i++) + /* key = "depth", "hosts", "server.a.url", ... */; +``` + +Both work from `init()` on. The config is read once at start; a value does +not change while the plugin runs. The `mcp` plugin is the one exception to +the dotted names: its `server NAME { }` blocks become the MCP server list it +always read (`sp_host_config()`), so `plugin mcp { server ... }` and the old +`mcp NAME { }` block mean the same. + ## What a plugin may do | hook | thread | may block | may touch curses | diff --git a/include/splinter.h b/include/splinter.h index 682a445..ee1312c 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -355,6 +355,14 @@ int sp_delegate_prio(const struct sp_config *, const char *id); */ void sp_delegate_cli_plugin(const char *self, char *out, size_t len); +/* + * A shipped plugin by name ("lcm", "mcp"): the build tree next to the binary + * at 'self', the installed layout ($(PREFIX)/lib/splinter/plugins), then the + * build directory compiled in. None found: the first candidate. + */ +void sp_plugin_path(const char *self, const char *name, char *out, + size_t len); + /* * Limits. sp_limit_parse: is this error text a rate/usage limit (429, "weekly * limit", "quota", ...) and until when - the reset it names, else an hour. diff --git a/include/splinter_plugin.h b/include/splinter_plugin.h index dcccf48..1630440 100644 --- a/include/splinter_plugin.h +++ b/include/splinter_plugin.h @@ -224,6 +224,26 @@ void sp_host_turn(struct sp_host *); /* The loaded configuration, so plugins (the exit ring) can read it. */ const void *sp_host_config(struct sp_host *); void sp_host_set_config(struct sp_host *, const void *); + +/* + * A plugin's own configuration, from its block in the config file: + * + * plugin lcm { + * keep = 40 + * server junction { url = http://... } + * } + * + * sp_host_plugin_config(h, "lcm", "keep") is "40", a nested block gives + * dotted keys ("server.junction.url"), a list "a,b,c". NULL when the key is + * not set (or no config is loaded). 'plugin' is the block's name. + * + * sp_host_plugin_config_at() walks every setting of 'plugin' in file order: + * index 0, 1, ... until it returns NULL; *key gets the setting's name. + */ +const char *sp_host_plugin_config(struct sp_host *, const char *plugin, + const char *key); +const char *sp_host_plugin_config_at(struct sp_host *, const char *plugin, + size_t i, const char **key); struct sp_registry *sp_host_registry(struct sp_host *); /* ---- host side, implemented by splinter, called by the harness ---- */ diff --git a/src/confblock.c b/src/confblock.c index f4fb751..eec807b 100644 --- a/src/confblock.c +++ b/src/confblock.c @@ -18,8 +18,10 @@ struct val { struct ent { char key[48]; + char sub[96]; /* key NAME { ... }: the NAME */ struct val v; unsigned line; + int block; /* a nested { } block: items are A=b */ }; struct cb { @@ -459,6 +461,140 @@ done: sp_buf_free(&l); } +/* one setting of a plugin: plugin_conf = NAME | key=value */ +static void +plugin_kv(struct cb *c, const struct ent *e, const char *plugin, + const char *key, const char *val) +{ + if (strchr(key, '=') != NULL || strchr(key, '|') != NULL) { + fail(c, e->line, "bad plugin setting name '%s'", key); + return; + } + if (!clean(c, e, val)) + return; + emit(c, e->line, "plugin_conf = %s | %s=%s", plugin, key, val); +} + +/* plugin mcp { server ID { url = ... } }: the servers the mcp plugin runs */ +static void +mcp_server(struct cb *c, const struct ent *e) +{ + struct sp_buf l; + size_t j; + + if (e->sub[0] == '\0') { + fail(c, e->line, "server needs a name: server NAME { url = ... }"); + return; + } + if (sp_buf_init(&l) != 0) + return; + sp_buf_printf(&l, "mcp = %s", e->sub); + for (j = 0; j < e->v.n; j++) { + const char *kv = e->v.items[j], *eq = strchr(kv, '='); + size_t kl = eq != NULL ? (size_t)(eq - kv) : 0; + + if (eq == NULL || !clean(c, e, kv)) + goto done; + if ((kl == 3 && !strncmp(kv, "url", 3)) || + (kl == 3 && !strncmp(kv, "cmd", 3)) || + (kl == 6 && !strncmp(kv, "header", 6))) + sp_buf_printf(&l, " | %s", kv); + else if (kl == 7 && !strncmp(kv, "enabled", 7)) + sp_buf_printf(&l, " | enabled=%d", boolean(eq + 1)); + else { + fail(c, e->line, "unknown mcp server field '%.*s'", + (int)kl, kv); + goto done; + } + } + emit(c, e->line, "%s", l.p); +done: + sp_buf_free(&l); +} + +/* + * plugin NAME { path = ... enabled = false anything = else } + * + * The block loads the plugin -- the shipped one for NAME unless 'path' names + * another -- and everything else in it is the plugin's own configuration, + * which it reads with sp_host_plugin_config(host, NAME, key). + */ +static void +do_plugin(struct cb *c, const char *name, unsigned line, struct ent *e, + size_t n) +{ + const char *path = NULL; + char full[1024], list[2048], key[200]; + size_t i, j; + int on = 1; + + for (i = 0; name[i] != '\0'; i++) + if (!isalnum((unsigned char)name[i]) && name[i] != '_' && + name[i] != '-') { + fail(c, line, "plugin name '%s': letters, digits, _ and - " + "only", name); + return; + } + for (i = 0; i < n && !c->failed; i++) { + if (!strcmp(e[i].key, "path") && !e[i].block) { + path = one(c, &e[i]); + if (path != NULL && !clean(c, &e[i], path)) + return; + } else if (!strcmp(e[i].key, "enabled") && !e[i].block) { + const char *s = one(c, &e[i]); + + if (s == NULL) + return; + on = boolean(s); + } + } + if (c->failed || !on) + return; + if (path != NULL && path[0] == '~' && path[1] == '/' && + getenv("HOME") != NULL) { + snprintf(full, sizeof(full), "%s%s", getenv("HOME"), path + 1); + path = full; + } + /* a bare name is the shipped plugin: main.c looks it up */ + emit(c, line, "plugin = %s", path != NULL ? path : name); + for (i = 0; i < n && !c->failed; i++) { + const struct ent *x = &e[i]; + + if ((!strcmp(x->key, "path") || !strcmp(x->key, "enabled")) && + !x->block) + continue; + if (x->block) { + if (!strcmp(name, "mcp") && !strcmp(x->key, "server")) { + mcp_server(c, x); + continue; + } + for (j = 0; j < x->v.n && !c->failed; j++) { + const char *kv = x->v.items[j]; + const char *eq = strchr(kv, '='); + + if (eq == NULL) + continue; + if (x->sub[0] != '\0') + snprintf(key, sizeof(key), "%s.%s.%.*s", + x->key, x->sub, (int)(eq - kv), kv); + else + snprintf(key, sizeof(key), "%s.%.*s", + x->key, (int)(eq - kv), kv); + plugin_kv(c, x, name, key, eq + 1); + } + } else if (x->v.list) { + joined(&x->v, list, sizeof(list)); + plugin_kv(c, x, name, x->key, list); + } else { + const char *s = one(c, x); + + if (s == NULL) + return; + plugin_kv(c, x, name, x->key, s); + } + } +} + static void do_exit(struct cb *c, const char *name, unsigned line, struct ent *e, size_t n) { @@ -533,10 +669,30 @@ body(struct cb *c, struct ent *e, int *closed) snprintf(e[n].key, sizeof(e[n].key), "%s", key); e[n].line = line; skip(c, 0); + if (*c->p != '{' && *c->p != '=' && *c->p != '\0' && + *c->p != '\n') { + /* key NAME { ... }: a named nested block (server junction) */ + size_t m = 0; + + while (isalnum((unsigned char)*c->p) || *c->p == '_' || + *c->p == '-' || *c->p == '.') { + if (m + 1 < sizeof(e[n].sub)) + e[n].sub[m++] = *c->p; + c->p++; + } + e[n].sub[m] = '\0'; + skip(c, 0); + if (m == 0 || *c->p != '{') { + fail(c, line, "expected '{' after %s %s", key, + e[n].sub); + return n; + } + } if (*c->p == '{') { - /* env { A = b } */ + /* env { A = b }, server junction { url = ... } */ c->p++; e[n].v.list = 1; + e[n].block = 1; for (;;) { char nm[96]; size_t m = 0; @@ -554,7 +710,8 @@ body(struct cb *c, struct ent *e, int *closed) return n; } vl = c->line; - while (isalnum((unsigned char)*c->p) || *c->p == '_') { + while (isalnum((unsigned char)*c->p) || *c->p == '_' || + *c->p == '-' || *c->p == '.') { if (m + 1 < sizeof(nm)) nm[m++] = *c->p; c->p++; @@ -699,7 +856,7 @@ sp_confblock_translate(const char *text, const char *path, struct sp_buf *out, if (!c.failed && closed) { int needs_name = !strcmp(kind, "provider") || !strcmp(kind, "pool") || !strcmp(kind, "exit") || - !strcmp(kind, "mcp"); + !strcmp(kind, "mcp") || !strcmp(kind, "plugin"); if (needs_name && name[0] == '\0') fail(&c, line, "%s needs a name: %s NAME { ... }", @@ -727,11 +884,14 @@ sp_confblock_translate(const char *text, const char *path, struct sp_buf *out, do_pool(&c, name, line, e, n); else if (!strcmp(kind, "exit")) do_exit(&c, name, line, e, n); + else if (!strcmp(kind, "plugin")) + do_plugin(&c, name, line, e, n); else if (!strcmp(kind, "mcp")) + /* deprecated: plugin mcp { server ID { ... } } */ do_mcp(&c, name, line, e, n); else fail(&c, line, "unknown block '%s' (core, telegram, " - "provider, pool, exit, mcp)", kind); + "provider, pool, exit, plugin)", kind); } ents_free(e, n); if (c.failed) diff --git a/src/config.c b/src/config.c index 18fe6d0..e71770b 100644 --- a/src/config.c +++ b/src/config.c @@ -776,6 +776,29 @@ load_stream(struct sp_config *c, FILE *f, const char *path, } else if (!strcmp(key, "default")) set_str(c->default_id, sizeof(c->default_id), val); + else if (!strcmp(key, "plugin_conf")) { + /* plugin_conf = NAME | key=value (from a plugin { } block) */ + struct sp_plugin_kv *kv; + char *bar = strstr(val, " | "), *eq2; + + if (bar == NULL || (eq2 = strchr(bar + 3, '=')) == NULL) { + sp_buf_printf(err, "config: %s:%u: plugin_conf " + "wants NAME | key=value", path, lineno); + return -1; + } + if (c->nplugin_conf == SP_CONFIG_MAX_PLUGIN_KV) { + sp_buf_printf(err, "config: %s:%u: more than %d " + "plugin settings", path, lineno, + SP_CONFIG_MAX_PLUGIN_KV); + return -1; + } + kv = &c->plugin_conf[c->nplugin_conf++]; + *bar = '\0'; + *eq2 = '\0'; + set_str(kv->plugin, sizeof(kv->plugin), trim(val)); + set_str(kv->key, sizeof(kv->key), trim(bar + 3)); + set_str(kv->val, sizeof(kv->val), eq2 + 1); + } else if (!strcmp(key, "plugin")) { if (c->nplugins == SP_CONFIG_MAX_PLUGINS) { sp_buf_printf(err, "config: %s:%u: more than %d " @@ -848,10 +871,14 @@ sp_config_write_example(const char *path, struct sp_buf *err) " # 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" + "# One block per plugin; what is in it is that plugin's own setting.\n" + "#plugin lcm { }\n" + "#plugin mcp {\n" + "# server junction { url = http://127.0.0.1:8090/mcp }\n" + "#}\n" + "\n" "#telegram { allow = [123456789] }\n" "\n" "provider local {\n" @@ -1086,3 +1113,21 @@ sp_cli_command(const struct sp_delegate *d, const char *model, char *out, snprintf(out, cap, "%s --model %s", cmd, full); return 0; } + +const char * +sp_config_plugin_value(const struct sp_config *c, const char *plugin, + const char *key) +{ + size_t i; + + if (c == NULL || plugin == NULL || key == NULL) + return NULL; + /* the last one wins, like every other setting */ + for (i = c->nplugin_conf; i > 0; i--) { + const struct sp_plugin_kv *kv = &c->plugin_conf[i - 1]; + + if (strcmp(kv->plugin, plugin) == 0 && strcmp(kv->key, key) == 0) + return kv->val; + } + return NULL; +} diff --git a/src/config.h b/src/config.h index 6bdb467..0bfc475 100644 --- a/src/config.h +++ b/src/config.h @@ -22,6 +22,18 @@ #include "splinter.h" #define SP_CONFIG_MAX_PLUGINS 32 +#define SP_CONFIG_MAX_PLUGIN_KV 96 + +/* + * One setting of a plugin's own configuration: plugin NAME { key = value }. + * A nested block (server junction { url = ... }) gives dotted keys, + * "server.junction.url"; a list becomes "a,b,c". + */ +struct sp_plugin_kv { + char plugin[48]; + char key[96]; + char val[512]; +}; /* * A delegate is another model splinter may hand work to: a name, what it is @@ -136,6 +148,8 @@ struct sp_config { char locale[32]; /* language code, exported as SPLINTER_LOCALE */ char plugins[SP_CONFIG_MAX_PLUGINS][512]; size_t nplugins; + struct sp_plugin_kv plugin_conf[SP_CONFIG_MAX_PLUGIN_KV]; + size_t nplugin_conf; struct sp_mcpserver mcp[SP_CONFIG_MAX_MCP]; size_t nmcp; size_t steps; /* 0: not set */ @@ -182,6 +196,13 @@ const char *sp_config_loaded_path(void); /* Write a commented starter file. Returns 0 or -1. */ int sp_config_write_example(const char *path, struct sp_buf *err); +/* + * A plugin's own setting (plugin NAME { key = value }), or NULL. 'key' may be + * dotted for a nested block: "server.junction.url". + */ +const char *sp_config_plugin_value(const struct sp_config *, + const char *plugin, const char *key); + /* Set every NAME=VALUE of 'env' in this process, overwriting. */ void sp_config_apply_env(const char (*env)[SP_CONFIG_ENV_LEN], size_t n); diff --git a/src/delegate.c b/src/delegate.c index 01a14b9..65c6206 100644 --- a/src/delegate.c +++ b/src/delegate.c @@ -569,15 +569,30 @@ shell_quote(struct sp_buf *b, const char *s) void sp_delegate_cli_plugin(const char *self, char *out, size_t len) { - char dir[512], cand[3][600]; - const char *env = getenv("SPLINTER_CLI_PLUGIN"); - const char *slash; - size_t n = 0, i; + const char *env = getenv("SPLINTER_CLI_PLUGIN"); if (env != NULL && *env != '\0') { snprintf(out, len, "%s", env); return; } + sp_plugin_path(self, "cli", out, len); +} + +/* + * A shipped plugin by name, for a binary at 'self'. Order: + *