Upload index.js
Browse files
index.js
CHANGED
@@ -51,6 +51,9 @@ const DEFAULT_HEADERS = {
|
|
51 |
'baggage': 'sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c'
|
52 |
};
|
53 |
|
|
|
|
|
|
|
54 |
class Utils {
|
55 |
static async extractGrokHeaders() {
|
56 |
try {
|
@@ -93,8 +96,13 @@ class Utils {
|
|
93 |
if (headers) {
|
94 |
console.log("获取认证信息成功");
|
95 |
CONFIG.API.SIGNATURE_COOKIE = { cookie: `x-anonuserid=${headers["x-anonuserid"]}; x-challenge=${headers["x-challenge"]}; x-signature=${headers["x-signature"]}` };
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
98 |
}
|
99 |
retryCount++;
|
100 |
if (retryCount >= CONFIG.RETRY.MAX_ATTEMPTS) {
|
@@ -147,6 +155,72 @@ class Utils {
|
|
147 |
}
|
148 |
});
|
149 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
}
|
151 |
|
152 |
|
@@ -389,65 +463,10 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
389 |
return res.status(400).json({ error: '该模型不支持流式' });
|
390 |
}
|
391 |
|
392 |
-
const makeRequest = async () => {
|
393 |
-
if (!CONFIG.API.SIGNATURE_COOKIE) {
|
394 |
-
await Utils.get_signature();
|
395 |
-
CONFIG.API.SIGNATURE_COOKIE = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), 'signature.json')));
|
396 |
-
}
|
397 |
-
const grokClient = new GrokApiClient(req.body.model);
|
398 |
-
const requestPayload = await grokClient.prepareChatRequest(req.body);
|
399 |
-
//创建新对话
|
400 |
-
const newMessageReq = await fetch(`${CONFIG.API.BASE_URL}/api/rpc`, {
|
401 |
-
method: 'POST',
|
402 |
-
headers: {
|
403 |
-
...DEFAULT_HEADERS,
|
404 |
-
...CONFIG.API.SIGNATURE_COOKIE
|
405 |
-
},
|
406 |
-
body: JSON.stringify({
|
407 |
-
rpc: "createConversation",
|
408 |
-
req: {
|
409 |
-
temporary: false
|
410 |
-
}
|
411 |
-
})
|
412 |
-
});
|
413 |
-
if (!newMessageReq.ok) {
|
414 |
-
throw new Error(`上游服务请求失败! status: ${newMessageReq.status}`);
|
415 |
-
}
|
416 |
-
|
417 |
-
// 获取响应文本
|
418 |
-
const responseText = await newMessageReq.json();
|
419 |
-
const conversationId = responseText.conversationId;
|
420 |
-
console.log("会话ID:conversationId", conversationId);
|
421 |
-
if (!conversationId) {
|
422 |
-
throw new Error(`创建会话失败! status: ${newMessageReq.status}`);
|
423 |
-
}
|
424 |
-
//发送对话
|
425 |
-
const response = await fetch(`${CONFIG.API.BASE_URL}/api/conversations/${conversationId}/responses`, {
|
426 |
-
method: 'POST',
|
427 |
-
headers: {
|
428 |
-
"accept": "text/event-stream",
|
429 |
-
"baggage": "sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c",
|
430 |
-
"content-type": "text/plain;charset=UTF-8",
|
431 |
-
"Connection": "keep-alive",
|
432 |
-
...CONFIG.API.SIGNATURE_COOKIE
|
433 |
-
},
|
434 |
-
body: JSON.stringify(requestPayload)
|
435 |
-
});
|
436 |
-
|
437 |
-
if (!response.ok) {
|
438 |
-
throw new Error(`上游服务请求失败! status: ${response.status}`);
|
439 |
-
}
|
440 |
-
|
441 |
-
if (req.body.stream) {
|
442 |
-
await handleStreamResponse(response, req.body.model, res);
|
443 |
-
} else {
|
444 |
-
await handleNormalResponse(response, req.body.model, res);
|
445 |
-
}
|
446 |
-
}
|
447 |
try {
|
448 |
-
await makeRequest();
|
449 |
} catch (error) {
|
450 |
-
await Utils.handleError(error, res
|
451 |
}
|
452 |
});
|
453 |
|
|
|
51 |
'baggage': 'sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c'
|
52 |
};
|
53 |
|
54 |
+
// 定义签名文件路径为 /tmp 目录
|
55 |
+
const SIGNATURE_FILE_PATH = '/tmp/signature.json';
|
56 |
+
|
57 |
class Utils {
|
58 |
static async extractGrokHeaders() {
|
59 |
try {
|
|
|
96 |
if (headers) {
|
97 |
console.log("获取认证信息成功");
|
98 |
CONFIG.API.SIGNATURE_COOKIE = { cookie: `x-anonuserid=${headers["x-anonuserid"]}; x-challenge=${headers["x-challenge"]}; x-signature=${headers["x-signature"]}` };
|
99 |
+
try {
|
100 |
+
fs.writeFileSync(SIGNATURE_FILE_PATH, JSON.stringify(CONFIG.API.SIGNATURE_COOKIE));//保存认证信息
|
101 |
+
return CONFIG.API.SIGNATURE_COOKIE;
|
102 |
+
} catch (error) {
|
103 |
+
console.error('写入签名文件失败:', error);
|
104 |
+
return CONFIG.API.SIGNATURE_COOKIE;
|
105 |
+
}
|
106 |
}
|
107 |
retryCount++;
|
108 |
if (retryCount >= CONFIG.RETRY.MAX_ATTEMPTS) {
|
|
|
155 |
}
|
156 |
});
|
157 |
}
|
158 |
+
|
159 |
+
static async makeRequest(req) {
|
160 |
+
try {
|
161 |
+
if (!CONFIG.API.SIGNATURE_COOKIE) {
|
162 |
+
await Utils.get_signature();
|
163 |
+
try {
|
164 |
+
CONFIG.API.SIGNATURE_COOKIE = JSON.parse(fs.readFileSync(SIGNATURE_FILE_PATH));
|
165 |
+
} catch (error) {
|
166 |
+
console.error('读取签名文件失败:', error);
|
167 |
+
await Utils.get_signature(); // 如果读取失败,重新获取签名
|
168 |
+
}
|
169 |
+
}
|
170 |
+
const grokClient = new GrokApiClient(req.body.model);
|
171 |
+
const requestPayload = await grokClient.prepareChatRequest(req.body);
|
172 |
+
//创建新对话
|
173 |
+
const newMessageReq = await fetch(`${CONFIG.API.BASE_URL}/api/rpc`, {
|
174 |
+
method: 'POST',
|
175 |
+
headers: {
|
176 |
+
...DEFAULT_HEADERS,
|
177 |
+
...CONFIG.API.SIGNATURE_COOKIE
|
178 |
+
},
|
179 |
+
body: JSON.stringify({
|
180 |
+
rpc: "createConversation",
|
181 |
+
req: {
|
182 |
+
temporary: false
|
183 |
+
}
|
184 |
+
})
|
185 |
+
});
|
186 |
+
|
187 |
+
if (!newMessageReq.ok) {
|
188 |
+
throw new Error(`上游服务请求失败! status: ${newMessageReq.status}`);
|
189 |
+
}
|
190 |
+
|
191 |
+
// 获取响应文本
|
192 |
+
const responseText = await newMessageReq.json();
|
193 |
+
const conversationId = responseText.conversationId;
|
194 |
+
console.log("会话ID:conversationId", conversationId);
|
195 |
+
if (!conversationId) {
|
196 |
+
throw new Error(`创建会话失败! status: ${newMessageReq.status}`);
|
197 |
+
}
|
198 |
+
//发送对话
|
199 |
+
const response = await fetch(`${CONFIG.API.BASE_URL}/api/conversations/${conversationId}/responses`, {
|
200 |
+
method: 'POST',
|
201 |
+
headers: {
|
202 |
+
"accept": "text/event-stream",
|
203 |
+
"baggage": "sentry-public_key=b311e0f2690c81f25e2c4cf6d4f7ce1c",
|
204 |
+
"content-type": "text/plain;charset=UTF-8",
|
205 |
+
"Connection": "keep-alive",
|
206 |
+
...CONFIG.API.SIGNATURE_COOKIE
|
207 |
+
},
|
208 |
+
body: JSON.stringify(requestPayload)
|
209 |
+
});
|
210 |
+
|
211 |
+
if (!response.ok) {
|
212 |
+
throw new Error(`上游服务请求失败! status: ${response.status}`);
|
213 |
+
}
|
214 |
+
|
215 |
+
if (req.body.stream) {
|
216 |
+
await handleStreamResponse(response, req.body.model, res);
|
217 |
+
} else {
|
218 |
+
await handleNormalResponse(response, req.body.model, res);
|
219 |
+
}
|
220 |
+
} catch (error) {
|
221 |
+
throw error;
|
222 |
+
}
|
223 |
+
}
|
224 |
}
|
225 |
|
226 |
|
|
|
463 |
return res.status(400).json({ error: '该模型不支持流式' });
|
464 |
}
|
465 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
466 |
try {
|
467 |
+
await Utils.makeRequest(req);
|
468 |
} catch (error) {
|
469 |
+
await Utils.handleError(error, res);
|
470 |
}
|
471 |
});
|
472 |
|