6
6
"fmt"
7
7
"io"
8
8
"os"
9
+ "path/filepath"
9
10
"sort"
10
11
"strconv"
11
12
"strings"
@@ -17,6 +18,7 @@ import (
17
18
"github.com/gptscript-ai/gptscript/pkg/builtin"
18
19
"github.com/gptscript-ai/gptscript/pkg/cache"
19
20
"github.com/gptscript-ai/gptscript/pkg/chat"
21
+ "github.com/gptscript-ai/gptscript/pkg/env"
20
22
"github.com/gptscript-ai/gptscript/pkg/gptscript"
21
23
"github.com/gptscript-ai/gptscript/pkg/input"
22
24
"github.com/gptscript-ai/gptscript/pkg/loader"
@@ -65,6 +67,7 @@ type GPTScript struct {
65
67
ForceChat bool `usage:"Force an interactive chat session if even the top level tool is not a chat tool"`
66
68
ForceSequential bool `usage:"Force parallel calls to run sequentially"`
67
69
Workspace string `usage:"Directory to use for the workspace, if specified it will not be deleted on exit"`
70
+ UI bool `usage:"Launch the UI" hidden:"true" local:"true" name:"ui"`
68
71
69
72
readData []byte
70
73
}
@@ -319,6 +322,39 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
319
322
return err
320
323
}
321
324
325
+ // If the user is trying to launch the chat-builder UI, then set up the tool and options here.
326
+ if r .UI {
327
+ args = append ([]string {env .VarOrDefault ("GPTSCRIPT_CHAT_UI_TOOL" , "github.com/gptscript-ai/ui@v2" )}, args ... )
328
+
329
+ // If args has more than one element, then the user has provided a file.
330
+ if len (args ) > 1 {
331
+ if args [1 ] == "-" {
332
+ return fmt .Errorf ("chat UI only supports files, cannot read from stdin" )
333
+ }
334
+
335
+ absPathToScript , err := filepath .Abs (args [1 ])
336
+ if err != nil {
337
+ return fmt .Errorf ("cannot determine absolute path to script %s: %v" , args [1 ], err )
338
+ }
339
+
340
+ gptOpt .Env = append (gptOpt .Env , "SCRIPTS_PATH=" + filepath .Dir (absPathToScript ))
341
+
342
+ args = append ([]string {args [0 ]}, "--file=" + filepath .Base (args [1 ]))
343
+ if len (args ) > 2 {
344
+ args = append (args , args [2 :]... )
345
+ }
346
+ } else {
347
+ cwd , err := os .Getwd ()
348
+ if err != nil {
349
+ return fmt .Errorf ("could not determine current working directory: %w" , err )
350
+ }
351
+ gptOpt .Env = append (gptOpt .Env , "SCRIPTS_PATH=" + cwd )
352
+ }
353
+
354
+ // The UI must run in daemon mode.
355
+ r .Daemon = true
356
+ }
357
+
322
358
ctx := cmd .Context ()
323
359
324
360
if r .Server {
@@ -385,7 +421,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
385
421
}
386
422
387
423
if r .ChatState != "" {
388
- resp , err := gptScript .Chat (cmd .Context (), r .ChatState , prg , os . Environ () , toolInput )
424
+ resp , err := gptScript .Chat (cmd .Context (), r .ChatState , prg , gptOpt . Env , toolInput )
389
425
if err != nil {
390
426
return err
391
427
}
@@ -399,10 +435,10 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
399
435
if prg .IsChat () || r .ForceChat {
400
436
return chat .Start (cmd .Context (), nil , gptScript , func () (types.Program , error ) {
401
437
return r .readProgram (ctx , gptScript , args )
402
- }, os . Environ () , toolInput )
438
+ }, gptOpt . Env , toolInput )
403
439
}
404
440
405
- s , err := gptScript .Run (cmd .Context (), prg , os . Environ () , toolInput )
441
+ s , err := gptScript .Run (cmd .Context (), prg , gptOpt . Env , toolInput )
406
442
if err != nil {
407
443
return err
408
444
}
0 commit comments