Skip to content

Commit c5fbc58

Browse files
authored
fix:(go) File.Pacakge wrongly replaced by test package (#52)
* fix:(go) `File.Package` wrongly covered by test pacakge * reduce error log * opt:(agent) prompt check tests
1 parent 9b9a73a commit c5fbc58

File tree

6 files changed

+427
-405
lines changed

6 files changed

+427
-405
lines changed

lang/golang/parser/pkg.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ func (p *GoParser) loadPackages(mod *Module, dir string, pkgPath PkgPath) (err e
228228
f = NewFile(relpath)
229229
mod.Files[relpath] = f
230230
}
231-
pkgid := pkg.ID
232-
f.Package = pkgid
233-
f.Imports = imports.Origins
231+
if f.Package == "" {
232+
f.Package = pkg.ID
233+
f.Imports = imports.Origins
234+
}
234235
if err := p.parseFile(ctx, file); err != nil {
235236
return err
236237
}

lang/uniast/ast.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package uniast
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"os"
2322
"path/filepath"
2423
"strconv"
2524
"strings"
@@ -314,10 +313,10 @@ type Identity struct {
314313
}
315314

316315
func NewIdentity(mod, pkg, name string) Identity {
317-
if mod == "" {
318-
fmt.Fprintf(os.Stderr, "module name cannot be empty: %s#%s\n", pkg, name)
319-
// panic(fmt.Sprintf("module name cannot be empty: %s.%s", pkg, name))
320-
}
316+
// if mod == "" {
317+
// fmt.Fprintf(os.Stderr, "module name cannot be empty: %s#%s\n", pkg, name)
318+
// panic(fmt.Sprintf("module name cannot be empty: %s.%s", pkg, name))
319+
// }
321320
return Identity{ModPath: mod, PkgPath: pkg, Name: name}
322321
}
323322

llm/prompt/analyzer.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ You are a code-analysis expert. Based on the Abstract-Syntax-Tree (AST) of a spe
3636
3. Self Reflection: Before answering the user's question, try to understand the complete code calling- chain and the contextual-relationship that causes the problem. If the results returned in step 2 cannot clearly explain the operating mechanism or do not meet the user's needs, try to adjust the selection list and repeat step 2 until the user's question can be accurately answered.
3737

3838
# Notes
39+
- Use the `sequential_thinking` tool during the analysis process to help break down the problem and record information, avoiding information loss.
40+
3941
- Use 'list_repos' to ensure repo_name if you are not sure
4042

4143
- Answer the users' question in the language they use.
4244

43-
- Use the `sequential_thinking` tool during the analysis process to help break down the problem and record information, avoiding information loss.
45+
- Try to check test files (like '*_test.*') or nodes (like 'Test*') to get more example codes, for writing more standardized code
4446

4547
- The answer should list the accurate metadata of the relevant code, including AST node (or package) identity, file location, and code. **MUST providing the exact file location (including line numbers)!**

llm/tool/ast_read.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,19 @@ type FileStruct struct {
207207
}
208208

209209
type NodeStruct struct {
210-
Name string `json:"name" jsonschema:"description=the name of the node"`
211-
Type string `json:"type,omitempty" jsonschema:"description=the type of the node"`
212-
Signature string `json:"signature,omitempty" jsonschema:"description=the func signature of the node"`
213-
File string `json:"file,omitempty" jsonschema:"description=the file path of the node"`
214-
Line int `json:"line,omitempty" jsonschema:"description=the line of the node"`
215-
Codes string `json:"codes,omitempty" jsonschema:"description=the codes of the node"`
216-
Dependencies []NodeID `json:"dependencies,omitempty" jsonschema:"description=the dependencies of the node"`
217-
References []NodeID `json:"references,omitempty" jsonschema:"description=the references of the node"`
218-
Implements []NodeID `json:"implements,omitempty" jsonschema:"description=the implements of the node"`
219-
Groups []NodeID `json:"groups,omitempty" jsonschema:"description=the groups of the node"`
220-
Inherits []NodeID `json:"inherits,omitempty" jsonschema:"description=the inherits of the node"`
210+
ModPath uniast.ModPath `json:"mod_path,omitempty" jsonschema:"description=the module path"`
211+
PkgPath uniast.PkgPath `json:"pkg_path,omitempty" jsonschema:"description=the package path"`
212+
Name string `json:"name" jsonschema:"description=the name of the node"`
213+
Type string `json:"type,omitempty" jsonschema:"description=the type of the node"`
214+
Signature string `json:"signature,omitempty" jsonschema:"description=the func signature of the node"`
215+
File string `json:"file,omitempty" jsonschema:"description=the file path of the node"`
216+
Line int `json:"line,omitempty" jsonschema:"description=the line of the node"`
217+
Codes string `json:"codes,omitempty" jsonschema:"description=the codes of the node"`
218+
Dependencies []NodeID `json:"dependencies,omitempty" jsonschema:"description=the dependencies of the node"`
219+
References []NodeID `json:"references,omitempty" jsonschema:"description=the references of the node"`
220+
Implements []NodeID `json:"implements,omitempty" jsonschema:"description=the implements of the node"`
221+
Groups []NodeID `json:"groups,omitempty" jsonschema:"description=the groups of the node"`
222+
Inherits []NodeID `json:"inherits,omitempty" jsonschema:"description=the inherits of the node"`
221223
}
222224

223225
type NodeID struct {
@@ -366,6 +368,17 @@ func (t *ASTReadTools) GetPackageStructure(ctx context.Context, req GetPackageSt
366368
}
367369
}
368370
}
371+
372+
if len(resp.Files) == 0 {
373+
candidates := []string{}
374+
if mod, ok := repo.Modules[req.ModPath]; ok {
375+
for p := range mod.Packages {
376+
candidates = append(candidates, p)
377+
}
378+
}
379+
resp.Error = fmt.Sprintf("package '%s' not found, maybe you want one of %v", req.PkgPath, candidates)
380+
}
381+
369382
log.Debug("get repo structure, resp: %v", abutil.MarshalJSONIndentNoError(resp))
370383
return resp, nil
371384
}
@@ -398,10 +411,11 @@ func (t *ASTReadTools) getFileStructure(_ context.Context, req GetFileStructReq,
398411
}
399412

400413
resp := new(GetFileStructResp)
401-
file, _ := repo.GetFile(req.FilePath)
414+
file, mod := repo.GetFile(req.FilePath)
402415
if file == nil {
403416
return nil, fmt.Errorf("file '%s' not found", req.FilePath)
404417
}
418+
405419
nodes := repo.GetFileNodes(req.FilePath)
406420
ff := FileStruct{
407421
FilePath: req.FilePath,
@@ -411,7 +425,9 @@ func (t *ASTReadTools) getFileStructure(_ context.Context, req GetFileStructReq,
411425
}
412426
for _, n := range nodes {
413427
nn := NodeStruct{
414-
Name: n.Identity.Name,
428+
ModPath: mod.Name,
429+
PkgPath: file.Package,
430+
Name: n.Identity.Name,
415431
}
416432
if needNodeDetail {
417433
nn.Type = n.Type.String()
@@ -487,6 +503,11 @@ func (t *ASTReadTools) GetASTNode(_ context.Context, params GetASTNodeReq) (*Get
487503
Groups: grps,
488504
})
489505
}
506+
507+
if len(resp.Nodes) == 0 {
508+
resp.Error = "node not found, maybe you should check the pkg_path or node_name?"
509+
}
510+
490511
log.Debug("get repo structure, resp: %v", abutil.MarshalJSONIndentNoError(resp))
491512
return resp, nil
492513
}

llm/tool/ast_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"reflect"
2222
"testing"
2323

24-
"github.com/cloudwego/abcoder/lang/uniast"
2524
"github.com/cloudwego/eino/components/tool"
2625
"github.com/cloudwego/eino/components/tool/utils"
2726
"github.com/cloudwego/eino/schema"
@@ -121,7 +120,7 @@ func TestASTTools_GetFileStructure(t *testing.T) {
121120
in0: context.Background(),
122121
req: GetFileStructReq{
123122
RepoName: "localsession",
124-
FilePath: "backup/metainfo.go",
123+
FilePath: "backup/metainfo_test.go",
125124
},
126125
},
127126
},
@@ -301,7 +300,7 @@ func TestASTTools_GetASTNode(t *testing.T) {
301300
in0: context.Background(),
302301
params: GetASTNodeReq{
303302
RepoName: "localsession",
304-
NodeIDs: []uniast.Identity{
303+
NodeIDs: []NodeID{
305304
{
306305
ModPath: "github.com/cloudwego/localsession",
307306
PkgPath: "github.com/cloudwego/localsession/backup",

0 commit comments

Comments
 (0)