use borsh::{BorshDeserialize, BorshSerialize};
use solana_sdk::pubkey::Pubkey;
use std::collections::HashSet;
pub const MAX_CAPABILITIES: usize = 32;
pub const MAX_METADATA_SIZE: usize = 1024;
pub const MAX_VERSION_LENGTH: usize = 64;
pub const MAX_QUERY_LIMIT: usize = 100;
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
pub struct AgentAccount {
pub pubkey: Pubkey,
pub owner: Pubkey,
pub capabilities: Vec<String>,
pub version: String,
pub metadata: Vec<u8>,
pub registration_time: i64,
pub last_update_time: i64,
pub active: bool,
pub reputation: u64,
pub bump: u8,
}
impl AgentAccount {
pub fn validate(&self) -> Result<(), crate::agent_registry::errors::AgentRegistryError> {
if self.capabilities.len() > MAX_CAPABILITIES {
return Err(
crate::agent_registry::errors::AgentRegistryError::TooManyCapabilities {
max: MAX_CAPABILITIES,
provided: self.capabilities.len(),
},
);
}
if self.metadata.len() > MAX_METADATA_SIZE {
return Err(
crate::agent_registry::errors::AgentRegistryError::MetadataTooLarge {
max: MAX_METADATA_SIZE,
provided: self.metadata.len(),
},
);
}
if self.version.len() > MAX_VERSION_LENGTH {
return Err(
crate::agent_registry::errors::AgentRegistryError::InvalidVersion(
"Version string too long".to_string(),
),
);
}
for cap in &self.capabilities {
if cap.is_empty() || cap.len() > 128 {
return Err(
crate::agent_registry::errors::AgentRegistryError::InvalidCapability(
cap.clone(),
),
);
}
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct AgentInfo {
pub pubkey: Pubkey,
pub owner: Pubkey,
pub capabilities: HashSet<String>,
pub version: String,
pub metadata: Option<Vec<u8>>,
pub registration_time: i64,
pub last_update_time: i64,
pub active: bool,
pub reputation: u64,
}
impl From<AgentAccount> for AgentInfo {
fn from(account: AgentAccount) -> Self {
Self {
pubkey: account.pubkey,
owner: account.owner,
capabilities: account.capabilities.into_iter().collect(),
version: account.version,
metadata: if account.metadata.is_empty() {
None
} else {
Some(account.metadata)
},
registration_time: account.registration_time,
last_update_time: account.last_update_time,
active: account.active,
reputation: account.reputation,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Capability {
pub name: String,
pub version: Option<String>,
pub params: Option<serde_json::Value>,
}
impl Capability {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
version: None,
params: None,
}
}
pub fn with_version(mut self, version: &str) -> Self {
self.version = Some(version.to_string());
self
}
pub fn with_params(mut self, params: serde_json::Value) -> Self {
self.params = Some(params);
self
}
}
#[derive(Debug, Clone)]
pub struct RegistrationParams {
pub owner: Pubkey,
pub capabilities: Vec<String>,
pub version: String,
pub metadata: Option<Vec<u8>>,
}
impl RegistrationParams {
pub fn new(owner: Pubkey, capabilities: Vec<String>, version: &str) -> Self {
Self {
owner,
capabilities,
version: version.to_string(),
metadata: None,
}
}
pub fn with_metadata(mut self, metadata: Vec<u8>) -> Self {
self.metadata = Some(metadata);
self
}
}
#[derive(Debug, Clone)]
pub struct UpdateParams {
pub capabilities: Option<Vec<String>>,
pub version: Option<String>,
pub metadata: Option<Vec<u8>>,
pub active: Option<bool>,
}
impl UpdateParams {
pub fn new() -> Self {
Self {
capabilities: None,
version: None,
metadata: None,
active: None,
}
}
pub fn with_capabilities(mut self, capabilities: Vec<String>) -> Self {
self.capabilities = Some(capabilities);
self
}
pub fn with_version(mut self, version: &str) -> Self {
self.version = Some(version.to_string());
self
}
pub fn with_metadata(mut self, metadata: Vec<u8>) -> Self {
self.metadata = Some(metadata);
self
}
pub fn with_active(mut self, active: bool) -> Self {
self.active = Some(active);
self
}
}
#[derive(Debug, Clone, Default)]
pub struct AgentFilter {
pub capability: Option<String>,
pub min_reputation: Option<u64>,
pub active_only: bool,
pub limit: Option<usize>,
pub offset: usize,
}
impl AgentFilter {
pub fn new() -> Self {
Self::default()
}
pub fn with_capability(mut self, capability: &str) -> Self {
self.capability = Some(capability.to_string());
self
}
pub fn with_min_reputation(mut self, reputation: u64) -> Self {
self.min_reputation = Some(reputation);
self
}
pub fn active_only(mut self) -> Self {
self.active_only = true;
self
}
pub fn with_limit(mut self, limit: usize) -> Self {
self.limit = Some(limit.min(MAX_QUERY_LIMIT));
self
}
pub fn with_offset(mut self, offset: usize) -> Self {
self.offset = offset;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RegistryEvent {
AgentRegistered { agent: Pubkey, owner: Pubkey },
AgentUpdated { agent: Pubkey },
AgentDeregistered { agent: Pubkey },
CapabilityChanged { agent: Pubkey, capability: String },
}
#[derive(Debug, Clone, Default)]
pub struct PaginationParams {
pub limit: Option<usize>,
pub offset: usize,
}
#[derive(Debug, Clone)]
pub struct PaginatedResult<T> {
pub items: Vec<T>,
pub total: usize,
pub has_more: bool,
pub offset: usize,
}
impl<T> PaginatedResult<T> {
pub fn new(items: Vec<T>, total: usize, offset: usize) -> Self {
let has_more = offset + items.len() < total;
Self {
items,
total,
has_more,
offset,
}
}
pub fn next_offset(&self) -> Option<usize> {
if self.has_more {
Some(self.offset + self.items.len())
} else {
None
}
}
}