Spaces:
Running
Running
Update server.js
Browse files
server.js
CHANGED
@@ -427,6 +427,7 @@ async function makeFalRequestWithRetry(falInput, stream = false) {
|
|
427 |
if (isKeyRelatedError(error)) {
|
428 |
console.warn(`Marking Fal Key index ${keyInfo.index} as invalid due to caught initiation error.`);
|
429 |
invalidKeys.add(keyInfo.key);
|
|
|
430 |
// Continue loop to try the next key
|
431 |
} else {
|
432 |
console.error("Initiation error does not appear to be key-related. Failing request without further key retries.");
|
@@ -510,9 +511,9 @@ app.post('/v1/chat/completions', async (req, res) => {
|
|
510 |
id: `chatcmpl-${Date.now()}-error`, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: model,
|
511 |
choices: [{ index: 0, delta: {}, finish_reason: "error", message: { role: 'assistant', content: `Fal Stream Event Error: ${JSON.stringify(errorInfo)}` } }]
|
512 |
};
|
|
|
513 |
if (!res.writableEnded) { res.write(`data: ${JSON.stringify(errorChunk)}\n\n`); }
|
514 |
else { console.warn("Stream ended before writing event error."); }
|
515 |
-
// Potentially break or add logic based on error type here
|
516 |
}
|
517 |
|
518 |
// Calculate the delta (new content)
|
@@ -539,6 +540,7 @@ app.post('/v1/chat/completions', async (req, res) => {
|
|
539 |
finish_reason: isPartial === false ? "stop" : null
|
540 |
}]
|
541 |
};
|
|
|
542 |
if (!res.writableEnded) { res.write(`data: ${JSON.stringify(openAIChunk)}\n\n`); }
|
543 |
else { console.warn("Stream ended before writing data chunk."); }
|
544 |
}
|
@@ -553,42 +555,65 @@ app.post('/v1/chat/completions', async (req, res) => {
|
|
553 |
console.log("<-- Stream processing finished, but connection was already ended before [DONE].");
|
554 |
}
|
555 |
|
|
|
|
|
|
|
556 |
} catch (streamError) {
|
557 |
// **Catch block for errors during stream processing OR initiation failure**
|
558 |
console.error('Error during stream request processing/initiation:', streamError.message || streamError);
|
|
|
|
|
559 |
|
560 |
// **Check if the error is key-related AND if initiation succeeded**
|
561 |
// This ensures we only invalidate the key if the error happened *during* processing
|
562 |
// using a key that successfully initiated the stream.
|
563 |
if (keyUsedForRequest && isKeyRelatedError(streamError)) {
|
564 |
-
console.warn(
|
565 |
invalidKeys.add(keyUsedForRequest);
|
|
|
566 |
}
|
567 |
-
// else: Error was
|
568 |
|
569 |
-
// --- Report error back to the client ---
|
570 |
try {
|
|
|
571 |
if (!res.headersSent) {
|
572 |
-
|
573 |
const errorMessage = (streamError instanceof Error) ? streamError.message : JSON.stringify(streamError);
|
574 |
-
|
|
|
|
|
575 |
console.log("<-- Stream initiation failed response sent (502).");
|
576 |
-
}
|
577 |
-
|
|
|
|
|
578 |
const errorDetails = (streamError instanceof Error) ? streamError.message : JSON.stringify(streamError);
|
579 |
-
|
580 |
-
|
581 |
-
res.
|
|
|
|
|
|
|
582 |
console.log("<-- Stream error sent within stream, stream ended.");
|
583 |
} else {
|
584 |
-
|
|
|
585 |
}
|
586 |
} catch (finalError) {
|
587 |
-
|
588 |
-
|
|
|
|
|
|
|
|
|
|
|
589 |
}
|
590 |
// --- End error reporting ---
|
591 |
}
|
|
|
|
|
|
|
592 |
|
593 |
} else {
|
594 |
// --- Non-Stream Request ---
|
|
|
427 |
if (isKeyRelatedError(error)) {
|
428 |
console.warn(`Marking Fal Key index ${keyInfo.index} as invalid due to caught initiation error.`);
|
429 |
invalidKeys.add(keyInfo.key);
|
430 |
+
console.log(`--> Invalid Keys Set now contains: [${Array.from(invalidKeys).join(', ')}]`); // Log the set content
|
431 |
// Continue loop to try the next key
|
432 |
} else {
|
433 |
console.error("Initiation error does not appear to be key-related. Failing request without further key retries.");
|
|
|
511 |
id: `chatcmpl-${Date.now()}-error`, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: model,
|
512 |
choices: [{ index: 0, delta: {}, finish_reason: "error", message: { role: 'assistant', content: `Fal Stream Event Error: ${JSON.stringify(errorInfo)}` } }]
|
513 |
};
|
514 |
+
// Check write status before sending
|
515 |
if (!res.writableEnded) { res.write(`data: ${JSON.stringify(errorChunk)}\n\n`); }
|
516 |
else { console.warn("Stream ended before writing event error."); }
|
|
|
517 |
}
|
518 |
|
519 |
// Calculate the delta (new content)
|
|
|
540 |
finish_reason: isPartial === false ? "stop" : null
|
541 |
}]
|
542 |
};
|
543 |
+
// Check write status before sending
|
544 |
if (!res.writableEnded) { res.write(`data: ${JSON.stringify(openAIChunk)}\n\n`); }
|
545 |
else { console.warn("Stream ended before writing data chunk."); }
|
546 |
}
|
|
|
555 |
console.log("<-- Stream processing finished, but connection was already ended before [DONE].");
|
556 |
}
|
557 |
|
558 |
+
// ==================================================
|
559 |
+
// START: UPDATED CATCH BLOCK FOR STREAM HANDLING
|
560 |
+
// ==================================================
|
561 |
} catch (streamError) {
|
562 |
// **Catch block for errors during stream processing OR initiation failure**
|
563 |
console.error('Error during stream request processing/initiation:', streamError.message || streamError);
|
564 |
+
// **Log the specific error object for more details**
|
565 |
+
console.error('Full streamError object:', streamError);
|
566 |
|
567 |
// **Check if the error is key-related AND if initiation succeeded**
|
568 |
// This ensures we only invalidate the key if the error happened *during* processing
|
569 |
// using a key that successfully initiated the stream.
|
570 |
if (keyUsedForRequest && isKeyRelatedError(streamError)) {
|
571 |
+
console.warn(`--> Marking Fal Key index ${indexUsedForRequest} as invalid due to error during stream processing.`);
|
572 |
invalidKeys.add(keyUsedForRequest);
|
573 |
+
console.log(`--> Invalid Keys Set now contains: [${Array.from(invalidKeys).map(k => k.substring(0, 5) + '...').join(', ')}]`); // Log obfuscated keys in set
|
574 |
}
|
575 |
+
// else: Error was non-key-related or happened during initiation (already handled/logged in makeFalRequestWithRetry).
|
576 |
|
577 |
+
// --- Safely Report error back to the client ---
|
578 |
try {
|
579 |
+
// **Check headersSent first for initiation errors**
|
580 |
if (!res.headersSent) {
|
581 |
+
console.log("<-- Attempting to send 502 error response (headers not sent).");
|
582 |
const errorMessage = (streamError instanceof Error) ? streamError.message : JSON.stringify(streamError);
|
583 |
+
// Avoid sending huge objects if stringify fails or is too large
|
584 |
+
const detail = String(errorMessage).length < 1000 ? errorMessage : "Error details too large or complex, check server logs.";
|
585 |
+
res.status(502).json({ error: 'Failed to initiate Fal stream', details: detail });
|
586 |
console.log("<-- Stream initiation failed response sent (502).");
|
587 |
+
}
|
588 |
+
// **Check writableEnded for errors during processing**
|
589 |
+
else if (!res.writableEnded) {
|
590 |
+
console.log("<-- Attempting to send error within stream (headers sent, not ended).");
|
591 |
const errorDetails = (streamError instanceof Error) ? streamError.message : JSON.stringify(streamError);
|
592 |
+
const detail = String(errorDetails).length < 1000 ? errorDetails : "Error details too large or complex, check server logs.";
|
593 |
+
// Send an error object in the SSE stream format
|
594 |
+
res.write(`data: ${JSON.stringify({ error: { message: "Stream processing error after initiation", type: "proxy_error", details: detail } })}\n\n`);
|
595 |
+
// Still attempt to send [DONE] for robust client handling
|
596 |
+
res.write(`data: [DONE]\n\n`);
|
597 |
+
res.end(); // Explicitly end the response
|
598 |
console.log("<-- Stream error sent within stream, stream ended.");
|
599 |
} else {
|
600 |
+
// Stream already ended, just log server-side.
|
601 |
+
console.log("<-- Stream error occurred, but connection was already ended. Cannot send error to client.");
|
602 |
}
|
603 |
} catch (finalError) {
|
604 |
+
// Error trying to send the error message itself (e.g., network gone)
|
605 |
+
console.error('!!! Error sending stream error message to client:', finalError);
|
606 |
+
// Attempt to end the response if it wasn't already
|
607 |
+
if (!res.writableEnded) {
|
608 |
+
console.log("<-- Forcefully ending response after error during error reporting.");
|
609 |
+
res.end();
|
610 |
+
}
|
611 |
}
|
612 |
// --- End error reporting ---
|
613 |
}
|
614 |
+
// ==================================================
|
615 |
+
// END: UPDATED CATCH BLOCK FOR STREAM HANDLING
|
616 |
+
// ==================================================
|
617 |
|
618 |
} else {
|
619 |
// --- Non-Stream Request ---
|