Skip to content

Bug #93: Chat sessions not persisted #20

@lllyys

Description

@lllyys

Bug #93: Chat sessions not persisted across panel dismiss

Severity: Medium

Repro

  1. Open any book → open AI panel → switch to Chat tab
  2. Send several messages, have a conversation
  3. Close the AI panel (swipe down or tap outside)
  4. Close the book and return to library
  5. 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):

  1. SwiftData persistence: Add @Model class ChatSession + @Model class ChatMessageRecord with book fingerprint FK. Load on book open, save on each message.
  2. File-based persistence: JSON-encode messages to Documents/<bookFingerprint>/chat.json. Simple but less queryable.
  3. 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-memory messages array, clearHistory() method
  • vreader/Views/AI/AIChatView.swift — UI display, clear button
  • vreader/Views/Reader/ReaderAICoordinator.swift — owns chat VM lifecycle
  • vreader/Views/Reader/ReaderContainerView+Sheets.swift — sheet dismiss only resets tab

Refs #6

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingseverity:mediumMedium severity

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions