diff --git a/Makefile b/Makefile index 514cb91..84957d4 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ CURSES_A = $(CURSES_DIR)/libcurses/libcurses.a \ LIB = libsplinter.so LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \ src/json.o src/sse.o src/openai.o src/transport_openai.o src/builtin.o \ - src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/config.o src/plugin.o + src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/config.o src/plugin.o BIN = splinter BINOBJ = src/main.o src/ui.o PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \ @@ -102,6 +102,8 @@ src/decomp.o: src/decomp.h src/i18n.o: src/i18n.h include/splinter.h src/delegate.o: src/config.h include/splinter.h src/telegram.o: include/splinter.h +src/log.o: src/log.h +src/cmdscript.o: src/cmdscript.h include/splinter.h src/config.o: src/config.h include/splinter.h src/plugin.o: include/splinter.h include/splinter_plugin.h src/ui.o: src/ui.h src/theme.h src/decomp.h src/i18n.h include/splinter.h include/splinter_plugin.h diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md new file mode 100644 index 0000000..8b6ce13 --- /dev/null +++ b/docs/ROADMAP.md @@ -0,0 +1,26 @@ +# Offene Punkte + +Stand: 2026-11-08. Reihenfolge ist Absicht, nicht Wunschliste. + +1. **Skills** — `~/.splinter/skills/` plus konfigurierbare Zusatzordner. Kein + Vorladen von Beschreibungen: `skill_search` findet, `skill_load` holt. Der + System-Prompt bleibt klein, weil er sonst bei 8k Kontext der teuerste Teil ist. +2. **Memory** — mem0-Anbindung, **pro Benutzer getrennt**. Benutzer kommt aus dem + Environment (wer laeuft), jeder Kanal muss einem Benutzer zugeordnet sein, + jede Eingabe ist einem Benutzer zuweisbar. +3. **A2A-Plugin** — splinter als A2A-Client (anderen Agenten als Backend) und + spaeter als Server, damit die Flotte splinter rufen kann. +4. **Remote/Jumphost** — `opencode`, `claude` und andere CLIs ueber SSH auf einer + anderen Maschine starten (neue IP, anderes Netz). Transport-Plugin mit + `ssh host -- cmd`. +5. **8k-Kontext** — mit LCM/continuous compression pruefen, was bei 8192 Token + wirklich passiert: messen, wo es bricht, nicht schaetzen. + +## Erledigt (Auswahl) + +- Werkzeuge: read, write, edit, search, list, run, echo, clock +- Freigabe im TUI (Condition Variable), Diff-Anzeige, Ctrl-C killt curl +- Session-Resume, MCP-Client, Exit-Ring, Delegation mit Modellauswahl +- Gateway (Telegram) mit Typing-Indicator und Slash-Commands +- Session-Logs: `~/.splinter/logs//{session.log,wire.jsonl}` +- Slash-Programme: `~/.splinter/commands/`, Ausgabe geht ans Modell diff --git a/include/splinter.h b/include/splinter.h index 8a55d1d..5439abf 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -253,9 +253,16 @@ int sp_builtin_register(struct sp_registry *); struct sp_config; int sp_delegate_register(struct sp_registry *, const struct sp_config *); /* Gateway mode: answers on Telegram until SIGINT/SIGTERM. */ -int sp_telegram_run(struct sp_agent *, const char *token, const char *allow, - struct sp_buf *err); +struct sp_host; +int sp_telegram_run(struct sp_agent *, struct sp_host *host, + const char *token, const char *allow, struct sp_buf *err); int sp_telegram_token(char *buf, size_t len, const char *path); +/* + * Slash commands that are programs: /commands/. The program + * gets the rest of the line, and its output becomes context for the model. + */ +int sp_slash_script(const char *dir, const char *name, const char *args, + struct sp_buf *out, struct sp_buf *err); int sp_delegate_choose(const struct sp_config *, const char *task, const char *tags, char *id, size_t cap, char *why, size_t whylen, int *score); diff --git a/libsplinter.so b/libsplinter.so index 789f3e8..7df6013 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/src/agent.c b/src/agent.c index 32fe229..01ca8f2 100644 --- a/src/agent.c +++ b/src/agent.c @@ -21,6 +21,7 @@ #include #include "splinter.h" +#include "log.h" #include "splinter_plugin.h" static int @@ -210,10 +211,16 @@ sp_agent_run(struct sp_agent *a, const char *user_text, struct sp_buf *answer, struct sp_msg tm; sp_buf_clear(&obs); + sp_logf("tool", "%s %s", reply.calls[i].name, + reply.calls[i].args != NULL ? reply.calls[i].args : ""); if (a->cfg->tool_begin != NULL) a->cfg->tool_begin(a->cfg->tool_begin_ctx, reply.calls[i].name, reply.calls[i].args); run_one_call(a, &reply.calls[i], &obs); + sp_logf("observation", "%s", obs.p != NULL ? obs.p : ""); + if (obs.p != NULL && strncmp(obs.p, "error", 5) == 0) + sp_logf("tool failed", "%s: %s", + reply.calls[i].name, obs.p); if (a->cfg->tool_end != NULL) a->cfg->tool_end(a->cfg->tool_end_ctx, reply.calls[i].name, obs.p != NULL ? obs.p : ""); diff --git a/src/cmdscript.c b/src/cmdscript.c new file mode 100644 index 0000000..7041d5b --- /dev/null +++ b/src/cmdscript.c @@ -0,0 +1,223 @@ +/* + * splinter -- slash commands that are just programs. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + * + * Anything executable in ~/.splinter/commands/ becomes /name. It gets the rest + * of the line as its first argument, and whatever it prints goes to the model as + * context - not to the operator as a message. That is the point: a command is a + * way to hand the model something it cannot get itself (a git log, a database + * query, a screenshot's OCR), without the operator copying text around. + * + * Rule: the program decides what to print; the harness decides that the model + * sees it. A program that prints nothing is a bug the operator will notice, + * because the model will be told exactly that. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "splinter.h" +#include "cmdscript.h" + +#define CMD_TIMEOUT_MS 60000 +#define CMD_MAX_BYTES 262144 + +/* + * Where the programs live: $SPLINTER_COMMANDS_DIR, else ~/.splinter/commands. + * + * Not the session directory: commands are the operator's tools, not a session's + * private property - the first version keyed them off --session and found + * nothing whenever a session lived anywhere else. + */ +int +sp_cmdscript_dir(const char *session_dir, char *buf, size_t len) +{ + const char *env = getenv("SPLINTER_COMMANDS_DIR"); + const char *home = getenv("HOME"); + + (void)session_dir; + if (env != NULL && *env != '\0') { + snprintf(buf, len, "%s", env); + return 0; + } + if (home == NULL || *home == '\0') + return -1; + snprintf(buf, len, "%s/.splinter/commands", home); + return 0; +} + +/* Is there an executable by that name? No paths, no dots: /foo is a name. */ +int +sp_cmdscript_exists(const char *dir, const char *name) +{ + char path[700]; + size_t i; + + if (dir == NULL || name == NULL || *name == '\0') + return 0; + for (i = 0; name[i] != '\0'; i++) + if (!((name[i] >= 'a' && name[i] <= 'z') || + (name[i] >= 'A' && name[i] <= 'Z') || + (name[i] >= '0' && name[i] <= '9') || + name[i] == '-' || name[i] == '_' || name[i] == '.')) + return 0; + if (strstr(name, "..") != NULL) + return 0; + snprintf(path, sizeof(path), "%s/%s", dir, name); + return access(path, X_OK) == 0; +} + +int +sp_cmdscript_list(const char *dir, struct sp_buf *out) +{ + DIR *d; + struct dirent *e; + int n = 0; + + d = opendir(dir); + if (d == NULL) + return 0; + while ((e = readdir(d)) != NULL) { + char path[700]; + + if (e->d_name[0] == '.') + continue; + snprintf(path, sizeof(path), "%s/%s", dir, e->d_name); + if (access(path, X_OK) != 0) + continue; + sp_buf_printf(out, "/%s ", e->d_name); + n++; + } + closedir(d); + return n; +} + +/* + * Run it. The argument is the whole rest of the line, one string: a command + * that wants words can split it itself, and a command that takes a commit + * message keeps its spaces. + */ +int +sp_cmdscript_run(const char *dir, const char *name, const char *args, + struct sp_buf *out, struct sp_buf *err) +{ + char path[700]; + int fds[2]; + pid_t child; + + snprintf(path, sizeof(path), "%s/%s", dir, name); + if (pipe(fds) != 0) { + sp_buf_printf(err, "%s: pipe: %s", name, strerror(errno)); + return -1; + } + child = fork(); + if (child < 0) { + sp_buf_printf(err, "%s: fork: %s", name, strerror(errno)); + close(fds[0]); + close(fds[1]); + return -1; + } + if (child == 0) { + char *argv[3]; + int devnull = open("/dev/null", O_RDONLY); + + (void)dup2(fds[1], STDOUT_FILENO); + (void)dup2(fds[1], STDERR_FILENO); + if (devnull >= 0) { + (void)dup2(devnull, STDIN_FILENO); + close(devnull); + } + close(fds[0]); + close(fds[1]); + argv[0] = path; + argv[1] = (char *)(args != NULL ? args : ""); + argv[2] = NULL; + execv(path, argv); + fprintf(stderr, "%s: %s\n", path, strerror(errno)); + _exit(126); + } + close(fds[1]); + { + struct pollfd pfd; + char chunk[4096]; + int timed_out = 0; + + for (;;) { + ssize_t got; + + pfd.fd = fds[0]; + pfd.events = POLLIN; + if (poll(&pfd, 1, CMD_TIMEOUT_MS) <= 0) { + (void)kill(child, SIGKILL); + timed_out = 1; + break; + } + got = read(fds[0], chunk, sizeof(chunk)); + if (got <= 0) + break; + if (out->len + (size_t)got > CMD_MAX_BYTES) { + sp_buf_puts(out, "\n[command output truncated]\n"); + (void)kill(child, SIGKILL); + break; + } + if (sp_buf_add(out, chunk, (size_t)got) != 0) + break; + } + close(fds[0]); + { + int st = 0; + + (void)waitpid(child, &st, 0); + if (timed_out) { + sp_buf_printf(err, "/%s: timed out after %d s", + name, CMD_TIMEOUT_MS / 1000); + return -1; + } + if (WIFEXITED(st) && WEXITSTATUS(st) != 0) + sp_buf_printf(out, "[exit status %d]\n", + WEXITSTATUS(st)); + } + } + return 0; +} + +/* + * The entry point UI and gateway share: run /name and hand back the text the + * model should see, already framed so it cannot be mistaken for the user's own + * words. + */ +int +sp_slash_script(const char *dir, const char *name, const char *args, + struct sp_buf *out, struct sp_buf *err) +{ + struct sp_buf raw, errb; + + (void)err; + if (sp_buf_init(&raw) != 0 || sp_buf_init(&errb) != 0) + return -1; + if (sp_cmdscript_run(dir, name, args, &raw, &errb) != 0) { + sp_buf_printf(out, "error: %s", errb.p != NULL ? errb.p : "failed"); + sp_buf_free(&raw); + sp_buf_free(&errb); + return 0; + } + if (raw.p == NULL || raw.p[0] == '\0') + sp_buf_printf(out, "Der Befehl /%s hat nichts ausgegeben.", name); + else + sp_buf_printf(out, "Ausgabe von /%s%s%s:\n\n%s", name, + args != NULL && *args != '\0' ? " " : "", + args != NULL ? args : "", raw.p); + sp_buf_free(&raw); + sp_buf_free(&errb); + return 0; +} diff --git a/src/cmdscript.h b/src/cmdscript.h new file mode 100644 index 0000000..7a25a54 --- /dev/null +++ b/src/cmdscript.h @@ -0,0 +1,21 @@ +/* + * splinter -- programs as slash commands. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + */ +#ifndef SPLINTER_CMDSCRIPT_H +#define SPLINTER_CMDSCRIPT_H + +#include +#include + +#include "splinter.h" + +int sp_cmdscript_dir(const char *session_dir, char *buf, size_t len); +int sp_cmdscript_exists(const char *dir, const char *name); +int sp_cmdscript_list(const char *dir, struct sp_buf *out); +int sp_cmdscript_run(const char *dir, const char *name, const char *args, + struct sp_buf *out, struct sp_buf *err); + +#endif /* SPLINTER_CMDSCRIPT_H */ diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..6232ede --- /dev/null +++ b/src/log.c @@ -0,0 +1,143 @@ +/* + * splinter -- what happened, on disk. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + * + * Every session keeps a log directory next to it: + * + * ~/.splinter/logs//session.log readable, everything, in order + * ~/.splinter/logs//wire.jsonl request bodies and replies, raw + * + * "Everything" means everything: the user's words, the model's answer, its + * reasoning, every tool call with its arguments, every observation, the token + * counts, and every failure. A log that only keeps what looked good is a log + * that cannot explain a bad turn. + * + * Text is written as it arrived - no truncation, no escaping - so a diff or a + * stack trace reads the same as it did on screen. Records are separated by a + * blank line and start with a timestamp and the kind. + */ +#include +#include +#include +#include +#include + +#include "splinter.h" +#include "log.h" + +static char log_path[700]; +static char wire_path[700]; +static int active; + +static void +mkdirs(const char *path) +{ + char tmp[700]; + size_t i; + + snprintf(tmp, sizeof(tmp), "%s", path); + for (i = 1; tmp[i] != '\0'; i++) { + if (tmp[i] != '/') + continue; + tmp[i] = '\0'; + (void)mkdir(tmp, 0700); + tmp[i] = '/'; + } + (void)mkdir(tmp, 0700); +} + +int +sp_log_open(const char *session_dir, const char *id) +{ + if (session_dir == NULL || id == NULL) + return -1; + snprintf(log_path, sizeof(log_path), "%s/logs/%s", session_dir, id); + mkdirs(log_path); + snprintf(wire_path, sizeof(wire_path), "%s/logs/%s/wire.jsonl", + session_dir, id); + active = 1; + return 0; +} + +int +sp_log_active(void) +{ + return active; +} + +/* + * One record: timestamp, kind, then the text untouched, then a blank line. + * Append-only and unbuffered per call, so a crash leaves what happened up to + * the crash instead of nothing. + */ +void +sp_logf(const char *kind, const char *fmt, ...) +{ + char path[800], stamp[32]; + time_t now = time(NULL); + struct tm tm; + va_list ap; + FILE *f; + + if (!active) + return; + (void)localtime_r(&now, &tm); + strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", &tm); + snprintf(path, sizeof(path), "%s/session.log", log_path); + f = fopen(path, "a"); + if (f == NULL) + return; + fprintf(f, "[%s] %s\n", stamp, kind); + va_start(ap, fmt); + vfprintf(f, fmt, ap); + va_end(ap); + fprintf(f, "\n\n"); + fclose(f); +} + +/* The wire: what was actually sent and received, one JSON record per line. */ +void +sp_log_wire(const char *dir, const char *direction, const char *body, + size_t len) +{ + char path[800]; + FILE *f; + + if (!active || body == NULL) + return; + snprintf(path, sizeof(path), "%s/wire.jsonl", dir); + f = fopen(path, "a"); + if (f == NULL) + return; + fprintf(f, "{\"dir\":\"%s\",\"bytes\":%zu,\"body\":\"", direction, len); + { + size_t i; + + for (i = 0; i < len && i < 262144; i++) { + unsigned char c = (unsigned char)body[i]; + + if (c == '"' || c == '\\') + fprintf(f, "\\%c", c); + else if (c == '\n') + fputs("\\n", f); + else if (c == '\r') + fputs("\\r", f); + else if (c == '\t') + fputs("\\t", f); + else if (c < 0x20) + fprintf(f, "\\u%04x", c); + else + fputc(c, f); + } + } + fprintf(f, "\"}\n"); + fclose(f); +} + +const char * +sp_log_dir(void) +{ + return active ? log_path : NULL; +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..b4ee919 --- /dev/null +++ b/src/log.h @@ -0,0 +1,24 @@ +/* + * splinter -- session logs. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + */ +#ifndef SPLINTER_LOG_H +#define SPLINTER_LOG_H + +#include + +/* Open the log directory for a session: /logs//. */ +int sp_log_open(const char *session_dir, const char *id); +int sp_log_active(void); +const char *sp_log_dir(void); + +/* Append a record: timestamp, kind, text, blank line. */ +void sp_logf(const char *kind, const char *fmt, ...); + +/* Append one wire record (request/reply) to wire.jsonl in the same directory. */ +void sp_log_wire(const char *dir, const char *direction, const char *body, + size_t len); + +#endif /* SPLINTER_LOG_H */ diff --git a/src/main.c b/src/main.c index 4bda4e8..16a8607 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ #include "splinter.h" #include "config.h" #include "i18n.h" +#include "log.h" #include "splinter_plugin.h" #include "theme.h" #include "ui.h" @@ -387,6 +388,8 @@ main(int argc, char **argv) return 3; } ag.sess = &sess; + (void)sp_log_open(sess.dir, sess.id); + sp_logf("session", "started: %s", sess.id); if (nplugins > 0) { @@ -501,7 +504,7 @@ main(int argc, char **argv) "nobody would be answered\n"); return 2; } - rc = sp_telegram_run(&ag, token, conf.telegram_allow, &err); + 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"); diff --git a/src/session.c b/src/session.c index c1d516f..19c548c 100644 --- a/src/session.c +++ b/src/session.c @@ -18,6 +18,7 @@ #include "splinter.h" #include "mcp.h" +#include "log.h" #include #include diff --git a/src/telegram.c b/src/telegram.c index 3a9239e..249107f 100644 --- a/src/telegram.c +++ b/src/telegram.c @@ -21,6 +21,7 @@ */ #include #include +#include #include #include #include @@ -32,6 +33,9 @@ #include "splinter.h" #include "mcp.h" +#include "log.h" +#include "cmdscript.h" +#include "splinter_plugin.h" #define TG_API "https://api.telegram.org/bot" #define TG_MAX_CHUNK 3500 @@ -39,6 +43,48 @@ static volatile int tg_stop; +static int tg_curl(const char *url, const char *post_body, struct sp_buf *out, + struct sp_buf *err); + +/* Telegram shows "typing" for about five seconds; a turn takes longer. */ +struct typing { + char token[256]; + long chat_id; + volatile int *stop; +}; + +static void * +typing_thread(void *arg) +{ + struct typing *t = arg; + struct sp_buf url, reply, err; + int i; + + if (sp_buf_init(&url) != 0 || sp_buf_init(&reply) != 0 || + sp_buf_init(&err) != 0) + return NULL; + sp_buf_printf(&url, "%s%s/sendChatAction", TG_API, t->token); + { + struct sp_buf body; + + if (sp_buf_init(&body) != 0) + return NULL; + sp_buf_printf(&body, "{\"chat_id\":%ld,\"action\":\"typing\"}", + t->chat_id); + for (i = 0; i < 300 && *t->stop == 0; i++) { + sp_buf_clear(&reply); + (void)tg_curl(url.p, body.p, &reply, &err); + sp_buf_clear(&err); + sleep(4); + } + sp_buf_free(&body); + } + sp_buf_free(&url); + sp_buf_free(&reply); + sp_buf_free(&err); + return NULL; +} + static void on_signal(int sig) { @@ -198,8 +244,8 @@ allowed(const char *allow, long id) } int -sp_telegram_run(struct sp_agent *ag, const char *token, const char *allow, - struct sp_buf *err) +sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token, + const char *allow, struct sp_buf *err) { long offset = 0; struct sp_buf url, reply, answer, aerr; @@ -246,7 +292,12 @@ sp_telegram_run(struct sp_agent *ag, const char *token, const char *allow, for (i = 0; (update = mcp_array_at(result, i)) != NULL; i++) { const mcp_json *msg; - const char *text; + const char *text, *prompt_used = NULL; + char prompt[4096]; + pthread_t tty; + volatile int tstop = 0; + struct typing ty; + int typing_on = 0; long chat_id, from; int update_id; @@ -269,22 +320,84 @@ sp_telegram_run(struct sp_agent *ag, const char *token, const char *allow, } sp_buf_clear(&answer); sp_buf_clear(&aerr); - if (sp_agent_run(ag, text, &answer, &aerr) != 0) { + /* + * A slash command is the harness talking, not the + * model: /goal and /recap come from plugins and + * answer without a request. + */ + if (text[0] == '/' && text[1] != '\0') { + char name[32], *rest; + size_t n = strcspn(text + 1, " "); + int crc; + + snprintf(name, sizeof(name), "%.*s", + (int)n, text + 1); + rest = (char *)text + 1 + n; + while (*rest == ' ') + rest++; + crc = host != NULL ? + sp_host_command(host, name, rest, + &answer) : -1; + if (crc == 0) { + (void)send_message(token, chat_id, + answer.p != NULL ? answer.p : + "(leer)", err); + sp_buf_clear(err); + continue; + } + /* + * Not a plugin command: a program in + * commands/ gets the rest of the line and its + * output becomes the model's context. + */ + { + char cdir[700]; + struct sp_buf cerr; + + sp_buf_init(&cerr); + if (sp_cmdscript_dir(ag->sess != NULL ? + ag->sess->dir : NULL, cdir, + sizeof(cdir)) == 0 && + sp_cmdscript_exists(cdir, name) && + sp_slash_script(cdir, name, rest, + &answer, &cerr) == 0 && + answer.p != NULL) { + snprintf(prompt, sizeof(prompt), + "%s", answer.p); + prompt_used = prompt; + } + sp_buf_free(&cerr); + } + } + /* "typing" while the model thinks, not while a + * plugin answers a slash command */ + snprintf(ty.token, sizeof(ty.token), "%s", token); + ty.chat_id = chat_id; + ty.stop = &tstop; + if (pthread_create(&tty, NULL, typing_thread, + &ty) == 0) + typing_on = 1; + + if (sp_agent_run(ag, prompt_used != NULL ? + prompt_used : text, &answer, &aerr) != 0) { (void)send_message(token, chat_id, aerr.p != NULL ? aerr.p : "turn failed", err); + if (typing_on) { + tstop = 1; + (void)pthread_join(tty, NULL); + } sp_buf_clear(err); continue; } + if (typing_on) { + tstop = 1; + (void)pthread_join(tty, NULL); + } (void)send_message(token, chat_id, answer.p != NULL ? answer.p : "(leer)", err); sp_buf_clear(err); - if (ag->sess != NULL && answer.p != NULL) - (void)sp_session_append(ag->sess, "user", - text, err); - if (ag->sess != NULL && answer.p != NULL) - (void)sp_session_append(ag->sess, - "assistant", answer.p, err); + /* the agent already recorded both sides */ } } mcp_json_free(v); diff --git a/src/transport_openai.c b/src/transport_openai.c index c715013..8534ddc 100644 --- a/src/transport_openai.c +++ b/src/transport_openai.c @@ -24,6 +24,7 @@ #include #include "splinter.h" +#include "log.h" struct openai_ctx { char url[512]; @@ -174,6 +175,7 @@ openai_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, goto out; } dump_request(body.p, body.len); + sp_log_wire(sp_log_dir(), "request", body.p, body.len); n = body.len; w = write(fd, body.p, n); close(fd); @@ -220,6 +222,8 @@ openai_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, } unlink(path); + sp_log_wire(sp_log_dir(), "response", raw.p != NULL ? raw.p : "", + raw.len); if (raw.len < 2 || (nl = strrchr(raw.p, '\n')) == NULL) { sp_buf_puts(err, "openai: unparseable response (no status)"); goto out; @@ -289,6 +293,7 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out, goto out; } dump_request(body.p, body.len); + sp_log_wire(sp_log_dir(), "request", body.p, body.len); n = body.len; w = write(fd, body.p, n); close(fd); @@ -381,6 +386,7 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out, } if (sp_sse_finish(sse, out, err) != 0) goto out; + sp_logf("reply", "%s", out->content != NULL ? out->content : ""); if (out->content != NULL && out->content[0] == '\0' && out->ncalls == 0) { sp_buf_printf(err, "openai: stream ended with no answer: %.300s", tail.p != NULL ? tail.p : "(empty)"); diff --git a/src/ui.c b/src/ui.c index 454dc32..f3ac801 100644 --- a/src/ui.c +++ b/src/ui.c @@ -35,6 +35,8 @@ #include "splinter.h" #include "i18n.h" +#include "log.h" +#include "cmdscript.h" #include "splinter_plugin.h" #include "theme.h" #include "ui.h" @@ -1051,7 +1053,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, * never reaches the model, and its reply is shown as the harness * speaking. Unknown commands list what exists. */ - if (input[0] == '/' && host != NULL) { + if (input[0] == '/' && input[1] != '\0') { char name[64], args[1536], outbuf[2048]; struct sp_buf out; const char *sp; @@ -1071,18 +1073,53 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, } if (sp_buf_init(&out) != 0) continue; - 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); + if (host != NULL && + sp_host_command(host, name, args, &out) == 0) { + snprintf(outbuf, sizeof(outbuf), "%s", + out.p != NULL ? out.p : ""); + sp_buf_free(&out); + add_entry(&s, "splinter", outbuf); + continue; + } + /* + * Not a plugin command. A program in commands/ wins over + * 'unknown': its output is not a reply for the operator, + * it is context for the model, so the turn runs below. + */ + { + char cdir[700]; + struct sp_buf cerr; + + sp_buf_clear(&out); + sp_buf_init(&cerr); + if (sp_cmdscript_dir(ag->sess != NULL ? + ag->sess->dir : NULL, cdir, + sizeof(cdir)) == 0 && + sp_cmdscript_exists(cdir, name) && + sp_slash_script(cdir, name, args, &out, + &cerr) == 0 && out.p != NULL) { + add_entry(&s, "shell", out.p); + snprintf(input, sizeof(input), "%s", + out.p); + sp_buf_free(&out); + sp_buf_free(&cerr); + } else { + sp_buf_free(&out); + sp_buf_free(&cerr); + if (sp_buf_init(&out) != 0) + continue; + 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); + snprintf(outbuf, sizeof(outbuf), "%s", + out.p != NULL ? out.p : ""); + sp_buf_free(&out); + add_entry(&s, "splinter", outbuf); + continue; + } } - snprintf(outbuf, sizeof(outbuf), "%s", - out.p != NULL ? out.p : ""); - sp_buf_free(&out); - add_entry(&s, "splinter", outbuf); - continue; } hist_push(&s, ag->sess != NULL ? ag->sess->dir : NULL, input); @@ -1402,6 +1439,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, * before, one layer deeper. It grew here, it stays here. */ if (tu.think.len > 0) { + sp_logf("thinking", "%s", tu.think.p); if (think_idx == (size_t)-1) { think_idx = s.n; add_entry(&s, "thinking", diff --git a/tests/test_ascii b/tests/test_ascii index 8a57325..0ce44ef 100755 Binary files a/tests/test_ascii and b/tests/test_ascii differ