One-shot mode always installed a stream callback and then, on success, printed only a newline - correct for the openai transport, which had already streamed the text, and wrong for every transport that cannot stream. With --transport cli the answer vanished: 'splinter -p ... --transport cli' printed an empty line and exit 0, so a claude or opencode backend looked like a model that had nothing to say. The callback is now installed only when the transport publishes send_stream. SPLINTER_CLI_CMD=hostname splinter -p 'sag hallo' --transport cli --plugin ... kitt Also: libsplinter.so and plugins/*.so were tracked, so every build produced a dirty tree (and a conflict on any pull). They are ignored and removed from the index; the binaries are rebuilt by make.
815 lines
23 KiB
C
815 lines
23 KiB
C
/*
|
|
* splinter -- command line entry point.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* Exit statuses: 0 the turn produced an answer, 1 the turn failed, 2 usage or
|
|
* configuration error, 3 the environment could not be set up (session dir).
|
|
*/
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "config.h"
|
|
#include "i18n.h"
|
|
#include "log.h"
|
|
#include "skills.h"
|
|
#include "memory.h"
|
|
#include "prompt.h"
|
|
#include "folder.h"
|
|
#include "mcpserver.h"
|
|
#include "interview.h"
|
|
#include "splinter_plugin.h"
|
|
#include "theme.h"
|
|
#include "ui.h"
|
|
|
|
static void
|
|
usage(FILE *f)
|
|
{
|
|
fprintf(f,
|
|
"usage: splinter [options]\n\n"
|
|
" -p, --prompt TEXT run one turn with TEXT and exit\n"
|
|
" --endpoint URL transport: OpenAI-compatible chat endpoint\n"
|
|
" --model NAME model name for --endpoint (required)\n"
|
|
" --api-key-env VAR read the API key from VAR (default\n"
|
|
" SPLINTER_API_KEY; missing means no auth)\n"
|
|
" --script FILE transport: replay replies from FILE, one\n"
|
|
" per line (no network; for tests and demos)\n"
|
|
" --config PATH read this config file instead of the default\n"
|
|
" --no-config ignore the config file entirely\n"
|
|
" --init-config write a commented example config and exit\n"
|
|
" --plugin PATH load a plugin (.so) before the first turn\n"
|
|
" --transport NAME use a transport a plugin provides (\"cli\")\n"
|
|
" --chat interactive curses UI instead of one turn\n"
|
|
" --theme NAME CDE palette for the UI (default Delphinium)\n"
|
|
" --list-themes print the palette names and exit\n"
|
|
" --resume load the session's history into the conversation\n"
|
|
" --choose TEXT print which delegate fits TEXT, then exit\n"
|
|
" --daemon answer on Telegram until SIGINT\n"
|
|
" --no-stdin ignore data on standard input\n"
|
|
" -c, --continue continue the last session started in this "
|
|
"directory\n"
|
|
" --interview ask the first-run questions and write the "
|
|
"profile\n"
|
|
" --mcp-server-listen IP serve MCP on IP (tools of this "
|
|
"registry)\n"
|
|
" --mcp-listen-port N MCP port, default 8098\n"
|
|
" --autonomous run mutating tools without asking\n"
|
|
" --steps N tool rounds per turn (default 8)\n"
|
|
" --session DIR session directory (default ~/.splinter)\n"
|
|
" --session-id ID session id (default sess-<time>-<pid>)\n"
|
|
" --max-bytes N request budget in bytes (0 = no limit)\n"
|
|
" -V, --version print the version\n"
|
|
" -h, --help this text\n\n"
|
|
"Exactly one transport is required. With none, splinter refuses to\n"
|
|
"start rather than pretend it can talk to a model.\n");
|
|
}
|
|
|
|
static int
|
|
need_value(int argc, int i, const char *flag)
|
|
{
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr, "splinter: %s needs a value\n", flag);
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
* A script file is one reply per line, in the mock transport's own protocol
|
|
* ("answer ...", "tool <name> <args>"). Loading it up front means the whole
|
|
* turn is deterministic and re-runnable, with no network anywhere.
|
|
*/
|
|
static int
|
|
load_script(struct sp_mock *m, const char *path, struct sp_buf *err)
|
|
{
|
|
FILE *f;
|
|
char line[8192];
|
|
|
|
f = fopen(path, "r");
|
|
if (f == NULL) {
|
|
sp_buf_printf(err, "%s: %s", path, strerror(errno));
|
|
return -1;
|
|
}
|
|
while (fgets(line, sizeof(line), f) != NULL) {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
if (line[0] == '\0' || line[0] == '#')
|
|
continue;
|
|
if (sp_mock_push(m, 200, line) != 0) {
|
|
fclose(f);
|
|
sp_buf_puts(err, sp_t("err.oom"));
|
|
return -1;
|
|
}
|
|
}
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
|
|
/* one-shot mode: print the answer as it arrives, no buffering */
|
|
static void
|
|
stdout_stream(void *ctx, int kind, const char *text)
|
|
{
|
|
(void)ctx;
|
|
if (kind != SP_STREAM_TEXT || text == NULL)
|
|
return;
|
|
fputs(text, stdout);
|
|
fflush(stdout);
|
|
}
|
|
|
|
|
|
/*
|
|
* Data on stdin becomes context.
|
|
*
|
|
* cat /etc/passwd | splinter "explain the file"
|
|
*
|
|
* The block is framed and labelled with its size, so the model can tell the
|
|
* operator's question from the bytes that came through a pipe. A binary input is
|
|
* reported as such instead of being sprayed into the conversation, and the size
|
|
* is capped - an accidental `cat /dev/zero` must not eat the budget.
|
|
*/
|
|
#define STDIN_LIMIT (192 * 1024)
|
|
|
|
static int
|
|
read_stdin_block(struct sp_buf *out, int *is_binary, size_t *raw_len)
|
|
{
|
|
char chunk[8192];
|
|
ssize_t got;
|
|
size_t total = 0;
|
|
int nul = 0;
|
|
|
|
*is_binary = 0;
|
|
*raw_len = 0;
|
|
for (;;) {
|
|
size_t i;
|
|
|
|
got = read(STDIN_FILENO, chunk, sizeof(chunk));
|
|
if (got < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
return -1;
|
|
}
|
|
if (got == 0)
|
|
break;
|
|
total += (size_t)got;
|
|
for (i = 0; i < (size_t)got; i++)
|
|
if (chunk[i] == '\0')
|
|
nul = 1;
|
|
if (out->len < STDIN_LIMIT)
|
|
(void)sp_buf_add(out, chunk, (size_t)got);
|
|
}
|
|
*is_binary = nul;
|
|
*raw_len = total;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* The gateway has nobody to stream to: it sends one finished message. The
|
|
* callback still matters, because without it the agent asks the transport for a
|
|
* plain reply - and a provider that only speaks SSE then answers with a stream
|
|
* the non-streaming reader cannot parse.
|
|
*/
|
|
static void
|
|
daemon_stream(void *ctx, int kind, const char *text)
|
|
{
|
|
(void)ctx;
|
|
(void)kind;
|
|
(void)text;
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
struct sp_registry reg;
|
|
struct sp_agent ag;
|
|
struct sp_agent_config cfg;
|
|
struct sp_session_info sess;
|
|
struct sp_buf answer = { 0 }, err = { 0 };
|
|
const char *prompt = NULL, *endpoint = NULL, *script = NULL;
|
|
const char *sessdir = NULL, *sessid = NULL, *theme = NULL;
|
|
const char *model = NULL, *key_env = "SPLINTER_API_KEY";
|
|
const char *transport_name = NULL;
|
|
const char *config_path = NULL;
|
|
int init_config = 0, no_config = 0;
|
|
int list_themes = 0;
|
|
char default_id[64];
|
|
struct sp_mock mock;
|
|
int have_mock = 0;
|
|
int i, rc, chatmode = 0, resume = 0;
|
|
int daemon_mode = 0, no_stdin = 0, continue_here = 0;
|
|
int interview = 0;
|
|
const char *mcp_ip = NULL;
|
|
int mcp_port = 8098;
|
|
struct sp_config conf;
|
|
const char *choose_text = NULL;
|
|
const char *plugins[16];
|
|
size_t nplugins = 0;
|
|
struct sp_host *host = NULL;
|
|
|
|
memset(®, 0, sizeof(reg));
|
|
memset(&ag, 0, sizeof(ag));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
cfg.max_steps = 8;
|
|
|
|
sp_config_init(&conf);
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
|
|
usage(stdout);
|
|
return 0;
|
|
} else if (!strcmp(argv[i], "-V") ||
|
|
!strcmp(argv[i], "--version")) {
|
|
printf("splinter %s\n", SP_VERSION);
|
|
return 0;
|
|
} else if (!strcmp(argv[i], "-p") ||
|
|
!strcmp(argv[i], "--prompt")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
prompt = argv[++i];
|
|
} else if (!strcmp(argv[i], "--endpoint")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
endpoint = argv[++i];
|
|
} else if (!strcmp(argv[i], "--model")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
model = argv[++i];
|
|
} else if (!strcmp(argv[i], "--api-key-env")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
key_env = argv[++i];
|
|
} else if (!strcmp(argv[i], "--init-config")) {
|
|
init_config = 1;
|
|
} else if (!strcmp(argv[i], "--no-config")) {
|
|
no_config = 1;
|
|
} else if (!strcmp(argv[i], "--config")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
config_path = argv[++i];
|
|
} else if (!strcmp(argv[i], "--transport")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
transport_name = argv[++i];
|
|
} else if (!strcmp(argv[i], "--script")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
script = argv[++i];
|
|
} else if (!strcmp(argv[i], "--plugin")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
if (nplugins == 16) {
|
|
fprintf(stderr, "splinter: at most 16 plugins\n");
|
|
return 2;
|
|
}
|
|
plugins[nplugins++] = argv[++i];
|
|
} else if (!strcmp(argv[i], "--chat")) {
|
|
chatmode = 1;
|
|
} else if (!strcmp(argv[i], "--choose")) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr, "splinter: --choose needs text\n");
|
|
return 2;
|
|
}
|
|
choose_text = argv[++i];
|
|
} else if (!strcmp(argv[i], "--daemon")) {
|
|
daemon_mode = 1;
|
|
} else if (!strcmp(argv[i], "--interview")) {
|
|
interview = 1;
|
|
} else if (!strcmp(argv[i], "--mcp-server-listen")) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr, "splinter: --mcp-server-listen "
|
|
"needs an address\n");
|
|
return 2;
|
|
}
|
|
mcp_ip = argv[++i];
|
|
} else if (!strcmp(argv[i], "--mcp-listen-port")) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr, "splinter: --mcp-listen-port "
|
|
"needs a number\n");
|
|
return 2;
|
|
}
|
|
mcp_port = atoi(argv[++i]);
|
|
} else if (!strcmp(argv[i], "-c") ||
|
|
!strcmp(argv[i], "--continue")) {
|
|
continue_here = 1;
|
|
} else if (!strcmp(argv[i], "--resume")) {
|
|
resume = 1;
|
|
} else if (!strcmp(argv[i], "--autonomous")) {
|
|
cfg.autonomous = 1;
|
|
} else if (!strcmp(argv[i], "--steps")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
cfg.max_steps = (size_t)strtoul(argv[++i], NULL, 10);
|
|
} else if (!strcmp(argv[i], "--max-bytes")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
cfg.max_bytes = (size_t)strtoul(argv[++i], NULL, 10);
|
|
} else if (!strcmp(argv[i], "--theme")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
theme = argv[++i];
|
|
} else if (!strcmp(argv[i], "--list-themes")) {
|
|
list_themes = 1;
|
|
} else if (!strcmp(argv[i], "--session")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
sessdir = argv[++i];
|
|
} else if (!strcmp(argv[i], "--session-id")) {
|
|
if (!need_value(argc, i, argv[i]))
|
|
return 2;
|
|
sessid = argv[++i];
|
|
} else if (argv[i][0] != '-') {
|
|
/* a bare word is the prompt: splinter "explain this" */
|
|
if (prompt != NULL) {
|
|
fprintf(stderr, "splinter: two prompts given\n");
|
|
return 2;
|
|
}
|
|
prompt = argv[i];
|
|
} else if (!strcmp(argv[i], "--no-stdin")) {
|
|
no_stdin = 1;
|
|
} else {
|
|
fprintf(stderr, "splinter: unknown option %s\n", argv[i]);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
if (list_themes) {
|
|
const char **names;
|
|
size_t count, k;
|
|
|
|
sp_theme_list(&names, &count);
|
|
for (k = 0; k < count; k++)
|
|
printf("%s\n", names[k]);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Piped input is context, not a prompt. Only when the caller gave no
|
|
* prompt at all does the input become the prompt.
|
|
*/
|
|
{
|
|
struct sp_buf intake;
|
|
int is_binary = 0, have_stdin = 0;
|
|
size_t raw = 0;
|
|
|
|
/* the interview asks its questions on stdin: leave it alone */
|
|
if (!no_stdin && !chatmode && !daemon_mode && !interview &&
|
|
!isatty(STDIN_FILENO) &&
|
|
sp_buf_init(&intake) == 0) {
|
|
if (read_stdin_block(&intake, &is_binary, &raw) == 0 &&
|
|
raw > 0)
|
|
have_stdin = 1;
|
|
}
|
|
if (have_stdin) {
|
|
static char combined[STDIN_LIMIT + 4096];
|
|
|
|
if (is_binary)
|
|
snprintf(combined, sizeof(combined),
|
|
"%s\n\n[stdin: %zu bytes of binary data, not "
|
|
"shown]\n", prompt != NULL ? prompt : "", raw);
|
|
else if (prompt != NULL)
|
|
snprintf(combined, sizeof(combined),
|
|
"%s\n\n[stdin: %zu bytes]\n%s%s\n[/stdin]\n",
|
|
prompt, raw,
|
|
intake.p != NULL ? intake.p : "",
|
|
raw > STDIN_LIMIT ? "\n[truncated]" : "");
|
|
else
|
|
snprintf(combined, sizeof(combined),
|
|
"[stdin: %zu bytes]\n%s%s\n[/stdin]\n", raw,
|
|
intake.p != NULL ? intake.p : "",
|
|
raw > STDIN_LIMIT ? "\n[truncated]" : "");
|
|
prompt = combined;
|
|
}
|
|
sp_buf_free(&intake);
|
|
}
|
|
|
|
/* ---- configuration file: defaults, the command line still wins ---- */
|
|
{
|
|
char cpath[512];
|
|
int have_conf = 0;
|
|
|
|
sp_config_init(&conf);
|
|
if (config_path == NULL) {
|
|
if (sp_config_path(cpath, sizeof(cpath)) != 0)
|
|
cpath[0] = '\0';
|
|
} else {
|
|
snprintf(cpath, sizeof(cpath), "%s", config_path);
|
|
}
|
|
|
|
if (init_config) {
|
|
sp_buf_clear(&err);
|
|
if (sp_config_write_example(cpath, &err) != 0) {
|
|
fprintf(stderr, "splinter: %s\n",
|
|
err.p != NULL ? err.p : "cannot write config");
|
|
return 3;
|
|
}
|
|
printf("splinter: wrote %s\n", cpath);
|
|
return 0;
|
|
}
|
|
|
|
if (!no_config && cpath[0] != '\0') {
|
|
sp_buf_clear(&err);
|
|
if (sp_config_load(&conf, cpath, &err) == 0) {
|
|
have_conf = 1;
|
|
} else if (strstr(err.p != NULL ? err.p : "", "not found") ==
|
|
NULL) {
|
|
fprintf(stderr, "splinter: %s\n", err.p);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
if (have_conf) {
|
|
size_t k;
|
|
|
|
if (endpoint == NULL && conf.endpoint[0] != '\0')
|
|
endpoint = conf.endpoint;
|
|
if (model == NULL && conf.model[0] != '\0')
|
|
model = conf.model;
|
|
if (theme == NULL && conf.theme[0] != '\0')
|
|
theme = conf.theme;
|
|
if (transport_name == NULL && conf.transport[0] != '\0')
|
|
transport_name = conf.transport;
|
|
if (nplugins == 0)
|
|
for (k = 0; k < conf.nplugins; k++)
|
|
plugins[nplugins++] = conf.plugins[k];
|
|
if (conf.steps > 0)
|
|
cfg.max_steps = conf.steps;
|
|
if (conf.max_bytes > 0)
|
|
cfg.max_bytes = conf.max_bytes;
|
|
if (conf.autonomous >= 0)
|
|
cfg.autonomous = conf.autonomous;
|
|
if (sessdir == NULL && conf.session_dir[0] != '\0')
|
|
sessdir = conf.session_dir;
|
|
if (cfg.system_prompt == NULL &&
|
|
conf.system_prompt[0] != '\0')
|
|
cfg.system_prompt = conf.system_prompt;
|
|
if (conf.key_env[0] != '\0')
|
|
key_env = conf.key_env;
|
|
/* the cli transport reads its command from the environment */
|
|
if (conf.locale[0] != '\0')
|
|
setenv("SPLINTER_LOCALE", conf.locale, 1);
|
|
if (conf.cli_cmd[0] != '\0')
|
|
setenv("SPLINTER_CLI_CMD", conf.cli_cmd, 0);
|
|
/* a config means "this is my setup": start the chat */
|
|
if (prompt == NULL)
|
|
chatmode = 1;
|
|
}
|
|
}
|
|
|
|
/* providers from the environment: OLLAMA_KEY(_N), CLAUDE_CODE_OAUTH_TOKEN(_N) */
|
|
{
|
|
size_t auto_added = sp_config_scan_env(&conf);
|
|
|
|
if (auto_added > 0)
|
|
fprintf(stderr, "splinter: %zu provider(s) from the "
|
|
"environment\n", auto_added);
|
|
}
|
|
|
|
if (choose_text != NULL) {
|
|
char id[64], why[192];
|
|
int score = 0;
|
|
|
|
if (conf.ndelegates == 0) {
|
|
fprintf(stderr, "splinter: no delegate configured\n");
|
|
return 2;
|
|
}
|
|
if (sp_delegate_choose(&conf, choose_text, NULL, id, sizeof(id),
|
|
why, sizeof(why), &score) != 0) {
|
|
fprintf(stderr, "splinter: no delegate matches\n");
|
|
return 1;
|
|
}
|
|
printf("%s\tscore=%d\tprio=%d\t%s\n", id, score,
|
|
sp_delegate_prio(&conf, id), why);
|
|
return 0;
|
|
}
|
|
|
|
/* the catalogue is chosen now: a config file may set the locale */
|
|
(void)sp_i18n_init(NULL);
|
|
|
|
if ((endpoint != NULL) + (script != NULL) + (transport_name != NULL) > 1) {
|
|
fprintf(stderr, "splinter: choose one transport\n");
|
|
return 2;
|
|
}
|
|
if (transport_name == NULL && endpoint == NULL && script == NULL &&
|
|
mcp_ip == NULL) {
|
|
fprintf(stderr, "splinter: no transport (--endpoint, --script "
|
|
"or --transport)\n");
|
|
return 2;
|
|
}
|
|
if (prompt == NULL && !chatmode && !daemon_mode && mcp_ip == NULL) {
|
|
fprintf(stderr, "splinter: -p/--prompt is required unless you "
|
|
"pass --chat, --daemon or --mcp-server-listen\n");
|
|
return 2;
|
|
}
|
|
|
|
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0) {
|
|
fprintf(stderr, "splinter: %s\n", sp_t("err.oom"));
|
|
return 3;
|
|
}
|
|
if (sp_builtin_register(®) != 0) {
|
|
fprintf(stderr, "splinter: cannot register builtins\n");
|
|
return 3;
|
|
}
|
|
if (sp_memory_register(®, &conf) != 0) {
|
|
fprintf(stderr, "splinter: cannot register memory\n");
|
|
return 3;
|
|
}
|
|
if (sp_skills_register(®, &conf) != 0) {
|
|
fprintf(stderr, "splinter: cannot register skills\n");
|
|
return 3;
|
|
}
|
|
if (sp_folder_register(®, sess.dir) != 0) {
|
|
fprintf(stderr, "splinter: cannot register folder_history\n");
|
|
return 3;
|
|
}
|
|
if (sp_delegate_register(®, &conf) != 0) {
|
|
fprintf(stderr, "splinter: cannot register delegate\n");
|
|
return 3;
|
|
}
|
|
|
|
if (sessdir == NULL) {
|
|
const char *home = getenv("HOME");
|
|
static char buf[512];
|
|
|
|
snprintf(buf, sizeof(buf), "%s/.splinter", home ? home : ".");
|
|
sessdir = buf;
|
|
}
|
|
/*
|
|
* -c means "the last session that happened *here*". The cwd at start is
|
|
* the key, so a session in another tree is never silently loaded.
|
|
*/
|
|
if (continue_here && sessid == NULL && sessdir != NULL) {
|
|
char cwd[512];
|
|
|
|
if (getcwd(cwd, sizeof(cwd)) != NULL &&
|
|
sp_session_last_for(sessdir, cwd, default_id,
|
|
sizeof(default_id)) == 0) {
|
|
sessid = default_id;
|
|
resume = 1;
|
|
} else {
|
|
fprintf(stderr, "splinter: no session in %s yet\n", cwd);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* --resume continues the newest session in the directory unless an id was
|
|
* named. Opening a fresh id would resume nothing and still look like it
|
|
* worked - the first version of this flag did exactly that.
|
|
*/
|
|
if (resume && sessid == NULL &&
|
|
sp_session_latest(sessdir, default_id, sizeof(default_id)) == 0)
|
|
sessid = default_id;
|
|
if (sessid == NULL) {
|
|
snprintf(default_id, sizeof(default_id), "sess-%ld-%d",
|
|
(long)time(NULL), (int)getpid());
|
|
sessid = default_id;
|
|
}
|
|
|
|
if (sp_session_open(&sess, sessdir, sessid, &err) != 0) {
|
|
fprintf(stderr, "splinter: %s\n", err.p ? err.p : sp_t("err.session"));
|
|
return 3;
|
|
}
|
|
ag.sess = &sess;
|
|
(void)sp_log_open(sess.dir, sess.id);
|
|
if (interview) {
|
|
rc = sp_interview_run(sess.dir, &err);
|
|
if (rc != 0)
|
|
fprintf(stderr, "splinter: %s\n", err.p != NULL ? err.p :
|
|
"interview failed");
|
|
return rc == 0 ? 0 : 1;
|
|
}
|
|
|
|
|
|
sp_logf("session", "started: %s", sess.id);
|
|
|
|
|
|
if (nplugins > 0) {
|
|
size_t k;
|
|
|
|
host = sp_host_new(®, &ag, &sess);
|
|
if (host != NULL) {
|
|
/*
|
|
* The agent compacts through this pointer. It was never set,
|
|
* so every compactor - lcm, focus, reminder - was registered,
|
|
* tested in isolation, and never called by the real binary.
|
|
*/
|
|
ag.host = host;
|
|
sp_host_set_config(host, &conf);
|
|
}
|
|
if (host == NULL) {
|
|
fprintf(stderr, "splinter: cannot create plugin host\n");
|
|
return 3;
|
|
}
|
|
for (k = 0; k < nplugins; k++) {
|
|
sp_buf_clear(&err);
|
|
if (sp_plugin_load(host, plugins[k], &err) != 0)
|
|
fprintf(stderr, "splinter: %s\n",
|
|
err.p != NULL ? err.p : sp_t("err.plugin_failed"));
|
|
else
|
|
fprintf(stderr, "splinter: plugin %s loaded\n",
|
|
plugins[k]);
|
|
}
|
|
}
|
|
|
|
if (transport_name != NULL) {
|
|
struct sp_transport *tr;
|
|
|
|
if (host == NULL) {
|
|
fprintf(stderr, "splinter: --transport needs a plugin "
|
|
"that provides it (--plugin)\n");
|
|
return 2;
|
|
}
|
|
tr = sp_host_transport(host, transport_name);
|
|
if (tr == NULL) {
|
|
fprintf(stderr, "splinter: no plugin provides the '%s' "
|
|
"transport\n", transport_name);
|
|
return 2;
|
|
}
|
|
ag.tr = *tr;
|
|
} else if (script != NULL) {
|
|
if (sp_mock_init(&mock) != 0) {
|
|
fprintf(stderr, "splinter: %s\n", sp_t("err.oom"));
|
|
return 3;
|
|
}
|
|
have_mock = 1;
|
|
if (load_script(&mock, script, &err) != 0) {
|
|
fprintf(stderr, "splinter: %s\n",
|
|
err.p != NULL ? err.p : "script");
|
|
return 3;
|
|
}
|
|
ag.tr = sp_mock_transport(&mock);
|
|
} else if (mcp_ip == NULL) {
|
|
/* in MCP server mode no model is needed: we are the tool provider */
|
|
const char *key = NULL;
|
|
|
|
if (model == NULL) {
|
|
fprintf(stderr, "splinter: --endpoint needs --model\n");
|
|
return 2;
|
|
}
|
|
if (getenv(key_env) != NULL)
|
|
key = getenv(key_env);
|
|
ag.tr = sp_openai_transport(endpoint, model, key, ®, 180);
|
|
}
|
|
|
|
{
|
|
static struct sp_conv conv;
|
|
|
|
sp_conv_init(&conv);
|
|
/*
|
|
* The system prompt is expanded here: placeholders are facts about
|
|
* this machine, and the model should not have to ask for them.
|
|
*/
|
|
{
|
|
static char expanded[8192];
|
|
|
|
sp_prompt_expand(cfg.system_prompt, expanded,
|
|
sizeof(expanded), model);
|
|
cfg.system_prompt = expanded;
|
|
}
|
|
if (cfg.system_prompt != NULL &&
|
|
sp_conv_push(&conv, "system", NULL, cfg.system_prompt) != 0) {
|
|
fprintf(stderr, "splinter: %s\n", sp_t("err.oom"));
|
|
return 3;
|
|
}
|
|
ag.conv = &conv;
|
|
|
|
if (resume) {
|
|
size_t loaded = 0;
|
|
|
|
sp_buf_clear(&err);
|
|
if (sp_session_load(&sess, &conv, &loaded, &err) != 0) {
|
|
fprintf(stderr, "splinter: %s\n",
|
|
err.p ? err.p : "cannot load the session");
|
|
return 3;
|
|
}
|
|
if (loaded > 0)
|
|
fprintf(stderr,
|
|
"splinter: resumed %zu messages from %s\n",
|
|
loaded, sess.dir);
|
|
}
|
|
}
|
|
|
|
cfg.approval_in = stdin;
|
|
cfg.approval_out = stderr;
|
|
ag.reg = ®
|
|
ag.cfg = &cfg;
|
|
|
|
/*
|
|
* Plugins are loaded after the registry and the session exist, before the
|
|
* first turn. A plugin that fails to load is reported and skipped: the
|
|
* harness keeps working, the operator sees why.
|
|
*/
|
|
|
|
if (mcp_ip != NULL) {
|
|
rc = sp_mcp_serve(®, mcp_ip, mcp_port, cfg.autonomous == 1,
|
|
&err);
|
|
if (rc != 0)
|
|
fprintf(stderr, "splinter: %s\n",
|
|
err.p != NULL ? err.p : "MCP server failed");
|
|
return rc == 0 ? 0 : 1;
|
|
}
|
|
|
|
if (daemon_mode) {
|
|
char token[256], tpath[700];
|
|
|
|
snprintf(tpath, sizeof(tpath), "%s/telegram.token", sess.dir);
|
|
if (sp_telegram_token(token, sizeof(token), tpath) != 0) {
|
|
fprintf(stderr, "splinter: no Telegram token: put it in "
|
|
"%s (0600) or set SPLINTER_TELEGRAM_TOKEN\n", tpath);
|
|
return 2;
|
|
}
|
|
if (conf.telegram_allow[0] == '\0') {
|
|
fprintf(stderr, "splinter: telegram_allow is empty - "
|
|
"nobody would be answered\n");
|
|
return 2;
|
|
}
|
|
cfg.stream = daemon_stream;
|
|
cfg.stream_ctx = NULL;
|
|
rc = sp_telegram_run(&ag, host, token, conf.telegram_allow, &err);
|
|
if (rc != 0)
|
|
fprintf(stderr, "splinter: %s\n",
|
|
err.p != NULL ? err.p : "gateway failed");
|
|
return rc == 0 ? 0 : 1;
|
|
}
|
|
|
|
if (chatmode) {
|
|
if (!sp_profile_exists(sess.dir))
|
|
fprintf(stderr, "splinter: kein Profil fuer '%s' - "
|
|
"`splinter --interview` legt eins an\n",
|
|
sp_memory_user());
|
|
rc = sp_ui_run(&ag, endpoint, theme, host);
|
|
if (have_mock)
|
|
sp_mock_free(&mock);
|
|
sp_host_free(host);
|
|
sp_buf_free(&answer);
|
|
sp_buf_free(&err);
|
|
sp_registry_free(®);
|
|
return rc;
|
|
}
|
|
|
|
/* a leading slash is a harness command, also without a TUI */
|
|
if (prompt != NULL && prompt[0] == '/' && host != NULL) {
|
|
char name[64], args[1536];
|
|
struct sp_buf out;
|
|
const char *sp;
|
|
|
|
sp = strchr(prompt, ' ');
|
|
if (sp == NULL) {
|
|
snprintf(name, sizeof(name), "%.63s", prompt + 1);
|
|
args[0] = '\0';
|
|
} else {
|
|
size_t nlen = (size_t)(sp - (prompt + 1));
|
|
|
|
if (nlen >= sizeof(name))
|
|
nlen = sizeof(name) - 1;
|
|
memcpy(name, prompt + 1, nlen);
|
|
name[nlen] = '\0';
|
|
snprintf(args, sizeof(args), "%s", sp + 1);
|
|
}
|
|
if (sp_buf_init(&out) != 0)
|
|
return 3;
|
|
if (sp_host_command(host, name, args, &out) != 0) {
|
|
sp_buf_clear(&out);
|
|
sp_buf_printf(&out, sp_t("cmd.unknown"), name);
|
|
sp_buf_printf(&out, "\n %s ", sp_t("cmd.available"));
|
|
(void)sp_host_command_help(host, &out);
|
|
printf("%s\n", out.p != NULL ? out.p : "");
|
|
sp_buf_free(&out);
|
|
return 2;
|
|
}
|
|
printf("%s\n", out.p != NULL ? out.p : "");
|
|
sp_buf_free(&out);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Only claim streaming when the transport can stream. With a transport that
|
|
* cannot (the cli plugin, an MCP-backed one), setting the callback made the
|
|
* agent take the non-streaming path while main still assumed the text had
|
|
* already been printed - the answer vanished and only a newline came out.
|
|
*/
|
|
cfg.stream = ag.tr.send_stream != NULL ? stdout_stream : NULL;
|
|
cfg.stream_ctx = NULL;
|
|
rc = sp_agent_run(&ag, prompt, &answer, &err);
|
|
if (rc == 0) {
|
|
if (cfg.stream == NULL)
|
|
printf("%s\n", answer.p != NULL ? answer.p : "");
|
|
else
|
|
printf("\n"); /* the stream already printed it */
|
|
} else {
|
|
fprintf(stderr, "splinter: %s\n", err.p ? err.p : "turn failed");
|
|
}
|
|
if (have_mock)
|
|
sp_mock_free(&mock);
|
|
sp_host_free(host);
|
|
sp_buf_free(&answer);
|
|
sp_buf_free(&err);
|
|
sp_registry_free(®);
|
|
return rc == 0 ? 0 : 1;
|
|
}
|