@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
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonParser
|
|
4
|
+
import dev.croche.AppStreamContract
|
|
5
|
+
import dev.croche.ffi.CoreDelivery
|
|
6
|
+
import dev.croche.ffi.CoreEventSink
|
|
7
|
+
import kotlinx.coroutines.CoroutineDispatcher
|
|
8
|
+
import kotlinx.coroutines.runBlocking
|
|
9
|
+
|
|
10
|
+
/** A Core event the generated reader cannot read (e.g. an error category outside the closed set).
|
|
11
|
+
* It is an SDK/Core fault, not the app's: [appEventSink] throws it from `deliver` BEFORE entry, so
|
|
12
|
+
* the handler never runs and is never blamed (no `threw` / `handler_failed`). The sink has no error
|
|
13
|
+
* codes (Atlas's PAL error contract): UniFFI hands this untyped exception to Core as an unexpected
|
|
14
|
+
* callback error, and Core completes the delivery `unavailable`, ending only this subscription.
|
|
15
|
+
* Swift's `MalformedEvent` is the same rule. */
|
|
16
|
+
internal class MalformedEvent(cause: Throwable) : RuntimeException("Core's event is not an AgentEvent: ${cause.message}", cause)
|
|
17
|
+
|
|
18
|
+
/** Core owns the serial queue. Decode on Core's stack, dispatch to the platform UI thread, enter
|
|
19
|
+
* immediately before invoking the handler, and complete exactly once after the suspend handler settles. */
|
|
20
|
+
internal fun appEventSink(dispatcher: CoroutineDispatcher, handler: suspend (AppStreamContract.AgentEvent) -> Unit): CoreEventSink =
|
|
21
|
+
object : CoreEventSink {
|
|
22
|
+
override fun deliver(deliveryId: String, eventJson: String, delivery: CoreDelivery) {
|
|
23
|
+
val event = try { AppStreamContract.decode(JsonParser.parseString(eventJson).asJsonObject) }
|
|
24
|
+
catch (error: Exception) { throw MalformedEvent(error) }
|
|
25
|
+
runBlocking(dispatcher) {
|
|
26
|
+
if (!delivery.tryEnter()) return@runBlocking
|
|
27
|
+
val outcome = try {
|
|
28
|
+
handler(event)
|
|
29
|
+
"""{"outcome":"returned"}"""
|
|
30
|
+
} catch (_: Throwable) { """{"outcome":"threw"}""" }
|
|
31
|
+
delivery.complete(outcome)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.StateListOptions
|
|
4
|
+
import dev.croche.StateStore
|
|
5
|
+
import dev.croche.VersionedValue
|
|
6
|
+
import dev.croche.ffi.*
|
|
7
|
+
import kotlinx.coroutines.Dispatchers
|
|
8
|
+
import kotlinx.coroutines.runBlocking
|
|
9
|
+
|
|
10
|
+
internal interface LocalStoragePal : CoreStoragePal {
|
|
11
|
+
fun purgeStorage(
|
|
12
|
+
prefix: String, localState: Boolean, localSecret: Boolean,
|
|
13
|
+
deleteSecret: () -> Unit = {}, deleteState: () -> Unit = {},
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Core invokes these on its effect worker. The wrapper bridges suspend app I/O. */
|
|
18
|
+
internal fun appStorage(platform: LocalStoragePal, state: StateStore): CoreStoragePal =
|
|
19
|
+
object : CoreStoragePal by platform {
|
|
20
|
+
private fun <T> call(block: suspend () -> T): T = storageCallback { runBlocking(Dispatchers.IO) { block() } }
|
|
21
|
+
|
|
22
|
+
override fun commitState(ops: List<CoreStateOp>, condition: CoreStateCondition): CoreStateCommit {
|
|
23
|
+
return call {
|
|
24
|
+
val batch = ops.map { it.toApp() }
|
|
25
|
+
val guard = condition.toApp()
|
|
26
|
+
validateStateBatch(batch, guard)
|
|
27
|
+
state.commit(batch, guard).toCore()
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
override fun listState(prefix: String, options: CoreStateListOptions): CoreStatePage {
|
|
32
|
+
return call {
|
|
33
|
+
val range = options.toApp()
|
|
34
|
+
StateRange(prefix, StateListOptions(range.start, range.reverse, range.limit))
|
|
35
|
+
state.list(prefix, range).toCore()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
override fun readVersioned(key: String): CoreVersionedValue {
|
|
40
|
+
return call {
|
|
41
|
+
val row = state.get(stateKey(key))
|
|
42
|
+
VersionedValue(row?.value, row?.version).toCore()
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
override fun writeIf(key: String, value: String?, expect: String?): CoreWriteOutcome {
|
|
47
|
+
val op = if (value == null) CoreStateOp.Delete(key) else CoreStateOp.Put(key, value)
|
|
48
|
+
return when (val fact = commitState(listOf(op), CoreStateCondition(key, expect))) {
|
|
49
|
+
is CoreStateCommit.Committed -> CoreWriteOutcome.Written(fact.version)
|
|
50
|
+
CoreStateCommit.Conflict -> CoreWriteOutcome.Conflict(null)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
override fun read(key: String): String? = readVersioned(key).value
|
|
55
|
+
|
|
56
|
+
private fun modify(key: String, value: String?) {
|
|
57
|
+
val version = readVersioned(key).version
|
|
58
|
+
if (writeIf(key, value, version) !is CoreWriteOutcome.Written) {
|
|
59
|
+
throw CoreStorageFailure.Failure("StateStore key changed before commit")
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
override fun write(key: String, value: String) {
|
|
64
|
+
modify(key, value)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
override fun remove(key: String) {
|
|
68
|
+
modify(key, null)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
override fun list(prefix: String): List<String> {
|
|
72
|
+
return buildList {
|
|
73
|
+
val seen = mutableSetOf<String>()
|
|
74
|
+
var cursor: String? = null
|
|
75
|
+
do {
|
|
76
|
+
val page = listState(prefix, CoreStateListOptions(null, false, null, cursor))
|
|
77
|
+
for (row in page.entries) {
|
|
78
|
+
if (!row.key.startsWith(prefix) || lastOrNull()?.let { it >= row.key } == true) {
|
|
79
|
+
throw CoreStorageFailure.Failure("StateStore returned invalid list order or scope")
|
|
80
|
+
}
|
|
81
|
+
add(row.key)
|
|
82
|
+
}
|
|
83
|
+
cursor = page.nextCursor
|
|
84
|
+
if (cursor != null && !seen.add(cursor)) {
|
|
85
|
+
throw CoreStorageFailure.Failure("StateStore cursor did not advance")
|
|
86
|
+
}
|
|
87
|
+
} while (cursor != null)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
override fun purgeInstance(prefix: String, secretKey: String?) {
|
|
92
|
+
platform.purgeStorage(prefix, localState = false, localSecret = true, deleteState = {
|
|
93
|
+
for (key in list(prefix)) modify(key, null)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ffi.CoreBackgroundWake
|
|
4
|
+
import dev.croche.ffi.CorePalException
|
|
5
|
+
|
|
6
|
+
/** Only the platform install/replace acknowledgement; Core owns retry and wake policy. */
|
|
7
|
+
internal class BackgroundOsRegistration(private val enqueue: (String, Boolean, Long) -> Unit) {
|
|
8
|
+
private var identifier: String? = null
|
|
9
|
+
@Volatile private var callback: CoreBackgroundWake? = null
|
|
10
|
+
|
|
11
|
+
@Synchronized fun install(name: String, wake: CoreBackgroundWake) {
|
|
12
|
+
val previous = callback
|
|
13
|
+
callback = wake // The Core object holds a WEAK service reference, not a Croche.
|
|
14
|
+
try {
|
|
15
|
+
enqueue(name, true, 0)
|
|
16
|
+
identifier = name
|
|
17
|
+
} catch (error: Exception) {
|
|
18
|
+
callback = previous
|
|
19
|
+
if (error is InterruptedException) Thread.currentThread().interrupt()
|
|
20
|
+
throw CorePalException.Failure("host_unavailable", "OS background registration failed")
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@Synchronized fun schedule(earliest: Long?, now: Long) {
|
|
25
|
+
val name = identifier ?: return
|
|
26
|
+
if (earliest == null) return
|
|
27
|
+
try { enqueue(name, false, if (earliest <= now) 0 else earliest - now) }
|
|
28
|
+
catch (error: Exception) {
|
|
29
|
+
if (error is InterruptedException) Thread.currentThread().interrupt()
|
|
30
|
+
throw CorePalException.Failure("host_unavailable", "OS background scheduling failed")
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fun wake(): CoreBackgroundWake? = callback
|
|
35
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonParser
|
|
4
|
+
import dev.croche.BackgroundEvent
|
|
5
|
+
import dev.croche.ffi.CoreEventSink
|
|
6
|
+
import dev.croche.ffi.CoreDelivery
|
|
7
|
+
import kotlinx.coroutines.CoroutineDispatcher
|
|
8
|
+
import kotlinx.coroutines.runBlocking
|
|
9
|
+
|
|
10
|
+
/** Core owns event policy. Unknown object fields/types survive; malformed Core data is dropped. */
|
|
11
|
+
internal fun backgroundEvent(json: String): BackgroundEvent? {
|
|
12
|
+
val value = try { JsonParser.parseString(json) } catch (_: RuntimeException) { return null }
|
|
13
|
+
if (!value.isJsonObject) return null
|
|
14
|
+
val fields = value.asJsonObject
|
|
15
|
+
fun string(key: String) = fields.get(key)?.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }?.asString
|
|
16
|
+
val type = string("type") ?: return null
|
|
17
|
+
return BackgroundEvent(type, string("at"), string("reason"), fields)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
internal fun backgroundSink(dispatcher: CoroutineDispatcher, handler: (BackgroundEvent) -> Unit): CoreEventSink =
|
|
21
|
+
object : CoreEventSink {
|
|
22
|
+
override fun deliver(deliveryId: String, eventJson: String, delivery: CoreDelivery) = runBlocking(dispatcher) {
|
|
23
|
+
val value = backgroundEvent(eventJson)
|
|
24
|
+
if (value == null) {
|
|
25
|
+
// A malformed Core event cannot enter app code. Report the binding fact so
|
|
26
|
+
// Core can retire this flight rather than leave its queue waiting forever.
|
|
27
|
+
delivery.complete("""{"outcome":"unavailable"}""")
|
|
28
|
+
return@runBlocking
|
|
29
|
+
}
|
|
30
|
+
if (!delivery.tryEnter()) return@runBlocking
|
|
31
|
+
val outcome = try {
|
|
32
|
+
handler(value)
|
|
33
|
+
"""{"outcome":"returned"}"""
|
|
34
|
+
} catch (_: Throwable) { """{"outcome":"threw"}""" }
|
|
35
|
+
delivery.complete(outcome)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonElement
|
|
4
|
+
import com.google.gson.JsonNull
|
|
5
|
+
import dev.croche.BindingFacts
|
|
6
|
+
import dev.croche.ffi.CoreStoragePal
|
|
7
|
+
import dev.croche.ffi.coreWebSearch
|
|
8
|
+
import dev.croche.ffi.CoreToolRegistration
|
|
9
|
+
import dev.croche.ffi.CoreToolDefinition
|
|
10
|
+
import dev.croche.ffi.CoreToolExecute
|
|
11
|
+
import dev.croche.ffi.CoreEventSink
|
|
12
|
+
import dev.croche.ffi.HostPal
|
|
13
|
+
import dev.croche.ffi.coreRegisterBackground
|
|
14
|
+
|
|
15
|
+
/** Direct generated constructor: Core validates the manifest and owns tool identity/metadata.
|
|
16
|
+
* No handwritten built-in definition or callback-result policy is supplied by this adapter. */
|
|
17
|
+
internal object ToolDefinitionCalls {
|
|
18
|
+
fun defineTool(manifest: JsonElement, execute: CoreToolExecute): CoreToolDefinition =
|
|
19
|
+
CoreToolDefinition(manifest.toString(), attached(execute))
|
|
20
|
+
|
|
21
|
+
val webSearch: CoreToolDefinition get() = coreWebSearch()
|
|
22
|
+
|
|
23
|
+
/** Replace only the opaque reference. Unknown/malformed registration options reach Core. */
|
|
24
|
+
fun registration(tool: CoreToolDefinition, wrapper: JsonElement? = null): CoreToolRegistration {
|
|
25
|
+
val options = wrapper?.deepCopy()?.also { if (it.isJsonObject) it.asJsonObject.add("tool", JsonNull.INSTANCE) }
|
|
26
|
+
return CoreToolRegistration(tool, options?.toString())
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Direct generated function; Core owns enrollment, wake policy, delivery and retries. */
|
|
31
|
+
internal object BackgroundCalls {
|
|
32
|
+
fun registerBackground(optionsJson: String, storage: CoreStoragePal, host: HostPal, sink: CoreEventSink? = null, platformStorage: CoreStoragePal? = null,
|
|
33
|
+
bindingFactsJson: String = BindingFacts.json()) =
|
|
34
|
+
coreRegisterBackground(optionsJson, attached(sink), attached(storage), attached(platformStorage), attached(host), bindingFactsJson)
|
|
35
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.BindingFacts
|
|
4
|
+
import dev.croche.ffi.*
|
|
5
|
+
import kotlinx.coroutines.Dispatchers
|
|
6
|
+
import kotlinx.coroutines.withContext
|
|
7
|
+
|
|
8
|
+
/** One native factory. No SDK project/agent registry, config resolver or fallback factory. */
|
|
9
|
+
internal object CoreFactoryCalls {
|
|
10
|
+
/** [bindingFactsJson]: the Kotlin SDK's own facts, or the RN Android bridge's ("react-native"). */
|
|
11
|
+
suspend fun open(openJson: String, registrations: List<CoreToolRegistration>, storage: CoreStoragePal, host: HostPal,
|
|
12
|
+
bindingFactsJson: String = BindingFacts.json(), recoveryJson: String? = null): CoreCroche =
|
|
13
|
+
withContext(Dispatchers.IO) { GeneratedLifecycleBinding.open(openJson, registrations, attached(storage), attached(host), bindingFactsJson, recoveryJson) }
|
|
14
|
+
|
|
15
|
+
suspend fun wakeTimes(openJson: String, queryJson: String?, storage: CoreStoragePal, host: HostPal): String =
|
|
16
|
+
withContext(Dispatchers.IO) { coreWakeTimes(openJson, queryJson, attached(storage), attached(host)) }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Facade groupings forward to the same Core object; they keep no state of their own. */
|
|
20
|
+
internal class CrocheCalls(private val core: CoreCrocheInterface) {
|
|
21
|
+
fun agent(id: String, scopeJson: String?) = core.agent(id, scopeJson)
|
|
22
|
+
suspend fun deliverDue(optionsJson: String?, cancel: CancelToken?): String =
|
|
23
|
+
withContext(Dispatchers.IO) { core.deliverDue(optionsJson, cancel) }
|
|
24
|
+
fun wikiPut(docJson: String) = core.wikiPut(docJson)
|
|
25
|
+
fun wikiRemove(path: String) = core.wikiRemove(path)
|
|
26
|
+
fun wikiList(optionsJson: String?): String = core.wikiList(optionsJson)
|
|
27
|
+
fun debugAttach(optionsJson: String): String = core.debugAttach(optionsJson)
|
|
28
|
+
fun debugDetach() = core.debugDetach()
|
|
29
|
+
fun close() = core.shutdown()
|
|
30
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonNull
|
|
4
|
+
import com.google.gson.JsonObject
|
|
5
|
+
import com.google.gson.JsonParser
|
|
6
|
+
import dev.croche.ffi.CoreStoragePal
|
|
7
|
+
import dev.croche.ffi.CoreToolRegistration
|
|
8
|
+
import dev.croche.ffi.CoreEventSink
|
|
9
|
+
|
|
10
|
+
/** Internal marshalling only. Public typed options do not admit undeclared keys.
|
|
11
|
+
* Core receives every serializable value unchanged, with only opaque references replaced.
|
|
12
|
+
*/
|
|
13
|
+
internal object CoreOptionsEncoding {
|
|
14
|
+
data class Encoded(
|
|
15
|
+
val optionsJson: String,
|
|
16
|
+
val registrations: List<CoreToolRegistration>,
|
|
17
|
+
val storage: CoreStoragePal,
|
|
18
|
+
val sink: CoreEventSink?,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
fun encode(
|
|
22
|
+
options: JsonObject,
|
|
23
|
+
registrations: List<CoreToolRegistration>?,
|
|
24
|
+
platformStorage: CoreStoragePal,
|
|
25
|
+
customStorage: CoreStoragePal? = null,
|
|
26
|
+
sink: CoreEventSink? = null,
|
|
27
|
+
): Encoded {
|
|
28
|
+
val json = options.deepCopy()
|
|
29
|
+
if (registrations != null) {
|
|
30
|
+
val tools = com.google.gson.JsonArray()
|
|
31
|
+
registrations.forEach { registration ->
|
|
32
|
+
tools.add(registration.optionsJson?.let { JsonParser.parseString(it) } ?: JsonNull.INSTANCE)
|
|
33
|
+
}
|
|
34
|
+
json.add("tools", tools)
|
|
35
|
+
}
|
|
36
|
+
// OpenOptions already carries ordered adapter-kind markers. Opaque
|
|
37
|
+
// storage implementations travel only through this callback argument.
|
|
38
|
+
if (sink != null) json.add("sink", JsonNull.INSTANCE)
|
|
39
|
+
return Encoded(json.toString(), registrations ?: emptyList(), customStorage ?: platformStorage, sink)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ffi.CancelToken
|
|
4
|
+
import dev.croche.ffi.CoreAttachmentInput
|
|
5
|
+
import dev.croche.ffi.CoreAgentInterface
|
|
6
|
+
import dev.croche.ffi.CoreEventSink
|
|
7
|
+
import dev.croche.ffi.CoreSubscriptionInterface
|
|
8
|
+
import kotlinx.coroutines.Dispatchers
|
|
9
|
+
import kotlinx.coroutines.withContext
|
|
10
|
+
|
|
11
|
+
/** Thin calls to S1/S3's generated agent, not a staging port or a second request state machine.
|
|
12
|
+
* JSON/options/cancellation are forwarded unchanged; Core owns all outcomes and queues. */
|
|
13
|
+
internal class AgentStreamCalls(private val agent: CoreAgentInterface) {
|
|
14
|
+
suspend fun send(inputJson: String, images: List<CoreAttachmentInput> = emptyList(), optionsJson: String? = null, cancel: CancelToken? = null): String =
|
|
15
|
+
coreStreamIO { agent.send(inputJson, images, optionsJson, cancel) }
|
|
16
|
+
|
|
17
|
+
suspend fun request(inputJson: String, images: List<CoreAttachmentInput> = emptyList(), optionsJson: String? = null, cancel: CancelToken? = null): String =
|
|
18
|
+
coreStreamIO { agent.request(inputJson, images, optionsJson, cancel) }
|
|
19
|
+
|
|
20
|
+
fun subscribe(selectionJson: String, eventSink: CoreEventSink): SubscriptionCalls =
|
|
21
|
+
SubscriptionCalls(agent.subscribe(selectionJson, attached(eventSink)))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** History uses the original subscription sink. No SDK queue, lifetime flag or callback cache. */
|
|
25
|
+
internal class SubscriptionCalls(private val subscription: CoreSubscriptionInterface) {
|
|
26
|
+
suspend fun history(optionsJson: String) = coreStreamIO { subscription.history(optionsJson) }
|
|
27
|
+
fun close() = subscription.shutdown()
|
|
28
|
+
val ended: Boolean get() = subscription.ended()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Carry failures as values across the coroutine dispatcher boundary, then throw the original.
|
|
32
|
+
* Otherwise coroutine stack-trace recovery may copy a Core/host exception on the caller's stack.
|
|
33
|
+
*/
|
|
34
|
+
private suspend fun <T> coreStreamIO(block: () -> T): T =
|
|
35
|
+
withContext(Dispatchers.IO) { runCatching(block) }.getOrThrow()
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.sun.jna.Native
|
|
4
|
+
import dev.croche.ffi.*
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Keeps a Core thread attached while a Kotlin callback runs.
|
|
8
|
+
*
|
|
9
|
+
* Core calls Kotlin from its own native threads, and a callback can call Core again on the same
|
|
10
|
+
* thread: `HostPal.http` hands each piece of the body to `HttpSink.chunk`, inside which Core delivers
|
|
11
|
+
* stream deltas and asks `nowMs`/`random`; an app listener or tool can call the runtime. JNA 5.17.0
|
|
12
|
+
* recomputes "detach on return" for every callback, a nested one too, from the thread state the
|
|
13
|
+
* outer attach left (native/callback.c:750), so the inner return detached the thread under the
|
|
14
|
+
* still-running outer frame. ART aborts on that ("attempting to detach while still running code");
|
|
15
|
+
* HotSpot refuses it and prints "JNA: could not detach thread".
|
|
16
|
+
*/
|
|
17
|
+
internal fun keepCallbackThreadAttached() = Native.detach(false)
|
|
18
|
+
|
|
19
|
+
/** Per thread: Kotlin callbacks running now, and whether the thread is the VM's own (JNA never detaches it). */
|
|
20
|
+
private class CallbackThread { var depth = 0; var vmThread = false }
|
|
21
|
+
private val callbackThread = ThreadLocal.withInitial { CallbackThread() }
|
|
22
|
+
|
|
23
|
+
/** Tests only: every JNA callback frame on the stack must be one of ours (one per depth). */
|
|
24
|
+
private val checkFrames = System.getProperty("croche.test.foreignCallbackFrames") == "true"
|
|
25
|
+
private fun checkFrames(depth: Int) {
|
|
26
|
+
val frames = Thread.currentThread().stackTrace
|
|
27
|
+
.count { it.methodName == "callback" && it.className == "com.sun.jna.CallbackReference\$DefaultCallbackProxy" }
|
|
28
|
+
if (frames != depth + 1) System.err.println("Croche: foreign callback outside hostCallback ($frames JNA frames, depth $depth)")
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Every Kotlin object Core can call enters here: each Core-facing interface is wrapped below, and an
|
|
33
|
+
* object is wrapped where it is handed to Core. With every entry counted, depth 0 is the outermost
|
|
34
|
+
* callback on the thread; it gives back JNA's usual detach on return, so a long-lived Core thread
|
|
35
|
+
* (croche-timer, croche-drive) is not left attached: as a non-daemon it would hold the JVM's exit.
|
|
36
|
+
* Core also calls back on the app's own thread during a call; JNA refuses to detach that one, which
|
|
37
|
+
* is learned once per thread.
|
|
38
|
+
*/
|
|
39
|
+
internal fun <T> hostCallback(block: () -> T): T {
|
|
40
|
+
val thread = callbackThread.get()
|
|
41
|
+
if (checkFrames) checkFrames(thread.depth)
|
|
42
|
+
val release = thread.depth == 0 && !thread.vmThread
|
|
43
|
+
thread.depth++
|
|
44
|
+
keepCallbackThreadAttached()
|
|
45
|
+
try { return block() } finally {
|
|
46
|
+
thread.depth--
|
|
47
|
+
if (release) try { Native.detach(true) } catch (_: IllegalStateException) { thread.vmThread = true }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A new member of a Core-facing interface must be added to its wrapper. */
|
|
52
|
+
internal class AttachedHostPal(private val host: HostPal) : HostPal {
|
|
53
|
+
private fun <T> enter(block: () -> T): T = hostCallback(block)
|
|
54
|
+
override fun blobOperation(operation: String, key: String, cursor: String?, bytes: ByteArray?) =
|
|
55
|
+
enter { host.blobOperation(operation, key, cursor, bytes) }
|
|
56
|
+
override fun streamPlatform() = enter { host.streamPlatform() }
|
|
57
|
+
override fun imageSupport() = enter { host.imageSupport() }
|
|
58
|
+
override fun imageOperation(operation: String, bytes: ByteArray, optionsJson: String, cancel: CancelToken) = enter { host.imageOperation(operation, bytes, optionsJson, cancel) }
|
|
59
|
+
override fun readImage(request: PalImageReadRequest, cancel: CancelToken) = enter { host.readImage(request, cancel) }
|
|
60
|
+
override fun releaseImage(capture: String) = enter { host.releaseImage(capture) }
|
|
61
|
+
override fun http(request: PalHttpRequest, sink: HttpSink, cancel: CancelToken) = enter { host.http(request, sink, cancel) }
|
|
62
|
+
override fun resolve(host: String, cancel: CancelToken) = enter { this.host.resolve(host, cancel) }
|
|
63
|
+
override fun platformFacts() = enter { host.platformFacts() }
|
|
64
|
+
override fun readAsset(path: String) = enter { host.readAsset(path) }
|
|
65
|
+
override fun list(prefix: String) = enter { host.list(prefix) }
|
|
66
|
+
override fun read(key: String) = enter { host.read(key) }
|
|
67
|
+
override fun write(key: String, value: String) = enter { host.write(key, value) }
|
|
68
|
+
override fun readSecret(key: String) = enter { host.readSecret(key) }
|
|
69
|
+
override fun writeSecret(key: String, value: String) = enter { host.writeSecret(key, value) }
|
|
70
|
+
override fun remove(key: String) = enter { host.remove(key) }
|
|
71
|
+
override fun purgeInstance(prefix: String) = enter { host.purgeInstance(prefix) }
|
|
72
|
+
override fun removeSecret(key: String) = enter { host.removeSecret(key) }
|
|
73
|
+
override fun lock(key: String) = enter { host.lock(key) }
|
|
74
|
+
override fun unlock(key: String) = enter { host.unlock(key) }
|
|
75
|
+
override fun storeIdentity() = enter { host.storeIdentity() }
|
|
76
|
+
override fun tool(invocation: String, cancel: CancelToken) = enter { host.tool(invocation, cancel) }
|
|
77
|
+
override fun attest(request: String, timeoutMs: ULong) = enter { host.attest(request, timeoutMs) }
|
|
78
|
+
override fun nowMs() = enter { host.nowMs() }
|
|
79
|
+
override fun utcOffsetMinutes(atMs: Double) = enter { host.utcOffsetMinutes(atMs) }
|
|
80
|
+
override fun random() = enter { host.random() }
|
|
81
|
+
override fun wake(atMs: Double?) = enter { host.wake(atMs) }
|
|
82
|
+
override fun schedule(entries: String) = enter { host.schedule(entries) }
|
|
83
|
+
override fun registerBackground(identifier: String, wake: CoreBackgroundWake) = enter { host.registerBackground(identifier, wake) }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
internal class AttachedStoragePal(private val storage: CoreStoragePal) : CoreStoragePal {
|
|
87
|
+
private fun <T> enter(block: () -> T): T = hostCallback(block)
|
|
88
|
+
override fun commitState(ops: List<CoreStateOp>, condition: CoreStateCondition) = enter {
|
|
89
|
+
storage.commitState(ops, condition)
|
|
90
|
+
}
|
|
91
|
+
override fun listState(prefix: String, options: CoreStateListOptions) = enter {
|
|
92
|
+
storage.listState(prefix, options)
|
|
93
|
+
}
|
|
94
|
+
override fun readVersioned(key: String) = enter { storage.readVersioned(key) }
|
|
95
|
+
override fun writeIf(key: String, value: String?, expect: String?) = enter { storage.writeIf(key, value, expect) }
|
|
96
|
+
override fun read(key: String) = enter { storage.read(key) }
|
|
97
|
+
override fun write(key: String, value: String) = enter { storage.write(key, value) }
|
|
98
|
+
override fun remove(key: String) = enter { storage.remove(key) }
|
|
99
|
+
override fun purgeInstance(prefix: String, secretKey: String?) = enter { storage.purgeInstance(prefix, secretKey) }
|
|
100
|
+
override fun list(prefix: String) = enter { storage.list(prefix) }
|
|
101
|
+
override fun lock(name: String) = enter { storage.lock(name) }
|
|
102
|
+
override fun unlock(name: String) = enter { storage.unlock(name) }
|
|
103
|
+
override fun readSecret(key: String) = enter { storage.readSecret(key) }
|
|
104
|
+
override fun writeSecret(key: String, value: String) = enter { storage.writeSecret(key, value) }
|
|
105
|
+
override fun removeSecret(key: String) = enter { storage.removeSecret(key) }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private class AttachedExecute(val execute: CoreToolExecute) : CoreToolExecute {
|
|
109
|
+
override fun execute(callJson: String, contextJson: String, cancel: CancelToken, attachments: dev.croche.ffi.CoreAttachmentReader) =
|
|
110
|
+
hostCallback { execute.execute(callJson, contextJson, cancel, attachments) }
|
|
111
|
+
}
|
|
112
|
+
private class AttachedSink(val sink: CoreEventSink) : CoreEventSink {
|
|
113
|
+
override fun deliver(deliveryId: String, eventJson: String, delivery: CoreDelivery) =
|
|
114
|
+
hostCallback { sink.deliver(deliveryId, eventJson, delivery) }
|
|
115
|
+
}
|
|
116
|
+
private class AttachedCancel(val callback: CancelCallback) : CancelCallback {
|
|
117
|
+
override fun cancelled() = hostCallback { callback.cancelled() }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Wrap once, where the object is handed to Core; an already attached object is returned as it is. */
|
|
121
|
+
internal fun attached(host: HostPal): HostPal = host as? AttachedHostPal ?: AttachedHostPal(host)
|
|
122
|
+
internal fun attached(storage: CoreStoragePal): CoreStoragePal = storage as? AttachedStoragePal ?: AttachedStoragePal(storage)
|
|
123
|
+
@JvmName("attachedOrNull") internal fun attached(storage: CoreStoragePal?): CoreStoragePal? = storage?.let(::attached)
|
|
124
|
+
internal fun attached(execute: CoreToolExecute): CoreToolExecute = execute as? AttachedExecute ?: AttachedExecute(execute)
|
|
125
|
+
internal fun attached(sink: CoreEventSink): CoreEventSink = sink as? AttachedSink ?: AttachedSink(sink)
|
|
126
|
+
@JvmName("attachedSinkOrNull") internal fun attached(sink: CoreEventSink?): CoreEventSink? = sink?.let(::attached)
|
|
127
|
+
internal fun attached(callback: CancelCallback): CancelCallback = callback as? AttachedCancel ?: AttachedCancel(callback)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import com.google.gson.Gson
|
|
4
|
+
import dev.croche.ImageAdapter
|
|
5
|
+
import dev.croche.ImageResizeLimits
|
|
6
|
+
import dev.croche.ffi.*
|
|
7
|
+
import kotlinx.coroutines.*
|
|
8
|
+
|
|
9
|
+
/** The only override is the app's image implementation; all other host effects stay native. */
|
|
10
|
+
internal class ImageAdapterHost(private val base: HostPal, private val image: ImageAdapter) : HostPal by base {
|
|
11
|
+
override fun imageSupport(): String = Gson().toJson(image.support)
|
|
12
|
+
override fun imageOperation(operation: String, bytes: ByteArray, optionsJson: String, cancel: CancelToken): PalImageReply = effectCallback {
|
|
13
|
+
dev.croche.NativeImagePal.check(cancel)
|
|
14
|
+
runBlocking(Dispatchers.IO) {
|
|
15
|
+
val pending = async {
|
|
16
|
+
dev.croche.NativeImagePal.check(cancel)
|
|
17
|
+
when (operation) {
|
|
18
|
+
"info" -> PalImageReply(Gson().toJson(image.info(bytes)), byteArrayOf())
|
|
19
|
+
"resize" -> PalImageReply("{}", image.resize(bytes, Gson().fromJson(optionsJson, ImageResizeLimits::class.java)))
|
|
20
|
+
else -> throw CorePalException.Failure("image_unsupported", "The image operation is not supported.")
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
cancel.onCancel(attached(object : CancelCallback { override fun cancelled() { pending.cancel() } }))
|
|
24
|
+
if (cancel.isCancelled()) pending.cancel()
|
|
25
|
+
pending.await()
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
package dev.croche.s7
|
|
2
|
+
|
|
3
|
+
import dev.croche.ResponseSink
|
|
4
|
+
import dev.croche.RuntimeCancellation
|
|
5
|
+
import dev.croche.ffi.*
|
|
6
|
+
import dev.croche.s7.pal.BlobFiles
|
|
7
|
+
import dev.croche.s7.pal.DirectHttpPal
|
|
8
|
+
import java.io.File
|
|
9
|
+
import java.io.FileNotFoundException
|
|
10
|
+
import java.io.IOException
|
|
11
|
+
import java.net.URI
|
|
12
|
+
import java.util.TimeZone
|
|
13
|
+
import java.util.UUID
|
|
14
|
+
|
|
15
|
+
/** Plain JVM effects only. No image codec and no acquisition before Core's codec preflight. */
|
|
16
|
+
internal class JvmCoreHost(
|
|
17
|
+
private val storage: CoreStoragePal,
|
|
18
|
+
private val blobs: BlobFiles? = (storage as? JvmStoragePal)?.blobs,
|
|
19
|
+
) : HostPal {
|
|
20
|
+
private val http = DirectHttpPal()
|
|
21
|
+
override fun blobOperation(operation: String, key: String, cursor: String?, bytes: ByteArray?): PalBlobReply =
|
|
22
|
+
(blobs ?: throw FfiException.Failure("storage_error", "No JVM blob adapter is attached"))
|
|
23
|
+
.operation(operation, key, cursor, bytes)
|
|
24
|
+
override fun streamPlatform() = HostStreamPlatform.OTHER
|
|
25
|
+
override fun platformFacts() = PalPlatformFacts(PalTargetKind.OTHER, null, java.util.Locale.getDefault().toLanguageTag(), null)
|
|
26
|
+
override fun imageSupport(): String? = null
|
|
27
|
+
override fun readImage(request: PalImageReadRequest, cancel: CancelToken): ByteArray = throw CorePalException.Failure("image_fetch_failed", "No JVM image reader is attached")
|
|
28
|
+
override fun imageOperation(operation: String, bytes: ByteArray, optionsJson: String, cancel: CancelToken): PalImageReply = throw CorePalException.Failure("image_unsupported", "No JVM image adapter is attached")
|
|
29
|
+
override fun releaseImage(capture: String) = Unit
|
|
30
|
+
override fun resolve(host: String, cancel: CancelToken) = effectCallback { http.resolve(host, signal(cancel)) }
|
|
31
|
+
override fun http(request: PalHttpRequest, sink: HttpSink, cancel: CancelToken) = effectCallback {
|
|
32
|
+
http.http(request, object : ResponseSink {
|
|
33
|
+
override fun head(status: Int, headers: List<PalHeader>) = sink.head(status.toUShort(), headers)
|
|
34
|
+
override fun chunk(bytes: ByteArray) = sink.chunk(bytes)
|
|
35
|
+
}, signal(cancel))
|
|
36
|
+
}
|
|
37
|
+
override fun readAsset(path: String): ByteArray? = effectCallback {
|
|
38
|
+
val file = if (path.startsWith("file:")) File(URI(path)) else File(path)
|
|
39
|
+
try { file.readBytes() }
|
|
40
|
+
catch (error: FileNotFoundException) { if (file.exists()) throw CorePalException.Failure("asset_read_failed", error.message ?: "Asset unavailable") else null }
|
|
41
|
+
catch (error: IOException) { throw CorePalException.Failure("asset_read_failed", error.message ?: "Asset unavailable") }
|
|
42
|
+
catch (error: SecurityException) { throw CorePalException.Failure("asset_read_failed", error.message ?: "Asset unavailable") }
|
|
43
|
+
}
|
|
44
|
+
override fun read(key: String) = storage.read(key)
|
|
45
|
+
override fun write(key: String, value: String) = storage.write(key, value)
|
|
46
|
+
override fun remove(key: String) = storage.remove(key)
|
|
47
|
+
override fun purgeInstance(prefix: String) = storage.purgeInstance(prefix, null)
|
|
48
|
+
override fun list(prefix: String) = storage.list(prefix)
|
|
49
|
+
override fun lock(key: String) = storage.lock(key)
|
|
50
|
+
override fun unlock(key: String) = storage.unlock(key)
|
|
51
|
+
override fun readSecret(key: String) = storage.readSecret(key)
|
|
52
|
+
override fun writeSecret(key: String, value: String) = storage.writeSecret(key, value)
|
|
53
|
+
override fun removeSecret(key: String) = storage.removeSecret(key)
|
|
54
|
+
override fun storeIdentity() = "jvm-core-storage-pal"
|
|
55
|
+
override fun nowMs() = System.currentTimeMillis().toDouble()
|
|
56
|
+
override fun utcOffsetMinutes(atMs: Double) = TimeZone.getDefault().getOffset(atMs.toLong()).toLong() / 60000
|
|
57
|
+
override fun random() = UUID.randomUUID().toString()
|
|
58
|
+
// CORE-GAP: no factory tick callback at this pin. Core must own scheduled upkeep;
|
|
59
|
+
// a delivery call is not maintenance, so the SDK cannot invent that substitution.
|
|
60
|
+
override fun wake(atMs: Double?) = Unit
|
|
61
|
+
override fun registerBackground(identifier: String, wake: CoreBackgroundWake): Unit =
|
|
62
|
+
throw CorePalException.Failure("capability_unsupported", "Plain JVM has no OS background installer")
|
|
63
|
+
override fun schedule(entries: String) = Unit
|
|
64
|
+
override fun tool(invocation: String, cancel: CancelToken): ToolOutput = throw CorePalException.Failure("tool_not_dispatched", "S1 uses registered tool definitions")
|
|
65
|
+
override fun attest(request: String, timeoutMs: ULong): String = throw CorePalException.Failure("attestation_unsupported", "No JVM attestation provider")
|
|
66
|
+
private fun signal(cancel: CancelToken) = RuntimeCancellation().also { result ->
|
|
67
|
+
if (cancel.isCancelled()) result.cancel()
|
|
68
|
+
cancel.onCancel(attached(object : CancelCallback { override fun cancelled() = result.cancel() }))
|
|
69
|
+
}
|
|
70
|
+
}
|