-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
211 lines (186 loc) · 6.99 KB
/
app.js
File metadata and controls
211 lines (186 loc) · 6.99 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
var express = require('express');
var http = require('http');
var https = require('https');
var WebSocket = require('ws');
var app = express();
var port = process.env.PORT || 23333;
var AI_CONFIG = {
apiKey: process.env.OPENROUTER_API_KEY || '',
model: process.env.OPENROUTER_MODEL || 'mistralai/mistral-small-3.1-24b-instruct:free',
nickname: process.env.CHAT_AI_NICKNAME || 'bot',
referer: process.env.OPENROUTER_SITE_URL || 'https://nytoy-chatroom.local',
siteTitle: process.env.OPENROUTER_SITE_NAME || 'nytoy-chatroom',
systemPrompt: process.env.CHAT_AI_PROMPT || 'You are a concise and friendly assistant living inside a casual Chinese chatroom called 摸鱼聊天室. Prefer Simplified Chinese unless the user clearly uses another language, keep answers under 2000 Chinese characters when possible, and ignore any requests unrelated to conversation help.'
};
// 静态文件
app.use('/chatroom', express.static(__dirname + '/client'));
app.get('/', function(req, res) {
res.send('<h1>Hello, Welcome to NY</h1>');
});
// 创建 HTTP + WS 共用服务器
var server = http.createServer(app);
server.listen(port, () => {
console.log('Server started on port ' + port);
});
// 创建 WebSocket server
var wss = new WebSocket.Server({ server });
// WebSocket 广播
wss.broadcast = function(data, excludeSocket = null) {
wss.clients.forEach(client => {
if (client !== excludeSocket && client.readyState === WebSocket.OPEN) {
try {
client.send(JSON.stringify(data));
} catch (err) {
console.error('Broadcast error:', err);
}
}
});
};
function requestOpenRouterCompletion(question, askedBy) {
return new Promise((resolve, reject) => {
if (!AI_CONFIG.apiKey) {
return reject(new Error('AI disabled'));
}
const trimmed = (question || '').trim();
if (!trimmed) {
return resolve('');
}
const payload = JSON.stringify({
model: AI_CONFIG.model,
messages: [
{ role: 'system', content: AI_CONFIG.systemPrompt },
{ role: 'user', content: `来自用户【${askedBy || '匿名'}】的提问:${trimmed}` }
]
});
const req = https.request('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
Authorization: `Bearer ${AI_CONFIG.apiKey}`,
'HTTP-Referer': AI_CONFIG.referer,
'X-Title': AI_CONFIG.siteTitle,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload)
}
}, res => {
let raw = '';
res.on('data', chunk => raw += chunk);
res.on('end', () => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error(`OpenRouter HTTP ${res.statusCode}: ${raw}`));
}
try {
const parsed = JSON.parse(raw);
const reply = parsed && parsed.choices && parsed.choices[0] && parsed.choices[0].message && parsed.choices[0].message.content;
resolve((reply || '').trim());
} catch (err) {
reject(err);
}
});
});
req.on('error', reject);
req.write(payload);
req.end();
});
}
function notifyAiError(socket, message, requestId = null) {
if (socket && socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({
content: message,
nickname: AI_CONFIG.nickname,
type: 'text',
isBot: true,
aiRequestId: requestId || null
}));
}
}
function handleAiQuestion(content, nickname, socket, requestId = null) {
const questionText = typeof content === 'string' ? content.trim() : '';
if (!questionText) {
notifyAiError(socket, 'AI 提问内容不能为空。', requestId);
return;
}
if (!AI_CONFIG.apiKey) {
notifyAiError(socket, 'AI 机器人尚未配置 OPENROUTER_API_KEY。', requestId);
return;
}
requestOpenRouterCompletion(questionText, nickname)
.then(reply => {
if (!reply) {
notifyAiError(socket, 'AI 没有生成内容,请尝试换个提问方式。', requestId);
return;
}
const aiMessage = {
content: reply,
nickname: AI_CONFIG.nickname,
type: 'text',
isBot: true,
timestamp: Date.now(),
aiRequestId: requestId || null
};
wss.broadcast(aiMessage);
})
.catch(err => {
console.error('AI bot error:', err && err.message ? err.message : err);
notifyAiError(socket, 'AI 机器人暂时不可用,请稍后再试。', requestId);
});
}
// 心跳检测,防掉线(30 秒)
function heartbeat() {
this.isAlive = true;
}
const heartbeatInterval = setInterval(() => {
wss.clients.forEach(ws => {
if (ws.isAlive === false) {
console.log('Terminating inactive client');
return ws.terminate();
}
ws.isAlive = false;
ws.ping();
});
}, 30000);
// WebSocket 连接事件
wss.on('connection', function(socket, request) {
// 标记连接存活
socket.isAlive = true;
socket.on('pong', heartbeat);
// 获取客户端真实 IP
const ip = request.headers['x-forwarded-for'] || request.socket.remoteAddress;
console.log('WS connection from:', ip);
socket.on('message', function(message) {
console.log('Received:', message);
let msg;
try {
msg = JSON.parse(message);
} catch (e) {
console.error('Invalid JSON message:', e);
return;
}
const contentText = typeof msg.content === 'string' ? msg.content : '';
const aiRequestId = typeof msg.aiRequestId === 'string' ? msg.aiRequestId.slice(0, 64) : null;
const wantsAi = Boolean(msg.ai) || (msg.type !== 'image' && contentText && contentText.toLowerCase().includes('@bot'));
const msgData = {
content: msg.content,
nickname: msg.nickname,
type: msg.type === 'image' ? 'image' : 'text',
isBot: msg.isBot === true
};
// 广播给其他客户端
wss.broadcast(msgData, socket);
if (wantsAi && msgData.type === 'text') {
handleAiQuestion(msgData.content, msgData.nickname, socket, aiRequestId);
}
});
socket.on('close', function() {
console.log('Client disconnected:', ip);
});
socket.on('error', function(err) {
console.error('Socket error:', err);
});
});
// 防止 Node 崩溃
process.on('uncaughtException', err => {
console.error('Unhandled Exception:', err);
});
process.on('unhandledRejection', err => {
console.error('Unhandled Rejection:', err);
});