UNPKG

@mastra/core

Version:
89 lines (88 loc) 3.36 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_utils = require("../utils-CNiGU0Uf.cjs"); //#region src/observability/context-storage.ts /** * Ambient storage for the current span. Populated by executeWithContext/executeWithContextSync * so that infrastructure code (e.g. DualLogger) can resolve the active span without * being passed it explicitly. */ const spanContextStorage = new (require("async_hooks")).AsyncLocalStorage(); /** * Returns the current span from AsyncLocalStorage, if one is active. * Used by DualLogger to forward logs to a span-correlated loggerVNext. */ function getCurrentSpan() { return spanContextStorage.getStore(); } let initialized = false; /** * Registers the AsyncLocalStorage-backed context resolvers with the browser-safe * utils module. Must be called once at startup (e.g. from the Mastra constructor). * * This is an explicit initialization function rather than a top-level side-effect * because the package declares `"sideEffects": false` and bundler tree-shaking * strips bare side-effect imports. */ function initContextStorage() { if (initialized) return; initialized = true; require_utils.setCurrentSpanResolver(getCurrentSpan); require_utils.setExecuteWithContext(executeWithContext); require_utils.setExecuteWithContextSync(executeWithContextSync); } /** * Execute an async function within the span's tracing context if available. * Falls back to direct execution if no span exists. * * When a bridge is configured, this enables auto-instrumented operations * (HTTP requests, database queries, etc.) to be properly nested under the * current span in the external tracing system. * * @param span - The span to use as context (or undefined to execute without context) * @param fn - The async function to execute * @returns The result of the function execution * * @example * ```typescript * const result = await executeWithContext(llmSpan, async () => * model.generateText(args) * ); * ``` */ async function executeWithContext(params) { const { span, fn } = params; const wrappedFn = span ? () => spanContextStorage.run(span, fn) : fn; if (span?.executeInContext) return span.executeInContext(wrappedFn); return wrappedFn(); } /** * Execute a synchronous function within the span's tracing context if available. * Falls back to direct execution if no span exists. * * When a bridge is configured, this enables auto-instrumented operations * (HTTP requests, database queries, etc.) to be properly nested under the * current span in the external tracing system. * * @param span - The span to use as context (or undefined to execute without context) * @param fn - The synchronous function to execute * @returns The result of the function execution * * @example * ```typescript * const result = executeWithContextSync(llmSpan, () => * model.streamText(args) * ); * ``` */ function executeWithContextSync(params) { const { span, fn } = params; const wrappedFn = span ? () => spanContextStorage.run(span, fn) : fn; if (span?.executeInContextSync) return span.executeInContextSync(wrappedFn); return wrappedFn(); } //#endregion exports.executeWithContext = executeWithContext; exports.executeWithContextSync = executeWithContextSync; exports.getCurrentSpan = getCurrentSpan; exports.initContextStorage = initContextStorage; //# sourceMappingURL=context-storage.cjs.map