v0.0.5: context compaction, minimal config, retry/search/code symbols/diff/status/test commands
This commit is contained in:
+61
-10
@@ -223,7 +223,9 @@ func (t *FileSearchTool) Definition() llm.Tool {
|
||||
"properties": {
|
||||
"query": {"type": "string", "description": "Text or regular expression to search for."},
|
||||
"path": {"type": "string", "description": "Optional directory or file to search. Defaults to the configured agent working directory."},
|
||||
"glob": {"type": "string", "description": "Optional file glob, for example '*.go'."}
|
||||
"glob": {"type": "string", "description": "Optional file glob, for example '*.go'."},
|
||||
"context_lines": {"type": "integer", "description": "Optional number of context lines around each match. Defaults to 0."},
|
||||
"max_results": {"type": "integer", "description": "Optional maximum matches to return. Defaults to 100."}
|
||||
},
|
||||
"required": ["query"],
|
||||
"additionalProperties": false
|
||||
@@ -233,9 +235,11 @@ func (t *FileSearchTool) Definition() llm.Tool {
|
||||
}
|
||||
|
||||
type fileSearchArgs struct {
|
||||
Query string `json:"query"`
|
||||
Path string `json:"path"`
|
||||
Glob string `json:"glob"`
|
||||
Query string `json:"query"`
|
||||
Path string `json:"path"`
|
||||
Glob string `json:"glob"`
|
||||
ContextLines int `json:"context_lines"`
|
||||
MaxResults int `json:"max_results"`
|
||||
}
|
||||
|
||||
func (t *FileSearchTool) Execute(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||
@@ -256,8 +260,31 @@ func (t *FileSearchTool) Execute(ctx context.Context, raw json.RawMessage) (stri
|
||||
return t.walkSearch(ctx, args, path)
|
||||
}
|
||||
|
||||
func normalizedSearchOptions(args fileSearchArgs) (contextLines int, maxResults int) {
|
||||
contextLines = args.ContextLines
|
||||
if contextLines < 0 {
|
||||
contextLines = 0
|
||||
}
|
||||
if contextLines > 5 {
|
||||
contextLines = 5
|
||||
}
|
||||
maxResults = args.MaxResults
|
||||
if maxResults <= 0 {
|
||||
maxResults = 100
|
||||
}
|
||||
if maxResults > 1000 {
|
||||
maxResults = 1000
|
||||
}
|
||||
return contextLines, maxResults
|
||||
}
|
||||
|
||||
func (t *FileSearchTool) ripgrep(ctx context.Context, args fileSearchArgs, path string) (string, error) {
|
||||
rgArgs := []string{"--line-number", "--color", "never"}
|
||||
contextLines, maxResults := normalizedSearchOptions(args)
|
||||
if contextLines > 0 {
|
||||
rgArgs = append(rgArgs, "--context", fmt.Sprint(contextLines))
|
||||
}
|
||||
rgArgs = append(rgArgs, "--max-count", fmt.Sprint(maxResults))
|
||||
if args.Glob != "" {
|
||||
rgArgs = append(rgArgs, "--glob", args.Glob)
|
||||
}
|
||||
@@ -282,7 +309,10 @@ func (t *FileSearchTool) ripgrep(ctx context.Context, args fileSearchArgs, path
|
||||
}
|
||||
|
||||
func (t *FileSearchTool) walkSearch(ctx context.Context, args fileSearchArgs, root string) (string, error) {
|
||||
contextLines, maxResults := normalizedSearchOptions(args)
|
||||
var out bytes.Buffer
|
||||
matchCount := 0
|
||||
stop := errors.New("result limit reached")
|
||||
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -312,16 +342,22 @@ func (t *FileSearchTool) walkSearch(ctx context.Context, args fileSearchArgs, ro
|
||||
}
|
||||
lines := strings.Split(string(data), "\n")
|
||||
for i, line := range lines {
|
||||
if strings.Contains(line, args.Query) {
|
||||
fmt.Fprintf(&out, "%s:%d:%s\n", path, i+1, line)
|
||||
if out.Len() > MaxToolOutputBytes {
|
||||
return errors.New("output limit reached")
|
||||
}
|
||||
if !strings.Contains(line, args.Query) {
|
||||
continue
|
||||
}
|
||||
matchCount++
|
||||
appendSearchMatch(&out, path, lines, i, contextLines)
|
||||
if matchCount >= maxResults {
|
||||
fmt.Fprintf(&out, "[stopped after %d matches; narrow path/query or raise max_results]\n", maxResults)
|
||||
return stop
|
||||
}
|
||||
if out.Len() > MaxToolOutputBytes {
|
||||
return stop
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil && err.Error() != "output limit reached" {
|
||||
if err != nil && !errors.Is(err, stop) {
|
||||
return "", err
|
||||
}
|
||||
if out.Len() == 0 {
|
||||
@@ -329,3 +365,18 @@ func (t *FileSearchTool) walkSearch(ctx context.Context, args fileSearchArgs, ro
|
||||
}
|
||||
return FormatResult(out.String()), nil
|
||||
}
|
||||
|
||||
func appendSearchMatch(out *bytes.Buffer, path string, lines []string, matchIndex int, contextLines int) {
|
||||
start := max(0, matchIndex-contextLines)
|
||||
end := min(len(lines)-1, matchIndex+contextLines)
|
||||
if contextLines > 0 {
|
||||
fmt.Fprintf(out, "-- %s:%d --\n", path, matchIndex+1)
|
||||
}
|
||||
for i := start; i <= end; i++ {
|
||||
marker := ":"
|
||||
if i == matchIndex {
|
||||
marker = "*"
|
||||
}
|
||||
fmt.Fprintf(out, "%s%s%d:%s\n", path, marker, i+1, lines[i])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user