| 1 | // |
| 2 | // InferenceEnvironment.swift |
| 3 | // Siti AI visionOS |
| 4 | // |
| 5 | |
| 6 | import Foundation |
| 7 | |
| 8 | /// Points `HF_HOME` and `HF_HUB_CACHE` at a writable location inside the app |
| 9 | /// sandbox. Uses the same `group.com.ondeinference.apps` App Group as the |
| 10 | /// Tauri build of Siti (see `src-tauri/src/setup/setup_application_filesystem.rs`) |
| 11 | /// so a model downloaded by one Siti app on this device is reused by the |
| 12 | /// other instead of being fetched twice. Falls back to the app's own |
| 13 | /// Application Support directory if the group is unavailable. |
| 14 | /// |
| 15 | /// Call this once at launch, before creating an `OndeChatEngine`. |
| 16 | func setupInferenceEnvironment() { |
| 17 | let fm = FileManager.default |
| 18 | |
| 19 | let base: URL = fm.containerURL( |
| 20 | forSecurityApplicationGroupIdentifier: "group.com.ondeinference.apps" |
| 21 | ) ?? fm.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0] |
| 22 | |
| 23 | let hfHome = base.appendingPathComponent("models", isDirectory: true) |
| 24 | let hfHubCache = hfHome.appendingPathComponent("hub", isDirectory: true) |
| 25 | try? fm.createDirectory(at: hfHubCache, withIntermediateDirectories: true) |
| 26 | |
| 27 | setenv("HF_HOME", hfHome.path, 1) |
| 28 | setenv("HF_HUB_CACHE", hfHubCache.path, 1) |
| 29 | |
| 30 | // Make sure TMPDIR points inside the sandbox for the Rust side. |
| 31 | let tmp = base.appendingPathComponent("tmp", isDirectory: true) |
| 32 | try? fm.createDirectory(at: tmp, withIntermediateDirectories: true) |
| 33 | setenv("TMPDIR", tmp.path, 1) |
| 34 | } |