fix: 清理代码

This commit is contained in:
ikechan8370 2024-01-01 20:01:01 +08:00
parent 35c2788463
commit 5e41205728
6 changed files with 951 additions and 979 deletions

File diff suppressed because it is too large Load diff

View file

@ -11,7 +11,6 @@
"@google/generative-ai": "^0.1.1",
"@slack/bolt": "^3.13.2",
"asn1.js": "^5.0.0",
"delay": "^6.0.0",
"diff": "^5.1.0",
"emoji-strip": "^1.0.1",
"eventsource": "^2.0.2",

View file

@ -8,10 +8,9 @@ import crypto from 'crypto'
import WebSocket from 'ws'
import { Config, pureSydneyInstruction } from './config.js'
import { formatDate, getMasterQQ, isCN, getUserData, limitString } from './common.js'
import delay from 'delay'
import moment from 'moment'
import { getProxy } from './proxy.js'
import Version from './version.js'
import common from '../../../lib/common/common.js'
if (!globalThis.fetch) {
globalThis.fetch = fetch
@ -87,9 +86,12 @@ export default class SydneyAIClient {
// 'x-forwarded-for': '1.1.1.1'
}
}
if (this.opts.cookies || this.opts.userToken) {
let initCk = 'SRCHD=AF=NOFORM; PPLState=1; SRCHHPGUSR=HV=' + new Date().getTime() + ';'
if (this.opts.userToken) {
// 疑似无需token了
fetchOptions.headers.cookie = this.opts.cookies || `_U=${this.opts.userToken}`
fetchOptions.headers.cookie = `${initCk} _U=${this.opts.userToken}`
} else {
fetchOptions.headers.cookie = initCk
}
if (this.opts.proxy) {
fetchOptions.agent = proxy(Config.proxy)
@ -105,7 +107,7 @@ export default class SydneyAIClient {
let text = await response.text()
let retry = 10
while (retry >= 0 && response.status === 200 && !text) {
await delay(400)
await common.sleep(400)
response = await fetch(`${this.opts.host}/turing/conversation/create?bundleVersion=1.1381.12`, fetchOptions)
text = await response.text()
retry--
@ -308,8 +310,7 @@ export default class SydneyAIClient {
const text = (pureSydney ? pureSydneyInstruction : (useCast?.bing || Config.sydney)).replaceAll(namePlaceholder, botName || defaultBotName) +
((Config.enableGroupContext && groupId) ? groupContextTip : '') +
((Config.enforceMaster && master) ? masterTip : '') +
(Config.sydneyMood ? moodTip : '') +
(Config.sydneySystemCode ? '' : '')
(Config.sydneyMood ? moodTip : '')
// logger.info(text)
if (pureSydney) {
previousMessages = invocationId === 0

View file

@ -4,6 +4,7 @@ import StealthPlugin from 'puppeteer-extra-plugin-stealth'
import { getOpenAIAuth } from './openai-auth.js'
import delay from 'delay'
import { v4 as uuidv4 } from 'uuid'
import common from '../../../lib/common/common.js'
const chatUrl = 'https://chat.openai.com/chat'
let puppeteer = {}
@ -181,7 +182,7 @@ export class ChatGPTPuppeteer extends Puppeteer {
if (Config['2captchaToken']) {
await this._page.solveRecaptchas()
}
await delay(300)
await common.sleep(300)
timeout = timeout - 300
}
} catch (e) {
@ -243,7 +244,7 @@ export class ChatGPTPuppeteer extends Puppeteer {
break
}
await delay(300)
await common.sleep(300)
} while (true)
if (!await this.getIsAuthenticated()) {
@ -405,7 +406,7 @@ export class ChatGPTPuppeteer extends Puppeteer {
if (isAuthenticated) {
while (!this._accessToken) {
// wait for async response hook result
await delay(300)
await common.sleep(300)
timeout = timeout - 300
if (timeout < 0) {
const error = new Error('Not signed in')
@ -493,7 +494,7 @@ export class ChatGPTPuppeteer extends Puppeteer {
// const responseP = new Promise<string>(async (resolve, reject) => {
// try {
// do {
// await delay(1000)
// await common.sleep(1000)
// // TODO: this logic needs some work because we can have repeat messages...
// const newLastMessage = await this.getLastMessage()

View file

@ -1,6 +1,6 @@
import { Config } from '../utils/config.js'
import delay from 'delay'
import random from 'random'
import common from '../../../lib/common/common.js'
let hasRecaptchaPlugin = !!Config['2captchaToken']
@ -58,7 +58,7 @@ export async function getOpenAIAuth (opt) {
await waitForConditionOrAtCapacity(page, () =>
page.waitForSelector('#__next .btn-primary', { timeout: timeoutMs / 3 })
)
await delay(500)
await common.sleep(500)
// click login button and wait for navigation to finish
do {
@ -69,7 +69,7 @@ export async function getOpenAIAuth (opt) {
}),
page.click('#__next .btn-primary')
])
await delay(1000)
await common.sleep(1000)
} while (page.url().endsWith('/auth/login'))
logger.mark('进入登录页面')
await checkForChatGPTAtCapacity(page)
@ -90,7 +90,7 @@ export async function getOpenAIAuth (opt) {
} else {
await page.waitForSelector('#username')
await page.type('#username', email, { delay: 20 })
await delay(100)
await common.sleep(100)
if (hasRecaptchaPlugin) {
// console.log('solveRecaptchas()')
@ -114,7 +114,7 @@ export async function getOpenAIAuth (opt) {
submitP()
])
} else {
await delay(2000)
await common.sleep(2000)
await checkForChatGPTAtCapacity(page)
}
@ -183,7 +183,7 @@ async function checkForChatGPTAtCapacity (page, opts = {}) {
timeout: timeoutMs
})
await delay(pollingIntervalMs)
await common.sleep(pollingIntervalMs)
}
} catch (err) {
// ignore errors likely due to navigation
@ -251,29 +251,29 @@ async function solveSimpleCaptchas (page) {
const verifyYouAreHuman = await page.$('text=Verify you are human')
if (verifyYouAreHuman) {
logger.mark('encounter cloudflare simple captcha "Verify you are human"')
await delay(2000)
await common.sleep(2000)
await verifyYouAreHuman.click({
delay: random.int(5, 25)
})
await delay(1000)
await common.sleep(1000)
}
const verifyYouAreHumanCN = await page.$('text=确认您是真人')
if (verifyYouAreHumanCN) {
logger.mark('encounter cloudflare simple captcha "确认您是真人"')
await delay(2000)
await common.sleep(2000)
await verifyYouAreHumanCN.click({
delay: random.int(5, 25)
})
await delay(1000)
await common.sleep(1000)
}
const cloudflareButton = await page.$('.hcaptcha-box')
if (cloudflareButton) {
await delay(2000)
await common.sleep(2000)
await cloudflareButton.click({
delay: random.int(5, 25)
})
await delay(1000)
await common.sleep(1000)
}
} catch (err) {
// ignore errors

View file

@ -1,7 +1,7 @@
import { Config } from '../config.js'
import slack from '@slack/bolt'
import delay from 'delay'
import { limitString } from '../common.js'
import common from '../../../../lib/common/common.js'
let proxy
if (Config.proxy) {
try {
@ -24,7 +24,7 @@ export class SlackClaudeClient {
if (Config.proxy) {
option.agent = proxy(Config.proxy)
}
option.logLevel = Config.debug ? 'debug': 'info'
option.logLevel = Config.debug ? 'debug' : 'info'
this.app = new slack.App(option)
} else {
throw new Error('未配置Slack信息')
@ -61,7 +61,7 @@ export class SlackClaudeClient {
channel: channel.id,
users: Config.slackClaudeUserId
})
await delay(1000)
await common.sleep(1000)
} else {
channel = channel[0]
}
@ -78,7 +78,7 @@ export class SlackClaudeClient {
let response = '_Typing…_'
let tryTimes = 0
// 发完先等3喵
await delay(3000)
await common.sleep(3000)
while (response.trim().endsWith('_Typing…_')) {
let replies = await this.app.client.conversations.replies({
token: this.config.slackUserToken,
@ -106,7 +106,7 @@ export class SlackClaudeClient {
}
}
}
await delay(2000)
await common.sleep(2000)
tryTimes++
if (tryTimes > 3 && response === '_Typing…_') {
// 过了6秒还没任何回复就重新发一下试试
@ -127,7 +127,7 @@ export class SlackClaudeClient {
let response = '_Typing…_'
let tryTimes = 0
// 发完先等3喵
await delay(3000)
await common.sleep(3000)
while (response.trim().endsWith('_Typing…_')) {
let replies = await this.app.client.conversations.replies({
token: this.config.slackUserToken,
@ -156,7 +156,7 @@ export class SlackClaudeClient {
}
}
}
await delay(2000)
await common.sleep(2000)
tryTimes++
if (tryTimes > 3 && response === '_Typing…_') {
// 过了6秒还没任何回复就重新发一下试试