|
|
@@ -3,8 +3,8 @@ package com.yhlxj.service.midjourney.impl;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.core.io.FileUtil;
|
|
|
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;
|
|
|
@@ -16,7 +16,6 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
-import com.cyksj.common.util.IoKit;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.common.util.QueryWrapperUtils;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
@@ -50,9 +49,7 @@ import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
-import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
-import java.nio.charset.Charset;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.util.*;
|
|
|
@@ -72,19 +69,18 @@ import static com.yhlxj.web.wss.MidjourneyServerEndpoint.WSS_SESSION_MAP;
|
|
|
@Slf4j
|
|
|
@RequiredArgsConstructor
|
|
|
public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
- private static final String RELAX_HOST = "http://172.19.0.17:8080";
|
|
|
- private static final String FAST_HOST = "https://api.bltcy.ai";
|
|
|
- private static final String FAST_TOKEN = "Bearer sk-YTg6N3QH6PLTFLbM9291B1Ae12754bC4B26225F1B95f442c";
|
|
|
-
|
|
|
- private static final String RELAX_TOKEN = "5a9a3e9b-5a5e-4b6b-9a5a-9a5a5a5a5a5a";
|
|
|
-
|
|
|
- private static final Map<Long, ImgSize> TASK_IMG_SIZE_MAP = new ConcurrentHashMap<>();
|
|
|
+ private static final String RELAX_HOST = "http://172.17.0.1:8086";
|
|
|
+ private static final String RELAX_TOKEN = "chen188710";
|
|
|
+ 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" );
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 需要切换账号或服务商进行重试的操作
|
|
|
@@ -168,11 +164,13 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (CollectionUtil.isNotEmpty(base64Array)) {
|
|
|
imagineParam.put("base64Array", base64Array);
|
|
|
}
|
|
|
- SubmitResult result = submit(user,mode,"imagine", null,imagineParam, null,false, num);
|
|
|
- if (mode == 2) {
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ SubmitResult result = submit(user,mode,"imagine", null,imagineParam, false, num);
|
|
|
+ if (mode == 1){
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_FAST_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
+ }else {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getProperties().get("discordInstanceId").toString(), 1.0);
|
|
|
}
|
|
|
- saveConversation(user.getId(), mode,result,"IMAGINE", StringUtils.EMPTY, botType,result.getApiChannelId());
|
|
|
+ saveConversation(user.getId(), mode,result,"IMAGINE", StringUtils.EMPTY, botType);
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
String modeStr;
|
|
|
String mjVersionStatusKey;
|
|
|
@@ -198,11 +196,11 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public MidjourneyUserConversation saveConversation(Long userId,Integer mode,SubmitResult result, String action,String prompt, String botType, Long apiChannelId) throws Exception {
|
|
|
+ public MidjourneyUserConversation saveConversation(Long userId,Integer mode,SubmitResult result, String action,String prompt,String botType) throws Exception {
|
|
|
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")){
|
|
|
@@ -218,19 +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(apiChannelId != null && mode == 1){
|
|
|
- conversation.setApiChannelId(apiChannelId);
|
|
|
- }
|
|
|
if (conversation.getId() == null) {
|
|
|
conversationMapper.insert(conversation);
|
|
|
}else {
|
|
|
@@ -246,11 +237,11 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("state", user.getId().toString())
|
|
|
.put("base64", base64)
|
|
|
.build();
|
|
|
- SubmitResult result = submit(user,mode,"describe", null, param, null,false, num);
|
|
|
+ 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, result.getApiChannelId());
|
|
|
+ saveConversation(user.getId(), mode,result,"DESCRIBE", StringUtils.EMPTY, botType);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@@ -263,11 +254,11 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (dimensions != null) {
|
|
|
param.put("dimensions", dimensions);
|
|
|
}
|
|
|
- SubmitResult result = submit(user,mode,"blend", null, param, null,false, num);
|
|
|
+ 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, result.getApiChannelId());
|
|
|
+ saveConversation(user.getId(), mode,result,"BLEND", StringUtils.EMPTY, botType);
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
MjVersionStatus mjVersionStatus = MJ_VERSION_STATUS_MAP.get(mode == 1 ? "v6-fast-model" : "v6-relax-model");
|
|
|
int count = 1;
|
|
|
@@ -284,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())
|
|
|
@@ -296,8 +287,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (StringUtils.isNotBlank(maskBase64)) {
|
|
|
param.put("maskBase64", maskBase64);
|
|
|
}
|
|
|
- Long apiChannelId = midjourneyUserConversation == null ? null : midjourneyUserConversation.getApiChannelId();
|
|
|
- JSONArray jsonArray = listByIds(mode, List.of(taskId), apiChannelId);
|
|
|
+ JSONArray jsonArray = listByIds(List.of(taskId));
|
|
|
if (!jsonArray.isEmpty()){
|
|
|
JSONObject jsonObject = JSONUtil.parseObj(Jsons.toJson(jsonArray.get(0)));
|
|
|
if (!"MODAL".equals(jsonObject.getStr("status"))){
|
|
|
@@ -309,8 +299,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
return submitModal(user, mode, taskId, prompt, maskBase64, num);
|
|
|
}
|
|
|
}
|
|
|
- SubmitResult result = submit(user,mode,"modal", null, param, apiChannelId,false, num);
|
|
|
- saveConversation(user.getId(), mode,result,"MODAL", StringUtils.EMPTY, StringUtils.EMPTY,apiChannelId);
|
|
|
+ SubmitResult result = submit(user,mode,"modal", null, param,false, num);
|
|
|
+ saveConversation(user.getId(), mode,result,"MODAL", StringUtils.EMPTY, StringUtils.EMPTY);
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
String modeStr;
|
|
|
String mjVersionStatusKey;
|
|
|
@@ -320,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);
|
|
|
@@ -344,16 +334,16 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("prompt", prompt)
|
|
|
.put("state", user.getId().toString())
|
|
|
.build();
|
|
|
- SubmitResult result = submit(user,mode,"shorten", null, param, null,false, num);
|
|
|
+ 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, result.getApiChannelId());
|
|
|
+ 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;
|
|
|
@@ -434,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, Long apiChannelId) 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));
|
|
|
@@ -465,11 +451,11 @@ 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();
|
|
|
- SubmitResult result = submit(user,mode,"action", conversation.getInstanceId(),param, apiChannelId,false, num);
|
|
|
+ SubmitResult result = submit(user,mode,"action", conversation.getInstanceId(),param,false, num);
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
String modeStr;
|
|
|
String mjVersionStatusKey;
|
|
|
@@ -482,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);
|
|
|
@@ -510,98 +490,48 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
properties.put("estimateTime", System.currentTimeMillis() + (second * count));
|
|
|
properties.put("finalPrompt",conversation.getPrompt());
|
|
|
- MidjourneyUserConversation midjourneyUserConversation = saveConversation(user.getId(), mode, result, "ACTION", StringUtils.EMPTY, botType, apiChannelId);
|
|
|
+ MidjourneyUserConversation midjourneyUserConversation = saveConversation(user.getId(), mode, result, "ACTION", StringUtils.EMPTY, botType);
|
|
|
properties.put("id",midjourneyUserConversation.getId());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
- public SubmitResult submit(MidjourneyUser user, Integer mode, String action, Long instanceId, Map<String, Object> param, Long apiChannelId,Boolean again, Long num) throws Exception {
|
|
|
-
|
|
|
- String accountWithMinUsage = "";
|
|
|
- String url = "";
|
|
|
- String token = "";
|
|
|
- if (mode == 1) {
|
|
|
- param.put("mode", "FAST");
|
|
|
-
|
|
|
- MidjourneyApiChannel midjourneyApiChannel = midjourneyApiChannelMapper.selectOne(QueryWrapperUtils.buildWrapper((wrapper)->{
|
|
|
- if(apiChannelId != null && apiChannelId != 0){
|
|
|
- wrapper.eq(MidjourneyApiChannel::getId,apiChannelId);
|
|
|
- }else {
|
|
|
- wrapper.eq(MidjourneyApiChannel::getIsDefault, true);
|
|
|
- }
|
|
|
- }));
|
|
|
- if (midjourneyApiChannel == null) {
|
|
|
- throw BusinessRuntimeException.getInstance("FAST模式账号异常");
|
|
|
- }
|
|
|
-
|
|
|
- url = midjourneyApiChannel.getUrl();
|
|
|
- token = midjourneyApiChannel.getToken();
|
|
|
-
|
|
|
- } else if (mode == 2) {
|
|
|
+ public SubmitResult submit(MidjourneyUser user, Integer mode, String action, Long instanceId, Map<String, Object> param,Boolean again, Long num) throws Exception {
|
|
|
+ //查询在使用次数最少的账号
|
|
|
+// String accountWithMinUsage = getAccountWithMinUsage(mode,instanceId);
|
|
|
+// if (accountWithMinUsage == null) {
|
|
|
+// throw new Exception("没有可用的账号");
|
|
|
+// }
|
|
|
+ param.put("accountFilter",MapUtil.builder(new HashMap<String,Object>())
|
|
|
+ //.put("instanceId",accountWithMinUsage).build());
|
|
|
+ .put("modes",List.of(mode == 1 ? "FAST" : "RELAX")).build());
|
|
|
+ if (mode == 2) {
|
|
|
synchronized (user.getId()){
|
|
|
userSubmitLimit(user,action,param.get("customId"),again);
|
|
|
}
|
|
|
- url = RELAX_HOST;
|
|
|
- token = RELAX_TOKEN;
|
|
|
- //慢速查询在使用次数最少的账号
|
|
|
- accountWithMinUsage = getAccountWithMinUsage(instanceId);
|
|
|
- if (accountWithMinUsage == null) {
|
|
|
- throw new Exception("没有可用的账号");
|
|
|
- }
|
|
|
- param.put("accountFilter",MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("instanceId",accountWithMinUsage).build());
|
|
|
}
|
|
|
- SubmitResult submitResult = submit2MjProxy(url, token, mode, action, param);
|
|
|
+ SubmitResult submitResult = submit2MjProxy(mode, action, param);
|
|
|
+ //submitResult.setInstanceId(Long.valueOf(accountWithMinUsage));
|
|
|
int code = submitResult.getCode();
|
|
|
- if (StringUtils.isNotBlank(accountWithMinUsage) && mode == 2) {
|
|
|
- submitResult.setInstanceId(Long.valueOf(accountWithMinUsage));
|
|
|
- }
|
|
|
if (code == 23){
|
|
|
recoverUserLimit(user.getId(), mode, num);
|
|
|
return submitResult;
|
|
|
}
|
|
|
if (code != 1 && code != 21 && code != 22) {
|
|
|
- submitResult = checkResult(user, mode, action, instanceId, param, code, accountWithMinUsage, submitResult, num);
|
|
|
+ submitResult = checkResult(user, mode, action, instanceId, param, code, submitResult, num);
|
|
|
}
|
|
|
return submitResult;
|
|
|
}
|
|
|
|
|
|
- private SubmitResult checkResult(MidjourneyUser user, Integer mode, String action, Long instanceId, Map<String, Object> param, int code, String accountWithMinUsage, SubmitResult submitResult, Long num) throws Exception {
|
|
|
+ private SubmitResult checkResult(MidjourneyUser user, Integer mode, String action, Long instanceId, Map<String, Object> param, int code, SubmitResult submitResult, Long num) throws Exception {
|
|
|
if (code == 3) {
|
|
|
- if(mode == 2){
|
|
|
- redisService.hdel(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), accountWithMinUsage);
|
|
|
- TASK_EXECUTOR.execute(() -> {
|
|
|
- MidjourneyAccount account = midjourneyAccountService.getOne(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getInstanceId, accountWithMinUsage).last("limit 1"));
|
|
|
- midjourneyAccountService.updateStatus(account.getId());
|
|
|
- });
|
|
|
- if(RETRY_ACTION.contains(action)){
|
|
|
- return submit(user,mode, action, instanceId, param, null,true, num);
|
|
|
- }
|
|
|
- } else if(mode == 1) {
|
|
|
- // 重试逻辑,切换供应商域名
|
|
|
- if(RETRY_ACTION.contains(action)){
|
|
|
- List<MidjourneyApiChannel> midjourneyApiChannels = midjourneyApiChannelMapper.selectList(QueryWrapperUtils.buildWrapper((wrapper) -> {
|
|
|
- wrapper.eq(MidjourneyApiChannel::getIsDefault, false);
|
|
|
- }));
|
|
|
- for (MidjourneyApiChannel midjourneyApiChannel : midjourneyApiChannels) {
|
|
|
- submitResult = submit2MjProxy(midjourneyApiChannel.getUrl(), midjourneyApiChannel.getToken(), mode, action, param);
|
|
|
- if (submitResult.getCode() == 1 || submitResult.getCode() == 22){
|
|
|
- log.info("taskId:{},切换到:{}", submitResult.getResult(),midjourneyApiChannel.getName());
|
|
|
- submitResult.setApiChannelId(midjourneyApiChannel.getId());
|
|
|
- return submitResult;
|
|
|
- }else {
|
|
|
- if(submitResult.getCode() == 3){
|
|
|
- log.info("服务商 :{}, 账号短缺", midjourneyApiChannel.getName());
|
|
|
- throw BusinessRuntimeException.getInstance("该任务所属的源Mj账号已被官网封禁或暂时风控。请稍后再试,或重新生成图片");
|
|
|
- }else {
|
|
|
- log.error("服务商:{}, action:{}, error message:{}", midjourneyApiChannel.getName(), action, submitResult.getDescription());
|
|
|
- throw BusinessRuntimeException.getInstance("该任务源Mj账号队列已满,请稍后重试");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+// redisService.hdel(mode == 1 ? RedisService.key.MIDJOURNEY_FAST_ACCOUNT.getName() : RedisService.key.MIDJOURNEY_ACCOUNT.getName(), accountWithMinUsage);
|
|
|
+// TASK_EXECUTOR.execute(() -> {
|
|
|
+// MidjourneyAccount account = midjourneyAccountService.getOne(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getInstanceId, accountWithMinUsage).last("limit 1"));
|
|
|
+// midjourneyAccountService.updateStatus(account.getId());
|
|
|
+// });
|
|
|
+ if(RETRY_ACTION.contains(action)){
|
|
|
+ return submit(user,mode, action, instanceId, param,true, num);
|
|
|
}
|
|
|
}
|
|
|
if (code == 4) {
|
|
|
@@ -609,32 +539,24 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}else if (code == 24) {
|
|
|
Map<String, Object> properties = submitResult.getProperties();
|
|
|
throw BusinessRuntimeException.getInstance("prompt包含敏感词:"+properties.get("bannedWord"));
|
|
|
- }
|
|
|
- else if (code == 3) {
|
|
|
+ }else if (code == 3) {
|
|
|
log.error("action:" + action + " error message:" + submitResult.getDescription());
|
|
|
throw BusinessRuntimeException.getInstance("该任务所属的源Mj账号已被官网封禁或暂时风控,请重新生成图片");
|
|
|
- }
|
|
|
- else {
|
|
|
+ }else {
|
|
|
log.error("action:" + action + " error message:" + submitResult.getDescription());
|
|
|
throw BusinessRuntimeException.getInstance("网络出现异常");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private SubmitResult submit2MjProxy(String url, String token, Integer mode, String action, Map<String, Object> param) throws Exception {
|
|
|
-
|
|
|
- url = url + getActionUrl(action);
|
|
|
+ private SubmitResult submit2MjProxy(Integer mode, String action, Map<String, Object> param) throws Exception {
|
|
|
String host = midjourneyHost;
|
|
|
if (EnvCommonService.active_prd.equals(envCommonService.getEnv())){
|
|
|
- host = host + "/8082/api/applets/midjourney/notifyHook";
|
|
|
- if (mode == 2) {
|
|
|
- host = "http://172.19.0.15:8082/applets/midjourney/notifyHook";
|
|
|
- }
|
|
|
+ host = "http://172.17.0.1:8082/applets/midjourney/notifyHook";
|
|
|
}else {
|
|
|
host = host + "/8083/api/applets/midjourney/notifyHook";
|
|
|
}
|
|
|
param.put("notifyHook",host);
|
|
|
- HttpRequest request = HttpRequest.post(url).body(Jsons.toJson(param));
|
|
|
- request = mode == 1 ? request.header("Authorization", token) : request.header("mj-api-secret", token);
|
|
|
+ HttpRequest request = HttpRequest.post(RELAX_HOST + getActionUrl(action)).header("mj-api-secret", RELAX_TOKEN).body(Jsons.toJson(param));
|
|
|
String body = request.execute().body();
|
|
|
log.info("action body:{}", body);
|
|
|
return Jsons.parseObject(body, SubmitResult.class);
|
|
|
@@ -711,14 +633,14 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public String getAccountWithMinUsage(Long instanceId) {
|
|
|
- RedisService.key key = RedisService.key.MIDJOURNEY_ACCOUNT;
|
|
|
+ public String getAccountWithMinUsage(Integer mode,Long instanceId) {
|
|
|
+ RedisService.key key = mode == 1 ? RedisService.key.MIDJOURNEY_FAST_ACCOUNT : RedisService.key.MIDJOURNEY_ACCOUNT;
|
|
|
try {
|
|
|
// 获取所有账号ID和使用次数
|
|
|
Map<Object, Object> accountsUsage = redisService.hmget(key.getName());
|
|
|
|
|
|
if (accountsUsage == null || accountsUsage.isEmpty()) {
|
|
|
- accountsUsage = midjourneyAccountService.list(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getStatus,Boolean.TRUE)).stream().collect(Collectors.toMap(k -> k.getInstanceId().toString(), v -> 0));
|
|
|
+ accountsUsage = midjourneyAccountService.list(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getStatus,Boolean.TRUE).eq(MidjourneyAccount::getMode,mode)).stream().collect(Collectors.toMap(k -> k.getInstanceId().toString(), v -> 0));
|
|
|
// 保存所有账号ID和使用次数
|
|
|
redisService.hmset(key.getName(), accountsUsage,key.getTimeout());
|
|
|
}
|
|
|
@@ -761,21 +683,21 @@ 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();
|
|
|
Long count = redisService.incr(queryKey + id, 1L);
|
|
|
if (count%5 == 0) {
|
|
|
try {
|
|
|
- listByIds(mode,List.of(id),null).forEach(json ->{
|
|
|
+ listByIds(List.of(id)).forEach(json ->{
|
|
|
JSONObject jsons = JSONUtil.parseObj(json);
|
|
|
MidjourneyUserConversation conversation = JSONUtil.toBean(jsons, MidjourneyUserConversation.class);
|
|
|
if (StringUtil.isNotBlank(conversation.getImageUrl())) {
|
|
|
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 {
|
|
|
@@ -805,7 +727,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation getConversationById(Long id){
|
|
|
+ public MidjourneyUserConversation getConversationById(String id){
|
|
|
|
|
|
//从缓存中查询 该任务是否存在
|
|
|
MidjourneyUserConversation conversation;
|
|
|
@@ -841,12 +763,15 @@ 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);
|
|
|
//失败返还次数
|
|
|
if ("FAILURE".equals(conversation.getStatus()) && !dbConversation.getStatus().equals("MODEL")){
|
|
|
+ Integer mode = dbConversation.getMode();
|
|
|
if (StringUtils.equals("未知频道",conversation.getFailReason())) {
|
|
|
- redisService.hdel(RedisService.key.MIDJOURNEY_ACCOUNT.getName(),dbConversation.getInstanceId().toString());
|
|
|
+ redisService.hdel(mode == 1 ? RedisService.key.MIDJOURNEY_FAST_ACCOUNT.getName() : RedisService.key.MIDJOURNEY_ACCOUNT.getName(),dbConversation.getInstanceId().toString());
|
|
|
String finalAccountWithMinUsage = dbConversation.getInstanceId().toString();
|
|
|
TASK_EXECUTOR.execute(() -> {
|
|
|
MidjourneyAccount account = midjourneyAccountService.getOne(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getInstanceId, finalAccountWithMinUsage).last("limit 1"));
|
|
|
@@ -858,7 +783,6 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
Object num = redisService.get(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + userId);
|
|
|
LambdaUpdateWrapper<MidjourneyUser> wrapper = Wrappers.lambdaUpdate(MidjourneyUser.class)
|
|
|
.eq(MidjourneyUser::getId, userId);
|
|
|
- Integer mode = dbConversation.getMode();
|
|
|
if (mode == 1){
|
|
|
num = redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + userId, 1L);
|
|
|
wrapper.set(MidjourneyUser::getMjFastNum, num);
|
|
|
@@ -965,31 +889,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
- public JSONArray listByIds(Integer mode, List<Long> ids, Long apiChannelId) throws Exception {
|
|
|
+ public JSONArray listByIds(List<String> ids) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String, Object>()).put("ids", ids).build();
|
|
|
- String url = "";
|
|
|
- String token = "";
|
|
|
- if (mode == 1) {
|
|
|
- MidjourneyApiChannel midjourneyApiChannel = midjourneyApiChannelMapper.selectOne(QueryWrapperUtils.buildWrapper((wrapper)->{
|
|
|
- if(apiChannelId != null && apiChannelId != 0){
|
|
|
- wrapper.eq(MidjourneyApiChannel::getId,apiChannelId);
|
|
|
- }else {
|
|
|
- wrapper.eq(MidjourneyApiChannel::getIsDefault, true);
|
|
|
- }
|
|
|
- }));
|
|
|
- if (midjourneyApiChannel == null) {
|
|
|
- throw BusinessRuntimeException.getInstance("FAST模式账号异常");
|
|
|
- }
|
|
|
-
|
|
|
- url = midjourneyApiChannel.getUrl();
|
|
|
- token = midjourneyApiChannel.getToken();
|
|
|
-
|
|
|
- } else if (mode == 2) {
|
|
|
- url = RELAX_HOST;
|
|
|
- token = RELAX_TOKEN;
|
|
|
- }
|
|
|
- HttpRequest request = HttpRequest.post(url + "/mj/task/list-by-condition");
|
|
|
- request = mode == 1 ? request.header("Authorization", token) : request.header("mj-api-secret", token);
|
|
|
+ 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();
|
|
|
log.info("listByIds body:{}", body);
|
|
|
return JSONUtil.parseArray(body);
|
|
|
@@ -1013,8 +915,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
//});
|
|
|
}
|
|
|
|
|
|
- public void cancel(Integer mode,Long taskId) {
|
|
|
- String body = HttpUtil.post(mode == 1 ? FAST_HOST : RELAX_HOST + "/mj/task/"+taskId+"/cancel",MapUtil.empty());
|
|
|
+ public void cancel(Long taskId) {
|
|
|
+ String body = HttpUtil.post(RELAX_HOST + "/mj/task/"+taskId+"/cancel",MapUtil.empty());
|
|
|
log.info("cancel body:{}", body);
|
|
|
JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
if (jsonObject.getInt("code") != 1) {
|
|
|
@@ -1027,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");
|
|
|
@@ -1220,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) {
|
|
|
@@ -1285,27 +1200,10 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
@Override
|
|
|
public synchronized void syscConversation() throws Exception {
|
|
|
List<MidjourneyUserConversation> fastMidjourneyUserConversations = conversationMapper.selectList(Wrappers.lambdaQuery(MidjourneyUserConversation.class)
|
|
|
- .notIn(MidjourneyUserConversation::getStatus, List.of("SUCCESS","MODAL","FAILURE")).eq(MidjourneyUserConversation::getMode,1).orderByAsc(MidjourneyUserConversation::getId).last(" limit 100"));
|
|
|
-
|
|
|
- List<MidjourneyUserConversation> relaxMidjourneyUserConversations = conversationMapper.selectList(Wrappers.lambdaQuery(MidjourneyUserConversation.class)
|
|
|
- .notIn(MidjourneyUserConversation::getStatus, List.of("SUCCESS","MODAL","FAILURE")).eq(MidjourneyUserConversation::getMode,2).orderByAsc(MidjourneyUserConversation::getId).last(" limit 100"));
|
|
|
-
|
|
|
- Map<Long, List<MidjourneyUserConversation>> fastCollect = fastMidjourneyUserConversations.stream().collect(Collectors.groupingBy(MidjourneyUserConversation::getApiChannelId));
|
|
|
-
|
|
|
- fastCollect.forEach((channel,list)->{
|
|
|
- try {
|
|
|
- JSONArray jsonArray = listByIds(1, list.stream().map(MidjourneyUserConversation::getTaskId).collect(Collectors.toList()), channel);
|
|
|
- for (Object json : jsonArray) {
|
|
|
- log.info("定时任务同步回调notifyHook");
|
|
|
- notifyHook(Jsons.toJson(json));
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- });
|
|
|
+ .notIn(MidjourneyUserConversation::getStatus, List.of("SUCCESS","MODAL","FAILURE")).orderByAsc(MidjourneyUserConversation::getId).last(" limit 100"));
|
|
|
|
|
|
- JSONArray relaxArray = listByIds(2, relaxMidjourneyUserConversations.stream().map(MidjourneyUserConversation::getTaskId).collect(Collectors.toList()), null);
|
|
|
- for (Object json : relaxArray) {
|
|
|
+ JSONArray jsonArray = listByIds(fastMidjourneyUserConversations.stream().map(MidjourneyUserConversation::getTaskId).collect(Collectors.toList()));
|
|
|
+ for (Object json : jsonArray) {
|
|
|
log.info("定时任务同步回调notifyHook");
|
|
|
notifyHook(Jsons.toJson(json));
|
|
|
}
|