'use strict';

var chunk5K2PRY26_cjs = require('./chunk-5K2PRY26.cjs');

// src/observability/utils.ts
var entityTypeValues = new Set(Object.values(chunk5K2PRY26_cjs.EntityType));
var currentSpanResolver;
function setCurrentSpanResolver(resolver) {
  currentSpanResolver = resolver;
}
function resolveCurrentSpan() {
  return currentSpanResolver?.();
}
function generateSignalId() {
  return crypto.randomUUID();
}
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 chunk5K2PRY26_cjs.EntityType.AGENT;
    case "rag_ingestion" /* RAG_INGESTION */:
      return chunk5K2PRY26_cjs.EntityType.RAG_INGESTION;
    case "scorer_run" /* SCORER_RUN */:
    case "scorer_step" /* SCORER_STEP */:
      return chunk5K2PRY26_cjs.EntityType.SCORER;
    case "workflow_run" /* WORKFLOW_RUN */:
      return chunk5K2PRY26_cjs.EntityType.WORKFLOW_RUN;
    case "workflow_step" /* WORKFLOW_STEP */:
      return chunk5K2PRY26_cjs.EntityType.WORKFLOW_STEP;
    case "tool_call" /* TOOL_CALL */:
    case "mcp_tool_call" /* MCP_TOOL_CALL */:
      return chunk5K2PRY26_cjs.EntityType.TOOL;
    case "processor_run" /* PROCESSOR_RUN */:
      return chunk5K2PRY26_cjs.EntityType.OUTPUT_PROCESSOR;
    default:
      return void 0;
  }
}

exports.executeWithContext = executeWithContext;
exports.executeWithContextSync = executeWithContextSync;
exports.generateSignalId = generateSignalId;
exports.getEntityTypeForSpan = getEntityTypeForSpan;
exports.getOrCreateSpan = getOrCreateSpan;
exports.getRootExportSpan = getRootExportSpan;
exports.resolveCurrentSpan = resolveCurrentSpan;
exports.setCurrentSpanResolver = setCurrentSpanResolver;
exports.setExecuteWithContext = setExecuteWithContext;
exports.setExecuteWithContextSync = setExecuteWithContextSync;
//# sourceMappingURL=chunk-YGD6CZV4.cjs.map
//# sourceMappingURL=chunk-YGD6CZV4.cjs.map