{"version":3,"file":"context-storage.cjs","names":["AsyncLocalStorage"],"sources":["../../src/observability/context-storage.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nimport type { AnySpan } from './types';\nimport { setCurrentSpanResolver, setExecuteWithContext, setExecuteWithContextSync } from './utils';\n\n/**\n * Ambient storage for the current span. Populated by executeWithContext/executeWithContextSync\n * so that infrastructure code (e.g. DualLogger) can resolve the active span without\n * being passed it explicitly.\n */\nconst spanContextStorage = new AsyncLocalStorage<AnySpan>();\n\n/**\n * Returns the current span from AsyncLocalStorage, if one is active.\n * Used by DualLogger to forward logs to a span-correlated loggerVNext.\n */\nexport function getCurrentSpan(): AnySpan | undefined {\n  return spanContextStorage.getStore();\n}\n\nlet initialized = false;\n\n/**\n * Registers the AsyncLocalStorage-backed context resolvers with the browser-safe\n * utils module. Must be called once at startup (e.g. from the Mastra constructor).\n *\n * This is an explicit initialization function rather than a top-level side-effect\n * because the package declares `\"sideEffects\": false` and bundler tree-shaking\n * strips bare side-effect imports.\n */\nexport function initContextStorage(): void {\n  if (initialized) return;\n  initialized = true;\n  setCurrentSpanResolver(getCurrentSpan);\n  setExecuteWithContext(executeWithContext);\n  setExecuteWithContextSync(executeWithContextSync);\n}\n\n/**\n * Execute an async function within the span's tracing context if available.\n * Falls back to direct execution if no span exists.\n *\n * When a bridge is configured, this enables auto-instrumented operations\n * (HTTP requests, database queries, etc.) to be properly nested under the\n * current span in the external tracing system.\n *\n * @param span - The span to use as context (or undefined to execute without context)\n * @param fn - The async function to execute\n * @returns The result of the function execution\n *\n * @example\n * ```typescript\n * const result = await executeWithContext(llmSpan, async () =>\n *   model.generateText(args)\n * );\n * ```\n */\nexport async function executeWithContext<T>(params: { span?: AnySpan; fn: () => Promise<T> }): Promise<T> {\n  const { span, fn } = params;\n\n  // Wrap fn so the span is available via getCurrentSpan() inside the async context.\n  const wrappedFn = span ? () => spanContextStorage.run(span, fn) : fn;\n\n  if (span?.executeInContext) {\n    return span.executeInContext(wrappedFn);\n  }\n\n  return wrappedFn();\n}\n\n/**\n * Execute a synchronous function within the span's tracing context if available.\n * Falls back to direct execution if no span exists.\n *\n * When a bridge is configured, this enables auto-instrumented operations\n * (HTTP requests, database queries, etc.) to be properly nested under the\n * current span in the external tracing system.\n *\n * @param span - The span to use as context (or undefined to execute without context)\n * @param fn - The synchronous function to execute\n * @returns The result of the function execution\n *\n * @example\n * ```typescript\n * const result = executeWithContextSync(llmSpan, () =>\n *   model.streamText(args)\n * );\n * ```\n */\nexport function executeWithContextSync<T>(params: { span?: AnySpan; fn: () => T }): T {\n  const { span, fn } = params;\n\n  // Wrap fn so the span is available via getCurrentSpan() inside the sync context.\n  const wrappedFn = span ? () => spanContextStorage.run(span, fn) : fn;\n\n  if (span?.executeInContextSync) {\n    return span.executeInContextSync(wrappedFn);\n  }\n\n  return wrappedFn();\n}\n"],"mappings":";;;;;;;;AAUA,MAAM,qBAAqB,0BAAIA,EAAAA,CAAAA,kBAA2B;;;;;AAM1D,SAAgB,iBAAsC;CACpD,OAAO,mBAAmB,SAAS;AACrC;AAEA,IAAI,cAAc;;;;;;;;;AAUlB,SAAgB,qBAA2B;CACzC,IAAI,aAAa;CACjB,cAAc;CACd,cAAA,uBAAuB,cAAc;CACrC,cAAA,sBAAsB,kBAAkB;CACxC,cAAA,0BAA0B,sBAAsB;AAClD;;;;;;;;;;;;;;;;;;;;AAqBA,eAAsB,mBAAsB,QAA8D;CACxG,MAAM,EAAE,MAAM,OAAO;CAGrB,MAAM,YAAY,aAAa,mBAAmB,IAAI,MAAM,EAAE,IAAI;CAElE,IAAI,MAAM,kBACR,OAAO,KAAK,iBAAiB,SAAS;CAGxC,OAAO,UAAU;AACnB;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,uBAA0B,QAA4C;CACpF,MAAM,EAAE,MAAM,OAAO;CAGrB,MAAM,YAAY,aAAa,mBAAmB,IAAI,MAAM,EAAE,IAAI;CAElE,IAAI,MAAM,sBACR,OAAO,KAAK,qBAAqB,SAAS;CAG5C,OAAO,UAAU;AACnB"}