Files
splinter/plugins/cli/cli.c
T
lotherkandClaude Opus 5.5 75d8beeaa4 delegates and cli_env: hardcoded environment per cli program; swarm, provider, planner, scheduler, tmux
env=NAME=VALUE on a delegate line (repeatable) is set in the child right
before exec and beats what the parent exported, so two delegates can run
the same cli with two accounts. Remotely it goes in front as
`env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the
same for the main cli transport. A bad name is an error with the line.

Also in this commit, the work in progress from the tree: swarm and
provider state, the provider/scheduler/planner/tmux plugins, the ui test,
whose tmux session name is now per run so parallel runs do not collide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 19:15:48 +00:00

268 lines
6.9 KiB
C

/*
* splinter -- cli: any command-line agent as the model.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Splinter does not need an HTTP endpoint. It needs something that answers.
* This transport runs a program and takes its stdout as the answer, so
* `claude -p`, `opencode run`, `ollama run`, `llm` or a shell script can all be
* the brains -- and each of them brings its own keys, its own quota, its own
* free tier.
*
* Configuration is by environment, deliberately: no config file to drift.
*
* SPLINTER_CLI_CMD the program, default "claude -p". If it contains
* {file}, the prompt file path is substituted there;
* {prompt} substitutes a shell-quoted prompt. Without
* a placeholder the prompt is fed on stdin.
* SPLINTER_CLI_FULL=1 send the whole transcript instead of the last user
* message (the program keeps no history of its own)
* SPLINTER_CLI_TIMEOUT seconds, default 1800
*
* Trust note, because it matters: the command runs through /bin/sh -c, so the
* string comes from the operator's environment, never from the model. Output
* grows dynamically (capped at 64 MiB) and a non-zero exit is an error with the
* program's own stderr attached -- a silent empty answer is the one thing this
* must not do.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "i18n.h"
#define OUT_CAP (64u << 20)
struct cli_cfg {
char cmd[512];
char timeout[16];
int full;
};
static void
render(const struct sp_conv *conv, int full, struct sp_buf *out)
{
size_t i, from = 0;
sp_buf_clear(out);
if (!full) {
for (i = conv->n; i > 0; i--) {
if (conv->v[i - 1].role != NULL &&
strcmp(conv->v[i - 1].role, "user") == 0) {
from = i - 1;
break;
}
}
}
for (i = from; i < conv->n; i++) {
const struct sp_msg *m = &conv->v[i];
if (m->content == NULL || m->content[0] == '\0')
continue;
if (full)
sp_buf_printf(out, "%s: ", m->role != NULL ? m->role :
"?");
if (sp_buf_puts(out, m->content) != 0 ||
sp_buf_puts(out, "\n") != 0)
return;
}
}
/* 's' as one single-quoted shell word */
static int
sh_word(struct sp_buf *b, const char *s)
{
if (sp_buf_puts(b, "'") != 0)
return -1;
for (; *s != '\0'; s++)
if (*s == '\'' ? sp_buf_puts(b, "'\\''") != 0 :
sp_buf_add(b, s, 1) != 0)
return -1;
return sp_buf_puts(b, "'");
}
static int
cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
struct sp_buf *err)
{
struct cli_cfg *c = ctx;
struct sp_buf body, cmd, raw;
char path[512], errpath[540] = "", errtext[512];
const char *tmpdir = getenv("TMPDIR");
int fd, status;
ssize_t w;
size_t n;
FILE *p;
if (c == NULL)
return -1;
if (sp_buf_init(&body) != 0 || sp_buf_init(&cmd) != 0 ||
sp_buf_init(&raw) != 0) {
sp_buf_puts(err, "cli: out of memory");
return -1;
}
render(conv, c->full, &body);
if (tmpdir == NULL || *tmpdir == '\0')
tmpdir = "/var/tmp";
snprintf(path, sizeof(path), "%s/splinter-cli-XXXXXX", tmpdir);
fd = mkstemp(path);
if (fd < 0) {
sp_buf_printf(err, "cli: mkstemp: %s", strerror(errno));
goto fail;
}
n = body.len;
w = write(fd, body.p, n);
close(fd);
if (w < 0 || (size_t)w != n) {
sp_buf_printf(err, "cli: short write: %s", strerror(errno));
unlink(path);
goto fail;
}
/*
* The program reads the prompt on stdin unless the command names where it
* goes: {file} becomes the prompt file path, {prompt} becomes the prompt
* as a shell word. timeout guards a hang. stderr goes to a file next to
* the prompt so it never lands in the middle of a curses screen.
*/
snprintf(errpath, sizeof(errpath), "%s.err", path);
sp_buf_printf(&cmd, "timeout %s ", c->timeout);
if (strstr(c->cmd, "{file}") != NULL || strstr(c->cmd, "{prompt}") != NULL) {
const char *p = c->cmd;
while (*p != '\0') {
if (strncmp(p, "{file}", 6) == 0) {
if (sh_word(&cmd, path) != 0)
goto fail_build;
p += 6;
} else if (strncmp(p, "{prompt}", 8) == 0) {
if (sh_word(&cmd, body.p != NULL ? body.p : "") != 0)
goto fail_build;
p += 8;
} else if (sp_buf_add(&cmd, p, 1) != 0) {
goto fail_build;
} else
p++;
}
if (sp_buf_puts(&cmd, " 2> ") != 0 || sh_word(&cmd, errpath) != 0)
goto fail_build;
} else {
sp_buf_printf(&cmd, "%s < ", c->cmd);
if (sh_word(&cmd, path) != 0 || sp_buf_puts(&cmd, " 2> ") != 0 ||
sh_word(&cmd, errpath) != 0) {
fail_build:
unlink(path);
unlink(errpath);
goto fail;
}
}
p = popen(cmd.p, "r");
if (p == NULL) {
sp_buf_printf(err, "cli: popen: %s", strerror(errno));
unlink(path);
goto fail;
}
{
char chunk[4096];
size_t got;
while ((got = fread(chunk, 1, sizeof(chunk), p)) > 0) {
if (raw.len + got > OUT_CAP)
break;
if (sp_buf_add(&raw, chunk, got) != 0) {
pclose(p);
unlink(path);
goto fail;
}
}
}
status = pclose(p);
unlink(path);
errtext[0] = '\0';
{
FILE *ef = fopen(errpath, "r");
size_t en = 0;
if (ef != NULL) {
en = fread(errtext, 1, sizeof(errtext) - 1, ef);
fclose(ef);
}
errtext[en] = '\0';
(void)unlink(errpath);
}
if (status < 0) {
sp_buf_printf(err, "cli: %s", strerror(errno));
goto fail;
}
if (WIFEXITED(status) && WEXITSTATUS(status) == 124) {
sp_buf_printf(err, sp_td("cli.timeout", "cli: '%s' timed out after %s s"), c->cmd,
c->timeout);
goto fail;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
sp_buf_printf(err, sp_td("cli.failed", "cli: '%s' failed (exit %d): %.400s"), c->cmd,
WIFEXITED(status) ? WEXITSTATUS(status) : -1,
errtext[0] != '\0' ? errtext : raw.p != NULL ? raw.p : "");
goto fail;
}
if (raw.len == 0) {
sp_buf_printf(err, sp_td("cli.no_output", "cli: '%s' produced no output"), c->cmd);
goto fail;
}
out->status = 200;
out->content = strdup(raw.p);
if (out->content == NULL)
goto fail;
sp_buf_free(&body);
sp_buf_free(&cmd);
sp_buf_free(&raw);
return 0;
fail:
if (errpath[0] != '\0')
(void)unlink(errpath);
sp_buf_free(&body);
sp_buf_free(&cmd);
sp_buf_free(&raw);
return -1;
}
static int
cli_init(struct sp_host *h)
{
static struct cli_cfg cfg;
static struct sp_transport tr;
const char *cmd = getenv("SPLINTER_CLI_CMD");
const char *to = getenv("SPLINTER_CLI_TIMEOUT");
const char *full = getenv("SPLINTER_CLI_FULL");
memset(&cfg, 0, sizeof(cfg));
snprintf(cfg.cmd, sizeof(cfg.cmd), "%s",
cmd != NULL && *cmd != '\0' ? cmd : "claude -p");
snprintf(cfg.timeout, sizeof(cfg.timeout), "%s",
to != NULL && *to != '\0' ? to : "1800");
cfg.full = full != NULL && strcmp(full, "0") != 0;
tr.send = cli_send;
tr.ctx = &cfg;
return sp_host_add_transport(h, "cli", tr);
}
static const struct sp_plugin cli_plugin = {
SP_PLUGIN_ABI, "cli", "1.0.0", cli_init, NULL, NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &cli_plugin;
}