UNPKG

@ooples/token-optimizer-mcp

Version:

Intelligent context window optimization for Claude Code - store content externally via caching and compression, freeing up your context window for what matters

192 lines 5.83 kB
/** * Smart SQL Tool - 83% Token Reduction * * SQL query analyzer with intelligent features: * - Query analysis (type, tables, complexity) * - Execution plan analysis (EXPLAIN) * - Query validation and syntax checking * - Optimization suggestions (indexes, query rewrites) * - Query history tracking * - Token-optimized output with intelligent caching */ import { CacheEngine } from '../../core/cache-engine.js'; import { TokenCounter } from '../../core/token-counter.js'; import { MetricsCollector } from '../../core/metrics.js'; export interface SmartSqlOptions { /** * SQL query to analyze */ query?: string; /** * Action to perform */ action?: 'analyze' | 'explain' | 'validate' | 'optimize' | 'history'; /** * Database type (for syntax-specific validation) */ database?: 'postgresql' | 'mysql' | 'sqlite' | 'sqlserver'; /** * Schema name (optional) */ schema?: string; /** * Include execution plan analysis */ includeExecutionPlan?: boolean; /** * Cache TTL in seconds (default: 300 = 5 minutes) */ ttl?: number; /** * Force fresh analysis (bypass cache) */ force?: boolean; } export interface QueryAnalysis { queryType: 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'CREATE' | 'ALTER' | 'DROP' | 'UNKNOWN'; tables: string[]; columns: string[]; complexity: 'low' | 'medium' | 'high'; estimatedCost: number; } export interface ExecutionPlanStep { operation: string; table: string; cost: number; rows: number; } export interface ExecutionPlan { steps: ExecutionPlanStep[]; totalCost: number; } export interface OptimizationSuggestion { type: 'index' | 'rewrite' | 'schema' | 'performance'; severity: 'info' | 'warning' | 'critical'; message: string; optimizedQuery?: string; } export interface Optimization { suggestions: OptimizationSuggestion[]; potentialSpeedup: string; } export interface ValidationError { line: number; column: number; message: string; } export interface Validation { isValid: boolean; errors: string[]; warnings: string[]; } export interface HistoryEntry { query: string; timestamp: string; executionTime: number; rowsAffected: number; } export interface SmartSqlOutput { analysis?: QueryAnalysis; executionPlan?: ExecutionPlan; optimization?: Optimization; validation?: Validation; history?: HistoryEntry[]; metrics: { originalTokens: number; compactedTokens: number; reductionPercentage: number; cacheHit: boolean; }; } export declare class SmartSql { private cache; private tokenCounter; private metrics; private static readonly MAX_TOP_ITEMS; private static readonly MAX_RECENT_QUERIES; private static readonly MAX_QUERY_DISPLAY_LENGTH; constructor(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector); run(options: SmartSqlOptions): Promise<SmartSqlOutput>; private analyzeQuery; private performAnalysis; private detectQueryType; private extractTables; private extractColumns; private calculateComplexity; private estimateCost; private generateExecutionPlan; private generateOptimizations; private validateQuery; private getQueryHistory; private transformOutput; /** * Format output based on the type of result and cache status * * Formatting percentages shown in headers (e.g., '95%', '86%', '80%') * represent estimated token reduction compared to a verbose baseline format. * These percentages optimize for Claude's context window by showing only * essential information while maintaining clarity. * * Percentage breakdown: * - Cached (95%): Minimal output for cache hits * - Analysis/Optimization (86%): Moderate reduction for core data * - Plan/Validation/History (80%): Balanced reduction for detailed data */ formatOutput(result: SmartSqlOutput): string; /** * Formats output with token reduction percentages shown in headers. * Percentages (e.g., 95%, 86%, 80%) represent token reduction compared * to a verbose baseline format, optimizing for Claude's context window. */ private formatCachedOutput; private formatPlanOutput; private formatOptimizationOutput; private formatValidationOutput; private formatHistoryOutput; private formatAnalysisOutput; private generateCacheKey; private getCachedResult; private cacheResult; } export declare function getSmartSql(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector): SmartSql; export declare function runSmartSql(options: SmartSqlOptions): Promise<string>; export declare const SMART_SQL_TOOL_DEFINITION: { name: string; description: string; inputSchema: { type: "object"; properties: { query: { type: "string"; description: string; }; action: { type: "string"; enum: string[]; description: string; }; database: { type: "string"; enum: string[]; description: string; }; schema: { type: "string"; description: string; }; includeExecutionPlan: { type: "boolean"; description: string; }; force: { type: "boolean"; description: string; }; ttl: { type: "number"; description: string; }; }; }; }; //# sourceMappingURL=smart-sql.d.ts.map