@@ -8,25 +8,23 @@ export interface GlobalOpts {
8
8
APIKey ?: string
9
9
BaseURL ?: string
10
10
DefaultModel ?: string
11
+ Env ?: string [ ]
11
12
}
12
13
13
- function globalOptsToArgs ( opts ?: GlobalOpts ) : string [ ] {
14
- const args : string [ ] = [ ]
14
+ function globalOptsToEnv ( env : NodeJS . ProcessEnv , opts ?: GlobalOpts ) {
15
15
if ( ! opts ) {
16
- return args
16
+ return
17
17
}
18
18
19
19
if ( opts . APIKey ) {
20
- args . push ( "--openai-api-key" , opts . APIKey )
20
+ env [ "OPENAI_API_KEY" ] = opts . APIKey
21
21
}
22
22
if ( opts . BaseURL ) {
23
- args . push ( "--openai-base-url" , opts . BaseURL )
23
+ env [ "OPENAI_BASE_URL" ] = opts . BaseURL
24
24
}
25
25
if ( opts . DefaultModel ) {
26
- args . push ( "--default-model" , opts . DefaultModel )
26
+ env [ "GPTSCRIPT_DEFAULT_MODEL" ] = opts . DefaultModel
27
27
}
28
-
29
- return args
30
28
}
31
29
32
30
export interface RunOpts {
@@ -83,10 +81,23 @@ export class GPTScript {
83
81
GPTScript . serverURL = "http://" + u . hostname + ":" + String ( ( s . address ( ) as net . AddressInfo ) . port )
84
82
srv . close ( )
85
83
86
- const args = globalOptsToArgs ( opts )
87
- args . push ( "--listen-address" , GPTScript . serverURL . replace ( "http://" , "" ) , "sdkserver" )
88
- GPTScript . serverProcess = child_process . spawn ( getCmdPath ( ) , args , {
89
- env : process . env ,
84
+ let env = process . env
85
+ if ( opts && opts . Env ) {
86
+ env = { }
87
+ for ( const v of opts . Env ) {
88
+ const equalIndex = v . indexOf ( "=" )
89
+ if ( equalIndex === - 1 ) {
90
+ env [ v ] = ""
91
+ } else {
92
+ env [ v . substring ( 0 , equalIndex ) ] = v . substring ( equalIndex + 1 )
93
+ }
94
+ }
95
+ }
96
+
97
+ globalOptsToEnv ( env , opts )
98
+
99
+ GPTScript . serverProcess = child_process . spawn ( getCmdPath ( ) , [ "--listen-address" , GPTScript . serverURL . replace ( "http://" , "" ) , "sdkserver" ] , {
100
+ env : env ,
90
101
stdio : [ "pipe" ]
91
102
} )
92
103
0 commit comments