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UAAA,IAAc,gBAAA,CAAiB,GAAA,CAAI,IAAA,CAAK,UAAwB,CAAA,EAAG;AAC1E,IAAA,OAAO,IAAA,CAAK,UAAA;AAAA,EACd;AAEA,EAAA,QAAQ,KAAK,QAAA;AAAU,IACrB,KAAA,WAAA;AACE,MAAA,OAAOA,4BAAA,CAAW,KAAA;AAAA,IACpB,KAAA,YAAA;AAAA,IACA,KAAA,aAAA;AACE,MAAA,OAAOA,4BAAA,CAAW,MAAA;AAAA,IACpB,KAAA,cAAA;AACE,MAAA,OAAOA,4BAAA,CAAW,YAAA;AAAA,IACpB,KAAA,eAAA;AACE,MAAA,OAAOA,4BAAA,CAAW,aAAA;AAAA,IACpB,KAAA,WAAA;AAAA,IACA,KAAA,eAAA;AACE,MAAA,OAAOA,4BAAA,CAAW,IAAA;AAAA,IACpB,KAAA,eAAA;AACE,MAAA,OAAOA,4BAAA,CAAW,gBAAA;AAAA,IACpB;AACE,MAAA,OAAO,MAAA;AAAA;AAEb","file":"chunk-S6FUGBT3.cjs","sourcesContent":["/**\n * Browser-safe observability utilities.\n *\n * Functions that depend on AsyncLocalStorage (getCurrentSpan, executeWithContext,\n * executeWithContextSync) are in context-storage.ts and should only be imported\n * by server-side code.\n */\n\nimport { EntityType, SpanType } from './types';\nimport type { Span, GetOrCreateSpanOptions, AnySpan } from './types';\n\nconst entityTypeValues = new Set(Object.values(EntityType));\nlet currentSpanResolver: (() => AnySpan | undefined) | undefined;\n\nexport function setCurrentSpanResolver(resolver: (() => AnySpan | undefined) | undefined): void {\n  currentSpanResolver = resolver;\n}\n\nexport function resolveCurrentSpan(): AnySpan | undefined {\n  return currentSpanResolver?.();\n}\n\n// --- Lazy resolvers for executeWithContext / executeWithContextSync ---\n// The real implementations live in context-storage.ts (which imports AsyncLocalStorage).\n// context-storage.ts registers them at import time so that consumer code can call these\n// browser-safe wrappers without pulling async_hooks into shared chunks.\n\ntype ExecuteWithContextFn = <T>(params: { span?: AnySpan; fn: () => Promise<T> }) => Promise<T>;\ntype ExecuteWithContextSyncFn = <T>(params: { span?: AnySpan; fn: () => T }) => T;\n\nlet executeWithContextImpl: ExecuteWithContextFn | undefined;\nlet executeWithContextSyncImpl: ExecuteWithContextSyncFn | undefined;\n\nexport function setExecuteWithContext(impl: ExecuteWithContextFn): void {\n  executeWithContextImpl = impl;\n}\n\nexport function setExecuteWithContextSync(impl: ExecuteWithContextSyncFn): void {\n  executeWithContextSyncImpl = impl;\n}\n\n/**\n * Execute an async function within a span's tracing context.\n * Falls back to direct execution if no context-storage implementation is registered or no span exists.\n */\nexport async function executeWithContext<T>(params: { span?: AnySpan; fn: () => Promise<T> }): Promise<T> {\n  if (executeWithContextImpl) {\n    return executeWithContextImpl(params);\n  }\n  const { span, fn } = params;\n  if (span?.executeInContext) {\n    return span.executeInContext(fn);\n  }\n  return fn();\n}\n\n/**\n * Execute a sync function within a span's tracing context.\n * Falls back to direct execution if no context-storage implementation is registered or no span exists.\n */\nexport function executeWithContextSync<T>(params: { span?: AnySpan; fn: () => T }): T {\n  if (executeWithContextSyncImpl) {\n    return executeWithContextSyncImpl(params);\n  }\n  const { span, fn } = params;\n  if (span?.executeInContextSync) {\n    return span.executeInContextSync(fn);\n  }\n  return fn();\n}\n\n/**\n * Creates or gets a child span from existing tracing context or starts a new trace.\n * This helper consolidates the common pattern of creating spans that can either be:\n * 1. Children of an existing span (when tracingContext.currentSpan exists)\n * 2. New root spans (when no current span exists)\n *\n * @param options - Configuration object for span creation\n * @returns The created Span or undefined if tracing is disabled\n */\nexport function getOrCreateSpan<T extends SpanType>(options: GetOrCreateSpanOptions<T>): Span<T> | undefined {\n  const { type, attributes, tracingContext, requestContext, tracingOptions, ...rest } = options;\n\n  const metadata = {\n    ...(rest.metadata ?? {}),\n    ...(tracingOptions?.metadata ?? {}),\n  };\n\n  // If we have a current span, create a child span\n  if (tracingContext?.currentSpan) {\n    return tracingContext.currentSpan.createChildSpan({\n      type,\n      attributes,\n      ...rest,\n      metadata,\n      requestContext,\n    });\n  }\n\n  // Otherwise, try to create a new root span\n  const instance = options.mastra?.observability?.getSelectedInstance({ requestContext });\n\n  return instance?.startSpan<T>({\n    type,\n    attributes,\n    ...rest,\n    metadata,\n    requestContext,\n    tracingOptions,\n    traceId: tracingOptions?.traceId,\n    parentSpanId: tracingOptions?.parentSpanId,\n    customSamplerOptions: {\n      requestContext,\n      metadata,\n    },\n  });\n}\n\n/**\n * Returns the top-most non-internal span that would appear in exported tracing output.\n *\n * Public API results should use this span for trace/span correlation because internal Mastra\n * workflow spans are omitted from external exporters.\n */\nexport function getRootExportSpan(span?: AnySpan): AnySpan | undefined {\n  if (!span?.isValid) {\n    return undefined;\n  }\n\n  let current: AnySpan | undefined = span;\n  let rootExportSpan: AnySpan | undefined = span.isInternal ? undefined : span;\n\n  while (current?.parent) {\n    current = current.parent;\n\n    if (!current.isInternal) {\n      rootExportSpan = current;\n    }\n  }\n\n  return rootExportSpan;\n}\n\n/**\n * Resolves the best available entity type for a span-like record.\n *\n * Prefers an explicit `entityType` when present and valid, then falls back to the\n * span type for common observability entities.\n */\nexport function getEntityTypeForSpan(span: {\n  entityType?: string | null;\n  spanType?: SpanType | string | null;\n}): EntityType | undefined {\n  if (span.entityType && entityTypeValues.has(span.entityType as EntityType)) {\n    return span.entityType as EntityType;\n  }\n\n  switch (span.spanType) {\n    case SpanType.AGENT_RUN:\n      return EntityType.AGENT;\n    case SpanType.SCORER_RUN:\n    case SpanType.SCORER_STEP:\n      return EntityType.SCORER;\n    case SpanType.WORKFLOW_RUN:\n      return EntityType.WORKFLOW_RUN;\n    case SpanType.WORKFLOW_STEP:\n      return EntityType.WORKFLOW_STEP;\n    case SpanType.TOOL_CALL:\n    case SpanType.MCP_TOOL_CALL:\n      return EntityType.TOOL;\n    case SpanType.PROCESSOR_RUN:\n      return EntityType.OUTPUT_PROCESSOR;\n    default:\n      return undefined;\n  }\n}\n"]}