Artifact 984aca2b0496b1edf7327f37283d06561629eab196ddb85a70fc56ccb3e98676:
- File
src/events.c
— 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: 8908) [annotate] [blame] [check-ins using] [more...]
/* Live events while pic runs: Pi's JSON mode shapes, or streamed text. */ #include <stdio.h> #include <string.h> #include "events.h" #include "pic.h" void pic_events_init(pic_events *e, FILE *out, FILE *notes, int json, int stream) { memset(e, 0, sizeof *e); e->out = out; e->notes = notes; e->json = json; e->stream = stream; e->block = -1; } /* A JSON string: quotes, backslashes and control characters escaped; other * bytes, UTF-8 included, pass through. */ static void jstr(FILE *f, const char *s, size_t n) { static const char hex[] = "0123456789abcdef"; fputc('"', f); for (size_t i = 0; s && i < n; i++) { unsigned char c = (unsigned char)s[i]; if (c == '"' || c == '\\') { fputc('\\', f); fputc(c, f); } else if (c == '\n') { fputs("\\n", f); } else if (c == '\r') { fputs("\\r", f); } else if (c == '\t') { fputs("\\t", f); } else if (c < 0x20) { fprintf(f, "\\u00%c%c", hex[c >> 4], hex[c & 15]); } else { fputc(c, f); } } fputc('"', f); } static void js(FILE *f, const char *s) { jstr(f, s ? s : "", s ? strlen(s) : 0); } static void line_end(pic_events *e) { fputc('\n', e->out); fflush(e->out); } /* Text mode: finish a streamed line before anything else is written. */ static void settle(pic_events *e) { if (!e->json && e->midline) { fputc('\n', e->out); fflush(e->out); e->midline = 0; } } static void turn_end(pic_events *e) { if (e->turn_open && e->json) { fputs("{\"type\":\"turn_end\"}", e->out); line_end(e); } e->turn_open = 0; } void pic_ev_agent_start(pic_store *s, const char *request_key, const char *text) { pic_events *e = s->events; if (!e) { return; } e->streamed = 0; if (!e->json) { return; } fputs("{\"type\":\"agent_start\",\"request\":", e->out); js(e->out, request_key); fputc('}', e->out); line_end(e); for (int i = 0; i < 2; i++) { fputs(i ? "{\"type\":\"message_end\"" : "{\"type\":\"message_start\"", e->out); fputs(",\"message\":{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":", e->out); js(e->out, text); fputs("}]}}", e->out); line_end(e); } } void pic_ev_agent_end(pic_store *s, const char *request_key, const char *status, const char *error) { pic_events *e = s->events; if (!e) { return; } settle(e); turn_end(e); if (!e->json) { return; } fputs("{\"type\":\"agent_end\",\"messages\":[],\"request\":", e->out); js(e->out, request_key); fputs(",\"status\":", e->out); js(e->out, status); if (error) { fputs(",\"errorMessage\":", e->out); js(e->out, error); } fputc('}', e->out); line_end(e); } void pic_ev_turn_start(pic_store *s) { pic_events *e = s->events; if (!e) { return; } turn_end(e); e->turn_open = 1; e->msg_open = 0; e->block = -1; if (e->json) { fputs("{\"type\":\"turn_start\"}", e->out); line_end(e); } } static void message_start(pic_events *e) { if (e->msg_open) { return; } e->msg_open = 1; e->block = -1; if (e->json) { fputs("{\"type\":\"message_start\",\"message\":{\"role\":\"assistant\",\"content\":[]}}", e->out); line_end(e); } } void pic_ev_delta(void *store, int thinking, const char *text, size_t len) { pic_events *e = ((pic_store *)store)->events; if (!e || len == 0) { return; } message_start(e); if (e->block < 0 || e->thinking != thinking) { e->block++; e->thinking = thinking; } if (e->json) { fprintf(e->out, "{\"type\":\"message_update\",\"assistantMessageEvent\":{\"type\":\"%s\",\"contentIndex\":%d,\"delta\":", thinking ? "thinking_delta" : "text_delta", e->block); jstr(e->out, text, len); fputs("}}", e->out); line_end(e); } else if (e->stream && !thinking) { fwrite(text, 1, len, e->out); fflush(e->out); e->midline = text[len - 1] != '\n'; e->streamed = 1; } } void pic_ev_message_end(pic_store *s, const char *neutral, const char *stop_reason, const char *provider, const char *api, const char *model) { pic_events *e = s->events; if (!e) { return; } settle(e); if (e->json) { message_start(e); fputs("{\"type\":\"message_end\",\"message\":{\"role\":\"assistant\",\"content\":", e->out); fputs(neutral && *neutral ? neutral : "[]", e->out); fputs(",\"stopReason\":", e->out); js(e->out, stop_reason); fputs(",\"provider\":", e->out); js(e->out, provider); fputs(",\"api\":", e->out); js(e->out, api); fputs(",\"model\":", e->out); js(e->out, model); fputs("}}", e->out); line_end(e); } e->msg_open = 0; } void pic_ev_message_error(pic_store *s, const char *error, int attempt, int max_attempts, int will_retry) { pic_events *e = s->events; if (!e) { return; } settle(e); if (e->json) { message_start(e); fputs("{\"type\":\"message_end\",\"message\":{\"role\":\"assistant\",\"content\":[],\"stopReason\":\"error\"," "\"errorMessage\":", e->out); js(e->out, error); fputs("}}", e->out); line_end(e); if (will_retry) { fprintf(e->out, "{\"type\":\"auto_retry_start\",\"attempt\":%d,\"maxAttempts\":%d,\"delayMs\":0,\"errorMessage\":", attempt + 1, max_attempts); js(e->out, error); fputc('}', e->out); line_end(e); } } else if (e->notes) { fprintf(e->notes, "pic: %s%s\n", error, will_retry ? (e->streamed ? "; trying again (the reply above is discarded)" : "; trying again") : ""); fflush(e->notes); } e->msg_open = 0; } void pic_ev_tool_start(pic_store *s, const char *call_id, const char *tool, const char *args_json, const char *summary) { pic_events *e = s->events; if (!e) { return; } settle(e); if (e->json) { fputs("{\"type\":\"tool_execution_start\",\"toolCallId\":", e->out); js(e->out, call_id); fputs(",\"toolName\":", e->out); js(e->out, tool); fputs(",\"args\":", e->out); fputs(args_json && *args_json ? args_json : "{}", e->out); fputc('}', e->out); line_end(e); } else if (e->notes) { const char *what = summary ? summary : ""; fprintf(e->notes, "-> %s %.*s%s\n", tool, 100, what, strlen(what) > 100 ? "..." : ""); fflush(e->notes); } } void pic_ev_tool_end(pic_store *s, const char *call_id, const char *tool, const char *result, int is_error) { pic_events *e = s->events; if (!e || !e->json) { return; } fputs("{\"type\":\"tool_execution_end\",\"toolCallId\":", e->out); js(e->out, call_id); fputs(",\"toolName\":", e->out); js(e->out, tool); fputs(",\"result\":{\"content\":[{\"type\":\"text\",\"text\":", e->out); js(e->out, result); fprintf(e->out, "}]},\"isError\":%s}", is_error ? "true" : "false"); line_end(e); } void pic_ev_compaction_start(pic_store *s, const char *reason, long long tokens) { pic_events *e = s->events; if (!e) { return; } settle(e); if (e->json) { fputs("{\"type\":\"compaction_start\",\"reason\":", e->out); js(e->out, reason); fputc('}', e->out); line_end(e); } else if (e->notes) { fprintf(e->notes, "pic: compacting the conversation (%lld tokens)\n", tokens); fflush(e->notes); } } void pic_ev_compaction_end(pic_store *s, const char *reason, const char *summary, long long first_kept, long long tokens_before, const char *error) { pic_events *e = s->events; if (!e || !e->json) { return; } fputs("{\"type\":\"compaction_end\",\"reason\":", e->out); js(e->out, reason); if (summary) { fputs(",\"result\":{\"summary\":", e->out); js(e->out, summary); fprintf(e->out, ",\"firstKeptEntryId\":\"%lld\",\"tokensBefore\":%lld}", first_kept, tokens_before); } fputs(",\"aborted\":false,\"willRetry\":false", e->out); if (error) { fputs(",\"errorMessage\":", e->out); js(e->out, error); } fputc('}', e->out); line_end(e); } void pic_ev_note(pic_store *s, const char *text) { pic_events *e = s->events; if (!e || e->json || !e->notes) { return; } settle(e); fprintf(e->notes, "pic: %s\n", text); fflush(e->notes); }