func fetchAllLocalModels()

in HuggingChat-Mac/LocalLLM/ModelManager.swift [290:320]


    func fetchAllLocalModels() {
        if let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            do {
                let items = try FileManager.default.contentsOfDirectory(at: documentsPath.appendingPathComponent("huggingface").appendingPathComponent("models").appendingPathComponent("mlx-community"), includingPropertiesForKeys: [.isDirectoryKey])
                let downloadedModelNames = Set(items.map { $0.lastPathComponent })
                for (index, model) in availableModels.enumerated() {
                    if let hfURL = model.hfURL {
                        let modelName = hfURL.split(separator: "/").last.map(String.init) ?? ""
                        
                        if downloadedModelNames.contains(modelName) {
                            if let modelPath = items.first(where: { $0.lastPathComponent == modelName }) {
                                let fileSize = getDirectorySize(url: modelPath.standardizedFileURL)
                                
                                // Update the model with local info
                                availableModels[index].downloadState = .downloaded
                                availableModels[index].localURL = modelPath
                                availableModels[index].size = fileSize
                            }
                        } else {
                            // Reset properties if model isn't found locally
                            availableModels[index].downloadState = .notDownloaded
                            availableModels[index].localURL = nil
                            
                        }
                    }
                 }
            } catch {
                print("Error fetching local models: \(error.localizedDescription)")
            }
        }
    }