Artifact 5b2a29509c7ccfc6e0c130d1122b8edcb64bce325fcce016990d391a7b5bb661:
- File
src/events.c
— part of check-in
[cb2ad5fbe9]
at
2026-11-19 13:09:42
on branch tui
— Add a terminal interface: plain pic in a terminal
A module in the pic binary (src/tui) that runs each request as a child pic --json process and knows it only through its events, so a fault in the interface cannot touch a run. Finished output is committed once to the terminal's scrollback; a small live area (the reply being streamed, a running tool call, status, queued messages, the editor and a footer with the directory, conversation, tokens, context and model) is redrawn from its first changed line, inside synchronized output. A reply that fails mid-stream only ever lived in the live area and simply goes away.
Editor: multi-line, history, bracketed paste with large pastes shown as atomic markers. Keys: Enter sends (queues while a reply runs), Ctrl+J or Alt+Enter adds a line, Esc interrupts, Ctrl+C clears and twice quits, Ctrl+D quits, Ctrl+O expands later tool output. Commands: new, resume with a picker, name, fork, compact, model, help, quit; commands typed during a reply wait for it. pic tui -c continues the latest conversation and shows its history; pic chat remains the plain prompt.
For clients, agent_start now carries the conversation, model and context window and message_end the usage; the conversation list and a conversation's entries are available as JSON. Tested with unit tests for the width, key, render and editor modules and end to end on a pseudo-terminal. (user: dmestas, size: 9302) [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, const char *conversation, const char *model, long context_window) { 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); fputs(",\"conversation\":", e->out); js(e->out, conversation); fputs(",\"model\":", e->out); js(e->out, model); fprintf(e->out, ",\"contextWindow\":%ld}", context_window); 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, const char *usage) { 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); if (usage && *usage) { fputs(",\"usage\":", e->out); fputs(usage, e->out); } 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); }