{"version":3,"sources":["../src/observability/context-storage.ts"],"names":["AsyncLocalStorage","setCurrentSpanResolver","setExecuteWithContext","setExecuteWithContextSync"],"mappings":";;;;;AAUA,IAAM,kBAAA,GAAqB,IAAIA,6BAAA,EAA2B;AAMnD,SAAS,cAAA,GAAsC;AACpD,EAAA,OAAO,mBAAmB,QAAA,EAAS;AACrC;AAEA,IAAI,WAAA,GAAc,KAAA;AAUX,SAAS,kBAAA,GAA2B;AACzC,EAAA,IAAI,WAAA,EAAa;AACjB,EAAA,WAAA,GAAc,IAAA;AACd,EAAAC,wCAAA,CAAuB,cAAc,CAAA;AACrC,EAAAC,uCAAA,CAAsB,kBAAkB,CAAA;AACxC,EAAAC,2CAAA,CAA0B,sBAAsB,CAAA;AAClD;AAqBA,eAAsB,mBAAsB,MAAA,EAA8D;AACxG,EAAA,MAAM,EAAE,IAAA,EAAM,EAAA,EAAG,GAAI,MAAA;AAGrB,EAAA,MAAM,YAAY,IAAA,GAAO,MAAM,mBAAmB,GAAA,CAAI,IAAA,EAAM,EAAE,CAAA,GAAI,EAAA;AAElE,EAAA,IAAI,MAAM,gBAAA,EAAkB;AAC1B,IAAA,OAAO,IAAA,CAAK,iBAAiB,SAAS,CAAA;AAAA,EACxC;AAEA,EAAA,OAAO,SAAA,EAAU;AACnB;AAqBO,SAAS,uBAA0B,MAAA,EAA4C;AACpF,EAAA,MAAM,EAAE,IAAA,EAAM,EAAA,EAAG,GAAI,MAAA;AAGrB,EAAA,MAAM,YAAY,IAAA,GAAO,MAAM,mBAAmB,GAAA,CAAI,IAAA,EAAM,EAAE,CAAA,GAAI,EAAA;AAElE,EAAA,IAAI,MAAM,oBAAA,EAAsB;AAC9B,IAAA,OAAO,IAAA,CAAK,qBAAqB,SAAS,CAAA;AAAA,EAC5C;AAEA,EAAA,OAAO,SAAA,EAAU;AACnB","file":"chunk-K5X6DWHC.cjs","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 tsup's tree-shaking\n * (`preset: 'smallest'`) 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"]}