Skip to content

Commit

Permalink
Merge pull request #179 from erictik:queue
Browse files Browse the repository at this point in the history
Queue
  • Loading branch information
zcpua authored Jul 8, 2023
2 parents 1461f11 + 9919fa1 commit dfbe5ab
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 172 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
5 changes: 3 additions & 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,10 @@
},
"dependencies": {
"@huggingface/inference": "^2.5.0",
"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"
}
}
}
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
25 changes: 14 additions & 11 deletions src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const NijiBot = "1022952195194359889";
export interface MJConfig {
ChannelId: string;
SalaiToken: string;
BotId: typeof MJBot | typeof NijiBot;
BotId: typeof MJBot | typeof NijiBot;
Debug: boolean;
Limit: number;
MaxWait: number;
Expand All @@ -19,29 +19,32 @@ export interface MJConfig {
DiscordBaseUrl: string;
WsBaseUrl: string;
fetch: FetchFn;
ApiInterval: number;
WebSocket: WebSocketCl;
}
export interface MJConfigParam {
SalaiToken: string;
ChannelId?: string;
BotId?: typeof MJBot | typeof NijiBot;
Debug?: boolean;
Limit?: number;
SalaiToken: string; //DISCORD_TOKEN
ChannelId?: string; //DISCORD_CHANNEL_ID
ServerId?: string; //DISCORD_SERVER_ID
BotId?: typeof MJBot | typeof NijiBot; //DISCORD_BOT_ID MJBot OR NijiBot
Debug?: boolean; // print log
ApiInterval?: number; //ApiInterval request api interval
Limit?: number; //Limit of get message list
MaxWait?: number;
Ws?: boolean;
HuggingFaceToken?: string;
ServerId?: string;
Ws?: boolean; //Ws:true use websocket get discord message (ephemeral message)
HuggingFaceToken?: string; //HuggingFaceToken for verify human
SessionId?: string;
DiscordBaseUrl?: string;
WsBaseUrl?: string;
fetch?: FetchFn;
WebSocket?: WebSocketCl;
fetch?: FetchFn; //Node.js<18 need node.fetch Or proxy
WebSocket?: WebSocketCl; //isomorphic-ws Or proxy
}

export const DefaultMJConfig: MJConfig = {
BotId: MJBot,
ChannelId: "1077800642086703114",
SalaiToken: "",
ApiInterval: 350,
SessionId: "8bb7f5b79c7a49f7d0824ab4b8773a81",
Debug: false,
Limit: 50,
Expand Down
Loading

0 comments on commit dfbe5ab

Please sign in to comment.