| 1 | import Foundation |
| 2 | |
| 3 | /// Points `HF_HOME` and `HF_HUB_CACHE` at a writable location inside the app |
| 4 | /// sandbox. SDK examples prefer the shared Onde App Group so downloaded models |
| 5 | /// can be reused across Swift, Flutter, and React Native example apps on the |
| 6 | /// same device. If the group is unavailable, fall back to the app's own |
| 7 | /// Application Support directory. |
| 8 | /// |
| 9 | /// Call this once at launch, before creating an `OndeChatEngine`. |
| 10 | func setupInferenceEnvironment() { |
| 11 | let fm = FileManager.default |
| 12 | |
| 13 | // The Apple SDK examples share one App Group so downloaded models can be |
| 14 | // reused across Swift, Flutter, and React Native sample apps. Try the |
| 15 | // shared group first, then fall back to the app's private sandbox. |
| 16 | let base: URL = fm.containerURL( |
| 17 | forSecurityApplicationGroupIdentifier: "group.com.ondeinference.apps" |
| 18 | ) ?? fm.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0] |
| 19 | |
| 20 | let hfHome = base.appendingPathComponent("models", isDirectory: true) |
| 21 | let hfHubCache = hfHome.appendingPathComponent("hub", isDirectory: true) |
| 22 | try? fm.createDirectory(at: hfHubCache, withIntermediateDirectories: true) |
| 23 | |
| 24 | setenv("HF_HOME", hfHome.path, 1) |
| 25 | setenv("HF_HUB_CACHE", hfHubCache.path, 1) |
| 26 | |
| 27 | // Make sure TMPDIR points inside the sandbox for the Rust side. |
| 28 | let tmp = base.appendingPathComponent("tmp", isDirectory: true) |
| 29 | try? fm.createDirectory(at: tmp, withIntermediateDirectories: true) |
| 30 | setenv("TMPDIR", tmp.path, 1) |
| 31 | } |