import WebSocket from "ws";
const handler = async (m, { command, usedPrefix, conn, text, args }) => {
  const input_data = [
    "stabilityai/StableBeluga2",
    "meta-llama/Llama-2-70b-chat-hf",
    "tiiuae/falcon-180B-chat",
    "timdettmers/guanaco-65b",
    "bigscience/bloomz",
  ];
  let [urutan, tema] = text.split("|");
  if (!tema)
    return m.reply("Input query!\n*Example:*\n.chatpetals [nomor]|[query]");
  m.react(wait);
  try {
    let data = input_data.map((item, index) => ({
      title: item.split("/")[1],
      id: item,
    }));
    if (!urutan)
      return m.reply(
        "Input query!\n*Example:*\n.chatpetals [nomor]|[query]\n\n*Pilih angka yg ada*\n" +
          data.map((item, index) => `*${index + 1}.* ${item.title}`).join("\n"),
      );
    if (isNaN(urutan))
      return m.reply(
        "Input query!\n*Example:*\n.chatpetals [nomor]|[query]\n\n*Pilih angka yg ada*\n" +
          data.map((item, index) => `*${index + 1}.* ${item.title}`).join("\n"),
      );
    if (urutan > data.length)
      return m.reply(
        "Input query!\n*Example:*\n.chatpetals [nomor]|[query]\n\n*Pilih angka yg ada*\n" +
          data.map((item, index) => `*${index + 1}.* ${item.title}`).join("\n"),
      );
    let out = data[urutan - 1].id;
    if ("stabilityai/StableBeluga2" === out) {
      const response = await chatPetals(tema, "stabilityai/StableBeluga2");
      m.reply(response);
    } else if ("meta-llama/Llama-2-70b-chat-hf" === out) {
      const response = await chatPetals(tema, "meta-llama/Llama-2-70b-chat-hf");
      m.reply(response);
    } else if ("tiiuae/falcon-180B-chat" === out) {
      const response = await chatPetals(tema, "tiiuae/falcon-180B-chat");
      m.reply(response);
    } else if ("timdettmers/guanaco-65b" === out) {
      const response = await chatPetals(tema, "timdettmers/guanaco-65b");
      m.reply(response);
    } else if ("bigscience/bloomz" === out) {
      const response = await chatPetals(tema, "bigscience/bloomz");
      m.reply(response);
    }
  } catch (e) {
    m.react(eror);
  }
};
(handler.help = ["chatpetals *[nomor]|[query]*"]),
  (handler.tags = ["fun"]),
  (handler.command = /^(chatpetals)$/i);
export default handler;
async function chatPetals(inputs, model) {
  const ws = new WebSocket("wss://chat.petals.dev/api/v2/generate");
  await new Promise((resolve) => (ws.onopen = resolve));
  ws.send(
    JSON.stringify({
      type: "open_inference_session",
      model: model,
      max_length: 30,
    }),
  );
  return await new Promise((resolve, reject) => {
    ws.send(
      JSON.stringify({
        type: "generate",
        inputs: inputs,
        max_length: 30,
        do_sample: 1,
        temperature: 0.6,
        top_p: 0.9,
      }),
    ),
      (ws.onmessage = (event) => {
        const response = JSON.parse(event.data);
        if (response.ok)
          if (void 0 === response.outputs)
            console.log("Session opened, generating...");
          else {
            const generatedText = response.outputs;
            console.log("Generated: " + generatedText),
              ws.close(),
              resolve(generatedText);
          }
        else
          console.log("Error: " + response.traceback),
            ws.close(),
            reject(response.traceback);
      });
  });
}
