Commit 3fe04ac98e899864642e96efc9d1dd1892361862
1 parent
defe6280
抽离 常用key到 mj constants 里面
Showing
2 changed files
with
43 additions
and
14 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MjConstants.java
| @@ -2,7 +2,36 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants; | @@ -2,7 +2,36 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants; | ||
| 2 | 2 | ||
| 3 | public final class MjConstants { | 3 | public final class MjConstants { |
| 4 | 4 | ||
| 5 | - public static final String CHANNEL_ID = "channel_id"; | 5 | + /** |
| 6 | + * 消息 - 编号 | ||
| 7 | + */ | ||
| 8 | + public static final String MSG_ID = "id"; | ||
| 9 | + /** | ||
| 10 | + * 消息 - 类型 | ||
| 11 | + * 现在已知: | ||
| 12 | + * 0:我们发送的消息,和指令 | ||
| 13 | + * 20: mj生成图片发送过程中 | ||
| 14 | + * 19: 选择了某一张图片后的通知 | ||
| 15 | + */ | ||
| 16 | + public static final String MSG_TYPE = "type"; | ||
| 17 | + /** | ||
| 18 | + * 平道id | ||
| 19 | + */ | ||
| 20 | + public static final String MSG_CHANNEL_ID = "channel_id"; | ||
| 21 | + /** | ||
| 22 | + * 内容 | ||
| 23 | + * | ||
| 24 | + * "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (32%) (fast, stealth)", | ||
| 25 | + */ | ||
| 26 | + public static final String MSG_CONTENT = "content"; | ||
| 27 | + /** | ||
| 28 | + * 组件(图片生成好之后才有) | ||
| 29 | + */ | ||
| 30 | + public static final String MSG_COMPONENTS = "components"; | ||
| 31 | + /** | ||
| 32 | + * 附件(生成中比较模糊的图片) | ||
| 33 | + */ | ||
| 34 | + public static final String MSG_ATTACHMENTS = "attachments"; | ||
| 6 | 35 | ||
| 7 | 36 | ||
| 8 | } | 37 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/webSocket/listener/MjMessageListener.java
| @@ -12,12 +12,9 @@ import cn.iocoder.yudao.framework.ai.midjourney.constants.MjGennerateStatusEnum; | @@ -12,12 +12,9 @@ import cn.iocoder.yudao.framework.ai.midjourney.constants.MjGennerateStatusEnum; | ||
| 12 | import cn.iocoder.yudao.framework.ai.midjourney.constants.MjMessageTypeEnum; | 12 | import cn.iocoder.yudao.framework.ai.midjourney.constants.MjMessageTypeEnum; |
| 13 | import cn.iocoder.yudao.framework.ai.midjourney.util.MjUtil; | 13 | import cn.iocoder.yudao.framework.ai.midjourney.util.MjUtil; |
| 14 | import com.alibaba.fastjson.JSON; | 14 | import com.alibaba.fastjson.JSON; |
| 15 | -import com.google.common.collect.Lists; | ||
| 16 | import lombok.extern.slf4j.Slf4j; | 15 | import lombok.extern.slf4j.Slf4j; |
| 17 | import net.dv8tion.jda.api.utils.data.DataObject; | 16 | import net.dv8tion.jda.api.utils.data.DataObject; |
| 18 | 17 | ||
| 19 | -import java.io.UnsupportedEncodingException; | ||
| 20 | -import java.util.Arrays; | ||
| 21 | import java.util.List; | 18 | import java.util.List; |
| 22 | 19 | ||
| 23 | @Slf4j | 20 | @Slf4j |
| @@ -39,25 +36,28 @@ public class MjMessageListener { | @@ -39,25 +36,28 @@ public class MjMessageListener { | ||
| 39 | return; | 36 | return; |
| 40 | } | 37 | } |
| 41 | 38 | ||
| 39 | + // 转换几个重要的信息 | ||
| 42 | MjMessage mjMessage = new MjMessage(); | 40 | MjMessage mjMessage = new MjMessage(); |
| 43 | - mjMessage.setId(data.getString("id")); | ||
| 44 | - mjMessage.setType(data.getInt("type")); | 41 | + mjMessage.setId(data.getString(MjConstants.MSG_ID)); |
| 42 | + mjMessage.setType(data.getInt(MjConstants.MSG_TYPE)); | ||
| 45 | mjMessage.setRawData(StrUtil.str(raw.toJson(), "UTF-8")); | 43 | mjMessage.setRawData(StrUtil.str(raw.toJson(), "UTF-8")); |
| 46 | - mjMessage.setContent(MjUtil.parseContent(data.getString("content"))); | ||
| 47 | - | ||
| 48 | - if (!data.getArray("components").isEmpty()) { | ||
| 49 | - String componentsJson = StrUtil.str(data.getArray("components").toJson(), "UTF-8"); | 44 | + mjMessage.setContent(MjUtil.parseContent(data.getString(MjConstants.MSG_CONTENT))); |
| 45 | + // 转换 components | ||
| 46 | + if (!data.getArray(MjConstants.MSG_COMPONENTS).isEmpty()) { | ||
| 47 | + String componentsJson = StrUtil.str(data.getArray(MjConstants.MSG_COMPONENTS).toJson(), "UTF-8"); | ||
| 50 | List<MjMessage.ComponentType> components = JSON.parseArray(componentsJson, MjMessage.ComponentType.class); | 48 | List<MjMessage.ComponentType> components = JSON.parseArray(componentsJson, MjMessage.ComponentType.class); |
| 51 | mjMessage.setComponents(components); | 49 | mjMessage.setComponents(components); |
| 52 | } | 50 | } |
| 53 | - if (!data.getArray("attachments").isEmpty()) { | ||
| 54 | - String attachmentsJson = StrUtil.str(data.getArray("attachments").toJson(), "UTF-8"); | 51 | + // 转换附件 |
| 52 | + if (!data.getArray(MjConstants.MSG_ATTACHMENTS).isEmpty()) { | ||
| 53 | + String attachmentsJson = StrUtil.str(data.getArray(MjConstants.MSG_ATTACHMENTS).toJson(), "UTF-8"); | ||
| 55 | List<MjMessage.Attachment> attachments = JSON.parseArray(attachmentsJson, MjMessage.Attachment.class); | 54 | List<MjMessage.Attachment> attachments = JSON.parseArray(attachmentsJson, MjMessage.Attachment.class); |
| 56 | mjMessage.setAttachments(attachments); | 55 | mjMessage.setAttachments(attachments); |
| 57 | } | 56 | } |
| 58 | - | ||
| 59 | // 转换状态 | 57 | // 转换状态 |
| 60 | convertGenerateStatus(mjMessage); | 58 | convertGenerateStatus(mjMessage); |
| 59 | + // | ||
| 60 | + log.info("message 信息 {}", JSONUtil.toJsonPrettyStr(mjMessage)); | ||
| 61 | System.err.println(JSONUtil.toJsonPrettyStr(mjMessage)); | 61 | System.err.println(JSONUtil.toJsonPrettyStr(mjMessage)); |
| 62 | } | 62 | } |
| 63 | 63 | ||
| @@ -72,7 +72,7 @@ public class MjMessageListener { | @@ -72,7 +72,7 @@ public class MjMessageListener { | ||
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | private boolean ignoreAndLogMessage(DataObject data, MjMessageTypeEnum messageType) { | 74 | private boolean ignoreAndLogMessage(DataObject data, MjMessageTypeEnum messageType) { |
| 75 | - String channelId = data.getString(MjConstants.CHANNEL_ID); | 75 | + String channelId = data.getString(MjConstants.MSG_CHANNEL_ID); |
| 76 | if (!CharSequenceUtil.equals(channelId, midjourneyConfig.getChannelId())) { | 76 | if (!CharSequenceUtil.equals(channelId, midjourneyConfig.getChannelId())) { |
| 77 | return true; | 77 | return true; |
| 78 | } | 78 | } |