ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
79 lines (78 loc) • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultRun = void 0;
const nanoid_1 = require("nanoid");
const SuccessfulModelCall_js_1 = require("../model-function/SuccessfulModelCall.cjs");
const calculateCost_js_1 = require("../cost/calculateCost.cjs");
class DefaultRun {
constructor({ runId = (0, nanoid_1.nanoid)(), sessionId, userId, abortSignal, observers, costCalculators = [], } = {}) {
Object.defineProperty(this, "runId", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "sessionId", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "userId", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "abortSignal", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "costCalculators", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "modelCallEvents", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(this, "observers", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.runId = runId;
this.sessionId = sessionId;
this.userId = userId;
this.abortSignal = abortSignal;
this.costCalculators = costCalculators;
this.observers = [
{
onModelCallStarted: (event) => {
this.modelCallEvents.push(event);
},
onModelCallFinished: (event) => {
this.modelCallEvents.push(event);
},
},
...(observers ?? []),
];
}
get successfulModelCalls() {
return (0, SuccessfulModelCall_js_1.extractSuccessfulModelCalls)(this.modelCallEvents);
}
calculateCost() {
return (0, calculateCost_js_1.calculateCost)({
calls: this.successfulModelCalls,
costCalculators: this.costCalculators,
});
}
}
exports.DefaultRun = DefaultRun;