|
|
@@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.URLUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
@@ -70,13 +71,16 @@ import static com.yhlxj.web.wss.MidjourneyServerEndpoint.WSS_SESSION_MAP;
|
|
|
public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
private static final String RELAX_HOST = "http://172.17.0.1:8086";
|
|
|
private static final String RELAX_TOKEN = "chen188710";
|
|
|
- private static final Map<Long, ImgSize> TASK_IMG_SIZE_MAP = new ConcurrentHashMap<>();
|
|
|
+ private static final Map<String, ImgSize> TASK_IMG_SIZE_MAP = new ConcurrentHashMap<>();
|
|
|
|
|
|
private final MidjourneyUserConversationMapper conversationMapper;
|
|
|
|
|
|
private static final List<String> progress = List.of("35%","65%","100%");
|
|
|
|
|
|
private static final List<String> status = List.of("MODAL","CANCEL","FAILURE");
|
|
|
+ private static final List<String> status_list = List.of( "NOT_START", "SUBMITTED", "IN_PROGRESS", "FAILURE", "SUCCESS", "MODAL", "CANCEL" );
|
|
|
+ private static final List<String> action_list = List.of( "IMAGINE", "UPSCALE", "VARIATION", "REROLL", "DESCRIBE", "BLEND", "ACTION", "PAN", "OUTPAINT", "INPAINT", "ZOOM", "SHOW", "SHORTEN", "SWAP_FACE", "SWAP_VIDEO_FACE" );
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 需要切换账号或服务商进行重试的操作
|
|
|
@@ -162,9 +166,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
SubmitResult result = submit(user,mode,"imagine", null,imagineParam, false, num);
|
|
|
if (mode == 1){
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_FAST_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_FAST_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
}else {
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
}
|
|
|
saveConversation(user.getId(), mode,result,"IMAGINE", StringUtils.EMPTY, botType);
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
@@ -196,7 +200,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (result.getCode() == 23) {
|
|
|
return null;
|
|
|
}
|
|
|
- Long taskId = Long.parseLong(result.getResult());
|
|
|
+ String taskId = result.getResult();
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
MidjourneyUserConversation conversation = null;
|
|
|
if (action.equals("MODAL")){
|
|
|
@@ -212,16 +216,12 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
conversation.setStartTime(System.currentTimeMillis());
|
|
|
conversation.setProgress("0%");
|
|
|
conversation.setStatus(result.getCode() == 21 ? "MODAL" : "IN_PROGRESS");
|
|
|
- conversation.setTaskId(taskId);
|
|
|
}
|
|
|
if (MapUtil.isNotEmpty(properties)) {
|
|
|
conversation.setProperties(Jsons.toJson(properties));
|
|
|
conversation.setChannelId(properties.get("discordChannelId") == null ? null : Long.parseLong(properties.get("discordChannelId").toString()));
|
|
|
conversation.setInstanceId(properties.get("discordInstanceId") == null ? null : Long.parseLong(properties.get("discordInstanceId").toString()));
|
|
|
}
|
|
|
- if (conversation.getInstanceId() == null) {
|
|
|
- conversation.setInstanceId(result.getInstanceId());
|
|
|
- }
|
|
|
if (conversation.getId() == null) {
|
|
|
conversationMapper.insert(conversation);
|
|
|
}else {
|
|
|
@@ -239,7 +239,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.build();
|
|
|
SubmitResult result = submit(user,mode,"describe", null, param,false, num);
|
|
|
if (mode == 2) {
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
}
|
|
|
saveConversation(user.getId(), mode,result,"DESCRIBE", StringUtils.EMPTY, botType);
|
|
|
return result;
|
|
|
@@ -256,7 +256,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
SubmitResult result = submit(user,mode,"blend", null, param,false, num);
|
|
|
if (mode == 2) {
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
}
|
|
|
saveConversation(user.getId(), mode,result,"BLEND", StringUtils.EMPTY, botType);
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
@@ -275,7 +275,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SubmitResult submitModal(MidjourneyUser user,Integer mode, Long taskId, String prompt, String maskBase64, Long num) throws Exception {
|
|
|
+ public SubmitResult submitModal(MidjourneyUser user,Integer mode, String taskId, String prompt, String maskBase64, Long num) throws Exception {
|
|
|
MidjourneyUserConversation midjourneyUserConversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getTaskId, taskId).last("limit 1"));
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
.put("taskId", taskId.toString())
|
|
|
@@ -310,7 +310,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}else {
|
|
|
modeStr = "(relaxed)";
|
|
|
mjVersionStatusKey = "v6-relax-model";
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
}
|
|
|
properties.put("messageContent","**"+prompt+"** - <@mj>" + modeStr);
|
|
|
MjVersionStatus mjVersionStatus = MJ_VERSION_STATUS_MAP.get(mjVersionStatusKey);
|
|
|
@@ -336,14 +336,14 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.build();
|
|
|
SubmitResult result = submit(user,mode,"shorten", null, param,false, num);
|
|
|
if (mode == 2) {
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
}
|
|
|
saveConversation(user.getId(), mode, result,"SHORTEN", StringUtils.EMPTY, botType);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SubmitResult submitSeed(MidjourneyUser user, Integer mode,Long taskId, String botType) throws Exception {
|
|
|
+ public SubmitResult submitSeed(MidjourneyUser user, Integer mode,String taskId, String botType) throws Exception {
|
|
|
//SubmitResult result = seed(user.getMode(),"seed", taskId);
|
|
|
//return saveConversation(user.getId(), user.getMode(), result,"SEED", StringUtils.EMPTY, botType);
|
|
|
return null;
|
|
|
@@ -424,17 +424,13 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
// }
|
|
|
|
|
|
@Override
|
|
|
- public SubmitResult submitAction(MidjourneyUser user,MidjourneyUserConversation conversation,Integer mode, Long taskId, String customId, Long num, String botType) throws Exception {
|
|
|
- AtomicBoolean modalFlag = new AtomicBoolean(false);
|
|
|
+ public SubmitResult submitAction(MidjourneyUser user,MidjourneyUserConversation conversation,Integer mode, String taskId, String customId, Long num, String botType) throws Exception {
|
|
|
if (StringUtils.isNotBlank(conversation.getButtons())){
|
|
|
List<MessageButton> messageButtons = Jsons.parseList(conversation.getButtons(), MessageButton.class);
|
|
|
messageButtons.forEach(button -> {
|
|
|
if (button.getCustomId().equals(customId)) {
|
|
|
log.info("action customId:{}", customId);
|
|
|
button.setStyle(3);
|
|
|
- if (!"Vary (Region)".equals(button.getLabel()) && !"Custom Zoom".equals(button.getLabel()) && !customId.contains("PicReader")) {
|
|
|
- modalFlag.set(true);
|
|
|
- }
|
|
|
}
|
|
|
});
|
|
|
conversation.setButtons(Jsons.toJson(messageButtons));
|
|
|
@@ -455,7 +451,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
return null;
|
|
|
}
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("taskId", taskId.toString())
|
|
|
+ .put("taskId", taskId)
|
|
|
.put("state", user.getId().toString())
|
|
|
.put("customId", customId)
|
|
|
.build();
|
|
|
@@ -472,20 +468,14 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
properties.put("messageContent","**"+conversation.getPrompt()+"** - <@mj>" + modeStr);
|
|
|
if (result.getCode() == 21) {
|
|
|
- if (mode == 2 && modalFlag.get()){
|
|
|
- SubmitResult modal = submitModal(user, mode,Long.valueOf(result.getResult()), conversation.getPrompt(), null, num);
|
|
|
- log.info("action-modal:{}", modal);
|
|
|
- return modal;
|
|
|
- }else {
|
|
|
- // 以上操作有弹窗确认,恢复次数
|
|
|
- recoverUserLimit(user.getId(), mode,num);
|
|
|
- }
|
|
|
+ // 以上操作有弹窗确认,恢复次数
|
|
|
+ recoverUserLimit(user.getId(), mode,num);
|
|
|
} else if (result.getCode() == 23) {
|
|
|
return result;
|
|
|
} else {
|
|
|
//syncUser(user.getId(), user.getMode(),num);
|
|
|
if (!customId.contains("upsample") && mode == 2) {
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
}
|
|
|
}
|
|
|
MjVersionStatus mjVersionStatus = MJ_VERSION_STATUS_MAP.get(mjVersionStatusKey);
|
|
|
@@ -693,7 +683,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
}
|
|
|
@Override
|
|
|
- public List<MidjourneyUserConversation> listConversationByIds(Integer mode, List<Long> ids) {
|
|
|
+ public List<MidjourneyUserConversation> listConversationByIds(Integer mode, List<String> ids) {
|
|
|
List<MidjourneyUserConversation> list = new ArrayList<>();
|
|
|
ids.forEach(id ->{
|
|
|
String queryKey = RedisService.key.MIDJOURNEY_QUERY.getName();
|
|
|
@@ -707,7 +697,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
conversation.setImageUrl(conversation.getImageUrl().replace("cdn.discordapp.com", "mj.galaxydvd.com"));
|
|
|
}
|
|
|
conversation.setUserId(jsons.getLong("state"));
|
|
|
- conversation.setTaskId(jsons.getLong("id"));
|
|
|
+ conversation.setTaskId(jsons.getStr("id"));
|
|
|
list.add(conversation);
|
|
|
TASK_EXECUTOR.execute(() -> {
|
|
|
try {
|
|
|
@@ -737,7 +727,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation getConversationById(Long id){
|
|
|
+ public MidjourneyUserConversation getConversationById(String id){
|
|
|
|
|
|
//从缓存中查询 该任务是否存在
|
|
|
MidjourneyUserConversation conversation;
|
|
|
@@ -773,6 +763,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (conversation.getProgressNum() <= dbConversation.getProgressNum() && !conversation.getStatus().equals("FAILURE")){
|
|
|
return;
|
|
|
}
|
|
|
+ conversation.setOriginalImageUrl(conversation.getImageUrl());
|
|
|
+ conversation.setAction(dbConversation.getAction());
|
|
|
conversation.setId(dbConversation.getId());
|
|
|
conversationMapper.updateById(conversation);
|
|
|
//失败返还次数
|
|
|
@@ -897,7 +889,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
- public JSONArray listByIds(List<Long> ids) throws Exception {
|
|
|
+ public JSONArray listByIds(List<String> ids) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String, Object>()).put("ids", ids).build();
|
|
|
HttpRequest request = HttpRequest.post(RELAX_HOST + "/mj/task/list-by-condition").header("mj-api-secret", RELAX_TOKEN);
|
|
|
String body = request.body(Jsons.toJson(param)).execute().body();
|
|
|
@@ -937,8 +929,20 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
log.info("notifyHook:{}", json);
|
|
|
JSONObject jsons = JSONUtil.parseObj(json);
|
|
|
MidjourneyUserConversation conversation = JSONUtil.toBean(jsons, MidjourneyUserConversation.class);
|
|
|
+ if (NumberUtil.isNumber(conversation.getStatus())){
|
|
|
+ conversation.setStatus(status_list.get(jsons.getInt("status")-1));
|
|
|
+ }
|
|
|
+ if (NumberUtil.isNumber(conversation.getAction())){
|
|
|
+ conversation.setAction(action_list.get(jsons.getInt("action")));
|
|
|
+ }
|
|
|
+ if (conversation.getPrompt() == null){
|
|
|
+ conversation.setPrompt(jsons.getStr("promptEn"));
|
|
|
+ }
|
|
|
+ if (conversation.getPrompt() == null){
|
|
|
+ conversation.setPrompt(jsons.getStr("promptFull"));
|
|
|
+ }
|
|
|
conversation.setUserId(jsons.getLong("state"));
|
|
|
- conversation.setTaskId(jsons.getLong("id"));
|
|
|
+ conversation.setTaskId(jsons.getStr("id"));
|
|
|
conversation.setId(null);
|
|
|
JSONObject jsonObject = new JSONObject(conversation.getProperties());
|
|
|
String content = jsonObject.getStr("messageContent");
|
|
|
@@ -1130,6 +1134,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
private void sendProgressToClient( MidjourneyUserConversation conversation) {
|
|
|
+ log.info("sendProgressToClient: {}", conversation);
|
|
|
//WebSocket 直接发送 任务状态
|
|
|
WssSession wssSession = WSS_SESSION_MAP.get(conversation.getUserId());
|
|
|
if (wssSession != null) {
|