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" 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: 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.", Description: "If set, display the tree starting from this path relative to the current one.",
}, },
{ {
Name: "max_depth", Name: "depth",
Type: "integer", 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) { Impl: func(tool *model.Tool, args map[string]interface{}) (string, error) {
var relativeDir string var relativeDir string
tmp, ok := args["relative_dir"] if tmp, ok := args["relative_path"]; ok {
if ok {
relativeDir, ok = tmp.(string) relativeDir, ok = tmp.(string)
if !ok { 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 var depth int = 0 // Default value if not provided
tmp, ok = args["max_depth"] if tmp, ok := args["depth"]; ok {
if ok { switch v := tmp.(type) {
maxDepth, ok = tmp.(int) case float64:
if !ok { depth = int(v)
if tmps, ok := tmp.(string); ok { case string:
tmpi, err := strconv.Atoi(tmps) var err error
maxDepth = tmpi if depth, err = strconv.Atoi(v); err != nil {
if err != nil { return "", fmt.Errorf("invalid `depth` value, expected integer but got string that cannot convert: %v", tmp)
return "", fmt.Errorf("Invalid max_depth in function arguments: %v", tmp)
}
} else {
return "", fmt.Errorf("Invalid max_depth in function arguments: %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() ret, err := result.ToJson()
if err != nil { if err != nil {
return "", fmt.Errorf("Could not serialize result: %v", err) return "", fmt.Errorf("could not serialize result: %v", err)
} }
return ret, nil return ret, nil
}, },
} }
func tree(path string, maxDepth int) model.CallResult { func tree(path string, depth int) model.CallResult {
if path == "" { if path == "" {
path = "." path = "."
} }
@ -87,7 +86,7 @@ func tree(path string, maxDepth int) model.CallResult {
var treeOutput strings.Builder var treeOutput strings.Builder
treeOutput.WriteString(path + "\n") treeOutput.WriteString(path + "\n")
err := buildTree(&treeOutput, path, "", maxDepth) err := buildTree(&treeOutput, path, "", depth)
if err != nil { if err != nil {
return model.CallResult{ return model.CallResult{
Message: err.Error(), Message: err.Error(),
@ -97,7 +96,7 @@ func tree(path string, maxDepth int) model.CallResult {
return model.CallResult{Result: treeOutput.String()} 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) files, err := os.ReadDir(path)
if err != nil { if err != nil {
return err return err
@ -124,14 +123,14 @@ func buildTree(output *strings.Builder, path string, prefix string, maxDepth int
output.WriteString(prefix + branch + file.Name()) output.WriteString(prefix + branch + file.Name())
if file.IsDir() { if file.IsDir() {
output.WriteString("/\n") output.WriteString("/\n")
if maxDepth != 0 { if depth != 0 {
var nextPrefix string var nextPrefix string
if isLast { if isLast {
nextPrefix = prefix + " " nextPrefix = prefix + " "
} else { } else {
nextPrefix = prefix + "│ " nextPrefix = prefix + "│ "
} }
buildTree(output, filepath.Join(path, file.Name()), nextPrefix, maxDepth-1) buildTree(output, filepath.Join(path, file.Name()), nextPrefix, depth-1)
} }
} else { } else {
output.WriteString(sizeStr + "\n") output.WriteString(sizeStr + "\n")
@ -140,4 +139,3 @@ func buildTree(output *strings.Builder, path string, prefix string, maxDepth int
return nil return nil
} }

View File

@ -9,7 +9,9 @@ import (
toolutil "git.mlow.ca/mlow/lmcli/pkg/lmcli/tools/util" 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). Each line of the returned content is prefixed with its line number and a tab (\t).