159 lines
11 KiB
JavaScript
159 lines
11 KiB
JavaScript
import {actualRequest,outputReservation,requestMetrics} from './model-request.mjs';
|
|
import {accountedTokens,checkBudget,BudgetExceeded} from './execution.mjs';
|
|
|
|
const count=value=>Number.isSafeInteger(value)&&value>=0;
|
|
const bounded=value=>Math.min(Number.MAX_SAFE_INTEGER,Math.max(0,Math.ceil(value)));
|
|
const endpoint=value=>String(value??'').replace(/\/+$/,'');
|
|
export function localModelEstimate(body){
|
|
const metrics=requestMetrics(body);
|
|
const output = outputReservation(body);
|
|
return {schemaVersion:1,inputTokens:metrics.estimatedInputTokens,outputTokens:output ?? 0,...(output === null ? { outputLimitKnown: false } : {}),
|
|
requestSha256:metrics.sha256,requestBytes:metrics.bytes,estimateMethod:metrics.estimateMethod};
|
|
}
|
|
export function taskConsumption(store,taskId){
|
|
return [...store.core.run.values()].filter(run=>run.taskId===taskId)
|
|
.flatMap(run=>(run.consumption??[]).map(receipt=>({...receipt,runId:run.id})));
|
|
}
|
|
|
|
export function checkModelDispatchBudget(store,session,run,config,body,overheadTokens=0,previousFailure=null){
|
|
const task=store.owned('task',run.taskId,session.id);
|
|
try{
|
|
checkBudget(run,task);
|
|
const tokenReservation=modelReservation(config,body,taskConsumption(store,task.id),overheadTokens);
|
|
const remaining=value=>value.budget.maxTokens>0?Math.max(0,value.budget.maxTokens-accountedTokens(value.usage)):Infinity;
|
|
const runRemainingTokens=remaining(run),taskRemainingTokens=remaining(task),remainingTokens=Math.min(runRemainingTokens,taskRemainingTokens);
|
|
if(tokenReservation.reservedTokens>remainingTokens){
|
|
const error=new BudgetExceeded(runRemainingTokens<=taskRemainingTokens?'token_limit':'task_token_limit','下一次模型请求的用量预留超过剩余预算,已在发送前暂停。原用量和已完成工具结果保留。');
|
|
error.contextBudget={pauseKind:'request_reservation',tokenReservation,remainingTokens,runRemainingTokens:Number.isFinite(runRemainingTokens)?runRemainingTokens:null,
|
|
taskRemainingTokens:Number.isFinite(taskRemainingTokens)?taskRemainingTokens:null};
|
|
throw error;
|
|
}
|
|
return tokenReservation;
|
|
}catch(error){
|
|
if(error instanceof BudgetExceeded&&previousFailure)error.contextBudget={...error.contextBudget,modelFailure:structuredClone(previousFailure)};
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export const RESERVATION_METHOD='observed-route-max-v3';
|
|
export const ISOLATED_OUTLIER_FACTOR=1.5,ISOLATED_OUTLIER_MIN_RECEIPTS=3;
|
|
const members=config=>config.modelRouting?.members??[{id:null,model:config.model,protocol:config.protocol??'chat-completions',baseUrl:config.baseUrl}];
|
|
function routeSources(member,receipts){
|
|
const seen=new Set(),sources=[];
|
|
for(const receipt of receipts){
|
|
const estimate=receipt.localEstimate,usage=receipt.usage;
|
|
if(receipt.model!==member.model||(receipt.protocol??'chat-completions')!==(member.protocol??'chat-completions')
|
|
||(receipt.connectionId??null)!==(member.id??null)||endpoint(receipt.endpoint)!==endpoint(member.baseUrl)
|
|
||receipt.usageStatus!=='reported'||!usage||estimate?.schemaVersion!==1||!count(estimate.inputTokens)||estimate.inputTokens<1
|
|
||!receipt.attemptId||seen.has(receipt.attemptId))continue;
|
|
if(!count(usage.total_tokens)&&!(count(usage.prompt_tokens)&&count(usage.completion_tokens)))continue;
|
|
seen.add(receipt.attemptId);
|
|
const total=count(usage.total_tokens)?usage.total_tokens:usage.prompt_tokens+usage.completion_tokens;
|
|
sources.push({runId:receipt.runId,attemptId:receipt.attemptId,localInputTokens:estimate.inputTokens,
|
|
reportedInputTokens:count(usage.prompt_tokens)?usage.prompt_tokens:null,reportedOutputTokens:count(usage.completion_tokens)?usage.completion_tokens:null,totalTokens:total});
|
|
}
|
|
return sources;
|
|
}
|
|
function calibrate(local,sources,overheadTokens,outputSources=sources){
|
|
let inputRatio=1,observedOutputTokens=local.outputTokens,observedTotalFloor=0;
|
|
for(const source of sources){
|
|
if(source.reportedInputTokens!==null)inputRatio=Math.max(inputRatio,source.reportedInputTokens/source.localInputTokens);
|
|
observedTotalFloor=Math.max(observedTotalFloor,source.totalTokens);
|
|
}
|
|
for(const source of outputSources)if(source.reportedOutputTokens!==null)observedOutputTokens=Math.max(observedOutputTokens,source.reportedOutputTokens);
|
|
return {inputRatio,observedOutputTokens,observedTotalFloor,
|
|
reservedTokens:bounded(Math.max(local.inputTokens*inputRatio+observedOutputTokens,observedTotalFloor)+overheadTokens)};
|
|
}
|
|
const ratioOf=source=>source.reportedInputTokens===null?null:source.reportedInputTokens/source.localInputTokens;
|
|
// One receipt may be set aside only when it is the single input-ratio outlier,
|
|
// at least ISOLATED_OUTLIER_FACTOR above every other receipt, with at least one
|
|
// later normal receipt on the same route. A repeated high ratio is never excluded.
|
|
export function isolatedOutlier(sources){
|
|
if(sources.length<ISOLATED_OUTLIER_MIN_RECEIPTS)return null;
|
|
const ratios=sources.map(ratioOf);if(ratios.some(value=>value===null))return null;
|
|
const top=ratios.indexOf(Math.max(...ratios)),rest=ratios.filter((_,index)=>index!==top),next=Math.max(1,...rest);
|
|
if(ratios[top]<next*ISOLATED_OUTLIER_FACTOR||top===sources.length-1)return null;
|
|
return {index:top,ratio:ratios[top],nextRatio:next};
|
|
}
|
|
function reservationRoutes(config,body,receipts,overheadTokens,method){
|
|
if(!count(overheadTokens))throw new Error('模型请求封装预留无效。');
|
|
const routes=members(config).map(member=>{
|
|
const local=localModelEstimate(actualRequest(body,member));
|
|
if(!count(local.inputTokens)||!count(local.outputTokens))throw new Error('模型请求输入或输出预留无效。');
|
|
const all=routeSources(member,receipts),outlier=method!=='observed-route-max-v1'?isolatedOutlier(all):null;
|
|
const sources=outlier?all.filter((_,index)=>index!==outlier.index):all;
|
|
const route={connectionId:member.id??null,model:member.model,protocol:member.protocol??'chat-completions',endpoint:member.baseUrl,
|
|
localInputTokens:local.inputTokens,outputReservation:local.outputTokens,...(local.outputLimitKnown === false ? { outputLimitKnown: false } : {}),...calibrate(local,sources,overheadTokens,method===RESERVATION_METHOD?all:sources),sources};
|
|
if(method!=='observed-route-max-v1')route.excludedSources=outlier?[{...all[outlier.index],sourceIndex:outlier.index,reason:'isolated_input_ratio_outlier',
|
|
inputRatio:outlier.ratio,nextHighestRatio:outlier.nextRatio,factor:ISOLATED_OUTLIER_FACTOR}]:[];
|
|
return route;
|
|
});
|
|
if(!routes.length)throw new Error('模型预留没有实际候选线路。');
|
|
return {schemaVersion:1,method,overheadTokens,reservedTokens:Math.max(...routes.map(route=>route.reservedTokens)),routes,
|
|
...(method===RESERVATION_METHOD?{receiptHistory:receipts.map(({runId,attemptId})=>({runId,attemptId}))}:{})};
|
|
}
|
|
|
|
// A conservative reservation for the next dispatch, not corrected historical
|
|
// billing or a promise about an unseen upstream response. It never lowers the
|
|
// local estimate; an excluded outlier stays in the ledger and in excludedSources.
|
|
export function modelReservation(config,body,receipts=[],overheadTokens=0){
|
|
return reservationRoutes(config,body,receipts,overheadTokens,RESERVATION_METHOD);
|
|
}
|
|
// Original v1 calibration, kept only so sealed historical reservations verify.
|
|
export function legacyModelReservation(config,body,receipts=[],overheadTokens=0){
|
|
return reservationRoutes(config,body,receipts,overheadTokens,'observed-route-max-v1');
|
|
}
|
|
export function legacyV2ModelReservation(config,body,receipts=[],overheadTokens=0){
|
|
return reservationRoutes(config,body,receipts,overheadTokens,'observed-route-max-v2');
|
|
}
|
|
|
|
export function verifyModelReservation(store,session,taskId,config,body,reservation){
|
|
const compute=reservation?.method===RESERVATION_METHOD?modelReservation:reservation?.method==='observed-route-max-v2'?legacyV2ModelReservation:
|
|
reservation?.method==='observed-route-max-v1'?legacyModelReservation:null;
|
|
if(reservation?.schemaVersion!==1||!compute||!Array.isArray(reservation.routes))throw new Error('模型用量预留版本或线路缺失。');
|
|
const receipts=new Map(),ordered=route=>{
|
|
const list=[...(route.sources??[])];
|
|
for(const value of route.excludedSources??[]){
|
|
if(!Number.isSafeInteger(value.sourceIndex)||value.sourceIndex<0||value.sourceIndex>list.length)throw new Error('模型预算预留的排除来源位置无效。');
|
|
list.splice(value.sourceIndex,0,value);
|
|
}
|
|
return list;
|
|
};
|
|
for(const route of reservation.routes)for(const source of ordered(route)){
|
|
const run=store.owned('run',source.runId,session.id);
|
|
if(run.taskId!==taskId)throw new Error('模型预算校准来源不属于当前Task。');
|
|
const receipt=run.consumption?.find(value=>value.attemptId===source.attemptId);
|
|
if(!receipt)throw new Error('模型预算校准缺少原消费记录。');
|
|
receipts.set(source.attemptId,{...receipt,runId:run.id});
|
|
}
|
|
// The proof may describe an earlier request: later consumption must not make
|
|
// that sealed snapshot stale. Recover its prefix from the original ledger,
|
|
// never from the supplied source order or excludedSources.sourceIndex.
|
|
const ledger=taskConsumption(store,taskId),key=value=>JSON.stringify([value.runId,value.attemptId]);
|
|
let original;
|
|
if(reservation.method===RESERVATION_METHOD){
|
|
if(!Array.isArray(reservation.receiptHistory)||reservation.receiptHistory.length>ledger.length)throw new Error('模型预算预留的原消费范围不一致。');
|
|
original=ledger.slice(0,reservation.receiptHistory.length);
|
|
if(JSON.stringify(original.map(({runId,attemptId})=>({runId,attemptId})))!==JSON.stringify(reservation.receiptHistory))throw new Error('模型预算预留的原消费顺序不一致。');
|
|
for(const receipt of original){const run=store.owned('run',receipt.runId,session.id);if(run.taskId!==taskId)throw new Error('模型预算校准来源不属于当前Task。');}
|
|
}else{
|
|
const selected=new Set([...receipts.values()].map(key));
|
|
original=ledger.filter(receipt=>selected.has(key(receipt)));
|
|
if(original.length!==receipts.size)throw new Error('模型预算预留的原消费范围不一致。');
|
|
}
|
|
const expected=compute(config,body,original,reservation.overheadTokens);
|
|
if(JSON.stringify(expected)!==JSON.stringify(reservation))throw new Error('模型预算预留与原估算/用量/实际线路不一致。');
|
|
return reservation;
|
|
}
|
|
|
|
export function tokenBudgetState(run,task){
|
|
const state=value=>{
|
|
const reportedTokens=value.usage?.totalTokens??0,estimatedUnknownTokens=value.usage?.estimatedUnknownTokens??0,
|
|
accountedTokens=reportedTokens+estimatedUnknownTokens,maximumTokens=value.budget?.maxTokens??0;
|
|
return {maximumTokens,reportedTokens,unknownRequests:value.usage?.unknownRequests??0,estimatedUnknownTokens,accountedTokens,
|
|
remainingTokens:maximumTokens>0?Math.max(0,maximumTokens-accountedTokens):null,
|
|
exceededTokens:maximumTokens>0?Math.max(0,accountedTokens-maximumTokens):0};
|
|
};
|
|
return {schemaVersion:1,singleInFlightResponseMayExceed:true,run:state(run),task:state(task)};
|
|
}
|