From 210e9da5f92fce52ac7fc3c294860ea3235f843f Mon Sep 17 00:00:00 2001 From: ddiu8081 Date: Mon, 1 May 2023 23:25:14 +0800 Subject: [PATCH] feat: data migration --- src/stores/conversation.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/stores/conversation.ts b/src/stores/conversation.ts index 3a3b16d2..8d09961f 100644 --- a/src/stores/conversation.ts +++ b/src/stores/conversation.ts @@ -18,10 +18,37 @@ export const conversationMapSortList = computed(conversationMap, (map) => { return Object.values(map).sort((a, b) => b.lastUseTime - a.lastUseTime) }) +const migrateConversationStoreIfNeeded = () => { + const rawData = conversationMap.get() + Object.values(rawData).forEach((conversation) => { + // @ts-expect-error migrate old data + if (conversation.providerId && conversation.conversationType) { + const typeDict = { + single: 'chat_single', + continuous: 'chat_continuous', + image: 'image_generation', + } + const newConversationData = { + id: conversation.id, + // @ts-expect-error migrate old data + bot: `${conversation.providerId}:${typeDict[conversation.conversationType] || 'chat_single'}`, + name: conversation.name, + icon: conversation.icon, + systemInfo: conversation.systemInfo, + mockMessages: conversation.mockMessages, + lastUseTime: conversation.lastUseTime, + } + conversationMap.setKey(conversation.id, newConversationData) + db.setItem(conversation.id, newConversationData) + } + }) +} + export const rebuildConversationStore = async() => { const data = await db.exportData() || {} conversationMap.set(data) // conversationMap.set(conversationMapData) + migrateConversationStoreIfNeeded() } export const addConversation = action(conversationMap, 'addConversation', (map, instance?: Partial) => {