SPB Git

Repository Public

KnowledgeScreen.swift 22.5 KB 381 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/Features/Knowledge/KnowledgeScreen.swift3// Description: Knowledge Studio: private on-device collections, document indexing with live progress, search playground and research mode.4import SwiftUI5import UniformTypeIdentifiers6import LocalChatCore7import LocalChatKnowledge89public struct KnowledgeScreen: View {10    @Environment(AppModel.self) private var app11    @State private var model = KnowledgeModel()12    @State private var showCreate = false13    @State private var showResearch = false1415    public init() {}1617    public var body: some View {18        StudioSplit(showsDetail: model.selectedID != nil || showResearch, backTitle: "Collections", onBack: { showResearch = false; model.select(nil, app) }) {19            master20        } detail: {21            if showResearch { ResearchPanel() }22            else if let collection = model.collection(app) { CollectionDetail(model: model, collection: collection).id(collection.id) }23            else {24                EmptyStateView(symbol: "books.vertical", title: "Private knowledge", message: "Import documents into a collection, then attach it to a chat or an agent. Extraction, embeddings and search run on this device.") {25                    Button("New collection") { showCreate = true }.buttonStyle(.lcPrimary)26                }27            }28        }29        .sheet(isPresented: $showCreate) { CollectionEditor(model: model, existing: nil) }30        .onAppear { model.start(app) }31    }3233    private var master: some View {34        ScrollView {35            VStack(alignment: .leading, spacing: DS.S.lg) {36                ScreenTitle("Knowledge", subtitle: "Documents the models can cite") {37                    Button { showCreate = true } label: { Label("New", systemImage: "plus") }.buttonStyle(.lcPrimaryCompact).accessibilityLabel("New collection")38                }39                InlineNotice(.privacy, "Stays on this device. Documents are extracted, embedded and searched locally unless you pick a remote embedder.")40                if let message = model.message { InlineNotice(.info, message) }41                if model.canReclaim {42                    Button { model.reclaim(app) } label: { Label("Reclaim space", systemImage: "arrow.3.trianglepath") }.buttonStyle(.lcSecondaryCompact)43                }44                Button { model.select(nil, app); showResearch = true } label: {45                    HStack(spacing: 10) {46                        Image(systemName: "doc.text.magnifyingglass").foregroundStyle(DS.G.accent).font(.system(size: 16, weight: .semibold))47                        VStack(alignment: .leading, spacing: 1) {48                            Text("Research mode").font(DS.F.headline).foregroundStyle(DS.C.text)49                            Text("Plan, retrieve, answer with citations, verify").font(DS.F.caption).foregroundStyle(DS.C.muted)50                        }51                        Spacer(); Image(systemName: "chevron.right").font(.system(size: 11, weight: .semibold)).foregroundStyle(DS.C.muted)52                    }53                    .card(padding: DS.S.md, elevated: showResearch)54                }55                .buttonStyle(.plain)5657                SectionHeader("Collections", subtitle: "\(model.counts?.documents ?? 0) documents · \((model.counts?.chunks ?? 0).formatted()) chunks · \((model.counts?.vectors ?? 0).formatted()) vectors")58                if app.collections.isEmpty {59                    Text("No collection yet.").font(DS.F.callout).foregroundStyle(DS.C.muted)60                }61                ForEach(app.collections) { c in62                    Button { showResearch = false; model.select(c.id, app) } label: {63                        HStack(spacing: 10) {64                            Image(systemName: c.symbol).font(.system(size: 15)).foregroundStyle(DS.C.accent).frame(width: 30, height: 30).background(DS.G.soft, in: RoundedRectangle(cornerRadius: 8, style: .continuous))65                            VStack(alignment: .leading, spacing: 2) {66                                Text(c.name).font(DS.F.body.weight(.medium)).foregroundStyle(DS.C.text).lineLimit(1)67                                Text(model.embedderName(c.embedderID)).font(DS.F.micro).foregroundStyle(DS.C.muted).lineLimit(1)68                            }69                            Spacer(minLength: 4)70                            if model.isRemote(c) { Pill("Remote", symbol: "network", tone: .warn) }71                        }72                        .card(padding: DS.S.md, elevated: model.selectedID == c.id)73                    }74                    .buttonStyle(.plain)75                    .accessibilityLabel("Collection \(c.name)")76                }77            }78            .padding(DS.S.lg)79        }80        .screenBackground()81    }82}8384// MARK: Collection editor8586struct CollectionEditor: View {87    @Environment(AppModel.self) private var app88    @Environment(\.dismiss) private var dismiss89    let model: KnowledgeModel90    let existing: KnowledgeCollection?91    @State private var name = ""92    @State private var symbol = "books.vertical"93    @State private var embedderID = ""94    @State private var chunkSize = 1200.095    @State private var overlap = 200.096    @State private var busy = false9798    var body: some View {99        VStack(alignment: .leading, spacing: DS.S.lg) {100            Text(existing == nil ? "New collection" : "Edit collection").font(DS.F.title)101            TextField("Name", text: $name).textFieldStyle(.roundedBorder).accessibilityLabel("Collection name")102            SymbolPicker(symbol: $symbol)103            if existing == nil {104                VStack(alignment: .leading, spacing: 6) {105                    Text("Embedder").font(DS.F.callout.weight(.medium))106                    Picker("Embedder", selection: $embedderID) {107                        ForEach(model.embedders) { e in Text(e.name + (e.remote ? " — remote" : "")).tag(e.id) }108                    }109                    .labelsHidden()110                    if model.embedders.isEmpty { InlineNotice(.warning, "No embedder is available on this device. Install an embedding model from the Model Library.") }111                    else if model.embedders.first(where: { $0.id == embedderID })?.remote == true {112                        InlineNotice(.warning, "Remote embedder: document text will be sent to that node for embedding. You will be asked to confirm before indexing.")113                    } else { InlineNotice(.privacy, "On-device embedder: document text never leaves this device.") }114                    Text("Vectors from different embedders cannot be mixed; changing it later re-indexes the collection.").font(DS.F.caption).foregroundStyle(DS.C.muted)115                }116            }117            VStack(alignment: .leading, spacing: 4) {118                SettingRow("Chunk size", detail: "\(Int(chunkSize)) characters") { Slider(value: $chunkSize, in: 400...3000, step: 100).frame(maxWidth: 200) }119                SettingRow("Overlap", detail: "\(Int(overlap)) characters") { Slider(value: $overlap, in: 0...600, step: 50).frame(maxWidth: 200) }120                if existing != nil { Text("New chunking applies to documents indexed from now on; re-index the collection to apply it everywhere.").font(DS.F.caption).foregroundStyle(DS.C.muted) }121            }122            HStack {123                Spacer()124                Button("Cancel") { dismiss() }.buttonStyle(.lcSecondary)125                Button(existing == nil ? "Create" : "Save") { save() }.buttonStyle(.lcPrimary)126                    .disabled(busy || name.trimmingCharacters(in: .whitespaces).isEmpty || (existing == nil && embedderID.isEmpty))127            }128        }129        .padding(DS.S.xl).frame(minWidth: 320, idealWidth: 460)130        .presentationDetents([.large])131        .task {132            await model.loadEmbedders(app)133            if let existing { name = existing.name; symbol = existing.symbol; chunkSize = Double(existing.chunkSize); overlap = Double(existing.chunkOverlap) }134            else if embedderID.isEmpty { embedderID = model.embedders.first?.id ?? "" }135        }136    }137138    private func save() {139        let clean = name.trimmingCharacters(in: .whitespaces)140        if var c = existing {141            c.name = clean; c.symbol = symbol; c.chunkSize = Int(chunkSize); c.chunkOverlap = min(Int(overlap), Int(chunkSize) / 2)142            model.save(c, app); dismiss()143        } else if let embedder = model.embedders.first(where: { $0.id == embedderID }) {144            busy = true145            Task { await model.create(name: clean, symbol: symbol, embedder: embedder, chunkSize: Int(chunkSize), overlap: Int(overlap), app); busy = false; dismiss() }146        }147    }148}149150// MARK: Collection detail151152struct CollectionDetail: View {153    @Environment(AppModel.self) private var app154    let model: KnowledgeModel155    let collection: KnowledgeCollection156    enum Tab: String, Identifiable, CaseIterable { case documents, search; var id: String { rawValue } }157    @State private var tab: Tab = .documents158    @State private var importing = false159    @State private var editing = false160    @State private var confirmDelete = false161    @State private var changingEmbedder = false162    @State private var dropTargeted = false163164    static let importTypes: [UTType] = {165        var types: [UTType] = [.pdf, .plainText, .text, .commaSeparatedText, .tabSeparatedText, .sourceCode, .json, .xml, .html, .data]166        for ext in ["md", "markdown", "docx", "csv", "tsv", "swift", "py", "rs", "go", "ts", "kt"] { if let t = UTType(filenameExtension: ext) { types.append(t) } }167        return types168    }()169170    private var isRemote: Bool { model.isRemote(collection) }171    private var consent: Binding<Bool> {172        Binding(get: { model.remoteConsent.contains(collection.id) }, set: { if $0 { model.remoteConsent.insert(collection.id) } else { model.remoteConsent.remove(collection.id) } })173    }174175    var body: some View {176        ScrollView {177            VStack(alignment: .leading, spacing: DS.S.lg) {178                header179                if isRemote {180                    InlineNotice(.warning, "Remote processing: this collection is embedded by “\(model.embedderName(collection.embedderID))”. Document text and search queries are sent to that node.")181                    Toggle("I allow sending this collection's text to the remote embedder", isOn: consent).font(DS.F.callout).toggleStyle(.switch)182                } else {183                    InlineNotice(.privacy, "Stays on this device — embedded by \(model.embedderName(collection.embedderID)).")184                }185                if let e = model.errorText { InlineNotice(.error, e) }186                TileGrid(minimum: 130) {187                    MetricTile("Documents", value: "\(model.collectionCounts?.documents ?? 0)", symbol: "doc")188                    MetricTile("Chunks", value: (model.collectionCounts?.chunks ?? 0).formatted(), symbol: "square.stack.3d.up")189                    MetricTile("Vectors", value: (model.collectionCounts?.vectors ?? 0).formatted(), unit: "× \(collection.dimension)d", symbol: "point.3.connected.trianglepath.dotted")190                    MetricTile("Chunking", value: "\(collection.chunkSize)", unit: "chars, \(collection.chunkOverlap) overlap", symbol: "scissors")191                }192                StudioTabs(tabs: Tab.allCases, title: { $0 == .documents ? "Documents" : "Search playground" }, symbol: { $0 == .documents ? "doc.on.doc" : "magnifyingglass" }, selection: $tab)193                switch tab {194                case .documents: documents195                case .search: SearchPlayground(model: model)196                }197            }198            .padding(DS.S.lg).frame(maxWidth: 900, alignment: .leading).frame(maxWidth: .infinity)199        }200        .screenBackground()201        .overlay { if dropTargeted { RoundedRectangle(cornerRadius: DS.R.panel).strokeBorder(DS.G.aurora, style: StrokeStyle(lineWidth: 2, dash: [8, 6])).padding(8).allowsHitTesting(false) } }202        .dropDestination(for: URL.self) { urls, _ in model.add(urls.filter(\.isFileURL), app); return true } isTargeted: { dropTargeted = $0 }203        .fileImporter(isPresented: $importing, allowedContentTypes: Self.importTypes, allowsMultipleSelection: true) { result in204            if case .success(let urls) = result { model.add(urls, app) } else if case .failure(let error) = result { model.errorText = error.localizedDescription }205        }206        .sheet(isPresented: $editing) { CollectionEditor(model: model, existing: collection) }207        .sheet(isPresented: $changingEmbedder) { EmbedderChange(model: model, collection: collection) }208        .confirmationDialog("Delete “\(collection.name)”?", isPresented: $confirmDelete, titleVisibility: .visible) {209            Button("Delete collection and all its data", role: .destructive) { model.delete(collection, app) }210        } message: { Text("Documents, chunks and vectors are removed from this device. The original files are not touched.") }211    }212213    private var header: some View {214        HStack(alignment: .top, spacing: 12) {215            Image(systemName: collection.symbol).font(.system(size: 22)).foregroundStyle(DS.G.accent).frame(width: 48, height: 48).background(DS.G.soft, in: RoundedRectangle(cornerRadius: 12, style: .continuous))216            VStack(alignment: .leading, spacing: 3) {217                Text(collection.name).font(DS.F.title).foregroundStyle(DS.C.text)218                Text("Created \(collection.createdAt.formatted(date: .abbreviated, time: .omitted))").font(DS.F.caption).foregroundStyle(DS.C.muted)219            }220            Spacer(minLength: 8)221            Menu {222                Button { editing = true } label: { Label("Rename & chunking…", systemImage: "pencil") }223                Button { model.reindexCollection(app) } label: { Label("Re-index collection", systemImage: "arrow.triangle.2.circlepath") }224                Button { changingEmbedder = true } label: { Label("Change embedder…", systemImage: "point.3.connected.trianglepath.dotted") }225                Divider()226                Button(role: .destructive) { confirmDelete = true } label: { Label("Delete collection…", systemImage: "trash") }227            } label: { Image(systemName: "ellipsis.circle").font(.system(size: 17)).frame(width: 32, height: 32) }228            .menuStyle(.borderlessButton).fixedSize().accessibilityLabel("Collection actions")229            Button { importing = true } label: { Label("Add", systemImage: "plus") }.buttonStyle(.lcPrimaryCompact)230                .disabled(isRemote && !consent.wrappedValue).accessibilityLabel("Add documents")231        }232    }233234    @ViewBuilder private var documents: some View {235        if !model.liveProgress.isEmpty || model.activeJobs > 0 {236            VStack(alignment: .leading, spacing: 10) {237                HStack {238                    ThinkingDots(); Text("Indexing").font(DS.F.headline); Spacer()239                    Button("Cancel") { model.cancelJobs() }.buttonStyle(.lcDestructive)240                }241                ForEach(model.liveProgress, id: \.documentID) { p in242                    VStack(alignment: .leading, spacing: 4) {243                        HStack {244                            Text(p.documentName).font(DS.F.callout).lineLimit(1)245                            Spacer()246                            Text(p.chunksTotal > 0 ? "\(p.stage.rawValue.capitalized) · \(p.chunksDone)/\(p.chunksTotal) chunks" : p.stage.rawValue.capitalized).font(DS.F.micro).foregroundStyle(DS.C.muted)247                        }248                        if let f = p.fraction { ProgressBar(f) } else { ProgressView().progressViewStyle(.linear) }249                    }250                }251            }252            .card(padding: DS.S.md)253        }254        if model.documents.isEmpty && model.activeJobs == 0 {255            VStack(spacing: 10) {256                Image(systemName: "arrow.down.doc").font(.system(size: 28)).foregroundStyle(DS.G.accent)257                Text("Add PDF, Markdown, text, DOCX, CSV or source files").font(DS.F.callout).foregroundStyle(DS.C.text2).multilineTextAlignment(.center)258                if Platform.isMac { Text("or drop them here").font(DS.F.caption).foregroundStyle(DS.C.muted) }259            }260            .frame(maxWidth: .infinity).padding(DS.S.xl).card()261        }262        ForEach(model.documents) { doc in DocumentRow(model: model, document: doc) }263    }264}265266private struct DocumentRow: View {267    @Environment(AppModel.self) private var app268    let model: KnowledgeModel269    let document: KnowledgeDocument270271    var body: some View {272        HStack(alignment: .top, spacing: 10) {273            Image(systemName: symbol).font(.system(size: 15)).foregroundStyle(DS.C.accent).frame(width: 30, height: 30).background(DS.C.surface2, in: RoundedRectangle(cornerRadius: 8, style: .continuous))274            VStack(alignment: .leading, spacing: 4) {275                Text(document.name).font(DS.F.body.weight(.medium)).foregroundStyle(DS.C.text).lineLimit(2)276                HStack(spacing: 6) {277                    status278                    Text("\(document.chunkCount) chunks · \(ByteFormat.string(Int64(document.byteCount))) · \(document.characterCount.formatted()) chars").font(DS.F.micro).foregroundStyle(DS.C.muted).lineLimit(1)279                }280                if let e = document.errorText { Text(e).font(DS.F.caption).foregroundStyle(DS.C.danger).fixedSize(horizontal: false, vertical: true) }281            }282            Spacer(minLength: 4)283            Menu {284                Button { model.reindex(document: document, app) } label: { Label("Re-index", systemImage: "arrow.triangle.2.circlepath") }285                Button(role: .destructive) { model.deleteDocument(document, app) } label: { Label("Delete from this device", systemImage: "trash") }286            } label: { Image(systemName: "ellipsis").frame(width: 28, height: 28) }287            .menuStyle(.borderlessButton).fixedSize().accessibilityLabel("Actions for \(document.name)")288        }289        .card(padding: DS.S.md)290    }291292    private var symbol: String {293        switch document.kind.lowercased() { case "pdf": "doc.richtext"; case "csv": "tablecells"; case "docx": "doc.text"; case "md", "markdown": "text.alignleft"; case "txt": "doc.plaintext"; default: "chevron.left.forwardslash.chevron.right" }294    }295    @ViewBuilder private var status: some View {296        switch document.status {297        case .ready: Pill("Ready", symbol: "checkmark", tone: .ok)298        case .indexing: Pill("Indexing", symbol: "hourglass", tone: .accent)299        case .pending: Pill("Pending", tone: .neutral)300        case .failed: Pill("Failed", symbol: "xmark", tone: .danger)301        }302    }303}304305private struct EmbedderChange: View {306    @Environment(AppModel.self) private var app307    @Environment(\.dismiss) private var dismiss308    let model: KnowledgeModel309    let collection: KnowledgeCollection310    @State private var embedderID = ""311    @State private var allowRemote = false312313    var body: some View {314        let choice = model.embedders.first { $0.id == embedderID }315        VStack(alignment: .leading, spacing: DS.S.lg) {316            Text("Change embedder").font(DS.F.title)317            Text("Every vector of “\(collection.name)” will be recomputed with the new embedder. The documents themselves are kept.").font(DS.F.callout).foregroundStyle(DS.C.text2)318            Picker("Embedder", selection: $embedderID) { ForEach(model.embedders) { Text($0.name + ($0.remote ? " — remote" : "")).tag($0.id) } }.labelsHidden()319            if choice?.remote == true {320                InlineNotice(.warning, "Remote embedder: all document text of this collection will be sent to that node.")321                Toggle("I allow remote processing for this collection", isOn: $allowRemote)322            }323            HStack {324                Spacer()325                Button("Cancel") { dismiss() }.buttonStyle(.lcSecondary)326                Button("Re-index") {327                    if let choice {328                        if choice.remote { model.remoteConsent.insert(collection.id) } else { model.remoteConsent.remove(collection.id) }329                        model.reindexCollection(newEmbedder: choice, app)330                    }331                    dismiss()332                }333                .buttonStyle(.lcPrimary).disabled(choice == nil || embedderID == collection.embedderID || (choice?.remote == true && !allowRemote))334            }335        }336        .padding(DS.S.xl).frame(minWidth: 320, idealWidth: 440)337        .presentationDetents([.medium, .large])338        .task { await model.loadEmbedders(app); embedderID = collection.embedderID }339    }340}341342// MARK: Search playground343344private struct SearchPlayground: View {345    @Environment(AppModel.self) private var app346    @Bindable var model: KnowledgeModel347348    var body: some View {349        VStack(alignment: .leading, spacing: DS.S.md) {350            HStack(spacing: 8) {351                TextField("Ask something your documents answer…", text: $model.query).textFieldStyle(.roundedBorder).onSubmit { Task { await model.search(app) } }352                    .accessibilityLabel("Search query")353                Button { Task { await model.search(app) } } label: { if model.isSearching { ProgressView().controlSize(.small) } else { Text("Search") } }354                    .buttonStyle(.lcPrimaryCompact).disabled(model.query.trimmingCharacters(in: .whitespaces).isEmpty || model.isSearching)355            }356            if model.searchMode != .none {357                HStack(spacing: 6) {358                    Pill(model.searchMode.label, symbol: model.searchMode == .keywordOnly ? "exclamationmark.triangle.fill" : "checkmark.seal", tone: model.searchMode == .keywordOnly ? .warn : .ok)359                    if let d = model.searchDuration { Pill(MetricFormat.seconds(d), symbol: "timer") }360                    Pill("\(model.hits.count) hits")361                }362                ForEach(model.searchNotes, id: \.self) { InlineNotice(.info, $0) }363                if model.hits.isEmpty { Text("Nothing relevant was found in this collection.").font(DS.F.callout).foregroundStyle(DS.C.muted) }364            }365            ForEach(Array(model.hits.enumerated()), id: \.element.id) { i, hit in366                VStack(alignment: .leading, spacing: 6) {367                    HStack(spacing: 6) {368                        Text("[\(i + 1)]").font(DS.F.mono(12, weight: .semibold)).foregroundStyle(DS.C.accent)369                        Text(hit.document.name).font(DS.F.callout.weight(.medium)).lineLimit(1)370                        if let page = hit.page { Pill("p. \(page)") }371                        Spacer(minLength: 4)372                        Text(String(format: "score %.3f", hit.score)).font(DS.F.mono(10.5)).foregroundStyle(DS.C.muted)373                    }374                    Text(hit.text).font(DS.F.caption).foregroundStyle(DS.C.text2).lineLimit(8).textSelection(.enabled).fixedSize(horizontal: false, vertical: true)375                }376                .card(padding: DS.S.md)377            }378        }379    }380}381
Files main
Menu

Theme