@h1r-ai/croche-react-native 0.5.5 → 0.7.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 (248) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +90 -451
  3. package/THIRD-PARTY-NOTICES +906 -14
  4. package/android/build.gradle +6 -1
  5. package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +126 -0
  6. package/android/src/main/AndroidManifest.xml +0 -5
  7. package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +106 -0
  8. package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +217 -0
  9. package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +48 -824
  10. package/android/src/main/java/dev/croche/reactnative/NativePal.kt +68 -0
  11. package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
  12. package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +64 -41
  13. package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +64 -0
  14. package/android/src/test/java/dev/croche/reactnative/BlobFilesTest.kt +107 -0
  15. package/android/src/test/java/dev/croche/reactnative/ImageAdapterWireTest.kt +70 -0
  16. package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +44 -0
  17. package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +19 -32
  18. package/android/src/test/java/dev/croche/reactnative/ProjectHandlerStagesTest.kt +32 -0
  19. package/android/src/test/java/dev/croche/reactnative/ProjectRecoveryTest.kt +12 -0
  20. package/android/src/test/java/dev/croche/reactnative/ProjectTimeZoneTest.kt +11 -15
  21. package/android/src/test/java/dev/croche/reactnative/StorageVersionWireTest.kt +37 -0
  22. package/android/src/test/java/dev/croche/reactnative/VerificationDispatchTest.kt +24 -0
  23. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
  24. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +10 -8
  25. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
  26. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +55 -0
  27. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -0
  28. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageCodec.kt +1 -0
  29. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageInput.kt +15 -0
  30. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapOrientation.kt +40 -0
  31. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapPalCodec.kt +66 -0
  32. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidAssetPal.kt +30 -0
  33. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreFactory.kt +35 -0
  34. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +116 -0
  35. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt +17 -0
  36. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +17 -0
  37. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidS2Pal.kt +14 -0
  38. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStateStore.kt +216 -0
  39. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +108 -0
  40. package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +8 -2
  41. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Adapters.kt +75 -0
  42. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
  43. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +31 -0
  44. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +25 -1
  45. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +121 -103
  46. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +89 -103
  47. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +20 -360
  48. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +136 -0
  49. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Decision.kt +170 -0
  50. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +23 -0
  51. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +26 -21
  52. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/EncodedImageCodec.kt +51 -0
  53. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +715 -0
  54. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +9 -322
  55. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
  56. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +16 -28
  57. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeEvent.kt +59 -0
  58. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeHandleOwner.kt +118 -0
  59. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +10 -74
  60. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +294 -37
  61. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +127 -0
  62. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
  63. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +73 -0
  64. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
  65. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StateStore.kt +90 -0
  66. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +88 -24
  67. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +5723 -3088
  68. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
  69. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +96 -0
  70. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundOsRegistration.kt +35 -0
  71. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundSink.kt +37 -0
  72. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreDefinitionCalls.kt +35 -0
  73. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +30 -0
  74. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +41 -0
  75. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +35 -0
  76. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +127 -0
  77. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ImageAdapterHost.kt +28 -0
  78. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +70 -0
  79. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStateStore.kt +125 -0
  80. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +64 -0
  81. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
  82. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativeCoreInitialization.kt +21 -0
  83. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +60 -0
  84. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PalCallbacks.kt +27 -0
  85. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PlainToolJson.kt +56 -0
  86. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +43 -0
  87. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
  88. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StateStoreContract.kt +103 -0
  89. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StorageVersionContract.kt +40 -0
  90. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
  91. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +100 -0
  92. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/BlobFiles.kt +157 -0
  93. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -0
  94. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/InstancePayloadPurge.kt +79 -0
  95. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/VersionedKeyFiles.kt +71 -0
  96. package/android/vendor/jniLibs/arm64-v8a/libcroche_ffi.so +0 -0
  97. package/android/vendor/jniLibs/armeabi-v7a/libcroche_ffi.so +0 -0
  98. package/android/vendor/jniLibs/x86_64/libcroche_ffi.so +0 -0
  99. package/croche-react-native.podspec +1 -0
  100. package/dist/package.json +1 -0
  101. package/dist/react-native/src/NativeCrocheRuntime.d.ts +16 -0
  102. package/dist/react-native/src/NativeCrocheRuntime.js +2 -0
  103. package/dist/react-native/src/core-native.d.ts +6 -0
  104. package/dist/react-native/src/core-native.js +316 -0
  105. package/dist/react-native/src/core-wire.d.ts +55 -0
  106. package/dist/react-native/src/core-wire.js +1 -0
  107. package/dist/react-native/src/native-module.d.ts +8 -0
  108. package/dist/react-native/src/native-module.js +53 -0
  109. package/dist/rn-shared-build.json +16 -0
  110. package/dist/runtime/src/adapters.d.ts +8 -0
  111. package/dist/runtime/src/adapters.js +29 -0
  112. package/dist/runtime/src/agent.d.ts +91 -0
  113. package/dist/runtime/src/agent.js +137 -0
  114. package/dist/runtime/src/app-api.d.ts +572 -0
  115. package/dist/runtime/src/app-api.js +1 -0
  116. package/dist/runtime/src/cancel.d.ts +9 -0
  117. package/dist/runtime/src/cancel.js +27 -0
  118. package/dist/runtime/src/core/foreground.d.ts +3 -0
  119. package/dist/runtime/src/core/foreground.js +40 -0
  120. package/dist/runtime/src/core/port.d.ts +247 -0
  121. package/dist/runtime/src/core/port.js +1 -0
  122. package/dist/runtime/src/core/wasm-owner.d.ts +32 -0
  123. package/dist/runtime/src/core/wasm-owner.js +237 -0
  124. package/dist/runtime/src/core/wasm.d.ts +1 -0
  125. package/dist/runtime/src/core/wasm.js +2 -0
  126. package/dist/runtime/src/croche.d.ts +97 -0
  127. package/dist/runtime/src/croche.js +172 -0
  128. package/dist/runtime/src/errors.d.ts +33 -0
  129. package/dist/runtime/src/errors.js +63 -0
  130. package/dist/runtime/src/events.d.ts +5 -0
  131. package/dist/runtime/src/events.js +31 -0
  132. package/dist/runtime/src/generated/app-stream.d.ts +328 -0
  133. package/dist/runtime/src/generated/app-stream.js +127 -0
  134. package/dist/runtime/src/image-input.d.ts +5 -0
  135. package/dist/runtime/src/image-input.js +60 -0
  136. package/dist/runtime/src/index.d.ts +6 -0
  137. package/dist/runtime/src/index.js +5 -0
  138. package/dist/runtime/src/index.native.d.ts +1 -0
  139. package/dist/runtime/src/index.native.js +2 -0
  140. package/dist/runtime/src/json.d.ts +13 -0
  141. package/dist/runtime/src/json.js +120 -0
  142. package/dist/runtime/src/native-adapter.d.ts +3 -0
  143. package/dist/runtime/src/native-adapter.js +30 -0
  144. package/dist/runtime/src/pal/host.d.ts +5 -0
  145. package/dist/runtime/src/pal/host.js +60 -0
  146. package/dist/runtime/src/pal/platform.d.ts +21 -0
  147. package/dist/runtime/src/pal/platform.js +7 -0
  148. package/dist/runtime/src/pal/state-store-lock.d.ts +8 -0
  149. package/dist/runtime/src/pal/state-store-lock.js +2 -0
  150. package/dist/runtime/src/pal/state-store.d.ts +66 -0
  151. package/dist/runtime/src/pal/state-store.js +1 -0
  152. package/dist/runtime/src/pal/storage-version-contract.d.ts +19 -0
  153. package/dist/runtime/src/pal/storage-version-contract.js +68 -0
  154. package/dist/runtime/src/pal/transactional-storage.d.ts +3 -0
  155. package/dist/runtime/src/pal/transactional-storage.js +124 -0
  156. package/dist/runtime/src/rn-entry.d.ts +1 -0
  157. package/dist/runtime/src/rn-entry.js +4 -0
  158. package/dist/runtime/src/run-tool.d.ts +33 -0
  159. package/dist/runtime/src/run-tool.js +7 -0
  160. package/dist/runtime/src/tools.d.ts +35 -0
  161. package/dist/runtime/src/tools.js +94 -0
  162. package/ios/CoreAgentTransport.swift +14 -0
  163. package/ios/CoreDeliveryTransport.swift +58 -0
  164. package/ios/CoreNativePal.swift +98 -0
  165. package/ios/CorePlatformTransport.swift +44 -0
  166. package/ios/CoreWireDispatch.swift +122 -0
  167. package/ios/CoreWireSupport.swift +286 -0
  168. package/ios/CrocheRuntimeBridge.swift +34 -994
  169. package/ios/ProjectPal.swift +49 -0
  170. package/ios/RNCrocheRuntime.mm +18 -201
  171. package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
  172. package/ios/vendor/CrocheRuntimeSDK/BlobFiles.swift +124 -0
  173. package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +60 -0
  174. package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
  175. package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
  176. package/ios/vendor/CrocheRuntimeSDK/EncodedImages.swift +85 -0
  177. package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +26 -157
  178. package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +672 -0
  179. package/ios/vendor/CrocheRuntimeSDK/Host.swift +34 -371
  180. package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
  181. package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
  182. package/ios/vendor/CrocheRuntimeSDK/InstancePayloadPurge.swift +88 -0
  183. package/ios/vendor/CrocheRuntimeSDK/JSON.swift +30 -14
  184. package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
  185. package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +1 -155
  186. package/ios/vendor/CrocheRuntimeSDK/Platform.swift +33 -59
  187. package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +289 -0
  188. package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +302 -0
  189. package/ios/vendor/CrocheRuntimeSDK/PlatformStateStore.swift +253 -0
  190. package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +281 -0
  191. package/ios/vendor/CrocheRuntimeSDK/Project.swift +4 -269
  192. package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +24 -461
  193. package/ios/vendor/CrocheRuntimeSDK/RuntimeHTTPAdapter.swift +42 -0
  194. package/ios/vendor/CrocheRuntimeSDK/S1/S1Adapters.swift +110 -0
  195. package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +248 -0
  196. package/ios/vendor/CrocheRuntimeSDK/S1/S1Allowance.swift +20 -0
  197. package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
  198. package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +170 -0
  199. package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +313 -0
  200. package/ios/vendor/CrocheRuntimeSDK/S1/S1Decision.swift +146 -0
  201. package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +37 -0
  202. package/ios/vendor/CrocheRuntimeSDK/S1/S1HandleOwner.swift +58 -0
  203. package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +346 -0
  204. package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +89 -0
  205. package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +165 -0
  206. package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +100 -0
  207. package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +278 -0
  208. package/ios/vendor/CrocheRuntimeSDK/S1/S1Verification.swift +27 -0
  209. package/ios/vendor/CrocheRuntimeSDK/S1/StateStoreContract.swift +189 -0
  210. package/ios/vendor/CrocheRuntimeSDK/S1/StorageVersionContract.swift +68 -0
  211. package/ios/vendor/CrocheRuntimeSDK/Storage.swift +23 -269
  212. package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +163 -116
  213. package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
  214. package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +4712 -2469
  215. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +899 -608
  216. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/libcroche_ffi.a +0 -0
  217. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/Headers/croche_ffiFFI.h +899 -608
  218. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +0 -0
  219. package/package.json +18 -8
  220. package/scripts/README.md +11 -0
  221. package/scripts/build-shared.mjs +46 -0
  222. package/scripts/build.mjs +11 -0
  223. package/scripts/check-swift-vendor-modules.sh +15 -0
  224. package/scripts/vendor-native.sh +8 -6
  225. package/src/NativeCrocheRuntime.ts +12 -159
  226. package/android/src/main/java/dev/croche/reactnative/AgentMemory.kt +0 -25
  227. package/android/src/main/java/dev/croche/reactnative/ProjectAttestation.kt +0 -17
  228. package/android/src/main/java/dev/croche/reactnative/ProjectTimeZone.kt +0 -14
  229. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/CrocheProjectRecordProvider.kt +0 -32
  230. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/WorkManagerScheduler.kt +0 -146
  231. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +0 -110
  232. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Control.kt +0 -178
  233. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +0 -173
  234. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
  235. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +0 -25
  236. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
  237. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +0 -87
  238. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +0 -181
  239. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +0 -227
  240. package/ios/vendor/CrocheRuntimeSDK/Background.swift +0 -59
  241. package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
  242. package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +0 -453
  243. package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +0 -38
  244. package/src/index.ts +0 -664
  245. package/src/project-types.ts +0 -35
  246. package/src/project.ts +0 -434
  247. package/src/protocol.ts +0 -28
  248. package/src/types.ts +0 -9
@@ -0,0 +1,122 @@
1
+ import Foundation
2
+
3
+ // The same S1 factory/handles used by the native facade; never AgentEngine.
4
+ // Arguments already contain the app's entire JSON. No scope/config interpreter.
5
+ final class RNCoreDispatch: @unchecked Sendable {
6
+ let handles = RNCoreHandles()
7
+ let callbacks = RNCoreCallbacks()
8
+ let deliveries = RNCoreDeliveries()
9
+ private func storage(_ args: [Any], _ storageIndex: Int, _ hostIndex: Int) throws -> RNCallbackStorage {
10
+ RNCallbackStorage(try rnString(args, storageIndex), host: try rnString(args, hostIndex), callbacks: callbacks)
11
+ }
12
+ private func image(_ args: [Any], _ index: Int) throws -> RNImageCallback {
13
+ try RNImageCallback(rnString(args, 3), support: index < args.count ? args[index] : nil, callbacks: callbacks, handles: handles)
14
+ }
15
+ private func blobs(_ args: [Any], _ index: Int) throws -> RNBlobCallback? {
16
+ guard index < args.count, args[index] as? Bool == true else { return nil }
17
+ return try RNBlobCallback(rnString(args, 3), callbacks)
18
+ }
19
+ private func registrations(_ args: [Any]) throws -> [CoreToolRegistration] {
20
+ guard let entries = args[1] as? [[String: Any]] else { throw RNWireError.badPacket }
21
+ return try entries.map { entry in
22
+ guard let id = entry["tool"] as? String else { throw RNWireError.badPacket }
23
+ return try CoreToolRegistration(tool: handles.get(id), optionsJson: entry["optionsJson"] as? String)
24
+ }
25
+ }
26
+ func eventSink(_ sink: String) -> RNCoreEventSink {
27
+ RNCoreEventSink(sink: sink, deliveries: deliveries) { [callbacks] json in
28
+ try callbacks.sendJSON(json)
29
+ }
30
+ }
31
+ func call(_ id: String, _ method: String, _ args: [Any], _ images: [CoreAttachmentInput] = []) throws -> Any {
32
+ if method == "release" { handles.forget(id); return NSNull() }
33
+ if method == "tryEnter" { guard let entered = deliveries.tryEnter(id) else { throw RNWireError.missingHandle(id) }; return entered }
34
+ if method == "complete" { guard deliveries.complete(id, try rnString(args, 0)) else { throw RNWireError.missingHandle(id) }; return NSNull() }
35
+ if id.isEmpty {
36
+ switch method {
37
+ case "platformImageSupport": return try PlatformPal.standard().imageSupport().map { try JSONSerialization.jsonObject(with: Data($0.utf8)) } as Any? ?? NSNull()
38
+ case "platformImageOperation":
39
+ guard let values = args[1] as? [NSNumber] else { throw RNWireError.badPacket }
40
+ let token: CancelToken = try handles.get(rnString(args, 3))
41
+ let reply = try PlatformPal.standard().imageOperation(operation: rnString(args, 0), bytes: Data(values.map { $0.uint8Value }), optionsJson: rnJSON(args[2]), cancel: token)
42
+ return ["info": try JSONSerialization.jsonObject(with: Data(reply.metadataJson.utf8)), "bytes": Array(reply.bytes)]
43
+ case "newCancel": return handles.hold(CancelToken())
44
+ case "defineTool":
45
+ let execute = RNToolExecute(try rnString(args, 1), callbacks, handles)
46
+ return handles.hold(try CoreToolDefinition(manifestJson: rnString(args, 0), execute: execute))
47
+ case "webSearch": return handles.hold(coreWebSearch())
48
+ case "image": return handles.hold(coreImage())
49
+ case "open":
50
+ let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage, image: image(args, 5), blobs: blobs(args, 6))
51
+ return handles.hold(try platform.open(rnString(args, 0), registrations: registrations(args), recoveryJson: rnOptional(args, 4)))
52
+ case "wakeTimes":
53
+ let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage, image: image(args, 4), blobs: blobs(args, 5))
54
+ return try coreWakeTimes(openJson: rnString(args, 0), queryJson: rnOptional(args, 1), storage: storage, host: platform.host)
55
+ case "registerBackground":
56
+ let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage, blobs: blobs(args, 6))
57
+ // Selected storage and OS-default marker storage are separate arguments;
58
+ // neither is inferred from the app JSON.
59
+ try platform.registerBackground(rnString(args, 0), sink: rnOptional(args, 1).map(eventSink), platformStorage: rnOptional(args, 4) == nil ? nil : platform.platformStorage)
60
+ return NSNull()
61
+ default: throw RNWireError.badPacket
62
+ }
63
+ }
64
+ switch method {
65
+ case "cancel":
66
+ if let token: CancelToken = try? handles.get(id) { token.cancel(); return NSNull() }
67
+ let agent: CoreAgent = try handles.get(id); try agent.cancel(inputId: rnOptional(args, 0)); return NSNull()
68
+ case "isCancelled": let token: CancelToken = try handles.get(id); return token.isCancelled()
69
+ case "attachment":
70
+ if let reader: any CoreAttachmentReader = try? handles.get(id) { return try reader.attachment(id: rnString(args, 0)) }
71
+ if let agent: CoreAgent = try? handles.get(id) { return try agent.attachment(id: rnString(args, 0)) }
72
+ let owner: CoreCroche = try handles.get(id); return try owner.attachment(id: rnString(args, 0))
73
+ case "deleteAttachment":
74
+ if let agent: CoreAgent = try? handles.get(id) { try agent.deleteAttachment(id: rnString(args, 0)) }
75
+ else { let owner: CoreCroche = try handles.get(id); try owner.deleteAttachment(id: rnString(args, 0)) }; return NSNull()
76
+ case "listAttachments":
77
+ if let agent: CoreAgent = try? handles.get(id) { return try agent.listAttachments(optionsJson: rnOptional(args, 0)) }
78
+ let owner: CoreCroche = try handles.get(id); return try owner.listAttachments(optionsJson: rnOptional(args, 0))
79
+ case "copyAttachment": let agent: CoreAgent = try handles.get(id); return try agent.copyAttachment(sourceJson: rnString(args, 0))
80
+ case "releaseAttachment": let agent: CoreAgent = try handles.get(id); try agent.releaseAttachment(id: rnString(args, 0)); return NSNull()
81
+ case "name": let tool: CoreToolDefinition = try handles.get(id); return tool.name()
82
+ case "agent": let owner: CoreCroche = try handles.get(id); return handles.hold(try owner.agent(id: rnString(args, 0), scopeJson: rnOptional(args, 1)))
83
+ case "deleteData": let owner: CoreCroche = try handles.get(id); try owner.deleteData(scopeJson: rnOptional(args, 0)); return NSNull()
84
+ case "deliverDue": let owner: CoreCroche = try handles.get(id); return try owner.deliverDue(optionsJson: rnOptional(args, 0), cancel: handles.get(rnString(args, 1)))
85
+ case "runTool": let owner: CoreCroche = try handles.get(id); return try owner.runTool(name: rnString(args, 0), argsJson: rnString(args, 1), optionsJson: rnString(args, 2), cancel: handles.get(rnString(args, 3)))
86
+ case "allowanceSet": let owner: CoreCroche = try handles.get(id); try owner.allowanceSet(limitJson: rnString(args, 0)); return NSNull()
87
+ case "wikiList": let owner: CoreCroche = try handles.get(id); return try owner.wikiList(optionsJson: rnOptional(args, 0))
88
+ case "verification": let owner: CoreCroche = try handles.get(id); return try owner.verification()
89
+ case "wikiPut": let owner: CoreCroche = try handles.get(id); try owner.wikiPut(docJson: rnString(args, 0)); return NSNull()
90
+ case "wikiRemove": let owner: CoreCroche = try handles.get(id); try owner.wikiRemove(path: rnString(args, 0)); return NSNull()
91
+ case "debugAttach": let owner: CoreCroche = try handles.get(id); return try owner.debugAttach(optionsJson: rnString(args, 0))
92
+ case "debugDetach": let owner: CoreCroche = try handles.get(id); try owner.debugDetach(); return NSNull()
93
+ case "id": let agent: CoreAgent = try handles.get(id); return agent.id()
94
+ case "instance": let agent: CoreAgent = try handles.get(id); return agent.instance() as Any? ?? NSNull()
95
+ case "decide":
96
+ let agent: CoreAgent = try handles.get(id), cancel: CancelToken = try handles.get(rnString(args, 2))
97
+ return try agent.decide(inputJson: rnString(args, 0), optionsJson: rnOptional(args, 1), cancel: cancel)
98
+ case "send", "request":
99
+ let agent: CoreAgent = try handles.get(id), cancel: CancelToken = try handles.get(rnString(args, 2))
100
+ if method == "send" { return try agent.send(inputJson: rnString(args, 0), images: images, optionsJson: rnOptional(args, 1), cancel: cancel) }
101
+ return try agent.request(inputJson: rnString(args, 0), images: images, optionsJson: rnOptional(args, 1), cancel: cancel)
102
+ case "subscribe": let agent: CoreAgent = try handles.get(id); return handles.hold(try agent.subscribe(selectionJson: rnString(args, 0), eventSink: eventSink(rnString(args, 1))))
103
+ case "clearHistory": let agent: CoreAgent = try handles.get(id); try agent.clearHistory(optionsJson: rnOptional(args, 0)); return NSNull()
104
+ case "memoryList": let agent: CoreAgent = try handles.get(id); return try agent.memoryList(optionsJson: rnOptional(args, 0))
105
+ case "memoryDelete": let agent: CoreAgent = try handles.get(id); try agent.memoryDelete(id: rnString(args, 0)); return NSNull()
106
+ case "memoryClear": let agent: CoreAgent = try handles.get(id); try agent.memoryClear(); return NSNull()
107
+ case "memoryEnabled": let agent: CoreAgent = try handles.get(id); try agent.memoryEnabled(on: args[0] as! Bool); return NSNull()
108
+ case "setTools": let agent: CoreAgent = try handles.get(id); try agent.setTools(scopeJson: rnString(args, 0)); return NSNull()
109
+ case "resetTools": let agent: CoreAgent = try handles.get(id); try agent.resetTools(); return NSNull()
110
+ case "setWiki": let agent: CoreAgent = try handles.get(id); try agent.setWiki(scopeJson: rnString(args, 0)); return NSNull()
111
+ case "resetWiki": let agent: CoreAgent = try handles.get(id); try agent.resetWiki(); return NSNull()
112
+ case "history": let sub: CoreSubscription = try handles.get(id); try sub.history(optionsJson: rnString(args, 0)); return NSNull()
113
+ case "ended": let sub: CoreSubscription = try handles.get(id); return sub.ended()
114
+ case "shutdown":
115
+ if let owner: CoreCroche = try? handles.get(id) { try owner.shutdown() }
116
+ else if let agent: CoreAgent = try? handles.get(id) { try agent.shutdown() }
117
+ else { let sub: CoreSubscription = try handles.get(id); sub.shutdown() }
118
+ return NSNull()
119
+ default: throw RNWireError.badPacket
120
+ }
121
+ }
122
+ }
@@ -0,0 +1,286 @@
1
+ import Foundation
2
+
3
+ // Transport handles only. Core owns project/agent reuse and all lifecycle rules.
4
+ enum RNWireError: Error { case badPacket, missingHandle(String), unavailable }
5
+ func rnJSON(_ value: Any) throws -> String {
6
+ String(decoding: try JSONSerialization.data(withJSONObject: value, options: [.fragmentsAllowed, .sortedKeys]), as: UTF8.self)
7
+ }
8
+ func rnArgs(_ json: String) throws -> [Any] {
9
+ guard let args = try JSONSerialization.jsonObject(with: Data(json.utf8)) as? [Any] else { throw RNWireError.badPacket }
10
+ return args
11
+ }
12
+ func rnString(_ args: [Any], _ i: Int) throws -> String {
13
+ guard i < args.count, let value = args[i] as? String else { throw RNWireError.badPacket }; return value
14
+ }
15
+ func rnStorageString(_ value: Any?) throws -> String? { try storageNullableString(value) }
16
+ func rnOptional(_ args: [Any], _ i: Int) -> String? { i < args.count ? args[i] as? String : nil }
17
+
18
+ final class RNCoreHandles: @unchecked Sendable {
19
+ private let lock = NSLock()
20
+ private var values: [String: Any] = [:]
21
+ func hold(_ value: Any) -> String {
22
+ lock.lock(); defer { lock.unlock() }
23
+ let id = UUID().uuidString; values[id] = value
24
+ return id
25
+ }
26
+ func get<T>(_ id: String, as: T.Type = T.self) throws -> T {
27
+ lock.lock(); defer { lock.unlock() }
28
+ guard let value = values[id] as? T else { throw RNWireError.missingHandle(id) }; return value
29
+ }
30
+ func forget(_ id: String) {
31
+ lock.lock()
32
+ var removed: [Any] = []
33
+ if let value = values.removeValue(forKey: id) { removed.append(value) }
34
+ lock.unlock()
35
+ // Destroy outside the map lock: a PAL destructor can post a retirement fact.
36
+ withExtendedLifetime(removed) {}
37
+ }
38
+ func clear() { lock.lock(); let previous = values; values = [:]; lock.unlock(); withExtendedLifetime(previous) {} }
39
+ }
40
+
41
+ // A request/reply latch, not an event queue or deadline policy. Core runs these
42
+ // callbacks off the JS thread; module teardown wakes every unreachable callback.
43
+ final class RNCoreCallbacks: @unchecked Sendable {
44
+ private let lock = NSCondition()
45
+ private var replies: [String: String] = [:]
46
+ private var alive = true
47
+ private var sender: (@Sendable (String) -> Void)?
48
+ var emit: (@Sendable (String) -> Void)? {
49
+ get { lock.lock(); defer { lock.unlock() }; return alive ? sender : nil }
50
+ set { lock.lock(); sender = newValue; lock.unlock() }
51
+ }
52
+ func sendJSON(_ json: String) throws {
53
+ guard let send = emit else { throw RNWireError.unavailable }; send(json)
54
+ }
55
+ func post(_ packet: [String: Any]) throws { try sendJSON(rnJSON(packet)) }
56
+ func invoke(_ packet: [String: Any]) throws -> String {
57
+ let id = UUID().uuidString
58
+ var packet = packet; packet["call"] = id
59
+ try post(packet)
60
+ lock.lock(); defer { lock.unlock() }
61
+ while alive && replies[id] == nil { lock.wait() }
62
+ guard let reply = replies.removeValue(forKey: id) else { throw RNWireError.unavailable }; return reply
63
+ }
64
+ func reply(_ id: String, _ json: String) { lock.lock(); if alive { replies[id] = json }; lock.broadcast(); lock.unlock() }
65
+ func invalidate() { lock.lock(); alive = false; sender = nil; replies = [:]; lock.broadcast(); lock.unlock() }
66
+ }
67
+
68
+ func rnStateRequiredString(_ value: Any?) throws -> String {
69
+ guard let text = try rnStorageString(value) else { throw RNWireError.badPacket }
70
+ return text
71
+ }
72
+
73
+ /// An absent field and a JSON null both mean "not given".
74
+ func rnStateOptionalString(_ value: Any?) throws -> String? {
75
+ guard let value else { return nil }
76
+ return try rnStorageString(value)
77
+ }
78
+
79
+ /// The app StateStore's list options, as the wire carries them: every field optional.
80
+ func rnStateListOptions(_ value: Any?) throws -> CoreStateListOptions {
81
+ guard let fields = value as? [String: Any] else { throw RNWireError.badPacket }
82
+ var limit: UInt32?
83
+ if let raw = fields["limit"], !(raw is NSNull) {
84
+ guard let number = raw as? NSNumber, let exact = UInt32(exactly: number), exact > 0 else { throw RNWireError.badPacket }
85
+ limit = exact
86
+ }
87
+ var reverse = false
88
+ if let raw = fields["reverse"], !(raw is NSNull) {
89
+ guard let flag = raw as? Bool else { throw RNWireError.badPacket }
90
+ reverse = flag
91
+ }
92
+ return CoreStateListOptions(start: try rnStateOptionalString(fields["start"]), reverse: reverse, limit: limit,
93
+ cursor: try rnStateOptionalString(fields["cursor"]))
94
+ }
95
+
96
+ func rnStateListOptionsJSON(_ options: CoreStateListOptions) -> [String: Any] {
97
+ var fields: [String: Any] = [:]
98
+ if let start = options.start { fields["start"] = start }
99
+ if options.reverse { fields["reverse"] = true }
100
+ if let limit = options.limit { fields["limit"] = limit }
101
+ if let cursor = options.cursor { fields["cursor"] = cursor }
102
+ return fields
103
+ }
104
+
105
+ func rnStateOps(_ raw: Any) throws -> [CoreStateOp] {
106
+ guard let rows = raw as? [[String: Any]] else { throw RNWireError.badPacket }
107
+ return try rows.map { row in
108
+ let key = try rnStateRequiredString(row["key"])
109
+ switch row["type"] as? String {
110
+ case "put": return .put(key: key, value: try rnStateRequiredString(row["value"]))
111
+ case "delete": return .delete(key: key)
112
+ default: throw RNWireError.badPacket
113
+ }
114
+ }
115
+ }
116
+
117
+ func rnStateOpsJSON(_ ops: [CoreStateOp]) -> [[String: String]] {
118
+ ops.map { op in
119
+ switch op {
120
+ case let .put(key, value): return ["type": "put", "key": key, "value": value]
121
+ case let .delete(key): return ["type": "delete", "key": key]
122
+ }
123
+ }
124
+ }
125
+
126
+ func rnStateStorageCall(_ storage: any CoreStoragePal, _ method: String, _ args: [Any]) throws -> Any {
127
+ switch method {
128
+ case "listState":
129
+ guard args.count == 2 else { throw RNWireError.badPacket }
130
+ let page = try storage.listState(prefix: rnString(args, 0), options: rnStateListOptions(args[1]))
131
+ var reply: [String: Any] = ["entries": page.entries.map { ["key": $0.key, "value": $0.value] }]
132
+ if let next = page.nextCursor { reply["nextCursor"] = next }
133
+ return reply
134
+ case "commit":
135
+ guard args.count == 2, let condition = args[1] as? [String: Any] else { throw RNWireError.badPacket }
136
+ let result = try storage.commitState(
137
+ ops: rnStateOps(args[0]),
138
+ condition: CoreStateCondition(
139
+ key: rnStateRequiredString(condition["key"]),
140
+ version: rnStorageString(condition["version"])
141
+ )
142
+ )
143
+ switch result {
144
+ case let .committed(version): return ["type": "committed", "version": version as Any? ?? NSNull()]
145
+ case .conflict: return ["type": "conflict"]
146
+ }
147
+ default: throw RNWireError.badPacket
148
+ }
149
+ }
150
+
151
+ final class RNCallbackStorage: CoreStoragePal, @unchecked Sendable {
152
+ let id: String, host: String
153
+ let callbacks: RNCoreCallbacks
154
+ init(_ id: String, host: String, callbacks: RNCoreCallbacks) { self.id = id; self.host = host; self.callbacks = callbacks }
155
+ deinit { try? callbacks.post(["type":"retired", "storages":[id], "hosts":[host], "sinks":[], "executors":[], "tokens":[]]) }
156
+ private func call(_ method: String, _ args: [Any]) throws -> Any {
157
+ do {
158
+ let text = try callbacks.invoke(["type":"storage", "storage":id, "method":method, "args":args])
159
+ guard let result = try JSONSerialization.jsonObject(with: Data(text.utf8)) as? [String: Any] else { throw RNWireError.badPacket }
160
+ if let failure = result["error"] as? [String: Any] { throw CoreStorageFailure.Failure(detail: failure["detail"] as? String ?? "") }
161
+ guard let value = result["value"] else { throw RNWireError.unavailable }; return value
162
+ } catch let error as CoreStorageFailure { throw error }
163
+ catch { throw CoreStorageFailure.Failure(detail: String(describing: error)) }
164
+ }
165
+ func listState(prefix: String, options: CoreStateListOptions) throws -> CoreStatePage {
166
+ guard let page = try call("listState", [prefix, rnStateListOptionsJSON(options)]) as? [String: Any],
167
+ let rows = page["entries"] as? [[String: Any]] else {
168
+ throw CoreStorageFailure.Failure(detail: "Invalid StateStore range reply")
169
+ }
170
+ let entries = try rows.map {
171
+ try CoreStateEntry(key: rnStateRequiredString($0["key"]), value: rnStateRequiredString($0["value"]))
172
+ }
173
+ return CoreStatePage(entries: entries, nextCursor: try rnStateOptionalString(page["nextCursor"]))
174
+ }
175
+ func commitState(ops: [CoreStateOp], condition: CoreStateCondition) throws -> CoreStateCommit {
176
+ let guardRow: [String: Any] = ["key": condition.key, "version": condition.version as Any? ?? NSNull()]
177
+ guard let result = try call("commit", [rnStateOpsJSON(ops), guardRow]) as? [String: Any] else {
178
+ throw CoreStorageFailure.Failure(detail: "Invalid StateStore commit reply")
179
+ }
180
+ switch result["type"] as? String {
181
+ case "committed": return .committed(version: try rnStorageString(result["version"]))
182
+ case "conflict": return .conflict
183
+ default: throw CoreStorageFailure.Failure(detail: "Invalid StateStore commit reply")
184
+ }
185
+ }
186
+ func readVersioned(key: String) throws -> CoreVersionedValue {
187
+ try decodeStorageValue(call("readVersioned", [versionedStorageKey(key)]))
188
+ }
189
+ func writeIf(key: String, value: String?, expect: String?) throws -> CoreWriteOutcome {
190
+ let key = try versionedStorageKey(key), expect = try storageVersion(expect)
191
+ return try decodeStorageOutcome(call("writeIf", [key, value as Any? ?? NSNull(), expect as Any? ?? NSNull()]), for: value)
192
+ }
193
+ func read(key: String) throws -> String? { try call("read", [key]) as? String }
194
+ func write(key: String, value: String) throws {
195
+ _ = try call("write", [key, value])
196
+ }
197
+ func remove(key: String) throws { _ = try call("remove", [key]) }
198
+ func purgeInstance(prefix: String, secretKey: String?) throws {
199
+ _ = try call("purgeInstance", [prefix, secretKey as Any? ?? NSNull()])
200
+ }
201
+ func list(prefix: String) throws -> [String] { guard let value = try call("list", [prefix]) as? [String] else { throw CoreStorageFailure.Failure(detail: "Invalid list transport reply") }; return value }
202
+ func lock(name: String) throws -> PalLockOutcome {
203
+ switch try call("lock", [name]) as? String { case "acquired": return .acquired; case "held": return .held; default: throw CoreStorageFailure.Failure(detail: "Invalid lock transport reply") }
204
+ }
205
+ func unlock(name: String) throws { _ = try call("unlock", [name]) }
206
+ func readSecret(key: String) throws -> String? { try call("readSecret", [key]) as? String }
207
+ func writeSecret(key: String, value: String) throws { _ = try call("writeSecret", [key, value]) }
208
+ func removeSecret(key: String) throws { _ = try call("removeSecret", [key]) }
209
+ }
210
+
211
+ final class RNCancelNotice: CancelCallback, @unchecked Sendable {
212
+ let id: String; let callbacks: RNCoreCallbacks
213
+ init(_ id: String, _ callbacks: RNCoreCallbacks) { self.id = id; self.callbacks = callbacks }
214
+ func cancelled() { try? callbacks.post(["type":"cancel", "token":id]) }
215
+ }
216
+ final class RNToolExecute: CoreToolExecute, @unchecked Sendable {
217
+ let id: String; let callbacks: RNCoreCallbacks; let handles: RNCoreHandles
218
+ init(_ id: String, _ callbacks: RNCoreCallbacks, _ handles: RNCoreHandles) { self.id = id; self.callbacks = callbacks; self.handles = handles }
219
+ deinit { try? callbacks.post(["type":"retired", "executors":[id], "hosts":[], "storages":[], "sinks":[], "tokens":[]]) }
220
+ func execute(callJson: String, contextJson: String, cancel: CancelToken, attachments: any CoreAttachmentReader) throws -> String {
221
+ let token = handles.hold(cancel), reader = handles.hold(attachments); cancel.onCancel(callback: RNCancelNotice(token, callbacks))
222
+ defer { handles.forget(token) }
223
+ do { return try callbacks.invoke(["type":"execute", "execute":id, "callJson":callJson, "contextJson":contextJson, "cancel":token, "attachments":reader]) }
224
+ catch { handles.forget(reader); return #"{"type":"unavailable"}"# }
225
+ }
226
+ }
227
+
228
+ /// A selected JS BlobStore travels separately from StateStore and the default file host.
229
+ final class RNBlobCallback: Sendable {
230
+ let id: String
231
+ let callbacks: RNCoreCallbacks
232
+ init(_ id: String, _ callbacks: RNCoreCallbacks) { self.id = id; self.callbacks = callbacks }
233
+
234
+ func call(_ operation: String, _ key: String, _ cursor: String?, _ bytes: Data?) throws -> PalBlobReply {
235
+ do {
236
+ let raw = try callbacks.invoke(["type": "blob", "host": id, "operation": operation,
237
+ "key": key, "cursor": cursor as Any? ?? NSNull(),
238
+ "bytes": bytes.map { Array($0) } as Any? ?? NSNull()])
239
+ guard let reply = try JSONSerialization.jsonObject(with: Data(raw.utf8)) as? [String: Any] else {
240
+ throw RNWireError.badPacket
241
+ }
242
+ if let failure = reply["error"] as? [String: Any] {
243
+ throw FfiError.Failure(code: "storage_error", message: failure["detail"] as? String ?? "")
244
+ }
245
+ guard let value = reply["value"] as? [String: Any] else { throw RNWireError.unavailable }
246
+ var entries: [PalBlobEntry] = []
247
+ if operation == "list" {
248
+ guard let rows = value["entries"] as? [[String: Any]] else { throw RNWireError.badPacket }
249
+ entries = try rows.map { row in
250
+ guard let key = row["key"] as? String, let size = row["size"] as? UInt64 else { throw RNWireError.badPacket }
251
+ return PalBlobEntry(key: key, size: size)
252
+ }
253
+ }
254
+ let result: Data?
255
+ if operation == "get", !(value["bytes"] is NSNull) {
256
+ guard let bytes = value["bytes"] as? [UInt8] else { throw RNWireError.badPacket }
257
+ result = Data(bytes)
258
+ } else { result = nil }
259
+ return PalBlobReply(bytes: result, entries: entries, nextCursor: value["nextCursor"] as? String)
260
+ } catch let error as FfiError { throw error }
261
+ catch { throw FfiError.Failure(code: "storage_error", message: "Invalid blob transport reply") }
262
+ }
263
+ }
264
+
265
+ /// The encoded image implementation selected by the shared facade, with the native cancel token.
266
+ final class RNImageCallback: @unchecked Sendable {
267
+ let id: String, support: String?
268
+ let callbacks: RNCoreCallbacks, handles: RNCoreHandles
269
+ init(_ id: String, support: Any?, callbacks: RNCoreCallbacks, handles: RNCoreHandles) throws {
270
+ self.id = id; self.callbacks = callbacks; self.handles = handles
271
+ self.support = support == nil || support is NSNull ? nil : try rnJSON(support!)
272
+ }
273
+ func call(_ operation: String, _ bytes: Data, _ options: String, _ cancel: CancelToken) throws -> PalImageReply {
274
+ let token = handles.hold(cancel)
275
+ cancel.onCancel(callback: RNCancelNotice(token, callbacks))
276
+ defer { handles.forget(token) }
277
+ let reply = try callbacks.invoke(["type":"image", "host":id, "operation":operation, "bytes":Array(bytes),
278
+ "options":JSONSerialization.jsonObject(with: Data(options.utf8)), "cancel":token])
279
+ guard let packet = try JSONSerialization.jsonObject(with: Data(reply.utf8)) as? [String: Any] else { throw RNWireError.badPacket }
280
+ if let error = packet["error"] as? [String: Any] {
281
+ throw CorePalError.Failure(code: error["code"] as? String ?? "image_unsupported", detail: error["message"] as? String ?? "Image adapter failed")
282
+ }
283
+ guard let value = packet["value"] as? [String: Any] else { throw RNWireError.unavailable }
284
+ return PalImageReply(metadataJson: try rnJSON(value["info"] ?? [:]), bytes: Data((value["bytes"] as? [NSNumber] ?? []).map { $0.uint8Value }))
285
+ }
286
+ }