Skip to content

Commit f0b4fb8

Browse files
committed
fix(chat): handle invalid image payload and stream n fallback
1 parent 7f9627d commit f0b4fb8

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

app/static/chat/chat.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
</main>
175175

176176
<script src="/static/common/toast.js"></script>
177-
<script src="/static/chat/chat.js?v=2"></script>
177+
<script src="/static/chat/chat.js?v=3"></script>
178178
</body>
179179

180180
</html>

app/static/chat/chat.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,24 @@ function detectBase64ImageMime(base64Text) {
5959
function toImageDataUrl(raw) {
6060
const value = String(raw || '').trim();
6161
if (!value) return '';
62+
const lowered = value.toLowerCase();
63+
if (['error', 'null', 'none', 'undefined'].includes(lowered)) return '';
6264
if (value.startsWith('data:image/')) return value;
6365
const mime = detectBase64ImageMime(value);
6466
return `data:${mime};base64,${value}`;
6567
}
6668

6769
function pickImageSrc(item) {
6870
const rawUrl = String(item?.url || '').trim();
69-
if (rawUrl && rawUrl !== 'https://assets.grok.com/' && rawUrl !== 'https://assets.grok.com') {
71+
const rawUrlLower = rawUrl.toLowerCase();
72+
if (
73+
rawUrl &&
74+
rawUrl !== 'https://assets.grok.com/' &&
75+
rawUrl !== 'https://assets.grok.com' &&
76+
rawUrlLower !== 'error' &&
77+
rawUrlLower !== 'null' &&
78+
rawUrlLower !== 'undefined'
79+
) {
7080
return toAbsoluteUrl(rawUrl);
7181
}
7282
const b64json = String(item?.b64_json || '').trim();
@@ -635,6 +645,7 @@ async function generateImage() {
635645
const model = String(q('model-select').value || 'grok-imagine-1.0').trim();
636646
const n = Math.max(1, Math.min(10, Math.floor(Number(q('image-n').value || 1) || 1)));
637647
const stream = Boolean(q('stream-toggle').checked);
648+
const useStream = stream && n <= 2;
638649
const { size, concurrency } = buildImageRequestConfig();
639650

640651
const headers = { ...buildApiHeaders(), 'Content-Type': 'application/json' };
@@ -645,7 +656,11 @@ async function generateImage() {
645656

646657
const reqBody = { prompt, model, n, size, concurrency };
647658
try {
648-
if (stream) {
659+
if (stream && !useStream) {
660+
showToast('n > 2 automatically disables Stream and falls back to non-stream mode.', 'warning');
661+
}
662+
663+
if (useStream) {
649664
const rendered = await streamImage(reqBody, headers);
650665
if (!rendered) throw new Error('没有生成结果');
651666
return;

app/static/chat/chat_admin.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
<script src="/static/common/admin-auth.js"></script>
171171
<script src="/static/common/header.js?v=3"></script>
172172
<script src="/static/common/footer.js"></script>
173-
<script src="/static/chat/chat.js?v=2"></script>
173+
<script src="/static/chat/chat.js?v=3"></script>
174174
</body>
175175

176176
</html>

0 commit comments

Comments
 (0)