|
| 1 | +import { spawn } from 'child_process' |
| 2 | +import * as path from 'path' |
| 3 | + |
| 4 | +import { rgPath } from '@vscode/ripgrep' |
| 5 | + |
| 6 | +import type { CodebuffToolOutput } from '../../../common/src/tools/list' |
| 7 | + |
| 8 | +export function codeSearch({ |
| 9 | + projectPath, |
| 10 | + pattern, |
| 11 | + flags, |
| 12 | + cwd, |
| 13 | +}: { |
| 14 | + projectPath: string |
| 15 | + pattern: string |
| 16 | + flags?: string |
| 17 | + cwd?: string |
| 18 | +}): Promise<CodebuffToolOutput<'code_search'>> { |
| 19 | + return new Promise((resolve) => { |
| 20 | + let stdout = '' |
| 21 | + let stderr = '' |
| 22 | + |
| 23 | + const flagsArray = (flags || '').split(' ').filter(Boolean) |
| 24 | + let searchCwd = projectPath |
| 25 | + if (cwd) { |
| 26 | + const requestedPath = path.resolve(projectPath, cwd) |
| 27 | + // Ensure the search path is within the project directory |
| 28 | + if (!requestedPath.startsWith(projectPath)) { |
| 29 | + resolve([ |
| 30 | + { |
| 31 | + type: 'json', |
| 32 | + value: { |
| 33 | + errorMessage: `Invalid cwd: Path '${cwd}' is outside the project directory.`, |
| 34 | + }, |
| 35 | + }, |
| 36 | + ]) |
| 37 | + return |
| 38 | + } |
| 39 | + searchCwd = requestedPath |
| 40 | + } |
| 41 | + |
| 42 | + const args = [...flagsArray, pattern, '.'] |
| 43 | + |
| 44 | + const childProcess = spawn(rgPath, args, { |
| 45 | + cwd: searchCwd, |
| 46 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 47 | + }) |
| 48 | + |
| 49 | + childProcess.stdout.on('data', (data) => { |
| 50 | + stdout += data.toString() |
| 51 | + }) |
| 52 | + |
| 53 | + childProcess.stderr.on('data', (data) => { |
| 54 | + stderr += data.toString() |
| 55 | + }) |
| 56 | + |
| 57 | + childProcess.on('close', (code) => { |
| 58 | + // Truncate output to prevent memory issues |
| 59 | + const maxLength = 10000 |
| 60 | + const truncatedStdout = |
| 61 | + stdout.length > maxLength |
| 62 | + ? stdout.substring(0, maxLength) + '\n\n[Output truncated]' |
| 63 | + : stdout |
| 64 | + |
| 65 | + const maxErrorLength = 1000 |
| 66 | + const truncatedStderr = |
| 67 | + stderr.length > maxErrorLength |
| 68 | + ? stderr.substring(0, maxErrorLength) + '\n\n[Error output truncated]' |
| 69 | + : stderr |
| 70 | + |
| 71 | + const result = { |
| 72 | + stdout: truncatedStdout, |
| 73 | + ...(truncatedStderr && { stderr: truncatedStderr }), |
| 74 | + ...(code !== null && { exitCode: code }), |
| 75 | + message: 'Code search completed', |
| 76 | + } |
| 77 | + |
| 78 | + resolve([ |
| 79 | + { |
| 80 | + type: 'json', |
| 81 | + value: result, |
| 82 | + }, |
| 83 | + ]) |
| 84 | + }) |
| 85 | + |
| 86 | + childProcess.on('error', (error) => { |
| 87 | + resolve([ |
| 88 | + { |
| 89 | + type: 'json', |
| 90 | + value: { |
| 91 | + errorMessage: `Failed to execute ripgrep: ${error.message}. Make sure ripgrep is installed and available in PATH.`, |
| 92 | + }, |
| 93 | + }, |
| 94 | + ]) |
| 95 | + }) |
| 96 | + }) |
| 97 | +} |
0 commit comments