Commit 47498cc3 authored by nanahira's avatar nanahira

login timeout and retry

parent e526f6e8
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
"class-validator": "^0.14.0", "class-validator": "^0.14.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"nestjs-aragami": "^1.0.0", "nestjs-aragami": "^1.0.0",
"p-timeout": "4.1.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rxjs": "^7.2.0", "rxjs": "^7.2.0",
...@@ -3268,6 +3269,17 @@ ...@@ -3268,6 +3269,17 @@
"puppeteer": "*" "puppeteer": "*"
} }
}, },
"node_modules/chatgpt3/node_modules/p-timeout": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-6.1.0.tgz",
"integrity": "sha512-s0y6Le9QYGELLzNpFIt6h8B2DHTVUDLStvxtvRMSKNKeuNVVWby2dZ+pIJpW4/pWr5a3s8W85wBNtc0ZA+lzCg==",
"engines": {
"node": ">=14.16"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/chokidar": { "node_modules/chokidar": {
"version": "3.5.3", "version": "3.5.3",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz",
...@@ -7602,14 +7614,11 @@ ...@@ -7602,14 +7614,11 @@
} }
}, },
"node_modules/p-timeout": { "node_modules/p-timeout": {
"version": "6.1.0", "version": "4.1.0",
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-6.1.0.tgz", "resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-4.1.0.tgz",
"integrity": "sha512-s0y6Le9QYGELLzNpFIt6h8B2DHTVUDLStvxtvRMSKNKeuNVVWby2dZ+pIJpW4/pWr5a3s8W85wBNtc0ZA+lzCg==", "integrity": "sha512-+/wmHtzJuWii1sXn3HCuH/FTwGhrp4tmJTxSKJbfS+vkipci6osxXM5mY0jUiRzWKMTgUT8l7HFbeSwZAynqHw==",
"engines": { "engines": {
"node": ">=14.16" "node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/p-try": { "node_modules/p-try": {
...@@ -12766,6 +12775,13 @@ ...@@ -12766,6 +12775,13 @@
"strip-markdown": "^5.0.0", "strip-markdown": "^5.0.0",
"tempy": "^3.0.0", "tempy": "^3.0.0",
"uuid": "^9.0.0" "uuid": "^9.0.0"
},
"dependencies": {
"p-timeout": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-6.1.0.tgz",
"integrity": "sha512-s0y6Le9QYGELLzNpFIt6h8B2DHTVUDLStvxtvRMSKNKeuNVVWby2dZ+pIJpW4/pWr5a3s8W85wBNtc0ZA+lzCg=="
}
} }
}, },
"chokidar": { "chokidar": {
...@@ -15903,9 +15919,9 @@ ...@@ -15903,9 +15919,9 @@
} }
}, },
"p-timeout": { "p-timeout": {
"version": "6.1.0", "version": "4.1.0",
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-6.1.0.tgz", "resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-4.1.0.tgz",
"integrity": "sha512-s0y6Le9QYGELLzNpFIt6h8B2DHTVUDLStvxtvRMSKNKeuNVVWby2dZ+pIJpW4/pWr5a3s8W85wBNtc0ZA+lzCg==" "integrity": "sha512-+/wmHtzJuWii1sXn3HCuH/FTwGhrp4tmJTxSKJbfS+vkipci6osxXM5mY0jUiRzWKMTgUT8l7HFbeSwZAynqHw=="
}, },
"p-try": { "p-try": {
"version": "2.2.0", "version": "2.2.0",
......
...@@ -14,6 +14,7 @@ import { OpenAIAccount } from '../utility/config'; ...@@ -14,6 +14,7 @@ import { OpenAIAccount } from '../utility/config';
import { BetterLock } from 'better-lock/dist/better_lock'; import { BetterLock } from 'better-lock/dist/better_lock';
import _ from 'lodash'; import _ from 'lodash';
import { BlankReturnMessageDto } from '../dto/ReturnMessage.dto'; import { BlankReturnMessageDto } from '../dto/ReturnMessage.dto';
import pTimeout from 'p-timeout';
interface AccountState { interface AccountState {
email: string; email: string;
...@@ -47,7 +48,7 @@ export class ChatgptService ...@@ -47,7 +48,7 @@ export class ChatgptService
isMicrosoftLogin: account.loginType === 'microsoft', isMicrosoftLogin: account.loginType === 'microsoft',
}); });
try { try {
await instance.initSession(); await pTimeout(instance.initSession(), 600000);
this.log(`Initialized ChatGPT API for ${account.email}`); this.log(`Initialized ChatGPT API for ${account.email}`);
this.accounts.set(account.email, { this.accounts.set(account.email, {
email: account.email, email: account.email,
...@@ -57,7 +58,11 @@ export class ChatgptService ...@@ -57,7 +58,11 @@ export class ChatgptService
}); });
} catch (e) { } catch (e) {
this.error(`Failed to initialize ChatGPT API for ${account.email}`, e); this.error(`Failed to initialize ChatGPT API for ${account.email}`, e);
return; await Promise.all([
new Promise((r) => setTimeout(r, 60000)),
instance.closeSession().catch(),
]);
return this.initAccount(account);
} }
} }
...@@ -90,7 +95,7 @@ export class ChatgptService ...@@ -90,7 +95,7 @@ export class ChatgptService
); );
} }
async chat(question: TalkDto, failedAccounts: string[] = []) { async chatProcess(question: TalkDto, failedAccounts: string[] = []) {
const session = question.session || uuid(); const session = question.session || uuid();
const previousConversation = failedAccounts.length const previousConversation = failedAccounts.length
? undefined ? undefined
...@@ -124,7 +129,7 @@ export class ChatgptService ...@@ -124,7 +129,7 @@ export class ChatgptService
} }
}); });
if (!result) { if (!result) {
return this.chat(question, [...failedAccounts, account.email]); return this.chatProcess(question, [...failedAccounts, account.email]);
} }
await this.conversationService.saveConversation( await this.conversationService.saveConversation(
session, session,
...@@ -136,4 +141,12 @@ export class ChatgptService ...@@ -136,4 +141,12 @@ export class ChatgptService
dto.text = result.response.replace(/^<!--(.*)-->$/gm, ''); dto.text = result.response.replace(/^<!--(.*)-->$/gm, '');
return dto; return dto;
} }
private sessionLock = new BetterLock();
async chat(dto: TalkDto) {
return dto
? this.sessionLock.acquire(dto.session, () => this.chatProcess(dto))
: this.chatProcess(dto);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment