fix: use only one browser instance with debbugger url endpoint

This commit is contained in:
葛胤池 2022-12-17 13:07:33 +08:00
parent d0dcc50764
commit 4df0addffb
2 changed files with 29 additions and 22 deletions

View file

@ -195,7 +195,6 @@ export class chatgpt extends plugin {
await this.chatGPTApi.init()
} catch (e) {
await this.reply('chatgpt初始化出错' + e.msg, true)
await this.chatGPTApi.close()
}
let previousConversation = await redis.get(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
let conversation = null
@ -236,7 +235,6 @@ export class chatgpt extends plugin {
const blockWord = blockWords.split(',').find(word => response.toLowerCase().includes(word.toLowerCase()))
if (blockWord) {
await this.reply('返回内容存在敏感词,我不想回答你', true)
await this.chatGPTApi.close()
return
}
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
@ -260,7 +258,6 @@ export class chatgpt extends plugin {
const blockWord = blockWords.split(',').find(word => responseAppend.toLowerCase().includes(word.toLowerCase()))
if (blockWord) {
await this.reply('返回内容存在敏感词,我不想回答你', true)
await this.chatGPTApi.close()
return
}
if (responseAppend.indexOf('conversation') > -1 || responseAppend.startsWith("I'm sorry")) {
@ -284,6 +281,5 @@ export class chatgpt extends plugin {
logger.error(e)
await this.reply(`与OpenAI通信异常请稍后重试${e}`, true, { recallMsg: e.isGroup ? 10 : 0 })
}
await this.chatGPTApi.close()
}
}

View file

@ -4,9 +4,6 @@ import StealthPlugin from 'puppeteer-extra-plugin-stealth'
import { getOpenAIAuth } from './openai-auth.js'
import delay from 'delay'
import { v4 as uuidv4 } from 'uuid'
import pTimeout, { TimeoutError } from 'p-timeout'
import { getBrowser } from 'chatgpt'
import { PuppeteerExtraPluginRecaptcha } from 'puppeteer-extra-plugin-recaptcha'
const chatUrl = 'https://chat.openai.com/chat'
let puppeteer = {}
@ -15,7 +12,8 @@ class Puppeteer {
let args = [
'--exclude-switches',
'--no-sandbox',
'enable-automation'
'enable-automation',
'--remote-debugging-port=51777'
// '--shm-size=1gb'
]
if (Config.proxy) {
@ -63,7 +61,10 @@ class Puppeteer {
this.lock = true
logger.mark('puppeteer Chromium 启动中...')
const browserURL = 'http://127.0.0.1:51777'
try {
this.browser = await puppeteer.connect({ browserURL })
} catch (e) {
/** 初始化puppeteer */
this.browser = await puppeteer.launch(this.config).catch((err) => {
logger.error(err.toString())
@ -71,7 +72,7 @@ class Puppeteer {
logger.error('没有正确安装Chromium可以尝试执行安装命令node ./node_modules/puppeteer/install.js')
}
})
}
this.lock = false
if (!this.browser) {
@ -126,7 +127,7 @@ export class ChatGPTPuppeteer extends Puppeteer {
async init () {
if (this.inited) {
return
return true
}
try {
// this.browser = await getBrowser({
@ -147,8 +148,14 @@ export class ChatGPTPuppeteer extends Puppeteer {
await this._page.goto(chatUrl, {
waitUntil: 'networkidle2'
})
while ((await this._page.title()).toLowerCase().indexOf('moment') > -1) {
// if meet captcha
// await this._page.solveRecaptchas()
await delay(300)
}
await delay(300)
if (!await this.getIsAuthenticated()) {
console.log('需要登录,准备进行自动化登录')
logger.info('需要登录,准备进行自动化登录')
await getOpenAIAuth({
email: this._email,
password: this._password,
@ -156,7 +163,9 @@ export class ChatGPTPuppeteer extends Puppeteer {
page: this._page,
isGoogleLogin: this._isGoogleLogin
})
console.log('登陆完成')
logger.info('登陆完成')
} else {
logger.info('无需登录')
}
this.inited = true
} catch (err) {
@ -524,7 +533,9 @@ export class ChatGPTPuppeteer extends Puppeteer {
}
async close () {
if (this.browser) {
await this.browser.close()
}
this._page = null
this.browser = null
}
@ -533,7 +544,7 @@ export class ChatGPTPuppeteer extends Puppeteer {
async _getInputBox () {
// [data-id="root"]
return this._page.$('textarea')
return this._page?.$('textarea')
}
}