Artifact 5696a1feb5aca0aee0d5dfe20ec2101140bf5401755c59c1ca9df4d7ed6a4feb:
- File
src/rpc.h
— part of check-in
[8f2d30d39e]
at
2026-11-22 20:18:02
on branch keel-rpc
— Cut the RPC command registry seam
Components register RPC commands (name, help, flags, answer) and main.c wires them; rpc.c dispatches by name without including catalog.h, compact.h, prompts.h or skills.h. get_available_models and get_commands move to src/listings.c; rewind uses the new call shape. Whether compaction is automatic and the token estimate come through the context policy (automatic, tokens). Allowance 29 -> 25, stubs 29 -> 20. (user: pi-c-agent, size: 3783) [annotate] [blame] [check-ins using] [more...]
/* 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 *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; /* 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); /* 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); /* 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); #endif