diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 7135240..2e824e0 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -71,6 +71,25 @@ pool claude { members = [claude-a, claude-b, anthropic-a] strategy = balance } | `anthropic` | `https://api.anthropic.com/v1` (POSTs to `/messages`) | `sk-ant-oat...` as a Bearer token with the oauth beta (a Claude Code token); `sk-ant-api...` as `x-api-key` | | `cli` | none; a program (`cmd`) | its own, from `env { }` | +A `cli` provider can take its model from `-m` like any other: + +``` +provider occli { + kind = cli + cmd = "opencode run" # {model} marks where the model goes + provider = opencode # prefix for a bare model name + model = big-pickle # the default +} +``` + +`-m occli` runs `opencode run --model opencode/big-pickle`, +`-m occli/deepseek-v4.1-flash-free` runs `... --model +opencode/deepseek-v4.1-flash-free`, and a model that already has a `/` +(`-m occli/opencode-go/kimi-k2`) is passed as it is. Without `{model}` in +`cmd` the model is appended as `--model`; without any model the command runs +unchanged. A model name may hold only letters, digits and `._:/@+-`, since the +command goes through `/bin/sh`. + A `kind = anthropic` provider is called directly, without the `claude` CLI: tools become `tool_use` blocks, their observations `tool_result` blocks, and the system prompt is sent as the top-level `system` field. One provider per diff --git a/plugins/cli/cli.c b/plugins/cli/cli.c index 3ba9639..d60b8b1 100644 --- a/plugins/cli/cli.c +++ b/plugins/cli/cli.c @@ -647,8 +647,10 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, "cli_fallback names no cli delegate"); continue; } - snprintf(alt.cmd, sizeof(alt.cmd), "%s", - d->cmd[0] != '\0' ? d->cmd : "claude -p"); + if (sp_cli_command(d, NULL, alt.cmd, + sizeof(alt.cmd)) != 0) + snprintf(alt.cmd, sizeof(alt.cmd), "%s", + d->cmd[0] != '\0' ? d->cmd : "claude -p"); alt.env = (const char (*)[SP_CONFIG_ENV_LEN])d->env; alt.nenv = d->nenv; } diff --git a/src/confblock.c b/src/confblock.c index 2bcbe9a..f4fb751 100644 --- a/src/confblock.c +++ b/src/confblock.c @@ -321,8 +321,8 @@ static void do_provider(struct cb *c, const char *name, unsigned line, struct ent *e, size_t n) { - static const char *const plain[] = { "kind", "cmd", "host", "endpoint", - "model", "key_env", "prio", "max_parallel", "limit", "tokens", NULL }; + static const char *const plain[] = { "kind", "cmd", "provider", "host", + "endpoint", "model", "key_env", "prio", "max_parallel", "limit", "tokens", NULL }; struct sp_buf l; const char *about = ""; size_t i, j; diff --git a/src/config.c b/src/config.c index 3b7ecfe..18fe6d0 100644 --- a/src/config.c +++ b/src/config.c @@ -280,6 +280,9 @@ parse_delegate(struct sp_delegate *d, const char *val, struct sp_buf *err, } else if (!strncmp(s, "cmd=", 4)) snprintf(d->cmd, sizeof(d->cmd), "%s", s + 4); + else if (!strncmp(s, "provider=", 9)) + snprintf(d->cli_provider, sizeof(d->cli_provider), "%s", + s + 9); else if (!strncmp(s, "tags=", 5)) snprintf(d->tags, sizeof(d->tags), "%s", s + 5); else if (!strncmp(s, "endpoint=", 9)) @@ -1048,3 +1051,38 @@ sp_config_scan_env(struct sp_config *c) n += scan_family(c, "CLAUDE_CODE_OAUTH_TOKEN", 0, 0); return n; } + +int +sp_cli_command(const struct sp_delegate *d, const char *model, char *out, + size_t cap) +{ + const char *cmd, *ph, *m; + char full[320]; + size_t k; + + if (d == NULL || out == NULL || cap == 0) + return -1; + cmd = d->cmd[0] != '\0' ? d->cmd : "claude -p"; + m = model != NULL && *model != '\0' ? model : d->model; + if (m == NULL || *m == '\0') { + snprintf(out, cap, "%s", cmd); + return 0; + } + for (k = 0; m[k] != '\0'; k++) { + unsigned char ch = (unsigned char)m[k]; + + if (!(isalnum(ch) || strchr("._:/@+-", ch) != NULL)) + return -1; + } + if (strchr(m, '/') == NULL && d->cli_provider[0] != '\0') + snprintf(full, sizeof(full), "%s/%s", d->cli_provider, m); + else + snprintf(full, sizeof(full), "%s", m); + ph = strstr(cmd, "{model}"); + if (ph != NULL) + snprintf(out, cap, "%.*s%s%s", (int)(ph - cmd), cmd, full, + ph + 7); + else + snprintf(out, cap, "%s --model %s", cmd, full); + return 0; +} diff --git a/src/config.h b/src/config.h index 03a8211..6bdb467 100644 --- a/src/config.h +++ b/src/config.h @@ -55,6 +55,8 @@ struct sp_delegate { char id[32]; char kind[16]; /* "openai" (default) or "cli" */ char cmd[256]; /* for kind=cli: the program, e.g. "claude -p" */ + char cli_provider[64]; /* kind=cli: prefix for a bare model, + * "opencode" -> opencode/ */ char key[256]; /* a literal key from the environment, if any */ char host[128]; /* ssh target; empty means this machine */ int prio; /* tie-break: higher wins */ @@ -183,4 +185,15 @@ int sp_config_write_example(const char *path, struct sp_buf *err); /* 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); +/* + * The command a cli provider runs with 'model' (NULL or "": its configured + * model). A bare model gets the provider prefix ("provider = opencode" turns + * big-pickle into opencode/big-pickle); one with a '/' is taken as it is. + * {model} in cmd is replaced; without it the model is appended as + * --model. No model at all: cmd unchanged. -1 for a model with characters a + * shell would read (the command goes through /bin/sh). + */ +int sp_cli_command(const struct sp_delegate *d, const char *model, + char *out, size_t cap); + #endif /* SPLINTER_CONFIG_H */ diff --git a/src/delegate.c b/src/delegate.c index ee67db8..01a14b9 100644 --- a/src/delegate.c +++ b/src/delegate.c @@ -630,8 +630,12 @@ exec_delegate(const struct del_ctx *c, const struct sp_delegate *d, */ if (cli) { sp_delegate_cli_plugin(c->self, plug, sizeof(plug)); - if (d->cmd[0] != '\0') - (void)setenv("SPLINTER_CLI_CMD", d->cmd, 1); + { + char cmd[600]; + + if (sp_cli_command(d, NULL, cmd, sizeof(cmd)) == 0) + (void)setenv("SPLINTER_CLI_CMD", cmd, 1); + } if (d->key[0] != '\0' && !remote) { const char *kenv = d->key_env[0] != '\0' ? d->key_env : "CLAUDE_CODE_OAUTH_TOKEN"; diff --git a/src/main.c b/src/main.c index d4029c6..1185737 100644 --- a/src/main.c +++ b/src/main.c @@ -942,9 +942,15 @@ apply_selection(struct opts *o, struct sp_config *conf) ssize_t n; size_t k; + char cmd[600]; + o->transport = "cli"; - setenv("SPLINTER_CLI_CMD", d->cmd[0] != '\0' ? d->cmd : - "claude -p", 1); + if (sp_cli_command(d, ch.model, cmd, sizeof(cmd)) != 0) { + fprintf(stderr, "splinter: '%s' is not a model name\n", + ch.model); + return 2; + } + setenv("SPLINTER_CLI_CMD", cmd, 1); setenv("SPLINTER_CLI_ID", d->id, 1); sp_config_apply_env((const char (*)[SP_CONFIG_ENV_LEN])d->env, d->nenv); @@ -1037,10 +1043,14 @@ run_model_list(const struct sp_config *conf, const struct opts *o) sp_usage_for(conf, d->id, &u); sp_usage_brief(&u, use, sizeof(use)); - snprintf(line, sizeof(line), "%s%s%s", d->kind[0] != '\0' && - strcmp(d->kind, "cli") == 0 ? "cli: " : "", - d->kind[0] != '\0' && strcmp(d->kind, "cli") == 0 ? d->cmd : - d->model, d->about[0] != '\0' ? "" : ""); + if (strcmp(d->kind, "cli") == 0) { + char cmd[300]; + + if (sp_cli_command(d, NULL, cmd, sizeof(cmd)) != 0) + snprintf(cmd, sizeof(cmd), "%s", d->cmd); + snprintf(line, sizeof(line), "cli: %s", cmd); + } else + snprintf(line, sizeof(line), "%s", d->model); printf("%-34s %s [%s]\n", d->id, line, use); if (o->no_probe || strcmp(d->kind, "cli") == 0 || d->endpoint[0] == '\0') diff --git a/tests/test_config.c b/tests/test_config.c index b4ffdd3..dc6c90a 100644 --- a/tests/test_config.c +++ b/tests/test_config.c @@ -275,6 +275,33 @@ main(void) } } + /* a cli provider takes its model from -m, its prefix from provider = */ + { + struct sp_delegate d; + char out[400]; + + memset(&d, 0, sizeof(d)); + snprintf(d.cmd, sizeof(d.cmd), "opencode run"); + CHECK(sp_cli_command(&d, NULL, out, sizeof(out)) == 0 && + strcmp(out, "opencode run") == 0); + snprintf(d.cli_provider, sizeof(d.cli_provider), "opencode"); + snprintf(d.model, sizeof(d.model), "big-pickle"); + CHECK(sp_cli_command(&d, NULL, out, sizeof(out)) == 0 && + strcmp(out, "opencode run --model opencode/big-pickle") == 0); + CHECK(sp_cli_command(&d, "deepseek-v4.1-flash-free", out, + sizeof(out)) == 0 && strcmp(out, "opencode run --model " + "opencode/deepseek-v4.1-flash-free") == 0); + CHECK(sp_cli_command(&d, "opencode-go/kimi-k2", out, + sizeof(out)) == 0 && strcmp(out, + "opencode run --model opencode-go/kimi-k2") == 0); + snprintf(d.cmd, sizeof(d.cmd), "claude -p --model {model} --x"); + d.cli_provider[0] = '\0'; + CHECK(sp_cli_command(&d, "opus", out, sizeof(out)) == 0 && + strcmp(out, "claude -p --model opus --x") == 0); + CHECK(sp_cli_command(&d, "a;rm -rf ~", out, sizeof(out)) == -1); + CHECK(sp_cli_command(&d, "a'b", out, sizeof(out)) == -1); + } + if (failures == 0) printf("test_config: ok\n"); return failures == 0 ? 0 : 1;