Skip to content

Commit

Permalink
Merge pull request #41 from erictik/ws-wait-message
Browse files Browse the repository at this point in the history
👋  Important update
  • Loading branch information
zcpua authored May 18, 2023
2 parents 315a3b4 + abcedfa commit 28117fd
Show file tree
Hide file tree
Showing 19 changed files with 853 additions and 39 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: v2.1.${{ github.run_number }}
APPVERSION: v2.2.${{ github.run_number }}
on:
workflow_dispatch:
push:
Expand Down
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Node.js client for the unofficial MidJourney API.

[discord bot example](https://github.com/erictik/midjourney-discord-wrapper/)

[web ui example](https://github.com/erictik/midjourney-ui/)

[web ui example](https://github.com/erictik/midjourney-ui/)

## Install

Expand All @@ -37,6 +36,7 @@ import { Midjourney } from "midjourney";
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
Ws:true,
});
const msg = await client.Imagine("A little pink elephant", (uri: string) => {
console.log("loading", uri);
Expand Down Expand Up @@ -75,13 +75,24 @@ export SALAI_TOKEN="your-salai-token"
Then, run the example with the following command:

```bash
npx tsx example/imagine.ts
npx tsx example/imagine-ws.ts
```

```bash
npx tsx example/upscale.ts
npx tsx example/upscale-w.ts
```

```bash
npx tsx example/variation.ts
npx tsx example/variation-ws.ts
```


## route-map
- [x] websocket get message
- [x] call back error
- [ ] add `/info` `/fast` and `/relax`



## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=erictik/midjourney-api&type=Date)](https://star-history.com/#erictik/midjourney-api&Date)
31 changes: 31 additions & 0 deletions example/imagine-err.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of how to use the imagine command
* ```
* npx tsx example/imagine-err.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,
SessionId: process.env.SALAI_TOKEN || "8bb7f5b79c7a49f7d0824ab4b8773a81",
});

await client.init();
const msg = await client.Imagine(
"A little white elephant --ar1:1",
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log({ msg });
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
36 changes: 36 additions & 0 deletions example/imagine-ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of using the imagine api with ws
* ```
* npx tsx example/imagine-ws.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.init();
const msg = await client.Imagine(
"A little white dog",
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log({ msg });
}
main()
.then(() => {
console.log("finished");
process.exit(0);
})
.catch((err) => {
console.log("finished");
console.error(err);
process.exit(1);
});
8 changes: 4 additions & 4 deletions example/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "dotenv/config";
import { Midjourney, MidjourneyMessage } from "../src";
import { WsMessage } from "../src";
/**
*
* a simple example of how to use the imagine command
Expand All @@ -8,12 +8,12 @@ import { Midjourney, MidjourneyMessage } from "../src";
* ```
*/
async function main() {
const client = new MidjourneyMessage({
const client = new WsMessage({
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
});
const msg = await client.RetrieveMessages();
console.log({ msg });
console.log("client");
}
main().catch((err) => {
console.error(err);
Expand Down
39 changes: 39 additions & 0 deletions example/upscale-ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of how to use the Upscale with ws command
* ```
* npx tsx example/upscale-ws.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.init();
const msg = await client.Imagine("a cool cat, blue ears, yellow hat");
console.log({ msg });
if (!msg) {
console.log("no message");
return;
}
const msg2 = await client.Upscale(
msg.content,
2,
<string>msg.id,
<string>msg.hash,
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log({ msg2 });
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
4 changes: 2 additions & 2 deletions example/upscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ async function main() {
const msg2 = await client.Upscale(
msg.content,
2,
msg.id,
msg.hash,
<string>msg.id,
<string>msg.hash,
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
Expand Down
39 changes: 39 additions & 0 deletions example/variation-ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of how to use the Variation with ws command
* ```
* npx tsx example/variation-ws.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.init();
const msg = await client.Imagine("a dog, blue ears, and a red nose");
console.log({ msg });
if (!msg) {
console.log("no message");
return;
}
const msg2 = await client.Variation(
msg.content,
2,
<string>msg.id,
<string>msg.hash,
(uri: string) => {
console.log("loading", uri);
}
);
console.log({ msg2 });
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
4 changes: 2 additions & 2 deletions example/variation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ async function main() {
const msg2 = await client.Variation(
msg.content,
2,
msg.id,
msg.hash,
<string>msg.id,
<string>msg.hash,
(uri: string) => {
console.log("loading", uri);
}
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "midjourney",
"version": "2.1.0",
"version": "2.2.0",
"description": "Node.js client for the unofficial MidJourney API.",
"main": "libs/index.js",
"types": "libs/index.d.ts",
Expand Down Expand Up @@ -34,15 +34,19 @@
"homepage": "https://github.com/erictik/midjourney-api#readme",
"devDependencies": {
"@types/node": "^18.16.0",
"@types/ws": "^8.5.4",
"dotenv": "^16.0.3",
"prettier": "^2.8.8",
"ts-node": "^10.9.1",
"tsx": "^3.12.6",
"typescript": "^5.0.4"
},
"dependencies": {
"discord-api-types": "^0.37.42",
"discord.js": "^14.11.0",
"p-queue": "^6.6.2",
"throat": "^6.0.2",
"tslib": "^2.5.0"
"tslib": "^2.5.0",
"ws": "^8.13.0"
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./midjourney";
export * from "./midjourney.message";
export * from "./ws.message";
export * from "./interfaces/index";
2 changes: 2 additions & 0 deletions src/interfaces/message.config.ts → src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ export interface MessageConfig {
Debug: boolean;
Limit: number;
MaxWait: number;
Ws?: boolean;
}
export interface MessageConfigParam {
ChannelId: string;
SalaiToken: string;
Debug?: boolean;
Limit?: number;
MaxWait?: number;
Ws?: boolean;
}
export interface MidjourneyConfig extends MessageConfig {
ServerId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./message";
export * from "./message.config";
export * from "./config";
22 changes: 15 additions & 7 deletions src/interfaces/message.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
export interface Message {
text: string;
img: string;
}

export interface MJMessage {
id: string;
uri: string;
hash: string;
content: string;
id?: string;
hash?: string;
progress?: string;
}

export type LoadingHandler = (uri: string, progress: string) => void;

export interface WaitMjEvent {
type: "imagine" | "upscale" | "variation" | "info";
nonce: string;
prompt?: string;
id?: string;
index?: number;
}
export interface WsEventMsg {
error?: Error;
message?: MJMessage;
}
export type ImageEventType = "imagine" | "upscale" | "variation";
3 changes: 3 additions & 0 deletions src/midjourney.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class MidjourneyMessage {
this.log(i, "wait no message found");
await sleep(1000 * 2);
}
return null;
}

async WaitOptionMessage(
Expand All @@ -120,6 +121,7 @@ export class MidjourneyMessage {
this.log(i, content, "wait no message found");
await sleep(1000 * 2);
}
return null;
}
async WaitUpscaledMessage(
content: string,
Expand All @@ -139,6 +141,7 @@ export class MidjourneyMessage {
this.log(i, content, "wait no message found");
await sleep(1000 * 2);
}
return null;
}

// limit the number of concurrent interactions
Expand Down
Loading

0 comments on commit 28117fd

Please sign in to comment.