use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use chrono::Utc;
use uuid::Uuid;
use crate::synapse::blockchain::serialization::{DateTimeWrapper, UuidWrapper};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EntityType {
Human,
AiModel,
Tool,
Service,
Router,
}
impl std::fmt::Display for EntityType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EntityType::Human => write!(f, "human"),
EntityType::AiModel => write!(f, "ai_model"),
EntityType::Tool => write!(f, "tool"),
EntityType::Service => write!(f, "service"),
EntityType::Router => write!(f, "router"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageType {
Direct,
ToolCall,
ToolResponse,
System,
Broadcast,
StreamChunk,
}
impl std::fmt::Display for MessageType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MessageType::Direct => write!(f, "direct"),
MessageType::ToolCall => write!(f, "tool_call"),
MessageType::ToolResponse => write!(f, "tool_response"),
MessageType::System => write!(f, "system"),
MessageType::Broadcast => write!(f, "broadcast"),
MessageType::StreamChunk => write!(f, "stream_chunk"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, bincode::Encode, bincode::Decode)]
#[serde(rename_all = "snake_case")]
pub enum SecurityLevel {
Public,
Private,
Authenticated,
Secure,
}
impl std::fmt::Display for SecurityLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SecurityLevel::Public => write!(f, "public"),
SecurityLevel::Private => write!(f, "private"),
SecurityLevel::Authenticated => write!(f, "authenticated"),
SecurityLevel::Secure => write!(f, "secure"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SimpleMessage {
pub to: String,
pub from_entity: String,
pub content: String,
pub message_type: MessageType,
#[serde(default)]
pub metadata: HashMap<String, String>,
}
impl SimpleMessage {
pub fn new(
to: impl Into<String>,
from_entity: impl Into<String>,
content: impl Into<String>,
) -> Self {
Self {
to: to.into(),
from_entity: from_entity.into(),
content: content.into(),
message_type: MessageType::Direct,
metadata: HashMap::new(),
}
}
pub fn tool_call(
to: impl Into<String>,
from_entity: impl Into<String>,
content: impl Into<String>,
) -> Self {
Self {
to: to.into(),
from_entity: from_entity.into(),
content: content.into(),
message_type: MessageType::ToolCall,
metadata: HashMap::new(),
}
}
pub fn tool_response(
to: impl Into<String>,
from_entity: impl Into<String>,
content: impl Into<String>,
) -> Self {
Self {
to: to.into(),
from_entity: from_entity.into(),
content: content.into(),
message_type: MessageType::ToolResponse,
metadata: HashMap::new(),
}
}
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalIdentity {
pub local_name: String,
pub global_id: String,
pub entity_type: EntityType,
pub public_key: String,
pub capabilities: Vec<String>,
pub trust_level: u8,
pub last_seen: DateTimeWrapper,
#[serde(default)]
pub routing_preferences: HashMap<String, String>,
}
impl Default for GlobalIdentity {
fn default() -> Self {
Self {
local_name: String::new(),
global_id: String::new(),
entity_type: EntityType::AiModel,
public_key: String::new(),
capabilities: Vec::new(),
trust_level: 0,
last_seen: DateTimeWrapper::new(Utc::now()),
routing_preferences: HashMap::new(),
}
}
}
impl GlobalIdentity {
pub fn new(
local_name: impl Into<String>,
global_id: impl Into<String>,
entity_type: EntityType,
public_key: impl Into<String>,
) -> Self {
Self {
local_name: local_name.into(),
global_id: global_id.into(),
entity_type,
public_key: public_key.into(),
capabilities: Vec::new(),
trust_level: 50,
last_seen: DateTimeWrapper::new(Utc::now()),
routing_preferences: HashMap::new(),
}
}
pub fn add_capability(&mut self, capability: impl Into<String>) {
self.capabilities.push(capability.into());
}
pub fn has_capability(&self, capability: &str) -> bool {
self.capabilities.iter().any(|c| c == capability)
}
pub fn update_last_seen(&mut self) {
self.last_seen = DateTimeWrapper::new(Utc::now());
}
}
#[derive(Debug, Clone, Serialize, Deserialize, bincode::Encode, bincode::Decode)]
pub struct SecureMessage {
pub message_id: UuidWrapper,
pub to_global_id: String,
pub from_global_id: String,
pub encrypted_content: Vec<u8>,
pub signature: Vec<u8>,
pub timestamp: DateTimeWrapper,
pub security_level: SecurityLevel,
#[serde(default)]
pub routing_path: Vec<String>,
#[serde(default)]
pub metadata: HashMap<String, String>,
}
impl SecureMessage {
pub fn new(
to_global_id: impl Into<String>,
from_global_id: impl Into<String>,
encrypted_content: Vec<u8>,
signature: Vec<u8>,
security_level: SecurityLevel,
) -> Self {
Self {
message_id: UuidWrapper::new(Uuid::new_v4()),
to_global_id: to_global_id.into(),
from_global_id: from_global_id.into(),
encrypted_content,
signature,
timestamp: DateTimeWrapper::new(Utc::now()),
security_level,
routing_path: Vec::new(),
metadata: HashMap::new(),
}
}
pub fn add_routing_hop(&mut self, hop: impl Into<String>) {
self.routing_path.push(hop.into());
}
pub fn add_metadata(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.metadata.insert(key.into(), value.into());
}
pub fn content(&self) -> String {
String::from_utf8_lossy(&self.encrypted_content).to_string()
}
pub fn content_is_empty(&self) -> bool {
self.encrypted_content.is_empty()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmailConfig {
pub smtp: SmtpConfig,
pub imap: ImapConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmtpConfig {
pub host: String,
pub port: u16,
pub username: String,
pub password: String,
pub use_tls: bool,
pub use_ssl: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImapConfig {
pub host: String,
pub port: u16,
pub username: String,
pub password: String,
pub use_ssl: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StreamPriority {
RealTime,
NearRealTime,
Background,
Batch,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StreamType {
ToolOutput,
LlmToTool,
UserToTool,
LogStream,
DataFeed,
Progress,
Interactive,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamChunk {
pub stream_id: Uuid,
pub sequence_number: u64,
pub chunk_type: String,
pub data: String,
pub timestamp: DateTimeWrapper,
pub priority: StreamPriority,
pub is_final: bool,
pub compression: String,
}
impl StreamChunk {
pub fn new_data(
stream_id: Uuid,
sequence_number: u64,
data: impl Into<String>,
priority: StreamPriority,
) -> Self {
Self {
stream_id,
sequence_number,
chunk_type: "data".to_string(),
data: data.into(),
timestamp: DateTimeWrapper::new(Utc::now()),
priority,
is_final: false,
compression: "none".to_string(),
}
}
pub fn new_final(stream_id: Uuid, sequence_number: u64) -> Self {
Self {
stream_id,
sequence_number,
chunk_type: "end".to_string(),
data: String::new(),
timestamp: DateTimeWrapper::new(Utc::now()),
priority: StreamPriority::Background,
is_final: true,
compression: "none".to_string(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamMetadata {
pub stream_id: Uuid,
pub stream_type: StreamType,
pub source: String,
pub destination: String,
pub started_at: DateTimeWrapper,
pub expected_duration: Option<u64>,
pub total_size_estimate: Option<u64>,
pub chunk_size: usize,
pub compression: String,
pub encryption: String,
}
impl StreamMetadata {
pub fn new(
stream_type: StreamType,
source: impl Into<String>,
destination: impl Into<String>,
) -> Self {
Self {
stream_id: Uuid::new_v4(),
stream_type,
source: source.into(),
destination: destination.into(),
started_at: DateTimeWrapper::new(Utc::now()),
expected_duration: None,
total_size_estimate: None,
chunk_size: 32 * 1024, compression: "gzip".to_string(),
encryption: "none".to_string(),
}
}
}