import { EntityType } from './chunk-2PBCTS3B.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();
}
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 currentSpan = tracingContext?.currentSpan ?? (tracingOptions?.traceId || tracingOptions?.parentSpanId ? void 0 : resolveCurrentSpan());
  const metadata = {
    ...rest.metadata ?? {},
    ...tracingOptions?.metadata ?? {}
  };
  if (currentSpan) {
    return 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, resolveCurrentSpan, setCurrentSpanResolver, setExecuteWithContext, setExecuteWithContextSync };
//# sourceMappingURL=chunk-JK5J6E2A.js.map
//# sourceMappingURL=chunk-JK5J6E2A.js.map