Artifact acc9ab0a527988d7f3d400e2e4bcf7fe2577ae302da7774efb68b29f4c12a871:
- File
src/events.h
— part of check-in
[784650d9f0]
at
2026-11-19 22:38:47
on branch live-events
— Events: the real retry delay, and bash output while it runs
auto_retry_start carries the delay pic is about to wait before retrying a 429 or 5xx (Retry-After or backoff), so a client can count it down. While a bash command runs, tool_execution_update events carry its output so far, throttled to 100 ms as in Pi, emitted outside any journal transaction. (user: dmestas, size: 3860) [annotate] [blame] [check-ins using] [more...]
/* 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 *conversation, const char *model, long context_window, const char *thinking); void pic_ev_agent_end(struct pic_store *s, const char *request_key, const char *status, const char *error); 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); 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); void pic_ev_tool_end(struct pic_store *s, const char *call_id, const char *tool, const char *result, 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). */ void pic_ev_user_message(struct pic_store *s, const char *text); /* 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