recover from corrupted conversationHistory data (#162)

If an item from conversationHistory is badly corrupted, skip it instead of crashing the app
This commit is contained in:
Thomas LÉVEIL 2023-03-25 21:10:17 +01:00 committed by GitHub
parent 1253565a69
commit 0038bb8366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 15 deletions

View File

@ -39,21 +39,25 @@ export const cleanConversationHistory = (history: Conversation[]) => {
// added system prompt for each conversation (3/21/23)
// added folders (3/23/23)
let updatedHistory = [...history];
return history.reduce((acc: Conversation[], conversation) => {
try {
if (!conversation.model) {
conversation.model = OpenAIModels[OpenAIModelID.GPT_3_5];
}
updatedHistory.forEach((conversation) => {
if (!conversation.model) {
conversation.model = OpenAIModels[OpenAIModelID.GPT_3_5];
if (!conversation.prompt) {
conversation.prompt = DEFAULT_SYSTEM_PROMPT;
}
if (!conversation.folderId) {
conversation.folderId = 0;
}
acc.push(conversation);
return acc;
} catch (error) {
console.warn(`error while cleaning conversations' history. Removing culprit`, error);
}
if (!conversation.prompt) {
conversation.prompt = DEFAULT_SYSTEM_PROMPT;
}
if (!conversation.folderId) {
conversation.folderId = 0;
}
});
return updatedHistory;
return acc;
}, []);
};