erictik / midjourney-api

MidJourney client. Unofficial Node.js client
Apache License 2.0
1.65k stars 271 forks source link

missing response from server #210

Open testAccountDeltas opened 11 months ago

testAccountDeltas commented 11 months ago

the code that reproduces this issue or a replay of the bug


let completion;
let ReconnectCounter = 2;

while (--ReconnectCounter) {
      try {
        await logInfo("Midjourney Imagine: ${prompt}");
        completion = await Promise.race([
          client.Imagine(prompt, async (uri, progress) => {
            await bot.sendChatAction(userCurrent.username, "typing");
            await sendPhotoData(bot, uri, "request in processing...", {
              chat_id: userCurrent.username,
              message_id: waitMessage
            }).then(async message => {
              if (!waitMessage) {
                waitMessage = message.message_id;
              }
            }).catch((error) => logInfo(error));
          }),
          new Promise((_, reject) =>
            setTimeout(() => reject(new Error("Imagine timeout")), 2 * 60000)
          )
        ]);
        break;
      } catch (e) {
        if (e.message !== "Imagine timeout") {
          throw e;
          break;
        } else {
          await logInfo("close client Midjourney");
          await client.Close();
          await logInfo("sleep client Midjourney 150 ms");
          await new Promise((resolve) => setTimeout(resolve, 150));
          await logInfo("connect client Midjourney");
          await client.init();
        }
      }
    }

Describe the bug

Hello, the connection with midjourney is lost after 20-30 minutes of connection. Closing and reopening it doesn't provide a connection and reconnecting doesn't yield any result. What should I do?

error log

Error: ImagineApi failed with status 429 at Midjourney.Imagine (/opt/node_modules/midjourney/libs/midjourney.js:52:19) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async MidjourneyGeneration (file:///opt/index.dev.js:463:22) at async MidjourneyGeneration (file:///opt/index.dev.js:548:7) at async MidjourneyGeneration (file:///opt/index.dev.js:548:7) at async MidjourneyGeneration (file:///opt/index.dev.js:548:7) at async file:///opt/index.dev.js:324:13 at async Object.callback (file:///opt/index.dev.js:1157:7)

Error: Invalid aspect ratio: 9/16 (Should be W:H, for example 16:9) at WsMessage.messageCreate (/opt/node_modules/midjourney/libs/ws.message.js:91:39) at WsMessage.parseMessage (/opt/node_modules/midjourney/libs/ws.message.js:193:18) at WebSocket. (/opt/node_modules/midjourney/libs/ws.message.js:52:18) at callListener (/opt/node_modules/ws/lib/event-target.js:290:14) at WebSocket.onMessage (/opt/node_modules/ws/lib/event-target.js:209:9) at WebSocket.emit (node:events:513:28) at Receiver.receiverOnMessage (/opt/node_modules/ws/lib/websocket.js:1184:20) at Receiver.emit (node:events:513:28) at Receiver.dataMessage (/opt/node_modules/ws/lib/receiver.js:541:14) at Receiver.getData (/opt/node_modules/ws/lib/receiver.js:459:17) at Receiver.startLoop (/opt/node_modules/ws/lib/receiver.js:158:22) at Receiver._write (/opt/node_modules/ws/lib/receiver.js:84:10) at writeOrBuffer (node:internal/streams/writable:392:12) at _write (node:internal/streams/writable:333:10) at Writable.write (node:internal/streams/writable:337:10) at TLSSocket.socketOnData (/opt/node_modules/ws/lib/websocket.js:1278:35) at TLSSocket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:324:12) at readableAddChunk (node:internal/streams/readable:297:9) at Readable.push (node:internal/streams/readable:234:10) at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23)

Error: Could not validate this link. Please try again later. at WsMessage.messageCreate (/opt/node_modules/midjourney/libs/ws.message.js:91:39) at WsMessage.parseMessage (/opt/node_modules/midjourney/libs/ws.message.js:193:18) at WebSocket. (/opt/node_modules/midjourney/libs/ws.message.js:52:18) at callListener (/opt/node_modules/ws/lib/event-target.js:290:14) at WebSocket.onMessage (/opt/node_modules/ws/lib/event-target.js:209:9) at WebSocket.emit (node:events:513:28) at Receiver.receiverOnMessage (/opt/node_modules/ws/lib/websocket.js:1184:20) at Receiver.emit (node:events:513:28) at Receiver.dataMessage (/opt/node_modules/ws/lib/receiver.js:541:14) at Receiver.getData (/opt/node_modules/ws/lib/receiver.js:459:17) at Receiver.startLoop (/opt/node_modules/ws/lib/receiver.js:158:22) at Receiver._write (/opt/node_modules/ws/lib/receiver.js:84:10) at writeOrBuffer (node:internal/streams/writable:392:12) at _write (node:internal/streams/writable:333:10) at Writable.write (node:internal/streams/writable:337:10) at TLSSocket.socketOnData (/opt/node_modules/ws/lib/websocket.js:1278:35) at TLSSocket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:324:12) at readableAddChunk (node:internal/streams/readable:297:9) at Readable.push (node:internal/streams/readable:234:10) at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23)

Error: You've already requested an upscale for this image! at WsMessage.messageCreate (/opt/node_modules/midjourney/libs/ws.message.js:91:39) at WsMessage.parseMessage (/opt/node_modules/midjourney/libs/ws.message.js:193:18) at WebSocket. (/opt/node_modules/midjourney/libs/ws.message.js:52:18) at callListener (/opt/node_modules/ws/lib/event-target.js:290:14) at WebSocket.onMessage (/opt/node_modules/ws/lib/event-target.js:209:9) at WebSocket.emit (node:events:513:28) at Receiver.receiverOnMessage (/opt/node_modules/ws/lib/websocket.js:1184:20) at Receiver.emit (node:events:513:28) at Receiver.dataMessage (/opt/node_modules/ws/lib/receiver.js:541:14) at Receiver.getData (/opt/node_modules/ws/lib/receiver.js:459:17) at Receiver.startLoop (/opt/node_modules/ws/lib/receiver.js:158:22) at Receiver._write (/opt/node_modules/ws/lib/receiver.js:84:10) at writeOrBuffer (node:internal/streams/writable:392:12) at _write (node:internal/streams/writable:333:10) at Writable.write (node:internal/streams/writable:337:10) at TLSSocket.socketOnData (/opt/node_modules/ws/lib/websocket.js:1278:35) at TLSSocket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:324:12) at readableAddChunk (node:internal/streams/readable:297:9) at Readable.push (node:internal/streams/readable:234:10)

testAccountDeltas commented 11 months ago

Everything works well for the first 20-30 minutes, but afterwards, when the connection is lost (the server stops responding), I attempt to reconnect. At that point, I encounter various types of errors. I am unable to establish a new connection with Midjourney. It complains that it couldn't find a callback for the result.

testAccountDeltas commented 11 months ago

string New user id [xxxxx] string [event text:user: xxxxx = Go] - init string Midjourney Imagine: Go Imagine Go nonce 1137308208306585600 2023-08-05T08:56:47.025Z string [event text:user: xxxxx = Mister] - init string Midjourney Imagine: Mister Imagine Mister nonce 1137308633239912448 2023-08-05T08:58:28.337Z string close client Midjourney string sleep client Midjourney 150 ms string connect client Midjourney open.time 0 2023-08-05T08:58:47.449Z ws ready 2023-08-05T08:58:48.386Z

testAccountDeltas commented 11 months ago
FilterMessages not found {
  id: '*',
  flags: 0,
  content: '**Mister** - <@1093132196581216257> (fast)',
  hash: '*-*-*-*-*',
  progress: 'done',
  uri: 'https://cdn.discordapp.com/attachments//.png',
  options: [
    {
      type: 2,
      style: 2,
      label: 'U1',
      custom: 'MJ::JOB::upsample::1::*-*-4aec-*-*'
    },
   ...
  ]
} Map(0) {} 2023-08-05T08:59:11.060Z
testAccountDeltas commented 11 months ago

In general, nothing is clear, it would be nice if the library itself monitored that the connection was not processed for a long time. And tried to reconnect without losing the handlers.

jeffcrouse commented 11 months ago

Any resolution to this issue? Mine works well for far longer -- maybe 5 hours. I have also made a timeout system like yours. But after many hours it starts timing out over and over. My app is running on an AWS sever, so I am fairly certain the internet connection is not the problem.

testAccountDeltas commented 11 months ago

Any resolution to this issue? Mine works well for far longer -- maybe 5 hours. I have also made a timeout system like yours. But after many hours it starts timing out over and over. My app is running on an AWS sever, so I am fairly certain the internet connection is not the problem.

I uninstalled the updates and reinstalled. And I controlled nodemon to track only 1 target. There were no problems after that.

"dev": "nodemon --watch index.dev.js index.dev.js",

So if you have logs. Then the tracking extensions can restart the script

testAccountDeltas commented 11 months ago

The latest version of midjourney-api behaves unconditionally well

const client = new Midjourney({
  ServerId: process.env.MIDJOURNEY_SERVERID,
  ChannelId: process.env.MIDJOURNEY_CHANNELID,
  SalaiToken: process.env.MIDJOURNEY_SALAITOKEN,
  Debug: debugConsole,
  Ws: true,
});

async function MidjourneyGeneration(text, prompt, userCurrent, waitMessage = false, count = 5) {
  try {
    if (moderation(prompt)) {
      await bot.sendMessage(userCurrent.username, `Запрос "${text}" не прошёл модерацию.`, {
        parse_mode: 'Markdown'
      }).catch((error) => logInfo(error));
      return;
    }

    await bot.sendChatAction(userCurrent.username, 'typing');
    userCurrent.subscribe = (userCurrent.subscribe / 1) - 1;
    await DB.saveData(userCurrent, userCurrent.username);
    //await userCurrent.save();

    let completion;
    let ReconnectCounter = 2;
    while (--ReconnectCounter) {
      try {
        await logInfo(`Midjourney Imagine: ${prompt}`);
        completion = await Promise.race([
          client.Imagine(prompt, async (uri, progress) => {
            await logInfo(`Imagine URL: ${uri}`);

            await bot.sendChatAction(userCurrent.username, 'typing');
            await sendPhotoData(bot, uri, '👀 Пожалуйста, подождите... ', {
              chat_id: userCurrent.username,
              message_id: waitMessage
            }).then(async message => {
              if (!waitMessage) {
                waitMessage = message.message_id;
              }
            }).catch((error) => logInfo(error));
          }),
          new Promise((_, reject) =>
            setTimeout(() => reject(new Error('Imagine timeout')), 3 * 60000)
          )
        ]);
        break;
      } catch (e) {
        if (e.message !== 'Imagine timeout') {
          await logInfo(e);
          await cancelOperation(e, text, prompt, userCurrent, waitMessage, -1, false, false, false);
          return;
        } else {
          await logInfo('close client Midjourney');
          await client.Close();
          await logInfo('sleep client Midjourney 150 ms');
          await new Promise((resolve) => setTimeout(resolve, 150));
          await logInfo('connect client Midjourney');
          await client.init();
        }
      }
    }
    let isError;
    if (ReconnectCounter <= 0) {
      isError = true;
    } else {
      let inline_keyboard = inline_keyboard_button(completion);
      let max = 10;
      isError = true;
      for (let i = 0; isError && (i < 10); i++) {
        await sendPhotoData(bot, completion.uri, `[Ссылка на изображение](${completion.uri})`, {
          chat_id: userCurrent.username,
          message_id: waitMessage,
          reply_markup: {
            inline_keyboard: inline_keyboard
          }
        }).then(async message => {
          isError = false;
          waitMessage = message.message_id;
        }).catch((error) => {
          if (i >= max) {
            logInfo(error)
          }
        });
      }
    }
    if (isError) {
      await cancelOperation(true, text, prompt, userCurrent, waitMessage, -1, false, false, false);
    } else if (waitMessage) {
      await contentMessageSet(userCurrent, waitMessage, completion, text);
      userCurrent.last = prompt;
      await DB.saveData(userCurrent, userCurrent.username);
      //await userCurrent.save();
    }
  } catch (error) {
    await logInfo(error);
    await cancelOperation(error, text, prompt, userCurrent, waitMessage, --count, false, false, false);
  };
}

async function MidjourneyGenerationCustom(BDButtonFile, BDButton, id, userCurrent, waitMessage = false, count = 5) {
  try {
    BDButton[id][1] = id;
    await writeJsonFile(BDButtonFile, BDButton);

    await bot.sendChatAction(userCurrent.username, 'typing');
    userCurrent.subscribe = (userCurrent.subscribe / 1) - 1;
    await DB.saveData(userCurrent, userCurrent.username);
    //await userCurrent.save();

    let completion;
    let ReconnectCounter = 2;
    while (--ReconnectCounter) {
      try {
        await logInfo(`Midjourney Custom: ${BDButton[id][0]}`);
        completion = await Promise.race([
          client.Custom({
            msgId: BDButton.id,
            flags: BDButton.flags,
            content: BDButton.content,
            customId: BDButton[id][0],
            loading: async (uri, progress) => {
              await bot.sendChatAction(userCurrent.username, 'typing');
              await sendPhotoData(bot, uri, '👀 Пожалуйста, подождите... ', {
                chat_id: userCurrent.username,
                message_id: waitMessage
              }).then(async message => {
                if (!waitMessage)
                  waitMessage = message.message_id;
              }).catch((error) => logInfo(error));
            }
          }),
          new Promise((_, reject) =>
            setTimeout(() => reject(new Error('Custom timeout')), 3 * 60000)
          )
        ]);
        break; // Выход из цикла, если Promise завершился успешно
      } catch (e) {
        if (e.message !== 'Custom timeout') {
          await logInfo(e);
          await cancelOperation(e, `${BDButton.promptSave} -- ${id}`, false, userCurrent, waitMessage, -1, BDButton, id, BDButtonFile);
          return;
        } else {
          await logInfo('close client Midjourney');
          await client.Close();
          await logInfo('sleep client Midjourney 150 ms');
          await new Promise((resolve) => setTimeout(resolve, 150));
          await logInfo('connect client Midjourney');
          await client.init();
        }
      }
    }
    let isError;
    if (ReconnectCounter <= 0) {
      isError = true;
    } else {
      let inline_keyboard = inline_keyboard_button(completion);
      let max = 10;
      isError = true;
      for (let i = 0; isError && (i < 10); i++) {
        await sendPhotoData(bot, completion.uri, `[Ссылка на изображение](${completion.uri})`, {
          chat_id: userCurrent.username,
          message_id: waitMessage,
          reply_markup: {
            inline_keyboard: inline_keyboard
          }
        }).then(async message => {
          isError = false;
          waitMessage = message.message_id;
        }).catch((error) => {
          if (i >= max) {
            logInfo(error)
          }
        });
      }
    }
    if (isError) {
      await cancelOperation(true, `${BDButton.promptSave} -- ${id}`, false, userCurrent, waitMessage, -1, BDButton, id, BDButtonFile);
    } else if (waitMessage) {
      BDButton[id][1] = completion.uri;
      await writeJsonFile(BDButtonFile, BDButton);

      await contentMessageSet(userCurrent, waitMessage, completion, id);
    }
  } catch (error) {
    await logInfo(error);
    await cancelOperation(error, `${BDButton.promptSave} -- ${id}`, false, userCurrent, waitMessage, --count, BDButton, id, BDButtonFile);
  };
}

function inline_keyboard_button(completion) {
  if (completion.options === undefined) return [];

  let inline_keyboard = [];

  for (const row of completion.options) {
    let labeltext = row.label.trim();
    if (sortButton[labeltext] === false) {
      continue;
    }
    if ((typeof sortButton[labeltext] === 'object') && sortButton[labeltext]['text']) {
      labeltext = sortButton[labeltext]['text'].trim();
    }
    inline_keyboard.push({
      text: labeltext,
      callback_data: labeltext
    });
  }
  inline_keyboard.sort((a, b) => {
    let indexA = Infinity;
    if ((typeof sortButton[a.text] === 'object') && sortButton[a.text]['index']) {
      indexA = sortButton[a.text]['index'];
    } else if (sortButton[a.text] != undefined) {
      indexA = sortButton[a.text];
    }
    let indexB = Infinity;
    if ((typeof sortButton[b.text] === 'object') && sortButton[b.text]['index']) {
      indexB = sortButton[b.text]['index'];
    } else if (sortButton[b.text] != undefined) {
      indexB = sortButton[b.text];
    }
    return indexA - indexB;
  });

  return inline_keyboard.reduce((acc, curr, index) => {
    const groupIndex = Math.floor(index / 3);
    if (!acc[groupIndex]) {
      acc[groupIndex] = [];
    }
    acc[groupIndex].push(curr);
    return acc;
  }, []);
}

async function contentMessageSet(userCurrent, waitMessage, completion, prompt) {
  let content = {
    id: completion.id,
    flags: completion.flags,
    content: completion.content,
    data: +(new Date()),
    promptSave: prompt
  };

  for (const comOpt of completion.options) {
    let labeltext = comOpt.label.trim();
    if (sortButton[labeltext] === false) {
      continue;
    }
    if ((typeof sortButton[labeltext] === 'object') && sortButton[labeltext]['text']) {
      labeltext = sortButton[labeltext]['text'].trim();
    }
    content[labeltext] = [comOpt.custom, false];
  }

  await writeJsonFile('tmp/' + userCurrent.username + '/' + waitMessage, content);
}
ajimix commented 10 months ago

I'm having the same issue, after several hours, no more responses from the server com back