Artifact aa75f44afd7acf4068c1f39b39fc98861717cdfd493d019d74fbbabe01542efb:
- File
src/openai_chat.h
— part of check-in
[9983dd9955]
at
2026-11-19 11:05:56
on branch events
— Stream live events: Pi's JSON-mode events, or the reply as it arrives
--json writes one event per line as the run goes, with Pi's names and shapes: agent, turn and message start and end, text and thinking deltas from every adapter, tool execution start and end, compaction start and end, and auto_retry_start. A reply cut off mid-stream ends in message_end with stopReason error before it is requested again, so a client drops the partial text. pic ask streams the reply text when stdout is a terminal (--stream forces it) and prints it only once; tool calls stay on stderr. Events are not journaled: the journal remains the record. (user: dmestas, size: 2502) [annotate] [blame] [check-ins using] [more...]
/* Chat Completions stream decoder (OpenAI, OpenRouter and compatible * servers): SSE "data:" chunks to "[DONE]" in, one assistant message out, in * the native form Chat Completions takes back and in Pi's neutral form. * Parsing follows Pi's openai-completions adapter; JSON is read with SQLite's * JSON functions on a caller-supplied connection. */ #ifndef PIC_OPENAI_CHAT_H #define PIC_OPENAI_CHAT_H #include <stddef.h> #include "sqlite3.h" #define PIC_CHAT_CAP (16u << 20) /* bound on one assistant message */ #define PIC_CHAT_CALLS 64 /* tool calls in one message */ typedef struct { char *buf; size_t len, cap; } pic_chat_buf; typedef struct { char *id, *name; pic_chat_buf args; } pic_chat_call; typedef struct { sqlite3 *db; sqlite3_stmt *fields; /* every field of a chunk, in one query */ sqlite3_stmt *each; /* the elements of a JSON array */ sqlite3_stmt *call; /* every field of one tool-call delta */ int done; /* saw "[DONE]" */ char *finish_reason; /* the provider's, verbatim */ pic_chat_buf text, reasoning; char reasoning_field[24]; /* reasoning_content, reasoning or reasoning_text */ pic_chat_buf details; /* OpenRouter reasoning_details items, comma-joined JSON */ pic_chat_call calls[PIC_CHAT_CALLS]; int ncalls; char *usage; /* the usage object, verbatim */ char *error; /* an in-stream error object's message */ size_t total; /* Optional: each text or reasoning delta as it arrives (set after init). */ void (*on_delta)(void *ctx, int thinking, const char *text, size_t len); void *delta_ctx; int failed; char err[256]; } pic_chat; int pic_chat_init(pic_chat *c, sqlite3 *db); void pic_chat_free(pic_chat *c); /* Fits pic_sse_event_fn with ctx = the decoder. 0, or -1 with c->err set. */ int pic_chat_event(void *ctx, const char *event, const char *data, size_t len); /* End of stream. 0 when a complete message arrived (a finish_reason was * seen and no error), else -1 with c->err saying why. */ int pic_chat_finish(pic_chat *c); /* After a successful finish (malloc'd, or NULL on failure): * the assistant message as Chat Completions takes it back, verbatim; */ char *pic_chat_native(pic_chat *c); /* Pi's neutral content array; */ char *pic_chat_neutral(pic_chat *c); /* Pi's stop reason: stop, length, toolUse, refusal. */ const char *pic_chat_stop(const pic_chat *c); #endif