Spaces:
Paused
Paused
Update hf.js
Browse files
hf.js
CHANGED
|
@@ -166,16 +166,18 @@ app.get('/hf/v1/models', (req, res) => {
|
|
| 166 |
// 聊天完成度代理
|
| 167 |
app.post('/hf/v1/chat/completions', async (req, res) => {
|
| 168 |
try {
|
| 169 |
-
//
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
|
| 173 |
// 生成 checksum
|
| 174 |
const checksum = `ICMlQsvO${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
|
| 175 |
|
| 176 |
// 构建请求头
|
| 177 |
const headers = {
|
| 178 |
-
'Authorization': req.headers.authorization,
|
| 179 |
'Content-Type': 'application/json',
|
| 180 |
'x-cursor-checksum': checksum,
|
| 181 |
'User-Agent': 'cursor/0.2.0',
|
|
@@ -191,8 +193,10 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 191 |
max_tokens: req.body.max_tokens || 1000
|
| 192 |
};
|
| 193 |
|
| 194 |
-
console.log('Outgoing request
|
| 195 |
-
|
|
|
|
|
|
|
| 196 |
|
| 197 |
// 转发请求到目标服务器
|
| 198 |
const response = await axios.post('http://localhost:3010/v1/chat/completions', requestBody, {
|
|
@@ -200,9 +204,6 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 200 |
timeout: 30000
|
| 201 |
});
|
| 202 |
|
| 203 |
-
// 打印响应内容
|
| 204 |
-
console.log('Response data:', JSON.stringify(response.data, null, 2));
|
| 205 |
-
|
| 206 |
// 返回响应
|
| 207 |
res.status(response.status).json(response.data);
|
| 208 |
} catch (error) {
|
|
@@ -210,8 +211,7 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 210 |
if (error.response) {
|
| 211 |
console.error('Response error:', {
|
| 212 |
status: error.response.status,
|
| 213 |
-
data: error.response.data
|
| 214 |
-
headers: error.response.headers
|
| 215 |
});
|
| 216 |
res.status(error.response.status).json(error.response.data);
|
| 217 |
} else {
|
|
@@ -228,6 +228,7 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 228 |
|
| 229 |
|
| 230 |
|
|
|
|
| 231 |
// 主页路由
|
| 232 |
app.get('/', (req, res) => {
|
| 233 |
const htmlContent = `
|
|
|
|
| 166 |
// 聊天完成度代理
|
| 167 |
app.post('/hf/v1/chat/completions', async (req, res) => {
|
| 168 |
try {
|
| 169 |
+
// 检查认证头
|
| 170 |
+
const authHeader = req.headers.authorization;
|
| 171 |
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
| 172 |
+
return res.status(401).json({ error: 'Missing or invalid authorization token' });
|
| 173 |
+
}
|
| 174 |
|
| 175 |
// 生成 checksum
|
| 176 |
const checksum = `ICMlQsvO${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
|
| 177 |
|
| 178 |
// 构建请求头
|
| 179 |
const headers = {
|
| 180 |
+
'Authorization': req.headers.authorization, // 保持原始的认证头
|
| 181 |
'Content-Type': 'application/json',
|
| 182 |
'x-cursor-checksum': checksum,
|
| 183 |
'User-Agent': 'cursor/0.2.0',
|
|
|
|
| 193 |
max_tokens: req.body.max_tokens || 1000
|
| 194 |
};
|
| 195 |
|
| 196 |
+
console.log('Outgoing request headers:', {
|
| 197 |
+
...headers,
|
| 198 |
+
'Authorization': headers.Authorization ? 'Bearer [TOKEN]' : 'Missing' // 隐藏实际token
|
| 199 |
+
});
|
| 200 |
|
| 201 |
// 转发请求到目标服务器
|
| 202 |
const response = await axios.post('http://localhost:3010/v1/chat/completions', requestBody, {
|
|
|
|
| 204 |
timeout: 30000
|
| 205 |
});
|
| 206 |
|
|
|
|
|
|
|
|
|
|
| 207 |
// 返回响应
|
| 208 |
res.status(response.status).json(response.data);
|
| 209 |
} catch (error) {
|
|
|
|
| 211 |
if (error.response) {
|
| 212 |
console.error('Response error:', {
|
| 213 |
status: error.response.status,
|
| 214 |
+
data: error.response.data
|
|
|
|
| 215 |
});
|
| 216 |
res.status(error.response.status).json(error.response.data);
|
| 217 |
} else {
|
|
|
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
+
|
| 232 |
// 主页路由
|
| 233 |
app.get('/', (req, res) => {
|
| 234 |
const htmlContent = `
|