Skip to content

Commit 80d0e95

Browse files
feat: add sys.ls
1 parent 102af6a commit 80d0e95

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

pkg/builtin/builtin.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ import (
2525
)
2626

2727
var tools = map[string]types.Tool{
28+
"sys.ls": {
29+
Parameters: types.Parameters{
30+
Description: "Lists the contents of a directory",
31+
Arguments: types.ObjectSchema(
32+
"dir", "The directory to list"),
33+
},
34+
BuiltinFunc: SysLs,
35+
},
2836
"sys.read": {
2937
Parameters: types.Parameters{
3038
Description: "Reads the contents of a file",
@@ -268,6 +276,37 @@ func SysExec(ctx context.Context, env []string, input string) (string, error) {
268276
return string(out), err
269277
}
270278

279+
func SysLs(_ context.Context, _ []string, input string) (string, error) {
280+
var params struct {
281+
Dir string `json:"dir,omitempty"`
282+
}
283+
if err := json.Unmarshal([]byte(input), &params); err != nil {
284+
return "", err
285+
}
286+
287+
if params.Dir == "" {
288+
params.Dir = "."
289+
}
290+
291+
entries, err := os.ReadDir(params.Dir)
292+
if errors.Is(err, fs.ErrNotExist) {
293+
return "", fmt.Errorf("directory does not exist: %s", params.Dir)
294+
} else if err != nil {
295+
return "", err
296+
}
297+
298+
var result []string
299+
for _, entry := range entries {
300+
if entry.IsDir() {
301+
result = append(result, entry.Name()+"/")
302+
} else {
303+
result = append(result, entry.Name())
304+
}
305+
}
306+
307+
return strings.Join(result, "\n"), nil
308+
}
309+
271310
func SysRead(ctx context.Context, env []string, input string) (string, error) {
272311
var params struct {
273312
Filename string `json:"filename,omitempty"`

0 commit comments

Comments
 (0)