Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

26.0 KB
Raw Blame
import AppKit
import Foundation
import MessagesObserverKit
import ServiceManagement
import SwiftUI

/// What the window shows at the top: can we read Messages, is Claude-Mem up, are we listening.
enum ListenerState: Equatable {
    case stopped
    case starting
    case listening(observing: Int)
    case failed(String)
}

enum ChatFilter: String, CaseIterable, Identifiable {
    case all = "All chats"
    case groups = "Group chats"
    case observing = "Observing"
    var id: String { rawValue }
}

/// Contacts permission as the chat list cares about it. `unknown` until the
/// list has asked. `denied` keeps the "Phone ending …" names and does not ask again.
enum ContactsAccess: Equatable {
    case unknown
    case granted
    case denied
}

struct LogLine: Identifiable, Equatable {
    let id = UUID()
    let at: Date
    let level: String
    let text: String
}

@MainActor
final class AppModel: ObservableObject {
    // Setup
    @Published var access: FullDiskAccess.State = .denied
    @Published var launch: CommandLaunch?
    @Published var workerReachable = false
    @Published var workerURL = ""
    @Published var commandProblem: String?

    // Chats
    @Published var chats: [ChatItem] = []
    @Published var defaultFocus = ""
    @Published var filter: ChatFilter = .all
    @Published var search = ""
    @Published var selection: String?
    @Published var loadingChats = false
    /// Chats removed from the list only. Observing them does not change.
    @Published private(set) var hiddenGuids: Set<String>
    @Published var showingHidden = false
    @Published var contactsAccess: ContactsAccess = .unknown

    // Listener
    @Published var listener: ListenerState = .stopped
    @Published var sent = 0
    @Published var skipped = 0
    @Published var log: [LogLine] = []

    // Kept in UserDefaults. Plain @Published so the window updates when they change.
    private let defaults: UserDefaults
    @Published private(set) var source: MessageSource
    @Published var photonConfigured = false
    @Published var photonNumbers: [String] = []
    @Published var connectingPhoton = false
    @Published private var pendingSource: MessageSource?
    var selectedSource: MessageSource { pendingSource ?? source }
    private var photonWasListening: Bool {
        get { defaults.bool(forKey: "photonWasListening") }
        set { defaults.set(newValue, forKey: "photonWasListening") }
    }
    @Published var listenOnLaunch: Bool {
        didSet { defaults.set(listenOnLaunch, forKey: "listenOnLaunch") }
    }

    private var listenerID: UUID?
    private var running: RunningCommand?
    private var userStopped = false
    private var isShuttingDown = false
    private var focusSaves: [String: Task<Void, Never>] = [:]
    private var lastSave: Task<Bool, Never>?
    private var sourceGeneration = 0
    private var chatsGeneration = 0
    @Published private(set) var savingPicks = Set<String>()
    @Published private(set) var pendingOptOuts: Set<String> {
        didSet { defaults.set(Array(pendingOptOuts), forKey: "pendingOptOuts") }
    }
    private var restartAfterStop = false
    private var notedEmptyContacts = false
    /// Reads the Mac's address book. Tests put a fixed book here.
    var readAddressBook: @Sendable () throws -> [String: String] = { try ContactNames.addressBook() }

    init(defaults: UserDefaults = .standard) {
        self.defaults = defaults
        source = MessageSource(rawValue: defaults.string(forKey: "messageSource") ?? "") ?? .mac
        listenOnLaunch = defaults.object(forKey: "listenOnLaunch") as? Bool ?? true
        pendingOptOuts = Set(defaults.stringArray(forKey: "pendingOptOuts") ?? [])
        hiddenGuids = Set(defaults.stringArray(forKey: Self.hiddenChatsKey) ?? [])
    }

    static let hiddenChatsKey = "hiddenChatGuids"
    private static let contactsDigestKey = "contactsDigest"

    var needsSetup: Bool { launch == nil || commandProblem != nil || !workerReachable || (source == .mac && access != .granted) }
    var canListen: Bool {
        guard !isShuttingDown, !needsSetup, workerReachable, pendingSource == nil else { return false }
        return source == .photon ? photonConfigured && !connectingPhoton : observedCount > 0 && pendingOptOuts.isEmpty
    }

    var visibleChats: [ChatItem] {
        let query = search.trimmingCharacters(in: .whitespaces).lowercased()
        let filtered = chats.filter { chat in
            let hidden = hiddenGuids.contains(chat.guid)
            if showingHidden {
                if !hidden { return false }
            } else if hidden {
                return false
            }
            if !showingHidden {
                switch filter {
                case .all: break
                case .groups: if !chat.isGroup { return false }
                case .observing: if !chat.observed { return false }
                }
            }
            guard !query.isEmpty else { return true }
            return matches(chat, query: query)
        }
        return newestFirst(filtered)
    }

    /// Hidden chats that match the current search, while the normal list is showing.
    var hiddenMatches: Int {
        guard !showingHidden else { return 0 }
        let query = search.trimmingCharacters(in: .whitespaces).lowercased()
        guard !query.isEmpty else { return 0 }
        return chats.filter { hiddenGuids.contains($0.guid) && matches($0, query: query) }.count
    }

    var hiddenCount: Int { chats.filter { hiddenGuids.contains($0.guid) }.count }

    func isHidden(_ guid: String) -> Bool { hiddenGuids.contains(guid) }

    private func matches(_ chat: ChatItem, query: String) -> Bool {
        chat.name.lowercased().contains(query) || chat.members.contains { $0.lowercased().contains(query) }
    }

    /// Newest activity at the top. Chats with no messages go last. Equal times
    /// keep their previous order.
    private func newestFirst(_ items: [ChatItem]) -> [ChatItem] {
        items.sorted { lhs, rhs in
            switch (lhs.lastMessageDate, rhs.lastMessageDate) {
            case let (left?, right?) where left != right:
                return left > right
            case (_?, nil):
                return true
            default:
                return false
            }
        }
    }

    var observedCount: Int { chats.filter(\.observed).count }

    // MARK: Setup

    private var refreshGeneration = 0

    func selectSource(_ next: MessageSource) {
        guard !isShuttingDown else { return }
        defaults.set(next.rawValue, forKey: "messageSource")
        guard next != source else {
            pendingSource = nil
            return
        }
        if running != nil {
            pendingSource = next
            stopListening()
            return
        }
        refreshGeneration += 1
        source = next
        sourceGeneration += 1
        chatsGeneration += 1
        listener = .stopped
        selection = nil
        showingHidden = false
        chats = []
        sent = 0
        skipped = 0
        log = []
        commandProblem = nil
        workerReachable = false
        // Selecting a source explicitly gives the person a chance to inspect it before listening.
        userStopped = true
        Task { await refreshAll() }
    }

    func refreshAll() async {
        guard !isShuttingDown else { return }
        refreshGeneration += 1
        let generation = refreshGeneration
        if source == .mac { access = FullDiskAccess.check() }
        if launch == nil {
            let packaged = await Task.detached { CommandLaunch.packaged() }.value
            guard generation == refreshGeneration else { return }
            launch = packaged
        }
        guard let runner else {
            commandProblem = "The packaged observer could not be verified. Install Claude-Mem Messages again."
            return
        }
        do {
            let result = try await runner.run(source.command("check"))
            guard generation == refreshGeneration else { return }
            commandProblem = nil
            if let event = result.events.first {
                switch event {
                case let .check(messages, worker, _):
                    if messages.ok { access = .granted }
                    else if messages.reason == "no-permission" { access = .denied }
                    workerReachable = worker.reachable
                    workerURL = worker.url
                case let .photon(configured, numbers, worker):
                    photonConfigured = configured
                    photonNumbers = numbers
                    workerReachable = worker.reachable
                    workerURL = worker.url
                case let .error(_, message): commandProblem = message
                default: commandProblem = "The observer returned an unexpected response."
                }
            } else { commandProblem = "The observer did not answer. Check again." }
            if source == .mac, access == .granted {
                // Names before the list, and before listening starts, so the
                // first listener and the list share one address book. Setup
                // still in progress waits, so the prompt is not shown over it.
                if !needsSetup {
                    loadingChats = true
                    await prepareContactNames(prompt: true)
                    guard generation == refreshGeneration else {
                        loadingChats = false
                        return
                    }
                }
                await loadChats()
            }
            guard generation == refreshGeneration else { return }
            if listenOnLaunch, canListen, running == nil, !userStopped, source == .mac || photonWasListening {
                startListening()
            }
        } catch {
            guard generation == refreshGeneration else { return }
            commandProblem = error.localizedDescription
        }
    }

    func connectPhoton(projectID: String, secret: String) async {
        guard source == .photon, !connectingPhoton, running == nil, let runner else { return }
        connectingPhoton = true
        defer { connectingPhoton = false }
        do {
            let data = try JSONEncoder().encode(["projectId": projectID, "projectSecret": secret])
            let result = try await runner.run(source.command("connect"), input: data)
            guard source == .photon else { return }
            if case let .error(_, message)? = result.events.first { commandProblem = message; return }
            guard result.status == 0 else { commandProblem = "Photon could not connect to this project."; return }
            await refreshAll()
        } catch { commandProblem = error.localizedDescription }
    }

    private var runner: CommandRunner? { launch.map(CommandRunner.init(launch:)) }

    func openFullDiskAccessSettings() {
        NSWorkspace.shared.open(FullDiskAccess.settingsURL)
    }

    func revealAppInFinder() {
        NSWorkspace.shared.activateFileViewerSelecting([Bundle.main.bundleURL])
    }

    // MARK: Chats

    func loadChats() async {
        guard source == .mac, let runner else { return }
        chatsGeneration += 1
        let generation = chatsGeneration
        loadingChats = true
        defer { loadingChats = false }
        do {
            let result = try await runner.run(["imessage", "mac", "chats", "--json"])
            guard source == .mac, generation == chatsGeneration else { return }
            for event in result.events {
                if case let .chats(focus, list) = event {
                    chats = list
                    defaultFocus = focus
                    pendingOptOuts.subtract(list.filter { !$0.observed && !savingPicks.contains($0.guid) }.map(\.guid))
                }
            }
            if result.status != 0, !result.errorText.isEmpty { append("warn", result.errorText) }
        } catch {
            append("warn", "Could not list chats: \(error.localizedDescription)")
        }
    }

    func setObserved(_ guid: String, _ on: Bool) {
        let name = chats.first(where: { $0.guid == guid })?.name
        guard source == .mac, name != nil || (!on && pendingOptOuts.contains(guid)),
              savingPicks.insert(guid).inserted else { return }
        chatsGeneration += 1
        let chatName = name ?? "this chat"
        if !on { pendingOptOuts.insert(guid) }
        let generation = sourceGeneration
        Task {
            defer { savingPicks.remove(guid) }
            let saved = await pick(guid, ["--\(on ? "on" : "off")"])
            guard saved else {
                if !on, source == .mac {
                    userStopped = true
                    restartAfterStop = false
                    running?.discardAndStop()
                    append("warn", "Listening paused. Retry turning off \"\(chatName)\" to continue.")
                }
                return
            }
            pendingOptOuts.remove(guid)
            guard generation == sourceGeneration else { return }
            if let index = chats.firstIndex(where: { $0.guid == guid }) { chats[index].observed = on }
            append("info", on ? "Now observing \"\(chatName)\"." : "Stopped observing \"\(chatName)\".")
            if on, running == nil { startListening() }
        }
    }

    func retryOptOuts() {
        for guid in pendingOptOuts { setObserved(guid, false) }
    }

    /// Saves a chat's "observe for" text a moment after typing stops.
    func setFocus(_ guid: String, _ text: String) {
        guard let index = chats.firstIndex(where: { $0.guid == guid }), chats[index].focus != text else { return }
        chats[index].focus = text
        focusSaves[guid]?.cancel()
        focusSaves[guid] = Task {
            try? await Task.sleep(for: .milliseconds(700))
            guard !Task.isCancelled else { return }
            await pick(guid, ["--focus", text])
        }
    }

    func saveDefaultFocus(_ text: String) {
        guard text != defaultFocus else { return }
        defaultFocus = text
        focusSaves["__default__"]?.cancel()
        focusSaves["__default__"] = Task {
            try? await Task.sleep(for: .milliseconds(700))
            guard !Task.isCancelled else { return }
            await self.save(["imessage", "mac", "pick", "--default-focus", text, "--json"])
        }
    }

    @discardableResult
    private func pick(_ guid: String, _ flags: [String]) async -> Bool {
        await save(["imessage", "mac", "pick", guid] + flags + ["--json"])
    }

    /// Saves run one after another, so a tick and a focus edit never race.
    @discardableResult
    private func save(_ arguments: [String]) async -> Bool {
        let previous = lastSave
        let next = Task { [weak self] in
            _ = await previous?.value
            guard let self, let runner = self.runner else { return false }
            do {
                let result = try await runner.run(arguments)
                if result.status == 0, result.events.contains(.other(type: "picked")) { return true }
                if self.source == .mac { self.append("warn", result.errorText.isEmpty ? "Could not save the choice." : result.errorText) }
            } catch {
                if self.source == .mac { self.append("warn", "Could not save the choice: \(error.localizedDescription)") }
            }
            return false
        }
        lastSave = next
        return await next.value
    }

    /// Hides a chat from the list, or shows it again. This does not change
    /// whether the chat is observed: a ticked chat keeps being read.
    func setHidden(_ guid: String, _ hidden: Bool) {
        guard chats.contains(where: { $0.guid == guid }) || hiddenGuids.contains(guid) else { return }
        var next = hiddenGuids
        if hidden {
            guard next.insert(guid).inserted else { return }
        } else {
            guard next.remove(guid) != nil else { return }
        }
        hiddenGuids = next
        defaults.set(next.sorted(), forKey: Self.hiddenChatsKey)
        if !hidden, hiddenCount == 0 { showingHidden = false }
        let name = chats.first(where: { $0.guid == guid })?.name ?? "this chat"
        if hidden {
            let still = chats.first(where: { $0.guid == guid })?.observed == true
            append("info", still ? "Hid \"\(name)\" from the list. Still observing it." : "Hid \"\(name)\" from the list.")
        } else {
            append("info", "Showing \"\(name)\" in the list again.")
        }
    }

    func openContactsSettings() {
        NSWorkspace.shared.open(ContactNames.settingsURL)
    }

    /// The overview button. Reads Contacts again and always saves the names,
    /// so a missing or overwritten contacts.json is restored. When the names
    /// changed and listening is on, it starts the listener again so notes use
    /// them. An unchanged book does not interrupt listening.
    func importContacts() async {
        if ContactNames.step(ContactNames.authorization()) == .skip {
            contactsAccess = .denied
            append("info", "Contacts access is off. Allow Claude-Mem Messages under Privacy & Security → Contacts.")
            openContactsSettings()
            return
        }
        guard await allowContacts(prompt: true) else { return }
        let changed = await writeContactNames(force: true, noteIfListening: false)
        guard source == .mac else { return }
        await loadChats()
        if changed, running != nil {
            restartAfterStop = true
            running?.stop()
        }
    }

    /// Coming back from System Settings: if Contacts was allowed, pick up names.
    /// Focusing the window does not show the prompt again.
    func refreshContactNamesIfGranted() async {
        guard source == .mac, !needsSetup, !isShuttingDown else { return }
        guard ContactNames.step(ContactNames.authorization()) == .read else { return }
        contactsAccess = .granted
        if await writeContactNames(force: false, noteIfListening: true) {
            guard source == .mac else { return }
            await loadChats()
        }
    }

    /// Runs when the list opens, before listening starts. It always saves, so
    /// names come back after the data folder was reset even though this Mac
    /// still remembers the book as saved.
    private func prepareContactNames(prompt: Bool) async {
        guard await allowContacts(prompt: prompt) else { return }
        _ = await writeContactNames(force: true, noteIfListening: true)
    }

    /// True when Contacts can be read. A refusal is recorded and not asked again.
    private func allowContacts(prompt: Bool) async -> Bool {
        guard source == .mac, !isShuttingDown else { return false }
        let generation = sourceGeneration
        switch ContactNames.step(ContactNames.authorization()) {
        case .read:
            contactsAccess = .granted
            return true
        case .skip:
            contactsAccess = .denied
            return false
        case .ask:
            guard prompt else { return false }
            let granted = await ContactNames.requestAccess()
            guard generation == sourceGeneration, source == .mac, !isShuttingDown else { return false }
            if granted {
                contactsAccess = .granted
                return true
            }
            if ContactNames.step(ContactNames.authorization()) == .skip {
                contactsAccess = .denied
                append("info", "Contacts access was not given. Chats stay as \"Phone ending …\".")
            } else {
                append("warn", "Contacts could not be opened. Chats stay as \"Phone ending …\".")
            }
            return false
        }
    }

    /// Saves the address book to contacts.json. `force` saves even when the
    /// book matches the last one saved: the stored digest lives in this app's
    /// settings, and the file can be gone (data folder reset) or rewritten by
    /// Photon without it changing. Coming back to the window passes false, so
    /// an unchanged book starts no process. An empty read does not wipe names
    /// already saved. Returns true when the saved book changed.
    func writeContactNames(force: Bool, noteIfListening: Bool) async -> Bool {
        guard source == .mac, let runner else { return false }
        let book: [String: String]
        let read = readAddressBook
        do {
            book = try await Task.detached { try read() }.value
        } catch {
            append("warn", "Could not read Contacts: \(error.localizedDescription)")
            return false
        }
        guard source == .mac, !isShuttingDown else { return false }
        if book.isEmpty {
            if !notedEmptyContacts {
                notedEmptyContacts = true
                append("info", "Contacts has no names to use yet. Chats stay as \"Phone ending …\".")
            }
            return false
        }
        let digest = ContactNames.digest(book)
        let changed = digest != defaults.string(forKey: Self.contactsDigestKey)
        guard changed || force else { return false }
        do {
            let data = try JSONSerialization.data(withJSONObject: book)
            let result = try await runner.run(["imessage", "mac", "contacts", "--replace", "--json"], input: data)
            guard source == .mac else { return false }
            guard result.status == 0 else {
                append("warn", result.errorText.isEmpty ? "Could not save contact names." : result.errorText)
                return false
            }
            defaults.set(digest, forKey: Self.contactsDigestKey)
            append("info", "Using \(book.count) names from Contacts.")
            if changed, noteIfListening, running != nil {
                append("info", "Notes keep the previous names until listening starts again. Use Names from Contacts applies them now.")
            }
            return changed
        } catch {
            append("warn", "Could not save contact names: \(error.localizedDescription)")
            return false
        }
    }

    // MARK: Listener

    func startListening() {
        guard running == nil, canListen, let runner else { return }
        let id = UUID()
        listenerID = id
        userStopped = false
        listener = .starting
        if source == .photon { photonWasListening = false }
        do {
            running = try runner.stream(
                source.command("observe"),
                onEvent: { [weak self] event in Task { @MainActor in self?.handle(event, from: id) } },
                onStderr: { [weak self] text in Task { @MainActor in guard self?.listenerID == id, self?.userStopped == false else { return }; self?.append("warn", text) } },
                onExit: { [weak self] status in Task { @MainActor in guard self?.listenerID == id else { return }; self?.listenerExited(status) } }
            )
        } catch {
            listenerID = nil
            listenerFailed(error.localizedDescription)
        }
    }

    func stopListening() {
        guard !isShuttingDown else { return }
        userStopped = true
        restartAfterStop = false
        if source == .photon { photonWasListening = false }
        running?.stop()
    }

    func handle(_ event: CommandEvent, from id: UUID? = nil) {
        // Exit still owns cleanup for a stopping process. Its late output must
        // not change the active source, status, or the person's launch choice.
        guard !isShuttingDown, !userStopped, id == nil || id == listenerID else { return }
        if case .failed = listener { return }
        switch event {
        case let .started(observing):
            listener = .listening(observing: observing)
            if source == .photon {
                photonWasListening = true
                append("info", "Listening to groups that include the Photon line.")
            } else { append("info", "Listening for new messages in \(observing) chat\(observing == 1 ? "" : "s").") }
        case .reconnecting:
            listener = .starting
        case let .status(observing, sent, skipped):
            if source == .mac, case .listening = listener { listener = .listening(observing: observing) }
            self.sent = sent
            self.skipped = skipped
        case let .log(level, message):
            append(level, message)
            if message.hasPrefix("Sent ") { sent += 1 }
        case let .error(reason, message):
            if reason == "no-permission" { access = .denied }
            listenerFailed(message)
            append("warn", message)
        case .stopped:
            listener = .stopped
        default:
            break
        }
    }

    private func listenerExited(_ status: Int32) {
        running = nil
        listenerID = nil
        if source == .photon { photonWasListening = false }
        if let next = pendingSource {
            pendingSource = nil
            selectSource(next)
            return
        }
        if restartAfterStop, !userStopped {
            restartAfterStop = false
            listener = .stopped
            startListening()
            return
        }
        if case .failed = listener { return }
        listener = .stopped
        if status != 0 && !userStopped {
            listener = .failed("The listener stopped (exit \(status)). See Activity.")
        }
    }

    private func listenerFailed(_ message: String) {
        if source == .photon { photonWasListening = false }
        listener = .failed(message)
    }

    func append(_ level: String, _ text: String) {
        log.append(LogLine(at: Date(), level: level, text: text))
        if log.count > 200 { log.removeFirst(log.count - 200) }
    }

    func shutdown() {
        guard !isShuttingDown else { return }
        isShuttingDown = true
        refreshGeneration += 1
        // The app is quitting. Preserve whether a healthy Photon listener was
        // active and ignore process events caused by our own teardown.
        listenerID = nil
        running?.stop()
        running = nil
    }

    // MARK: Open at login

    var opensAtLogin: Bool {
        get { SMAppService.mainApp.status == .enabled }
        set {
            do {
                if newValue { try SMAppService.mainApp.register() } else { try SMAppService.mainApp.unregister() }
            } catch {
                append("warn", "Could not change Open at Login: \(error.localizedDescription)")
            }
            objectWillChange.send()
        }
    }
}