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

Update hf.js

Browse files
Files changed (1) hide show
  1. hf.js +27 -11
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 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
 
@@ -190,12 +193,22 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
190
  const requestBody = {
191
  ...req.body,
192
  stream: false,
193
- temperature: req.body.temperature || 0.7,
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, {
@@ -204,8 +217,11 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
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);
 
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);