Spaces:
Paused
Paused
Update src/index.js
Browse files- src/index.js +51 -0
src/index.js
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const fetch = require('node-fetch');
|
| 3 |
+
const app = express();
|
| 4 |
+
|
| 5 |
+
app.use(express.json());
|
| 6 |
+
|
| 7 |
+
// 处理聊天完成请求
|
| 8 |
+
app.post('/v1/chat/completions', async (req, res) => {
|
| 9 |
+
try {
|
| 10 |
+
// 获取必要的头部
|
| 11 |
+
const token = req.headers['x-cursor-token'];
|
| 12 |
+
const checksum = req.headers['x-cursor-checksum'];
|
| 13 |
+
|
| 14 |
+
// 验证必要的头部
|
| 15 |
+
if (!token || !checksum) {
|
| 16 |
+
return res.status(401).json({ error: 'Missing required headers' });
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// 构建请求体
|
| 20 |
+
const requestBody = {
|
| 21 |
+
...req.body,
|
| 22 |
+
stream: false
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
// 构建请求头
|
| 26 |
+
const headers = {
|
| 27 |
+
'Authorization': `Bearer ${token}`,
|
| 28 |
+
'Content-Type': 'application/json',
|
| 29 |
+
'x-cursor-checksum': checksum,
|
| 30 |
+
'User-Agent': 'cursor/0.2.0'
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
// 转发到 Cursor API
|
| 34 |
+
const response = await fetch('https://cursor.sh/api/v1/chat/completions', {
|
| 35 |
+
method: 'POST',
|
| 36 |
+
headers: headers,
|
| 37 |
+
body: JSON.stringify(requestBody)
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
const data = await response.json();
|
| 41 |
+
res.json(data);
|
| 42 |
+
} catch (error) {
|
| 43 |
+
console.error('Error:', error);
|
| 44 |
+
res.status(500).json({ error: 'Internal server error' });
|
| 45 |
+
}
|
| 46 |
+
});
|
| 47 |
+
|
| 48 |
+
const port = 3010;
|
| 49 |
+
app.listen(port, () => {
|
| 50 |
+
console.log(`The server listens port: ${port}`);
|
| 51 |
+
});
|