Skip to content

Commit edacd65

Browse files
authored
allow wsh ai to attach directory listings to the chat (#2469)
1 parent 271c355 commit edacd65

File tree

7 files changed

+366
-221
lines changed

7 files changed

+366
-221
lines changed

cmd/wsh/cmd/wshcmd-ai.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"encoding/base64"
8+
"encoding/json"
89
"fmt"
910
"io"
1011
"net/http"
@@ -13,6 +14,7 @@ import (
1314
"strings"
1415

1516
"github.com/spf13/cobra"
17+
"github.com/wavetermdev/waveterm/pkg/util/fileutil"
1618
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
1719
"github.com/wavetermdev/waveterm/pkg/wshrpc"
1820
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient"
@@ -107,22 +109,38 @@ func aiRun(cmd *cobra.Command, args []string) (rtnErr error) {
107109
if err != nil {
108110
return fmt.Errorf("accessing file %s: %w", filePath, err)
109111
}
110-
if fileInfo.IsDir() {
111-
return fmt.Errorf("%s is a directory, not a file", filePath)
112+
absPath, err := filepath.Abs(filePath)
113+
if err != nil {
114+
return fmt.Errorf("getting absolute path for %s: %w", filePath, err)
112115
}
113116

114-
data, err = os.ReadFile(filePath)
115-
if err != nil {
116-
return fmt.Errorf("reading file %s: %w", filePath, err)
117+
if fileInfo.IsDir() {
118+
result, err := fileutil.ReadDir(filePath, 500)
119+
if err != nil {
120+
return fmt.Errorf("reading directory %s: %w", filePath, err)
121+
}
122+
jsonData, err := json.Marshal(result)
123+
if err != nil {
124+
return fmt.Errorf("marshaling directory listing for %s: %w", filePath, err)
125+
}
126+
data = jsonData
127+
fileName = absPath
128+
mimeType = "directory"
129+
} else {
130+
data, err = os.ReadFile(filePath)
131+
if err != nil {
132+
return fmt.Errorf("reading file %s: %w", filePath, err)
133+
}
134+
fileName = absPath
135+
mimeType = detectMimeType(data)
117136
}
118-
fileName = filepath.Base(filePath)
119-
mimeType = detectMimeType(data)
120137
}
121138

122139
isPDF := mimeType == "application/pdf"
123140
isImage := strings.HasPrefix(mimeType, "image/")
124-
125-
if !isPDF && !isImage {
141+
isDirectory := mimeType == "directory"
142+
143+
if !isPDF && !isImage && !isDirectory {
126144
mimeType = "text/plain"
127145
if utilfn.ContainsBinaryData(data) {
128146
return fmt.Errorf("file %s contains binary data and cannot be uploaded as text", fileName)

0 commit comments

Comments
 (0)