SPB Git

Repository Public

KnowledgeStore.swift 24.8 KB 383 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatKnowledge/Sources/KnowledgeStore.swift3// Description: SQLite (GRDB) store of collections, documents, extracted sections, chunks, Float32 vectors and an FTS5 index; exact vector, BM25 and hybrid (RRF, MMR) search.4import Foundation5import GRDB6import LocalChatCore7import Synchronization89public struct StoredChunk: Sendable, Hashable, Identifiable {10    public var rowID: Int6411    public var documentID: UUID12    public var collectionID: UUID13    public var index: Int14    public var page: Int?15    public var heading: String?16    public var text: String17    public var hasVector: Bool18    public var id: Int64 { rowID }19}2021public struct StoreCounts: Sendable, Equatable {22    public var collections = 0, documents = 0, sections = 0, chunks = 0, vectors = 0, ftsRows = 023}2425public struct KnowledgeSearchResult: Sendable {26    public var hits: [KnowledgeHit]27    public var usedVectors: Bool28    public var usedKeywords: Bool29}3031public enum KnowledgeStoreError: LocalizedError, Sendable, Equatable {32    case collectionNotFound, documentNotFound33    case dimensionMismatch(expected: Int, got: Int)34    case vectorCountMismatch(chunks: Int, vectors: Int)35    public var errorDescription: String? {36        switch self {37        case .collectionNotFound: "The collection no longer exists."38        case .documentNotFound: "The document no longer exists."39        case .dimensionMismatch(let e, let g): "Vector size mismatch: the collection uses \(e) dimensions, the embedding model returned \(g)."40        case .vectorCountMismatch(let c, let v): "The embedding model returned \(v) vectors for \(c) chunks."41        }42    }43}4445public final class KnowledgeStore: Sendable {46    private let db: any DatabaseWriter47    private struct Cache { var generation: [UUID: Int] = [:]; var indexes: [UUID: VectorIndex] = [:] }48    private let cache = Mutex(Cache())4950    /// File-backed store (WAL). Deleted content is overwritten (`secure_delete`).51    public convenience init(url: URL) throws {52        try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)53        var config = Configuration()54        config.prepareDatabase { db in try db.execute(sql: "PRAGMA secure_delete = ON") }55        try self.init(writer: DatabasePool(path: url.path, configuration: config))56    }57    public static func inMemory() throws -> KnowledgeStore { try KnowledgeStore(writer: DatabaseQueue()) }5859    private init(writer: any DatabaseWriter) throws {60        db = writer61        var migrator = DatabaseMigrator()62        migrator.registerMigration("v1") { db in63            try db.execute(sql: """64            CREATE TABLE collection (id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, symbol TEXT NOT NULL, embedderID TEXT NOT NULL, dimension INTEGER NOT NULL,65              chunkSize INTEGER NOT NULL, chunkOverlap INTEGER NOT NULL, createdAt DOUBLE NOT NULL, updatedAt DOUBLE NOT NULL, deletedAt DOUBLE);66            CREATE TABLE document (id TEXT PRIMARY KEY NOT NULL, collectionID TEXT NOT NULL, name TEXT NOT NULL, kind TEXT NOT NULL, byteCount INTEGER NOT NULL,67              characterCount INTEGER NOT NULL, chunkCount INTEGER NOT NULL, contentHash TEXT NOT NULL, status TEXT NOT NULL, errorText TEXT, addedAt DOUBLE NOT NULL,68              language TEXT);69            CREATE INDEX document_collection ON document(collectionID, contentHash);70            CREATE TABLE section (documentID TEXT NOT NULL, idx INTEGER NOT NULL, page INTEGER, heading TEXT, text TEXT NOT NULL, PRIMARY KEY (documentID, idx)) WITHOUT ROWID;71            CREATE TABLE chunk (id INTEGER PRIMARY KEY AUTOINCREMENT, documentID TEXT NOT NULL, collectionID TEXT NOT NULL, idx INTEGER NOT NULL, page INTEGER,72              heading TEXT, text TEXT NOT NULL, vector BLOB);73            CREATE INDEX chunk_document ON chunk(documentID, idx);74            CREATE INDEX chunk_collection ON chunk(collectionID);75            CREATE VIRTUAL TABLE chunk_fts USING fts5(text, content='chunk', content_rowid='id', tokenize='unicode61 remove_diacritics 2');76            CREATE TRIGGER chunk_ai AFTER INSERT ON chunk BEGIN INSERT INTO chunk_fts(rowid, text) VALUES (new.id, new.text); END;77            CREATE TRIGGER chunk_ad AFTER DELETE ON chunk BEGIN INSERT INTO chunk_fts(chunk_fts, rowid, text) VALUES ('delete', old.id, old.text); END;78            """)79        }80        try migrator.migrate(writer)81    }8283    // MARK: Collections8485    public func collections(includingDeleted: Bool = false) throws -> [KnowledgeCollection] {86        try db.read { db in87            try Row.fetchAll(db, sql: "SELECT * FROM collection\(includingDeleted ? "" : " WHERE deletedAt IS NULL") ORDER BY name COLLATE NOCASE, createdAt").map(Self.collection)88        }89    }90    public func collection(_ id: UUID) throws -> KnowledgeCollection? {91        try db.read { db in try Row.fetchOne(db, sql: "SELECT * FROM collection WHERE id = ?", arguments: [id.uuidString]).map(Self.collection) }92    }93    /// Inserts or updates the collection's settings. Changing `embedderID` / `dimension` here does not touch vectors: use `KnowledgeIndexer.reindex`.94    public func saveCollection(_ c: KnowledgeCollection) throws {95        try db.write { db in try Self.write(c, db) }96        invalidate(c.id)97    }98    /// Removes the collection and everything indexed in it (documents, sections, chunks, vectors, full-text rows).99    public func deleteCollection(_ id: UUID) throws {100        try db.write { db in101            let key = id.uuidString102            try db.execute(sql: "DELETE FROM chunk WHERE collectionID = ?", arguments: [key])103            try db.execute(sql: "DELETE FROM section WHERE documentID IN (SELECT id FROM document WHERE collectionID = ?)", arguments: [key])104            try db.execute(sql: "DELETE FROM document WHERE collectionID = ?", arguments: [key])105            try db.execute(sql: "DELETE FROM collection WHERE id = ?", arguments: [key])106        }107        invalidate(id)108    }109110    // MARK: Documents111112    public func documents(in collectionID: UUID) throws -> [KnowledgeDocument] {113        try db.read { db in try Row.fetchAll(db, sql: "SELECT * FROM document WHERE collectionID = ? ORDER BY addedAt, name", arguments: [collectionID.uuidString]).map(Self.document) }114    }115    public func document(_ id: UUID) throws -> KnowledgeDocument? {116        try db.read { db in try Row.fetchOne(db, sql: "SELECT * FROM document WHERE id = ?", arguments: [id.uuidString]).map(Self.document) }117    }118    public func document(inCollection collectionID: UUID, contentHash: String) throws -> KnowledgeDocument? {119        try db.read { db in120            try Row.fetchOne(db, sql: "SELECT * FROM document WHERE collectionID = ? AND contentHash = ? ORDER BY addedAt LIMIT 1",121                             arguments: [collectionID.uuidString, contentHash]).map(Self.document)122        }123    }124    public func saveDocument(_ d: KnowledgeDocument) throws { try db.write { db in try Self.write(d, db) } }125126    /// Removes the document with its sections, chunks, vectors and full-text rows.127    public func deleteDocument(_ id: UUID) throws {128        let collection: UUID? = try db.write { db in129            let key = id.uuidString130            let c = try String.fetchOne(db, sql: "SELECT collectionID FROM document WHERE id = ?", arguments: [key]).flatMap(UUID.init(uuidString:))131            try db.execute(sql: "DELETE FROM chunk WHERE documentID = ?", arguments: [key])132            try db.execute(sql: "DELETE FROM section WHERE documentID = ?", arguments: [key])133            try db.execute(sql: "DELETE FROM document WHERE id = ?", arguments: [key])134            return c135        }136        if let collection { invalidate(collection) }137    }138139    public func chunks(of documentID: UUID) throws -> [StoredChunk] {140        try db.read { db in141            try Row.fetchAll(db, sql: "SELECT id, documentID, collectionID, idx, page, heading, text, vector IS NOT NULL AS hasVector FROM chunk WHERE documentID = ? ORDER BY idx",142                             arguments: [documentID.uuidString]).map(Self.chunk)143        }144    }145146    /// The text extracted when the document was added (used to re-chunk without the original file).147    public func extractedDocument(_ id: UUID) throws -> ExtractedDocument? {148        try db.read { db in149            guard let row = try Row.fetchOne(db, sql: "SELECT kind, language FROM document WHERE id = ?", arguments: [id.uuidString]) else { return nil }150            let sections = try Row.fetchAll(db, sql: "SELECT page, heading, text FROM section WHERE documentID = ? ORDER BY idx", arguments: [id.uuidString])151                .map { ExtractedDocument.Section(text: $0["text"], page: $0["page"], heading: $0["heading"]) }152            return ExtractedDocument(kind: DocumentKind(rawValue: row["kind"]) ?? .txt, language: row["language"], sections: sections)153        }154    }155156    public struct IndexedContent: Sendable {157        public var document: KnowledgeDocument158        public var extracted: ExtractedDocument?     // nil keeps the stored sections159        public var chunks: [TextChunk]160        public var vectors: [[Float]]161        public init(document: KnowledgeDocument, extracted: ExtractedDocument?, chunks: [TextChunk], vectors: [[Float]]) {162            self.document = document; self.extracted = extracted; self.chunks = chunks; self.vectors = vectors163        }164    }165166    /// Atomically replaces what is indexed for these documents (all in one collection). `collection`, when given, is saved in the same transaction (embedder change).167    public func replaceIndex(_ contents: [IndexedContent], collection updated: KnowledgeCollection? = nil) throws {168        guard let collectionID = updated?.id ?? contents.first?.document.collectionID else { return }169        try db.write { db in170            if let updated { try Self.write(updated, db) }171            guard let dimension = try Int.fetchOne(db, sql: "SELECT dimension FROM collection WHERE id = ?", arguments: [collectionID.uuidString]) else { throw KnowledgeStoreError.collectionNotFound }172            for content in contents {173                guard content.chunks.count == content.vectors.count else { throw KnowledgeStoreError.vectorCountMismatch(chunks: content.chunks.count, vectors: content.vectors.count) }174                let key = content.document.id.uuidString175                try db.execute(sql: "DELETE FROM chunk WHERE documentID = ?", arguments: [key])176                if let extracted = content.extracted {177                    try db.execute(sql: "DELETE FROM section WHERE documentID = ?", arguments: [key])178                    let insert = try db.makeStatement(sql: "INSERT INTO section (documentID, idx, page, heading, text) VALUES (?, ?, ?, ?, ?)")179                    for (i, s) in extracted.sections.enumerated() { try insert.execute(arguments: [key, i, s.page, s.heading, s.text]) }180                }181                let insert = try db.makeStatement(sql: "INSERT INTO chunk (documentID, collectionID, idx, page, heading, text, vector) VALUES (?, ?, ?, ?, ?, ?, ?)")182                for (chunk, vector) in zip(content.chunks, content.vectors) {183                    guard vector.count == dimension else { throw KnowledgeStoreError.dimensionMismatch(expected: dimension, got: vector.count) }184                    try insert.execute(arguments: [key, collectionID.uuidString, chunk.index, chunk.page, chunk.heading, chunk.text, VectorMath.data(VectorMath.normalized(vector))])185                }186                var document = content.document187                document.collectionID = collectionID; document.chunkCount = content.chunks.count; document.status = .ready; document.errorText = nil188                if let extracted = content.extracted { document.characterCount = extracted.characterCount }189                try Self.write(document, db, language: content.extracted?.language)190            }191        }192        invalidate(collectionID)193    }194195    public func counts() throws -> StoreCounts {196        try db.read { db in197            func n(_ sql: String) throws -> Int { try Int.fetchOne(db, sql: sql) ?? 0 }198            return StoreCounts(collections: try n("SELECT count(*) FROM collection"), documents: try n("SELECT count(*) FROM document"),199                               sections: try n("SELECT count(*) FROM section"), chunks: try n("SELECT count(*) FROM chunk"),200                               vectors: try n("SELECT count(*) FROM chunk WHERE vector IS NOT NULL"), ftsRows: try n("SELECT count(*) FROM chunk_fts_docsize"))201        }202    }203204    public func counts(inCollection id: UUID) throws -> StoreCounts {205        try db.read { db in206            func n(_ sql: String) throws -> Int { try Int.fetchOne(db, sql: sql, arguments: [id.uuidString]) ?? 0 }207            let chunks = try n("SELECT count(*) FROM chunk WHERE collectionID = ?")208            return StoreCounts(collections: try n("SELECT count(*) FROM collection WHERE id = ?"), documents: try n("SELECT count(*) FROM document WHERE collectionID = ?"),209                               sections: try n("SELECT count(*) FROM section WHERE documentID IN (SELECT id FROM document WHERE collectionID = ?)"), chunks: chunks,210                               vectors: try n("SELECT count(*) FROM chunk WHERE collectionID = ? AND vector IS NOT NULL"), ftsRows: chunks)211        }212    }213214    /// Rebuilds the file so that the space of deleted content is returned to the system.215    public func vacuum() throws {216        try db.writeWithoutTransaction { db in217            try db.execute(sql: "INSERT INTO chunk_fts(chunk_fts) VALUES ('optimize')")218            try db.execute(sql: "VACUUM")219        }220        try? db.writeWithoutTransaction { db in try db.execute(sql: "PRAGMA wal_checkpoint(TRUNCATE)") }221    }222223    // MARK: Search224225    /// Exact cosine top-k inside one collection. `query` does not need to be normalized.226    public func vectorSearch(collection id: UUID, query: [Float], limit: Int) throws -> [ScoredChunk] {227        try index(for: id).search(VectorMath.normalized(query), limit: limit)228    }229230    /// BM25 (FTS5) over the given collections; scores are positive, higher is better.231    public func keywordSearch(_ text: String, collections: [UUID], limit: Int) throws -> [ScoredChunk] {232        guard let match = Self.ftsQuery(text), !collections.isEmpty, limit > 0 else { return [] }233        return try db.read { db in234            let marks = databaseQuestionMarks(count: collections.count)235            let rows = try Row.fetchAll(db, sql: """236                SELECT chunk.id AS id, bm25(chunk_fts) AS rank FROM chunk_fts JOIN chunk ON chunk.id = chunk_fts.rowid237                WHERE chunk_fts MATCH ? AND chunk.collectionID IN (\(marks)) ORDER BY rank LIMIT ?238                """, arguments: StatementArguments([match] + collections.map(\.uuidString) + [limit]))239            return rows.map { ScoredChunk(rowID: $0["id"], score: -($0["rank"] as Double)) }240        }241    }242243    /// Hybrid search: reciprocal-rank fusion of the vector results (one list per collection that has a query vector) and the BM25 results,244    /// optionally diversified with MMR (`mmrLambda` in 0…1, 1 = relevance only). With vectors only, scores are cosine similarities.245    public func search(text: String, queryVectors: [UUID: [Float]], collections: [UUID], limit: Int, useKeywords: Bool = true, mmrLambda: Double? = nil) throws -> KnowledgeSearchResult {246        guard limit > 0, !collections.isEmpty else { return KnowledgeSearchResult(hits: [], usedVectors: false, usedKeywords: false) }247        let pool = max(limit * 4, 24)248        var lists: [[ScoredChunk]] = []; var cosine: [Int64: Double] = [:]; var owner: [Int64: UUID] = [:]249        for id in collections {250            guard let q = queryVectors[id] else { continue }251            let found = try vectorSearch(collection: id, query: q, limit: pool)252            if !found.isEmpty { lists.append(found); for f in found { cosine[f.rowID] = f.score; owner[f.rowID] = id } }253        }254        let usedVectors = !lists.isEmpty255        let keywords = useKeywords ? try keywordSearch(text, collections: collections, limit: pool) : []256        var ranked: [ScoredChunk]257        if keywords.isEmpty && lists.count == 1 { ranked = lists[0] }258        else {259            var fused: [Int64: Double] = [:]260            for list in lists + (keywords.isEmpty ? [] : [keywords]) { for (rank, item) in list.enumerated() { fused[item.rowID, default: 0] += 1.0 / Double(60 + rank + 1) } }261            ranked = fused.map { ScoredChunk(rowID: $0.key, score: $0.value) }.sorted { ($0.score, $1.rowID) > ($1.score, $0.rowID) }262        }263        if let lambda = mmrLambda, usedVectors, ranked.count > 1 { ranked = try diversify(ranked, owner: owner, limit: limit, lambda: min(1, max(0, lambda))) }264        return KnowledgeSearchResult(hits: try hits(for: Array(ranked.prefix(limit))), usedVectors: usedVectors, usedKeywords: !keywords.isEmpty)265    }266267    /// Maximal marginal relevance over the candidates (similarity is known only between chunks of the same collection).268    private func diversify(_ candidates: [ScoredChunk], owner: [Int64: UUID], limit: Int, lambda: Double) throws -> [ScoredChunk] {269        let top = candidates.first?.score ?? 1270        var remaining = candidates; var selected: [ScoredChunk] = []271        var positions: [Int64: (VectorIndex, Int)] = [:]272        for c in candidates { if let id = owner[c.rowID] { let ix = try index(for: id); if let p = ix.position(of: c.rowID) { positions[c.rowID] = (ix, p) } } }273        while selected.count < limit, !remaining.isEmpty {274            var best = 0; var bestValue = -Double.infinity275            for (i, c) in remaining.enumerated() {276                var redundancy = 0.0277                if let (ix, p) = positions[c.rowID] {278                    for s in selected { if let (sx, sp) = positions[s.rowID], sx === ix { redundancy = max(redundancy, ix.similarity(p, sp)) } }279                }280                let value = lambda * (top > 0 ? c.score / top : 0) - (1 - lambda) * redundancy281                if value > bestValue { bestValue = value; best = i }282            }283            selected.append(remaining.remove(at: best))284        }285        return selected286    }287288    public func hits(for scored: [ScoredChunk]) throws -> [KnowledgeHit] {289        guard !scored.isEmpty else { return [] }290        return try db.read { db in291            let rows = try Row.fetchAll(db, sql: "SELECT id, documentID, idx, page, text FROM chunk WHERE id IN (\(databaseQuestionMarks(count: scored.count)))",292                                        arguments: StatementArguments(scored.map(\.rowID)))293            var byID: [Int64: Row] = [:]; for r in rows { byID[r["id"]] = r }294            var documents: [String: KnowledgeDocument] = [:]295            return try scored.compactMap { s in296                guard let row = byID[s.rowID] else { return nil }297                let key: String = row["documentID"]298                if documents[key] == nil { documents[key] = try Row.fetchOne(db, sql: "SELECT * FROM document WHERE id = ?", arguments: [key]).map(Self.document) }299                guard let document = documents[key] else { return nil }300                return KnowledgeHit(document: document, chunkIndex: row["idx"], page: row["page"], text: row["text"], score: s.score)301            }302        }303    }304305    /// `"token" OR "token"` over the distinct words of the query (2+ characters), or nil when nothing is searchable.306    static func ftsQuery(_ text: String) -> String? {307        var seen = Set<String>(); var tokens: [String] = []308        for part in text.lowercased().split(whereSeparator: { !($0.isLetter || $0.isNumber) }) where part.count >= 2 && seen.insert(String(part)).inserted {309            tokens.append("\"\(part)\""); if tokens.count == 32 { break }310        }311        return tokens.isEmpty ? nil : tokens.joined(separator: " OR ")312    }313314    // MARK: Vector index cache315316    private func invalidate(_ id: UUID) { cache.withLock { $0.generation[id, default: 0] += 1; $0.indexes[id] = nil } }317318    private func index(for id: UUID) throws -> VectorIndex {319        let (cached, generation) = cache.withLock { ($0.indexes[id], $0.generation[id, default: 0]) }320        if let cached { return cached }321        let built: VectorIndex = try db.read { db in322            let key = id.uuidString323            guard let dimension = try Int.fetchOne(db, sql: "SELECT dimension FROM collection WHERE id = ?", arguments: [key]), dimension > 0 else {324                return VectorIndex(dimension: 0, rowIDs: [], matrix: [])325            }326            let total = try Int.fetchOne(db, sql: "SELECT count(*) FROM chunk WHERE collectionID = ? AND vector IS NOT NULL", arguments: [key]) ?? 0327            var rowIDs: [Int64] = []; rowIDs.reserveCapacity(total)328            var matrix = [Float](repeating: 0, count: total * dimension)329            let bytes = dimension * MemoryLayout<Float>.size330            let cursor = try Row.fetchCursor(db, sql: "SELECT id, vector FROM chunk WHERE collectionID = ? AND vector IS NOT NULL ORDER BY id", arguments: [key])331            try matrix.withUnsafeMutableBytes { destination in332                while let row = try cursor.next(), rowIDs.count < total {333                    let slot = UnsafeMutableRawBufferPointer(rebasing: destination[(rowIDs.count * bytes)..<((rowIDs.count + 1) * bytes)])334                    let copied = try row.withUnsafeData(atIndex: 1) { data -> Bool in335                        guard let data, data.count == bytes else { return false }336                        data.copyBytes(to: slot); return true337                    }338                    if copied { rowIDs.append(row[0]) }339                }340            }341            if rowIDs.count < total { matrix.removeLast((total - rowIDs.count) * dimension) }342            return VectorIndex(dimension: dimension, rowIDs: rowIDs, matrix: matrix)343        }344        cache.withLock { if $0.generation[id, default: 0] == generation { $0.indexes[id] = built } }345        return built346    }347348    // MARK: Rows349350    private static func write(_ c: KnowledgeCollection, _ db: Database) throws {351        try db.execute(sql: """352            INSERT INTO collection (id, name, symbol, embedderID, dimension, chunkSize, chunkOverlap, createdAt, updatedAt, deletedAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)353            ON CONFLICT(id) DO UPDATE SET name = excluded.name, symbol = excluded.symbol, embedderID = excluded.embedderID, dimension = excluded.dimension,354              chunkSize = excluded.chunkSize, chunkOverlap = excluded.chunkOverlap, updatedAt = excluded.updatedAt, deletedAt = excluded.deletedAt355            """, arguments: [c.id.uuidString, c.name, c.symbol, c.embedderID, c.dimension, c.chunkSize, c.chunkOverlap, c.createdAt.timeIntervalSince1970,356                             c.updatedAt.timeIntervalSince1970, c.deletedAt?.timeIntervalSince1970])357    }358    private static func write(_ d: KnowledgeDocument, _ db: Database, language: String? = nil) throws {359        try db.execute(sql: """360            INSERT INTO document (id, collectionID, name, kind, byteCount, characterCount, chunkCount, contentHash, status, errorText, addedAt, language)361            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)362            ON CONFLICT(id) DO UPDATE SET collectionID = excluded.collectionID, name = excluded.name, kind = excluded.kind, byteCount = excluded.byteCount,363              characterCount = excluded.characterCount, chunkCount = excluded.chunkCount, contentHash = excluded.contentHash, status = excluded.status,364              errorText = excluded.errorText, addedAt = excluded.addedAt, language = coalesce(excluded.language, document.language)365            """, arguments: [d.id.uuidString, d.collectionID.uuidString, d.name, d.kind, d.byteCount, d.characterCount, d.chunkCount, d.contentHash, d.status.rawValue,366                             d.errorText, d.addedAt.timeIntervalSince1970, language])367    }368    private static func collection(_ r: Row) -> KnowledgeCollection {369        KnowledgeCollection(id: UUID(uuidString: r["id"]) ?? UUID(), name: r["name"], symbol: r["symbol"], embedderID: r["embedderID"], dimension: r["dimension"],370                            chunkSize: r["chunkSize"], chunkOverlap: r["chunkOverlap"], createdAt: Date(timeIntervalSince1970: r["createdAt"]),371                            updatedAt: Date(timeIntervalSince1970: r["updatedAt"]), deletedAt: (r["deletedAt"] as Double?).map(Date.init(timeIntervalSince1970:)))372    }373    private static func document(_ r: Row) -> KnowledgeDocument {374        KnowledgeDocument(id: UUID(uuidString: r["id"]) ?? UUID(), collectionID: UUID(uuidString: r["collectionID"]) ?? UUID(), name: r["name"], kind: r["kind"],375                          byteCount: r["byteCount"], characterCount: r["characterCount"], chunkCount: r["chunkCount"], contentHash: r["contentHash"],376                          status: KnowledgeDocument.Status(rawValue: r["status"]) ?? .failed, errorText: r["errorText"], addedAt: Date(timeIntervalSince1970: r["addedAt"]))377    }378    private static func chunk(_ r: Row) -> StoredChunk {379        StoredChunk(rowID: r["id"], documentID: UUID(uuidString: r["documentID"]) ?? UUID(), collectionID: UUID(uuidString: r["collectionID"]) ?? UUID(), index: r["idx"],380                    page: r["page"], heading: r["heading"], text: r["text"], hasVector: r["hasVector"])381    }382}383
Files main
Menu

Theme