isididiidid commited on
Commit
ab504b8
·
verified ·
1 Parent(s): 88c933d

Update hf.js

Browse files
Files changed (1) hide show
  1. hf.js +25 -26
hf.js CHANGED
@@ -164,38 +164,37 @@ app.get('/hf/v1/models', (req, res) => {
164
  });
165
 
166
  // 聊天完成度代理
167
- app.use('/hf/v1/chat/completions', (req, res, next) => {
168
- // 生成 checksum
169
- const checksum = `zo${getRandomIDPro({ dictType: 'max', size: 6 })}${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
 
170
 
171
- // 创建代理
172
- const proxy = createProxyMiddleware({
173
- target: 'http://localhost:3010',
174
- changeOrigin: true,
175
- pathRewrite: {
176
- '^/hf/v1/chat/completions': '/v1/chat/completions'
177
- },
178
- onProxyReq: (proxyReq, req) => {
179
- // 设置 checksum header
180
- proxyReq.setHeader('x-cursor-checksum', checksum);
181
-
182
- // 如果有请求体,重写请求体
183
- if (req.body) {
184
- const bodyData = JSON.stringify(req.body);
185
- proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
186
- proxyReq.write(bodyData);
187
  }
188
- },
189
- onError: (err, req, res) => {
190
- console.error('Proxy Error:', err);
191
- res.status(500).json({ error: 'Proxy error', details: err.message });
192
- }
193
- });
194
 
195
- proxy(req, res, next);
 
 
 
 
 
 
 
 
 
 
 
 
196
  });
197
 
198
 
 
199
  // 主页路由
200
  app.get('/', (req, res) => {
201
  const htmlContent = `
 
164
  });
165
 
166
  // 聊天完成度代理
167
+ app.post('/hf/v1/chat/completions', async (req, res) => {
168
+ try {
169
+ // 生成 checksum
170
+ const checksum = `zo${getRandomIDPro({ dictType: 'max', size: 6 })}${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
171
 
172
+ // 转发请求到目标服务器
173
+ const response = await axios.post('http://localhost:3010/v1/chat/completions', req.body, {
174
+ headers: {
175
+ ...req.headers,
176
+ 'x-cursor-checksum': checksum,
177
+ 'host': 'localhost:3010'
 
 
 
 
 
 
 
 
 
 
178
  }
179
+ });
 
 
 
 
 
180
 
181
+ // 返回响应
182
+ res.status(response.status).json(response.data);
183
+ } catch (error) {
184
+ console.error('Error in chat completions:', error);
185
+ if (error.response) {
186
+ res.status(error.response.status).json(error.response.data);
187
+ } else {
188
+ res.status(500).json({
189
+ error: 'Internal server error',
190
+ message: error.message
191
+ });
192
+ }
193
+ }
194
  });
195
 
196
 
197
+
198
  // 主页路由
199
  app.get('/', (req, res) => {
200
  const htmlContent = `