/* pic --mode rpc (rpc.c): Pi's RPC mode. A long-lived process reading Pi's * JSONL commands on stdin and writing responses and Pi's JSON events on * stdout, one object per line. The journal is opened per command and per * request, as the line mode opens it, so other pic commands can use it * while the process waits; a crash mid-request recovers as any pic run's. */ #ifndef PIC_RPC_H #define PIC_RPC_H #include "pic.h" /* The RPC command registry: a component answers commands by name. Each * declares a pic_rpc_command, the composition root hands the entries over * in pic_rpc_host, and the server dispatches by name without naming the * component. A command the server answers itself is not in the registry. */ enum { PIC_RPC_JOURNAL_FREE = 1, /* answered with no journal open (s NULL) */ PIC_RPC_IMMEDIATE = 2 /* answered at once while a request runs, not after it */ }; /* One command being answered. The server fills the inputs; ANSWER fills the * outputs and returns 0, or -1 with err (s->err is used when err is empty). */ typedef struct pic_rpc_call { pic_store *s; /* the journal; NULL for a PIC_RPC_JOURNAL_FREE command */ const char *session; /* the conversation's id, or "" before it has one */ const char *model; /* "provider/id" the process uses */ const char *workspace; /* as given (not resolved) */ const char *db; /* the journal's path, for a journal-free command that opens it itself */ const char *line; /* the command, a JSON object */ char *data; /* out: the response's data (malloc'd JSON), or NULL for none */ int switched; /* out: 1 to move to conversation next ("" a new, empty one) */ char next[64]; char err[PIC_ERR_CAP]; } pic_rpc_call; typedef struct pic_rpc_command { const char *type; /* the command's name, as Pi's clients send it */ const char *help; /* one line: what it answers */ unsigned flags; /* PIC_RPC_* */ int (*answer)(pic_rpc_call *c); } pic_rpc_command; typedef struct pic_rpc_host { const char *db, *workspace; int exec_seconds; /* --model was given: it stands, and a conversation taken up does not bring its own. */ int model_named; /* Opens the journal with the hooks attached (0, or -1 with s->err). */ int (*open)(pic_store *s, const char *db); /* Runs TEXT as a request of MODEL at THINKING, in SESSION, or (SESSION * NULL) a new conversation with the id NEW_ID; once it is admitted, * admitted(ctx) runs before the request does. 0, or -1 (s->err). */ int (*prompt)(pic_store *s, const char *model, const char *thinking, const char *session, const char *new_id, const char *text, pic_image *images, size_t n, void (*admitted)(void *ctx), void *ctx); /* MODEL's provider at THINKING (0, or -1 with err). */ int (*provider)(pic_provider *p, const char *model, const char *thinking, char *err, size_t errcap); /* MODEL's credential, opened now if it is sealed (never asking on a terminal: RPC mode * forbids that). 0 when it is ready; 1 when the store is locked and nothing here can ask, * with PROVIDER named and err the way out; -1 with err otherwise. */ int (*key_ready)(const char *model, char *provider, size_t pcap, char *err, size_t errcap); /* The level MODEL starts at: --thinking, else the saved settings. */ int (*level)(const char *model, char *out, size_t cap, char *err, size_t errcap); /* A template's or skill's expansion of TEXT (malloc'd), or NULL. */ char *(*expand)(void *workspace, const char *text); /* SessionEnd for the conversations this process started. */ void (*end)(const char *db, const char *reason); /* The registry: the components' commands (NCOMMANDS of them). */ const pic_rpc_command *const *commands; size_t ncommands; } pic_rpc_host; /* Serves stdin until it ends (or SIGTERM/SIGHUP). MODEL and SESSION (NULL: * a new conversation; "latest" or an id to continue one) start the state. * The process's exit status. */ int pic_rpc_main(const pic_rpc_host *h, const char *model, const char *session); /* Answers LINE (a command, a JSON object) in this process with H's registry, as * pic --mode rpc answers a component's command: Pi's response object (its id, * type, command, success, and data or error), malloc'd; NULL when out of * memory. A command that is not PIC_RPC_JOURNAL_FREE gets the journal opened * from H's db for its answer. This is how the terminal interface asks * (pic_tui_options.ask): it reaches components and core state only so. */ char *pic_rpc_answer(const pic_rpc_host *h, const char *session, const char *model, const char *line); /* Helpers for an ANSWER: the text at PATH in the command (malloc'd), or NULL * when it is absent or null; and S appended to OUT as a JSON string. */ char *pic_rpc_text(const pic_rpc_call *c, const char *path); void pic_rpc_quote(sqlite3_str *out, const char *s); /* Pi's session.messages from ENTRIES_JSON (pic_pi_entries' array): the context after the latest * compaction, as AgentMessages; DB is any connection (its JSON functions). Malloc'd, or NULL. */ char *pic_rpc_messages(sqlite3 *db, const char *entries_json); /* Pi's Model object for NAME ("provider/id"), DISPLAY its name (NULL: the * id), as malloc'd JSON; NULL when NAME does not resolve. */ char *pic_rpc_model_json(const char *name, const char *display); /* get_session_stats' data for the conversation SESSION of S, its context use measured against MODEL's * window with S's policy (no policy: 0 tokens), malloc'd; NULL with s->err. */ char *pic_rpc_session_stats(pic_store *s, const char *session, const char *model); /* The same over the MOST newest entries of its path (pic_pi_entries_recent). */ char *pic_rpc_session_stats_recent(pic_store *s, const char *session, const char *model, int most); /* get_available_thinking_levels' list for MODEL (every level when it does not resolve), as a JSON array * in OUT. The number of levels. */ int pic_rpc_levels(const char *model, char *out, size_t cap); /* Core's own commands for a client (rpc_core.c): settings and the settings list, trust, the model * scope, the models used lately, bug reports, paired phones, hooks and process records. */ extern const pic_rpc_command pic_get_setting_rpc, pic_set_setting_rpc, pic_get_settings_rpc, pic_get_trust_rpc, pic_set_trust_rpc, pic_get_scoped_rpc, pic_set_scoped_rpc, pic_get_recent_rpc, pic_report_bug_rpc, pic_get_phones_rpc, pic_get_hooks_rpc, pic_proc_start_rpc, pic_proc_end_rpc; #endif