Skip to content

Commit

Permalink
add progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed May 9, 2023
1 parent 41c6fe0 commit 86e77a6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/releases.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Node.js Package
env:
APPVERSION: v1.0.${{ github.run_number }}
APPVERSION: v1.1.${{ github.run_number }}
on:
workflow_dispatch:
push:
Expand Down
9 changes: 6 additions & 3 deletions example/imagine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ async function main() {
<string>process.env.SALAI_TOKEN,
true
);
const msg = await client.Imagine("A little pink elephant", (uri: string) => {
console.log("loading", uri);
});
const msg = await client.Imagine(
"A little white elephant",
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log({ msg });
}
main().catch((err) => {
Expand Down
4 changes: 2 additions & 2 deletions example/upscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ async function main() {
2,
msg.id,
msg.hash,
(uri: string) => {
console.log("loading", uri);
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log({ msg2 });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "midjourney",
"version": "1.0.0",
"version": "1.1.0",
"description": "Node.js client for the unofficial MidJourney API.",
"main": "libs/index.js",
"types": "libs/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ export interface MJMessage {
uri: string;
hash: string;
content: string;
progress?: string;
}

export type LoadingHandler = (uri: string, progress: string) => void;
25 changes: 17 additions & 8 deletions src/midjourney.message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MJMessage } from "./interfaces";
import { LoadingHandler, MJMessage } from "./interfaces";
import { CreateQueue } from "./queue";
import { sleep } from "./utls";

Expand All @@ -18,7 +18,7 @@ export class MidjourneyMessage {
}
async FilterMessages(
prompt: string,
loading?: (uri: string) => void,
loading?: LoadingHandler,
options?: string,
index?: number
) {
Expand All @@ -35,8 +35,7 @@ export class MidjourneyMessage {
options &&
!(
item.content.includes(options) ||
(options === "Upscaled" &&
item.content.includes(` - Image #${index}`))
(options === "Upscaled" && item.content.includes(`Image #${index}`))
)
) {
this.log("no options");
Expand All @@ -48,7 +47,16 @@ export class MidjourneyMessage {
}
const imageUrl = item.attachments[0].url;
if (!imageUrl.endsWith(".png")) {
loading?.(imageUrl);
this.log(`content`, item.content);
const regex = /\(([^)]+)\)/; // matches the value inside the first parenthesis
const match = item.content.match(regex);
let progress = "wait";
if (match) {
progress = match[1];
} else {
this.log("No match found");
}
loading?.(imageUrl, progress);
break;
}
const content = item.content.split("**")[1];
Expand All @@ -57,6 +65,7 @@ export class MidjourneyMessage {
uri: imageUrl,
hash: this.UriToHash(imageUrl),
content: content,
progress: "done",
};
return msg;
}
Expand All @@ -66,7 +75,7 @@ export class MidjourneyMessage {
UriToHash(uri: string) {
return uri.split("_").pop()?.split(".")[0] ?? "";
}
async WaitMessage(prompt: string, loading?: (uri: string) => void) {
async WaitMessage(prompt: string, loading?: LoadingHandler) {
for (let i = 0; i < this.maxWait; i++) {
const msg = await this.FilterMessages(prompt, loading);
if (msg !== null) {
Expand All @@ -80,7 +89,7 @@ export class MidjourneyMessage {
async WaitOptionMessage(
content: string,
options: string,
loading?: (uri: string) => void
loading?: LoadingHandler
) {
for (let i = 0; i < this.maxWait; i++) {
const msg = await this.FilterMessages(content, loading, options);
Expand All @@ -94,7 +103,7 @@ export class MidjourneyMessage {
async WaitUpscaledMessage(
content: string,
index: number,
loading?: (uri: string) => void
loading?: LoadingHandler
) {
for (let i = 0; i < this.maxWait; i++) {
const msg = await this.FilterMessages(
Expand Down

0 comments on commit 86e77a6

Please sign in to comment.