Commit 2f078966514a1bb0c86f054701193b588402865d

Authored by cherishsince
1 parent 937992fd

【优化】MidjourneyApi 保持 spring ai 代码风格,使用 record

yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/api/MidjourneyApi.java
1 1 package cn.iocoder.yudao.framework.ai.core.model.midjourney.api;
2 2  
3 3 import cn.iocoder.yudao.framework.ai.core.model.midjourney.MidjourneyProperties;
4   -import cn.iocoder.yudao.framework.ai.core.model.midjourney.vo.MidjourneyActionRequest;
5   -import cn.iocoder.yudao.framework.ai.core.model.midjourney.vo.MidjourneyImagineRequest;
6   -import cn.iocoder.yudao.framework.ai.core.model.midjourney.vo.MidjourneyNotifyRequest;
7   -import cn.iocoder.yudao.framework.ai.core.model.midjourney.vo.MidjourneySubmitResponse;
8 4 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
9 5 import com.google.common.collect.ImmutableMap;
  6 +import com.google.common.collect.Lists;
  7 +import lombok.AllArgsConstructor;
  8 +import lombok.Getter;
10 9 import lombok.extern.slf4j.Slf4j;
11 10 import org.springframework.ai.openai.api.ApiUtils;
12 11 import org.springframework.web.reactive.function.client.WebClient;
... ... @@ -14,6 +13,7 @@ import reactor.core.publisher.Mono;
14 13  
15 14 import java.util.Collection;
16 15 import java.util.List;
  16 +import java.util.Map;
17 17  
18 18 /**
19 19 * Midjourney api
... ... @@ -71,11 +71,11 @@ public class MidjourneyApi {
71 71 * @param taskIds
72 72 * @return
73 73 */
74   - public List<MidjourneyNotifyRequest> listByCondition(Collection<String> taskIds) {
  74 + public List<NotifyRequest> listByCondition(Collection<String> taskIds) {
75 75 // 1、发送 post 请求
76 76 String res = post(URI_LIST_BY_CONDITION, ImmutableMap.of("ids", taskIds));
77 77 // 2、转换 对象
78   - return JsonUtils.parseArray(res, MidjourneyNotifyRequest.class);
  78 + return JsonUtils.parseArray(res, NotifyRequest.class);
79 79 }
80 80  
81 81 private String post(String uri, Object body) {
... ... @@ -93,4 +93,205 @@ public class MidjourneyApi {
93 93 .bodyToMono(String.class)
94 94 .block();
95 95 }
  96 +
  97 + // ====== record 结构
  98 +
  99 + /**
  100 + * Midjourney - Imagine 请求
  101 + *
  102 + * @param base64Array 垫图(参考图)base64数组
  103 + * @param notifyHook 通知地址
  104 + * @param prompt 提示词
  105 + * @param state 自定义参数
  106 + */
  107 + public record MidjourneyImagineRequest(List<String> base64Array,
  108 + String notifyHook,
  109 + String prompt,
  110 + String state) {
  111 + }
  112 +
  113 + /**
  114 + * Midjourney - Action 请求
  115 + *
  116 + * @param customId 操作按钮id
  117 + * @param taskId 操作按钮id
  118 + * @param notifyHook 通知地址
  119 + * @param state 自定义参数
  120 + */
  121 + public record MidjourneyActionRequest(String customId,
  122 + String taskId,
  123 + String notifyHook,
  124 + String state) {
  125 + }
  126 +
  127 + /**
  128 + * Midjourney - Submit 返回
  129 + *
  130 + * @param code 状态码: 1(提交成功), 21(已存在), 22(排队中), other(错误)
  131 + * @param description 描述
  132 + * @param properties 扩展字段
  133 + * @param result 任务ID
  134 + */
  135 + public record MidjourneySubmitResponse(String code,
  136 + String description,
  137 + Map<String, Object> properties,
  138 + String result) {
  139 + }
  140 +
  141 + /**
  142 + * Midjourney - 通知 request
  143 + *
  144 + * @param id job id
  145 + * @param action 任务类型 {@link TaskActionEnum}
  146 + * @param status 任务状态 {@link TaskStatusEnum}
  147 + * @param prompt 提示词
  148 + * @param promptEn 提示词-英文
  149 + * @param description 任务描述
  150 + * @param state 自定义参数
  151 + * @param submitTime 提交时间
  152 + * @param startTime 开始执行时间
  153 + * @param finishTime 结束时间
  154 + * @param imageUrl 图片url
  155 + * @param progress 任务进度
  156 + * @param failReason 失败原因
  157 + * @param buttons 任务完成后的可执行按钮
  158 + */
  159 + public record NotifyRequest(String id,
  160 + String action,
  161 + String status,
  162 +
  163 + String prompt,
  164 + String promptEn,
  165 +
  166 + String description,
  167 + String state,
  168 +
  169 + Long submitTime,
  170 + Long startTime,
  171 + Long finishTime,
  172 +
  173 + String imageUrl,
  174 + String progress,
  175 + String failReason,
  176 + List<Button> buttons) {
  177 +
  178 + /**
  179 + * button
  180 + *
  181 + * @param customId MJ::JOB::upsample::1::85a4b4c1-8835-46c5-a15c-aea34fad1862 动作标识
  182 + * @param emoji 图标 emoji
  183 + * @param label Make Variations 文本
  184 + * @param type 类型,系统内部使用
  185 + * @param style 样式: 2(Primary)、3(Green)
  186 + */
  187 + public record Button(String customId,
  188 + String emoji,
  189 + String label,
  190 + String type,
  191 + String style) {
  192 +
  193 + }
  194 + }
  195 +
  196 + // ====== enums
  197 +
  198 + @AllArgsConstructor
  199 + @Getter
  200 + public enum ModelEnum {
  201 +
  202 + MIDJOURNEY("midjourney", "midjourney"),
  203 + NIJI("Niji", "Niji"),
  204 +
  205 + ;
  206 +
  207 + private String model;
  208 + private String name;
  209 +
  210 + public static ModelEnum valueOfModel(String model) {
  211 + for (ModelEnum itemEnum : ModelEnum.values()) {
  212 + if (itemEnum.getModel().equals(model)) {
  213 + return itemEnum;
  214 + }
  215 + }
  216 + throw new IllegalArgumentException("Invalid MessageType value: " + model);
  217 + }
  218 + }
  219 +
  220 + @Getter
  221 + @AllArgsConstructor
  222 + public enum SubmitCodeEnum {
  223 +
  224 + SUBMIT_SUCCESS("1", "提交成功"),
  225 + ALREADY_EXISTS("21", "已存在"),
  226 + QUEUING("22", "排队中"),
  227 + ;
  228 +
  229 + public static final List<String> SUCCESS_CODES = Lists.newArrayList(
  230 + SUBMIT_SUCCESS.code,
  231 + ALREADY_EXISTS.code,
  232 + QUEUING.code
  233 + );
  234 +
  235 + private final String code;
  236 + private final String name;
  237 + }
  238 +
  239 + @Getter
  240 + @AllArgsConstructor
  241 + public enum TaskActionEnum {
  242 + /**
  243 + * 生成图片.
  244 + */
  245 + IMAGINE,
  246 + /**
  247 + * 选中放大.
  248 + */
  249 + UPSCALE,
  250 + /**
  251 + * 选中其中的一张图,生成四张相似的.
  252 + */
  253 + VARIATION,
  254 + /**
  255 + * 重新执行.
  256 + */
  257 + REROLL,
  258 + /**
  259 + * 图转prompt.
  260 + */
  261 + DESCRIBE,
  262 + /**
  263 + * 多图混合.
  264 + */
  265 + BLEND
  266 + }
  267 +
  268 + @Getter
  269 + @AllArgsConstructor
  270 + public enum TaskStatusEnum {
  271 + /**
  272 + * 未启动.
  273 + */
  274 + NOT_START(0),
  275 + /**
  276 + * 已提交.
  277 + */
  278 + SUBMITTED(1),
  279 + /**
  280 + * 执行中.
  281 + */
  282 + IN_PROGRESS(3),
  283 + /**
  284 + * 失败.
  285 + */
  286 + FAILURE(4),
  287 + /**
  288 + * 成功.
  289 + */
  290 + SUCCESS(4);
  291 +
  292 + @Getter
  293 + private final int order;
  294 +
  295 + }
  296 +
96 297 }
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/enums/MidjourneyModelEnum.java deleted
1   -package cn.iocoder.yudao.framework.ai.core.model.midjourney.enums;
2   -
3   -
4   -import lombok.AllArgsConstructor;
5   -import lombok.Getter;
6   -
7   -/**
8   - * 来源于 midjourney-proxy
9   - */
10   -@Getter
11   -@AllArgsConstructor
12   -public enum MidjourneyModelEnum {
13   -
14   - MIDJOURNEY("midjourney", "midjourney"),
15   - NIJI("Niji", "Niji"),
16   -
17   - ;
18   -
19   - private String model;
20   - private String name;
21   -
22   - public static MidjourneyModelEnum valueOfModel(String model) {
23   - for (MidjourneyModelEnum itemEnum : MidjourneyModelEnum.values()) {
24   - if (itemEnum.getModel().equals(model)) {
25   - return itemEnum;
26   - }
27   - }
28   - throw new IllegalArgumentException("Invalid MessageType value: " + model);
29   - }
30   -}
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/enums/MidjourneySubmitCodeEnum.java deleted
1   -package cn.iocoder.yudao.framework.ai.core.model.midjourney.enums;
2   -
3   -import com.google.common.collect.Lists;
4   -import lombok.AllArgsConstructor;
5   -import lombok.Getter;
6   -
7   -import java.util.List;
8   -
9   -// TODO @fan:待定
10   -/**
11   - * Midjourney 提交任务 code 枚举
12   - *
13   - * @author fansili
14   - */
15   -@Getter
16   -@AllArgsConstructor
17   -public enum MidjourneySubmitCodeEnum {
18   -
19   - SUBMIT_SUCCESS("1", "提交成功"),
20   - ALREADY_EXISTS("21", "已存在"),
21   - QUEUING("22", "排队中"),
22   - ;
23   -
24   - public static final List<String> SUCCESS_CODES = Lists.newArrayList(
25   - SUBMIT_SUCCESS.code,
26   - ALREADY_EXISTS.code,
27   - QUEUING.code
28   - );
29   -
30   - private final String code;
31   - private final String name;
32   -
33   -}
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/enums/MidjourneyTaskActionEnum.java deleted
1   -package cn.iocoder.yudao.framework.ai.core.model.midjourney.enums;
2   -
3   -import lombok.Getter;
4   -
5   -/**
6   - * 来源于 midjourney-proxy
7   - */
8   -@Getter
9   -public enum MidjourneyTaskActionEnum {
10   - /**
11   - * 生成图片.
12   - */
13   - IMAGINE,
14   - /**
15   - * 选中放大.
16   - */
17   - UPSCALE,
18   - /**
19   - * 选中其中的一张图,生成四张相似的.
20   - */
21   - VARIATION,
22   - /**
23   - * 重新执行.
24   - */
25   - REROLL,
26   - /**
27   - * 图转prompt.
28   - */
29   - DESCRIBE,
30   - /**
31   - * 多图混合.
32   - */
33   - BLEND
34   -
35   -}
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/enums/MidjourneyTaskStatusEnum.java deleted
1   -package cn.iocoder.yudao.framework.ai.core.model.midjourney.enums;
2   -
3   -
4   -import lombok.Getter;
5   -
6   -/**
7   - * 来源于 midjourney-proxy
8   - */
9   -public enum MidjourneyTaskStatusEnum {
10   - /**
11   - * 未启动.
12   - */
13   - NOT_START(0),
14   - /**
15   - * 已提交.
16   - */
17   - SUBMITTED(1),
18   - /**
19   - * 执行中.
20   - */
21   - IN_PROGRESS(3),
22   - /**
23   - * 失败.
24   - */
25   - FAILURE(4),
26   - /**
27   - * 成功.
28   - */
29   - SUCCESS(4);
30   -
31   - @Getter
32   - private final int order;
33   -
34   - MidjourneyTaskStatusEnum(int order) {
35   - this.order = order;
36   - }
37   -
38   -}
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/vo/MidjourneyActionRequest.java deleted
1   -package cn.iocoder.yudao.framework.ai.core.model.midjourney.vo;
2   -
3   -import io.swagger.v3.oas.annotations.media.Schema;
4   -import lombok.Data;
5   -
6   -/**
7   - * Midjourney:action 请求
8   - *
9   - * @author fansili
10   - * @time 2024/5/30 14:02
11   - * @since 1.0
12   - */
13   -@Data
14   -public class MidjourneyActionRequest {
15   -
16   - @Schema(description = "操作按钮id", required = true)
17   - private String customId;
18   -
19   - @Schema(description = "操作按钮id", required = true)
20   - private String taskId;
21   -
22   - @Schema(description = "通知地址", required = false)
23   - private String notifyHook;
24   -
25   - @Schema(description = "自定义参数", required = false)
26   - private String state;
27   -}
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/vo/MidjourneyImagineRequest.java deleted
1   -package cn.iocoder.yudao.framework.ai.core.model.midjourney.vo;
2   -
3   -import io.swagger.v3.oas.annotations.media.Schema;
4   -import lombok.Data;
5   -
6   -import java.util.List;
7   -
8   -// TODO @fan:待定
9   -/**
10   - * Midjourney:Imagine 请求
11   - *
12   - * @author fansili
13   - * @time 2024/5/30 14:02
14   - * @since 1.0
15   - */
16   -@Data
17   -public class MidjourneyImagineRequest {
18   -
19   - @Schema(description = "垫图(参考图)base64数组", required = false)
20   - private List<String> base64Array;
21   -
22   - @Schema(description = "通知地址", required = false)
23   - private String notifyHook;
24   -
25   - @Schema(description = "提示词", required = true)
26   - private String prompt;
27   -
28   - @Schema(description = "自定义参数", required = false)
29   - private String state;
30   -}
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/vo/MidjourneyNotifyRequest.java deleted
1   -package cn.iocoder.yudao.framework.ai.core.model.midjourney.vo;
2   -
3   -import io.swagger.v3.oas.annotations.media.Schema;
4   -import lombok.Data;
5   -
6   -import java.util.List;
7   -
8   -/**
9   - * Midjourney Proxy 通知回调
10   - *
11   - * - Midjourney Proxy:通知回调 bean 是 com.github.novicezk.midjourney.support.Task
12   - * - 毫秒 api 通知回调文档地址:https://gpt-best.apifox.cn/doc-3530863
13   - *
14   - * @author fansili
15   - * @time 2024/5/31 10:37
16   - * @since 1.0
17   - */
18   -@Data
19   -public class MidjourneyNotifyRequest {
20   -
21   - @Schema(description = "job id")
22   - private String id;
23   -
24   - @Schema(description = "任务类型 MidjourneyTaskActionEnum")
25   - private String action;
26   - @Schema(description = "任务状态 MidjourneyTaskStatusEnum")
27   - private String status;
28   -
29   - @Schema(description = "提示词")
30   - private String prompt;
31   - @Schema(description = "提示词-英文")
32   - private String promptEn;
33   -
34   - @Schema(description = "任务描述")
35   - private String description;
36   - @Schema(description = "自定义参数")
37   - private String state;
38   -
39   - @Schema(description = "提交时间")
40   - private Long submitTime;
41   - @Schema(description = "开始执行时间")
42   - private Long startTime;
43   - @Schema(description = "结束时间")
44   - private Long finishTime;
45   -
46   - @Schema(description = "图片url")
47   - private String imageUrl;
48   -
49   - @Schema(description = "任务进度")
50   - private String progress;
51   - @Schema(description = "失败原因")
52   - private String failReason;
53   -
54   - @Schema(description = "任务完成后的可执行按钮")
55   - private List<Button> buttons;
56   -
57   - @Data
58   - public static class Button {
59   -
60   - @Schema(description = "MJ::JOB::upsample::1::85a4b4c1-8835-46c5-a15c-aea34fad1862 动作标识")
61   - private String customId;
62   -
63   - @Schema(description = "图标 emoji")
64   - private String emoji;
65   -
66   - @Schema(description = "Make Variations 文本")
67   - private String label;
68   -
69   - @Schema(description = "类型,系统内部使用")
70   - private String type;
71   -
72   - @Schema(description = "样式: 2(Primary)、3(Green)")
73   - private String style;
74   - }
75   -}
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/midjourney/vo/MidjourneySubmitResponse.java deleted
1   -package cn.iocoder.yudao.framework.ai.core.model.midjourney.vo;
2   -
3   -import io.swagger.v3.oas.annotations.media.Schema;
4   -import lombok.Data;
5   -
6   -import java.util.Map;
7   -
8   -// TODO @fan:待定
9   -/**
10   - * Midjourney:Imagine 请求
11   - *
12   - * @author fansili
13   - * @time 2024/5/30 14:02
14   - * @since 1.0
15   - */
16   -@Data
17   -public class MidjourneySubmitResponse {
18   -
19   - @Schema(description = "状态码: 1(提交成功), 21(已存在), 22(排队中), other(错误)")
20   - private String code;
21   -
22   - @Schema(description = "描述")
23   - private String description;
24   -
25   - @Schema(description = "扩展字段")
26   - private Map<String, Object> properties;
27   -
28   - @Schema(description = "任务ID")
29   - private String result;
30   -}