diff --git a/src/ui.c b/src/ui.c index d07b787..da2df1e 100644 --- a/src/ui.c +++ b/src/ui.c @@ -54,7 +54,6 @@ struct ui_state { size_t tokens_in; size_t tokens_out; size_t out_chars; /* characters streamed in this/last turn */ - size_t think_back; /* lines scrolled back in the thinking field */ char hist[128][1024]; /* what was typed before */ size_t nhist; int hist_pos; /* -1: editing a fresh line */ @@ -393,6 +392,22 @@ ui_add_text(WINDOW *w, const char *s, int maxcols) +/* A role is stored as its English key and displayed in the chosen language. */ +static const char * +role_label(const char *role) +{ + char key[48]; + const char *t; + + if (role == NULL) + return ""; + snprintf(key, sizeof(key), "role.%s", role); + t = sp_t(key); + if (t == NULL || strcmp(t, key) == 0) + return role; + return t; +} + /* * Row accounting for text that contains newlines. * @@ -423,116 +438,6 @@ text_rows(const char *text, int cols) return rows; } -/* Byte offset of row `want`, walking the same structure text_rows uses. */ -static size_t -row_offset(const char *text, int cols, size_t want) -{ - size_t row = 0; - const char *p = text; - - while (*p != '\0') { - const char *nl = strchr(p, '\n'); - size_t linelen = nl != NULL ? (size_t)(nl - p) : strlen(p); - size_t rows = linelen == 0 ? 1 : - (linelen + (size_t)cols - 1) / (size_t)cols; - - if (want < row + rows) - return (size_t)(p - text) + - (want - row) * (size_t)cols; - row += rows; - if (nl == NULL) - break; - p = nl + 1; - } - return strlen(text); -} - -/* - * Print the tail of `text` into a fixed region, bottom-anchored, optional - * scroll-back in rows. Newline aware, so the last row on screen really is the - * last row of the text. - */ -static void -draw_wrapped_tail(int y0, int h, int cols, const char *text, int pair, - size_t back) -{ - size_t rows, first, off, row; - - if (text == NULL || *text == '\0' || h < 1 || cols < 4) - return; - rows = text_rows(text, cols); - if (rows == 0) - return; - first = rows > (size_t)h ? rows - (size_t)h : 0; - if (back > 0) { - if (back > first) - back = first; - first -= back; - } - - if (pair) - wattron(ui_hist, COLOR_PAIR(pair)); - off = row_offset(text, cols, first); - row = 0; - while (row < (size_t)h && off < strlen(text)) { - const char *nl = strchr(text + off, '\n'); - size_t linelen = nl != NULL ? - (size_t)(nl - (text + off)) : strlen(text + off); - size_t take = linelen > (size_t)cols ? - (size_t)cols : linelen; - char slice[512]; - - if (take >= sizeof(slice)) - take = sizeof(slice) - 1; - memcpy(slice, text + off, take); - slice[take] = '\0'; - wmove(ui_hist, y0 + (int)row, 0); - ui_add_text(ui_hist, slice, cols - 1); - row++; - off += take; - if (linelen > (size_t)cols) { - size_t wrapped = take; - - /* the rest of this long line continues on the next row */ - while (wrapped < linelen && row < (size_t)h) { - size_t t2 = linelen - wrapped; - - if (t2 > (size_t)cols) - t2 = (size_t)cols; - if (t2 >= sizeof(slice)) - t2 = sizeof(slice) - 1; - memcpy(slice, text + off, t2); - slice[t2] = '\0'; - wmove(ui_hist, y0 + (int)row, 0); - ui_add_text(ui_hist, slice, cols - 1); - row++; - off += t2; - wrapped += t2; - } - } else if (nl != NULL) { - off++; /* skip the newline itself */ - } - } - if (pair) - wattroff(ui_hist, COLOR_PAIR(pair)); -} - -/* A role is stored as its English key and displayed in the chosen language. */ -static const char * -role_label(const char *role) -{ - char key[48]; - const char *t; - - if (role == NULL) - return ""; - snprintf(key, sizeof(key), "role.%s", role); - t = sp_t(key); - if (t == NULL || strcmp(t, key) == 0) - return role; - return t; -} - /* * Rows an entry really occupies: the role prefix shortens the first row, so the * height is computed from exactly the string that gets drawn ("role: text"), @@ -655,81 +560,6 @@ draw_history(struct ui_state *s, int reserve) * place the conversation lives: the model's reasoning dimmed above, the answer * as it arrives below. The sidebar keeps the counters - numbers are not text. */ -/* - * The live block has a fixed height, chosen when the turn starts and never - * recomputed: a region that grows with the text moves everything around it, and - * text that "goes anywhere on the screen" is unreadable. - */ -#define LIVE_THINK_ROWS 8 -#define LIVE_ANSWER_ROWS 5 -#define LIVE_TOOL_ROWS 1 - -static int -live_height(int rows, int len, const char *tool, const char *think) -{ - int h = LIVE_THINK_ROWS + LIVE_ANSWER_ROWS + LIVE_TOOL_ROWS; - - (void)len; - (void)tool; - (void)think; - if (h >= rows - 2) - h = rows - 3; - if (h < 3) - h = 3; - return h; -} - -/* - * While a turn runs, the bottom of the chat pane shows what is happening: the - * model's reasoning dimmed directly above the answer as it arrives, tool name - * under it. Anchored at the bottom, so it appears where the conversation is, - * not at the top of the window. - */ -static void -draw_live(struct ui_state *s, const char *text, const char *tool, - const char *think) -{ - int rows, cols, h, answer_rows, think_rows, y_answer; - - if ((text == NULL || text[0] == '\0') && - (think == NULL || think[0] == '\0') && - (tool == NULL || tool[0] == '\0')) - return; - getmaxyx(ui_hist, rows, cols); - if (rows < 5 || cols < 8) - return; - - h = live_height(rows, text != NULL ? (int)strlen(text) : 0, tool, think); - if (h <= 0) - return; - answer_rows = LIVE_ANSWER_ROWS; - think_rows = LIVE_THINK_ROWS; - if (answer_rows + think_rows + LIVE_TOOL_ROWS > h) { - answer_rows = h / 2; - think_rows = h - answer_rows - LIVE_TOOL_ROWS; - } - y_answer = rows - answer_rows - LIVE_TOOL_ROWS; - - if (think_rows > 0) - draw_wrapped_tail(y_answer - think_rows, think_rows, cols, think, - PAIR_DIM, s->think_back); - if (answer_rows > 0) - draw_wrapped_tail(y_answer, answer_rows, cols, text, - PAIR_ACCENT, 0); - if (tool != NULL && tool[0] != '\0') { - char head[320]; - - snprintf(head, sizeof(head), " %s", tool); - wattron(ui_hist, COLOR_PAIR(PAIR_MAIN) | A_BOLD); - wmove(ui_hist, rows - 1, 0); - ui_add_text(ui_hist, head, cols - 1); - wclrtoeol(ui_hist); - wattroff(ui_hist, A_BOLD); - } - (void)s; - wnoutrefresh(ui_hist); -} - static void sidebar_heading(int *y, int cols, const char *text) { @@ -874,27 +704,20 @@ hist_push(struct ui_state *s, const char *dir, const char *line) fclose(f); } -/* Insert at a position: the reasoning happened before the tools that followed - * it, and the transcript should read in that order. */ +/* Replace an entry's text in place: a streaming turn keeps one entry per role + * and rewrites it, so the transcript grows where the reader is looking. */ static void -entry_insert(struct ui_state *s, size_t at, const char *role, const char *text) +entry_set_text(struct ui_state *s, size_t idx, const char *text) { - char *r, *x; + char *copy; - if (at > s->n || s->n == MAX_ENTRIES) + if (idx >= s->n) return; - r = strdup(role != NULL ? role : ""); - x = strdup(text != NULL ? text : ""); - if (r == NULL || x == NULL) { - free(r); - free(x); + copy = strdup(text != NULL ? text : ""); + if (copy == NULL) return; - } - memmove(&s->entries[at + 1], &s->entries[at], - sizeof(s->entries[0]) * (s->n - at)); - s->entries[at].role = r; - s->entries[at].text = x; - s->n++; + free(s->entries[idx].text); + s->entries[idx].text = copy; } static void @@ -943,6 +766,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, { struct ui_state s; struct sp_buf answer = { 0 }, err = { 0 }; + size_t ans_idx = (size_t)-1; /* live answer entry, if any */ char input[1024]; int rc = 0; @@ -1231,7 +1055,9 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, char line[1024]; size_t llen = 0; int cancelled = 0, drained = 0; - size_t first_tool = (size_t)-1; + size_t think_idx = (size_t)-1; + + ans_idx = (size_t)-1; wtimeout(ui_input, 100); for (;;) { @@ -1248,57 +1074,63 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, c = wgetch(ui_input); if (c == ERR) { - char live[4096]; - char think[2048]; - int live_reserve = 0; - char tool[256]; - size_t llen2 = 0, tlen2 = 0; + char think[4096]; + char live[8192]; + size_t tlen2 = 0, llen2 = 0; pthread_mutex_lock(&tu.lock); - if (tu.live.len > 0) { - llen2 = tu.live.len; - if (llen2 > sizeof(live) - 1) - llen2 = sizeof(live) - 1; - memcpy(live, tu.live.p, llen2); - } if (tu.think.len > 0) { tlen2 = tu.think.len; if (tlen2 > sizeof(think) - 1) tlen2 = sizeof(think) - 1; memcpy(think, tu.think.p, tlen2); } - snprintf(tool, sizeof(tool), "%.200s", - tu.tool_line[0] != '\0' ? - tu.tool_line : tu.tool); + if (tu.live.len > 0) { + llen2 = tu.live.len; + if (llen2 > sizeof(live) - 1) + llen2 = sizeof(live) - 1; + memcpy(live, tu.live.p, llen2); + } pthread_mutex_unlock(&tu.lock); - live[llen2] = '\0'; think[tlen2] = '\0'; - live_reserve = live_height( - getmaxy(ui_hist), - (int)llen2, tool, think); + live[llen2] = '\0'; + + /* + * The turn is not drawn into a box of its own: + * it is two entries that grow in place, the + * reasoning first, the answer after the tool + * calls. Nothing moves when the turn ends - + * which is what a jumping block feels like. + */ if (tlen2 > 0) { - sp_buf_clear(&s.last_think); - (void)sp_buf_puts(&s.last_think, + if (think_idx == (size_t)-1) { + think_idx = s.n; + add_entry(&s, "thinking", ""); + } + entry_set_text(&s, think_idx, think); } - s.out_chars = llen2; if (tu.ntools_done > drained) { int k; - for (k = drained; k < tu.ntools_done; - k++) { - char line[300]; - - snprintf(line, sizeof(line), - "%s", tu.tools_done[k]); - if (first_tool == (size_t)-1) - first_tool = s.n; - add_entry(&s, "tool", line); - } + pthread_mutex_lock(&tu.lock); + for (k = drained; + k < tu.ntools_done; k++) + add_entry(&s, "tool", + tu.tools_done[k]); drained = tu.ntools_done; + pthread_mutex_unlock(&tu.lock); } - draw_history(&s, live_reserve); - draw_live(&s, live, tool, think); + if (llen2 > 0) { + if (ans_idx == (size_t)-1) { + ans_idx = s.n; + add_entry(&s, "agent", ""); + } + entry_set_text(&s, ans_idx, + live); + } + s.out_chars = llen2; + draw_history(&s, 0); doupdate(); /* redraw the clock in the status bar */ clock_gettime(CLOCK_MONOTONIC, &now); @@ -1315,7 +1147,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, continue; } if (c == KEY_PPAGE) { - s.think_back += 3; + s.scroll += 3; draw_history(&s, 0); draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL, @@ -1326,10 +1158,10 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, continue; } if (c == KEY_NPAGE) { - if (s.think_back > 3) - s.think_back -= 3; + if (s.scroll > 3) + s.scroll = s.scroll >= 3 ? s.scroll - 3 : 0; else - s.think_back = 0; + s.scroll = 0; continue; } if (c == 3) { @@ -1402,40 +1234,29 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, int k; pthread_mutex_lock(&tu.lock); - for (k = drained; k < tu.ntools_done; k++) { - if (first_tool == (size_t)-1) - first_tool = s.n; + for (k = drained; k < tu.ntools_done; k++) add_entry(&s, "tool", tu.tools_done[k]); - } drained = tu.ntools_done; pthread_mutex_unlock(&tu.lock); } /* the reasoning stays in the transcript, as a dim * entry, capped so a long plan cannot push the chat * out of the window */ + /* + * The reasoning keeps its full text. Cutting it to a tail + * at the end of the turn made the block shrink, and + * everything below it jumped up - the same complaint as + * before, one layer deeper. It grew here, it stays here. + */ if (tu.think.len > 0) { - const char *th = tu.think.p; - size_t tl = tu.think.len; - - if (tl > 400) { - size_t skip = tl - 400; - - /* start at a word boundary: a tail - * that begins mid-word reads as - * corruption */ - while (tu.think.p[skip] != '\0' && - tu.think.p[skip] != ' ' && - tu.think.p[skip] != '\n') - skip++; - th = tu.think.p + skip; - tl -= skip; - } - if (first_tool == (size_t)-1) - add_entry(&s, "thinking", th); - else - entry_insert(&s, first_tool, - "thinking", th); + if (think_idx == (size_t)-1) { + think_idx = s.n; + add_entry(&s, "thinking", + tu.think.p); + } else + entry_set_text(&s, think_idx, + tu.think.p); } sp_buf_free(&tu.answer); sp_buf_free(&tu.err); @@ -1453,9 +1274,12 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, s.tokens_in = ag->stats.tokens_in; s.tokens_out = ag->stats.tokens_out; - if (rc == 0) - add_entry(&s, "agent", answer.p); - else + if (rc == 0) { + if (ans_idx != (size_t)-1) + entry_set_text(&s, ans_idx, answer.p); + else + add_entry(&s, "agent", answer.p); + } else add_entry(&s, "error", err.p != NULL ? err.p : sp_t("err.turn_failed")); diff --git a/tests/test_ascii b/tests/test_ascii index e290c4e..0886ff0 100755 Binary files a/tests/test_ascii and b/tests/test_ascii differ