@@ -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,7 +51,23 @@ 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 = "http://" + serverURL
65
+ return & GPTScript {
66
+ globalOpts : opt ,
67
+ }, nil
68
+ }
69
+ }
70
+
54
71
ctx , cancel := context .WithCancel (context .Background ())
55
72
in , _ := io .Pipe ()
56
73
@@ -95,12 +112,11 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
95
112
96
113
serverURL = strings .TrimSpace (serverURL )
97
114
}
98
- g := & GPTScript {
99
- url : "http://" + serverURL ,
100
- globalOpts : opt ,
101
- }
102
115
103
- return g , nil
116
+ opt .URL = "http://" + serverURL
117
+ return & GPTScript {
118
+ globalOpts : opt ,
119
+ }, nil
104
120
}
105
121
106
122
func readAddress (stdErr io.Reader ) (string , error ) {
@@ -131,7 +147,8 @@ func (g *GPTScript) Close() {
131
147
func (g * GPTScript ) Evaluate (ctx context.Context , opts Options , tools ... ToolDef ) (* Run , error ) {
132
148
opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
133
149
return (& Run {
134
- url : g .url ,
150
+ url : opts .URL ,
151
+ token : opts .Token ,
135
152
requestPath : "evaluate" ,
136
153
state : Creating ,
137
154
opts : opts ,
@@ -142,7 +159,8 @@ func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef
142
159
func (g * GPTScript ) Run (ctx context.Context , toolPath string , opts Options ) (* Run , error ) {
143
160
opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
144
161
return (& Run {
145
- url : g .url ,
162
+ url : opts .URL ,
163
+ token : opts .Token ,
146
164
requestPath : "run" ,
147
165
state : Creating ,
148
166
opts : opts ,
@@ -319,7 +337,7 @@ func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) err
319
337
320
338
func (g * GPTScript ) runBasicCommand (ctx context.Context , requestPath string , body any ) (string , error ) {
321
339
run := & Run {
322
- url : g .url ,
340
+ url : g .globalOpts . URL ,
323
341
requestPath : requestPath ,
324
342
state : Creating ,
325
343
basicCommand : true ,
0 commit comments