ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
75 lines (74 loc) • 2.4 kB
JavaScript
import { nanoid as createId } from "nanoid";
import { extractSuccessfulModelCalls, } from "../model-function/SuccessfulModelCall.js";
import { calculateCost } from "../cost/calculateCost.js";
export class DefaultRun {
constructor({ runId = createId(), 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 extractSuccessfulModelCalls(this.modelCallEvents);
}
calculateCost() {
return calculateCost({
calls: this.successfulModelCalls,
costCalculators: this.costCalculators,
});
}
}