Merge branch 'btn' of github.com:ikechan8370/chatgpt-plugin into btn

This commit is contained in:
ikechan8370 2024-03-10 17:27:47 +08:00
commit 2f79f2e1bc
8 changed files with 128 additions and 35 deletions

View file

@ -1,4 +1,5 @@
import plugin from '../../../lib/plugins/plugin.js' import plugin from '../../../lib/plugins/plugin.js'
import {Config} from "../utils/config.js";
const PLUGIN_CHAT = 'ChatGpt 对话' const PLUGIN_CHAT = 'ChatGpt 对话'
const PLUGIN_MANAGEMENT = 'ChatGPT-Plugin 管理' const PLUGIN_MANAGEMENT = 'ChatGPT-Plugin 管理'
@ -52,8 +53,8 @@ export class ChatGPTButtonHandler extends plugin {
} }
async btnHandler (e, options, reject) { async btnHandler (e, options, reject) {
logger.mark('[chatgpt按钮处理器]') // logger.mark('[chatgpt按钮处理器]')
if (e.adapter !== 'shamrock' && (!segment.button || segment.button(1)?.content !== 1)) { if (!Config.enableMd || (e.adapter !== 'shamrock' && (!segment.button || segment.button(1)?.content !== 1))) {
return null return null
} }
const fnc = e.logFnc const fnc = e.logFnc
@ -107,33 +108,86 @@ export class ChatGPTButtonHandler extends plugin {
/** /**
* *
* @param {{data: {suggested: string?, use: string}}} options * @param {{suggested: string?, use: string}?} options
*/ */
makeButtonChat (options) { async makeButtonChat (options) {
let endCommand = '#摧毁对话'
switch (options?.use) {
case 'api': {
endCommand = '#api结束对话'
break
}
case 'api3': {
endCommand = '#api3结束对话'
break
}
case 'bing': {
endCommand = '#必应结束对话'
break
}
case 'claude2': {
endCommand = '#克劳德结束对话'
break
}
case 'gemini': {
endCommand = '#双子星结束对话'
break
}
case 'xh': {
endCommand = '#星火结束对话'
break
}
case 'qwen': {
endCommand = '#通义千问结束对话'
break
}
case 'chatglm4': {
endCommand = '#智谱结束对话'
break
}
}
let rows = [ let rows = [
{ {
buttons: [ buttons: [
createButtonBase('结束对话', '#摧毁对话'), createButtonBase('结束对话', '#对话'),
createButtonBase('切换模式', '#chatgpt切换', false), createButtonBase('结束当前对话', endCommand),
createButtonBase('at我对话', '', false) createButtonBase('at我对话', '', false)
] ]
},
{
buttons: [
createButtonBase('OpenAPI', '#chat1', false),
createButtonBase('ChatGPT', '#chat3', false),
createButtonBase('Copilot', '#bing', false),
createButtonBase('Gemini', '#gemini', false)
]
},
{
buttons: [
createButtonBase('讯飞星火', '#xh', false),
createButtonBase('通义千问', '#qwen', false),
createButtonBase('ChatGLM4', '#glm4', false)
]
} }
] ]
let buttons = [[], []]
if (Config.apiKey) {
buttons[0].push(createButtonBase('OpenAPI', '#chat1', false))
}
if (await redis.get('CHATGPT:TOKEN')) {
buttons[0].push(createButtonBase('ChatGPT', '#chat3', false))
}
if (await redis.get('CHATGPT:BING_TOKENS')) {
buttons[0].push(createButtonBase('Copilot', '#bing', false))
}
if (Config.geminiKey) {
buttons[0].push(createButtonBase('Gemini', '#gemini', false))
}
if (Config.xhAPIKey) {
buttons[buttons[0].length >= 4 ? 1 : 0].push(createButtonBase('讯飞星火', '#xh', false))
}
if (Config.qwenApiKey) {
buttons[buttons[0].length >= 4 ? 1 : 0].push(createButtonBase('通义千问', '#qwen', false))
}
if (Config.chatglmRefreshToken) {
buttons[buttons[0].length >= 4 ? 1 : 0].push(createButtonBase('ChatGLM4', '#glm4', false))
}
if (Config.claudeAISessionKey) {
buttons[buttons[0].length >= 4 ? 1 : 0].push(createButtonBase('Claude', '#claude.ai', false))
}
rows.push({
buttons: buttons[0]
})
if (buttons[1].length > 0) {
rows.push({
buttons: buttons[1]
})
}
if (options?.suggested) { if (options?.suggested) {
rows.unshift({ rows.unshift({
buttons: options.suggested.split('\n').map(s => { buttons: options.suggested.split('\n').map(s => {
@ -192,8 +246,8 @@ export class ChatGPTButtonHandler extends plugin {
{ {
buttons: [ buttons: [
createButtonBase('恢复本群回复', '#chatgpt本群张嘴', false), createButtonBase('恢复本群回复', '#chatgpt本群张嘴', false),
createButtonBase('开启上下文', '#chatgpt打开上下文'), createButtonBase('开启上下文', '#打开群聊上下文'),
createButtonBase('关闭上下文 ', '#chatgpt关闭上下文') createButtonBase('关闭上下文 ', '#关闭群聊上下文')
] ]
}, },

View file

@ -173,7 +173,7 @@ export class chatgpt extends plugin {
}, },
{ {
/** 命令正则匹配 */ /** 命令正则匹配 */
reg: '^#claude2[sS]*', reg: '^#claude(2|3|.ai)[sS]*',
/** 执行方法 */ /** 执行方法 */
fnc: 'claude2' fnc: 'claude2'
}, },
@ -292,6 +292,9 @@ export class chatgpt extends plugin {
}) })
this.toggleMode = toggleMode this.toggleMode = toggleMode
this.reply = async (msg, quote, data) => { this.reply = async (msg, quote, data) => {
if (!Config.enableMd) {
return e.reply(msg, quote, data)
}
let handler = e.runtime?.handler || {} let handler = e.runtime?.handler || {}
const btns = await handler.call('chatgpt.button.post', this.e, data) const btns = await handler.call('chatgpt.button.post', this.e, data)
const btnElement = { const btnElement = {
@ -303,7 +306,7 @@ export class chatgpt extends plugin {
} else { } else {
msg = [msg, btnElement] msg = [msg, btnElement]
} }
return e.reply(msg, data) return e.reply(msg, quote, data)
} }
} }
@ -907,7 +910,7 @@ export class chatgpt extends plugin {
* #chatgpt * #chatgpt
*/ */
async chatgpt (e) { async chatgpt (e) {
let msg = (Version.isTrss || e.adapter === 'shamrock') ? e.msg : e.raw_message let msg = e.msg
let prompt let prompt
if (this.toggleMode === 'at') { if (this.toggleMode === 'at') {
if (!msg || e.msg?.startsWith('#')) { if (!msg || e.msg?.startsWith('#')) {
@ -959,7 +962,7 @@ export class chatgpt extends plugin {
} }
return false return false
} }
prompt = _.replace(e.raw_message.trimStart(), '#chat', '').trim() prompt = _.replace(e.msg.trimStart(), '#chat', '').trim()
if (prompt.length === 0) { if (prompt.length === 0) {
return false return false
} }
@ -1512,7 +1515,7 @@ export class chatgpt extends plugin {
} }
async claude2 (e) { async claude2 (e) {
return await this.otherMode(e, 'claude2') return await this.otherMode(e, 'claude2', /^#claude(2|3|.ai)/)
} }
async claude (e) { async claude (e) {
@ -2804,6 +2807,13 @@ export class chatgpt extends plugin {
return await this.chatGPTApi.sendMessage(prompt, sendMessageOption) return await this.chatGPTApi.sendMessage(prompt, sendMessageOption)
} }
/**
* 其他模式
* @param e
* @param mode
* @param {string|RegExp} pattern
* @returns {Promise<boolean>}
*/
async otherMode (e, mode, pattern = `#${mode}`) { async otherMode (e, mode, pattern = `#${mode}`) {
if (!Config.allowOtherMode) { if (!Config.allowOtherMode) {
return false return false

View file

@ -83,6 +83,9 @@ export class Entertainment extends plugin {
} }
] ]
this.reply = async (msg, quote, data) => { this.reply = async (msg, quote, data) => {
if (!Config.enableMd) {
return e.reply(msg, quote, data)
}
let handler = e.runtime?.handler || {} let handler = e.runtime?.handler || {}
const btns = await handler.call('chatgpt.button.post', this.e) const btns = await handler.call('chatgpt.button.post', this.e)
const btnElement = { const btnElement = {
@ -94,7 +97,7 @@ export class Entertainment extends plugin {
} else { } else {
msg = [msg, btnElement] msg = [msg, btnElement]
} }
return e.reply(msg, data) return e.reply(msg, quote, data)
} }
} }

View file

@ -334,6 +334,9 @@ export class ChatgptManagement extends plugin {
] ]
}) })
this.reply = async (msg, quote, data) => { this.reply = async (msg, quote, data) => {
if (!Config.enableMd) {
return e.reply(msg, quote, data)
}
let handler = e.runtime?.handler || {} let handler = e.runtime?.handler || {}
const btns = await handler.call('chatgpt.button.post', this.e) const btns = await handler.call('chatgpt.button.post', this.e)
if (btns) { if (btns) {
@ -347,7 +350,7 @@ export class ChatgptManagement extends plugin {
msg = [msg, btnElement] msg = [msg, btnElement]
} }
} }
return e.reply(msg, data) return e.reply(msg, quote, data)
} }
} }

View file

@ -1,5 +1,6 @@
import plugin from '../../../lib/plugins/plugin.js' import plugin from '../../../lib/plugins/plugin.js'
import cfg from '../../../lib/config/config.js' import {Config} from '../utils/config.js'
export class ChatGPTMarkdownHandler extends plugin { export class ChatGPTMarkdownHandler extends plugin {
constructor () { constructor () {
super({ super({
@ -15,11 +16,25 @@ export class ChatGPTMarkdownHandler extends plugin {
async mdHandler (e, options, reject) { async mdHandler (e, options, reject) {
const { content, prompt, use } = options const { content, prompt, use } = options
if (cfg.bot.global_md || e.adapter === 'shamrock') { if (Config.enableMd) {
let md = `> ${prompt}\n\n---\n${content}\n\n---\n*当前模式:${use}*` let mode = transUse(use)
return md return `> ${prompt}\n\n---\n${content}\n\n---\n*当前模式:${mode}*`
} else { } else {
return content return content
} }
} }
} }
function transUse (use) {
let useMap = {
api: Config.model,
bing: '必应(Copilot)' + Config.toneStyle,
gemini: Config.geminiModel,
xh: '讯飞星火 ' + Config.xhmode,
qwen: '通义千问 ' + Config.qwenModel,
claude2: 'Claude 3 Sonnet',
glm4: 'ChatGLM4',
chat3: 'ChatGPT官网'
}
return useMap[use] || use
}

View file

@ -57,6 +57,12 @@ export function supportGuoba () {
bottomHelpMessage: '将输出更多调试信息,如果不希望控制台刷屏的话,可以关闭', bottomHelpMessage: '将输出更多调试信息,如果不希望控制台刷屏的话,可以关闭',
component: 'Switch' component: 'Switch'
}, },
{
field: 'enableMd',
label: 'QQ开启markdown',
bottomHelpMessage: 'qq的第三方md非QQBot。需要适配器实现segment.markdown和segment.button方可使用否则不建议开启会造成各种错误。默认关闭',
component: 'Switch'
},
{ {
field: 'translateSource', field: 'translateSource',
label: '翻译来源', label: '翻译来源',

View file

@ -132,7 +132,8 @@ export class ClaudeAIClient {
let body = { let body = {
attachments, attachments,
files: [], files: [],
model: 'claude-2.1', // 官方更新后这里没有传值了
// model: 'claude-2.1',
prompt: text, prompt: text,
timezone: 'Asia/Hong_Kong' timezone: 'Asia/Hong_Kong'
} }

View file

@ -179,6 +179,7 @@ const defaultConfig = {
sunoSessToken: '', sunoSessToken: '',
sunoClientToken: '', sunoClientToken: '',
translateSource: 'openai', translateSource: 'openai',
enableMd: false, // 第三方md非QQBot。需要适配器实现segment.markdown和segment.button方可使用否则不建议开启会造成各种错误
version: 'v2.7.10' version: 'v2.7.10'
} }
const _path = process.cwd() const _path = process.cwd()