-
Notifications
You must be signed in to change notification settings - Fork 1
Bug #93: Chat sessions not persisted #20
Copy link
Copy link
Open
Labels
Description
Bug #93: Chat sessions not persisted across panel dismiss
Severity: Medium
Repro
- Open any book → open AI panel → switch to Chat tab
- Send several messages, have a conversation
- Close the AI panel (swipe down or tap outside)
- Close the book and return to library
- Reopen the same book → open AI panel → Chat tab
Expected
Previous chat history is restored — at minimum for the same book.
Actual
Chat history is completely lost. A fresh empty chat appears.
Root Cause
ChatMessage is a plain Swift struct (not a SwiftData @Model). Chat history lives only in AIChatViewModel.messages: [ChatMessage] — a pure in-memory array.
Lifecycle:
Book opens → ReaderContainerView created
AI tapped → ReaderAICoordinator.setupIfNeeded() → AIChatViewModel created
User chats → messages appended to in-memory array
Panel dismissed → showAIPanel = false (VM stays alive)
Panel reopened (same session) → same VM → history visible ✓
Book closed → ReaderContainerView destroyed → ALL chat history lost ✗
App killed → same ✗
Within a single book session, history survives panel dismiss/reopen (coordinator stays alive). But closing the book or app destroys everything.
Proper Fix
Options (in order of complexity):
- SwiftData persistence: Add
@Model class ChatSession+@Model class ChatMessageRecordwith book fingerprint FK. Load on book open, save on each message. - File-based persistence: JSON-encode messages to
Documents/<bookFingerprint>/chat.json. Simple but less queryable. - Minimum viable: At least warn users that chat isn't saved, or add "Export Chat" option.
Files
vreader/Models/ChatMessage.swift— plain struct, no persistence (comment on line 7: "potential persistence")vreader/ViewModels/AIChatViewModel.swift— in-memorymessagesarray,clearHistory()methodvreader/Views/AI/AIChatView.swift— UI display, clear buttonvreader/Views/Reader/ReaderAICoordinator.swift— owns chat VM lifecyclevreader/Views/Reader/ReaderContainerView+Sheets.swift— sheet dismiss only resets tab
Refs #6
Reactions are currently unavailable