@ng-native/desktop 0.0.0-stage → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (393) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -2
  3. package/app.plugin.cjs +17 -0
  4. package/bin/app-config.mjs +168 -0
  5. package/bin/autolink.mjs +224 -0
  6. package/bin/cli.mjs +158 -0
  7. package/bin/host.mjs +427 -0
  8. package/bin/ng-native-desktop.mjs +4 -0
  9. package/bin/package.mjs +971 -0
  10. package/bin/plist.mjs +68 -0
  11. package/bin/prebuilt.mjs +328 -0
  12. package/bin/toolchain.mjs +192 -0
  13. package/native/CMakeLists.txt +276 -0
  14. package/native/appkit/CMakeLists.txt +109 -0
  15. package/native/appkit/address-search.h +26 -0
  16. package/native/appkit/address-search.mm +277 -0
  17. package/native/appkit/animation-frames.h +14 -0
  18. package/native/appkit/animation-frames.mm +106 -0
  19. package/native/appkit/app-info.h +12 -0
  20. package/native/appkit/app-info.mm +98 -0
  21. package/native/appkit/app-updater.mm +128 -0
  22. package/native/appkit/app-windows.h +81 -0
  23. package/native/appkit/app-windows.mm +318 -0
  24. package/native/appkit/appkit-backend.h +73 -0
  25. package/native/appkit/appkit-backend.mm +276 -0
  26. package/native/appkit/apple-authentication.h +19 -0
  27. package/native/appkit/apple-authentication.mm +248 -0
  28. package/native/appkit/apple-button-view.h +10 -0
  29. package/native/appkit/apple-button-view.mm +37 -0
  30. package/native/appkit/apple-id-button.h +16 -0
  31. package/native/appkit/apple-id-button.mm +50 -0
  32. package/native/appkit/audio.h +16 -0
  33. package/native/appkit/audio.mm +275 -0
  34. package/native/appkit/battery.h +13 -0
  35. package/native/appkit/battery.mm +92 -0
  36. package/native/appkit/bookmarks.h +17 -0
  37. package/native/appkit/bookmarks.mm +145 -0
  38. package/native/appkit/calendar.h +25 -0
  39. package/native/appkit/calendar.mm +660 -0
  40. package/native/appkit/camera-view.h +15 -0
  41. package/native/appkit/camera-view.mm +130 -0
  42. package/native/appkit/camera.h +39 -0
  43. package/native/appkit/camera.mm +362 -0
  44. package/native/appkit/cg-canvas.h +17 -0
  45. package/native/appkit/cg-canvas.mm +518 -0
  46. package/native/appkit/component-view.h +17 -0
  47. package/native/appkit/crypto.h +19 -0
  48. package/native/appkit/crypto.mm +78 -0
  49. package/native/appkit/desktop-expo.h +29 -0
  50. package/native/appkit/desktop-expo.mm +385 -0
  51. package/native/appkit/expo-image-drawing.h +46 -0
  52. package/native/appkit/expo-image-drawing.mm +131 -0
  53. package/native/appkit/expo-image-view.h +15 -0
  54. package/native/appkit/expo-image-view.mm +182 -0
  55. package/native/appkit/expo-ui-renderer.h +68 -0
  56. package/native/appkit/expo-ui-renderer.mm +101 -0
  57. package/native/appkit/expo-ui-view.h +26 -0
  58. package/native/appkit/expo-ui-view.mm +134 -0
  59. package/native/appkit/expo-ui.swift +1284 -0
  60. package/native/appkit/file-system.h +16 -0
  61. package/native/appkit/file-system.mm +140 -0
  62. package/native/appkit/fonts.h +12 -0
  63. package/native/appkit/fonts.mm +48 -0
  64. package/native/appkit/geocoder.h +19 -0
  65. package/native/appkit/geocoder.mm +101 -0
  66. package/native/appkit/glass-effect.h +22 -0
  67. package/native/appkit/glass-effect.mm +63 -0
  68. package/native/appkit/glass-view.h +8 -0
  69. package/native/appkit/glass-view.mm +65 -0
  70. package/native/appkit/header-toolbar.h +60 -0
  71. package/native/appkit/header-toolbar.mm +629 -0
  72. package/native/appkit/host-view.h +54 -0
  73. package/native/appkit/host-view.mm +1200 -0
  74. package/native/appkit/image-manipulator.h +14 -0
  75. package/native/appkit/image-manipulator.mm +191 -0
  76. package/native/appkit/image-request.h +22 -0
  77. package/native/appkit/image-request.mm +62 -0
  78. package/native/appkit/image-view.h +8 -0
  79. package/native/appkit/image-view.mm +142 -0
  80. package/native/appkit/input-replay.h +25 -0
  81. package/native/appkit/input-replay.mm +418 -0
  82. package/native/appkit/language-model.h +14 -0
  83. package/native/appkit/language-model.mm +99 -0
  84. package/native/appkit/language-model.swift +472 -0
  85. package/native/appkit/local-authentication.h +15 -0
  86. package/native/appkit/local-authentication.mm +133 -0
  87. package/native/appkit/location.h +16 -0
  88. package/native/appkit/location.mm +214 -0
  89. package/native/appkit/main.mm +1515 -0
  90. package/native/appkit/map-view.h +15 -0
  91. package/native/appkit/map-view.mm +109 -0
  92. package/native/appkit/maps.h +35 -0
  93. package/native/appkit/maps.mm +343 -0
  94. package/native/appkit/menu-bar.h +22 -0
  95. package/native/appkit/menu-bar.mm +208 -0
  96. package/native/appkit/modal-focus.h +16 -0
  97. package/native/appkit/modal-focus.mm +26 -0
  98. package/native/appkit/modal-host-view.h +79 -0
  99. package/native/appkit/modal-host-view.mm +442 -0
  100. package/native/appkit/native-controls.h +22 -0
  101. package/native/appkit/native-controls.mm +431 -0
  102. package/native/appkit/network-state.h +12 -0
  103. package/native/appkit/network-state.mm +64 -0
  104. package/native/appkit/network.h +14 -0
  105. package/native/appkit/network.mm +396 -0
  106. package/native/appkit/notifications.h +19 -0
  107. package/native/appkit/notifications.mm +309 -0
  108. package/native/appkit/photo-library.h +33 -0
  109. package/native/appkit/photo-library.mm +548 -0
  110. package/native/appkit/pointer-buttons.h +32 -0
  111. package/native/appkit/presentation-label.h +15 -0
  112. package/native/appkit/presentation-label.mm +12 -0
  113. package/native/appkit/presented-screen.h +19 -0
  114. package/native/appkit/presented-screen.mm +181 -0
  115. package/native/appkit/printing.h +42 -0
  116. package/native/appkit/printing.mm +393 -0
  117. package/native/appkit/push.h +19 -0
  118. package/native/appkit/push.mm +81 -0
  119. package/native/appkit/red-box.h +26 -0
  120. package/native/appkit/red-box.mm +387 -0
  121. package/native/appkit/screen-capture.h +21 -0
  122. package/native/appkit/screen-capture.mm +92 -0
  123. package/native/appkit/scroll-view.h +13 -0
  124. package/native/appkit/scroll-view.mm +251 -0
  125. package/native/appkit/scrollers.h +25 -0
  126. package/native/appkit/scrollers.mm +43 -0
  127. package/native/appkit/search-field.h +34 -0
  128. package/native/appkit/search-field.mm +291 -0
  129. package/native/appkit/search-toolbar-item.h +19 -0
  130. package/native/appkit/search-toolbar-item.mm +269 -0
  131. package/native/appkit/share-picker.h +34 -0
  132. package/native/appkit/share-picker.mm +121 -0
  133. package/native/appkit/split-view.h +77 -0
  134. package/native/appkit/split-view.mm +1206 -0
  135. package/native/appkit/store-review.h +13 -0
  136. package/native/appkit/store-review.mm +28 -0
  137. package/native/appkit/surface-view.h +45 -0
  138. package/native/appkit/surface-view.mm +558 -0
  139. package/native/appkit/svg-view.h +17 -0
  140. package/native/appkit/svg-view.mm +57 -0
  141. package/native/appkit/symbol-drawing.h +24 -0
  142. package/native/appkit/symbol-drawing.mm +117 -0
  143. package/native/appkit/symbol-view.h +8 -0
  144. package/native/appkit/symbol-view.mm +33 -0
  145. package/native/appkit/system-access.h +34 -0
  146. package/native/appkit/system-access.mm +210 -0
  147. package/native/appkit/system.h +24 -0
  148. package/native/appkit/system.mm +403 -0
  149. package/native/appkit/test-launch.h +36 -0
  150. package/native/appkit/test-launch.mm +115 -0
  151. package/native/appkit/text-input-field.h +76 -0
  152. package/native/appkit/text-input-field.mm +444 -0
  153. package/native/appkit/text-layout.h +58 -0
  154. package/native/appkit/text-layout.mm +477 -0
  155. package/native/appkit/text-recognition.h +13 -0
  156. package/native/appkit/text-recognition.mm +102 -0
  157. package/native/appkit/tracking.h +13 -0
  158. package/native/appkit/tracking.mm +51 -0
  159. package/native/appkit/tray-and-dock.h +45 -0
  160. package/native/appkit/tray-and-dock.mm +130 -0
  161. package/native/appkit/undo.h +23 -0
  162. package/native/appkit/undo.mm +125 -0
  163. package/native/appkit/updates.h +17 -0
  164. package/native/appkit/updates.mm +71 -0
  165. package/native/appkit/video-view.h +10 -0
  166. package/native/appkit/video-view.mm +42 -0
  167. package/native/appkit/video.h +38 -0
  168. package/native/appkit/video.mm +404 -0
  169. package/native/appkit/web-browser.h +12 -0
  170. package/native/appkit/web-browser.mm +97 -0
  171. package/native/appkit/web-view-component.h +9 -0
  172. package/native/appkit/web-view-component.mm +56 -0
  173. package/native/appkit/web-view.h +31 -0
  174. package/native/appkit/web-view.mm +394 -0
  175. package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
  176. package/native/cmake/libraries/react-native-worklets.cmake +46 -0
  177. package/native/cmake/libraries.cmake +109 -0
  178. package/native/cmake/linked-modules.cmake +112 -0
  179. package/native/cmake/react-native.cmake +175 -0
  180. package/native/cmake/runtime-dependencies.cmake +34 -0
  181. package/native/cmake/swift.cmake +43 -0
  182. package/native/cmake/third-party.cmake +74 -0
  183. package/native/core/include/ngn/absent-views.h +27 -0
  184. package/native/core/include/ngn/accessibility.h +140 -0
  185. package/native/core/include/ngn/address-search.h +69 -0
  186. package/native/core/include/ngn/animation-frames.h +39 -0
  187. package/native/core/include/ngn/app-info.h +52 -0
  188. package/native/core/include/ngn/app-menus.h +146 -0
  189. package/native/core/include/ngn/app-windows.h +181 -0
  190. package/native/core/include/ngn/apple-authentication.h +116 -0
  191. package/native/core/include/ngn/asset.h +28 -0
  192. package/native/core/include/ngn/audio.h +125 -0
  193. package/native/core/include/ngn/back-swipe.h +112 -0
  194. package/native/core/include/ngn/battery.h +48 -0
  195. package/native/core/include/ngn/bytes.h +59 -0
  196. package/native/core/include/ngn/calendar.h +180 -0
  197. package/native/core/include/ngn/camera.h +128 -0
  198. package/native/core/include/ngn/control-sizes.h +111 -0
  199. package/native/core/include/ngn/core-modules.h +105 -0
  200. package/native/core/include/ngn/crypto.h +38 -0
  201. package/native/core/include/ngn/default-menu.h +91 -0
  202. package/native/core/include/ngn/desktop-expo.h +193 -0
  203. package/native/core/include/ngn/desktop-modal.h +109 -0
  204. package/native/core/include/ngn/desktop-props.h +75 -0
  205. package/native/core/include/ngn/desktop-view-props.h +110 -0
  206. package/native/core/include/ngn/dev-server.h +33 -0
  207. package/native/core/include/ngn/download.h +24 -0
  208. package/native/core/include/ngn/expo-image.h +192 -0
  209. package/native/core/include/ngn/expo-ui.h +133 -0
  210. package/native/core/include/ngn/file-system.h +274 -0
  211. package/native/core/include/ngn/focus-order.h +63 -0
  212. package/native/core/include/ngn/fonts.h +64 -0
  213. package/native/core/include/ngn/gestures.h +305 -0
  214. package/native/core/include/ngn/glass.h +67 -0
  215. package/native/core/include/ngn/header-bar.h +112 -0
  216. package/native/core/include/ngn/host-state.h +243 -0
  217. package/native/core/include/ngn/image-manipulator.h +70 -0
  218. package/native/core/include/ngn/input-dispatcher.h +211 -0
  219. package/native/core/include/ngn/input-script.h +56 -0
  220. package/native/core/include/ngn/input.h +112 -0
  221. package/native/core/include/ngn/key-value-store.h +41 -0
  222. package/native/core/include/ngn/language-model.h +69 -0
  223. package/native/core/include/ngn/launch-frame.h +47 -0
  224. package/native/core/include/ngn/launch-settings.h +98 -0
  225. package/native/core/include/ngn/layout-animations.h +24 -0
  226. package/native/core/include/ngn/library-components.h +13 -0
  227. package/native/core/include/ngn/library-glue.h +53 -0
  228. package/native/core/include/ngn/live-regions.h +54 -0
  229. package/native/core/include/ngn/local-authentication.h +74 -0
  230. package/native/core/include/ngn/location.h +89 -0
  231. package/native/core/include/ngn/maps.h +226 -0
  232. package/native/core/include/ngn/module-registry.h +51 -0
  233. package/native/core/include/ngn/mounting-backend.h +96 -0
  234. package/native/core/include/ngn/mounting-manager.h +155 -0
  235. package/native/core/include/ngn/native-controls.h +161 -0
  236. package/native/core/include/ngn/native-module.h +109 -0
  237. package/native/core/include/ngn/network-state.h +44 -0
  238. package/native/core/include/ngn/network.h +100 -0
  239. package/native/core/include/ngn/notifications.h +185 -0
  240. package/native/core/include/ngn/photo-library.h +130 -0
  241. package/native/core/include/ngn/printing.h +69 -0
  242. package/native/core/include/ngn/processes.h +114 -0
  243. package/native/core/include/ngn/red-box.h +119 -0
  244. package/native/core/include/ngn/reload-thread.h +258 -0
  245. package/native/core/include/ngn/runtime.h +161 -0
  246. package/native/core/include/ngn/safe-areas.h +77 -0
  247. package/native/core/include/ngn/screen-capture.h +35 -0
  248. package/native/core/include/ngn/screen-presentation.h +85 -0
  249. package/native/core/include/ngn/screen-stacks.h +80 -0
  250. package/native/core/include/ngn/scrollers.h +106 -0
  251. package/native/core/include/ngn/sharing.h +39 -0
  252. package/native/core/include/ngn/spawn-rules.h +78 -0
  253. package/native/core/include/ngn/splash-screen.h +41 -0
  254. package/native/core/include/ngn/split-view.h +270 -0
  255. package/native/core/include/ngn/sqlite.h +38 -0
  256. package/native/core/include/ngn/store-review.h +26 -0
  257. package/native/core/include/ngn/surface-book.h +101 -0
  258. package/native/core/include/ngn/svg.h +393 -0
  259. package/native/core/include/ngn/symbol.h +90 -0
  260. package/native/core/include/ngn/system.h +102 -0
  261. package/native/core/include/ngn/tabs-host.h +171 -0
  262. package/native/core/include/ngn/text-input.h +125 -0
  263. package/native/core/include/ngn/text-measurer.h +38 -0
  264. package/native/core/include/ngn/text-recognition.h +55 -0
  265. package/native/core/include/ngn/tracking.h +34 -0
  266. package/native/core/include/ngn/tray-and-dock.h +107 -0
  267. package/native/core/include/ngn/undo.h +37 -0
  268. package/native/core/include/ngn/updates.h +253 -0
  269. package/native/core/include/ngn/video.h +126 -0
  270. package/native/core/include/ngn/view-box.h +126 -0
  271. package/native/core/include/ngn/view-transform.h +26 -0
  272. package/native/core/include/ngn/view-tree.h +85 -0
  273. package/native/core/include/ngn/web-browser.h +45 -0
  274. package/native/core/include/ngn/web-view.h +133 -0
  275. package/native/core/include/ngn/worklet-host.h +98 -0
  276. package/native/core/src/absent-views.cpp +41 -0
  277. package/native/core/src/accessibility.cpp +374 -0
  278. package/native/core/src/animation-frames.cpp +49 -0
  279. package/native/core/src/app-menus.cpp +352 -0
  280. package/native/core/src/app-windows.cpp +444 -0
  281. package/native/core/src/back-swipe.cpp +91 -0
  282. package/native/core/src/bytes.cpp +158 -0
  283. package/native/core/src/control-sizes.cpp +232 -0
  284. package/native/core/src/default-menu.cpp +96 -0
  285. package/native/core/src/desktop-modal.cpp +162 -0
  286. package/native/core/src/desktop-view-props.cpp +80 -0
  287. package/native/core/src/dev-server-helper.cpp +152 -0
  288. package/native/core/src/dev-server.cpp +103 -0
  289. package/native/core/src/download.cpp +53 -0
  290. package/native/core/src/expo-image.cpp +548 -0
  291. package/native/core/src/expo-ui.cpp +203 -0
  292. package/native/core/src/focus-order.cpp +441 -0
  293. package/native/core/src/gestures.cpp +1806 -0
  294. package/native/core/src/glass.cpp +49 -0
  295. package/native/core/src/header-bar.cpp +104 -0
  296. package/native/core/src/host-services.cpp +50 -0
  297. package/native/core/src/host-state.cpp +61 -0
  298. package/native/core/src/input-dispatcher.cpp +727 -0
  299. package/native/core/src/input-script.cpp +123 -0
  300. package/native/core/src/key-value-store.cpp +169 -0
  301. package/native/core/src/launch-settings.cpp +148 -0
  302. package/native/core/src/layout-animations.cpp +62 -0
  303. package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
  304. package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
  305. package/native/core/src/libraries/react-native-worklets.cpp +192 -0
  306. package/native/core/src/libraries/rnscreens.cpp +125 -0
  307. package/native/core/src/libraries/rnsvg.cpp +345 -0
  308. package/native/core/src/libraries/safeareacontext.cpp +70 -0
  309. package/native/core/src/libraries/worklets-idle-frames.js +70 -0
  310. package/native/core/src/library-glue.cpp +35 -0
  311. package/native/core/src/live-regions.cpp +122 -0
  312. package/native/core/src/module-registry.cpp +50 -0
  313. package/native/core/src/modules/accessibility-manager.cpp +98 -0
  314. package/native/core/src/modules/address-search.cpp +219 -0
  315. package/native/core/src/modules/alert-manager.cpp +140 -0
  316. package/native/core/src/modules/app-info.cpp +74 -0
  317. package/native/core/src/modules/app-modules.cpp +405 -0
  318. package/native/core/src/modules/apple-authentication.cpp +188 -0
  319. package/native/core/src/modules/asset.cpp +111 -0
  320. package/native/core/src/modules/async-storage.cpp +192 -0
  321. package/native/core/src/modules/audio.cpp +541 -0
  322. package/native/core/src/modules/battery.cpp +88 -0
  323. package/native/core/src/modules/calendar.cpp +457 -0
  324. package/native/core/src/modules/camera.cpp +221 -0
  325. package/native/core/src/modules/core-modules.cpp +62 -0
  326. package/native/core/src/modules/crypto.cpp +117 -0
  327. package/native/core/src/modules/desktop-expo.cpp +527 -0
  328. package/native/core/src/modules/file-system.cpp +941 -0
  329. package/native/core/src/modules/fonts.cpp +163 -0
  330. package/native/core/src/modules/image-loader.cpp +105 -0
  331. package/native/core/src/modules/image-manipulator.cpp +305 -0
  332. package/native/core/src/modules/language-model.cpp +195 -0
  333. package/native/core/src/modules/local-authentication.cpp +140 -0
  334. package/native/core/src/modules/location.cpp +186 -0
  335. package/native/core/src/modules/maps.cpp +500 -0
  336. package/native/core/src/modules/network-state.cpp +72 -0
  337. package/native/core/src/modules/networking.cpp +387 -0
  338. package/native/core/src/modules/notifications.cpp +549 -0
  339. package/native/core/src/modules/photo-library.cpp +371 -0
  340. package/native/core/src/modules/printing.cpp +201 -0
  341. package/native/core/src/modules/screen-capture.cpp +71 -0
  342. package/native/core/src/modules/sharing.cpp +59 -0
  343. package/native/core/src/modules/splash-screen.cpp +70 -0
  344. package/native/core/src/modules/sqlite.cpp +421 -0
  345. package/native/core/src/modules/store-review.cpp +23 -0
  346. package/native/core/src/modules/text-recognition.cpp +107 -0
  347. package/native/core/src/modules/tracking.cpp +46 -0
  348. package/native/core/src/modules/undo.cpp +59 -0
  349. package/native/core/src/modules/video.cpp +366 -0
  350. package/native/core/src/modules/web-browser.cpp +100 -0
  351. package/native/core/src/modules/web-socket.cpp +266 -0
  352. package/native/core/src/mounting-manager.cpp +380 -0
  353. package/native/core/src/native-controls.cpp +129 -0
  354. package/native/core/src/native-module.cpp +250 -0
  355. package/native/core/src/network.cpp +219 -0
  356. package/native/core/src/posix-launcher.cpp +366 -0
  357. package/native/core/src/processes.cpp +324 -0
  358. package/native/core/src/red-box-errors.js +83 -0
  359. package/native/core/src/red-box.cpp +306 -0
  360. package/native/core/src/runtime.cpp +422 -0
  361. package/native/core/src/safe-areas.cpp +130 -0
  362. package/native/core/src/screen-presentation.cpp +61 -0
  363. package/native/core/src/screen-stacks.cpp +254 -0
  364. package/native/core/src/scrollers.cpp +181 -0
  365. package/native/core/src/spawn-rules.cpp +292 -0
  366. package/native/core/src/split-view.cpp +343 -0
  367. package/native/core/src/svg.cpp +1883 -0
  368. package/native/core/src/symbol.cpp +94 -0
  369. package/native/core/src/tabs-host.cpp +234 -0
  370. package/native/core/src/text-input.cpp +126 -0
  371. package/native/core/src/text-layout-manager.cpp +63 -0
  372. package/native/core/src/tray-and-dock.cpp +154 -0
  373. package/native/core/src/updates.cpp +965 -0
  374. package/native/core/src/view-box.cpp +788 -0
  375. package/native/core/src/view-transform.cpp +45 -0
  376. package/native/core/src/view-tree.cpp +234 -0
  377. package/native/core/src/web-view.cpp +143 -0
  378. package/native/core/src/worklet-host.cpp +10 -0
  379. package/native/scripts/build-hermes.sh +44 -0
  380. package/native/scripts/build.sh +171 -0
  381. package/native/scripts/cache-dir.sh +18 -0
  382. package/native/scripts/codegen-library.sh +193 -0
  383. package/native/scripts/compiler-launcher.sh +17 -0
  384. package/native/scripts/fetch.sh +174 -0
  385. package/native/scripts/hermes-built.sh +10 -0
  386. package/native/scripts/host-dir.sh +16 -0
  387. package/native/scripts/macos-target.sh +8 -0
  388. package/native/scripts/make-prebuilt.mjs +360 -0
  389. package/native/scripts/prebuilt-host-cache.mjs +158 -0
  390. package/native/shim/openssl/sha.h +22 -0
  391. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
  392. package/package.json +70 -3
  393. package/prebuilt.json +33 -0
@@ -0,0 +1,472 @@
1
+ // The Swift half of NgNativeLocalLlm: Apple's Foundation Models, which only Swift can call, behind
2
+ // a C interface that language-model.mm adapts to the core's LanguageModelEngine. macOS 26 and
3
+ // later; on an older system every session fails and the availability is notEligible.
4
+ //
5
+ // The model's mechanics follow expo-local-llm's iOS module (MIT): the availability names, the
6
+ // schema for structured output and tool arguments, tool calls answered from JavaScript with a
7
+ // timeout, and the error codes JavaScript reads.
8
+ import Foundation
9
+
10
+ #if canImport(FoundationModels)
11
+ import FoundationModels
12
+ #endif
13
+
14
+ public typealias NGNLMText = @convention(c) (UnsafeMutableRawPointer?, UnsafePointer<CChar>) -> Void
15
+ // The turn's context, then the text, or the error's code and message.
16
+ public typealias NGNLMDone = @convention(c) (UnsafeMutableRawPointer?, UnsafePointer<CChar>?, UnsafePointer<CChar>?, UnsafePointer<CChar>?) -> Void
17
+ // The session's context, the call's id, the tool's name and its arguments as JSON.
18
+ public typealias NGNLMTool = @convention(c) (UnsafeMutableRawPointer?, UnsafePointer<CChar>, UnsafePointer<CChar>, UnsafePointer<CChar>) -> Void
19
+ public typealias NGNLMFree = @convention(c) (UnsafeMutableRawPointer?) -> Void
20
+
21
+ private func copyOut(_ text: String, _ out: UnsafeMutablePointer<CChar>, _ size: Int32) {
22
+ let bytes = Array(text.utf8.prefix(Int(size) - 1))
23
+ for (index, byte) in bytes.enumerated() {
24
+ out[index] = CChar(bitPattern: byte)
25
+ }
26
+ out[bytes.count] = 0
27
+ }
28
+
29
+ /// The availability as expo-local-llm names it, into `out`.
30
+ @_cdecl("ngn_lm_availability")
31
+ public func ngnLMAvailability(_ out: UnsafeMutablePointer<CChar>, _ size: Int32) {
32
+ var name = "notEligible"
33
+ #if canImport(FoundationModels)
34
+ if #available(macOS 26, *) {
35
+ name = NGNLanguageModel.availability()
36
+ }
37
+ #endif
38
+ copyOut(name, out, size)
39
+ }
40
+
41
+ /// A session for `config` (expo-local-llm's SessionConfig as JSON), or null with the reason in
42
+ /// `error`. Tool calls go to `onTool` with `context`, which `onFree` frees once nothing can call it.
43
+ @_cdecl("ngn_lm_session_create")
44
+ public func ngnLMSessionCreate(
45
+ _ config: UnsafePointer<CChar>, _ context: UnsafeMutableRawPointer?, _ onTool: NGNLMTool, _ onFree: NGNLMFree,
46
+ _ error: UnsafeMutablePointer<CChar>, _ size: Int32
47
+ ) -> UnsafeMutableRawPointer? {
48
+ let owner = NGNContext(context, onFree)
49
+ #if canImport(FoundationModels)
50
+ if #available(macOS 26, *) {
51
+ do {
52
+ let session = try NGNLanguageModel(config: String(cString: config), owner: owner, onTool: onTool)
53
+ return Unmanaged.passRetained(session).toOpaque()
54
+ } catch let failure {
55
+ copyOut(failure.localizedDescription, error, size)
56
+ return nil
57
+ }
58
+ }
59
+ #endif
60
+ copyOut("Apple's on-device model needs macOS 26", error, size)
61
+ return nil
62
+ }
63
+
64
+ /// One turn. `onDone` is called exactly once, `onText` with the text so far before it when
65
+ /// streaming.
66
+ @_cdecl("ngn_lm_session_respond")
67
+ public func ngnLMSessionRespond(
68
+ _ handle: UnsafeMutableRawPointer, _ prompt: UnsafePointer<CChar>, _ stream: Bool, _ context: UnsafeMutableRawPointer?,
69
+ _ onText: NGNLMText, _ onDone: NGNLMDone
70
+ ) {
71
+ #if canImport(FoundationModels)
72
+ if #available(macOS 26, *) {
73
+ let session = Unmanaged<NGNLanguageModel>.fromOpaque(handle).takeUnretainedValue()
74
+ // The address, which is Sendable where the pointer is not; the C side keeps the turn's context
75
+ // until onDone.
76
+ let turn = UInt(bitPattern: context)
77
+ session.respond(String(cString: prompt), stream: stream) { text in
78
+ text.withCString { onText(UnsafeMutableRawPointer(bitPattern: turn), $0) }
79
+ } done: { text, code, message in
80
+ let context = UnsafeMutableRawPointer(bitPattern: turn)
81
+ if let text {
82
+ text.withCString { onDone(context, $0, nil, nil) }
83
+ } else {
84
+ (code ?? "ERR_GENERATION_FAILED").withCString { c in
85
+ (message ?? "").withCString { m in onDone(context, nil, c, m) }
86
+ }
87
+ }
88
+ }
89
+ return
90
+ }
91
+ #endif
92
+ onDone(context, nil, "ERR_MODEL_UNAVAILABLE", "Apple's on-device model needs macOS 26")
93
+ }
94
+
95
+ @_cdecl("ngn_lm_session_cancel")
96
+ public func ngnLMSessionCancel(_ handle: UnsafeMutableRawPointer) {
97
+ #if canImport(FoundationModels)
98
+ if #available(macOS 26, *) {
99
+ Unmanaged<NGNLanguageModel>.fromOpaque(handle).takeUnretainedValue().cancel()
100
+ }
101
+ #endif
102
+ }
103
+
104
+ /// Answers a tool call: its output, or the error when `output` is null.
105
+ @_cdecl("ngn_lm_tool_answer")
106
+ public func ngnLMToolAnswer(
107
+ _ handle: UnsafeMutableRawPointer, _ callId: UnsafePointer<CChar>, _ output: UnsafePointer<CChar>?, _ error: UnsafePointer<CChar>?
108
+ ) -> Bool {
109
+ #if canImport(FoundationModels)
110
+ if #available(macOS 26, *) {
111
+ let session = Unmanaged<NGNLanguageModel>.fromOpaque(handle).takeUnretainedValue()
112
+ let id = String(cString: callId)
113
+ if let output {
114
+ return session.store.resolve(callId: id, result: String(cString: output))
115
+ }
116
+ return session.store.reject(callId: id, error: NGNToolError.handlerFailed(error.map { String(cString: $0) } ?? ""))
117
+ }
118
+ #endif
119
+ return false
120
+ }
121
+
122
+ /// Ends the session: its turn is cancelled and its tool calls rejected.
123
+ @_cdecl("ngn_lm_session_release")
124
+ public func ngnLMSessionRelease(_ handle: UnsafeMutableRawPointer) {
125
+ #if canImport(FoundationModels)
126
+ if #available(macOS 26, *) {
127
+ let session = Unmanaged<NGNLanguageModel>.fromOpaque(handle).takeRetainedValue()
128
+ session.close()
129
+ }
130
+ #endif
131
+ }
132
+
133
+ /// The C side's context, freed when the last thing that could call it goes.
134
+ final class NGNContext: @unchecked Sendable {
135
+ let pointer: UnsafeMutableRawPointer?
136
+ private let free: NGNLMFree
137
+ init(_ pointer: UnsafeMutableRawPointer?, _ free: @escaping NGNLMFree) {
138
+ self.pointer = pointer
139
+ self.free = free
140
+ }
141
+ deinit {
142
+ free(pointer)
143
+ }
144
+ }
145
+
146
+ enum NGNToolError: LocalizedError {
147
+ case timeout
148
+ case handlerFailed(String)
149
+ case invalidArguments
150
+
151
+ var errorDescription: String? {
152
+ switch self {
153
+ case .timeout: return "Tool call timed out waiting for JavaScript response"
154
+ case .handlerFailed(let message): return "Tool handler error: \(message)"
155
+ case .invalidArguments: return "Tool arguments were not a JSON object"
156
+ }
157
+ }
158
+ }
159
+
160
+ enum NGNSchemaError: LocalizedError {
161
+ case unsupportedType(String)
162
+ case malformed(String)
163
+ case badOption(String)
164
+
165
+ var errorDescription: String? {
166
+ switch self {
167
+ case .unsupportedType(let type): return "Unsupported schema type: \(type)"
168
+ case .malformed(let message): return "Malformed schema: \(message)"
169
+ case .badOption(let message): return message
170
+ }
171
+ }
172
+ }
173
+
174
+ /// Pending tool calls, each answered once: by JavaScript, its timeout, or the session closing.
175
+ final class NGNToolCalls: @unchecked Sendable {
176
+ private var pending: [String: (CheckedContinuation<String, Error>, Task<Void, Never>?)] = [:]
177
+ private var closed = false
178
+ private let lock = NSLock()
179
+
180
+ func store(callId: String, continuation: CheckedContinuation<String, Error>) -> Bool {
181
+ lock.lock()
182
+ if closed {
183
+ lock.unlock()
184
+ continuation.resume(throwing: CancellationError())
185
+ return false
186
+ }
187
+ pending[callId] = (continuation, nil)
188
+ lock.unlock()
189
+ return true
190
+ }
191
+
192
+ func attachTimeout(callId: String, task: Task<Void, Never>) {
193
+ lock.lock()
194
+ if pending[callId] != nil {
195
+ pending[callId]?.1 = task
196
+ lock.unlock()
197
+ } else {
198
+ lock.unlock()
199
+ task.cancel()
200
+ }
201
+ }
202
+
203
+ private func remove(callId: String) -> CheckedContinuation<String, Error>? {
204
+ lock.lock()
205
+ let entry = pending.removeValue(forKey: callId)
206
+ lock.unlock()
207
+ entry?.1?.cancel()
208
+ return entry?.0
209
+ }
210
+
211
+ func resolve(callId: String, result: String) -> Bool {
212
+ guard let continuation = remove(callId: callId) else { return false }
213
+ continuation.resume(returning: result)
214
+ return true
215
+ }
216
+
217
+ func reject(callId: String, error: Error) -> Bool {
218
+ guard let continuation = remove(callId: callId) else { return false }
219
+ continuation.resume(throwing: error)
220
+ return true
221
+ }
222
+
223
+ func close() {
224
+ lock.lock()
225
+ closed = true
226
+ let entries = pending
227
+ pending.removeAll()
228
+ lock.unlock()
229
+ for (_, entry) in entries {
230
+ entry.1?.cancel()
231
+ entry.0.resume(throwing: CancellationError())
232
+ }
233
+ }
234
+ }
235
+
236
+ #if canImport(FoundationModels)
237
+
238
+ @available(macOS 26, *)
239
+ enum NGNSchema {
240
+ static func build(properties: [String: Any], rootName: String) throws -> GenerationSchema {
241
+ try GenerationSchema(root: object(name: rootName, properties: properties), dependencies: [])
242
+ }
243
+
244
+ private static func object(name: String, properties: [String: Any]) throws -> DynamicGenerationSchema {
245
+ let fields: [DynamicGenerationSchema.Property] = try properties.keys.sorted().map { key in
246
+ guard let field = properties[key] as? [String: Any] else {
247
+ throw NGNSchemaError.malformed("Field '\(key)' must be an object")
248
+ }
249
+ return DynamicGenerationSchema.Property(
250
+ name: key, description: field["description"] as? String,
251
+ schema: try schema(name: key, type: field["type"] as? String ?? "string", field: field))
252
+ }
253
+ return DynamicGenerationSchema(name: name, properties: fields)
254
+ }
255
+
256
+ private static func schema(name: String, type: String, field: [String: Any]) throws -> DynamicGenerationSchema {
257
+ switch type {
258
+ case "string":
259
+ if let values = field["enum"] as? [String] {
260
+ return DynamicGenerationSchema(name: name, anyOf: values)
261
+ }
262
+ return DynamicGenerationSchema(type: String.self)
263
+ case "number": return DynamicGenerationSchema(type: Double.self)
264
+ case "integer": return DynamicGenerationSchema(type: Int.self)
265
+ case "boolean": return DynamicGenerationSchema(type: Bool.self)
266
+ case "array":
267
+ guard let items = field["items"] as? [String: Any] else {
268
+ throw NGNSchemaError.malformed("Array field '\(name)' must declare 'items'")
269
+ }
270
+ return DynamicGenerationSchema(
271
+ arrayOf: try schema(name: "\(name)Item", type: items["type"] as? String ?? "string", field: items))
272
+ case "object":
273
+ return try object(name: name, properties: field["properties"] as? [String: Any] ?? [:])
274
+ default:
275
+ throw NGNSchemaError.unsupportedType(type)
276
+ }
277
+ }
278
+ }
279
+
280
+ /// A tool whose calls JavaScript answers. Its arguments are decoded against its parameters.
281
+ @available(macOS 26, *)
282
+ final class NGNTool: Tool {
283
+ let name: String
284
+ let description: String
285
+ let parameters: GenerationSchema
286
+ typealias Arguments = GeneratedContent
287
+
288
+ private let owner: NGNContext
289
+ private let onTool: NGNLMTool
290
+ private let calls: NGNToolCalls
291
+ private let timeout: TimeInterval
292
+
293
+ init(name: String, description: String, parameters: GenerationSchema, timeout: TimeInterval, calls: NGNToolCalls,
294
+ owner: NGNContext, onTool: NGNLMTool) {
295
+ self.name = name
296
+ self.description = description
297
+ self.parameters = parameters
298
+ self.timeout = timeout
299
+ self.calls = calls
300
+ self.owner = owner
301
+ self.onTool = onTool
302
+ }
303
+
304
+ func call(arguments: GeneratedContent) async throws -> String {
305
+ let callId = UUID().uuidString
306
+ let json = arguments.jsonString
307
+ guard let data = json.data(using: .utf8), (try? JSONSerialization.jsonObject(with: data)) is [String: Any] else {
308
+ throw NGNToolError.invalidArguments
309
+ }
310
+ let calls = self.calls
311
+ return try await withTaskCancellationHandler {
312
+ // Stored before JavaScript hears of it, so an answer at once has a call to resume.
313
+ try await withCheckedThrowingContinuation { continuation in
314
+ guard calls.store(callId: callId, continuation: continuation) else { return }
315
+ let timeout = self.timeout
316
+ let timer = Task {
317
+ try? await Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
318
+ if !Task.isCancelled {
319
+ _ = calls.reject(callId: callId, error: NGNToolError.timeout)
320
+ }
321
+ }
322
+ calls.attachTimeout(callId: callId, task: timer)
323
+ if Task.isCancelled {
324
+ _ = calls.reject(callId: callId, error: CancellationError())
325
+ return
326
+ }
327
+ callId.withCString { id in
328
+ name.withCString { tool in
329
+ json.withCString { args in onTool(owner.pointer, id, tool, args) }
330
+ }
331
+ }
332
+ }
333
+ } onCancel: {
334
+ _ = calls.reject(callId: callId, error: CancellationError())
335
+ }
336
+ }
337
+ }
338
+
339
+ @available(macOS 26, *)
340
+ final class NGNLanguageModel: @unchecked Sendable {
341
+ let store: NGNToolCalls
342
+ private let session: LanguageModelSession
343
+ private let options: GenerationOptions
344
+ private let schema: GenerationSchema?
345
+ private let includeSchemaInPrompt: Bool
346
+ private let lock = NSLock()
347
+ private var task: Task<Void, Never>?
348
+
349
+ static func availability() -> String {
350
+ switch SystemLanguageModel.default.availability {
351
+ case .available: return "available"
352
+ case .unavailable(let reason):
353
+ switch reason {
354
+ case .deviceNotEligible: return "notEligible"
355
+ case .modelNotReady: return "notReady"
356
+ case .appleIntelligenceNotEnabled: return "notEnabled"
357
+ @unknown default: return "unknown"
358
+ }
359
+ @unknown default: return "unknown"
360
+ }
361
+ }
362
+
363
+ init(config text: String, owner: NGNContext, onTool: NGNLMTool) throws {
364
+ let config = (try? JSONSerialization.jsonObject(with: Data(text.utf8))) as? [String: Any] ?? [:]
365
+ let instructions = config["instructions"] as? String
366
+ let sampling = config["options"] as? [String: Any] ?? [:]
367
+ let topK = sampling["topK"] as? Int
368
+ options = GenerationOptions(
369
+ sampling: topK.map { .random(top: $0) }, temperature: sampling["temperature"] as? Double,
370
+ maximumResponseTokens: sampling["maxTokens"] as? Int)
371
+ includeSchemaInPrompt = config["includeSchemaInPrompt"] as? Bool ?? true
372
+ if config["responseFormat"] as? String == "json", let properties = config["schema"] as? [String: Any] {
373
+ schema = try NGNSchema.build(properties: properties, rootName: "Response")
374
+ } else {
375
+ schema = nil
376
+ }
377
+ let timeout = config["toolTimeout"] as? Double ?? 30
378
+ // Checked here: a tool call turns it into nanoseconds, which traps outside UInt64.
379
+ guard timeout.isFinite, timeout > 0, timeout <= 86_400 else {
380
+ throw NGNSchemaError.badOption("toolTimeout must be a number of seconds above 0 and at most 86400")
381
+ }
382
+ let store = NGNToolCalls()
383
+ self.store = store
384
+ let tools: [any Tool] = try (config["tools"] as? [[String: Any]] ?? []).map { tool in
385
+ let name = tool["name"] as? String ?? ""
386
+ return NGNTool(
387
+ name: name, description: tool["description"] as? String ?? "",
388
+ parameters: try NGNSchema.build(properties: tool["parameters"] as? [String: Any] ?? [:], rootName: "\(name)Arguments"),
389
+ timeout: timeout, calls: store, owner: owner, onTool: onTool)
390
+ }
391
+ session = instructions.map { LanguageModelSession(tools: tools, instructions: $0) } ?? LanguageModelSession(tools: tools)
392
+ // Keeps the C side's context for as long as the session is, even with no tools.
393
+ self.owner = owner
394
+ }
395
+
396
+ private let owner: NGNContext
397
+
398
+ func respond(_ prompt: String, stream: Bool, onText: @escaping @Sendable (String) -> Void,
399
+ done: @escaping @Sendable (String?, String?, String?) -> Void) {
400
+ let session = self.session
401
+ let options = self.options
402
+ let schema = self.schema
403
+ let includeSchemaInPrompt = self.includeSchemaInPrompt
404
+ let running = Task {
405
+ do {
406
+ var text = ""
407
+ if stream, let schema {
408
+ for try await snapshot in session.streamResponse(
409
+ to: prompt, schema: schema, includeSchemaInPrompt: includeSchemaInPrompt, options: options) {
410
+ text = snapshot.content.jsonString
411
+ onText(text)
412
+ }
413
+ } else if stream {
414
+ for try await partial in session.streamResponse(to: prompt, options: options) {
415
+ text = partial.content
416
+ onText(text)
417
+ }
418
+ } else if let schema {
419
+ text = try await session.respond(
420
+ to: prompt, schema: schema, includeSchemaInPrompt: includeSchemaInPrompt, options: options
421
+ ).content.jsonString
422
+ } else {
423
+ text = try await session.respond(to: prompt, options: options).content
424
+ }
425
+ try Task.checkCancellation()
426
+ done(text, nil, nil)
427
+ } catch {
428
+ done(nil, NGNLanguageModel.code(for: error), error.localizedDescription)
429
+ }
430
+ }
431
+ lock.lock()
432
+ task = running
433
+ lock.unlock()
434
+ }
435
+
436
+ func cancel() {
437
+ lock.lock()
438
+ let running = task
439
+ lock.unlock()
440
+ running?.cancel()
441
+ }
442
+
443
+ func close() {
444
+ store.close()
445
+ cancel()
446
+ }
447
+
448
+ static func code(for error: Error) -> String {
449
+ if error is CancellationError { return "ERR_CANCELLED" }
450
+ if error is NGNToolError { return "ERR_TOOL_FAILED" }
451
+ if let failure = error as? LanguageModelSession.GenerationError {
452
+ switch failure {
453
+ case .exceededContextWindowSize: return "ERR_CONTEXT_WINDOW_EXCEEDED"
454
+ case .assetsUnavailable: return "ERR_MODEL_UNAVAILABLE"
455
+ case .guardrailViolation: return "ERR_GUARDRAIL_VIOLATION"
456
+ case .unsupportedGuide: return "ERR_UNSUPPORTED_REQUEST"
457
+ case .unsupportedLanguageOrLocale: return "ERR_UNSUPPORTED_LANGUAGE"
458
+ case .decodingFailure: return "ERR_DECODING_FAILED"
459
+ case .rateLimited: return "ERR_RATE_LIMITED"
460
+ case .concurrentRequests: return "ERR_SESSION_BUSY"
461
+ case .refusal: return "ERR_REFUSAL"
462
+ @unknown default: return "ERR_GENERATION_FAILED"
463
+ }
464
+ }
465
+ if let failure = error as? LanguageModelSession.ToolCallError {
466
+ return code(for: failure.underlyingError) == "ERR_CANCELLED" ? "ERR_CANCELLED" : "ERR_TOOL_FAILED"
467
+ }
468
+ return "ERR_GENERATION_FAILED"
469
+ }
470
+ }
471
+
472
+ #endif
@@ -0,0 +1,15 @@
1
+ // The OS half of NgNativeLocalAuthentication (the core's local-authentication.h): LAContext.
2
+ #pragma once
3
+
4
+ #include "ngn/local-authentication.h"
5
+
6
+ #include <memory>
7
+
8
+ namespace ngn::appkit {
9
+
10
+ // With `noPrompts` (NGN_NO_PROMPTS=1, or a test launch's fake system UI) no prompt is ever shown: an
11
+ // evaluation fails at once as not_interactive, so an automated run never waits on a dialog nobody
12
+ // can answer.
13
+ std::shared_ptr<Authenticator> makeAuthenticator(bool noPrompts);
14
+
15
+ } // namespace ngn::appkit
@@ -0,0 +1,133 @@
1
+ // The OS half of NgNativeLocalAuthentication: Touch ID and the login password through LAContext,
2
+ // one context for each prompt so cancelAuthenticate can end it.
3
+ #import "local-authentication.h"
4
+
5
+ #import <Foundation/Foundation.h>
6
+ #import <LocalAuthentication/LocalAuthentication.h>
7
+
8
+ #include <cstdlib>
9
+ #include <mutex>
10
+
11
+ namespace ngn::appkit {
12
+
13
+ namespace {
14
+
15
+ NSString *ns(const std::string &s) {
16
+ NSString *out = [[NSString alloc] initWithBytes:s.data() length:s.size() encoding:NSUTF8StringEncoding];
17
+ return out != nil ? out : @"";
18
+ }
19
+
20
+ Authenticator::Failure failureOf(NSError *error) {
21
+ using F = Authenticator::Failure;
22
+ if (![error.domain isEqualToString:LAErrorDomain]) {
23
+ return F::Other;
24
+ }
25
+ switch (error.code) {
26
+ case LAErrorAuthenticationFailed:
27
+ return F::AuthenticationFailed;
28
+ case LAErrorUserCancel:
29
+ return F::UserCancel;
30
+ case LAErrorUserFallback:
31
+ return F::UserFallback;
32
+ case LAErrorSystemCancel:
33
+ return F::SystemCancel;
34
+ case LAErrorAppCancel:
35
+ return F::AppCancel;
36
+ case LAErrorPasscodeNotSet:
37
+ return F::PasscodeNotSet;
38
+ case LAErrorBiometryNotAvailable:
39
+ return F::BiometryNotAvailable;
40
+ case LAErrorBiometryNotEnrolled:
41
+ return F::BiometryNotEnrolled;
42
+ case LAErrorBiometryLockout:
43
+ return F::BiometryLockout;
44
+ case LAErrorInvalidContext:
45
+ return F::InvalidContext;
46
+ case LAErrorNotInteractive:
47
+ return F::NotInteractive;
48
+ default:
49
+ return F::Other;
50
+ }
51
+ }
52
+
53
+ class LAAuthenticator final : public Authenticator {
54
+ public:
55
+ explicit LAAuthenticator(bool noPrompts) : noPrompts_(noPrompts) {}
56
+ Facts facts() override {
57
+ LAContext *context = [LAContext new];
58
+ NSError *error = nil;
59
+ Facts facts;
60
+ if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
61
+ facts.biometryUnusable = failureOf(error);
62
+ }
63
+ switch (context.biometryType) {
64
+ case LABiometryTypeTouchID:
65
+ facts.biometry = Biometry::TouchId;
66
+ break;
67
+ case LABiometryTypeFaceID:
68
+ facts.biometry = Biometry::FaceId;
69
+ break;
70
+ default:
71
+ if (@available(macOS 14.0, *)) {
72
+ if (context.biometryType == LABiometryTypeOpticID) {
73
+ facts.biometry = Biometry::OpticId;
74
+ }
75
+ }
76
+ break;
77
+ }
78
+ facts.passwordSet = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil];
79
+ return facts;
80
+ }
81
+
82
+ void evaluate(const Request &request, std::function<void(std::optional<Failure>)> done) override {
83
+ LAContext *context = [LAContext new];
84
+ if (request.cancelTitle) {
85
+ context.localizedCancelTitle = ns(*request.cancelTitle);
86
+ }
87
+ if (request.fallbackTitle) {
88
+ context.localizedFallbackTitle = ns(*request.fallbackTitle);
89
+ }
90
+ if (noPrompts_) {
91
+ context.interactionNotAllowed = YES;
92
+ }
93
+ {
94
+ std::lock_guard lock(mutex_);
95
+ [pending_ addObject:context];
96
+ }
97
+ auto policy = request.biometricsOnly ? LAPolicyDeviceOwnerAuthenticationWithBiometrics : LAPolicyDeviceOwnerAuthentication;
98
+ [context evaluatePolicy:policy
99
+ localizedReason:ns(request.reason)
100
+ reply:^(BOOL success, NSError *error) {
101
+ {
102
+ std::lock_guard lock(mutex_);
103
+ [pending_ removeObject:context];
104
+ }
105
+ done(success ? std::nullopt : std::optional(failureOf(error)));
106
+ }];
107
+ }
108
+
109
+ // Every evaluation still running, an earlier one a second authenticateAsync overlapped too.
110
+ void cancel() override {
111
+ NSArray<LAContext *> *contexts;
112
+ {
113
+ std::lock_guard lock(mutex_);
114
+ contexts = pending_.allObjects;
115
+ }
116
+ for (LAContext *context in contexts) {
117
+ [context invalidate];
118
+ }
119
+ }
120
+
121
+ private:
122
+ bool noPrompts_;
123
+ std::mutex mutex_;
124
+ NSMutableSet<LAContext *> *pending_ = [NSMutableSet set];
125
+ };
126
+
127
+ } // namespace
128
+
129
+ std::shared_ptr<Authenticator> makeAuthenticator(bool noPrompts) {
130
+ return std::make_shared<LAAuthenticator>(noPrompts);
131
+ }
132
+
133
+ } // namespace ngn::appkit
@@ -0,0 +1,16 @@
1
+ // The OS half of NgNativeLocation (the core's location.h): CLLocationManager.
2
+ #pragma once
3
+
4
+ #include "ngn/location.h"
5
+
6
+ #include <memory>
7
+
8
+ namespace ngn::appkit {
9
+
10
+ // Made on the main thread, whose run loop CLLocationManager reports on. It asks for the permission
11
+ // only where the app's Info.plist explains why (NSLocationUsageDescription or
12
+ // NSLocationWhenInUseUsageDescription): without one macOS shows no prompt, so the request answers
13
+ // what is decided rather than wait for an answer that never comes.
14
+ std::shared_ptr<LocationSource> makeLocationSource();
15
+
16
+ } // namespace ngn::appkit