/model offers the models the configuration names -- the main endpoint, every delegate with a model, every exit -- as inline buttons and points the agent's transport at the one that is pressed. /model <name> keeps the endpoint and takes another model from it. A provider whose key variable is not exported is marked in the menu. The choice is not written: it lasts for the session, a restart goes back to the config. /connect asks for name, endpoint, model and key variable, validates each answer, and writes the delegate line to <config>.d/20-connect.conf before anything is used, so the same run has the provider and the next start loads it. and [?2031h]10;?]11;?[>0q[?25l[s[6nP+q4d73\[?1016$p[?2027$p[?2031$p[?1004$p[?2004$p[?2026$p[?u]99;i=opentui-notifications:p=?;\]1337;Capabilities\]7501;?\[H]66;w=1; \[6n[H]66;s=2; \[6n[u[s[?1049h[?25l[>4;1m[?2004h[?1000h[?1002h[?1003h[?1006h[14t]7501;state=idle:app=opencode\[?2026h[?25l[1;1H[38;2;255;255;255m[48;2;17;28;24m [2;1H[38;2;255;255;255m[48;2;17;28;24m [3;1H[38;2;255;255;255m[48;2;17;28;24m [4;1H[38;2;255;255;255m[48;2;17;28;24m [5;1H[38;2;255;255;255m[48;2;17;28;24m [6;1H[38;2;255;255;255m[48;2;17;28;24m [7;1H[38;2;255;255;255m[48;2;17;28;24m [8;1H[38;2;255;255;255m[48;2;17;28;24m [9;1H[38;2;255;255;255m[48;2;17;28;24m [10;1H[38;2;255;255;255m[48;2;17;28;24m [11;1H[38;2;255;255;255m[48;2;17;28;24m [12;1H[38;2;255;255;255m[48;2;17;28;24m [13;1H[38;2;255;255;255m[48;2;17;28;24m [14;1H[38;2;255;255;255m[48;2;17;28;24m [15;1H[38;2;255;255;255m[48;2;17;28;24m [16;1H[38;2;255;255;255m[48;2;17;28;24m [17;1H[38;2;255;255;255m[48;2;17;28;24m [18;1H[38;2;255;255;255m[48;2;17;28;24m [19;1H[38;2;255;255;255m[48;2;17;28;24m [20;1H[38;2;255;255;255m[48;2;17;28;24m [21;1H[38;2;255;255;255m[48;2;17;28;24m [22;1H[38;2;255;255;255m[48;2;17;28;24m [23;1H[38;2;255;255;255m[48;2;17;28;24m [24;1H[38;2;255;255;255m[48;2;17;28;24m [0m]22;default\[?2026l]0;OpenCode[?2026h[?25l[7;22H[38;2;83;104;91m[48;2;17;28;24m [0m[7;42H[38;2;193;196;151m[48;2;17;28;24m[1m ▄[7;56H [0m[8;22H[38;2;83;104;91m[48;2;17;28;24m█[8;23H▀[8;24H▀[8;25H█[8;26H █[8;28H▀[8;29H▀[8;30H█[8;31H █[8;33H▀[8;34H▀[8;35H█[8;36H █[8;38H▀[8;39H▀[8;40H▄[8;41H[0m[8;42H[38;2;193;196;151m[48;2;17;28;24m[1m█[8;43H▀[8;44H▀[8;45H▀[8;46H █[8;48H▀[8;49H▀[8;50H█[8;51H █[8;53H▀[8;54H▀[8;55H█[8;56H █[8;58H▀[8;59H▀[8;60H█[8;61H[0m[9;22H[38;2;83;104;91m[48;2;17;28;24m█[9;23H[0m[9;23H[38;2;83;104;91m[48;2;34;47;41m [0m[9;25H[38;2;83;104;91m[48;2;17;28;24m█[9;26H █[9;28H[0m[9;28H[38;2;83;104;91m[48;2;34;47;41m [0m[9;30H[38;2;83;104;91m[48;2;17;28;24m█[9;31H █[9;33H[0m[9;33H[38;2;83;104;91m[48;2;34;47;41m▀[9;34H▀[9;35H▀[9;36H[0m[9;36H[38;2;83;104;91m[48;2;17;28;24m █[9;38H[0m[9;38H[38;2;83;104;91m[48;2;34;47;41m [0m[9;40H[38;2;83;104;91m[48;2;17;28;24m█[9;41H[0m[9;42H[38;2;193;196;151m[48;2;17;28;24m[1m█[9;43H[0m[9;43H[38;2;193;196;151m[48;2;61;70;56m[1m [0m[9;46H[38;2;193;196;151m[48;2;17;28;24m[1m █[9;48H[0m[9;48H[38;2;193;196;151m[48;2;61;70;56m[1m [0m[9;50H[38;2;193;196;151m[48;2;17;28;24m[1m█[9;51H █[9;53H[0m[9;53H[38;2;193;196;151m[48;2;61;70;56m[1m [0m[9;55H[38;2;193;196;151m[48;2;17;28;24m[1m█[9;56H █[9;58H[0m[9;58H[38;2;193;196;151m[48;2;61;70;56m[1m▀[9;59H▀[9;60H▀[9;61H[0m[10;22H[38;2;83;104;91m[48;2;17;28;24m▀[10;23H▀[10;24H▀[10;25H▀[10;26H █[10;28H▀[10;29H▀[10;30H▀[10;31H ▀[10;33H▀[10;34H▀[10;35H▀[10;36H ▀[10;38H[0m[10;38H[38;2;34;47;41m[48;2;17;28;24m▀[10;39H▀[10;40H[0m[10;40H[38;2;83;104;91m[48;2;17;28;24m▀[10;41H[0m[10;42H[38;2;193;196;151m[48;2;17;28;24m[1m▀[10;43H▀[10;44H▀[10;45H▀[10;46H ▀[10;48H▀[10;49H▀[10;50H▀[10;51H ▀[10;53H▀[10;54H▀[10;55H▀[10;56H ▀[10;58H▀[10;59H▀[10;60H▀[10;61H[0m[13;4H[38;2;61;74;68m[48;2;17;28;24m┃[0m[13;5H[38;2;255;255;255m[48;2;35;55;43m [0m[14;4H[38;2;61;74;68m[48;2;17;28;24m┃[0m[14;5H[38;2;255;255;255m[48;2;35;55;43m [0m[14;7H[38;2;83;104;91m[48;2;35;55;43mAsk anything…[14;20H "What is the tech stack of this project?"[0m[14;62H[38;2;255;255;255m[48;2;35;55;43m [0m[15;4H[38;2;61;74;68m[48;2;17;28;24m┃[0m[15;5H[38;2;255;255;255m[48;2;35;55;43m [0m[16;4H[38;2;61;74;68m[48;2;17;28;24m┃[0m[16;5H[38;2;255;255;255m[48;2;35;55;43m [0m[17;4H[38;2;61;74;68m[48;2;17;28;24m╹[0m[17;5H[38;2;35;55;43m[48;2;17;28;24m▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀[0m[18;4H[38;2;83;104;91m[48;2;17;28;24m~/src/splinter:main[0m[18;46H[38;2;193;196;151m[48;2;17;28;24mshift+tab [0m[18;56H[38;2;83;104;91m[48;2;17;28;24magents[0m[18;64H[38;2;193;196;151m[48;2;17;28;24mctrl+p [0m[18;71H[38;2;83;104;91m[48;2;17;28;24mcommands[0m[23;74H[38;2;83;104;91m[48;2;17;28;24m2.2.6[0m[0m]12;#c1c497[1 q[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;67;74;68m[48;2;17;28;24m┃[0m[14;4H[38;2;67;74;68m[48;2;17;28;24m┃[0m[15;4H[38;2;67;74;68m[48;2;17;28;24m┃[0m[16;4H[38;2;67;74;68m[48;2;17;28;24m┃[0m[16;7H[38;2;41;56;44m[48;2;35;55;43mBuild[0m[16;13H[38;2;36;56;44m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;39;59;46m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;36;56;44m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;67;74;68m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;82;75;68m[48;2;17;28;24m┃[0m[14;4H[38;2;82;75;68m[48;2;17;28;24m┃[0m[15;4H[38;2;82;75;68m[48;2;17;28;24m┃[0m[16;4H[38;2;82;75;68m[48;2;17;28;24m┃[0m[16;7H[38;2;58;58;46m[48;2;35;55;43mBuild[0m[16;13H[38;2;40;60;48m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;52;70;54m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;40;60;48m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;82;75;68m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;104;76;68m[48;2;17;28;24m┃[0m[14;4H[38;2;104;76;68m[48;2;17;28;24m┃[0m[15;4H[38;2;104;76;68m[48;2;17;28;24m┃[0m[16;4H[38;2;104;76;68m[48;2;17;28;24m┃[0m[16;7H[38;2;83;61;49m[48;2;35;55;43mBuild[0m[16;13H[38;2;46;66;54m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;70;86;67m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;46;66;54m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;104;76;68m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;130;77;68m[48;2;17;28;24m┃[0m[14;4H[38;2;130;77;68m[48;2;17;28;24m┃[0m[15;4H[38;2;130;77;68m[48;2;17;28;24m┃[0m[16;4H[38;2;130;77;68m[48;2;17;28;24m┃[0m[16;7H[38;2;114;65;52m[48;2;35;55;43mBuild[0m[16;13H[38;2;52;72;60m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;91;105;82m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;52;72;60m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;130;77;68m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;159;79;69m[48;2;17;28;24m┃[0m[14;4H[38;2;159;79;69m[48;2;17;28;24m┃[0m[15;4H[38;2;159;79;69m[48;2;17;28;24m┃[0m[16;4H[38;2;159;79;69m[48;2;17;28;24m┃[0m[16;7H[38;2;146;69;56m[48;2;35;55;43mBuild[0m[16;13H[38;2;59;80;67m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;115;126;98m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;59;80;67m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;159;79;69m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;199;80;69m[48;2;17;28;24m┃[0m[14;4H[38;2;199;80;69m[48;2;17;28;24m┃[0m[15;4H[38;2;199;80;69m[48;2;17;28;24m┃[0m[16;4H[38;2;199;80;69m[48;2;17;28;24m┃[0m[16;7H[38;2;191;75;61m[48;2;35;55;43mBuild[0m[16;13H[38;2;69;90;77m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;147;155;120m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;69;90;77m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;199;80;69m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;224;82;69m[48;2;17;28;24m┃[0m[14;4H[38;2;224;82;69m[48;2;17;28;24m┃[0m[15;4H[38;2;224;82;69m[48;2;17;28;24m┃[0m[16;4H[38;2;224;82;69m[48;2;17;28;24m┃[0m[16;7H[38;2;220;78;65m[48;2;35;55;43mBuild[0m[16;13H[38;2;75;96;83m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;168;173;134m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;75;96;83m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;224;82;69m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;243;82;69m[48;2;17;28;24m┃[0m[14;4H[38;2;243;82;69m[48;2;17;28;24m┃[0m[15;4H[38;2;243;82;69m[48;2;17;28;24m┃[0m[16;4H[38;2;243;82;69m[48;2;17;28;24m┃[0m[16;7H[38;2;241;81;67m[48;2;35;55;43mBuild[0m[16;13H[38;2;80;101;88m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;183;187;144m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;80;101;88m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;243;82;69m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;254;83;69m[48;2;17;28;24m┃[0m[14;4H[38;2;254;83;69m[48;2;17;28;24m┃[0m[15;4H[38;2;254;83;69m[48;2;17;28;24m┃[0m[16;4H[38;2;254;83;69m[48;2;17;28;24m┃[0m[16;7H[38;2;254;83;69m[48;2;35;55;43mBuild[0m[16;13H[38;2;83;104;91m[48;2;35;55;43m·[16;14H[0m[16;15H[38;2;192;195;151m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[16;40H[38;2;83;104;91m[48;2;35;55;43mOllama Cloud[0m[17;4H[38;2;254;83;69m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l[?2026h[?25l[13;4H[38;2;255;83;69m[48;2;17;28;24m┃[0m[14;4H[38;2;255;83;69m[48;2;17;28;24m┃[0m[15;4H[38;2;255;83;69m[48;2;17;28;24m┃[0m[16;4H[38;2;255;83;69m[48;2;17;28;24m┃[0m[16;7H[38;2;255;83;69m[48;2;35;55;43mBuild[0m[16;15H[38;2;193;196;151m[48;2;35;55;43mDeepSeek V4.1 Flash 1017[0m[17;4H[38;2;255;83;69m[48;2;17;28;24m╹[0m[0m[14;7H[?25h[?2026l]7501;state=clear\[?25h[0m]22;\[>4;0m[?1003l[?1002l[?1000l[?1006l[?2004l[?1049l[?2031l]0;]12;default]112[0 q[?25h[?25h stand for their usual endpoints. The drop-in goes next to the file that was actually read (sp_config_loaded_path), not next to a path guessed from the environment, so --config works. providers.c holds the list, the validation, the transport switch and the provider identity main.c used to compute inline. test_providers covers the list, the refusals, replacement of a line and a reload of what was written; test_gateway presses the model button, proves the next turn goes to the new endpoint (the test one is dead) and runs /connect to the written file.
354 lines
12 KiB
C
354 lines
12 KiB
C
/*
|
|
* splinter -- a small C99/POSIX agent harness.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* Design rules this header encodes:
|
|
* - no fixed-size buffers anywhere; every growing thing is a struct sp_buf
|
|
* with geometric growth and overflow checks;
|
|
* - a tool declares its authorization class; the harness decides whether it
|
|
* may run, never the model and never a prompt;
|
|
* - a session is append-only JSONL, so a crash can only truncate its tail;
|
|
* - the transport is a vtable, so the whole loop is testable with no network.
|
|
*/
|
|
#ifndef SPLINTER_H
|
|
#define SPLINTER_H
|
|
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
|
|
#define SP_VERSION "0.1.0"
|
|
|
|
/* ------------------------------------------------------------------ buffer */
|
|
|
|
struct sp_buf {
|
|
char *p;
|
|
size_t len; /* bytes used, excluding the NUL we keep at p[len] */
|
|
size_t cap; /* allocated bytes, always > len */
|
|
};
|
|
|
|
int sp_buf_init(struct sp_buf *);
|
|
int sp_buf_add(struct sp_buf *, const char *, size_t);
|
|
int sp_buf_puts(struct sp_buf *, const char *);
|
|
int sp_buf_printf(struct sp_buf *, const char *, ...);
|
|
int sp_buf_reserve(struct sp_buf *, size_t);
|
|
void sp_buf_clear(struct sp_buf *);
|
|
void sp_buf_free(struct sp_buf *);
|
|
|
|
/* --------------------------------------------------------------- escaping */
|
|
|
|
/* Append s as a JSON string body (no surrounding quotes). */
|
|
int sp_json_escape(struct sp_buf *, const char *);
|
|
|
|
/* --------------------------------------------------------------- tools */
|
|
|
|
enum sp_auth {
|
|
SP_AUTH_READ = 0, /* may run without asking */
|
|
SP_AUTH_MUTATE, /* changes files or sends data out: consent */
|
|
SP_AUTH_DESTRUCTIVE /* runs commands: needs operator consent */
|
|
};
|
|
|
|
struct sp_tool;
|
|
|
|
/* Return 0 on success. The observation is written into out. */
|
|
typedef int (*sp_tool_run)(const char *args_json, struct sp_buf *out,
|
|
void *ctx);
|
|
|
|
struct sp_tool {
|
|
const char *name;
|
|
const char *description;
|
|
const char *params; /* JSON schema of the arguments, may be NULL */
|
|
enum sp_auth auth;
|
|
sp_tool_run run;
|
|
void *ctx;
|
|
};
|
|
|
|
struct sp_registry {
|
|
struct sp_tool *tools;
|
|
size_t len;
|
|
size_t cap;
|
|
void *ctx; /* the builtin that owns the tools, if any */
|
|
};
|
|
|
|
int sp_registry_add(struct sp_registry *, const struct sp_tool *);
|
|
const struct sp_tool *sp_registry_find(const struct sp_registry *,
|
|
const char *);
|
|
size_t sp_registry_count(const struct sp_registry *);
|
|
void sp_registry_free(struct sp_registry *);
|
|
|
|
/* ---------------------------------------------------------- conversation */
|
|
|
|
struct sp_msg {
|
|
char *role; /* system | user | assistant | tool */
|
|
char *name; /* tool name when role is "tool" */
|
|
char *call_id; /* tool_call id, when role is "tool" */
|
|
char *content;
|
|
char *calls; /* raw tool_calls JSON of an assistant turn */
|
|
};
|
|
|
|
struct sp_conv {
|
|
struct sp_msg *v;
|
|
size_t n;
|
|
size_t cap;
|
|
};
|
|
|
|
void sp_conv_init(struct sp_conv *);
|
|
void sp_conv_free(struct sp_conv *);
|
|
int sp_conv_add(struct sp_conv *, const struct sp_msg *);
|
|
int sp_conv_insert(struct sp_conv *, size_t at, const struct sp_msg *);
|
|
int sp_conv_push(struct sp_conv *, const char *role, const char *name,
|
|
const char *content);
|
|
size_t sp_conv_bytes(const struct sp_conv *);
|
|
int sp_conv_trim(struct sp_conv *, size_t keep_last);
|
|
|
|
/* ------------------------------------------------------------- transport */
|
|
|
|
struct sp_toolcall {
|
|
char *id;
|
|
char *name;
|
|
char *args; /* the arguments as a JSON text */
|
|
};
|
|
|
|
struct sp_reply {
|
|
long status;
|
|
long tokens_in; /* provider-reported, 0 when unknown */
|
|
long tokens_out;
|
|
long retry_after_s; /* parsed Retry-After header, 0 unknown */
|
|
char *content; /* assistant text, may be "" */
|
|
struct sp_toolcall *calls;
|
|
char *calls_json; /* the provider's own tool_calls array */
|
|
size_t ncalls;
|
|
};
|
|
|
|
/*
|
|
* A transport turns a whole conversation into one request and one reply. The
|
|
* seam is the conversation, not a raw string: a provider adapter cannot invent
|
|
* history, and the loop cannot depend on a vendor's JSON.
|
|
*/
|
|
/*
|
|
* Streaming: a transport that can deliver a turn incrementally reports text
|
|
* deltas and tool-call starts through this callback while it reads. A transport
|
|
* that cannot simply leaves send_stream NULL and the loop uses send().
|
|
*/
|
|
enum {
|
|
SP_STREAM_TEXT = 1, /* a chunk of the assistant's answer */
|
|
SP_STREAM_TOOL, /* a tool call has begun: text is its name */
|
|
SP_STREAM_THINK /* the model is reasoning: show it, do not use it */
|
|
};
|
|
|
|
typedef void (*sp_stream_cb)(void *ctx, int kind, const char *text);
|
|
|
|
struct sp_transport {
|
|
int (*send)(void *ctx, const struct sp_conv *, struct sp_reply *out,
|
|
struct sp_buf *err);
|
|
int (*send_stream)(void *ctx, const struct sp_conv *,
|
|
struct sp_reply *out, struct sp_buf *err,
|
|
sp_stream_cb, void *cbctx);
|
|
void *ctx;
|
|
/* optional: provider/key identity for quota/usage tracking (plugins) */
|
|
const char *provider; /* e.g. "openai-compat", "ollama", "opencode" */
|
|
const char *key_id; /* stable key fingerprint/id, never the secret */
|
|
};
|
|
|
|
void sp_reply_free(struct sp_reply *);
|
|
|
|
/* A deterministic in-process transport: replies are queued FIFO. */
|
|
struct sp_mock {
|
|
struct sp_reply *queue;
|
|
size_t head;
|
|
size_t len;
|
|
size_t cap;
|
|
struct sp_buf last_request;
|
|
size_t requests;
|
|
};
|
|
|
|
int sp_mock_init(struct sp_mock *);
|
|
int sp_mock_push(struct sp_mock *, long status, const char *body);
|
|
void sp_mock_free(struct sp_mock *);
|
|
struct sp_transport sp_mock_transport(struct sp_mock *);
|
|
|
|
/* --------------------------------------------------------------- session */
|
|
|
|
struct sp_session_info {
|
|
char id[64];
|
|
char dir[512];
|
|
};
|
|
|
|
int sp_session_open(struct sp_session_info *, const char *dir,
|
|
const char *id, struct sp_buf *err);
|
|
int sp_session_append(const struct sp_session_info *, const char *role,
|
|
const char *text, struct sp_buf *err);
|
|
int sp_session_count(const struct sp_session_info *, size_t *n,
|
|
struct sp_buf *err);
|
|
int sp_session_latest(const char *dir, char *id, size_t cap);
|
|
/* The newest session started in this directory - what '-c' means. */
|
|
int sp_session_last_for(const char *dir, const char *cwd, char *id,
|
|
size_t cap);
|
|
/* What happened in a directory: sessions, turns, last question. */
|
|
int sp_session_folder_history(const char *dir, const char *cwd,
|
|
unsigned limit, struct sp_buf *out);
|
|
int sp_session_load(const struct sp_session_info *, struct sp_conv *conv,
|
|
size_t *loaded, struct sp_buf *err);
|
|
|
|
/* ----------------------------------------------------------------- agent */
|
|
|
|
struct sp_agent_config {
|
|
size_t max_steps; /* tool rounds per turn */
|
|
size_t max_bytes; /* request budget */
|
|
const char *system_prompt;
|
|
int autonomous; /* 1: run mutate tools without asking */
|
|
FILE *approval_in; /* NULL with !autonomous means deny */
|
|
FILE *approval_out;
|
|
/*
|
|
* UI path for approval: when set, it is asked instead of the streams, so a
|
|
* curses program can put the question on the screen instead of reading
|
|
* stdin behind its own back. Returns 1 to allow, 0 to deny.
|
|
*/
|
|
int (*approve)(void *ctx, const char *name, int auth,
|
|
const char *args);
|
|
void *approve_ctx;
|
|
/*
|
|
* Nonzero: 'approve' is itself the channel (the Telegram gateway asks in
|
|
* its own chat) and is asked directly. Otherwise the ack mechanism goes
|
|
* first when SPLINTER_ACK_CHANNELS is set - which in the gateway meant
|
|
* waiting for an answer that only the blocked gateway could read.
|
|
*/
|
|
int approve_is_channel;
|
|
|
|
/*
|
|
* Steering: while a turn runs, the operator may type. The UI cannot
|
|
* interrupt a request that is already on the wire, so the text is picked
|
|
* up at the next step boundary -- stated here, not discovered later.
|
|
* `cancel` is read before every request; `steer` is asked for text.
|
|
*/
|
|
volatile int *cancel;
|
|
int (*steer)(void *ctx, struct sp_buf *out);
|
|
void *steer_ctx;
|
|
|
|
/* Called before a tool runs, with its name and its arguments as sent. */
|
|
void (*tool_begin)(void *ctx, const char *name,
|
|
const char *args);
|
|
void *tool_begin_ctx;
|
|
/* Called after a tool ran, with its observation. */
|
|
void (*tool_end)(void *ctx, const char *name,
|
|
const char *observation);
|
|
void *tool_end_ctx;
|
|
|
|
/* Called when the conversation was compacted to fit the budget. */
|
|
void (*on_compact)(void *ctx, size_t before, size_t after,
|
|
size_t msgs_before, size_t msgs_after);
|
|
void *on_compact_ctx;
|
|
|
|
/* incremental output, if the transport and the caller both support it */
|
|
sp_stream_cb stream;
|
|
void *stream_ctx;
|
|
};
|
|
|
|
struct sp_agent_stats {
|
|
size_t steps;
|
|
size_t tokens_in;
|
|
size_t tokens_out;
|
|
size_t compactions;
|
|
size_t tool_calls;
|
|
size_t denied;
|
|
size_t bytes_sent;
|
|
};
|
|
|
|
struct sp_host; /* plugin host, see splinter_plugin.h */
|
|
|
|
struct sp_agent {
|
|
struct sp_registry *reg;
|
|
struct sp_host *host; /* optional: compactors live there */
|
|
struct sp_transport tr;
|
|
const struct sp_agent_config *cfg;
|
|
struct sp_session_info *sess;
|
|
struct sp_conv *conv; /* optional: history lives here */
|
|
struct sp_agent_stats stats;
|
|
/*
|
|
* A turn that stopped before a tool ran because a person has to decide.
|
|
* The assistant message with the calls is already in the conversation;
|
|
* the calls from pending_from on are still unobserved.
|
|
*/
|
|
struct sp_reply pending;
|
|
size_t pending_from;
|
|
int has_pending;
|
|
};
|
|
|
|
#define SP_AGENT_INPUT_REQUIRED 2
|
|
|
|
int sp_agent_run(struct sp_agent *, const char *user_text,
|
|
struct sp_buf *answer, struct sp_buf *err);
|
|
/* Resume a paused turn: decision 1 allows the pending calls, 0 denies. */
|
|
int sp_agent_resume(struct sp_agent *, int decision, struct sp_buf *answer,
|
|
struct sp_buf *err);
|
|
|
|
/* builtin.c */
|
|
int sp_builtin_register(struct sp_registry *);
|
|
/*
|
|
* Environment names the 'run' tool's shell does not inherit (API keys,
|
|
* tokens). A trailing '*' makes a prefix. sp_secret_env_is takes "NAME=value".
|
|
*/
|
|
int sp_secret_env_add(const char *name);
|
|
int sp_secret_env_is(const char *entry);
|
|
struct sp_config;
|
|
int sp_delegate_register(struct sp_registry *, const struct sp_config *,
|
|
struct sp_host *);
|
|
/* Gateway mode: answers on Telegram until SIGINT/SIGTERM. */
|
|
struct sp_host;
|
|
int sp_telegram_run(struct sp_agent *, struct sp_host *host,
|
|
const char *token, const char *allow, struct sp_config *conf,
|
|
struct sp_buf *err);
|
|
int sp_telegram_token(char *buf, size_t len, const char *path);
|
|
const char *sp_memory_user(void);
|
|
/*
|
|
* Slash commands that are programs: <session dir>/commands/<name>. 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_prio(const struct sp_config *, const char *id);
|
|
|
|
/*
|
|
* Limits. sp_limit_parse: is this error text a rate/usage limit (429, "weekly
|
|
* limit", "quota", ...) and until when - the reset it names, else an hour.
|
|
* The bench is ~/.splinter/cooldown/<id> ($SPLINTER_COOLDOWN_DIR), shared by
|
|
* every splinter on the machine: sp_cooldown_until is 0 when <id> is free.
|
|
*/
|
|
int sp_limit_parse(const char *err, long *until);
|
|
long sp_cooldown_until(const char *id);
|
|
void sp_cooldown_set(const char *id, long until, const char *why);
|
|
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);
|
|
|
|
/* transport_openai.c -- OpenAI-compatible chat completions, via curl(1) */
|
|
/* The turn's cancellation flag; set by the UI before a turn, cleared after. */
|
|
void sp_openai_set_cancel(const volatile int *flag);
|
|
|
|
struct sp_transport sp_openai_transport(const char *url, const char *model,
|
|
const char *api_key, const struct sp_registry *, long timeout_s);
|
|
|
|
/* openai.c -- pure JSON shaping, testable without a network */
|
|
int sp_openai_request(const struct sp_conv *, const char *model,
|
|
const struct sp_registry *, int streaming, struct sp_buf *out);
|
|
int sp_openai_reply(const char *json, struct sp_reply *out,
|
|
struct sp_buf *err);
|
|
|
|
/* SSE accumulation, one line at a time, so it can be tested without a socket */
|
|
struct sp_sse;
|
|
struct sp_sse *sp_sse_new(void);
|
|
void sp_sse_free(struct sp_sse *);
|
|
/* Feed one line (with or without its newline). Returns 1 when a delta was
|
|
* produced (written to out, may be empty), 0 for lines that carry nothing,
|
|
* -1 on a parse error with the line quoted. */
|
|
int sp_sse_line(struct sp_sse *, const char *line, struct sp_buf *out,
|
|
struct sp_buf *err);
|
|
/* Turn the accumulated state into a reply. Returns 0 or -1. */
|
|
int sp_sse_finish(struct sp_sse *, struct sp_reply *out, struct sp_buf *err);
|
|
int sp_sse_done(const struct sp_sse *);
|
|
int sp_sse_usage(const struct sp_sse *, long *in, long *out);
|
|
const char *sp_sse_tool_start(const struct sp_sse *, size_t index);
|
|
|
|
#endif /* SPLINTER_H */
|