Spaces:
Sleeping
Sleeping
Update plugins/gpt-4-turbo.js
Browse files- plugins/gpt-4-turbo.js +18 -15
plugins/gpt-4-turbo.js
CHANGED
|
@@ -2,6 +2,8 @@ const axios = require('axios');
|
|
| 2 |
const fs = require('fs');
|
| 3 |
const puppeteer = require('puppeteer');
|
| 4 |
|
|
|
|
|
|
|
| 5 |
async function gpt4Skywork(question, chatId = null, imagePath = null) {
|
| 6 |
const isNewChat = !chatId;
|
| 7 |
const targetUrl = isNewChat
|
|
@@ -22,29 +24,28 @@ async function gpt4Skywork(question, chatId = null, imagePath = null) {
|
|
| 22 |
try {
|
| 23 |
const page = await browser.newPage();
|
| 24 |
await page.goto(targetUrl, { waitUntil: 'networkidle2' });
|
| 25 |
-
await
|
| 26 |
|
| 27 |
if (imageBase64) {
|
| 28 |
const fileInput = await page.$('input[type="file"][accept="image/*"]');
|
| 29 |
const buffer = Buffer.from(imageBase64, 'base64');
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
await page.waitForTimeout(3000);
|
| 36 |
}
|
| 37 |
|
| 38 |
await page.waitForSelector('textarea.scrollbar-custom', { timeout: 10000 });
|
| 39 |
await page.type('textarea.scrollbar-custom', question);
|
| 40 |
-
await
|
| 41 |
|
| 42 |
await page.click('button[type=submit][aria-label="Send message"]');
|
| 43 |
|
| 44 |
if (isNewChat) {
|
| 45 |
-
await page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 });
|
| 46 |
}
|
| 47 |
-
await
|
| 48 |
|
| 49 |
let isGenerating = true;
|
| 50 |
let attempts = 0;
|
|
@@ -53,14 +54,15 @@ async function gpt4Skywork(question, chatId = null, imagePath = null) {
|
|
| 53 |
if (!stopBtn) {
|
| 54 |
isGenerating = false;
|
| 55 |
} else {
|
| 56 |
-
await
|
| 57 |
attempts++;
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
| 61 |
-
await
|
| 62 |
-
const
|
| 63 |
-
const
|
|
|
|
| 64 |
|
| 65 |
const currentUrl = page.url();
|
| 66 |
const chatIdMatch = currentUrl.match(/\/conversation\/([^/?]+)/);
|
|
@@ -97,7 +99,8 @@ const handler = async (req, res) => {
|
|
| 97 |
success: true,
|
| 98 |
data: {
|
| 99 |
msg: result.response,
|
| 100 |
-
chatId: result.chatId
|
|
|
|
| 101 |
}
|
| 102 |
});
|
| 103 |
|
|
|
|
| 2 |
const fs = require('fs');
|
| 3 |
const puppeteer = require('puppeteer');
|
| 4 |
|
| 5 |
+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
| 6 |
+
|
| 7 |
async function gpt4Skywork(question, chatId = null, imagePath = null) {
|
| 8 |
const isNewChat = !chatId;
|
| 9 |
const targetUrl = isNewChat
|
|
|
|
| 24 |
try {
|
| 25 |
const page = await browser.newPage();
|
| 26 |
await page.goto(targetUrl, { waitUntil: 'networkidle2' });
|
| 27 |
+
await delay(2000);
|
| 28 |
|
| 29 |
if (imageBase64) {
|
| 30 |
const fileInput = await page.$('input[type="file"][accept="image/*"]');
|
| 31 |
const buffer = Buffer.from(imageBase64, 'base64');
|
| 32 |
+
const tempPath = `/tmp/upload_${Date.now()}.png`;
|
| 33 |
+
fs.writeFileSync(tempPath, buffer);
|
| 34 |
+
await fileInput.uploadFile(tempPath);
|
| 35 |
+
fs.unlinkSync(tempPath);
|
| 36 |
+
await delay(3000);
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
await page.waitForSelector('textarea.scrollbar-custom', { timeout: 10000 });
|
| 40 |
await page.type('textarea.scrollbar-custom', question);
|
| 41 |
+
await delay(500);
|
| 42 |
|
| 43 |
await page.click('button[type=submit][aria-label="Send message"]');
|
| 44 |
|
| 45 |
if (isNewChat) {
|
| 46 |
+
await page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 }).catch(() => {});
|
| 47 |
}
|
| 48 |
+
await delay(3000);
|
| 49 |
|
| 50 |
let isGenerating = true;
|
| 51 |
let attempts = 0;
|
|
|
|
| 54 |
if (!stopBtn) {
|
| 55 |
isGenerating = false;
|
| 56 |
} else {
|
| 57 |
+
await delay(1000);
|
| 58 |
attempts++;
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
+
await delay(2000);
|
| 63 |
+
const responseDivs = await page.$$('div.relative.flex.min-w-\\[60px\\]');
|
| 64 |
+
const lastDiv = responseDivs[responseDivs.length - 1];
|
| 65 |
+
const responseText = await page.evaluate(el => el.innerText, lastDiv);
|
| 66 |
|
| 67 |
const currentUrl = page.url();
|
| 68 |
const chatIdMatch = currentUrl.match(/\/conversation\/([^/?]+)/);
|
|
|
|
| 99 |
success: true,
|
| 100 |
data: {
|
| 101 |
msg: result.response,
|
| 102 |
+
chatId: result.chatId,
|
| 103 |
+
url: result.url
|
| 104 |
}
|
| 105 |
});
|
| 106 |
|