relax-mj
Version:
Node.js client for the unofficial MidJourney API.
147 lines • 5.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MidjourneyMessage = void 0;
const tslib_1 = require("tslib");
const interfaces_1 = require("./interfaces");
const queue_1 = require("./queue");
const utls_1 = require("./utls");
const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
const https_proxy_agent_1 = require("https-proxy-agent");
class MidjourneyMessage {
magApiQueue = (0, queue_1.CreateQueue)(1);
config;
agent;
constructor(defaults) {
const { SalaiToken } = defaults;
if (!SalaiToken) {
throw new Error("SalaiToken are required");
}
this.config = {
...interfaces_1.DefaultMJConfig,
...defaults,
};
if (this.config.ProxyUrl && this.config.ProxyUrl !== "") {
this.agent = new https_proxy_agent_1.HttpsProxyAgent(this.config.ProxyUrl);
}
}
log(...args) {
this.config.Debug && console.log(...args, new Date().toISOString());
}
async FilterMessages(timestamp, prompt, loading, options, index) {
const seed = prompt.match(/--seed (\d+)/)?.[1];
const data = await this.safeRetrieveMessages(this.config.Limit);
for (let i = 0; i < data.length; i++) {
const item = data[i];
if (item.author.id === "936929561302675456" &&
item.content.includes(`${seed}`)) {
this.log(JSON.stringify(item));
// Upscaled or Variation
if (item.timestamp < timestamp) {
this.log("old message");
continue;
}
if (options &&
!(item.content.includes(options) ||
(options === "Upscaled" && item.content.includes(`Image #${index}`)))) {
this.log("no options");
continue;
}
if (item.attachments.length === 0) {
this.log("no attachment");
break;
}
const imageUrl = item.attachments[0].url;
//waiting
this.log(`waiting.content`, item.content);
if (item.attachments[0].filename.startsWith("grid") ||
item.components.length === 0) {
this.log(`content`, item.content);
const regex = /\(([^)]+)\)/; // matches the value inside the first parenthesis
const match = item.content.match(regex);
let progress = "wait";
if (match) {
progress = match[1];
}
else {
this.log("No match found");
}
loading?.(imageUrl, progress);
break;
}
//finished
const content = item.content.split("**")[1];
const msg = {
id: item.id,
uri: imageUrl,
hash: this.UriToHash(imageUrl),
content: content,
progress: "done",
};
return msg;
}
}
return null;
}
UriToHash(uri) {
return uri.split("_").pop()?.split(".")[0] ?? "";
}
async WaitMessage(prompt, loading) {
var timestamp = Date.now();
for (let i = 0; i < this.config.MaxWait; i++) {
const msg = await this.FilterMessages(timestamp, prompt, loading);
if (msg !== null) {
return msg;
}
this.log(i, "wait no message found");
await (0, utls_1.sleep)(1000 * 2);
}
return null;
}
async WaitOptionMessage(content, options, loading) {
var timestamp = Date.now();
for (let i = 0; i < this.config.MaxWait; i++) {
const msg = await this.FilterMessages(timestamp, content, loading, options);
if (msg !== null) {
return msg;
}
this.log(i, content, "wait no message found");
await (0, utls_1.sleep)(1000 * 2);
}
return null;
}
async WaitUpscaledMessage(content, index, loading) {
var timestamp = Date.now();
for (let i = 0; i < this.config.MaxWait; i++) {
const msg = await this.FilterMessages(timestamp, content, loading, "Upscaled", index);
if (msg !== null) {
return msg;
}
this.log(i, content, "wait no message found");
await (0, utls_1.sleep)(1000 * 2);
}
return null;
}
// limit the number of concurrent interactions
async safeRetrieveMessages(limit = 50) {
return this.magApiQueue.addTask(() => this.RetrieveMessages(limit));
}
async RetrieveMessages(limit = this.config.Limit) {
const headers = {
"Content-Type": "application/json",
Authorization: this.config.SalaiToken,
};
const agent = this.agent;
const response = await (0, node_fetch_1.default)(`${this.config.DiscordBaseUrl}/api/v10/channels/${this.config.ChannelId}/messages?limit=${limit}`, {
headers,
agent,
});
if (!response.ok) {
this.log("error config", { config: this.config });
this.log(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
}
}
exports.MidjourneyMessage = MidjourneyMessage;
//# sourceMappingURL=midjourney.message.js.map