| 17 | } |
| 18 | |
| 19 | function searchInFile(filePath: string, query: string): SearchResult[] { |
| 20 | const results: SearchResult[] = []; |
| 21 | try { |
| 22 | const content = fs.readFileSync(filePath, 'utf-8'); |
| 23 | const lines = content.split('\n'); |
| 24 | const lowerQuery = query.toLowerCase(); |
| 25 | |
| 26 | lines.forEach((line, index) => { |
| 27 | if (line.toLowerCase().includes(lowerQuery)) { |
| 28 | const start = Math.max(0, index - 1); |
| 29 | const end = Math.min(lines.length, index + 2); |
| 30 | const snippet = lines.slice(start, end).join('\n'); |
| 31 | |
| 32 | results.push({ |
| 33 | type: 'memory', |
| 34 | title: path.basename(filePath), |
| 35 | snippet: snippet.substring(0, 200), |
| 36 | path: filePath |
| 37 | }); |
| 38 | } |
| 39 | }); |
| 40 | } catch { |
| 41 | // Skip files that can't be read |
| 42 | } |
| 43 | return results; |
| 44 | } |
| 45 | |
| 46 | export async function GET(request: Request) { |
| 47 | const { searchParams } = new URL(request.url); |