Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-protocols-mv/blob/main/crates/aien-inference-protocol/src/lib.rs # aien-protocols-mv Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-protocols-mv / crates / aien-inference-protocol / src / lib.rs` 183 lines ยท 5485 bytes Raw Blame History 1 use aien_protocol_types::{Digest32, ProtocolVersion}; 2 use serde::{Deserialize, Serialize}; 3 4 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)] 5 pub struct ContextId(pub uuid::Uuid); 6 7 impl ContextId { 8 pub fn new_v4() -> Self { 9 Self(uuid::Uuid::new_v4()) 10 } 11 } 12 13 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)] 14 pub struct BranchId(pub uuid::Uuid); 15 16 impl BranchId { 17 pub fn new_v4() -> Self { 18 Self(uuid::Uuid::new_v4()) 19 } 20 } 21 22 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)] 23 pub struct Generation(pub u64); 24 25 impl Generation { 26 pub fn next(&self) -> Self { 27 Self(self.0.saturating_add(1)) 28 } 29 } 30 31 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 32 pub struct ModelFingerprint { 33 pub weights_digest: Digest32, 34 pub model_config_digest: Digest32, 35 } 36 37 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 38 pub struct TokenizerFingerprint { 39 pub tokenizer_digest: Digest32, 40 } 41 42 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 43 pub struct KvFormatFingerprint { 44 pub format_version: u16, 45 pub dtype: String, 46 } 47 48 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 49 pub struct ContextLineage { 50 pub parent_context: Option, 51 pub parent_branch: Option, 52 pub parent_digest: Option, 53 pub fork_token_index: Option, 54 } 55 56 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] 57 pub struct CacheIsolationKey { 58 pub domain_id: uuid::Uuid, 59 pub domain_digest: Digest32, 60 } 61 62 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 63 pub struct OpaqueBindingRef { 64 pub engine_id: String, 65 pub runtime_epoch: u64, 66 pub binding_id: uuid::Uuid, 67 pub lease_generation: u64, 68 } 69 70 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 71 pub struct ContextRecipeRef { 72 pub recipe_id: uuid::Uuid, 73 pub recipe_digest: Digest32, 74 } 75 76 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 77 pub struct InferenceContextRef { 78 pub abi_version: u16, 79 pub context_id: ContextId, 80 pub branch_id: BranchId, 81 pub generation: Generation, 82 pub model: ModelFingerprint, 83 pub tokenizer: TokenizerFingerprint, 84 pub kv_format: KvFormatFingerprint, 85 pub logical_state_digest: Digest32, 86 pub token_count: u64, 87 pub lineage: ContextLineage, 88 pub isolation: CacheIsolationKey, 89 pub binding: Option, 90 pub recovery: ContextRecipeRef, 91 } 92 93 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 94 pub enum MergeStrategy { 95 FastForward, 96 SelectBranch { branch: BranchId }, 97 RebuildFrom { recipe: ContextRecipeRef }, 98 } 99 100 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 101 pub struct InferenceMessage { 102 pub role: String, 103 pub content: String, 104 } 105 106 #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] 107 pub struct InferenceRequest { 108 pub request_id: uuid::Uuid, 109 pub model: String, 110 pub messages: Vec, 111 pub context: Option, 112 pub max_tokens: u32, 113 pub temperature: f32, 114 pub stop_sequences: Vec, 115 } 116 117 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 118 pub struct InferenceResponse { 119 pub request_id: uuid::Uuid, 120 pub content: String, 121 pub finish_reason: String, 122 pub prompt_tokens: u32, 123 pub completion_tokens: u32, 124 pub context: Option, 125 } 126 127 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 128 pub struct BranchContextRequest { 129 pub operation_id: uuid::Uuid, 130 pub parent_context: InferenceContextRef, 131 pub child_branch_id: BranchId, 132 pub isolation: CacheIsolationKey, 133 } 134 135 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 136 pub struct BranchContextReceipt { 137 pub operation_id: uuid::Uuid, 138 pub child_context: InferenceContextRef, 139 pub shared_pages: u32, 140 pub copied_pages: u32, 141 pub lease_expires_at_monotonic: u64, 142 } 143 144 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] 145 pub struct InferenceCapabilities { 146 pub protocol: ProtocolVersion, 147 pub context_branching: bool, 148 pub physical_cow: bool, 149 pub streaming: bool, 150 pub cancellation: bool, 151 pub max_context_tokens: Option, 152 } 153 154 #[derive(thiserror::Error, Debug)] 155 pub enum InferenceError { 156 #[error("Incompatible protocol version: expected {expected}, got {received}")] 157 VersionMismatch { expected: String, received: String }, 158 #[error("Context not found: {0:?}")] 159 ContextNotFound(ContextId), 160 #[error("Stale generation: expected {expected:?}, got {actual:?}")] 161 StaleGeneration { 162 expected: Generation, 163 actual: Generation, 164 }, 165 #[error("Stale runtime epoch: engine restarted, must reconstruct from recipe")] 166 StaleRuntimeEpoch, 167 #[error("Cache isolation error: domain unauthorized")] 168 IsolationDenied, 169 #[error("Resource exhaustion: {0}")] 170 ResourceExhausted(String), 171 #[error("Internal execution error: {0}")] 172 Internal(String), 173 } 174 175 #[async_trait::async_trait] 176 pub trait InferenceService: Send + Sync { 177 async fn infer(&self, req: InferenceRequest) -> Result; 178 async fn branch_context( 179 &self, 180 req: BranchContextRequest, 181 ) -> Result; 182 async fn get_capabilities(&self) -> Result; 183 }