-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (47 loc) · 2.11 KB
/
server.js
File metadata and controls
55 lines (47 loc) · 2.11 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
// server.js - Express backend to connect website to AI harness
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');
const mainHarness = require('./ai-harness/mainHarness');
const app = express();
const PORT = 3000;
app.use(cors());
app.use(bodyParser.json());
// Serve static files (including index.html) from the project root
app.use(express.static(__dirname));
// POST /api/ask - expects { question: "..." }
app.post('/api/ask', async (req, res) => {
const { question, conversation, availableFunctions, functionRegistry, memoryStore } = req.body;
if (!question || typeof question !== 'string' || !question.trim()) {
return res.status(400).json({ error: 'Invalid or empty question.' });
}
// Set up SSE headers
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
try {
// For demo: fallback to empty or default values if not provided
const result = await mainHarness({
userMessage: question.trim(),
conversation: Array.isArray(conversation) ? conversation.slice(-6) : [],
availableFunctions: Array.isArray(availableFunctions) ? availableFunctions : [],
functionRegistry: typeof functionRegistry === 'object' && functionRegistry !== null ? functionRegistry : {},
memoryStore: typeof memoryStore === 'object' && memoryStore !== null ? memoryStore : {},
onChunk: (chunk) => {
// Sanitize chunk: ensure it's a string to prevent [object Object] on frontend
const safeChunk = typeof chunk === 'object' ? JSON.stringify(chunk) : chunk;
res.write(`data: ${JSON.stringify({ chunk: safeChunk })}\n\n`);
}
});
res.write(`data: ${JSON.stringify({ finalResponse: result.response })}\n\n`);
res.end();
} catch (err) {
console.error('[SERVER ERROR]', err);
res.write(`data: ${JSON.stringify({ error: 'AI harness error', details: err.message })}\n\n`);
res.end();
}
});
app.listen(PORT, () => {
console.log(`AI Harness server running at http://localhost:${PORT}`);
});