@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.
- package/LICENSE +1 -1
- package/README.md +90 -451
- package/THIRD-PARTY-NOTICES +906 -14
- package/android/build.gradle +6 -1
- package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +126 -0
- package/android/src/main/AndroidManifest.xml +0 -5
- package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +106 -0
- package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +217 -0
- package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +48 -824
- package/android/src/main/java/dev/croche/reactnative/NativePal.kt +68 -0
- package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
- package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +64 -41
- package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +64 -0
- package/android/src/test/java/dev/croche/reactnative/BlobFilesTest.kt +107 -0
- package/android/src/test/java/dev/croche/reactnative/ImageAdapterWireTest.kt +70 -0
- package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +44 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +19 -32
- package/android/src/test/java/dev/croche/reactnative/ProjectHandlerStagesTest.kt +32 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectRecoveryTest.kt +12 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectTimeZoneTest.kt +11 -15
- package/android/src/test/java/dev/croche/reactnative/StorageVersionWireTest.kt +37 -0
- package/android/src/test/java/dev/croche/reactnative/VerificationDispatchTest.kt +24 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +10 -8
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +55 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageCodec.kt +1 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageInput.kt +15 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapOrientation.kt +40 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapPalCodec.kt +66 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidAssetPal.kt +30 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreFactory.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +116 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt +17 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +17 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidS2Pal.kt +14 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStateStore.kt +216 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +108 -0
- package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +8 -2
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Adapters.kt +75 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +31 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +25 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +121 -103
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +89 -103
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +20 -360
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +136 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Decision.kt +170 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +23 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +26 -21
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/EncodedImageCodec.kt +51 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +715 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +9 -322
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +16 -28
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeEvent.kt +59 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeHandleOwner.kt +118 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +10 -74
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +294 -37
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +127 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +73 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StateStore.kt +90 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +88 -24
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +5723 -3088
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +96 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundOsRegistration.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundSink.kt +37 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreDefinitionCalls.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +30 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +41 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +127 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ImageAdapterHost.kt +28 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +70 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStateStore.kt +125 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +64 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativeCoreInitialization.kt +21 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +60 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PalCallbacks.kt +27 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PlainToolJson.kt +56 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +43 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StateStoreContract.kt +103 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StorageVersionContract.kt +40 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +100 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/BlobFiles.kt +157 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/InstancePayloadPurge.kt +79 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/VersionedKeyFiles.kt +71 -0
- package/android/vendor/jniLibs/arm64-v8a/libcroche_ffi.so +0 -0
- package/android/vendor/jniLibs/armeabi-v7a/libcroche_ffi.so +0 -0
- package/android/vendor/jniLibs/x86_64/libcroche_ffi.so +0 -0
- package/croche-react-native.podspec +1 -0
- package/dist/package.json +1 -0
- package/dist/react-native/src/NativeCrocheRuntime.d.ts +16 -0
- package/dist/react-native/src/NativeCrocheRuntime.js +2 -0
- package/dist/react-native/src/core-native.d.ts +6 -0
- package/dist/react-native/src/core-native.js +316 -0
- package/dist/react-native/src/core-wire.d.ts +55 -0
- package/dist/react-native/src/core-wire.js +1 -0
- package/dist/react-native/src/native-module.d.ts +8 -0
- package/dist/react-native/src/native-module.js +53 -0
- package/dist/rn-shared-build.json +16 -0
- package/dist/runtime/src/adapters.d.ts +8 -0
- package/dist/runtime/src/adapters.js +29 -0
- package/dist/runtime/src/agent.d.ts +91 -0
- package/dist/runtime/src/agent.js +137 -0
- package/dist/runtime/src/app-api.d.ts +572 -0
- package/dist/runtime/src/app-api.js +1 -0
- package/dist/runtime/src/cancel.d.ts +9 -0
- package/dist/runtime/src/cancel.js +27 -0
- package/dist/runtime/src/core/foreground.d.ts +3 -0
- package/dist/runtime/src/core/foreground.js +40 -0
- package/dist/runtime/src/core/port.d.ts +247 -0
- package/dist/runtime/src/core/port.js +1 -0
- package/dist/runtime/src/core/wasm-owner.d.ts +32 -0
- package/dist/runtime/src/core/wasm-owner.js +237 -0
- package/dist/runtime/src/core/wasm.d.ts +1 -0
- package/dist/runtime/src/core/wasm.js +2 -0
- package/dist/runtime/src/croche.d.ts +97 -0
- package/dist/runtime/src/croche.js +172 -0
- package/dist/runtime/src/errors.d.ts +33 -0
- package/dist/runtime/src/errors.js +63 -0
- package/dist/runtime/src/events.d.ts +5 -0
- package/dist/runtime/src/events.js +31 -0
- package/dist/runtime/src/generated/app-stream.d.ts +328 -0
- package/dist/runtime/src/generated/app-stream.js +127 -0
- package/dist/runtime/src/image-input.d.ts +5 -0
- package/dist/runtime/src/image-input.js +60 -0
- package/dist/runtime/src/index.d.ts +6 -0
- package/dist/runtime/src/index.js +5 -0
- package/dist/runtime/src/index.native.d.ts +1 -0
- package/dist/runtime/src/index.native.js +2 -0
- package/dist/runtime/src/json.d.ts +13 -0
- package/dist/runtime/src/json.js +120 -0
- package/dist/runtime/src/native-adapter.d.ts +3 -0
- package/dist/runtime/src/native-adapter.js +30 -0
- package/dist/runtime/src/pal/host.d.ts +5 -0
- package/dist/runtime/src/pal/host.js +60 -0
- package/dist/runtime/src/pal/platform.d.ts +21 -0
- package/dist/runtime/src/pal/platform.js +7 -0
- package/dist/runtime/src/pal/state-store-lock.d.ts +8 -0
- package/dist/runtime/src/pal/state-store-lock.js +2 -0
- package/dist/runtime/src/pal/state-store.d.ts +66 -0
- package/dist/runtime/src/pal/state-store.js +1 -0
- package/dist/runtime/src/pal/storage-version-contract.d.ts +19 -0
- package/dist/runtime/src/pal/storage-version-contract.js +68 -0
- package/dist/runtime/src/pal/transactional-storage.d.ts +3 -0
- package/dist/runtime/src/pal/transactional-storage.js +124 -0
- package/dist/runtime/src/rn-entry.d.ts +1 -0
- package/dist/runtime/src/rn-entry.js +4 -0
- package/dist/runtime/src/run-tool.d.ts +33 -0
- package/dist/runtime/src/run-tool.js +7 -0
- package/dist/runtime/src/tools.d.ts +35 -0
- package/dist/runtime/src/tools.js +94 -0
- package/ios/CoreAgentTransport.swift +14 -0
- package/ios/CoreDeliveryTransport.swift +58 -0
- package/ios/CoreNativePal.swift +98 -0
- package/ios/CorePlatformTransport.swift +44 -0
- package/ios/CoreWireDispatch.swift +122 -0
- package/ios/CoreWireSupport.swift +286 -0
- package/ios/CrocheRuntimeBridge.swift +34 -994
- package/ios/ProjectPal.swift +49 -0
- package/ios/RNCrocheRuntime.mm +18 -201
- package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
- package/ios/vendor/CrocheRuntimeSDK/BlobFiles.swift +124 -0
- package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +60 -0
- package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
- package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
- package/ios/vendor/CrocheRuntimeSDK/EncodedImages.swift +85 -0
- package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +26 -157
- package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +672 -0
- package/ios/vendor/CrocheRuntimeSDK/Host.swift +34 -371
- package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
- package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
- package/ios/vendor/CrocheRuntimeSDK/InstancePayloadPurge.swift +88 -0
- package/ios/vendor/CrocheRuntimeSDK/JSON.swift +30 -14
- package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
- package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +1 -155
- package/ios/vendor/CrocheRuntimeSDK/Platform.swift +33 -59
- package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +289 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +302 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformStateStore.swift +253 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +281 -0
- package/ios/vendor/CrocheRuntimeSDK/Project.swift +4 -269
- package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +24 -461
- package/ios/vendor/CrocheRuntimeSDK/RuntimeHTTPAdapter.swift +42 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Adapters.swift +110 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +248 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Allowance.swift +20 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +170 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +313 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Decision.swift +146 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +37 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1HandleOwner.swift +58 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +346 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +89 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +165 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +100 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +278 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Verification.swift +27 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/StateStoreContract.swift +189 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/StorageVersionContract.swift +68 -0
- package/ios/vendor/CrocheRuntimeSDK/Storage.swift +23 -269
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +163 -116
- package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
- package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +4712 -2469
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +899 -608
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/libcroche_ffi.a +0 -0
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/Headers/croche_ffiFFI.h +899 -608
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +0 -0
- package/package.json +18 -8
- package/scripts/README.md +11 -0
- package/scripts/build-shared.mjs +46 -0
- package/scripts/build.mjs +11 -0
- package/scripts/check-swift-vendor-modules.sh +15 -0
- package/scripts/vendor-native.sh +8 -6
- package/src/NativeCrocheRuntime.ts +12 -159
- package/android/src/main/java/dev/croche/reactnative/AgentMemory.kt +0 -25
- package/android/src/main/java/dev/croche/reactnative/ProjectAttestation.kt +0 -17
- package/android/src/main/java/dev/croche/reactnative/ProjectTimeZone.kt +0 -14
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/CrocheProjectRecordProvider.kt +0 -32
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/WorkManagerScheduler.kt +0 -146
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +0 -110
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Control.kt +0 -178
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +0 -173
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +0 -25
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +0 -87
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +0 -181
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +0 -227
- package/ios/vendor/CrocheRuntimeSDK/Background.swift +0 -59
- package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
- package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +0 -453
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +0 -38
- package/src/index.ts +0 -664
- package/src/project-types.ts +0 -35
- package/src/project.ts +0 -434
- package/src/protocol.ts +0 -28
- package/src/types.ts +0 -9
|
@@ -1,143 +1,9 @@
|
|
|
1
1
|
import Foundation
|
|
2
2
|
|
|
3
|
-
//
|
|
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
|
|
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
|
|
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
|
|
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 ->
|
|
44
|
+
static func decodeFailure(_ text: String) throws -> LegacyStreamError {
|
|
179
45
|
let value = try decoder().decode(Failure.self, from: Data(text.utf8))
|
|
180
|
-
return
|
|
46
|
+
return LegacyStreamError(code: value.code, message: value.message, poisoned: value.poisoned ?? false)
|
|
181
47
|
}
|
|
182
48
|
private struct Event: Decodable {
|
|
183
|
-
let value:
|
|
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: [
|
|
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(
|
|
193
|
-
case "message.start": value = .messageStart(try
|
|
194
|
-
case "message.delta": value = .messageDelta(try
|
|
195
|
-
case "message.end": value = .messageEnd(try
|
|
196
|
-
case "state": value = .state(try
|
|
197
|
-
case "snapshot": value = .snapshot(try
|
|
198
|
-
case "gap": value = .gap(try
|
|
199
|
-
case "app": value = .app(try
|
|
200
|
-
case "turn": value = .turn(try
|
|
201
|
-
case "cleared": value = .cleared(try
|
|
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
|
|
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(
|
|
72
|
+
value = .streamError(LegacyStreamError(code: e.code, message: e.message, poisoned: e.poisoned ?? false))
|
|
207
73
|
}
|
|
208
|
-
case "page": value = .page(
|
|
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:
|
|
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 ->
|
|
83
|
+
static func decode(_ text: String) throws -> LegacyRuntimeEvent { try decodeValue(Event.self, text).value }
|
|
215
84
|
}
|