/** * Search Result Processing Utilities * * Utilities for post-processing search results to reduce token count: * - Whitespace trimming: Remove leading/trailing blank lines from chunks * - Same-file deduplication: Merge adjacent/overlapping chunks from the same file * - Compact formatting: Shorter field names and combined location info * * These optimizations can reduce token usage by 25-35%. */ /** * Generic search result interface for processing */ export interface SearchResultItem { path: string; text: string; score: number; startLine: number; endLine: number; } /** * Trim leading and trailing blank lines from chunk text. * * Removes: * - Leading blank lines (lines containing only whitespace) * - Trailing blank lines (lines containing only whitespace) * * Preserves: * - Internal blank lines * - Indentation within the content * * @param text - The chunk text to trim * @returns The trimmed text * * @example * ```typescript * const input = "\n\n function foo() {\n return 42;\n }\n\n"; * const output = trimChunkWhitespace(input); * // output: " function foo() {\n return 42;\n }" * ``` */ export declare function trimChunkWhitespace(text: string): string; /** * Check if two line ranges overlap or are adjacent. * * Ranges are considered mergeable if: * - They overlap (any shared lines) * - They are adjacent (end of one is immediately before start of another) * * @param range1Start - Start line of first range * @param range1End - End line of first range * @param range2Start - Start line of second range * @param range2End - End line of second range * @returns True if ranges can be merged */ export declare function areRangesMergeable(range1Start: number, range1End: number, range2Start: number, range2End: number): boolean; /** * Deduplicate search results by merging adjacent/overlapping chunks from the same file. * * Algorithm: * 1. Group results by file path * 2. For each file, sort chunks by start line * 3. Merge adjacent or overlapping chunks * 4. Use the highest score among merged chunks * 5. Flatten back to a single list, sorted by score * * This reduces token usage by: * - Eliminating redundant file path repetition * - Removing overlapping content * - Combining related code context * * @param results - Search results to deduplicate * @returns Deduplicated results */ export declare function deduplicateSameFileResults(results: T[]): T[]; /** * Process search results with both trimming and deduplication. * * This is the main entry point for optimizing search results. * Applies both optimizations in order: * 1. Trim whitespace from each chunk * 2. Deduplicate same-file results * * @param results - Raw search results * @returns Optimized results with reduced token count */ export declare function processSearchResults(results: T[]): T[]; /** * Compact search result with shortened field names. * * Field name mappings: * - l (loc): Combined path + line range (e.g., "src/errors/index.ts:317-355") * - t (text): Chunk content text * - s (score): Similarity score rounded to 2 decimal places */ export interface CompactSearchResult { /** Location: path:startLine-endLine */ l: string; /** Text content */ t: string; /** Score (rounded to 2 decimal places) */ s: number; } /** * Compact search output with shortened field names. * * Field name mappings: * - r (results): Array of compact results * - n (count): Total number of results * - ms (searchTimeMs): Search time in milliseconds * - w (warning): Optional warning message */ export interface CompactSearchOutput { /** Results array */ r: CompactSearchResult[]; /** Count of results */ n: number; /** Search time in milliseconds */ ms: number; /** Optional warning */ w?: string; } /** * Format a single search result in compact format. * * Combines path and line numbers into a single `loc` field, * rounds score to 2 decimal places, and uses short field names. * * @param result - The search result to format * @returns Compact formatted result * * @example * ```typescript * const compact = formatCompactResult({ * path: 'src/utils/hash.ts', * text: 'function hash() { ... }', * score: 0.87654321, * startLine: 10, * endLine: 25, * }); * // Result: { l: 'src/utils/hash.ts:10-25', t: 'function hash() { ... }', s: 0.88 } * ``` */ export declare function formatCompactResult(result: SearchResultItem): CompactSearchResult; /** * Format search results array in compact format. * * @param results - Array of search results * @returns Array of compact formatted results */ export declare function formatCompactResults(results: SearchResultItem[]): CompactSearchResult[]; /** * Format the full search output in compact format. * * @param results - Processed search results * @param searchTimeMs - Search time in milliseconds * @param warning - Optional warning message * @returns Compact formatted output * * @example * ```typescript * const output = formatCompactOutput(results, 45); * // Result: { r: [...], n: 10, ms: 45 } * ``` */ export declare function formatCompactOutput(results: SearchResultItem[], searchTimeMs: number, warning?: string): CompactSearchOutput; //# sourceMappingURL=searchResultProcessing.d.ts.map