Reply.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const API = require('../../../lib/API')
  2. const { BaseStdResponse } = require('../../../BaseStdResponse')
  3. const OneBotV11 = require('../../../plugin/OneBot/OneBotV11')
  4. const { ConversationService } = require('../../../lib/AIChat/ConversationService')
  5. class BotReply extends API {
  6. constructor() {
  7. super()
  8. this.noEncrypt()
  9. this.setPath('/AIChat/Bot/Reply')
  10. this.setMethod('POST')
  11. }
  12. async onRequest(req, res) {
  13. const onebot = OneBotV11.getAiChatOneBotConfig()
  14. if (!onebot.enabled) return res.json({ ...BaseStdResponse.ERR, msg: 'AIChat OneBot v11 未启用' })
  15. const token = req.headers['x-onebot-access-token'] || req.headers['authorization'] || ''
  16. if (onebot.callbackToken && !String(token).includes(onebot.callbackToken)) {
  17. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED, msg: 'AIChat 回调 token 校验失败' })
  18. }
  19. const { conversation_id, conversation_no, id, content, images = [] } = req.body || {}
  20. const conversationId = conversation_id || id
  21. if ([conversationId || conversation_no, content].some(v => v === '' || v === null || v === undefined)) {
  22. return res.json({ ...BaseStdResponse.MISSING_PARAMETER, msg: '缺少 conversation_id/conversation_no 或 content' })
  23. }
  24. const ok = await ConversationService.addAssistantMessage({
  25. conversationId,
  26. conversationNo: conversation_no,
  27. content,
  28. images
  29. })
  30. if (!ok) return res.json({ ...BaseStdResponse.ERR, msg: '会话不存在或已删除' })
  31. return res.json({ ...BaseStdResponse.OK })
  32. }
  33. }
  34. module.exports.BotReply = BotReply