()
| 47 | |
| 48 | // Save chats to localStorage with error handling |
| 49 | function saveChatsToStorage() { |
| 50 | if (!window.Alpine || !Alpine.store("chat")) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | const chatStore = Alpine.store("chat"); |
| 56 | const data = { |
| 57 | chats: chatStore.chats.map(chat => ({ |
| 58 | id: chat.id, |
| 59 | name: chat.name, |
| 60 | model: chat.model, |
| 61 | history: chat.history, |
| 62 | systemPrompt: chat.systemPrompt, |
| 63 | mcpMode: chat.mcpMode, |
| 64 | tokenUsage: chat.tokenUsage, |
| 65 | contextSize: chat.contextSize, |
| 66 | createdAt: chat.createdAt, |
| 67 | updatedAt: chat.updatedAt |
| 68 | })), |
| 69 | activeChatId: chatStore.activeChatId, |
| 70 | lastSaved: Date.now() |
| 71 | }; |
| 72 | |
| 73 | const jsonData = JSON.stringify(data); |
| 74 | localStorage.setItem(CHATS_STORAGE_KEY, jsonData); |
| 75 | return true; |
| 76 | } catch (error) { |
| 77 | // Handle quota exceeded or other storage errors |
| 78 | if (error.name === 'QuotaExceededError' || error.code === 22) { |
| 79 | console.warn('localStorage quota exceeded. Consider cleaning up old chats.'); |
| 80 | // Try to save without history (last resort) |
| 81 | try { |
| 82 | const chatStore = Alpine.store("chat"); |
| 83 | const data = { |
| 84 | chats: chatStore.chats.map(chat => ({ |
| 85 | id: chat.id, |
| 86 | name: chat.name, |
| 87 | model: chat.model, |
| 88 | history: [], // Clear history to save space |
| 89 | systemPrompt: chat.systemPrompt, |
| 90 | mcpMode: chat.mcpMode, |
| 91 | tokenUsage: chat.tokenUsage, |
| 92 | contextSize: chat.contextSize, |
| 93 | createdAt: chat.createdAt, |
| 94 | updatedAt: chat.updatedAt |
| 95 | })), |
| 96 | activeChatId: chatStore.activeChatId, |
| 97 | lastSaved: Date.now() |
| 98 | }; |
| 99 | localStorage.setItem(CHATS_STORAGE_KEY, JSON.stringify(data)); |
| 100 | return true; |
| 101 | } catch (e2) { |
| 102 | console.error('Failed to save chats even without history:', e2); |
| 103 | return false; |
| 104 | } |
| 105 | } else { |
| 106 | console.error('Error saving chats to localStorage:', error); |
no test coverage detected