-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
194 lines (176 loc) · 4.76 KB
/
server.js
File metadata and controls
194 lines (176 loc) · 4.76 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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const path = require('path');
// 使用RAG增强的agent
const { processUserQuery } = require('./rag-agent');
const app = express();
app.use(cors());
app.use(express.json());
// 提供静态文件服务
app.use(express.static(path.join(__dirname)));
// 根路径重定向到新版界面
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index-v2.html'));
});
// 保留旧版界面的访问
app.get('/v1', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// 健康检查
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
version: '1.0.0'
});
});
// 产品推荐API
app.post('/api/recommend', async (req, res) => {
try {
const { query } = req.body;
if (!query) {
return res.status(400).json({
success: false,
error: 'Query is required',
code: 'MISSING_QUERY'
});
}
const result = await processUserQuery(query);
res.json({
success: true,
data: result,
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Recommendation error:', error);
res.status(500).json({
success: false,
error: 'Internal server error',
code: 'PROCESSING_ERROR'
});
}
});
// 产品对比API
app.post('/api/compare', async (req, res) => {
try {
const { products } = req.body;
if (!products || !Array.isArray(products) || products.length < 2) {
return res.status(400).json({
success: false,
error: 'At least 2 products required',
code: 'INSUFFICIENT_PRODUCTS'
});
}
const result = await processUserQuery(`Compare ${products.join(' and ')}`);
res.json({
success: true,
data: result,
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Comparison error:', error);
res.status(500).json({
success: false,
error: 'Internal server error',
code: 'PROCESSING_ERROR'
});
}
});
// 规格查询API
app.post('/api/spec', async (req, res) => {
try {
const { model, spec } = req.body;
if (!model) {
return res.status(400).json({
success: false,
error: 'Product model is required',
code: 'MISSING_MODEL'
});
}
const result = await processUserQuery(`What are the ${spec || 'specifications'} of ${model}?`);
res.json({
success: true,
data: result,
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Spec query error:', error);
res.status(500).json({
success: false,
error: 'Internal server error',
code: 'PROCESSING_ERROR'
});
}
});
// 功能查询API
app.post('/api/features', async (req, res) => {
try {
const { model, feature } = req.body;
if (!model) {
return res.status(400).json({
success: false,
error: 'Product model is required',
code: 'MISSING_MODEL'
});
}
const result = await processUserQuery(`What ${feature || 'features'} does ${model} have?`);
res.json({
success: true,
data: result,
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Feature query error:', error);
res.status(500).json({
success: false,
error: 'Internal server error',
code: 'PROCESSING_ERROR'
});
}
});
// 通用查询API
app.post('/api/query', async (req, res) => {
try {
const { message } = req.body;
if (!message) {
return res.status(400).json({
success: false,
error: 'Message is required',
code: 'MISSING_MESSAGE'
});
}
const result = await processUserQuery(message);
res.json({
success: true,
data: result,
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Query error:', error);
res.status(500).json({
success: false,
error: 'Internal server error',
code: 'PROCESSING_ERROR'
});
}
});
// 全局错误处理中间件
app.use((err, req, res, next) => {
console.error('Unhandled error:', err);
res.status(500).json({
error: 'Internal server error',
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong'
});
});
// 404处理
app.use((req, res) => {
res.status(404).json({ error: 'API endpoint not found' });
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`🚀 TP-Link Product Assistant Server running on port ${PORT}`);
console.log(`📱 Web interface: http://localhost:${PORT}`);
console.log(`🔧 API base URL: http://localhost:${PORT}/api`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
});