@h1r-ai/croche-react-native 0.5.5 → 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,44 @@
|
|
|
1
|
+
package dev.croche
|
|
2
|
+
|
|
3
|
+
import com.google.gson.*
|
|
4
|
+
|
|
5
|
+
/** Reader codes stay open; Core owns the available capabilities and deferral reasons. */
|
|
6
|
+
data class AttachedDebugger(val debuggerId: String, val capabilities: List<String>)
|
|
7
|
+
data class Wake(val user: String?, val at: Long)
|
|
8
|
+
data class WakeTimes(val wakes: List<Wake>, val next: Long?)
|
|
9
|
+
data class DeliveryDeferred(val user: String?, val agent: String, val instance: String?, val code: String, val message: String)
|
|
10
|
+
data class DueDelivery(val ran: Boolean, val delivered: Int, val deferred: List<DeliveryDeferred>)
|
|
11
|
+
data class MemoryItem(val id: String, val text: String, val fields: Map<String, JsonElement>)
|
|
12
|
+
|
|
13
|
+
/** Known reply fields are projected; unknown fields never prevent a read. */
|
|
14
|
+
internal object PublicResults {
|
|
15
|
+
private fun objectValue(json: String) = JsonParser.parseString(json).asJsonObject
|
|
16
|
+
private fun JsonObject.optionalLong(key: String) = get(key)?.takeUnless { it.isJsonNull }?.asLong
|
|
17
|
+
fun wakes(json: String): WakeTimes = objectValue(json).let { value ->
|
|
18
|
+
WakeTimes(value.getAsJsonArray("wakes").map { entry -> entry.asJsonObject.let {
|
|
19
|
+
Wake(it.get("user")?.takeUnless { user -> user.isJsonNull }?.asString, it.get("at").asLong)
|
|
20
|
+
} }, value.optionalLong("next"))
|
|
21
|
+
}
|
|
22
|
+
fun delivery(json: String): DueDelivery = objectValue(json).let { value ->
|
|
23
|
+
DueDelivery(value.get("ran").asBoolean, value.get("delivered").asInt,
|
|
24
|
+
value.getAsJsonArray("deferred").map { entry -> entry.asJsonObject.let {
|
|
25
|
+
DeliveryDeferred(it.get("user")?.takeUnless { user -> user.isJsonNull }?.asString, it.get("agent").asString,
|
|
26
|
+
it.get("instance")?.takeUnless { v -> v.isJsonNull }?.asString,
|
|
27
|
+
it.get("code").asString, it.get("message").asString)
|
|
28
|
+
} })
|
|
29
|
+
}
|
|
30
|
+
fun debug(json: String): AttachedDebugger = objectValue(json).let {
|
|
31
|
+
AttachedDebugger(it.get("debuggerId").asString, it.getAsJsonArray("capabilities").map { v -> v.asString })
|
|
32
|
+
}
|
|
33
|
+
fun memories(json: String): List<MemoryItem> = JsonParser.parseString(json).asJsonArray.map { entry ->
|
|
34
|
+
val value = entry.asJsonObject
|
|
35
|
+
MemoryItem(value.get("id").asString, value.get("text").asString,
|
|
36
|
+
value.entrySet().associate { it.key to it.value.deepCopy() })
|
|
37
|
+
}
|
|
38
|
+
fun wiki(json: String): List<WikiDoc> = JsonParser.parseString(json).asJsonArray.map { entry ->
|
|
39
|
+
val value = entry.asJsonObject
|
|
40
|
+
val path = value.get("path").asString; val description = value.get("description").asString
|
|
41
|
+
if (value.has("markdown")) WikiDoc.Markdown(path, description, value.get("markdown").asString)
|
|
42
|
+
else WikiDoc.File(path, description, value.get("file").asString)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
package dev.croche
|
|
2
|
+
|
|
3
|
+
/** Reader values are open. Ignore values this app does not understand; never invent a default.
|
|
4
|
+
* The exact Core string is retained, including case. ErrorCategory is the sole closed reader set.
|
|
5
|
+
*/
|
|
6
|
+
internal interface ReaderString { val value: String }
|
|
7
|
+
|
|
8
|
+
internal data class InputKind(override val value: String) : ReaderString {
|
|
9
|
+
companion object {
|
|
10
|
+
val USER = InputKind("user")
|
|
11
|
+
val APP_EVENT = InputKind("app_event")
|
|
12
|
+
val APP_STATE = InputKind("app_state")
|
|
13
|
+
val UNKNOWN = InputKind("unknown")
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
internal data class InputStatus(override val value: String) : ReaderString {
|
|
18
|
+
companion object {
|
|
19
|
+
val QUEUED = InputStatus("queued")
|
|
20
|
+
val SEEN = InputStatus("seen")
|
|
21
|
+
val ANSWERED = InputStatus("answered")
|
|
22
|
+
val NO_REPLY = InputStatus("no_reply")
|
|
23
|
+
val FAILED = InputStatus("failed")
|
|
24
|
+
val CANCELLED = InputStatus("cancelled")
|
|
25
|
+
val SETTLED_BEFORE_UPGRADE = InputStatus("settled_before_upgrade")
|
|
26
|
+
val UNKNOWN = InputStatus("unknown")
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
internal data class MessageStatus(override val value: String) : ReaderString {
|
|
31
|
+
companion object {
|
|
32
|
+
val COMPLETED = MessageStatus("completed")
|
|
33
|
+
val FAILED = MessageStatus("failed")
|
|
34
|
+
val CANCELLED = MessageStatus("cancelled")
|
|
35
|
+
val INTERRUPTED = MessageStatus("interrupted")
|
|
36
|
+
val UNKNOWN = MessageStatus("unknown")
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
internal data class AgentStatus(override val value: String) : ReaderString {
|
|
41
|
+
companion object {
|
|
42
|
+
val RUNNING = AgentStatus("running")
|
|
43
|
+
val WAITING = AgentStatus("waiting")
|
|
44
|
+
val IDLE = AgentStatus("idle")
|
|
45
|
+
val UNKNOWN = AgentStatus("unknown")
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
internal data class AgentActivity(override val value: String) : ReaderString {
|
|
50
|
+
companion object {
|
|
51
|
+
val MODEL = AgentActivity("model")
|
|
52
|
+
val SUMMARY = AgentActivity("summary")
|
|
53
|
+
val TOOL = AgentActivity("tool")
|
|
54
|
+
val UNKNOWN = AgentActivity("unknown")
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
internal data class CostStatus(override val value: String) : ReaderString {
|
|
59
|
+
companion object {
|
|
60
|
+
val REPORTED = CostStatus("reported")
|
|
61
|
+
val ESTIMATED = CostStatus("estimated")
|
|
62
|
+
val NONE = CostStatus("none")
|
|
63
|
+
val UNKNOWN = CostStatus("unknown")
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
internal data class GapReason(override val value: String) : ReaderString {
|
|
68
|
+
companion object {
|
|
69
|
+
val CLEARED = GapReason("cleared")
|
|
70
|
+
val LIMIT = GapReason("limit")
|
|
71
|
+
val UNKNOWN = GapReason("unknown")
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
package dev.croche
|
|
2
|
+
|
|
3
|
+
import com.google.gson.*
|
|
4
|
+
import dev.croche.ffi.CoreCrocheInterface
|
|
5
|
+
import dev.croche.s7.publicCall
|
|
6
|
+
|
|
7
|
+
/** The same envelope as TS; all outcomes and costs are supplied by Core. */
|
|
8
|
+
sealed interface RunToolResult {
|
|
9
|
+
data class Ok(val id: String, val output: JsonElement, val attachments: List<AppStreamContract.Attachment>,
|
|
10
|
+
val cost: Double?, val costStatus: String) : RunToolResult
|
|
11
|
+
data class Failed(val id: String, val code: String, val category: ErrorCategory, val message: String,
|
|
12
|
+
val cost: Double?, val costStatus: String) : RunToolResult
|
|
13
|
+
data class Lost(val id: String, val code: String, val category: ErrorCategory, val message: String) : RunToolResult
|
|
14
|
+
data class Unknown(val status: String, val fields: JsonElement) : RunToolResult
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
internal object RunToolJson {
|
|
18
|
+
private val gson = Gson()
|
|
19
|
+
private fun JsonObject.cost(): Double? = get("cost").takeUnless { it.isJsonNull }?.asDouble
|
|
20
|
+
private fun JsonObject.category() = ErrorCategory.entries.single { it.name.equals(get("category").asString, ignoreCase = true) }
|
|
21
|
+
fun result(json: String): RunToolResult {
|
|
22
|
+
val value = JsonParser.parseString(json).asJsonObject
|
|
23
|
+
return when (val status = value.get("status").asString) {
|
|
24
|
+
"ok" -> RunToolResult.Ok(value.get("id").asString, value.get("output").deepCopy(),
|
|
25
|
+
value.getAsJsonArray("attachments").map { gson.fromJson(it, AppStreamContract.Attachment::class.java) },
|
|
26
|
+
value.cost(), value.get("costStatus").asString)
|
|
27
|
+
"failed" -> RunToolResult.Failed(value.get("id").asString, value.get("code").asString,
|
|
28
|
+
value.category(), value.get("message").asString, value.cost(), value.get("costStatus").asString)
|
|
29
|
+
"lost" -> RunToolResult.Lost(value.get("id").asString, value.get("code").asString,
|
|
30
|
+
value.category(), value.get("message").asString)
|
|
31
|
+
else -> RunToolResult.Unknown(status, value.deepCopy())
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
internal suspend fun runTool(core: CoreCrocheInterface, name: String, args: JsonObject, id: String): RunToolResult {
|
|
37
|
+
val arguments = args.toString()
|
|
38
|
+
val options = JsonObject().apply { addProperty("id", id) }.toString()
|
|
39
|
+
return publicCall { cancel -> RunToolJson.result(core.runTool(name, arguments, options, cancel)) }
|
|
40
|
+
}
|
|
@@ -3,25 +3,27 @@ package dev.croche
|
|
|
3
3
|
import com.google.gson.JsonArray
|
|
4
4
|
import com.google.gson.JsonObject
|
|
5
5
|
|
|
6
|
-
/** One encoding boundary
|
|
7
|
-
internal fun SendInput.encoded(options: SendOptions = SendOptions()): JsonObject {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
context?.let { out.add("context", it.deepCopy()) }
|
|
26
|
-
return CrocheImages.prepareInput(out, options.imageResolution)
|
|
6
|
+
/** One encoding boundary; Core owns kinds, defaults, limits, source support and admission. */
|
|
7
|
+
internal fun SendInput.encoded(options: SendOptions = SendOptions()): JsonObject = JsonObject().apply {
|
|
8
|
+
id?.let { addProperty("id", it) }
|
|
9
|
+
add("input", JsonArray().apply {
|
|
10
|
+
input.forEach { element -> add(JsonObject().apply {
|
|
11
|
+
when (element) {
|
|
12
|
+
is TextInput -> {
|
|
13
|
+
addProperty("type", "text"); addProperty("text", element.text)
|
|
14
|
+
element.kind?.let { addProperty("kind", it.rawValue) }
|
|
15
|
+
}
|
|
16
|
+
is ImageInput -> {
|
|
17
|
+
// Bytes never enter JSON: only the image type/alt, bytes in CoreImageInput.
|
|
18
|
+
addProperty("type", "image")
|
|
19
|
+
if (element.uri != null) addProperty("uri", element.uri)
|
|
20
|
+
element.alt?.let { addProperty("alt", it) }
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}) }
|
|
24
|
+
})
|
|
27
25
|
}
|
|
26
|
+
|
|
27
|
+
/** Byte images by element index, for Core's reserved entry. */
|
|
28
|
+
internal fun SendInput.imageCaptures(): List<Pair<Int, ByteArray>> =
|
|
29
|
+
input.withIndex().mapNotNull { (index, element) -> (element as? ImageInput)?.image?.let { index to it } }
|
|
@@ -68,14 +68,14 @@ class FileRuntimeStorage(private val file: File) : KeyedRuntimeStorage, ArchiveR
|
|
|
68
68
|
/** Runs after a segment's temporary is complete and before the move (tests inject a crash). */
|
|
69
69
|
internal var beforeCommit: ((File) -> Unit)? = null
|
|
70
70
|
@Synchronized override fun acquire() {
|
|
71
|
-
if (handle != null) throw
|
|
71
|
+
if (handle != null) throw CrocheError("store_busy", "Storage is already owned")
|
|
72
72
|
file.absoluteFile.parentFile.mkdirs()
|
|
73
73
|
val opened = RandomAccessFile(File(file.path + ".lock"), "rw")
|
|
74
74
|
try {
|
|
75
|
-
val acquired = opened.channel.tryLock() ?: throw
|
|
75
|
+
val acquired = opened.channel.tryLock() ?: throw CrocheError("store_busy", "Storage is already owned")
|
|
76
76
|
handle = opened; lock = acquired
|
|
77
77
|
} catch (error: Exception) {
|
|
78
|
-
opened.close(); throw
|
|
78
|
+
opened.close(); throw CrocheError("store_busy", "Could not acquire storage ownership")
|
|
79
79
|
}
|
|
80
80
|
removeStaleTemporaries()
|
|
81
81
|
}
|
|
@@ -83,7 +83,7 @@ class FileRuntimeStorage(private val file: File) : KeyedRuntimeStorage, ArchiveR
|
|
|
83
83
|
check(handle != null) { "Storage is not owned" }
|
|
84
84
|
if (!file.exists()) return JsonNull.INSTANCE
|
|
85
85
|
val saved = JsonParser.parseString(file.readText(Charsets.UTF_8))
|
|
86
|
-
if (!saved.isJsonObject) throw
|
|
86
|
+
if (!saved.isJsonObject) throw CrocheError("storage_error", "Existing Runtime snapshot must be an object")
|
|
87
87
|
return saved
|
|
88
88
|
}
|
|
89
89
|
@Synchronized override fun save(state: JsonElement) {
|
|
@@ -98,7 +98,7 @@ class FileRuntimeStorage(private val file: File) : KeyedRuntimeStorage, ArchiveR
|
|
|
98
98
|
@Synchronized override fun save(entry: String, value: JsonElement) = replace(sidecar(entry), value)
|
|
99
99
|
private fun sidecar(entry: String): File {
|
|
100
100
|
check(handle != null) { "Storage is not owned" }
|
|
101
|
-
if (!Regex("[a-z0-9]+").matches(entry)) throw
|
|
101
|
+
if (!Regex("[a-z0-9]+").matches(entry)) throw CrocheError("storage_error", "Invalid storage entry")
|
|
102
102
|
return File(file.path + "." + entry)
|
|
103
103
|
}
|
|
104
104
|
private fun replace(target: File, value: JsonElement) {
|
|
@@ -113,7 +113,7 @@ class FileRuntimeStorage(private val file: File) : KeyedRuntimeStorage, ArchiveR
|
|
|
113
113
|
// The turn archive (F20.6)
|
|
114
114
|
@Synchronized override fun appendLine(entry: String, line: String) {
|
|
115
115
|
val target = segment(entry)
|
|
116
|
-
if (line.contains('\n')) throw
|
|
116
|
+
if (line.contains('\n')) throw CrocheError("storage_error", "An archive line holds no newline")
|
|
117
117
|
RandomAccessFile(target, "rw").use { output ->
|
|
118
118
|
val size = output.length()
|
|
119
119
|
val bytes = java.io.ByteArrayOutputStream()
|
|
@@ -137,7 +137,7 @@ class FileRuntimeStorage(private val file: File) : KeyedRuntimeStorage, ArchiveR
|
|
|
137
137
|
@Synchronized override fun replaceLines(entry: String, lines: List<String>) {
|
|
138
138
|
val target = segment(entry)
|
|
139
139
|
if (lines.isEmpty()) { Files.deleteIfExists(target.toPath()); return }
|
|
140
|
-
if (lines.any { it.contains('\n') }) throw
|
|
140
|
+
if (lines.any { it.contains('\n') }) throw CrocheError("storage_error", "An archive line holds no newline")
|
|
141
141
|
// '.' and '-' are outside the entry rule: a temporary is never an entry, `held` or `lock`.
|
|
142
142
|
val temporary = File(target.path + ".tmp-" + java.util.UUID.randomUUID().toString())
|
|
143
143
|
try {
|
|
@@ -161,7 +161,7 @@ class FileRuntimeStorage(private val file: File) : KeyedRuntimeStorage, ArchiveR
|
|
|
161
161
|
/** The archive's entries are segments only (`archive` and four digits): never the snapshot's
|
|
162
162
|
* own sidecars (`held`) or its lock file (`lock`). */
|
|
163
163
|
private fun segment(entry: String): File {
|
|
164
|
-
if (!SEGMENT.matches(entry)) throw
|
|
164
|
+
if (!SEGMENT.matches(entry)) throw CrocheError("storage_error", "Invalid archive entry")
|
|
165
165
|
return sidecar(entry)
|
|
166
166
|
}
|
|
167
167
|
/** The files beside the snapshot, `<file>.<suffix>`, as (suffix, file). */
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
package dev.croche
|
|
2
|
+
|
|
3
|
+
/** ARCHITECTURE §2a: app storage reports facts only; Core owns keys, framing and error policy. */
|
|
4
|
+
interface StoragePal {
|
|
5
|
+
suspend fun read(key: String): String?
|
|
6
|
+
suspend fun write(key: String, value: String)
|
|
7
|
+
suspend fun remove(key: String)
|
|
8
|
+
suspend fun list(prefix: String): List<String>
|
|
9
|
+
suspend fun lock(name: String): LockOutcome
|
|
10
|
+
suspend fun unlock(name: String)
|
|
11
|
+
val secrets: SecretsPal
|
|
12
|
+
}
|
|
13
|
+
interface SecretsPal {
|
|
14
|
+
suspend fun read(key: String): String?
|
|
15
|
+
suspend fun write(key: String, value: String)
|
|
16
|
+
suspend fun remove(key: String)
|
|
17
|
+
}
|
|
18
|
+
enum class LockOutcome { ACQUIRED, HELD }
|
|
@@ -13,6 +13,9 @@ internal interface CoreStreamPort {
|
|
|
13
13
|
fun observe(event: (RuntimeEvent) -> Unit, end: (Throwable?) -> Unit): AutoCloseable
|
|
14
14
|
fun select(initialMessages: Int?, after: Cursor?, event: (RuntimeEvent) -> Unit): Projection
|
|
15
15
|
suspend fun send(input: JsonObject, options: SendOptions): Receipt
|
|
16
|
+
/** Byte images need Core's reserved entry and a native PAL; JSON-only ports refuse them. */
|
|
17
|
+
suspend fun sendWithImages(input: SendInput, options: SendOptions): Receipt =
|
|
18
|
+
throw CrocheError("image_unsupported", "The image could not be prepared.")
|
|
16
19
|
suspend fun readOutcome(id: String, fromSeq: Long, captured: (CapturedOutcome) -> Unit): RetainedEnds
|
|
17
20
|
suspend fun cancel(id: String)
|
|
18
21
|
suspend fun clearHistory()
|
|
@@ -31,7 +34,7 @@ internal interface CoreStreamPort {
|
|
|
31
34
|
internal class CoreStreamFailure(val code: String, override val message: String, val poisoned: Boolean) : RuntimeException(message)
|
|
32
35
|
internal fun streamEnd(cause: Throwable?): RuntimeEvent.StreamError {
|
|
33
36
|
val typed = cause as? CoreStreamFailure
|
|
34
|
-
val code = typed?.code ?: (cause as?
|
|
37
|
+
val code = typed?.code ?: (cause as? CrocheError)?.code ?: "closed"
|
|
35
38
|
return RuntimeEvent.StreamError(code, cause?.message ?: "Runtime is closed", typed?.poisoned ?: false)
|
|
36
39
|
}
|
|
37
40
|
internal data class CapturedOutcome(val head: Long, val floor: Long, val status: InputStatus, val idle: Boolean)
|
|
@@ -67,9 +70,9 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
67
70
|
synchronized(lock) { if (app === branch) { app = null; projection = null } }
|
|
68
71
|
}
|
|
69
72
|
suspend fun history(before: Long?, limit: Int) {
|
|
70
|
-
if (limit !in 1..256) throw
|
|
73
|
+
if (limit !in 1..256) throw CrocheError("invalid_configuration", "history limit must be 1..256")
|
|
71
74
|
val current = synchronized(lock) { if (app === branch && !branch.isEnded) selected else null }
|
|
72
|
-
?: throw
|
|
75
|
+
?: throw CrocheError("invalid_configuration", "history requires an active subscription")
|
|
73
76
|
current.history(before, limit)
|
|
74
77
|
}
|
|
75
78
|
}
|
|
@@ -101,24 +104,31 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
101
104
|
|
|
102
105
|
suspend fun send(input: SendInput, options: SendOptions): Receipt {
|
|
103
106
|
currentCoroutineContext().ensureActive()
|
|
104
|
-
synchronized(lock) { if (ended) throw
|
|
105
|
-
return
|
|
107
|
+
synchronized(lock) { if (ended) throw CrocheError("closed", "Runtime is closed") }
|
|
108
|
+
return sendOwned(input.owned(), options.owned())
|
|
106
109
|
}
|
|
107
110
|
|
|
108
|
-
|
|
111
|
+
/** Plain inputs stay on the JSON send; only byte images take Core's reserved entry. */
|
|
112
|
+
private suspend fun sendOwned(input: SendInput, options: SendOptions): Receipt =
|
|
113
|
+
if (input.imageCaptures().isEmpty()) port.send(input.encoded(options), options) else port.sendWithImages(input, options)
|
|
114
|
+
|
|
115
|
+
suspend fun request(input: SendInput, options: SendOptions, wait: RequestWait?): LegacyRequestResult = coroutineScope {
|
|
109
116
|
ensureActive()
|
|
110
117
|
val owned = input.owned()
|
|
111
|
-
val waiter = Waiter(
|
|
118
|
+
val waiter = Waiter(null, wait)
|
|
112
119
|
synchronized(lock) {
|
|
113
|
-
if (ended) throw
|
|
120
|
+
if (ended) throw CrocheError("closed", "Runtime is closed")
|
|
114
121
|
waiters.add(waiter) // Before send: immediate canonical progress must not outrun registration.
|
|
115
122
|
}
|
|
116
123
|
val read = launch(start = CoroutineStart.UNDISPATCHED) {
|
|
117
124
|
try {
|
|
118
|
-
val
|
|
119
|
-
val receipt = port.send(encoded, options.owned())
|
|
125
|
+
val receipt = sendOwned(owned, options.owned())
|
|
120
126
|
val proceed = synchronized(lock) {
|
|
127
|
+
waiter.id = receipt.requestId
|
|
121
128
|
waiter.receipt = receipt
|
|
129
|
+
val early = waiter.beforeReceipt.toList()
|
|
130
|
+
waiter.dropBeforeReceipt()
|
|
131
|
+
early.forEach { waiter.event(it) }
|
|
122
132
|
// Terminal same-id retries emit no new input event. Seed before idle
|
|
123
133
|
// overtakes capture, without regressing an already observed disposition.
|
|
124
134
|
if (waiter.status == null || (waiter.status?.terminal() != true && receipt.status.terminal())) {
|
|
@@ -128,11 +138,11 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
128
138
|
}
|
|
129
139
|
if (!proceed) return@launch
|
|
130
140
|
if (receipt.seq == null) {
|
|
131
|
-
waiter.result.complete(
|
|
141
|
+
waiter.result.complete(LegacyRequestResult(receipt.requestId, receipt.status, emptyList(), false, true))
|
|
132
142
|
return@launch
|
|
133
143
|
}
|
|
134
144
|
var captured = false
|
|
135
|
-
val retained = port.readOutcome(receipt.
|
|
145
|
+
val retained = port.readOutcome(receipt.requestId, receipt.seq) { snapshot ->
|
|
136
146
|
synchronized(lock) {
|
|
137
147
|
captured = true
|
|
138
148
|
if (waiter.status?.terminal() != true) waiter.status = snapshot.status
|
|
@@ -143,7 +153,7 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
143
153
|
check(captured) { "Core outcome read omitted its ordered capture" }
|
|
144
154
|
synchronized(lock) {
|
|
145
155
|
waiter.gap = waiter.gap || retained.gap
|
|
146
|
-
retained.messages.filter { receipt.
|
|
156
|
+
retained.messages.filter { receipt.requestId in it.causedBy }.forEach { waiter.add(it) }
|
|
147
157
|
waiter.reading = false
|
|
148
158
|
waiter.finish()
|
|
149
159
|
}
|
|
@@ -152,22 +162,22 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
152
162
|
} catch (failure: Exception) {
|
|
153
163
|
synchronized(lock) {
|
|
154
164
|
waiter.result.completeExceptionally(if (waiter.receipt != null)
|
|
155
|
-
|
|
165
|
+
CrocheError("observation_lost", "Observation ended after acceptance") else failure)
|
|
156
166
|
}
|
|
157
167
|
}
|
|
158
168
|
}
|
|
159
169
|
try { waiter.result.await() }
|
|
160
170
|
finally {
|
|
161
|
-
synchronized(lock) { waiters.remove(waiter) }
|
|
171
|
+
synchronized(lock) { waiter.dropBeforeReceipt(); waiters.remove(waiter) }
|
|
162
172
|
read.cancel() // Stops observation/storage wait only; never calls port.cancel.
|
|
163
173
|
}
|
|
164
174
|
}
|
|
165
175
|
|
|
166
176
|
// Keel #361 6071161214: Both forms require an attached stream; pin its projection generation.
|
|
167
177
|
suspend fun history(before: Long?, limit: Int) {
|
|
168
|
-
if (limit !in 1..256) throw
|
|
178
|
+
if (limit !in 1..256) throw CrocheError("invalid_configuration", "history limit must be 1..256")
|
|
169
179
|
val selected = synchronized(lock) { projection }
|
|
170
|
-
?: throw
|
|
180
|
+
?: throw CrocheError("invalid_configuration", "history requires an app collection")
|
|
171
181
|
selected.history(before, limit)
|
|
172
182
|
}
|
|
173
183
|
suspend fun cancel(id: String) = port.cancel(id)
|
|
@@ -180,7 +190,7 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
180
190
|
projection?.close()
|
|
181
191
|
val terminal = streamEnd(cause)
|
|
182
192
|
app?.end(terminal)
|
|
183
|
-
waiters.toList().forEach { it.lost = true; if (it.receipt != null) it.failLost() }
|
|
193
|
+
waiters.toList().forEach { it.lost = true; it.dropBeforeReceipt(); if (it.receipt != null) it.failLost() }
|
|
184
194
|
}
|
|
185
195
|
}
|
|
186
196
|
|
|
@@ -192,8 +202,8 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
192
202
|
RequestWait.IDLE -> status?.terminal() == true && idle
|
|
193
203
|
}
|
|
194
204
|
|
|
195
|
-
private inner class Waiter(
|
|
196
|
-
val result = CompletableDeferred<
|
|
205
|
+
private inner class Waiter(var id: String?, val wait: RequestWait?) {
|
|
206
|
+
val result = CompletableDeferred<LegacyRequestResult>()
|
|
197
207
|
var receipt: Receipt? = null
|
|
198
208
|
var status: InputStatus? = null
|
|
199
209
|
var complete = false
|
|
@@ -201,12 +211,26 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
201
211
|
var lost = false
|
|
202
212
|
var gap = false
|
|
203
213
|
val messages = linkedMapOf<Pair<String, Long>, RuntimeEvent.MessageEnd>()
|
|
214
|
+
// Per-call SDK observation bounds; losing observation never cancels Core.
|
|
215
|
+
val beforeReceipt = mutableListOf<RuntimeEvent>()
|
|
216
|
+
var beforeReceiptBytes = 0
|
|
217
|
+
fun dropBeforeReceipt() { beforeReceipt.clear(); beforeReceiptBytes = 0 }
|
|
218
|
+
fun retain(event: RuntimeEvent) {
|
|
219
|
+
if (lost) return
|
|
220
|
+
val bytes = try { event.serialized().toByteArray(Charsets.UTF_8).size }
|
|
221
|
+
catch (_: Exception) { lost = true; dropBeforeReceipt(); return }
|
|
222
|
+
if (beforeReceipt.size >= 256 || beforeReceiptBytes.toLong() + bytes > 1_048_576) {
|
|
223
|
+
lost = true; dropBeforeReceipt(); return
|
|
224
|
+
}
|
|
225
|
+
beforeReceipt.add(event.owned()); beforeReceiptBytes += bytes
|
|
226
|
+
}
|
|
204
227
|
fun add(event: RuntimeEvent.MessageEnd) { messages[event.id to event.seq] = event.owned() as RuntimeEvent.MessageEnd }
|
|
205
228
|
fun event(event: RuntimeEvent) {
|
|
206
229
|
if (complete || result.isCompleted) return
|
|
230
|
+
if (id == null) { retain(event); return }
|
|
207
231
|
when (event) {
|
|
208
232
|
is RuntimeEvent.Input -> if (event.id == id && status?.terminal() != true) status = event.status
|
|
209
|
-
is RuntimeEvent.MessageEnd -> if (id in event.causedBy) add(event)
|
|
233
|
+
is RuntimeEvent.MessageEnd -> if (id?.let { it in event.causedBy } == true) add(event)
|
|
210
234
|
is RuntimeEvent.State -> if (completion(status, event.status == AgentStatus.IDLE, wait)) complete = true
|
|
211
235
|
else -> Unit
|
|
212
236
|
}
|
|
@@ -215,13 +239,14 @@ internal class StreamHub(private val port: CoreStreamPort) : AutoCloseable {
|
|
|
215
239
|
fun finish() {
|
|
216
240
|
val accepted = receipt ?: return
|
|
217
241
|
val disposition = status ?: return
|
|
218
|
-
if (!reading && complete) result.complete(
|
|
242
|
+
if (!reading && complete) result.complete(LegacyRequestResult(accepted.requestId, disposition,
|
|
219
243
|
messages.values.sortedBy { it.seq }, gap, false))
|
|
220
244
|
}
|
|
221
|
-
fun failLost() { result.completeExceptionally(
|
|
245
|
+
fun failLost() { result.completeExceptionally(CrocheError("observation_lost", "Reopen and retry the same id and input")) }
|
|
222
246
|
}
|
|
223
247
|
}
|
|
224
248
|
|
|
225
|
-
internal fun SendInput.owned() = copy(
|
|
226
|
-
|
|
249
|
+
internal fun SendInput.owned() = copy(input = input.map {
|
|
250
|
+
when (it) { is TextInput -> it.copy(); is ImageInput -> it.owned() }
|
|
251
|
+
})
|
|
227
252
|
internal fun SendOptions.owned() = copy(responseFormat = responseFormat?.deepCopy())
|
|
@@ -4,34 +4,33 @@ import com.google.gson.JsonElement
|
|
|
4
4
|
import com.google.gson.JsonObject
|
|
5
5
|
import java.time.Instant
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
data class
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
/** Ordered request elements; normalization and admission belong to Core. */
|
|
8
|
+
sealed interface SendElement
|
|
9
|
+
/** [kind] is the generated reader value Core also reports on `input` events; null = Core's default (user). */
|
|
10
|
+
data class TextInput(val text: String, val kind: AppStreamContract.InputKind? = null) : SendElement
|
|
11
|
+
/** One image element, supplied by URI or encoded bytes. */
|
|
12
|
+
class ImageInput private constructor(internal val uri: String?, internal val image: ByteArray?,
|
|
13
|
+
val alt: String?) : SendElement {
|
|
14
|
+
constructor(uri: String, alt: String? = null) : this(uri, null, alt)
|
|
15
|
+
constructor(image: ByteArray, alt: String? = null) : this(null, image.copyOf(), alt)
|
|
16
|
+
internal fun owned(): ImageInput = uri?.let { ImageInput(it, alt) } ?: ImageInput(image!!, alt)
|
|
17
|
+
}
|
|
18
|
+
data class SendInput(val id: String? = null, val input: List<SendElement>)
|
|
19
|
+
data class SendOptions(val responseFormat: JsonObject? = null)
|
|
20
|
+
internal data class Receipt(val requestId: String, val status: InputStatus, val seq: Long?)
|
|
15
21
|
// OPEN(§18.24.D): No pending field until Atlas approves the consuming-unit contract.
|
|
16
22
|
enum class RequestWait { IDLE }
|
|
17
|
-
data class
|
|
23
|
+
internal data class LegacyRequestResult(val id: String, val status: InputStatus, val messages: List<RuntimeEvent.MessageEnd>,
|
|
18
24
|
val gap: Boolean, val legacy: Boolean)
|
|
19
25
|
data class Cursor(val logId: String, val seq: Long)
|
|
20
|
-
data class RuntimeError(val code: String, val message: String)
|
|
26
|
+
internal data class RuntimeError(val code: String, val message: String)
|
|
21
27
|
data class Streaming(val messageId: String, val firstSeq: Long, val text: String)
|
|
22
28
|
data class ImageSummary(val alt: String?, val mime: String?, val bytes: Long?)
|
|
23
29
|
data class ToolSource(val command: String, val callId: String)
|
|
24
|
-
|
|
25
|
-
enum class InputStatus { QUEUED, SEEN, ANSWERED, NO_REPLY, FAILED, CANCELLED, SETTLED_BEFORE_UPGRADE, UNKNOWN }
|
|
26
|
-
enum class MessageStatus { COMPLETED, FAILED, CANCELLED, INTERRUPTED, UNKNOWN }
|
|
27
|
-
enum class AgentStatus { RUNNING, WAITING, IDLE, UNKNOWN }
|
|
28
|
-
/** The stream `state`'s `activity`: a model call, a summary call (held before the reply), or a
|
|
29
|
-
* tool round. A value this SDK does not know yet is UNKNOWN, never a stream error. */
|
|
30
|
-
enum class AgentActivity { MODEL, SUMMARY, TOOL, UNKNOWN }
|
|
31
|
-
enum class CostStatus { REPORTED, ESTIMATED, NONE, UNKNOWN }
|
|
32
|
-
enum class GapReason { CLEARED, LIMIT, UNKNOWN }
|
|
30
|
+
|
|
33
31
|
enum class ImageMime { PNG, JPEG, WEBP, GIF }
|
|
34
32
|
|
|
33
|
+
/** Tool-result image attachment. Send requests use ImageInput. */
|
|
35
34
|
class InputImage {
|
|
36
35
|
internal val json: JsonObject
|
|
37
36
|
constructor(data: ByteArray, mime: ImageMime, alt: String? = null, resolution: ImageResolution? = null) {
|
|
@@ -47,7 +46,7 @@ class InputImage {
|
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
sealed class RuntimeEvent {
|
|
49
|
+
internal sealed class RuntimeEvent {
|
|
51
50
|
open val seq: Long? get() = null
|
|
52
51
|
data class Input(override val seq: Long, val firstSeq: Long, val id: String, val kind: InputKind, val text: String?,
|
|
53
52
|
val images: List<ImageSummary>, val status: InputStatus, val error: RuntimeError?, val at: Instant) : RuntimeEvent()
|
|
@@ -57,7 +56,7 @@ sealed class RuntimeEvent {
|
|
|
57
56
|
val error: RuntimeError?, val at: Instant) : RuntimeEvent()
|
|
58
57
|
data class Cleared(override val seq: Long, val id: String, val at: Instant) : RuntimeEvent()
|
|
59
58
|
data class App(override val seq: Long, val id: String, val name: String, val data: JsonElement, val source: ToolSource, val at: Instant) : RuntimeEvent()
|
|
60
|
-
data class Turn(override val seq: Long, val id: String, val causedBy: List<String>, val inputs: List<String>, val purpose: String, val usage: Map<String, Long>, val
|
|
59
|
+
data class Turn(override val seq: Long, val id: String, val causedBy: List<String>, val inputs: List<String>, val purpose: String, val usage: Map<String, Long>, val cost: Double,
|
|
61
60
|
val costStatus: CostStatus, val model: String, val imagesOmitted: Int?, val at: Instant) : RuntimeEvent()
|
|
62
61
|
data class Error(override val seq: Long, val id: String, val code: String, val message: String, val inputs: List<String>, val at: Instant) : RuntimeEvent()
|
|
63
62
|
data class MessageDelta(val id: String, val text: String) : RuntimeEvent()
|
|
@@ -70,5 +69,6 @@ sealed class RuntimeEvent {
|
|
|
70
69
|
data class Snapshot(val logId: String, val state: State, val streaming: Streaming?) : RuntimeEvent()
|
|
71
70
|
data class StreamError(val code: String, val message: String, val poisoned: Boolean) : RuntimeEvent()
|
|
72
71
|
data object Replaced : RuntimeEvent()
|
|
73
|
-
|
|
72
|
+
/** Ignore unknown types and fields. The complete unknown payload is retained. */
|
|
73
|
+
data class Unknown(val type: String, val fields: JsonObject = JsonObject()) : RuntimeEvent()
|
|
74
74
|
}
|