UNPKG

@mastra/core

Version:
133 lines • 6.7 kB
/** * Browser-safe observability utilities. * * Functions that depend on AsyncLocalStorage (getCurrentSpan, executeWithContext, * executeWithContextSync) are in context-storage.ts and should only be imported * by server-side code. */ import { EntityType, SpanType } from './types/index.js'; import type { Span, GetOrCreateSpanOptions, AnySpan, AnyExportedSpan, ResumeTracingContext, TracingOptions, TracingProperties } from './types/index.js'; export declare function setCurrentSpanResolver(resolver: (() => AnySpan | undefined) | undefined): void; export declare function resolveCurrentSpan(): AnySpan | undefined; /** Generate a unique id for an observability signal (log, metric, score, feedback). */ export declare function generateSignalId(): string; /** * Compute the names of tools the model can call on a single inference step, * applying `activeTools` filtering when present. Used to populate the * `availableTools` attribute on MODEL_INFERENCE spans so observers see the * post-processor tool set, which can differ per-step from the AGENT_RUN view. * * `activeTools` is treated by presence, not truthiness: an explicit empty * array means "no tools enabled for this step" and is honored as such. * Returns `[]` (not `undefined`) when `tools` is provided but empty, so a * tool-less agent still reports a definitive empty list to observers. * * @deprecated No longer used. `availableTools` is now derived from the tool * definitions serialized for the MODEL_INFERENCE `tools` attribute, which * also apply `toolChoice` and drop names that are not registered tools. */ export declare function getStepAvailableToolNames(tools?: Record<string, unknown> | undefined, activeTools?: readonly string[] | undefined): string[] | undefined; type ExecuteWithContextFn = <T>(params: { span?: AnySpan; fn: () => Promise<T>; }) => Promise<T>; type ExecuteWithContextSyncFn = <T>(params: { span?: AnySpan; fn: () => T; }) => T; export declare function setExecuteWithContext(impl: ExecuteWithContextFn): void; export declare function setExecuteWithContextSync(impl: ExecuteWithContextSyncFn): void; /** * Execute an async function within a span's tracing context. * Falls back to direct execution if no context-storage implementation is registered or no span exists. */ export declare function executeWithContext<T>(params: { span?: AnySpan; fn: () => Promise<T>; }): Promise<T>; /** * Execute a sync function within a span's tracing context. * Falls back to direct execution if no context-storage implementation is registered or no span exists. */ export declare function executeWithContextSync<T>(params: { span?: AnySpan; fn: () => T; }): T; /** * Resolve the spanId an observability signal should reference for a span. * * Signals (logs, metrics, and a suspending run's resume link) must name a span * that actually reached exporters. An internal or excluded span is never * stored, so referencing its raw id leaves the signal pointing at nothing: * log/metric span lookups 404, and a resumed run's exported children inherit a * dangling parentSpanId and land as orphans. `undefined` is a valid answer — it * omits the reference rather than pointing it at a span that does not exist. * * `getExportedSpanId` is optional on the `Span` interface, so the typeof guard * separates "this implementation predates the method" (keep the old behavior of * referencing the span's own id) from "the method ran and found nothing * exportable" (undefined). Without it the two collapse and custom span * implementations silently lose correlation. */ export declare function resolveExportedSpanId(span: { id?: string; getExportedSpanId?: () => string | undefined; } | undefined | null): string | undefined; /** * The lineage a suspending run saves so that its resume continues it (see `resumeFrom`). * Takes the run's live span, or its exported span when another process persisted it. * An unsampled run saves nothing: its no-op span has no real trace for a resume to join. */ export declare function getResumeTracingContext(span: AnySpan | AnyExportedSpan | undefined): ResumeTracingContext | undefined; /** * The context a suspending workflow run saves: its lineage plus the trace's sampling decision and * state, so a resume continues the trace as it was instead of deciding again. An unsampled run * saves only that decision, since its no-op span has no trace for a resume to join. Agent runs * save `getResumeTracingContext` alone and decide on resume. */ export declare function getWorkflowResumeTracingContext(span: AnySpan | AnyExportedSpan | undefined): ResumeTracingContext | undefined; /** * Creates or gets a child span from existing tracing context or starts a new trace. * This helper consolidates the common pattern of creating spans that can either be: * 1. Children of an existing span (when tracingContext.currentSpan exists) * 2. New root spans (when no current span exists) * * @param options - Configuration object for span creation * @returns The created Span or undefined if tracing is disabled */ export declare function getOrCreateSpan<T extends SpanType>(options: GetOrCreateSpanOptions<T>): Span<T> | undefined; /** * Builds `tracingOptions` that attach the next run as a child of a previous * run, for example an LLM judge under the run it scores. Pass the previous * run's result (or its `traceId` and `spanId`). When that run was not traced, * the given options are returned unchanged. */ export declare function nestUnderRun(run: TracingProperties, tracingOptions?: TracingOptions): TracingOptions | undefined; /** * Returns the top-most non-internal span that would appear in exported tracing output. * * Public API results should use this span for trace/span correlation because internal Mastra * workflow spans are omitted from external exporters. */ export declare function getRootExportSpan(span?: AnySpan): AnySpan | undefined; /** * Resolves the best available entity type for a span-like record. * * Prefers an explicit `entityType` when present and valid, then falls back to the * span type for common observability entities. */ export declare function getEntityTypeForSpan(span: { entityType?: string | null; spanType?: SpanType | string | null; }): EntityType | undefined; /** * Time to first chunk in milliseconds for any span whose attributes carry * `completionStartTime`, measured from that span's own start. Undefined when * the span never saw a chunk. */ export declare function getTimeToFirstChunkMs(span: { startTime: Date | string; attributes?: object | undefined; }): number | undefined; export {}; //# sourceMappingURL=utils.d.ts.map