Skip to content

Commit ef14e4f

Browse files
committed
pkg/codesearch: add read-file command
Just provides full file contents as last resort.
1 parent 67cfddb commit ef14e4f

File tree

7 files changed

+66
-0
lines changed

7 files changed

+66
-0
lines changed

pkg/aflow/tool/codesearcher/codesearcher.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import (
1616
var Tools = []aflow.Tool{
1717
aflow.NewFuncTool("codesearch-dir-index", dirIndex, `
1818
Tool provides list of source files and subdirectories in the given directory in the source tree.
19+
`),
20+
aflow.NewFuncTool("read-file", readFile, `
21+
Tool provides full contents of a single source file as is. Avoid using this tool if there are better
22+
and more specialized tools for the job, because source files may be large and contain lots
23+
of unrelated information.
1924
`),
2025
aflow.NewFuncTool("codesearch-file-index", fileIndex, `
2126
Tool provides list of entities defined in the given source file.
@@ -68,6 +73,15 @@ type dirIndexResult struct {
6873
Files []string `jsonschema:"List of source files."`
6974
}
7075

76+
type readFileArgs struct {
77+
File string `jsonschema:"Source file path."`
78+
}
79+
80+
type readFileResult struct {
81+
Missing bool `jsonschema:"Set to true if the requested file does not exist."`
82+
Contents string `jsonschema:"File contents."`
83+
}
84+
7185
type fileIndexArgs struct {
7286
SourceFile string `jsonschema:"Source file path."`
7387
}
@@ -154,6 +168,15 @@ func dirIndex(ctx *aflow.Context, state prepareResult, args dirIndexArgs) (dirIn
154168
return res, err
155169
}
156170

171+
func readFile(ctx *aflow.Context, state prepareResult, args readFileArgs) (readFileResult, error) {
172+
ok, contents, err := state.Index.ReadFile(args.File)
173+
res := readFileResult{
174+
Missing: !ok,
175+
Contents: contents,
176+
}
177+
return res, err
178+
}
179+
157180
func fileIndex(ctx *aflow.Context, state prepareResult, args fileIndexArgs) (fileIndexResult, error) {
158181
ok, entities, err := state.Index.FileIndex(args.SourceFile)
159182
res := fileIndexResult{

pkg/codesearch/codesearch.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ var Commands = []Command{
4545
}
4646
return b.String(), nil
4747
}},
48+
{"read-file", 1, func(index *Index, args []string) (string, error) {
49+
ok, contents, err := index.ReadFile(args[0])
50+
if err != nil || !ok {
51+
return notFound, err
52+
}
53+
return contents, nil
54+
}},
4855
{"file-index", 1, func(index *Index, args []string) (string, error) {
4956
ok, entities, err := index.FileIndex(args[0])
5057
if err != nil || !ok {
@@ -137,6 +144,27 @@ func (index *Index) DirIndex(dir string) (bool, []string, []string, error) {
137144
return exists, subdirs, files, nil
138145
}
139146

147+
func (index *Index) ReadFile(file string) (bool, string, error) {
148+
if err := escaping(file); err != nil {
149+
return false, "", nil
150+
}
151+
for _, dir := range index.srcDirs {
152+
data, err := os.ReadFile(filepath.Join(dir, file))
153+
if err != nil {
154+
if os.IsNotExist(err) {
155+
continue
156+
}
157+
var errno syscall.Errno
158+
if errors.As(err, &errno) && errno == syscall.EISDIR {
159+
return false, "", nil
160+
}
161+
return false, "", err
162+
}
163+
return true, string(data), nil
164+
}
165+
return false, "", nil
166+
}
167+
140168
func (index *Index) FileIndex(file string) (bool, []Entity, error) {
141169
var entities []Entity
142170
for _, def := range index.db.Definitions {

pkg/codesearch/testdata/mm/slub.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// slub.c contents.
2+
// This file is used in read-file test.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
read-file mm
2+
3+
not found
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
read-file mm/../../codesearch.go
2+
3+
not found
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
read-file mm/slub.c
2+
3+
// slub.c contents.
4+
// This file is used in read-file test.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
read-file file-that-does-not-exist.c
2+
3+
not found

0 commit comments

Comments
 (0)