Spaces:
Paused
Paused
Update hf.js
Browse files
hf.js
CHANGED
|
@@ -164,24 +164,37 @@ app.get('/hf/v1/models', (req, res) => {
|
|
| 164 |
});
|
| 165 |
|
| 166 |
// 聊天完成度代理
|
| 167 |
-
app.use('/hf/v1/chat/completions',
|
| 168 |
-
|
| 169 |
-
|
| 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 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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) => {
|