UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

111 lines 3.38 kB
/** * Base Exporter for AI Tracing * * Provides common functionality shared by all AI tracing exporters: * - Logger initialization with proper Mastra logger support * - Disabled state management * - Graceful shutdown lifecycle */ import { LogLevel } from '../../logger/index.js'; import type { IMastraLogger } from '../../logger/index.js'; import type { AITracingEvent, AITracingExporter } from '../types.js'; /** * Base configuration that all exporters should support */ export interface BaseExporterConfig { /** Optional Mastra logger instance */ logger?: IMastraLogger; /** Log level for the exporter (defaults to INFO) - accepts both enum and string */ logLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error'; } /** * Abstract base class for AI tracing exporters * * Handles common concerns: * - Logger setup with proper Mastra logger * - Disabled state management * - Basic lifecycle methods * * @example * ```typescript * class MyExporter extends BaseExporter { * name = 'my-exporter'; * * constructor(config: MyExporterConfig) { * super(config); * * if (!config.apiKey) { * this.setDisabled('Missing API key'); * return; * } * * // Initialize exporter-specific logic * } * * async _exportEvent(event: AITracingEvent): Promise<void> { * // Export logic * } * } * ``` */ export declare abstract class BaseExporter implements AITracingExporter { /** Exporter name - must be implemented by subclasses */ abstract name: string; /** Mastra logger instance */ protected logger: IMastraLogger; /** Whether this exporter is disabled */ protected isDisabled: boolean; /** * Initialize the base exporter with logger */ constructor(config?: BaseExporterConfig); /** * Set the logger for the exporter (called by Mastra/AITracing during initialization) */ __setLogger(logger: IMastraLogger): void; /** * Convert string log level to LogLevel enum */ private resolveLogLevel; /** * Mark the exporter as disabled and log a message * * @param reason - Reason why the exporter is disabled */ protected setDisabled(reason: string): void; /** * Export a tracing event * * This method checks if the exporter is disabled before calling _exportEvent. * Subclasses should implement _exportEvent instead of overriding this method. */ exportEvent(event: AITracingEvent): Promise<void>; /** * Export a tracing event - must be implemented by subclasses * * This method is called by exportEvent after checking if the exporter is disabled. */ protected abstract _exportEvent(event: AITracingEvent): Promise<void>; /** * Optional initialization hook called after Mastra is fully configured */ init?(_config?: any): void; /** * Optional method to add scores to traces */ addScoreToTrace?(_args: { traceId: string; spanId?: string; score: number; reason?: string; scorerName: string; metadata?: Record<string, any>; }): Promise<void>; /** * Shutdown the exporter and clean up resources * * Default implementation just logs. Override to add custom cleanup. */ shutdown(): Promise<void>; } //# sourceMappingURL=base.d.ts.map