1package tools
2
3import (
4 "cmp"
5 "context"
6 _ "embed"
7 "fmt"
8 "os"
9 "path/filepath"
10 "strings"
11
12 "charm.land/fantasy"
13 "github.com/charmbracelet/crush/internal/config"
14 "github.com/charmbracelet/crush/internal/filepathext"
15 "github.com/charmbracelet/crush/internal/fsext"
16 "github.com/charmbracelet/crush/internal/permission"
17)
18
19type LSParams struct {
20 Path string `json:"path,omitempty" description:"The path to the directory to list (defaults to current working directory)"`
21 Ignore []string `json:"ignore,omitempty" description:"List of glob patterns to ignore"`
22 Depth int `json:"depth,omitempty" description:"The maximum depth to traverse"`
23}
24
25type LSPermissionsParams struct {
26 Path string `json:"path"`
27 Ignore []string `json:"ignore"`
28 Depth int `json:"depth"`
29}
30
31type NodeType string
32
33const (
34 NodeTypeFile NodeType = "file"
35 NodeTypeDirectory NodeType = "directory"
36)
37
38type TreeNode struct {
39 Name string `json:"name"`
40 Path string `json:"path"`
41 Type NodeType `json:"type"`
42 Children []*TreeNode `json:"children,omitempty"`
43}
44
45type LSResponseMetadata struct {
46 NumberOfFiles int `json:"number_of_files"`
47 Truncated bool `json:"truncated"`
48}
49
50const (
51 LSToolName = "ls"
52 maxLSFiles = 1000
53)
54
55//go:embed ls.md
56var lsDescription []byte
57
58func NewLsTool(permissions permission.Service, workingDir string, lsConfig config.ToolLs) fantasy.AgentTool {
59 return fantasy.NewAgentTool(
60 LSToolName,
61 string(lsDescription),
62 func(ctx context.Context, params LSParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
63 searchPath, err := fsext.Expand(cmp.Or(params.Path, workingDir))
64 if err != nil {
65 return fantasy.NewTextErrorResponse(fmt.Sprintf("error expanding path: %v", err)), nil
66 }
67
68 searchPath = filepathext.SmartJoin(workingDir, searchPath)
69
70 // Check if directory is outside working directory and request permission if needed
71 absWorkingDir, err := filepath.Abs(workingDir)
72 if err != nil {
73 return fantasy.NewTextErrorResponse(fmt.Sprintf("error resolving working directory: %v", err)), nil
74 }
75
76 absSearchPath, err := filepath.Abs(searchPath)
77 if err != nil {
78 return fantasy.NewTextErrorResponse(fmt.Sprintf("error resolving search path: %v", err)), nil
79 }
80
81 relPath, err := filepath.Rel(absWorkingDir, absSearchPath)
82 if err != nil || strings.HasPrefix(relPath, "..") {
83 // Directory is outside working directory, request permission
84 sessionID := GetSessionFromContext(ctx)
85 if sessionID == "" {
86 return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for accessing directories outside working directory")
87 }
88
89 granted, err := permissions.Request(ctx,
90 permission.CreatePermissionRequest{
91 SessionID: sessionID,
92 Path: absSearchPath,
93 ToolCallID: call.ID,
94 ToolName: LSToolName,
95 Action: "list",
96 Description: fmt.Sprintf("List directory outside working directory: %s", absSearchPath),
97 Params: LSPermissionsParams(params),
98 },
99 )
100 if err != nil {
101 return fantasy.ToolResponse{}, err
102 }
103 if !granted {
104 return fantasy.ToolResponse{}, permission.ErrorPermissionDenied
105 }
106 }
107
108 output, metadata, err := ListDirectoryTree(searchPath, params, lsConfig)
109 if err != nil {
110 return fantasy.NewTextErrorResponse(err.Error()), nil
111 }
112
113 return fantasy.WithResponseMetadata(
114 fantasy.NewTextResponse(output),
115 metadata,
116 ), nil
117 })
118}
119
120func ListDirectoryTree(searchPath string, params LSParams, lsConfig config.ToolLs) (string, LSResponseMetadata, error) {
121 if _, err := os.Stat(searchPath); os.IsNotExist(err) {
122 return "", LSResponseMetadata{}, fmt.Errorf("path does not exist: %s", searchPath)
123 }
124
125 depth, limit := lsConfig.Limits()
126 maxFiles := cmp.Or(limit, maxLSFiles)
127 files, truncated, err := fsext.ListDirectory(
128 searchPath,
129 params.Ignore,
130 cmp.Or(params.Depth, depth),
131 maxFiles,
132 )
133 if err != nil {
134 return "", LSResponseMetadata{}, fmt.Errorf("error listing directory: %w", err)
135 }
136
137 metadata := LSResponseMetadata{
138 NumberOfFiles: len(files),
139 Truncated: truncated,
140 }
141 tree := createFileTree(files, searchPath)
142
143 var output string
144 if truncated {
145 output = fmt.Sprintf("There are more than %d files in the directory. Use a more specific path or use the Glob tool to find specific files. The first %[1]d files and directories are included below.\n", maxFiles)
146 }
147 if depth > 0 {
148 output = fmt.Sprintf("The directory tree is shown up to a depth of %d. Use a higher depth and a specific path to see more levels.\n", cmp.Or(params.Depth, depth))
149 }
150 return output + "\n" + printTree(tree, searchPath), metadata, nil
151}
152
153func createFileTree(sortedPaths []string, rootPath string) []*TreeNode {
154 root := []*TreeNode{}
155 pathMap := make(map[string]*TreeNode)
156
157 for _, path := range sortedPaths {
158 relativePath := strings.TrimPrefix(path, rootPath)
159 parts := strings.Split(relativePath, string(filepath.Separator))
160 currentPath := ""
161 var parentPath string
162
163 var cleanParts []string
164 for _, part := range parts {
165 if part != "" {
166 cleanParts = append(cleanParts, part)
167 }
168 }
169 parts = cleanParts
170
171 if len(parts) == 0 {
172 continue
173 }
174
175 for i, part := range parts {
176 if currentPath == "" {
177 currentPath = part
178 } else {
179 currentPath = filepath.Join(currentPath, part)
180 }
181
182 if _, exists := pathMap[currentPath]; exists {
183 parentPath = currentPath
184 continue
185 }
186
187 isLastPart := i == len(parts)-1
188 isDir := !isLastPart || strings.HasSuffix(relativePath, string(filepath.Separator))
189 nodeType := NodeTypeFile
190 if isDir {
191 nodeType = NodeTypeDirectory
192 }
193 newNode := &TreeNode{
194 Name: part,
195 Path: currentPath,
196 Type: nodeType,
197 Children: []*TreeNode{},
198 }
199
200 pathMap[currentPath] = newNode
201
202 if i > 0 && parentPath != "" {
203 if parent, ok := pathMap[parentPath]; ok {
204 parent.Children = append(parent.Children, newNode)
205 }
206 } else {
207 root = append(root, newNode)
208 }
209
210 parentPath = currentPath
211 }
212 }
213
214 return root
215}
216
217func printTree(tree []*TreeNode, rootPath string) string {
218 var result strings.Builder
219
220 result.WriteString("- ")
221 result.WriteString(filepath.ToSlash(rootPath))
222 if rootPath[len(rootPath)-1] != '/' {
223 result.WriteByte('/')
224 }
225 result.WriteByte('\n')
226
227 for _, node := range tree {
228 printNode(&result, node, 1)
229 }
230
231 return result.String()
232}
233
234func printNode(builder *strings.Builder, node *TreeNode, level int) {
235 indent := strings.Repeat(" ", level)
236
237 nodeName := node.Name
238 if node.Type == NodeTypeDirectory {
239 nodeName = nodeName + "/"
240 }
241
242 fmt.Fprintf(builder, "%s- %s\n", indent, nodeName)
243
244 if node.Type == NodeTypeDirectory && len(node.Children) > 0 {
245 for _, child := range node.Children {
246 printNode(builder, child, level+1)
247 }
248 }
249}