Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

3.8 KB
Raw Blame
import Foundation

/// The kinds of content the sidebar search indexes in Zeppelin. Each is one
/// record-only namespace, so results are grouped by kind rather than ranked across
/// kinds: BM25 scores from different namespaces are not comparable.
nonisolated enum SearchIndexKind: String, CaseIterable, Sendable {
    case history = "search-history"
    case transcripts = "search-transcripts"
    case chats = "search-chats"
}

/// One row as the index sees it: identity, a revision that rises when the text
/// changes, and the text itself. The stores build these; the index never reads a
/// store directly.
nonisolated struct SearchIndexRecord: Sendable, Equatable {
    let id: UUID
    /// Compared with what the namespace holds. A higher value re-indexes the row.
    /// Rows whose text never changes use 1; chats use their update time, and history
    /// entries the revision a restore bumped.
    let revision: UInt64
    let timestamp: Date
    let text: String

    init(id: UUID, revision: UInt64 = 1, timestamp: Date, text: String) {
        self.id = id
        self.revision = revision
        self.timestamp = timestamp
        self.text = text
    }

    /// Joins the searchable fields of a row. Empty fields are dropped so they do not
    /// add blank lines to the text.
    static func joined(_ parts: [String]) -> String {
        parts
            .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
            .filter { !$0.isEmpty }
            .joined(separator: "\n")
    }
}

extension TranscriptionHistoryEntry {
    /// What was pasted, plus where. Raw text is left out: the user remembers what
    /// landed in the document, and `processedText` is that text whether or not AI
    /// cleanup ran.
    ///
    /// An entry is immutable, so its revision only moves when a restore replaces the
    /// id with different text; everything else stays at the revision it was indexed at.
    nonisolated var searchRecord: SearchIndexRecord {
        SearchIndexRecord(
            id: self.id,
            revision: self.searchRevision ?? 1,
            timestamp: self.timestamp,
            text: SearchIndexRecord.joined([self.processedText, self.appName, self.windowTitle])
        )
    }
}

extension FileTranscriptionEntry {
    /// `text` only. When diarization ran, `text` is already the speaker segments
    /// joined together, so indexing both would count every word twice.
    ///
    /// Renaming advances the persisted revision without changing the recording date.
    nonisolated var searchRecord: SearchIndexRecord {
        SearchIndexRecord(
            id: self.id,
            revision: self.searchRevision ?? 1,
            timestamp: self.timestamp,
            text: SearchIndexRecord.joined([self.displayTitle, self.fileName, self.text])
        )
    }
}

extension ChatSession {
    /// One record per session. Message ids are regenerated every time a chat is
    /// reloaded, so they cannot be keys; the session id is stable and the persisted
    /// search revision rises on every save regardless of wall-clock corrections.
    ///
    /// Archived sessions belong only in the Command Mode archive. Also returns
    /// `nil` for a malformed id, which a hand-edited defaults file could contain.
    var searchRecord: SearchIndexRecord? {
        guard !self.isArchived, let id = UUID(uuidString: self.id) else { return nil }
        var parts = [self.title]
        for message in self.messages {
            parts.append(message.content)
            if let command = message.toolCall?.command {
                parts.append(command)
            }
        }
        return SearchIndexRecord(
            id: id,
            revision: self.searchRevision ?? UInt64(max(1, self.updatedAt.timeIntervalSince1970 * 1000)),
            timestamp: self.updatedAt,
            text: SearchIndexRecord.joined(parts)
        )
    }
}