Tweaks to read_file and dir_tree

This commit is contained in:
Matt Low 2024-05-14 23:00:00 +00:00
parent 9ff4322995
commit 75bf9f6125
2 changed files with 28 additions and 28 deletions

View File

@ -11,7 +11,9 @@ import (
toolutil "git.mlow.ca/mlow/lmcli/pkg/lmcli/tools/util"
)
const TREE_DESCRIPTION = `Retrieve a tree view of a directory's contents.
const TREE_DESCRIPTION = `Retrieve a tree-like view of a directory's contents.
Use these results for your own reference in completing your task, they do not need to be shown to the user.
Example result:
{
@ -35,48 +37,45 @@ var DirTreeTool = model.Tool{
Description: "If set, display the tree starting from this path relative to the current one.",
},
{
Name: "max_depth",
Name: "depth",
Type: "integer",
Description: "Maximum depth of recursion. Default is unlimited.",
Description: "Depth of directory recursion. Default 0. Use -1 for unlimited.",
},
},
Impl: func(tool *model.Tool, args map[string]interface{}) (string, error) {
var relativeDir string
tmp, ok := args["relative_dir"]
if ok {
if tmp, ok := args["relative_path"]; ok {
relativeDir, ok = tmp.(string)
if !ok {
return "", fmt.Errorf("Invalid relative_dir in function arguments: %v", tmp)
return "", fmt.Errorf("expected string for relative_path, got %T", tmp)
}
}
var maxDepth int = -1
tmp, ok = args["max_depth"]
if ok {
maxDepth, ok = tmp.(int)
if !ok {
if tmps, ok := tmp.(string); ok {
tmpi, err := strconv.Atoi(tmps)
maxDepth = tmpi
if err != nil {
return "", fmt.Errorf("Invalid max_depth in function arguments: %v", tmp)
}
} else {
return "", fmt.Errorf("Invalid max_depth in function arguments: %v", tmp)
var depth int = 0 // Default value if not provided
if tmp, ok := args["depth"]; ok {
switch v := tmp.(type) {
case float64:
depth = int(v)
case string:
var err error
if depth, err = strconv.Atoi(v); err != nil {
return "", fmt.Errorf("invalid `depth` value, expected integer but got string that cannot convert: %v", tmp)
}
default:
return "", fmt.Errorf("expected int or string for max_depth, got %T", tmp)
}
}
result := tree(relativeDir, maxDepth)
result := tree(relativeDir, depth)
ret, err := result.ToJson()
if err != nil {
return "", fmt.Errorf("Could not serialize result: %v", err)
return "", fmt.Errorf("could not serialize result: %v", err)
}
return ret, nil
},
}
func tree(path string, maxDepth int) model.CallResult {
func tree(path string, depth int) model.CallResult {
if path == "" {
path = "."
}
@ -87,7 +86,7 @@ func tree(path string, maxDepth int) model.CallResult {
var treeOutput strings.Builder
treeOutput.WriteString(path + "\n")
err := buildTree(&treeOutput, path, "", maxDepth)
err := buildTree(&treeOutput, path, "", depth)
if err != nil {
return model.CallResult{
Message: err.Error(),
@ -97,7 +96,7 @@ func tree(path string, maxDepth int) model.CallResult {
return model.CallResult{Result: treeOutput.String()}
}
func buildTree(output *strings.Builder, path string, prefix string, maxDepth int) error {
func buildTree(output *strings.Builder, path string, prefix string, depth int) error {
files, err := os.ReadDir(path)
if err != nil {
return err
@ -124,14 +123,14 @@ func buildTree(output *strings.Builder, path string, prefix string, maxDepth int
output.WriteString(prefix + branch + file.Name())
if file.IsDir() {
output.WriteString("/\n")
if maxDepth != 0 {
if depth != 0 {
var nextPrefix string
if isLast {
nextPrefix = prefix + " "
} else {
nextPrefix = prefix + "│ "
}
buildTree(output, filepath.Join(path, file.Name()), nextPrefix, maxDepth-1)
buildTree(output, filepath.Join(path, file.Name()), nextPrefix, depth-1)
}
} else {
output.WriteString(sizeStr + "\n")
@ -140,4 +139,3 @@ func buildTree(output *strings.Builder, path string, prefix string, maxDepth int
return nil
}

View File

@ -9,7 +9,9 @@ import (
toolutil "git.mlow.ca/mlow/lmcli/pkg/lmcli/tools/util"
)
const READ_FILE_DESCRIPTION = `Read the contents of a text file relative to the current working directory.
const READ_FILE_DESCRIPTION = `Retrieve the contents of a text file relative to the current working directory.
Use the file contents for your own reference in completing your task, they do not need to be shown to the user.
Each line of the returned content is prefixed with its line number and a tab (\t).