diff --git a/src/delegate.c b/src/delegate.c index ecf7d01..291a3bb 100644 --- a/src/delegate.c +++ b/src/delegate.c @@ -43,6 +43,132 @@ struct del_ctx { char self[512]; }; + +/* + * What is actually available. The model cannot delegate well if it has to guess + * which ids and tags exist, and guessing produces 'no delegate matches this task' + * at the moment the work should start. + * + * Without probe=1 it lists the configuration. With probe=1 it also asks every + * endpoint what models it serves (/v1/models), so 'the config says so' and 'the + * server answers' are two different columns. + */ +/* the string value of one argument, without the whole JSON parser */ +static const char * +json_str(const char *args, const char *key, struct sp_buf *keep) +{ + char pat[64]; + const char *p, *start; + size_t n; + + snprintf(pat, sizeof(pat), "\"%s\"", key); + p = args != NULL ? strstr(args, pat) : NULL; + if (p == NULL) + return NULL; + p = strchr(p + strlen(pat), ':'); + if (p == NULL || (start = strchr(p, '"')) == NULL) + return NULL; + start++; + n = strcspn(start, "\""); + sp_buf_add(keep, start, n); + return keep->p; +} + +static void +models_url(const char *endpoint, char *out, size_t len) +{ + const char *cut = strstr(endpoint, "/chat/completions"); + + if (cut != NULL) + snprintf(out, len, "%.*s/models", (int)(cut - endpoint), endpoint); + else + snprintf(out, len, "%s/models", endpoint); +} + +static int +tool_delegate_list(const char *args, struct sp_buf *out, void *ctx) +{ + struct del_ctx *c = ctx; + struct sp_buf keep; + const char *probe_s; + int probe = 0; + size_t i; + + (void)c; + if (sp_buf_init(&keep) != 0) + return -1; + probe_s = json_str(args, "probe", &keep); + probe = probe_s != NULL && (strcmp(probe_s, "true") == 0 || + strcmp(probe_s, "1") == 0); + sp_buf_free(&keep); + + if (c->cfg->ndelegates == 0) { + sp_buf_puts(out, "keine Delegates konfiguriert\n"); + return 0; + } + for (i = 0; i < c->cfg->ndelegates; i++) { + const struct sp_delegate *d = &c->cfg->delegate[i]; + + sp_buf_printf(out, "%s\n tags: %s\n kind: %s prio: %d\n", + d->id, d->tags[0] != '\0' ? d->tags : "(keine)", + d->kind[0] != '\0' ? d->kind : "openai", d->prio); + sp_buf_printf(out, " model: %s\n", d->model[0] != '\0' ? + d->model : "(cli)"); + sp_buf_printf(out, " endpoint: %s%s%s\n", d->endpoint[0] != '\0' ? + d->endpoint : "(cli)", + d->host[0] != '\0' ? " auf " : "", + d->host[0] != '\0' ? d->host : ""); + sp_buf_printf(out, " wofuer: %s\n", d->about); + if (probe && d->endpoint[0] != '\0') { + char url[600], cmd[700]; + FILE *fp; + char line[512]; + int seen = 0; + + models_url(d->endpoint, url, sizeof(url)); + snprintf(cmd, sizeof(cmd), "curl -sS --max-time 8 '%s'", + url); + fp = popen(cmd, "r"); + sp_buf_puts(out, " models: "); + if (fp == NULL) { + sp_buf_puts(out, "nicht erreichbar\n"); + } else { + while (fgets(line, sizeof(line), fp) != NULL) { + const char *m = strstr(line, "\"id\""); + + if (m == NULL) + continue; + m = strchr(m, ':'); + if (m == NULL) + continue; + m = strchr(m, '"'); + if (m == NULL) + continue; + { + const char *e = strchr(m + 1, '"'); + size_t n = e != NULL ? + (size_t)(e - m - 1) : 0; + + if (n > 0 && n < 80) { + if (seen++) + sp_buf_puts(out, + ", "); + sp_buf_printf(out, + "%.*s", (int)n, m + 1); + } + } + } + (void)pclose(fp); + if (!seen) + sp_buf_puts(out, "(keine Modellliste)"); + sp_buf_puts(out, "\n"); + } + } + sp_buf_puts(out, "\n"); + } + return 0; +} + static void lower(char *dst, size_t cap, const char *src) { @@ -806,6 +932,19 @@ sp_delegate_register(struct sp_registry *r, const struct sp_config *cfg, if (sp_registry_add(r, &t) != 0) return -1; + + memset(&t, 0, sizeof(t)); + t.name = "delegate_list"; + t.description = "List the configured delegates: id, tags, model, endpoint. " + "probe=1 also asks each endpoint which models it really serves."; + t.params = "{\"type\":\"object\",\"properties\":{\"probe\":" + "{\"type\":\"string\",\"description\":\"'1' to query the endpoints\"}}}"; + t.auth = SP_AUTH_READ; + t.run = tool_delegate_list; + t.ctx = &ctx; + if (sp_registry_add(r, &t) != 0) + return -1; + memset(&t, 0, sizeof(t)); t.name = "delegate_parallel"; t.description = "Hand several tasks to different models at the same time " diff --git a/src/ui.c b/src/ui.c index ff63355..7de8b34 100644 --- a/src/ui.c +++ b/src/ui.c @@ -666,13 +666,30 @@ md_draw(int y, int cols, int maxrows, const char *text, const unsigned char *att if (row >= maxrows) break; } - one[0] = text[i]; - one[1] = '\0'; + /* + * A UTF-8 character is one unit. Handing sp_ui_ascii a single byte of + * a two-byte sequence produced '??hler' where 'Zaehler' belonged - + * the transliteration needs the whole character. + */ if ((unsigned char)text[i] < 0x80) { out[0] = text[i]; out[1] = '\0'; - } else if (sp_ui_ascii(one, out, sizeof(out)) != 0) - out[0] = '?', out[1] = '\0'; + } else { + size_t len = 1, k; + + if (((unsigned char)text[i] & 0xe0) == 0xc0) + len = 2; + else if (((unsigned char)text[i] & 0xf0) == 0xe0) + len = 3; + else if (((unsigned char)text[i] & 0xf8) == 0xf0) + len = 4; + for (k = 0; k < len && text[i + k] != '\0'; k++) + one[k] = text[i + k]; + one[k] = '\0'; + if (sp_ui_ascii(one, out, sizeof(out)) != 0) + out[0] = '?', out[1] = '\0'; + i += k - 1; + } attrset(COLOR_PAIR(PAIR_MAIN)); if (a & MD_BOLD) @@ -1128,6 +1145,9 @@ layout(void) ui_status = newwin(1, cols, rows - 2, 0); ui_input = newwin(1, cols, rows - 1, 0); keypad(ui_input, TRUE); + /* a one-line window that may scroll is a screen that jumps */ + scrollok(ui_input, FALSE); + scrollok(ui_status, FALSE); } int @@ -1647,11 +1667,27 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *model, 1000 + (now.tv_nsec - t0.tv_nsec) / 1000000); werase(ui_input); - mvwprintw(ui_input, 0, 0, - sp_t("ui.running_steer"), - ms / 1000); - ui_add_text(ui_input, line, - getmaxx(ui_input) - 20); + /* + * One row, never more. Text that wraps in a + * one-line window scrolls the whole screen - + * which is exactly how the chat ended up + * looking like it had zones that jumped. + */ + { + char st[192]; + int w = getmaxx(ui_input) - 1; + int used; + + snprintf(st, sizeof(st), + sp_t("ui.running_steer"), + ms / 1000); + mvwaddnstr(ui_input, 0, 0, st, w); + used = (int)strlen(st); + if (used < w) + ui_add_text(ui_input, + line, w - used - 1); + wclrtoeol(ui_input); + } wrefresh(ui_input); continue; } diff --git a/tests/test_ascii b/tests/test_ascii index 8bdbb90..8be4fd5 100755 Binary files a/tests/test_ascii and b/tests/test_ascii differ