ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
29 lines (28 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateCost = void 0;
const Cost_js_1 = require("./Cost.cjs");
async function calculateCost({ calls, costCalculators, }) {
let costInMillicents = 0;
const callsWithUnknownCost = [];
for (const call of calls) {
const model = call.model;
const providerCostCalculator = costCalculators.find((providerCostCalculator) => providerCostCalculator.provider === model.provider);
if (!providerCostCalculator) {
callsWithUnknownCost.push(call);
continue;
}
const cost = await providerCostCalculator.calculateCostInMillicents(call);
if (cost === null) {
callsWithUnknownCost.push(call);
continue;
}
costInMillicents += cost;
}
return new Cost_js_1.Cost({
costInMillicents,
hasUnknownCost: callsWithUnknownCost.length > 0,
callsWithUnknownCost,
});
}
exports.calculateCost = calculateCost;