diff --git a/.github/workflows/releases.yaml b/.github/workflows/releases.yaml index fd8d543..0ebf93d 100644 --- a/.github/workflows/releases.yaml +++ b/.github/workflows/releases.yaml @@ -1,6 +1,6 @@ name: Node.js Package env: - APPVERSION: v2.7.${{ github.run_number }} + APPVERSION: v3.0.${{ github.run_number }} on: workflow_dispatch: push: diff --git a/README.md b/README.md index b360cf4..9177faf 100644 --- a/README.md +++ b/README.md @@ -30,31 +30,31 @@ import { Midjourney } from "midjourney"; Debug: true, Ws:true, }); - await client.init(); + await client.Connect(); const Imagine = await client.Imagine("A little pink elephant", (uri: string, progress:string) => { onsole.log("Imagine", uri, "progress", progress); }); console.log({ Imagine }); - const Variation = await client.Variation( - Imagine.content, - 2, - Imagine.id, - Imagine.hash, - (uri: string, progress:string) => { - onsole.log("Imagine", uri, "progress", progress); - } - ); + const Variation = await client.Variation({ + index: 2, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("Variation.loading", uri, "progress", progress); + }, + }); console.log({ Variation }); - const Upscale = await client.Upscale( - Variation.content, - 2, - Variation.id, - Variation.hash, - (uri: string, progress: string) => { - console.log("Upscale", uri, "progress", progress); - } - ); + const Upscale = await client.Upscale({ + index: 2, + msgId: Variation.id, + hash: Variation.hash, + flags: Variation.flags, + loading: (uri: string, progress: string) => { + console.log("Upscale.loading", uri, "progress", progress); + }, + }); console.log({ Upscale }); ``` @@ -101,7 +101,7 @@ npx tsx example/imagine-ws.ts ``` ## route-map -- [x] `/imagine` `variation` `upscale` +- [x] `/imagine` `variation` `upscale` `reroll` - [x] websocket get message - [x] callback error - [x] verify human diff --git a/example/describe.ts b/example/describe.ts index 98402d1..c2eac20 100644 --- a/example/describe.ts +++ b/example/describe.ts @@ -15,7 +15,7 @@ async function main() { Debug: true, Ws: true, }); - await client.init(); + await client.Connect(); const msg = await client.Describe( "https://img.ohdat.io/midjourney-image/1b74cab8-70c9-474e-bfbb-093e9a3cfd5c/0_1.png" ); diff --git a/example/fast.ts b/example/fast.ts index aff4919..8cdbb40 100644 --- a/example/fast.ts +++ b/example/fast.ts @@ -15,7 +15,7 @@ async function main() { Debug: true, Ws: true, }); - await client.init(); + await client.Connect(); await client.Fast(); const msg = await client.Info(); console.log({ msg }); diff --git a/example/imagine-dm.ts b/example/imagine-dm.ts index bdc642b..796c46b 100644 --- a/example/imagine-dm.ts +++ b/example/imagine-dm.ts @@ -13,7 +13,7 @@ async function main() { Debug: true, Ws: true, }); - await client.init(); + await client.Connect(); const msg = await client.Imagine( "A little white dog", (uri: string, progress: string) => { diff --git a/example/imagine-err.ts b/example/imagine-err.ts index 7ef3f46..52789f6 100644 --- a/example/imagine-err.ts +++ b/example/imagine-err.ts @@ -16,7 +16,7 @@ async function main() { Ws: true, }); - await client.init(); + await client.Connect(); const msg = await client.Imagine( "https://edge-runtime.vercel.app/features/available-apis", (uri: string, progress: string) => { diff --git a/example/imagine-uri.ts b/example/imagine-uri.ts index 5f90630..aa910ed 100644 --- a/example/imagine-uri.ts +++ b/example/imagine-uri.ts @@ -25,15 +25,16 @@ async function main() { console.log("no message"); return; } - const msg2 = await client.Upscale( - msg.content, - 2, - msg.id, - msg.hash, - (uri: string, progress: string) => { + const msg2 = await client.Upscale({ + index: 2, + msgId: msg.id, + hash: msg.hash, + flags: msg.flags, + content: msg.content, + loading: (uri: string, progress: string) => { console.log("loading", uri, "progress", progress); - } - ); + }, + }); console.log({ msg2 }); } main().catch((err) => { diff --git a/example/imagine-ws-m.ts b/example/imagine-ws-m.ts index 4688856..cf8e063 100644 --- a/example/imagine-ws-m.ts +++ b/example/imagine-ws-m.ts @@ -14,9 +14,8 @@ async function main() { SalaiToken: process.env.SALAI_TOKEN, HuggingFaceToken: process.env.HUGGINGFACE_TOKEN, Debug: true, - Ws: true, }); - await client.init(); + await client.Connect(); client .Imagine("A little pink elephant", (uri) => { console.log("loading123---", uri); diff --git a/example/imagine-ws-proxy.ts b/example/imagine-ws-proxy.ts deleted file mode 100644 index 35ef7b0..0000000 --- a/example/imagine-ws-proxy.ts +++ /dev/null @@ -1,38 +0,0 @@ -import "dotenv/config"; -import { Midjourney } from "../src"; -/** - * - * a simple example of using the imagine api with ws and proxy - * ``` - * npx tsx example/imagine-ws-proxy.ts - * ``` - */ -async function main() { - const client = new Midjourney({ - ServerId: process.env.SERVER_ID, - ChannelId: process.env.CHANNEL_ID, - SalaiToken: process.env.SALAI_TOKEN, - ProxyUrl: process.env.PROXY_URL, - HuggingFaceToken: process.env.HUGGINGFACE_TOKEN, - Debug: true, - Ws: true, - }); - await client.init(); - const msg = await client.Imagine( - "A little white dog", - (uri: string, progress: string) => { - console.log("loading", uri, "progress", progress); - } - ); - console.log({ msg }); -} -main() - .then(() => { - console.log("finished"); - process.exit(0); - }) - .catch((err) => { - console.log("finished"); - console.error(err); - process.exit(1); - }); diff --git a/example/imagine-ws.ts b/example/imagine-ws.ts index c25c297..cdc0d87 100644 --- a/example/imagine-ws.ts +++ b/example/imagine-ws.ts @@ -16,7 +16,7 @@ async function main() { Debug: true, Ws: true, }); - await client.init(); + await client.Connect(); const Imagine = await client.Imagine( "Red hamster smoking a cigaret, https://media.discordapp.net/attachments/1108515696385720410/1118385339732590682/DanielH_A_giant_hamster_monster._Friendly_in_a_business_suit_si_d4be1836-a4e1-41a8-b1d7-99eebc521220.png?width=1878&height=1878 ", (uri: string, progress: string) => { @@ -27,30 +27,41 @@ async function main() { if (!Imagine) { return; } - const Variation = await client.Variation( - Imagine.content, - 2, - Imagine.id, - Imagine.hash, - (uri: string, progress: string) => { + const reroll = await client.Reroll({ + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("Reroll.loading", uri, "progress", progress); + }, + }); + console.log({ reroll }); + + const Variation = await client.Variation({ + index: 2, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { console.log("Variation.loading", uri, "progress", progress); - } - ); + }, + }); console.log({ Variation }); if (!Variation) { return; } - const Upscale = await client.Upscale( - Variation.content, - 2, - Variation.id, - Variation.hash, - (uri: string, progress: string) => { + const Upscale = await client.Upscale({ + index: 2, + msgId: Variation.id, + hash: Variation.hash, + flags: Variation.flags, + loading: (uri: string, progress: string) => { console.log("Upscale.loading", uri, "progress", progress); - } - ); + }, + }); console.log({ Upscale }); + client.Close(); } main() diff --git a/example/imagine.ts b/example/imagine.ts index bccf6c9..0a31c04 100644 --- a/example/imagine.ts +++ b/example/imagine.ts @@ -16,12 +16,12 @@ async function main() { }); const msg = await client.Imagine( - "Red hamster smoking a cigaret, https://media.discordapp.net/attachments/1108515696385720410/1118385339732590682/DanielH_A_giant_hamster_monster._Friendly_in_a_business_suit_si_d4be1836-a4e1-41a8-b1d7-99eebc521220.png?width=1878&height=1878", + "Red hamster", (uri: string, progress: string) => { console.log("loading", uri, "progress", progress); } ); - console.log({ msg }); + console.log(JSON.stringify(msg)); } main().catch((err) => { console.error(err); diff --git a/example/info.ts b/example/info.ts index 89e2a76..610f6e8 100644 --- a/example/info.ts +++ b/example/info.ts @@ -15,7 +15,7 @@ async function main() { Debug: true, Ws: true, }); - await client.init(); + await client.Connect(); const msg = await client.Info(); console.log({ msg }); client.Close(); diff --git a/example/relax.ts b/example/relax.ts index 1b89b15..330635a 100644 --- a/example/relax.ts +++ b/example/relax.ts @@ -15,7 +15,7 @@ async function main() { Debug: true, Ws: true, }); - await client.init(); + await client.Connect(); await client.Relax(); const msg = await client.Info(); console.log({ msg }); diff --git a/example/upscale-ws.ts b/example/upscale-ws.ts index 2d39440..ffc8f1d 100644 --- a/example/upscale-ws.ts +++ b/example/upscale-ws.ts @@ -15,23 +15,24 @@ async function main() { Debug: true, Ws: true, }); - await client.init(); + await client.Connect(); const msg = await client.Imagine("a cool cat, blue ears, yellow hat"); console.log({ msg }); if (!msg) { console.log("no message"); return; } - const msg2 = await client.Upscale( - msg.content, - 2, - msg.id, - msg.hash, - (uri: string, progress: string) => { + const msg2 = await client.Upscale({ + index: 2, + msgId: msg.id, + hash: msg.hash, + flags: msg.flags, + loading: (uri: string, progress: string) => { console.log("loading", uri, "progress", progress); - } - ); + }, + }); console.log({ msg2 }); + client.Close(); } main().catch((err) => { console.error(err); diff --git a/example/upscale.ts b/example/upscale.ts index 80e6142..113d8dd 100644 --- a/example/upscale.ts +++ b/example/upscale.ts @@ -20,15 +20,16 @@ async function main() { console.log("no message"); return; } - const msg2 = await client.Upscale( - msg.content, - 2, - msg.id, - msg.hash, - (uri: string, progress: string) => { + const msg2 = await client.Upscale({ + index: 2, + msgId: msg.id, + hash: msg.hash, + flags: msg.flags, + content: msg.content, + loading: (uri: string, progress: string) => { console.log("loading", uri, "progress", progress); - } - ); + }, + }); console.log({ msg2 }); } main().catch((err) => { diff --git a/example/variation-ws.ts b/example/variation-ws.ts index 06fab78..da5130e 100644 --- a/example/variation-ws.ts +++ b/example/variation-ws.ts @@ -15,36 +15,39 @@ async function main() { Debug: true, Ws: true, }); - await client.init(); - const msg = await client.Imagine("a dog, blue ears, and a red nose"); - console.log({ msg }); - if (!msg) { + await client.Connect(); + const Imagine = await client.Imagine( + "the queen of the underworld, race: vampire, appearance: delicate features with detailed portrayal, super exquisite facial features, silver long hair reaching ankles, silver pupils, fair skin with a hint of melancholy in the eyes, beautiful and noble, clothing: wearing a blood-red rose on the hair, skirt with layers of lace, sitting in a (pose), captured in ultra-high resolution, film-like realism, 8k for the best visual quality, super clear and finely drawn. --ar 9:16 --v 5" + ); + console.log({ Imagine }); + if (!Imagine) { console.log("no message"); return; } + client - .Variation( - msg.content, - 2, - msg.id, - msg.hash, - (uri: string) => { - console.log("loading2", uri); - } - ) + .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( - msg.content, - 3, - msg.id, - msg.hash, - (uri: string) => { - console.log("loading3", uri); - } - ) + .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 }); }); diff --git a/example/variation.ts b/example/variation.ts index 1e40d55..a3e497b 100644 --- a/example/variation.ts +++ b/example/variation.ts @@ -25,25 +25,27 @@ async function main() { console.log("no message"); return; } - const Variation = await client.Variation( - msg.content, - 1, - msg.id, - msg.hash, - (uri: string, progress: string) => { + const Variation = await client.Variation({ + index: 1, + msgId: msg.id, + hash: msg.hash, + flags: msg.flags, + content: msg.content, + loading: (uri: string, progress: string) => { console.log("Variation.loading", uri, "progress", progress); - } - ); + }, + }); console.log({ Variation }); - const Variation2 = await client.Variation( - msg.content, - 2, - msg.id, - msg.hash, - (uri: string, progress: string) => { + const Variation2 = await client.Variation({ + index: 2, + msgId: msg.id, + hash: msg.hash, + flags: msg.flags, + content: msg.content, + loading: (uri: string, progress: string) => { console.log("Variation2.loading", uri, "progress", progress); - } - ); + }, + }); console.log({ Variation2 }); } main().catch((err) => { diff --git a/package.json b/package.json index 95e5dec..c53475c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "midjourney", - "version": "2.7.0", + "version": "3.0.0", "description": "Node.js client for the unofficial MidJourney API.", "main": "libs/index.js", "types": "libs/index.d.ts", diff --git a/src/interfaces/config.ts b/src/interfaces/config.ts index cd073f0..fedf97d 100644 --- a/src/interfaces/config.ts +++ b/src/interfaces/config.ts @@ -40,6 +40,7 @@ export const DefaultMJConfig: MJConfig = { SessionId: "8bb7f5b79c7a49f7d0824ab4b8773a81", Debug: false, Limit: 50, + Ws: true, MaxWait: 200, DiscordBaseUrl: "https://discord.com", WsBaseUrl: "wss://gateway.discord.gg?v=9&encoding=json&compress=gzip-stream", diff --git a/src/interfaces/message.ts b/src/interfaces/message.ts index 75048e5..f165841 100644 --- a/src/interfaces/message.ts +++ b/src/interfaces/message.ts @@ -1,9 +1,11 @@ export interface MJMessage { uri: string; content: string; + flags: number; id?: string; hash?: string; progress?: string; + options?: MJOptions[]; } export type LoadingHandler = (uri: string, progress: string) => void; @@ -30,3 +32,9 @@ export interface MJInfo { queuedJobsRelax: string; runningJobs: string; } + +export interface MJOptions { + label: string; + type: string; + custom: string; +} diff --git a/src/midjourne.api.ts b/src/midjourne.api.ts index 0845eec..08bc4b0 100644 --- a/src/midjourne.api.ts +++ b/src/midjourne.api.ts @@ -4,9 +4,6 @@ import { nextNonce, sleep } from "./utls"; import * as fs from "fs"; import path from "path"; import * as mime from "mime"; -interface CustomRequestInit extends RequestInit { - agent?: any; -} export class MidjourneyApi { private apiQueue = CreateQueue(1); UpId = Date.now() % 10; // upload id @@ -97,61 +94,84 @@ export class MidjourneyApi { }; return this.safeIteractions(payload); } - async VariationApi( - index: number, - messageId: string, - messageHash: string, - nonce?: string - ) { - const payload = { - type: 3, - guild_id: this.config.ServerId, - channel_id: this.config.ChannelId, - message_flags: 0, - message_id: messageId, - application_id: "936929561302675456", - session_id: this.config.SessionId, - data: { - component_type: 2, - custom_id: `MJ::JOB::variation::${index}::${messageHash}`, - }, + async VariationApi({ + index, + msgId, + hash, + nonce = nextNonce(), + flags = 0, + }: { + index: 1 | 2 | 3 | 4; + msgId: string; + hash: string; + nonce?: string; + flags?: number; + }) { + return this.CustomApi({ + msgId, + customId: `MJ::JOB::variation::${index}::${hash}`, + flags, nonce, - }; - return this.safeIteractions(payload); + }); } - async UpscaleApi( - index: number, - messageId: string, - messageHash: string, - nonce?: string - ) { - const guild_id = this.config.ServerId; - const payload = { - type: 3, - guild_id, - channel_id: this.config.ChannelId, - message_flags: 0, - message_id: messageId, - application_id: "936929561302675456", - session_id: this.config.SessionId, - data: { - component_type: 2, - custom_id: `MJ::JOB::upsample::${index}::${messageHash}`, - }, + async UpscaleApi({ + index, + msgId, + hash, + nonce = nextNonce(), + flags, + }: { + index: 1 | 2 | 3 | 4; + msgId: string; + hash: string; + nonce?: string; + flags: number; + }) { + return this.CustomApi({ + msgId, + customId: `MJ::JOB::upsample::${index}::${hash}`, + flags, nonce, - }; - return this.safeIteractions(payload); + }); + } + async RerollApi({ + msgId, + hash, + nonce = nextNonce(), + flags, + }: { + msgId: string; + hash: string; + nonce?: string; + flags: number; + }) { + return this.CustomApi({ + msgId, + customId: `MJ::JOB::reroll::0::${hash}::SOLO`, + flags, + nonce, + }); } - async ClickBtnApi(messageId: string, customId: string, nonce?: string) { + async CustomApi({ + msgId: msgId, + customId, + flags, + nonce = nextNonce(), + }: { + msgId: string; + customId: string; + flags: number; + nonce?: string; + }) { const guild_id = this.config.ServerId; const payload = { type: 3, nonce, guild_id, channel_id: this.config.ChannelId, - message_flags: 0, - message_id: messageId, + message_flags: flags, + message_id: msgId, application_id: "936929561302675456", session_id: this.config.SessionId, data: { diff --git a/src/midjourney.message.ts b/src/midjourney.message.ts index 5b064f6..ff274e8 100644 --- a/src/midjourney.message.ts +++ b/src/midjourney.message.ts @@ -1,3 +1,4 @@ +import { format } from "path"; import { DefaultMJConfig, LoadingHandler, @@ -6,7 +7,7 @@ import { MJConfigParam, } from "./interfaces"; import { CreateQueue } from "./queue"; -import { sleep } from "./utls"; +import { formatOptions, sleep } from "./utls"; export class MidjourneyMessage { private magApiQueue = CreateQueue(1); @@ -27,9 +28,7 @@ export class MidjourneyMessage { async FilterMessages( timestamp: number, prompt: string, - loading?: LoadingHandler, - options?: string, - index?: number + loading?: LoadingHandler ) { const seed = prompt.match(/\[(.*?)\]/)?.[1]; this.log(`seed:`, seed); @@ -63,11 +62,13 @@ export class MidjourneyMessage { //finished const content = item.content.split("**")[1]; const msg: MJMessage = { + content, id: item.id, uri: imageUrl, + flags: item.flags, hash: this.UriToHash(imageUrl), - content: content, progress: "done", + options: formatOptions(item.components), }; return msg; } @@ -108,51 +109,6 @@ export class MidjourneyMessage { return null; } - async WaitOptionMessage( - content: string, - options: string, - loading?: LoadingHandler - ) { - 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 sleep(1000 * 2); - } - return null; - } - async WaitUpscaledMessage( - content: string, - index: number, - loading?: LoadingHandler - ) { - 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 sleep(1000 * 2); - } - return null; - } - // limit the number of concurrent interactions protected async safeRetrieveMessages(limit = 50) { return this.magApiQueue.addTask(() => this.RetrieveMessages(limit)); diff --git a/src/midjourney.ts b/src/midjourney.ts index 2f294d3..e49bc71 100644 --- a/src/midjourney.ts +++ b/src/midjourney.ts @@ -24,7 +24,7 @@ export class Midjourney extends MidjourneyMessage { }; this.MJApi = new MidjourneyApi(this.config); } - async init() { + async Connect() { if (!this.config.Ws) { return this; } @@ -37,6 +37,9 @@ export class Midjourney extends MidjourneyMessage { }); }); } + async init() { + return this.Connect(); + } async Imagine(prompt: string, loading?: LoadingHandler) { prompt = prompt.trim(); if (!this.wsClient) { @@ -102,60 +105,106 @@ export class Midjourney extends MidjourneyMessage { return null; } - async Variation( - content: string, - index: number, - msgId: string, - msgHash: string, - loading?: LoadingHandler - ) { - // index is 1-4 - if (index < 1 || index > 4) { - throw new Error(`Variation index must be between 1 and 4, got ${index}`); - } + async Variation({ + index, + msgId, + hash, + content, + flags, + loading, + }: { + index: 1 | 2 | 3 | 4; + msgId: string; + hash: string; + content?: string; + flags: number; + loading?: LoadingHandler; + }) { const nonce = nextNonce(); - const httpStatus = await this.MJApi.VariationApi( + const httpStatus = await this.MJApi.VariationApi({ index, msgId, - msgHash, - nonce - ); + hash, + flags, + nonce, + }); if (httpStatus !== 204) { throw new Error(`VariationApi failed with status ${httpStatus}`); } if (this.wsClient) { return await this.wsClient.waitImageMessage(nonce, loading); - } else { - return await this.WaitOptionMessage(content, `Variations`, loading); } + if (content === undefined || content === "") { + throw new Error(`content is required`); + } + return await this.WaitMessage(content, loading); } - async Upscale( - content: string, - index: number, - msgId: string, - msgHash: string, - loading?: LoadingHandler - ) { - // index is 1-4 - if (index < 1 || index > 4) { - throw new Error(`Variation index must be between 1 and 4, got ${index}`); - } + async Upscale({ + index, + msgId, + hash, + content, + flags, + loading, + }: { + index: 1 | 2 | 3 | 4; + msgId: string; + hash: string; + content?: string; + flags: number; + loading?: LoadingHandler; + }) { const nonce = nextNonce(); - const httpStatus = await this.MJApi.UpscaleApi( + const httpStatus = await this.MJApi.UpscaleApi({ index, msgId, - msgHash, - nonce - ); + hash, + flags, + nonce, + }); if (httpStatus !== 204) { - throw new Error(`VariationApi failed with status ${httpStatus}`); + throw new Error(`UpscaleApi failed with status ${httpStatus}`); + } + if (this.wsClient) { + return await this.wsClient.waitImageMessage(nonce, loading); + } + if (content === undefined || content === "") { + throw new Error(`content is required`); + } + return await this.WaitMessage(content, loading); + } + + async Reroll({ + msgId, + hash, + content, + flags, + loading, + }: { + msgId: string; + hash: string; + content?: string; + flags: number; + loading?: LoadingHandler; + }) { + const nonce = nextNonce(); + const httpStatus = await this.MJApi.RerollApi({ + msgId, + hash: hash, + flags, + nonce, + }); + if (httpStatus !== 204) { + throw new Error(`RerollApi failed with status ${httpStatus}`); } - this.log(`await generate image`); if (this.wsClient) { return await this.wsClient.waitImageMessage(nonce, loading); } - return await this.WaitUpscaledMessage(content, index, loading); + if (content === undefined || content === "") { + throw new Error(`content is required`); + } + return await this.WaitMessage(content, loading); } Close() { diff --git a/src/utls/index.ts b/src/utls/index.ts index feafa5b..040d43a 100644 --- a/src/utls/index.ts +++ b/src/utls/index.ts @@ -1,4 +1,5 @@ import { Snowyflake, Epoch } from "snowyflake"; +import { MJOptions } from "../interfaces"; export const sleep = async (ms: number): Promise => await new Promise((resolve) => setTimeout(resolve, ms)); @@ -6,7 +7,6 @@ export const sleep = async (ms: number): Promise => export const random = (min: number, max: number): number => Math.floor(Math.random() * (max - min) + min); -// const snowflake = new Snowflake(1); const snowflake = new Snowyflake({ workerId: 0n, processId: 0n, @@ -14,3 +14,20 @@ const snowflake = new Snowyflake({ }); export const nextNonce = (): string => snowflake.nextId().toString(); + +export const formatOptions = (components: any) => { + var data: MJOptions[] = []; + for (var i = 0; i < components.length; i++) { + const component = components[i]; + if (component.components && component.components.length > 0) { + const item = formatOptions(component.components); + data = data.concat(item); + } + data.push({ + type: component.type, + label: component.label || component.emoji?.name, + custom: component.custom_id, + }); + } + return data; +}; diff --git a/src/ws.message.ts b/src/ws.message.ts index 7f67c45..bfe1ce3 100644 --- a/src/ws.message.ts +++ b/src/ws.message.ts @@ -8,6 +8,7 @@ import { } from "./interfaces"; import { MidjourneyApi } from "./midjourne.api"; +import { formatOptions } from "./utls"; import { VerifyHuman } from "./verify.human"; import WebSocket from "isomorphic-ws"; export class WsMessage { @@ -156,7 +157,7 @@ export class WsMessage { this.processingImage(message); } private processingImage(message: any) { - const { content, id, attachments } = message; + const { content, id, attachments, flags } = message; const event = this.getEventById(id); if (!event) { return; @@ -169,6 +170,7 @@ export class WsMessage { const MJmsg: MJMessage = { uri: attachments[0].url, content: content, + flags: flags, progress: this.content2progress(content), }; const eventMsg: WsEventMsg = { @@ -207,7 +209,7 @@ export class WsMessage { this.log("HuggingFaceToken is empty"); return; } - const { embeds, components } = message; + const { embeds, components, id, flags } = message; const uri = embeds[0].image.url; const categories = components[0].components; const classify = categories.map((c: any) => c.label); @@ -217,9 +219,12 @@ export class WsMessage { const custom_id = categories.find( (c: any) => c.label === category ).custom_id; - const httpStatus = await this.MJApi.ClickBtnApi(custom_id, message.id); + const httpStatus = await this.MJApi.CustomApi({ + msgId: id, + customId: custom_id, + flags, + }); this.log("verifyHumanApi", httpStatus, custom_id, message.id); - // this.log("verify success", category); } } private EventError(id: string, error: Error) { @@ -234,13 +239,16 @@ export class WsMessage { } private done(message: any) { - const { content, id, attachments } = message; + const { content, id, attachments, components, flags } = message; + console.log("components=====", JSON.stringify(components)); const MJmsg: MJMessage = { id, + flags, + content, hash: this.uriToHash(attachments[0].url), progress: "done", uri: attachments[0].url, - content: content, + options: formatOptions(components), }; this.filterMessages(MJmsg); return;