@@ -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,11 +51,31 @@ 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 = 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+
5475 ctx , cancel := context .WithCancel (context .Background ())
5576 in , _ := io .Pipe ()
5677
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://" ) )
5879 serverProcess .Env = opt .Env [:]
5980
6081 serverProcess .Stdin = in
@@ -95,12 +116,14 @@ func NewGPTScript(opts ...GlobalOptions) (*GPTScript, error) {
95116
96117 serverURL = strings .TrimSpace (serverURL )
97118 }
98- g := & GPTScript {
99- url : "http://" + serverURL ,
100- globalOpts : opt ,
101- }
102119
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
104127}
105128
106129func readAddress (stdErr io.Reader ) (string , error ) {
@@ -117,6 +140,10 @@ func readAddress(stdErr io.Reader) (string, error) {
117140 return addr , nil
118141}
119142
143+ func (g * GPTScript ) URL () string {
144+ return g .globalOpts .URL
145+ }
146+
120147func (g * GPTScript ) Close () {
121148 lock .Lock ()
122149 defer lock .Unlock ()
@@ -131,7 +158,8 @@ func (g *GPTScript) Close() {
131158func (g * GPTScript ) Evaluate (ctx context.Context , opts Options , tools ... ToolDef ) (* Run , error ) {
132159 opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
133160 return (& Run {
134- url : g .url ,
161+ url : opts .URL ,
162+ token : opts .Token ,
135163 requestPath : "evaluate" ,
136164 state : Creating ,
137165 opts : opts ,
@@ -142,7 +170,8 @@ func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef
142170func (g * GPTScript ) Run (ctx context.Context , toolPath string , opts Options ) (* Run , error ) {
143171 opts .GlobalOptions = completeGlobalOptions (g .globalOpts , opts .GlobalOptions )
144172 return (& Run {
145- url : g .url ,
173+ url : opts .URL ,
174+ token : opts .Token ,
146175 requestPath : "run" ,
147176 state : Creating ,
148177 opts : opts ,
@@ -309,7 +338,7 @@ func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) err
309338
310339func (g * GPTScript ) runBasicCommand (ctx context.Context , requestPath string , body any ) (string , error ) {
311340 run := & Run {
312- url : g .url ,
341+ url : g .globalOpts . URL ,
313342 requestPath : requestPath ,
314343 state : Creating ,
315344 basicCommand : true ,
0 commit comments