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

196 lines 6.18 kB
/** * Smart Edit Tool - 90% Token Reduction * * Achieves token reduction through: * 1. Line-based editing (edit only specific ranges, not full file) * 2. Return only diffs (show changes, not entire file content) * 3. Pattern-based replacement (regex/search-replace) * 4. Multi-edit batching (apply multiple edits in one operation) * 5. Verification before commit (preview changes before applying) * * Target: 90% reduction vs reading full file + writing changes */ import { CacheEngine } from '../../core/cache-engine.js'; import { TokenCounter } from '../../core/token-counter.js'; import { MetricsCollector } from '../../core/metrics.js'; export interface EditOperation { type: 'replace' | 'insert' | 'delete'; startLine: number; endLine?: number; content?: string; pattern?: string | RegExp; replacement?: string; } export interface SmartEditOptions { verifyBeforeApply?: boolean; dryRun?: boolean; createBackup?: boolean; batchEdits?: boolean; returnDiff?: boolean; contextLines?: number; updateCache?: boolean; ttl?: number; encoding?: BufferEncoding; } export interface SmartEditResult { success: boolean; path: string; operation: 'applied' | 'preview' | 'unchanged' | 'failed'; metadata: { editsApplied: number; linesChanged: number; originalLines: number; finalLines: number; tokensSaved: number; tokenCount: number; originalTokenCount: number; compressionRatio: number; duration: number; verified: boolean; wasBackedUp: boolean; }; diff?: { added: string[]; removed: string[]; unchanged: number; unifiedDiff: string; }; preview?: string; error?: string; } export declare class SmartEditTool { private cache; private tokenCounter; private metrics; constructor(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector); /** * Smart edit with line-based operations and diff-only output */ edit(filePath: string, operations: EditOperation | EditOperation[], options?: SmartEditOptions): Promise<SmartEditResult>; /** * Validate edit operations */ private validateOperations; /** * Apply edit operations to lines */ private applyEdits; /** * Calculate diff between old and new content */ private calculateDiff; /** * Get edit statistics */ getStats(): { totalEdits: number; unchangedSkips: number; totalTokensSaved: number; averageReduction: number; }; } /** * Get smart edit tool instance */ export declare function getSmartEditTool(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector): SmartEditTool; /** * CLI function - Creates resources and uses factory */ export declare function runSmartEdit(filePath: string, operations: EditOperation | EditOperation[], options?: SmartEditOptions): Promise<SmartEditResult>; /** * MCP Tool Definition */ export declare const SMART_EDIT_TOOL_DEFINITION: { name: string; description: string; inputSchema: { type: string; properties: { path: { type: string; description: string; }; operations: { oneOf: ({ type: string; properties: { type: { type: string; enum: string[]; description: string; }; startLine: { type: string; description: string; }; endLine: { type: string; description: string; }; content: { type: string; description: string; }; pattern: { type: string; description: string; }; replacement: { type: string; description: string; }; }; required: string[]; items?: undefined; } | { type: string; items: { type: string; properties: { type: { type: string; enum: string[]; }; startLine: { type: string; }; endLine: { type: string; }; content: { type: string; }; pattern: { type: string; }; replacement: { type: string; }; }; required: string[]; }; properties?: undefined; required?: undefined; })[]; description: string; }; dryRun: { type: string; description: string; default: boolean; }; returnDiff: { type: string; description: string; default: boolean; }; createBackup: { type: string; description: string; default: boolean; }; }; required: string[]; }; }; //# sourceMappingURL=smart-edit.d.ts.map