@@ -10,6 +10,7 @@ import (
10
10
"fmt"
11
11
"io"
12
12
"log/slog"
13
+ "net/url"
13
14
"os"
14
15
"os/exec"
15
16
"path/filepath"
28
29
const relativeToBinaryPath = "<me>"
29
30
30
31
type GPTScript struct {
31
- url string
32
32
globalOpts GlobalOptions
33
33
}
34
34
@@ -38,10 +38,11 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
38
38
defer lock .Unlock ()
39
39
gptscriptCount ++
40
40
41
- disableServer := os .Getenv ("GPTSCRIPT_DISABLE_SERVER" ) == "true"
42
-
43
41
if serverURL == "" {
44
- serverURL = os .Getenv ("GPTSCRIPT_URL" )
42
+ serverURL = opt .URL
43
+ if serverURL == "" {
44
+ serverURL = os .Getenv ("GPTSCRIPT_URL" )
45
+ }
45
46
}
46
47
47
48
if opt .Env == nil {
@@ -50,11 +51,31 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
50
51
51
52
opt .Env = append (opt .Env , opt .toEnv ()... )
52
53
53
- if serverProcessCancel == nil && ! disableServer {
54
+ if serverProcessCancel == nil && os .Getenv ("GPTSCRIPT_DISABLE_SERVER" ) != "true" {
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
+ }
60
+
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
+ return & GPTScript {
70
+ globalOpts : opt ,
71
+ }, nil
72
+ }
73
+ }
74
+
54
75
ctx , cancel := context .WithCancel (context .Background ())
55
76
in , _ := io .Pipe ()
56
77
57
- serverProcess = exec .CommandContext (ctx , getCommand (), "sys.sdkserver" , "--listen-address" , serverURL )
78
+ serverProcess = exec .CommandContext (ctx , getCommand (), "sys.sdkserver" , "--listen-address" , strings . TrimPrefix ( serverURL , "http://" ) )
58
79
serverProcess .Env = opt .Env [:]
59
80
60
81
serverProcess .Stdin = in
@@ -95,12 +116,14 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
95
116
96
117
serverURL = strings .TrimSpace (serverURL )
97
118
}
98
- g := & GPTScript {
99
- url : "http://" + serverURL ,
100
- globalOpts : opt ,
101
- }
102
119
103
- return g , nil
120
+ opt .URL = serverURL
121
+ if ! strings .HasPrefix (opt .URL , "http://" ) && ! strings .HasPrefix (opt .URL , "https://" ) {
122
+ opt .URL = "http://" + opt .URL
123
+ }
124
+ return & GPTScript {
125
+ globalOpts : opt ,
126
+ }, nil
104
127
}
105
128
106
129
func readAddress (stdErr io.Reader ) (string , error ) {
@@ -131,7 +154,8 @@ func (g *GPTScript) Close() {
131
154
func (g * GPTScript ) Evaluate (ctx context.Context , opts Options , tools ... ToolDef ) (* Run , error ) {
132
155
opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
133
156
return (& Run {
134
- url : g .url ,
157
+ url : opts .URL ,
158
+ token : opts .Token ,
135
159
requestPath : "evaluate" ,
136
160
state : Creating ,
137
161
opts : opts ,
@@ -142,7 +166,8 @@ func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef
142
166
func (g * GPTScript ) Run (ctx context.Context , toolPath string , opts Options ) (* Run , error ) {
143
167
opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
144
168
return (& Run {
145
- url : g .url ,
169
+ url : opts .URL ,
170
+ token : opts .Token ,
146
171
requestPath : "run" ,
147
172
state : Creating ,
148
173
opts : opts ,
@@ -319,7 +344,7 @@ func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) err
319
344
320
345
func (g * GPTScript ) runBasicCommand (ctx context.Context , requestPath string , body any ) (string , error ) {
321
346
run := & Run {
322
- url : g .url ,
347
+ url : g .globalOpts . URL ,
323
348
requestPath : requestPath ,
324
349
state : Creating ,
325
350
basicCommand : true ,
0 commit comments