128 lines
8.8 KiB
JavaScript
128 lines
8.8 KiB
JavaScript
// Provider configuration and transport declarations owned by the proxy module.
|
|
export const interfaceVersion = 1;
|
|
export const ZAI_URL = 'https://api.z.ai/api/anthropic';
|
|
export const MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/;
|
|
const MODEL = MODEL_ID;
|
|
const MAX_MODELS = 10000;
|
|
const family = url => url === ZAI_URL ? 'zai' : 'other';
|
|
// #1873 (Jason 2026-11-19): a Claude model from an OAuth account runs on claude-code only;
|
|
// a model from an API key, Claude or not, runs on claude-code and pi. The rule keys on the account
|
|
// kind: the proxy lists OAuth accounts (credentials, upstream `claude`) apart from API-key groups.
|
|
// It routes a request by model name across every account, so a group model whose name an OAuth
|
|
// Claude account offers (`oauth`) could be served by that account and is held to claude-code too.
|
|
const harnesses = (upstream, oauth = false) => upstream === 'claude' || oauth ? ['claude-code'] : ['claude-code', 'pi'];
|
|
const slug = (upstream, model) => `${upstream}-${model}`.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/-+$/g, '');
|
|
|
|
export async function catalogue({ listAccounts, listModels, listGroups, now }) {
|
|
try {
|
|
const [accounts, groups] = await Promise.all([listAccounts(), listGroups()]);
|
|
if (!Array.isArray(accounts?.files) || !Array.isArray(groups) || accounts.files.length > MAX_MODELS || groups.length > MAX_MODELS) throw Error();
|
|
const models = [], notices = [], keys = new Map();
|
|
const add = (upstream, model) => {
|
|
if (typeof model !== 'string' || !MODEL.test(model)) throw Error();
|
|
const key = slug(upstream, model);
|
|
if (key.length > 64 || !/^[a-z][a-z0-9-]*$/.test(key)) throw Error();
|
|
const identity = JSON.stringify([upstream, model]);
|
|
if (keys.has(key) && keys.get(key) !== identity) throw Object.assign(Error(), { code: 'model-key-collision' });
|
|
if (keys.has(key)) return;
|
|
keys.set(key, identity);
|
|
models.push({ key, module: 'cliproxy', upstream, provider: upstream === 'claude' ? 'anthropic' : upstream, model, label: `${upstream} / ${model}`, available: true,
|
|
reason: upstream === 'other' || upstream === 'codex' ? 'unverified' : null, harnesses: harnesses(upstream) });
|
|
if (models.length > MAX_MODELS) throw Error();
|
|
};
|
|
// Every OAuth Claude account's names, whatever its state: one disabled, cooling down or signed
|
|
// out now can serve a request later, before the catalogue is read again.
|
|
const oauth = new Set();
|
|
for (const row of accounts.files) {
|
|
if (!['claude', 'codex'].includes(row?.provider)) continue;
|
|
const listed = row.disabled !== true && row.unavailable !== true && ['ready', 'active'].includes(row.status);
|
|
if (!listed && row.provider !== 'claude') continue;
|
|
if (typeof row.name !== 'string' || !row.name || row.name.length > 512) throw Error();
|
|
const response = await listModels(row.name);
|
|
if (!Array.isArray(response?.models) || response.models.length > MAX_MODELS) throw Error();
|
|
for (const model of response.models) {
|
|
if (row.provider === 'claude' && typeof model?.id === 'string') oauth.add(model.id.toLowerCase());
|
|
if (listed) add(row.provider, model?.id);
|
|
}
|
|
}
|
|
for (const group of groups) {
|
|
if (!Array.isArray(group?.keys)) throw Error();
|
|
if (group.disabled === true || !group.keys.some(key => key && key.disabled !== true)) continue;
|
|
const upstream = family(group['base-url']);
|
|
if (group.models === undefined || (Array.isArray(group.models) && !group.models.length)) {
|
|
if (upstream === 'zai') notices.push('z.ai account has no models configured. Add them on Model proxy.');
|
|
continue;
|
|
}
|
|
if (!Array.isArray(group.models) || group.models.length > MAX_MODELS) throw Error();
|
|
for (const model of group.models) add(upstream, model?.alias || model?.name);
|
|
}
|
|
for (const m of models) m.harnesses = harnesses(m.upstream, oauth.has(m.model.toLowerCase()));
|
|
return { status: 'ready', observedAt: now(), models, notices: [...new Set(notices)] };
|
|
} catch (error) {
|
|
return { status: 'unavailable', observedAt: null, models: [], notices: [], ...(error.code === 'model-key-collision' ? { reason: error.code } : {}) };
|
|
}
|
|
}
|
|
export function preflight({ selection, harness, catalogue: c }) {
|
|
if (c?.status !== 'ready') return { allow: false, code: 'model-catalogue-unavailable' };
|
|
const model = c.models.find(m => m.upstream === selection?.upstream && m.model === selection?.model);
|
|
if (!model?.available) return { allow: false, code: 'model-provider-unavailable' };
|
|
if (!model.harnesses.includes(harness)) return { allow: false, code: 'model-harness-incompatible' };
|
|
return { allow: true };
|
|
}
|
|
|
|
// Declarative transport only: credentials and endpoints are supplied by the host.
|
|
const ROUTES = Object.freeze({
|
|
claude: { routeId: 'anthropic', endpoint: 'root', api: 'anthropic-messages', piProvider: 'anthropic', compat: null },
|
|
zai: { routeId: 'glm', endpoint: 'v1', api: 'openai-completions', piProvider: 'mosaic-proxy-glm', compat: { thinkingFormat: 'zai', zaiToolStream: true } },
|
|
codex: { routeId: 'codex', endpoint: 'v1', api: 'openai-responses', piProvider: 'mosaic-proxy-codex', compat: null },
|
|
// #1873: its own pi provider, never pi's built-in "anthropic", whose Claude models an entry there
|
|
// would keep and send to the proxy.
|
|
other: { routeId: 'anthropic', endpoint: 'root', api: 'anthropic-messages', piProvider: 'mosaic-proxy-other', compat: null },
|
|
});
|
|
export function route({ selection, harness }) {
|
|
let upstream = selection?.upstream;
|
|
if (!upstream && selection?.provider === 'proxy') {
|
|
if (!selection.route) return { code: 'proxy-route-missing' };
|
|
upstream = ({ anthropic: 'claude', glm: 'zai', codex: 'codex' })[selection.route];
|
|
if (!upstream) return { code: 'proxy-route-unknown' };
|
|
}
|
|
if (!upstream) upstream = ({ anthropic: 'claude', zai: 'zai' })[selection?.provider];
|
|
if (!Object.hasOwn(ROUTES, upstream ?? '')) return { code: 'proxy-provider-unsupported' };
|
|
// A selection that names this module came from the catalogue, which knows the account kind and is
|
|
// checked at admission. A legacy one (a provider or route name only) gets no catalogue check, so
|
|
// the kind is unknown and a model named as Claude is treated as OAuth.
|
|
const legacyClaude = selection?.module !== 'cliproxy' && /claude/i.test(String(selection?.model ?? ''));
|
|
if (harness && !harnesses(upstream, legacyClaude).includes(harness)) return { code: 'model-harness-incompatible' };
|
|
if (typeof selection.model !== 'string' || !MODEL.test(selection.model)) return { code: 'model-provider-unavailable' };
|
|
return { provider: selection.provider ?? upstream, model: selection.model, upstream, ...ROUTES[upstream] };
|
|
}
|
|
|
|
// #1873 ride-default slice 1: a new organization that uses the owner's models through the
|
|
// instance's model proxy. The Console reads the owner's catalogue from a riding bus and passes
|
|
// it as `owner`; this module never reaches the proxy itself.
|
|
const THINKING = Object.freeze(['low', 'medium', 'high', 'xhigh', 'max']);
|
|
export const usableModel = m => !!m && m.module === 'cliproxy' && m.available === true && m.reason === null
|
|
&& typeof m.key === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(m.key) && typeof m.upstream === 'string' && Object.hasOwn(ROUTES, m.upstream)
|
|
&& typeof m.provider === 'string' && typeof m.model === 'string' && MODEL.test(m.model)
|
|
&& Array.isArray(m.harnesses) && (m.harnesses.includes('claude-code') || m.harnesses.includes('pi'));
|
|
export function orgPresets() {
|
|
return [{ name: 'cliproxy', label: 'Your models through the model proxy',
|
|
description: 'Uses a model your model proxy lists. Each reply spends your own account.',
|
|
thinking: { levels: [...THINKING], default: 'low' },
|
|
available: ({ owner } = {}) => {
|
|
if (!owner || owner.proxy !== true) return { ok: false, reason: 'Set up the model proxy first, on the Model proxy page.' };
|
|
if (owner.riding !== true) return { ok: false, reason: 'No organization uses the model proxy yet, or none answered. Use Ride all on the Model proxy page.' };
|
|
if (!Array.isArray(owner.models) || !owner.models.some(usableModel)) return { ok: false, reason: 'The model proxy lists no model you can use. Sign in to an account on the Model proxy page.' };
|
|
return { ok: true };
|
|
},
|
|
overlay: ({ thinking, model }) => {
|
|
if (!usableModel(model)) throw Object.assign(Error('the model proxy lists no such model'), { code: 'org-invalid' });
|
|
const harness = model.harnesses.includes('claude-code') ? 'claude-code' : 'pi';
|
|
return {
|
|
plugins: { channel: ['cli', 'webui'], harness: [harness], 'model-provider': ['cliproxy'] },
|
|
launch: { by: 'chief', instances: ['assistant'], models: { [model.key]: { provider: model.provider, model: model.model, module: 'cliproxy', upstream: model.upstream } } },
|
|
assistantVars: { harness, model: model.key, thinking },
|
|
};
|
|
} }];
|
|
}
|