Spaces:
Paused
Paused
Update hf.js
Browse files
hf.js
CHANGED
|
@@ -163,6 +163,7 @@ app.get('/hf/v1/models', (req, res) => {
|
|
| 163 |
res.json(models);
|
| 164 |
});
|
| 165 |
|
|
|
|
| 166 |
// 聊天完成度代理
|
| 167 |
app.post('/hf/v1/chat/completions', async (req, res) => {
|
| 168 |
try {
|
|
@@ -170,19 +171,21 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 170 |
const checksum = `ICMlQsvO${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
|
| 171 |
|
| 172 |
// 从请求头获取认证信息
|
| 173 |
-
const
|
| 174 |
-
|
| 175 |
-
return res.status(401).json({ error: 'Missing authorization token' });
|
| 176 |
-
}
|
| 177 |
|
| 178 |
// 构建请求头
|
| 179 |
const headers = {
|
| 180 |
-
'Authorization':
|
| 181 |
'Content-Type': 'application/json',
|
| 182 |
'x-cursor-checksum': checksum,
|
| 183 |
'User-Agent': 'cursor/0.2.0',
|
| 184 |
'Accept': 'application/json',
|
| 185 |
'Connection': 'keep-alive',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
'Host': 'cursor.sh'
|
| 187 |
};
|
| 188 |
|
|
@@ -190,12 +193,22 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 190 |
const requestBody = {
|
| 191 |
...req.body,
|
| 192 |
stream: false,
|
| 193 |
-
|
| 194 |
-
|
|
|
|
| 195 |
};
|
| 196 |
|
| 197 |
-
console.log('
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
// 转发请求到目标服务器
|
| 201 |
const response = await axios.post('http://localhost:3010/v1/chat/completions', requestBody, {
|
|
@@ -204,8 +217,11 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 204 |
validateStatus: status => true
|
| 205 |
});
|
| 206 |
|
| 207 |
-
console.log('Response
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
// 返回响应
|
| 211 |
res.status(response.status).json(response.data);
|
|
|
|
| 163 |
res.json(models);
|
| 164 |
});
|
| 165 |
|
| 166 |
+
|
| 167 |
// 聊天完成度代理
|
| 168 |
app.post('/hf/v1/chat/completions', async (req, res) => {
|
| 169 |
try {
|
|
|
|
| 171 |
const checksum = `ICMlQsvO${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
|
| 172 |
|
| 173 |
// 从请求头获取认证信息
|
| 174 |
+
const authHeader = req.headers.authorization;
|
| 175 |
+
const token = authHeader?.replace('Bearer ', '') || '';
|
|
|
|
|
|
|
| 176 |
|
| 177 |
// 构建请求头
|
| 178 |
const headers = {
|
| 179 |
+
'Authorization': authHeader,
|
| 180 |
'Content-Type': 'application/json',
|
| 181 |
'x-cursor-checksum': checksum,
|
| 182 |
'User-Agent': 'cursor/0.2.0',
|
| 183 |
'Accept': 'application/json',
|
| 184 |
'Connection': 'keep-alive',
|
| 185 |
+
'Cookie': `user_token=${token}`, // 添加 cookie
|
| 186 |
+
'x-cursor-token': token, // 添加 cursor token
|
| 187 |
+
'Origin': 'https://cursor.sh',
|
| 188 |
+
'Referer': 'https://cursor.sh/',
|
| 189 |
'Host': 'cursor.sh'
|
| 190 |
};
|
| 191 |
|
|
|
|
| 193 |
const requestBody = {
|
| 194 |
...req.body,
|
| 195 |
stream: false,
|
| 196 |
+
user: {
|
| 197 |
+
id: token.split('::')[0] // 添加用户ID
|
| 198 |
+
}
|
| 199 |
};
|
| 200 |
|
| 201 |
+
console.log('Request details:', {
|
| 202 |
+
url: 'http://localhost:3010/v1/chat/completions',
|
| 203 |
+
method: 'POST',
|
| 204 |
+
headers: {
|
| 205 |
+
...headers,
|
| 206 |
+
'Authorization': 'Bearer [TOKEN]',
|
| 207 |
+
'Cookie': 'user_token=[TOKEN]',
|
| 208 |
+
'x-cursor-token': '[TOKEN]'
|
| 209 |
+
},
|
| 210 |
+
body: requestBody
|
| 211 |
+
});
|
| 212 |
|
| 213 |
// 转发请求到目标服务器
|
| 214 |
const response = await axios.post('http://localhost:3010/v1/chat/completions', requestBody, {
|
|
|
|
| 217 |
validateStatus: status => true
|
| 218 |
});
|
| 219 |
|
| 220 |
+
console.log('Response details:', {
|
| 221 |
+
status: response.status,
|
| 222 |
+
headers: response.headers,
|
| 223 |
+
data: response.data
|
| 224 |
+
});
|
| 225 |
|
| 226 |
// 返回响应
|
| 227 |
res.status(response.status).json(response.data);
|