@@ -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 ) {
@@ -117,6 +140,10 @@ func readAddress(stdErr io.Reader) (string, error) {
117
140
return addr , nil
118
141
}
119
142
143
+ func (g * GPTScript ) URL () string {
144
+ return g .globalOpts .URL
145
+ }
146
+
120
147
func (g * GPTScript ) Close () {
121
148
lock .Lock ()
122
149
defer lock .Unlock ()
@@ -131,7 +158,8 @@ func (g *GPTScript) Close() {
131
158
func (g * GPTScript ) Evaluate (ctx context.Context , opts Options , tools ... ToolDef ) (* Run , error ) {
132
159
opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
133
160
return (& Run {
134
- url : g .url ,
161
+ url : opts .URL ,
162
+ token : opts .Token ,
135
163
requestPath : "evaluate" ,
136
164
state : Creating ,
137
165
opts : opts ,
@@ -142,7 +170,8 @@ func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef
142
170
func (g * GPTScript ) Run (ctx context.Context , toolPath string , opts Options ) (* Run , error ) {
143
171
opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
144
172
return (& Run {
145
- url : g .url ,
173
+ url : opts .URL ,
174
+ token : opts .Token ,
146
175
requestPath : "run" ,
147
176
state : Creating ,
148
177
opts : opts ,
@@ -309,7 +338,7 @@ func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) err
309
338
310
339
func (g * GPTScript ) runBasicCommand (ctx context.Context , requestPath string , body any ) (string , error ) {
311
340
run := & Run {
312
- url : g .url ,
341
+ url : g .globalOpts . URL ,
313
342
requestPath : requestPath ,
314
343
state : Creating ,
315
344
basicCommand : true ,
0 commit comments