forked from CJackHwang/AIstudioProxyAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cjs
More file actions
1189 lines (1042 loc) · 57.1 KB
/
Copy pathserver.cjs
File metadata and controls
1189 lines (1042 loc) · 57.1 KB
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// server.cjs (优化版 v2.17 - 增加日志ID & 常量)
const express = require('express');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
// --- 依赖检查 ---
let playwright, expect;
const requiredModules = ['express', 'playwright', '@playwright/test', 'cors'];
const missingModules = [];
for (const modName of requiredModules) {
try {
if (modName === 'playwright') {
playwright = require(modName);
} else if (modName === '@playwright/test') {
expect = require(modName).expect;
} else {
require(modName);
}
// console.log(`✅ 模块 ${modName} 已加载。`); // Optional: Log success
} catch (e) {
console.error(`❌ 模块 ${modName} 未找到。`);
missingModules.push(modName);
}
}
if (missingModules.length > 0) {
console.error("-------------------------------------------------------------");
console.error("❌ 错误:缺少必要的依赖模块!");
console.error("请根据您使用的包管理器运行以下命令安装依赖:");
console.error("-------------------------------------------------------------");
console.error(` npm install ${missingModules.join(' ')}`);
console.error(" 或");
console.error(` yarn add ${missingModules.join(' ')}`);
console.error(" 或");
console.error(` pnpm install ${missingModules.join(' ')}`);
console.error("-------------------------------------------------------------");
process.exit(1);
}
// --- 配置 ---
const SERVER_PORT = process.env.PORT || 2048;
const CHROME_DEBUGGING_PORT = 8848;
const CDP_ADDRESS = `http://127.0.0.1:${CHROME_DEBUGGING_PORT}`;
const AI_STUDIO_URL_PATTERN = 'aistudio.google.com/';
const RESPONSE_COMPLETION_TIMEOUT = 300000; // 5分钟总超时
const POLLING_INTERVAL = 500; // 非流式/通用检查间隔
const POLLING_INTERVAL_STREAM = 200; // 流式检查轮询间隔 (ms)
// v2.12: Timeout for secondary checks *after* spinner disappears
const POST_SPINNER_CHECK_DELAY_MS = 500; // Spinner消失后稍作等待再检查其他状态
const FINAL_STATE_CHECK_TIMEOUT_MS = 1500; // 检查按钮和输入框最终状态的超时
const SPINNER_CHECK_TIMEOUT_MS = 1000; // 检查Spinner状态的超时
const POST_COMPLETION_BUFFER = 1000; // JSON模式下可以缩短检查后等待时间
const SILENCE_TIMEOUT_MS = 1500; // 文本静默多久后认为稳定 (Spinner消失后)
// --- 常量 ---
const MODEL_NAME = 'google-ai-studio-via-playwright-cdp-json';
const CHAT_COMPLETION_ID_PREFIX = 'chatcmpl-';
// --- 选择器常量 ---
const INPUT_SELECTOR = 'ms-prompt-input-wrapper textarea';
const SUBMIT_BUTTON_SELECTOR = 'button[aria-label="Run"]';
const RESPONSE_CONTAINER_SELECTOR = 'ms-chat-turn .chat-turn-container.model';
const RESPONSE_TEXT_SELECTOR = 'ms-cmark-node.cmark-node'; // Target the container for raw text
const LOADING_SPINNER_SELECTOR = 'button[aria-label="Run"] svg .stoppable-spinner'; // Spinner circle
const ERROR_TOAST_SELECTOR = 'div.toast.warning, div.toast.error'; // 页面错误提示
// v2.16: JSON Structure Prompt (Restored for non-streaming)
const prepareAIStudioPrompt = (userPrompt, systemPrompt = null) => {
let fullPrompt = `
IMPORTANT: Your entire response MUST be a single JSON object. Do not include any text outside of this JSON object.
The JSON object must have a single key named "response".
Inside the value of the "response" key (which is a string), you MUST put the exact marker "<<<START_RESPONSE>>>"" at the very beginning of your actual answer. There should be NO text before this marker within the response string.
`;
if (systemPrompt && systemPrompt.trim() !== '') {
fullPrompt += `\nSystem Instruction: ${systemPrompt}\n`;
}
fullPrompt += `
Example 1:
User asks: "What is the capital of France?"
Your response MUST be:
{
"response": "<<<START_RESPONSE>>>The capital of France is Paris."
}
Example 2:
User asks: "Write a python function to add two numbers"
Your response MUST be:
{
"response": "<<<START_RESPONSE>>>\\\`\\\`\\\`python\\\\ndef add(a, b):\\\\n return a + b\\\\n\\\`\\\`\\\`"
}
Now, answer the following user prompt, ensuring your output strictly adheres to the JSON format AND the start marker requirement described above:
User Prompt: "${userPrompt}"
Your JSON Response:
`;
return fullPrompt;
};
// v2.26: Use JSON prompt for streaming as well -> vNEXT: Use Markdown Code Block for streaming
// vNEXT: Instruct AI to output *incomplete* JSON for streaming -> vNEXT: Instruct AI to output Markdown Code Block
const prepareAIStudioPromptStream = (userPrompt, systemPrompt = null) => {
let fullPrompt = `
IMPORTANT: For this streaming request, your entire response MUST be enclosed in a single markdown code block (like \`\`\` block \`\`\`).
Inside this code block, your actual answer text MUST start immediately after the exact marker "<<<START_RESPONSE>>>".
Start your response exactly with "\`\`\`\n<<<START_RESPONSE>>>" followed by your answer content.
Continue outputting your answer content. You SHOULD include the final closing "\`\`\`" at the very end of your full response stream.
`;
if (systemPrompt && systemPrompt.trim() !== '') {
fullPrompt += `\nSystem Instruction: ${systemPrompt}\n`;
}
fullPrompt += `
Example 1 (Streaming):
User asks: "What is the capital of France?"
Your streamed response MUST look like this over time:
Stream part 1: \`\`\`\n<<<START_RESPONSE>>>The capital
Stream part 2: of France is
Stream part 3: Paris.\n\`\`\`
Example 2 (Streaming):
User asks: "Write a python function to add two numbers"
Your streamed response MUST look like this over time:
Stream part 1: \`\`\`\n<<<START_RESPONSE>>>\`\`\`python\ndef add(a, b):
Stream part 2: \n return a + b\n
Stream part 3: \`\`\`\n\`\`\`
Now, answer the following user prompt, ensuring your output strictly adheres to the markdown code block, start marker, and streaming requirements described above:
User Prompt: "${userPrompt}"
Your Response (Streaming, within a markdown code block):
`;
return fullPrompt;
};
const app = express();
// --- 全局变量 ---
let browser = null;
let page = null;
let isPlaywrightReady = false;
let isInitializing = false;
// v2.18: 请求队列和处理状态
let requestQueue = [];
let isProcessing = false;
// --- Playwright 初始化函数 ---
async function initializePlaywright() {
if (isPlaywrightReady || isInitializing) return;
isInitializing = true;
console.log(`--- 初始化 Playwright: 连接到 ${CDP_ADDRESS} ---`);
try {
browser = await playwright.chromium.connectOverCDP(CDP_ADDRESS, { timeout: 20000 });
console.log('✅ 成功连接到正在运行的 Chrome 实例!');
browser.once('disconnected', () => {
console.error('❌ Playwright 与 Chrome 的连接已断开!');
isPlaywrightReady = false;
browser = null;
page = null;
// v2.18: Clear queue on disconnect? Maybe not, let requests fail naturally.
});
await new Promise(resolve => setTimeout(resolve, 500));
const contexts = browser.contexts();
let context;
if (!contexts || contexts.length === 0) {
await new Promise(resolve => setTimeout(resolve, 1500));
const retryContexts = browser.contexts();
if (!retryContexts || retryContexts.length === 0) {
throw new Error('无法获取浏览器上下文。请检查 Chrome 是否已正确启动并响应。');
}
context = retryContexts[0];
} else {
context = contexts[0];
}
let foundPage = null;
const pages = context.pages();
console.log(`-> 发现 ${pages.length} 个页面。正在搜索 AI Studio (匹配 "${AI_STUDIO_URL_PATTERN}")...`);
for (const p of pages) {
try {
if (p.isClosed()) continue;
const url = p.url();
if (url.includes(AI_STUDIO_URL_PATTERN) && url.includes('/prompts/')) {
console.log(`-> 找到 AI Studio 页面: ${url}`);
foundPage = p;
break;
}
} catch (pageError) {
if (!p.isClosed()) {
console.warn(` 警告:评估页面 URL 时出错: ${pageError.message.split('\\n')[0]}`);
}
}
}
if (!foundPage) {
throw new Error(`未在已连接的 Chrome 中找到包含 "${AI_STUDIO_URL_PATTERN}" 和 "/prompts/" 的页面。请确保 auto_connect_aistudio.js 已成功运行,并且 AI Studio 页面 (例如 prompts/new_chat) 已打开。`);
}
page = foundPage;
console.log('-> 已定位到 AI Studio 页面。');
await page.bringToFront();
console.log('-> 尝试将页面置于前台。检查加载状态...');
await page.waitForLoadState('domcontentloaded', { timeout: 15000 });
console.log('-> 页面 DOM 已加载。');
try {
console.log("-> 尝试定位核心输入区域以确认页面就绪...");
await page.locator('ms-prompt-input-wrapper').waitFor({ state: 'visible', timeout: 15000 });
console.log("-> 核心输入区域容器已找到。");
} catch(initCheckError) {
console.warn(`⚠️ 初始化检查警告:未能快速定位到核心输入区域容器。页面可能仍在加载或结构有变: ${initCheckError.message.split('\\n')[0]}`);
await saveErrorSnapshot('init_check_fail');
}
isPlaywrightReady = true;
console.log('✅ Playwright 已准备就绪。');
// v2.18: Start processing queue if playwright just became ready and queue has items
if (requestQueue.length > 0 && !isProcessing) {
console.log(`[Queue] Playwright 就绪,队列中有 ${requestQueue.length} 个请求,开始处理...`);
processQueue();
}
} catch (error) {
console.error(`❌ 初始化 Playwright 失败: ${error.message}`);
await saveErrorSnapshot('init_fail');
isPlaywrightReady = false;
browser = null;
page = null;
} finally {
isInitializing = false;
}
}
// --- 中间件 ---
app.use(cors());
app.use(express.json());
// --- Web UI Route ---
app.get('/', (req, res) => {
const htmlPath = path.join(__dirname, 'index.html');
if (fs.existsSync(htmlPath)) {
res.sendFile(htmlPath);
} else {
res.status(404).send('Error: index.html not found.');
}
});
// --- 健康检查 ---
app.get('/health', (req, res) => {
const isConnected = browser?.isConnected() ?? false;
const isPageValid = page && !page.isClosed();
const queueLength = requestQueue.length;
const status = {
status: 'Unknown',
message: '',
playwrightReady: isPlaywrightReady,
browserConnected: isConnected,
pageValid: isPageValid,
initializing: isInitializing,
processing: isProcessing,
queueLength: queueLength
};
if (isPlaywrightReady && isPageValid && isConnected) {
status.status = 'OK';
status.message = `Server running, Playwright connected, page valid. Currently ${isProcessing ? 'processing' : 'idle'} with ${queueLength} item(s) in queue.`;
res.status(200).json(status);
} else {
status.status = 'Error';
const reasons = [];
if (!isPlaywrightReady) reasons.push("Playwright not initialized or ready");
if (!isPageValid) reasons.push("Target page not found or closed");
if (!isConnected) reasons.push("Browser disconnected");
if (isInitializing) reasons.push("Playwright is currently initializing");
status.message = `Service Unavailable. Issues: ${reasons.join(', ')}. Currently ${isProcessing ? 'processing' : 'idle'} with ${queueLength} item(s) in queue.`;
res.status(503).json(status);
}
});
// --- 新增:API 辅助函数 ---
// 验证聊天请求
function validateChatRequest(messages) {
if (!messages || !Array.isArray(messages) || messages.length === 0) {
throw new Error('Invalid request: "messages" array is missing or empty.');
}
const lastUserMessage = messages.filter(msg => msg.role === 'user').pop();
if (!lastUserMessage || !lastUserMessage.content) {
throw new Error('Invalid request: No valid user message content found in the "messages" array.');
}
return {
userPrompt: lastUserMessage.content,
systemPrompt: messages.find(msg => msg.role === 'system')?.content
};
}
// 与页面交互并提交 Prompt
async function interactAndSubmitPrompt(page, prompt, reqId) {
console.log(`[${reqId}] 开始页面交互...`);
const inputField = page.locator(INPUT_SELECTOR);
const submitButton = page.locator(SUBMIT_BUTTON_SELECTOR);
const loadingSpinner = page.locator(LOADING_SPINNER_SELECTOR); // Keep spinner locator here for later use
console.log(`[${reqId}] - 等待输入框可用...`);
try {
await inputField.waitFor({ state: 'visible', timeout: 10000 });
} catch (e) {
console.error(`[${reqId}] ❌ 查找输入框失败!`);
await saveErrorSnapshot(`input_field_not_visible_${reqId}`);
throw new Error(`[${reqId}] Failed to find visible input field. Error: ${e.message}`);
}
console.log(`[${reqId}] - 清空并填充输入框...`);
await inputField.fill(prompt, { timeout: 10000 });
console.log(`[${reqId}] - 等待运行按钮可用...`);
try {
await expect(submitButton).toBeEnabled({ timeout: 10000 });
} catch (e) {
console.error(`[${reqId}] ❌ 等待运行按钮变为可用状态超时!`);
await saveErrorSnapshot(`submit_button_not_enabled_before_click_${reqId}`);
throw new Error(`[${reqId}] Submit button not enabled before click. Error: ${e.message}`);
}
console.log(`[${reqId}] - 点击运行按钮...`);
await submitButton.click({ timeout: 10000 });
return { inputField, submitButton, loadingSpinner }; // Return locators
}
// 定位最新的回复元素
async function locateResponseElements(page, { inputField, submitButton, loadingSpinner }, reqId) {
console.log(`[${reqId}] 定位 AI 回复元素...`);
let lastResponseContainer;
let responseElement;
let locatedResponseElements = false;
for (let i = 0; i < 3 && !locatedResponseElements; i++) {
try {
console.log(`[${reqId}] 尝试定位最新回复容器及文本元素 (第 ${i + 1} 次)`);
await page.waitForTimeout(500 + i * 500); // 固有延迟
const isEndState = await checkEndConditionQuickly(page, loadingSpinner, inputField, submitButton, 250, reqId);
const locateTimeout = isEndState ? 3000 : 60000;
if (isEndState) {
console.log(`[${reqId}] -> 检测到结束条件已满足,使用 ${locateTimeout / 1000}s 超时进行定位。`);
}
lastResponseContainer = page.locator(RESPONSE_CONTAINER_SELECTOR).last();
await lastResponseContainer.waitFor({ state: 'attached', timeout: locateTimeout });
responseElement = lastResponseContainer.locator(RESPONSE_TEXT_SELECTOR);
await responseElement.waitFor({ state: 'attached', timeout: locateTimeout });
console.log(`[${reqId}] 回复容器和文本元素定位成功。`);
locatedResponseElements = true;
} catch (locateError) {
console.warn(`[${reqId}] 第 ${i + 1} 次定位回复元素失败: ${locateError.message.split('\n')[0]}`);
if (i === 2) {
await saveErrorSnapshot(`response_locate_fail_${reqId}`);
throw new Error(`[${reqId}] Failed to locate response elements after multiple attempts.`);
}
}
}
if (!locatedResponseElements) throw new Error(`[${reqId}] Could not locate response elements.`);
return { responseElement, lastResponseContainer }; // Return located elements
}
// --- 新增:处理流式响应 (vNEXT: 标记优先,静默结束,无JSON处理) ---
async function handleStreamingResponse(res, responseElement, page, { inputField, submitButton, loadingSpinner }, operationTimer, reqId) {
console.log(`[${reqId}] - 流式传输开始 (vNEXT: Marker priority, silence end, no JSON handling)...`); // TODO: Update version
let lastRawText = "";
let lastSentResponseContent = ""; // Tracks content *after* the marker that has been SENT
let responseStarted = false; // Tracks if <<<START_RESPONSE>>> has been seen
const startTime = Date.now();
let spinnerHasDisappeared = false;
let lastTextChangeTimestamp = Date.now();
const startMarker = '<<<START_RESPONSE>>>';
let streamFinishedNaturally = false;
while (Date.now() - startTime < RESPONSE_COMPLETION_TIMEOUT && !streamFinishedNaturally) {
const loopStartTime = Date.now();
// 1. Get current raw text
const currentRawText = await getRawTextContent(responseElement, lastRawText, reqId);
if (currentRawText !== lastRawText) {
lastTextChangeTimestamp = Date.now();
let potentialNewDelta = "";
let currentContentAfterMarker = "";
// 2. Marker Check & Delta Calculation
const markerIndex = currentRawText.indexOf(startMarker);
if (markerIndex !== -1) {
if (!responseStarted) {
console.log(`[${reqId}] (流式 Simple) 检测到 ${startMarker},开始传输...`);
responseStarted = true;
}
// Content after marker in the current raw text
currentContentAfterMarker = currentRawText.substring(markerIndex + startMarker.length);
// Calculate new content since last *sent* content
potentialNewDelta = currentContentAfterMarker.substring(lastSentResponseContent.length);
} else if(responseStarted) {
// If marker was seen before, but now disappears (e.g., AI cleared output?), treat as no new delta.
potentialNewDelta = "";
console.warn(`[${reqId}] Marker disappeared after being seen. Raw: ${currentRawText.substring(0,100)}`);
}
// 3. Send Delta if found
if (potentialNewDelta) {
// console.log(`[${reqId}] (Send Stream Simple) Sending Delta (len: ${potentialNewDelta.length})`);
sendStreamChunk(res, potentialNewDelta, reqId);
lastSentResponseContent += potentialNewDelta; // Update tracking
}
// Update last raw text
lastRawText = currentRawText;
} // End if(currentRawText !== lastRawText)
// 4. Check Spinner status
if (!spinnerHasDisappeared) {
try {
await expect(loadingSpinner).toBeHidden({ timeout: 50 });
spinnerHasDisappeared = true;
lastTextChangeTimestamp = Date.now(); // Reset silence timer when spinner disappears
console.log(`[${reqId}] Spinner 已消失,进入静默期检测...`);
} catch (e) { /* Spinner still visible */ }
}
// 5. Silence Check (Standard)
const isSilent = spinnerHasDisappeared && (Date.now() - lastTextChangeTimestamp > SILENCE_TIMEOUT_MS);
if (isSilent) {
console.log(`[${reqId}] Silence detected. Finishing stream.`);
streamFinishedNaturally = true;
break; // Exit loop
}
// 6. Control polling interval
const loopEndTime = Date.now();
const loopDuration = loopEndTime - loopStartTime;
const waitTime = Math.max(0, POLLING_INTERVAL_STREAM - loopDuration);
await page.waitForTimeout(waitTime);
} // --- End main loop ---
// --- Cleanup and End ---
clearTimeout(operationTimer); // Clear the specific timer for THIS request
if (!streamFinishedNaturally && Date.now() - startTime >= RESPONSE_COMPLETION_TIMEOUT) {
// Timeout case
console.warn(`[${reqId}] - 流式传输(Simple模式)因总超时 (${RESPONSE_COMPLETION_TIMEOUT / 1000}s) 结束。`);
await saveErrorSnapshot(`streaming_simple_timeout_${reqId}`);
if (!res.writableEnded) {
sendStreamError(res, "Stream processing timed out on server (Simple mode).", reqId);
}
} else if (streamFinishedNaturally && !res.writableEnded) {
// Natural end (Silence detected)
// --- Final Sync (Simple Mode) ---
// Check one last time for any content received after the last delta was sent but before silence was declared.
console.log(`[${reqId}] (Simple Stream) Loop ended naturally, performing final sync check...`);
const finalRawText = await getRawTextContent(responseElement, lastRawText, reqId);
console.log(`[${reqId}] (Simple Stream) Performing final marker check and delta calculation...`);
try {
let finalExtractedContent = ""; // Content after marker
const finalMarkerIndex = finalRawText.indexOf(startMarker);
if (finalMarkerIndex !== -1) {
finalExtractedContent = finalRawText.substring(finalMarkerIndex + startMarker.length);
}
const finalDelta = finalExtractedContent.substring(lastSentResponseContent.length);
if (finalDelta){
console.log(`[${reqId}] (Final Sync Simple) Sending final delta (len: ${finalDelta.length})`);
sendStreamChunk(res, finalDelta, reqId);
} else {
console.log(`[${reqId}] (Final Sync Simple) No final delta to send based on lastSent comparison.`);
}
} catch (e) { console.warn(`[${reqId}] (Simple Stream) Final sync error during marker/delta calc: ${e.message}`); }
// --- End Final Sync ---
res.write('data: [DONE]\n\n');
res.end();
console.log(`[${reqId}] ✅ 流式(Simple模式)响应 [DONE] 已发送。`);
} else if (res.writableEnded) {
console.log(`[${reqId}] 流(Simple模式)已提前结束 (writableEnded=true),不再发送 [DONE]。`);
} else {
console.log(`[${reqId}] 流(Simple模式)结束时状态异常 (finishedNaturally=${streamFinishedNaturally}, writableEnded=${res.writableEnded}),不再发送 [DONE]。`);
}
}
// --- 新增:处理非流式响应 --- vNEXT: Restore JSON Parsing
async function handleNonStreamingResponse(res, page, locators, operationTimer, reqId) {
console.log(`[${reqId}] - 等待 AI 处理完成 (检查 Spinner 消失 + 输入框空 + 按钮禁用)...`);
let processComplete = false;
const nonStreamStartTime = Date.now();
let finalStateCheckInitiated = false;
const { inputField, submitButton, loadingSpinner } = locators;
// Completion check logic
while (!processComplete && Date.now() - nonStreamStartTime < RESPONSE_COMPLETION_TIMEOUT) {
let isSpinnerHidden = false;
let isInputEmpty = false;
let isButtonDisabled = false;
try {
await expect(loadingSpinner).toBeHidden({ timeout: SPINNER_CHECK_TIMEOUT_MS });
isSpinnerHidden = true;
} catch { /* Spinner still visible */ }
if (isSpinnerHidden) {
try {
await expect(inputField).toHaveValue('', { timeout: FINAL_STATE_CHECK_TIMEOUT_MS });
isInputEmpty = true;
} catch { /* Input not empty */ }
if (isInputEmpty) {
try {
await expect(submitButton).toBeDisabled({ timeout: FINAL_STATE_CHECK_TIMEOUT_MS });
isButtonDisabled = true;
} catch { /* Button not disabled */ }
}
}
if (isSpinnerHidden && isInputEmpty && isButtonDisabled) {
if (!finalStateCheckInitiated) {
finalStateCheckInitiated = true;
console.log(`[${reqId}] 检测到潜在最终状态。等待 ${POST_COMPLETION_BUFFER}ms 进行确认...`); // Use constant
await page.waitForTimeout(POST_COMPLETION_BUFFER); // Wait a bit first
console.log(`[${reqId}] ${POST_COMPLETION_BUFFER}ms 等待结束,重新检查状态...`);
try {
await expect(loadingSpinner).toBeHidden({ timeout: 500 });
await expect(inputField).toHaveValue('', { timeout: 500 });
await expect(submitButton).toBeDisabled({ timeout: 500 });
console.log(`[${reqId}] 状态确认成功。开始文本静默检查...`);
// --- NEW: Text Silence Check ---
let lastCheckText = '';
let currentCheckText = '';
let textStable = false;
const silenceCheckStartTime = Date.now();
// Re-locate response element here for the check
const { responseElement: checkResponseElement } = await locateResponseElements(page, locators, reqId);
while (Date.now() - silenceCheckStartTime < SILENCE_TIMEOUT_MS * 2) { // Check for up to 2*silence duration
lastCheckText = currentCheckText;
currentCheckText = await getRawTextContent(checkResponseElement, lastCheckText, reqId);
if (currentCheckText === lastCheckText) {
// Text hasn't changed since last check in this loop
if (Date.now() - silenceCheckStartTime >= SILENCE_TIMEOUT_MS) {
// And enough time has passed
console.log(`[${reqId}] 文本内容静默 ${SILENCE_TIMEOUT_MS}ms,确认处理完成。`);
textStable = true;
break;
}
} else {
// Text changed, reset silence timer within this check
// silenceCheckStartTime = Date.now(); // Option: Reset timer on any change
console.log(`[${reqId}] (静默检查) 文本仍在变化...`);
}
await page.waitForTimeout(POLLING_INTERVAL); // Use standard poll interval for checks
}
if (textStable) {
processComplete = true; // Mark process as complete
} else {
console.warn(`[${reqId}] 警告: 文本静默检查超时,可能仍在输出。将继续尝试解析。`);
processComplete = true; // Proceed anyway after timeout, but log warning
}
// --- END NEW: Text Silence Check ---
} catch (recheckError) {
console.log(`[${reqId}] 状态在确认期间发生变化 (${recheckError.message.split('\\n')[0]})。继续轮询...`);
finalStateCheckInitiated = false;
}
}
} else {
if (finalStateCheckInitiated) {
console.log(`[${reqId}] 最终状态不再满足,重置确认标志。`);
finalStateCheckInitiated = false;
}
await page.waitForTimeout(POLLING_INTERVAL * 2); // Longer wait if not in final state check
}
} // --- End Completion check logic loop ---
// Check for Page Errors BEFORE attempting to parse JSON
console.log(`[${reqId}] - 检查页面上是否存在错误提示...`);
const pageError = await detectAndExtractPageError(page, reqId);
if (pageError) {
console.error(`[${reqId}] ❌ 检测到 AI Studio 页面错误: ${pageError}`);
await saveErrorSnapshot(`page_error_detected_${reqId}`);
throw new Error(`[${reqId}] AI Studio Error: ${pageError}`);
}
if (!processComplete) {
console.warn(`[${reqId}] 警告:等待最终完成状态超时或未能稳定确认 (${(Date.now() - nonStreamStartTime) / 1000}s)。将直接尝试获取并解析JSON。`);
await saveErrorSnapshot(`nonstream_final_state_timeout_${reqId}`);
} else {
console.log(`[${reqId}] - 开始获取并解析最终 JSON...`);
}
// Get and Parse JSON
let aiResponseText = null;
const maxRetries = 3;
let attempts = 0;
while (attempts < maxRetries && aiResponseText === null) {
attempts++;
console.log(`[${reqId}] - 尝试获取原始文本并解析 JSON (第 ${attempts} 次)...`);
try {
// Re-locate response element within the retry loop for robustness
const { responseElement: currentResponseElement } = await locateResponseElements(page, locators, reqId);
const rawText = await getRawTextContent(currentResponseElement, '', reqId);
if (!rawText || rawText.trim() === '') {
console.warn(`[${reqId}] - 第 ${attempts} 次获取的原始文本为空。`);
throw new Error("Raw text content is empty.");
}
console.log(`[${reqId}] - 获取到原始文本 (长度: ${rawText.length}): \"${rawText.substring(0,100)}...\"`);
const parsedJson = tryParseJson(rawText, reqId);
if (parsedJson) {
if (typeof parsedJson.response === 'string') {
aiResponseText = parsedJson.response;
console.log(`[${reqId}] - 成功解析 JSON 并提取 'response' 字段。`);
} else {
// JSON 有效但无 response 字段
try {
aiResponseText = JSON.stringify(parsedJson);
console.log(`[${reqId}] - 警告: 未找到 'response' 字段,但解析到有效 JSON。将整个 JSON 字符串化作为回复。`);
} catch (stringifyError) {
console.error(`[${reqId}] - 错误:无法将解析出的 JSON 字符串化: ${stringifyError.message}`);
aiResponseText = null;
throw new Error("Failed to stringify the parsed JSON object.");
}
}
} else {
// JSON 解析失败
console.warn(`[${reqId}] - 第 ${attempts} 次未能解析 JSON。`);
aiResponseText = null;
if (attempts >= maxRetries) {
await saveErrorSnapshot(`json_parse_fail_final_attempt_${reqId}`);
}
throw new Error("Failed to parse JSON from raw text.");
}
break;
} catch (e) {
console.warn(`[${reqId}] - 第 ${attempts} 次获取或解析失败: ${e.message.split('\n')[0]}`);
aiResponseText = null;
if (attempts >= maxRetries) {
console.error(`[${reqId}] - 多次尝试获取并解析 JSON 失败。`);
if (!e.message?.includes('snapshot')) await saveErrorSnapshot(`get_parse_json_failed_final_${reqId}`);
aiResponseText = ""; // Fallback to empty string
} else {
await new Promise(resolve => setTimeout(resolve, 1500 + attempts * 500));
}
}
}
if (aiResponseText === null) {
console.log(`[${reqId}] - JSON 解析失败,再次检查页面错误...`);
const finalCheckError = await detectAndExtractPageError(page, reqId);
if (finalCheckError) {
console.error(`[${reqId}] ❌ 检测到 AI Studio 页面错误 (在 JSON 解析失败后): ${finalCheckError}`);
await saveErrorSnapshot(`page_error_post_json_fail_${reqId}`);
throw new Error(`[${reqId}] AI Studio Error after JSON parse failed: ${finalCheckError}`);
}
console.warn(`[${reqId}] 警告:所有尝试均未能获取并解析出有效的 JSON 回复。返回空回复。`);
aiResponseText = "";
}
// Handle potential nested JSON
let cleanedResponse = aiResponseText;
try {
// Attempt to parse the potential stringified JSON again for nested 'response' check
// Only attempt if aiResponseText is likely a stringified JSON object/array
if (aiResponseText && aiResponseText.startsWith('{') || aiResponseText.startsWith('[')) {
const outerParsed = JSON.parse(aiResponseText); // Use JSON.parse directly here
const innerParsed = tryParseJson(outerParsed.response, reqId); // Try parsing the inner 'response' field if it exists
if (innerParsed && typeof innerParsed.response === 'string') {
console.log(`[${reqId}] (非流式) 检测到嵌套 JSON,使用内层 response 内容。`);
cleanedResponse = innerParsed.response;
} else if (typeof outerParsed.response === 'string') {
// If the *outer* 'response' was already a string (not nested JSON), use it directly
console.log(`[${reqId}] (非流式) 使用外层 'response' 字段内容。`);
cleanedResponse = outerParsed.response;
}
// If neither inner nor outer 'response' fields are relevant strings, keep the stringified JSON as cleanedResponse
}
} catch (e) {
// If parsing aiResponseText fails, it means it wasn't a stringified JSON in the first place,
// or it was malformed. Keep the original aiResponseText.
// console.warn(`[${reqId}] (Info) Post-processing check: aiResponseText ('${aiResponseText.substring(0,50)}...') is not a parseable JSON or lacks 'response'. Keeping original value. Error: ${e.message}`);
cleanedResponse = aiResponseText; // Keep original if parsing fails
}
console.log(`[${reqId}] ✅ 获取到解析后的 AI 回复 (来自JSON, 长度: ${cleanedResponse?.length ?? 0}): \"${cleanedResponse?.substring(0, 100)}...\"`);
// --- 新增步骤:在非流式响应中移除标记 ---
const startMarker = '<<<START_RESPONSE>>>';
let finalContentForUser = cleanedResponse; // 默认使用清理后的响应
// Check for and remove the starting marker if present
if (finalContentForUser?.startsWith(startMarker)) {
finalContentForUser = finalContentForUser.substring(startMarker.length);
console.log(`[${reqId}] (非流式 JSON) 移除前缀 ${startMarker},最终内容长度: ${finalContentForUser.length}`);
} else if (aiResponseText !== null && aiResponseText !== "") { // 仅在获取到非空文本但无标记时警告
console.warn(`[${reqId}] (非流式 JSON) 警告: 未在 response 字段中找到预期的 ${startMarker} 前缀。内容: \"${aiResponseText.substring(0,50)}...\"`);
}
// --- 结束新增步骤 ---
// 使用移除标记后的内容构建最终响应
const responsePayload = {
id: `${CHAT_COMPLETION_ID_PREFIX}${Date.now()}-${Math.random().toString(36).substring(2, 15)}`,
object: 'chat.completion',
created: Math.floor(Date.now() / 1000),
model: MODEL_NAME,
choices: [{
index: 0,
message: { role: 'assistant', content: finalContentForUser }, // Use cleaned content
finish_reason: 'stop',
}],
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
};
console.log(`[${reqId}] ✅ 返回 JSON 响应 (来自解析后的JSON)。`);
clearTimeout(operationTimer); // Clear the specific timer for THIS request
res.json(responsePayload);
}
// --- 新增:处理 /v1/models 请求以满足 Open WebUI 验证 ---
app.get('/v1/models', (req, res) => {
const modelId = 'aistudio-proxy'; // 您计划在 Open WebUI 中使用的模型名称
// 使用简短的日志ID或时间戳
const logPrefix = `[${Date.now().toString(36).slice(-5)}]`;
console.log(`${logPrefix} --- 收到 /v1/models 请求,返回模拟模型列表 ---`);
res.json({
object: "list",
data: [
{
id: modelId, // 返回您要用的那个名字
object: "model",
created: Math.floor(Date.now() / 1000),
owned_by: "openai-proxy", // 可以随便写
permission: [],
root: modelId,
parent: null
}
// 如果需要添加更多名称指向同一个代理,可以在此添加
// ,{
// id: "gemini-pro-proxy",
// object: "model",
// created: Math.floor(Date.now() / 1000),
// owned_by: "openai-proxy",
// permission: [],
// root: "gemini-pro-proxy",
// parent: null
// }
]
});
});
// --- v2.18: 新增队列处理函数 ---
async function processQueue() {
if (isProcessing || requestQueue.length === 0) {
// console.log(`[Queue] Process check: Already processing (${isProcessing}) or queue empty (${requestQueue.length}). Exiting.`);
return; // 如果正在处理或队列为空,则退出
}
isProcessing = true;
const { req, res, reqId } = requestQueue.shift(); // 从队列头部取出一个请求
console.log(`\n[${reqId}] ---开始处理队列中的请求 (剩余 ${requestQueue.length} 个)---`);
let operationTimer; // Timer for this specific request
try {
// 1. 检查 Playwright 状态 (针对当前请求)
if (!isPlaywrightReady && !isInitializing) {
console.warn(`[${reqId}] Playwright 未就绪,尝试重新初始化...`);
await initializePlaywright();
}
if (!isPlaywrightReady || !page || page.isClosed() || !browser?.isConnected()) {
console.error(`[${reqId}] API 请求失败:Playwright 未就绪、页面关闭或连接断开。`);
let detail = 'Unknown issue.';
if (!browser?.isConnected()) detail = "Browser connection lost.";
else if (!page || page.isClosed()) detail = "Target AI Studio page is not available or closed.";
else if (!isPlaywrightReady) detail = "Playwright initialization failed or incomplete.";
console.error(`[${reqId}] Playwright 连接不可用详情: ${detail}`);
// 直接为当前请求返回错误,不需要抛出,因为要继续处理队列
if (!res.headersSent) {
res.status(503).json({
error: { message: `[${reqId}] Playwright connection is not active. ${detail} Please ensure Chrome is running correctly, the AI Studio tab is open, and potentially restart the server.`, type: 'server_error' }
});
}
throw new Error("Playwright not ready for this request."); // Throw to skip further processing in try block
}
const { messages, stream, ...otherParams } = req.body;
const isStreaming = stream === true;
console.log(`[${reqId}] 请求模式: ${isStreaming ? '流式 (SSE)' : '非流式 (JSON)'}`);
// 2. 设置此请求的总操作超时
operationTimer = setTimeout(async () => {
await saveErrorSnapshot(`operation_timeout_${reqId}`);
console.error(`[${reqId}] Operation timed out after ${RESPONSE_COMPLETION_TIMEOUT / 1000} seconds.`);
if (!res.headersSent) {
res.status(504).json({ error: { message: `[${reqId}] Operation timed out`, type: 'timeout_error' } });
} else if (isStreaming && !res.writableEnded) {
sendStreamError(res, "Operation timed out on server.", reqId);
}
// Note: Timeout error now managed within processQueue, allowing next item to proceed
}, RESPONSE_COMPLETION_TIMEOUT);
// 3. 验证请求
const { userPrompt, systemPrompt: extractedSystemPrompt } = validateChatRequest(messages);
const systemPrompt = extractedSystemPrompt || otherParams?.system_prompt; // Combine sources
console.log(`[${reqId}] 原始 User Prompt (start): \"${userPrompt?.substring(0, 80)}...\"`);
if (systemPrompt) {
console.log(`[${reqId}] System Prompt (start): \"${systemPrompt.substring(0, 80)}...\"`);
}
if (Object.keys(otherParams).length > 0) {
console.log(`[${reqId}] 记录到的额外参数: ${JSON.stringify(otherParams)}`);
}
// 4. 准备 Prompt
let prompt;
if (isStreaming) {
prompt = prepareAIStudioPromptStream(userPrompt, systemPrompt);
console.log(`[${reqId}] 构建的流式 Prompt (Raw): \"${prompt.substring(0, 200)}...\"`);
} else {
prompt = prepareAIStudioPrompt(userPrompt, systemPrompt);
console.log(`[${reqId}] 构建的非流式 Prompt (JSON): \"${prompt.substring(0, 200)}...\"`);
}
// 5. 与页面交互并提交
const locators = await interactAndSubmitPrompt(page, prompt, reqId);
// 6. 定位响应元素
const { responseElement } = await locateResponseElements(page, locators, reqId);
// 7. 处理响应 (流式或非流式)
console.log(`[${reqId}] 处理 AI 回复...`);
if (isStreaming) {
// --- 设置流式响应头 ---
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.flushHeaders();
// 调用流式处理函数
await handleStreamingResponse(res, responseElement, page, locators, operationTimer, reqId);
} else {
// 调用非流式处理函数
await handleNonStreamingResponse(res, page, locators, operationTimer, reqId);
}
console.log(`[${reqId}] ✅ 请求处理成功完成。`);
// Clear timeout only on successful completion within try block
clearTimeout(operationTimer);
} catch (error) {
clearTimeout(operationTimer); // 确保在任何错误情况下都清除此请求的定时器
console.error(`[${reqId}] ❌ 处理队列中的请求时出错: ${error.message}\n${error.stack}`);
if (!error.message?.includes('snapshot') && !error.stack?.includes('saveErrorSnapshot') && !error.message?.includes('Playwright not ready')) {
// 避免在保存快照失败或已知Playwright问题时再次尝试保存
await saveErrorSnapshot(`general_api_error_${reqId}`);
}
// 发送错误响应,如果尚未发送
if (!res.headersSent) {
let statusCode = 500;
let errorType = 'server_error';
if (error.message?.includes('timed out') || error.message?.includes('timeout')) {
statusCode = 504; // Gateway Timeout
errorType = 'timeout_error';
} else if (error.message?.includes('AI Studio Error')) {
statusCode = 502; // Bad Gateway (error from upstream)
errorType = 'upstream_error';
} else if (error.message?.includes('Invalid request')) {
statusCode = 400; // Bad Request
errorType = 'invalid_request_error';
} else if (error.message?.includes('Playwright not ready')) { // Specific handling for PW not ready here
statusCode = 503;
errorType = 'server_error';
}
res.status(statusCode).json({ error: { message: `[${reqId}] ${error.message}`, type: errorType } });
} else if (req.body.stream === true && !res.writableEnded) { // Check if it WAS a streaming request
// 如果是流式响应且头部已发送,则发送流式错误
sendStreamError(res, error.message, reqId);
}
else if (!res.writableEnded) {
// 对于非流式但已发送部分内容的罕见情况,或流式错误发送后的清理
res.end();
}
} finally {
isProcessing = false; // 标记处理已结束
console.log(`[${reqId}] ---结束处理队列中的请求---`);
// 触发处理下一个请求(如果队列中有)
processQueue();
}
}
// --- API 端点 (v2.18: 使用队列) ---
app.post('/v1/chat/completions', async (req, res) => {
const reqId = Math.random().toString(36).substring(2, 9); // 生成简短的请求 ID
console.log(`\n[${reqId}] === 收到 /v1/chat/completions 请求 ===`);
// 将请求加入队列
requestQueue.push({ req, res, reqId });
console.log(`[${reqId}] 请求已加入队列 (当前队列长度: ${requestQueue.length})`);
// 尝试处理队列 (如果当前未在处理)
if (!isProcessing) {
console.log(`[Queue] 触发队列处理 (收到新请求 ${reqId} 时处于空闲状态)`);
processQueue();
} else {
console.log(`[Queue] 当前正在处理其他请求,请求 ${reqId} 已排队等待。`);
}
});
// --- Helper: 获取当前文本 (v2.14 - 获取原始文本) -> vNEXT: Try innerText
async function getRawTextContent(responseElement, previousText, reqId) {
try {
await responseElement.waitFor({ state: 'attached', timeout: 1500 });
const preElement = responseElement.locator('pre').last();
let rawText = null;
try {
await preElement.waitFor({ state: 'attached', timeout: 500 });
// 尝试使用 innerText 获取渲染后的文本,可能更好地保留换行
rawText = await preElement.innerText({ timeout: 1000 });
} catch {
// 如果 pre 元素获取失败,回退到 responseElement 的 innerText
console.warn(`[${reqId}] (Warn) Failed to get innerText from <pre>, falling back to parent.`);
rawText = await responseElement.innerText({ timeout: 2000 });
}
// 移除 trim(),直接返回获取到的文本
return rawText !== null ? rawText : previousText;
} catch (e) {
console.warn(`[${reqId}] (Warn) getRawTextContent (innerText) failed: ${e.message.split('\n')[0]}. Returning previous.`);
return previousText;
}
}
// --- Helper: 发送流式块 ---
function sendStreamChunk(res, delta, reqId) {
if (delta && !res.writableEnded) {
const chunk = {
id: `${CHAT_COMPLETION_ID_PREFIX}${Date.now()}-${Math.random().toString(36).substring(2, 15)}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: MODEL_NAME,
choices: [{ index: 0, delta: { content: delta }, finish_reason: null }]
};
try {
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
} catch (writeError) {
console.error(`[${reqId}] Error writing stream chunk:`, writeError.message);
if (!res.writableEnded) res.end(); // End stream on write error
}
}
}
// --- Helper: 发送流式错误块 ---
function sendStreamError(res, errorMessage, reqId) {
if (!res.writableEnded) {
const errorPayload = { error: { message: `[${reqId}] Server error during streaming: ${errorMessage}`, type: 'server_error' } };
try {
// Avoid writing multiple DONE messages if error occurs after normal DONE
if (!res.writableEnded) res.write(`data: ${JSON.stringify(errorPayload)}\n\n`);
if (!res.writableEnded) res.write('data: [DONE]\n\n');
} catch (e) {
console.error(`[${reqId}] Error writing stream error chunk:`, e.message);
} finally {
if (!res.writableEnded) res.end(); // Ensure stream ends
}
}