Skip to content

Commit

Permalink
Merge pull request #12 from imkratos/dev_features
Browse files Browse the repository at this point in the history
Dev features
  • Loading branch information
imkratos authored Nov 15, 2023
2 parents c34d56f + 6d4b788 commit d6f5814
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
| mj.ng-discord.server || https://discord.com 反代地址 |
| mj.ng-discord.cdn || https://cdn.discordapp.com 反代地址 |
| mj.ng-discord.wss || wss://gateway.discord.gg 反代地址 |
| mj.ng-discord.upload-server || https://discord-attachments-uploads-prd.storage.googleapis.com 反代地址 |
| mj.translate-way || 中文prompt翻译成英文的方式,可选null(默认)、baidu、gpt |
| mj.baidu-translate.appid || 百度翻译的appid |
| mj.baidu-translate.app-secret || 百度翻译的app-secret |
Expand Down
4 changes: 2 additions & 2 deletions docs/docker-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
docker run -d --name midjourney-proxy \
-p 8080:8080 \
-v /xxx/xxx/config:/home/spring/config \
kratos1937/midjourney-proxy:v1.0.7
kratos1937/midjourney-proxy:v1.1.0
```
3. 访问 `http://ip:port/mj` 查看API文档

Expand All @@ -17,5 +17,5 @@ docker run -d --name midjourney-proxy \
-e mj.discord.guild-id=xxx \
-e mj.discord.channel-id=xxx \
-e mj.discord.user-token=xxx \
kratos1937/midjourney-proxy:v1.0.8
kratos1937/midjourney-proxy:v1.1.0
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<hutool.version>5.8.18</hutool.version>
<org-json.version>20220924</org-json.version>
<jda.version>5.0.0-beta.9</jda.version>
<chatgpt-java.version>1.1.1-beta0</chatgpt-java.version>
<chatgpt-java.version>1.1.2-beta0</chatgpt-java.version>
<dataurl.version>2.0.0</dataurl.version>
<knife4j.verison>4.1.0</knife4j.verison>
<user-agent-utils.verison>1.21</user-agent-utils.verison>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ public static class NgDiscordConfig {
* wss://gateway.discord.gg 反代.
*/
private String wss;
/**
* https://discord-attachments-uploads-prd.storage.googleapis.com 反代.
*/
private String uploadServer;
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import com.github.novicezk.midjourney.domain.DiscordAccount;
import com.github.novicezk.midjourney.enums.TaskAction;
import com.github.novicezk.midjourney.result.Message;
import com.github.novicezk.midjourney.result.SubmitResultVO;
import com.github.novicezk.midjourney.service.DiscordService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ public class DiscordInstanceImpl implements DiscordInstance {
private final Map<String, Future<?>> taskFutureMap = Collections.synchronizedMap(new HashMap<>());

public DiscordInstanceImpl(DiscordAccount account, UserWebSocketStarter socketStarter, RestTemplate restTemplate,
TaskStoreService taskStoreService, NotifyService notifyService,
String discordServer, Map<String, String> paramsMap) {
TaskStoreService taskStoreService, NotifyService notifyService, Map<String, String> paramsMap) {
this.account = account;
this.socketStarter = socketStarter;
this.taskStoreService = taskStoreService;
this.notifyService = notifyService;
this.service = new DiscordServiceImpl(account, restTemplate, discordServer, paramsMap);
this.service = new DiscordServiceImpl(account, restTemplate, paramsMap);
this.runningTasks = new CopyOnWriteArrayList<>();
this.taskExecutor = new ThreadPoolTaskExecutor();
this.taskExecutor.setCorePoolSize(account.getCoreSize());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.github.novicezk.midjourney.loadbalancer.rule;

import cn.hutool.core.util.RandomUtil;
import com.github.novicezk.midjourney.loadbalancer.DiscordInstance;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 最少等待空闲.
* 选择等待数最少的实例,如果都不需要等待,则选择空闲数最多的实例
* 选择等待数最少的实例,如果都不需要等待,则随机选择
*/
public class BestWaitIdleRule implements IRule {

Expand All @@ -15,17 +19,13 @@ public DiscordInstance choose(List<DiscordInstance> instances) {
if (instances.isEmpty()) {
return null;
}
return instances.stream().min((i1, i2) -> {
int wait1 = i1.getRunningFutures().size() - i1.account().getCoreSize();
int wait2 = i2.getRunningFutures().size() - i2.account().getCoreSize();
if (wait1 == wait2 && wait1 == 0) {
// 都不需要等待时,选择空闲数最多的
int idle1 = i1.account().getCoreSize() - i1.getRunningTasks().size();
int idle2 = i2.account().getCoreSize() - i2.getRunningTasks().size();
return idle2 - idle1;
}
return wait1 - wait2;
}).orElse(null);
Map<Integer, List<DiscordInstance>> map = instances.stream()
.collect(Collectors.groupingBy(i -> {
int wait = i.getRunningFutures().size() - i.account().getCoreSize();
return wait >= 0 ? wait : -1;
}));
List<DiscordInstance> instanceList = map.entrySet().stream().min(Comparator.comparingInt(Map.Entry::getKey)).orElseThrow().getValue();
return RandomUtil.randomEle(instanceList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
import com.github.novicezk.midjourney.domain.DiscordAccount;
import com.github.novicezk.midjourney.enums.BlendDimensions;
import com.github.novicezk.midjourney.result.Message;
import com.github.novicezk.midjourney.support.DiscordHelper;
import com.github.novicezk.midjourney.support.SpringContextHolder;
import eu.maxschuster.dataurl.DataUrl;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;

Expand All @@ -24,21 +30,21 @@ public class DiscordServiceImpl implements DiscordService {
private final DiscordAccount account;
private final Map<String, String> paramsMap;
private final RestTemplate restTemplate;
private final DiscordHelper discordHelper;

private final String discordInteractionUrl;
private final String discordAttachmentUrl;
private final String discordMessageUrl;

private final String regionUrl;

public DiscordServiceImpl(DiscordAccount account, RestTemplate restTemplate, String discordServer, Map<String, String> paramsMap) {
public DiscordServiceImpl(DiscordAccount account, RestTemplate restTemplate, Map<String, String> paramsMap) {
this.account = account;
this.restTemplate = restTemplate;
this.discordHelper = SpringContextHolder.getApplicationContext().getBean(DiscordHelper.class);
this.paramsMap = paramsMap;
String discordServer = this.discordHelper.getServer();
this.discordInteractionUrl = discordServer + "/api/v9/interactions";
this.discordAttachmentUrl = discordServer + "/api/v9/channels/" + account.getChannelId() + "/attachments";
this.discordMessageUrl = discordServer + "/api/v9/channels/" + account.getChannelId() + "/messages";
this.regionUrl = "https://936929561302675456.discordsays.com/inpaint/api/submit-job";
}

@Override
Expand Down Expand Up @@ -116,7 +122,6 @@ public Message<Void> vary(String messageId, String messageHash, String nonce, St
.replace("$vary",vary)
.replace("$message_id", messageId)
.replace("$message_hash", messageHash);

return postJsonAndCheckStatus(paramsStr);
}

Expand Down Expand Up @@ -222,6 +227,7 @@ public Message<String> sendImageMessage(String content, String finalFileName) {
}

private void putFile(String uploadUrl, DataUrl dataUrl) {
uploadUrl = this.discordHelper.getDiscordUploadUrl(uploadUrl);
HttpHeaders headers = new HttpHeaders();
headers.add("User-Agent", this.account.getUserAgent());
headers.setContentType(MediaType.valueOf(dataUrl.getMimeType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public DiscordInstance createDiscordInstance(DiscordAccount account) {
var messageListener = new UserMessageListener(account, this.messageHandlers);
var webSocketStarter = new UserWebSocketStarter(this.discordHelper.getWss(), account, messageListener, this.properties.getProxy());
return new DiscordInstanceImpl(account, webSocketStarter, this.restTemplate,
this.taskStoreService, this.notifyService,
this.discordHelper.getServer(), this.paramsMap);
this.taskStoreService, this.notifyService, this.paramsMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class DiscordHelper {
* DISCORD_WSS_URL.
*/
public static final String DISCORD_WSS_URL = "wss://gateway.discord.gg";
/**
* DISCORD_UPLOAD_URL.
*/
public static final String DISCORD_UPLOAD_URL = "https://discord-attachments-uploads-prd.storage.googleapis.com";

public String getServer() {
if (CharSequenceUtil.isBlank(this.properties.getNgDiscord().getServer())) {
Expand Down Expand Up @@ -55,6 +59,17 @@ public String getWss() {
return wssUrl;
}

public String getDiscordUploadUrl(String uploadUrl) {
if (CharSequenceUtil.isBlank(this.properties.getNgDiscord().getUploadServer()) || CharSequenceUtil.isBlank(uploadUrl)) {
return uploadUrl;
}
String uploadServer = this.properties.getNgDiscord().getUploadServer();
if (uploadServer.endsWith("/")) {
uploadServer = uploadServer.substring(0, uploadServer.length() - 1);
}
return uploadUrl.replaceFirst(DISCORD_UPLOAD_URL, uploadServer);
}

public String findTaskIdWithCdnUrl(String url) {
if (!CharSequenceUtil.startWith(url, DISCORD_CDN_URL)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.novicezk.midjourney.support;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext APPLICATION_CONTEXT;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
APPLICATION_CONTEXT = applicationContext;
}

public static ApplicationContext getApplicationContext() {
if (APPLICATION_CONTEXT == null) {
throw new IllegalStateException("SpringContextHolder is not ready.");
}
return APPLICATION_CONTEXT;
}
}
32 changes: 32 additions & 0 deletions src/main/resources/api-params/list-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"type": 2,
"application_id": "936929561302675456",
"guild_id": "$guild_id",
"channel_id": "$channel_id",
"session_id": "12b3d9d8dbc0db4536a0fc4664ab7bad",
"data": {
"version": "1166847114609958943",
"id": "1000850743479255081",
"name": "settings",
"type": 1,
"options": [],
"application_command": {
"id": "1000850743479255081",
"application_id": "936929561302675456",
"version": "1166847114609958943",
"default_member_permissions": null,
"type": 1,
"nsfw": false,
"name": "settings",
"description": "View and adjust your personal settings.",
"dm_permission": true,
"contexts": null,
"integration_types": [
0
]
},
"attachments": []
},
"nonce": "$nonce",
"analytics_location": "slash_ui"
}
2 changes: 1 addition & 1 deletion src/main/resources/api-params/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"guild_id": "$guild_id",
"channel_id": "$channel_id",
"message_flags": 64,
"message_id": "1160405256170110986",
"message_id": "1174275851265781770",
"application_id": "936929561302675456",
"session_id": "$session_id",
"data": {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ ___ _(/ ___ __ __ _ __ __ _____/
.-/ .-/ .-/ / .-/
(_/ (_/ (_/ (_/

:: MidJourney Proxy :: v2.5.4
:: MidJourney Proxy :: v2.5.5
2 changes: 1 addition & 1 deletion src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ knife4j:
description: 代理 MidJourney 的discord频道,实现api形式调用AI绘图
concat: novicezk
url: https://github.com/novicezk/midjourney-proxy
version: v2.5.4
version: v2.5.5
terms-of-service-url: https://github.com/novicezk/midjourney-proxy
group:
api:
Expand Down

0 comments on commit d6f5814

Please sign in to comment.