| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- const API = require('../../../lib/API')
- const { BaseStdResponse } = require('../../../BaseStdResponse')
- const OneBotV11 = require('../../../plugin/OneBot/OneBotV11')
- const { ConversationService } = require('../../../lib/AIChat/ConversationService')
- class BotReply extends API {
- constructor() {
- super()
- this.noEncrypt()
- this.setPath('/AIChat/Bot/Reply')
- this.setMethod('POST')
- }
- async onRequest(req, res) {
- const onebot = OneBotV11.getAiChatOneBotConfig()
- if (!onebot.enabled) return res.json({ ...BaseStdResponse.ERR, msg: 'AIChat OneBot v11 未启用' })
- const token = req.headers['x-onebot-access-token'] || req.headers['authorization'] || ''
- if (onebot.callbackToken && !String(token).includes(onebot.callbackToken)) {
- return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED, msg: 'AIChat 回调 token 校验失败' })
- }
- const { conversation_id, conversation_no, id, content, images = [] } = req.body || {}
- const conversationId = conversation_id || id
- if ([conversationId || conversation_no, content].some(v => v === '' || v === null || v === undefined)) {
- return res.json({ ...BaseStdResponse.MISSING_PARAMETER, msg: '缺少 conversation_id/conversation_no 或 content' })
- }
- const ok = await ConversationService.addAssistantMessage({
- conversationId,
- conversationNo: conversation_no,
- content,
- images
- })
- if (!ok) return res.json({ ...BaseStdResponse.ERR, msg: '会话不存在或已删除' })
- return res.json({ ...BaseStdResponse.OK })
- }
- }
- module.exports.BotReply = BotReply
|