Artifact 45696df29e9b5aadcbd43264b71f67d0ca20f567896c60b8ff35ff0d640663e5:
- File
src/events.h
— 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: 3187) [annotate] [blame] [check-ins using] [more...]
/* 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") and context_window (tokens, 0 * unknown) 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); 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