@@ -25,6 +25,14 @@ import (
25
25
)
26
26
27
27
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
+ },
28
36
"sys.read" : {
29
37
Parameters : types.Parameters {
30
38
Description : "Reads the contents of a file" ,
@@ -268,6 +276,37 @@ func SysExec(ctx context.Context, env []string, input string) (string, error) {
268
276
return string (out ), err
269
277
}
270
278
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
+
271
310
func SysRead (ctx context.Context , env []string , input string ) (string , error ) {
272
311
var params struct {
273
312
Filename string `json:"filename,omitempty"`
0 commit comments