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

152 lines 4.27 kB
/** * Smart Write Tool - 85% Token Reduction * * Achieves token reduction through: * 1. Verify before write (skip if content identical) * 2. Atomic operations (temporary file + rename) * 3. Automatic formatting (prettier/eslint integration) * 4. Change tracking (report only changes made) * * Target: 85% reduction vs standard write operations */ import { CacheEngine } from '../../core/cache-engine.js'; import { TokenCounter } from '../../core/token-counter.js'; import { MetricsCollector } from '../../core/metrics.js'; export interface SmartWriteOptions { verifyBeforeWrite?: boolean; createBackup?: boolean; atomic?: boolean; tempDir?: string; autoFormat?: boolean; formatType?: 'prettier' | 'eslint' | 'none'; trackChanges?: boolean; returnDiff?: boolean; updateCache?: boolean; ttl?: number; createDirectories?: boolean; encoding?: BufferEncoding; mode?: number; } export interface SmartWriteResult { success: boolean; path: string; operation: 'created' | 'updated' | 'unchanged' | 'failed'; metadata: { bytesWritten: number; originalSize: number; wasFormatted: boolean; linesChanged: number; tokensSaved: number; tokenCount: number; originalTokenCount: number; compressionRatio: number; atomic: boolean; verified: boolean; duration: number; }; diff?: { added: string[]; removed: string[]; unchanged: number; unifiedDiff: string; }; error?: string; } export declare class SmartWriteTool { private cache; private tokenCounter; private metrics; constructor(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector); /** * Smart write with verification, atomic operations, and change tracking */ write(filePath: string, content: string, options?: SmartWriteOptions): Promise<SmartWriteResult>; /** * Atomic write using temporary file and rename */ private performWrite; /** * Calculate diff between old and new content */ private calculateDiff; /** * Format content using prettier or eslint */ private formatContent; /** * Basic JavaScript/TypeScript formatting */ private formatJavaScript; /** * Format JSON with 2-space indentation */ private formatJSON; /** * Batch write multiple files */ writeMany(files: Array<{ path: string; content: string; options?: SmartWriteOptions; }>): Promise<SmartWriteResult[]>; /** * Get write statistics */ getStats(): { totalWrites: number; unchangedSkips: number; bytesWritten: number; totalTokensSaved: number; averageReduction: number; }; } /** * Get smart write tool instance */ export declare function getSmartWriteTool(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector): SmartWriteTool; /** * CLI function - Creates resources and uses factory */ export declare function runSmartWrite(filePath: string, content: string, options?: SmartWriteOptions): Promise<SmartWriteResult>; /** * MCP Tool Definition */ export declare const SMART_WRITE_TOOL_DEFINITION: { name: string; description: string; inputSchema: { type: string; properties: { path: { type: string; description: string; }; content: { type: string; description: string; }; verifyBeforeWrite: { type: string; description: string; default: boolean; }; atomic: { type: string; description: string; default: boolean; }; autoFormat: { type: string; description: string; default: boolean; }; returnDiff: { type: string; description: string; default: boolean; }; }; required: string[]; }; }; //# sourceMappingURL=smart-write.d.ts.map