-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: Add pattern search support to read_file tool #8739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -22,6 +22,7 @@ import { | |||
| processImageFile, | ||||
| ImageMemoryTracker, | ||||
| } from "./helpers/imageHelpers" | ||||
| import { regexSearchFiles } from "../../services/ripgrep" | ||||
|
|
||||
| export function getReadFileToolDescription(blockName: string, blockParams: any): string { | ||||
| // Handle both single path and multiple files via args | ||||
|
|
@@ -54,6 +55,120 @@ export function getReadFileToolDescription(blockName: string, blockParams: any): | |||
| return `[${blockName} with missing path/args]` | ||||
| } | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Pattern 搜索結果介面 | ||||
| */ | ||||
| interface PatternMatch { | ||||
| startLine: number | ||||
| endLine: number | ||||
| matchLine: number // 實際匹配的行號 | ||||
| content: string // 帶行號的上下文(前後各 2 行) | ||||
| } | ||||
|
|
||||
| /** | ||||
| * 在單個檔案中搜索 pattern | ||||
| * @param filePath - 完整檔案路徑 | ||||
| * @param cwd - 工作目錄 | ||||
| * @param pattern - Regex pattern | ||||
| * @param rooIgnoreController - Ignore 控制器 | ||||
| * @returns 匹配結果數組 | ||||
| */ | ||||
| async function searchPatternInFile( | ||||
| filePath: string, | ||||
| cwd: string, | ||||
| pattern: string, | ||||
| rooIgnoreController?: any, | ||||
| ): Promise<PatternMatch[]> { | ||||
| try { | ||||
| // 使用 ripgrep 搜索單個檔案 | ||||
| const searchResults = await regexSearchFiles(cwd, filePath, pattern, undefined, rooIgnoreController) | ||||
|
|
||||
| // 解析 ripgrep 輸出 | ||||
| // 輸出格式範例: | ||||
| // # src/app.ts | ||||
| // 45 | export async function fetchData() { | ||||
| // 46 | const response = await fetch(url) | ||||
| // 47 | return response.json() | ||||
| // ---- | ||||
|
|
||||
| const matches: PatternMatch[] = [] | ||||
| const lines = searchResults.split("\n") | ||||
|
|
||||
| let currentMatchLines: { line: number; text: string }[] = [] | ||||
| let inMatchBlock = false | ||||
|
||||
| let inMatchBlock = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation doesn't support combining pattern with line_range as documented in example 5 of the tool description. When both parameters are provided, line_range is processed first (line 651) and returns early with continue (line 664), preventing this pattern logic from executing. This means users cannot search for patterns within a specific line range as the documentation suggests. To fix this, the pattern search logic would need to either: (1) be executed before line_range handling and filter matches to the specified ranges, or (2) read only the specified line ranges first and then search within them.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User input (fileResult.pattern) is embedded directly into XML. For security and well-formed XML, please sanitize or escape XML special characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable 'inMatchBlock' is declared and updated but never used. Consider removing it.