SPB Git

Repository Public

Search.swift 4.8 KB 89 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatConversations/Sources/Search.swift3// Description: Full-text search (FTS5) over message content and conversation titles, with safe query building and highlighted snippets.4import Foundation5import GRDB6import LocalChatCore78public struct SearchHit: Sendable, Hashable, Identifiable {9    /// Markers wrapped around matched terms in `snippet` (control characters that never occur in chat text).10    public static let highlightStart: Character = "\u{02}"11    public static let highlightEnd: Character = "\u{03}"1213    public enum Kind: String, Sendable { case title, message }14    public var kind: Kind15    public var conversationID: UUID16    public var conversationTitle: String17    public var messageID: UUID?           // nil for a title hit18    public var role: ChatMessage.Role?19    public var snippet: String            // contains highlightStart / highlightEnd around matches20    public var rank: Double               // bm25: lower is better21    public var id: String { (messageID ?? conversationID).uuidString + kind.rawValue }2223    /// The snippet split into (text, isMatch) runs, ready for display.24    public var snippetRuns: [(text: String, isMatch: Bool)] {25        var runs: [(String, Bool)] = []; var current = ""; var inMatch = false26        for ch in snippet {27            if ch == Self.highlightStart || ch == Self.highlightEnd {28                if !current.isEmpty { runs.append((current, inMatch)); current = "" }29                inMatch = ch == Self.highlightStart30            } else { current.append(ch) }31        }32        if !current.isEmpty { runs.append((current, inMatch)) }33        return runs34    }35    public var plainSnippet: String { snippet.filter { $0 != Self.highlightStart && $0 != Self.highlightEnd } }3637    public static func == (a: SearchHit, b: SearchHit) -> Bool { a.id == b.id && a.snippet == b.snippet && a.rank == b.rank }38    public func hash(into hasher: inout Hasher) { hasher.combine(id) }39}4041public enum SearchQuery {42    /// Turns free user input into a safe FTS5 expression: every word becomes a quoted prefix term, all required. Returns nil when nothing searchable remains.43    public static func ftsExpression(_ input: String) -> String? {44        let terms = input.split(whereSeparator: { !($0.isLetter || $0.isNumber) }).map(String.init).filter { !$0.isEmpty }.prefix(12)45        guard !terms.isEmpty else { return nil }46        return terms.map { "\"\($0)\"*" }.joined(separator: " ")47    }48}4950extension ConversationStore {51    /// Searches titles and messages of live conversations. `includeArchived` false hides archived conversations.52    public func search(_ query: String, limit: Int = 50, includeArchived: Bool = true) throws -> [SearchHit] {53        guard let expression = SearchQuery.ftsExpression(query) else { return [] }54        return try db.read { db in55            let rows = try Row.fetchAll(db, sql: """56                SELECT s.kind AS kind, s.conversationID AS cid, s.messageID AS mid, s.role AS role,57                       snippet(search, 0, char(2), char(3), '…', 18) AS snip, bm25(search) AS rank, c.title AS title58                FROM search s JOIN conversation c ON c.id = s.conversationID59                WHERE search MATCH ? AND c.deletedAt IS NULL \(includeArchived ? "" : "AND c.isArchived = 0")60                ORDER BY rank LIMIT ?61                """, arguments: [expression, limit])62            return rows.compactMap { row -> SearchHit? in63                guard let cid = UUID(uuidString: row["cid"] as String) else { return nil }64                let mid = (row["mid"] as String?).flatMap(UUID.init(uuidString:))65                return SearchHit(kind: (row["kind"] as String) == "c" ? .title : .message, conversationID: cid, conversationTitle: row["title"],66                                 messageID: mid, role: (row["role"] as String?).flatMap(ChatMessage.Role.init(rawValue:)),67                                 snippet: row["snip"], rank: row["rank"])68            }69        }70    }7172    /// Rebuilds the search index from the stored rows (repair tool).73    public func rebuildSearchIndex() throws {74        try db.write { db in75            try db.execute(sql: "DELETE FROM search")76            try db.execute(sql: """77                INSERT INTO search(rowid, content, kind, conversationID, messageID, role)78                SELECT -rowid, title, 'c', id, NULL, NULL FROM conversation WHERE deletedAt IS NULL79                """)80            try db.execute(sql: """81                INSERT INTO search(rowid, content, kind, conversationID, messageID, role)82                SELECT m.rowid, json_extract(m.json, '$.content'), 'm', m.conversationID, m.id, m.role FROM message m83                JOIN conversation c ON c.id = m.conversationID84                WHERE m.deletedAt IS NULL AND c.deletedAt IS NULL AND json_extract(m.json, '$.content') <> ''85                """)86        }87    }88}89
Files main
Menu

Theme