isididiidid commited on
Commit
15bccc3
·
verified ·
1 Parent(s): a10d18f

Update hf.js

Browse files
Files changed (1) hide show
  1. hf.js +19 -14
hf.js CHANGED
@@ -166,23 +166,24 @@ app.get('/hf/v1/models', (req, res) => {
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',
184
  'Accept': 'application/json',
185
- 'Connection': 'keep-alive'
 
186
  };
187
 
188
  // 构建请求体
@@ -193,17 +194,19 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
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, {
203
  headers: headers,
204
- timeout: 30000
 
205
  });
206
 
 
 
 
207
  // 返回响应
208
  res.status(response.status).json(response.data);
209
  } catch (error) {
@@ -211,7 +214,8 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
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 {
@@ -229,6 +233,7 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
229
 
230
 
231
 
 
232
  // 主页路由
233
  app.get('/', (req, res) => {
234
  const htmlContent = `
 
166
  // 聊天完成度代理
167
  app.post('/hf/v1/chat/completions', async (req, res) => {
168
  try {
 
 
 
 
 
 
169
  // 生成 checksum
170
  const checksum = `ICMlQsvO${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
171
 
172
+ // 从请求头获取认证信息
173
+ const authToken = req.headers.authorization?.replace('Bearer ', '') || '';
174
+ if (!authToken) {
175
+ return res.status(401).json({ error: 'Missing authorization token' });
176
+ }
177
+
178
  // 构建请求头
179
  const headers = {
180
+ 'Authorization': `Bearer ${decodeURIComponent(authToken)}`, // 解码 token
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
 
189
  // 构建请求体
 
194
  max_tokens: req.body.max_tokens || 1000
195
  };
196
 
197
+ console.log('Outgoing request body:', JSON.stringify(requestBody, null, 2));
198
+ console.log('Using token:', authToken.substring(0, 20) + '...'); // 只打印token的开头部分
 
 
199
 
200
  // 转发请求到目标服务器
201
  const response = await axios.post('http://localhost:3010/v1/chat/completions', requestBody, {
202
  headers: headers,
203
+ timeout: 30000,
204
+ validateStatus: status => true
205
  });
206
 
207
+ console.log('Response status:', response.status);
208
+ console.log('Response data:', JSON.stringify(response.data, null, 2));
209
+
210
  // 返回响应
211
  res.status(response.status).json(response.data);
212
  } catch (error) {
 
214
  if (error.response) {
215
  console.error('Response error:', {
216
  status: error.response.status,
217
+ data: error.response.data,
218
+ headers: error.response.headers
219
  });
220
  res.status(error.response.status).json(error.response.data);
221
  } else {
 
233
 
234
 
235
 
236
+
237
  // 主页路由
238
  app.get('/', (req, res) => {
239
  const htmlContent = `