/* OpenAI Responses stream decoder: SSE events in, one reply out, as the list
* of output items (native, replayed verbatim to the same model, reasoning
* items with their encrypted_content) and as Pi's neutral content. The
* complete items from response.output_item.done, and the final list in
* response.completed, are what count; text deltas are not needed. */
#ifndef PIC_OPENAI_RESPONSES_H
#define PIC_OPENAI_RESPONSES_H
#include <stddef.h>
#include "sqlite3.h"
#define PIC_RESPONSES_CAP (16u << 20)
#define PIC_RESPONSES_ITEMS 128
typedef struct {
sqlite3 *db;
char *items[PIC_RESPONSES_ITEMS]; /* by output_index, from output_item.done */
int nitems;
char *final_output; /* response.output from the terminal event, verbatim */
char *status; /* completed, incomplete, failed */
char *incomplete; /* incomplete_details.reason */
char *usage;
char *response_id;
char *error;
int terminal; /* saw completed, incomplete or failed */
size_t total;
int failed;
char err[256];
} pic_responses;
int pic_responses_init(pic_responses *r, sqlite3 *db);
void pic_responses_free(pic_responses *r);
/* Fits pic_sse_event_fn with ctx = the decoder. 0, or -1 with r->err set. */
int pic_responses_event(void *ctx, const char *event, const char *data, size_t len);
/* End of stream: 0 when a complete reply arrived, else -1 with r->err. */
int pic_responses_finish(pic_responses *r);
/* After a successful finish (malloc'd): the output items as a JSON array; */
char *pic_responses_native(pic_responses *r);
/* Pi's neutral content array; */
char *pic_responses_neutral(pic_responses *r);
/* Pi's stop reason: stop, toolUse, length. */
const char *pic_responses_stop(pic_responses *r);
#endif