Skip to content

Commit

Permalink
add fix prefer-remix
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 3, 2023
1 parent 0010c93 commit 3fe4af6
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 74 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ npx tsx example/imagine-ws.ts
- [x] `/imagine` `variation` `upscale` `reroll` `blend` `zoomout` `vary`
- [x] `/info`
- [x] `/fast ` and `/relax `
- [x] [`/prefer remix`](https://github.com/erictik/midjourney-client/blob/main/example/prefer-remix.ts)
- [x] [`variation (remix mode)`](https://github.com/erictik/midjourney-client/blob/main/example/variation-ws.ts)
- [x] `/describe`
- [x] [`/shorten`](https://github.com/erictik/midjourney-client/blob/main/example/shorten.ts)
- [x] `/settings` `reset`
Expand Down
8 changes: 4 additions & 4 deletions example/options.ts → example/customzoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Midjourney } from "../src";
import { sleep } from "../src/utls";
/**
*
* a simple example of how to use the options with ws command
* a simple example of how to use the (custom zoom) options with ws command
* ```
* npx tsx example/options.ts
* npx tsx example/customzoom.ts
* ```
*/
async function main() {
Expand All @@ -14,9 +14,9 @@ async function main() {
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
Ws: true,
Ws: true, //enable ws is required for custom zoom
});
await client.Connect();
await client.init();
const prompt = "Christmas dinner with spaghetti with family in a cozy house, we see interior details , simple blue&white illustration"
const Imagine = await client.Imagine(prompt);
console.log(Imagine);
Expand Down
32 changes: 32 additions & 0 deletions example/prefer-remix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of using the prefer remix api
* ```
* npx tsx example/prefer-remix.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, //enable ws is required for prefer remix
});
await client.Connect();
const msg = await client.SwitchRemix();
console.log(msg);
client.Close();
}
main()
.then(() => {
console.log("finished");
process.exit(0);
})
.catch((err) => {
console.log("finished");
console.error(err);
process.exit(1);
});
10 changes: 5 additions & 5 deletions example/variation-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of how to use the Variation with ws command
* a simple example of how to use the Variation (remix mode) with ws command
* ```
* npx tsx example/variation-ws.ts
* ```
Expand All @@ -13,10 +13,10 @@ async function main() {
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
Ws: true,
Ws: true, //enable ws is required for remix mode
});
await client.init(); //init auto enable remix
const prompt = "the queen of the underworld, race"
await client.init(); //init auto enable remix mode
const prompt = "the queen of the underworld, race";
const Imagine = await client.Imagine(prompt);
console.log({ Imagine });
if (!Imagine) {
Expand All @@ -28,7 +28,7 @@ async function main() {
msgId: <string>Imagine.id,
hash: <string>Imagine.hash,
flags: Imagine.flags,
content:prompt,
content: prompt,
loading: (uri: string, progress: string) => {
console.log("Variation2.loading", uri, "progress", progress);
},
Expand Down
13 changes: 13 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DiscordImage, MJConfig } from "./interfaces";
type CommandName =
| "imagine"
| "shorten"
| "prefer"
| "describe"
| "info"
| "fast"
Expand All @@ -16,6 +17,7 @@ export class Command {
info: undefined,
fast: undefined,
relax: undefined,
prefer: undefined,
shorten: undefined,
settings: undefined,
};
Expand Down Expand Up @@ -56,6 +58,17 @@ export class Command {
]);
return this.data2Paylod(data, nonce);
}
async PreferPayload(prompt: string, nonce?: string) {
const data = await this.commandData("prefer", [
{
"type": 1,
"name": "remix",
"options": []
},
]);
return this.data2Paylod(data, nonce);
}


async shortenPayload(prompt: string, nonce?: string) {
const data = await this.commandData("shorten", [
Expand Down
84 changes: 22 additions & 62 deletions src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class WsMessage {
this.messageUpdate(message);
}
private messageUpdate(message: any) {
// this.log("messageUpdate",message);
const {
content,
embeds,
Expand All @@ -182,14 +183,19 @@ export class WsMessage {
options: formatOptions(components),
});
break;
case "prefer remix":
if (content != "") {
this.emit("prefer-remix", content);
}
break;
case "shorten":
const shorten : MJShorten = {
const shorten: MJShorten = {
description: embeds?.[0]?.description,
prompts: formatPrompts(embeds?.[0]?.description as string),
options: formatOptions(components),
id,
flags:message.flags,
}
flags: message.flags,
};
this.emitMJ(id, shorten);
break;
case "info":
Expand Down Expand Up @@ -222,14 +228,12 @@ export class WsMessage {
const { channel_id, author } = message;
if (!(author && author.id === this.MJBotId)) return;
if (channel_id !== this.config.ChannelId) return;
// this.log("messageCreate====", message);
this.messageCreate(message);
}
private async onMessageUpdate(message: any) {
const { channel_id, author } = message;
if (!(author && author.id === this.MJBotId)) return;
if (channel_id !== this.config.ChannelId) return;
// this.log("messageUpdate====", message);
this.messageUpdate(message);
}

Expand All @@ -252,12 +256,14 @@ export class WsMessage {
this.emitSystem("messageUpdate", message);
break;
case "INTERACTION_SUCCESS":
this.log("INTERACTION_SUCCESS", message);
this.emitSystem("interactionSuccess", message);
if (message.nonce) {
this.emitSystem("interactionSuccess", message);
}
break;
case "INTERACTION_CREATE":
this.log("INTERACTION_CREATE", message);
this.emitSystem("interactionCreate", message);
if (message.nonce) {
this.emitSystem("interactionCreate", message);
}
}
}
private async verifyHuman(message: any) {
Expand Down Expand Up @@ -473,59 +479,6 @@ export class WsMessage {
this.event.push({ event: nonce, callback: once });
}

// async waitImageMessage({
// nonce,
// prompt,
// onmodal,
// loading,
// }: {
// nonce: string;
// prompt?: string;
// onmodal?: OnModal;
// loading?: LoadingHandler;
// }) {
// return new Promise<MJMessage | null>((resolve, reject) => {
// this.waitMjEvents.set(nonce, {
// nonce,
// prompt,
// onmodal: async (nonce,id) => {
// if (!onmodal) return "";
// var newnonce = await onmodal(nonce,id)
// this.removeWaitMjEvent(nonce);
// this.onceImage(newnonce, ({ message, error }) => {
// if (error) {
// this.removeWaitMjEvent(newnonce);
// reject(error);
// return;
// }
// if (message && message.progress === "done") {
// this.removeWaitMjEvent(newnonce);
// resolve(message);
// return;
// }
// message && loading && loading(message.uri, message.progress || "");
// });
// return newnonce
// },
// });

// //FIXME
// this.onceImage(nonce, ({ message, error }) => {
// if (error) {
// this.removeWaitMjEvent(nonce);
// reject(error);
// return;
// }
// if (message && message.progress === "done") {
// this.removeWaitMjEvent(nonce);
// resolve(message);
// return;
// }
// message && loading && loading(message.uri, message.progress || "");
// });
// });
// }

async waitImageMessage({
nonce,
prompt,
Expand Down Expand Up @@ -590,6 +543,13 @@ export class WsMessage {
});
});
}
async waitContent(event: string) {
return new Promise<string | null>((resolve) => {
this.once(event, (message) => {
resolve(message);
});
});
}
async waitInfo() {
return new Promise<MJInfo | null>((resolve, reject) => {
this.onceInfo((message) => {
Expand Down
5 changes: 5 additions & 0 deletions src/midjourne.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export class MidjourneyApi extends Command {
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);
}

async ShortenApi(prompt: string, nonce: string = nextNonce()) {
const payload = await this.shortenPayload(prompt, nonce);
return this.safeIteractions(payload);
Expand Down
16 changes: 13 additions & 3 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ export class Midjourney extends MidjourneyMessage {
async Connect() {
//if auth failed, will throw error
await this.MJApi.cacheCommand("info");

if (!this.config.Ws) {
return this;
}
if (this.wsClient) return this;
return new Promise<Midjourney>((resolve) => {
this.wsClient = new WsMessage(this.config, this.MJApi);
this.wsClient.once("ready", (user) => {
//TODO: print user nickname
this.log(`🎊 ws ready!!! hi:${user.global_name}`);
//print user nickname
console.log(`🎊 ws ready!!! Hi: ${user.global_name}`);
resolve(this);
});
});
Expand Down Expand Up @@ -133,6 +132,17 @@ export class Midjourney extends MidjourneyMessage {
}
return null;
}
async SwitchRemix() {
const nonce = nextNonce();
const httpStatus = await this.MJApi.SwitchRemixApi(nonce);
if (httpStatus !== 204) {
throw new Error(`RelaxApi failed with status ${httpStatus}`);
}
if (this.wsClient) {
return this.wsClient.waitContent("prefer-remix");
}
return null;
}
async Describe(imgUri: string) {
const nonce = nextNonce();
const DcImage = await this.MJApi.UploadImage(imgUri);
Expand Down

0 comments on commit 3fe4af6

Please sign in to comment.