@h1r-ai/croche-react-native 0.6.0 → 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 +32 -4
- package/THIRD-PARTY-NOTICES +899 -6
- package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +4 -3
- package/android/src/main/AndroidManifest.xml +0 -5
- package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +33 -8
- package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +139 -5
- package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +5 -2
- package/android/src/main/java/dev/croche/reactnative/NativePal.kt +23 -6
- package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +28 -6
- package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +4 -11
- 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 +19 -4
- package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +2 -2
- 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/AndroidPlatform.kt +5 -3
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +4 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +11 -11
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageCodec.kt +1 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +11 -16
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +3 -3
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStateStore.kt +216 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +45 -10
- package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +0 -3
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Adapters.kt +75 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +1 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +9 -23
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +11 -2
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +83 -20
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +54 -8
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +19 -360
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +79 -10
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Decision.kt +170 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +8 -5
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +8 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/EncodedImageCodec.kt +51 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +426 -33
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +1 -351
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +15 -29
- 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 +196 -10
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +86 -3
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +33 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +1 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StateStore.kt +90 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +68 -4
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +3403 -6894
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +89 -18
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +1 -2
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +2 -2
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +3 -3
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +17 -18
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ImageAdapterHost.kt +28 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +12 -14
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStateStore.kt +125 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +36 -6
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativeCoreInitialization.kt +21 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +21 -6
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +16 -7
- 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/pal/AtomicKeyFiles.kt +25 -4
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/BlobFiles.kt +157 -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/react-native/src/core-native.d.ts +3 -0
- package/dist/react-native/src/core-native.js +148 -25
- package/dist/react-native/src/core-wire.d.ts +20 -3
- package/dist/rn-shared-build.json +8 -8
- package/dist/runtime/src/adapters.d.ts +8 -0
- package/dist/runtime/src/adapters.js +29 -0
- package/dist/runtime/src/agent.d.ts +53 -5
- package/dist/runtime/src/agent.js +69 -7
- package/dist/runtime/src/app-api.d.ts +307 -19
- package/dist/runtime/src/cancel.d.ts +2 -2
- package/dist/runtime/src/cancel.js +7 -4
- 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 +48 -35
- 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/croche.d.ts +64 -9
- package/dist/runtime/src/croche.js +78 -17
- package/dist/runtime/src/errors.d.ts +7 -0
- package/dist/runtime/src/errors.js +15 -1
- package/dist/runtime/src/generated/app-stream.d.ts +155 -1
- package/dist/runtime/src/generated/app-stream.js +7 -4
- 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 -21
- package/dist/runtime/src/index.native.js +2 -8
- package/dist/runtime/src/native-adapter.d.ts +1 -1
- package/dist/runtime/src/native-adapter.js +10 -4
- package/dist/runtime/src/pal/host.d.ts +3 -3
- package/dist/runtime/src/pal/host.js +34 -9
- package/dist/runtime/src/pal/platform.d.ts +11 -6
- 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/tools.d.ts +20 -4
- package/dist/runtime/src/tools.js +31 -7
- package/ios/CoreAgentTransport.swift +2 -2
- package/ios/CoreNativePal.swift +26 -21
- package/ios/CorePlatformTransport.swift +4 -4
- package/ios/CoreWireDispatch.swift +38 -8
- package/ios/CoreWireSupport.swift +179 -4
- package/ios/CrocheRuntimeBridge.swift +1 -1
- package/ios/ProjectPal.swift +15 -12
- package/ios/vendor/CrocheRuntimeSDK/BlobFiles.swift +124 -0
- package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +2 -6
- package/ios/vendor/CrocheRuntimeSDK/EncodedImages.swift +85 -0
- package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +1 -174
- package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +233 -4
- package/ios/vendor/CrocheRuntimeSDK/Host.swift +1 -399
- package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +3 -3
- package/ios/vendor/CrocheRuntimeSDK/InstancePayloadPurge.swift +88 -0
- package/ios/vendor/CrocheRuntimeSDK/JSON.swift +26 -10
- package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +0 -155
- package/ios/vendor/CrocheRuntimeSDK/Platform.swift +1 -27
- package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +8 -4
- package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +57 -21
- package/ios/vendor/CrocheRuntimeSDK/PlatformStateStore.swift +253 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +96 -10
- package/ios/vendor/CrocheRuntimeSDK/Project.swift +3 -382
- package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +1 -412
- package/ios/vendor/CrocheRuntimeSDK/RuntimeHTTPAdapter.swift +42 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Adapters.swift +110 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +86 -33
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Allowance.swift +20 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +1 -1
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +12 -3
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +135 -56
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Decision.swift +146 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +17 -8
- package/ios/vendor/CrocheRuntimeSDK/S1/S1HandleOwner.swift +58 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +184 -15
- package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +31 -3
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +131 -61
- package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +10 -15
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +86 -17
- 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 +3 -249
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +40 -8
- package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +1720 -3883
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +816 -1649
- 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 +816 -1649
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +2 -2
- package/package.json +3 -3
- package/scripts/README.md +1 -1
- package/scripts/check-swift-vendor-modules.sh +15 -0
- package/scripts/vendor-native.sh +4 -3
- 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 -113
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +0 -201
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +0 -25
- 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/StoragePal.kt +0 -18
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +0 -252
- package/dist/runtime/src/pal/codec.d.ts +0 -15
- package/dist/runtime/src/pal/codec.js +0 -52
- package/ios/vendor/CrocheRuntimeSDK/Background.swift +0 -59
- package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +0 -496
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +0 -44
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
import { signalFor } from './cancel.js';
|
|
2
2
|
import { coreModule } from './core/wasm.js';
|
|
3
3
|
import { encodable, jsonText } from './json.js';
|
|
4
|
-
import { thrownError } from './errors.js';
|
|
5
|
-
/**
|
|
4
|
+
import { thrownError, coreFailure } from './errors.js';
|
|
5
|
+
/**
|
|
6
|
+
* Give the agent a useful explanation when an app action cannot proceed.
|
|
7
|
+
*
|
|
8
|
+
* Its message reaches the model verbatim.
|
|
9
|
+
*/
|
|
6
10
|
export class ToolError extends Error {
|
|
11
|
+
/** Create an error whose message describes why the app action could not proceed. */
|
|
7
12
|
constructor(message, options) {
|
|
8
13
|
super(message, options);
|
|
9
14
|
this.name = 'ToolError';
|
|
10
15
|
}
|
|
11
16
|
}
|
|
12
17
|
const BRAND = Symbol('croche.toolResult');
|
|
13
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* Return content the agent can use, such as a caption and a photo from the app.
|
|
20
|
+
*
|
|
21
|
+
* The ONLY way to make a ToolResult: any other object a tool returns is Json, even one with `json` or `content`.
|
|
22
|
+
*/
|
|
14
23
|
export function toolResult(value) {
|
|
15
24
|
return Object.freeze({ [BRAND]: value });
|
|
16
25
|
}
|
|
@@ -28,16 +37,22 @@ function wrap(handle) {
|
|
|
28
37
|
export function coreDefinition(value) {
|
|
29
38
|
return typeof value === 'object' && value !== null ? handles.get(value) : undefined;
|
|
30
39
|
}
|
|
31
|
-
/**
|
|
32
|
-
*
|
|
40
|
+
/**
|
|
41
|
+
* Let the agent use app-owned features such as looking up an order.
|
|
42
|
+
*
|
|
43
|
+
* `manifest` is typed as ToolManifest (a JSON module import or a const with mixed commands fits it); the SDK still
|
|
44
|
+
* serializes it unchanged (undefined throws, json.ts) and Core validates it.
|
|
45
|
+
*/
|
|
33
46
|
export function defineTool(manifest, execute) {
|
|
34
47
|
const core = coreModule();
|
|
35
48
|
return wrap(core.defineTool(jsonText(manifest), bridge(execute)));
|
|
36
49
|
}
|
|
37
50
|
let webSearch;
|
|
51
|
+
let image;
|
|
38
52
|
/** Croche tools: they run in Core through Egress and are billed. */
|
|
39
53
|
export const tools = Object.freeze({
|
|
40
54
|
get webSearch() { return webSearch ??= wrap(coreModule().webSearch()); },
|
|
55
|
+
get image() { return image ??= wrap(coreModule().image()); },
|
|
41
56
|
});
|
|
42
57
|
// A ToolCompletion as JSON text. The app's value is spliced in as the strict serializer wrote it, never re-encoded.
|
|
43
58
|
const fact = (completion) => JSON.stringify(completion);
|
|
@@ -53,9 +68,18 @@ function completion(value) {
|
|
|
53
68
|
}
|
|
54
69
|
/** The callback Core holds for one definition. */
|
|
55
70
|
export function bridge(execute) {
|
|
56
|
-
return async (callJson, contextJson, cancel) => {
|
|
71
|
+
return async (callJson, contextJson, cancel, attachments) => {
|
|
57
72
|
const signal = signalFor(cancel);
|
|
58
|
-
const ctx = { ...JSON.parse(contextJson), signal
|
|
73
|
+
const ctx = { ...JSON.parse(contextJson), signal,
|
|
74
|
+
async attachment(id) {
|
|
75
|
+
try {
|
|
76
|
+
return JSON.parse(await attachments.attachment(id));
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
throw coreFailure(error);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
};
|
|
59
83
|
let outcome;
|
|
60
84
|
try {
|
|
61
85
|
outcome = completion(await execute(JSON.parse(callJson), ctx));
|
|
@@ -5,10 +5,10 @@ import Foundation
|
|
|
5
5
|
struct RNCoreAgentTransport: Sendable {
|
|
6
6
|
let core: any CoreAgentProtocol
|
|
7
7
|
|
|
8
|
-
func send(inputJson: String, images: [
|
|
8
|
+
func send(inputJson: String, images: [CoreAttachmentInput], optionsJson: String?, cancel: CancelToken?) throws -> String {
|
|
9
9
|
try core.send(inputJson: inputJson, images: images, optionsJson: optionsJson, cancel: cancel)
|
|
10
10
|
}
|
|
11
|
-
func request(inputJson: String, images: [
|
|
11
|
+
func request(inputJson: String, images: [CoreAttachmentInput], optionsJson: String?, cancel: CancelToken?) throws -> String {
|
|
12
12
|
try core.request(inputJson: inputJson, images: images, optionsJson: optionsJson, cancel: cancel)
|
|
13
13
|
}
|
|
14
14
|
}
|
package/ios/CoreNativePal.swift
CHANGED
|
@@ -16,16 +16,33 @@ final class RNDefaultPal: @unchecked Sendable {
|
|
|
16
16
|
}
|
|
17
17
|
func call(_ method: String, _ args: [Any]) throws -> Any {
|
|
18
18
|
let host = try host()
|
|
19
|
+
let storage = PlatformStorageAdapter(host)
|
|
19
20
|
switch method {
|
|
20
|
-
case "
|
|
21
|
-
|
|
22
|
-
case "
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
case "
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
case "listState", "commit":
|
|
22
|
+
return try rnStateStorageCall(storage, method, args)
|
|
23
|
+
case "readVersioned":
|
|
24
|
+
let pair = try storage.readVersioned(key: rnString(args, 0))
|
|
25
|
+
return ["value": pair.value as Any? ?? NSNull(), "version": pair.version as Any? ?? NSNull()]
|
|
26
|
+
case "writeIf":
|
|
27
|
+
guard args.count == 3 else { throw RNWireError.badPacket }
|
|
28
|
+
let outcome = try storage.writeIf(key: rnString(args, 0), value: rnStorageString(args[1]), expect: rnStorageString(args[2]))
|
|
29
|
+
switch outcome {
|
|
30
|
+
case let .written(version): return ["type": "written", "version": version as Any? ?? NSNull()]
|
|
31
|
+
case let .conflict(version): return ["type": "conflict", "version": version as Any? ?? NSNull()]
|
|
32
|
+
}
|
|
33
|
+
case "read": return try storage.read(key: rnString(args, 0)) as Any? ?? NSNull()
|
|
34
|
+
case "write": try storage.write(key: rnString(args, 0), value: rnString(args, 1)); return NSNull()
|
|
35
|
+
case "remove": try storage.remove(key: rnString(args, 0)); return NSNull()
|
|
36
|
+
case "list": return try storage.list(prefix: rnString(args, 0))
|
|
37
|
+
case "lock": return try storage.lock(name: rnString(args, 0)) == .acquired ? "acquired" : "held"
|
|
38
|
+
case "unlock": try storage.unlock(name: rnString(args, 0)); return NSNull()
|
|
39
|
+
case "readSecret": return try storage.readSecret(key: rnString(args, 0)) as Any? ?? NSNull()
|
|
40
|
+
case "writeSecret": try storage.writeSecret(key: rnString(args, 0), value: rnString(args, 1)); return NSNull()
|
|
41
|
+
case "removeSecret": try storage.removeSecret(key: rnString(args, 0)); return NSNull()
|
|
42
|
+
case "purgeInstance":
|
|
43
|
+
let secretKey = args.count > 1 ? try rnStorageString(args[1]) : nil
|
|
44
|
+
try storage.purgeInstance(prefix: rnString(args, 0), secretKey: secretKey)
|
|
45
|
+
return NSNull()
|
|
29
46
|
case "platformFacts":
|
|
30
47
|
let facts = host.platformFacts(); var json: [String: Any] = [:]
|
|
31
48
|
switch facts.targetKind {
|
|
@@ -37,24 +54,12 @@ final class RNDefaultPal: @unchecked Sendable {
|
|
|
37
54
|
if let value = facts.debuggable { json["debuggable"] = value }
|
|
38
55
|
if let value = facts.locale { json["locale"] = value }
|
|
39
56
|
if let value = facts.attestationPlatform { json["attestationPlatform"] = value }
|
|
40
|
-
if let value = facts.browserFetch { json["browserFetch"] = value }
|
|
41
57
|
return json
|
|
42
58
|
case "readAsset": return try host.readAsset(path: rnString(args, 0)).map { Array($0) } as Any? ?? NSNull()
|
|
43
59
|
case "resolve": return try host.resolve(host: rnString(args, 0), cancel: CancelToken())
|
|
44
60
|
case "readImage":
|
|
45
61
|
let max = (args[1] as! NSNumber).uint64Value
|
|
46
62
|
return Array(try host.readImage(request: PalImageReadRequest(source: .local(uri: rnString(args, 0)), maxBytes: max), cancel: CancelToken()))
|
|
47
|
-
case "codecAvailable": return host.imageCodecAvailable()
|
|
48
|
-
case "decode":
|
|
49
|
-
let data = Data((args[0] as! [NSNumber]).map { $0.uint8Value })
|
|
50
|
-
let image = try host.decodeImage(bytes: data, options: PalImageDecodeOptions(frame: 0, applyOrientation: true), cancel: CancelToken())
|
|
51
|
-
return ["handle":image.handle, "width":image.width, "height":image.height, "hasAlpha":image.hasAlpha as Any? ?? NSNull()]
|
|
52
|
-
case "render":
|
|
53
|
-
let p = args[0] as! [String: Any]
|
|
54
|
-
let request = PalImageRenderRequest(handle: p["handle"] as! String, width: (p["width"] as! NSNumber).uint32Value,
|
|
55
|
-
height: (p["height"] as! NSNumber).uint32Value, format: p["format"] as! String == "png" ? .png : .jpeg, quality: p["quality"] as? Double)
|
|
56
|
-
return Array(try host.renderImage(request: request, cancel: CancelToken()))
|
|
57
|
-
case "release": host.releaseDecodedImage(handle: try rnString(args, 0)); return NSNull()
|
|
58
63
|
case "cancelHttp":
|
|
59
64
|
let id = try rnString(args, 0)
|
|
60
65
|
guardLock.lock(); let token = httpTokens[id]; if token == nil { cancelledBeforeStart.insert(id) }; guardLock.unlock()
|
|
@@ -7,14 +7,14 @@ struct RNCorePlatform: @unchecked Sendable {
|
|
|
7
7
|
let storage: any CoreStoragePal
|
|
8
8
|
let platformStorage: any CoreStoragePal
|
|
9
9
|
|
|
10
|
-
init(host: PlatformPal, storage: any CoreStoragePal) {
|
|
10
|
+
init(host: PlatformPal, storage: any CoreStoragePal, image: RNImageCallback? = nil, blobs: RNBlobCallback? = nil) {
|
|
11
11
|
host.streamPlatformFact = .reactNative
|
|
12
|
-
self.host = RNProjectPal(host); self.storage = storage
|
|
12
|
+
self.host = RNProjectPal(host, image: image, blobs: blobs); self.storage = storage
|
|
13
13
|
self.platformStorage = PlatformStorageAdapter(host)
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
static func standard(storage: any CoreStoragePal) throws -> RNCorePlatform {
|
|
17
|
-
RNCorePlatform(host: try PlatformPal.standard(), storage: storage)
|
|
16
|
+
static func standard(storage: any CoreStoragePal, image: RNImageCallback? = nil, blobs: RNBlobCallback? = nil) throws -> RNCorePlatform {
|
|
17
|
+
RNCorePlatform(host: try PlatformPal.standard(), storage: storage, image: image, blobs: blobs)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/// The bridge's own binding facts: sdk "react-native" (BindingFacts, from the SDK sources it builds).
|
|
@@ -9,6 +9,13 @@ final class RNCoreDispatch: @unchecked Sendable {
|
|
|
9
9
|
private func storage(_ args: [Any], _ storageIndex: Int, _ hostIndex: Int) throws -> RNCallbackStorage {
|
|
10
10
|
RNCallbackStorage(try rnString(args, storageIndex), host: try rnString(args, hostIndex), callbacks: callbacks)
|
|
11
11
|
}
|
|
12
|
+
private func image(_ args: [Any], _ index: Int) throws -> RNImageCallback {
|
|
13
|
+
try RNImageCallback(rnString(args, 3), support: index < args.count ? args[index] : nil, callbacks: callbacks, handles: handles)
|
|
14
|
+
}
|
|
15
|
+
private func blobs(_ args: [Any], _ index: Int) throws -> RNBlobCallback? {
|
|
16
|
+
guard index < args.count, args[index] as? Bool == true else { return nil }
|
|
17
|
+
return try RNBlobCallback(rnString(args, 3), callbacks)
|
|
18
|
+
}
|
|
12
19
|
private func registrations(_ args: [Any]) throws -> [CoreToolRegistration] {
|
|
13
20
|
guard let entries = args[1] as? [[String: Any]] else { throw RNWireError.badPacket }
|
|
14
21
|
return try entries.map { entry in
|
|
@@ -21,25 +28,32 @@ final class RNCoreDispatch: @unchecked Sendable {
|
|
|
21
28
|
try callbacks.sendJSON(json)
|
|
22
29
|
}
|
|
23
30
|
}
|
|
24
|
-
func call(_ id: String, _ method: String, _ args: [Any], _ images: [
|
|
31
|
+
func call(_ id: String, _ method: String, _ args: [Any], _ images: [CoreAttachmentInput] = []) throws -> Any {
|
|
25
32
|
if method == "release" { handles.forget(id); return NSNull() }
|
|
26
33
|
if method == "tryEnter" { guard let entered = deliveries.tryEnter(id) else { throw RNWireError.missingHandle(id) }; return entered }
|
|
27
34
|
if method == "complete" { guard deliveries.complete(id, try rnString(args, 0)) else { throw RNWireError.missingHandle(id) }; return NSNull() }
|
|
28
35
|
if id.isEmpty {
|
|
29
36
|
switch method {
|
|
37
|
+
case "platformImageSupport": return try PlatformPal.standard().imageSupport().map { try JSONSerialization.jsonObject(with: Data($0.utf8)) } as Any? ?? NSNull()
|
|
38
|
+
case "platformImageOperation":
|
|
39
|
+
guard let values = args[1] as? [NSNumber] else { throw RNWireError.badPacket }
|
|
40
|
+
let token: CancelToken = try handles.get(rnString(args, 3))
|
|
41
|
+
let reply = try PlatformPal.standard().imageOperation(operation: rnString(args, 0), bytes: Data(values.map { $0.uint8Value }), optionsJson: rnJSON(args[2]), cancel: token)
|
|
42
|
+
return ["info": try JSONSerialization.jsonObject(with: Data(reply.metadataJson.utf8)), "bytes": Array(reply.bytes)]
|
|
30
43
|
case "newCancel": return handles.hold(CancelToken())
|
|
31
44
|
case "defineTool":
|
|
32
45
|
let execute = RNToolExecute(try rnString(args, 1), callbacks, handles)
|
|
33
46
|
return handles.hold(try CoreToolDefinition(manifestJson: rnString(args, 0), execute: execute))
|
|
34
47
|
case "webSearch": return handles.hold(coreWebSearch())
|
|
48
|
+
case "image": return handles.hold(coreImage())
|
|
35
49
|
case "open":
|
|
36
|
-
let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage)
|
|
50
|
+
let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage, image: image(args, 5), blobs: blobs(args, 6))
|
|
37
51
|
return handles.hold(try platform.open(rnString(args, 0), registrations: registrations(args), recoveryJson: rnOptional(args, 4)))
|
|
38
52
|
case "wakeTimes":
|
|
39
|
-
let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage)
|
|
53
|
+
let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage, image: image(args, 4), blobs: blobs(args, 5))
|
|
40
54
|
return try coreWakeTimes(openJson: rnString(args, 0), queryJson: rnOptional(args, 1), storage: storage, host: platform.host)
|
|
41
55
|
case "registerBackground":
|
|
42
|
-
let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage)
|
|
56
|
+
let storage = try storage(args, 2, 3), platform = try RNCorePlatform.standard(storage: storage, blobs: blobs(args, 6))
|
|
43
57
|
// Selected storage and OS-default marker storage are separate arguments;
|
|
44
58
|
// neither is inferred from the app JSON.
|
|
45
59
|
try platform.registerBackground(rnString(args, 0), sink: rnOptional(args, 1).map(eventSink), platformStorage: rnOptional(args, 4) == nil ? nil : platform.platformStorage)
|
|
@@ -52,26 +66,42 @@ final class RNCoreDispatch: @unchecked Sendable {
|
|
|
52
66
|
if let token: CancelToken = try? handles.get(id) { token.cancel(); return NSNull() }
|
|
53
67
|
let agent: CoreAgent = try handles.get(id); try agent.cancel(inputId: rnOptional(args, 0)); return NSNull()
|
|
54
68
|
case "isCancelled": let token: CancelToken = try handles.get(id); return token.isCancelled()
|
|
69
|
+
case "attachment":
|
|
70
|
+
if let reader: any CoreAttachmentReader = try? handles.get(id) { return try reader.attachment(id: rnString(args, 0)) }
|
|
71
|
+
if let agent: CoreAgent = try? handles.get(id) { return try agent.attachment(id: rnString(args, 0)) }
|
|
72
|
+
let owner: CoreCroche = try handles.get(id); return try owner.attachment(id: rnString(args, 0))
|
|
73
|
+
case "deleteAttachment":
|
|
74
|
+
if let agent: CoreAgent = try? handles.get(id) { try agent.deleteAttachment(id: rnString(args, 0)) }
|
|
75
|
+
else { let owner: CoreCroche = try handles.get(id); try owner.deleteAttachment(id: rnString(args, 0)) }; return NSNull()
|
|
76
|
+
case "listAttachments":
|
|
77
|
+
if let agent: CoreAgent = try? handles.get(id) { return try agent.listAttachments(optionsJson: rnOptional(args, 0)) }
|
|
78
|
+
let owner: CoreCroche = try handles.get(id); return try owner.listAttachments(optionsJson: rnOptional(args, 0))
|
|
79
|
+
case "copyAttachment": let agent: CoreAgent = try handles.get(id); return try agent.copyAttachment(sourceJson: rnString(args, 0))
|
|
80
|
+
case "releaseAttachment": let agent: CoreAgent = try handles.get(id); try agent.releaseAttachment(id: rnString(args, 0)); return NSNull()
|
|
55
81
|
case "name": let tool: CoreToolDefinition = try handles.get(id); return tool.name()
|
|
56
82
|
case "agent": let owner: CoreCroche = try handles.get(id); return handles.hold(try owner.agent(id: rnString(args, 0), scopeJson: rnOptional(args, 1)))
|
|
57
83
|
case "deleteData": let owner: CoreCroche = try handles.get(id); try owner.deleteData(scopeJson: rnOptional(args, 0)); return NSNull()
|
|
58
|
-
case "agentIds": let owner: CoreCroche = try handles.get(id); return try owner.agentIds()
|
|
59
84
|
case "deliverDue": let owner: CoreCroche = try handles.get(id); return try owner.deliverDue(optionsJson: rnOptional(args, 0), cancel: handles.get(rnString(args, 1)))
|
|
60
85
|
case "runTool": let owner: CoreCroche = try handles.get(id); return try owner.runTool(name: rnString(args, 0), argsJson: rnString(args, 1), optionsJson: rnString(args, 2), cancel: handles.get(rnString(args, 3)))
|
|
61
|
-
case "
|
|
86
|
+
case "allowanceSet": let owner: CoreCroche = try handles.get(id); try owner.allowanceSet(limitJson: rnString(args, 0)); return NSNull()
|
|
87
|
+
case "wikiList": let owner: CoreCroche = try handles.get(id); return try owner.wikiList(optionsJson: rnOptional(args, 0))
|
|
88
|
+
case "verification": let owner: CoreCroche = try handles.get(id); return try owner.verification()
|
|
62
89
|
case "wikiPut": let owner: CoreCroche = try handles.get(id); try owner.wikiPut(docJson: rnString(args, 0)); return NSNull()
|
|
63
90
|
case "wikiRemove": let owner: CoreCroche = try handles.get(id); try owner.wikiRemove(path: rnString(args, 0)); return NSNull()
|
|
64
91
|
case "debugAttach": let owner: CoreCroche = try handles.get(id); return try owner.debugAttach(optionsJson: rnString(args, 0))
|
|
65
92
|
case "debugDetach": let owner: CoreCroche = try handles.get(id); try owner.debugDetach(); return NSNull()
|
|
66
93
|
case "id": let agent: CoreAgent = try handles.get(id); return agent.id()
|
|
67
94
|
case "instance": let agent: CoreAgent = try handles.get(id); return agent.instance() as Any? ?? NSNull()
|
|
95
|
+
case "decide":
|
|
96
|
+
let agent: CoreAgent = try handles.get(id), cancel: CancelToken = try handles.get(rnString(args, 2))
|
|
97
|
+
return try agent.decide(inputJson: rnString(args, 0), optionsJson: rnOptional(args, 1), cancel: cancel)
|
|
68
98
|
case "send", "request":
|
|
69
99
|
let agent: CoreAgent = try handles.get(id), cancel: CancelToken = try handles.get(rnString(args, 2))
|
|
70
100
|
if method == "send" { return try agent.send(inputJson: rnString(args, 0), images: images, optionsJson: rnOptional(args, 1), cancel: cancel) }
|
|
71
101
|
return try agent.request(inputJson: rnString(args, 0), images: images, optionsJson: rnOptional(args, 1), cancel: cancel)
|
|
72
102
|
case "subscribe": let agent: CoreAgent = try handles.get(id); return handles.hold(try agent.subscribe(selectionJson: rnString(args, 0), eventSink: eventSink(rnString(args, 1))))
|
|
73
|
-
case "clearHistory": let agent: CoreAgent = try handles.get(id); try agent.clearHistory(); return NSNull()
|
|
74
|
-
case "memoryList": let agent: CoreAgent = try handles.get(id); return try agent.memoryList()
|
|
103
|
+
case "clearHistory": let agent: CoreAgent = try handles.get(id); try agent.clearHistory(optionsJson: rnOptional(args, 0)); return NSNull()
|
|
104
|
+
case "memoryList": let agent: CoreAgent = try handles.get(id); return try agent.memoryList(optionsJson: rnOptional(args, 0))
|
|
75
105
|
case "memoryDelete": let agent: CoreAgent = try handles.get(id); try agent.memoryDelete(id: rnString(args, 0)); return NSNull()
|
|
76
106
|
case "memoryClear": let agent: CoreAgent = try handles.get(id); try agent.memoryClear(); return NSNull()
|
|
77
107
|
case "memoryEnabled": let agent: CoreAgent = try handles.get(id); try agent.memoryEnabled(on: args[0] as! Bool); return NSNull()
|
|
@@ -12,6 +12,7 @@ func rnArgs(_ json: String) throws -> [Any] {
|
|
|
12
12
|
func rnString(_ args: [Any], _ i: Int) throws -> String {
|
|
13
13
|
guard i < args.count, let value = args[i] as? String else { throw RNWireError.badPacket }; return value
|
|
14
14
|
}
|
|
15
|
+
func rnStorageString(_ value: Any?) throws -> String? { try storageNullableString(value) }
|
|
15
16
|
func rnOptional(_ args: [Any], _ i: Int) -> String? { i < args.count ? args[i] as? String : nil }
|
|
16
17
|
|
|
17
18
|
final class RNCoreHandles: @unchecked Sendable {
|
|
@@ -64,6 +65,89 @@ final class RNCoreCallbacks: @unchecked Sendable {
|
|
|
64
65
|
func invalidate() { lock.lock(); alive = false; sender = nil; replies = [:]; lock.broadcast(); lock.unlock() }
|
|
65
66
|
}
|
|
66
67
|
|
|
68
|
+
func rnStateRequiredString(_ value: Any?) throws -> String {
|
|
69
|
+
guard let text = try rnStorageString(value) else { throw RNWireError.badPacket }
|
|
70
|
+
return text
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// An absent field and a JSON null both mean "not given".
|
|
74
|
+
func rnStateOptionalString(_ value: Any?) throws -> String? {
|
|
75
|
+
guard let value else { return nil }
|
|
76
|
+
return try rnStorageString(value)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// The app StateStore's list options, as the wire carries them: every field optional.
|
|
80
|
+
func rnStateListOptions(_ value: Any?) throws -> CoreStateListOptions {
|
|
81
|
+
guard let fields = value as? [String: Any] else { throw RNWireError.badPacket }
|
|
82
|
+
var limit: UInt32?
|
|
83
|
+
if let raw = fields["limit"], !(raw is NSNull) {
|
|
84
|
+
guard let number = raw as? NSNumber, let exact = UInt32(exactly: number), exact > 0 else { throw RNWireError.badPacket }
|
|
85
|
+
limit = exact
|
|
86
|
+
}
|
|
87
|
+
var reverse = false
|
|
88
|
+
if let raw = fields["reverse"], !(raw is NSNull) {
|
|
89
|
+
guard let flag = raw as? Bool else { throw RNWireError.badPacket }
|
|
90
|
+
reverse = flag
|
|
91
|
+
}
|
|
92
|
+
return CoreStateListOptions(start: try rnStateOptionalString(fields["start"]), reverse: reverse, limit: limit,
|
|
93
|
+
cursor: try rnStateOptionalString(fields["cursor"]))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
func rnStateListOptionsJSON(_ options: CoreStateListOptions) -> [String: Any] {
|
|
97
|
+
var fields: [String: Any] = [:]
|
|
98
|
+
if let start = options.start { fields["start"] = start }
|
|
99
|
+
if options.reverse { fields["reverse"] = true }
|
|
100
|
+
if let limit = options.limit { fields["limit"] = limit }
|
|
101
|
+
if let cursor = options.cursor { fields["cursor"] = cursor }
|
|
102
|
+
return fields
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
func rnStateOps(_ raw: Any) throws -> [CoreStateOp] {
|
|
106
|
+
guard let rows = raw as? [[String: Any]] else { throw RNWireError.badPacket }
|
|
107
|
+
return try rows.map { row in
|
|
108
|
+
let key = try rnStateRequiredString(row["key"])
|
|
109
|
+
switch row["type"] as? String {
|
|
110
|
+
case "put": return .put(key: key, value: try rnStateRequiredString(row["value"]))
|
|
111
|
+
case "delete": return .delete(key: key)
|
|
112
|
+
default: throw RNWireError.badPacket
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
func rnStateOpsJSON(_ ops: [CoreStateOp]) -> [[String: String]] {
|
|
118
|
+
ops.map { op in
|
|
119
|
+
switch op {
|
|
120
|
+
case let .put(key, value): return ["type": "put", "key": key, "value": value]
|
|
121
|
+
case let .delete(key): return ["type": "delete", "key": key]
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
func rnStateStorageCall(_ storage: any CoreStoragePal, _ method: String, _ args: [Any]) throws -> Any {
|
|
127
|
+
switch method {
|
|
128
|
+
case "listState":
|
|
129
|
+
guard args.count == 2 else { throw RNWireError.badPacket }
|
|
130
|
+
let page = try storage.listState(prefix: rnString(args, 0), options: rnStateListOptions(args[1]))
|
|
131
|
+
var reply: [String: Any] = ["entries": page.entries.map { ["key": $0.key, "value": $0.value] }]
|
|
132
|
+
if let next = page.nextCursor { reply["nextCursor"] = next }
|
|
133
|
+
return reply
|
|
134
|
+
case "commit":
|
|
135
|
+
guard args.count == 2, let condition = args[1] as? [String: Any] else { throw RNWireError.badPacket }
|
|
136
|
+
let result = try storage.commitState(
|
|
137
|
+
ops: rnStateOps(args[0]),
|
|
138
|
+
condition: CoreStateCondition(
|
|
139
|
+
key: rnStateRequiredString(condition["key"]),
|
|
140
|
+
version: rnStorageString(condition["version"])
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
switch result {
|
|
144
|
+
case let .committed(version): return ["type": "committed", "version": version as Any? ?? NSNull()]
|
|
145
|
+
case .conflict: return ["type": "conflict"]
|
|
146
|
+
}
|
|
147
|
+
default: throw RNWireError.badPacket
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
67
151
|
final class RNCallbackStorage: CoreStoragePal, @unchecked Sendable {
|
|
68
152
|
let id: String, host: String
|
|
69
153
|
let callbacks: RNCoreCallbacks
|
|
@@ -78,11 +162,42 @@ final class RNCallbackStorage: CoreStoragePal, @unchecked Sendable {
|
|
|
78
162
|
} catch let error as CoreStorageFailure { throw error }
|
|
79
163
|
catch { throw CoreStorageFailure.Failure(detail: String(describing: error)) }
|
|
80
164
|
}
|
|
165
|
+
func listState(prefix: String, options: CoreStateListOptions) throws -> CoreStatePage {
|
|
166
|
+
guard let page = try call("listState", [prefix, rnStateListOptionsJSON(options)]) as? [String: Any],
|
|
167
|
+
let rows = page["entries"] as? [[String: Any]] else {
|
|
168
|
+
throw CoreStorageFailure.Failure(detail: "Invalid StateStore range reply")
|
|
169
|
+
}
|
|
170
|
+
let entries = try rows.map {
|
|
171
|
+
try CoreStateEntry(key: rnStateRequiredString($0["key"]), value: rnStateRequiredString($0["value"]))
|
|
172
|
+
}
|
|
173
|
+
return CoreStatePage(entries: entries, nextCursor: try rnStateOptionalString(page["nextCursor"]))
|
|
174
|
+
}
|
|
175
|
+
func commitState(ops: [CoreStateOp], condition: CoreStateCondition) throws -> CoreStateCommit {
|
|
176
|
+
let guardRow: [String: Any] = ["key": condition.key, "version": condition.version as Any? ?? NSNull()]
|
|
177
|
+
guard let result = try call("commit", [rnStateOpsJSON(ops), guardRow]) as? [String: Any] else {
|
|
178
|
+
throw CoreStorageFailure.Failure(detail: "Invalid StateStore commit reply")
|
|
179
|
+
}
|
|
180
|
+
switch result["type"] as? String {
|
|
181
|
+
case "committed": return .committed(version: try rnStorageString(result["version"]))
|
|
182
|
+
case "conflict": return .conflict
|
|
183
|
+
default: throw CoreStorageFailure.Failure(detail: "Invalid StateStore commit reply")
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
func readVersioned(key: String) throws -> CoreVersionedValue {
|
|
187
|
+
try decodeStorageValue(call("readVersioned", [versionedStorageKey(key)]))
|
|
188
|
+
}
|
|
189
|
+
func writeIf(key: String, value: String?, expect: String?) throws -> CoreWriteOutcome {
|
|
190
|
+
let key = try versionedStorageKey(key), expect = try storageVersion(expect)
|
|
191
|
+
return try decodeStorageOutcome(call("writeIf", [key, value as Any? ?? NSNull(), expect as Any? ?? NSNull()]), for: value)
|
|
192
|
+
}
|
|
81
193
|
func read(key: String) throws -> String? { try call("read", [key]) as? String }
|
|
82
194
|
func write(key: String, value: String) throws {
|
|
83
195
|
_ = try call("write", [key, value])
|
|
84
196
|
}
|
|
85
197
|
func remove(key: String) throws { _ = try call("remove", [key]) }
|
|
198
|
+
func purgeInstance(prefix: String, secretKey: String?) throws {
|
|
199
|
+
_ = try call("purgeInstance", [prefix, secretKey as Any? ?? NSNull()])
|
|
200
|
+
}
|
|
86
201
|
func list(prefix: String) throws -> [String] { guard let value = try call("list", [prefix]) as? [String] else { throw CoreStorageFailure.Failure(detail: "Invalid list transport reply") }; return value }
|
|
87
202
|
func lock(name: String) throws -> PalLockOutcome {
|
|
88
203
|
switch try call("lock", [name]) as? String { case "acquired": return .acquired; case "held": return .held; default: throw CoreStorageFailure.Failure(detail: "Invalid lock transport reply") }
|
|
@@ -102,10 +217,70 @@ final class RNToolExecute: CoreToolExecute, @unchecked Sendable {
|
|
|
102
217
|
let id: String; let callbacks: RNCoreCallbacks; let handles: RNCoreHandles
|
|
103
218
|
init(_ id: String, _ callbacks: RNCoreCallbacks, _ handles: RNCoreHandles) { self.id = id; self.callbacks = callbacks; self.handles = handles }
|
|
104
219
|
deinit { try? callbacks.post(["type":"retired", "executors":[id], "hosts":[], "storages":[], "sinks":[], "tokens":[]]) }
|
|
105
|
-
func execute(callJson: String, contextJson: String, cancel: CancelToken) throws -> String {
|
|
106
|
-
let token = handles.hold(cancel); cancel.onCancel(callback: RNCancelNotice(token, callbacks))
|
|
220
|
+
func execute(callJson: String, contextJson: String, cancel: CancelToken, attachments: any CoreAttachmentReader) throws -> String {
|
|
221
|
+
let token = handles.hold(cancel), reader = handles.hold(attachments); cancel.onCancel(callback: RNCancelNotice(token, callbacks))
|
|
222
|
+
defer { handles.forget(token) }
|
|
223
|
+
do { return try callbacks.invoke(["type":"execute", "execute":id, "callJson":callJson, "contextJson":contextJson, "cancel":token, "attachments":reader]) }
|
|
224
|
+
catch { handles.forget(reader); return #"{"type":"unavailable"}"# }
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/// A selected JS BlobStore travels separately from StateStore and the default file host.
|
|
229
|
+
final class RNBlobCallback: Sendable {
|
|
230
|
+
let id: String
|
|
231
|
+
let callbacks: RNCoreCallbacks
|
|
232
|
+
init(_ id: String, _ callbacks: RNCoreCallbacks) { self.id = id; self.callbacks = callbacks }
|
|
233
|
+
|
|
234
|
+
func call(_ operation: String, _ key: String, _ cursor: String?, _ bytes: Data?) throws -> PalBlobReply {
|
|
235
|
+
do {
|
|
236
|
+
let raw = try callbacks.invoke(["type": "blob", "host": id, "operation": operation,
|
|
237
|
+
"key": key, "cursor": cursor as Any? ?? NSNull(),
|
|
238
|
+
"bytes": bytes.map { Array($0) } as Any? ?? NSNull()])
|
|
239
|
+
guard let reply = try JSONSerialization.jsonObject(with: Data(raw.utf8)) as? [String: Any] else {
|
|
240
|
+
throw RNWireError.badPacket
|
|
241
|
+
}
|
|
242
|
+
if let failure = reply["error"] as? [String: Any] {
|
|
243
|
+
throw FfiError.Failure(code: "storage_error", message: failure["detail"] as? String ?? "")
|
|
244
|
+
}
|
|
245
|
+
guard let value = reply["value"] as? [String: Any] else { throw RNWireError.unavailable }
|
|
246
|
+
var entries: [PalBlobEntry] = []
|
|
247
|
+
if operation == "list" {
|
|
248
|
+
guard let rows = value["entries"] as? [[String: Any]] else { throw RNWireError.badPacket }
|
|
249
|
+
entries = try rows.map { row in
|
|
250
|
+
guard let key = row["key"] as? String, let size = row["size"] as? UInt64 else { throw RNWireError.badPacket }
|
|
251
|
+
return PalBlobEntry(key: key, size: size)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
let result: Data?
|
|
255
|
+
if operation == "get", !(value["bytes"] is NSNull) {
|
|
256
|
+
guard let bytes = value["bytes"] as? [UInt8] else { throw RNWireError.badPacket }
|
|
257
|
+
result = Data(bytes)
|
|
258
|
+
} else { result = nil }
|
|
259
|
+
return PalBlobReply(bytes: result, entries: entries, nextCursor: value["nextCursor"] as? String)
|
|
260
|
+
} catch let error as FfiError { throw error }
|
|
261
|
+
catch { throw FfiError.Failure(code: "storage_error", message: "Invalid blob transport reply") }
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/// The encoded image implementation selected by the shared facade, with the native cancel token.
|
|
266
|
+
final class RNImageCallback: @unchecked Sendable {
|
|
267
|
+
let id: String, support: String?
|
|
268
|
+
let callbacks: RNCoreCallbacks, handles: RNCoreHandles
|
|
269
|
+
init(_ id: String, support: Any?, callbacks: RNCoreCallbacks, handles: RNCoreHandles) throws {
|
|
270
|
+
self.id = id; self.callbacks = callbacks; self.handles = handles
|
|
271
|
+
self.support = support == nil || support is NSNull ? nil : try rnJSON(support!)
|
|
272
|
+
}
|
|
273
|
+
func call(_ operation: String, _ bytes: Data, _ options: String, _ cancel: CancelToken) throws -> PalImageReply {
|
|
274
|
+
let token = handles.hold(cancel)
|
|
275
|
+
cancel.onCancel(callback: RNCancelNotice(token, callbacks))
|
|
107
276
|
defer { handles.forget(token) }
|
|
108
|
-
|
|
109
|
-
|
|
277
|
+
let reply = try callbacks.invoke(["type":"image", "host":id, "operation":operation, "bytes":Array(bytes),
|
|
278
|
+
"options":JSONSerialization.jsonObject(with: Data(options.utf8)), "cancel":token])
|
|
279
|
+
guard let packet = try JSONSerialization.jsonObject(with: Data(reply.utf8)) as? [String: Any] else { throw RNWireError.badPacket }
|
|
280
|
+
if let error = packet["error"] as? [String: Any] {
|
|
281
|
+
throw CorePalError.Failure(code: error["code"] as? String ?? "image_unsupported", detail: error["message"] as? String ?? "Image adapter failed")
|
|
282
|
+
}
|
|
283
|
+
guard let value = packet["value"] as? [String: Any] else { throw RNWireError.unavailable }
|
|
284
|
+
return PalImageReply(metadataJson: try rnJSON(value["info"] ?? [:]), bytes: Data((value["bytes"] as? [NSNumber] ?? []).map { $0.uint8Value }))
|
|
110
285
|
}
|
|
111
286
|
}
|
|
@@ -20,7 +20,7 @@ public final class CrocheRuntimeBridge: NSObject, @unchecked Sendable {
|
|
|
20
20
|
@objc public func coreCall(_ handle: String, method: String, args: String, images: [[String: Any]], resolve: @escaping Resolve, reject: @escaping Reject) {
|
|
21
21
|
DispatchQueue.global(qos: .userInitiated).async { [self] in
|
|
22
22
|
do {
|
|
23
|
-
let images = images.map {
|
|
23
|
+
let images = images.map { CoreAttachmentInput(element: ($0["element"] as! NSNumber).uint32Value,
|
|
24
24
|
bytes: Data(($0["bytes"] as! [NSNumber]).map { $0.uint8Value })) }
|
|
25
25
|
resolve(try envelope { try core.call(handle, method, rnArgs(args), images) })
|
|
26
26
|
} catch { reject("native_transport", String(describing: error), error) }
|
package/ios/ProjectPal.swift
CHANGED
|
@@ -4,15 +4,23 @@ import Foundation
|
|
|
4
4
|
/// S6. RN must never install a competing BGTaskScheduler launch handler.
|
|
5
5
|
final class RNProjectPal: HostPal, @unchecked Sendable {
|
|
6
6
|
private let base: any HostPal
|
|
7
|
-
|
|
7
|
+
private let image: RNImageCallback?
|
|
8
|
+
private let blobs: RNBlobCallback?
|
|
9
|
+
init(_ base: any HostPal, image: RNImageCallback? = nil, blobs: RNBlobCallback? = nil) {
|
|
10
|
+
self.base = base; self.image = image; self.blobs = blobs
|
|
11
|
+
}
|
|
12
|
+
func blobOperation(operation: String, key: String, cursor: String?, bytes: Data?) throws -> PalBlobReply {
|
|
13
|
+
if let blobs { return try blobs.call(operation, key, cursor, bytes) }
|
|
14
|
+
return try base.blobOperation(operation: operation, key: key, cursor: cursor, bytes: bytes)
|
|
15
|
+
}
|
|
8
16
|
func streamPlatform() -> HostStreamPlatform { base.streamPlatform() }
|
|
9
|
-
func
|
|
17
|
+
func imageSupport() -> String? { if let image { return image.support }; return base.imageSupport() }
|
|
18
|
+
func imageOperation(operation: String, bytes: Data, optionsJson: String, cancel: CancelToken) throws -> PalImageReply {
|
|
19
|
+
if let image { return try image.call(operation, bytes, optionsJson, cancel) }
|
|
20
|
+
return try base.imageOperation(operation: operation, bytes: bytes, optionsJson: optionsJson, cancel: cancel)
|
|
21
|
+
}
|
|
10
22
|
func readImage(request: PalImageReadRequest, cancel: CancelToken) throws -> Data { try base.readImage(request: request, cancel: cancel) }
|
|
11
|
-
func prepareImage(source: PalImageBytes, plan: PalImagePreparePlan, cancel: CancelToken) throws -> PalImageBytes { try base.prepareImage(source: source, plan: plan, cancel: cancel) }
|
|
12
23
|
func releaseImage(capture: String) { base.releaseImage(capture: capture) }
|
|
13
|
-
func decodeImage(bytes: Data, options: PalImageDecodeOptions, cancel: CancelToken) throws -> PalDecodedImage { try base.decodeImage(bytes: bytes, options: options, cancel: cancel) }
|
|
14
|
-
func renderImage(request: PalImageRenderRequest, cancel: CancelToken) throws -> Data { try base.renderImage(request: request, cancel: cancel) }
|
|
15
|
-
func releaseDecodedImage(handle: String) { base.releaseDecodedImage(handle: handle) }
|
|
16
24
|
func http(request: PalHttpRequest, sink: HttpSink, cancel: CancelToken) throws { try base.http(request: request, sink: sink, cancel: cancel) }
|
|
17
25
|
func resolve(host: String, cancel: CancelToken) throws -> [String] { try base.resolve(host: host, cancel: cancel) }
|
|
18
26
|
func platformFacts() -> PalPlatformFacts { base.platformFacts() }
|
|
@@ -23,6 +31,7 @@ final class RNProjectPal: HostPal, @unchecked Sendable {
|
|
|
23
31
|
func readSecret(key: String) throws -> String? { try base.readSecret(key: key) }
|
|
24
32
|
func writeSecret(key: String, value: String) throws { try base.writeSecret(key: key, value: value) }
|
|
25
33
|
func remove(key: String) throws { try base.remove(key: key) }
|
|
34
|
+
func purgeInstance(prefix: String) throws { try base.purgeInstance(prefix: prefix) }
|
|
26
35
|
func removeSecret(key: String) throws { try base.removeSecret(key: key) }
|
|
27
36
|
func lock(key: String) throws -> PalLockOutcome { try base.lock(key: key) }
|
|
28
37
|
func unlock(key: String) throws { try base.unlock(key: key) }
|
|
@@ -37,10 +46,4 @@ final class RNProjectPal: HostPal, @unchecked Sendable {
|
|
|
37
46
|
func registerBackground(identifier: String, wake: CoreBackgroundWake) throws {
|
|
38
47
|
try base.registerBackground(identifier: identifier, wake: wake)
|
|
39
48
|
}
|
|
40
|
-
func archiveAppendLine(entry: String, line: String) throws { try base.archiveAppendLine(entry: entry, line: line) }
|
|
41
|
-
func archiveReadLines(entry: String) throws -> [String] { try base.archiveReadLines(entry: entry) }
|
|
42
|
-
func archiveReplaceLines(entry: String, lines: [String]) throws { try base.archiveReplaceLines(entry: entry, lines: lines) }
|
|
43
|
-
func archiveRemove(entry: String) throws { try base.archiveRemove(entry: entry) }
|
|
44
|
-
func archiveEntries(prefix: String) throws -> [ArchiveEntry] { try base.archiveEntries(prefix: prefix) }
|
|
45
|
-
func storageCapability() -> StorageCapability { base.storageCapability() }
|
|
46
49
|
}
|