Skip to content

Commit

Permalink
Fix Nexra provider
Browse files Browse the repository at this point in the history
  • Loading branch information
XInTheDark committed Nov 17, 2024
1 parent a74aa0d commit c064ceb
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/api/Providers/nexra.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fetch from "node-fetch";
import { format_chat_to_prompt, messages_to_json } from "../../classes/message.js";
import { sleep } from "#root/src/helpers/helper.js";

// Reference: https://nexra.aryahcr.cc/documentation/chatgpt/en (under ChatGPT v2)
const api_url_stream = "https://nexra.aryahcr.cc/api/chat/complements";
const api_url_no_stream = "https://nexra.aryahcr.cc/api/chat/gpt";
const api_url_no_stream2 = "https://nexra.aryahcr.cc/api/chat/task";
const headers = {
"Content-Type": "application/json",
};
Expand Down Expand Up @@ -98,5 +100,39 @@ export const getNexraResponseNoStream = async (chat, options) => {
});

const json = await response.json();
return json["gpt"];
const id = json.id;
if (!id) {
throw new Error(`No ID returned: ${json}`);
}

let url = `${api_url_no_stream2}/${encodeURIComponent(id)}`;
let ready = false;
let result2 = null;

while (!ready) {
const response2 = await fetch(url, {
method: "GET",
headers: headers,
});
result2 = await response2.json();

if (result2?.gpt && result2?.gpt?.length > 0) {
break;
}

switch (result2?.status) {
case "pending":
await sleep(500);
break;
case "error":
throw new Error(`Error: ${result2}`);
case "not_found":
throw new Error(`Error: Not found: ${result2}`);
case "completed":
ready = true;
break;
}
}

return result2?.gpt;
};

0 comments on commit c064ceb

Please sign in to comment.