on main help sign in
fix(ls): fix path expand on ls tool
0c1b71cAndrey Nering
internal/fsext/expand.gonew file+29
@@ -0,0 +1,29 @@
1+package fsext
2+
3+import (
4+ "os"
5+ "strings"
6+
7+ "mvdan.cc/sh/v3/expand"
8+ "mvdan.cc/sh/v3/syntax"
9+)
10+
11+// Expand is a wrapper around [expand.Literal]. It will escape the input
12+// string, expand any shell symbols (such as '~') and resolve any environment
13+// variables.
14+func Expand(s string) (string, error) {
15+ if s == "" {
16+ return "", nil
17+ }
18+ p := syntax.NewParser()
19+ word, err := p.Document(strings.NewReader(s))
20+ if err != nil {
21+ return "", err
22+ }
23+ cfg := &expand.Config{
24+ Env: expand.FuncEnviron(os.Getenv),
25+ ReadDir2: os.ReadDir,
26+ GlobStar: true,
27+ }
28+ return expand.Literal(cfg, word)
29+}
internal/llm/tools/ls.go+4−6
@@ -121,12 +121,10 @@ func (l *lsTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
121 searchPath = l.workingDir
121 searchPath = l.workingDir
122 }
122 }
123
123
124− if searchPath == "~" {
124+ var err error
125− homeDir, err := os.UserHomeDir()
125+ searchPath, err = fsext.Expand(searchPath)
126− if err != nil {
126+ if err != nil {
127− return ToolResponse{}, fmt.Errorf("error resolving home directory: %w", err)
127+ return ToolResponse{}, fmt.Errorf("error expanding path: %w", err)
128− }
129− searchPath = homeDir
130 }
128 }
131
129
132 if !filepath.IsAbs(searchPath) {
130 if !filepath.IsAbs(searchPath) {