diff --git a/README.md b/README.md index 544bdb1..19565db 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,21 @@ key that `en.msg` has. Ships with `en` and `de`. Plugins translate their own strings with `sp_t()`. +## Piped input + + cat /etc/passwd | splinter "explain the file" + +Anything on standard input becomes context, framed and labelled with its size: + + [stdin: 12 bytes] + ... + [/stdin] + +The question and the bytes are kept apart, so the model knows which is which. A +binary input is reported as binary instead of being sprayed into the request, the +size is capped at 192 KiB (with `[truncated]`), and `--no-stdin` ignores the pipe +entirely. With no prompt at all, the input becomes the prompt. + ## Gateway mode `--daemon` turns splinter into a Telegram bot: long-poll `getUpdates`, one turn diff --git a/src/main.c b/src/main.c index 21b46f3..c564049 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ */ #include #include +#include #include #include #include @@ -48,6 +49,7 @@ usage(FILE *f) " --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" " --autonomous run mutating tools without asking\n" " --steps N tool rounds per turn (default 8)\n" " --session DIR session directory (default ~/.splinter)\n" @@ -110,6 +112,52 @@ stdout_stream(void *ctx, int kind, const char *text) 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 @@ -143,7 +191,7 @@ main(int argc, char **argv) struct sp_mock mock; int have_mock = 0; int i, rc, chatmode = 0, resume = 0; - int daemon_mode = 0; + int daemon_mode = 0, no_stdin = 0; struct sp_config conf; const char *choose_text = NULL; const char *plugins[16]; @@ -242,6 +290,15 @@ main(int argc, char **argv) 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; @@ -258,6 +315,44 @@ main(int argc, char **argv) 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; + + if (!no_stdin && !chatmode && !daemon_mode && !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];