Skip to content

Commit

Permalink
del queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed Jul 8, 2023
1 parent bb65ab4 commit 9919fa1
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 84 deletions.
72 changes: 43 additions & 29 deletions example/variation-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -29,42 +30,55 @@ async function main() {
return;
}
const Variation = await client.Variation({
index: 2,
index: 1,
msgId: <string>Imagine.id,
hash: <string>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: <string>Imagine.id,
// hash: <string>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: <string>Imagine.id,
// hash: <string>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: <string>Imagine.id,
hash: <string>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: <string>Imagine.id,
hash: <string>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: <string>Imagine.id,
hash: <string>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);
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
39 changes: 33 additions & 6 deletions src/discord.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +20,38 @@ export class MidjourneyMessage {
...defaults,
};
}
private safeRetrieveMessages = (request = 50) => {
return new Promise<any>((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());
}
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const DefaultMJConfig: MJConfig = {
BotId: MJBot,
ChannelId: "1077800642086703114",
SalaiToken: "",
ApiInterval: 950,
ApiInterval: 350,
SessionId: "8bb7f5b79c7a49f7d0824ab4b8773a81",
Debug: false,
Limit: 50,
Expand Down
24 changes: 20 additions & 4 deletions src/midjourne.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>((resolve, reject) => {
this.queue.push(
Expand Down Expand Up @@ -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 = {
Expand All @@ -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);
Expand All @@ -89,6 +92,7 @@ export class MidjourneyApi extends Command {
const payload = await this.shortenPayload(prompt, nonce);
return this.safeIteractions(payload);
}

async VariationApi({
index,
msgId,
Expand All @@ -109,6 +113,7 @@ export class MidjourneyApi extends Command {
nonce,
});
}

async UpscaleApi({
index,
msgId,
Expand All @@ -129,6 +134,7 @@ export class MidjourneyApi extends Command {
nonce,
});
}

async RerollApi({
msgId,
hash,
Expand Down Expand Up @@ -177,6 +183,7 @@ export class MidjourneyApi extends Command {
};
return this.safeIteractions(payload);
}

//FIXME: get SubmitCustomId from discord api
async ModalSubmitApi({
nonce,
Expand Down Expand Up @@ -218,6 +225,7 @@ export class MidjourneyApi extends Command {
console.log("submitCustomId", JSON.stringify(payload));
return this.safeIteractions(payload);
}

async RemixApi({
nonce,
msgId,
Expand All @@ -237,6 +245,7 @@ export class MidjourneyApi extends Command {
submitCustomId: RemixModalSubmitID,
});
}

async ShortenImagineApi({
nonce,
msgId,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -395,6 +409,7 @@ export class MidjourneyApi extends Command {
} ${await response.text()}`;
throw new Error(error);
}

private async uploadImage(
slot: UploadSlot,
data: ArrayBuffer,
Expand All @@ -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);
Expand Down
17 changes: 0 additions & 17 deletions src/queue.ts

This file was deleted.

25 changes: 0 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 9919fa1

Please sign in to comment.