-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
496 lines (416 loc) · 14.8 KB
/
server.js
File metadata and controls
496 lines (416 loc) · 14.8 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
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)');
console.log('Install with: npm install ws');
}
const DIST_DIR = path.join(__dirname, 'dist');
const SIMULATIONS_DIR = path.join(__dirname, 'simulations');
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 = process.env.PORT || 3000;
const OPENAI_BASE_URL = (process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1').replace(/\/+$/, '');
const wsClients = new Set();
const LOG_DIR = path.join(__dirname, 'logs');
const LOG_FILE = path.join(LOG_DIR, 'events.jsonl');
if (!fs.existsSync(LOG_DIR)) fs.mkdirSync(LOG_DIR, { recursive: true });
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.eot': 'application/vnd.ms-fontobject'
};
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;
}
const mimeType = getMimeType(filePath);
res.writeHead(200, { 'Content-Type': mimeType });
res.end(data);
});
}
// --- Simulation proxy registry ---
let simUrlMap = new Map();
function loadSimulationConfig() {
const configPath = path.join(SIMULATIONS_DIR, 'simulations.json');
try {
if (!fs.existsSync(configPath)) {
console.log('No simulations.json found -- proxy disabled, serving from filesystem');
return;
}
const raw = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(raw);
simUrlMap = new Map();
for (const sim of (config.simulations || [])) {
if (sim.url) {
simUrlMap.set(sim.id, sim.url);
console.log(` Proxy: /simulations/${sim.id}/* -> ${sim.url}`);
}
}
if (simUrlMap.size === 0) {
console.log('No simulation URLs configured -- serving from filesystem');
}
} catch (err) {
console.error('Failed to load simulations.json:', err.message);
}
}
function proxyRequest(req, res, targetUrl) {
const parsed = new URL(targetUrl);
const options = {
hostname: parsed.hostname,
port: parsed.port || 80,
path: parsed.pathname + parsed.search,
method: req.method,
headers: { ...req.headers, host: parsed.host }
};
const proxyReq = http.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.on('error', (err) => {
console.error(`Proxy error -> ${targetUrl}:`, err.message);
if (!res.headersSent) {
res.writeHead(502, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Simulation server unavailable' }));
}
});
req.pipe(proxyReq);
}
function tryProxy(req, res, pathName) {
if (!pathName.startsWith('/simulations/')) return false;
const rest = pathName.slice('/simulations/'.length);
const slashIdx = rest.indexOf('/');
const simId = slashIdx === -1 ? rest : rest.slice(0, slashIdx);
const simPath = slashIdx === -1 ? '/' : rest.slice(slashIdx);
const targetBase = simUrlMap.get(simId);
if (!targetBase) return false;
const targetUrl = targetBase.replace(/\/+$/, '') + simPath;
proxyRequest(req, res, targetUrl);
return true;
}
// --- Host API handlers ---
function readBody(req) {
return new Promise((resolve, reject) => {
let body = '';
req.on('data', chunk => { body += chunk.toString(); });
req.on('end', () => {
try { resolve(JSON.parse(body)); }
catch (e) { reject(e); }
});
req.on('error', reject);
});
}
async function handleOrchestrateRequest(req, res) {
try {
const data = await readBody(req);
const { event, history, scenario } = data;
if (!event || !scenario) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'event and scenario are required' }));
return;
}
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ actions: [] }));
return;
}
const systemPrompt = `You are a scenario orchestrator for a workplace simulation platform. Your job is to decide what cross-application actions should be triggered when a user performs an action.
SCENARIO: ${scenario.description || 'No description provided.'}
${scenario.llmContext || ''}
AVAILABLE ACTIONS (by target simulation):
${JSON.stringify(scenario.actionSchemas || {}, null, 2)}
RULES:
- Only return actions that make sense for the scenario and the user's behavior.
- Return an empty actions array if no reaction is needed.
- Each action must have: target (simulation id), type (action type from schemas), payload (object with relevant data).
- Keep it realistic -- not every user action needs a reaction.
- Respond ONLY with valid JSON, no markdown.`;
const userContent = `EVENT HISTORY (last ${(history || []).length} events):
${JSON.stringify((history || []).slice(-10), null, 2)}
NEW EVENT:
${JSON.stringify(event, null, 2)}
Based on this event and the scenario context, what actions (if any) should be dispatched to other simulations? Return JSON: { "actions": [...], "reasoning": "..." }`;
const apiRes = await fetch(`${OPENAI_BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userContent }
],
max_tokens: 500,
temperature: 0.3,
response_format: { type: 'json_object' }
})
});
if (!apiRes.ok) {
console.error('OpenAI orchestrate error:', apiRes.status);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ actions: [] }));
return;
}
const result = await apiRes.json();
const raw = result.choices?.[0]?.message?.content || '{"actions":[]}';
let parsed;
try { parsed = JSON.parse(raw); }
catch { parsed = { actions: [] }; }
const validActions = (parsed.actions || []).filter(a =>
a.target && a.type && typeof a.target === 'string'
);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ actions: validActions, reasoning: parsed.reasoning || '' }));
} catch (error) {
console.error('Orchestrate endpoint error:', error);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Internal server error' }));
}
}
async function handleGuardRequest(req, res) {
try {
const data = await readBody(req);
const { question, event, scenarioDescription } = data;
if (!question || !event) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'question and event are required' }));
return;
}
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ allow: true }));
return;
}
const systemPrompt = `You are a guard for a workplace simulation scenario engine. You will be given a scenario context, a user interaction event, and a yes/no question. Answer ONLY with JSON: { "allow": true } or { "allow": false }. No explanation needed.
SCENARIO: ${scenarioDescription || 'Workplace simulation.'}`;
const userContent = `EVENT:
${JSON.stringify(event, null, 2)}
QUESTION: ${question}`;
const apiRes = await fetch(`${OPENAI_BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userContent }
],
max_tokens: 20,
temperature: 0.1,
response_format: { type: 'json_object' }
})
});
if (!apiRes.ok) {
console.error('OpenAI guard error:', apiRes.status);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ allow: true }));
return;
}
const result = await apiRes.json();
const raw = result.choices?.[0]?.message?.content || '{"allow":true}';
let parsed;
try { parsed = JSON.parse(raw); } catch { parsed = { allow: true }; }
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ allow: !!parsed.allow }));
} catch (error) {
console.error('Guard endpoint error:', error);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ allow: true }));
}
}
async function handleLogRequest(req, res) {
try {
const data = await readBody(req);
const entries = data.entries;
if (!Array.isArray(entries) || entries.length === 0) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'entries array is required' }));
return;
}
const lines = entries.map(e => JSON.stringify(e)).join('\n') + '\n';
fs.appendFile(LOG_FILE, lines, (err) => {
if (err) console.error('Failed to write log:', err);
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: true, count: entries.length }));
} catch (error) {
console.error('Log endpoint error:', error);
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
}
function handlePostRequest(req, res, parsedUrl) {
if (parsedUrl.pathname === '/api/log') {
handleLogRequest(req, res);
return;
}
if (parsedUrl.pathname === '/api/guard') {
handleGuardRequest(req, res);
return;
}
if (parsedUrl.pathname === '/api/orchestrate') {
handleOrchestrateRequest(req, res);
return;
}
if (parsedUrl.pathname === '/message') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
const data = JSON.parse(body);
const message = data.message;
if (!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 functionality not available',
details: 'Install the ws package with: npm install ws'
}));
return;
}
wsClients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'message', message: message }));
}
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true, clientCount: wsClients.size }));
} catch (error) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
});
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
}
}
// --- HTTP server ---
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
let pathName = parsedUrl.pathname === '/' ? '/index.html' : parsedUrl.pathname;
if (tryProxy(req, res, parsedUrl.pathname)) return;
if (req.method === 'POST') {
handlePostRequest(req, res, parsedUrl);
return;
}
// Serve local simulation files (simulations.json, scenarios/) that aren't proxied
if (pathName.startsWith('/simulations/')) {
const simPath = pathName.replace(/^\/simulations\//, '');
const filePath = path.join(SIMULATIONS_DIR, simPath);
const resolvedSimDir = path.resolve(SIMULATIONS_DIR);
const resolvedFilePath = path.resolve(filePath);
const relativePath = path.relative(resolvedSimDir, resolvedFilePath);
if (relativePath.startsWith('..')) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.end('Forbidden');
return;
}
serveFile(filePath, res);
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 `npm run start:dev`)');
}
});
if (isWebSocketAvailable) {
const wss = new WebSocket.Server({
server,
path: '/ws'
});
wss.on('connection', (ws, req) => {
console.log('New WebSocket client connected');
wsClients.add(ws);
ws.on('close', () => {
console.log('WebSocket client disconnected');
wsClients.delete(ws);
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
wsClients.delete(ws);
});
});
}
loadSimulationConfig();
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
if (isProduction) {
console.log(`Serving static files from: ${DIST_DIR}`);
} else {
console.log(`Development mode - static files served by Vite`);
}
if (simUrlMap.size > 0) {
console.log(`Proxying ${simUrlMap.size} simulation(s)`);
}
if (isWebSocketAvailable) {
console.log(`WebSocket server running on /ws`);
}
console.log('Press Ctrl+C to stop the server');
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use. Please try a different port.`);
} else {
console.error('Server error:', err);
}
process.exit(1);
});
process.on('SIGINT', () => {
console.log('\nShutting down server...');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});