mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-13 02:25:06 +08:00
【增加】对接 Midjourney,增加nonce传递,更新Midjourney image 状态
This commit is contained in:
@ -14,6 +14,10 @@ public class MidjourneyMessage {
|
||||
* id是一个重要的字段,在同时生成多个的时候,可以区分生成信息
|
||||
*/
|
||||
private String id;
|
||||
/**
|
||||
* 提交id(nonce 可能会不存在,系统提示的时候,这个为空)
|
||||
*/
|
||||
private String nonce;
|
||||
/**
|
||||
* 现在已知:
|
||||
* 0:我们发送的消息,和指令
|
||||
@ -45,6 +49,14 @@ public class MidjourneyMessage {
|
||||
* {@link MidjourneyGennerateStatusEnum}
|
||||
*/
|
||||
private String generateStatus;
|
||||
/**
|
||||
* 一般用于提示信息
|
||||
* - 错误
|
||||
* - 并发队列满了
|
||||
* - 账号违规了、敏感词
|
||||
* - 账号被封
|
||||
*/
|
||||
private List<Embed> embeds;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@ -123,4 +135,39 @@ public class MidjourneyMessage {
|
||||
private String progress;
|
||||
private String status;
|
||||
}
|
||||
|
||||
/**
|
||||
* embed 用于警告、提示、错误
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public static class Embed {
|
||||
|
||||
// 内容扫描版本号
|
||||
private int contentScanVersion;
|
||||
|
||||
// 颜色值,这里用Java的Color类来表示,注意实际使用中可能需要自定义方法来从int转换为Color对象
|
||||
private String color;
|
||||
|
||||
// 页脚信息,包含文本
|
||||
private Footer footer;
|
||||
|
||||
// 描述信息
|
||||
private String description;
|
||||
|
||||
// 消息类型,这里是富文本类型(这个区分不同提示类型)
|
||||
private String type;
|
||||
|
||||
// 标题
|
||||
private String title;
|
||||
|
||||
// Footer类,作为嵌套类存在,用来表示footer部分的JSON对象
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public static class Footer {
|
||||
// 页脚文本
|
||||
private String text;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -38,11 +38,13 @@ public class MidjourneyInteractionsApi extends MidjourneyInteractions {
|
||||
this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions());
|
||||
}
|
||||
|
||||
public Boolean imagine(String prompt) {
|
||||
public Boolean imagine(Long id, String prompt) {
|
||||
String nonce = String.valueOf(id);
|
||||
// 获取请求模板
|
||||
String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine");
|
||||
// 设置参数
|
||||
HashMap<String, String> requestParams = getDefaultParams();
|
||||
requestParams.put("nonce", nonce);
|
||||
requestParams.put("prompt", prompt);
|
||||
// 解析 template 参数占位符
|
||||
String requestBody = MidjourneyUtil.parseTemplate(requestTemplate, requestParams);
|
||||
|
@ -6,6 +6,10 @@ public final class MidjourneyConstants {
|
||||
* 消息 - 编号
|
||||
*/
|
||||
public static final String MSG_ID = "id";
|
||||
/**
|
||||
* 用于区分操作唯一性
|
||||
*/
|
||||
public static final String MSG_NONCE = "nonce";
|
||||
/**
|
||||
* 消息 - 类型
|
||||
* 现在已知:
|
||||
@ -32,6 +36,10 @@ public final class MidjourneyConstants {
|
||||
* 附件(生成中比较模糊的图片)
|
||||
*/
|
||||
public static final String MSG_ATTACHMENTS = "attachments";
|
||||
/**
|
||||
* 一般用于提示
|
||||
*/
|
||||
public static final String MSG_EMBEDS = "embeds";
|
||||
|
||||
|
||||
//
|
||||
|
@ -42,12 +42,14 @@ public class MidjourneyMessageListener {
|
||||
if (ignoreAndLogMessage(data, messageType)) {
|
||||
return;
|
||||
}
|
||||
log.info("socket message: {}", raw);
|
||||
// 转换几个重要的信息
|
||||
MidjourneyMessage mjMessage = new MidjourneyMessage();
|
||||
mjMessage.setId(data.getString(MidjourneyConstants.MSG_ID));
|
||||
mjMessage.setId(getString(data, MidjourneyConstants.MSG_ID, ""));
|
||||
mjMessage.setNonce(getString(data, MidjourneyConstants.MSG_NONCE, ""));
|
||||
mjMessage.setType(data.getInt(MidjourneyConstants.MSG_TYPE));
|
||||
mjMessage.setRawData(StrUtil.str(raw.toJson(), "UTF-8"));
|
||||
mjMessage.setContent(MidjourneyUtil.parseContent(data.getString(MidjourneyConstants.MSG_CONTENT)));
|
||||
mjMessage.setContent(MidjourneyUtil.parseContent(data.getString(MidjourneyConstants.MSG_CONTENT)));
|
||||
// 转换 components
|
||||
if (!data.getArray(MidjourneyConstants.MSG_COMPONENTS).isEmpty()) {
|
||||
String componentsJson = StrUtil.str(data.getArray(MidjourneyConstants.MSG_COMPONENTS).toJson(), "UTF-8");
|
||||
@ -60,6 +62,12 @@ public class MidjourneyMessageListener {
|
||||
List<MidjourneyMessage.Attachment> attachments = JsonUtils.parseArray(attachmentsJson, MidjourneyMessage.Attachment.class);
|
||||
mjMessage.setAttachments(attachments);
|
||||
}
|
||||
// 转换 embeds 提示信息
|
||||
if (!data.getArray(MidjourneyConstants.MSG_EMBEDS).isEmpty()) {
|
||||
String embedJson = StrUtil.str(data.getArray(MidjourneyConstants.MSG_EMBEDS).toJson(), "UTF-8");
|
||||
List<MidjourneyMessage.Embed> embeds = JsonUtils.parseArray(embedJson, MidjourneyMessage.Embed.class);
|
||||
mjMessage.setEmbeds(embeds);
|
||||
}
|
||||
// 转换状态
|
||||
convertGenerateStatus(mjMessage);
|
||||
// message handler 调用
|
||||
@ -68,7 +76,20 @@ public class MidjourneyMessageListener {
|
||||
}
|
||||
}
|
||||
|
||||
private String getString(DataObject data, String key, String defaultValue) {
|
||||
if (!data.hasKey(key)) {
|
||||
return defaultValue;
|
||||
}
|
||||
return data.getString(key);
|
||||
}
|
||||
|
||||
private void convertGenerateStatus(MidjourneyMessage mjMessage) {
|
||||
//
|
||||
// tip:提示、警告、异常 content是没有内容的
|
||||
// tip: 一般错误信息在 Embeds 只要 Embeds有值,content就没信息。
|
||||
if (CollUtil.isNotEmpty(mjMessage.getEmbeds())) {
|
||||
return;
|
||||
}
|
||||
if (mjMessage.getType() == 20 && mjMessage.getContent().getStatus().contains("Waiting")) {
|
||||
mjMessage.setGenerateStatus(MidjourneyGennerateStatusEnum.WAITING.getStatus());
|
||||
} else if (mjMessage.getType() == 20 && !StrUtil.isBlank(mjMessage.getContent().getProgress())) {
|
||||
|
Reference in New Issue
Block a user