/* The context-policy seam: core asks, a component decides (docs/design/keel.md,
* docs/dev/durability.md). Before each reply request core asks one question,
* PreRequest, through the hook host (pic_hooks_ask): the facts core owns go
* in, and the component's answer is journaled as a hook run before it takes
* effect. A journaled answer stands: recovery and retries read it and never
* ask again. An answer to compact names a mode; core asks the component for
* that mode's plan, runs the plan's summary requests (if any) and commits the
* compaction as it always has. With no policy (pic_store.policy NULL) core
* never compacts on its own and refuses pic compact. */
#ifndef PIC_POLICY_H
#define PIC_POLICY_H
#include "hooks.h"
#include "pic.h"
/* What a compaction keeps and how its entry's text is made: the entry keeps
* the context from FIRST_KEPT_SEQ on. With prompt[0] NULL its text is BODY
* (no request is made); else it is join() over the replies to prompt[0] and
* prompt[1] (NULL when one request does), each sent with SYSTEM. */
typedef struct {
sqlite3_int64 first_kept_seq, tokens_before;
const char *system;
char *prompt[2];
char *body;
char *details; /* journaled with the entry (pic_entry.native) for the next plan */
void *state; /* the component's */
} pic_compact_plan;
typedef struct pic_context_policy {
const char *name; /* the answering run's command in the journal */
/* PreRequest for SUBMISSION's TURN. FACTS is a JSON object of what core
* knows: model, contextWindow, cacheSeconds, idleSeconds (since the
* conversation's last reply; null before one). An answer to compact sets
* a->context to a JSON object with "compact" (the mode) and a->reason;
* an answer with no "compact" is kept as the component's record and does
* nothing; nothing at all (every field NULL) is not journaled. 0, or -1. */
int (*answer)(sqlite3 *db, int64_t submission, int64_t turn, const char *facts, pic_hook_answer *a);
/* The plan to compact SUBMISSION's context in MODE ("summary", "trim")
* for MODEL, with INSTRUCTIONS (NULL: none) for a summary. 0 with a plan,
* 1 when there is nothing to compact, -1 on error. */
int (*plan)(sqlite3 *db, int64_t submission, const char *model, const char *mode, const char *instructions,
pic_compact_plan *p);
/* The plan for a branch summary made when /tree leaves the conversation
* ending at FROM for entry AT, for a model with WINDOW tokens (0: not
* known). 0 with a plan (with no prompt when nothing fit), 1 when FROM's
* path leaves nothing behind, -1 on error. */
int (*branch_plan)(sqlite3 *db, sqlite3_int64 from, sqlite3_int64 at, long window, const char *instructions,
pic_compact_plan *p);
/* The entry's text from the replies to p->prompt (malloc'd, NULL when out of memory). */
char *(*join)(const pic_compact_plan *p, char *const reply[2]);
void (*plan_free)(pic_compact_plan *p);
/* Whether a request MODEL found too long is compacted and tried again. */
int (*overflow)(const char *model);
} pic_context_policy;
#endif