/* 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"
/* A command a component answers, handed over in pic_rpc_host by the
* composition root. ANSWER gets the journal, the conversation (its id, or ""
* before it has one) and the command line: 0 with *data the response's data
* (malloc'd JSON, or NULL for none), or -1 with s->err. To move to another
* conversation it sets *switched, and next ("" a new, empty one). */
typedef struct pic_rpc_command {
const char *type;
int (*answer)(pic_store *s, const char *session, const char *line, char **data, int *switched, char *next,
size_t cap);
} 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 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);
#endif