Skip to content

feat: add chat-ui option #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cli/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (e *Eval) Run(cmd *cobra.Command, args []string) error {
}, os.Environ(), toolInput)
}

toolOutput, err := runner.Run(cmd.Context(), prg, os.Environ(), toolInput)
toolOutput, err := runner.Run(cmd.Context(), prg, opts.Env, toolInput)
if err != nil {
return err
}
Expand Down
42 changes: 39 additions & 3 deletions pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/gptscript-ai/gptscript/pkg/builtin"
"github.com/gptscript-ai/gptscript/pkg/cache"
"github.com/gptscript-ai/gptscript/pkg/chat"
"github.com/gptscript-ai/gptscript/pkg/env"
"github.com/gptscript-ai/gptscript/pkg/gptscript"
"github.com/gptscript-ai/gptscript/pkg/input"
"github.com/gptscript-ai/gptscript/pkg/loader"
Expand Down Expand Up @@ -65,6 +67,7 @@ type GPTScript struct {
ForceChat bool `usage:"Force an interactive chat session if even the top level tool is not a chat tool"`
ForceSequential bool `usage:"Force parallel calls to run sequentially"`
Workspace string `usage:"Directory to use for the workspace, if specified it will not be deleted on exit"`
UI bool `usage:"Launch the UI" hidden:"true" local:"true" name:"ui"`

readData []byte
}
Expand Down Expand Up @@ -319,6 +322,39 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
return err
}

// If the user is trying to launch the chat-builder UI, then set up the tool and options here.
if r.UI {
args = append([]string{env.VarOrDefault("GPTSCRIPT_CHAT_UI_TOOL", "github.com/gptscript-ai/ui@v2")}, args...)

// If args has more than one element, then the user has provided a file.
if len(args) > 1 {
if args[1] == "-" {
return fmt.Errorf("chat UI only supports files, cannot read from stdin")
}

absPathToScript, err := filepath.Abs(args[1])
if err != nil {
return fmt.Errorf("cannot determine absolute path to script %s: %v", args[1], err)
}

gptOpt.Env = append(gptOpt.Env, "SCRIPTS_PATH="+filepath.Dir(absPathToScript))

args = append([]string{args[0]}, "--file="+filepath.Base(args[1]))
if len(args) > 2 {
args = append(args, args[2:]...)
}
} else {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("could not determine current working directory: %w", err)
}
gptOpt.Env = append(gptOpt.Env, "SCRIPTS_PATH="+cwd)
}

// The UI must run in daemon mode.
r.Daemon = true
}

ctx := cmd.Context()

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

if r.ChatState != "" {
resp, err := gptScript.Chat(cmd.Context(), r.ChatState, prg, os.Environ(), toolInput)
resp, err := gptScript.Chat(cmd.Context(), r.ChatState, prg, gptOpt.Env, toolInput)
if err != nil {
return err
}
Expand All @@ -399,10 +435,10 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
if prg.IsChat() || r.ForceChat {
return chat.Start(cmd.Context(), nil, gptScript, func() (types.Program, error) {
return r.readProgram(ctx, gptScript, args)
}, os.Environ(), toolInput)
}, gptOpt.Env, toolInput)
}

s, err := gptScript.Run(cmd.Context(), prg, os.Environ(), toolInput)
s, err := gptScript.Run(cmd.Context(), prg, gptOpt.Env, toolInput)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ func execEquals(bin, check string) bool {
bin == check+".exe"
}

func VarOrDefault(key, defaultValue string) string {
if val := os.Getenv(key); val != "" {
return val
}

return defaultValue
}

func ToEnvLike(v string) string {
v = strings.ReplaceAll(v, ".", "_")
v = strings.ReplaceAll(v, "-", "_")
Expand Down