File size: 25,876 Bytes
ed8e0ee
9df5dcc
ed8e0ee
5de0798
713f2f6
5de0798
2cae477
713f2f6
5de0798
 
713f2f6
c9e0fd6
5de0798
713f2f6
5de0798
 
c9e0fd6
713f2f6
5de0798
 
ed8e0ee
c9e0fd6
5de0798
c9e0fd6
e706cfb
 
5de0798
713f2f6
c9e0fd6
5de0798
 
 
713f2f6
5de0798
ed8e0ee
 
 
c9e0fd6
ed8e0ee
 
5de0798
2cae477
 
c9e0fd6
2cae477
 
 
 
c9e0fd6
2cae477
 
5de0798
2cae477
 
c9e0fd6
2cae477
 
5de0798
2cae477
 
c9e0fd6
2cae477
 
c9e0fd6
5de0798
2cae477
 
5de0798
 
 
 
c9e0fd6
5de0798
c9e0fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de0798
c9e0fd6
5de0798
c9e0fd6
 
 
 
ed8e0ee
5de0798
c9e0fd6
 
 
 
5de0798
c9e0fd6
 
 
 
 
 
 
 
9df5dcc
5de0798
c9e0fd6
5de0798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9e0fd6
5de0798
 
 
 
 
 
c9e0fd6
5de0798
 
c9e0fd6
 
5de0798
 
 
 
 
 
 
c9e0fd6
 
5de0798
 
 
 
 
 
 
c9e0fd6
5de0798
 
 
 
c9e0fd6
5de0798
 
 
713f2f6
5de0798
 
 
 
 
 
 
713f2f6
5de0798
 
713f2f6
5de0798
 
 
 
 
 
 
 
713f2f6
5de0798
 
713f2f6
 
5de0798
 
 
 
 
713f2f6
5de0798
 
 
 
 
e706cfb
5de0798
 
 
 
e706cfb
5de0798
 
 
e706cfb
5de0798
 
 
e706cfb
5de0798
 
 
e706cfb
5de0798
e706cfb
5de0798
e706cfb
 
5de0798
 
 
 
 
 
713f2f6
5de0798
713f2f6
 
e706cfb
ed8e0ee
984e8a0
e706cfb
ed8e0ee
e706cfb
5de0798
c9e0fd6
e706cfb
5de0798
 
e706cfb
5de0798
c9e0fd6
984e8a0
e706cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8e0ee
e706cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
c9e0fd6
e706cfb
 
 
 
 
 
 
 
c9e0fd6
e706cfb
 
ed8e0ee
e706cfb
 
 
 
 
 
5de0798
e706cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de0798
e706cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de0798
e706cfb
 
984e8a0
e706cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8e0ee
5de0798
e706cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de0798
e706cfb
ed8e0ee
e706cfb
 
 
ed8e0ee
e706cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de0798
e706cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8e0ee
 
e706cfb
ed8e0ee
 
5de0798
c9e0fd6
5de0798
e706cfb
5de0798
 
 
 
 
 
 
c9e0fd6
 
5de0798
c9e0fd6
e706cfb
c9e0fd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import express from 'express';
import { fal } from '@fal-ai/client';

// --- Key Management ---
const FAL_KEY_STRING = process.env.FAL_KEY;
const API_KEY = process.env.API_KEY; // 自定义 API Key 环境变量保持不变

if (!FAL_KEY_STRING) {
    console.error("Error: FAL_KEY environment variable is not set.");
    process.exit(1);
}

// 解析 FAL_KEY 字符串为数组,去除空白并过滤空值
const falKeys = FAL_KEY_STRING.split(',')
                             .map(key => key.trim())
                             .filter(key => key.length > 0);

if (falKeys.length === 0) {
    console.error("Error: FAL_KEY environment variable is set, but no valid keys were found after parsing.");
    process.exit(1);
}

console.log(`Loaded ${falKeys.length} Fal AI Keys.`);

let currentFalKeyIndex = 0; // Index of the *next* key to try for a *new* request

// --- End Key Management ---


if (!API_KEY) {
    console.error("Error: API_KEY environment variable is not set.");
    process.exit(1);
}

const app = express();
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));

const PORT = process.env.PORT || 3000;

// API Key 鉴权中间件 (保持不变)
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');
        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');
        return res.status(401).json({ error: 'Unauthorized: Invalid API Key' });
    }

    next();
};

// 应用 API Key 鉴权中间件到所有 API 路由 (保持不变)
app.use(['/v1/models', '/v1/chat/completions'], apiKeyAuth);

// === 全局定义限制 === (保持不变)
const PROMPT_LIMIT = 4800;
const SYSTEM_PROMPT_LIMIT = 4800;
// === 限制定义结束 ===

// 定义 fal-ai/any-llm 支持的模型列表 (保持不变)
const FAL_SUPPORTED_MODELS = [
    "anthropic/claude-3.7-sonnet",
    "anthropic/claude-3.5-sonnet",
    "anthropic/claude-3-5-haiku",
    "anthropic/claude-3-haiku",
    "google/gemini-pro-1.5",
    "google/gemini-flash-1.5",
    "google/gemini-flash-1.5-8b",
    "google/gemini-2.0-flash-001",
    "meta-llama/llama-3.2-1b-instruct",
    "meta-llama/llama-3.2-3b-instruct",
    "meta-llama/llama-3.1-8b-instruct",
    "meta-llama/llama-3.1-70b-instruct",
    "openai/gpt-4o-mini",
    "openai/gpt-4o",
    "deepseek/deepseek-r1",
    "meta-llama/llama-4-maverick",
    "meta-llama/llama-4-scout"
];

// Helper function to get owner from model ID (保持不变)
const getOwner = (modelId) => {
    if (modelId && modelId.includes('/')) {
        return modelId.split('/')[0];
    }
    return 'fal-ai';
}

// GET /v1/models endpoint (保持不变)
app.get('/v1/models', (req, res) => {
    console.log("Received request for GET /v1/models");
    try {
        const modelsData = FAL_SUPPORTED_MODELS.map(modelId => ({
            id: modelId, object: "model", created: 1700000000, owned_by: getOwner(modelId)
        }));
        res.json({ object: "list", data: modelsData });
        console.log("Successfully returned model list.");
    } catch (error) {
        console.error("Error processing GET /v1/models:", error);
        res.status(500).json({ error: "Failed to retrieve model list." });
    }
});

// === convertMessagesToFalPrompt 函数 (保持不变) ===
function convertMessagesToFalPrompt(messages) {
    let fixed_system_prompt_content = "";
    const conversation_message_blocks = [];
    console.log(`Original messages count: ${messages.length}`);

    // 1. 分离 System 消息,格式化 User/Assistant 消息
    for (const message of messages) {
        let content = (message.content === null || message.content === undefined) ? "" : String(message.content);
        switch (message.role) {
            case 'system':
                fixed_system_prompt_content += `System: ${content}\n\n`;
                break;
            case 'user':
                conversation_message_blocks.push(`Human: ${content}\n\n`);
                break;
            case 'assistant':
                conversation_message_blocks.push(`Assistant: ${content}\n\n`);
                break;
            default:
                console.warn(`Unsupported role: ${message.role}`);
                continue;
        }
    }

    // 2. 截断合并后的 system 消息(如果超长)
    if (fixed_system_prompt_content.length > SYSTEM_PROMPT_LIMIT) {
        const originalLength = fixed_system_prompt_content.length;
        fixed_system_prompt_content = fixed_system_prompt_content.substring(0, SYSTEM_PROMPT_LIMIT);
        console.warn(`Combined system messages truncated from ${originalLength} to ${SYSTEM_PROMPT_LIMIT}`);
    }
    // 清理末尾可能多余的空白,以便后续判断和拼接
    fixed_system_prompt_content = fixed_system_prompt_content.trim();


    // 3. 计算 system_prompt 中留给对话历史的剩余空间
    let space_occupied_by_fixed_system = 0;
    if (fixed_system_prompt_content.length > 0) {
         space_occupied_by_fixed_system = fixed_system_prompt_content.length + 4; // 预留 \n\n...\n\n 的长度
    }
     const remaining_system_limit = Math.max(0, SYSTEM_PROMPT_LIMIT - space_occupied_by_fixed_system);
    console.log(`Trimmed fixed system prompt length: ${fixed_system_prompt_content.length}. Approx remaining system history limit: ${remaining_system_limit}`);


    // 4. 反向填充 User/Assistant 对话历史
    const prompt_history_blocks = [];
    const system_prompt_history_blocks = [];
    let current_prompt_length = 0;
    let current_system_history_length = 0;
    let promptFull = false;
    let systemHistoryFull = (remaining_system_limit <= 0);

    console.log(`Processing ${conversation_message_blocks.length} user/assistant messages for recency filling.`);
    for (let i = conversation_message_blocks.length - 1; i >= 0; i--) {
        const message_block = conversation_message_blocks[i];
        const block_length = message_block.length;

        if (promptFull && systemHistoryFull) {
            console.log(`Both prompt and system history slots full. Omitting older messages from index ${i}.`);
            break;
        }

        // 优先尝试放入 prompt
        if (!promptFull) {
            if (current_prompt_length + block_length <= PROMPT_LIMIT) {
                prompt_history_blocks.unshift(message_block);
                current_prompt_length += block_length;
                continue;
            } else {
                promptFull = true;
                console.log(`Prompt limit (${PROMPT_LIMIT}) reached. Trying system history slot.`);
            }
        }

        // 如果 prompt 满了,尝试放入 system_prompt 的剩余空间
        if (!systemHistoryFull) {
            if (current_system_history_length + block_length <= remaining_system_limit) {
                 system_prompt_history_blocks.unshift(message_block);
                 current_system_history_length += block_length;
                 continue;
            } else {
                 systemHistoryFull = true;
                 console.log(`System history limit (${remaining_system_limit}) reached.`);
            }
        }
    }

    // 5. *** 组合最终的 prompt 和 system_prompt (包含分隔符逻辑) ***
    const system_prompt_history_content = system_prompt_history_blocks.join('').trim();
    const final_prompt = prompt_history_blocks.join('').trim();

    // 定义分隔符
    const SEPARATOR = "\n\n-------下面是比较早之前的对话内容-----\n\n";

    let final_system_prompt = "";

    // 检查各部分是否有内容 (使用 trim 后的固定部分)
    const hasFixedSystem = fixed_system_prompt_content.length > 0;
    const hasSystemHistory = system_prompt_history_content.length > 0;

    if (hasFixedSystem && hasSystemHistory) {
        // 两部分都有,用分隔符连接
        final_system_prompt = fixed_system_prompt_content + SEPARATOR + system_prompt_history_content;
        console.log("Combining fixed system prompt and history with separator.");
    } else if (hasFixedSystem) {
        // 只有固定部分
        final_system_prompt = fixed_system_prompt_content;
        console.log("Using only fixed system prompt.");
    } else if (hasSystemHistory) {
        // 只有历史部分 (固定部分为空)
        final_system_prompt = system_prompt_history_content;
        console.log("Using only history in system prompt slot.");
    }
    // 如果两部分都为空,final_system_prompt 保持空字符串 ""

    // 6. 返回结果
    const result = {
        system_prompt: final_system_prompt, // 最终结果不需要再 trim
        prompt: final_prompt              // final_prompt 在组合前已 trim
    };

    console.log(`Final system_prompt length (Sys+Separator+Hist): ${result.system_prompt.length}`);
    console.log(`Final prompt length (Hist): ${result.prompt.length}`);

    return result;
}
// === convertMessagesToFalPrompt 函数结束 ===


// POST /v1/chat/completions endpoint (带 Key 重试逻辑)
app.post('/v1/chat/completions', async (req, res) => {
    const { model, messages, stream = false, reasoning = false, ...restOpenAIParams } = req.body;
    const requestId = `req-${Date.now()}`; // Unique ID for this incoming request

    console.log(`[${requestId}] Received chat completion request for model: ${model}, stream: ${stream}`);

    if (!FAL_SUPPORTED_MODELS.includes(model)) {
        console.warn(`[${requestId}] Warning: Requested model '${model}' is not in the explicitly supported list.`);
    }
    if (!model || !messages || !Array.isArray(messages) || messages.length === 0) {
        console.error(`[${requestId}] Invalid request parameters:`, { model, messages: Array.isArray(messages) ? messages.length : typeof messages });
        return res.status(400).json({ error: 'Missing or invalid parameters: model and messages array are required.' });
    }

    let lastError = null; // Store the last error encountered during key rotation
    let success = false; // Flag to indicate if any key succeeded

    // *** 重试循环:尝试最多 falKeys.length 次 ***
    for (let attempt = 0; attempt < falKeys.length; attempt++) {
        const keyIndexToTry = (currentFalKeyIndex + attempt) % falKeys.length;
        const selectedFalKey = falKeys[keyIndexToTry];
        console.log(`[${requestId}] Attempt ${attempt + 1}/${falKeys.length}: Trying Fal Key at index ${keyIndexToTry}`);

        try {
            // 配置 fal 客户端 (每次尝试都重新配置)
            fal.config({
                credentials: selectedFalKey,
            });

            // 准备 Fal Input (只需要准备一次)
            // 注意:如果 convertMessagesToFalPrompt 很耗时,可以移到循环外
            const { prompt, system_prompt } = convertMessagesToFalPrompt(messages);
            const falInput = {
                model: model,
                prompt: prompt,
                ...(system_prompt && { system_prompt: system_prompt }),
                reasoning: !!reasoning,
            };

            // 打印一次 Fal Input 和 Prompt 信息
            if (attempt === 0) {
                console.log(`[${requestId}] Fal Input:`, JSON.stringify(falInput, null, 2));
                console.log(`[${requestId}] Forwarding request to fal-ai with system-priority + separator + recency input:`);
                console.log(`[${requestId}] System Prompt Length:`, system_prompt?.length || 0);
                console.log(`[${requestId}] Prompt Length:`, prompt?.length || 0);
                // 为了简洁,默认注释掉详细内容的打印
                // console.log(`[${requestId}] --- System Prompt Start ---`);
                // console.log(system_prompt);
                // console.log(`[${requestId}] --- System Prompt End ---`);
                // console.log(`[${requestId}] --- Prompt Start ---`);
                // console.log(prompt);
                // console.log(`[${requestId}] --- Prompt End ---`);
            }

            // --- 执行 Fal AI 调用 ---
            if (stream) {
                // --- 流式处理 ---
                res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
                res.setHeader('Cache-Control', 'no-cache');
                res.setHeader('Connection', 'keep-alive');
                res.setHeader('Access-Control-Allow-Origin', '*');
                // 注意:Headers 只能发送一次

                let previousOutput = '';
                const falStream = await fal.stream("fal-ai/any-llm", { input: falInput });

                // 标记成功,设置下一次请求的起始 Key 索引
                success = true;
                currentFalKeyIndex = (keyIndexToTry + 1) % falKeys.length;
                console.log(`[${requestId}] Key at index ${keyIndexToTry} successful. Next request starts at index ${currentFalKeyIndex}.`);

                // 处理流
                try {
                     if (!res.headersSent) { // 确保 header 只发送一次
                         res.flushHeaders();
                         console.log(`[${requestId}] Stream headers flushed.`);
                     }
                    for await (const event of falStream) {
                        // ... (流处理逻辑基本不变,添加 requestId 用于日志) ...
                        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) {
                            console.error(`[${requestId}] Error received in fal stream event:`, errorInfo);
                            const errorChunk = { id: `chatcmpl-${Date.now()}-error`, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: model, choices: [{ index: 0, delta: {}, finish_reason: "error", message: { role: 'assistant', content: `Fal Stream Error: ${JSON.stringify(errorInfo)}` } }] };
                            if (!res.writableEnded) {
                                res.write(`data: ${JSON.stringify(errorChunk)}\n\n`);
                            }
                            break; // Stop processing on error in stream event
                        }

                        let deltaContent = '';
                        if (currentOutput.startsWith(previousOutput)) {
                            deltaContent = currentOutput.substring(previousOutput.length);
                        } else if (currentOutput.length > 0) {
                            console.warn(`[${requestId}] Fal stream output mismatch detected. Sending full current output as delta.`, { previousLength: previousOutput.length, currentLength: currentOutput.length });
                            deltaContent = currentOutput;
                            previousOutput = ''; // Reset previous if mismatch
                        }
                        previousOutput = currentOutput;

                        // Send chunk if there's content or if it's the final chunk (isPartial is false)
                        if (deltaContent || !isPartial) {
                            const openAIChunk = { id: `chatcmpl-${Date.now()}`, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: model, choices: [{ index: 0, delta: { content: deltaContent }, finish_reason: isPartial === false ? "stop" : null }] };
                             if (!res.writableEnded) {
                                res.write(`data: ${JSON.stringify(openAIChunk)}\n\n`);
                             }
                        }
                     }
                     // After the loop, ensure the [DONE] signal is sent if the stream finished normally
                     if (!res.writableEnded) {
                        res.write(`data: [DONE]\n\n`);
                        res.end();
                        console.log(`[${requestId}] Stream finished and [DONE] sent.`);
                     }
                } catch (streamError) {
                    console.error(`[${requestId}] Error during fal stream processing loop:`, streamError);
                    lastError = streamError; // Store error from stream processing
                    try {
                        // Don't mark success=false here, the key worked but the stream itself failed.
                        // The outer loop should break because the response has likely been ended.
                        if (!res.writableEnded) { // Check if we can still write to the response
                            const errorDetails = (streamError instanceof Error) ? streamError.message : JSON.stringify(streamError);
                            res.write(`data: ${JSON.stringify({ error: { message: "Stream processing error", type: "proxy_error", details: errorDetails } })}\n\n`);
                            res.write(`data: [DONE]\n\n`); // Send DONE even after error
                            res.end();
                        } else {
                             console.error(`[${requestId}] Stream already ended, cannot send error message.`);
                        }
                    } catch (finalError) {
                         console.error(`[${requestId}] Error sending stream error message to client:`, finalError);
                        if (!res.writableEnded) { res.end(); }
                    }
                     // Break the outer key retry loop as the stream failed mid-way
                     break;
                }
                // 如果流成功处理完,直接跳出重试循环
                break; // Exit the key retry loop

            } else {
                // --- 非流式处理 ---
                console.log(`[${requestId}] Executing non-stream request with key index ${keyIndexToTry}...`);
                const result = await fal.subscribe("fal-ai/any-llm", { input: falInput, logs: true });

                // 检查 Fal AI 返回的业务错误 (例如输入无效),这种错误不应该因为换 Key 而解决
                if (result && result.error) {
                     console.error(`[${requestId}] Fal-ai returned a business error with key index ${keyIndexToTry}:`, result.error);
                     // 将此视为最终错误,不重试其他 key
                     lastError = new Error(`Fal-ai error: ${JSON.stringify(result.error)}`);
                     lastError.status = 500; // Or map from Fal error if possible, default 500
                     lastError.type = "fal_ai_error";
                     break; // Exit retry loop, no point trying other keys for bad input
                }

                console.log(`[${requestId}] Received non-stream result from fal-ai with key index ${keyIndexToTry}`);
                // console.log("Full non-stream result:", JSON.stringify(result, null, 2)); // Uncomment for detailed logs

                // 标记成功,设置下一次请求的起始 Key 索引
                success = true;
                currentFalKeyIndex = (keyIndexToTry + 1) % falKeys.length;
                console.log(`[${requestId}] Key at index ${keyIndexToTry} successful. Next request starts at index ${currentFalKeyIndex}.`);


                const openAIResponse = {
                    id: `chatcmpl-${result.requestId || Date.now()}`, object: "chat.completion", created: Math.floor(Date.now() / 1000), model: model,
                    choices: [{ index: 0, message: { role: "assistant", content: result.output || "" }, finish_reason: "stop" }],
                    usage: { prompt_tokens: null, completion_tokens: null, total_tokens: null }, // Fal doesn't provide token usage
                    system_fingerprint: null, // Fal doesn't provide system fingerprint
                    ...(result.reasoning && { fal_reasoning: result.reasoning }), // Include reasoning if present
                };
                res.json(openAIResponse);
                console.log(`[${requestId}] Returned non-stream response.`);
                break; // 成功,跳出重试循环
            }

        } catch (error) {
            lastError = error; // Store the error from this attempt
            const status = error?.status; // Fal client errors should have status
            const errorMessage = error?.body?.detail || error?.message || 'Unknown error'; // Get detailed message if possible

            console.warn(`[${requestId}] Attempt ${attempt + 1} with key index ${keyIndexToTry} failed. Status: ${status || 'N/A'}, Message: ${errorMessage}`);

            // 检查是否是与 Key 相关的错误 (401 Unauthorized, 403 Forbidden, 429 Rate Limit)
            if (status === 401 || status === 403 || status === 429) {
                console.log(`[${requestId}] Key-related error (${status}). Trying next key...`);
                // 继续循环尝试下一个 Key
            } else {
                 // 如果是其他类型的错误 (如网络问题、Fal内部服务器错误5xx、请求参数错误400等)
                 // 通常重试其他 Key 没有意义,直接中断重试
                 console.error(`[${requestId}] Unrecoverable error encountered. Status: ${status || 'N/A'}. Stopping key rotation for this request.`);
                 console.error("Error details:", error); // Log the full error object for debugging
                 break; // 跳出重试循环
            }
        }
    } // --- 结束重试循环 ---

    // 如果循环结束了还没有成功 (所有 Key 都失败了或遇到不可恢复错误)
    if (!success) {
        console.error(`[${requestId}] All Fal Key attempts failed or an unrecoverable error occurred.`);
        if (!res.headersSent) {
            const statusCode = lastError?.status || 503; // Use status from last error if available, default to 503 Service Unavailable
            const errorMessage = (lastError instanceof Error) ? lastError.message : JSON.stringify(lastError);
            // Try to extract a more specific message if available
            const detailMessage = lastError?.body?.detail || errorMessage;
            const errorType = lastError?.type || (statusCode === 401 || statusCode === 403 || statusCode === 429 ? "key_error" : "proxy_error");

             console.error(`[${requestId}] Sending final error response. Status: ${statusCode}, Type: ${errorType}, Message: ${detailMessage}`);

             // 返回一个标准的 OpenAI 错误格式
             res.status(statusCode).json({
                 object: "error",
                 message: `All Fal Key attempts failed or an unrecoverable error occurred. Last error: ${detailMessage}`,
                 type: errorType,
                 param: null,
                 code: statusCode === 429 ? "rate_limit_exceeded" : (statusCode === 401 || statusCode === 403 ? "invalid_api_key" : null)
             });
        } else if (!res.writableEnded) {
            console.error(`[${requestId}] Headers already sent, but request failed after stream started or during processing. Ending response with error chunk.`);
            // 尝试在流式响应中发送错误(如果可能)
            try {
                const errorDetails = (lastError instanceof Error) ? lastError.message : JSON.stringify(lastError);
                const detailMessage = lastError?.body?.detail || errorDetails;
                const errorChunk = { id: `chatcmpl-${Date.now()}-final-error`, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: model, choices: [{ index: 0, delta: {}, finish_reason: "error", message: { role: 'assistant', content: `Proxy Error: All key attempts failed or stream processing error. Last error: ${detailMessage}` } }] };
                res.write(`data: ${JSON.stringify(errorChunk)}\n\n`);
                res.write(`data: [DONE]\n\n`);
                res.end();
            } catch (e) {
                console.error(`[${requestId}] Failed to write final error to stream:`, e);
                if (!res.writableEnded) res.end(); // Force end if possible
            }
        } else {
            console.error(`[${requestId}] Request failed, but response stream was already fully ended. Cannot send error.`);
        }
    }

});

// 启动服务器 (更新启动信息)
app.listen(PORT, () => {
    console.log(`===================================================`);
    console.log(` Fal OpenAI Proxy Server (Key Rotation with Retry + System Top + Separator + Recency)`); // 更新描述
    console.log(` Listening on port: ${PORT}`);
    console.log(` Loaded ${falKeys.length} Fal AI Keys for rotation.`);
    console.log(` Using Limits: System Prompt=${SYSTEM_PROMPT_LIMIT}, Prompt=${PROMPT_LIMIT}`);
    console.log(` API Key Auth Enabled: ${API_KEY ? 'Yes' : 'No'}`);
    console.log(` Chat Completions Endpoint: POST http://localhost:${PORT}/v1/chat/completions`);
    console.log(` Models Endpoint: GET http://localhost:${PORT}/v1/models`);
    console.log(`===================================================`);
});

// 根路径响应 (更新信息)
app.get('/', (req, res) => {
    res.send('Fal OpenAI Proxy (Key Rotation with Retry + System Top + Separator + Recency Strategy) is running.'); // 更新描述
});