| "use strict"; |
| |
| |
| |
| |
| |
| |
| Object.defineProperty(exports, "__esModule", { value: true }); |
| exports.getFileSummary = getFileSummary; |
| exports.getFileSummaryString = getFileSummaryString; |
| exports.clearFileCache = clearFileCache; |
| exports.getFileCacheStats = getFileCacheStats; |
| exports.isFileCached = isFileCached; |
| |
| |
| |
| function hashContent(content) { |
| let hash = 0; |
| for (let i = 0; i < content.length; i++) { |
| hash = ((hash << 5) - hash + content.charCodeAt(i)) | 0; |
| } |
| return Math.abs(hash).toString(36); |
| } |
| |
| |
| |
| function extractSummary(content, language = 'typescript') { |
| const lines = content.split('\n'); |
| const summary = []; |
| |
| const functionRegex = /(?:function|const|let|var)\s+(\w+)\s*(?:=\s*(?:async\s*)?\(|\()/; |
| const functions = []; |
| |
| const importRegex = /import\s+.*from\s+['"]([^'"]+)['"]/; |
| const imports = []; |
| |
| const exportRegex = /export\s+(?:const|function|class|interface)\s+(\w+)/; |
| const exports = []; |
| for (const line of lines) { |
| const funcMatch = line.match(functionRegex); |
| if (funcMatch) |
| functions.push(funcMatch[1]); |
| const importMatch = line.match(importRegex); |
| if (importMatch) |
| imports.push(importMatch[1]); |
| const exportMatch = line.match(exportRegex); |
| if (exportMatch) |
| exports.push(exportMatch[1]); |
| } |
| |
| if (functions.length > 0) { |
| summary.push(`Functions: ${functions.slice(0, 5).join(', ')}`); |
| } |
| if (imports.length > 0) { |
| summary.push(`Imports: ${imports.slice(0, 5).join(', ')}`); |
| } |
| if (exports.length > 0) { |
| summary.push(`Exports: ${exports.join(', ')}`); |
| } |
| |
| if (content.includes('useState') || content.includes('useEffect')) { |
| summary.push('React hooks detected'); |
| } |
| if (content.includes('async') || content.includes('await')) { |
| summary.push('Async operations'); |
| } |
| if (content.includes('fetch') || content.includes('axios')) { |
| summary.push('HTTP requests'); |
| } |
| return summary.join(' | '); |
| } |
| |
| |
| |
| class FileCache { |
| cache = new Map(); |
| TTL_MS = 7 * 24 * 60 * 60 * 1000; |
| MAX_CACHE_SIZE = 1000; |
| |
| |
| |
| get(path, content) { |
| const cached = this.cache.get(path); |
| if (!cached) |
| return null; |
| |
| if (Date.now() - cached.lastAnalyzed > this.TTL_MS) { |
| this.cache.delete(path); |
| return null; |
| } |
| |
| const currentHash = hashContent(content); |
| if (cached.hash !== currentHash) { |
| this.cache.delete(path); |
| return null; |
| } |
| return cached; |
| } |
| |
| |
| |
| set(path, content, language = 'typescript') { |
| const summary = extractSummary(content, language); |
| const lines = content.split('\n'); |
| const functions = []; |
| const imports = []; |
| const exports = []; |
| for (const line of lines) { |
| const funcMatch = line.match(/(?:function|const|let|var)\s+(\w+)\s*(?:=\s*(?:async\s*)?\(|\()/); |
| if (funcMatch) |
| functions.push(funcMatch[1]); |
| const importMatch = line.match(/import\s+.*from\s+['"]([^'"]+)['"]/); |
| if (importMatch) |
| imports.push(importMatch[1]); |
| const exportMatch = line.match(/export\s+(?:const|function|class|interface)\s+(\w+)/); |
| if (exportMatch) |
| exports.push(exportMatch[1]); |
| } |
| const fileSummary = { |
| path, |
| summary, |
| functions, |
| imports, |
| exports, |
| lastAnalyzed: Date.now(), |
| hash: hashContent(content) |
| }; |
| this.cache.set(path, fileSummary); |
| |
| if (this.cache.size > this.MAX_CACHE_SIZE) { |
| this.evictOld(); |
| } |
| return fileSummary; |
| } |
| |
| |
| |
| evictOld() { |
| const entries = Array.from(this.cache.entries()); |
| entries.sort((a, b) => a[1].lastAnalyzed - b[1].lastAnalyzed); |
| |
| const toRemove = Math.floor(entries.length * 0.1); |
| for (let i = 0; i < toRemove; i++) { |
| this.cache.delete(entries[i][0]); |
| } |
| } |
| |
| |
| |
| clear() { |
| this.cache.clear(); |
| } |
| |
| |
| |
| getStats() { |
| return { size: this.cache.size }; |
| } |
| |
| |
| |
| has(path) { |
| return this.cache.has(path); |
| } |
| } |
| |
| const fileCache = new FileCache(); |
| |
| |
| |
| function getFileSummary(path, content, language = 'typescript') { |
| const cached = fileCache.get(path, content); |
| if (cached) { |
| return cached; |
| } |
| |
| return fileCache.set(path, content, language); |
| } |
| |
| |
| |
| function getFileSummaryString(path, content, language = 'typescript') { |
| const summary = getFileSummary(path, content, language); |
| if (!summary) { |
| return extractSummary(content, language); |
| } |
| return summary.summary; |
| } |
| |
| |
| |
| function clearFileCache() { |
| fileCache.clear(); |
| } |
| |
| |
| |
| function getFileCacheStats() { |
| return fileCache.getStats(); |
| } |
| |
| |
| |
| function isFileCached(path) { |
| return fileCache.has(path); |
| } |
| |