Der Judge-Ziel darf ein Pool sein; die Mitglieder behalten ihr eigenes "as" und werden in Pool-Reihenfolge versucht. Mehrere `judge every N` Klauseln einer Regel bilden eine Kette: die Zahlen sind kumulativ, müssen aufsteigend sein, und nach dem letzten Schritt beginnt der Zähler wieder bei 0. Beispiel: every 5 ds41flash, every 10 claude-openai0 -> 5. Call DeepSeek, 10. Call Claude, 15./20. wieder DeepSeek/Claude. /v1/models nimmt die engere Grenze über Worker und alle Judge-Schritte. Absteigende Ketten und Judge-Ziele ohne Modell werden beim Laden abgelehnt; die Meldung nennt den Schritt. Tests: 364 OK, 0 FAIL. tests/run.sh definiert wieder fail(); der MCP-Lastabschnitt konnte einen Fehlschlag sonst verschlucken.
609 lines
23 KiB
C
609 lines
23 KiB
C
/*
|
|
* junction — was ein Endpunkt über seine Modelle veröffentlicht.
|
|
*
|
|
* Der Endpunkt-Block in der Konfiguration trägt genau eine Kontextzahl. Das ist
|
|
* eine Aussage über den Endpunkt, nicht über die Modelle: ein Anbieter mit drei
|
|
* Modellen hat drei Grenzen, und die Wahrheit steht in seiner Modellliste:
|
|
*
|
|
* GET <base>/models
|
|
* {"data":[{"id":"deepseek-v4.1-flash","limit":{"context":1000000,"output":384000}}]}
|
|
*
|
|
* Diese Datei holt die Liste beim Start einmal je Endpunkt und legt sie dort ab.
|
|
* Die Kontextprüfung nimmt danach die Zahl des *Modells*; die Zahl aus dem
|
|
* Endpunkt-Block bleibt der Rückfall, wenn ein Anbieter keine Liste liefert.
|
|
* Erfunden wird nichts: fehlt beides, gilt die Grenze als unbekannt und ein Ziel
|
|
* wird nicht wegen Kontext abgelehnt.
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <ctype.h>
|
|
#include <fnmatch.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "config.h"
|
|
#include "models.h"
|
|
|
|
extern j_config CFG; /* der Router hält genau eine Konfiguration */
|
|
|
|
/* Der Abruf läuft im Hintergrund, Anfragen lesen gleichzeitig. */
|
|
static pthread_mutex_t g_models_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
#define J_MODELS_CMD 8192
|
|
|
|
static const char *find_in(const char *from, const char *end, const char *needle)
|
|
{
|
|
size_t n = strlen(needle);
|
|
if (from >= end) return NULL;
|
|
for (const char *p = from; p + n <= end; p++)
|
|
if (!memcmp(p, needle, n)) return p;
|
|
return NULL;
|
|
}
|
|
|
|
/* obj zeigt auf '{': liefert die passende '}', String-Inhalte werden übersprungen. */
|
|
static const char *match_brace(const char *obj, const char *end)
|
|
{
|
|
int depth = 0, instr = 0;
|
|
for (const char *p = obj; p < end; p++) {
|
|
if (instr) {
|
|
if (*p == '\\' && p + 1 < end) { p++; continue; }
|
|
if (*p == '"') instr = 0;
|
|
continue;
|
|
}
|
|
if (*p == '"') { instr = 1; continue; }
|
|
if (*p == '{') depth++;
|
|
else if (*p == '}') { if (--depth == 0) return p; }
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* p zeigt auf das öffnende Anführungszeichen. */
|
|
static int string_at(const char *p, const char *end, char *out, size_t outlen)
|
|
{
|
|
size_t o = 0;
|
|
if (!p || p >= end || *p != '"') return 0;
|
|
for (p++; p < end && *p && *p != '"'; p++) {
|
|
if (*p == '\\' && p + 1 < end) p++;
|
|
if (o + 1 < outlen) out[o++] = *p;
|
|
}
|
|
out[o] = '\0';
|
|
return o > 0;
|
|
}
|
|
|
|
/* p zeigt auf den Doppelpunkt nach einem Schlüssel. */
|
|
static long long_after(const char *p, const char *end)
|
|
{
|
|
if (!p || p >= end) return 0;
|
|
p++;
|
|
while (p < end && (isspace((unsigned char)*p) || *p == ':')) p++;
|
|
if (p >= end) return 0;
|
|
char *e = NULL;
|
|
long v = strtol(p, &e, 10);
|
|
if (e == p || v < 0) return 0;
|
|
return v;
|
|
}
|
|
|
|
int j_models_parse(j_endpoint *ep, const char *json, size_t len)
|
|
{
|
|
const char *end = json + len;
|
|
const char *data = find_in(json, end, "\"data\"");
|
|
if (!data) return 0;
|
|
const char *p = find_in(data, end, "[");
|
|
if (!p) return 0;
|
|
|
|
j_model_meta *arr = NULL;
|
|
int n = 0, cap = 0;
|
|
|
|
const char *q = p;
|
|
while (q < end) {
|
|
const char *obj = find_in(q, end, "{");
|
|
if (!obj) break;
|
|
const char *oend = match_brace(obj, end);
|
|
if (!oend) break;
|
|
|
|
char id[J_GLOB_MAX] = "";
|
|
const char *idk = find_in(obj, oend, "\"id\"");
|
|
if (idk) {
|
|
const char *colon = find_in(idk, oend, ":");
|
|
const char *v = colon ? find_in(colon, oend, "\"") : NULL;
|
|
if (!string_at(v, oend, id, sizeof id)) id[0] = '\0';
|
|
}
|
|
|
|
if (id[0]) {
|
|
long ctx = 0, out = 0;
|
|
const char *lim = find_in(obj, oend, "\"limit\"");
|
|
if (lim) {
|
|
const char *ck = find_in(lim, oend, "\"context\"");
|
|
if (ck) ctx = long_after(find_in(ck, oend, ":"), oend);
|
|
const char *ok = find_in(lim, oend, "\"output\"");
|
|
if (ok) out = long_after(find_in(ok, oend, ":"), oend);
|
|
}
|
|
/* Anthropic's model list names the same limits without the
|
|
* OpenAI "limit" wrapper: max_input_tokens and max_tokens. */
|
|
if (ctx == 0) {
|
|
const char *ck = find_in(obj, oend, "\"max_input_tokens\"");
|
|
if (ck) ctx = long_after(find_in(ck, oend, ":"), oend);
|
|
}
|
|
if (out == 0) {
|
|
const char *ok = find_in(obj, oend, "\"max_tokens\"");
|
|
if (ok) out = long_after(find_in(ok, oend, ":"), oend);
|
|
}
|
|
if (n == cap) {
|
|
int ncap = cap ? cap * 2 : 16;
|
|
j_model_meta *grown = realloc(arr, (size_t)ncap * sizeof *arr);
|
|
if (!grown) { free(arr); return 0; }
|
|
arr = grown;
|
|
cap = ncap;
|
|
}
|
|
snprintf(arr[n].id, sizeof arr[n].id, "%s", id);
|
|
arr[n].ctx = ctx;
|
|
arr[n].out = out;
|
|
n++;
|
|
}
|
|
q = oend + 1;
|
|
}
|
|
|
|
pthread_mutex_lock(&g_models_lock);
|
|
free(ep->models);
|
|
ep->models = arr;
|
|
ep->model_count = n;
|
|
pthread_mutex_unlock(&g_models_lock);
|
|
return n;
|
|
}
|
|
|
|
long j_models_limit(const j_endpoint *ep, const char *model, int which)
|
|
{
|
|
if (!model || !*model) return 0;
|
|
long found = 0;
|
|
pthread_mutex_lock(&g_models_lock);
|
|
if (ep->models && ep->model_count > 0) {
|
|
for (int i = 0; i < ep->model_count; i++)
|
|
if (!strcmp(ep->models[i].id, model)) {
|
|
found = which ? ep->models[i].out : ep->models[i].ctx;
|
|
goto done;
|
|
}
|
|
/* Manche Anbieter führen denselben Namen kanonisch
|
|
* ("deepseek/deepseek-v4.1-flash"); ein "as" nennt oft nur den kurzen Teil. */
|
|
for (int i = 0; i < ep->model_count; i++) {
|
|
const char *slash = strrchr(ep->models[i].id, '/');
|
|
if (slash && !strcmp(slash + 1, model)) {
|
|
found = which ? ep->models[i].out : ep->models[i].ctx;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
done:
|
|
pthread_mutex_unlock(&g_models_lock);
|
|
return found;
|
|
}
|
|
|
|
/* Ein Muster gegen die volle ID und, falls vorhanden, gegen die ID ohne
|
|
* "#variante" prüfen. So bleibt eine Freigabe für das Modell auch dann gültig,
|
|
* wenn der Client einen opencode-Variantenselektor anhängt. */
|
|
static int model_match(const char *pattern, const char *model, const char *base)
|
|
{
|
|
return fnmatch(pattern, model, 0) == 0 ||
|
|
(base && base[0] && fnmatch(pattern, base, 0) == 0);
|
|
}
|
|
|
|
int j_model_allowed(const char *model)
|
|
{
|
|
if (!model || !*model) return 1;
|
|
|
|
char base[J_GLOB_MAX];
|
|
snprintf(base, sizeof base, "%s", model);
|
|
char *hash = strchr(base, '#');
|
|
if (hash) *hash = '\0';
|
|
|
|
for (int i = 0; i < CFG.model_deny_count; i++)
|
|
if (model_match(CFG.model_deny[i], model, base)) return 0;
|
|
|
|
if (CFG.model_allow_count == 0) return 1;
|
|
for (int i = 0; i < CFG.model_allow_count; i++)
|
|
if (model_match(CFG.model_allow[i], model, base)) return 1;
|
|
return 0;
|
|
}
|
|
|
|
/* Manche Endpunkte veröffentlichen ihre Grenzen nicht in der OpenAI-Liste, sondern
|
|
* in ihrer eigenen API. ollama.com tut das: /api/show liefert
|
|
* model_info["<arch>.context_length"] — kimi-k2.6 etwa 262144, nicht 128000.
|
|
* Das ist gelesen, nicht geschätzt. Fehlt der Endpunkt, kommt 0 zurück. */
|
|
static long publish_context_length(const char *root, const char *model,
|
|
const j_endpoint *ep)
|
|
{
|
|
char cmd[J_MODELS_CMD];
|
|
int off = snprintf(cmd, sizeof cmd, "curl -sS -m 6 -X POST ");
|
|
const char *key = ep->key_env_count > 0 ? getenv(ep->api_key_env[0]) : NULL;
|
|
if (key && *key)
|
|
off += snprintf(cmd + off, sizeof cmd - (size_t)off,
|
|
"-H 'Authorization: Bearer %s' ", key);
|
|
off += snprintf(cmd + off, sizeof cmd - (size_t)off,
|
|
"-H 'Content-Type: application/json' -d '{\"model\":\"%s\"}' "
|
|
"'%s/api/show' 2>/dev/null", model, root);
|
|
if (off <= 0 || (size_t)off >= sizeof cmd) return 0;
|
|
|
|
FILE *f = popen(cmd, "r");
|
|
if (!f) return 0;
|
|
char buf[8192];
|
|
size_t len = fread(buf, 1, sizeof buf - 1, f);
|
|
pclose(f);
|
|
if (len == 0) return 0;
|
|
buf[len] = '\0';
|
|
|
|
/* "…context_length":262144 — das Feld heißt je nach Architektur anders
|
|
* ("kimi-k2.context_length", "llama.context_length"), der Schwanz ist gleich. */
|
|
const char *end = buf + len;
|
|
for (const char *p = buf; (p = find_in(p, end, "context_length")) != NULL; p += 14) {
|
|
const char *colon = find_in(p, end, ":");
|
|
if (!colon) continue;
|
|
long v = long_after(colon, end);
|
|
if (v > 0) return v;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Holt <base>/models. Header und Schlüssel sind die des Endpunkts — ohne die
|
|
* antwortet ein Cloudflare-geschützter Anbieter mit 403 statt mit einer Liste. */
|
|
static int fetch_json(const j_endpoint *ep, char **out, size_t *out_len)
|
|
{
|
|
char base[J_URL_MAX];
|
|
snprintf(base, sizeof base, "%s", ep->base);
|
|
size_t bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/') base[--bl] = '\0';
|
|
if (bl == 0) return -1;
|
|
|
|
char cmd[J_MODELS_CMD];
|
|
int off = snprintf(cmd, sizeof cmd, "curl -sS -m 8 ");
|
|
const char *key = ep->key_env_count > 0 ? getenv(ep->api_key_env[0]) : NULL;
|
|
if (key && *key) {
|
|
off += snprintf(cmd + off, sizeof cmd - (size_t)off,
|
|
"-H 'Authorization: Bearer %s' ", key);
|
|
}
|
|
for (int i = 0; i < ep->header_count && off > 0 && (size_t)off < sizeof cmd; i++) {
|
|
/* ein Header kann ein Anführungszeichen enthalten; das ist in der
|
|
* Konfiguration nicht vorgesehen und wird hier nicht nachgebaut */
|
|
if (strchr(ep->headers[i], '\'')) continue;
|
|
off += snprintf(cmd + off, sizeof cmd - (size_t)off, "-H '%s' ", ep->headers[i]);
|
|
}
|
|
if (off > 0 && (size_t)off < sizeof cmd)
|
|
snprintf(cmd + off, sizeof cmd - (size_t)off, "'%s/models' 2>/dev/null", base);
|
|
|
|
FILE *f = popen(cmd, "r");
|
|
if (!f) return -1;
|
|
|
|
size_t cap = 65536, len = 0;
|
|
char *buf = malloc(cap);
|
|
if (!buf) { pclose(f); return -1; }
|
|
for (;;) {
|
|
if (len + 4096 + 1 > cap) {
|
|
size_t ncap = cap * 2;
|
|
char *grown = realloc(buf, ncap);
|
|
if (!grown) { free(buf); pclose(f); return -1; }
|
|
buf = grown; cap = ncap;
|
|
}
|
|
size_t got = fread(buf + len, 1, 4096, f);
|
|
len += got;
|
|
if (got < 4096) break;
|
|
}
|
|
int status = pclose(f);
|
|
if (status != 0 || len == 0) { free(buf); return -1; }
|
|
buf[len] = '\0';
|
|
*out = buf;
|
|
*out_len = len;
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Kleine Hilfsfunktion: Endpunkt per Name. */
|
|
static j_endpoint *find_ep(const char *name)
|
|
{
|
|
for (int i = 0; i < CFG.endpoint_count; i++)
|
|
if (!strcmp(CFG.endpoints[i].name, name)) return &CFG.endpoints[i];
|
|
return NULL;
|
|
}
|
|
|
|
static const j_pool *find_pool_cfg(const char *name)
|
|
{
|
|
for (int i = 0; i < CFG.pool_count; i++)
|
|
if (!strcmp(CFG.pools[i].name, name)) return &CFG.pools[i];
|
|
return NULL;
|
|
}
|
|
|
|
/* Ist das ein "einfacher" Modellname (kein Globjoker), der als virtuelle/alias
|
|
* Kette in Regeln auftaucht? Wir nutzen Regeln mit exakt passendem model_glob. */
|
|
long j_models_effective_limit(const char *model, int which)
|
|
{
|
|
if (!model || !*model) return 0;
|
|
long best = 0;
|
|
int found_rule = 0;
|
|
|
|
for (int ri = 0; ri < CFG.rule_count; ri++) {
|
|
const j_rule *r = &CFG.rules[ri];
|
|
if (r->block || r->target_count <= 0) continue;
|
|
/* Nur explizite Modellregeln ohne Joker als virtuelle Alias-Namen betrachten,
|
|
* und nur wenn es eine Kette/Pool (mehrere Ziele) oder ein "as" ist. */
|
|
if (strcmp(r->model_glob, model) != 0) continue;
|
|
/* Pfadspezifische Regeln (z.B. /v1/models) sind keine Modellalias-Ketten. */
|
|
if (r->path_glob[0] && strcmp(r->path_glob, "*") != 0) continue;
|
|
int is_chain = (r->target_count > 1) || r->judge_count > 0;
|
|
for (int t = 0; !is_chain && t < r->target_count; t++)
|
|
if (r->target_as[t][0]) is_chain = 1;
|
|
if (!is_chain) continue;
|
|
|
|
found_rule = 1;
|
|
for (int t = 0; t < r->target_count; t++) {
|
|
j_endpoint *ep = find_ep(r->targets[t]);
|
|
if (!ep) continue;
|
|
const char *em = r->target_as[t][0] ? r->target_as[t] : model;
|
|
long lim = j_models_limit(ep, em, which);
|
|
if (lim <= 0) lim = ep->context; /* Rückfall wie im Routing-Kontext */
|
|
if (which == 1 && lim <= 0) lim = 0; /* output: kein guter Rückfall außer Modellwert */
|
|
if (lim > 0) {
|
|
if (best <= 0 || lim < best) best = lim;
|
|
}
|
|
}
|
|
for (int j = 0; j < r->judge_count; j++) {
|
|
const char *target = r->judges[j].target;
|
|
const char *judge_as = r->judges[j].as;
|
|
if (!target[0]) continue;
|
|
const j_pool *jp = find_pool_cfg(target);
|
|
if (jp) {
|
|
for (int c = 0; c < jp->count; c++) {
|
|
j_endpoint *ep = find_ep(jp->targets[c]);
|
|
if (!ep) continue;
|
|
const char *em = jp->target_as[c][0] ? jp->target_as[c]
|
|
: (judge_as[0] ? judge_as : model);
|
|
long lim = j_models_limit(ep, em, which);
|
|
if (lim <= 0) lim = ep->context;
|
|
if (which == 1 && lim <= 0) lim = 0;
|
|
if (lim > 0 && (best <= 0 || lim < best)) best = lim;
|
|
}
|
|
} else {
|
|
j_endpoint *ep = find_ep(target);
|
|
if (ep) {
|
|
const char *em = judge_as[0] ? judge_as : model;
|
|
long lim = j_models_limit(ep, em, which);
|
|
if (lim <= 0) lim = ep->context;
|
|
if (which == 1 && lim <= 0) lim = 0;
|
|
if (lim > 0 && (best <= 0 || lim < best)) best = lim;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return found_rule ? best : 0;
|
|
}
|
|
|
|
/* Prüft grob, ob eine Modell-ID bereits im JSON vorkommt: "id":"<model>" */
|
|
static int json_has_model_id(const char *json, size_t len, const char *model)
|
|
{
|
|
char needle[256];
|
|
size_t nlen = (size_t)snprintf(needle, sizeof needle, "\"id\":\"%s\"", model);
|
|
if (nlen >= sizeof needle || len < nlen) return 0;
|
|
for (size_t i = 0; i + nlen <= len; i++) {
|
|
if (json[i] == '"' && memcmp(json + i, needle, nlen) == 0) return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Findet das data-Array einer OpenAI-Modellliste. arr zeigt auf '[', endarr auf ']'. */
|
|
static int find_data_array(const char *json, size_t len, const char **arr, const char **endarr)
|
|
{
|
|
const char *end = json + len;
|
|
const char *data = find_in(json, end, "\"data\"");
|
|
if (!data) return -1;
|
|
const char *a = find_in(data, end, "[");
|
|
if (!a) return -1;
|
|
|
|
int depth = 0;
|
|
for (const char *p = a; p < end; p++) {
|
|
if (*p == '"') {
|
|
p++;
|
|
while (p < end) {
|
|
if (*p == '\\' && p + 1 < end) { p += 2; continue; }
|
|
if (*p == '"') break;
|
|
p++;
|
|
}
|
|
} else if (*p == '[') {
|
|
depth++;
|
|
} else if (*p == ']') {
|
|
depth--;
|
|
if (depth == 0) {
|
|
*arr = a;
|
|
*endarr = p;
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int j_models_filter_list(const char *json, size_t len, char **out, size_t *out_len)
|
|
{
|
|
*out = NULL; *out_len = 0;
|
|
if (!json || len == 0) return -1;
|
|
if (CFG.model_allow_count == 0 && CFG.model_deny_count == 0) return -1;
|
|
|
|
const char *arr = NULL, *endarr = NULL;
|
|
if (find_data_array(json, len, &arr, &endarr) != 0) return -1;
|
|
|
|
char *buf = malloc(len + 1);
|
|
if (!buf) return -1;
|
|
|
|
/* Prefix bis einschließlich '['. */
|
|
size_t o = (size_t)(arr - json) + 1;
|
|
memcpy(buf, json, o);
|
|
|
|
int kept = 0, dropped = 0;
|
|
const char *p = arr + 1;
|
|
while (p < endarr) {
|
|
while (p < endarr && (isspace((unsigned char)*p) || *p == ',')) p++;
|
|
if (p >= endarr) break;
|
|
if (*p != '{') { free(buf); return -1; } /* unbekannte Form: nicht anfassen */
|
|
|
|
const char *obj = p;
|
|
const char *oend = match_brace(obj, endarr);
|
|
if (!oend) { free(buf); return -1; }
|
|
|
|
char id[J_GLOB_MAX] = "";
|
|
const char *idk = find_in(obj, oend + 1, "\"id\"");
|
|
if (idk) {
|
|
const char *colon = find_in(idk, oend + 1, ":");
|
|
const char *v = colon ? find_in(colon, oend + 1, "\"") : NULL;
|
|
if (!string_at(v, oend + 1, id, sizeof id)) id[0] = '\0';
|
|
}
|
|
|
|
if (id[0] == '\0' || j_model_allowed(id)) {
|
|
if (kept) buf[o++] = ',';
|
|
size_t elen = (size_t)(oend - obj + 1);
|
|
memcpy(buf + o, obj, elen);
|
|
o += elen;
|
|
kept++;
|
|
} else {
|
|
dropped++;
|
|
}
|
|
p = oend + 1;
|
|
}
|
|
|
|
if (dropped == 0) { free(buf); return -1; }
|
|
|
|
/* Suffix ab ']' (inklusive). */
|
|
size_t tail = (size_t)(endarr - json);
|
|
memcpy(buf + o, endarr, len - tail);
|
|
o += len - tail;
|
|
buf[o] = '\0';
|
|
*out = buf;
|
|
*out_len = o;
|
|
return 0;
|
|
}
|
|
|
|
int j_models_patch_list(const char *json, size_t len, char **out, size_t *out_len)
|
|
{
|
|
*out = NULL; *out_len = 0;
|
|
if (!json || len == 0) return -1;
|
|
const char *arr = NULL, *endarr = NULL;
|
|
if (find_data_array(json, len, &arr, &endarr) != 0) return -1;
|
|
|
|
/* Sammle virtuelle Ketten-Modelle */
|
|
char extra[4096]; size_t eo = 0; extra[0] = '\0';
|
|
for (int ri = 0; ri < CFG.rule_count; ri++) {
|
|
const j_rule *r = &CFG.rules[ri];
|
|
if (r->block || r->target_count <= 0) continue;
|
|
if (!r->model_glob[0] || strchr(r->model_glob, '*') || strchr(r->model_glob, '?')) continue;
|
|
if (r->path_glob[0] && strcmp(r->path_glob, "*") != 0) continue;
|
|
int is_chain = (r->target_count > 1) || r->judge_count > 0;
|
|
for (int t = 0; !is_chain && t < r->target_count; t++) if (r->target_as[t][0]) is_chain = 1;
|
|
if (!is_chain) continue;
|
|
const char *name = r->model_glob;
|
|
if (!j_model_allowed(name)) continue;
|
|
if (json_has_model_id(json, len, name)) continue;
|
|
long ctx = j_models_effective_limit(name, 0);
|
|
long outlim = j_models_effective_limit(name, 1);
|
|
/* nur ergänzen, wenn wir wenigstens eine Grenze kennen */
|
|
if (ctx <= 0 && outlim <= 0) continue;
|
|
int need_comma = 0;
|
|
/* comma if array not empty */
|
|
for (const char *p = arr + 1; p < endarr; p++) { if (!isspace((unsigned char)*p)) { need_comma = 1; break; } }
|
|
/* if we already added entries, need comma; but easier handle: prepend comma if not first or array non-empty */
|
|
int first_extra = (eo == 0);
|
|
int prefix_comma = (!first_extra) || need_comma;
|
|
int n = snprintf(extra + eo, sizeof extra - eo,
|
|
"%s{\"id\":\"%s\",\"object\":\"model\",\"owned_by\":\"junction\"",
|
|
prefix_comma ? "," : "", name);
|
|
if (n < 0 || (size_t)n >= sizeof extra - eo) break;
|
|
eo += (size_t)n;
|
|
if (ctx > 0 || outlim > 0) {
|
|
n = snprintf(extra + eo, sizeof extra - eo, ",\"limit\":{");
|
|
if (n > 0) eo += (size_t)n;
|
|
int wrote = 0;
|
|
if (ctx > 0) { n = snprintf(extra + eo, sizeof extra - eo, "\"context\":%ld", ctx); if (n>0){eo+=(size_t)n; wrote=1;} }
|
|
if (outlim > 0) { n = snprintf(extra + eo, sizeof extra - eo, "%s\"output\":%ld", wrote?",":"", outlim); if (n>0) eo+=(size_t)n; }
|
|
n = snprintf(extra + eo, sizeof extra - eo, "}"); if (n>0) eo+=(size_t)n;
|
|
}
|
|
n = snprintf(extra + eo, sizeof extra - eo, "}"); if (n>0) eo+=(size_t)n;
|
|
if (eo >= sizeof extra - 64) break;
|
|
}
|
|
if (eo == 0) return -1;
|
|
|
|
size_t newlen = len + eo;
|
|
char *nb = malloc(newlen + 1);
|
|
if (!nb) return -1;
|
|
size_t pre = (size_t)(endarr - json);
|
|
memcpy(nb, json, pre);
|
|
memcpy(nb + pre, extra, eo);
|
|
memcpy(nb + pre + eo, endarr, len - pre);
|
|
nb[newlen] = '\0';
|
|
*out = nb; *out_len = newlen;
|
|
return 0;
|
|
}
|
|
|
|
|
|
void j_models_refresh_all(void)
|
|
{
|
|
for (int i = 0; i < CFG.endpoint_count; i++) {
|
|
j_endpoint *ep = &CFG.endpoints[i];
|
|
if (strncmp(ep->base, "http", 4) != 0) continue;
|
|
/* kind opencode hat keine OpenAI-Modellliste -- die baut junctions Brücke
|
|
* selbst aus der Sitzungs-API. Und wer seine Basis per Kommando holt, wird
|
|
* nicht vorher gefragt: da steht noch keine Adresse. */
|
|
if (!strcmp(ep->kind, "opencode") || ep->base_cmd[0]) continue;
|
|
|
|
char *json = NULL;
|
|
size_t len = 0;
|
|
if (fetch_json(ep, &json, &len) != 0) {
|
|
if (ep->context > 0)
|
|
fprintf(stderr, "[%s] models: %s unreachable — Kontextprüfung fällt auf die "
|
|
"Endpunktzahl %ld zurück\n", CFG.log_prefix, ep->name, ep->context);
|
|
else
|
|
fprintf(stderr, "[%s] models: %s unreachable — keine Grenze bekannt, es wird "
|
|
"nicht wegen Kontext verworfen\n", CFG.log_prefix, ep->name);
|
|
continue;
|
|
}
|
|
int n = j_models_parse(ep, json, len);
|
|
free(json);
|
|
if (n <= 0) {
|
|
if (ep->context > 0)
|
|
fprintf(stderr, "[%s] models: %s liefert keine Modellliste — Kontextprüfung "
|
|
"fällt auf die Endpunktzahl %ld zurück\n",
|
|
CFG.log_prefix, ep->name, ep->context);
|
|
else
|
|
fprintf(stderr, "[%s] models: %s liefert keine Modellliste — keine Grenze "
|
|
"bekannt, es wird nicht wegen Kontext verworfen\n",
|
|
CFG.log_prefix, ep->name);
|
|
continue;
|
|
}
|
|
/* Veröffentlicht die Liste keine Grenzen, fragt junction die eigene API des
|
|
* Endpunkts (ollama.com: /api/show). Erst ein Modell als Probe: antwortet es
|
|
* nicht, sind die übrigen Anfragen nur Wartezeit. */
|
|
int filled = 0;
|
|
if (ep->models[0].ctx == 0) {
|
|
char root[J_URL_MAX];
|
|
snprintf(root, sizeof root, "%s", ep->base);
|
|
size_t rl = strlen(root);
|
|
while (rl > 0 && root[rl - 1] == '/') root[--rl] = '\0';
|
|
if (rl >= 3 && !strcmp(root + rl - 3, "/v1")) root[rl - 3] = '\0';
|
|
|
|
long probe = publish_context_length(root, ep->models[0].id, ep);
|
|
if (probe > 0) {
|
|
ep->models[0].ctx = probe;
|
|
filled = 1;
|
|
for (int k = 1; k < ep->model_count; k++) {
|
|
if (ep->models[k].ctx > 0) continue;
|
|
long v = publish_context_length(root, ep->models[k].id, ep);
|
|
if (v > 0) { ep->models[k].ctx = v; filled++; }
|
|
}
|
|
}
|
|
}
|
|
|
|
fprintf(stderr, "[%s] models: %s %d Modelle%s", CFG.log_prefix, ep->name, n,
|
|
filled ? " (Grenzen aus /api/show)" : "");
|
|
for (int k = 0; k < ep->model_count && k < 3; k++)
|
|
fprintf(stderr, "%s%s ctx=%ld out=%ld", k ? "," : " ·",
|
|
ep->models[k].id, ep->models[k].ctx, ep->models[k].out);
|
|
fprintf(stderr, "\n");
|
|
}
|
|
}
|