UNPKG

@mastra/core

Version:
86 lines (85 loc) 3.23 kB
import { d as setExecuteWithContextSync, l as setCurrentSpanResolver, u as setExecuteWithContext } from "../utils-DxsDNzD2.js"; import { AsyncLocalStorage } from "async_hooks"; //#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 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; setCurrentSpanResolver(getCurrentSpan); setExecuteWithContext(executeWithContext); 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 export { executeWithContext, executeWithContextSync, getCurrentSpan, initContextStorage }; //# sourceMappingURL=context-storage.js.map