diff --git a/src/backend/delegate.c b/src/backend/delegate.c index 65c6206..ed87f79 100644 --- a/src/backend/delegate.c +++ b/src/backend/delegate.c @@ -98,12 +98,6 @@ json_str(const char *args, const char *key, struct sp_buf *keep) return keep->p; } -static void -models_url(const char *endpoint, char *out, size_t len) -{ - sp_endpoint_url(endpoint, "models", out, len); -} - static int tool_delegate_list(const char *args, struct sp_buf *out, void *ctx) { @@ -148,49 +142,55 @@ tool_delegate_list(const char *args, struct sp_buf *out, void *ctx) 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; + struct sp_provider pv; + struct sp_buf ids = { 0 }, perr = { 0 }; + char kenv[sizeof(pv.key_env)]; - models_url(d->endpoint, url, sizeof(url)); - snprintf(cmd, sizeof(cmd), "curl -sS --max-time 8 '%s'", - url); - fp = popen(cmd, "r"); + /* + * The same route and the same reader as every other + * model list: /models under the endpoint's base, with + * the delegate's key and headers. The old popen(1) + * variant sent no key and saw only the first "id" of a + * one-line JSON answer. + */ + memset(&pv, 0, sizeof(pv)); + snprintf(pv.id, sizeof(pv.id), "%s", d->id); + snprintf(pv.kind, sizeof(pv.kind), "%s", d->kind); + snprintf(pv.endpoint, sizeof(pv.endpoint), "%s", + d->endpoint); + snprintf(pv.model, sizeof(pv.model), "%s", d->model); + snprintf(pv.key_env, sizeof(pv.key_env), "%s", + d->key_env); + /* a literal key lives in 'key' until the child starts */ + if (pv.key_env[0] == '\0' && d->key[0] != '\0') { + snprintf(kenv, sizeof(kenv), + "SPLINTER_PROBE_KEY_%ld", (long)getpid()); + (void)setenv(kenv, d->key, 1); + snprintf(pv.key_env, sizeof(pv.key_env), "%s", + kenv); + } sp_buf_puts(out, " models: "); - if (fp == NULL) { - sp_buf_puts(out, "nicht erreichbar\n"); + if (sp_buf_init(&ids) != 0 || + sp_buf_init(&perr) != 0) { + sp_buf_puts(out, "(keine Modellliste)\n"); + } else if (sp_provider_served(&pv, &ids, &perr) != 0) { + sp_buf_printf(out, "%s\n", perr.p != NULL ? + perr.p : "(keine Modellliste)"); } else { - while (fgets(line, sizeof(line), fp) != NULL) { - const char *m = strstr(line, "\"id\""); + char *m, *save = NULL; + int seen = 0; - 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); - } - } + for (m = strtok_r(ids.p, "\n", &save); + m != NULL; + m = strtok_r(NULL, "\n", &save)) { + if (seen++) + sp_buf_puts(out, ", "); + sp_buf_printf(out, "%s", m); } - (void)pclose(fp); - if (!seen) - sp_buf_puts(out, "(keine Modellliste)"); sp_buf_puts(out, "\n"); } + sp_buf_free(&ids); + sp_buf_free(&perr); } sp_buf_puts(out, "\n"); } diff --git a/src/backend/providers.c b/src/backend/providers.c index b4264e3..a4a0b9d 100644 --- a/src/backend/providers.c +++ b/src/backend/providers.c @@ -248,6 +248,11 @@ sp_provider_use(struct sp_agent *ag, const struct sp_config *cfg, size_t idx, * One GET, no body. curl writes to a pipe; the URL and the header go to curl * on stdin/argv respectively, so a token never shows up in ps. */ +/* A probe may not hang on a host that does not answer: 3s to connect, 8s + * for the whole answer. */ +#define PROVIDER_CONNECT_TIMEOUT_S 3 +#define PROVIDER_GET_TIMEOUT_S 8 + static int http_get(const char *url, const char *const *headers, struct sp_buf *out, struct sp_buf *err) @@ -256,10 +261,13 @@ http_get(const char *url, const char *const *headers, struct sp_buf *out, pid_t child; const char *args[8]; int n = 0; - char tbuf[16]; + char tbuf[16], cbuf[16]; - snprintf(tbuf, sizeof(tbuf), "%d", 8); + snprintf(tbuf, sizeof(tbuf), "%d", PROVIDER_GET_TIMEOUT_S); + snprintf(cbuf, sizeof(cbuf), "%d", PROVIDER_CONNECT_TIMEOUT_S); args[n++] = "-sS"; + args[n++] = "--connect-timeout"; + args[n++] = cbuf; args[n++] = "-m"; args[n++] = tbuf; args[n] = NULL; diff --git a/tests/test_providers.c b/tests/test_providers.c index 31388f5..684bb52 100644 --- a/tests/test_providers.c +++ b/tests/test_providers.c @@ -300,6 +300,16 @@ main(void) "https://api.anthropic.com/v1/models") == 0); CHECK(strcmp(sp_endpoint_url("https://h/v1/messages", NULL, u, sizeof(u)), "https://h/v1") == 0); + /* the model list of the three providers Konrad uses */ + CHECK(strcmp(sp_endpoint_url("https://opencode.ai/zen/go/v1", + "models", u, sizeof(u)), + "https://opencode.ai/zen/go/v1/models") == 0); + CHECK(strcmp(sp_endpoint_url("https://ollama.com/v1", + "models", u, sizeof(u)), + "https://ollama.com/v1/models") == 0); + CHECK(strcmp(sp_endpoint_url("https://api.anthropic.com/v1", + "models", u, sizeof(u)), + "https://api.anthropic.com/v1/models") == 0); } CHECK(sp_provider_endpoint_ok("https://x/v1") == 1); CHECK(sp_provider_endpoint_ok("x/v1") == 0); diff --git a/tests/test_tmux.c b/tests/test_tmux.c index 2d8e397..9550464 100644 --- a/tests/test_tmux.c +++ b/tests/test_tmux.c @@ -53,6 +53,15 @@ main(void) const char *s; CHECK(mkdtemp(dir) != NULL); + /* + * A tmux server of our own. $TMUX (set when this runs inside the + * user's tmux) wins over TMUX_TMPDIR, and without either the test + * would create its session on the user's server - a stray window, and + * keys meant for `cat` could land in a real pane. + */ + unsetenv("TMUX"); + unsetenv("TMUX_PANE"); + setenv("TMUX_TMPDIR", dir, 1); memset(&sess, 0, sizeof(sess)); snprintf(sess.dir, sizeof(sess.dir), "%s", dir); memset(®, 0, sizeof(reg)); @@ -68,8 +77,8 @@ main(void) } CHECK(sp_registry_find(®, "tmux_watch") == NULL); - /* nothing is targeted yet */ - s = call(®, "tmux_ask", "{\"text\":\"x\"}", &out); + /* nothing is targeted yet: the text must not reach any terminal */ + s = call(®, "tmux_ask", "{\"text\":\"probe\"}", &out); CHECK(strstr(s, "error: no tmux target") != NULL); /* a name that could be an option, a second target or a command */ @@ -115,6 +124,8 @@ done: sp_registry_free(®); sp_buf_free(&out); sp_buf_free(&err); + /* only the private server: TMUX_TMPDIR points into dir */ + (void)system("tmux kill-server 2>/dev/null"); snprintf(args, sizeof(args), "rm -rf '%s'", dir); (void)system(args); if (failures == 0)