Fix: return when failed to getNewSession, add 5 times retry to getNewSession

This commit is contained in:
Pawan Osman
2024-04-20 23:33:22 +03:00
parent deacb3b418
commit 21dc6ef01f
2 changed files with 8 additions and 4 deletions
+1
View File
@@ -1,5 +1,6 @@
SERVER_PORT=3040
USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
NEW_SESSION_RETRIES=5
CLOUDFLARED=true
PROXY=false
+7 -4
View File
@@ -18,6 +18,8 @@ const baseUrl = "https://chat.openai.com";
const apiUrl = `${baseUrl}/backend-anon/conversation`;
const refreshInterval = 60000; // Interval to refresh token in ms
const errorWait = 120000; // Wait time in ms after an error
const newSessionRetries: number =
parseInt(process.env.NEW_SESSION_RETRIES) || 5; // Number of retries to get a new session
const userAgent =
process.env.USER_AGENT ||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36";
@@ -162,7 +164,7 @@ function GenerateProofToken(
}
// Function to get a new session ID and token from the OpenAI API
async function getNewSession(): Promise<Session> {
async function getNewSession(retries: number = 0): Promise<Session> {
let newDeviceId = randomUUID();
try {
const response = await axiosInstance.post(
@@ -178,7 +180,8 @@ async function getNewSession(): Promise<Session> {
return session;
} catch (error) {
return null;
await wait(500);
return retries < newSessionRetries ? getNewSession(retries + 1) : null;
}
}
@@ -216,7 +219,7 @@ async function handleChatCompletion(req: Request, res: Response) {
})
);
res.end();
return res.end();
}
let proofToken = GenerateProofToken(
@@ -399,7 +402,7 @@ async function handleChatCompletion(req: Request, res: Response) {
status: false,
error: {
message:
"An error occurred. Please check the server console to confirm it is ready and free of errors. Additionally, ensure that your request complies with OpenAI's policy.",
"An error occurred. please try again. Additionally, ensure that your request complies with OpenAI's policy.",
type: "invalid_request_error",
},
support: "https://discord.pawan.krd",