/* Asks (docs/dev/asks.md): a question a conversation puts to a person, durable in pic_ask, which * any client answers once. The asker opens it under a key of its own (the same key is the same * question, so a replay after a crash finds it, answered or still open) and waits; an answer is * first-writer-wins in one IMMEDIATE transaction. A loop or goal the model asked for is one, keyed * wake:ID, and its answer is the wake's decision (wake.c) in that transaction. */ #ifndef PIC_ASK_H #define PIC_ASK_H #include #include "pic.h" #include "rpc.h" #define PIC_ASK_EXPIRY_MS (7LL * 86400000LL) /* an ask nobody answered closes then */ #define PIC_ASK_TEXT_CAP 4000 /* a title, a body, an option or an input answer */ typedef struct { const char *key; /* the asker's: the same key is the same question */ const char *session; /* the conversation's id */ const char *kind; /* "confirm", "select" or "input" */ const char *title; const char *body; /* NULL: none */ const char *options; /* select: a JSON array of strings, at least one */ const char *placeholder; /* input: NULL for none */ long long expires_ms; /* wall ms; 0: PIC_ASK_EXPIRY_MS from now */ } pic_ask_new; enum { PIC_ASK_ANSWERED = 0, PIC_ASK_REFUSED = 1, PIC_ASK_ALREADY = 2 }; /* Opens A, in a transaction of its own (a savepoint inside the caller's). A known key changes * nothing and gives its ask. 0 with its id in *ID; 1 refused (s->err: a kind, title or option * that is not one, no such conversation); -1. */ int pic_ask_open(pic_store *s, const pic_ask_new *a, long long *id); /* The answer VALUE (JSON: true or false to a confirm, one of the options to a select, a string * to an input; null dismisses it) from BY ("user", "cli", "attach", "rpc", "remote:DEVICE"). * PIC_ASK_ANSWERED: this call's answer stands (or the same BY gave the same answer before); * PIC_ASK_ALREADY: it was answered, withdrawn or had expired, *STANDING (when not NULL) set to * {"state","value","answeredBy"} (malloc'd); PIC_ASK_REFUSED (s->err): no such ask, or VALUE * is not an answer to it; -1. Fault point ask_answer: arrival 1 before the commit, 2 after. */ int pic_ask_answer(pic_store *s, long long id, const char *value, const char *by, char **standing); /* Where ask ID stands: "open", "answered", "expired" or "withdrawn" in STATE ("" when there is no * such ask) and, when VALUE is not NULL, its answer (malloc'd JSON, NULL when none). 0 or -1. */ int pic_ask_state(pic_store *s, long long id, char *state, size_t cap, char **value); /* Waits until ask ID is no longer open, reading it every 100 ms. 0 with STATE and *VALUE as * pic_ask_state's; 1 when pic_interrupted cut the wait short (the ask stays open); -1. */ int pic_ask_wait(pic_store *s, long long id, char *state, size_t cap, char **value); /* KEY's ask, while open, closed as withdrawn: its asker stopped waiting (a wake stopped). Inside * or outside a transaction; nothing when the journal has no asks. 0 or -1. */ int pic_ask_withdraw(pic_store *s, const char *key); /* Inside the caller's transaction: KEY's ask, while open, closed as answered with VALUE by BY * (a wake decided by its own command). Nothing when there is none. 0 or -1. */ int pic_ask_settle(pic_store *s, const char *key, const char *value, const char *by); /* Asks as a JSON array (malloc'd), each {id, session, kind, title, body, options, placeholder, * createdMs, expiresMs, state, value, answeredBy, version, wake}, absent members left out. AFTER * < 0: the open ones, of SESSION (NULL: every conversation), oldest first; AFTER >= 0: every ask * opened or closed after version AFTER, in order. At most MAX. "[]" when the journal has none; * NULL with s->err. */ char *pic_ask_list(pic_store *s, const char *session, long long after, int max); /* get_asks (the conversation's open asks; all: true for every conversation's) and answer_ask * ({ask, value}), core's commands for every client. */ extern const pic_rpc_command pic_get_asks_rpc, pic_answer_ask_rpc; #endif