@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
@@ -1,143 +1,9 @@
1
1
  import Foundation
2
2
 
3
- // The only stream adapter to UniFFI. Core owns projection, ordering and capture.
4
- // Keep the receipt method spelling in this one file/call site for the reviewed rename.
5
- final class FFIStreamPort: CoreStreamPort, @unchecked Sendable {
6
- private let core: CoreRuntime
7
- let logId: String
8
- init(core: CoreRuntime) throws {
9
- self.core = core
10
- logId = try Self.blocking { try core.logId() }
11
- }
12
- private final class Box<T>: @unchecked Sendable { var result: Result<T, Error>? }
13
- // select/observe are synchronous seams. Main-thread callers wait on a dedicated thread;
14
- // callbacks already on a Core thread stay there, preserving FFI's reentrancy rule.
15
- private static func blocking<T>(record: Bool = true, _ body: @escaping @Sendable () throws -> T) throws -> T {
16
- let measured: @Sendable () throws -> T = {
17
- if record { CrocheRuntime.offloadStarting?() }
18
- return try body()
19
- }
20
- if !Thread.isMainThread { return try measured() }
21
- let done = DispatchSemaphore(value: 0), box = Box<T>()
22
- Thread.detachNewThread { box.result = Result { try measured() }; done.signal() }
23
- done.wait()
24
- return try box.result!.get()
25
- }
26
- private static func offload<T: Sendable>(_ body: @escaping @Sendable () throws -> T) async throws -> T {
27
- try await CrocheRuntime.offload(body)
28
- }
29
- private final class Listener: EventListener, @unchecked Sendable {
30
- let callback: @Sendable (String) -> Void
31
- init(_ callback: @escaping @Sendable (String) -> Void) { self.callback = callback }
32
- func onEvent(event: String) { callback(event) }
33
- }
34
- private static func failure(_ error: Error) -> StreamError {
35
- let error = CrocheError(error)
36
- return StreamError(code: error.code, message: error.message, poisoned: false)
37
- }
38
- func observe(_ event: @escaping @Sendable (RequestObservation) -> Void,
39
- end: @escaping @Sendable (StreamError?) -> Void) throws -> @Sendable () -> Void {
40
- do {
41
- try Self.blocking { [core] in
42
- try core.observe(onEvent: Listener { text in
43
- do {
44
- let decoded = try CoreEvent.decode(text)
45
- switch decoded {
46
- case .input(let v): event(.input(InputObservation(id: v.id, seq: v.seq, status: v.status, carried: v.status == .seen)))
47
- case .messageEnd(let v): event(.messageEnd(v))
48
- case .state(let v): event(.state(v))
49
- default: break
50
- }
51
- } catch { end(Self.failure(error)) }
52
- }, onEnd: Listener { text in
53
- do { end(try CoreEvent.decodeFailure(text)) }
54
- catch { end(Self.failure(error)) }
55
- })
56
- }
57
- } catch { throw CrocheError(error) }
58
- return { [core] in _ = try? Self.blocking { core.unobserve() } }
59
- }
60
- func select(_ selection: StreamSelection, event: @escaping @Sendable (RuntimeEvent) -> Void) -> CoreProjection {
61
- struct Selection: Encodable { let initialMessages: Int?; let after: Cursor? }
62
- do {
63
- if let after = selection.after { _ = try Self.coreSequence(after.seq) }
64
- let text = String(decoding: try JSONEncoder().encode(Selection(initialMessages: selection.initialMessages, after: selection.after)), as: UTF8.self)
65
- let id = try Self.blocking { [core] in
66
- try core.select(selection: text, onEvent: Listener { text in
67
- do { event(try CoreEvent.decode(text)) }
68
- catch { event(.streamError(Self.failure(error))) }
69
- })
70
- }
71
- return CoreProjection(close: { [core] in _ = try? Self.blocking { core.closeProjection(id: id) } }, history: { before, limit in
72
- guard let count = UInt32(exactly: limit) else { throw CrocheError("invalid_configuration", "Invalid history range") }
73
- let seq = try before.map(Self.coreSequence)
74
- try await Self.offload { try self.historyNow(id: id, before: seq, limit: count) }
75
- })
76
- } catch {
77
- event(.streamError(Self.failure(error)))
78
- return CoreProjection(close: {}, history: { _, _ in throw CrocheError("invalid_configuration", "No stream is attached") })
79
- }
80
- }
81
- // Also used at the synchronous callback boundary to retain FFI's reentrancy rule.
82
- func historyNow(id: UInt64, before: UInt64?, limit: UInt32) throws {
83
- try Self.blocking { [core] in try core.history(id: id, before: before, limit: limit) }
84
- }
85
- func send(_ input: JSON, options: SendOptions) async throws -> Receipt {
86
- let token = CancelToken()
87
- return try await withTaskCancellationHandler {
88
- try await send(input, options: options, cancel: token)
89
- } onCancel: { token.cancel() }
90
- }
91
- func send(_ input: JSON, options: SendOptions, cancel: CancelToken?) async throws -> Receipt {
92
- let text = String(decoding: try input.encoded(), as: UTF8.self)
93
- let format = try options.responseFormat.map { String(decoding: try $0.encoded(), as: UTF8.self) }
94
- let value = try await Self.offload { [core] in try core.send(input: text, cancel: cancel, responseFormat: format) }
95
- struct WireReceipt: Decodable { let id: String; let status: InputStatus; let seq: Int? }
96
- let receipt = try CoreEvent.decodeValue(WireReceipt.self, value)
97
- return Receipt(id: receipt.id, status: receipt.status, seq: receipt.seq)
98
- }
99
- func readOutcome(id: String, fromSeq: Int, captured: @escaping @Sendable (CapturedRequestState) -> Void) async throws -> RetainedRequestEnds {
100
- let seq = try Self.coreSequence(fromSeq)
101
- // Capture parsing failures must fail the read, never silently strand a waiter.
102
- let captureError = CaptureError()
103
- let text = try await Self.offload { [core] in
104
- try core.readOutcome(id: id, fromSeq: seq, captured: Listener { value in
105
- do {
106
- struct Capture: Decodable { let head: Int; let floor: Int; let status: InputStatus; let idle: Bool; let consumedBy: String? }
107
- let at = try CoreEvent.decodeValue(Capture.self, value)
108
- captured(CapturedRequestState(head: at.head, floor: at.floor, status: at.status, idle: at.idle, carried: at.consumedBy != nil, unitEnd: nil))
109
- } catch { captureError.set(error) }
110
- })
111
- }
112
- if let error = captureError.get() { throw error }
113
- struct Outcome: Decodable { let messages: [MessageEnd]; let gap: Bool? }
114
- let outcome = try CoreEvent.decodeValue(Outcome.self, text)
115
- return RetainedRequestEnds(messages: outcome.messages, gap: outcome.gap ?? false)
116
- }
117
- private final class CaptureError: @unchecked Sendable {
118
- private let lock = NSLock(); private var error: Error?
119
- func set(_ value: Error) { lock.withLock { error = value } }
120
- func get() -> Error? { lock.withLock { error } }
121
- }
122
- static func coreSequence(_ value: Int) throws -> UInt64 {
123
- guard let result = UInt64(exactly: value) else { throw CrocheError("invalid_input", "Sequence is outside the Core range") }
124
- return result
125
- }
126
- func cancel(_ id: String) async throws {
127
- try await Self.offload { [core] in try core.cancel(id: id) }
128
- }
129
- func clearHistory() async throws {
130
- // Preserve the existing admission fence: only waiting crosses the offload hop.
131
- let pending = try Self.blocking(record: false) { [core] in core.beginClearHistory() }
132
- try await Self.offload { try pending.finish() }
133
- }
134
- }
135
-
136
- // Decode numbers from the original JSON bytes, never through the legacy Double JSON tree.
137
- // Extra Core keys stay internal; unknown event types remain forward-compatible.
3
+ // Pure legacy-event reader retained for existing stream-kernel/reader witnesses.
138
4
  enum CoreEvent {
139
5
  static func publicSequence(_ value: UInt64) throws -> Int {
140
- guard let result = Int(exactly: value) else { throw CrocheError("invalid_input", "Core sequence is outside the public range") }
6
+ guard let result = Int(exactly: value) else { throw LegacyCrocheError("invalid_input", "Core sequence is outside the public range") }
141
7
  return result
142
8
  }
143
9
  // Validate only Core envelope fields, never arbitrary app data/output or usage values.
@@ -149,7 +15,7 @@ enum CoreEvent {
149
15
  if keys.contains(key), !(try keys.decodeNil(forKey: key)) {
150
16
  let value: UInt64
151
17
  do { value = try keys.decode(UInt64.self, forKey: key) }
152
- catch { throw CrocheError("invalid_input", "Invalid Core sequence") }
18
+ catch { throw LegacyCrocheError("invalid_input", "Invalid Core sequence") }
153
19
  _ = try CoreEvent.publicSequence(value)
154
20
  }
155
21
  }
@@ -168,48 +34,51 @@ enum CoreEvent {
168
34
  let text = try value.singleValueContainer().decode(String.self)
169
35
  let fractional = ISO8601DateFormatter(); fractional.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
170
36
  guard let date = fractional.date(from: text) ?? ISO8601DateFormatter().date(from: text) else {
171
- throw CrocheError("invalid_input", "Invalid Core event date")
37
+ throw LegacyCrocheError("invalid_input", "Invalid Core event date")
172
38
  }
173
39
  return date
174
40
  }
175
41
  return decoder
176
42
  }
177
43
  private struct Failure: Decodable { let code: String; let message: String; let poisoned: Bool? }
178
- static func decodeFailure(_ text: String) throws -> StreamError {
44
+ static func decodeFailure(_ text: String) throws -> LegacyStreamError {
179
45
  let value = try decoder().decode(Failure.self, from: Data(text.utf8))
180
- return StreamError(code: value.code, message: value.message, poisoned: value.poisoned ?? false)
46
+ return LegacyStreamError(code: value.code, message: value.message, poisoned: value.poisoned ?? false)
181
47
  }
182
48
  private struct Event: Decodable {
183
- let value: RuntimeEvent
49
+ let value: LegacyRuntimeEvent
184
50
  enum Keys: String, CodingKey { case type, seq, images, messages, reachedFloor, logId, clearedSeq }
185
51
  init(from decoder: Decoder) throws {
186
52
  let keys = try decoder.container(keyedBy: Keys.self)
187
53
  let type = try keys.decode(String.self, forKey: .type)
188
54
  switch type {
189
55
  case "input":
190
- struct Input: Decodable { let seq: Int; let firstSeq: Int; let id: String; let kind: InputKind; let text: String?; let images: [ImageSummary]?; let status: InputStatus; let error: RuntimeError?; let at: Date }
56
+ struct Input: Decodable { let seq: Int; let firstSeq: Int; let id: String; let kind: InputKind; let text: String?; let images: [LegacyImageSummary]?; let status: LegacyInputStatus; let error: LegacyRuntimeError?; let at: Date }
191
57
  let v = try Input(from: decoder)
192
- value = .input(InputEvent(seq: v.seq, firstSeq: v.firstSeq, id: v.id, kind: v.kind, text: v.text, images: v.images ?? [], status: v.status, error: v.error, at: v.at))
193
- case "message.start": value = .messageStart(try MessageStart(from: decoder))
194
- case "message.delta": value = .messageDelta(try MessageDelta(from: decoder))
195
- case "message.end": value = .messageEnd(try MessageEnd(from: decoder))
196
- case "state": value = .state(try StateEvent(from: decoder))
197
- case "snapshot": value = .snapshot(try Snapshot(from: decoder))
198
- case "gap": value = .gap(try Gap(from: decoder))
199
- case "app": value = .app(try AppStreamEvent(from: decoder))
200
- case "turn": value = .turn(try TurnEvent(from: decoder))
201
- case "cleared": value = .cleared(try Cleared(from: decoder))
58
+ value = .input(LegacyInputEvent(seq: v.seq, firstSeq: v.firstSeq, id: v.id, kind: v.kind, text: v.text, images: v.images ?? [], status: v.status, error: v.error, at: v.at))
59
+ case "message.start": value = .messageStart(try LegacyMessageStart(from: decoder))
60
+ case "message.delta": value = .messageDelta(try LegacyMessageDelta(from: decoder))
61
+ case "message.end": value = .messageEnd(try LegacyMessageEnd(from: decoder))
62
+ case "state": value = .state(try LegacyStateEvent(from: decoder))
63
+ case "snapshot": value = .snapshot(try LegacySnapshot(from: decoder))
64
+ case "gap": value = .gap(try LegacyGap(from: decoder))
65
+ case "app": value = .app(try LegacyAppStreamEvent(from: decoder))
66
+ case "turn": value = .turn(try LegacyTurnEvent(from: decoder))
67
+ case "cleared": value = .cleared(try LegacyCleared(from: decoder))
202
68
  case "error":
203
- if keys.contains(.seq) { value = .error(try ErrorEvent(from: decoder)) }
69
+ if keys.contains(.seq) { value = .error(try LegacyErrorEvent(from: decoder)) }
204
70
  else {
205
71
  let e = try Failure(from: decoder)
206
- value = .streamError(StreamError(code: e.code, message: e.message, poisoned: e.poisoned ?? false))
72
+ value = .streamError(LegacyStreamError(code: e.code, message: e.message, poisoned: e.poisoned ?? false))
207
73
  }
208
- case "page": value = .page(Page(messages: try keys.decode([Event].self, forKey: .messages).map(\.value), reachedFloor: try keys.decode(Bool.self, forKey: .reachedFloor), logId: try keys.decode(String.self, forKey: .logId), clearedSeq: try keys.decode(Int.self, forKey: .clearedSeq)))
74
+ case "page": value = .page(LegacyPage(messages: try keys.decode([Event].self, forKey: .messages).map(\.value), reachedFloor: try keys.decode(Bool.self, forKey: .reachedFloor), logId: try keys.decode(String.self, forKey: .logId), clearedSeq: try keys.decode(Int.self, forKey: .clearedSeq)))
209
75
  case "replaced": value = .replaced
210
- default: value = .unknown(type: type)
76
+ default:
77
+ var fields = try JSON(from: decoder).object ?? [:]
78
+ fields.removeValue(forKey: "type")
79
+ value = .unknown(type: type, fields: .object(fields))
211
80
  }
212
81
  }
213
82
  }
214
- static func decode(_ text: String) throws -> RuntimeEvent { try decodeValue(Event.self, text).value }
83
+ static func decode(_ text: String) throws -> LegacyRuntimeEvent { try decodeValue(Event.self, text).value }
215
84
  }