@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
125 lines (123 loc) • 3.57 kB
JavaScript
import { EntityType } from './chunk-QAXRURAT.js';
// src/observability/utils.ts
var entityTypeValues = new Set(Object.values(EntityType));
var currentSpanResolver;
function setCurrentSpanResolver(resolver) {
currentSpanResolver = resolver;
}
function resolveCurrentSpan() {
return currentSpanResolver?.();
}
function generateSignalId() {
return crypto.randomUUID();
}
function getStepAvailableToolNames(tools, activeTools) {
if (activeTools !== void 0) {
return [...activeTools];
}
if (tools) {
return Object.keys(tools);
}
return void 0;
}
var executeWithContextImpl;
var executeWithContextSyncImpl;
function setExecuteWithContext(impl) {
executeWithContextImpl = impl;
}
function setExecuteWithContextSync(impl) {
executeWithContextSyncImpl = impl;
}
async function executeWithContext(params) {
if (executeWithContextImpl) {
return executeWithContextImpl(params);
}
const { span, fn } = params;
if (span?.executeInContext) {
return span.executeInContext(fn);
}
return fn();
}
function executeWithContextSync(params) {
if (executeWithContextSyncImpl) {
return executeWithContextSyncImpl(params);
}
const { span, fn } = params;
if (span?.executeInContextSync) {
return span.executeInContextSync(fn);
}
return fn();
}
function getOrCreateSpan(options) {
const { type, attributes, tracingContext, requestContext, tracingOptions, ...rest } = options;
const metadata = {
...rest.metadata ?? {},
...tracingOptions?.metadata ?? {}
};
if (tracingContext?.currentSpan) {
return tracingContext.currentSpan.createChildSpan({
type,
attributes,
...rest,
metadata,
requestContext
});
}
const instance = options.mastra?.observability?.getSelectedInstance({ requestContext });
return instance?.startSpan({
type,
attributes,
...rest,
metadata,
requestContext,
tracingOptions,
traceId: tracingOptions?.traceId,
parentSpanId: tracingOptions?.parentSpanId,
customSamplerOptions: {
requestContext,
metadata
}
});
}
function getRootExportSpan(span) {
if (!span?.isValid) {
return void 0;
}
let current = span;
let rootExportSpan = span.isInternal ? void 0 : span;
while (current?.parent) {
current = current.parent;
if (!current.isInternal) {
rootExportSpan = current;
}
}
return rootExportSpan;
}
function getEntityTypeForSpan(span) {
if (span.entityType && entityTypeValues.has(span.entityType)) {
return span.entityType;
}
switch (span.spanType) {
case "agent_run" /* AGENT_RUN */:
return EntityType.AGENT;
case "rag_ingestion" /* RAG_INGESTION */:
return EntityType.RAG_INGESTION;
case "scorer_run" /* SCORER_RUN */:
case "scorer_step" /* SCORER_STEP */:
return EntityType.SCORER;
case "workflow_run" /* WORKFLOW_RUN */:
return EntityType.WORKFLOW_RUN;
case "workflow_step" /* WORKFLOW_STEP */:
return EntityType.WORKFLOW_STEP;
case "tool_call" /* TOOL_CALL */:
case "mcp_tool_call" /* MCP_TOOL_CALL */:
return EntityType.TOOL;
case "processor_run" /* PROCESSOR_RUN */:
return EntityType.OUTPUT_PROCESSOR;
default:
return void 0;
}
}
export { executeWithContext, executeWithContextSync, generateSignalId, getEntityTypeForSpan, getOrCreateSpan, getRootExportSpan, getStepAvailableToolNames, resolveCurrentSpan, setCurrentSpanResolver, setExecuteWithContext, setExecuteWithContextSync };
//# sourceMappingURL=chunk-MJEXAXIO.js.map
//# sourceMappingURL=chunk-MJEXAXIO.js.map