Files
Agent-Workbench/docs/task-agreement-1.25-evidence/source-bytes/src/shared/contracts/model-identity.mjs
T

45 lines
3.5 KiB
JavaScript

const validModel = value => typeof value === 'string' && value.length > 0 && value.length <= 200
&& value === value.trim() && !/[\x00-\x1f\x7f]/.test(value);
export function modelIdentitySettings(input = {}, previous = {}) {
const fields = ['modelIdentityPolicy', 'modelIdentityAliases', 'modelIdentityAliasSource'];
if (!fields.some(key => Object.hasOwn(input, key) || Object.hasOwn(previous, key))) return {};
const modelIdentityPolicy = input.modelIdentityPolicy ?? previous.modelIdentityPolicy ?? 'record';
if (!['record', 'require-match'].includes(modelIdentityPolicy)) throw new Error('请选择记录型号或型号不一致时暂停。');
const modelIdentityAliases = input.modelIdentityAliases ?? previous.modelIdentityAliases ?? [];
const modelIdentityAliasSource = input.modelIdentityAliasSource ?? previous.modelIdentityAliasSource ?? '';
if (!Array.isArray(modelIdentityAliases) || modelIdentityAliases.length > 8 || !modelIdentityAliases.every(validModel)
|| new Set(modelIdentityAliases).size !== modelIdentityAliases.length) throw new Error('返回型号映射最多8项,需使用不重复的完整型号。');
if (typeof modelIdentityAliasSource !== 'string' || modelIdentityAliasSource.length > 1000 || /[\x00-\x1f\x7f]/.test(modelIdentityAliasSource)
|| modelIdentityAliases.length && !modelIdentityAliasSource.trim()) throw new Error('登记型号映射时需要填写可核对的映射依据。');
return { modelIdentityPolicy, modelIdentityAliases: [...modelIdentityAliases], modelIdentityAliasSource: modelIdentityAliasSource.trim() };
}
export function modelIdentityObserver(requestedModel, config = {}) {
const settings = modelIdentitySettings(config), models = new Set(); let invalid = false;
return {
observe(value) {
for (const object of [value, value?.response, ...(value?.type === 'message_start' ? [value.message] : [])]) {
if (!object || !Object.hasOwn(object, 'model')) continue;
if (!validModel(object.model) || models.size >= 16 && !models.has(object.model)) { invalid = true; continue; }
models.add(object.model);
}
},
snapshot(complete) {
const reportedModels = [...models];
const status = !complete ? 'incomplete' : invalid ? 'invalid' : !reportedModels.length ? 'missing' : reportedModels.length > 1 ? 'mixed'
: reportedModels[0] === requestedModel ? 'matched' : settings.modelIdentityAliases?.includes(reportedModels[0]) ? 'mapped' : 'mismatch';
return { schemaVersion: 1, requestedModel, reportedModels, status, complete: complete === true, invalidIdentifiers: invalid,
policy: settings.modelIdentityPolicy ?? 'record',
...(settings.modelIdentityAliases?.length ? { mapping: { models: settings.modelIdentityAliases, source: settings.modelIdentityAliasSource, kind: 'user-declared' } } : {}),
scope: 'Identifiers in the outgoing request and upstream response; neither equal identifiers nor a declared mapping authenticate model weights.' };
},
};
}
export function modelIdentityAccepted(identity) { return identity?.complete === true && ['matched', 'mapped'].includes(identity.status); }
export function modelIdentityLabel(identity) {
return identity ? ({ matched: '请求与返回型号一致', mapped: '符合已登记型号映射', mismatch: '请求与返回型号不一致', mixed: '同次响应包含多个型号',
missing: '上游未报告型号', invalid: '上游型号字段无效', incomplete: '响应未完整,型号待核对' }[identity.status] ?? '型号待核对') : '历史记录未核对返回型号';
}