@sema-agent/core
Version:
Stateless, task-oriented AI agent core
16 lines (15 loc) • 1.01 kB
JavaScript
import { servedReadingOf } from "../../brain/model-lane.js";
import { priceTableOf } from "../pricing.js";
import { cacheFamilyOf, callPriced, usageCostMicroUsd } from "./usage-accounting.js";
export function billsOf(requested, msg) {
const reading = servedReadingOf(requested, msg);
const bills = reading.attempts.map((a) => ({ model: a.model, usage: a.usage, ...(a.usageMissing === true ? { usageMissing: true } : {}) }));
if (msg?.usage !== undefined)
bills.push({ model: reading.served, usage: msg.usage, ...(msg.usageMissing === true ? { usageMissing: true } : {}) });
return { reading, bills };
}
export function priceReply(usage, model, pricing, usageMissing) {
const { table: price, exists } = priceTableOf(model, pricing);
const { tierPriced, ...figures } = usageCostMicroUsd(cacheFamilyOf(model), usage, price);
return { served: model, modelId: model.id, price, priced: callPriced(exists, tierPriced, usage, usageMissing), tableExists: exists, ...figures };
}