@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
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { jsonText } from './json.js';
|
|
2
|
+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
3
|
+
const digits = new Int16Array(128).fill(-1);
|
|
4
|
+
for (let i = 0; i < alphabet.length; i++)
|
|
5
|
+
digits[alphabet.charCodeAt(i)] = i;
|
|
6
|
+
/** Standard padded base64, matching Core's decoder. Invalid encodings stay in
|
|
7
|
+
* JSON so Core supplies the refusal; this function never chooses an app error. */
|
|
8
|
+
function bytesFromBase64(data) {
|
|
9
|
+
if (data.length % 4 !== 0)
|
|
10
|
+
return undefined;
|
|
11
|
+
const padding = data.endsWith('==') ? 2 : data.endsWith('=') ? 1 : 0;
|
|
12
|
+
const end = data.length - padding;
|
|
13
|
+
for (let i = 0; i < end; i++) {
|
|
14
|
+
const code = data.charCodeAt(i);
|
|
15
|
+
if (code >= digits.length || digits[code] < 0)
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
if (padding && (end < 2 || (digits[data.charCodeAt(end - 1)] & (padding === 2 ? 15 : 3)) !== 0))
|
|
19
|
+
return undefined;
|
|
20
|
+
const bytes = new Uint8Array(data.length / 4 * 3 - padding);
|
|
21
|
+
let bits = 0, count = 0, offset = 0;
|
|
22
|
+
for (let i = 0; i < end; i++) {
|
|
23
|
+
bits = (bits << 6) | digits[data.charCodeAt(i)];
|
|
24
|
+
count += 6;
|
|
25
|
+
if (count >= 8) {
|
|
26
|
+
count -= 8;
|
|
27
|
+
bytes[offset++] = (bits >>> count) & 255;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return bytes;
|
|
31
|
+
}
|
|
32
|
+
/** Preserve the strict serializer's refusal for non-JSON values. For JSON image
|
|
33
|
+
* elements, move only valid base64 bytes beside the JSON, indexed by position.
|
|
34
|
+
* The caller's objects and every other member are left unchanged. */
|
|
35
|
+
export function imageInput(input) {
|
|
36
|
+
const raw = jsonText(input);
|
|
37
|
+
let value;
|
|
38
|
+
try {
|
|
39
|
+
value = JSON.parse(raw);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return [raw, []];
|
|
43
|
+
}
|
|
44
|
+
if (value === null || typeof value !== 'object' || !Array.isArray(value.input))
|
|
45
|
+
return [raw, []];
|
|
46
|
+
const images = [];
|
|
47
|
+
value.input.forEach((part, element) => {
|
|
48
|
+
if (part === null || typeof part !== 'object' || Array.isArray(part))
|
|
49
|
+
return;
|
|
50
|
+
const item = part;
|
|
51
|
+
if (item.type !== 'image' || typeof item.data !== 'string' || Object.hasOwn(item, 'uri'))
|
|
52
|
+
return;
|
|
53
|
+
const bytes = bytesFromBase64(item.data);
|
|
54
|
+
if (bytes === undefined)
|
|
55
|
+
return;
|
|
56
|
+
images.push({ element, bytes });
|
|
57
|
+
delete item.data;
|
|
58
|
+
});
|
|
59
|
+
return images.length ? [JSON.stringify(value), images] : [raw, []];
|
|
60
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Croche } from './croche.js';
|
|
2
|
+
export { Agent } from './agent.js';
|
|
3
|
+
export { defineTool, toolResult, ToolError, tools } from './tools.js';
|
|
4
|
+
export { CrocheError } from './errors.js';
|
|
5
|
+
export type { Adapter, BlobListPage, BlobStore, ImageAdapter, ImageSupport, ImageInfo, ImageResizeLimits, StateStore, StateCommitResult, StateCondition, StateEntry, StateListOptions, StateOp, StatePage, StateValue, RunToolResult, DecideInput, DecisionQuestion, DecisionConfidence, DecisionAnswer, DecisionResult, Attachment, BackgroundOptions, CrocheDebugger, DeleteScope, DeliverDueResult, SubscribeOptions, WakeTimes, AgentEvent, AgentOptions, RequestResult, SendReceipt, EventError, BackgroundEvent, DebugAttachOptions, AttachedDebugger, DebugCapability, ErrorCategory, ImageInput, InputDisposition, Json, ListOptions, MemoryItem, MessageEnd, OpenOptions, Page, PlatformHandle, ResponseFormat, SendInput, SendOptions, Subscription, TextInput, ToolArg, ToolCall, ToolContent, ToolContext, ToolDefinition, ToolEntryOptions, ToolExecute, ToolManifest, ToolRegistration, ToolResult, ToolScope, Verification, WikiDoc, WikiScope, } from './app-api.js';
|
|
6
|
+
export type { InputKind, MessageStatus, AgentStatus, TurnPurpose, MemoryOp, InputEvent, InputDropped, MessageStart, MessageDelta, TurnEvent, ToolEvent, StateEvent, SnapshotEvent, PageEvent, ClearedEvent, MemoryEvent, GapEvent, SubscriptionError, ReplacedEvent, } from './generated/app-stream.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Shared App API exports. The Node, workerd and native entries select their own PAL.
|
|
2
|
+
export { Croche } from './croche.js';
|
|
3
|
+
export { Agent } from './agent.js';
|
|
4
|
+
export { defineTool, toolResult, ToolError, tools } from './tools.js';
|
|
5
|
+
export { CrocheError } from './errors.js';
|
|
@@ -1,21 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Croche as SharedCroche } from './croche.js';
|
|
3
|
-
import type { AgentOptions, OpenOptions } from './app-api.js';
|
|
4
|
-
export type Agent = Omit<SharedAgent, 'decide'>;
|
|
5
|
-
export declare const Agent: Omit<typeof SharedAgent, "prototype"> & {
|
|
6
|
-
readonly prototype: Agent;
|
|
7
|
-
[Symbol.hasInstance](value: unknown): value is Agent;
|
|
8
|
-
};
|
|
9
|
-
export type Croche = Omit<SharedCroche, 'agent'> & {
|
|
10
|
-
agent(id: string, options?: AgentOptions): Promise<Agent>;
|
|
11
|
-
};
|
|
12
|
-
export declare const Croche: Omit<typeof SharedCroche, "open" | "prototype"> & {
|
|
13
|
-
readonly prototype: Croche;
|
|
14
|
-
[Symbol.hasInstance](value: unknown): value is Croche;
|
|
15
|
-
open(options: OpenOptions, recovery?: {
|
|
16
|
-
recover: "delete";
|
|
17
|
-
}): Promise<Croche>;
|
|
18
|
-
};
|
|
19
|
-
export { defineTool, toolResult, ToolError, tools } from './tools.js';
|
|
20
|
-
export { CrocheError } from './errors.js';
|
|
21
|
-
export type { RunToolResult, Attachment, AgentEvent, AgentOptions, RequestResult, SendReceipt, EventError, BackgroundEvent, DebugAttachOptions, AttachedDebugger, DebugCapability, ErrorCategory, ImageInput, InputDisposition, Json, MemoryItem, MessageEnd, OpenOptions, PlatformHandle, ResponseFormat, SendInput, SendOptions, StoragePal, Subscription, TextInput, ToolArg, ToolCall, ToolContent, ToolContext, ToolDefinition, ToolEntryOptions, ToolExecute, ToolManifest, ToolRegistration, ToolResult, ToolScope, WikiDoc, WikiScope, } from './app-api.js';
|
|
1
|
+
export * from './index.js';
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
import { Agent as SharedAgent } from './agent.js';
|
|
4
|
-
import { Croche as SharedCroche } from './croche.js';
|
|
5
|
-
export const Agent = SharedAgent;
|
|
6
|
-
export const Croche = SharedCroche;
|
|
7
|
-
export { defineTool, toolResult, ToolError, tools } from './tools.js';
|
|
8
|
-
export { CrocheError } from './errors.js';
|
|
1
|
+
// Native and server bindings expose the same App API; their Core transports differ.
|
|
2
|
+
export * from './index.js';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { Platform } from '../../runtime/src/pal/platform.js';
|
|
2
|
-
export declare const coreModule: () => import("
|
|
2
|
+
export declare const coreModule: () => import("../../runtime/src/core/port.js").CoreModule;
|
|
3
3
|
export declare const nativePlatform: Platform;
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
import { captureReadVersioned, captureWriteIf } from '../../runtime/src/pal/storage-version-contract.js';
|
|
1
2
|
// Compiled at the canonical facade source root; it imports the RN adapter, not
|
|
2
3
|
// another facade or the web/WASM loader. Every source file stays single-owned.
|
|
3
|
-
import { createNativeCore } from '../../react-native/src/core-native.js';
|
|
4
|
+
import { createNativeCore, createNativeImageAdapter } from '../../react-native/src/core-native.js';
|
|
4
5
|
import { nativeWire, nativeCall, nativeSync, nativeHTTP, nativeStorageCall } from '../../react-native/src/native-module.js';
|
|
5
6
|
const storage = {
|
|
7
|
+
listState: (prefix, options) => nativeStorageCall('listState', [prefix, options]),
|
|
8
|
+
commit: (ops, condition) => nativeStorageCall('commit', [ops, condition]),
|
|
9
|
+
readVersioned: key => { const request = captureReadVersioned(key); return nativeStorageCall('readVersioned', [request.key]).then(request.decode); },
|
|
10
|
+
writeIf: (key, value, expect) => { const request = captureWriteIf(key, value, expect); return nativeStorageCall('writeIf', [request.key, request.value, request.expect]).then(request.decode); },
|
|
6
11
|
read: key => nativeStorageCall('read', [key]), write: (key, value) => nativeStorageCall('write', [key, value]),
|
|
12
|
+
purgeInstance: (prefix, credentialKey) => nativeStorageCall('purgeInstance', [prefix, credentialKey ?? null]),
|
|
7
13
|
remove: key => nativeStorageCall('remove', [key]), list: prefix => nativeStorageCall('list', [prefix]),
|
|
8
14
|
lock: name => nativeStorageCall('lock', [name]), unlock: name => nativeStorageCall('unlock', [name]),
|
|
9
15
|
secrets: { read: key => nativeStorageCall('readSecret', [key]), write: (key, value) => nativeStorageCall('writeSecret', [key, value]), remove: key => nativeStorageCall('removeSecret', [key]) },
|
|
@@ -13,12 +19,12 @@ export const coreModule = () => module;
|
|
|
13
19
|
const bytes = async (method, args) => new Uint8Array(await nativeCall(method, args));
|
|
14
20
|
export const nativePlatform = {
|
|
15
21
|
defaultStorage: () => storage,
|
|
22
|
+
nativeBlobs: true,
|
|
23
|
+
credentialStorage: storage.secrets,
|
|
16
24
|
platformFacts: () => nativeSync('platformFacts'),
|
|
17
25
|
readAsset: async (path) => { const value = await nativeCall('readAsset', [path]); return value === null ? null : new Uint8Array(value); },
|
|
18
26
|
readImage: (uri, maxBytes) => bytes('readImage', [uri, maxBytes]),
|
|
19
27
|
resolve: (host) => nativeCall('resolve', [host]),
|
|
20
28
|
http: nativeHTTP,
|
|
21
|
-
|
|
22
|
-
decode: (input) => nativeCall('decode', [Array.from(input)]),
|
|
23
|
-
render: request => bytes('render', [request]), release: handle => { nativeSync('release', [handle]); } }),
|
|
29
|
+
image: () => createNativeImageAdapter(nativeWire),
|
|
24
30
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { PlatformHandle,
|
|
1
|
+
import type { BlobStore, PlatformHandle, StateStore, ImageAdapter } from '../app-api.js';
|
|
2
2
|
import type { CoreHost, CoreStoragePal } from '../core/port.js';
|
|
3
3
|
/** The app's storage, unchanged, or the platform default (Core has no default of its own). */
|
|
4
|
-
export declare function storageFor(storage:
|
|
5
|
-
export declare function createHost(handle: PlatformHandle | undefined): CoreHost;
|
|
4
|
+
export declare function storageFor(storage: StateStore | undefined): CoreStoragePal;
|
|
5
|
+
export declare function createHost(handle: PlatformHandle | undefined, suppliedImage?: ImageAdapter, suppliedBlobs?: BlobStore): CoreHost;
|
|
@@ -1,20 +1,38 @@
|
|
|
1
1
|
import { platform } from './platform.js';
|
|
2
|
+
import { CrocheError, HostError } from '../errors.js';
|
|
3
|
+
import { signalFor } from '../cancel.js';
|
|
4
|
+
import { transactionalStorage } from './transactional-storage.js';
|
|
2
5
|
/** The app's storage, unchanged, or the platform default (Core has no default of its own). */
|
|
3
6
|
export function storageFor(storage) {
|
|
4
|
-
|
|
7
|
+
const host = platform();
|
|
8
|
+
return storage === undefined
|
|
9
|
+
? host.defaultStorage()
|
|
10
|
+
: transactionalStorage(storage, host.credentialStorage);
|
|
5
11
|
}
|
|
6
|
-
export function createHost(handle) {
|
|
12
|
+
export function createHost(handle, suppliedImage, suppliedBlobs) {
|
|
7
13
|
const pal = platform();
|
|
8
|
-
const
|
|
14
|
+
const blobs = suppliedBlobs ?? pal.defaultBlobs?.();
|
|
15
|
+
if (!blobs && !pal.nativeBlobs) {
|
|
16
|
+
throw new CrocheError('invalid_configuration', 'A BlobStore is required. Pass one in adapters, e.g. a BlobStore backed by an R2 bucket on Workers.', 'setup');
|
|
17
|
+
}
|
|
18
|
+
const image = suppliedImage ?? pal.image();
|
|
19
|
+
const purgeBlobStaging = blobs && pal.blobStagingPurge?.(blobs);
|
|
9
20
|
let timer;
|
|
10
21
|
return {
|
|
22
|
+
...(blobs && {
|
|
23
|
+
blobGet: blobs.get.bind(blobs),
|
|
24
|
+
blobPut: blobs.put.bind(blobs),
|
|
25
|
+
blobDelete: blobs.delete.bind(blobs),
|
|
26
|
+
blobList: blobs.list.bind(blobs),
|
|
27
|
+
}),
|
|
28
|
+
...(purgeBlobStaging && { blobPurgeStaging: purgeBlobStaging }),
|
|
11
29
|
readAsset: path => pal.readAsset(path),
|
|
12
30
|
http: (request, sink, cancel) => pal.http(request, sink, cancel),
|
|
13
31
|
resolve: (host, cancel) => pal.resolve(host, cancel),
|
|
14
32
|
nowMs: () => Date.now(),
|
|
15
33
|
utcOffsetMinutes: atMs => -new Date(atMs).getTimezoneOffset(),
|
|
16
|
-
//
|
|
17
|
-
sleep: ms => new Promise(resolve => { setTimeout(resolve, ms).unref?.(); }),
|
|
34
|
+
// Node tracks foreground sleeps. Idle timers alone never hold the process open.
|
|
35
|
+
sleep: pal.sleep ?? (ms => new Promise(resolve => { setTimeout(resolve, ms).unref?.(); })),
|
|
18
36
|
wake(atMs, onWake) {
|
|
19
37
|
clearTimeout(timer);
|
|
20
38
|
timer = undefined;
|
|
@@ -27,9 +45,16 @@ export function createHost(handle) {
|
|
|
27
45
|
platformFacts: () => pal.platformFacts(), // the platform's own facts, locale included (absent, never defaulted)
|
|
28
46
|
platform: handle,
|
|
29
47
|
readImage: (request, cancel) => pal.readImage(request.source.uri, request.maxBytes, cancel),
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
48
|
+
imageSupport: () => image?.support,
|
|
49
|
+
async imageOperation(operation, bytes, options, cancel) {
|
|
50
|
+
if (!image)
|
|
51
|
+
throw new HostError('image_unsupported', 'No image adapter is registered.');
|
|
52
|
+
const signal = signalFor(cancel);
|
|
53
|
+
if (operation === 'info')
|
|
54
|
+
return { info: await image.info(bytes, { signal }) };
|
|
55
|
+
if (operation === 'resize')
|
|
56
|
+
return { bytes: await image.resize(bytes, options, { signal }) };
|
|
57
|
+
throw new HostError('image_unsupported', 'The image operation is not supported.');
|
|
58
|
+
},
|
|
34
59
|
};
|
|
35
60
|
}
|
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { CoreBodySink, CoreCancel, CoreHttpRequest, PlatformFacts } from '../core/port.js';
|
|
3
|
-
import type { Codec } from './codec.js';
|
|
1
|
+
import type { ImageAdapter, BlobStore } from '../app-api.js';
|
|
2
|
+
import type { CoreBodySink, CoreCancel, CoreHttpRequest, CoreStoragePal, PlatformFacts } from '../core/port.js';
|
|
4
3
|
export interface Platform {
|
|
5
4
|
/** The §2a storage this platform ships, used when the app passes none. */
|
|
6
|
-
defaultStorage():
|
|
5
|
+
defaultStorage(): CoreStoragePal;
|
|
6
|
+
credentialStorage?: CoreStoragePal['secrets'];
|
|
7
|
+
defaultBlobs?(): BlobStore;
|
|
8
|
+
/** The native transport supplies its own BlobStore when JS passes none. */
|
|
9
|
+
nativeBlobs?: true;
|
|
10
|
+
blobStagingPurge?(store: BlobStore): ((prefix: string) => Promise<void>) | undefined;
|
|
7
11
|
http(request: CoreHttpRequest, sink: CoreBodySink, cancel: CoreCancel): Promise<void>;
|
|
8
12
|
resolve(host: string, cancel: CoreCancel): Promise<string[]>;
|
|
9
13
|
readAsset(path: string): Promise<Uint8Array | null>;
|
|
10
14
|
readImage(uri: string, maxBytes: number, cancel: CoreCancel): Promise<Uint8Array>;
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
sleep?(ms: number): Promise<void>;
|
|
16
|
+
image(): ImageAdapter | undefined;
|
|
17
|
+
/** Build facts the platform knows natively (RN Android: a release build). Never inferred. */
|
|
13
18
|
platformFacts(): PlatformFacts;
|
|
14
19
|
}
|
|
15
20
|
export declare function usePlatform(platform: Platform): void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { StateStore } from './state-store.js';
|
|
2
|
+
type StoreLock = {
|
|
3
|
+
lock(name: string): Promise<'acquired' | 'held'>;
|
|
4
|
+
unlock(name: string): Promise<void>;
|
|
5
|
+
verify?(key: string): Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
export declare const builtinStateLocks: WeakMap<StateStore, StoreLock>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** Stored value together with the version used for a conditional commit. */
|
|
2
|
+
export interface StateValue {
|
|
3
|
+
/** String value returned for the key. */
|
|
4
|
+
value: string;
|
|
5
|
+
/** Opaque version for the observed value. */
|
|
6
|
+
version: string;
|
|
7
|
+
}
|
|
8
|
+
/** Key and value returned by a state range read. */
|
|
9
|
+
export interface StateEntry {
|
|
10
|
+
/** Full stored key. */
|
|
11
|
+
key: string;
|
|
12
|
+
/** String value associated with this key. */
|
|
13
|
+
value: string;
|
|
14
|
+
}
|
|
15
|
+
/** Bounds and continuation for a StateStore prefix read. */
|
|
16
|
+
export interface StateListOptions {
|
|
17
|
+
/** Inclusive lower bound on full keys within the prefix. */
|
|
18
|
+
start?: string;
|
|
19
|
+
/** Use descending UTF-8 key order. The start bound is still a lower bound. */
|
|
20
|
+
reverse?: boolean;
|
|
21
|
+
/** Positive page limit up to 128. Omit to use 128. */
|
|
22
|
+
limit?: number;
|
|
23
|
+
/** Opaque continuation from a page for the same prefix, start and direction. */
|
|
24
|
+
cursor?: string;
|
|
25
|
+
}
|
|
26
|
+
/** One bounded page of stored entries. */
|
|
27
|
+
export interface StatePage {
|
|
28
|
+
/** Entries in the order requested for the range. */
|
|
29
|
+
entries: StateEntry[];
|
|
30
|
+
/** Opaque continuation if another page is available. A short page may still have a cursor. */
|
|
31
|
+
nextCursor?: string;
|
|
32
|
+
}
|
|
33
|
+
/** One put or delete in a conditional state batch. */
|
|
34
|
+
export type StateOp = {
|
|
35
|
+
type: 'put';
|
|
36
|
+
key: string;
|
|
37
|
+
value: string;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'delete';
|
|
40
|
+
key: string;
|
|
41
|
+
};
|
|
42
|
+
/** Version check that guards an atomic state commit. */
|
|
43
|
+
export interface StateCondition {
|
|
44
|
+
/** Key checked before committing the batch. */
|
|
45
|
+
key: string;
|
|
46
|
+
/** Expected version, or null to require the key to be absent. */
|
|
47
|
+
version: string | null;
|
|
48
|
+
}
|
|
49
|
+
/** Outcome of comparing a state version and applying a write batch. */
|
|
50
|
+
export type StateCommitResult = {
|
|
51
|
+
type: 'committed';
|
|
52
|
+
version: string | null;
|
|
53
|
+
} | {
|
|
54
|
+
type: 'conflict';
|
|
55
|
+
};
|
|
56
|
+
/** Host-provided store for range reads and atomic conditional state commits. */
|
|
57
|
+
export interface StateStore {
|
|
58
|
+
/** Host adapter tag for state storage. */
|
|
59
|
+
readonly kind: 'state';
|
|
60
|
+
/** Read saved state so a user can continue where they left off. */
|
|
61
|
+
get(key: string): Promise<StateValue | null>;
|
|
62
|
+
/** Load just the relevant records so a long conversation still opens promptly. */
|
|
63
|
+
list(prefix: string, options?: StateListOptions): Promise<StatePage>;
|
|
64
|
+
/** Keep a saved reply and its related progress consistent if the app stops mid-write. */
|
|
65
|
+
commit(ops: readonly StateOp[], condition: StateCondition): Promise<StateCommitResult>;
|
|
66
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { StorageVersion, VersionedValue, WriteOutcome } from '../app-api.js';
|
|
2
|
+
export type { StorageVersion, VersionedValue, WriteOutcome } from '../app-api.js';
|
|
3
|
+
export interface VersionedStorageCallbacks {
|
|
4
|
+
readVersioned(key: string): Promise<VersionedValue>;
|
|
5
|
+
writeIf(key: string, value: string | null, expect: StorageVersion | null): Promise<WriteOutcome>;
|
|
6
|
+
}
|
|
7
|
+
export declare function decodeVersionedValue(result: unknown): VersionedValue;
|
|
8
|
+
/** Capture the logical read key before a callback is invoked. */
|
|
9
|
+
export declare function captureReadVersioned(key: unknown): Readonly<{
|
|
10
|
+
key: string;
|
|
11
|
+
decode: typeof decodeVersionedValue;
|
|
12
|
+
}>;
|
|
13
|
+
/** Validate before handoff and close over THIS request's primitive values. */
|
|
14
|
+
export declare function captureWriteIf(key: unknown, value: unknown, expect: unknown): Readonly<{
|
|
15
|
+
key: string;
|
|
16
|
+
value: string | null;
|
|
17
|
+
expect: string | null;
|
|
18
|
+
decode(result: unknown): WriteOutcome;
|
|
19
|
+
}>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
function record(value) {
|
|
2
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
3
|
+
throw new TypeError('Invalid storage callback result');
|
|
4
|
+
}
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
function nullableString(value) {
|
|
8
|
+
if (value !== null && typeof value !== 'string')
|
|
9
|
+
throw new TypeError('Expected a string or null');
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
function version(value) {
|
|
13
|
+
const token = nullableString(value);
|
|
14
|
+
if (token !== null && (token.length === 0 || token.length > 128 || /[^\x21-\x7e]/.test(token)))
|
|
15
|
+
throw new TypeError('Invalid storage version');
|
|
16
|
+
return token;
|
|
17
|
+
}
|
|
18
|
+
export function decodeVersionedValue(result) {
|
|
19
|
+
const fields = record(result);
|
|
20
|
+
const value = nullableString(fields.value);
|
|
21
|
+
const token = version(fields.version);
|
|
22
|
+
if ((value === null) !== (token === null))
|
|
23
|
+
throw new TypeError('Unpaired storage value and version');
|
|
24
|
+
return { value, version: token };
|
|
25
|
+
}
|
|
26
|
+
// The ASCII grammar makes JS length equal to the portable UTF-8 byte count.
|
|
27
|
+
// Keep ordinary legacy storage's separate 200-byte bound unchanged.
|
|
28
|
+
function versionedKey(key) {
|
|
29
|
+
if (typeof key !== 'string' || key.length === 0 || /[^A-Za-z0-9._/-]/.test(key)) {
|
|
30
|
+
throw new TypeError('Invalid versioned storage key');
|
|
31
|
+
}
|
|
32
|
+
if (key.length > 196)
|
|
33
|
+
throw new TypeError('Versioned storage key exceeds sibling budget');
|
|
34
|
+
const components = key.split('/');
|
|
35
|
+
if (components.includes('secret-index'))
|
|
36
|
+
throw new TypeError('Secret inventory cannot be versioned');
|
|
37
|
+
if (key.replace(/\/+$/, '').split('/').at(-1) === '.p1')
|
|
38
|
+
throw new TypeError('Reserved storage sibling');
|
|
39
|
+
return key;
|
|
40
|
+
}
|
|
41
|
+
/** Capture the logical read key before a callback is invoked. */
|
|
42
|
+
export function captureReadVersioned(key) {
|
|
43
|
+
const logicalKey = versionedKey(key);
|
|
44
|
+
return Object.freeze({ key: logicalKey, decode: decodeVersionedValue });
|
|
45
|
+
}
|
|
46
|
+
/** Validate before handoff and close over THIS request's primitive values. */
|
|
47
|
+
export function captureWriteIf(key, value, expect) {
|
|
48
|
+
const logicalKey = versionedKey(key);
|
|
49
|
+
const requestedValue = nullableString(value);
|
|
50
|
+
const expectedVersion = version(expect);
|
|
51
|
+
return Object.freeze({
|
|
52
|
+
key: logicalKey, value: requestedValue, expect: expectedVersion,
|
|
53
|
+
decode(result) {
|
|
54
|
+
const fields = record(result);
|
|
55
|
+
const type = fields.type;
|
|
56
|
+
const token = version(fields.version);
|
|
57
|
+
if (type !== 'written' && type !== 'conflict')
|
|
58
|
+
throw new TypeError('Invalid storage outcome');
|
|
59
|
+
if (type === 'written') {
|
|
60
|
+
if ((requestedValue === null) !== (token === null))
|
|
61
|
+
throw new TypeError('Written version does not match request');
|
|
62
|
+
if (token === 'legacy')
|
|
63
|
+
throw new TypeError('Written must return a new storage version');
|
|
64
|
+
}
|
|
65
|
+
return { type, version: token };
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { CrocheError, HostError, thrownFacts } from '../errors.js';
|
|
2
|
+
import { builtinStateLocks } from './state-store-lock.js';
|
|
3
|
+
const owners = new WeakMap();
|
|
4
|
+
const MAX_PURGE_WALKS = 8;
|
|
5
|
+
async function* pages(store, prefix) {
|
|
6
|
+
const cursors = new Set();
|
|
7
|
+
let cursor;
|
|
8
|
+
let previous;
|
|
9
|
+
do {
|
|
10
|
+
const page = await store.list(prefix, cursor === undefined ? undefined : { cursor });
|
|
11
|
+
for (const entry of page.entries) {
|
|
12
|
+
// Storage keys use the portable ASCII grammar, so comparison is byte order.
|
|
13
|
+
if (!entry.key.startsWith(prefix) || (previous !== undefined && entry.key <= previous)) {
|
|
14
|
+
throw Error('Invalid StateStore list key order or prefix');
|
|
15
|
+
}
|
|
16
|
+
previous = entry.key;
|
|
17
|
+
}
|
|
18
|
+
yield page.entries;
|
|
19
|
+
cursor = page.nextCursor;
|
|
20
|
+
if (cursor !== undefined) {
|
|
21
|
+
if (cursors.has(cursor))
|
|
22
|
+
throw Error('StateStore cursor did not advance');
|
|
23
|
+
cursors.add(cursor);
|
|
24
|
+
}
|
|
25
|
+
} while (cursor !== undefined);
|
|
26
|
+
}
|
|
27
|
+
function operation(key, value) {
|
|
28
|
+
return value === null ? { type: 'delete', key } : { type: 'put', key, value };
|
|
29
|
+
}
|
|
30
|
+
export function transactionalStorage(store, secrets) {
|
|
31
|
+
if (['get', 'list', 'commit'].some(name => typeof store[name] !== 'function')) {
|
|
32
|
+
throw new CrocheError('invalid_configuration', 'StateStore requires get, list, and commit.', 'setup');
|
|
33
|
+
}
|
|
34
|
+
let locks = owners.get(store);
|
|
35
|
+
if (!locks) {
|
|
36
|
+
locks = new Set();
|
|
37
|
+
owners.set(store, locks);
|
|
38
|
+
}
|
|
39
|
+
const held = locks;
|
|
40
|
+
const custody = builtinStateLocks.get(store);
|
|
41
|
+
async function mutate(key, value) {
|
|
42
|
+
let current;
|
|
43
|
+
try {
|
|
44
|
+
current = await store.get(key);
|
|
45
|
+
}
|
|
46
|
+
catch (cause) {
|
|
47
|
+
// This private bridge call can fail in either StateStore operation.
|
|
48
|
+
const detail = thrownFacts(cause).message ?? 'StateStore version read failed.';
|
|
49
|
+
throw new HostError('state_get_failed', detail, { cause });
|
|
50
|
+
}
|
|
51
|
+
const version = current?.version ?? null;
|
|
52
|
+
const result = await store.commit([operation(key, value)], { key, version });
|
|
53
|
+
if (result.type === 'conflict')
|
|
54
|
+
throw Error('StateStore key changed before commit');
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
commit: (ops, condition) => store.commit(ops, condition),
|
|
58
|
+
// The Core storage seam is the app interface: the page and its cursor pass through as they are.
|
|
59
|
+
listState: (prefix, options) => store.list(prefix, options),
|
|
60
|
+
async read(key) { return (await store.get(key))?.value ?? null; },
|
|
61
|
+
async readVersioned(key) { return (await store.get(key)) ?? { value: null, version: null }; },
|
|
62
|
+
async writeIf(key, value, version) {
|
|
63
|
+
const result = await store.commit([operation(key, value)], { key, version });
|
|
64
|
+
return result.type === 'committed'
|
|
65
|
+
? { type: 'written', version: result.version }
|
|
66
|
+
: { type: 'conflict', version: null };
|
|
67
|
+
},
|
|
68
|
+
write: mutate,
|
|
69
|
+
remove: key => mutate(key, null),
|
|
70
|
+
async list(prefix) {
|
|
71
|
+
const keys = [];
|
|
72
|
+
for await (const entries of pages(store, prefix))
|
|
73
|
+
keys.push(...entries.map(entry => entry.key));
|
|
74
|
+
return keys;
|
|
75
|
+
},
|
|
76
|
+
async purgeInstance(prefix, credentialKey) {
|
|
77
|
+
if (prefix.length > 200 || !/^(?:[A-Za-z0-9._-]+\/)+$/.test(prefix)) {
|
|
78
|
+
throw new HostError('invalid_input', 'Invalid StateStore purge prefix.');
|
|
79
|
+
}
|
|
80
|
+
await custody?.verify?.(prefix);
|
|
81
|
+
if (credentialKey !== undefined) {
|
|
82
|
+
if (secrets)
|
|
83
|
+
await secrets.remove(credentialKey);
|
|
84
|
+
else
|
|
85
|
+
await mutate(credentialKey, null);
|
|
86
|
+
}
|
|
87
|
+
for (let walk = 0; walk < MAX_PURGE_WALKS; walk++) {
|
|
88
|
+
let found = false;
|
|
89
|
+
for await (const entries of pages(store, prefix)) {
|
|
90
|
+
if (!entries.length)
|
|
91
|
+
continue;
|
|
92
|
+
found = true;
|
|
93
|
+
const key = entries[0].key;
|
|
94
|
+
const version = (await store.get(key))?.version ?? null;
|
|
95
|
+
const result = await store.commit(entries.map(({ key }) => ({ type: 'delete', key })), { key, version });
|
|
96
|
+
if (result.type === 'conflict')
|
|
97
|
+
throw Error('StateStore prefix changed before deletion');
|
|
98
|
+
}
|
|
99
|
+
if (!found)
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
throw Error('StateStore prefix did not empty');
|
|
103
|
+
},
|
|
104
|
+
async lock(name) {
|
|
105
|
+
if (custody)
|
|
106
|
+
return custody.lock(name);
|
|
107
|
+
if (held.has(name))
|
|
108
|
+
return 'held';
|
|
109
|
+
held.add(name);
|
|
110
|
+
return 'acquired';
|
|
111
|
+
},
|
|
112
|
+
async unlock(name) {
|
|
113
|
+
if (custody)
|
|
114
|
+
await custody.unlock(name);
|
|
115
|
+
else
|
|
116
|
+
held.delete(name);
|
|
117
|
+
},
|
|
118
|
+
secrets: secrets ?? {
|
|
119
|
+
async read() { throw Error('No platform credential store'); },
|
|
120
|
+
async write() { throw Error('No platform credential store'); },
|
|
121
|
+
async remove() { throw Error('No platform credential store'); },
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -1,19 +1,35 @@
|
|
|
1
1
|
import type { Json, ToolManifest, ToolContent, ToolDefinition, ToolExecute, ToolResult } from './app-api.js';
|
|
2
2
|
import type { CoreExecute, CoreToolDefinition } from './core/port.js';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Give the agent a useful explanation when an app action cannot proceed.
|
|
5
|
+
*
|
|
6
|
+
* Its message reaches the model verbatim.
|
|
7
|
+
*/
|
|
4
8
|
export declare class ToolError extends Error {
|
|
9
|
+
/** Create an error whose message describes why the app action could not proceed. */
|
|
5
10
|
constructor(message?: string, options?: ErrorOptions);
|
|
6
11
|
}
|
|
7
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* Return content the agent can use, such as a caption and a photo from the app.
|
|
14
|
+
*
|
|
15
|
+
* The ONLY way to make a ToolResult: any other object a tool returns is Json, even one with `json` or `content`.
|
|
16
|
+
*/
|
|
8
17
|
export declare function toolResult(value: string | Json | ToolContent[]): ToolResult;
|
|
9
18
|
/** Core's handle for a definition made by defineTool/tools, or undefined for anything else. */
|
|
10
19
|
export declare function coreDefinition(value: unknown): CoreToolDefinition | undefined;
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
20
|
+
/**
|
|
21
|
+
* Let the agent use app-owned features such as looking up an order.
|
|
22
|
+
*
|
|
23
|
+
* `manifest` is typed as ToolManifest (a JSON module import or a const with mixed commands fits it); the SDK still
|
|
24
|
+
* serializes it unchanged (undefined throws, json.ts) and Core validates it.
|
|
25
|
+
*/
|
|
13
26
|
export declare function defineTool(manifest: ToolManifest, execute: ToolExecute): ToolDefinition;
|
|
14
27
|
/** Croche tools: they run in Core through Egress and are billed. */
|
|
15
28
|
export declare const tools: {
|
|
29
|
+
/** Definition for the built-in web search tool. */
|
|
16
30
|
readonly webSearch: ToolDefinition;
|
|
31
|
+
/** Definition for the built-in image tool. */
|
|
32
|
+
readonly image: ToolDefinition;
|
|
17
33
|
};
|
|
18
34
|
/** The callback Core holds for one definition. */
|
|
19
35
|
export declare function bridge(execute: ToolExecute): CoreExecute;
|