Skip to content

Commit

Permalink
feat: Add chat history backup and restore functionality with Chrome e…
Browse files Browse the repository at this point in the history
…xtension support
  • Loading branch information
sidbetatester committed Nov 14, 2024
1 parent 3fa4b87 commit 8e79197
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
Empty file.
58 changes: 51 additions & 7 deletions app/components/sidebar/Menu.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,69 @@ export function Menu() {
version: backupData.version,
hasHistory: !!backupData.history,
historyIsArray: Array.isArray(backupData.history),
historyLength: backupData.history?.length
historyLength: backupData.history?.length,
rawKeys: Object.keys(backupData)
});

if (!backupData.version || !backupData.history || !Array.isArray(backupData.history)) {
throw new Error('Invalid backup file format');
}

if (!db) {
throw new Error('Database not initialized');
}

let chatHistory;

// Handle different backup formats
if (backupData.version && backupData.history) {
// Our standard format
chatHistory = backupData.history;
} else if (backupData.boltHistory) {
// Chrome extension IndexedDB backup format
chatHistory = Object.values(backupData.boltHistory.chats || {});
logger.info('Detected Chrome extension backup format', {
itemCount: chatHistory.length,
sampleItem: chatHistory[0]
});
} else if (Array.isArray(backupData)) {
// Direct array format
chatHistory = backupData;
} else {
// Try to find any object with chat-like properties
const possibleChats = Object.values(backupData).find(value =>
Array.isArray(value) ||
(typeof value === 'object' && value !== null && 'messages' in value)
);

if (possibleChats) {
chatHistory = Array.isArray(possibleChats) ? possibleChats : [possibleChats];
logger.info('Found possible chat data in alternate format', {
itemCount: chatHistory.length,
sampleItem: chatHistory[0]
});
} else {
throw new Error('Unrecognized backup file format');
}
}

// Validate and normalize chat items
const normalizedHistory = chatHistory.map(item => {
if (!item.id || !Array.isArray(item.messages)) {
throw new Error('Invalid chat item format');
}
return {
id: item.id,
messages: item.messages,
urlId: item.urlId || item.id,
description: item.description || `Imported chat ${item.id}`
};
});

// Store each chat history item
logger.info('Starting import of chat history items');
for (const item of backupData.history) {
for (const item of normalizedHistory) {
logger.info('Importing chat item:', { id: item.id, description: item.description });
await setMessages(db, item.id, item.messages, item.urlId, item.description);
}

toast.success('Chat history imported successfully');
toast.success(`Successfully imported ${normalizedHistory.length} chats`);
// Reload the page to show imported chats
window.location.reload();
} catch (error) {
Expand Down
Empty file added app/utils/backup.ts
Empty file.

0 comments on commit 8e79197

Please sign in to comment.