fal-openai-proxy / server.js
Calmlo's picture
Update server.js
984e8a0 verified
raw
history blame
20.6 kB
import express from 'express';
import { fal } from '@fal-ai/client';
// --- Key Management Setup ---
// Read comma-separated keys from the SINGLE environment variable FAL_KEY
const FAL_KEY_STRING = process.env.FAL_KEY;
// Read the custom API Key for proxy authentication
const API_KEY = process.env.API_KEY;
// --- (Initial checks for FAL_KEY_STRING, API_KEY, parsing falKeys remain the same) ---
if (!FAL_KEY_STRING) {
console.error("ERROR: FAL_KEY environment variable is not set.");
console.error("Ensure FAL_KEY contains a comma-separated list of your Fal AI keys.");
process.exit(1);
}
const falKeys = FAL_KEY_STRING.split(',')
.map(key => key.trim())
.filter(key => key.length > 0);
if (falKeys.length === 0) {
console.error("ERROR: No valid Fal keys found in the FAL_KEY environment variable after parsing.");
console.error("Ensure FAL_KEY is a comma-separated list, e.g., 'key1,key2,key3'.");
process.exit(1);
}
if (!API_KEY) {
console.error("ERROR: API_KEY environment variable is not set.");
process.exit(1);
}
// --- (End initial checks) ---
let currentKeyIndex = 0;
const invalidKeys = new Set();
console.log(`Loaded ${falKeys.length} Fal AI Key(s) from the FAL_KEY environment variable.`);
// --- (getNextValidKey function remains the same) ---
function getNextValidKey() {
if (invalidKeys.size >= falKeys.length) {
console.error("All Fal AI keys are marked as invalid.");
return null;
}
const initialIndex = currentKeyIndex;
let attempts = 0;
while (attempts < falKeys.length) {
const keyIndex = currentKeyIndex % falKeys.length;
const key = falKeys[keyIndex];
currentKeyIndex = (keyIndex + 1) % falKeys.length;
if (!invalidKeys.has(key)) {
console.log(`Using Fal Key index: ${keyIndex} (from FAL_KEY list)`);
return { key, index: keyIndex };
} else {
console.log(`Skipping invalid Fal Key index: ${keyIndex}`);
}
attempts++;
if (currentKeyIndex === initialIndex && attempts > 0) {
console.warn("Looped through all keys, potentially all are invalid.");
break;
}
}
console.error("Could not find a valid Fal AI key after checking all potentially available keys.");
return null;
}
// --- (isKeyRelatedError function remains the same) ---
function isKeyRelatedError(error) {
if (!error) return false;
const message = error.message?.toLowerCase() || '';
const status = error.status || error.statusCode;
if (status === 401 || status === 403 || status === 429) {
console.warn(`Detected potential key-related error (HTTP Status: ${status}).`);
return true;
}
const keyErrorPatterns = [
'invalid api key', 'authentication failed', 'permission denied',
'quota exceeded', 'forbidden', 'unauthorized', 'rate limit',
'credentials', 'api key missing', 'invalid credential',
'exhausted balance', 'user is locked' // Add specific messages if observed
];
if (keyErrorPatterns.some(pattern => message.includes(pattern))) {
console.warn(`Detected potential key-related error (message contains relevant pattern: "${message}")`);
return true;
}
// Also check the body.detail if status is 403, as seen in the logs
if (status === 403 && error.body?.detail) {
const detailMessage = error.body.detail.toLowerCase();
if (keyErrorPatterns.some(pattern => detailMessage.includes(pattern))) {
console.warn(`Detected potential key-related error (body.detail contains relevant pattern: "${detailMessage}")`);
return true;
}
}
return false;
}
// --- End Key Management Setup ---
const app = express();
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
const PORT = process.env.PORT || 3000;
// --- (apiKeyAuth middleware remains the same) ---
const apiKeyAuth = (req, res, next) => {
const authHeader = req.headers['authorization'];
if (!authHeader) {
console.warn('Unauthorized: No Authorization header provided');
return res.status(401).json({ error: 'Unauthorized: No API Key provided' });
}
const authParts = authHeader.split(' ');
if (authParts.length !== 2 || authParts[0].toLowerCase() !== 'bearer') {
console.warn('Unauthorized: Invalid Authorization header format. Expected "Bearer <key>".');
return res.status(401).json({ error: 'Unauthorized: Invalid Authorization header format' });
}
const providedKey = authParts[1];
if (providedKey !== API_KEY) {
console.warn('Unauthorized: Invalid API Key provided.');
return res.status(401).json({ error: 'Unauthorized: Invalid API Key' });
}
next();
};
app.use(['/v1/models', '/v1/chat/completions'], apiKeyAuth);
// --- (Global Limits, FAL_SUPPORTED_MODELS, getOwner remain the same) ---
const PROMPT_LIMIT = 4800;
const SYSTEM_PROMPT_LIMIT = 4800;
const FAL_SUPPORTED_MODELS = [ /* ... model list ... */ ];
const getOwner = (modelId) => { /* ... */ };
// --- (GET /v1/models endpoint remains the same) ---
app.get('/v1/models', (req, res) => { /* ... */ });
// --- (convertMessagesToFalPrompt function remains the same) ---
function convertMessagesToFalPrompt(messages) { /* ... */ }
/**
* MODIFIED: Makes a request to the Fal AI API, handling key rotation and retries on key-related errors.
* For stream requests, returns the stream AND the key info used.
* @param {object} falInput - The input object for the Fal AI API call.
* @param {boolean} [stream=false] - Whether to make a streaming request.
* @returns {Promise<object|{stream: AsyncIterable<object>, keyUsed: string, indexUsed: number}>}
* The result object for non-stream, or an object containing the stream and key info for stream.
* @throws {Error} If the request fails after trying all valid keys, or if a non-key-related error occurs during *initiation*.
*/
async function makeFalRequestWithRetry(falInput, stream = false) {
let attempts = 0;
const maxAttempts = falKeys.length;
const attemptedKeysInThisRequest = new Set();
while (attempts < maxAttempts) {
const keyInfo = getNextValidKey();
if (!keyInfo) {
console.error("makeFalRequestWithRetry: No valid Fal AI keys remaining.");
throw new Error("No valid Fal AI keys available (all marked as invalid).");
}
if (attemptedKeysInThisRequest.has(keyInfo.key)) {
console.warn(`Key at index ${keyInfo.index} was already attempted for this request. Skipping to find next different key.`);
continue;
}
attemptedKeysInThisRequest.add(keyInfo.key);
attempts++;
try {
console.log(`Attempt ${attempts}/${maxAttempts}: Trying Fal Key index ${keyInfo.index}...`);
console.warn(`Configuring GLOBAL fal client with key index ${keyInfo.index}. Review concurrency implications.`);
fal.config({ credentials: keyInfo.key });
if (stream) {
const falStream = await fal.stream("fal-ai/any-llm", { input: falInput });
console.log(`Successfully initiated stream with key index ${keyInfo.index}.`);
// **MODIFIED: Return stream AND key info**
return { stream: falStream, keyUsed: keyInfo.key, indexUsed: keyInfo.index };
} else {
// Non-stream logic remains the same
console.log(`Executing non-stream request with key index ${keyInfo.index}...`);
const result = await fal.subscribe("fal-ai/any-llm", { input: falInput, logs: true });
console.log(`Successfully received non-stream result with key index ${keyInfo.index}.`);
if (result && result.error) {
console.error(`Fal AI returned an error object within the non-stream result payload (Key Index ${keyInfo.index}):`, result.error);
if (isKeyRelatedError(result.error)) {
console.warn(`Marking Fal Key index ${keyInfo.index} as invalid due to error in response payload.`);
invalidKeys.add(keyInfo.key);
continue; // Try next key
} else {
throw new Error(`Fal AI error reported in result payload: ${JSON.stringify(result.error)}`);
}
}
return result; // Return only result for non-stream
}
} catch (error) {
// This catch block now primarily handles errors during *request initiation*
console.error(`Error caught during request initiation using Fal Key index ${keyInfo.index}:`, error.message || error);
if (isKeyRelatedError(error)) {
console.warn(`Marking Fal Key index ${keyInfo.index} as invalid due to caught initiation error.`);
invalidKeys.add(keyInfo.key);
// Continue loop to try the next key
} else {
console.error("Initiation error does not appear to be key-related. Failing request without further key retries.");
throw error; // Re-throw non-key-related initiation error
}
}
} // End while loop
// If loop finishes, all keys failed during initiation
throw new Error(`Request initiation failed after trying ${attempts} unique Fal key(s). All failed with key-related errors or were already marked invalid.`);
}
// POST /v1/chat/completions endpoint - Handles chat requests
app.post('/v1/chat/completions', async (req, res) => {
const { model, messages, stream = false, reasoning = false, ...restOpenAIParams } = req.body;
console.log(`--> POST /v1/chat/completions | Model: ${model} | Stream: ${stream}`);
// --- (Input validation for model, messages remains the same) ---
if (!model || !messages || !Array.isArray(messages) || messages.length === 0) {
console.error("Invalid request: Missing 'model' or 'messages' array is empty/invalid.");
return res.status(400).json({ error: 'Bad Request: `model` and a non-empty `messages` array are required.' });
}
let keyUsedForRequest = null; // Variable to store the key used for this request, if successful initiation
let indexUsedForRequest = null;
try {
const { prompt, system_prompt } = convertMessagesToFalPrompt(messages);
const falInput = { /* ... falInput setup ... */ };
falInput.model = model;
falInput.prompt = prompt;
if (system_prompt && system_prompt.length > 0) {
falInput.system_prompt = system_prompt;
}
falInput.reasoning = !!reasoning;
console.log("Attempting Fal request with key rotation/retry logic...");
console.log(`Prepared Input Lengths - System Prompt: ${system_prompt?.length || 0}, Prompt: ${prompt?.length || 0}`);
if (stream) {
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
/* ... other headers ... */
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.setHeader('Access-Control-Allow-Origin', '*');
res.flushHeaders();
let previousOutput = '';
let streamResult; // To hold the object { stream, keyUsed, indexUsed }
try {
// **MODIFIED: Get stream and key info**
streamResult = await makeFalRequestWithRetry(falInput, true);
const falStream = streamResult.stream;
keyUsedForRequest = streamResult.keyUsed; // Store the key used for this stream
indexUsedForRequest = streamResult.indexUsed;
// Process the stream events asynchronously
for await (const event of falStream) {
// --- (Stream event processing logic remains the same) ---
const currentOutput = (event && typeof event.output === 'string') ? event.output : '';
const isPartial = (event && typeof event.partial === 'boolean') ? event.partial : true;
const errorInfo = (event && event.error) ? event.error : null;
if (errorInfo) {
// Log error from within the stream, but might continue processing
console.error("Error received *within* fal stream event payload:", errorInfo);
const errorChunk = { /* ... error chunk details ... */ };
if (!res.writableEnded) { res.write(`data: ${JSON.stringify(errorChunk)}\n\n`); }
else { console.warn("Stream ended before writing event error."); }
// Decide whether to break or continue based on error severity if needed
}
let deltaContent = '';
if (currentOutput.startsWith(previousOutput)) {
deltaContent = currentOutput.substring(previousOutput.length);
} else if (currentOutput.length > 0) {
console.warn("Fal stream output mismatch/reset. Sending full current output as delta.");
deltaContent = currentOutput;
previousOutput = '';
}
previousOutput = currentOutput;
if (deltaContent || !isPartial) {
const openAIChunk = { /* ... chunk details ... */ };
openAIChunk.id = `chatcmpl-${Date.now()}`;
openAIChunk.object = "chat.completion.chunk";
openAIChunk.created = Math.floor(Date.now() / 1000);
openAIChunk.model = model;
openAIChunk.choices = [{ index: 0, delta: { content: deltaContent }, finish_reason: isPartial === false ? "stop" : null }];
if (!res.writableEnded) { res.write(`data: ${JSON.stringify(openAIChunk)}\n\n`); }
else { console.warn("Stream ended before writing data chunk."); }
}
// --- (End stream event processing) ---
} // End for-await loop
// Send [DONE] marker
if (!res.writableEnded) {
res.write(`data: [DONE]\n\n`);
res.end();
console.log("<-- Stream finished successfully and [DONE] sent.");
} else {
console.log("<-- Stream processing finished, but connection was already ended before [DONE].");
}
} catch (streamError) {
// **MODIFIED CATCH BLOCK for stream processing errors**
// This catches errors from makeFalRequestWithRetry (initiation failure)
// OR errors thrown during the 'for await...of falStream' loop.
console.error('Error during stream request processing:', streamError.message || streamError);
// **NEW: Check if the error is key-related and invalidate the key if needed**
// We only do this if keyUsedForRequest has been set (meaning initiation succeeded)
// And if the error occurred *during* the stream processing, not during initiation
// (initiation errors are handled inside makeFalRequestWithRetry)
// The check `keyUsedForRequest !== null` helps distinguish.
if (keyUsedForRequest && isKeyRelatedError(streamError)) {
console.warn(`Marking Fal Key index ${indexUsedForRequest} as invalid due to error during stream processing.`);
invalidKeys.add(keyUsedForRequest);
}
// else: The error was either not key-related, or occurred during initiation (already handled)
// --- (Error reporting logic to client remains the same) ---
try {
if (!res.headersSent) {
// Error likely during initiation (caught from makeFalRequestWithRetry)
const errorMessage = (streamError instanceof Error) ? streamError.message : JSON.stringify(streamError);
res.status(502).json({ error: 'Failed to initiate Fal stream', details: errorMessage });
console.log("<-- Stream initiation failed response sent (502).");
} else if (!res.writableEnded) {
// Error during stream processing after headers sent
const errorDetails = (streamError instanceof Error) ? streamError.message : JSON.stringify(streamError);
res.write(`data: ${JSON.stringify({ error: { message: "Stream processing error after initiation", type: "proxy_error", details: errorDetails } })}\n\n`);
res.write(`data: [DONE]\n\n`);
res.end();
console.log("<-- Stream error sent within stream, stream ended.");
} else {
console.log("<-- Stream error occurred, but connection was already ended.");
}
} catch (finalError) {
console.error('Error sending stream error message to client:', finalError);
if (!res.writableEnded) { res.end(); }
}
// --- (End error reporting) ---
}
} else {
// --- Non-Stream Logic (remains the same, uses makeFalRequestWithRetry directly) ---
try {
const result = await makeFalRequestWithRetry(falInput, false);
const openAIResponse = { /* ... construct response ... */ };
openAIResponse.id = `chatcmpl-${result.requestId || Date.now()}`;
openAIResponse.object = "chat.completion";
openAIResponse.created = Math.floor(Date.now() / 1000);
openAIResponse.model = model;
openAIResponse.choices = [{ index: 0, message: { role: "assistant", content: result.output || "" }, finish_reason: "stop" }];
openAIResponse.usage = { prompt_tokens: null, completion_tokens: null, total_tokens: null };
openAIResponse.system_fingerprint = null;
if (result.reasoning) { openAIResponse.fal_reasoning = result.reasoning; }
res.json(openAIResponse);
console.log("<-- Non-stream response sent successfully.");
} catch (error) {
console.error('Error during non-stream request processing:', error.message || error);
if (!res.headersSent) {
const errorMessage = (error instanceof Error) ? error.message : JSON.stringify(error);
const finalMessage = errorMessage.includes("No valid Fal AI keys available") || errorMessage.includes("Request failed after trying")
? `Fal request failed: ${errorMessage}`
: `Internal Server Error processing Fal request: ${errorMessage}`;
res.status(502).json({ error: 'Fal Request Failed', details: finalMessage });
console.log("<-- Non-stream error response sent (502).");
} else {
console.error("Headers already sent for non-stream error response? This is unexpected.");
if (!res.writableEnded) { res.end(); }
}
}
}
} catch (error) {
// --- (Outer catch block for setup errors remains the same) ---
console.error('Unhandled error before initiating Fal request (likely setup or input conversion):', error.message || error);
if (!res.headersSent) {
const errorMessage = (error instanceof Error) ? error.message : JSON.stringify(error);
res.status(500).json({ error: 'Internal Server Error in Proxy Setup', details: errorMessage });
console.log("<-- Proxy setup error response sent (500).");
} else {
console.error("Headers already sent when catching setup error. Ending response.");
if (!res.writableEnded) { res.end(); }
}
}
});
// --- (Server listen and root path handler remain the same) ---
app.listen(PORT, () => { /* ... startup messages ... */ });
app.get('/', (req, res) => { /* ... root message ... */ });