@h1r-ai/croche-react-native 0.5.4 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -458
- package/THIRD-PARTY-NOTICES +7 -8
- package/android/build.gradle +6 -1
- package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +125 -0
- package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +81 -0
- package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +83 -0
- package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +45 -824
- package/android/src/main/java/dev/croche/reactnative/NativePal.kt +51 -0
- package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
- package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +42 -41
- package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +71 -0
- package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +29 -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/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +6 -6
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +51 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -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/WorkManagerScheduler.kt +8 -8
- 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 +121 -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/AndroidStoragePal.kt +73 -0
- package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +9 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +5 -2
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +45 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +16 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +60 -105
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +47 -107
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +41 -40
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +67 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +20 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +18 -20
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +322 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +71 -34
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +9 -7
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +47 -19
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +5 -5
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +1 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +9 -9
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +108 -37
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +44 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +40 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +8 -8
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StoragePal.kt +18 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +50 -25
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +22 -22
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +8285 -2159
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +25 -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 +31 -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 +128 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +72 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +34 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +45 -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 +34 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +79 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -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/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 +3 -0
- package/dist/react-native/src/core-native.js +193 -0
- package/dist/react-native/src/core-wire.d.ts +38 -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/agent.d.ts +43 -0
- package/dist/runtime/src/agent.js +75 -0
- package/dist/runtime/src/app-api.d.ts +284 -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 +24 -0
- package/dist/runtime/src/core/port.d.ts +234 -0
- package/dist/runtime/src/core/port.js +1 -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 +42 -0
- package/dist/runtime/src/croche.js +111 -0
- package/dist/runtime/src/errors.d.ts +26 -0
- package/dist/runtime/src/errors.js +49 -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 +174 -0
- package/dist/runtime/src/generated/app-stream.js +124 -0
- package/dist/runtime/src/index.native.d.ts +21 -0
- package/dist/runtime/src/index.native.js +8 -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 +24 -0
- package/dist/runtime/src/pal/codec.d.ts +15 -0
- package/dist/runtime/src/pal/codec.js +52 -0
- package/dist/runtime/src/pal/host.d.ts +5 -0
- package/dist/runtime/src/pal/host.js +35 -0
- package/dist/runtime/src/pal/platform.d.ts +16 -0
- package/dist/runtime/src/pal/platform.js +7 -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 +19 -0
- package/dist/runtime/src/tools.js +70 -0
- package/ios/CoreAgentTransport.swift +14 -0
- package/ios/CoreDeliveryTransport.swift +58 -0
- package/ios/CoreNativePal.swift +93 -0
- package/ios/CorePlatformTransport.swift +44 -0
- package/ios/CoreWireDispatch.swift +92 -0
- package/ios/CoreWireSupport.swift +111 -0
- package/ios/CrocheRuntimeBridge.swift +34 -994
- package/ios/ProjectPal.swift +46 -0
- package/ios/RNCrocheRuntime.mm +18 -201
- package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
- package/ios/vendor/CrocheRuntimeSDK/Background.swift +11 -11
- package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +64 -0
- package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
- package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
- package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +85 -43
- package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +443 -0
- package/ios/vendor/CrocheRuntimeSDK/Host.swift +122 -61
- package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
- package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
- package/ios/vendor/CrocheRuntimeSDK/JSON.swift +4 -4
- package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
- package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +37 -36
- package/ios/vendor/CrocheRuntimeSDK/Platform.swift +35 -35
- package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +285 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +266 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +195 -0
- package/ios/vendor/CrocheRuntimeSDK/Project.swift +209 -95
- package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +97 -123
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +195 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +161 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +234 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +28 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +177 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +61 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +95 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +105 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +209 -0
- package/ios/vendor/CrocheRuntimeSDK/Storage.swift +52 -52
- package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +180 -137
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +16 -10
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +131 -116
- package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
- package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +7209 -2803
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +1342 -218
- 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 +1342 -218
- 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/vendor-native.sh +5 -4
- 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/core/dev/croche/Control.kt +0 -178
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
- package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
- 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,193 @@
|
|
|
1
|
+
import { CrocheError, thrownFacts } from '../../runtime/src/errors';
|
|
2
|
+
function answer(json) {
|
|
3
|
+
const result = JSON.parse(json);
|
|
4
|
+
if ('error' in result) {
|
|
5
|
+
const { code, message, category, path, retryAfter } = result.error;
|
|
6
|
+
const error = new CrocheError(code, message, category, retryAfter);
|
|
7
|
+
if (path !== undefined)
|
|
8
|
+
Object.assign(error, { path });
|
|
9
|
+
throw error;
|
|
10
|
+
}
|
|
11
|
+
return result.value;
|
|
12
|
+
}
|
|
13
|
+
export function createNativeCore(wire, defaultStorage) {
|
|
14
|
+
let next = 0;
|
|
15
|
+
const hosts = new Map(), storages = new Map(), sinks = new Map();
|
|
16
|
+
const executors = new Map(), tokens = new Map();
|
|
17
|
+
const ids = new WeakMap();
|
|
18
|
+
const references = new Map();
|
|
19
|
+
let sweeping = false;
|
|
20
|
+
const release = (id) => { try {
|
|
21
|
+
wire.callSync(id, 'release', '[]');
|
|
22
|
+
}
|
|
23
|
+
catch { /* module teardown */ } };
|
|
24
|
+
function sweep() {
|
|
25
|
+
if (sweeping)
|
|
26
|
+
return;
|
|
27
|
+
sweeping = true;
|
|
28
|
+
try {
|
|
29
|
+
for (const [id, record] of references) {
|
|
30
|
+
if (record.ref.deref())
|
|
31
|
+
continue;
|
|
32
|
+
references.delete(id);
|
|
33
|
+
tokens.delete(id);
|
|
34
|
+
if (record.closed || !record.mode) {
|
|
35
|
+
release(id);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (record.mode === 'sync') {
|
|
39
|
+
try {
|
|
40
|
+
wire.callSync(id, 'shutdown', '[]');
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
release(id);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else
|
|
47
|
+
void wire.call(id, 'shutdown', '[]').then(() => release(id), () => release(id));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
sweeping = false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function closed(id) { const record = references.get(id); if (record)
|
|
55
|
+
record.closed = true; }
|
|
56
|
+
const name = (kind) => `${kind}:${++next}`;
|
|
57
|
+
const sync = (id, method, args = []) => { sweep(); const value = answer(wire.callSync(id, method, JSON.stringify(args))); if (method === 'shutdown')
|
|
58
|
+
closed(id); return value; };
|
|
59
|
+
const call = async (id, method, args = [], images) => { sweep(); const value = answer(await wire.call(id, method, JSON.stringify(args), images)); if (method === 'shutdown')
|
|
60
|
+
closed(id); return value; };
|
|
61
|
+
const remember = (id, object, mode) => { ids.set(object, id); references.set(id, { ref: new WeakRef(object), mode, closed: false }); return object; };
|
|
62
|
+
const handle = (object) => {
|
|
63
|
+
const id = ids.get(object);
|
|
64
|
+
if (id === undefined)
|
|
65
|
+
throw Error('Foreign native Core handle');
|
|
66
|
+
return id;
|
|
67
|
+
};
|
|
68
|
+
class NativeCancel {
|
|
69
|
+
id;
|
|
70
|
+
listeners = [];
|
|
71
|
+
constructor(id) {
|
|
72
|
+
this.id = id;
|
|
73
|
+
tokens.set(id, new WeakRef(this));
|
|
74
|
+
ids.set(this, id);
|
|
75
|
+
references.set(id, { ref: new WeakRef(this), closed: false });
|
|
76
|
+
}
|
|
77
|
+
cancel() { sync(this.id, 'cancel'); }
|
|
78
|
+
isCancelled() { return sync(this.id, 'isCancelled'); }
|
|
79
|
+
onCancel(callback) { this.listeners.push(callback); }
|
|
80
|
+
}
|
|
81
|
+
const token = (id) => tokens.get(id)?.deref() ?? new NativeCancel(id);
|
|
82
|
+
const sinkId = (sink) => { const id = name('sink'); sinks.set(id, sink); return id; };
|
|
83
|
+
const hostId = (host) => { const id = name('host'); hosts.set(id, host); return id; };
|
|
84
|
+
const storageId = (storage) => { const id = name('storage'); storages.set(id, storage); return id; };
|
|
85
|
+
const registrations = (tools) => tools.map(({ tool, optionsJson }) => ({ tool: handle(tool), ...(optionsJson === undefined ? {} : { optionsJson }) }));
|
|
86
|
+
const subscription = (id) => remember(id, {
|
|
87
|
+
history: options => call(id, 'history', [options]),
|
|
88
|
+
shutdown: () => { sync(id, 'shutdown'); }, get ended() { return sync(id, 'ended'); },
|
|
89
|
+
}, 'sync');
|
|
90
|
+
const agent = (id) => remember(id, {
|
|
91
|
+
get id() { return sync(id, 'id'); }, get instance() { return sync(id, 'instance'); },
|
|
92
|
+
send: (input, images, options, cancel) => call(id, 'send', [input, options, handle(cancel)], images),
|
|
93
|
+
request: (input, images, options, cancel) => call(id, 'request', [input, options, handle(cancel)], images),
|
|
94
|
+
subscribe: (selection, sink) => subscription(sync(id, 'subscribe', [selection, sinkId(sink)])),
|
|
95
|
+
cancel: input => call(id, 'cancel', [input]), clearHistory: () => call(id, 'clearHistory'),
|
|
96
|
+
memoryList: () => call(id, 'memoryList'), memoryDelete: key => call(id, 'memoryDelete', [key]),
|
|
97
|
+
memoryClear: () => call(id, 'memoryClear'), memoryEnabled: on => call(id, 'memoryEnabled', [on]),
|
|
98
|
+
setTools: scope => call(id, 'setTools', [scope]), resetTools: () => call(id, 'resetTools'),
|
|
99
|
+
setWiki: scope => call(id, 'setWiki', [scope]), resetWiki: () => call(id, 'resetWiki'),
|
|
100
|
+
shutdown: () => call(id, 'shutdown'),
|
|
101
|
+
}, 'async');
|
|
102
|
+
const croche = (id) => remember(id, {
|
|
103
|
+
runTool: (name, args, options, cancel) => call(id, 'runTool', [name, args, options, handle(cancel)]),
|
|
104
|
+
deleteData: scope => call(id, 'deleteData', [scope]),
|
|
105
|
+
agent: async (key, options) => agent(await call(id, 'agent', [key, options])),
|
|
106
|
+
agentIds: () => sync(id, 'agentIds'),
|
|
107
|
+
deliverDue: (options, cancel) => call(id, 'deliverDue', [options, handle(cancel)]),
|
|
108
|
+
wikiPut: doc => call(id, 'wikiPut', [doc]), wikiRemove: path => call(id, 'wikiRemove', [path]),
|
|
109
|
+
wikiList: () => call(id, 'wikiList'),
|
|
110
|
+
debugAttach: options => call(id, 'debugAttach', [options]),
|
|
111
|
+
debugDetach: () => call(id, 'debugDetach'),
|
|
112
|
+
shutdown: () => call(id, 'shutdown'),
|
|
113
|
+
}, 'async');
|
|
114
|
+
const definition = (id) => remember(id, { get name() { return sync(id, 'name'); } });
|
|
115
|
+
// No SDK event queue: this runs on the JS invocation stack. The SAME shared
|
|
116
|
+
// sink checks entry immediately before the app call and completes after finish.
|
|
117
|
+
function receive(packet) {
|
|
118
|
+
switch (packet.type) {
|
|
119
|
+
case 'event': {
|
|
120
|
+
const delivery = { tryEnter: () => sync(packet.delivery, 'tryEnter'), complete: json => { sync(packet.delivery, 'complete', [json]); } };
|
|
121
|
+
const sink = sinks.get(packet.sink);
|
|
122
|
+
if (sink)
|
|
123
|
+
sink(packet.id, packet.eventJson, delivery);
|
|
124
|
+
else
|
|
125
|
+
delivery.complete('{"outcome":"unavailable"}');
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
case 'execute': {
|
|
129
|
+
const execute = executors.get(packet.execute);
|
|
130
|
+
if (!execute) {
|
|
131
|
+
wire.reply(packet.call, '{"type":"unavailable"}');
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// CoreExecute already yields the exact completion JSON. No re-mapping,
|
|
135
|
+
// JSON wrapping, error codes or result inference at this boundary.
|
|
136
|
+
void execute(packet.callJson, packet.contextJson, token(packet.cancel)).then(json => wire.reply(packet.call, json), () => wire.reply(packet.call, '{"type":"unavailable"}'));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
case 'cancel':
|
|
140
|
+
for (const listener of tokens.get(packet.token)?.deref()?.listeners ?? [])
|
|
141
|
+
listener();
|
|
142
|
+
return;
|
|
143
|
+
case 'storage': {
|
|
144
|
+
const storage = storages.get(packet.storage);
|
|
145
|
+
if (!storage) {
|
|
146
|
+
wire.reply(packet.call, '{"unavailable":true}');
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const secrets = { readSecret: 'read', writeSecret: 'write', removeSecret: 'remove' };
|
|
150
|
+
const owner = packet.method in secrets ? storage.secrets : storage;
|
|
151
|
+
const key = packet.method in secrets ? secrets[packet.method] : packet.method;
|
|
152
|
+
const method = owner[key];
|
|
153
|
+
void Promise.resolve().then(() => method.apply(owner, packet.args)).then(value => wire.reply(packet.call, JSON.stringify({ value: value ?? null })),
|
|
154
|
+
// Storage carries detail only. Core assigns storage_error; codes and categories never cross.
|
|
155
|
+
error => wire.reply(packet.call, JSON.stringify({ error: { detail: thrownFacts(error).message ?? '' } })));
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
case 'retired':
|
|
159
|
+
for (const id of packet.hosts)
|
|
160
|
+
hosts.delete(id);
|
|
161
|
+
for (const id of packet.storages)
|
|
162
|
+
storages.delete(id);
|
|
163
|
+
for (const id of packet.sinks)
|
|
164
|
+
sinks.delete(id);
|
|
165
|
+
for (const id of packet.executors)
|
|
166
|
+
executors.delete(id);
|
|
167
|
+
for (const id of packet.tokens)
|
|
168
|
+
tokens.delete(id);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
wire.listen(receive);
|
|
172
|
+
return {
|
|
173
|
+
newCancel: () => token(sync('', 'newCancel')),
|
|
174
|
+
open: async (json, tools, storage, host, recovery) => croche(await call('', 'open', [json, registrations(tools), storageId(storage), hostId(host), recovery])),
|
|
175
|
+
wakeTimes: (json, query, storage, host) => call('', 'wakeTimes', [json, query, storageId(storage), hostId(host)]),
|
|
176
|
+
registerBackground: (json, sink, storage, platformStorage, host) => {
|
|
177
|
+
const markerStorage = platformStorage ?? (storage === defaultStorage ? undefined : defaultStorage);
|
|
178
|
+
sync('', 'registerBackground', [json, sink && sinkId(sink), storageId(storage), hostId(host), markerStorage && storageId(markerStorage)]);
|
|
179
|
+
},
|
|
180
|
+
defineTool: (manifest, execute) => {
|
|
181
|
+
const id = name('execute');
|
|
182
|
+
executors.set(id, execute);
|
|
183
|
+
try {
|
|
184
|
+
return definition(sync('', 'defineTool', [manifest, id]));
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
executors.delete(id);
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
webSearch: () => definition(sync('', 'webSearch')),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { CoreImageInput } from '../../runtime/src/core/port';
|
|
2
|
+
export interface NativeCoreWire {
|
|
3
|
+
call(handle: string, method: string, argsJson: string, images?: readonly CoreImageInput[]): Promise<string>;
|
|
4
|
+
callSync(handle: string, method: string, argsJson: string): string;
|
|
5
|
+
reply(call: string, completionJson: string): void;
|
|
6
|
+
listen(receive: (packet: NativeCorePacket) => void): () => void;
|
|
7
|
+
}
|
|
8
|
+
export type NativeCorePacket = {
|
|
9
|
+
type: 'event';
|
|
10
|
+
sink: string;
|
|
11
|
+
id: string;
|
|
12
|
+
eventJson: string;
|
|
13
|
+
delivery: string;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'execute';
|
|
16
|
+
execute: string;
|
|
17
|
+
call: string;
|
|
18
|
+
callJson: string;
|
|
19
|
+
contextJson: string;
|
|
20
|
+
cancel: string;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'cancel';
|
|
23
|
+
token: string;
|
|
24
|
+
} | {
|
|
25
|
+
type: 'storage';
|
|
26
|
+
storage: string;
|
|
27
|
+
call: string;
|
|
28
|
+
method: StorageMethod;
|
|
29
|
+
args: unknown[];
|
|
30
|
+
} | {
|
|
31
|
+
type: 'retired';
|
|
32
|
+
hosts: string[];
|
|
33
|
+
storages: string[];
|
|
34
|
+
sinks: string[];
|
|
35
|
+
executors: string[];
|
|
36
|
+
tokens: string[];
|
|
37
|
+
};
|
|
38
|
+
export type StorageMethod = 'read' | 'write' | 'remove' | 'list' | 'lock' | 'unlock' | 'readSecret' | 'writeSecret' | 'removeSecret';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CoreHttpRequest, CoreBodySink, CoreCancel } from '../../runtime/src/core/port.js';
|
|
2
|
+
import type { NativeCoreWire } from './core-wire.js';
|
|
3
|
+
export declare const nativeWire: NativeCoreWire;
|
|
4
|
+
export declare function unpack<T>(json: string): T;
|
|
5
|
+
export declare const nativeCall: <T>(method: string, args?: unknown[]) => Promise<T>;
|
|
6
|
+
export declare const nativeSync: <T>(method: string, args?: unknown[]) => T;
|
|
7
|
+
export declare function nativeHTTP(request: CoreHttpRequest, sink: CoreBodySink, cancel: CoreCancel): Promise<void>;
|
|
8
|
+
export declare const nativeStorageCall: <T>(method: string, args?: unknown[]) => Promise<T>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// The native module forwards only the private Core wire; no legacy runtime API.
|
|
2
|
+
import { NativeEventEmitter } from 'react-native';
|
|
3
|
+
import Native from './NativeCrocheRuntime.js';
|
|
4
|
+
const emitter = new NativeEventEmitter(Native);
|
|
5
|
+
export const nativeWire = {
|
|
6
|
+
call: (handle, method, args, images) => Native.coreCall(handle, method, args, (images ?? []).map(image => ({ element: image.element, bytes: Array.from(image.bytes) }))),
|
|
7
|
+
callSync: (handle, method, args) => Native.coreCallSync(handle, method, args),
|
|
8
|
+
reply: (call, completion) => Native.coreReply(call, completion),
|
|
9
|
+
listen(receive) { const sub = emitter.addListener('CrocheCoreEvent', (json) => receive(JSON.parse(json))); return () => sub.remove(); },
|
|
10
|
+
};
|
|
11
|
+
// PAL replies carry data or the exact native fact/error envelope. Storage failures
|
|
12
|
+
// remain plain Errors: Core assigns storage_error; no SDK mapping or fallback.
|
|
13
|
+
export function unpack(json) {
|
|
14
|
+
const result = JSON.parse(json);
|
|
15
|
+
if ('error' in result) {
|
|
16
|
+
const error = new Error(result.error.detail);
|
|
17
|
+
if (result.error.code !== undefined)
|
|
18
|
+
Object.assign(error, { code: result.error.code });
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
return result.value;
|
|
22
|
+
}
|
|
23
|
+
export const nativeCall = async (method, args = []) => unpack(await Native.platformCall(method, JSON.stringify(args)));
|
|
24
|
+
export const nativeSync = (method, args = []) => unpack(Native.platformCallSync(method, JSON.stringify(args)));
|
|
25
|
+
let nextHttp = 0;
|
|
26
|
+
export async function nativeHTTP(request, sink, cancel) {
|
|
27
|
+
const id = `http:${++nextHttp}`;
|
|
28
|
+
let active = true;
|
|
29
|
+
const listener = emitter.addListener('CrochePalEvent', (json) => {
|
|
30
|
+
const packet = JSON.parse(json);
|
|
31
|
+
if (packet.id !== id)
|
|
32
|
+
return;
|
|
33
|
+
if (packet.type === 'head')
|
|
34
|
+
sink.head(packet.status, packet.headers);
|
|
35
|
+
else if (packet.type === 'chunk' && !sink.chunk(new Uint8Array(packet.bytes)))
|
|
36
|
+
nativeSync('cancelHttp', [id]);
|
|
37
|
+
});
|
|
38
|
+
cancel.onCancel(() => { if (active)
|
|
39
|
+
nativeSync('cancelHttp', [id]); });
|
|
40
|
+
try {
|
|
41
|
+
await nativeCall('http', [id, request]);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
active = false;
|
|
45
|
+
listener.remove();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export const nativeStorageCall = async (method, args = []) => {
|
|
49
|
+
const result = JSON.parse(await Native.platformCall(method, JSON.stringify(args)));
|
|
50
|
+
if ('error' in result)
|
|
51
|
+
throw new Error(result.error.detail);
|
|
52
|
+
return result.value;
|
|
53
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"entry": "runtime/src/rn-entry.js",
|
|
3
|
+
"adapter": "packages/react-native/src/shared-adapter.ts",
|
|
4
|
+
"sharedSources": {
|
|
5
|
+
"index.native.ts": "547a67537afc3779155b72908ff258d79c6942190e8e8dda889cc36d65c85688",
|
|
6
|
+
"croche.ts": "1199107945e28cec9df5d036a0db0fac2edc6cb176b784683b3580351ce22b09",
|
|
7
|
+
"agent.ts": "111db2caf774094da0fb3c8292ec0ee2beb8a1068f258ee3532ccb3986fdf7d0",
|
|
8
|
+
"tools.ts": "5225d7bd5b5a970a8c8aac38bbf650b079c99ac083140c549c0057de24423b7d",
|
|
9
|
+
"app-api.ts": "d5c718aa3ed87f2167f12ed42f273e1d9c3302e721a5f89dfa83d08f74b2510d",
|
|
10
|
+
"json.ts": "b46f716b14c7db7945e6e9291736f1594e7bd141dcb48dd0823bc34e0500255c",
|
|
11
|
+
"errors.ts": "9f2dfdc12d4a4e689e90c39544747cf8014d6e9a16b31d17e422bbec4df494cb",
|
|
12
|
+
"cancel.ts": "a0bfb67aab8543b19790da28f0b0014c3f77009bf5ab376d636aec9dd785cd93",
|
|
13
|
+
"core/port.ts": "686f27c508782b9d8fa66076989d849a871f13e1478c3ff3d32a35a3880db3fb"
|
|
14
|
+
},
|
|
15
|
+
"wasm": false
|
|
16
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { DecideInput, DecisionResult, MemoryItem, RequestResult, SendInput, SendOptions, SendReceipt, SubscribeOptions, Subscription, ToolScope, WikiScope } from './app-api.js';
|
|
2
|
+
import type { CoreAgent, CoreModule } from './core/port.js';
|
|
3
|
+
/** Core's handle for an Agent (the debugger takes it); undefined for anything else. */
|
|
4
|
+
export declare function coreAgent(agent: unknown): CoreAgent | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* The one way to make an Agent: `croche.agent()` wraps the handle Core returned. Reflect.construct
|
|
7
|
+
* keeps the constructor private to TypeScript without an ES2022 static initialization block, which the React
|
|
8
|
+
* Native 0.87 Babel preset cannot parse in an app's Metro bundle (0.6.1 P12).
|
|
9
|
+
*/
|
|
10
|
+
export declare const wrapAgent: (module: CoreModule, core: CoreAgent) => Agent;
|
|
11
|
+
/** Available through the web/Node entry; RN omits it until the native bridge is proven. */
|
|
12
|
+
export interface Agent {
|
|
13
|
+
decide(input: DecideInput, options?: {
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
}): Promise<DecisionResult>;
|
|
16
|
+
}
|
|
17
|
+
export declare class Agent {
|
|
18
|
+
#private;
|
|
19
|
+
private constructor();
|
|
20
|
+
get id(): string;
|
|
21
|
+
get instance(): string | null;
|
|
22
|
+
/** Resolves when Core has ACCEPTED the input. The signal is sugar for cancel(id): before acceptance = never queued. */
|
|
23
|
+
send(input: SendInput, options?: SendOptions): Promise<SendReceipt>;
|
|
24
|
+
/** Resolves when the input is final; with wait: 'idle', when the agent is idle too. */
|
|
25
|
+
request(input: SendInput, options?: SendOptions & {
|
|
26
|
+
wait?: 'idle';
|
|
27
|
+
}): Promise<RequestResult>;
|
|
28
|
+
/** Events arrive in Core's order; an unknown type passes through unchanged. */
|
|
29
|
+
subscribe(options: SubscribeOptions): Subscription;
|
|
30
|
+
cancel(id?: string): Promise<void>;
|
|
31
|
+
clearHistory(): Promise<void>;
|
|
32
|
+
readonly memory: {
|
|
33
|
+
list: () => Promise<MemoryItem[]>;
|
|
34
|
+
delete: (id: string) => Promise<void>;
|
|
35
|
+
clear: () => Promise<void>;
|
|
36
|
+
enabled: (on: boolean) => Promise<void>;
|
|
37
|
+
};
|
|
38
|
+
setTools(scope: ToolScope | 'all'): Promise<void>;
|
|
39
|
+
resetTools(): Promise<void>;
|
|
40
|
+
setWiki(scope: WikiScope | 'all'): Promise<void>;
|
|
41
|
+
resetWiki(): Promise<void>;
|
|
42
|
+
close(): Promise<void>;
|
|
43
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { tokenFor } from './cancel.js';
|
|
2
|
+
import { eventSink } from './events.js';
|
|
3
|
+
import { decodeAgentEvent } from './generated/app-stream.js';
|
|
4
|
+
import { jsonText, optionsText } from './json.js';
|
|
5
|
+
const handles = new WeakMap();
|
|
6
|
+
/** Core's handle for an Agent (the debugger takes it); undefined for anything else. */
|
|
7
|
+
export function coreAgent(agent) {
|
|
8
|
+
return typeof agent === 'object' && agent !== null ? handles.get(agent) : undefined;
|
|
9
|
+
}
|
|
10
|
+
/** Splits `signal` off a method's options: it crosses as a token, the rest as JSON. */
|
|
11
|
+
function withoutSignal(options) {
|
|
12
|
+
if (options === undefined || options === null || typeof options !== 'object')
|
|
13
|
+
return [undefined, options];
|
|
14
|
+
const { signal, ...rest } = options;
|
|
15
|
+
return [signal, rest];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The one way to make an Agent: `croche.agent()` wraps the handle Core returned. Reflect.construct
|
|
19
|
+
* keeps the constructor private to TypeScript without an ES2022 static initialization block, which the React
|
|
20
|
+
* Native 0.87 Babel preset cannot parse in an app's Metro bundle (0.6.1 P12).
|
|
21
|
+
*/
|
|
22
|
+
export const wrapAgent = (module, core) => Reflect.construct(Agent, [module, core]);
|
|
23
|
+
export class Agent {
|
|
24
|
+
#core;
|
|
25
|
+
#module;
|
|
26
|
+
constructor(module, core) {
|
|
27
|
+
this.#module = module;
|
|
28
|
+
this.#core = core;
|
|
29
|
+
handles.set(this, core);
|
|
30
|
+
const decisions = module.decisions;
|
|
31
|
+
if (decisions) {
|
|
32
|
+
Object.defineProperty(this, 'decide', { value: async (input, options) => {
|
|
33
|
+
const [signal, rest] = withoutSignal(options);
|
|
34
|
+
return JSON.parse(await decisions.decide(core, jsonText(input), optionsText(rest), tokenFor(module, signal)));
|
|
35
|
+
} });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
get id() { return this.#core.id; }
|
|
39
|
+
get instance() { return this.#core.instance ?? null; }
|
|
40
|
+
/** Resolves when Core has ACCEPTED the input. The signal is sugar for cancel(id): before acceptance = never queued. */
|
|
41
|
+
async send(input, options) {
|
|
42
|
+
const [signal, rest] = withoutSignal(options);
|
|
43
|
+
const answer = await this.#core.send(jsonText(input), [], optionsText(rest), tokenFor(this.#module, signal));
|
|
44
|
+
return JSON.parse(answer);
|
|
45
|
+
}
|
|
46
|
+
/** Resolves when the input is final; with wait: 'idle', when the agent is idle too. */
|
|
47
|
+
async request(input, options) {
|
|
48
|
+
const [signal, rest] = withoutSignal(options);
|
|
49
|
+
const answer = await this.#core.request(jsonText(input), [], optionsText(rest), tokenFor(this.#module, signal));
|
|
50
|
+
return JSON.parse(answer);
|
|
51
|
+
}
|
|
52
|
+
/** Events arrive in Core's order; an unknown type passes through unchanged. */
|
|
53
|
+
subscribe(options) {
|
|
54
|
+
const { onEvent, ...selection } = options;
|
|
55
|
+
const subscription = this.#core.subscribe(optionsText(selection), eventSink(onEvent, decodeAgentEvent));
|
|
56
|
+
return {
|
|
57
|
+
history: historyOptions => subscription.history(jsonText(historyOptions)),
|
|
58
|
+
close: () => subscription.shutdown(),
|
|
59
|
+
get ended() { return subscription.ended; },
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
cancel(id) { return this.#core.cancel(id); }
|
|
63
|
+
clearHistory() { return this.#core.clearHistory(); }
|
|
64
|
+
memory = {
|
|
65
|
+
list: async () => JSON.parse(await this.#core.memoryList()),
|
|
66
|
+
delete: (id) => this.#core.memoryDelete(id),
|
|
67
|
+
clear: () => this.#core.memoryClear(),
|
|
68
|
+
enabled: (on) => this.#core.memoryEnabled(on),
|
|
69
|
+
};
|
|
70
|
+
setTools(scope) { return this.#core.setTools(jsonText(scope)); }
|
|
71
|
+
resetTools() { return this.#core.resetTools(); }
|
|
72
|
+
setWiki(scope) { return this.#core.setWiki(jsonText(scope)); }
|
|
73
|
+
resetWiki() { return this.#core.resetWiki(); }
|
|
74
|
+
close() { return this.#core.shutdown(); }
|
|
75
|
+
}
|