From f43eb7b3df09fbd792b1f9919fab9c661015e398 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 25 Jun 2023 19:43:23 +0800 Subject: [PATCH] zoomout --- example/options.ts | 54 +++++++++++++++++++++++++++++ example/upscale-ws.ts | 16 ++++----- example/zoomout.ts | 51 +++++++++++++++++++++++++++ src/midjourney.ts | 81 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 194 insertions(+), 8 deletions(-) create mode 100644 example/options.ts create mode 100644 example/zoomout.ts diff --git a/example/options.ts b/example/options.ts new file mode 100644 index 0000000..b952dc4 --- /dev/null +++ b/example/options.ts @@ -0,0 +1,54 @@ +import "dotenv/config"; +import { Midjourney } from "../src"; +/** + * + * a simple example of how to use the options with ws command + * ``` + * npx tsx example/options.ts + * ``` + */ +async function main() { + const client = new Midjourney({ + ServerId: process.env.SERVER_ID, + ChannelId: process.env.CHANNEL_ID, + SalaiToken: process.env.SALAI_TOKEN, + Debug: true, + Ws: true, + }); + await client.Connect(); + const Imagine = await client.Imagine("a cool cat, blue ears, yellow hat"); + console.log(Imagine); + if (!Imagine) { + console.log("no message"); + return; + } + const Upscale = await client.Upscale({ + index: 2, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("loading", uri, "progress", progress); + }, + }); + const zoomout = Upscale?.options?.find((o) => o.label === "Zoom Out 2x"); + if (!zoomout) { + console.log("no zoomout"); + return; + } + const zoomout2x = client.Custom({ + msgId: Imagine.id, + flags: Imagine.flags, + customId: zoomout.custom, + loading: (uri: string, progress: string) => { + console.log("loading", uri, "progress", progress); + }, + }); + console.log("zoomout2x", zoomout2x); + + client.Close(); +} +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/example/upscale-ws.ts b/example/upscale-ws.ts index ffc8f1d..3977e7b 100644 --- a/example/upscale-ws.ts +++ b/example/upscale-ws.ts @@ -16,22 +16,22 @@ async function main() { Ws: true, }); await client.Connect(); - const msg = await client.Imagine("a cool cat, blue ears, yellow hat"); - console.log({ msg }); - if (!msg) { + const Imagine = await client.Imagine("a cool cat, blue ears, yellow hat"); + console.log(Imagine); + if (!Imagine) { console.log("no message"); return; } - const msg2 = await client.Upscale({ + const Upscale = await client.Upscale({ index: 2, - msgId: msg.id, - hash: msg.hash, - flags: msg.flags, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, loading: (uri: string, progress: string) => { console.log("loading", uri, "progress", progress); }, }); - console.log({ msg2 }); + console.log(Upscale); client.Close(); } main().catch((err) => { diff --git a/example/zoomout.ts b/example/zoomout.ts new file mode 100644 index 0000000..6bd21b2 --- /dev/null +++ b/example/zoomout.ts @@ -0,0 +1,51 @@ +import "dotenv/config"; +import { Midjourney } from "../src"; +/** + * + * a simple example of how to use the zoomout with ws command + * ``` + * npx tsx example/zoomout.ts + * ``` + */ +async function main() { + const client = new Midjourney({ + ServerId: process.env.SERVER_ID, + ChannelId: process.env.CHANNEL_ID, + SalaiToken: process.env.SALAI_TOKEN, + Debug: true, + Ws: true, + }); + await client.Connect(); + const Imagine = await client.Imagine("a cool cat, blue ears, yellow hat"); + console.log(Imagine); + if (!Imagine) { + console.log("no message"); + return; + } + const Upscale = await client.Upscale({ + index: 2, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("loading", uri, "progress", progress); + }, + }); + console.log(Upscale); + const Zoomout = await client.ZoomOut({ + level: "2x", + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("loading", uri, "progress", progress); + }, + }); + console.log(Zoomout); + + client.Close(); +} +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/src/midjourney.ts b/src/midjourney.ts index 7a244d3..11f4013 100644 --- a/src/midjourney.ts +++ b/src/midjourney.ts @@ -205,6 +205,87 @@ export class Midjourney extends MidjourneyMessage { return await this.WaitMessage(content, loading); } + async Custom({ + msgId, + customId, + content, + flags, + loading, + }: { + msgId: string; + customId: string; + content?: string; + flags: number; + loading?: LoadingHandler; + }) { + const nonce = nextNonce(); + const httpStatus = await this.MJApi.CustomApi({ + msgId, + customId, + flags, + nonce, + }); + if (httpStatus !== 204) { + throw new Error(`CustomApi 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 ZoomOut({ + level, + msgId, + hash, + content, + flags, + loading, + }: { + level: "high" | "low" | "2x" | "1.5x"; + msgId: string; + hash: string; + content?: string; + flags: number; + loading?: LoadingHandler; + }) { + const nonce = nextNonce(); + let customId: string; + switch (level) { + case "high": + customId = `MJ::JOB::high_variation::1::${hash}::SOLO`; + break; + case "low": + customId = `MJ::JOB::low_variation::1::${hash}::SOLO`; + break; + case "2x": + customId = `MJ::Outpaint::50::1::${hash}::SOLO`; + break; + case "1.5x": + customId = `MJ::Outpaint::75::1::${hash}::SOLO`; + break; + } + const httpStatus = await this.MJApi.CustomApi({ + msgId, + customId, + flags, + nonce, + }); + if (httpStatus !== 204) { + 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,