Skip to content

Commit

Permalink
zoomout
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed Jun 25, 2023
1 parent 987e2db commit f43eb7b
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 8 deletions.
54 changes: 54 additions & 0 deletions example/options.ts
Original file line number Diff line number Diff line change
@@ -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: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>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: <string>Imagine.id,
hash: <string>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: <string>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);
});
16 changes: 8 additions & 8 deletions example/upscale-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <string>msg.id,
hash: <string>msg.hash,
flags: msg.flags,
msgId: <string>Imagine.id,
hash: <string>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) => {
Expand Down
51 changes: 51 additions & 0 deletions example/zoomout.ts
Original file line number Diff line number Diff line change
@@ -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: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>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: <string>Imagine.id,
hash: <string>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: <string>Imagine.id,
hash: <string>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);
});
81 changes: 81 additions & 0 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f43eb7b

Please sign in to comment.