@h1r-ai/croche-react-native 0.5.5 → 0.6.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 (196) hide show
  1. package/README.md +69 -458
  2. package/THIRD-PARTY-NOTICES +7 -8
  3. package/android/build.gradle +6 -1
  4. package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +125 -0
  5. package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +81 -0
  6. package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +83 -0
  7. package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +45 -824
  8. package/android/src/main/java/dev/croche/reactnative/NativePal.kt +51 -0
  9. package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
  10. package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +42 -41
  11. package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +71 -0
  12. package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +29 -0
  13. package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +19 -32
  14. package/android/src/test/java/dev/croche/reactnative/ProjectHandlerStagesTest.kt +32 -0
  15. package/android/src/test/java/dev/croche/reactnative/ProjectRecoveryTest.kt +12 -0
  16. package/android/src/test/java/dev/croche/reactnative/ProjectTimeZoneTest.kt +11 -15
  17. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
  18. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +6 -6
  19. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
  20. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +51 -0
  21. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -0
  22. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageInput.kt +15 -0
  23. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapOrientation.kt +40 -0
  24. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapPalCodec.kt +66 -0
  25. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/WorkManagerScheduler.kt +8 -8
  26. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidAssetPal.kt +30 -0
  27. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreFactory.kt +35 -0
  28. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +121 -0
  29. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt +17 -0
  30. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +17 -0
  31. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidS2Pal.kt +14 -0
  32. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +73 -0
  33. package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +9 -0
  34. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +5 -2
  35. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
  36. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +45 -0
  37. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +16 -1
  38. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +60 -105
  39. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +47 -107
  40. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +41 -40
  41. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +67 -0
  42. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +20 -0
  43. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +18 -20
  44. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +322 -0
  45. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +71 -34
  46. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
  47. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +9 -7
  48. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +47 -19
  49. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +5 -5
  50. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +1 -1
  51. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +9 -9
  52. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +108 -37
  53. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +44 -0
  54. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
  55. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +40 -0
  56. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
  57. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +8 -8
  58. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StoragePal.kt +18 -0
  59. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +50 -25
  60. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +22 -22
  61. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +8285 -2159
  62. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
  63. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +25 -0
  64. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundOsRegistration.kt +35 -0
  65. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundSink.kt +37 -0
  66. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreDefinitionCalls.kt +35 -0
  67. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +31 -0
  68. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +41 -0
  69. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +35 -0
  70. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +128 -0
  71. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +72 -0
  72. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +34 -0
  73. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
  74. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +45 -0
  75. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PalCallbacks.kt +27 -0
  76. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PlainToolJson.kt +56 -0
  77. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +34 -0
  78. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
  79. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
  80. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +79 -0
  81. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -0
  82. package/android/vendor/jniLibs/arm64-v8a/libcroche_ffi.so +0 -0
  83. package/android/vendor/jniLibs/armeabi-v7a/libcroche_ffi.so +0 -0
  84. package/android/vendor/jniLibs/x86_64/libcroche_ffi.so +0 -0
  85. package/dist/package.json +1 -0
  86. package/dist/react-native/src/NativeCrocheRuntime.d.ts +16 -0
  87. package/dist/react-native/src/NativeCrocheRuntime.js +2 -0
  88. package/dist/react-native/src/core-native.d.ts +3 -0
  89. package/dist/react-native/src/core-native.js +193 -0
  90. package/dist/react-native/src/core-wire.d.ts +38 -0
  91. package/dist/react-native/src/core-wire.js +1 -0
  92. package/dist/react-native/src/native-module.d.ts +8 -0
  93. package/dist/react-native/src/native-module.js +53 -0
  94. package/dist/rn-shared-build.json +16 -0
  95. package/dist/runtime/src/agent.d.ts +43 -0
  96. package/dist/runtime/src/agent.js +75 -0
  97. package/dist/runtime/src/app-api.d.ts +284 -0
  98. package/dist/runtime/src/app-api.js +1 -0
  99. package/dist/runtime/src/cancel.d.ts +9 -0
  100. package/dist/runtime/src/cancel.js +24 -0
  101. package/dist/runtime/src/core/port.d.ts +234 -0
  102. package/dist/runtime/src/core/port.js +1 -0
  103. package/dist/runtime/src/core/wasm.d.ts +1 -0
  104. package/dist/runtime/src/core/wasm.js +2 -0
  105. package/dist/runtime/src/croche.d.ts +42 -0
  106. package/dist/runtime/src/croche.js +111 -0
  107. package/dist/runtime/src/errors.d.ts +26 -0
  108. package/dist/runtime/src/errors.js +49 -0
  109. package/dist/runtime/src/events.d.ts +5 -0
  110. package/dist/runtime/src/events.js +31 -0
  111. package/dist/runtime/src/generated/app-stream.d.ts +174 -0
  112. package/dist/runtime/src/generated/app-stream.js +124 -0
  113. package/dist/runtime/src/index.native.d.ts +21 -0
  114. package/dist/runtime/src/index.native.js +8 -0
  115. package/dist/runtime/src/json.d.ts +13 -0
  116. package/dist/runtime/src/json.js +120 -0
  117. package/dist/runtime/src/native-adapter.d.ts +3 -0
  118. package/dist/runtime/src/native-adapter.js +24 -0
  119. package/dist/runtime/src/pal/codec.d.ts +15 -0
  120. package/dist/runtime/src/pal/codec.js +52 -0
  121. package/dist/runtime/src/pal/host.d.ts +5 -0
  122. package/dist/runtime/src/pal/host.js +35 -0
  123. package/dist/runtime/src/pal/platform.d.ts +16 -0
  124. package/dist/runtime/src/pal/platform.js +7 -0
  125. package/dist/runtime/src/rn-entry.d.ts +1 -0
  126. package/dist/runtime/src/rn-entry.js +4 -0
  127. package/dist/runtime/src/run-tool.d.ts +33 -0
  128. package/dist/runtime/src/run-tool.js +7 -0
  129. package/dist/runtime/src/tools.d.ts +19 -0
  130. package/dist/runtime/src/tools.js +70 -0
  131. package/ios/CoreAgentTransport.swift +14 -0
  132. package/ios/CoreDeliveryTransport.swift +58 -0
  133. package/ios/CoreNativePal.swift +93 -0
  134. package/ios/CorePlatformTransport.swift +44 -0
  135. package/ios/CoreWireDispatch.swift +92 -0
  136. package/ios/CoreWireSupport.swift +111 -0
  137. package/ios/CrocheRuntimeBridge.swift +34 -994
  138. package/ios/ProjectPal.swift +46 -0
  139. package/ios/RNCrocheRuntime.mm +18 -201
  140. package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
  141. package/ios/vendor/CrocheRuntimeSDK/Background.swift +11 -11
  142. package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +64 -0
  143. package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
  144. package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
  145. package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +85 -43
  146. package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +443 -0
  147. package/ios/vendor/CrocheRuntimeSDK/Host.swift +122 -61
  148. package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
  149. package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
  150. package/ios/vendor/CrocheRuntimeSDK/JSON.swift +4 -4
  151. package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
  152. package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +37 -36
  153. package/ios/vendor/CrocheRuntimeSDK/Platform.swift +35 -35
  154. package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +285 -0
  155. package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +266 -0
  156. package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +195 -0
  157. package/ios/vendor/CrocheRuntimeSDK/Project.swift +209 -95
  158. package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +97 -123
  159. package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +195 -0
  160. package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
  161. package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +161 -0
  162. package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +234 -0
  163. package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +28 -0
  164. package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +177 -0
  165. package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +61 -0
  166. package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +95 -0
  167. package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +105 -0
  168. package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +209 -0
  169. package/ios/vendor/CrocheRuntimeSDK/Storage.swift +52 -52
  170. package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +180 -137
  171. package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +16 -10
  172. package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +131 -116
  173. package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
  174. package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +7209 -2803
  175. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +1342 -218
  176. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/libcroche_ffi.a +0 -0
  177. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/Headers/croche_ffiFFI.h +1342 -218
  178. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +0 -0
  179. package/package.json +18 -8
  180. package/scripts/README.md +11 -0
  181. package/scripts/build-shared.mjs +46 -0
  182. package/scripts/build.mjs +11 -0
  183. package/scripts/vendor-native.sh +5 -4
  184. package/src/NativeCrocheRuntime.ts +12 -159
  185. package/android/src/main/java/dev/croche/reactnative/AgentMemory.kt +0 -25
  186. package/android/src/main/java/dev/croche/reactnative/ProjectAttestation.kt +0 -17
  187. package/android/src/main/java/dev/croche/reactnative/ProjectTimeZone.kt +0 -14
  188. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Control.kt +0 -178
  189. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
  190. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
  191. package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
  192. package/src/index.ts +0 -664
  193. package/src/project-types.ts +0 -35
  194. package/src/project.ts +0 -434
  195. package/src/protocol.ts +0 -28
  196. package/src/types.ts +0 -9
@@ -0,0 +1,92 @@
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 registrations(_ args: [Any]) throws -> [CoreToolRegistration] {
13
+ guard let entries = args[1] as? [[String: Any]] else { throw RNWireError.badPacket }
14
+ return try entries.map { entry in
15
+ guard let id = entry["tool"] as? String else { throw RNWireError.badPacket }
16
+ return try CoreToolRegistration(tool: handles.get(id), optionsJson: entry["optionsJson"] as? String)
17
+ }
18
+ }
19
+ func eventSink(_ sink: String) -> RNCoreEventSink {
20
+ RNCoreEventSink(sink: sink, deliveries: deliveries) { [callbacks] json in
21
+ try callbacks.sendJSON(json)
22
+ }
23
+ }
24
+ func call(_ id: String, _ method: String, _ args: [Any], _ images: [CoreImageInput] = []) throws -> Any {
25
+ if method == "release" { handles.forget(id); return NSNull() }
26
+ if method == "tryEnter" { guard let entered = deliveries.tryEnter(id) else { throw RNWireError.missingHandle(id) }; return entered }
27
+ if method == "complete" { guard deliveries.complete(id, try rnString(args, 0)) else { throw RNWireError.missingHandle(id) }; return NSNull() }
28
+ if id.isEmpty {
29
+ switch method {
30
+ case "newCancel": return handles.hold(CancelToken())
31
+ case "defineTool":
32
+ let execute = RNToolExecute(try rnString(args, 1), callbacks, handles)
33
+ return handles.hold(try CoreToolDefinition(manifestJson: rnString(args, 0), execute: execute))
34
+ case "webSearch": return handles.hold(coreWebSearch())
35
+ case "open":
36
+ let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage)
37
+ return handles.hold(try platform.open(rnString(args, 0), registrations: registrations(args), recoveryJson: rnOptional(args, 4)))
38
+ case "wakeTimes":
39
+ let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage)
40
+ return try coreWakeTimes(openJson: rnString(args, 0), queryJson: rnOptional(args, 1), storage: storage, host: platform.host)
41
+ case "registerBackground":
42
+ let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage)
43
+ // Selected storage and OS-default marker storage are separate arguments;
44
+ // neither is inferred from the app JSON.
45
+ try platform.registerBackground(rnString(args, 0), sink: rnOptional(args, 1).map(eventSink), platformStorage: rnOptional(args, 4) == nil ? nil : platform.platformStorage)
46
+ return NSNull()
47
+ default: throw RNWireError.badPacket
48
+ }
49
+ }
50
+ switch method {
51
+ case "cancel":
52
+ if let token: CancelToken = try? handles.get(id) { token.cancel(); return NSNull() }
53
+ let agent: CoreAgent = try handles.get(id); try agent.cancel(inputId: rnOptional(args, 0)); return NSNull()
54
+ case "isCancelled": let token: CancelToken = try handles.get(id); return token.isCancelled()
55
+ case "name": let tool: CoreToolDefinition = try handles.get(id); return tool.name()
56
+ case "agent": let owner: CoreCroche = try handles.get(id); return handles.hold(try owner.agent(id: rnString(args, 0), scopeJson: rnOptional(args, 1)))
57
+ case "deleteData": let owner: CoreCroche = try handles.get(id); try owner.deleteData(scopeJson: rnOptional(args, 0)); return NSNull()
58
+ case "agentIds": let owner: CoreCroche = try handles.get(id); return try owner.agentIds()
59
+ case "deliverDue": let owner: CoreCroche = try handles.get(id); return try owner.deliverDue(optionsJson: rnOptional(args, 0), cancel: handles.get(rnString(args, 1)))
60
+ 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)))
61
+ case "wikiList": let owner: CoreCroche = try handles.get(id); return try owner.wikiList()
62
+ case "wikiPut": let owner: CoreCroche = try handles.get(id); try owner.wikiPut(docJson: rnString(args, 0)); return NSNull()
63
+ case "wikiRemove": let owner: CoreCroche = try handles.get(id); try owner.wikiRemove(path: rnString(args, 0)); return NSNull()
64
+ case "debugAttach": let owner: CoreCroche = try handles.get(id); return try owner.debugAttach(optionsJson: rnString(args, 0))
65
+ case "debugDetach": let owner: CoreCroche = try handles.get(id); try owner.debugDetach(); return NSNull()
66
+ case "id": let agent: CoreAgent = try handles.get(id); return agent.id()
67
+ case "instance": let agent: CoreAgent = try handles.get(id); return agent.instance() as Any? ?? NSNull()
68
+ case "send", "request":
69
+ let agent: CoreAgent = try handles.get(id), cancel: CancelToken = try handles.get(rnString(args, 2))
70
+ if method == "send" { return try agent.send(inputJson: rnString(args, 0), images: images, optionsJson: rnOptional(args, 1), cancel: cancel) }
71
+ return try agent.request(inputJson: rnString(args, 0), images: images, optionsJson: rnOptional(args, 1), cancel: cancel)
72
+ case "subscribe": let agent: CoreAgent = try handles.get(id); return handles.hold(try agent.subscribe(selectionJson: rnString(args, 0), eventSink: eventSink(rnString(args, 1))))
73
+ case "clearHistory": let agent: CoreAgent = try handles.get(id); try agent.clearHistory(); return NSNull()
74
+ case "memoryList": let agent: CoreAgent = try handles.get(id); return try agent.memoryList()
75
+ case "memoryDelete": let agent: CoreAgent = try handles.get(id); try agent.memoryDelete(id: rnString(args, 0)); return NSNull()
76
+ case "memoryClear": let agent: CoreAgent = try handles.get(id); try agent.memoryClear(); return NSNull()
77
+ case "memoryEnabled": let agent: CoreAgent = try handles.get(id); try agent.memoryEnabled(on: args[0] as! Bool); return NSNull()
78
+ case "setTools": let agent: CoreAgent = try handles.get(id); try agent.setTools(scopeJson: rnString(args, 0)); return NSNull()
79
+ case "resetTools": let agent: CoreAgent = try handles.get(id); try agent.resetTools(); return NSNull()
80
+ case "setWiki": let agent: CoreAgent = try handles.get(id); try agent.setWiki(scopeJson: rnString(args, 0)); return NSNull()
81
+ case "resetWiki": let agent: CoreAgent = try handles.get(id); try agent.resetWiki(); return NSNull()
82
+ case "history": let sub: CoreSubscription = try handles.get(id); try sub.history(optionsJson: rnString(args, 0)); return NSNull()
83
+ case "ended": let sub: CoreSubscription = try handles.get(id); return sub.ended()
84
+ case "shutdown":
85
+ if let owner: CoreCroche = try? handles.get(id) { try owner.shutdown() }
86
+ else if let agent: CoreAgent = try? handles.get(id) { try agent.shutdown() }
87
+ else { let sub: CoreSubscription = try handles.get(id); sub.shutdown() }
88
+ return NSNull()
89
+ default: throw RNWireError.badPacket
90
+ }
91
+ }
92
+ }
@@ -0,0 +1,111 @@
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 rnOptional(_ args: [Any], _ i: Int) -> String? { i < args.count ? args[i] as? String : nil }
16
+
17
+ final class RNCoreHandles: @unchecked Sendable {
18
+ private let lock = NSLock()
19
+ private var values: [String: Any] = [:]
20
+ func hold(_ value: Any) -> String {
21
+ lock.lock(); defer { lock.unlock() }
22
+ let id = UUID().uuidString; values[id] = value
23
+ return id
24
+ }
25
+ func get<T>(_ id: String, as: T.Type = T.self) throws -> T {
26
+ lock.lock(); defer { lock.unlock() }
27
+ guard let value = values[id] as? T else { throw RNWireError.missingHandle(id) }; return value
28
+ }
29
+ func forget(_ id: String) {
30
+ lock.lock()
31
+ var removed: [Any] = []
32
+ if let value = values.removeValue(forKey: id) { removed.append(value) }
33
+ lock.unlock()
34
+ // Destroy outside the map lock: a PAL destructor can post a retirement fact.
35
+ withExtendedLifetime(removed) {}
36
+ }
37
+ func clear() { lock.lock(); let previous = values; values = [:]; lock.unlock(); withExtendedLifetime(previous) {} }
38
+ }
39
+
40
+ // A request/reply latch, not an event queue or deadline policy. Core runs these
41
+ // callbacks off the JS thread; module teardown wakes every unreachable callback.
42
+ final class RNCoreCallbacks: @unchecked Sendable {
43
+ private let lock = NSCondition()
44
+ private var replies: [String: String] = [:]
45
+ private var alive = true
46
+ private var sender: (@Sendable (String) -> Void)?
47
+ var emit: (@Sendable (String) -> Void)? {
48
+ get { lock.lock(); defer { lock.unlock() }; return alive ? sender : nil }
49
+ set { lock.lock(); sender = newValue; lock.unlock() }
50
+ }
51
+ func sendJSON(_ json: String) throws {
52
+ guard let send = emit else { throw RNWireError.unavailable }; send(json)
53
+ }
54
+ func post(_ packet: [String: Any]) throws { try sendJSON(rnJSON(packet)) }
55
+ func invoke(_ packet: [String: Any]) throws -> String {
56
+ let id = UUID().uuidString
57
+ var packet = packet; packet["call"] = id
58
+ try post(packet)
59
+ lock.lock(); defer { lock.unlock() }
60
+ while alive && replies[id] == nil { lock.wait() }
61
+ guard let reply = replies.removeValue(forKey: id) else { throw RNWireError.unavailable }; return reply
62
+ }
63
+ func reply(_ id: String, _ json: String) { lock.lock(); if alive { replies[id] = json }; lock.broadcast(); lock.unlock() }
64
+ func invalidate() { lock.lock(); alive = false; sender = nil; replies = [:]; lock.broadcast(); lock.unlock() }
65
+ }
66
+
67
+ final class RNCallbackStorage: CoreStoragePal, @unchecked Sendable {
68
+ let id: String, host: String
69
+ let callbacks: RNCoreCallbacks
70
+ init(_ id: String, host: String, callbacks: RNCoreCallbacks) { self.id = id; self.host = host; self.callbacks = callbacks }
71
+ deinit { try? callbacks.post(["type":"retired", "storages":[id], "hosts":[host], "sinks":[], "executors":[], "tokens":[]]) }
72
+ private func call(_ method: String, _ args: [Any]) throws -> Any {
73
+ do {
74
+ let text = try callbacks.invoke(["type":"storage", "storage":id, "method":method, "args":args])
75
+ guard let result = try JSONSerialization.jsonObject(with: Data(text.utf8)) as? [String: Any] else { throw RNWireError.badPacket }
76
+ if let failure = result["error"] as? [String: Any] { throw CoreStorageFailure.Failure(detail: failure["detail"] as? String ?? "") }
77
+ guard let value = result["value"] else { throw RNWireError.unavailable }; return value
78
+ } catch let error as CoreStorageFailure { throw error }
79
+ catch { throw CoreStorageFailure.Failure(detail: String(describing: error)) }
80
+ }
81
+ func read(key: String) throws -> String? { try call("read", [key]) as? String }
82
+ func write(key: String, value: String) throws {
83
+ _ = try call("write", [key, value])
84
+ }
85
+ func remove(key: String) throws { _ = try call("remove", [key]) }
86
+ 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 }
87
+ func lock(name: String) throws -> PalLockOutcome {
88
+ switch try call("lock", [name]) as? String { case "acquired": return .acquired; case "held": return .held; default: throw CoreStorageFailure.Failure(detail: "Invalid lock transport reply") }
89
+ }
90
+ func unlock(name: String) throws { _ = try call("unlock", [name]) }
91
+ func readSecret(key: String) throws -> String? { try call("readSecret", [key]) as? String }
92
+ func writeSecret(key: String, value: String) throws { _ = try call("writeSecret", [key, value]) }
93
+ func removeSecret(key: String) throws { _ = try call("removeSecret", [key]) }
94
+ }
95
+
96
+ final class RNCancelNotice: CancelCallback, @unchecked Sendable {
97
+ let id: String; let callbacks: RNCoreCallbacks
98
+ init(_ id: String, _ callbacks: RNCoreCallbacks) { self.id = id; self.callbacks = callbacks }
99
+ func cancelled() { try? callbacks.post(["type":"cancel", "token":id]) }
100
+ }
101
+ final class RNToolExecute: CoreToolExecute, @unchecked Sendable {
102
+ let id: String; let callbacks: RNCoreCallbacks; let handles: RNCoreHandles
103
+ init(_ id: String, _ callbacks: RNCoreCallbacks, _ handles: RNCoreHandles) { self.id = id; self.callbacks = callbacks; self.handles = handles }
104
+ deinit { try? callbacks.post(["type":"retired", "executors":[id], "hosts":[], "storages":[], "sinks":[], "tokens":[]]) }
105
+ func execute(callJson: String, contextJson: String, cancel: CancelToken) throws -> String {
106
+ let token = handles.hold(cancel); cancel.onCancel(callback: RNCancelNotice(token, callbacks))
107
+ defer { handles.forget(token) }
108
+ do { return try callbacks.invoke(["type":"execute", "execute":id, "callJson":callJson, "contextJson":contextJson, "cancel":token]) }
109
+ catch { return #"{"type":"unavailable"}"# }
110
+ }
111
+ }