Skip to content

Commit 247a05f

Browse files
committed
feat: add chat-ui option
The chat UI option is sugar to launch the chat-builder tool. If the user enables the chat-ui option, then the appropriate setup is done to launch the chat-builder UI tool for the user. If the a file is provided, then the UI should launch to the "run" UI and the user can chat with the provided tool. If no file is provided, then the "browse" UI is opened and the user can decide what to do. The most important secondary option is the --workspace option. If the user provides a file, then the workspace will be set to the directory where that file exists. However, if the user specifies their own workspace, then it is not overridden.
1 parent 1fe500f commit 247a05f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pkg/cli/gptscript.go

Lines changed: 25 additions & 0 deletions
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+
ChatUI bool `usage:"Launch the chat builder UI" hidden:"true" local:"true" name:"chat-ui"`
6871

6972
readData []byte
7073
}
@@ -314,6 +317,28 @@ func (r *GPTScript) PrintOutput(toolInput, toolOutput string) (err error) {
314317
}
315318

316319
func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
320+
// If the user is trying to launch the chat-builder UI, then set up the tool and options here.
321+
if r.ChatUI {
322+
args = append([]string{env.EnvVarOrDefault("GPTSCRIPT_CHAT_UI_TOOL", "github.com/gptscript-ai/ui@v2")}, args...)
323+
324+
// If args has more than one element, then the user has provided a file.
325+
if len(args) > 1 {
326+
if args[1] == "-" {
327+
return fmt.Errorf("chat UI only supports files, cannot read from stdin")
328+
}
329+
330+
// Only set the workspace if the user has not already done so.
331+
if r.Workspace == "" {
332+
r.Workspace = filepath.Dir(args[1])
333+
}
334+
335+
args = append(append([]string{args[0]}, filepath.Base("--file="+args[0])), args[1:]...)
336+
}
337+
338+
// The UI must run in daemon mode.
339+
r.Daemon = true
340+
}
341+
317342
gptOpt, err := r.NewGPTScriptOpts()
318343
if err != nil {
319344
return err

pkg/env/env.go

Lines changed: 8 additions & 0 deletions
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 EnvVarOrDefault(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)