diff --git a/Makefile b/Makefile index f9a2e4e..bc1c307 100644 --- a/Makefile +++ b/Makefile @@ -23,14 +23,14 @@ CURSES_A = $(CURSES_DIR)/libcurses/libcurses.a \ LIB = libsplinter.so LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \ src/json.o src/sse.o src/openai.o src/transport_openai.o src/builtin.o \ - src/agent.o src/theme.o src/palettes.o src/config.o src/plugin.o + src/agent.o src/theme.o src/palettes.o src/decomp.o src/config.o src/plugin.o BIN = splinter BINOBJ = src/main.o src/ui.o PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \ plugins/cli/cli.so plugins/focus/focus.so TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \ tests/test_transport tests/test_agent tests/test_openai \ - tests/test_theme tests/test_plugin tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor + tests/test_theme tests/test_plugin tests/test_ascii tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor RPATH = -Wl,-rpath,'$$ORIGIN' PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..' @@ -91,9 +91,10 @@ src/transport_openai.o: include/splinter.h src/builtin.o: include/splinter.h src/theme.o: src/theme.h src/palettes.h include/splinter.h src/palettes.o: src/palettes.h +src/decomp.o: src/decomp.h src/config.o: src/config.h include/splinter.h src/plugin.o: include/splinter.h include/splinter_plugin.h -src/ui.o: src/ui.h src/theme.h include/splinter.h include/splinter_plugin.h +src/ui.o: src/ui.h src/theme.h src/decomp.h include/splinter.h include/splinter_plugin.h src/main.o: include/splinter.h src/config.h include/splinter_plugin.h src/ui.h src/theme.h tests/test_buf: tests/test_buf.c $(LIB) @@ -114,6 +115,9 @@ tests/test_theme: tests/test_theme.c $(LIB) $(CC) $(CFLAGS) -o $@ tests/test_theme.c -L. -lsplinter $(TEST_RPATH) $(LIBS) tests/test_focus: tests/test_focus.c $(LIB) $(PLUGIN) $(CC) $(CFLAGS) -o $@ tests/test_focus.c -L. -lsplinter $(TEST_RPATH) $(LIBS) +tests/test_ascii: tests/test_ascii.c $(LIB) $(BINOBJ) + $(CC) $(CFLAGS) -o $@ tests/test_ascii.c src/ui.o -L. -lsplinter \ + $(TEST_RPATH) $(CURSES_A) $(LIBS) tests/test_config: tests/test_config.c $(LIB) $(CC) $(CFLAGS) -o $@ tests/test_config.c -L. -lsplinter $(TEST_RPATH) $(LIBS) tests/test_sse: tests/test_sse.c $(LIB) diff --git a/libsplinter.so b/libsplinter.so index 16709d0..f41d6df 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/src/builtin.c b/src/builtin.c index 45c2b48..aa06898 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -16,12 +16,52 @@ #include #include "splinter.h" +#include "mcp.h" + + +/* + * The model sends arguments as a JSON object ("{\"command\":\"ls\"}"). + * Handing that string to popen() runs the JSON as a shell command - which is + * exactly the bug this function exists to prevent. It accepts both shapes: a + * JSON object with the named field, or a bare string (the mock transport and a + * hand-written script send the second). + */ +static const char * +arg_text(const char *args, const char *key, struct sp_buf *keep) +{ + mcp_json *v; + const char *val = NULL; + + if (args == NULL || *args == '\0') + return NULL; + if (args[0] != '{') + return args; + v = mcp_parse(args); + if (v == NULL) + return args; + val = mcp_arg_string(v, key); + if (val != NULL && sp_buf_puts(keep, val) == 0) + val = keep->p; + else + val = NULL; + mcp_json_free(v); + return val; +} static int tool_echo(const char *args, struct sp_buf *out, void *ctx) { + struct sp_buf keep; + const char *text; + int rc; + (void)ctx; - return sp_buf_puts(out, args != NULL ? args : ""); + if (sp_buf_init(&keep) != 0) + return -1; + text = arg_text(args, "text", &keep); + rc = sp_buf_puts(out, text != NULL ? text : ""); + sp_buf_free(&keep); + return rc; } static int @@ -32,13 +72,23 @@ tool_run(const char *args, struct sp_buf *out, void *ctx) size_t got, total = 0; int status; + struct sp_buf keep; + const char *cmd; + int rc; + (void)ctx; - if (args == NULL || *args == '\0') { + if (sp_buf_init(&keep) != 0) + return -1; + cmd = arg_text(args, "command", &keep); + if (cmd == NULL || *cmd == '\0') { + sp_buf_free(&keep); return sp_buf_puts(out, "error: empty command"); } - p = popen(args, "r"); + p = popen(cmd, "r"); if (p == NULL) { - return sp_buf_printf(out, "error: popen: %s", strerror(errno)); + rc = sp_buf_printf(out, "error: popen: %s", strerror(errno)); + sp_buf_free(&keep); + return rc; } while ((got = fread(chunk, 1, sizeof(chunk), p)) > 0) { if (total > 1u << 20) { /* 1 MiB cap, stated not silent */ @@ -52,6 +102,7 @@ tool_run(const char *args, struct sp_buf *out, void *ctx) total += got; } status = pclose(p); + sp_buf_free(&keep); if (status < 0) { return sp_buf_printf(out, "\n[exit status unknown: %s]", strerror(errno)); diff --git a/src/decomp.c b/src/decomp.c new file mode 100644 index 0000000..c9ceaf4 --- /dev/null +++ b/src/decomp.c @@ -0,0 +1,278 @@ +/* + * Generated by tools/gen_decomp.py from Unicode decompositions. + * Do not edit by hand. The UI transliterates with this table because the + * vendored curses writes Latin-1 bytes for wide characters. + */ +#include +#include "decomp.h" + +const struct sp_decomp sp_decomp_table[] = { + { 0x00c0, "A" }, + { 0x00c1, "A" }, + { 0x00c2, "A" }, + { 0x00c3, "A" }, + { 0x00c4, "Ae" }, + { 0x00c5, "A" }, + { 0x00c6, "AE" }, + { 0x00c7, "C" }, + { 0x00c8, "E" }, + { 0x00c9, "E" }, + { 0x00ca, "E" }, + { 0x00cb, "E" }, + { 0x00cc, "I" }, + { 0x00cd, "I" }, + { 0x00ce, "I" }, + { 0x00cf, "I" }, + { 0x00d0, "D" }, + { 0x00d1, "N" }, + { 0x00d2, "O" }, + { 0x00d3, "O" }, + { 0x00d4, "O" }, + { 0x00d5, "O" }, + { 0x00d6, "Oe" }, + { 0x00d8, "O" }, + { 0x00d9, "U" }, + { 0x00da, "U" }, + { 0x00db, "U" }, + { 0x00dc, "Ue" }, + { 0x00dd, "Y" }, + { 0x00de, "Th" }, + { 0x00df, "ss" }, + { 0x00e0, "a" }, + { 0x00e1, "a" }, + { 0x00e2, "a" }, + { 0x00e3, "a" }, + { 0x00e4, "ae" }, + { 0x00e5, "a" }, + { 0x00e6, "ae" }, + { 0x00e7, "c" }, + { 0x00e8, "e" }, + { 0x00e9, "e" }, + { 0x00ea, "e" }, + { 0x00eb, "e" }, + { 0x00ec, "i" }, + { 0x00ed, "i" }, + { 0x00ee, "i" }, + { 0x00ef, "i" }, + { 0x00f0, "d" }, + { 0x00f1, "n" }, + { 0x00f2, "o" }, + { 0x00f3, "o" }, + { 0x00f4, "o" }, + { 0x00f5, "o" }, + { 0x00f6, "oe" }, + { 0x00f8, "o" }, + { 0x00f9, "u" }, + { 0x00fa, "u" }, + { 0x00fb, "u" }, + { 0x00fc, "ue" }, + { 0x00fd, "y" }, + { 0x00fe, "th" }, + { 0x00ff, "y" }, + { 0x0100, "A" }, + { 0x0101, "a" }, + { 0x0102, "A" }, + { 0x0103, "a" }, + { 0x0104, "A" }, + { 0x0105, "a" }, + { 0x0106, "C" }, + { 0x0107, "c" }, + { 0x0108, "C" }, + { 0x0109, "c" }, + { 0x010a, "C" }, + { 0x010b, "c" }, + { 0x010c, "C" }, + { 0x010d, "c" }, + { 0x010e, "D" }, + { 0x010f, "d" }, + { 0x0110, "D" }, + { 0x0111, "d" }, + { 0x0112, "E" }, + { 0x0113, "e" }, + { 0x0114, "E" }, + { 0x0115, "e" }, + { 0x0116, "E" }, + { 0x0117, "e" }, + { 0x0118, "E" }, + { 0x0119, "e" }, + { 0x011a, "E" }, + { 0x011b, "e" }, + { 0x011c, "G" }, + { 0x011d, "g" }, + { 0x011e, "G" }, + { 0x011f, "g" }, + { 0x0120, "G" }, + { 0x0121, "g" }, + { 0x0122, "G" }, + { 0x0123, "g" }, + { 0x0124, "H" }, + { 0x0125, "h" }, + { 0x0126, "H" }, + { 0x0127, "h" }, + { 0x0128, "I" }, + { 0x0129, "i" }, + { 0x012a, "I" }, + { 0x012b, "i" }, + { 0x012c, "I" }, + { 0x012d, "i" }, + { 0x012e, "I" }, + { 0x012f, "i" }, + { 0x0130, "I" }, + { 0x0131, "i" }, + { 0x0134, "J" }, + { 0x0135, "j" }, + { 0x0136, "K" }, + { 0x0137, "k" }, + { 0x0138, "k" }, + { 0x0139, "L" }, + { 0x013a, "l" }, + { 0x013b, "L" }, + { 0x013c, "l" }, + { 0x013d, "L" }, + { 0x013e, "l" }, + { 0x0141, "L" }, + { 0x0142, "l" }, + { 0x0143, "N" }, + { 0x0144, "n" }, + { 0x0145, "N" }, + { 0x0146, "n" }, + { 0x0147, "N" }, + { 0x0148, "n" }, + { 0x014c, "O" }, + { 0x014d, "o" }, + { 0x014e, "O" }, + { 0x014f, "o" }, + { 0x0150, "O" }, + { 0x0151, "o" }, + { 0x0152, "OE" }, + { 0x0153, "oe" }, + { 0x0154, "R" }, + { 0x0155, "r" }, + { 0x0156, "R" }, + { 0x0157, "r" }, + { 0x0158, "R" }, + { 0x0159, "r" }, + { 0x015a, "S" }, + { 0x015b, "s" }, + { 0x015c, "S" }, + { 0x015d, "s" }, + { 0x015e, "S" }, + { 0x015f, "s" }, + { 0x0160, "S" }, + { 0x0161, "s" }, + { 0x0162, "T" }, + { 0x0163, "t" }, + { 0x0164, "T" }, + { 0x0165, "t" }, + { 0x0166, "T" }, + { 0x0167, "t" }, + { 0x0168, "U" }, + { 0x0169, "u" }, + { 0x016a, "U" }, + { 0x016b, "u" }, + { 0x016c, "U" }, + { 0x016d, "u" }, + { 0x016e, "U" }, + { 0x016f, "u" }, + { 0x0170, "U" }, + { 0x0171, "u" }, + { 0x0172, "U" }, + { 0x0173, "u" }, + { 0x0174, "W" }, + { 0x0175, "w" }, + { 0x0176, "Y" }, + { 0x0177, "y" }, + { 0x0178, "Y" }, + { 0x0179, "Z" }, + { 0x017a, "z" }, + { 0x017b, "Z" }, + { 0x017c, "z" }, + { 0x017d, "Z" }, + { 0x017e, "z" }, + { 0x017f, "s" }, + { 0x01a0, "O" }, + { 0x01a1, "o" }, + { 0x01af, "U" }, + { 0x01b0, "u" }, + { 0x01cd, "A" }, + { 0x01ce, "a" }, + { 0x01cf, "I" }, + { 0x01d0, "i" }, + { 0x01d1, "O" }, + { 0x01d2, "o" }, + { 0x01d3, "U" }, + { 0x01d4, "u" }, + { 0x01d5, "U" }, + { 0x01d6, "u" }, + { 0x01d7, "U" }, + { 0x01d8, "u" }, + { 0x01d9, "U" }, + { 0x01da, "u" }, + { 0x01db, "U" }, + { 0x01dc, "u" }, + { 0x01de, "A" }, + { 0x01df, "a" }, + { 0x01e0, "A" }, + { 0x01e1, "a" }, + { 0x01e6, "G" }, + { 0x01e7, "g" }, + { 0x01e8, "K" }, + { 0x01e9, "k" }, + { 0x01ea, "O" }, + { 0x01eb, "o" }, + { 0x01ec, "O" }, + { 0x01ed, "o" }, + { 0x01f0, "j" }, + { 0x01f4, "G" }, + { 0x01f5, "g" }, + { 0x01f8, "N" }, + { 0x01f9, "n" }, + { 0x01fa, "A" }, + { 0x01fb, "a" }, + { 0x0200, "A" }, + { 0x0201, "a" }, + { 0x0202, "A" }, + { 0x0203, "a" }, + { 0x0204, "E" }, + { 0x0205, "e" }, + { 0x0206, "E" }, + { 0x0207, "e" }, + { 0x0208, "I" }, + { 0x0209, "i" }, + { 0x020a, "I" }, + { 0x020b, "i" }, + { 0x020c, "O" }, + { 0x020d, "o" }, + { 0x020e, "O" }, + { 0x020f, "o" }, + { 0x0210, "R" }, + { 0x0211, "r" }, + { 0x0212, "R" }, + { 0x0213, "r" }, + { 0x0214, "U" }, + { 0x0215, "u" }, + { 0x0216, "U" }, + { 0x0217, "u" }, + { 0x0218, "S" }, + { 0x0219, "s" }, + { 0x021a, "T" }, + { 0x021b, "t" }, + { 0x021e, "H" }, + { 0x021f, "h" }, + { 0x0226, "A" }, + { 0x0227, "a" }, + { 0x0228, "E" }, + { 0x0229, "e" }, + { 0x022a, "O" }, + { 0x022b, "o" }, + { 0x022c, "O" }, + { 0x022d, "o" }, + { 0x022e, "O" }, + { 0x022f, "o" }, + { 0x0230, "O" }, + { 0x0231, "o" }, + { 0x0232, "Y" }, + { 0x0233, "y" }, +}; + +const size_t sp_decomp_count = sizeof(sp_decomp_table) / sizeof(sp_decomp_table[0]); diff --git a/src/decomp.h b/src/decomp.h new file mode 100644 index 0000000..71024d9 --- /dev/null +++ b/src/decomp.h @@ -0,0 +1,15 @@ +/* splinter -- UTF-8 letter table for the screen, generated by tools/gen_decomp.py. */ +#ifndef SPLINTER_DECOMP_H +#define SPLINTER_DECOMP_H + +#include + +struct sp_decomp { + unsigned short cp; /* Unicode code point */ + const char *ascii; /* ASCII replacement, never empty */ +}; + +extern const struct sp_decomp sp_decomp_table[]; +extern const size_t sp_decomp_count; + +#endif /* SPLINTER_DECOMP_H */ diff --git a/src/ui.c b/src/ui.c index abff169..911b639 100644 --- a/src/ui.c +++ b/src/ui.c @@ -23,6 +23,8 @@ */ #include #include +#include +#include "decomp.h" #include #include #include @@ -65,6 +67,8 @@ static WINDOW *ui_side; static WINDOW *ui_input; static WINDOW *ui_status; +static size_t align_utf8(const char *s, size_t len, size_t pos); + enum { PAIR_MAIN = 1, /* fg on bg */ PAIR_ACCENT, /* accent on bg */ @@ -206,74 +210,205 @@ init_colors(const struct sp_theme *t) * Text goes to curses as ASCII. * * Measured, not assumed: the vendored NetBSD curses emits Latin-1 bytes for - * wide characters (ü -> 0xFC) instead of re-encoding to the locale's charset, - * so a UTF-8 answer would reach the terminal as mojibake. ncurses passes the - * bytes through; this port does not. Until that is fixed in the library, the - * UI transliterates: the session log and the conversation keep the exact UTF-8, - * only the screen shows "gruen" instead of "grün". + * wide characters (ü -> 0xFC) instead of re-encoding to the locale charset, so + * UTF-8 would reach the terminal as mojibake. ncurses passes the bytes through; + * this port does not. Until that is fixed in the library, the UI transliterates + * and the data keeps its exact UTF-8. + * + * iconv(3) does the work, with //TRANSLIT: a hand-written table turned German + * typography into question marks (— “ ” … all became '?'), which is how a + * readable answer turns into noise. Characters that cannot be transliterated + * become '?' - one per character, never silently dropped. */ +static const struct sp_decomp * +decomp_find(unsigned int cp) +{ + size_t lo = 0, hi = sp_decomp_count; + + while (lo < hi) { + size_t mid = (lo + hi) / 2; + + if (sp_decomp_table[mid].cp == cp) + return &sp_decomp_table[mid]; + if (sp_decomp_table[mid].cp < cp) + lo = mid + 1; + else + hi = mid; + } + return NULL; +} + +/* Decode one UTF-8 sequence; returns bytes consumed, or 0 on a broken byte. */ +static size_t +utf8_next(const unsigned char *s, size_t left, unsigned int *cp) +{ + if (left == 0) + return 0; + if (s[0] < 0x80) { + *cp = s[0]; + return 1; + } + if ((s[0] & 0xe0) == 0xc0 && left >= 2) { + *cp = ((unsigned int)(s[0] & 0x1f) << 6) | (s[1] & 0x3f); + return 2; + } + if ((s[0] & 0xf0) == 0xe0 && left >= 3) { + *cp = ((unsigned int)(s[0] & 0x0f) << 12) | + ((unsigned int)(s[1] & 0x3f) << 6) | (s[2] & 0x3f); + return 3; + } + if ((s[0] & 0xf8) == 0xf0 && left >= 4) { + *cp = ((unsigned int)(s[0] & 0x07) << 18) | + ((unsigned int)(s[1] & 0x3f) << 12) | + ((unsigned int)(s[2] & 0x3f) << 6) | (s[3] & 0x3f); + return 4; + } + return 0; +} + +/* + * UTF-8 -> ASCII for the screen. + * + * Two mechanisms, and the split is the point: letters come from a table + * generated out of Unicode's decompositions (glibc's iconv transliterates "ß" + * but turns "ü" into "?"), punctuation goes through iconv (— becomes --, … + * becomes ...). A character neither can express becomes one '?' — visible, not + * silently dropped. + * + * The conversation and the session log keep the exact UTF-8; only the screen + * is ASCII, because this curses port writes Latin-1 bytes for wide characters. + */ +int +sp_ui_ascii(const char *in, char *out, size_t cap) +{ + iconv_t cd; + const unsigned char *s; + size_t left, used = 0; + int rc = 0; + + if (in == NULL || out == NULL || cap == 0) + return -1; + cd = iconv_open("ASCII//TRANSLIT", "UTF-8"); + s = (const unsigned char *)in; + left = strlen(in); + + while (left > 0) { + unsigned int cp = 0; + size_t n = utf8_next(s, left, &cp); + const char *rep = NULL; + char tmp[16]; + + if (n == 0) { /* broken byte: one '?' and move on */ + if (used + 2 > cap) { + rc = -1; + break; + } + out[used++] = '?'; + s++; + left--; + continue; + } + if (cp < 0x80) { + tmp[0] = (char)cp; + tmp[1] = '\0'; + rep = tmp; + } else { + const struct sp_decomp *d = decomp_find(cp); + + if (d != NULL) { + rep = d->ascii; + } else if (cd != (iconv_t)-1) { + char *ip = (char *)s, *op = tmp; + size_t il = n, ol = sizeof(tmp) - 1; + + if (iconv(cd, &ip, &il, &op, &ol) != (size_t)-1) { + *op = '\0'; + rep = tmp; + } + } + if (rep == NULL) + rep = "?"; + } + { + size_t rl = strlen(rep); + + if (used + rl + 1 > cap) { + rc = -1; + break; + } + memcpy(out + used, rep, rl); + used += rl; + } + s += n; + left -= n; + } + out[used] = '\0'; + if (cd != (iconv_t)-1) + iconv_close(cd); + return rc; +} + static void ui_add_text(WINDOW *w, const char *s, int maxcols) { char out[1024]; - size_t o = 0, i = 0; if (s == NULL) return; - while (s[i] != '\0' && o < sizeof(out) - 8) { - unsigned char c = (unsigned char)s[i]; - - if (c < 0x80) { - out[o++] = (char)c; - i++; - continue; - } - /* UTF-8 sequences of two bytes cover Latin-1 and Latin Extended-A */ - if ((c & 0xe0) == 0xc0 && ((unsigned char)s[i + 1] & 0xc0) == 0x80) { - unsigned int cp = ((unsigned int)(c & 0x1f) << 6) | - (unsigned int)((unsigned char)s[i + 1] & 0x3f); - - switch (cp) { - case 0xe4: case 0xc4: /* ae AE */ - out[o++] = 'a'; out[o++] = 'e'; break; - case 0xf6: case 0xd6: /* oe OE */ - out[o++] = 'o'; out[o++] = 'e'; break; - case 0xfc: case 0xdc: /* ue UE */ - out[o++] = 'u'; out[o++] = 'e'; break; - case 0xdf: /* ss */ - out[o++] = 's'; out[o++] = 's'; break; - case 0xe0: case 0xe1: case 0xe2: case 0xe3: case 0xe5: - case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc5: - out[o++] = 'a'; break; /* a a a a a */ - case 0xe8: case 0xe9: case 0xea: case 0xeb: - case 0xc8: case 0xc9: case 0xca: case 0xcb: - out[o++] = 'e'; break; /* e e e e */ - case 0xec: case 0xed: case 0xee: case 0xef: - case 0xcc: case 0xcd: case 0xce: case 0xcf: - out[o++] = 'i'; break; /* i i i i */ - case 0xf2: case 0xf3: case 0xf4: case 0xf5: - case 0xd2: case 0xd3: case 0xd4: case 0xd5: - out[o++] = 'o'; break; /* o o o o */ - case 0xf9: case 0xfa: case 0xfb: - case 0xd9: case 0xda: case 0xdb: - out[o++] = 'u'; break; /* u u u */ - case 0xe7: case 0xc7: /* c C cedilla */ - out[o++] = 'c'; break; - case 0xf1: case 0xd1: /* n N tilde */ - out[o++] = 'n'; break; - default: - out[o++] = '?'; break; - } - i += 2; - continue; - } - out[o++] = '?'; - i++; - } - out[o] = '\0'; + if (sp_ui_ascii(s, out, sizeof(out)) != 0) + snprintf(out, sizeof(out), "%.900s", s); waddnstr(w, out, maxcols); } + + +/* + * Print the tail of `text` wrapped into a fixed region, bottom-anchored, with + * an optional scroll-back in lines. Used for the reasoning and for the answer + * while it streams: both are the same problem and should look the same. + */ +static void +draw_wrapped_tail(int y0, int h, int cols, const char *text, int pair, + size_t back) +{ + size_t len, lines, first; + int y; + + if (text == NULL || text[0] == '\0' || h < 1 || cols < 4) + return; + len = strlen(text); + lines = (len + (size_t)cols - 1) / (size_t)cols; + if ((size_t)h < lines) + first = lines - (size_t)h; + else + first = 0; + if (back > 0) { + if (back > first) + back = first; + first -= back; + } + if (pair) + wattron(ui_hist, COLOR_PAIR(pair)); + for (y = 0; y < h && first < lines; y++, first++) { + size_t off = align_utf8(text, len, first * (size_t)cols); + char slice[512]; + size_t take = len - off; + + if (take > (size_t)cols) + take = (size_t)cols; + take = align_utf8(text, len, off + take) - off; + if (take >= sizeof(slice)) + take = sizeof(slice) - 1; + memcpy(slice, text + off, take); + slice[take] = '\0'; + wmove(ui_hist, y0 + y, 0); + ui_add_text(ui_hist, slice, (int)take); + } + if (pair) + wattroff(ui_hist, COLOR_PAIR(pair)); +} + + static int entry_lines(int cols, const struct entry *e) { @@ -284,6 +419,21 @@ entry_lines(int cols, const struct entry *e) return (int)((len + (size_t)cols - 1) / (size_t)cols); } +/* + * Byte offsets computed from column arithmetic can land inside a UTF-8 + * sequence, and half a sequence is not a character: it draws as a replacement + * glyph. Every slice therefore starts and ends on a sequence boundary. + */ +static size_t +align_utf8(const char *s, size_t len, size_t pos) +{ + if (s == NULL || pos >= len) + return pos; + while (pos < len && ((unsigned char)s[pos] & 0xc0) == 0x80) + pos++; + return pos; +} + /* * A real viewport, not a best effort: total height is computed first, the * window shows the bottom of it, and an entry taller than the screen is shown @@ -326,7 +476,8 @@ draw_history(struct ui_state *s) break; snprintf(render, len, "%s: %s", e->role, e->text); - off = skip * cols; + off = (long)align_utf8(render, strlen(render), + (size_t)(skip * cols)); if (off > (long)strlen(render)) off = (long)strlen(render); skip = 0; @@ -356,50 +507,44 @@ draw_history(struct ui_state *s) * completed, and mixing the two would make the log look like it changed. */ /* - * The streaming turn is painted at the bottom of the history pane: the tail of - * what has arrived, wrapped over up to five rows, with the model's reasoning on - * one dim row above it. It is not an entry yet - entries are what was - * completed, and mixing the two would make the log look like it changed. + * While a turn runs, the left pane shows what the harness is doing, in the same + * 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. */ static void draw_live(struct ui_state *s, const char *text, const char *tool, const char *think) { - int rows, cols, use; - size_t len, take, start; + int rows, cols, answer_rows, think_rows; + size_t len; if ((text == NULL || text[0] == '\0') && - (think == NULL || think[0] == '\0')) + (think == NULL || think[0] == '\0') && + (tool == NULL || tool[0] == '\0')) return; getmaxyx(ui_hist, rows, cols); if (rows < 4 || cols < 8) return; len = text != NULL ? strlen(text) : 0; - use = rows - 1 < 5 ? rows - 1 : 5; - take = (size_t)use * (size_t)cols; - start = len > take ? len - take : 0; + answer_rows = rows / 3; + if (answer_rows < 3) + answer_rows = 3; + if (answer_rows > 8) + answer_rows = 8; + if (len == 0) + answer_rows = tool != NULL && tool[0] != '\0' ? 1 : 0; + think_rows = rows - answer_rows - 1; - if (think != NULL && think[0] != '\0') { - size_t tlen = strlen(think); - size_t tstart = tlen > (size_t)(cols - 1) ? - tlen - (size_t)(cols - 1) : 0; - - wattron(ui_hist, COLOR_PAIR(PAIR_DIM)); - wmove(ui_hist, rows - use - 1 >= 0 ? rows - use - 1 : 0, 0); - ui_add_text(ui_hist, think + tstart, cols - 1); - wattroff(ui_hist, COLOR_PAIR(PAIR_DIM)); - } - if (len > 0) { - wattron(ui_hist, COLOR_PAIR(PAIR_ACCENT)); - wmove(ui_hist, rows - use, 0); - ui_add_text(ui_hist, text + start, (int)take); - wattroff(ui_hist, COLOR_PAIR(PAIR_ACCENT)); - } + draw_wrapped_tail(0, think_rows > 0 ? think_rows : 0, cols, think, + PAIR_DIM, s->think_back); + if (len > 0) + draw_wrapped_tail(rows - answer_rows, answer_rows, cols, text, + PAIR_ACCENT, 0); if (tool != NULL && tool[0] != '\0') { char head[96]; - snprintf(head, sizeof(head), "tool %s", tool); + snprintf(head, sizeof(head), " tool %s", tool); wattron(ui_hist, COLOR_PAIR(PAIR_MAIN) | A_BOLD); mvwaddnstr(ui_hist, rows - 1, 0, head, cols - 1); wattroff(ui_hist, A_BOLD); @@ -408,51 +553,6 @@ draw_live(struct ui_state *s, const char *text, const char *tool, wnoutrefresh(ui_hist); } -/* - * The reasoning gets its own area: wrapped to the sidebar width, bottom- - * anchored like a log, and scrollable with PgUp/PgDn while it streams. It is - * not squeezed into one line, because a model's planning is exactly the thing - * you want to read when a turn takes a minute. - */ -static void -draw_think_field(int y0, int rows, int cols, const char *text, size_t back) -{ - size_t len, lines, first, i; - int y; - - if (text == NULL || text[0] == '\0' || rows < 2 || cols < 4) - return; - len = strlen(text); - lines = (len + (size_t)cols - 1) / (size_t)cols; - if (lines == 0) - return; - first = lines > (size_t)rows ? lines - (size_t)rows : 0; - if (back > 0) { - if (back > first) - back = first; - first -= back; - } - wattron(ui_side, COLOR_PAIR(PAIR_DIM)); - for (y = 0, i = first; y < rows && i < lines; y++, i++) { - size_t off = i * (size_t)cols; - size_t take = len - off; - - if (take > (size_t)cols) - take = (size_t)cols; - wmove(ui_side, y0 + y, 0); - { - char slice[256]; - - if (take >= sizeof(slice)) - take = sizeof(slice) - 1; - memcpy(slice, text + off, take); - slice[take] = '\0'; - ui_add_text(ui_side, slice, (int)take); - } - } - wattroff(ui_side, COLOR_PAIR(PAIR_DIM)); -} - static void sidebar_heading(int *y, int cols, const char *text) { @@ -522,15 +622,6 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir, } wattrset(ui_side, A_NORMAL); - if (s->last_think.p != NULL && s->last_think.len > 0 && - y + 3 < getmaxy(ui_side)) { - int avail; - - y++; - sidebar_heading(&y, cols, "thinking (PgUp/PgDn)"); - avail = getmaxy(ui_side) - y; - draw_think_field(y, avail, cols, s->last_think.p, s->think_back); - } wnoutrefresh(ui_side); } @@ -996,6 +1087,19 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, tu.steers, tu.steers == 1 ? "" : "s"); sp_buf_puts(&answer, note); } + /* the reasoning stays in the transcript, as a dim + * entry, capped so a long plan cannot push the chat + * out of the window */ + if (tu.think.len > 0) { + const char *th = tu.think.p; + size_t tl = tu.think.len; + + if (tl > 1500) { + th += tl - 1500; + tl = 1500; + } + add_entry(&s, "thinking", th); + } sp_buf_free(&tu.answer); sp_buf_free(&tu.err); sp_buf_free(&tu.live); diff --git a/src/ui.h b/src/ui.h index 9f2dacd..709ab94 100644 --- a/src/ui.h +++ b/src/ui.h @@ -13,4 +13,12 @@ int sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, struct sp_host *host); +/* + * UTF-8 -> ASCII for the screen. The vendored curses writes Latin-1 bytes for + * wide characters, so text is transliterated before it is drawn; the + * conversation and the session log keep the exact UTF-8. + * Returns 0, or -1 when the result does not fit. + */ +int sp_ui_ascii(const char *in, char *out, size_t cap); + #endif /* SPLINTER_UI_H */ diff --git a/tests/test_ascii b/tests/test_ascii new file mode 100755 index 0000000..696f2bc Binary files /dev/null and b/tests/test_ascii differ diff --git a/tests/test_ascii.c b/tests/test_ascii.c new file mode 100644 index 0000000..6205c05 --- /dev/null +++ b/tests/test_ascii.c @@ -0,0 +1,76 @@ +/* + * splinter -- UTF-8 to ASCII for the screen. + * + * The vendored curses mangles anything above 0x7f, so text is transliterated + * before drawing. The regression this test exists for: a hand-written table + * turned every character it did not know into '?', so a German answer full of + * typographic punctuation ("Gedankenstrich — Anfuehrungszeichen") arrived as a + * row of question marks. + */ +#include +#include + +#include "ui.h" + +static int failures; + +#define CHECK(cond) do { \ + if (!(cond)) { \ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + failures++; \ + } \ +} while (0) + +static int +no_questions(const char *s) +{ + return strchr(s, '?') == NULL; +} + +int +main(void) +{ + char out[256]; + + /* ASCII survives untouched */ + CHECK(sp_ui_ascii("hallo welt", out, sizeof(out)) == 0); + CHECK(strcmp(out, "hallo welt") == 0); + + /* umlauts become something readable, not blocks */ + CHECK(sp_ui_ascii("grün weiß schön", out, sizeof(out)) == 0); + CHECK(no_questions(out)); + CHECK(strstr(out, "gr") != NULL && strstr(out, "sch") != NULL); + + /* the typographic punctuation that used to produce ????? */ + CHECK(sp_ui_ascii("ein — Gedankenstrich, „Zitat“, Auslassung…", out, + sizeof(out)) == 0); + CHECK(no_questions(out)); + CHECK(strstr(out, "Gedankenstrich") != NULL); + CHECK(strstr(out, "Zitat") != NULL); + + /* an ellipsis is not dropped silently */ + CHECK(sp_ui_ascii("warten…", out, sizeof(out)) == 0); + CHECK(strlen(out) >= 6); + + /* something genuinely unrepresentable becomes one '?', not a crash */ + CHECK(sp_ui_ascii("a\U0001F600b", out, sizeof(out)) == 0); + CHECK(strlen(out) >= 2); + CHECK(strchr(out, 'a') != NULL && strchr(out, 'b') != NULL); + + /* empty and NULL are handled */ + CHECK(sp_ui_ascii("", out, sizeof(out)) == 0); + CHECK(strcmp(out, "") == 0); + CHECK(sp_ui_ascii(NULL, out, sizeof(out)) == -1); + + /* a too-small buffer reports failure instead of writing past its end */ + { + char small[8]; + + (void)sp_ui_ascii("abcdefghijklmnop", small, sizeof(small)); + CHECK(strlen(small) < sizeof(small)); + } + + if (failures == 0) + printf("test_ascii: ok\n"); + return failures == 0 ? 0 : 1; +} diff --git a/tools/gen_decomp.py b/tools/gen_decomp.py new file mode 100644 index 0000000..298c781 --- /dev/null +++ b/tools/gen_decomp.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +"""Generate the UTF-8 -> ASCII table for the screen. + +Why this exists: glibc's iconv ASCII//TRANSLIT handles punctuation (— becomes +--, … becomes ...) but not every accented letter - "grün" came back as "gr?n" +while "weiß" became "weiss". A harness whose answers turn into question marks is +unreadable, so the letters are mapped here, from Unicode's own decomposition, +and iconv is used only for what is left. + +Coverage: U+00A0..U+024F (Latin-1 Supplement and Latin Extended-A), which is +every European letter this UI will meet. + +Usage: gen_decomp.py > decomp_table.c +""" +import sys +import unicodedata + +# Letters whose ASCII form is not "base letter without accent". +SPECIAL = { + # German convention: an umlaut written out is ae/oe/ue, not a/e/u. + 0x00C4: "Ae", 0x00E4: "ae", + 0x00D6: "Oe", 0x00F6: "oe", + 0x00DC: "Ue", 0x00FC: "ue", + 0x00C6: "AE", 0x00E6: "ae", # AE ligature + 0x00D0: "D", 0x00F0: "d", # eth + 0x00D8: "O", 0x00F8: "o", # o slash + 0x00DE: "Th", 0x00FE: "th", # thorn + 0x00DF: "ss", # sharp s + 0x0110: "D", 0x0111: "d", # d with stroke + 0x0126: "H", 0x0127: "h", # h with stroke + 0x0131: "i", # dotless i + 0x0138: "k", + 0x0141: "L", 0x0142: "l", # l with stroke + 0x0152: "OE", 0x0153: "oe", # OE ligature + 0x0166: "T", 0x0167: "t", # t with stroke + 0x017F: "s", # long s +} + + +def ascii_for(cp: int) -> str: + if cp in SPECIAL: + return SPECIAL[cp] + ch = chr(cp) + decomposed = unicodedata.normalize("NFD", ch) + base = "".join(c for c in decomposed if not unicodedata.combining(c)) + if len(base) == 1 and base.isascii() and base.isprintable(): + return base + return "" + + +def main() -> int: + rows = [] + for cp in range(0x00A0, 0x0250): + a = ascii_for(cp) + if not a: + continue + rows.append((cp, a)) + + w = sys.stdout.write + w("/*\n" + " * Generated by tools/gen_decomp.py from Unicode decompositions.\n" + " * Do not edit by hand. The UI transliterates with this table because the\n" + " * vendored curses writes Latin-1 bytes for wide characters.\n" + " */\n" + "#include \n" + "#include \"decomp.h\"\n\n" + "const struct sp_decomp sp_decomp_table[] = {\n") + for cp, a in rows: + esc = a.replace("\\", "\\\\").replace('"', '\\"') + w("\t{ 0x%04x, \"%s\" },\n" % (cp, esc)) + w("};\n\n" + "const size_t sp_decomp_count = " + "sizeof(sp_decomp_table) / sizeof(sp_decomp_table[0]);\n") + return 0 + + +if __name__ == "__main__": + sys.exit(main())