chrome-ai-bridge 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/tools/chatgpt-web.js +34 -15
- package/package.json +1 -1
|
@@ -363,6 +363,18 @@ export const askChatGPTWeb = defineTool({
|
|
|
363
363
|
await new Promise(resolve => setTimeout(resolve, 200));
|
|
364
364
|
}
|
|
365
365
|
}
|
|
366
|
+
// Capture initial message counts BEFORE sending
|
|
367
|
+
// This is critical to detect if our message was actually sent
|
|
368
|
+
const initialCounts = await page.evaluate(() => {
|
|
369
|
+
const userMessages = document.querySelectorAll('[data-message-author-role="user"]');
|
|
370
|
+
const assistantMessages = document.querySelectorAll('[data-message-author-role="assistant"]');
|
|
371
|
+
return {
|
|
372
|
+
userCount: userMessages.length,
|
|
373
|
+
assistantCount: assistantMessages.length,
|
|
374
|
+
};
|
|
375
|
+
});
|
|
376
|
+
const initialUserMsgCount = initialCounts.userCount;
|
|
377
|
+
const initialAssistantMsgCount = initialCounts.assistantCount;
|
|
366
378
|
// Step 4: Send question with retry
|
|
367
379
|
response.appendResponseLine('質問を送信中...');
|
|
368
380
|
let questionSent = false;
|
|
@@ -403,11 +415,12 @@ export const askChatGPTWeb = defineTool({
|
|
|
403
415
|
response.appendResponseLine('❌ 送信ボタンが見つかりません');
|
|
404
416
|
return;
|
|
405
417
|
}
|
|
406
|
-
// Wait for message to actually be sent (user message
|
|
407
|
-
|
|
418
|
+
// Wait for message to actually be sent (user message count INCREASED)
|
|
419
|
+
// This ensures we detect our NEW message, not existing ones
|
|
420
|
+
await page.waitForFunction(initialCount => {
|
|
408
421
|
const messages = document.querySelectorAll('[data-message-author-role="user"]');
|
|
409
|
-
return messages.length >
|
|
410
|
-
}, { timeout: 10000 });
|
|
422
|
+
return messages.length > initialCount;
|
|
423
|
+
}, { timeout: 10000 }, initialUserMsgCount);
|
|
411
424
|
response.appendResponseLine('✅ 質問送信完了');
|
|
412
425
|
// Step 5: Monitor streaming with progress updates
|
|
413
426
|
response.appendResponseLine('ChatGPTの回答を待機中... (10秒ごとに進捗を表示)');
|
|
@@ -420,39 +433,45 @@ export const askChatGPTWeb = defineTool({
|
|
|
420
433
|
await new Promise(resolve => setTimeout(resolve, 500));
|
|
421
434
|
}
|
|
422
435
|
isFirstCheck = false;
|
|
423
|
-
const status = await page.evaluate(
|
|
436
|
+
const status = await page.evaluate(initialAssistantCount => {
|
|
424
437
|
// Streaming detection - check for stop button by data-testid
|
|
425
438
|
// When ChatGPT is generating, send-button becomes stop-button
|
|
426
439
|
const stopButton = document.querySelector('button[data-testid="stop-button"]');
|
|
427
440
|
const isStreaming = !!stopButton;
|
|
428
441
|
if (!isStreaming) {
|
|
429
|
-
// Get final response
|
|
442
|
+
// Get final response - only look at NEW messages
|
|
430
443
|
const assistantMessages = document.querySelectorAll('[data-message-author-role="assistant"]');
|
|
431
|
-
if (
|
|
444
|
+
// Check if we have a NEW assistant message (not old ones)
|
|
445
|
+
if (assistantMessages.length <= initialAssistantCount) {
|
|
432
446
|
return { completed: false };
|
|
433
|
-
|
|
434
|
-
|
|
447
|
+
}
|
|
448
|
+
// Get the NEW message (first one after initial count)
|
|
449
|
+
const newMessage = assistantMessages[initialAssistantCount];
|
|
450
|
+
const thinkingButton = newMessage.querySelector('button[aria-label*="思考時間"]');
|
|
435
451
|
const thinkingTime = thinkingButton
|
|
436
452
|
? parseInt((thinkingButton.textContent || '').match(/\d+/)?.[0] || '0')
|
|
437
453
|
: undefined;
|
|
438
454
|
return {
|
|
439
455
|
completed: true,
|
|
440
|
-
text:
|
|
456
|
+
text: newMessage.textContent || '',
|
|
441
457
|
thinkingTime,
|
|
442
458
|
};
|
|
443
459
|
}
|
|
444
|
-
// Get current text
|
|
460
|
+
// Get current text from NEW message during streaming
|
|
445
461
|
const assistantMessages = document.querySelectorAll('[data-message-author-role="assistant"]');
|
|
446
|
-
|
|
447
|
-
const
|
|
448
|
-
?
|
|
462
|
+
// Only check new messages
|
|
463
|
+
const newMessage = assistantMessages.length > initialAssistantCount
|
|
464
|
+
? assistantMessages[initialAssistantCount]
|
|
465
|
+
: null;
|
|
466
|
+
const currentText = newMessage
|
|
467
|
+
? newMessage.textContent?.substring(0, 200)
|
|
449
468
|
: '';
|
|
450
469
|
return {
|
|
451
470
|
completed: false,
|
|
452
471
|
streaming: true,
|
|
453
472
|
currentText,
|
|
454
473
|
};
|
|
455
|
-
});
|
|
474
|
+
}, initialAssistantMsgCount);
|
|
456
475
|
if (status.completed) {
|
|
457
476
|
response.appendResponseLine(`\n✅ 回答完了 (所要時間: ${Math.floor((Date.now() - startTime) / 1000)}秒)`);
|
|
458
477
|
if (status.thinkingTime) {
|
package/package.json
CHANGED