mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 05:47:11 +00:00
fix: use only one browser instance with debbugger url endpoint
This commit is contained in:
parent
d0dcc50764
commit
4df0addffb
2 changed files with 29 additions and 22 deletions
|
|
@ -195,7 +195,6 @@ export class chatgpt extends plugin {
|
||||||
await this.chatGPTApi.init()
|
await this.chatGPTApi.init()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await this.reply('chatgpt初始化出错:' + e.msg, true)
|
await this.reply('chatgpt初始化出错:' + e.msg, true)
|
||||||
await this.chatGPTApi.close()
|
|
||||||
}
|
}
|
||||||
let previousConversation = await redis.get(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
|
let previousConversation = await redis.get(`CHATGPT:CONVERSATIONS:${e.sender.user_id}`)
|
||||||
let conversation = null
|
let conversation = null
|
||||||
|
|
@ -236,7 +235,6 @@ export class chatgpt extends plugin {
|
||||||
const blockWord = blockWords.split(',').find(word => response.toLowerCase().includes(word.toLowerCase()))
|
const blockWord = blockWords.split(',').find(word => response.toLowerCase().includes(word.toLowerCase()))
|
||||||
if (blockWord) {
|
if (blockWord) {
|
||||||
await this.reply('返回内容存在敏感词,我不想回答你', true)
|
await this.reply('返回内容存在敏感词,我不想回答你', true)
|
||||||
await this.chatGPTApi.close()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let userSetting = await redis.get(`CHATGPT:USER:${e.sender.user_id}`)
|
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()))
|
const blockWord = blockWords.split(',').find(word => responseAppend.toLowerCase().includes(word.toLowerCase()))
|
||||||
if (blockWord) {
|
if (blockWord) {
|
||||||
await this.reply('返回内容存在敏感词,我不想回答你', true)
|
await this.reply('返回内容存在敏感词,我不想回答你', true)
|
||||||
await this.chatGPTApi.close()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (responseAppend.indexOf('conversation') > -1 || responseAppend.startsWith("I'm sorry")) {
|
if (responseAppend.indexOf('conversation') > -1 || responseAppend.startsWith("I'm sorry")) {
|
||||||
|
|
@ -284,6 +281,5 @@ export class chatgpt extends plugin {
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
await this.reply(`与OpenAI通信异常,请稍后重试:${e}`, true, { recallMsg: e.isGroup ? 10 : 0 })
|
await this.reply(`与OpenAI通信异常,请稍后重试:${e}`, true, { recallMsg: e.isGroup ? 10 : 0 })
|
||||||
}
|
}
|
||||||
await this.chatGPTApi.close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,6 @@ import StealthPlugin from 'puppeteer-extra-plugin-stealth'
|
||||||
import { getOpenAIAuth } from './openai-auth.js'
|
import { getOpenAIAuth } from './openai-auth.js'
|
||||||
import delay from 'delay'
|
import delay from 'delay'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
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'
|
const chatUrl = 'https://chat.openai.com/chat'
|
||||||
let puppeteer = {}
|
let puppeteer = {}
|
||||||
|
|
||||||
|
|
@ -15,7 +12,8 @@ class Puppeteer {
|
||||||
let args = [
|
let args = [
|
||||||
'--exclude-switches',
|
'--exclude-switches',
|
||||||
'--no-sandbox',
|
'--no-sandbox',
|
||||||
'enable-automation'
|
'enable-automation',
|
||||||
|
'--remote-debugging-port=51777'
|
||||||
// '--shm-size=1gb'
|
// '--shm-size=1gb'
|
||||||
]
|
]
|
||||||
if (Config.proxy) {
|
if (Config.proxy) {
|
||||||
|
|
@ -63,7 +61,10 @@ class Puppeteer {
|
||||||
this.lock = true
|
this.lock = true
|
||||||
|
|
||||||
logger.mark('puppeteer Chromium 启动中...')
|
logger.mark('puppeteer Chromium 启动中...')
|
||||||
|
const browserURL = 'http://127.0.0.1:51777'
|
||||||
|
try {
|
||||||
|
this.browser = await puppeteer.connect({ browserURL })
|
||||||
|
} catch (e) {
|
||||||
/** 初始化puppeteer */
|
/** 初始化puppeteer */
|
||||||
this.browser = await puppeteer.launch(this.config).catch((err) => {
|
this.browser = await puppeteer.launch(this.config).catch((err) => {
|
||||||
logger.error(err.toString())
|
logger.error(err.toString())
|
||||||
|
|
@ -71,7 +72,7 @@ class Puppeteer {
|
||||||
logger.error('没有正确安装Chromium,可以尝试执行安装命令:node ./node_modules/puppeteer/install.js')
|
logger.error('没有正确安装Chromium,可以尝试执行安装命令:node ./node_modules/puppeteer/install.js')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
this.lock = false
|
this.lock = false
|
||||||
|
|
||||||
if (!this.browser) {
|
if (!this.browser) {
|
||||||
|
|
@ -126,7 +127,7 @@ export class ChatGPTPuppeteer extends Puppeteer {
|
||||||
|
|
||||||
async init () {
|
async init () {
|
||||||
if (this.inited) {
|
if (this.inited) {
|
||||||
return
|
return true
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// this.browser = await getBrowser({
|
// this.browser = await getBrowser({
|
||||||
|
|
@ -147,8 +148,14 @@ export class ChatGPTPuppeteer extends Puppeteer {
|
||||||
await this._page.goto(chatUrl, {
|
await this._page.goto(chatUrl, {
|
||||||
waitUntil: 'networkidle2'
|
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()) {
|
if (!await this.getIsAuthenticated()) {
|
||||||
console.log('需要登录,准备进行自动化登录')
|
logger.info('需要登录,准备进行自动化登录')
|
||||||
await getOpenAIAuth({
|
await getOpenAIAuth({
|
||||||
email: this._email,
|
email: this._email,
|
||||||
password: this._password,
|
password: this._password,
|
||||||
|
|
@ -156,7 +163,9 @@ export class ChatGPTPuppeteer extends Puppeteer {
|
||||||
page: this._page,
|
page: this._page,
|
||||||
isGoogleLogin: this._isGoogleLogin
|
isGoogleLogin: this._isGoogleLogin
|
||||||
})
|
})
|
||||||
console.log('登陆完成')
|
logger.info('登陆完成')
|
||||||
|
} else {
|
||||||
|
logger.info('无需登录')
|
||||||
}
|
}
|
||||||
this.inited = true
|
this.inited = true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
@ -524,7 +533,9 @@ export class ChatGPTPuppeteer extends Puppeteer {
|
||||||
}
|
}
|
||||||
|
|
||||||
async close () {
|
async close () {
|
||||||
|
if (this.browser) {
|
||||||
await this.browser.close()
|
await this.browser.close()
|
||||||
|
}
|
||||||
this._page = null
|
this._page = null
|
||||||
this.browser = null
|
this.browser = null
|
||||||
}
|
}
|
||||||
|
|
@ -533,7 +544,7 @@ export class ChatGPTPuppeteer extends Puppeteer {
|
||||||
|
|
||||||
async _getInputBox () {
|
async _getInputBox () {
|
||||||
// [data-id="root"]
|
// [data-id="root"]
|
||||||
return this._page.$('textarea')
|
return this._page?.$('textarea')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue