Skip to content

Commit 80e3821

Browse files
authored
Merge pull request #399 from thedadams/add-chat-ui
feat: add chat-ui option
2 parents 29f5765 + 1d4b871 commit 80e3821

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

pkg/cli/eval.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (e *Eval) Run(cmd *cobra.Command, args []string) error {
7979
}, os.Environ(), toolInput)
8080
}
8181

82-
toolOutput, err := runner.Run(cmd.Context(), prg, os.Environ(), toolInput)
82+
toolOutput, err := runner.Run(cmd.Context(), prg, opts.Env, toolInput)
8383
if err != nil {
8484
return err
8585
}

pkg/cli/gptscript.go

+39-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"path/filepath"
910
"sort"
1011
"strconv"
1112
"strings"
@@ -17,6 +18,7 @@ import (
1718
"github.com/gptscript-ai/gptscript/pkg/builtin"
1819
"github.com/gptscript-ai/gptscript/pkg/cache"
1920
"github.com/gptscript-ai/gptscript/pkg/chat"
21+
"github.com/gptscript-ai/gptscript/pkg/env"
2022
"github.com/gptscript-ai/gptscript/pkg/gptscript"
2123
"github.com/gptscript-ai/gptscript/pkg/input"
2224
"github.com/gptscript-ai/gptscript/pkg/loader"
@@ -65,6 +67,7 @@ type GPTScript struct {
6567
ForceChat bool `usage:"Force an interactive chat session if even the top level tool is not a chat tool"`
6668
ForceSequential bool `usage:"Force parallel calls to run sequentially"`
6769
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"`
6871

6972
readData []byte
7073
}
@@ -319,6 +322,39 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
319322
return err
320323
}
321324

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+
322358
ctx := cmd.Context()
323359

324360
if r.Server {
@@ -385,7 +421,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
385421
}
386422

387423
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)
389425
if err != nil {
390426
return err
391427
}
@@ -399,10 +435,10 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
399435
if prg.IsChat() || r.ForceChat {
400436
return chat.Start(cmd.Context(), nil, gptScript, func() (types.Program, error) {
401437
return r.readProgram(ctx, gptScript, args)
402-
}, os.Environ(), toolInput)
438+
}, gptOpt.Env, toolInput)
403439
}
404440

405-
s, err := gptScript.Run(cmd.Context(), prg, os.Environ(), toolInput)
441+
s, err := gptScript.Run(cmd.Context(), prg, gptOpt.Env, toolInput)
406442
if err != nil {
407443
return err
408444
}

pkg/env/env.go

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ func execEquals(bin, check string) bool {
1212
bin == check+".exe"
1313
}
1414

15+
func VarOrDefault(key, defaultValue string) string {
16+
if val := os.Getenv(key); val != "" {
17+
return val
18+
}
19+
20+
return defaultValue
21+
}
22+
1523
func ToEnvLike(v string) string {
1624
v = strings.ReplaceAll(v, ".", "_")
1725
v = strings.ReplaceAll(v, "-", "_")

0 commit comments

Comments
 (0)