okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-fb3 into f-split-int (locale keys re-added after the card-list strings)7b06ea51b

main
4.7 KiB79 linesraw
1/**
2 * Does a model server read pictures? Asked of the server itself, cached for a few minutes:
3 *  - its model list (`/models`): `architecture.input_modalities`, `input_modalities`, `modalities`, or a `capabilities` list that names image / vision / multimodal;
4 *  - a llama-server's `/props` (`modalities.vision`), which is how `llama-server --mmproj` says it can read pictures (its `/v1/models` does not).
5 * Unknown or unreachable counts as "no" (a picture sent to a server without a vision encoder makes the whole request fail), but the person can
6 * always tick "This model reads pictures" in the model's settings: that answer wins over anything asked here.
7 */
8const TTL_MS = 5 * 60_000;
9
10/** True / false / null (the row does not say) from one entry of a server's model list. */
11export function modelReadsPictures(row) {
12  if (!row || typeof row !== 'object') return null;
13  const lists = [row.architecture?.input_modalities, row.input_modalities, row.modalities?.input, Array.isArray(row.modalities) ? row.modalities : null];
14  for (const list of lists) if (Array.isArray(list)) return list.some(x => /^(image|vision|images)$/i.test(String(x)));
15  if (row.modalities && typeof row.modalities === 'object' && typeof row.modalities.vision === 'boolean') return row.modalities.vision;
16  const caps = row.capabilities ?? row.meta?.capabilities;
17  if (Array.isArray(caps)) return caps.some(x => /vision|image|multimodal/i.test(String(x)));
18  if (caps && typeof caps === 'object' && typeof caps.vision === 'boolean') return caps.vision;
19  return null;
20}
21
22/**
23 * Does a model list row say the model can think (reasoning)? OpenRouter lists `supported_parameters` per model: `reasoning` or
24 * `include_reasoning` in it means the model takes a reasoning effort. True / false / null (the row does not say).
25 */
26export function modelSupportsReasoning(row) {
27  const list = row?.supported_parameters ?? row?.supportedParameters;
28  if (!Array.isArray(list)) return null;
29  return list.some(x => /^(include_)?reasoning$/i.test(String(x)));
30}
31
32/**
33 * Pictures are ON by default (owner rule): a model whose server does not say either way is sent pictures; only an explicit "no" (the person's
34 * own switch, or the server's own list or /props) turns them off.
35 */
36export function readsPicturesByDefault(model, asked) {
37  return effectiveVision(model, asked) ?? true;
38}
39
40/** The effective answer for a saved model: what the person ticked, else what the server said (undefined: never asked). */
41export function effectiveVision(model, asked) {
42  if (model?.vision === true) return true;
43  if (model?.vision === false) return false;
44  return asked;
45}
46
47export function createVisionProbe({ fetcher = fetch, now = Date.now } = {}) {
48  const cache = new Map();   // key -> { at, value }
49  const keyOf = (conn, modelId) => `${conn.id}|${modelId}`;
50  return {
51    /** Last known answer for a model (true / false), or undefined when never asked. */
52    known: (conn, modelId) => { const e = cache.get(keyOf(conn, modelId)); return e ? e.value : undefined; },
53    /** Whether the model takes a reasoning effort, as its server's list said (true / false), null when it did not say, undefined when never asked. */
54    knownThinks: (conn, modelId) => { const e = cache.get(keyOf(conn, modelId)); return e ? e.thinks : undefined; },
55    /** Ask now (at most once per TTL per model). Answers true / false / null (the server did not say). */
56    async check(conn, modelId) {
57      const k = keyOf(conn, modelId); const hit = cache.get(k);
58      if (hit && now() - hit.at < TTL_MS) return hit.value;
59      let value = null, thinks = null;
60      const base = String(conn.baseUrl).replace(/\/+$/, '');
61      const headers = conn.apiKey ? { authorization: `Bearer ${conn.apiKey}` } : {};
62      const get = async path => { const res = await fetcher(base + path, { headers, signal: AbortSignal.timeout(4000) }); return res.ok ? res.json() : null; };
63      try {
64        const list = (await get('/models'))?.data ?? [];
65        const row = list.find(m => m?.id === modelId) ?? (list.length === 1 ? list[0] : null);
66        value = modelReadsPictures(row);
67        thinks = modelSupportsReasoning(row);
68        if (value === null) {            // the list did not say: a llama-server tells in /props (next to /v1, not under it)
69          try {
70            const props = await (async () => { const res = await fetcher(base.replace(/\/v1$/, '') + '/props', { headers, signal: AbortSignal.timeout(4000) }); return res.ok ? res.json() : null; })();
71            value = props?.modalities?.vision === true ? true : props?.modalities?.vision === false ? false : null;
72          } catch { /* no /props: not a llama-server */ }
73        }
74      } catch { value = null; }
75      cache.set(k, { at: now(), value, thinks });
76      return value;
77    },
78  };
79}