/* 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 <stddef.h>
#include "pic.h"
#include "rpc.h"
#define PIC_ASK_EXPIRY_MS (7LL * 86400000LL) /* an ask nobody answered closes then */
#define PIC_ASK_ID_LEN 22 /* an id: 16 random bytes as base64url */
#define PIC_ASK_TITLE_MAX 120 /* characters; longer is cut when it is asked */
#define PIC_ASK_BODY_MAX 2000
#define PIC_ASK_OPTIONS_MAX 10
#define PIC_ASK_LABEL_MAX 60
#define PIC_ASK_TEXT_MAX 4000 /* an input's answer; longer is refused */
typedef struct {
const char *key; /* the asker's: the same key is the same question */
const char *session; /* the conversation's id */
long long submission; /* the request whose run waits on it, or 0 (a wake's OK: none) */
const char *kind; /* "confirm", "select" or "input" */
const char *title;
const char *body; /* NULL: none */
/* select: a JSON array, each a label or {"label", "destructive", "default"}; at most one
* default */
const char *options;
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, PIC_ASK_EXPIRED = 3 };
/* Opens A, in a transaction of its own (a savepoint inside the caller's), its texts cut to the
* limits above. A known key changes nothing and gives its ask. 0 with its id (PIC_ASK_ID_LEN
* characters) in ID; 1 refused (s->err: a kind or options that are not one, no such
* conversation, a journal that keeps no asks); -1. */
int pic_ask_open(pic_store *s, const pic_ask_new *a, char id[PIC_ASK_ID_LEN + 1]);
/* The answer VALUE (JSON: true or false to a confirm, one of the labels to a select, text to an
* input; null dismisses it) from BY ("terminal", "rpc", "attach", "remote:DEVICE").
* PIC_ASK_ANSWERED: this call's answer stands (or BY gave the same answer before);
* PIC_ASK_ALREADY: another answer stood first, or its asker withdrew it; PIC_ASK_EXPIRED: it
* expired unanswered; with either, *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 (over a limit too); -1. Fault point ask_answer: arrival 1 before the commit, 2 after. */
int pic_ask_answer(pic_store *s, const char *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, const char *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, const char *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 (each {label,
* destructive, default}), 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);
/* The asks of SESSION that were settled while a run waiting on them is still running (its request
* admitted), as pic_ask_list's array. */
char *pic_ask_settled(pic_store *s, const char *session);
/* get_asks (the conversation's open asks; all: true for every conversation's) and answer_ask
* ({ask, value, by: "terminal" for the terminal interface}), core's commands for every client. */
extern const pic_rpc_command pic_get_asks_rpc, pic_answer_ask_rpc;
#endif