Skip to content

Commit

Permalink
Merge branch 'main' into nico/update_to_pb_batch
Browse files Browse the repository at this point in the history
  • Loading branch information
nicarq authored Jan 17, 2024
2 parents 4dce672 + da5f77b commit d8c4256
Show file tree
Hide file tree
Showing 153 changed files with 9,837 additions and 34,569 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"vrpath"
]
}
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 87 additions & 22 deletions shinkai-libs/shinkai-message-primitives/src/schemas/shinkai_name.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{
shinkai_message::shinkai_message::{MessageBody, ShinkaiMessage},
shinkai_utils::shinkai_logging::{shinkai_log, ShinkaiLogLevel, ShinkaiLogOption},
};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fmt;
use crate::{shinkai_message::shinkai_message::{MessageBody, ShinkaiMessage}, shinkai_utils::shinkai_logging::{ShinkaiLogOption, ShinkaiLogLevel, shinkai_log}};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct ShinkaiName {
Expand Down Expand Up @@ -92,7 +95,11 @@ impl ShinkaiName {
} else if *s == "device" {
ShinkaiSubidentityType::Device
} else {
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Error, &format!("Invalid subidentity type: {}", s));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Error,
&format!("Invalid subidentity type: {}", s),
);
panic!("Invalid subidentity type");
}
});
Expand All @@ -111,7 +118,11 @@ impl ShinkaiName {
match Self::validate_name(&shinkai_name) {
Ok(_) => true,
Err(err) => {
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("Validation error: {}", err));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!("Validation error: {}", err),
);
false
}
}
Expand All @@ -121,17 +132,32 @@ impl ShinkaiName {
let parts: Vec<&str> = raw_name.split('/').collect();

if !(parts.len() >= 1 && parts.len() <= 4) {
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("Name should have one to four parts: node, profile, type (device or agent), and name: {}", raw_name));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!(
"Name should have one to four parts: node, profile, type (device or agent), and name: {}",
raw_name
),
);
return Err("Name should have one to four parts: node, profile, type (device or agent), and name.");
}

if !parts[0].starts_with("@@") || !parts[0].ends_with(".shinkai") {
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("Validation error: {}", raw_name));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!("Validation error: {}", raw_name),
);
return Err("Node part of the name should start with '@@' and end with '.shinkai'.");
}

if !Regex::new(r"^@@[a-zA-Z0-9\_\.]+\.shinkai$").unwrap().is_match(parts[0]) {
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("Node part of the name contains invalid characters: {}", raw_name));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!("Node part of the name contains invalid characters: {}", raw_name),
);
return Err("Node part of the name contains invalid characters.");
}

Expand All @@ -140,7 +166,11 @@ impl ShinkaiName {
for (index, part) in parts.iter().enumerate() {
if index == 0 {
if part.contains("/") {
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("Root node name cannot contain '/': {}", raw_name));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!("Root node name cannot contain '/': {}", raw_name),
);
return Err("Root node name cannot contain '/'.");
}
continue;
Expand All @@ -150,17 +180,35 @@ impl ShinkaiName {
&& !(part == &ShinkaiSubidentityType::Agent.to_string()
|| part == &ShinkaiSubidentityType::Device.to_string())
{
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("The third part should either be 'agent' or 'device': {}", raw_name));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!("The third part should either be 'agent' or 'device': {}", raw_name),
);
return Err("The third part should either be 'agent' or 'device'.");
}

if index == 3 && !re.is_match(part) {
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("The fourth part (name after 'agent' or 'device') should be alphanumeric or underscore: {}", raw_name));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!(
"The fourth part (name after 'agent' or 'device') should be alphanumeric or underscore: {}",
raw_name
),
);
return Err("The fourth part (name after 'agent' or 'device') should be alphanumeric or underscore.");
}

if index != 0 && index != 2 && (!re.is_match(part) || part.contains(".shinkai")) {
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("Name parts should be alphanumeric or underscore and not contain '.shinkai': {}", raw_name));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!(
"Name parts should be alphanumeric or underscore and not contain '.shinkai': {}",
raw_name
),
);
return Err("Name parts should be alphanumeric or underscore and not contain '.shinkai'.");
}
}
Expand All @@ -169,7 +217,14 @@ impl ShinkaiName {
&& (parts[2] == &ShinkaiSubidentityType::Agent.to_string()
|| parts[2] == &ShinkaiSubidentityType::Device.to_string())
{
shinkai_log(ShinkaiLogOption::Identity, ShinkaiLogLevel::Info, &format!("If type is 'agent' or 'device', a fourth part is expected: {}", raw_name));
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Info,
&format!(
"If type is 'agent' or 'device', a fourth part is expected: {}",
raw_name
),
);
return Err("If type is 'agent' or 'device', a fourth part is expected.");
}

Expand Down Expand Up @@ -225,7 +280,11 @@ impl ShinkaiName {
}

pub fn from_shinkai_message_using_sender_and_intra_sender(message: &ShinkaiMessage) -> Result<Self, &'static str> {
let name = format!("{}/{}", message.external_metadata.sender.clone(), message.external_metadata.intra_sender.clone());
let name = format!(
"{}/{}",
message.external_metadata.sender.clone(),
message.external_metadata.intra_sender.clone()
);
Self::new(name)
}

Expand Down Expand Up @@ -268,28 +327,34 @@ impl ShinkaiName {
}
}

pub fn from_shinkai_message_using_recipient_subidentity(message: &ShinkaiMessage) -> Result<Self, ShinkaiNameError> {
pub fn from_shinkai_message_using_recipient_subidentity(
message: &ShinkaiMessage,
) -> Result<Self, ShinkaiNameError> {
// Check if the message is encrypted
let body = match &message.body {
MessageBody::Unencrypted(body) => body,
_ => return Err(ShinkaiNameError::InvalidOperation(
"Cannot process encrypted ShinkaiMessage".to_string(),
)),
_ => {
return Err(ShinkaiNameError::InvalidOperation(
"Cannot process encrypted ShinkaiMessage".to_string(),
))
}
};

let node = match Self::new(message.external_metadata.recipient.clone()) {
Ok(name) => name,
Err(_) => return Err(ShinkaiNameError::InvalidNameFormat(
message.external_metadata.recipient.clone(),
)),
Err(_) => {
return Err(ShinkaiNameError::InvalidNameFormat(
message.external_metadata.recipient.clone(),
))
}
};

let recipient_subidentity = if body.internal_metadata.recipient_subidentity.is_empty() {
String::from("")
} else {
format!("/{}", body.internal_metadata.recipient_subidentity)
};

match Self::new(format!("{}{}", node, recipient_subidentity)) {
Ok(name) => Ok(name),
Err(_) => Err(ShinkaiNameError::InvalidNameFormat(format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::{Deserialize, Serialize};
use shinkai_vector_resources::vector_resource::VectorResource;
use shinkai_vector_resources::vector_resource::{VectorResource, VectorResourceCore};
use shinkai_vector_resources::{
base_vector_resources::BaseVectorResource,
source::{SourceFile, VRSource},
vector_resource_types::VRHeader,
vector_resource::BaseVectorResource,
vector_resource::VRHeader,
};
use std::fmt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use x25519_dalek::{PublicKey as EncryptionPublicKey, StaticSecret as EncryptionS
use crate::{
schemas::{
agents::serialized_agent::SerializedAgent, inbox_name::InboxName, registration_code::RegistrationCode,
shinkai_time::ShinkaiTime,
shinkai_time::ShinkaiStringTime,
},
shinkai_message::{
shinkai_message::{
ExternalMetadata, InternalMetadata, MessageBody, MessageData,
ShinkaiBody, ShinkaiData, ShinkaiMessage, ShinkaiVersion,
ExternalMetadata, InternalMetadata, MessageBody, MessageData, ShinkaiBody, ShinkaiData, ShinkaiMessage,
ShinkaiVersion,
},
shinkai_message_schemas::{
APIAddAgentRequest, APIGetMessagesFromInboxRequest, APIReadUpToTimeRequest, IdentityPermissions,
Expand Down Expand Up @@ -176,7 +176,7 @@ impl ShinkaiMessageBuilder {
let signature = "".to_string();
let other = "".to_string();
let intra_sender = "".to_string();
let scheduled_time = ShinkaiTime::generate_time_now();
let scheduled_time = ShinkaiStringTime::generate_time_now();
self.external_metadata = Some(ExternalMetadata {
sender,
recipient,
Expand All @@ -191,7 +191,7 @@ impl ShinkaiMessageBuilder {
pub fn external_metadata_with_other(mut self, recipient: ProfileName, sender: ProfileName, other: String) -> Self {
let signature = "".to_string();
let intra_sender = "".to_string();
let scheduled_time = ShinkaiTime::generate_time_now();
let scheduled_time = ShinkaiStringTime::generate_time_now();
self.external_metadata = Some(ExternalMetadata {
sender,
recipient,
Expand All @@ -211,7 +211,7 @@ impl ShinkaiMessageBuilder {
intra_sender: String,
) -> Self {
let signature = "".to_string();
let scheduled_time = ShinkaiTime::generate_time_now();
let scheduled_time = ShinkaiStringTime::generate_time_now();
self.external_metadata = Some(ExternalMetadata {
sender,
recipient,
Expand All @@ -231,7 +231,7 @@ impl ShinkaiMessageBuilder {
) -> Self {
let signature = "".to_string();
let other = "".to_string();
let scheduled_time = ShinkaiTime::generate_time_now();
let scheduled_time = ShinkaiStringTime::generate_time_now();
self.external_metadata = Some(ExternalMetadata {
sender,
recipient,
Expand Down Expand Up @@ -975,8 +975,7 @@ impl std::fmt::Debug for ShinkaiMessageBuilder {
#[cfg(test)]
mod tests {
use crate::shinkai_utils::{
encryption::unsafe_deterministic_encryption_keypair,
signatures::{unsafe_deterministic_signature_keypair},
encryption::unsafe_deterministic_encryption_keypair, signatures::unsafe_deterministic_signature_keypair,
};

use super::*;
Expand Down
Loading

0 comments on commit d8c4256

Please sign in to comment.