interplane.Get Omega
Read in your language

aien-protocols-mv

Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem

Brought by drakestapleton

branch main Branches Tags History

aien-protocols-mv / crates / aien-protocol-types / src / lib.rs

220 lines · 5366 bytes Raw Blame History
1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
5pub struct ProtocolVersion {
6 pub major: u16,
7 pub minor: u16,
8}
9
10impl ProtocolVersion {
11 pub const fn new(major: u16, minor: u16) -> Self {
12 Self { major, minor }
13 }
14
15 pub fn is_compatible_with(&self, other: &Self) -> bool {
16 self.major == other.major && self.minor <= other.minor
17 }
18}
19
20impl fmt::Display for ProtocolVersion {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 write!(f, "{}.{}", self.major, self.minor)
23 }
24}
25
26#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
27pub struct Digest32(pub [u8; 32]);
28
29impl Digest32 {
30 pub const ZERO: Self = Self([0u8; 32]);
31
32 pub const fn from_bytes(bytes: [u8; 32]) -> Self {
33 Self(bytes)
34 }
35
36 pub fn as_bytes(&self) -> &[u8; 32] {
37 &self.0
38 }
39
40 pub fn to_hex(&self) -> String {
41 hex::encode(self.0)
42 }
43
44 pub fn from_hex(s: &str) -> Result<Self, hex::FromHexError> {
45 let mut arr = [0u8; 32];
46 hex::decode_to_slice(s, &mut arr)?;
47 Ok(Self(arr))
48 }
49}
50
51impl fmt::Display for Digest32 {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 write!(f, "sha256:{}", self.to_hex())
54 }
55}
56
57#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
58pub struct AgentId(pub uuid::Uuid);
59
60impl AgentId {
61 pub fn new_v4() -> Self {
62 Self(uuid::Uuid::new_v4())
63 }
64}
65
66impl fmt::Display for AgentId {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 write!(f, "{}", self.0)
69 }
70}
71
72#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
73pub struct SessionId(pub uuid::Uuid);
74
75impl SessionId {
76 pub fn new_v4() -> Self {
77 Self(uuid::Uuid::new_v4())
78 }
79}
80
81impl fmt::Display for SessionId {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 write!(f, "{}", self.0)
84 }
85}
86
87#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
88pub struct RunId(pub uuid::Uuid);
89
90impl RunId {
91 pub fn new_v4() -> Self {
92 Self(uuid::Uuid::new_v4())
93 }
94}
95
96impl fmt::Display for RunId {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 write!(f, "{}", self.0)
99 }
100}
101
102#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
103pub struct ActionId(pub uuid::Uuid);
104
105impl ActionId {
106 pub fn new_v4() -> Self {
107 Self(uuid::Uuid::new_v4())
108 }
109}
110
111impl fmt::Display for ActionId {
112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113 write!(f, "{}", self.0)
114 }
115}
116
117#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
118pub struct EventId(pub uuid::Uuid);
119
120impl EventId {
121 pub fn new_v4() -> Self {
122 Self(uuid::Uuid::new_v4())
123 }
124}
125
126impl fmt::Display for EventId {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 write!(f, "{}", self.0)
129 }
130}
131
132#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
133pub struct ApprovalId(pub uuid::Uuid);
134
135impl ApprovalId {
136 pub fn new_v4() -> Self {
137 Self(uuid::Uuid::new_v4())
138 }
139}
140
141impl fmt::Display for ApprovalId {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 write!(f, "{}", self.0)
144 }
145}
146
147#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
148pub struct EvaluationId(pub uuid::Uuid);
149
150impl EvaluationId {
151 pub fn new_v4() -> Self {
152 Self(uuid::Uuid::new_v4())
153 }
154}
155
156impl fmt::Display for EvaluationId {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 write!(f, "{}", self.0)
159 }
160}
161
162#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
163pub struct SequenceNumber(pub u64);
164
165impl SequenceNumber {
166 pub fn next(&self) -> Self {
167 Self(self.0.saturating_add(1))
168 }
169}
170
171impl fmt::Display for SequenceNumber {
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173 write!(f, "{}", self.0)
174 }
175}
176
177#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
178pub struct Timestamp(pub u64);
179
180impl Timestamp {
181 pub fn from_micros(micros: u64) -> Self {
182 Self(micros)
183 }
184
185 pub fn as_micros(&self) -> u64 {
186 self.0
187 }
188}
189
190#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
191pub struct ArtifactRef {
192 pub artifact_id: uuid::Uuid,
193 pub digest: Digest32,
194 pub media_type: String,
195 pub byte_size: u64,
196}
197
198#[cfg(test)]
199mod tests {
200 use super::*;
201
202 #[test]
203 fn test_digest_hex_roundtrip() {
204 let original = Digest32([0xab; 32]);
205 let hex_str = original.to_hex();
206 let parsed = Digest32::from_hex(&hex_str).unwrap();
207 assert_eq!(original, parsed);
208 }
209
210 #[test]
211 fn test_protocol_version_compatibility() {
212 let v1_0 = ProtocolVersion::new(1, 0);
213 let v1_2 = ProtocolVersion::new(1, 2);
214 let v2_0 = ProtocolVersion::new(2, 0);
215
216 assert!(v1_0.is_compatible_with(&v1_2));
217 assert!(!v1_2.is_compatible_with(&v1_0));
218 assert!(!v1_0.is_compatible_with(&v2_0));
219 }
220}