fix(models): preserve cache usage categories and receipt sources
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
|
||||
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||||
import { withTokenAccounting } from '../../shared/contracts/model-usage.mjs';
|
||||
export const protocolFailure = (message, kind = 'protocol', extra = null) => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
|
||||
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString(), ...(extra ? extra : {}) });
|
||||
|
||||
@@ -12,6 +13,10 @@ export const outputTruncatedFailure = (kind = 'protocol') => protocolFailure(OUT
|
||||
export { protocolRoute } from '../../shared/contracts/model-endpoint.mjs';
|
||||
|
||||
export function reportedUsage(config, value) {
|
||||
return withTokenAccounting(baseReportedUsage(config, value));
|
||||
}
|
||||
|
||||
function baseReportedUsage(config, value) {
|
||||
const raw = value?.usage;
|
||||
if (!raw || typeof raw !== 'object') return null;
|
||||
if (config.protocol === 'anthropic-messages') {
|
||||
@@ -30,6 +35,7 @@ export function reportedUsage(config, value) {
|
||||
const output = config.protocol === 'responses' ? raw.output_tokens : raw.completion_tokens;
|
||||
const valid = number => Number.isSafeInteger(number) && number >= 0;
|
||||
if (!valid(raw.total_tokens) && !(valid(input) && valid(output))) return null;
|
||||
if (!valid(raw.total_tokens) && !valid(input + output)) return null;
|
||||
const cached = config.protocol === 'responses' ? raw.input_tokens_details?.cached_tokens : raw.prompt_tokens_details?.cached_tokens ?? raw.prompt_cache_hit_tokens;
|
||||
const cacheDetails = valid(cached) && valid(input) && cached <= input ? { cached_tokens: cached } : null;
|
||||
const reasoning = config.protocol === 'responses' ? raw.output_tokens_details?.reasoning_tokens : raw.completion_tokens_details?.reasoning_tokens;
|
||||
|
||||
Reference in New Issue
Block a user