Skip to content

Commit 0f93d6c

Browse files
authored
fix: don't start SDK server if URL opt is provided (#69)
Signed-off-by: Donnie Adams <[email protected]>
1 parent 9cfdd3f commit 0f93d6c

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

gptscript.go

+21-34
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"io"
1212
"log/slog"
13-
"net/url"
1413
"os"
1514
"os/exec"
1615
"path/filepath"
@@ -34,49 +33,27 @@ type GPTScript struct {
3433

3534
func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
3635
opt := completeGlobalOptions(opts...)
37-
lock.Lock()
38-
defer lock.Unlock()
39-
gptscriptCount++
40-
41-
if serverURL == "" {
42-
serverURL = opt.URL
43-
if serverURL == "" {
44-
serverURL = os.Getenv("GPTSCRIPT_URL")
45-
}
46-
}
47-
4836
if opt.Env == nil {
4937
opt.Env = os.Environ()
5038
}
5139

5240
opt.Env = append(opt.Env, opt.toEnv()...)
5341

54-
if serverProcessCancel == nil && os.Getenv("GPTSCRIPT_URL") == "" {
55-
if serverURL != "" {
56-
u, err := url.Parse(serverURL)
57-
if err != nil {
58-
return nil, fmt.Errorf("failed to parse server URL: %w", err)
59-
}
42+
lock.Lock()
43+
defer lock.Unlock()
44+
gptscriptCount++
6045

61-
// If the server URL has a path, then this implies that the server is already running.
62-
// In that case, we don't need to start the server.
63-
if u.Path != "" && u.Path != "/" {
64-
opt.URL = serverURL
65-
if !strings.HasPrefix(opt.URL, "http://") && !strings.HasPrefix(opt.URL, "https://") {
66-
opt.URL = "http://" + opt.URL
67-
}
68-
69-
opt.Env = append(opt.Env, "GPTSCRIPT_URL="+opt.URL)
70-
return &GPTScript{
71-
globalOpts: opt,
72-
}, nil
73-
}
74-
}
46+
startSDK := serverProcess == nil && serverURL == "" && opt.URL == ""
47+
if serverURL == "" {
48+
serverURL = os.Getenv("GPTSCRIPT_URL")
49+
startSDK = startSDK && serverURL == ""
50+
}
7551

52+
if startSDK {
7653
ctx, cancel := context.WithCancel(context.Background())
7754
in, _ := io.Pipe()
7855

79-
serverProcess = exec.CommandContext(ctx, getCommand(), "sys.sdkserver", "--listen-address", strings.TrimPrefix(serverURL, "http://"))
56+
serverProcess = exec.CommandContext(ctx, getCommand(), "sys.sdkserver", "--listen-address", "127.0.0.1:0")
8057
serverProcess.Env = opt.Env[:]
8158

8259
serverProcess.Stdin = in
@@ -118,13 +95,23 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
11895
serverURL = strings.TrimSpace(serverURL)
11996
}
12097

121-
opt.URL = serverURL
98+
if opt.URL == "" {
99+
opt.URL = serverURL
100+
}
101+
122102
if !strings.HasPrefix(opt.URL, "http://") && !strings.HasPrefix(opt.URL, "https://") {
123103
opt.URL = "http://" + opt.URL
124104
}
125105

126106
opt.Env = append(opt.Env, "GPTSCRIPT_URL="+opt.URL)
127107

108+
if opt.Token == "" {
109+
opt.Token = os.Getenv("GPTSCRIPT_TOKEN")
110+
}
111+
if opt.Token != "" {
112+
opt.Env = append(opt.Env, "GPTSCRIPT_TOKEN="+opt.Token)
113+
}
114+
128115
return &GPTScript{
129116
globalOpts: opt,
130117
}, nil

0 commit comments

Comments
 (0)