/* 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