-
-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathstart.js
More file actions
343 lines (293 loc) · 10.6 KB
/
start.js
File metadata and controls
343 lines (293 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* This script starts the FastAPI and Next.js servers, setting up user configuration if necessary. It reads environment variables to configure API keys and other settings, ensuring that the user configuration file is created if it doesn't exist. The script also handles the starting of both servers and keeps the Node.js process alive until one of the servers exits. */
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { spawn } from "child_process";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const fastapiDir = join(__dirname, "servers/fastapi");
const nextjsDir = join(__dirname, "servers/nextjs");
const nextjsStandaloneServer = join(nextjsDir, "server.js");
const exportSyncScript = join(__dirname, "scripts/sync-presentation-export.cjs");
const args = process.argv.slice(2);
const hasDevArg = args.includes("--dev") || args.includes("-d");
const isDev = hasDevArg;
const canChangeKeys = process.env.CAN_CHANGE_KEYS !== "false";
const fastapiPort = 8000;
const nextjsPort = 3000;
const appmcpPort = 8001;
const userConfigPath = join(process.env.APP_DATA_DIRECTORY, "userConfig.json");
const userDataDir = dirname(userConfigPath);
// Create user_data directory if it doesn't exist
if (!existsSync(userDataDir)) {
mkdirSync(userDataDir, { recursive: true });
}
// Setup node_modules for development
const setupNodeModules = () => {
return new Promise((resolve, reject) => {
console.log("Setting up node_modules for Next.js...");
const npmProcess = spawn("npm", ["install"], {
cwd: nextjsDir,
stdio: "inherit",
env: process.env,
});
npmProcess.on("error", (err) => {
console.error("npm install failed:", err);
reject(err);
});
npmProcess.on("exit", (code) => {
if (code === 0) {
console.log("npm install completed successfully");
resolve();
} else {
console.error(`npm install failed with exit code: ${code}`);
reject(new Error(`npm install failed with exit code: ${code}`));
}
});
});
};
const runCommand = (command, commandArgs, options = {}) => {
return new Promise((resolve, reject) => {
const child = spawn(command, commandArgs, {
cwd: options.cwd || __dirname,
stdio: options.stdio || "inherit",
env: options.env || process.env,
});
child.on("error", (err) => {
reject(err);
});
child.on("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`${command} exited with code: ${code}`));
}
});
});
};
const runNodeScript = (scriptPath, scriptArgs) => {
return runCommand(process.execPath, [scriptPath, ...scriptArgs], {
cwd: __dirname,
});
};
const isTruthyEnv = (value) => {
if (value == null) {
return false;
}
return !["", "0", "false", "no", "off"].includes(
String(value).trim().toLowerCase()
);
};
const isOllamaInstalled = () =>
existsSync("/usr/bin/ollama") || existsSync("/usr/local/bin/ollama");
const shouldStartOllama = () => isTruthyEnv(process.env.START_OLLAMA);
const ensureOllamaRuntime = async () => {
if (!shouldStartOllama() || isOllamaInstalled()) {
return;
}
console.log("START_OLLAMA=true; installing Ollama runtime...");
await runCommand("sh", ["-c", "curl -fsSL https://ollama.com/install.sh | sh"], {
cwd: "/",
});
};
const ensurePresentationExportRuntime = async () => {
if (process.env.ENSURE_PRESENTATION_EXPORT_RUNTIME === "false") {
return;
}
if (!existsSync(exportSyncScript)) {
console.warn("presentation-export sync script not found; skipping runtime check");
return;
}
try {
await runNodeScript(exportSyncScript, ["--check-only"]);
} catch (err) {
if (!isDev) {
throw new Error(
"presentation-export runtime is missing in this container image. Rebuild the image so the runtime package is installed."
);
}
console.warn("presentation-export runtime missing in dev mount. Syncing runtime package...");
await runNodeScript(exportSyncScript, ["--force"]);
}
};
process.env.USER_CONFIG_PATH = userConfigPath;
// Let Next.js middleware reach FastAPI over the loopback interface inside the
// container without having to bounce through nginx (the host-facing port is
// not reachable from inside the Next.js process).
if (!process.env.FAST_API_INTERNAL_URL) {
process.env.FAST_API_INTERNAL_URL = `http://127.0.0.1:${fastapiPort}`;
}
//? UserConfig is only setup if API Keys can be changed
const setupUserConfigFromEnv = () => {
let existingConfig = {};
if (existsSync(userConfigPath)) {
existingConfig = JSON.parse(readFileSync(userConfigPath, "utf8"));
}
if (!["ollama", "openai", "google", "anthropic", "custom", "codex"].includes(existingConfig.LLM)) {
existingConfig.LLM = undefined;
}
const userConfig = {
LLM: process.env.LLM || existingConfig.LLM,
OPENAI_API_KEY: process.env.OPENAI_API_KEY || existingConfig.OPENAI_API_KEY,
OPENAI_MODEL: process.env.OPENAI_MODEL || existingConfig.OPENAI_MODEL,
GOOGLE_API_KEY: process.env.GOOGLE_API_KEY || existingConfig.GOOGLE_API_KEY,
GOOGLE_MODEL: process.env.GOOGLE_MODEL || existingConfig.GOOGLE_MODEL,
OLLAMA_URL: process.env.OLLAMA_URL || existingConfig.OLLAMA_URL,
OLLAMA_MODEL: process.env.OLLAMA_MODEL || existingConfig.OLLAMA_MODEL,
ANTHROPIC_API_KEY:
process.env.ANTHROPIC_API_KEY || existingConfig.ANTHROPIC_API_KEY,
ANTHROPIC_MODEL:
process.env.ANTHROPIC_MODEL || existingConfig.ANTHROPIC_MODEL,
CUSTOM_LLM_URL: process.env.CUSTOM_LLM_URL || existingConfig.CUSTOM_LLM_URL,
CUSTOM_LLM_API_KEY:
process.env.CUSTOM_LLM_API_KEY || existingConfig.CUSTOM_LLM_API_KEY,
CUSTOM_MODEL: process.env.CUSTOM_MODEL || existingConfig.CUSTOM_MODEL,
PEXELS_API_KEY: process.env.PEXELS_API_KEY || existingConfig.PEXELS_API_KEY,
PIXABAY_API_KEY:
process.env.PIXABAY_API_KEY || existingConfig.PIXABAY_API_KEY,
IMAGE_PROVIDER: process.env.IMAGE_PROVIDER || existingConfig.IMAGE_PROVIDER,
DISABLE_THINKING:
process.env.DISABLE_THINKING || existingConfig.DISABLE_THINKING,
EXTENDED_REASONING:
process.env.EXTENDED_REASONING || existingConfig.EXTENDED_REASONING,
WEB_GROUNDING: process.env.WEB_GROUNDING || existingConfig.WEB_GROUNDING,
USE_CUSTOM_URL: process.env.USE_CUSTOM_URL || existingConfig.USE_CUSTOM_URL,
COMFYUI_URL: process.env.COMFYUI_URL || existingConfig.COMFYUI_URL,
COMFYUI_WORKFLOW:
process.env.COMFYUI_WORKFLOW || existingConfig.COMFYUI_WORKFLOW,
DALL_E_3_QUALITY:
process.env.DALL_E_3_QUALITY || existingConfig.DALL_E_3_QUALITY,
GPT_IMAGE_1_5_QUALITY:
process.env.GPT_IMAGE_1_5_QUALITY || existingConfig.GPT_IMAGE_1_5_QUALITY,
CODEX_MODEL: process.env.CODEX_MODEL || existingConfig.CODEX_MODEL,
CODEX_ACCESS_TOKEN: existingConfig.CODEX_ACCESS_TOKEN,
CODEX_REFRESH_TOKEN: existingConfig.CODEX_REFRESH_TOKEN,
CODEX_TOKEN_EXPIRES: existingConfig.CODEX_TOKEN_EXPIRES,
CODEX_ACCOUNT_ID: existingConfig.CODEX_ACCOUNT_ID,
AUTH_USERNAME: existingConfig.AUTH_USERNAME,
AUTH_PASSWORD_HASH: existingConfig.AUTH_PASSWORD_HASH,
AUTH_SECRET_KEY: existingConfig.AUTH_SECRET_KEY,
};
writeFileSync(userConfigPath, JSON.stringify(userConfig));
};
const startServers = async () => {
const fastApiProcess = spawn(
"python",
[
"server.py",
"--port",
fastapiPort.toString(),
"--reload",
isDev ? "true" : "false",
],
{
cwd: fastapiDir,
stdio: "inherit",
env: process.env,
}
);
fastApiProcess.on("error", (err) => {
console.error("FastAPI process failed to start:", err);
});
const appmcpProcess = spawn(
"python",
["mcp_server.py", "--port", appmcpPort.toString()],
{
cwd: fastapiDir,
stdio: "ignore",
env: process.env,
}
);
appmcpProcess.on("error", (err) => {
console.error("App MCP process failed to start:", err);
});
const useStandaloneNextjs = !isDev && existsSync(nextjsStandaloneServer);
const nextjsProcess = spawn(
useStandaloneNextjs ? process.execPath : "npm",
useStandaloneNextjs
? [nextjsStandaloneServer]
: [
"run",
isDev ? "dev" : "start",
"--",
"-H",
"127.0.0.1",
"-p",
nextjsPort.toString(),
],
{
cwd: nextjsDir,
stdio: "inherit",
env:
useStandaloneNextjs
? {
...process.env,
HOSTNAME: "127.0.0.1",
PORT: nextjsPort.toString(),
}
: process.env,
}
);
nextjsProcess.on("error", (err) => {
console.error("Next.js process failed to start:", err);
});
const shouldStartOllamaRuntime = shouldStartOllama();
const ollamaInstalled = isOllamaInstalled();
const exitPromises = [
new Promise((resolve) => fastApiProcess.on("exit", resolve)),
new Promise((resolve) => nextjsProcess.on("exit", resolve)),
];
if (shouldStartOllamaRuntime && ollamaInstalled) {
const ollamaProcess = spawn("ollama", ["serve"], {
cwd: "/",
stdio: "inherit",
env: process.env,
});
ollamaProcess.on("error", (err) => {
console.error("Ollama process failed to start:", err);
});
exitPromises.push(new Promise((resolve) => ollamaProcess.on("exit", resolve)));
} else if (shouldStartOllamaRuntime) {
console.log(
"Ollama requested, but the binary is not installed. Set START_OLLAMA=true to install it at startup, or set OLLAMA_URL to a remote daemon."
);
} else {
console.log(
"Ollama disabled (START_OLLAMA=false); use OLLAMA_URL for a remote daemon if needed."
);
}
// Keep the Node process alive until one of the servers exits
const exitCode = await Promise.race(exitPromises);
console.log(`One of the processes exited. Exit code: ${exitCode}`);
process.exit(exitCode);
};
// Start nginx service
const startNginx = () => {
const nginxProcess = spawn("service", ["nginx", "start"], {
stdio: "inherit",
env: process.env,
});
nginxProcess.on("error", (err) => {
console.error("Nginx process failed to start:", err);
});
nginxProcess.on("exit", (code) => {
if (code === 0) {
console.log("Nginx started successfully");
} else {
console.error(`Nginx failed to start with exit code: ${code}`);
}
});
};
const main = async () => {
await ensurePresentationExportRuntime();
await ensureOllamaRuntime();
if (isDev) {
await setupNodeModules();
}
if (canChangeKeys) {
setupUserConfigFromEnv();
}
startServers();
startNginx();
};
main();