@@ -10,6 +10,7 @@ import (
1010 "fmt"
1111 "io"
1212 "log/slog"
13+ "net/url"
1314 "os"
1415 "os/exec"
1516 "path/filepath"
2829const relativeToBinaryPath = "<me>"
2930
3031type GPTScript struct {
31- url string
3232 globalOpts GlobalOptions
3333}
3434
@@ -38,10 +38,11 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
3838 defer lock .Unlock ()
3939 gptscriptCount ++
4040
41- disableServer := os .Getenv ("GPTSCRIPT_DISABLE_SERVER" ) == "true"
42-
4341 if serverURL == "" {
44- serverURL = os .Getenv ("GPTSCRIPT_URL" )
42+ serverURL = opt .URL
43+ if serverURL == "" {
44+ serverURL = os .Getenv ("GPTSCRIPT_URL" )
45+ }
4546 }
4647
4748 if opt .Env == nil {
@@ -50,7 +51,23 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
5051
5152 opt .Env = append (opt .Env , opt .toEnv ()... )
5253
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+
5471 ctx , cancel := context .WithCancel (context .Background ())
5572 in , _ := io .Pipe ()
5673
@@ -95,12 +112,11 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
95112
96113 serverURL = strings .TrimSpace (serverURL )
97114 }
98- g := & GPTScript {
99- url : "http://" + serverURL ,
100- globalOpts : opt ,
101- }
102115
103- return g , nil
116+ opt .URL = "http://" + serverURL
117+ return & GPTScript {
118+ globalOpts : opt ,
119+ }, nil
104120}
105121
106122func readAddress (stdErr io.Reader ) (string , error ) {
@@ -131,7 +147,8 @@ func (g *GPTScript) Close() {
131147func (g * GPTScript ) Evaluate (ctx context.Context , opts Options , tools ... ToolDef ) (* Run , error ) {
132148 opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
133149 return (& Run {
134- url : g .url ,
150+ url : opts .URL ,
151+ token : opts .Token ,
135152 requestPath : "evaluate" ,
136153 state : Creating ,
137154 opts : opts ,
@@ -142,7 +159,8 @@ func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef
142159func (g * GPTScript ) Run (ctx context.Context , toolPath string , opts Options ) (* Run , error ) {
143160 opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
144161 return (& Run {
145- url : g .url ,
162+ url : opts .URL ,
163+ token : opts .Token ,
146164 requestPath : "run" ,
147165 state : Creating ,
148166 opts : opts ,
@@ -319,7 +337,7 @@ func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) err
319337
320338func (g * GPTScript ) runBasicCommand (ctx context.Context , requestPath string , body any ) (string , error ) {
321339 run := & Run {
322- url : g .url ,
340+ url : g .globalOpts . URL ,
323341 requestPath : requestPath ,
324342 state : Creating ,
325343 basicCommand : true ,
0 commit comments