Skip to content

Commit

Permalink
Merge pull request #165 from erictik:shorten
Browse files Browse the repository at this point in the history
add shorten
  • Loading branch information
zcpua authored Jun 29, 2023
2 parents 7ab1a5b + ae1e464 commit 856966f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ npx tsx example/imagine-ws.ts
- [x] `/info`
- [x] `/fast ` and `/relax `
- [x] `/describe`
- [x] [`/shorten`](https://github.com/erictik/midjourney-client/blob/main/example/shorten.ts)
- [x] `/settings` `reset`
- [x] verify human
- [x] [proxy](https://github.com/erictik/midjourney-discord/blob/main/examples/proxy.ts)
Expand Down
29 changes: 29 additions & 0 deletions example/shorten.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of using the shorten api
* ```
* npx tsx example/shorten.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 Shorten = await client.Shorten(
"Peeking out from the bushes, masterpiece, octane rendering, focus, realistic photography, colorful background, detailed, intricate details, rich colors, realistic style"
);
console.log(Shorten);
client.Close();
}
main().catch((err) => {
console.log("finished");
console.error(err);
process.exit(1);
});
13 changes: 13 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DiscordImage, MJConfig } from "./interfaces";

type CommandName =
| "imagine"
| "shorten"
| "describe"
| "info"
| "fast"
Expand All @@ -15,6 +16,7 @@ export class Command {
info: undefined,
fast: undefined,
relax: undefined,
shorten: undefined,
settings: undefined,
};

Expand Down Expand Up @@ -63,6 +65,17 @@ export class Command {
]);
return this.data2Paylod(data, nonce);
}

async shortenPayload(prompt: string, nonce?: string) {
const data = await this.commandData("shorten", [
{
type: 3,
name: "prompt",
value: prompt,
},
]);
return this.data2Paylod(data, nonce);
}
async infoPayload(nonce?: string) {
const data = await this.commandData("info");
return this.data2Paylod(data, nonce);
Expand Down
4 changes: 4 additions & 0 deletions src/midjourne.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class MidjourneyApi extends Command {
const payload = await this.imaginePayload(prompt, nonce);
return this.safeIteractions(payload);
}
async ShortenApi(prompt: string, nonce: string = nextNonce()) {
const payload = await this.shortenPayload(prompt, nonce);
return this.safeIteractions(payload);
}
async VariationApi({
index,
msgId,
Expand Down
12 changes: 12 additions & 0 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ export class Midjourney extends MidjourneyMessage {
return null;
}

async Shorten(prompt: string) {
const nonce = nextNonce();
const httpStatus = await this.MJApi.ShortenApi(prompt, nonce);
if (httpStatus !== 204) {
throw new Error(`ShortenApi failed with status ${httpStatus}`);
}
if (this.wsClient) {
return this.wsClient.waitShorten(nonce);
}
return null;
}

async Variation({
index,
msgId,
Expand Down
11 changes: 11 additions & 0 deletions src/utls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const snowflake = new Snowyflake({

export const nextNonce = (): string => snowflake.nextId().toString();

export const formatPrompts = (prompts: string) => {
const regex = /(\d️⃣ .+)/g;
const matches = prompts.match(regex);
if (matches) {
const shortenedPrompts = matches.map((match) => match.trim());
return shortenedPrompts;
} else {
console.log("No matches found.");
}
};

export const formatOptions = (components: any) => {
var data: MJOptions[] = [];
for (var i = 0; i < components.length; i++) {
Expand Down
28 changes: 23 additions & 5 deletions src/ws.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "./interfaces";

import { MidjourneyApi } from "./midjourne.api";
import { formatOptions } from "./utls";
import { formatOptions, formatPrompts } from "./utls";
import { VerifyHuman } from "./verify.human";
import WebSocket from "isomorphic-ws";
export class WsMessage {
Expand Down Expand Up @@ -155,11 +155,18 @@ export class WsMessage {
this.emit("settings", message);
return;
case "describe":
this.emitDescribe(id, {
this.emitMJ(id, {
descriptions: embeds?.[0]?.description.split("\n\n"),
options: formatOptions(components),
});
break;
case "shorten":
this.emitMJ(id, {
description: embeds?.[0]?.description,
prompts: formatPrompts(embeds?.[0]?.description as string),
options: formatOptions(components),
});
break;
case "info":
this.emit("info", embeds?.[0]?.description);
return;
Expand Down Expand Up @@ -346,7 +353,7 @@ export class WsMessage {
private emitImage(type: string, message: WsEventMsg) {
this.emit(type, message);
}
private emitDescribe(id: string, data: any) {
private emitMJ(id: string, data: any) {
const event = this.getEventById(id);
if (!event) return;
this.emit(event.nonce, data);
Expand Down Expand Up @@ -383,7 +390,7 @@ export class WsMessage {
};
this.event.push({ event: "settings", callback: once });
}
onceDescribe(nonce: string, callback: (data: any) => void) {
onceMJ(nonce: string, callback: (data: any) => void) {
const once = (message: any) => {
this.remove(nonce, once);
this.removeWaitMjEvent(nonce);
Expand Down Expand Up @@ -433,7 +440,18 @@ export class WsMessage {
options: MJOptions[];
descriptions: string[];
} | null>((resolve) => {
this.onceDescribe(nonce, (message) => {
this.onceMJ(nonce, (message) => {
resolve(message);
});
});
}
async waitShorten(nonce: string) {
return new Promise<{
options: MJOptions[];
prompts: string[];
description: string;
} | null>((resolve) => {
this.onceMJ(nonce, (message) => {
resolve(message);
});
});
Expand Down

0 comments on commit 856966f

Please sign in to comment.