@maximai/maxim-js
Version:
Maxim AI JS SDK. Visit https://getmaxim.ai for more info.
152 lines • 6.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvaluateContainer = exports.EventEmittingBaseContainer = exports.EvaluatableBaseContainer = exports.BaseContainer = void 0;
const utils_1 = require("../utils");
const types_1 = require("./types");
class BaseContainer {
constructor(entity, config, writer) {
this.entity = entity;
if (!config.id) {
config.id = (0, utils_1.uniqueId)();
}
if (!/^[a-zA-Z0-9_-]+$/.test(config.id)) {
if (writer.raiseExceptions) {
throw new Error(`Invalid ID: ${config.id}. ID must only contain alphanumeric characters, hyphens, and underscores. Event will not be logged.`);
}
else {
console.error(`Invalid ID: ${config.id}. ID must only contain alphanumeric characters, hyphens, and underscores. Event will not be logged.`);
}
}
this._id = config.id;
this._name = config.name;
this.spanId = config.spanId;
this.startTimestamp = (0, utils_1.utcNow)();
this.tags = config.tags || {};
this.writer = writer;
}
get id() {
return this._id;
}
addTag(key, value) {
this.commit("update", { tags: { [key]: value } });
}
static addTag_(writer, entity, id, key, value) {
BaseContainer.commit_(writer, entity, id, "update", { tags: { [key]: value } });
}
addMetadata(metadata) {
const sanitizedMetadata = Object.entries(metadata).reduce((acc, [key, value]) => {
acc[key] = JSON.stringify((0, utils_1.makeObjectSerializable)(value));
return acc;
}, {});
this.commit("update", { metadata: sanitizedMetadata });
}
static addMetadata_(writer, entity, id, metadata) {
const sanitizedMetadata = Object.entries(metadata).reduce((acc, [key, value]) => {
acc[key] = JSON.stringify((0, utils_1.makeObjectSerializable)(value));
return acc;
}, {});
BaseContainer.commit_(writer, entity, id, "update", { metadata: sanitizedMetadata });
}
end() {
this.endTimestamp = (0, utils_1.utcNow)();
this.commit("end", { endTimestamp: this.endTimestamp });
}
static end_(writer, entity, id, data) {
if (!data) {
data = { endTimestamp: (0, utils_1.utcNow)() };
}
else if (!data.endTimestamp) {
data.endTimestamp = (0, utils_1.utcNow)();
}
BaseContainer.commit_(writer, entity, id, "end", data);
}
data() {
return {
name: this._name,
spanId: this.spanId,
tags: this.tags,
startTimestamp: this.startTimestamp,
endTimestamp: this.endTimestamp,
};
}
commit(action, data) {
this.writer.commit(new types_1.CommitLog(this.entity, this._id, action, data ? data : this.data()));
}
static commit_(writer, entity, id, action, data) {
writer.commit(new types_1.CommitLog(entity, id, action, data !== null && data !== void 0 ? data : {}));
}
}
exports.BaseContainer = BaseContainer;
class EvaluatableBaseContainer extends BaseContainer {
get evaluate() {
return new EvaluateContainer(this.writer, this.entity, this.id);
}
static evaluate_(writer, entity, id) {
return new EvaluateContainer(writer, entity, id);
}
}
exports.EvaluatableBaseContainer = EvaluatableBaseContainer;
class EventEmittingBaseContainer extends EvaluatableBaseContainer {
event(id, name, tags, metadata) {
if (metadata) {
const sanitizedMetadata = Object.entries(metadata).reduce((acc, [key, value]) => {
acc[key] = JSON.stringify((0, utils_1.makeObjectSerializable)(value));
return acc;
}, {});
this.commit("add-event", { id: id, name, timestamp: (0, utils_1.utcNow)(), tags, metadata: sanitizedMetadata });
return;
}
this.commit("add-event", { id: id, name, timestamp: (0, utils_1.utcNow)(), tags });
}
static event_(writer, entity, id, eventId, name, tags, metadata) {
if (metadata) {
const sanitizedMetadata = Object.entries(metadata).reduce((acc, [key, value]) => {
acc[key] = JSON.stringify((0, utils_1.makeObjectSerializable)(value));
return acc;
}, {});
BaseContainer.commit_(writer, entity, id, "add-event", { id: eventId, name, timestamp: (0, utils_1.utcNow)(), tags, metadata: sanitizedMetadata });
return;
}
BaseContainer.commit_(writer, entity, id, "add-event", { id: eventId, name, timestamp: (0, utils_1.utcNow)(), tags });
}
}
exports.EventEmittingBaseContainer = EventEmittingBaseContainer;
class EvaluateContainer {
constructor(writer, entity, id) {
this._writer = writer;
this._entity = entity;
this._id = id;
}
withVariables(variables, forEvaluators) {
if (forEvaluators.length === 0)
return;
this._writer.commit(new types_1.CommitLog(this._entity, this._id, "evaluate", {
with: "variables",
variables,
evaluators: Array.from(new Set(forEvaluators)),
timestamp: (0, utils_1.utcNow)(),
}));
}
withEvaluators(...evaluators) {
if (evaluators.length === 0) {
return {
withVariables: (variables) => {
this.withVariables(variables, evaluators);
},
};
}
const uniqueEvaluators = Array.from(new Set(evaluators));
this._writer.commit(new types_1.CommitLog(this._entity, this._id, "evaluate", {
with: "evaluators",
evaluators: uniqueEvaluators,
timestamp: (0, utils_1.utcNow)(),
}));
return {
withVariables: (variables) => {
this.withVariables(variables, uniqueEvaluators);
},
};
}
}
exports.EvaluateContainer = EvaluateContainer;
//# sourceMappingURL=base.js.map