Files
lotherk 74df75d438 no approval gate: splinter runs what the operator's registry offers
Konrad, 24.11.: "v1 geht durch ohne irgendwas. ich will auch keine approvals
generell. ich will nur das der user mit ask nach sachen gefragt werden kann."

Removed the whole approval apparatus, not just the prompt:

- agent.c: the decision point (sp_host_gate/approve callbacks), the
  pause/resume path (has_pending, pending_from, sp_agent_resume), ack_approval,
  ask_operator and the ack stdin thread are gone. A tool runs; its exit status
  is the observation. sp_tool_policy (allow/deny per profile) stays - that is
  the operator's own configuration, not a question to the operator.
- engine.c: SP_EV_APPROVAL, sp_engine_approve, the approve_pending condition
  variable and the `approval`/`approve` protocol commands are gone.
- openai_server.c: the v1 endpoint runs gw_* tools without a gate; the
  autonomous flag is no longer part of sp_gateway_opts.
- a2aserver.c: INPUT_REQUIRED and the resume-by-taskId path are gone; CancelTask
  stays. An A2A caller has no operator sitting in front of a prompt.
- mcpserver.c: --autonomous no longer gates mutating tools; the flag and the
  config key are accepted for compatibility and documented as changing
  nothing.
- telegram.c: tg_approve and the approval callback are gone; ask is the only
  dialog, and /cancel inside it still ends the turn.
- front ends: protocol.c, curses.c, ui_local.c, ui_remote.c, client.c lose the
  approval event and the waiting flag.
- ack.c stays: it is a plugin API (plugins/kids asks its parent through it),
  not splinter's own gate. --ack is documented as such.

Tests: test_pause.c guarded the pause and now guards the opposite (a mutate and
a destructive tool run, nothing is asked). test_a2a_input.sh, test_protocol.sh,
test_ui.sh, test_agents_ui.sh, test_gateway.sh, test_gateway_openai.sh,
test_agent.c and test_mcpsession.sh were rewritten the same way;
splinter_approve must now be absent from the MCP tool list. Every rewritten
test was run red against the old behaviour where that was possible.

make check: all tests passed (428 s)
make check-asan: all 47 test binaries clean
make check-tsan: all 47 test binaries free of data races
2026-11-24 11:46:10 +00:00

667 lines
26 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"
/*
* The build stamp of the binary. Generated by make into
* src/backend/buildinfo.c, so --version says which build is running even
* though the release number stays the same.
*/
extern const char *sp_build_time;
extern const char *sp_build_rev;
/* ------------------------------------------------------------------ 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;
/*
* One choice /model takes, for a picker: the selector to type after /model,
* a label to show, and whether it is the current one. SP_MODEL_CHOICES is the
* most a caller's array has to hold (main + delegates + exits + pools).
*/
#define SP_MODEL_CHOICES 96
struct sp_model_choice {
char sel[320];
char label[512];
int current;
};
/* 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;
/*
* Called once when the registry is freed. A tool whose name,
* description, params or ctx were allocated (an MCP server's tools) sets
* it; builtins point at static strings and leave it NULL.
*/
void (*dispose)(struct sp_tool *);
};
struct sp_registry {
struct sp_tool *tools;
size_t len;
size_t cap;
void *ctx; /* the builtin that owns the tools, if any */
void **retired; /* outgrown arrays, kept: a reader on another */
size_t nretired; /* thread may still hold a pointer into one */
};
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 *);
/* tools may be added from another thread (an MCP server that connects late):
* read len first, then the array, as this does */
const struct sp_tool *sp_registry_tools(const struct sp_registry *, size_t *n);
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);
/*
* Start over: drop every message and keep only the system prompt (may be
* NULL). The session file stays; it is the record of what was said, and a
* fresh session is a new file next to it.
*/
int sp_conv_reset(struct sp_conv *, const char *system_prompt);
size_t sp_conv_bytes(const struct sp_conv *);
/* ------------------------------------------------------------- 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 tokens_cached; /* prompt tokens the provider served
* from its cache, 0 unknown */
long retry_after_s; /* parsed Retry-After header, 0 unknown */
double cost; /* provider-reported cost, USD; 0 unknown */
/*
* A router may say which upstream answered (junction sends
* X-Junction-Endpoint): the model a run is on is not always the
* endpoint that serves it. "" when the provider does not say.
*/
char served_by[128];
/*
* X-Junction-Judge-Next-*: when and where the next judge step runs.
* valid is 0 when the provider sent none (not a judged rule).
*/
int judge_valid;
int judge_next, judge_in, judge_step, judge_every;
char judge_target[64];
char judge_model[128];
char judge_cache[8];
char cache_mode[8]; /* X-Junction-Cache: off/5m/1h */
/*
* What the provider says ended the message: "stop", "length",
* "tool_calls", "max_tokens", "" when it did not say. A tool call
* cut off by the token limit is not the same as a broken one, and
* only this field tells them apart.
*/
char finish[24];
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);
/*
* Request shaping a front end may pass through to a provider. The gateway
* forwards what the calling harness asked for instead of dropping it; zero
* flags mean "not given" and leave the provider default.
*/
struct sp_gen_opts {
int have_temperature;
double temperature;
int have_top_p;
double top_p;
int have_max_tokens;
long max_tokens;
/* streaming: ask the provider for a final usage chunk */
int include_usage;
};
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 */
/*
* Optional: free the context a constructor allocated. A transport a
* caller builds per request (the OpenAI gateway) uses this; the agent's
* own transport lives as long as the agent. Zero for plugin transports,
* whose context belongs to the plugin.
*/
void (*free)(void *ctx);
};
void sp_reply_free(struct sp_reply *);
/* free a transport's context (if it owns one) and zero the struct */
void sp_transport_dispose(struct sp_transport *);
/* 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);
/*
* A full record. The short form above writes a role and a text; that was enough
* to read a session back as a chat log and not enough to resume one: a tool
* result without the name of its tool, and an assistant turn without its
* tool_calls, cannot be sent to a provider at all. An empty text writes
* nothing - a record with no content is not a message, it is noise in the file
* that made every resumed session carry blank user turns.
*/
struct sp_sess_rec {
const char *role;
const char *text;
const char *name; /* tool name, when role is "tool" */
const char *call_id; /* tool_call id */
const char *calls; /* raw tool_calls JSON of an assistant turn */
};
int sp_session_append_rec(const struct sp_session_info *,
const struct sp_sess_rec *, 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);
/*
* A paragraph for the system prompt when this working directory has earlier
* sessions: how many, the newest, and the tools that read them. Empty output
* when there are none.
*/
int sp_session_folder_note(const char *dir, const char *skip_id,
struct sp_buf *out);
/* a paragraph naming the working directory as the default task scope */
int sp_task_scope_note(struct sp_buf *out);
/* the working directory a session was started in (meta sidecar) */
int sp_session_cwd(const char *dir, const char *id, char *buf, size_t cap);
int sp_session_load(const struct sp_session_info *, struct sp_conv *conv,
size_t *loaded, struct sp_buf *err);
/* one line of a session listing: id, title, mtime, bytes; newest first */
struct sp_session_brief {
char id[64];
char title[96]; /* set title, or the first user line */
long mtime;
size_t bytes;
};
/* up to max entries from dir; returns the count (0..max), or -1 */
int sp_session_list(const char *dir, struct sp_session_brief *v,
size_t max);
/* give a session a title (one line, at most 95 bytes) */
int sp_session_set_title(const char *dir, const char *id,
const char *title, struct sp_buf *err);
/* the session's /goal, stored in its meta sidecar so a resume finds it */
int sp_session_set_goal(const char *dir, const char *id,
const char *goal, struct sp_buf *err);
int sp_session_get_goal(const char *dir, const char *id,
char *buf, size_t cap);
/*
* "open" or "succeeded". A model that reached the goal answers GOAL_COMPLETE;
* the state is what the sidebar and the status line show afterwards. Setting a
* new goal resets it to open.
*/
int sp_session_set_goal_state(const char *dir, const char *id,
const char *state, struct sp_buf *err);
int sp_session_get_goal_state(const char *dir, const char *id,
char *buf, size_t cap);
/*
* Remove a session: its log, meta, saved profile and the logs/<id> directory.
* The caller must not delete the session its engine has open.
*/
int sp_session_delete(const char *dir, const char *id, struct sp_buf *err);
/*
* Where a session came from, when a plugin imported it ("claude:<uuid>" as
* written by the foreign plugin). Returns 0 and fills buf, or -1 when the
* session is splinter's own. The engine reads this to continue the same
* imported session on a later start instead of importing it again.
*/
int sp_session_foreign(const char *dir, const char *id, char *buf,
size_t cap);
/*
* Remove a session that never got a message: the empty placeholder a run
* creates before it knows which session it will continue. A session with even
* one record is kept. Returns 0 when it was removed, -1 when it has content or
* does not exist.
*/
int sp_session_remove_if_empty(const char *dir, const char *id);
/* ----------------------------------------------------------------- agent */
/*
* The question a tool may put to the operator (`ask`). The sizes are fixed
* because the pending question lives in the engine struct, which is one per
* process: a model that asks a 4 KiB question should have written it in its
* answer instead.
*/
#define SP_ASK_MAX 1024 /* the question's text */
#define SP_ASK_OPTIONS 8 /* "which one?" alternatives */
#define SP_ASK_OPT_LEN 96
/*
* How long the `ask` tool waits for an answer, when nothing else says. Long
* enough that a person can read the question and think, short enough that a
* turn whose front end does not know the question (an old client, a script)
* comes back with "nobody answered" instead of hanging until somebody kills
* it. Overridden by $SPLINTER_ASK_TIMEOUT, then by `ask_timeout` in the
* config; see sp_ask_wait_s(). No upper bound: how long a turn may wait is
* the operator's call, not the program's.
*/
#define SP_ASK_WAIT_S 900
int sp_ask_wait_s(void);
struct sp_agent_config {
size_t max_steps; /* tool rounds per turn */
size_t max_bytes; /* request budget */
/*
* Compaction by tokens: past `compact_threshold` the next request is
* compacted to `compact_target` (both in tokens, 0 = off). The byte
* budget stays the hard ceiling.
*/
size_t compact_threshold;
size_t compact_target;
const char *system_prompt;
/*
* Kept so an existing config or profile still parses; splinter does not
* gate its own tools, so the value changes nothing. A tool the registry
* offers runs.
*/
int autonomous;
/*
* A question the `ask` tool puts to the operator. `opts` holds up to
* `nopts` suggested answers (may be 0) and the callback writes the
* answer into `answer` (cap bytes). Returns 0 on an answer, -1 when
* there is nobody to ask -- the tool then says so in its observation
* instead of hanging. NULL disables the tool's asking; splinter does
* not ask anything on its own, so a front end that cannot reach a
* person simply leaves this unset.
*/
int (*ask)(void *ctx, const char *text,
const char (*opts)[SP_ASK_OPT_LEN], size_t nopts,
char *answer, size_t cap);
void *ask_ctx;
/*
* 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;
/*
* A status line the operator should see while a request waits or is
* retried; "" clears it. The engine turns it into SP_EV_NOTE, so the
* screen can say "retrying in 5 s" instead of looking hung.
*/
void (*note)(void *ctx, const char *text);
void *note_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;
/*
* Called when a request hit a usage limit that lifts at 'until' (epoch).
* The callback waits (it may take hours) and returns 1 to ask again, or
* 0 to give up and let the turn fail with the error. NULL: fail at once.
*/
int (*limit_wait)(void *ctx, long until, const char *why);
void *limit_ctx;
/* tried before waiting: move to another provider; 1 if it did */
int (*limit_switch)(void *ctx);
void *switch_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 tokens_cached; /* prompt tokens served from cache, session */
double cost; /* provider-reported, USD */
size_t last_in; /* the last request's prompt, for the meter */
size_t last_cached; /* of which the provider had cached */
size_t compactions;
size_t tool_calls;
size_t denied;
size_t bytes_sent;
};
/* waits for a usage limit without a screen; ctx: const long *max_wait_secs */
int sp_limit_wait_default(void *ctx, long until, const char *why);
struct sp_host; /* plugin host, see splinter_plugin.h */
/*
* Which tools an agent profile (build, plan ...) may use. max_auth is the
* highest authorization class that runs without question (-1: any); allow and
* deny are comma lists of tool names, a trailing '*' is a prefix, and deny wins.
*/
struct sp_tool_policy {
char name[48]; /* the profile, for the refusal's wording */
int max_auth;
char allow[512];
char deny[512];
};
struct sp_agent {
const struct sp_tool_policy *policy; /* NULL: no restriction */
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;
};
int sp_agent_run(struct sp_agent *, const char *user_text,
struct sp_buf *answer, struct sp_buf *err);
/* builtin.c */
int sp_builtin_register(struct sp_registry *, const char *root);
/*
* 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,
const char *base_dir, struct sp_buf *err);
int sp_telegram_notify(const struct sp_config *conf, const char *text,
struct sp_buf *err);
int sp_telegram_token(char *buf, size_t len, const char *path);
const char *sp_user_name(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);
/*
* Where the child of a cli delegate finds the cli transport: the plugin path
* for a binary at 'self'. $SPLINTER_CLI_PLUGIN wins outright; otherwise the
* build tree next to the binary, the installed layout, then the build
* directory compiled in. When none of them exists the first candidate is
* returned, so the child's own error names the place that was expected.
*/
void sp_delegate_cli_plugin(const char *self, char *out, size_t len);
/*
* A shipped plugin by name ("lcm", "mcp"): the build tree next to the binary
* at 'self', the installed layout ($(PREFIX)/lib/splinter/plugins), then the
* build directory compiled in. None found: the first candidate.
*/
void sp_plugin_path(const char *self, const char *name, char *out,
size_t len);
/*
* 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);
/*
* The same flag for transports that are not this one: the cli plugin kills its
* child process with it, so Ctrl-C ends a `claude -p` too. NULL when no turn
* is running.
*/
const volatile int *sp_cancel_flag(void);
/*
* The session whose id goes to providers that want one (opencode-go routes
* and caches by "x-opencode-session"). A pointer, so /new is seen.
*/
void sp_openai_set_session(const char *id);
size_t sp_openai_session_header(const char *url, char *buf, size_t cap);
/* /effort: review depth for the next requests; -1 lets the router decide */
void sp_openai_set_review_depth(int depth);
int sp_openai_review_depth(void);
/*
* One route of an endpoint: the config names the API base (https://host/v1),
* this appends "chat/completions", "messages", "models", ... An old full
* route is cut back to the base first. NULL route: the base. Returns out.
*/
char *sp_endpoint_url(const char *endpoint, const char *route, char *out,
size_t cap);
struct sp_transport sp_openai_transport(const char *url, const char *model,
const char *api_key, const struct sp_registry *, long timeout_s);
/* the same, with request shaping the caller wants forwarded */
struct sp_transport sp_openai_transport_gen(const char *url, const char *model,
const char *api_key, const struct sp_registry *, long timeout_s,
const struct sp_gen_opts *);
/* 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_request_opts(const struct sp_conv *, const char *model,
const struct sp_registry *, int streaming,
const struct sp_gen_opts *, 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 *);
/* the code an in-stream error event carried, 0 if none */
int sp_sse_error_code(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);
/* What the last line carried: SP_STREAM_THINK, SP_STREAM_TEXT, SP_STREAM_TOOL,
* or -1 for nothing. Transports classify by this, not by sniffing the line:
* OpenAI sends `"content":null` next to `tool_calls`, and a substring test made
* the tool's name part of the answer ("<model> readread"). */
int sp_sse_kind(const struct sp_sse *);
/*
* transport_anthropic.c -- the Anthropic Messages API, via curl(1).
*
* Same seam as the OpenAI transport: a conversation in, a reply out. A token
* that starts with "sk-ant-oat" is a Claude Code OAuth token and is sent as a
* Bearer token with the oauth beta (and Claude Code's first system block);
* anything else goes as x-api-key.
*/
struct sp_transport sp_anthropic_transport(const char *url, const char *model,
const char *api_key, const struct sp_registry *, long timeout_s);
struct sp_transport sp_anthropic_transport_gen(const char *url,
const char *model, const char *api_key, const struct sp_registry *,
long timeout_s, const struct sp_gen_opts *);
/* anthropic.c -- pure JSON shaping, testable without a network */
int sp_anthropic_request(const struct sp_conv *, const char *model,
const struct sp_registry *, int streaming, int oauth,
struct sp_buf *out);
int sp_anthropic_request_opts(const struct sp_conv *, const char *model,
const struct sp_registry *, int streaming, int oauth,
const struct sp_gen_opts *, struct sp_buf *out);
int sp_anthropic_reply(const char *json, struct sp_reply *out,
struct sp_buf *err);
/* Anthropic event accumulation, one line at a time */
struct sp_as_sse;
struct sp_as_sse *sp_as_sse_new(void);
void sp_as_sse_free(struct sp_as_sse *);
/* Returns 0 for an event that carries nothing, an SP_STREAM_* kind for what it
* produced (in out), or -1 with the event quoted in err. */
int sp_as_sse_line(struct sp_as_sse *, const char *line, struct sp_buf *out,
struct sp_buf *err);
int sp_as_sse_finish(struct sp_as_sse *, struct sp_reply *out,
struct sp_buf *err);
int sp_as_sse_done(const struct sp_as_sse *);
int sp_as_sse_usage(const struct sp_as_sse *, long *in, long *out);
#endif /* SPLINTER_H */