mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 13:27:08 +00:00
Merge branch 'v2' into v2
This commit is contained in:
commit
799b3a0769
5 changed files with 42 additions and 26 deletions
19
apps/bym.js
19
apps/bym.js
|
|
@ -1,25 +1,6 @@
|
|||
import { CustomGoogleGeminiClient } from '../client/CustomGoogleGeminiClient.js'
|
||||
import { Config } from '../utils/config.js'
|
||||
import { getImg } from '../utils/common.js'
|
||||
import { getChatHistoryGroup } from '../utils/chat.js'
|
||||
import { SearchVideoTool } from '../utils/tools/SearchBilibiliTool.js'
|
||||
import { SerpImageTool } from '../utils/tools/SearchImageTool.js'
|
||||
import { SearchMusicTool } from '../utils/tools/SearchMusicTool.js'
|
||||
import { SendAvatarTool } from '../utils/tools/SendAvatarTool.js'
|
||||
import { SendVideoTool } from '../utils/tools/SendBilibiliTool.js'
|
||||
import { SendMusicTool } from '../utils/tools/SendMusicTool.js'
|
||||
import { SendPictureTool } from '../utils/tools/SendPictureTool.js'
|
||||
import { WebsiteTool } from '../utils/tools/WebsiteTool.js'
|
||||
import { convertFaces } from '../utils/face.js'
|
||||
import { WeatherTool } from '../utils/tools/WeatherTool.js'
|
||||
import { EditCardTool } from '../utils/tools/EditCardTool.js'
|
||||
import { JinyanTool } from '../utils/tools/JinyanTool.js'
|
||||
import { KickOutTool } from '../utils/tools/KickOutTool.js'
|
||||
import { SetTitleTool } from '../utils/tools/SetTitleTool.js'
|
||||
import { SerpTool } from '../utils/tools/SerpTool.js'
|
||||
import { initializeImageTool } from '../utils/tools/ImageTool.js'
|
||||
import { DailyNewsTool } from '../utils/tools/DailyNewsTool.js'
|
||||
import { SendMessageToSpecificGroupOrUserTool } from '../utils/tools/SendMessageToSpecificGroupOrUserTool.js'
|
||||
import { customSplitRegex, filterResponseChunk } from '../utils/text.js'
|
||||
import core from '../model/core.js'
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import { supportConfigurations as azureRoleList } from '../utils/tts/microsoft-a
|
|||
import fetch from 'node-fetch'
|
||||
import { newFetch } from '../utils/proxy.js'
|
||||
import { createServer, runServer, stopServer } from '../server/index.js'
|
||||
import { BingAIClient } from '../client/CopilotAIClient.js'
|
||||
|
||||
export class ChatgptManagement extends plugin {
|
||||
constructor (e) {
|
||||
|
|
@ -364,6 +365,15 @@ export class ChatgptManagement extends plugin {
|
|||
}
|
||||
]
|
||||
})
|
||||
this.task = [
|
||||
{
|
||||
cron: '0 0 */2 * * ?',
|
||||
// cron: '*/1 * * * *',
|
||||
name: 'refreshBingAi',
|
||||
fnc: this.refreshBingAi,
|
||||
log: false
|
||||
}
|
||||
]
|
||||
this.reply = async (msg, quote, data) => {
|
||||
if (!Config.enableMd) {
|
||||
return e.reply(msg, quote, data)
|
||||
|
|
@ -1848,7 +1858,6 @@ azure语音:Azure 语音是微软 Azure 平台提供的一项语音服务,
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
async switchBYM (e) {
|
||||
if (e.msg.includes('开启')) {
|
||||
if (Config.enableBYM) {
|
||||
|
|
@ -1916,4 +1925,15 @@ azure语音:Azure 语音是微软 Azure 平台提供的一项语音服务,
|
|||
await e.reply('操作成功')
|
||||
}
|
||||
|
||||
async refreshBingAi () {
|
||||
if (Config.bingAiRefreshToken) {
|
||||
let client = new BingAIClient(Config.bingAiToken, Config.sydneyReverseProxy, Config.debug, Config._2captchaKey, Config.bingAiClientId, Config.bingAiScope, Config.bingAiRefreshToken, Config.bingAiOid, Config.bingReasoning)
|
||||
let json = await client.doRefreshToken()
|
||||
if (json.refresh_token) {
|
||||
logger.mark('Bing AI Token Refreshed')
|
||||
} else {
|
||||
logger.mark('Failed to refresh Bing AI Token')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import WebSocket from 'ws'
|
|||
import common from '../../../lib/common/common.js'
|
||||
import _ from 'lodash'
|
||||
import { pTimeout } from '../utils/common.js'
|
||||
import { Config } from '../utils/config.js'
|
||||
|
||||
export class BingAIClient {
|
||||
constructor (accessToken, baseUrl = 'wss://copilot.microsoft.com', debug, _2captchaKey, clientId, scope, refreshToken, oid, reasoning = false) {
|
||||
|
|
@ -75,12 +76,12 @@ export class BingAIClient {
|
|||
logger.info('received message', String(message))
|
||||
})
|
||||
}
|
||||
this.ws.on('close', (code, reason) => {
|
||||
this.ws.on('close', async (code, reason) => {
|
||||
console.log('WebSocket connection closed. Code:', code, 'Reason:', reason)
|
||||
|
||||
if (code === 401) {
|
||||
logger.error('token expired. try to refresh with refresh token')
|
||||
this.doRefreshToken(this.clientId, this.scope, this.refreshToken, this.oid)
|
||||
await this.doRefreshToken()
|
||||
}
|
||||
})
|
||||
|
||||
|
|
@ -265,7 +266,10 @@ export class BingAIClient {
|
|||
return token
|
||||
}
|
||||
|
||||
async _generateConversationId () {
|
||||
async _generateConversationId (times = 3) {
|
||||
if (times < 0) {
|
||||
throw new Error('max retry exceed, maybe refresh token error')
|
||||
}
|
||||
const url = `${this.baseUrl}/c/api/conversations`
|
||||
const createConversationRsp = await fetch(url, {
|
||||
headers: {
|
||||
|
|
@ -277,6 +281,10 @@ export class BingAIClient {
|
|||
},
|
||||
method: 'POST'
|
||||
})
|
||||
if (createConversationRsp.status === 401) {
|
||||
await this.doRefreshToken()
|
||||
return await this._generateConversationId(times - 1)
|
||||
}
|
||||
const conversation = await createConversationRsp.json()
|
||||
return conversation.id
|
||||
}
|
||||
|
|
@ -316,7 +324,7 @@ export class BingAIClient {
|
|||
* refresh_token: string
|
||||
* }>}
|
||||
*/
|
||||
async doRefreshToken (clientId, scope, refreshToken, oid) {
|
||||
async doRefreshToken (clientId = this.clientId, scope = this.scope, refreshToken = this.refreshToken, oid = this.oid) {
|
||||
const myHeaders = new Headers()
|
||||
myHeaders.append('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0')
|
||||
myHeaders.append('priority', 'u=1, i')
|
||||
|
|
@ -351,6 +359,12 @@ export class BingAIClient {
|
|||
if (this.debug) {
|
||||
logger.info(JSON.stringify(tokenJson))
|
||||
}
|
||||
this.accessToken = tokenJson.access_token
|
||||
Config.bingAiToken = this.accessToken
|
||||
if (tokenJson.refresh_token) {
|
||||
this.refreshToken = tokenJson.refresh_token
|
||||
Config.bingAiRefreshToken = this.refreshToken
|
||||
}
|
||||
|
||||
return tokenJson
|
||||
}
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
|
|||
}
|
||||
: {
|
||||
role: 'user',
|
||||
parts: [{ text }],
|
||||
parts: text ? [{ text }] : [],
|
||||
id: idThis,
|
||||
parentMessageId: opt.parentMessageId || undefined
|
||||
}
|
||||
|
|
@ -173,6 +173,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
|
|||
data: imageData
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ export function filterResponseChunk (msg) {
|
|||
if (msg.trim() === '<EMPTY>') {
|
||||
return false
|
||||
}
|
||||
msg = trimSpecific(msg, '<EMPTY')
|
||||
msg = trimSpecific(msg, '<EMPTY>')
|
||||
return msg
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue