/* Live events while pic runs (events.c). JSON mode writes one event per line
* on out with Pi's names and shapes (agent_start, turn_start, message_start,
* message_update, message_end, tool_execution_start/end, compaction_start/end,
* auto_retry_start, turn_end, agent_end), so a client of Pi's JSON mode can
* read it. Text mode streams the reply text to out as it arrives and puts
* tool calls and notices on notes. Events are not journaled: the journal is
* the record, and a reply cut off mid-stream ends in message_end with
* stopReason "error" before it is requested again. */
#ifndef PIC_EVENTS_H
#define PIC_EVENTS_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
typedef struct pic_events {
FILE *out; /* JSON lines, or the streamed reply text */
FILE *notes; /* text mode: tool calls and notices (NULL: none) */
int json; /* 1: JSON lines on out */
int stream; /* text mode: stream reply text to out */
int streamed; /* text mode: reply text written for this request */
int midline; /* text mode: out does not end in a newline */
int turn_open; /* a turn_start not yet ended */
int msg_open; /* an assistant message_start not yet ended */
int block; /* contentIndex of the current delta block, -1 none */
int thinking; /* the current block is thinking */
} pic_events;
void pic_events_init(pic_events *e, FILE *out, FILE *notes, int json, int stream);
struct pic_store;
/* conversation, model ("provider/model"), context_window (tokens, 0
* unknown) and thinking (the level in effect, "" when none) are pic's
* additions to Pi's event, for clients. */
void pic_ev_agent_start(struct pic_store *s, const char *request_key, const char *text, const char *images,
const char *conversation, const char *model, long context_window, const char *thinking);
/* status: answered, failed (error) or stopped (stop_reason: request_cap). */
void pic_ev_agent_end(struct pic_store *s, const char *request_key, const char *status, const char *error,
const char *stop_reason);
void pic_ev_turn_start(struct pic_store *s);
/* A reply's text or thinking as it streams (decoders call it). */
void pic_ev_delta(void *store, int thinking, const char *text, size_t len);
/* The reply as journaled: neutral content (JSON), Pi stop reason, origin. usage is
* Pi's usage object (JSON) or NULL. */
void pic_ev_message_end(struct pic_store *s, const char *neutral, const char *stop_reason, const char *provider,
const char *api, const char *model, const char *usage);
/* A provider request failed; attempt is the one that failed (1-based). */
void pic_ev_message_error(struct pic_store *s, const char *error, int attempt, int max_attempts, int will_retry);
/* The provider request was aborted (Ctrl-C, Pi's abort): Pi's stopReason
* "aborted", never retried; the partial reply is not kept. */
void pic_ev_message_aborted(struct pic_store *s);
void pic_ev_tool_start(struct pic_store *s, const char *call_id, const char *tool, const char *args_json,
const char *summary);
void pic_ev_tool_update(struct pic_store *s, const char *call_id, const char *tool, const char *args_json,
const char *text);
void pic_ev_retry_wait(struct pic_store *s, int attempt, int max_attempts, long delay_ms, const char *error);
/* images: a JSON array of Pi image blocks after the text, or NULL. */
void pic_ev_tool_end(struct pic_store *s, const char *call_id, const char *tool, const char *result, const char *images,
int is_error);
void pic_ev_compaction_start(struct pic_store *s, const char *reason, long long tokens);
void pic_ev_compaction_end(struct pic_store *s, const char *reason, const char *summary, long long first_kept,
long long tokens_before, const char *error);
void pic_ev_note(struct pic_store *s, const char *text);
/* A user message: a request's text, or steering or a follow-up delivered
* into one (message_start and message_end, as Pi); images, when not NULL,
* a JSON array of Pi image blocks that follow the text. */
void pic_ev_user_message(struct pic_store *s, const char *text, const char *images);
/* One JSON object as a line on out, in JSON mode (queue_update, command
* responses). */
void pic_ev_line(struct pic_store *s, const char *json);
#endif