import { unrestricted } from './execution-policy.mjs'; import { officialModelCapacity } from './official-model-capacity.mjs'; import { resolveModelSpecification } from './official-model-registry.mjs'; // Editable fallbacks for upstreams that do not publish capacity. These values // are not verified model capabilities or ceilings on larger models. export const DEFAULT_MODEL_CAPACITY = Object.freeze({ contextLimit: 128000, maxOutputTokens: 16384 }); export const CAPACITY_RANGES = Object.freeze({ contextLimit: [1000, Number.MAX_SAFE_INTEGER], maxOutputTokens: [256, Number.MAX_SAFE_INTEGER] }); export function capacitySettings(input = {}, previous = DEFAULT_MODEL_CAPACITY, config = {}) { return Object.fromEntries(Object.entries(CAPACITY_RANGES).map(([key, [min, max]]) => { const value = input[key] ?? previous[key] ?? DEFAULT_MODEL_CAPACITY[key]; const minimum = unrestricted(config) ? 1 : min; if (!Number.isSafeInteger(value) || value < minimum || value > max) throw Object.assign(new Error(key + ' 需要不小于 ' + minimum + ' 的安全整数,请核对模型实际容量。'), { status: 400 }); return [key, value]; })); } export const isPassthrough = config => config.capacityMode === 'passthrough'; export function modelCapacityDeclaration(config, field) { if (!['contextLimit', 'maxOutputTokens'].includes(field)) return null; const member = config.modelRouting?.members?.find(value => value.id === config.modelConnectionId) ?? config; const fact = resolveModelSpecification(member).fields[field]; if (fact) return Number.isSafeInteger(fact.value) && fact.value > 0 ? { value: fact.value, source: fact.kind, sourceUrl: fact.url, checkedAt: fact.checkedAt, sourceSha256: fact.sha256, provider: fact.provider, conflict: fact.conflict, alternatives: fact.alternatives } : null; const official = officialModelCapacity(member.model, { providerId: member.providerId, apiVersion: member.apiVersion, protocol: member.protocol }); return official && Number.isSafeInteger(official[field]) && official[field] > 0 ? { value: official[field], source: official.source, sourceUrl: official.sourceUrl, checkedAt: official.checkedAt, sourceSha256: official.sourceSha256, provider: official.provider } : null; } export function messagesOutputSetting(config) { const member = config.modelRouting?.members?.find(value => value.id === config.modelConnectionId) ?? config; const native = config.nativeParameters ?? member.nativeParameters ?? {}; if (Object.hasOwn(native, 'max_tokens')) return { value: native.max_tokens, source: 'explicit_native' }; if (resolveModelSpecification(member).fields['request.maxTokensRequired']?.value === false) return { value: undefined, source: 'upstream_optional' }; const declaration = modelCapacityDeclaration(config, 'maxOutputTokens'); if (declaration) return declaration; return { value: member.maxOutputTokens ?? config.maxOutputTokens ?? DEFAULT_MODEL_CAPACITY.maxOutputTokens, source: 'connection_declaration' }; } export function capacityMode(value = 'manual') { if (!['passthrough', 'model', 'manual'].includes(value)) throw Object.assign(new Error('请选择默认透传、按模型容量或手动限制。'), { status: 400 }); return value; } export function routedCapacity(config, members) { const mode = capacityMode(config.capacityMode); // Connection capacity declarations remain metadata in passthrough mode. if (mode === 'passthrough') return { contextLimit: config.contextLimit, maxOutputTokens: config.maxOutputTokens }; return Object.fromEntries(Object.keys(DEFAULT_MODEL_CAPACITY).map(key => [key, mode === 'model' && key === 'maxOutputTokens' ? Math.max(...members.map(member => member[key])) : Math.min(...members.map(member => member[key]), ...(mode === 'manual' ? [config[key]] : []))])); }