UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

95 lines (94 loc) • 4.45 kB
export function priceTableOf(model, pricing) { const deployed = pricing?.[model.id]; return deployed !== undefined && deployed !== null ? { table: deployed, exists: isModelPriced(model, pricing), from: "deployment" } : { table: modelCostToPricing(model.cost), exists: isModelPriced(model, pricing), from: "model" }; } function fastTierOf(table) { const fast = table.fast; return typeof fast === "object" && fast !== null && malformedFastTier(table) === undefined ? fast : undefined; } export function callRatesOf(table, servedFast) { if (!servedFast) return { rates: table, tierDeclared: true }; const fast = fastTierOf(table); return fast !== undefined ? { rates: fast, tierDeclared: true } : { rates: table, tierDeclared: false }; } const COST_FAST_MEMBER = { inputPer1M: "input", outputPer1M: "output", cacheReadPer1M: "cacheRead", cacheWritePer1M: "cacheWrite", cacheWriteLongPer1M: "cacheWriteLong" }; export function fastTierFaultOf(model, pricing, servesFast) { if (!servesFast) return undefined; const { table, exists, from } = priceTableOf(model, pricing); if (!exists) return undefined; const deployed = from === "deployment"; const base = deployed ? `RunnerDeps.pricing[${JSON.stringify(model.id)}].fast` : "cost.fast"; if (table.fast === undefined) return { where: base, missing: true, from }; const bad = malformedFastTier(table); if (bad === undefined) return undefined; const column = bad.slice("fast.".length); return { where: bad === "fast" ? base : `${base}.${deployed ? column : (COST_FAST_MEMBER[column] ?? column)}`, missing: false, from }; } const per1M = (tokens, price) => price && tokens ? (tokens * price) / 1e6 : 0; export function computeCostMicroUsd(counts, pricing) { const billedCacheRead = pricing.cacheReadPer1M !== undefined ? counts.cacheReadTokens : 0; const billedCacheWrite = pricing.cacheWritePer1M !== undefined ? counts.cacheWriteTokens : 0; const billedCacheWriteLong = pricing.cacheWriteLongPer1M !== undefined ? counts.cacheWriteTokensLong : 0; const fullInput = Math.max(0, counts.totalInputTokens - billedCacheRead - billedCacheWrite - billedCacheWriteLong); const usd = per1M(fullInput, pricing.inputPer1M) + per1M(billedCacheRead, pricing.cacheReadPer1M) + per1M(billedCacheWrite, pricing.cacheWritePer1M) + per1M(billedCacheWriteLong, pricing.cacheWriteLongPer1M) + per1M(counts.outputTokens, pricing.outputPer1M); return Math.round(usd * 1e6); } export function isModelPriced(model, pricing) { return pricing?.[model.id] !== undefined || model.cost !== undefined; } export function malformedPricingField(pricing) { return malformedRate(pricing) ?? malformedFastTier(pricing); } function malformedFastTier(pricing) { const fast = pricing.fast; if (fast === undefined) return undefined; if (typeof fast !== "object" || fast === null) return "fast"; const inner = malformedRate(fast); return inner === undefined ? undefined : `fast.${inner}`; } function malformedRate(pricing) { for (const f of ["inputPer1M", "outputPer1M"]) { const v = pricing[f]; if (typeof v !== "number" || !Number.isFinite(v) || v < 0) return f; } for (const f of ["cacheReadPer1M", "cacheWritePer1M", "cacheWriteLongPer1M"]) { const v = pricing[f]; if (v === undefined) continue; if (typeof v !== "number" || !Number.isFinite(v) || v < 0) return f; } return undefined; } export function modelCostToPricing(cost) { const fast = cost?.fast; const at = (key) => (typeof fast === "object" && fast !== null ? fast[key] : undefined); return { inputPer1M: cost?.input ?? 0, outputPer1M: cost?.output ?? 0, cacheReadPer1M: cost?.cacheRead, cacheWritePer1M: cost?.cacheWrite, cacheWriteLongPer1M: cost?.cacheWriteLong ?? cost?.cacheWrite, ...(fast !== undefined ? { fast: typeof fast === "object" && fast !== null ? { inputPer1M: at("input"), outputPer1M: at("output"), cacheReadPer1M: at("cacheRead"), cacheWritePer1M: at("cacheWrite"), cacheWriteLongPer1M: at("cacheWriteLong") === undefined ? at("cacheWrite") : at("cacheWriteLong") } : fast, } : {}), }; }