fix(models): retain upstream identity and pause strict mismatches
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { taskContract } from './task-control.mjs';
|
||||
import { modelWaitSettings } from '../../core/models/request-deadline.mjs';
|
||||
import { modelIdentitySettings } from './model-identity.mjs';
|
||||
|
||||
export const EXECUTION_SCHEMA = 1;
|
||||
export const ACTIVE_STATES = new Set(['running', 'awaiting_approval', 'stopping']);
|
||||
@@ -20,6 +21,7 @@ export function modelSnapshot(config) {
|
||||
contextLimit: config.contextLimit, maxOutputTokens: config.maxOutputTokens,
|
||||
configRevision: config.configRevision ?? 1,
|
||||
...modelWaitSettings(config),
|
||||
...modelIdentitySettings(config),
|
||||
...(config.protocol ? { protocol: config.protocol, streaming: config.streaming === true } : {}),
|
||||
...(config.nativeParameters ? { nativeParameters: structuredClone(config.nativeParameters), outputTokenField: config.outputTokenField } : {}),
|
||||
...(config.modelRouting ? { routing: structuredClone(config.modelRouting) } : {}),
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export interface ModelIdentity {
|
||||
schemaVersion: number; requestedModel: string; reportedModels: string[]; status: string; complete: boolean; invalidIdentifiers: boolean; policy: string;
|
||||
mapping?: { models: string[]; source: string; kind: string }; responseSource?: { artifactId: string; sha256: string; sessionId: string; scope: string }; scope: string;
|
||||
}
|
||||
export function modelIdentityLabel(identity?: ModelIdentity | null): string;
|
||||
export function modelIdentityAccepted(identity?: ModelIdentity | null): boolean;
|
||||
@@ -0,0 +1,44 @@
|
||||
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]) {
|
||||
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] ?? '型号待核对') : '历史记录未核对返回型号';
|
||||
}
|
||||
Reference in New Issue
Block a user