/* Wakes (docs/design/wakes.md): "send this conversation this prompt when that happens". A loop
* fires on the clock; a monitor on new output from a command; a goal when a request ends with the
* goal unmet. A fire is one IMMEDIATE transaction that advances the pic_wake row and inserts the
* inbox row wake:ID:N (sender wake:ID), so the inbox's deliver-once rule is what stops a double
* fire after a crash. Nothing keeps time: whoever has the journal open asks what is due (the
* terminal interface, pic remote, a running request's poll, pic loop fire). People see loop,
* monitor and goal; "wake" is the internal word. */
#ifndef PIC_WAKE_H
#define PIC_WAKE_H
#include <stddef.h>
#include "pic.h"
#include "rpc.h"
#define PIC_WAKE_MAX 8 /* per conversation, not ended */
#define PIC_WAKE_EXPIRY_MS (7LL * 86400000LL) /* a wake ends 7 days after it was made */
#define PIC_WAKE_MIN_EVERY_MS 60000LL /* the shortest interval */
#define PIC_WAKE_SELF_PACED_MS 600000LL /* a loop given no interval */
#define PIC_WAKE_LATE_MS 90000LL /* due this long ago and not fired: nothing is firing */
/* SQL: the wake that started request U (a pic_submission alias), or NULL: one a wake admitted
* (inbox:wake:ID:N) or a wake's follow-up became (queued-ID, its inbox row from wake:ID). */
#define PIC_WAKE_OF_SQL(U) \
"(CASE WHEN " U ".request_key LIKE 'inbox:wake:%' THEN CAST(substr(" U ".request_key, 12) AS INTEGER)" \
" WHEN " U ".request_key LIKE 'queued-%' THEN (SELECT CAST(substr(w_i.sender, 6) AS INTEGER) FROM pic_inbox w_i" \
" WHERE w_i.queued_id = CAST(substr(" U ".request_key, 8) AS INTEGER) AND w_i.sender LIKE 'wake:%') END)"
/* The wall clock wakes keep time by, in ms (dev builds: PIC_WAKE_NOW_MS when set, for tests). */
long long pic_wake_now_ms(void);
/* A new wake. */
typedef struct {
const char *session; /* its conversation; NULL or "": a new, empty one */
const char *kind; /* "loop" */
const char *prompt; /* what each fire sends */
long long every_ms; /* a loop's interval; 0: PIC_WAKE_SELF_PACED_MS */
const char *creator; /* "user" (the terminal), "cli", "remote:DEVICE", "model" */
} pic_wake_new;
/* Makes W, due at once. 0 with its id in *ID and its conversation in SESSION_OUT; 1 refused
* (s->err: a bad interval, too many in the conversation, no such conversation); -1. */
int pic_wake_create(pic_store *s, const pic_wake_new *w, long long *id, char *session_out, size_t cap);
/* ID (as "L1", "1") of S's journal, into *ID. 0, or 1 when it names none (s->err). */
int pic_wake_resolve(pic_store *s, const char *name, long long *id);
/* ACTION "pause", "resume" or "stop" on wake ID, with WHY (NULL: the action's own words). 0
* done; 1 refused (ended, or no such wake; s->err); -1. */
int pic_wake_set(pic_store *s, long long id, const char *action, const char *why);
/* /stop: every active wake of SESSION paused, how many in *count. 0 or -1. */
int pic_wake_pause_all(pic_store *s, const char *session, const char *why, int *count);
/* Inside the cancel's transaction: the wake that started request SUBMISSION, if any and active,
* is paused (Esc on work a wake started). 0 or -1. */
int pic_wake_cancelled(pic_store *s, long long submission);
/* What is due now: every active wake of S's journal (of SESSION alone when not NULL) whose time
* has come fires, each in its own transaction. With START, a conversation that has a wake's fire
* waiting in its inbox and no pic working it gets a pic run, started through the registered
* starter (WORKSPACE is the journal's). How many fired in *fired; the next due time (wall ms,
* 0: none) in *next_ms. 0 or -1. Fault point wake_fire: arrival 1 before a fire's commit, 2
* after it. */
int pic_wake_due(pic_store *s, const char *workspace, const char *session, int start, int *fired, long long *next_ms);
/* Who starts a pic run for a conversation with a fire waiting: the composition root registers it
* (peer.c's). DB is the journal's path. 0 when it started one. */
void pic_wake_starter(int (*start)(const char *db, const char *workspace, const char *session));
/* The wakes of SESSION (NULL: the journal's), not ended unless ALL, as a JSON array (malloc'd),
* each {id, name, kind, session, prompt, everyMs, every, state, why, creator, fires, dueMs,
* firedMs, createdMs, expiresMs, cost, tokens, late}; NULL with s->err. */
char *pic_wake_list(pic_store *s, const char *session, int all);
/* "5m", "90s", "2h", "1d" (a number alone is minutes) as ms. 0, or 1 when it is not one. */
int pic_wake_parse_every(const char *text, long long *ms);
/* MS as the shortest of those that says it exactly. */
void pic_wake_every_text(long long ms, char *out, size_t cap);
/* The RPC commands (core's, for every client): get_wakes, create_wake, pause_wake, resume_wake,
* stop_wake, pause_wakes. */
extern const pic_rpc_command pic_get_wakes_rpc, pic_create_wake_rpc, pic_pause_wake_rpc, pic_resume_wake_rpc,
pic_stop_wake_rpc, pic_pause_wakes_rpc;
/* pic usage --by-wake: fires, requests, tokens and cost per wake. */
int pic_usage_by_wake(pic_store *s, int json, FILE *out);
#endif