Skip to content
pi-c Artifact [853b2447be]
Login

Artifact [853b2447be]

Artifact 853b2447be7a2992568842359f5bd0599482bdda3551a6bdf1d550704de85e22:


/* 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_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);

#endif