yxmiler commited on
Commit
5a3a682
·
verified ·
1 Parent(s): 32c26eb

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +15 -9
index.js CHANGED
@@ -591,6 +591,7 @@ async function handleResponse(response, model, res, isStream) {
591
  res.setHeader('Cache-Control', 'no-cache');
592
  res.setHeader('Connection', 'keep-alive');
593
  }
 
594
 
595
  return new Promise((resolve, reject) => {
596
  stream.on('data', async (chunk) => {
@@ -605,7 +606,7 @@ async function handleResponse(response, model, res, isStream) {
605
  const data = trimmedLine.substring(6);
606
  try {
607
  if (!data.trim()) continue;
608
- if (data === "[DONE]") continue;
609
  const linejosn = JSON.parse(data);
610
  if (linejosn?.error) {
611
  Logger.error(JSON.stringify(linejosn, null, 2), 'Server');
@@ -813,7 +814,8 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
813
  let retryCount = 0;
814
  const grokClient = new GrokApiClient(req.body.model);
815
  const requestPayload = await grokClient.prepareChatRequest(req.body);
816
- Logger.info(`当前请求体: ${JSON.stringify(requestPayload,null,2)}`, 'Server');
 
817
  while (retryCount < CONFIG.RETRY.MAX_ATTEMPTS) {
818
  retryCount++;
819
  if (!CONFIG.API.TEMP_COOKIE) {
@@ -844,9 +846,15 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
844
  }
845
  })
846
  });
847
-
848
- const responseText = await newMessageReq.json();
849
- const conversationId = responseText.conversationId;
 
 
 
 
 
 
850
 
851
  const response = await fetch(`${CONFIG.API.BASE_URL}/api/conversations/${conversationId}/responses`, {
852
  method: 'POST',
@@ -915,10 +923,8 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
915
  Logger.error(error, 'ChatAPI');
916
  res.status(500).json({
917
  error: {
918
- message: error.message,
919
- type: 'server_error',
920
- param: null,
921
- code: error.code || null
922
  }
923
  });
924
  }
 
591
  res.setHeader('Cache-Control', 'no-cache');
592
  res.setHeader('Connection', 'keep-alive');
593
  }
594
+ Logger.info("开始处理流式响应", 'Server');
595
 
596
  return new Promise((resolve, reject) => {
597
  stream.on('data', async (chunk) => {
 
606
  const data = trimmedLine.substring(6);
607
  try {
608
  if (!data.trim()) continue;
609
+ if (data === "[DONE]") continue;
610
  const linejosn = JSON.parse(data);
611
  if (linejosn?.error) {
612
  Logger.error(JSON.stringify(linejosn, null, 2), 'Server');
 
814
  let retryCount = 0;
815
  const grokClient = new GrokApiClient(req.body.model);
816
  const requestPayload = await grokClient.prepareChatRequest(req.body);
817
+ Logger.info(`请求体: ${JSON.stringify(requestPayload,null,2)}`, 'Server');
818
+
819
  while (retryCount < CONFIG.RETRY.MAX_ATTEMPTS) {
820
  retryCount++;
821
  if (!CONFIG.API.TEMP_COOKIE) {
 
846
  }
847
  })
848
  });
849
+ let conversationId;
850
+ var responseText2 = await newMessageReq.clone().text();
851
+ if (newMessageReq.status === 200) {
852
+ const responseText = await newMessageReq.json();
853
+ conversationId = responseText.conversationId;
854
+ } else {
855
+ Logger.error(`创建会话请求: ${responseText2}`, 'Server');
856
+ throw new Error(`创建会话响应错误: ${responseText2}`);
857
+ }
858
 
859
  const response = await fetch(`${CONFIG.API.BASE_URL}/api/conversations/${conversationId}/responses`, {
860
  method: 'POST',
 
923
  Logger.error(error, 'ChatAPI');
924
  res.status(500).json({
925
  error: {
926
+ message: error.message || error,
927
+ type: 'server_error'
 
 
928
  }
929
  });
930
  }