UNPKG

@maximai/maxim-js

Version:

Maxim AI JS SDK. Visit https://getmaxim.ai for more info.

355 lines 12.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpanContainer = exports.TraceContainer = exports.Container = exports.ContainerManager = exports.Metadata = exports.MetadataKeys = void 0; exports.MetadataKeys = [ "sessionId", "traceId", "spanId", "chainName", "spanName", "traceName", "generationName", "retrievalName", "toolCallName", "generationTags", "retrievalTags", "toolCallTags", "traceTags", "chainTags", ]; class Metadata { _parseTagsField(value) { if (typeof value === "object" && value !== null && !Array.isArray(value) && Object.entries(value).every(([k, v]) => typeof k === "string" && typeof v === "string")) { return value; } if (typeof value === "string") { try { const parsed = JSON.parse(value); if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) && Object.entries(parsed).every(([k, v]) => typeof k === "string" && typeof v === "string")) { return parsed; } } catch { } } return {}; } constructor(metadata) { if (!metadata) return; this.sessionId = typeof metadata["sessionId"] === "string" ? metadata["sessionId"] : undefined; this.traceId = typeof metadata["traceId"] === "string" ? metadata["traceId"] : undefined; this.spanId = typeof metadata["spanId"] === "string" ? metadata["spanId"] : undefined; this.spanName = typeof metadata["spanName"] === "string" ? metadata["spanName"] : undefined; this.chainName = typeof metadata["chainName"] === "string" ? metadata["chainName"] : undefined; this.traceName = typeof metadata["traceName"] === "string" ? metadata["traceName"] : undefined; this.generationName = typeof metadata["generationName"] === "string" ? metadata["generationName"] : undefined; this.toolCallName = typeof metadata["toolCallName"] === "string" ? metadata["toolCallName"] : undefined; this.retrievalName = typeof metadata["retrievalName"] === "string" ? metadata["retrievalName"] : undefined; this.generationTags = this._parseTagsField(metadata["generationTags"]); this.retrievalTags = this._parseTagsField(metadata["retrievalTags"]); this.toolCallTags = this._parseTagsField(metadata["toolCallTags"]); this.traceTags = this._parseTagsField(metadata["traceTags"]); this.chainTags = this._parseTagsField(metadata["chainTags"]); } toJSON() { return { sessionId: this.sessionId, traceId: this.traceId, spanId: this.spanId, chainName: this.chainName, spanName: this.spanName, traceName: this.traceName, generationName: this.generationName, retrievalName: this.retrievalName, toolCallName: this.toolCallName, generationTags: this.generationTags, retrievalTags: this.retrievalTags, toolCallTags: this.toolCallTags, traceTags: this.traceTags, chainTags: this.chainTags, }; } } exports.Metadata = Metadata; class ContainerManager { constructor() { this.allContainers = new Map(); this.traceIdToContainerIds = new Map(); this.containerIdToTraceId = new Map(); } addContainer(container) { this.allContainers.set(container.id, container); const traceId = container.getTraceId(); if (traceId) { if (!this.traceIdToContainerIds.has(traceId)) { this.traceIdToContainerIds.set(traceId, new Set()); } this.traceIdToContainerIds.get(traceId).add(container.id); this.containerIdToTraceId.set(container.id, traceId); } else if (container.parentId) { const traceIdFromParent = this.containerIdToTraceId.get(container.parentId); if (traceIdFromParent) { if (!this.traceIdToContainerIds.has(traceIdFromParent)) { this.traceIdToContainerIds.set(traceIdFromParent, new Set()); } this.traceIdToContainerIds.get(traceIdFromParent).add(container.id); this.containerIdToTraceId.set(container.id, traceIdFromParent); } } } getContainer(id) { return this.allContainers.get(id); } setContainer(runId, container) { this.allContainers.set(runId, container); if (!this.allContainers.has(container.id)) { this.addContainer(container); } } deleteContainer(id) { const container = this.allContainers.get(id); if (container) { this.removeContainer(container); } } getTraceContainer(containerId) { const traceId = this.containerIdToTraceId.get(containerId); return traceId ? this.allContainers.get(traceId) : undefined; } removeContainer(container) { this.allContainers.delete(container.id); const traceId = this.containerIdToTraceId.get(container.id); if (traceId) { this.containerIdToTraceId.delete(container.id); const traceContainers = this.traceIdToContainerIds.get(traceId); if (traceContainers) { traceContainers.delete(container.id); if (traceContainers.size === 0) { this.traceIdToContainerIds.delete(traceId); } } } } getContainersInTrace(traceId) { const containers = this.traceIdToContainerIds.get(traceId); return containers ? Array.from(containers) : []; } isTraceComplete(traceId) { const containers = this.traceIdToContainerIds.get(traceId); if (!containers) return true; for (const containerId of containers) { if (containerId === traceId) continue; const container = this.allContainers.get(containerId); if (container && !container.isEnded()) { return false; } } return true; } removeRunIdMapping(runId) { const container = this.allContainers.get(runId); if (!container) return; this.allContainers.delete(runId); if (container.type === "trace") { const traceId = container.getTraceId(); if (traceId && this.isTraceComplete(traceId) && !container.hasActiveChildren()) { container.end(); } } } } exports.ContainerManager = ContainerManager; class Container { constructor(containerManager, logger, _id, _type, _name, _parentId, markCreated = false) { this.containerManager = containerManager; this.logger = logger; this._id = _id; this._type = _type; this._name = _name; this._parentId = _parentId; this._created = false; this._ended = false; this._activeChildCount = 0; this._created = markCreated; } get id() { return this._id; } get type() { return this._type; } get parentId() { return this._parentId; } set parentId(id) { this._parentId = id; } isCreated() { return this._created; } isEnded() { return this._ended; } incrementChildCount() { this._activeChildCount++; } decrementChildCount() { this._activeChildCount = Math.max(0, this._activeChildCount - 1); } hasActiveChildren() { return this._activeChildCount > 0; } end() { if (this._ended) return; this.internalEnd(); this._ended = true; if (this.parentId) { const parent = this.containerManager.getContainer(this.parentId); if (parent) { parent.decrementChildCount(); } } this.cleanupIfComplete(); } cleanupIfComplete() { const traceId = this.getTraceId(); if (!traceId) return; if (this.type === "trace" && !this.hasActiveChildren()) { if (this.containerManager.isTraceComplete(traceId)) { this.containerManager.removeContainer(this); } } else if (this.type === "span") { this.containerManager.removeContainer(this); const traceContainer = this.containerManager.getContainer(traceId); if (traceContainer && this.containerManager.isTraceComplete(traceId)) { traceContainer.end(); } } } getTraceContainer() { return this.containerManager.getTraceContainer(this.id); } } exports.Container = Container; class TraceContainer extends Container { constructor(containerManager, logger, traceId, traceName, parentId, markCreated = false) { super(containerManager, logger, traceId, "trace", traceName, parentId, markCreated); containerManager.addContainer(this); } create(tags, sessionId) { const config = { id: this.id, name: this._name, tags, sessionId, }; this.logger.trace(config); this._created = true; } setInput(input) { if (this._input) return; this.logger.traceInput(this.id, input); this._input = input; } addGeneration(config) { return this.logger.traceGeneration(this.id, config); } addRetrieval(config) { return this.logger.traceRetrieval(this.id, config); } addEvent(eventId, name, tags) { this.logger.traceEvent(this.id, eventId, name, tags); } addSpan(config) { return this.logger.traceSpan(this.id, config); } addTags(tags) { Object.entries(tags).forEach(([key, value]) => { this.logger.traceTag(this.id, key, value); }); } addMetadata(metadata) { this.logger.traceMetadata(this.id, metadata); } addToolCall(config) { return this.logger.traceToolCall(this.id, config); } internalEnd() { this.logger.traceEnd(this.id); } getTraceId() { return this.id; } } exports.TraceContainer = TraceContainer; class SpanContainer extends Container { constructor(containerManager, logger, spanId, spanName, parentId, parentTraceId, markCreated = false) { super(containerManager, logger, spanId, "span", spanName, parentId, markCreated); this._parentTraceId = parentTraceId; containerManager.addContainer(this); if (parentId) { const parent = containerManager.getContainer(parentId); if (parent) { parent.incrementChildCount(); } } } create(tags) { if (!this.parentId) { throw new Error("[MaximSDK] Span without a parent is invalid"); } const config = { id: this.id, name: this._name, tags, }; this.logger.traceSpan(this.parentId, config); this._created = true; } addGeneration(config) { return this.logger.spanGeneration(this.id, config); } addRetrieval(config) { return this.logger.spanRetrieval(this.id, config); } addEvent(eventId, name, tags) { this.logger.spanEvent(this.id, eventId, name, tags); } addSpan(config) { return this.logger.spanSpan(this.id, config); } addTags(tags) { Object.entries(tags).forEach(([key, value]) => { this.logger.spanTag(this.id, key, value); }); } addMetadata(metadata) { this.logger.spanMetadata(this.id, metadata); } addToolCall(config) { return this.logger.spanToolCall(this.id, config); } internalEnd() { this.logger.spanEnd(this.id); } getTraceId() { return this._parentTraceId; } } exports.SpanContainer = SpanContainer; //# sourceMappingURL=containers.js.map