1
1
import { SpawnOptions , spawn } from "child_process" ;
2
- import { TemplateFramework } from "./types" ;
2
+ import { ModelConfig , TemplateFramework } from "./types" ;
3
3
4
4
const createProcess = (
5
5
command : string ,
@@ -26,7 +26,43 @@ const createProcess = (
26
26
} ) ;
27
27
} ;
28
28
29
- export function runReflexApp ( appPath : string , port : number ) {
29
+ const getRunEnvs = ( modelConfig : ModelConfig , llamaCloudKey ?: string ) => {
30
+ let modelAPIKey ;
31
+ switch ( modelConfig . provider ) {
32
+ case "openai" :
33
+ modelAPIKey = { OPENAI_API_KEY : modelConfig . apiKey } ;
34
+ break ;
35
+ case "azure-openai" :
36
+ modelAPIKey = { AZURE_OPENAI_API_KEY : modelConfig . apiKey } ;
37
+ break ;
38
+ case "groq" :
39
+ modelAPIKey = { GROQ_API_KEY : modelConfig . apiKey } ;
40
+ break ;
41
+ case "anthropic" :
42
+ modelAPIKey = { ANTHROPIC_API_KEY : modelConfig . apiKey } ;
43
+ break ;
44
+ case "gemini" :
45
+ modelAPIKey = { GOOGLE_API_KEY : modelConfig . apiKey } ;
46
+ break ;
47
+ case "mistral" :
48
+ modelAPIKey = { MISTRAL_API_KEY : modelConfig . apiKey } ;
49
+ break ;
50
+ case "t-systems" :
51
+ modelAPIKey = { T_SYSTEMS_LLMHUB_API_KEY : modelConfig . apiKey } ;
52
+ break ;
53
+ }
54
+
55
+ return {
56
+ ...modelAPIKey ,
57
+ LLAMA_CLOUD_API_KEY : llamaCloudKey ,
58
+ } ;
59
+ } ;
60
+
61
+ export function runReflexApp (
62
+ appPath : string ,
63
+ port : number ,
64
+ envs : Record < string , string | undefined > ,
65
+ ) {
30
66
const commandArgs = [
31
67
"run" ,
32
68
"reflex" ,
@@ -37,29 +73,51 @@ export function runReflexApp(appPath: string, port: number) {
37
73
return createProcess ( "poetry" , commandArgs , {
38
74
stdio : "inherit" ,
39
75
cwd : appPath ,
76
+ env : {
77
+ ...process . env ,
78
+ ...envs ,
79
+ } ,
40
80
} ) ;
41
81
}
42
82
43
- export function runFastAPIApp ( appPath : string , port : number ) {
83
+ export function runFastAPIApp (
84
+ appPath : string ,
85
+ port : number ,
86
+ envs : Record < string , string | undefined > ,
87
+ ) {
44
88
return createProcess ( "poetry" , [ "run" , "dev" ] , {
45
89
stdio : "inherit" ,
46
90
cwd : appPath ,
47
- env : { ...process . env , APP_PORT : `${ port } ` } ,
91
+ env : {
92
+ ...process . env ,
93
+ ...envs ,
94
+ APP_PORT : `${ port } ` ,
95
+ } ,
48
96
} ) ;
49
97
}
50
98
51
- export function runTSApp ( appPath : string , port : number ) {
99
+ export function runTSApp (
100
+ appPath : string ,
101
+ port : number ,
102
+ envs : Record < string , string | undefined > ,
103
+ ) {
52
104
return createProcess ( "npm" , [ "run" , "dev" ] , {
53
105
stdio : "inherit" ,
54
106
cwd : appPath ,
55
- env : { ...process . env , PORT : `${ port } ` } ,
107
+ env : {
108
+ ...process . env ,
109
+ ...envs ,
110
+ PORT : `${ port } ` ,
111
+ } ,
56
112
} ) ;
57
113
}
58
114
59
115
export async function runApp (
60
116
appPath : string ,
61
117
template : string ,
62
118
framework : TemplateFramework ,
119
+ modelConfig : ModelConfig ,
120
+ llamaCloudKey ?: string ,
63
121
port ?: number ,
64
122
) : Promise < void > {
65
123
try {
@@ -73,7 +131,8 @@ export async function runApp(
73
131
: framework === "fastapi"
74
132
? runFastAPIApp
75
133
: runTSApp ;
76
- await appRunner ( appPath , port || defaultPort ) ;
134
+ const envs = getRunEnvs ( modelConfig , llamaCloudKey ) ;
135
+ await appRunner ( appPath , port || defaultPort , envs ) ;
77
136
} catch ( error ) {
78
137
console . error ( "Failed to run app:" , error ) ;
79
138
throw error ;
0 commit comments