66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
/**
|
|
* chatCore cache-usage log meta helpers (Quality Gate v2 / Fase 9 — chatCore god-file decomposition,
|
|
* #3501).
|
|
*
|
|
* Pure helpers extracted from chatCore: coerce an unknown to a positive number, derive cache
|
|
* read/creation token counts from a usage object (handling both top-level and prompt_tokens_details
|
|
* shapes), and attach an `_omniroute` meta blob to a log payload. Side-effect-free; behaviour is
|
|
* byte-identical to the previous module-level functions.
|
|
*/
|
|
|
|
export function toPositiveNumber(value: unknown) {
|
|
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
}
|
|
|
|
export function buildCacheUsageLogMeta(usage: Record<string, unknown> | null | undefined) {
|
|
if (!usage || typeof usage !== "object") return null;
|
|
const promptTokenDetails =
|
|
usage.prompt_tokens_details && typeof usage.prompt_tokens_details === "object"
|
|
? (usage.prompt_tokens_details as Record<string, unknown>)
|
|
: undefined;
|
|
const hasCacheFields =
|
|
"cache_read_input_tokens" in usage ||
|
|
"cached_tokens" in usage ||
|
|
"cache_creation_input_tokens" in usage ||
|
|
(!!promptTokenDetails &&
|
|
("cached_tokens" in promptTokenDetails || "cache_creation_tokens" in promptTokenDetails));
|
|
const cacheReadTokens = toPositiveNumber(
|
|
usage.cache_read_input_tokens ?? usage.cached_tokens ?? promptTokenDetails?.cached_tokens
|
|
);
|
|
const cacheCreationTokens = toPositiveNumber(
|
|
usage.cache_creation_input_tokens ?? promptTokenDetails?.cache_creation_tokens
|
|
);
|
|
if (!hasCacheFields) return null;
|
|
return {
|
|
cacheReadTokens,
|
|
cacheCreationTokens,
|
|
};
|
|
}
|
|
|
|
export function attachLogMeta(
|
|
payload: Record<string, unknown> | null | undefined,
|
|
meta: Record<string, unknown> | null | undefined
|
|
) {
|
|
if (!meta || typeof meta !== "object") return payload;
|
|
const compactMeta = Object.fromEntries(
|
|
Object.entries(meta).filter(([, value]) => value !== null && value !== undefined)
|
|
);
|
|
if (Object.keys(compactMeta).length === 0) return payload;
|
|
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
return { _omniroute: compactMeta, _payload: payload ?? null };
|
|
}
|
|
const existing =
|
|
payload._omniroute &&
|
|
typeof payload._omniroute === "object" &&
|
|
!Array.isArray(payload._omniroute)
|
|
? payload._omniroute
|
|
: {};
|
|
return {
|
|
...payload,
|
|
_omniroute: {
|
|
...existing,
|
|
...compactMeta,
|
|
},
|
|
};
|
|
}
|