修复bug和旧代码清理 (#553)

* fix: 修复星火api上下文

* 将无星火ck的情况降低为warn

* feat: 添加星火设定自定义代码功能

* 修复星火api模式的一些问题

* 修复导出配置问题

* feat:添加工具箱快捷登录接口

* 添加工具箱快捷登录指令

* 阻止群聊使用快捷登录

* 添加Azure配置支持,修复重复的配置项冲突

* 移除旧版本渲染和新版本帮助

* 添加工具箱

* 更新工具箱替换原有后台

* 更新工具箱适配代码
This commit is contained in:
HalcyonAlcedo 2023-09-08 12:28:02 +08:00 committed by GitHub
parent 043b80ddd4
commit bf761c24da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
183 changed files with 1206 additions and 7675 deletions

View file

@ -1455,7 +1455,7 @@ export class chatgpt extends plugin {
}
async cacheContent(e, use, content, prompt, quote = [], mood = '', suggest = '', imgUrls = []) {
let cacheData = { file: '', cacheUrl: Config.cacheUrl, status: '' }
let cacheData = { file: '', status: '' }
cacheData.file = randomString()
const cacheresOption = {
method: 'POST',
@ -1497,49 +1497,7 @@ export class chatgpt extends plugin {
async renderImage(e, use, content, prompt, quote = [], mood = '', suggest = '', imgUrls = []) {
let cacheData = await this.cacheContent(e, use, content, prompt, quote, mood, suggest, imgUrls)
const template = use !== 'bing' ? 'content/ChatGPT/index' : 'content/Bing/index'
if (!Config.oldview) {
if (cacheData.error || cacheData.status != 200) { await this.reply(`出现错误:${cacheData.error || 'server error ' + cacheData.status}`, true) } else { await e.reply(await renderUrl(e, (Config.viewHost ? `${Config.viewHost}/` : `http://127.0.0.1:${Config.serverPort || 3321}/`) + `page/${cacheData.file}?qr=${Config.showQRCode ? 'true' : 'false'}`, { retType: Config.quoteReply ? 'base64' : '', Viewport: { width: parseInt(Config.chatViewWidth), height: parseInt(parseInt(Config.chatViewWidth) * 0.56) }, func: (parseFloat(Config.live2d) && !Config.viewHost) ? 'window.Live2d == true' : '', deviceScaleFactor: parseFloat(Config.cloudDPR) }), e.isGroup && Config.quoteReply) }
} else {
if (Config.cacheEntry) cacheData.file = randomString()
const cacheresOption = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
content: {
content: new Buffer.from(content).toString('base64'),
prompt: new Buffer.from(prompt).toString('base64'),
senderName: e.sender.nickname,
style: Config.toneStyle,
mood,
quote
},
model: use,
bing: use === 'bing',
entry: Config.cacheEntry ? cacheData.file : ''
})
}
if (Config.cacheEntry) {
fetch(`${Config.cacheUrl}/cache`, cacheresOption)
} else {
const cacheres = await fetch(`${Config.cacheUrl}/cache`, cacheresOption)
if (cacheres.ok) {
cacheData = Object.assign({}, cacheData, await cacheres.json())
}
}
await e.reply(await render(e, 'chatgpt-plugin', template, {
content: new Buffer.from(content).toString('base64'),
prompt: new Buffer.from(prompt).toString('base64'),
senderName: e.sender.nickname,
quote: quote.length > 0,
quotes: quote,
cache: cacheData,
style: Config.toneStyle,
mood,
version
}, { retType: Config.quoteReply ? 'base64' : '' }), e.isGroup && Config.quoteReply)
}
if (cacheData.error || cacheData.status != 200) { await this.reply(`出现错误:${cacheData.error || 'server error ' + cacheData.status}`, true) } else { await e.reply(await renderUrl(e, (Config.viewHost ? `${Config.viewHost}/` : `http://127.0.0.1:${Config.serverPort || 3321}/`) + `page/${cacheData.file}?qr=${Config.showQRCode ? 'true' : 'false'}`, { retType: Config.quoteReply ? 'base64' : '', Viewport: { width: parseInt(Config.chatViewWidth), height: parseInt(parseInt(Config.chatViewWidth) * 0.56) }, func: (parseFloat(Config.live2d) && !Config.viewHost) ? 'window.Live2d == true' : '', deviceScaleFactor: parseFloat(Config.cloudDPR) }), e.isGroup && Config.quoteReply) }
}
async sendMessage(prompt, conversation = {}, use, e) {
@ -1907,7 +1865,8 @@ export class chatgpt extends plugin {
}
const ssoSessionId = Config.xinghuoToken
if (!ssoSessionId) {
throw new Error('未绑定星火token请使用#chatgpt设置星火token命令绑定token。获取对话页面的ssoSessionId cookie值')
//throw new Error('未绑定星火token请使用#chatgpt设置星火token命令绑定token。获取对话页面的ssoSessionId cookie值')
logger.warn(`未绑定星火token请使用#chatgpt设置星火token命令绑定token。获取对话页面的ssoSessionId cookie值`)
}
let client = new XinghuoClient({
ssoSessionId,
@ -1915,7 +1874,11 @@ export class chatgpt extends plugin {
})
// 获取图片资源
const image = await getImg(e)
let response = await client.sendMessage(prompt, conversation?.conversationId, image ? image[0] : undefined)
let response = await client.sendMessage(prompt, {
e,
chatId: conversation?.conversationId,
image: image ? image[0] : undefined
})
return response
}
case 'azure': {
@ -1930,7 +1893,7 @@ export class chatgpt extends plugin {
let msg = conversation.messages
let content = { role: 'user', content: prompt }
msg.push(content)
const client = new OpenAIClient(Config.azureUrl, new AzureKeyCredential(Config.apiKey))
const client = new OpenAIClient(Config.azureUrl, new AzureKeyCredential(Config.azApiKey))
const deploymentName = Config.azureDeploymentName
const { choices } = await client.getChatCompletions(deploymentName, msg)
let completion = choices[0].message;

View file

@ -1,6 +1,6 @@
import plugin from '../../../lib/plugins/plugin.js'
import { Config } from '../utils/config.js'
import { render, renderUrl } from '../utils/common.js'
import { render } from '../utils/common.js'
let version = Config.version
let helpData = [
{
@ -326,25 +326,12 @@ export class help extends plugin {
{
reg: '^#(chatgpt|ChatGPT)(命令|帮助|菜单|help|说明|功能|指令|使用说明)$',
fnc: 'help'
},
{
reg: '^#帮助-',
fnc: 'newHelp'
}
]
})
}
async help (e) {
if (Config.newhelp && !Config.oldview) {
await renderUrl(e, `http://127.0.0.1:${Config.serverPort || 3321}/help/`, { Viewport: { width: 800, height: 600 } })
} else {
await render(e, 'chatgpt-plugin', 'help/index', { helpData, version })
}
}
async newHelp (e) {
let use = e.msg.replace(/^#帮助-/, '').toUpperCase().trim()
await renderUrl(e, `http://127.0.0.1:${Config.serverPort || 3321}/help/` + use, { Viewport: { width: 800, height: 600 } })
await render(e, 'chatgpt-plugin', 'help/index', { helpData, version })
}
}

View file

@ -9,7 +9,8 @@ import {
getVoicevoxRoleList,
makeForwardMsg,
parseDuration,
renderUrl
renderUrl,
randomString
} from '../utils/common.js'
import SydneyAIClient from '../utils/SydneyAIClient.js'
import { convertSpeaker, speakers as vitsRoleList } from '../utils/tts.js'
@ -212,6 +213,11 @@ export class ChatgptManagement extends plugin {
reg: '^#(设置|修改)用户密码',
fnc: 'setUserPassword'
},
{
reg: '^#工具箱',
fnc: 'toolsPage',
permission: 'master'
},
{
reg: '^#chatgpt系统(设置|配置|管理)',
fnc: 'adminPage',
@ -1234,7 +1240,7 @@ Poe 模式会调用 Poe 中的 Claude-instant 进行对话。需要提供 Cookie
return true
}
const viewHost = Config.serverHost ? `http://${Config.serverHost}/` : `http://${await getPublicIP()}:${Config.serverPort || 3321}/`
await this.reply(`请登录${viewHost + 'admin/settings'}进行系统配置`, true)
await this.reply(`请登录${viewHost}进行系统配置`, true)
}
async userPage (e) {
@ -1243,7 +1249,22 @@ Poe 模式会调用 Poe 中的 Claude-instant 进行对话。需要提供 Cookie
return true
}
const viewHost = Config.serverHost ? `http://${Config.serverHost}/` : `http://${await getPublicIP()}:${Config.serverPort || 3321}/`
await this.reply(`请登录${viewHost + 'admin/dashboard'}进行系统配置`, true)
await this.reply(`请登录${viewHost}进行系统配置`, true)
}
async toolsPage (e) {
if (e.isGroup || !e.isPrivate) {
await this.reply('请私聊发送命令', true)
return true
}
const viewHost = Config.serverHost ? `http://${Config.serverHost}/` : `http://${await getPublicIP()}:${Config.serverPort || 3321}/`
const otp = randomString(6)
await redis.set(
`CHATGPT:SERVER_QUICK`,
otp,
{ EX: 60000 }
)
await this.reply(`请登录http://tools.alcedogroup.com/login?server=${viewHost}&otp=${otp}`, true)
}
async setOpenAIPlatformToken (e) {
@ -1280,7 +1301,7 @@ Poe 模式会调用 Poe 中的 Claude-instant 进行对话。需要提供 Cookie
if (await redis.exists('CHATGPT:USE') != 0) {
redisConfig.useMode = await redis.get('CHATGPT:USE')
}
const filepath = path.join('plugins/chatgpt-plugin/resources', 'view.json')
const filepath = path.join('plugins/chatgpt-plugin/resources/view', 'setting_view.json')
const configView = JSON.parse(fs.readFileSync(filepath, 'utf8'))
const configJson = JSON.stringify({
chatConfig: Config,
@ -1289,7 +1310,7 @@ Poe 模式会调用 Poe 中的 Claude-instant 进行对话。需要提供 Cookie
})
console.log(configJson)
const buf = Buffer.from(configJson)
e.friend.sendFile(buf, `ChatGPT-Plugin Config ${new Date()}.json`)
e.friend.sendFile(buf, `ChatGPT-Plugin Config ${Date.now()}.json`)
return true
}