refactor(bym): 优化下

This commit is contained in:
ycxom 2025-01-01 23:52:26 +08:00
parent 6033d93028
commit c817a6d77d
2 changed files with 27 additions and 39 deletions

View file

@ -137,12 +137,10 @@ export class bym extends plugin {
replyPureTextCallback: e.reply, replyPureTextCallback: e.reply,
images: [] images: []
} }
let imgs = await getImg(e)
// 处理图片 // 处理图片
if (!e.msg) { let imgs = await getImg(e)
if (imgs?.length > 0) { async function processImages(imgs) {
// 并行处理多张图片 return Promise.all(imgs.map(async image => {
opt.images = await Promise.all(imgs.map(async image => {
try { try {
const response = await fetch(image) const response = await fetch(image)
const base64Image = Buffer.from(await response.arrayBuffer()) const base64Image = Buffer.from(await response.arrayBuffer())
@ -152,6 +150,12 @@ export class bym extends plugin {
return null return null
} }
})).then(results => results.filter(Boolean)) })).then(results => results.filter(Boolean))
}
if (!e.msg) {
if (imgs?.length > 0) {
// 并行处理多张图片
opt.images = await processImages(imgs)
e.msg = `[${opt.images.length}张图片]` e.msg = `[${opt.images.length}张图片]`
} else { } else {
@ -162,19 +166,10 @@ export class bym extends plugin {
} }
} else if (imgs?.length > 0 && !opt.images.length) { } else if (imgs?.length > 0 && !opt.images.length) {
// 处理有消息且有图片的情况 // 处理有消息且有图片的情况
opt.images = await Promise.all(imgs.map(async image => { opt.images = await processImages(imgs)
try {
const response = await fetch(image)
const base64Image = Buffer.from(await response.arrayBuffer())
return base64Image.toString('base64')
} catch (error) {
logger.error(`处理图片失败: ${error}`)
return null
}
})).then(results => results.filter(Boolean))
} }
logger.info('[bymGo] 开始处理回复') logger.info('[ChatGPT-Plugin 伪人bym] 开始处理~')
let previousRole = ALLRole let previousRole = ALLRole
if (opt.images?.length > 0 && !context.isAtBot && !NotToImg && !e.at && Config.AutoToDownImg) { if (opt.images?.length > 0 && !context.isAtBot && !NotToImg && !e.at && Config.AutoToDownImg) {
@ -322,10 +317,10 @@ export class bym extends plugin {
tools.push(new SetTitleTool()) tools.push(new SetTitleTool())
} }
const imageTool = await initializeImageTool(e, previousRole, bymGo) // 注意这里需要 await const imageTool = await initializeImageTool(e, previousRole, bymGo)
if (Config.AutoToDownImg) { if (Config.AutoToDownImg) {
tools.push(imageTool) tools.push(imageTool)
const imagePrompt = await imageTool.getSystemPrompt() // 使用 await const imagePrompt = await imageTool.getSystemPrompt()
opt.system += '\n' + imagePrompt opt.system += '\n' + imagePrompt
} }
@ -347,16 +342,11 @@ export class bym extends plugin {
// 处理工具返回结果 // 处理工具返回结果
if (processed && typeof processed === 'object') { if (processed && typeof processed === 'object') {
if (processed.switchRole) { if (processed.switchRole) ALLRole = processed.switchRole
ALLRole = processed.switchRole
}
if (processed.continueProcess) { if (processed.continueProcess) {
// 根据是否是重新处理来设置不同的消息 e.msg = processed.reprocess
if (processed.reprocess) { ? `[重新处理第${processed.currentIndex + 1}张图片的内容]`
e.msg = `[重新处理第${processed.currentIndex + 1}张图片的内容]` : `[处理第${processed.currentIndex + 1}张图片(共${opt.images.length}张)]`
} else {
e.msg = `[处理第${processed.currentIndex + 1}张图片(共${opt.images.length}张)]`
}
await bymGo(true) await bymGo(true)
return false return false
} else if (processed.needResponse) { } else if (processed.needResponse) {

View file

@ -64,8 +64,6 @@ export class ImageProcessTool extends AbstractTool {
} }
} }
async initializeImageList() { async initializeImageList() {
try { try {
this.#availableImages = await fileImgList() this.#availableImages = await fileImgList()