Spaces:
Sleeping
Sleeping
Upload index.js
Browse files
index.js
CHANGED
@@ -560,55 +560,69 @@ async function handleNormalResponse(response, model, res) {
|
|
560 |
Utils.handleError(error, res);
|
561 |
}
|
562 |
}
|
|
|
563 |
async function handleImageResponse(imageUrl) {
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
|
|
568 |
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
|
589 |
-
|
590 |
-
|
591 |
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
|
|
|
|
|
|
596 |
}
|
597 |
-
// 等待一段时间后重试
|
598 |
-
await new Promise(resolve => setTimeout(resolve, 500 * retryCount));
|
599 |
}
|
600 |
-
}
|
601 |
|
602 |
-
|
603 |
-
|
604 |
|
605 |
-
|
606 |
|
607 |
-
|
608 |
-
|
609 |
|
610 |
-
|
611 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
612 |
}
|
613 |
|
614 |
// 404处理
|
|
|
560 |
Utils.handleError(error, res);
|
561 |
}
|
562 |
}
|
563 |
+
|
564 |
async function handleImageResponse(imageUrl) {
|
565 |
+
try {
|
566 |
+
//对服务器发送图片请求
|
567 |
+
const MAX_RETRIES = 3;
|
568 |
+
let retryCount = 0;
|
569 |
+
let imageBase64Response;
|
570 |
|
571 |
+
while (retryCount < MAX_RETRIES) {
|
572 |
+
try {
|
573 |
+
//发送图片请求获取图片
|
574 |
+
imageBase64Response = await fetch(`https://assets.grok.com/${imageUrl}`, {
|
575 |
+
method: 'GET',
|
576 |
+
headers: {
|
577 |
+
...DEFAULT_HEADERS,
|
578 |
+
...CONFIG.API.SIGNATURE_COOKIE
|
579 |
+
}
|
580 |
+
});
|
581 |
|
582 |
+
if (imageBase64Response.ok) {
|
583 |
+
break; // 如果请求成功,跳出重试循环
|
584 |
+
}
|
585 |
|
586 |
+
retryCount++;
|
587 |
+
if (retryCount === MAX_RETRIES) {
|
588 |
+
throw new Error(`上游服务请求失败! status: ${imageBase64Response.status}`);
|
589 |
+
}
|
590 |
|
591 |
+
// 等待一段时间后重试(可以使用指数退避)
|
592 |
+
await new Promise(resolve => setTimeout(resolve, 500 * retryCount));
|
593 |
|
594 |
+
} catch (error) {
|
595 |
+
retryCount++;
|
596 |
+
if (retryCount === MAX_RETRIES) {
|
597 |
+
throw error;
|
598 |
+
}
|
599 |
+
// 等待一段时间后重试
|
600 |
+
await new Promise(resolve => setTimeout(resolve, 500 * retryCount));
|
601 |
}
|
|
|
|
|
602 |
}
|
|
|
603 |
|
604 |
+
const arrayBuffer = await imageBase64Response.arrayBuffer();
|
605 |
+
const imagebuffer = Buffer.from(arrayBuffer);
|
606 |
|
607 |
+
const base64String = imagebuffer.toString('base64');
|
608 |
|
609 |
+
// 获取图片类型(例如:image/jpeg, image/png)
|
610 |
+
const imageContentType = imageBase64Response.headers.get('content-type');
|
611 |
|
612 |
+
// 修改返回结构,确保返回Markdown格式的图片标签
|
613 |
+
return {
|
614 |
+
type: "image_url",
|
615 |
+
image_url: {
|
616 |
+
url: `data:image/jpg;base64,${base64String}`
|
617 |
+
}
|
618 |
+
};
|
619 |
+
|
620 |
+
// 或者如果需要直接返回可显示的字符串
|
621 |
+
// return ``;
|
622 |
+
} catch (error) {
|
623 |
+
console.error('图片处理失败:', error);
|
624 |
+
return '图片生成失败';
|
625 |
+
}
|
626 |
}
|
627 |
|
628 |
// 404处理
|