@suwujs/midjourney
Version:
Node.js client for the unofficial MidJourney API.
139 lines • 5.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MidjourneyMessage = void 0;
const interfaces_1 = require("./interfaces");
const queue_1 = require("./queue");
const utls_1 = require("./utls");
class MidjourneyMessage {
magApiQueue = (0, queue_1.CreateQueue)(1);
config;
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 !== "") {
}
}
log(...args) {
this.config.Debug && console.log(...args, new Date().toISOString());
}
async FilterMessages(timestamp, prompt, loading, options, index) {
const seed = prompt.match(/\[(.*?)\]/)?.[1];
this.log(`seed:`, seed);
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}`)) {
const itemTimestamp = new Date(item.timestamp).getTime();
if (itemTimestamp < timestamp) {
this.log("old message");
continue;
}
if (item.attachments.length === 0) {
this.log("no attachment");
break;
}
const imageUrl = item.attachments[0].url;
//waiting
if (item.attachments[0].filename.startsWith("grid") ||
item.components.length === 0) {
this.log(`content`, item.content);
const progress = this.content2progress(item.content);
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;
}
content2progress(content) {
const spcon = content.split("**");
if (spcon.length < 3) {
return "";
}
content = spcon[2];
const regex = /\(([^)]+)\)/; // matches the value inside the first parenthesis
const match = content.match(regex);
let progress = "";
if (match) {
progress = match[1];
}
return progress;
}
UriToHash(uri) {
return uri.split("_").pop()?.split(".")[0] ?? "";
}
async WaitMessage(prompt, loading, timestamp) {
timestamp = 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, timestamp) {
timestamp = 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, timestamp) {
timestamp = 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 response = await fetch(`${this.config.DiscordBaseUrl}/api/v10/channels/${this.config.ChannelId}/messages?limit=${limit}`, {
headers,
});
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