diff --git a/share/locale/de.msg b/share/locale/de.msg index b6e7d72..ae46e29 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -5,6 +5,7 @@ # selbst - nie als leere Zeile. ui.ready = bereit. Enter sendet, Ctrl-D beendet. +ui.more_lines = ... %zu Zeilen mehr ui.working = laeuft... ui.steering_queued = Steuerung eingereiht diff --git a/share/locale/en.msg b/share/locale/en.msg index 879cd32..0aad071 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -4,6 +4,7 @@ # key that exists nowhere is shown as itself, so nothing is silently empty. ui.ready = ready. Enter sends, Ctrl-D quits. +ui.more_lines = ... %zu more lines ui.working = working... ui.steering_queued = steering queued diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index 3c608bd..ea97db0 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -5,6 +5,7 @@ # uma linha vazia esconderia um bug. ui.ready = pronto. Enter envia, Ctrl-D sai. +ui.more_lines = ... %zu linhas a mais ui.working = trabalhando... ui.steering_queued = direcionamento na fila ui.prompt_mark = > diff --git a/src/ui.c b/src/ui.c index 4247813..89a61bd 100644 --- a/src/ui.c +++ b/src/ui.c @@ -69,11 +69,21 @@ static char g_model[128] = "model"; #define MAX_ENTRIES 512 +struct elayout; /* the wrapped lines of one entry, below */ + struct entry { char *role; char *text; time_t ts; /* when it arrived, for the IRC-style prefix */ size_t id; /* stable across eviction; see add_entry */ + /* + * The wrapped lines, kept between frames. Wrapping a long transcript + * from scratch on every keystroke and every 100 ms tick was the reason + * the window felt slow: one frame cost as much as the whole log. + * Rebuilt only when the text or the width changed. + */ + struct elayout *cache; + int cache_cols; }; struct ui_state { @@ -81,6 +91,8 @@ struct ui_state { size_t n; size_t next_id; /* ids start at 1 */ size_t scroll; /* lines scrolled back from the bottom */ + size_t scroll_max; /* maxscroll of the last frame */ + size_t scroll_base; /* maxscroll when the reader scrolled */ long last_ms; size_t tokens_in; size_t tokens_out; @@ -89,6 +101,7 @@ struct ui_state { char hist[128][1024]; /* what was typed before */ size_t nhist; int hist_pos; /* -1: editing a fresh line */ + int last_day; /* for the "--- 2026-11-13 ---" line */ struct sp_buf last_think; /* the previous turn's reasoning */ size_t last_steps; size_t last_calls; @@ -161,15 +174,25 @@ struct turn { char steer[1024]; int have_steer; int steers; - struct sp_buf live; /* streamed text so far */ - struct sp_buf think; /* the model's reasoning, raw */ - struct sp_buf flow; /* reasoning + tool markers, in order */ + struct sp_buf live; /* the current step's text, streamed */ + struct sp_buf think; /* the current step's reasoning */ + /* + * What the turn produced, in the order it happened: a finished step's + * reasoning, its text, the call it made, a note. The turn thread cannot + * touch the entry list, so it queues here and the UI drains this. Before, + * tool calls were pasted into the reasoning text and the answer text of + * all steps ran together in one buffer - two problems that only existed + * because there was no queue. + */ + struct tun_ev { + int kind; /* 't' reasoning, 'a' answer, 'x' tool, 'n' note */ + char *text; + } *ev; + int nev, capev; char tool[64]; /* last tool the model called */ char tool_line[512]; /* name + args, for the live area */ - char tools_done[16][256]; /* finished calls, in order */ char diffs[8][4096]; /* observations that carry a diff */ int ndiffs; - int ntools_done; int tool_tick; int compacted; /* compaction events to show in chat */ size_t c_bytes_before; @@ -339,33 +362,64 @@ pretty_args(const char *json, char *out, size_t cap) out[n < cap ? n : cap - 1] = '\0'; } +/* queue one event for the UI; the caller holds t->lock */ +static int +ev_push(struct turn *t, int kind, const char *text) +{ + struct tun_ev *nv; + + if (text == NULL || *text == '\0') + return 0; + if (t->nev == t->capev) { + int nc = t->capev != 0 ? t->capev * 2 : 16; + + nv = realloc(t->ev, (size_t)nc * sizeof(*nv)); + if (nv == NULL) + return -1; + t->ev = nv; + t->capev = nc; + } + t->ev[t->nev].text = strdup(text); + if (t->ev[t->nev].text == NULL) + return -1; + t->ev[t->nev].kind = kind; + t->nev++; + return 0; +} + +/* + * A tool call is its own line in the transcript, queued here (the turn runs on + * another thread and must not touch the entry list) and drained by the UI. + * It used to be faked into the reasoning text as "› name args", which made it + * uncountable, uncollapsible and a second copy of the same text. + */ static void tool_begin_cb(void *ctx, const char *name, const char *args) { struct turn *t = ctx; - char mark[640]; const char *a = args != NULL ? args : ""; + char shown[512]; if (name == NULL) return; + pretty_args(a, shown, sizeof(shown)); pthread_mutex_lock(&t->lock); snprintf(t->tool_line, sizeof(t->tool_line), "%s %s", name, a); - if (t->ntools_done < 16) - snprintf(t->tools_done[t->ntools_done++], - sizeof(t->tools_done[0]), "%s %s", name, a); - /* Flow inline in the thinking stream instead of a separate zone below. */ - /* the text before a tool call and the text after it are two paragraphs */ - if (t->live.len > 0 && t->live.p[t->live.len - 1] != '\n') - (void)sp_buf_puts(&t->live, "\n\n"); - if (t->flow.len > 0 && t->flow.p[t->flow.len - 1] != '\n') - (void)sp_buf_puts(&t->flow, "\n"); + /* + * The step is over as far as the screen is concerned: its reasoning and + * its text go into the queue in that order, then the call itself, and + * the buffers start empty for the next step. + */ + (void)ev_push(t, 't', t->think.p); + (void)ev_push(t, 'a', t->live.p); + sp_buf_clear(&t->think); + sp_buf_clear(&t->live); { - char shown[512]; + char call[640]; - pretty_args(a, shown, sizeof(shown)); - snprintf(mark, sizeof(mark), "\n› %s %.480s\n\n", name, shown); + snprintf(call, sizeof(call), "%s %.480s", name, shown); + (void)ev_push(t, 'x', call); } - (void)sp_buf_puts(&t->flow, mark); pthread_mutex_unlock(&t->lock); } @@ -381,7 +435,6 @@ stream_cb(void *ctx, int kind, const char *text) (void)sp_buf_puts(&t->live, text); } else if (kind == SP_STREAM_THINK) { (void)sp_buf_puts(&t->think, text); - (void)sp_buf_puts(&t->flow, text); } else if (kind == SP_STREAM_TOOL) { snprintf(t->tool, sizeof(t->tool), "%s", text); t->tool_tick++; @@ -411,11 +464,7 @@ ui_limit_wait(void *ctx, long until, const char *why) snprintf(line, sizeof(line), sp_t("ui.limit_wait"), when); pthread_mutex_lock(&t->lock); snprintf(t->tool_line, sizeof(t->tool_line), "%s", line); - if (t->flow.len > 0 && t->flow.p[t->flow.len - 1] != '\n') - (void)sp_buf_puts(&t->flow, "\n"); - (void)sp_buf_puts(&t->flow, "\xe2\x80\xba "); - (void)sp_buf_puts(&t->flow, line); - (void)sp_buf_puts(&t->flow, "\n\n"); + (void)ev_push(t, 'n', line); pthread_mutex_unlock(&t->lock); while ((long)time(NULL) < until && !t->cancel) { struct timespec ts = { 0, 250000000L }; @@ -1051,24 +1100,34 @@ elayout_free(struct elayout *l) memset(l, 0, sizeof(*l)); } -/* the visual lines of one entry, prefix included */ -static void -entry_layout(const struct entry *e, int cols, struct elayout *l) +/* the visual lines of one entry, prefix included; cached in the entry */ +static struct elayout * +entry_layout(struct entry *e, int cols) { char prefix[256]; char *render; size_t len; int ind; const char *brk; + struct elayout *l; - memset(l, 0, sizeof(*l)); + if (e->cache != NULL && e->cache_cols == cols) + return e->cache; + if (e->cache == NULL) { + e->cache = calloc(1, sizeof(*e->cache)); + if (e->cache == NULL) + return NULL; + } else + elayout_free(e->cache); + l = e->cache; + e->cache_cols = cols; entry_prefix(e, prefix, sizeof(prefix)); /* a table that starts the answer must not start behind the prefix */ brk = sp_md_table_starts(e->text) ? "\n" : ""; len = strlen(prefix) + strlen(brk) + strlen(e->text) + 1; render = malloc(len); if (render == NULL) - return; + return l; snprintf(render, len, "%s%s%s", prefix, brk, e->text); if (md_prepare(render, &l->plain, &l->attr) != 0) { free(l->plain); @@ -1106,6 +1165,7 @@ entry_layout(const struct entry *e, int cols, struct elayout *l) l->v[2 + k] = l->v[l->n - keep + k]; l->n = 2 + keep; } + return l; } static void @@ -1121,7 +1181,7 @@ draw_vline(int y, const struct elayout *l, const struct vline *vl) if (vl->skipped > 0) { char m[64]; - snprintf(m, sizeof(m), "... %zu more lines", vl->skipped); + snprintf(m, sizeof(m), sp_t("ui.more_lines"), vl->skipped); (void)wattr_set(ui_hist, base, PAIR_DIM, NULL); mvwaddstr(ui_hist, y, x, m); (void)wattr_set(ui_hist, base, pair, NULL); @@ -1148,37 +1208,43 @@ draw_vline(int y, const struct elayout *l, const struct vline *vl) } static void -draw_history(struct ui_state *s, int reserve) +draw_history(struct ui_state *s) { int rows, cols, y = 0; size_t idx, total = 0, first, line = 0, maxscroll; - struct elayout *lay; + struct elayout *l; + unsigned char *blank; getmaxyx(ui_hist, rows, cols); cols -= 2; /* padding left and right */ werase(ui_hist); wbkgd(ui_hist, COLOR_PAIR(PAIR_MAIN)); - /* while a turn runs the bottom lines belong to it */ - if (reserve > 0 && reserve < rows) - rows -= reserve; if (s->n == 0 || cols < 2 || rows < 1) { wnoutrefresh(ui_hist); return; } - lay = calloc(s->n, sizeof(*lay)); - if (lay == NULL) { + blank = calloc(s->n, 1); + if (blank == NULL) { wnoutrefresh(ui_hist); return; } for (idx = 0; idx < s->n; idx++) { - entry_layout(&s->entries[idx], cols, &lay[idx]); + l = entry_layout(&s->entries[idx], cols); + if (l == NULL) + continue; /* a new question starts a new exchange: one line of air */ - lay[idx].blank = idx > 0 && strcmp(s->entries[idx].role, "you") == 0; - total += lay[idx].n + (size_t)lay[idx].blank; + blank[idx] = idx > 0 && strcmp(s->entries[idx].role, "you") == 0; + total += l->n + (size_t)blank[idx]; } maxscroll = total > (size_t)rows ? total - (size_t)rows : 0; + /* scrolled up and text arrived below: keep the reader where he is */ + if (s->scroll > 0 && s->scroll <= s->scroll_max && + maxscroll > s->scroll_base) + s->scroll += maxscroll - s->scroll_base; if (s->scroll > maxscroll) s->scroll = maxscroll; + s->scroll_max = maxscroll; + s->scroll_base = maxscroll - s->scroll; first = maxscroll - s->scroll; /* index of the top visible line */ for (idx = 0; idx < s->n && y < rows; idx++) { @@ -1193,22 +1259,23 @@ draw_history(struct ui_state *s, int reserve) wattrset(ui_hist, COLOR_PAIR(PAIR_ERROR) | A_BOLD); else wattrset(ui_hist, COLOR_PAIR(PAIR_DIM)); - if (lay[idx].blank) { + if (blank[idx]) { if (line >= first && y < rows) y++; line++; } - for (k = 0; k < lay[idx].n && y < rows; k++, line++) { + l = s->entries[idx].cache; + if (l == NULL) + continue; + for (k = 0; k < l->n && y < rows; k++, line++) { if (line < first) continue; - draw_vline(y, &lay[idx], &lay[idx].v[k]); + draw_vline(y, l, &l->v[k]); y++; } wattrset(ui_hist, A_NORMAL); } - for (idx = 0; idx < s->n; idx++) - elayout_free(&lay[idx]); - free(lay); + free(blank); wnoutrefresh(ui_hist); } @@ -1541,6 +1608,12 @@ entry_set_text(struct ui_state *s, size_t id, const char *text) return; free(s->entries[i - 1].text); s->entries[i - 1].text = copy; + /* the wrapped lines belong to the old text */ + if (s->entries[i - 1].cache != NULL) { + elayout_free(s->entries[i - 1].cache); + free(s->entries[i - 1].cache); + s->entries[i - 1].cache = NULL; + } } /* @@ -1558,6 +1631,10 @@ add_entry(struct ui_state *s, const char *role, const char *text) if (s->n == MAX_ENTRIES) { free(s->entries[0].role); free(s->entries[0].text); + if (s->entries[0].cache != NULL) { + elayout_free(s->entries[0].cache); + free(s->entries[0].cache); + } memmove(&s->entries[0], &s->entries[1], sizeof(s->entries[0]) * (MAX_ENTRIES - 1)); s->n--; @@ -1574,14 +1651,15 @@ add_entry(struct ui_state *s, const char *role, const char *text) } e->ts = time(NULL); e->id = id = ++s->next_id; + e->cache = NULL; + e->cache_cols = 0; { - static int last_day; struct tm tm; int day; (void)localtime_r(&e->ts, &tm); day = tm.tm_yday + tm.tm_year * 400; - if (last_day != 0 && day != last_day) { + if (s->last_day != 0 && day != s->last_day) { char stamp[64]; struct entry *d; @@ -1598,6 +1676,8 @@ add_entry(struct ui_state *s, const char *role, const char *text) if (d->role != NULL && d->text != NULL) { d->ts = e->ts; d->id = ++s->next_id; + d->cache = NULL; + d->cache_cols = 0; s->n++; } else { free(d->role); @@ -1605,7 +1685,7 @@ add_entry(struct ui_state *s, const char *role, const char *text) } } } - last_day = day; + s->last_day = day; } return id; } @@ -1739,7 +1819,7 @@ relayout(int input_timeout_ms) static void redraw(struct ui_state *s, struct sp_agent *ag, struct sp_host *host) { - draw_history(s, 0); + draw_history(s); draw_sidebar(s, SESS_ID(ag), SESS_DIR(ag), host); doupdate(); } @@ -1905,7 +1985,7 @@ ui_read_line(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, got = 1; } if (got) - draw_history(s, 0); + draw_history(s); } draw_sidebar(s, SESS_ID(ag), SESS_DIR(ag), host); if (locked) { @@ -1935,7 +2015,7 @@ ui_read_line(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, relayout(500); else clearok(curscr, TRUE); - draw_history(s, 0); + draw_history(s); draw_sidebar(s, SESS_ID(ag), SESS_DIR(ag), host); if (locked) { werase(ui_input); @@ -1981,7 +2061,7 @@ ui_read_line(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, */ if (ch == KEY_PPAGE || ch == KEY_SR) { s->scroll += (ch == KEY_SR ? 1 : 10); - draw_history(s, 0); + draw_history(s); draw_sidebar(s, SESS_ID(ag), SESS_DIR(ag), host); if (locked) { werase(ui_input); @@ -1996,7 +2076,7 @@ ui_read_line(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, size_t step = (ch == KEY_SF ? 1 : 10); s->scroll = s->scroll > step ? s->scroll - step : 0; - draw_history(s, 0); + draw_history(s); draw_sidebar(s, SESS_ID(ag), SESS_DIR(ag), host); if (locked) { werase(ui_input); @@ -2032,7 +2112,7 @@ ui_read_line(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, (s->agent_cur + 1) % (int)s->agents.n : (s->agent_cur + (int)s->agents.n - 1) % (int)s->agents.n, 1); - draw_history(s, 0); + draw_history(s); draw_sidebar(s, SESS_ID(ag), SESS_DIR(ag), host); draw_prompt(input); wnoutrefresh(ui_input); @@ -2159,7 +2239,7 @@ ui_connect_ask(struct ui_state *s, struct sp_buf *out) } } -/* returns 1: the line was an answer (handled), 0: no flow is running */ +/* returns 1: the line was an answer (handled), 0: nothing is running */ static int ui_connect_answer(struct ui_state *s, struct sp_agent *ag, const char *line, struct sp_buf *out) @@ -2651,12 +2731,12 @@ ui_slash(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, /* what the screen keeps of a running turn between two ticks */ struct turn_view { struct timespec t0; - int drained; /* tool calls already in the transcript */ + int drained; /* events already in the transcript */ int drained_d; /* diffs already in the transcript */ struct seg { size_t id; /* entry showing the open part, 0 = none */ - size_t off; /* bytes already frozen in earlier entries */ - size_t shown; /* bytes in the open entry */ + size_t off; /* bytes before the open entry */ + size_t shown; /* bytes the open entry shows */ } think, ans; /* the reasoning, the answer */ char line[1024]; /* steering text being typed */ size_t llen; @@ -2680,6 +2760,12 @@ seg_show(struct ui_state *s, struct seg *g, const char *role, const char *text) g->shown = 0; g->id = 0; } + /* + * While the open entry is the last one it may keep growing. Once + * something was appended below it (a tool call, the answer), what is on + * screen stays as it is: the bytes it shows move into off and the rest + * continues in a new entry. That is the whole append-only rule. + */ if (g->id != 0 && !(s->n > 0 && s->entries[s->n - 1].id == g->id)) { g->off += g->shown; g->shown = 0; @@ -2698,7 +2784,7 @@ seg_show(struct ui_state *s, struct seg *g, const char *role, const char *text) g->shown = len - g->off; } -/* diffs of the turn into the transcript; tool calls flow in the thinking stream */ +/* a turn's side effects into the transcript: diffs, calls, compaction */ static void compact_cb(void *ctx, size_t before, size_t after, size_t mb, size_t ma) { @@ -2720,9 +2806,22 @@ turn_drain(struct ui_state *s, struct turn *tu, struct turn_view *v) { int k, compacted = 0; size_t cb = 0, ca = 0, cm = 0, cn = 0; + struct tun_ev ev[24]; + int nev = 0; pthread_mutex_lock(&tu->lock); - v->drained = tu->ntools_done; + /* + * Take the events that arrived since the last tick, in order. The + * strings change hands here: the turn drops its pointer, the UI adds + * the entry and frees it. + */ + while (v->drained < tu->nev && nev < (int)(sizeof(ev) / sizeof(ev[0]))) { + ev[nev].kind = tu->ev[v->drained].kind; + ev[nev].text = tu->ev[v->drained].text; + tu->ev[v->drained].text = NULL; + nev++; + v->drained++; + } for (k = v->drained_d; k < tu->ndiffs; k++) add_entry(s, "diff", tu->diffs[k]); v->drained_d = tu->ndiffs; @@ -2733,6 +2832,14 @@ turn_drain(struct ui_state *s, struct turn *tu, struct turn_view *v) tu->c_bytes_before = tu->c_bytes_after = 0; tu->c_msgs_before = tu->c_msgs_after = 0; pthread_mutex_unlock(&tu->lock); + for (k = 0; k < nev; k++) { + const char *role = ev[k].kind == 'x' ? "tool" : + ev[k].kind == 'n' ? "splinter" : + ev[k].kind == 't' ? "thinking" : "agent"; + + add_entry(s, role, ev[k].text); + free(ev[k].text); + } if (compacted > 0) { char msg[256]; size_t tb = cb / 4, ta = ca / 4; @@ -2814,27 +2921,22 @@ turn_paint(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, int w, used; pthread_mutex_lock(&tu->lock); - { - const struct sp_buf *show = - (tu->flow.len > 0 ? &tu->flow : &tu->think); - - if (show->len > 0 && show->p != NULL) - think = strdup(show->p); - } + if (tu->think.len > 0 && tu->think.p != NULL) + think = strdup(tu->think.p); if (tu->live.len > 0 && tu->live.p != NULL) live = strdup(tu->live.p); pthread_mutex_unlock(&tu->lock); + turn_drain(s, tu, v); if (think != NULL) seg_show(s, &v->think, "thinking", think); - turn_drain(s, tu, v); if (live != NULL) { seg_show(s, &v->ans, "agent", live); s->out_chars = strlen(live); } free(think); free(live); - draw_history(s, 0); + draw_history(s); draw_sidebar(s, SESS_ID(ag), SESS_DIR(ag), host); /* a fleet moves on while a turn runs; nothing draws it any more */ if (s->sw != NULL) @@ -2978,7 +3080,6 @@ ui_turn(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, sp_buf_init(&tu.err); sp_buf_init(&tu.live); sp_buf_init(&tu.think); - sp_buf_init(&tu.flow); pthread_mutex_init(&tu.lock, NULL); pthread_cond_init(&tu.cond, NULL); cfg.cancel = &tu.cancel; @@ -3078,18 +3179,11 @@ ui_turn(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, } turn_drain(s, &tu, &v); /* - * The reasoning keeps its full text. Tool calls flow inline in it, - * not in a separate zone below. + * The reasoning is on screen already (it streamed); the log keeps + * the whole of it, tool calls included as their own entries. */ - { - const struct sp_buf *show = - (tu.flow.len > 0 ? &tu.flow : &tu.think); - - if (show->len > 0 && show->p != NULL) { - sp_logf("thinking", "%s", show->p); - seg_show(s, &v.think, "thinking", show->p); - } - } + if (tu.think.len > 0 && tu.think.p != NULL) + sp_logf("thinking", "%s", tu.think.p); } /* * Restore the caller's config: the patched copy lives on this stack and @@ -3146,7 +3240,13 @@ ui_turn(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, sp_buf_free(&tu.err); sp_buf_free(&tu.live); sp_buf_free(&tu.think); - sp_buf_free(&tu.flow); + { + int k; + + for (k = 0; k < tu.nev; k++) + free(tu.ev[k].text); + free(tu.ev); + } pthread_cond_destroy(&tu.cond); pthread_mutex_destroy(&tu.lock); sp_buf_free(&answer); @@ -3269,7 +3369,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *model, if (ui_start(&s, ag, host, endpoint, model, theme_name, sw, conf) != 0) return 1; for (;;) { - draw_history(&s, 0); + draw_history(&s); draw_sidebar(&s, SESS_ID(ag), SESS_DIR(ag), host); werase(ui_input); mvwaddstr(ui_input, 0, 0, prompt_mark()); @@ -3310,7 +3410,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *model, hist_push(&s, SESS_DIR(ag), input); s.scroll = 0; add_entry(&s, "you", input); - draw_history(&s, 0); + draw_history(&s); draw_sidebar(&s, SESS_ID(ag), SESS_DIR(ag), host); werase(ui_input); mvwaddstr(ui_input, 0, 0, sp_t("ui.working"));