Commit 38a9c1a7ee0ddd718b25751fd27b1219d4e6bfd5
1 parent
b6096b76
【增加】mj图片处理成功消息,增加component操作,处理error信息保存
Showing
2 changed files
with
67 additions
and
2 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/AiImageConvert.java
| 1 | 1 | package cn.iocoder.yudao.module.ai.convert; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyMessage; | |
| 3 | 4 | import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingReqVO; |
| 4 | 5 | import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingRespVO; |
| 5 | 6 | import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageListRespVO; |
| 7 | +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageMidjourneyOperationsVO; | |
| 6 | 8 | import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO; |
| 7 | 9 | import org.mapstruct.Mapper; |
| 8 | 10 | import org.mapstruct.factory.Mappers; |
| ... | ... | @@ -36,4 +38,12 @@ public interface AiImageConvert { |
| 36 | 38 | * @return |
| 37 | 39 | */ |
| 38 | 40 | List<AiImageListRespVO> convertAiImageListRespVO(List<AiImageDO> list); |
| 41 | + | |
| 42 | + /** | |
| 43 | + * 转换 - AiImageMidjourneyOperationsVO | |
| 44 | + * | |
| 45 | + * @param component | |
| 46 | + * @return | |
| 47 | + */ | |
| 48 | + AiImageMidjourneyOperationsVO convertAiImageMidjourneyOperationsVO(MidjourneyMessage.Component component); | |
| 39 | 49 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/midjourneyHandler/YuDaoMidjourneyMessageHandler.java
| ... | ... | @@ -5,14 +5,21 @@ import cn.hutool.core.util.StrUtil; |
| 5 | 5 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyMessage; |
| 6 | 6 | import cn.iocoder.yudao.framework.ai.midjourney.constants.MidjourneyGennerateStatusEnum; |
| 7 | 7 | import cn.iocoder.yudao.framework.ai.midjourney.webSocket.MidjourneyMessageHandler; |
| 8 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | |
| 9 | +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageMidjourneyOperationsVO; | |
| 10 | +import cn.iocoder.yudao.module.ai.convert.AiImageConvert; | |
| 8 | 11 | import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO; |
| 9 | -import cn.iocoder.yudao.module.ai.enums.AiImageDrawingStatusEnum; | |
| 10 | 12 | import cn.iocoder.yudao.module.ai.dal.mysql.AiImageMapper; |
| 13 | +import cn.iocoder.yudao.module.ai.enums.AiImageDrawingStatusEnum; | |
| 11 | 14 | import com.alibaba.fastjson2.JSON; |
| 12 | 15 | import lombok.AllArgsConstructor; |
| 13 | 16 | import lombok.extern.slf4j.Slf4j; |
| 14 | 17 | import org.springframework.stereotype.Component; |
| 15 | 18 | |
| 19 | +import java.util.Collections; | |
| 20 | +import java.util.List; | |
| 21 | +import java.util.stream.Collectors; | |
| 22 | + | |
| 16 | 23 | /** |
| 17 | 24 | * yudao message handler |
| 18 | 25 | * |
| ... | ... | @@ -45,6 +52,36 @@ public class YuDaoMidjourneyMessageHandler implements MidjourneyMessageHandler { |
| 45 | 52 | if (StrUtil.isBlank(midjourneyMessage.getNonce())) { |
| 46 | 53 | return; |
| 47 | 54 | } |
| 55 | + // 根据 Embeds 来判断是否异常 | |
| 56 | + if (CollUtil.isEmpty(midjourneyMessage.getEmbeds())) { | |
| 57 | + successHandler(midjourneyMessage); | |
| 58 | + } else { | |
| 59 | + errorHandler(midjourneyMessage); | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + private void errorHandler(MidjourneyMessage midjourneyMessage) { | |
| 64 | + // image 编号 | |
| 65 | + Long aiImageId = Long.valueOf(midjourneyMessage.getNonce()); | |
| 66 | + // 获取 error message | |
| 67 | + String errorMessage = getErrorMessage(midjourneyMessage); | |
| 68 | + aiImageMapper.updateById( | |
| 69 | + new AiImageDO() | |
| 70 | + .setId(aiImageId) | |
| 71 | + .setDrawingErrorMessage(errorMessage) | |
| 72 | + .setDrawingStatus(AiImageDrawingStatusEnum.FAIL.getStatus()) | |
| 73 | + ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + private String getErrorMessage(MidjourneyMessage midjourneyMessage) { | |
| 77 | + StringBuilder errorMessage = new StringBuilder(); | |
| 78 | + for (MidjourneyMessage.Embed embed : midjourneyMessage.getEmbeds()) { | |
| 79 | + errorMessage.append(embed.getDescription()); | |
| 80 | + } | |
| 81 | + return errorMessage.toString(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + private void successHandler(MidjourneyMessage midjourneyMessage) { | |
| 48 | 85 | // 获取id |
| 49 | 86 | Long aiImageId = Long.valueOf(midjourneyMessage.getNonce()); |
| 50 | 87 | // 获取生成 url |
| ... | ... | @@ -59,14 +96,32 @@ public class YuDaoMidjourneyMessageHandler implements MidjourneyMessageHandler { |
| 59 | 96 | drawingStatusEnum = AiImageDrawingStatusEnum.COMPLETE; |
| 60 | 97 | } else if (MidjourneyGennerateStatusEnum.IN_PROGRESS.getStatus().equals(generateStatus)) { |
| 61 | 98 | drawingStatusEnum = AiImageDrawingStatusEnum.IN_PROGRESS; |
| 62 | - } else if (MidjourneyGennerateStatusEnum.WAITING.getStatus().equals(generateStatus)) { | |
| 99 | + } else if (MidjourneyGennerateStatusEnum.WAITING.getStatus().equals(generateStatus)) { | |
| 63 | 100 | drawingStatusEnum = AiImageDrawingStatusEnum.WAITING; |
| 64 | 101 | } |
| 102 | + // 获取 midjourneyOperations | |
| 103 | + List<AiImageMidjourneyOperationsVO> midjourneyOperations = getMidjourneyOperationsList(midjourneyMessage); | |
| 104 | + // 更新数据库 | |
| 65 | 105 | aiImageMapper.updateById( |
| 66 | 106 | new AiImageDO() |
| 67 | 107 | .setId(aiImageId) |
| 68 | 108 | .setDrawingImageUrl(imageUrl) |
| 69 | 109 | .setDrawingStatus(drawingStatusEnum == null ? null : drawingStatusEnum.getStatus()) |
| 110 | + .setMjMessageId(midjourneyMessage.getId()) | |
| 111 | + .setMjOperations(JsonUtils.toJsonString(midjourneyOperations)) | |
| 70 | 112 | ); |
| 71 | 113 | } |
| 114 | + | |
| 115 | + private List<AiImageMidjourneyOperationsVO> getMidjourneyOperationsList(MidjourneyMessage midjourneyMessage) { | |
| 116 | + // 为空直接返回 | |
| 117 | + if (CollUtil.isEmpty(midjourneyMessage.getComponents())) { | |
| 118 | + return Collections.emptyList(); | |
| 119 | + } | |
| 120 | + // 将 component 转成 AiImageMidjourneyOperationsVO | |
| 121 | + return midjourneyMessage.getComponents().stream() | |
| 122 | + .map(componentType -> componentType.getComponents().stream() | |
| 123 | + .map(AiImageConvert.INSTANCE::convertAiImageMidjourneyOperationsVO) | |
| 124 | + .collect(Collectors.toList())) | |
| 125 | + .toList().stream().flatMap(List::stream).toList(); | |
| 126 | + } | |
| 72 | 127 | } | ... | ... |