/** * Metrics Accumulator * * In-memory, Map-based accumulator for tool call telemetry. * No external dependencies. Exposes a singleton `globalMetrics` instance * that the interceptor and resource handlers share. * * Per-tool data collected: * - callCount — total invocations * - errorCount — invocations that returned {success: false} or threw * - totalDurationMs — sum of wall-clock durations * - totalInputTokens — sum of estimated input token counts * - totalOutputTokens — sum of estimated output token counts * - lastCalledAt — ISO timestamp of most recent call * - latency percentiles (p50/p95/p99) via LatencyTracker */ import type { LatencyPercentiles } from './latency-tracker.js'; export interface ToolMetrics { callCount: number; errorCount: number; totalDurationMs: number; totalInputTokens: number; totalOutputTokens: number; lastCalledAt: string | null; } export interface ResourceMetrics { readCount: number; errorCount: number; lastReadAt: string | null; } export interface MetricsSummary { totalCalls: number; totalErrors: number; totalDurationMs: number; totalInputTokens: number; totalOutputTokens: number; totalResourceReads: number; upSince: string; toolBreakdown: Record; resourceBreakdown: Record; } export interface SystemMetrics { upSince: string; uptimeSeconds: number; processMemoryMb: number; nodeVersion: string; platform: string; } export declare class MetricsAccumulator { private readonly toolData; private readonly resourceData; private readonly deprecationWarnings; private readonly latencyTracker; readonly upSince: string; private readonly startTime; constructor(maxLatencySamplesPerTool?: number); /** * Record a deprecation warning to be surfaced in the briefing. */ recordDeprecationWarning(warning: string): void; /** * Retrieve active deprecation warnings. */ getDeprecationWarnings(): string[]; /** * Record a single tool invocation result. */ record(opts: { toolName: string; durationMs: number; inputTokens: number; outputTokens: number; isError: boolean; }): void; /** * Record a single resource read. */ recordResourceRead(uri: string, isError?: boolean): void; /** * Aggregate summary across all recorded tools and resources. */ getSummary(): MetricsSummary; /** * Per-tool token usage breakdown, sorted by total output tokens desc. */ getTokenBreakdown(): { toolName: string; inputTokens: number; outputTokens: number; callCount: number; avgOutputTokens: number; }[]; /** * Per-resource read frequency breakdown, sorted by total reads desc. */ getResourceBreakdown(): { uri: string; readCount: number; errorCount: number; lastReadAt: string | null; }[]; /** * Per-user call counts, sourced from a user tag injected by the interceptor. * When no user tracking is configured this returns an empty breakdown. */ getUserBreakdown(): Record; private readonly userCounts; /** * Per-tool latency percentile summary, sorted by p99 descending. */ getLatencySummary(): LatencyPercentiles[]; recordUser(user: string): void; /** * System-level metrics snapshot. */ getSystemMetrics(): SystemMetrics; /** * Reset all accumulated data (primarily useful in tests). */ reset(): void; /** * Export the current metrics state as a JSON string. */ exportState(): string; /** * Import metrics state from a JSON string, merging with current state. */ importState(jsonState: string): void; /** * Save metrics to a file asynchronously. */ saveToFile(path: string): Promise; /** * Load metrics from a file asynchronously. */ loadFromFile(path: string): Promise; } //# sourceMappingURL=metrics.d.ts.map