@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.
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +276 -0
- package/native/appkit/CMakeLists.txt +109 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +130 -0
- package/native/appkit/camera.h +39 -0
- package/native/appkit/camera.mm +362 -0
- package/native/appkit/cg-canvas.h +17 -0
- package/native/appkit/cg-canvas.mm +518 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +629 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1200 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1515 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +558 -0
- package/native/appkit/svg-view.h +17 -0
- package/native/appkit/svg-view.mm +57 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +76 -0
- package/native/appkit/text-input-field.mm +444 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +477 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +9 -0
- package/native/appkit/web-view-component.mm +56 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +394 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +146 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +112 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +128 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +105 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +130 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +161 -0
- package/native/core/include/ngn/safe-areas.h +77 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/svg.h +393 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +125 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +133 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +352 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +91 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +345 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +405 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +221 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +371 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +380 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +422 -0
- package/native/core/src/safe-areas.cpp +130 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +1883 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +126 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +171 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +70 -3
- 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
|