@h1r-ai/croche-react-native 0.5.5 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (248) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +90 -451
  3. package/THIRD-PARTY-NOTICES +906 -14
  4. package/android/build.gradle +6 -1
  5. package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +126 -0
  6. package/android/src/main/AndroidManifest.xml +0 -5
  7. package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +106 -0
  8. package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +217 -0
  9. package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +48 -824
  10. package/android/src/main/java/dev/croche/reactnative/NativePal.kt +68 -0
  11. package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
  12. package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +64 -41
  13. package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +64 -0
  14. package/android/src/test/java/dev/croche/reactnative/BlobFilesTest.kt +107 -0
  15. package/android/src/test/java/dev/croche/reactnative/ImageAdapterWireTest.kt +70 -0
  16. package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +44 -0
  17. package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +19 -32
  18. package/android/src/test/java/dev/croche/reactnative/ProjectHandlerStagesTest.kt +32 -0
  19. package/android/src/test/java/dev/croche/reactnative/ProjectRecoveryTest.kt +12 -0
  20. package/android/src/test/java/dev/croche/reactnative/ProjectTimeZoneTest.kt +11 -15
  21. package/android/src/test/java/dev/croche/reactnative/StorageVersionWireTest.kt +37 -0
  22. package/android/src/test/java/dev/croche/reactnative/VerificationDispatchTest.kt +24 -0
  23. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
  24. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +10 -8
  25. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
  26. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +55 -0
  27. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -0
  28. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageCodec.kt +1 -0
  29. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageInput.kt +15 -0
  30. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapOrientation.kt +40 -0
  31. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapPalCodec.kt +66 -0
  32. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidAssetPal.kt +30 -0
  33. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreFactory.kt +35 -0
  34. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +116 -0
  35. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt +17 -0
  36. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +17 -0
  37. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidS2Pal.kt +14 -0
  38. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStateStore.kt +216 -0
  39. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +108 -0
  40. package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +8 -2
  41. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Adapters.kt +75 -0
  42. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
  43. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +31 -0
  44. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +25 -1
  45. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +121 -103
  46. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +89 -103
  47. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +20 -360
  48. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +136 -0
  49. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Decision.kt +170 -0
  50. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +23 -0
  51. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +26 -21
  52. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/EncodedImageCodec.kt +51 -0
  53. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +715 -0
  54. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +9 -322
  55. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
  56. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +16 -28
  57. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeEvent.kt +59 -0
  58. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeHandleOwner.kt +118 -0
  59. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +10 -74
  60. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +294 -37
  61. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +127 -0
  62. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
  63. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +73 -0
  64. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
  65. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StateStore.kt +90 -0
  66. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +88 -24
  67. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +5723 -3088
  68. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
  69. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +96 -0
  70. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundOsRegistration.kt +35 -0
  71. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundSink.kt +37 -0
  72. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreDefinitionCalls.kt +35 -0
  73. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +30 -0
  74. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +41 -0
  75. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +35 -0
  76. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +127 -0
  77. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ImageAdapterHost.kt +28 -0
  78. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +70 -0
  79. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStateStore.kt +125 -0
  80. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +64 -0
  81. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
  82. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativeCoreInitialization.kt +21 -0
  83. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +60 -0
  84. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PalCallbacks.kt +27 -0
  85. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PlainToolJson.kt +56 -0
  86. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +43 -0
  87. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
  88. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StateStoreContract.kt +103 -0
  89. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StorageVersionContract.kt +40 -0
  90. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
  91. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +100 -0
  92. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/BlobFiles.kt +157 -0
  93. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -0
  94. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/InstancePayloadPurge.kt +79 -0
  95. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/VersionedKeyFiles.kt +71 -0
  96. package/android/vendor/jniLibs/arm64-v8a/libcroche_ffi.so +0 -0
  97. package/android/vendor/jniLibs/armeabi-v7a/libcroche_ffi.so +0 -0
  98. package/android/vendor/jniLibs/x86_64/libcroche_ffi.so +0 -0
  99. package/croche-react-native.podspec +1 -0
  100. package/dist/package.json +1 -0
  101. package/dist/react-native/src/NativeCrocheRuntime.d.ts +16 -0
  102. package/dist/react-native/src/NativeCrocheRuntime.js +2 -0
  103. package/dist/react-native/src/core-native.d.ts +6 -0
  104. package/dist/react-native/src/core-native.js +316 -0
  105. package/dist/react-native/src/core-wire.d.ts +55 -0
  106. package/dist/react-native/src/core-wire.js +1 -0
  107. package/dist/react-native/src/native-module.d.ts +8 -0
  108. package/dist/react-native/src/native-module.js +53 -0
  109. package/dist/rn-shared-build.json +16 -0
  110. package/dist/runtime/src/adapters.d.ts +8 -0
  111. package/dist/runtime/src/adapters.js +29 -0
  112. package/dist/runtime/src/agent.d.ts +91 -0
  113. package/dist/runtime/src/agent.js +137 -0
  114. package/dist/runtime/src/app-api.d.ts +572 -0
  115. package/dist/runtime/src/app-api.js +1 -0
  116. package/dist/runtime/src/cancel.d.ts +9 -0
  117. package/dist/runtime/src/cancel.js +27 -0
  118. package/dist/runtime/src/core/foreground.d.ts +3 -0
  119. package/dist/runtime/src/core/foreground.js +40 -0
  120. package/dist/runtime/src/core/port.d.ts +247 -0
  121. package/dist/runtime/src/core/port.js +1 -0
  122. package/dist/runtime/src/core/wasm-owner.d.ts +32 -0
  123. package/dist/runtime/src/core/wasm-owner.js +237 -0
  124. package/dist/runtime/src/core/wasm.d.ts +1 -0
  125. package/dist/runtime/src/core/wasm.js +2 -0
  126. package/dist/runtime/src/croche.d.ts +97 -0
  127. package/dist/runtime/src/croche.js +172 -0
  128. package/dist/runtime/src/errors.d.ts +33 -0
  129. package/dist/runtime/src/errors.js +63 -0
  130. package/dist/runtime/src/events.d.ts +5 -0
  131. package/dist/runtime/src/events.js +31 -0
  132. package/dist/runtime/src/generated/app-stream.d.ts +328 -0
  133. package/dist/runtime/src/generated/app-stream.js +127 -0
  134. package/dist/runtime/src/image-input.d.ts +5 -0
  135. package/dist/runtime/src/image-input.js +60 -0
  136. package/dist/runtime/src/index.d.ts +6 -0
  137. package/dist/runtime/src/index.js +5 -0
  138. package/dist/runtime/src/index.native.d.ts +1 -0
  139. package/dist/runtime/src/index.native.js +2 -0
  140. package/dist/runtime/src/json.d.ts +13 -0
  141. package/dist/runtime/src/json.js +120 -0
  142. package/dist/runtime/src/native-adapter.d.ts +3 -0
  143. package/dist/runtime/src/native-adapter.js +30 -0
  144. package/dist/runtime/src/pal/host.d.ts +5 -0
  145. package/dist/runtime/src/pal/host.js +60 -0
  146. package/dist/runtime/src/pal/platform.d.ts +21 -0
  147. package/dist/runtime/src/pal/platform.js +7 -0
  148. package/dist/runtime/src/pal/state-store-lock.d.ts +8 -0
  149. package/dist/runtime/src/pal/state-store-lock.js +2 -0
  150. package/dist/runtime/src/pal/state-store.d.ts +66 -0
  151. package/dist/runtime/src/pal/state-store.js +1 -0
  152. package/dist/runtime/src/pal/storage-version-contract.d.ts +19 -0
  153. package/dist/runtime/src/pal/storage-version-contract.js +68 -0
  154. package/dist/runtime/src/pal/transactional-storage.d.ts +3 -0
  155. package/dist/runtime/src/pal/transactional-storage.js +124 -0
  156. package/dist/runtime/src/rn-entry.d.ts +1 -0
  157. package/dist/runtime/src/rn-entry.js +4 -0
  158. package/dist/runtime/src/run-tool.d.ts +33 -0
  159. package/dist/runtime/src/run-tool.js +7 -0
  160. package/dist/runtime/src/tools.d.ts +35 -0
  161. package/dist/runtime/src/tools.js +94 -0
  162. package/ios/CoreAgentTransport.swift +14 -0
  163. package/ios/CoreDeliveryTransport.swift +58 -0
  164. package/ios/CoreNativePal.swift +98 -0
  165. package/ios/CorePlatformTransport.swift +44 -0
  166. package/ios/CoreWireDispatch.swift +122 -0
  167. package/ios/CoreWireSupport.swift +286 -0
  168. package/ios/CrocheRuntimeBridge.swift +34 -994
  169. package/ios/ProjectPal.swift +49 -0
  170. package/ios/RNCrocheRuntime.mm +18 -201
  171. package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
  172. package/ios/vendor/CrocheRuntimeSDK/BlobFiles.swift +124 -0
  173. package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +60 -0
  174. package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
  175. package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
  176. package/ios/vendor/CrocheRuntimeSDK/EncodedImages.swift +85 -0
  177. package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +26 -157
  178. package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +672 -0
  179. package/ios/vendor/CrocheRuntimeSDK/Host.swift +34 -371
  180. package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
  181. package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
  182. package/ios/vendor/CrocheRuntimeSDK/InstancePayloadPurge.swift +88 -0
  183. package/ios/vendor/CrocheRuntimeSDK/JSON.swift +30 -14
  184. package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
  185. package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +1 -155
  186. package/ios/vendor/CrocheRuntimeSDK/Platform.swift +33 -59
  187. package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +289 -0
  188. package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +302 -0
  189. package/ios/vendor/CrocheRuntimeSDK/PlatformStateStore.swift +253 -0
  190. package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +281 -0
  191. package/ios/vendor/CrocheRuntimeSDK/Project.swift +4 -269
  192. package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +24 -461
  193. package/ios/vendor/CrocheRuntimeSDK/RuntimeHTTPAdapter.swift +42 -0
  194. package/ios/vendor/CrocheRuntimeSDK/S1/S1Adapters.swift +110 -0
  195. package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +248 -0
  196. package/ios/vendor/CrocheRuntimeSDK/S1/S1Allowance.swift +20 -0
  197. package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
  198. package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +170 -0
  199. package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +313 -0
  200. package/ios/vendor/CrocheRuntimeSDK/S1/S1Decision.swift +146 -0
  201. package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +37 -0
  202. package/ios/vendor/CrocheRuntimeSDK/S1/S1HandleOwner.swift +58 -0
  203. package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +346 -0
  204. package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +89 -0
  205. package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +165 -0
  206. package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +100 -0
  207. package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +278 -0
  208. package/ios/vendor/CrocheRuntimeSDK/S1/S1Verification.swift +27 -0
  209. package/ios/vendor/CrocheRuntimeSDK/S1/StateStoreContract.swift +189 -0
  210. package/ios/vendor/CrocheRuntimeSDK/S1/StorageVersionContract.swift +68 -0
  211. package/ios/vendor/CrocheRuntimeSDK/Storage.swift +23 -269
  212. package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +163 -116
  213. package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
  214. package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +4712 -2469
  215. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +899 -608
  216. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/libcroche_ffi.a +0 -0
  217. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/Headers/croche_ffiFFI.h +899 -608
  218. package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +0 -0
  219. package/package.json +18 -8
  220. package/scripts/README.md +11 -0
  221. package/scripts/build-shared.mjs +46 -0
  222. package/scripts/build.mjs +11 -0
  223. package/scripts/check-swift-vendor-modules.sh +15 -0
  224. package/scripts/vendor-native.sh +8 -6
  225. package/src/NativeCrocheRuntime.ts +12 -159
  226. package/android/src/main/java/dev/croche/reactnative/AgentMemory.kt +0 -25
  227. package/android/src/main/java/dev/croche/reactnative/ProjectAttestation.kt +0 -17
  228. package/android/src/main/java/dev/croche/reactnative/ProjectTimeZone.kt +0 -14
  229. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/CrocheProjectRecordProvider.kt +0 -32
  230. package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/WorkManagerScheduler.kt +0 -146
  231. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +0 -110
  232. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Control.kt +0 -178
  233. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +0 -173
  234. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
  235. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +0 -25
  236. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
  237. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +0 -87
  238. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +0 -181
  239. package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +0 -227
  240. package/ios/vendor/CrocheRuntimeSDK/Background.swift +0 -59
  241. package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
  242. package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +0 -453
  243. package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +0 -38
  244. package/src/index.ts +0 -664
  245. package/src/project-types.ts +0 -35
  246. package/src/project.ts +0 -434
  247. package/src/protocol.ts +0 -28
  248. package/src/types.ts +0 -9
@@ -0,0 +1,672 @@
1
+ // GENERATED by scripts/generate-app-stream.mjs from the Rust app contract. Do not edit.
2
+ import Foundation
3
+
4
+ /// The app-stream reader types, generated from Core's Rust contract. Core validates and emits them; the SDK only decodes.
5
+ public enum AppStreamContract {
6
+ /// Status of an accepted input: `queued` or `seen`, then one terminal status.
7
+ public enum InputDisposition: Codable, Sendable, Hashable {
8
+ /// Accepted and waiting for a turn.
9
+ case queued
10
+ /// Taken up by a turn for the agent to work on.
11
+ case seen
12
+ /// A completed reply answered the input.
13
+ case answered
14
+ /// The work finished without a reply answering the input.
15
+ case noReply
16
+ /// The input failed; its error says why.
17
+ case failed
18
+ /// The input was cancelled.
19
+ case cancelled
20
+ /// A value this SDK does not know, kept as `type` (`fields` holds the same string).
21
+ case unknown(type: String, fields: JSON)
22
+ /// The wire string.
23
+ public var rawValue: String {
24
+ switch self {
25
+ case .queued: return "queued"
26
+ case .seen: return "seen"
27
+ case .answered: return "answered"
28
+ case .noReply: return "no_reply"
29
+ case .failed: return "failed"
30
+ case .cancelled: return "cancelled"
31
+ case .unknown(let type, _): return type
32
+ }
33
+ }
34
+ /// Decodes the wire string. An unrecognized string becomes `unknown`.
35
+ public init(from decoder: Decoder) throws {
36
+ let value = try decoder.singleValueContainer().decode(String.self)
37
+ switch value {
38
+ case "queued": self = .queued
39
+ case "seen": self = .seen
40
+ case "answered": self = .answered
41
+ case "no_reply": self = .noReply
42
+ case "failed": self = .failed
43
+ case "cancelled": self = .cancelled
44
+ default: self = .unknown(type: value, fields: .string(value))
45
+ }
46
+ }
47
+ /// Encodes `rawValue`.
48
+ public func encode(to encoder: Encoder) throws {
49
+ var value = encoder.singleValueContainer()
50
+ try value.encode(rawValue)
51
+ }
52
+ }
53
+ /// Kind of an input's parts.
54
+ public enum InputKind: Codable, Sendable, Hashable {
55
+ /// Text from the person using the app.
56
+ case user
57
+ /// An event the app reported.
58
+ case appEvent
59
+ /// Opaque app state text.
60
+ case appState
61
+ /// A value this SDK does not know, kept as `type` (`fields` holds the same string).
62
+ case unknown(type: String, fields: JSON)
63
+ /// The wire string.
64
+ public var rawValue: String {
65
+ switch self {
66
+ case .user: return "user"
67
+ case .appEvent: return "app_event"
68
+ case .appState: return "app_state"
69
+ case .unknown(let type, _): return type
70
+ }
71
+ }
72
+ /// Decodes the wire string. An unrecognized string becomes `unknown`.
73
+ public init(from decoder: Decoder) throws {
74
+ let value = try decoder.singleValueContainer().decode(String.self)
75
+ switch value {
76
+ case "user": self = .user
77
+ case "app_event": self = .appEvent
78
+ case "app_state": self = .appState
79
+ default: self = .unknown(type: value, fields: .string(value))
80
+ }
81
+ }
82
+ /// Encodes `rawValue`.
83
+ public func encode(to encoder: Encoder) throws {
84
+ var value = encoder.singleValueContainer()
85
+ try value.encode(rawValue)
86
+ }
87
+ }
88
+ /// How an agent reply ended.
89
+ public enum MessageStatus: Codable, Sendable, Hashable {
90
+ /// The reply finished and answers its open inputs.
91
+ case completed
92
+ /// The reply ended because its call or turn failed.
93
+ case failed
94
+ /// The reply ended because its work was cancelled.
95
+ case cancelled
96
+ /// The process stopped during the reply.
97
+ case interrupted
98
+ /// A value this SDK does not know, kept as `type` (`fields` holds the same string).
99
+ case unknown(type: String, fields: JSON)
100
+ /// The wire string.
101
+ public var rawValue: String {
102
+ switch self {
103
+ case .completed: return "completed"
104
+ case .failed: return "failed"
105
+ case .cancelled: return "cancelled"
106
+ case .interrupted: return "interrupted"
107
+ case .unknown(let type, _): return type
108
+ }
109
+ }
110
+ /// Decodes the wire string. An unrecognized string becomes `unknown`.
111
+ public init(from decoder: Decoder) throws {
112
+ let value = try decoder.singleValueContainer().decode(String.self)
113
+ switch value {
114
+ case "completed": self = .completed
115
+ case "failed": self = .failed
116
+ case "cancelled": self = .cancelled
117
+ case "interrupted": self = .interrupted
118
+ default: self = .unknown(type: value, fields: .string(value))
119
+ }
120
+ }
121
+ /// Encodes `rawValue`.
122
+ public func encode(to encoder: Encoder) throws {
123
+ var value = encoder.singleValueContainer()
124
+ try value.encode(rawValue)
125
+ }
126
+ }
127
+ /// Whether the agent has work.
128
+ public enum AgentStatus: Codable, Sendable, Hashable {
129
+ /// Nothing to do and nothing to receive.
130
+ case idle
131
+ /// A turn runs, an accepted input is queued, or a tool result waits to continue the turn.
132
+ case running
133
+ /// Nothing to do, but a late tool result is still to arrive.
134
+ case waiting
135
+ /// A value this SDK does not know, kept as `type` (`fields` holds the same string).
136
+ case unknown(type: String, fields: JSON)
137
+ /// The wire string.
138
+ public var rawValue: String {
139
+ switch self {
140
+ case .idle: return "idle"
141
+ case .running: return "running"
142
+ case .waiting: return "waiting"
143
+ case .unknown(let type, _): return type
144
+ }
145
+ }
146
+ /// Decodes the wire string. An unrecognized string becomes `unknown`.
147
+ public init(from decoder: Decoder) throws {
148
+ let value = try decoder.singleValueContainer().decode(String.self)
149
+ switch value {
150
+ case "idle": self = .idle
151
+ case "running": self = .running
152
+ case "waiting": self = .waiting
153
+ default: self = .unknown(type: value, fields: .string(value))
154
+ }
155
+ }
156
+ /// Encodes `rawValue`.
157
+ public func encode(to encoder: Encoder) throws {
158
+ var value = encoder.singleValueContainer()
159
+ try value.encode(rawValue)
160
+ }
161
+ }
162
+ /// Why a model call other than an ordinary reply call was made.
163
+ public enum TurnPurpose: Codable, Sendable, Hashable {
164
+ /// The call made a context summary.
165
+ case summary
166
+ /// A value this SDK does not know, kept as `type` (`fields` holds the same string).
167
+ case unknown(type: String, fields: JSON)
168
+ /// The wire string.
169
+ public var rawValue: String {
170
+ switch self {
171
+ case .summary: return "summary"
172
+ case .unknown(let type, _): return type
173
+ }
174
+ }
175
+ /// Decodes the wire string. An unrecognized string becomes `unknown`.
176
+ public init(from decoder: Decoder) throws {
177
+ let value = try decoder.singleValueContainer().decode(String.self)
178
+ switch value {
179
+ case "summary": self = .summary
180
+ default: self = .unknown(type: value, fields: .string(value))
181
+ }
182
+ }
183
+ /// Encodes `rawValue`.
184
+ public func encode(to encoder: Encoder) throws {
185
+ var value = encoder.singleValueContainer()
186
+ try value.encode(rawValue)
187
+ }
188
+ }
189
+ /// A memory operation the app can make.
190
+ public enum MemoryOp: Codable, Sendable, Hashable {
191
+ /// One memory fact was deleted.
192
+ case delete
193
+ /// All memory facts were deleted.
194
+ case clear
195
+ /// Memory was turned on or off.
196
+ case enabled
197
+ /// A value this SDK does not know, kept as `type` (`fields` holds the same string).
198
+ case unknown(type: String, fields: JSON)
199
+ /// The wire string.
200
+ public var rawValue: String {
201
+ switch self {
202
+ case .delete: return "delete"
203
+ case .clear: return "clear"
204
+ case .enabled: return "enabled"
205
+ case .unknown(let type, _): return type
206
+ }
207
+ }
208
+ /// Decodes the wire string. An unrecognized string becomes `unknown`.
209
+ public init(from decoder: Decoder) throws {
210
+ let value = try decoder.singleValueContainer().decode(String.self)
211
+ switch value {
212
+ case "delete": self = .delete
213
+ case "clear": self = .clear
214
+ case "enabled": self = .enabled
215
+ default: self = .unknown(type: value, fields: .string(value))
216
+ }
217
+ }
218
+ /// Encodes `rawValue`.
219
+ public func encode(to encoder: Encoder) throws {
220
+ var value = encoder.singleValueContainer()
221
+ try value.encode(rawValue)
222
+ }
223
+ }
224
+ /// Metadata of a stored image original that Core keeps for the app, a message or a tool call.
225
+ public struct Attachment: Codable, Sendable {
226
+ /// Core-assigned attachment id, used to read, copy, release or delete the original.
227
+ public let id: String
228
+ /// What the attachment is; Core currently stores only `image` (open string).
229
+ public let kind: String
230
+ /// Where the stored original came from, for example `tool` (produced by a tool call) or `app` (copied in by the app).
231
+ public let origin: String
232
+ /// Image media type of the stored original, for example `image/png`.
233
+ public let mediaType: String
234
+ /// Size of the stored original in bytes.
235
+ public let bytes: UInt64
236
+ /// Lowercase hex SHA-256 of the stored original's bytes.
237
+ public let sha256: String
238
+ /// The original's pixel width, when Core reports it; absent otherwise.
239
+ public let width: UInt64?
240
+ /// The original's pixel height, when Core reports it; absent otherwise.
241
+ public let height: UInt64?
242
+ /// Description text supplied with the image; absent when none was given.
243
+ public let alt: String?
244
+ /// Id of the agent tool call that produced the original (the same id as `ToolEvent.id`); absent for originals not produced by a call.
245
+ public let callId: String?
246
+ /// Present only as `true` when the original is no longer stored at read time; the logged row itself is not changed.
247
+ public let deleted: Bool?
248
+ /// Why the original is gone: `deleted` (explicitly deleted or missing), `evicted` (removed to make room or past retention) or `cleared` (released by a history clear); present only with `deleted`.
249
+ public let deletedReason: String?
250
+ }
251
+ /// An error carried by an event or result, with the category that tells the app how to react.
252
+ public struct EventError: Codable, Sendable {
253
+ /// Error code, for example `store_full` or `interrupted`; a code outside the known table has category `internal`.
254
+ public let code: String
255
+ /// What the app does about the error.
256
+ public let category: ErrorCategory
257
+ /// Human-readable explanation of the error.
258
+ public let message: String
259
+ /// Integer seconds, present only on the hourly app-paid rate_limited refusal.
260
+ public let retryAfter: UInt64?
261
+ }
262
+ /// An image that could not be viewed for one model call; the call continues with the image's id and description.
263
+ public struct ViewError: Codable, Sendable {
264
+ /// Id of the attachment whose view failed.
265
+ public let attachmentId: String
266
+ /// Error code for the failed view, for example `image_unsupported`.
267
+ public let code: String
268
+ /// What the app does about the error.
269
+ public let category: ErrorCategory
270
+ /// Human-readable explanation of why the view failed.
271
+ public let message: String
272
+ }
273
+ /// Returned when Core accepts a send: the input's id, its status at acceptance and where its events start in the stream.
274
+ public struct SendReceipt: Codable, Sendable {
275
+ /// Id of the accepted input.
276
+ public let requestId: String
277
+ /// The input's status when the receipt was made; `queued` for a newly accepted input, or the recorded status when the same id was sent again.
278
+ public let status: InputDisposition
279
+ /// Stream sequence of the input's first event, or for a just-accepted input the position its events start from.
280
+ public let seq: UInt64
281
+ /// Why the input failed; present when `status` is `failed`.
282
+ public let error: EventError?
283
+ }
284
+ /// The terminal result of an accepted `request`: the input's final status and the replies it caused.
285
+ public struct RequestResult: Codable, Sendable {
286
+ /// Id of the input this result is for.
287
+ public let id: String
288
+ /// The input's terminal status.
289
+ public let status: InputDisposition
290
+ /// The retained `message.end` events whose `causedBy` includes this input.
291
+ public let messages: [MessageEnd]
292
+ /// Present only as `true` when some of this input's events were already pruned, so `messages` can be incomplete or empty.
293
+ public let gap: Bool?
294
+ /// Why the input failed; always present when `status` is `failed`.
295
+ public let error: EventError?
296
+ }
297
+ /// An input was accepted or its status changed; each change re-emits the input's full current state.
298
+ public struct InputEvent: Codable, Sendable {
299
+ /// Id of the input.
300
+ public let id: String
301
+ /// Stream sequence of this event.
302
+ public let seq: UInt64
303
+ /// Stream sequence of this input's first event; the same on every update of the input.
304
+ public let firstSeq: UInt64
305
+ /// Which kind of input parts this row shows.
306
+ public let kind: InputKind
307
+ /// The input's status after this change.
308
+ public let status: InputDisposition
309
+ /// ISO 8601 time of this change.
310
+ public let at: String
311
+ /// Text of the input's parts of this `kind`, joined with blank lines; absent when Core holds no summary for the input.
312
+ public let text: String?
313
+ /// Why the input failed; always present when `status` is `failed` and absent otherwise.
314
+ public let error: EventError?
315
+ }
316
+ /// A held send left without running; Core emits it after reopen for a send that was held when the app stopped.
317
+ public struct InputDropped: Codable, Sendable {
318
+ /// Id of the dropped input.
319
+ public let inputId: String
320
+ /// Why the input was dropped; Core's stream emits `reopened` (open string).
321
+ public let reason: String
322
+ /// Stream sequence of this event.
323
+ public let seq: UInt64
324
+ /// Stream sequence of the input's first event when it had one, otherwise this event's own sequence.
325
+ public let firstSeq: UInt64
326
+ }
327
+ /// An agent reply began; Core emits it when the reply has its first visible text, before any of its deltas.
328
+ public struct MessageStart: Codable, Sendable {
329
+ /// Id of the reply message, shared by its start, deltas and end.
330
+ public let id: String
331
+ /// Stream sequence of this event.
332
+ public let seq: UInt64
333
+ /// Stream sequence of the message's first event, which is this start.
334
+ public let firstSeq: UInt64
335
+ /// ISO 8601 time the reply started.
336
+ public let at: String
337
+ }
338
+ /// New text of the open reply as it is written; a preview only, since `message.end` carries the stored text.
339
+ public struct MessageDelta: Codable, Sendable {
340
+ /// Id of the open reply message.
341
+ public let id: String
342
+ /// Text added to the reply since the previous delta.
343
+ public let text: String
344
+ }
345
+ /// The end of an agent reply with its stored text and final status; never rewritten.
346
+ public struct MessageEnd: Codable, Sendable {
347
+ /// Id of the reply message.
348
+ public let id: String
349
+ /// Stream sequence of this event.
350
+ public let seq: UInt64
351
+ /// Stream sequence of the message's first event (its `message.start`).
352
+ public let firstSeq: UInt64
353
+ /// ISO 8601 time the reply ended.
354
+ public let at: String
355
+ /// How the reply ended.
356
+ public let status: MessageStatus
357
+ /// The stored reply text with markers removed; empty after an interruption or when the reply was too large to store.
358
+ public let text: String
359
+ /// Names of the registered markers found in the reply, in reply order.
360
+ public let markers: [String]
361
+ /// Ids of the inputs the turn producing this reply worked for.
362
+ public let causedBy: [String]
363
+ /// Ids of the inputs this reply answered; only a `completed` reply answers inputs, so it is empty otherwise.
364
+ public let inReplyTo: [String]
365
+ /// The reply before marker removal; equal to `text` when markers did not change it.
366
+ public let raw: String
367
+ /// The checked structured-output value of a reply made with a response format; absent otherwise.
368
+ public let output: JSON?
369
+ /// Why the reply did not complete; always present when `status` is `failed`.
370
+ public let error: EventError?
371
+ /// Originals attached to this reply; absent when there are none.
372
+ public let attachments: [Attachment]?
373
+ }
374
+ /// One model call was recorded, with its usage, cost and model; Core writes it when it records the call's cost.
375
+ public struct TurnEvent: Codable, Sendable {
376
+ /// Opaque id of the model call; do not parse it.
377
+ public let id: String
378
+ /// Ids of the inputs first seen by this call.
379
+ public let inputs: [String]
380
+ /// Ids of every input this call worked for.
381
+ public let causedBy: [String]
382
+ /// Token counts reported for the call (`prompt`, `completion`, `total`, `cached`, `cacheWrite`, `reasoning` when reported), or null when none were reported.
383
+ public let usage: JSON
384
+ /// Cost of the call in credits; null when unknown.
385
+ public let cost: Double?
386
+ /// How final `cost` is, for example `reported` (final), `unknown` (null cost), `estimated` (provisional) or `none` (zero cost).
387
+ public let costStatus: String
388
+ /// The model recorded for the call; the requested model when no response named one.
389
+ public let model: String
390
+ /// ISO 8601 time the call was recorded.
391
+ public let at: String
392
+ /// `summary` when the call made a context summary; absent for an ordinary model call.
393
+ public let purpose: TurnPurpose?
394
+ /// Reported serving-provider label; absent when unavailable. Diagnostic only; never changes pricing or settlement.
395
+ public let provider: String?
396
+ /// Images whose view failed; the call continues with their id and description.
397
+ public let viewErrors: [ViewError]?
398
+ }
399
+ /// An agent tool call started or changed status; updates keep the same id and `firstSeq`.
400
+ public struct ToolEvent: Codable, Sendable {
401
+ /// Opaque Core-assigned call id, the same as `ToolContext.callId` and an attachment's `callId`; do not parse it.
402
+ public let id: String
403
+ /// Stream sequence of this event.
404
+ public let seq: UInt64
405
+ /// Stream sequence of the call's first event; the same on every update of the call.
406
+ public let firstSeq: UInt64
407
+ /// Who made the call; Core emits `agent` for these events (open string).
408
+ public let origin: String
409
+ /// Ids of the inputs the call works for.
410
+ public let causedBy: [String]
411
+ /// Name of the called tool.
412
+ public let tool: String
413
+ /// Stable command key of the called command.
414
+ public let key: String
415
+ /// Arguments of the call, without app-bound arguments; `{}` when there are none.
416
+ public let args: JSON
417
+ /// Status of the call: `running`, `waiting` (its result arrives later), `completed`, `failed`, `cancelled` or `unused` (a late result whose cause was cancelled) (open string).
418
+ public let status: String
419
+ /// Cost of the call in credits; null while running or when unknown, 0 for an unpaid call.
420
+ public let cost: Double?
421
+ /// How final `cost` is, for example `unknown` while running, `reported`, or `none` for an unpaid call.
422
+ public let costStatus: String
423
+ /// ISO 8601 time the call started.
424
+ public let startedAt: String
425
+ /// ISO 8601 time of this update.
426
+ public let at: String
427
+ /// Id of the `turn` event for the model call that requested this tool call; absent when not recorded.
428
+ public let requestedBy: String?
429
+ /// What the model was given as the result: `{text}`, plus `images` metadata (`mediaType`, `bytes`, `attachment`) for result images; absent while running, and removed when Core closes an unsettled call as failed or cancelled.
430
+ public let delivered: JSON?
431
+ /// Originals the call kept as attachments, as `{attachments: [{id, mediaType, bytes}]}`; absent when none.
432
+ public let outputs: JSON?
433
+ }
434
+ /// The agent's current state: status, queue length, typing, and what running work is doing.
435
+ public struct StateEvent: Codable, Sendable {
436
+ /// Whether the agent is idle, running or waiting.
437
+ public let status: AgentStatus
438
+ /// Number of accepted sends waiting in the queue.
439
+ public let queued: UInt64
440
+ /// True while a reply's `message.start` is written and its `message.end` is not.
441
+ public let typing: Bool
442
+ /// What running work is doing, for example `model`, `summary` or `tool`; null unless `status` is `running`.
443
+ public let activity: String?
444
+ /// Names of the tools in the current round, in call order (built-in or app command key, else `unknown`); present only when `activity` is `tool`.
445
+ public let tools: [String]?
446
+ }
447
+ /// The subscription's starting point: the current log, state and the reply being streamed; delivered first on attach.
448
+ public struct SnapshotEvent: Codable, Sendable {
449
+ /// Id of the log the subscription reads; pass it back with a `seq` to resume.
450
+ public let logId: String
451
+ /// The current `state` event.
452
+ public let state: JSON
453
+ /// The reply being streamed now as `{messageId, firstSeq, text}`, or null when none.
454
+ public let streaming: JSON
455
+ }
456
+ /// An older page of the conversation, delivered after `subscription.history()`.
457
+ public struct PageEvent: Codable, Sendable {
458
+ /// The page's rows oldest first, each as its latest event.
459
+ public let messages: [AgentEvent]
460
+ /// True when no older retained row was left out of this page.
461
+ public let reachedFloor: Bool
462
+ /// Id of the log the page was read from.
463
+ public let logId: String
464
+ /// Stream sequence of the latest history clear, or 0 when there was none.
465
+ public let clearedSeq: UInt64
466
+ }
467
+ /// A full `clearHistory()` took effect; empty the displayed conversation.
468
+ public struct ClearedEvent: Codable, Sendable {
469
+ /// Core-assigned id of this clear.
470
+ public let id: String
471
+ /// ISO 8601 time the clear took effect.
472
+ public let at: String
473
+ }
474
+ /// A memory delete, clear or enable change took effect.
475
+ public struct MemoryEvent: Codable, Sendable {
476
+ /// Core-assigned id of the memory operation.
477
+ public let id: String
478
+ /// Which memory operation took effect.
479
+ public let op: MemoryOp
480
+ /// ISO 8601 time the operation took effect.
481
+ public let at: String
482
+ /// Id of the deleted memory fact; present only for `delete`.
483
+ public let item: String?
484
+ /// The new memory setting; present only for `enabled`.
485
+ public let enabled: Bool?
486
+ /// Number of memory facts removed; present for `delete` and `clear`.
487
+ public let removed: UInt64?
488
+ }
489
+ /// Events were missed; redraw from a `page`.
490
+ public struct GapEvent: Codable, Sendable {
491
+ /// Last stream sequence before the missed range.
492
+ public let afterSeq: UInt64
493
+ /// Last missed stream sequence (inclusive).
494
+ public let throughSeq: UInt64
495
+ /// Why the events are missing: `limit` (retention pruned them) or `cleared` (a history clear removed them).
496
+ public let reason: String
497
+ }
498
+ /// The subscription ended with an error; it receives no further events.
499
+ public struct SubscriptionError: Codable, Sendable {
500
+ /// Error code, for example `storage_error`.
501
+ public let code: String
502
+ /// Human-readable explanation of the error.
503
+ public let message: String
504
+ /// Present only as `true` when a storage failure stopped the agent from writing; reopen to continue.
505
+ public let poisoned: Bool?
506
+ }
507
+ /// The subscription ended because a newer subscription replaced it.
508
+ public struct ReplacedEvent: Codable, Sendable {
509
+
510
+ }
511
+ /// One subscription event, by its wire `type`. Ignore or log `unknown`.
512
+ public indirect enum AgentEvent: Codable, Sendable {
513
+ /// The `input` event: see ``InputEvent``.
514
+ case input(InputEvent)
515
+ /// The `input.dropped` event: see ``InputDropped``.
516
+ case inputDropped(InputDropped)
517
+ /// The `message.start` event: see ``MessageStart``.
518
+ case messageStart(MessageStart)
519
+ /// The `message.delta` event: see ``MessageDelta``.
520
+ case messageDelta(MessageDelta)
521
+ /// The `message.end` event: see ``MessageEnd``.
522
+ case messageEnd(MessageEnd)
523
+ /// The `turn` event: see ``TurnEvent``.
524
+ case turn(TurnEvent)
525
+ /// The `tool` event: see ``ToolEvent``.
526
+ case tool(ToolEvent)
527
+ /// The `state` event: see ``StateEvent``.
528
+ case state(StateEvent)
529
+ /// The `snapshot` event: see ``SnapshotEvent``.
530
+ case snapshot(SnapshotEvent)
531
+ /// The `page` event: see ``PageEvent``.
532
+ case page(PageEvent)
533
+ /// The `cleared` event: see ``ClearedEvent``.
534
+ case cleared(ClearedEvent)
535
+ /// The `memory` event: see ``MemoryEvent``.
536
+ case memory(MemoryEvent)
537
+ /// The `gap` event: see ``GapEvent``.
538
+ case gap(GapEvent)
539
+ /// The `error` event: see ``SubscriptionError``.
540
+ case error(SubscriptionError)
541
+ /// The `replaced` event: see ``ReplacedEvent``.
542
+ case replaced(ReplacedEvent)
543
+ /// An event type this SDK does not know, with every field kept.
544
+ case unknown(type: String, fields: [String: JSON])
545
+ private enum Tag: String, CodingKey { case type }
546
+ private struct FieldKey: CodingKey {
547
+ let stringValue: String
548
+ var intValue: Int? { nil }
549
+ init(stringValue: String) { self.stringValue = stringValue }
550
+ init?(intValue: Int) { nil }
551
+ }
552
+ /// Decodes an event by its `type`. An unrecognized type becomes `unknown`.
553
+ public init(from decoder: Decoder) throws {
554
+ let tag = try decoder.container(keyedBy: Tag.self).decode(String.self, forKey: .type)
555
+ switch tag {
556
+ case "input": self = .input(try InputEvent(from: decoder))
557
+ case "input.dropped": self = .inputDropped(try InputDropped(from: decoder))
558
+ case "message.start": self = .messageStart(try MessageStart(from: decoder))
559
+ case "message.delta": self = .messageDelta(try MessageDelta(from: decoder))
560
+ case "message.end": self = .messageEnd(try MessageEnd(from: decoder))
561
+ case "turn": self = .turn(try TurnEvent(from: decoder))
562
+ case "tool": self = .tool(try ToolEvent(from: decoder))
563
+ case "state": self = .state(try StateEvent(from: decoder))
564
+ case "snapshot": self = .snapshot(try SnapshotEvent(from: decoder))
565
+ case "page": self = .page(try PageEvent(from: decoder))
566
+ case "cleared": self = .cleared(try ClearedEvent(from: decoder))
567
+ case "memory": self = .memory(try MemoryEvent(from: decoder))
568
+ case "gap": self = .gap(try GapEvent(from: decoder))
569
+ case "error": self = .error(try SubscriptionError(from: decoder))
570
+ case "replaced": self = .replaced(try ReplacedEvent(from: decoder))
571
+ default:
572
+ let value = try JSON(from: decoder)
573
+ self = .unknown(type: tag, fields: value.object ?? [:])
574
+ }
575
+ }
576
+ /// Encodes the event with its `type`.
577
+ public func encode(to encoder: Encoder) throws {
578
+ var container = encoder.container(keyedBy: Tag.self)
579
+ switch self {
580
+ case .input(let value):
581
+ try value.encode(to: encoder)
582
+ try container.encode("input", forKey: .type)
583
+ case .inputDropped(let value):
584
+ try value.encode(to: encoder)
585
+ try container.encode("input.dropped", forKey: .type)
586
+ case .messageStart(let value):
587
+ try value.encode(to: encoder)
588
+ try container.encode("message.start", forKey: .type)
589
+ case .messageDelta(let value):
590
+ try value.encode(to: encoder)
591
+ try container.encode("message.delta", forKey: .type)
592
+ case .messageEnd(let value):
593
+ try value.encode(to: encoder)
594
+ try container.encode("message.end", forKey: .type)
595
+ case .turn(let value):
596
+ try value.encode(to: encoder)
597
+ try container.encode("turn", forKey: .type)
598
+ case .tool(let value):
599
+ try value.encode(to: encoder)
600
+ try container.encode("tool", forKey: .type)
601
+ case .state(let value):
602
+ try value.encode(to: encoder)
603
+ try container.encode("state", forKey: .type)
604
+ case .snapshot(let value):
605
+ try value.encode(to: encoder)
606
+ try container.encode("snapshot", forKey: .type)
607
+ case .page(let value):
608
+ try value.encode(to: encoder)
609
+ try container.encode("page", forKey: .type)
610
+ case .cleared(let value):
611
+ try value.encode(to: encoder)
612
+ try container.encode("cleared", forKey: .type)
613
+ case .memory(let value):
614
+ try value.encode(to: encoder)
615
+ try container.encode("memory", forKey: .type)
616
+ case .gap(let value):
617
+ try value.encode(to: encoder)
618
+ try container.encode("gap", forKey: .type)
619
+ case .error(let value):
620
+ try value.encode(to: encoder)
621
+ try container.encode("error", forKey: .type)
622
+ case .replaced(let value):
623
+ try value.encode(to: encoder)
624
+ try container.encode("replaced", forKey: .type)
625
+ case .unknown(let type, let fields):
626
+ // Keyed, like the known cases: a single-value write after the keyed container
627
+ // drops the fields on Linux Foundation (Keel #861 08:40Z).
628
+ var any = encoder.container(keyedBy: FieldKey.self)
629
+ for (key, value) in fields where key != "type" { try any.encode(value, forKey: FieldKey(stringValue: key)) }
630
+ try container.encode(type, forKey: .type)
631
+ }
632
+ }
633
+ }
634
+ /// What the app does about an error. A code outside Core's table has category `internal`.
635
+ public enum ErrorCategory: String, Codable, Sendable {
636
+ /// A network or Croche Cloud failure. Retry later; the input is kept.
637
+ case offline
638
+ /// Credit or allowance ran out. Tell the user; retrying after a top-up works.
639
+ case allowance
640
+ /// Usage is stopped by the project, an account or a rate limit. Wait, or contact the operator.
641
+ case paused
642
+ /// A developer fix is needed: key, config, model or device identity.
643
+ case setup
644
+ /// The work or the wait stopped; nothing to fix. An aborted wait can leave the input running.
645
+ case closed
646
+ /// The app's request cannot run as given.
647
+ case invalid
648
+ /// The work failed for a reason outside the app. Report it. A code outside Core's table is also `internal`.
649
+ case `internal`
650
+ }
651
+ static func errorCategory(_ code: String) -> ErrorCategory {
652
+ if code.range(of: "^http_[1-5][0-9]{2}$", options: .regularExpression) != nil { return .offline }
653
+ switch code {
654
+ case "offline", "network_error", "request_timeout", "gateway_unavailable", "invalid_cloud_response", "stale_control", "http_<status>": return .offline
655
+ case "account_credit_exhausted", "project_budget_exhausted", "user_allowance_exhausted", "development_allowance_exhausted", "unverified_allowance_exhausted", "gateway_key_exhausted", "provider_credit_unavailable": return .allowance
656
+ case "usage_paused", "runtime_blocked", "project_access_disabled", "account_disabled", "server_key_revoked", "development_key_revoked", "development_device_limit", "rate_limited", "provider_rate_limited": return .paused
657
+ case "user_allowance_disabled", "invalid_configuration", "model_missing", "model_withdrawn", "model_unavailable", "project_key_unknown", "server_key_invalid", "development_key_invalid", "user_required", "gateway_key_rejected", "gateway_unsupported", "schema_unsupported", "capability_unsupported", "invalid_snapshot", "snapshot_mismatch", "invalid_tool", "invalid_wiki", "installation_unverified", "attestation_required", "attestation_rejected", "attestation_unverifiable", "attestation_failed", "attestation_unavailable", "attestation_unsupported", "attestation_timeout", "recovery_required", "host_unavailable", "core_unavailable": return .setup
658
+ case "state_conflict", "outcome_unknown", "closed", "target_closed", "runtime_detached", "cancelled", "interrupted", "preempted", "aborted", "observation_lost", "store_busy", "instance_busy": return .closed
659
+ case "invalid_input", "input_too_large", "invalid_memory", "invalid_response_format", "invalid_request", "tool_unauthorized", "agent_conflict", "unknown_id", "attachment_storage_full", "idempotency_conflict", "identity_conflict", "busy", "queue_full", "cursor_reset", "context_limit", "output_limit", "iteration_limit", "structured_output_unsupported": return .invalid
660
+ case "failure_unrecorded", "internal", "asset_read_failed", "stream_incomplete", "provider_error", "provider_rejected", "invalid_provider_response", "invalid_response", "output_too_large", "output_invalid", "empty_response", "unexpected_tool_call", "storage_error", "store_refused", "store_full", "corrupt_store", "store_too_new", "fresh_reservation_lost", "archive_unavailable", "archive_error", "host_error", "host_unresponsive", "thread_unavailable", "core_error", "invalid_abi", "no_lock", "handler_failed", "subscriber_overflow": return .internal
661
+ default: return .internal
662
+ }
663
+ }
664
+ static func representationErrorCategory(_ code: String) -> ErrorCategory {
665
+ switch code {
666
+ case "invalid_configuration": return .setup
667
+ case "invalid_input": return .invalid
668
+ case "invalid_tool_result": return .invalid
669
+ default: return errorCategory(code)
670
+ }
671
+ }
672
+ }