A fixture-only organization reads a ready catalogue with no model proxy client: the catalogue asks the proxy only when a module calls it. A thinking-only change, and a limits patch that keeps harness, model and entry, no longer asks the catalogue. A change that truly needs it still refuses, and the WebUI names the next step: keep the model and change thinking, or press Disable model proxy catalogue. The Routing switch follows whether cliproxy is listed and removes or adds only cliproxy.
72 lines
5.5 KiB
JavaScript
72 lines
5.5 KiB
JavaScript
// In-memory only; a new loader is made for every refresh so disabled or refused
|
|
// modules can never inherit a loader's last-good authority.
|
|
import { createLoader } from '../../plugins/src/loader.mjs';
|
|
const validId = s => typeof s === 'string' && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(s);
|
|
const validModel = s => typeof s === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/.test(s);
|
|
const disabled = () => ({ status: 'disabled', observedAt: null, models: [], notices: [] });
|
|
export function createModelCatalogue({ client, settings, pluginDataRoot = null, now = Date.now, makeLoader = createLoader, proxyLock = null }) {
|
|
const cache = new Map(), flights = new Map(); let generation = 0, writing = 0;
|
|
function invalidate() { generation++; cache.clear(); }
|
|
function begin() { writing++; invalidate(); }
|
|
function end() { writing = Math.max(0, writing - 1); invalidate(); }
|
|
function off(id) {
|
|
const names = settings(id)?.['model-provider'];
|
|
if (names === undefined || (Array.isArray(names) && names.length === 0)) return true;
|
|
if (!Array.isArray(names) || names.length > 32 || names.some(n => !validId(n)) || new Set(names).size !== names.length) throw Error();
|
|
return false;
|
|
}
|
|
async function refresh(id, previous) {
|
|
try {
|
|
if (off(id)) return disabled();
|
|
const names = [...settings(id)['model-provider']];
|
|
const loader = makeLoader({ log: () => {}, dataRoot:pluginDataRoot });
|
|
const result = await loader.load({ 'model-provider': names });
|
|
if (result.refused.length || loader.snapshot('model-provider').length !== names.length) throw Error();
|
|
// A module that needs no model proxy (fixture) answers without one; a module
|
|
// that asks the proxy while none is configured gets a refusal and reads unavailable.
|
|
const c = client();
|
|
const proxy = method => async (...args) => { if (!c) throw Error('model proxy not configured'); return c[method](...args); };
|
|
const answers = await Promise.all(loader.snapshot('model-provider').map(async ({ name, module }) => {
|
|
const answer = await module.catalogue({ listAccounts: proxy('getCredentials'), listModels: proxy('getCredentialModels'), listGroups: proxy('getUpstreamGroups'), now: () => new Date(now()).toISOString() });
|
|
if (answer?.status === 'unavailable' && answer.reason === 'model-key-collision') throw Object.assign(Error(), { code: 'model-key-collision' });
|
|
if (answer?.status !== 'ready' || !Array.isArray(answer.models) || answer.models.length > 10000 || !Array.isArray(answer.notices) || answer.notices.length > 100) throw Error();
|
|
for (const m of answer.models) {
|
|
if (!validId(m.key) || m.module !== name || !validId(m.upstream) || !validId(m.provider) || !validModel(m.model) || typeof m.label !== 'string' || m.label.length > 300 || /[\x00-\x1f\x7f]/.test(m.label) || typeof m.available !== 'boolean' || ![null, 'unverified'].includes(m.reason) || !Array.isArray(m.harnesses) || m.harnesses.length > 32 || m.harnesses.some(h => !validId(h))) throw Error();
|
|
}
|
|
if (answer.notices.some(n => typeof n !== 'string' || n.length > 300 || /[\x00-\x1f\x7f]/.test(n))) throw Error();
|
|
return answer;
|
|
}));
|
|
const models = answers.flatMap(a => a.models), keys = new Set();
|
|
for (const m of models) { if (keys.has(m.key)) throw Object.assign(Error(), { code: 'model-key-collision' }); keys.add(m.key); }
|
|
return { status: 'ready', observedAt: new Date(now()).toISOString(), models, notices: [...new Set(answers.flatMap(a => a.notices))] };
|
|
} catch (error) {
|
|
return { status: 'unavailable', observedAt: previous?.observedAt ?? null, models: (previous?.models ?? []).map(m => ({ ...m, available: false, reason: 'proxy-down', harnesses: [] })), notices: [], ...(error.code === 'model-key-collision' ? { reason: error.code } : {}) };
|
|
}
|
|
}
|
|
async function read(id, { fresh = false } = {}) {
|
|
const unavailable = () => ({ status: 'unavailable', observedAt: null, models: [], notices: [] });
|
|
let sharedGeneration;
|
|
try { proxyLock?.assertCertain(); sharedGeneration = proxyLock?.generation(); } catch { return unavailable(); }
|
|
if (writing) return unavailable();
|
|
try { if (off(id)) { cache.delete(id); return disabled(); } } catch { return { status: 'unavailable', observedAt: null, models: [], notices: [] }; }
|
|
const existing = cache.get(id);
|
|
const fingerprint = JSON.stringify([settings(id)['model-provider'], sharedGeneration]);
|
|
if (!fresh && existing && existing.fingerprint === fingerprint && now() - existing.time < 30000) return structuredClone(existing.value);
|
|
const flightKey = `${id}:${fingerprint}:${generation}`;
|
|
let flight = flights.get(flightKey);
|
|
if (!flight || fresh) {
|
|
const at = generation;
|
|
flight = refresh(id, existing?.value).then(value => {
|
|
if (generation !== at || JSON.stringify([settings(id)?.['model-provider'], proxyLock?.generation()]) !== fingerprint) return null;
|
|
if (cache.size >= 128 && !cache.has(id)) cache.delete(cache.keys().next().value);
|
|
cache.set(id, { time: now(), fingerprint, value }); return value;
|
|
}).catch(() => null);
|
|
if (!fresh) { flights.set(flightKey, flight); flight.finally(() => { if (flights.get(flightKey) === flight) flights.delete(flightKey); }); }
|
|
}
|
|
const value = await flight;
|
|
try { proxyLock?.assertCertain(); if (proxyLock?.generation() !== sharedGeneration) return unavailable(); } catch { return unavailable(); }
|
|
return value ? structuredClone(value) : unavailable();
|
|
}
|
|
return { read, invalidate, begin, end };
|
|
}
|