-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
125 lines (108 loc) · 4.01 KB
/
server.js
File metadata and controls
125 lines (108 loc) · 4.01 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
// Bypass system proxy for ALL outbound connections
delete process.env.HTTP_PROXY;
delete process.env.HTTPS_PROXY;
delete process.env.http_proxy;
delete process.env.https_proxy;
process.env.NO_PROXY = '*';
process.env.no_proxy = '*';
const express = require('express');
const path = require('path');
const fs = require('fs');
const { MsEdgeTTS, OUTPUT_FORMAT } = require('msedge-tts');
const app = express();
const PORT = process.env.PORT || 3000;
const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY || '';
const OPENROUTER_MODEL = process.env.OPENROUTER_MODEL || 'google/gemma-3-12b-it:free';
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
next();
});
app.use(express.json());
// API routes FIRST (before static files)
// Chat endpoint — uses OpenRouter API with fallback models
const FREE_MODELS = [
'google/gemma-3-12b-it:free',
'google/gemma-3-27b-it:free',
'meta-llama/llama-3.3-70b-instruct:free',
'meta-llama/llama-3.2-3b-instruct:free',
'nousresearch/hermes-3-llama-3.1-405b:free',
];
app.post('/api/chat', async (req, res) => {
try {
const { messages } = req.body;
let lastError = null;
// Try each model until one works
for (const model of [OPENROUTER_MODEL, ...FREE_MODELS]) {
try {
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENROUTER_API_KEY}`,
},
body: JSON.stringify({ model, messages }),
});
if (!response.ok) {
const err = await response.text();
console.warn(`Model ${model} failed:`, err);
lastError = err;
continue;
}
const data = await response.json();
const reply = data.choices[0].message.content;
console.log(`Chat response from ${model}`);
return res.json({ message: { role: 'assistant', content: reply } });
} catch (e) {
lastError = e.message;
continue;
}
}
console.error('All models failed. Last error:', lastError);
throw new Error('All models failed');
} catch (err) {
console.error('Chat error:', err.message);
res.status(500).json({ error: 'Chat request failed' });
}
});
// TTS endpoint — Microsoft Edge natural voices
app.post('/api/tts', async (req, res) => {
const tmpDir = path.join(process.env.TEMP || '/tmp', `tts-${Date.now()}`);
try {
const { text, voice = 'fr-FR-DeniseNeural' } = req.body;
if (!text) return res.status(400).json({ error: 'No text provided' });
fs.mkdirSync(tmpDir, { recursive: true });
const tts = new MsEdgeTTS();
await tts.setMetadata(voice, OUTPUT_FORMAT.AUDIO_24KHZ_48KBITRATE_MONO_MP3);
const { audioFilePath } = await tts.toFile(tmpDir, text);
res.set('Content-Type', 'audio/mpeg');
const stream = fs.createReadStream(audioFilePath);
stream.pipe(res);
stream.on('end', () => {
fs.rm(tmpDir, { recursive: true, force: true }, () => {});
});
} catch (err) {
console.error('TTS error:', err, err?.message, err?.stack);
fs.rm(tmpDir, { recursive: true, force: true }, () => {});
if (!res.headersSent) res.status(500).json({ error: 'TTS failed' });
}
});
// Static files AFTER API routes
app.use(express.static(path.join(__dirname, 'dist')));
app.use(express.static(path.join(__dirname)));
// SPA fallback (Express 5 syntax)
app.get('/{*splat}', (req, res) => {
const distIndex = path.join(__dirname, 'dist', 'index.html');
const rootIndex = path.join(__dirname, 'index.html');
res.sendFile(fs.existsSync(distIndex) ? distIndex : rootIndex);
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server running at http://0.0.0.0:${PORT}`);
console.log(`OpenRouter model: ${OPENROUTER_MODEL}`);
});