-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathserver.ts
More file actions
436 lines (407 loc) · 15.7 KB
/
Copy pathserver.ts
File metadata and controls
436 lines (407 loc) · 15.7 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import "dotenv/config";
import express from "express";
import type { Response } from "express";
import { readFile } from "fs/promises";
import {
existsSync,
writeFileSync,
unlinkSync,
mkdirSync,
readFileSync as fsReadFileSync,
} from "fs";
import { dirname, join } from "path";
import { fileURLToPath, pathToFileURL } from "url";
import { onShutdown } from "./bin/lib/platform.js";
import { ensureDefaultSession } from "./lib/sessionStore.js";
import { startGrokProxy } from "./lib/grokProxyLauncher.js";
import { startOAuthProxy } from "./lib/oauthLauncher.js";
import { migrateGeneratedStorage } from "./lib/storageMigration.js";
import { purgeStaleJobs } from "./lib/inflight.js";
import { configureLogger } from "./lib/logger.js";
import { createRequestLogger } from "./lib/requestLogger.js";
import { configureApiCachePolicy } from "./lib/apiCachePolicy.js";
import { configureRoutes } from "./routes/index.js";
import { config } from "./config.js";
import { getServerPort, listenWithPortFallback } from "./lib/runtimePorts.js";
import type { RuntimeContext, RuntimeContextOverrides, ApiKeySource } from "./lib/runtimeContext.js";
import { closeDb } from "./lib/db.js";
import { stopAgentQueueWorker } from "./lib/agentQueueWorker.js";
import { reapCardNewsJobs } from "./lib/cardNewsJobStore.js";
import { reapTerminalJobs } from "./lib/inflight.js";
import { errInfo } from "./lib/errInfo.js";
type BootRuntimeContext = RuntimeContext & {
markGrokProxyPort: (info?: { url?: string; port?: number }) => void;
markOAuthReady: (info?: { url?: string; port?: number }) => void;
markOAuthFailed: () => void;
};
type ApiKeyLoadResult = { apiKey: string | null; apiKeySource: ApiKeySource };
const rootDir = dirname(fileURLToPath(import.meta.url));
async function loadApiKey(): Promise<ApiKeyLoadResult> {
if (process.env.OPENAI_API_KEY) {
return { apiKey: process.env.OPENAI_API_KEY, apiKeySource: "env" };
}
const candidates = [
config.storage.configFile,
join(rootDir, ".ima2", "config.json"),
];
for (const cfgPath of candidates) {
if (!existsSync(cfgPath)) continue;
try {
const cfg = JSON.parse(await readFile(cfgPath, "utf-8")) as { apiKey?: string };
if (cfg.apiKey) return { apiKey: cfg.apiKey, apiKeySource: "config" };
} catch {}
}
return { apiKey: null, apiKeySource: "none" };
}
async function loadXaiApiKey(): Promise<ApiKeyLoadResult> {
if (process.env.XAI_API_KEY) {
return { apiKey: process.env.XAI_API_KEY, apiKeySource: "env" };
}
const candidates = [
config.storage.configFile,
join(rootDir, ".ima2", "config.json"),
];
for (const cfgPath of candidates) {
if (!existsSync(cfgPath)) continue;
try {
const cfg = JSON.parse(await readFile(cfgPath, "utf-8")) as { xaiApiKey?: string };
if (cfg.xaiApiKey) return { apiKey: cfg.xaiApiKey, apiKeySource: "config" };
} catch {}
}
return { apiKey: null, apiKeySource: "none" };
}
async function loadGeminiApiKey(): Promise<ApiKeyLoadResult> {
if (process.env.GEMINI_API_KEY) {
return { apiKey: process.env.GEMINI_API_KEY, apiKeySource: "env" };
}
const candidates = [
config.storage.configFile,
join(rootDir, ".ima2", "config.json"),
];
for (const cfgPath of candidates) {
if (!existsSync(cfgPath)) continue;
try {
const cfg = JSON.parse(await readFile(cfgPath, "utf-8")) as { geminiApiKey?: string };
if (cfg.geminiApiKey) return { apiKey: cfg.geminiApiKey, apiKeySource: "config" };
} catch {}
}
return { apiKey: null, apiKeySource: "none" };
}
type VertexKeyLoadResult = { json: string | null; projectId: string | null; source: ApiKeySource };
async function loadVertexKey(): Promise<VertexKeyLoadResult> {
const envJson = process.env.VERTEX_SERVICE_ACCOUNT_JSON;
if (envJson) {
try {
const parsed = JSON.parse(envJson);
return { json: envJson, projectId: parsed.project_id || null, source: "env" };
} catch {
return { json: null, projectId: null, source: "none" };
}
}
const candidates = [
config.storage.configFile,
join(rootDir, ".ima2", "config.json"),
];
for (const cfgPath of candidates) {
if (!existsSync(cfgPath)) continue;
try {
const cfg = JSON.parse(await readFile(cfgPath, "utf-8")) as { vertexServiceAccountJson?: string };
if (cfg.vertexServiceAccountJson) {
const parsed = JSON.parse(cfg.vertexServiceAccountJson);
return { json: cfg.vertexServiceAccountJson, projectId: parsed.project_id || null, source: "config" };
}
} catch {}
}
return { json: null, projectId: null, source: "none" };
}
async function loadGeminiAuthMode(): Promise<string | undefined> {
const candidates = [
config.storage.configFile,
join(rootDir, ".ima2", "config.json"),
];
for (const cfgPath of candidates) {
if (!existsSync(cfgPath)) continue;
try {
const cfg = JSON.parse(await readFile(cfgPath, "utf-8")) as { geminiAuthMode?: string };
if (cfg.geminiAuthMode === "vertex" || cfg.geminiAuthMode === "apikey") return cfg.geminiAuthMode;
} catch {}
}
return undefined;
}
async function createOpenAI(apiKey: string | null | undefined) {
if (!apiKey) return null;
const OpenAI = (await import("openai")).default;
return new OpenAI({ apiKey });
}
function readPackageVersion(): string {
try {
return (JSON.parse(fsReadFileSync(join(rootDir, "package.json"), "utf-8")) as { version?: string }).version ?? "0.0.0";
} catch {
return "0.0.0";
}
}
function setUiStaticHeaders(res: Response, filePath: string) {
const normalized = filePath.replace(/\\/g, "/");
if (normalized.endsWith("/index.html")) {
res.setHeader("Cache-Control", "no-store, max-age=0");
return;
}
if (normalized.includes("/assets/")) {
res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
}
}
export function buildApp(ctx: RuntimeContext) {
const app = express();
configureApiCachePolicy(app);
configureLogger({ level: ctx.config.log.level });
app.use(createRequestLogger());
app.use(express.json({ limit: ctx.config.server.bodyLimit }));
app.use(express.static(join(ctx.rootDir, "ui", "dist"), {
setHeaders: setUiStaticHeaders,
}));
app.use("/assets", (_req, res) => {
res.status(404).type("text/plain").send("Asset not found");
});
app.use("/generated", (req, res, next) => {
if (req.path.endsWith(".json")) return res.status(404).type("text/plain").send("Generated metadata is not public");
return next();
}, express.static(ctx.config.storage.generatedDir, {
maxAge: ctx.config.storage.staticMaxAge,
immutable: true,
}));
configureRoutes(app, ctx);
return app;
}
function runtimeHostUrl(host: string | undefined): string {
if (!host || host === "0.0.0.0" || host === "::") return "localhost";
return host;
}
function advertise(ctx: RuntimeContext) {
try {
mkdirSync(dirname(ctx.config.storage.advertiseFile), { recursive: true });
writeFileSync(
ctx.config.storage.advertiseFile,
JSON.stringify({
port: Number(ctx.serverActualPort || ctx.config.server.port),
url: ctx.serverUrl,
pid: process.pid,
startedAt: ctx.startedAt,
version: ctx.packageVersion,
backend: {
configuredPort: Number(ctx.serverConfiguredPort || ctx.config.server.port),
actualPort: Number(ctx.serverActualPort || ctx.config.server.port),
url: ctx.serverUrl,
},
oauth: {
configuredPort: Number(ctx.oauthPort),
actualPort: Number(ctx.oauthActualPort || ctx.oauthPort),
url: ctx.oauthUrl,
status: ctx.oauthReadyState,
},
grok: {
configuredPort: Number(ctx.grokPort),
actualPort: Number(ctx.grokActualPort || ctx.grokPort),
url: ctx.grokUrl,
},
}),
);
} catch (e) {
const err = errInfo(e);
console.warn("[advertise] skipped:", err.message);
}
}
function unadvertise(ctx: RuntimeContext) {
try {
if (!existsSync(ctx.config.storage.advertiseFile)) return;
const cur = JSON.parse(fsReadFileSync(ctx.config.storage.advertiseFile, "utf-8")) as { pid?: number };
if (cur.pid === process.pid) unlinkSync(ctx.config.storage.advertiseFile);
} catch {}
}
type StartServerOverrides = RuntimeContextOverrides & {
startedAt?: number;
packageVersion?: string;
oauthChild?: { stop?: () => void; kill?: () => void } | null;
};
export async function createRuntimeContext(overrides: StartServerOverrides = {}): Promise<BootRuntimeContext> {
const loadedKey =
overrides.apiKey !== undefined
? {
apiKey: overrides.apiKey,
apiKeySource: overrides.apiKeySource ?? (overrides.apiKey ? "env" : "none"),
}
: await loadApiKey();
const loadedXaiKey = await loadXaiApiKey();
const loadedGeminiKey = await loadGeminiApiKey();
const loadedVertexKey = await loadVertexKey();
const geminiAuthMode = await loadGeminiAuthMode();
const apiKey = loadedKey.apiKey;
const openai = overrides.openai ?? await createOpenAI(apiKey);
const oauthPort = config.oauth.proxyPort;
const grokPort = config.grokProvider.proxyPort;
let resolveOAuthReady: (value: string | null) => void = () => {};
const oauthReadyPromise = new Promise<string | null>((resolve) => {
resolveOAuthReady = resolve;
});
const ctx: BootRuntimeContext = {
rootDir,
config,
serverConfiguredPort: config.server.port,
serverActualPort: undefined,
serverUrl: `http://${runtimeHostUrl(config.server.host)}:${config.server.port}`,
grokPort,
grokActualPort: grokPort,
grokUrl: `http://${config.grokProvider.proxyHost}:${grokPort}/v1`,
oauthPort,
oauthActualPort: oauthPort,
oauthUrl: `http://127.0.0.1:${oauthPort}`,
oauthReadyState: config.oauth.autoStart ? "starting" : "disabled",
hasApiKey: !!apiKey,
apiKey: apiKey ?? undefined,
apiKeySource: loadedKey.apiKeySource as ApiKeySource,
openai,
startedAt: overrides.startedAt ?? Date.now(),
packageVersion: overrides.packageVersion ?? readPackageVersion(),
xaiApiKey: loadedXaiKey.apiKey ?? undefined,
xaiApiKeySource: loadedXaiKey.apiKeySource as ApiKeySource,
hasXaiApiKey: !!loadedXaiKey.apiKey,
geminiApiKey: loadedGeminiKey.apiKey ?? undefined,
geminiApiKeySource: loadedGeminiKey.apiKeySource as ApiKeySource,
hasGeminiApiKey: !!loadedGeminiKey.apiKey,
vertexServiceAccountJson: loadedVertexKey.json ?? undefined,
vertexProjectId: loadedVertexKey.projectId ?? undefined,
hasVertexKey: !!loadedVertexKey.json,
geminiAuthMode,
oauthReadyPromise: oauthReadyPromise as unknown as Promise<void>,
markGrokProxyPort: ({ url, port }: { url?: string; port?: number } = {}) => {
if (port) ctx.grokActualPort = port;
if (url) ctx.grokUrl = url;
else if (port) ctx.grokUrl = `http://${ctx.config.grokProvider.proxyHost}:${port}/v1`;
},
markOAuthReady: ({ url, port }: { url?: string; port?: number } = {}) => {
if (url) ctx.oauthUrl = url;
if (port) ctx.oauthActualPort = port;
ctx.oauthReadyState = "ready";
resolveOAuthReady(ctx.oauthUrl);
},
markOAuthFailed: () => {
ctx.oauthReadyState = "failed";
resolveOAuthReady(null);
},
};
if (!config.oauth.autoStart) ctx.markOAuthReady({ url: ctx.oauthUrl, port: ctx.oauthPort });
if (loadedVertexKey.json) {
try {
const { initVertexAuth } = await import("./lib/vertexAuth.js");
initVertexAuth(loadedVertexKey.json);
} catch { /* vertex init failure is non-fatal */ }
}
return ctx;
}
export async function startServer(overrides: StartServerOverrides = {}) {
const ctx = await createRuntimeContext(overrides);
await migrateGeneratedStorage(ctx);
purgeStaleJobs();
const app = buildApp(ctx);
const oauthChild =
overrides.oauthChild !== undefined
? overrides.oauthChild
: !ctx.config.oauth.autoStart
? null
: startOAuthProxy({
oauthPort: ctx.oauthPort,
restartDelayMs: ctx.config.oauth.restartDelayMs,
onReady: ({ url, port }: { url: string; port: number }) => {
ctx.markOAuthReady({ url, port });
advertise(ctx);
},
onExit: () => ctx.markOAuthFailed(),
});
if (overrides.oauthChild !== undefined || !ctx.config.oauth.autoStart) {
ctx.markOAuthReady({ url: ctx.oauthUrl, port: ctx.oauthPort });
}
const grokChild = ctx.config.grokProvider.autoStart
? await startGrokProxy({
host: ctx.config.grokProvider.proxyHost,
port: ctx.config.grokProvider.proxyPort,
restartDelayMs: ctx.config.grokProvider.restartDelayMs,
onPortSelected: ({ url, port }: { url: string; port: number }) => {
ctx.markGrokProxyPort({ url, port });
advertise(ctx);
},
onReady: ({ url, port }: { url: string; port: number }) => {
ctx.markGrokProxyPort({ url, port });
advertise(ctx);
},
})
: null;
let server: import("node:net").Server;
let reapTimer: NodeJS.Timeout;
onShutdown(async () => {
unadvertise(ctx);
try { oauthChild?.stop?.(); } catch {}
try { oauthChild?.kill?.(); } catch {}
try { grokChild?.stop?.(); } catch {}
try { grokChild?.kill?.(); } catch {}
stopAgentQueueWorker();
clearInterval(reapTimer);
await new Promise<void>((resolve) => {
if (server) server.close(() => resolve()); else resolve();
});
closeDb();
});
process.on("exit", () => unadvertise(ctx));
server = await listenWithPortFallback(app, ctx.config.server.port, {
host: ctx.config.server.host,
label: "server",
onFallback: ({ requestedPort, actualPort }: { requestedPort: number; actualPort: number }) => {
console.log(`[server.port] requested=${requestedPort} actual=${actualPort} reason=EADDRINUSE`);
},
});
ctx.serverActualPort = getServerPort(server) || ctx.config.server.port;
ctx.serverUrl = `http://${runtimeHostUrl(ctx.config.server.host)}:${ctx.serverActualPort}`;
console.log(`Image Gen running at ${ctx.serverUrl}`);
console.log(`Provider policy: GPT OAuth, API-key Responses, and Grok Images providers. GPT OAuth proxy port ${ctx.oauthPort}; Grok proxy port ${ctx.grokActualPort || ctx.grokPort}.`);
advertise(ctx);
try {
const s = ensureDefaultSession();
console.log(`[db] default session: ${s.id} (${s.title})`);
} catch (e) {
const err = errInfo(e);
console.error("[db] bootstrap failed:", err.message);
}
// Background thumbnail backfill for updated users (recursive — covers video
// series subdirectories like continuous_*/clip_NN.mp4, not just top level).
(async () => {
try {
const { backfillThumbnails } = await import("./lib/thumbBackfill.js");
const r = await backfillThumbnails(ctx.config.storage.generatedDir);
if (r.created > 0) {
console.log(`[thumbs] backfill: ${r.created} created, ${r.skipped} skipped, ${r.failed} failed (${r.total} media files)`);
const { invalidateHistoryIndex } = await import("./lib/historyIndex.js");
invalidateHistoryIndex();
}
} catch (e) {
console.warn("[thumbs] backfill failed:", e instanceof Error ? e.message : e);
}
})();
server.on("error", (err: NodeJS.ErrnoException) => {
console.error("[server] Failed to start:", err?.message || err);
process.exit(1);
});
reapTimer = setInterval(() => {
reapTerminalJobs();
reapCardNewsJobs();
}, 60_000);
reapTimer.unref?.();
process.on("uncaughtException", (err) => {
console.error("[fatal] uncaughtException:", err);
process.exit(1);
});
process.on("unhandledRejection", (reason) => {
console.error("[fatal] unhandledRejection:", reason);
});
return { app, server, oauthChild, ctx };
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
await startServer();
}