@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
77 lines • 2.88 kB
TypeScript
import { IOptimizationModule, OptimizationResult } from './IOptimizationModule.js';
import { ITokenCounter } from '../interfaces/ITokenCounter.js';
/**
* Whitespace optimization module.
*
* This lightweight optimization module removes excessive whitespace from text
* while preserving readability and structure. It's an excellent example of a
* simple, composable optimization that can be combined with other modules.
*
* Optimizations applied:
* - Collapses multiple spaces into single spaces
* - Removes trailing whitespace from lines
* - Removes leading whitespace from lines (configurable)
* - Collapses multiple newlines into double newlines (preserves paragraph breaks)
* - Trims leading and trailing whitespace
*
* This module is particularly effective for:
* - Code snippets with inconsistent formatting
* - Copy-pasted content with extra whitespace
* - Generated text with formatting artifacts
* - Long documents where whitespace adds up
*
* @example
* ```typescript
* const tokenCounter = new TokenCounter();
* const whitespaceModule = new WhitespaceOptimizationModule(tokenCounter, {
* preserveIndentation: true, // Keep leading spaces
* maxConsecutiveNewlines: 2 // Allow up to 2 newlines
* });
*
* const result = await whitespaceModule.apply(textWithExtraSpaces);
* console.log(`Removed ${result.savings} tokens of whitespace`);
* ```
*/
export declare class WhitespaceOptimizationModule implements IOptimizationModule {
private readonly tokenCounter;
private readonly options?;
readonly name = "whitespace-optimization";
/**
* Create a whitespace optimization module.
*
* @param tokenCounter - Token counter for measuring savings
* @param options - Configuration options
*/
constructor(tokenCounter: ITokenCounter, options?: {
/**
* Preserve leading whitespace (indentation) in lines
* @default false
*/
preserveIndentation?: boolean;
/**
* Maximum consecutive newlines to allow
* @default 2
*/
maxConsecutiveNewlines?: number;
/**
* Preserve code block formatting (content between ``` markers)
* @default true
*/
preserveCodeBlocks?: boolean;
} | undefined);
/**
* Apply whitespace optimization to the input text.
*
* This method:
* 1. Counts tokens in the original text
* 2. Removes excessive whitespace while preserving structure
* 3. Optionally preserves code blocks and indentation
* 4. Counts tokens in the optimized text
* 5. Returns detailed results with statistics
*
* @param text - The text to optimize
* @returns Optimization result with whitespace statistics
*/
apply(text: string): Promise<OptimizationResult>;
}
//# sourceMappingURL=WhitespaceOptimizationModule.d.ts.map