Skip to content

Commit

Permalink
add options
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed Jun 18, 2023
1 parent dcf81c6 commit 114b79a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions example/imagine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface MJMessage {
id?: string;
hash?: string;
progress?: string;
options?: MJOptions[];
}

export type LoadingHandler = (uri: string, progress: string) => void;
Expand Down Expand Up @@ -34,5 +35,6 @@ export interface MJInfo {

export interface MJOptions {
label: string;
type: string;
custom: string;
}
6 changes: 4 additions & 2 deletions src/midjourney.message.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { format } from "path";
import {
DefaultMJConfig,
LoadingHandler,
Expand All @@ -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);
Expand Down Expand Up @@ -61,12 +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;
}
Expand Down
19 changes: 18 additions & 1 deletion src/utls/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import { Snowyflake, Epoch } from "snowyflake";
import { MJOptions } from "../interfaces";

export const sleep = async (ms: number): Promise<void> =>
await new Promise((resolve) => setTimeout(resolve, ms));

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,
epoch: Epoch.Discord, // BigInt timestamp
});

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;
};
8 changes: 5 additions & 3 deletions src/ws.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -239,14 +240,15 @@ export class WsMessage {

private done(message: any) {
const { content, id, attachments, components, flags } = message;
console.log("components=====", JSON.stringify(message));
console.log("components=====", JSON.stringify(components));
const MJmsg: MJMessage = {
id,
flags,
content,
hash: this.uriToHash(attachments[0].url),
progress: "done",
flags: flags,
uri: attachments[0].url,
content: content,
options: formatOptions(components),
};
this.filterMessages(MJmsg);
return;
Expand Down

0 comments on commit 114b79a

Please sign in to comment.