-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
503 lines (430 loc) · 16 KB
/
server.js
File metadata and controls
503 lines (430 loc) · 16 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
require('dotenv').config();
const LLM_LOG_PATH = path.join(process.cwd(), 'llm.log');
function logLLMCall(label, prompt, content) {
const sep = '─'.repeat(80);
const ts = new Date().toISOString();
const entry = [
'',
sep,
`[${ts}] ${label.toUpperCase()}`,
sep,
'--- PROMPT ---',
prompt,
'--- RESPONSE ---',
content,
'',
].join('\n');
fs.appendFile(LLM_LOG_PATH, entry, (err) => {
if (err) console.error('[llm-log] write error:', err.message);
});
console.log(`[llm] ${label} — ${content.length} chars`);
}
let WebSocket = null;
let isWebSocketAvailable = false;
try {
WebSocket = require('ws');
isWebSocketAvailable = true;
console.log('WebSocket support enabled');
} catch (error) {
console.log('WebSocket support disabled (ws package not installed)');
}
const DIST_DIR = path.join(__dirname, 'dist');
const isProduction = process.env.IS_PRODUCTION === 'true';
if (isProduction && !fs.existsSync(DIST_DIR)) {
throw new Error(`Production mode enabled but dist directory does not exist: ${DIST_DIR}`);
}
const PORT = isProduction ? 3000 : (process.env.PORT || 3000);
const wsClients = new Set();
const SOLUTION_PATH = path.join(process.cwd(), 'solution.json');
const INITIAL_STATE_PATH = path.join(process.cwd(), 'initial_state.json');
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf'
};
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
return mimeTypes[ext] || 'text/plain';
}
function serveFile(filePath, res) {
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('File not found');
return;
}
res.writeHead(200, { 'Content-Type': getMimeType(filePath) });
res.end(data);
});
}
function readBody(req) {
return new Promise((resolve, reject) => {
let body = '';
req.on('data', chunk => { body += chunk.toString(); });
req.on('end', () => resolve(body));
req.on('error', reject);
});
}
function handleGetState(req, res) {
fs.readFile(SOLUTION_PATH, 'utf8', (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'No saved state' }));
return;
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(data);
});
}
function handlePostState(req, res) {
readBody(req).then(body => {
JSON.parse(body);
fs.writeFile(SOLUTION_PATH, body, (err) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Failed to save state' }));
return;
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
});
}).catch(() => {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
});
}
function handleGetInitialState(req, res) {
fs.readFile(INITIAL_STATE_PATH, 'utf8', (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'No initial state' }));
return;
}
try {
JSON.parse(data);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(data);
} catch (parseErr) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid initial_state.json' }));
}
});
}
async function handleGenerateContent(req, res) {
const body = await readBody(req);
const { prompt, apiKey, model = 'gpt-4o', baseUrl, max_tokens, label = 'llm' } = JSON.parse(body);
if (!prompt) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'prompt is required' }));
return;
}
const resolvedBaseUrl = baseUrl || process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1';
const key = apiKey || process.env.OPENAI_API_KEY;
try {
const apiUrl = `${resolvedBaseUrl.replace(/\/+$/, '')}/chat/completions`;
const payload = JSON.stringify({
model,
messages: [{ role: 'user', content: prompt }],
max_completion_tokens: max_tokens || 16000,
});
const parsedApi = new URL(apiUrl);
const https = parsedApi.protocol === 'https:' ? require('https') : require('http');
const apiRes = await new Promise((resolve, reject) => {
const r = https.request(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${key}`,
},
}, resolve);
r.on('error', reject);
r.write(payload);
r.end();
});
let data = '';
for await (const chunk of apiRes) data += chunk;
if (apiRes.statusCode !== 200) {
res.writeHead(apiRes.statusCode, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: `LLM API error: ${apiRes.statusCode}`, details: data }));
return;
}
const parsed = JSON.parse(data);
const content = parsed.choices?.[0]?.message?.content || '';
logLLMCall(label, prompt, content);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ content }));
} catch (err) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: err.message }));
}
}
// ─── Icon generation ────────────────────────────────────────────────────────
let sharp = null;
try { sharp = require('sharp'); } catch (_) {}
const GEMINI_ICON_URL = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent';
const ICON_PROMPT_TEMPLATE = `
## RULE: The output image must contain ZERO text.
No letters, no numbers, no words, no labels, no titles, no hex codes — nothing that a human could read as text. This includes the card name below: it tells you WHAT TO DEPICT as a symbol, NOT what to write. Any image containing text will be rejected.
Generate a purely graphical badge icon. Depict the concept of "{{CARD_NAME}}" ({{CARD_TYPE}}: {{CARD_DESC}}) using ONLY shapes, symbols, and abstract icons — no labels.
## Composition
- Rounded-corner hexagon badge
- Centered, symmetrical primary symbol inside
- Dotted or dashed circle/hexagon framing the symbol
- 4–8 small floating accents in the margins (dots, tiny squares, plus signs, small circles, short dashes)
- Optional: subtle horizontal line patterns in the lower third suggesting data flow
## Style
Flat vector, clean geometry, consistent stroke weight. No gradients, no shadows, no texture. Sparse, evenly distributed decorative accents.
## Palette (use these colors, do NOT render them as text)
- Badge fill: #365FAF
- Mid-tone: #6292EF
- Light accent: #4CB4FF
- Highlight: #F4FAFF
## Size
Must read clearly at 24–32px. Primary symbol dominates; accents stay peripheral.
## Format
Square 1:1 image, solid bright green (#00FF00) background outside the badge for easy chroma-key removal.
`;
function isGreenScreen(r, g, b) {
if (g < r * 1.15 || g < b * 1.15) return false;
if (g < 100) return false;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const delta = max - min;
if (delta === 0) return false;
const lightness = (max + min) / 2;
const saturation = delta / (1 - Math.abs(2 * lightness / 255 - 1)) / 255;
let hue;
if (max === r) hue = 60 * (((g - b) / delta) % 6);
else if (max === g) hue = 60 * ((b - r) / delta + 2);
else hue = 60 * ((r - g) / delta + 4);
if (hue < 0) hue += 360;
return hue > 85 && hue < 150 && saturation > 0.3 && lightness > 70 && lightness < 220;
}
async function removeGreenBackgroundInPlace(filePath) {
const image = sharp(filePath);
const { width, height } = await image.metadata();
const { data } = await image.raw().ensureAlpha().toBuffer({ resolveWithObject: true });
const out = Buffer.from(data);
for (let i = 0; i < width * height; i++) {
const o = i * 4;
if (isGreenScreen(out[o], out[o + 1], out[o + 2])) out[o + 3] = 0;
}
const copy = Buffer.from(out);
for (let y = 1; y < height - 1; y++) {
for (let x = 1; x < width - 1; x++) {
const i = (y * width + x) * 4;
if (copy[i + 3] === 0) continue;
let t = 0;
for (const [dx, dy] of [[-1, 0], [1, 0], [0, -1], [0, 1]]) {
if (copy[((y + dy) * width + (x + dx)) * 4 + 3] === 0) t++;
}
if (t >= 2) out[i + 3] = Math.round(out[i + 3] * 0.4);
else if (t === 1) out[i + 3] = Math.round(out[i + 3] * 0.75);
}
}
const tmpPath = filePath + '.tmp';
await sharp(out, { raw: { width, height, channels: 4 } })
.png()
.trim()
.resize(512, 512, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
.toFile(tmpPath);
fs.copyFileSync(tmpPath, filePath);
fs.unlinkSync(tmpPath);
}
function geminiPostJSON(urlStr, body) {
return new Promise((resolve, reject) => {
const data = JSON.stringify(body);
const parsed = new URL(urlStr);
const httpsModule = require('https');
const req = httpsModule.request({
hostname: parsed.hostname,
path: parsed.pathname + parsed.search,
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data) },
}, (res) => {
const chunks = [];
res.on('data', c => chunks.push(c));
res.on('end', () => {
const text = Buffer.concat(chunks).toString();
if (res.statusCode !== 200) return reject(new Error(`Gemini API error ${res.statusCode}: ${text}`));
resolve(JSON.parse(text));
});
res.on('error', reject);
});
req.on('error', reject);
req.write(data);
req.end();
});
}
async function handleGenerateIconsForCard(req, res) {
if (!sharp) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'sharp package not available — run npm install' }));
return;
}
const body = await readBody(req);
const { cardId, cardName, cardType, cardDescription, deckId } = JSON.parse(body);
if (!cardId || !cardName || !deckId) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'cardId, cardName, and deckId are required' }));
return;
}
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'GEMINI_API_KEY environment variable not set' }));
return;
}
const outDir = path.join(__dirname, 'client/public/data/icons', deckId);
const rawDir = path.join(outDir, 'raw');
fs.mkdirSync(rawDir, { recursive: true });
const rawPath = path.join(rawDir, `${cardId}.png`);
const finalPath = path.join(outDir, `${cardId}.png`);
const prompt = ICON_PROMPT_TEMPLATE
.replace('{{CARD_NAME}}', cardName)
.replace('{{CARD_TYPE}}', cardType || '')
.replace('{{CARD_DESC}}', cardDescription || '');
const requestBody = {
contents: [{ parts: [{ text: prompt }] }],
generationConfig: { responseModalities: ['TEXT', 'IMAGE'] },
};
const geminiData = await geminiPostJSON(`${GEMINI_ICON_URL}?key=${apiKey}`, requestBody);
let imageBuffer = null;
for (const candidate of geminiData.candidates || []) {
for (const part of candidate.content?.parts || []) {
if (part.inlineData?.mimeType?.startsWith('image/')) {
imageBuffer = Buffer.from(part.inlineData.data, 'base64');
break;
}
}
if (imageBuffer) break;
}
if (!imageBuffer) throw new Error('No image in Gemini response');
fs.writeFileSync(rawPath, imageBuffer);
fs.copyFileSync(rawPath, finalPath);
await removeGreenBackgroundInPlace(finalPath);
const iconPath = `/data/icons/${deckId}/${cardId}.png`;
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ iconPath }));
}
// ────────────────────────────────────────────────────────────────────────────
function handlePostRequest(req, res, parsedUrl) {
if (parsedUrl.pathname === '/state') {
handlePostState(req, res);
return;
}
if (parsedUrl.pathname === '/api/generate-content') {
handleGenerateContent(req, res).catch(err => {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: err.message }));
});
return;
}
if (parsedUrl.pathname === '/api/generate-icons') {
handleGenerateIconsForCard(req, res).catch(err => {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: err.message }));
});
return;
}
if (parsedUrl.pathname === '/message') {
readBody(req).then(body => {
const data = JSON.parse(body);
if (!data.message) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Message is required' }));
return;
}
if (!isWebSocketAvailable) {
res.writeHead(503, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'WebSocket not available' }));
return;
}
wsClients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'message', message: data.message }));
}
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true, clientCount: wsClients.size }));
}).catch(() => {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
});
return;
}
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
}
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
let pathName = parsedUrl.pathname === '/' ? '/index.html' : parsedUrl.pathname;
if (process.env.AUTHOR_MODE === 'true' && req.method === 'GET' && parsedUrl.pathname === '/' && !parsedUrl.query.author) {
res.writeHead(302, { Location: '/?author=true' });
res.end();
return;
}
if (req.method === 'GET' && parsedUrl.pathname === '/state') {
handleGetState(req, res);
return;
}
if (req.method === 'GET' && parsedUrl.pathname === '/initial-state') {
handleGetInitialState(req, res);
return;
}
if (req.method === 'POST') {
handlePostRequest(req, res, parsedUrl);
return;
}
if (isProduction) {
let filePath = path.join(DIST_DIR, pathName.replace(/^\/+/, ''));
const resolvedDistDir = path.resolve(DIST_DIR);
const resolvedFilePath = path.resolve(filePath);
const relativePath = path.relative(resolvedDistDir, resolvedFilePath);
if (relativePath.startsWith('..')) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.end('Forbidden');
return;
}
serveFile(filePath, res);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found (development mode - use Vite dev server)');
}
});
if (isWebSocketAvailable) {
const wss = new WebSocket.Server({ server, path: '/ws' });
wss.on('connection', (ws) => {
console.log('WebSocket client connected');
wsClients.add(ws);
ws.on('close', () => { wsClients.delete(ws); });
ws.on('error', () => { wsClients.delete(ws); });
});
}
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
if (isProduction) console.log(`Serving from: ${DIST_DIR}`);
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use.`);
} else {
console.error('Server error:', err);
}
process.exit(1);
});
process.on('SIGINT', () => {
server.close(() => process.exit(0));
});