on main help sign in
fix(ls): fix path expand on ls tool
commit0c1b71c9f0c73042d5987318e284796faf13fb85 authorAndrey Nering <andreynering@users.noreply.github.com> dateTue 29 Jul 2025, 11:24 (1 year ago) parent4eaa13a
2 files changed+33−6
internal/fsext/expand.go+29 internal/llm/tools/ls.go+4−6
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) {
121121 searchPath = l.workingDir
122122 }
123123
124− if searchPath == "~" {
125− homeDir, err := os.UserHomeDir()
126− if err != nil {
127− return ToolResponse{}, fmt.Errorf("error resolving home directory: %w", err)
128− }
129− searchPath = homeDir
124+ var err error
125+ searchPath, err = fsext.Expand(searchPath)
126+ if err != nil {
127+ return ToolResponse{}, fmt.Errorf("error expanding path: %w", err)
130128 }
131129
132130 if !filepath.IsAbs(searchPath) {