diff --git a/example/variation-ws.ts b/example/variation-ws.ts index 4ebac4c..9e473f1 100644 --- a/example/variation-ws.ts +++ b/example/variation-ws.ts @@ -16,7 +16,8 @@ async function main() { Ws: true, //enable ws is required for remix mode }); await client.init(); //init auto enable remix mode - const prompt = "the queen of the underworld, race"; + const prompt = + "48 year old woman with auburn hair plays video games on a tablet in her bedroom and is a chemist. Engaged. Happy. Evening. Silver blue walls in room. In the style of anime. does not exceed 10 MB."; const Imagine = await client.Imagine( prompt, (uri: string, progress: string) => { @@ -29,42 +30,55 @@ async function main() { return; } const Variation = await client.Variation({ - index: 2, + index: 1, msgId: Imagine.id, hash: Imagine.hash, flags: Imagine.flags, content: prompt, loading: (uri: string, progress: string) => { - console.log("Variation2.loading", uri, "progress", progress); + console.log("Variation1.loading", uri, "progress", progress); }, }); console.log("Variation", Variation); - // await client - // .Variation({ - // index: 2, - // msgId: Imagine.id, - // hash: Imagine.hash, - // flags: Imagine.flags, - // loading: (uri: string, progress: string) => { - // console.log("Variation2.loading", uri, "progress", progress); - // }, - // }) - // .then((msg2) => { - // console.log({ msg2 }); - // }); - // client - // .Variation({ - // index: 3, - // msgId: Imagine.id, - // hash: Imagine.hash, - // flags: Imagine.flags, - // loading: (uri: string, progress: string) => { - // console.log("Variation3.loading", uri, "progress", progress); - // }, - // }) - // .then((msg3) => { - // console.log({ msg3 }); - // }); + client + .Variation({ + index: 2, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("Variation2.loading", uri, "progress", progress); + }, + }) + .then((msg2) => { + console.log({ msg2 }); + }); + client + .Variation({ + index: 3, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("Variation3.loading", uri, "progress", progress); + }, + }) + .then((msg3) => { + console.log({ msg3 }); + }); + client + .Variation({ + index: 4, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("Variation4.loading", uri, "progress", progress); + }, + }) + .then((msg4) => { + console.log({ msg4 }); + }); } main().catch((err) => { console.error(err); diff --git a/package.json b/package.json index 9bbf970..8997014 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ }, "homepage": "https://github.com/erictik/midjourney-client#readme", "devDependencies": { + "@types/async": "^3.2.20", "@types/node": "^18.16.0", "@types/ws": "^8.5.4", "dotenv": "^16.0.3", @@ -44,10 +45,8 @@ }, "dependencies": { "@huggingface/inference": "^2.5.0", - "@types/async": "^3.2.20", "async": "^3.2.4", "isomorphic-ws": "^5.0.0", - "p-queue": "^6.6.2", "snowyflake": "^2.0.0", "tslib": "^2.5.0", "ws": "^8.13.0" diff --git a/src/discord.message.ts b/src/discord.message.ts index 0062b26..b0395fa 100644 --- a/src/discord.message.ts +++ b/src/discord.message.ts @@ -5,11 +5,10 @@ import { MJConfig, MJConfigParam, } from "./interfaces"; -import { CreateQueue } from "./queue"; import { formatOptions, sleep } from "./utls"; +import async from "async"; export class MidjourneyMessage { - private magApiQueue = CreateQueue(1); public config: MJConfig; constructor(defaults: MJConfigParam) { const { SalaiToken } = defaults; @@ -21,6 +20,38 @@ export class MidjourneyMessage { ...defaults, }; } + private safeRetrieveMessages = (request = 50) => { + return new Promise((resolve, reject) => { + this.queue.push( + { + request, + callback: (any: any) => { + resolve(any); + }, + }, + (error: any, result: any) => { + if (error) { + reject(error); + } else { + resolve(result); + } + } + ); + }); + }; + private processRequest = async ({ + request, + callback, + }: { + request: any; + callback: (any: any) => void; + }) => { + const httpStatus = await this.RetrieveMessages(request); + callback(httpStatus); + await sleep(this.config.ApiInterval); + }; + private queue = async.queue(this.processRequest, 1); + protected log(...args: any[]) { this.config.Debug && console.log(...args, new Date().toISOString()); } @@ -109,10 +140,6 @@ export class MidjourneyMessage { return null; } - // limit the number of concurrent interactions - protected async safeRetrieveMessages(limit = 50) { - return this.magApiQueue.addTask(() => this.RetrieveMessages(limit)); - } async RetrieveMessages(limit = this.config.Limit) { const headers = { "Content-Type": "application/json", diff --git a/src/interfaces/config.ts b/src/interfaces/config.ts index 3888dfb..232b4df 100644 --- a/src/interfaces/config.ts +++ b/src/interfaces/config.ts @@ -44,7 +44,7 @@ export const DefaultMJConfig: MJConfig = { BotId: MJBot, ChannelId: "1077800642086703114", SalaiToken: "", - ApiInterval: 950, + ApiInterval: 350, SessionId: "8bb7f5b79c7a49f7d0824ab4b8773a81", Debug: false, Limit: 50, diff --git a/src/midjourne.api.ts b/src/midjourne.api.ts index 97ee81f..a6344bb 100644 --- a/src/midjourne.api.ts +++ b/src/midjourne.api.ts @@ -9,16 +9,17 @@ import { UploadParam, UploadSlot, } from "./interfaces"; -import async from "async"; + import { nextNonce, sleep } from "./utls"; -import path from "path"; import { Command } from "./command"; +import async from "async"; +import path from "path"; + export class MidjourneyApi extends Command { UpId = Date.now() % 10; // upload id constructor(public config: MJConfig) { super(config); } - private safeIteractions = (request: any) => { return new Promise((resolve, reject) => { this.queue.push( @@ -49,6 +50,7 @@ export class MidjourneyApi extends Command { callback(httpStatus); await sleep(this.config.ApiInterval); }; + private queue = async.queue(this.processRequest, 1); private interactions = async (payload: any) => { try { const headers = { @@ -75,11 +77,12 @@ export class MidjourneyApi extends Command { return 500; } }; - private queue = async.queue(this.processRequest, 1); + async ImagineApi(prompt: string, nonce: string = nextNonce()) { const payload = await this.imaginePayload(prompt, nonce); return this.safeIteractions(payload); } + async SwitchRemixApi(nonce: string = nextNonce()) { const payload = await this.PreferPayload(nonce); return this.safeIteractions(payload); @@ -89,6 +92,7 @@ export class MidjourneyApi extends Command { const payload = await this.shortenPayload(prompt, nonce); return this.safeIteractions(payload); } + async VariationApi({ index, msgId, @@ -109,6 +113,7 @@ export class MidjourneyApi extends Command { nonce, }); } + async UpscaleApi({ index, msgId, @@ -129,6 +134,7 @@ export class MidjourneyApi extends Command { nonce, }); } + async RerollApi({ msgId, hash, @@ -177,6 +183,7 @@ export class MidjourneyApi extends Command { }; return this.safeIteractions(payload); } + //FIXME: get SubmitCustomId from discord api async ModalSubmitApi({ nonce, @@ -218,6 +225,7 @@ export class MidjourneyApi extends Command { console.log("submitCustomId", JSON.stringify(payload)); return this.safeIteractions(payload); } + async RemixApi({ nonce, msgId, @@ -237,6 +245,7 @@ export class MidjourneyApi extends Command { submitCustomId: RemixModalSubmitID, }); } + async ShortenImagineApi({ nonce, msgId, @@ -305,18 +314,22 @@ export class MidjourneyApi extends Command { const payload = await this.infoPayload(nonce); return this.safeIteractions(payload); } + async SettingsApi(nonce?: string) { const payload = await this.settingsPayload(nonce); return this.safeIteractions(payload); } + async FastApi(nonce?: string) { const payload = await this.fastPayload(nonce); return this.safeIteractions(payload); } + async RelaxApi(nonce?: string) { const payload = await this.relaxPayload(nonce); return this.safeIteractions(payload); } + /** * * @param fileUrl http file path @@ -345,6 +358,7 @@ export class MidjourneyApi extends Command { }; return resp; } + async UploadImageByBole(blob: Blob, filename = "image.png") { const fileData = await blob.arrayBuffer(); const mimeType = blob.type; @@ -395,6 +409,7 @@ export class MidjourneyApi extends Command { } ${await response.text()}`; throw new Error(error); } + private async uploadImage( slot: UploadSlot, data: ArrayBuffer, @@ -415,6 +430,7 @@ export class MidjourneyApi extends Command { ); } } + async DescribeApi(image: DiscordImage, nonce?: string) { const payload = await this.describePayload(image, nonce); return this.safeIteractions(payload); diff --git a/src/queue.ts b/src/queue.ts deleted file mode 100644 index f91f701..0000000 --- a/src/queue.ts +++ /dev/null @@ -1,17 +0,0 @@ -import PQueue from "p-queue"; - -class ConcurrentQueue { - private limit: any; - constructor(concurrency: number) { - this.limit = new PQueue({ concurrency }); - } - public async addTask(task: () => Promise): Promise { - return await this.limit.add(async () => { - const result = await task(); - return result; - }); - } -} -export function CreateQueue(concurrency: number) { - return new ConcurrentQueue(concurrency); -} diff --git a/yarn.lock b/yarn.lock index 0899b2b..d3ba752 100644 --- a/yarn.lock +++ b/yarn.lock @@ -271,11 +271,6 @@ esbuild@~0.17.6: "@esbuild/win32-ia32" "0.17.18" "@esbuild/win32-x64" "0.17.18" -eventemitter3@^4.0.4: - version "4.0.7" - resolved "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz" - integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw== - fsevents@~2.3.2: version "2.3.2" resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz" @@ -296,26 +291,6 @@ make-error@^1.1.1: resolved "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz" integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== -p-finally@^1.0.0: - version "1.0.0" - resolved "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz" - integrity sha512-LICb2p9CB7FS+0eR1oqWnHhp0FljGLZCWBE9aix0Uye9W8LTQPwMTYVGWQWIw9RdQiDg4+epXQODwIYJtSJaow== - -p-queue@^6.6.2: - version "6.6.2" - resolved "https://registry.npmjs.org/p-queue/-/p-queue-6.6.2.tgz" - integrity sha512-RwFpb72c/BhQLEXIZ5K2e+AhgNVmIejGlTgiB9MzZ0e93GRvqZ7uSi0dvRF7/XIXDeNkra2fNHBxTyPDGySpjQ== - dependencies: - eventemitter3 "^4.0.4" - p-timeout "^3.2.0" - -p-timeout@^3.2.0: - version "3.2.0" - resolved "https://registry.npmjs.org/p-timeout/-/p-timeout-3.2.0.tgz" - integrity sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg== - dependencies: - p-finally "^1.0.0" - prettier@^2.8.8: version "2.8.8" resolved "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz"