(prompt: string, loading?: LoadingHandler)
| 55 | return this; |
| 56 | } |
| 57 | async Imagine(prompt: string, loading?: LoadingHandler) { |
| 58 | prompt = prompt.trim(); |
| 59 | if (!this.config.Ws) { |
| 60 | const seed = random(1000000000, 9999999999); |
| 61 | prompt = `[${seed}] ${prompt}`; |
| 62 | } else { |
| 63 | await this.getWsClient(); |
| 64 | } |
| 65 | |
| 66 | const nonce = nextNonce(); |
| 67 | this.log(`Imagine`, prompt, "nonce", nonce); |
| 68 | const httpStatus = await this.MJApi.ImagineApi(prompt, nonce); |
| 69 | if (httpStatus !== 204) { |
| 70 | throw new Error(`ImagineApi failed with status ${httpStatus}`); |
| 71 | } |
| 72 | if (this.wsClient) { |
| 73 | return await this.wsClient.waitImageMessage({ nonce, loading, prompt }); |
| 74 | } else { |
| 75 | this.log(`await generate image`); |
| 76 | const msg = await this.WaitMessage(prompt, loading); |
| 77 | this.log(`image generated`, prompt, msg?.uri); |
| 78 | return msg; |
| 79 | } |
| 80 | } |
| 81 | // check ws enabled && connect |
| 82 | private async getWsClient() { |
| 83 | if (!this.config.Ws) { |
no test coverage detected