|
| 1 | +import Foundation |
| 2 | +import OSLog |
| 3 | + |
| 4 | +/// Model registry configuration for downloading models and datasets |
| 5 | +/// Handles both registry URL and proxy configuration |
| 6 | +public enum ModelRegistry { |
| 7 | + private static let logger = AppLogger(category: "ModelRegistry") |
| 8 | + |
| 9 | + // MARK: - Registry URL Configuration |
| 10 | + |
| 11 | + private static var _customBaseURL: String? |
| 12 | + |
| 13 | + /// Base registry URL (default: HuggingFace) |
| 14 | + /// Can be overridden programmatically to use a different model registry or mirror. |
| 15 | + /// Priority: programmatic override → REGISTRY_URL env var → MODEL_REGISTRY_URL env var → default |
| 16 | + public static var baseURL: String { |
| 17 | + get { |
| 18 | + _customBaseURL |
| 19 | + ?? ProcessInfo.processInfo.environment["REGISTRY_URL"] |
| 20 | + ?? ProcessInfo.processInfo.environment["MODEL_REGISTRY_URL"] |
| 21 | + ?? "https://huggingface.co" |
| 22 | + } |
| 23 | + set { |
| 24 | + _customBaseURL = newValue |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // MARK: - URL Construction |
| 29 | + |
| 30 | + /// Construct API URL for listing model repository contents |
| 31 | + public static func apiModels(_ repoPath: String, _ apiPath: String) -> URL { |
| 32 | + URL(string: "\(baseURL)/api/models/\(repoPath)/\(apiPath)")! |
| 33 | + } |
| 34 | + |
| 35 | + /// Construct download URL for a model file |
| 36 | + public static func resolveModel(_ repoPath: String, _ filePath: String) -> URL { |
| 37 | + URL(string: "\(baseURL)/\(repoPath)/resolve/main/\(filePath)")! |
| 38 | + } |
| 39 | + |
| 40 | + /// Construct API URL for listing dataset contents |
| 41 | + public static func apiDatasets(_ dataset: String, _ apiPath: String) -> URL { |
| 42 | + URL(string: "\(baseURL)/api/datasets/\(dataset)/\(apiPath)")! |
| 43 | + } |
| 44 | + |
| 45 | + /// Construct download URL for a dataset file |
| 46 | + public static func resolveDataset(_ dataset: String, _ filePath: String) -> URL { |
| 47 | + URL(string: "\(baseURL)/datasets/\(dataset)/resolve/main/\(filePath)")! |
| 48 | + } |
| 49 | + |
| 50 | + // MARK: - Session Configuration |
| 51 | + |
| 52 | + /// Create a URLSession configured with registry URL and proxy settings |
| 53 | + static func configuredSession() -> URLSession { |
| 54 | + let configuration = URLSessionConfiguration.default |
| 55 | + |
| 56 | + // Configure proxy settings if environment variables are set |
| 57 | + if let proxyConfig = configureProxySettings() { |
| 58 | + configuration.connectionProxyDictionary = proxyConfig |
| 59 | + } |
| 60 | + |
| 61 | + return URLSession(configuration: configuration) |
| 62 | + } |
| 63 | + |
| 64 | + // MARK: - Proxy Configuration (macOS only) |
| 65 | + |
| 66 | + private static func configureProxySettings() -> [String: Any]? { |
| 67 | + #if os(macOS) |
| 68 | + var proxyConfig: [String: Any] = [:] |
| 69 | + var hasProxyConfig = false |
| 70 | + |
| 71 | + // Configure HTTPS proxy |
| 72 | + if let httpsProxy = ProcessInfo.processInfo.environment["https_proxy"], |
| 73 | + let proxySettings = parseProxyURL(httpsProxy, type: "HTTPS") |
| 74 | + { |
| 75 | + proxyConfig.merge(proxySettings) { _, new in new } |
| 76 | + hasProxyConfig = true |
| 77 | + } |
| 78 | + |
| 79 | + // Configure HTTP proxy |
| 80 | + if let httpProxy = ProcessInfo.processInfo.environment["http_proxy"], |
| 81 | + let proxySettings = parseProxyURL(httpProxy, type: "HTTP") |
| 82 | + { |
| 83 | + proxyConfig.merge(proxySettings) { _, new in new } |
| 84 | + hasProxyConfig = true |
| 85 | + } |
| 86 | + |
| 87 | + return hasProxyConfig ? proxyConfig : nil |
| 88 | + #else |
| 89 | + // Proxy configuration not available on iOS |
| 90 | + return nil |
| 91 | + #endif |
| 92 | + } |
| 93 | + |
| 94 | + private static func parseProxyURL(_ proxyURLString: String, type: String) -> [String: Any]? { |
| 95 | + #if os(macOS) |
| 96 | + guard let proxyURL = URL(string: proxyURLString), |
| 97 | + let host = proxyURL.host, |
| 98 | + let port = proxyURL.port |
| 99 | + else { |
| 100 | + logger.warning("Invalid \(type) proxy URL: \(proxyURLString)") |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + let config: [String: Any] |
| 105 | + switch type { |
| 106 | + case "HTTPS": |
| 107 | + config = [ |
| 108 | + kCFNetworkProxiesHTTPSEnable as String: true, |
| 109 | + kCFNetworkProxiesHTTPSProxy as String: host, |
| 110 | + kCFNetworkProxiesHTTPSPort as String: port, |
| 111 | + ] |
| 112 | + case "HTTP": |
| 113 | + config = [ |
| 114 | + kCFNetworkProxiesHTTPEnable as String: true, |
| 115 | + kCFNetworkProxiesHTTPProxy as String: host, |
| 116 | + kCFNetworkProxiesHTTPPort as String: port, |
| 117 | + ] |
| 118 | + default: |
| 119 | + return nil |
| 120 | + } |
| 121 | + |
| 122 | + logger.info("Configured \(type) proxy: \(host):\(port)") |
| 123 | + return config |
| 124 | + #else |
| 125 | + // Proxy configuration not available on iOS |
| 126 | + return nil |
| 127 | + #endif |
| 128 | + } |
| 129 | +} |
0 commit comments