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

Update hf.js

Browse files
Files changed (1) hide show
  1. hf.js +28 -15
hf.js CHANGED
@@ -164,24 +164,37 @@ app.get('/hf/v1/models', (req, res) => {
164
  });
165
 
166
  // 聊天完成度代理
167
- app.use('/hf/v1/chat/completions', createProxyMiddleware({
168
- target: 'http://localhost:3010/v1/chat/completions',
169
- changeOrigin: true,
170
- onProxyReq: (proxyReq, req, res) => {
171
- // 生成 checksum
172
- const checksum = `zo${getRandomIDPro({ dictType: 'max', size: 6 })}${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
173
 
174
- // 设置 x-cursor-checksum header
175
- proxyReq.setHeader('x-cursor-checksum', checksum);
 
 
 
 
 
 
 
 
176
 
177
- // 将请求数据写回代理请求
178
- if (req.body) {
179
- const bodyData = JSON.stringify(req.body);
180
- proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
181
- proxyReq.write(bodyData);
 
 
 
 
 
182
  }
183
- }
184
- }));
 
 
 
185
 
186
  // 主页路由
187
  app.get('/', (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) => {