From 574fbe03a18ff493583bdbe353f326f15017459e Mon Sep 17 00:00:00 2001 From: Nico Arqueros Date: Fri, 20 Dec 2024 15:24:45 +0530 Subject: [PATCH 1/6] fix --- .../shinkai-node/src/cron_tasks/cron_manager.rs | 2 +- .../src/llm_provider/providers/claude.rs | 2 +- .../src/llm_provider/providers/gemini.rs | 2 +- .../src/llm_provider/providers/openai.rs | 2 +- .../src/llm_provider/providers/openrouter.rs | 2 +- shinkai-libs/shinkai-sqlite/src/inbox_manager.rs | 13 +------------ 6 files changed, 6 insertions(+), 17 deletions(-) diff --git a/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs b/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs index 868a3374d..c9ccd4652 100644 --- a/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs +++ b/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs @@ -5,7 +5,7 @@ use std::{ sync::{Arc, Weak}, }; -use chrono::{Local, TimeZone, Utc}; +use chrono::{Local, Utc}; use ed25519_dalek::SigningKey; use futures::Future; use shinkai_message_primitives::{ diff --git a/shinkai-bin/shinkai-node/src/llm_provider/providers/claude.rs b/shinkai-bin/shinkai-node/src/llm_provider/providers/claude.rs index 2fa95494b..a81b0209b 100644 --- a/shinkai-bin/shinkai-node/src/llm_provider/providers/claude.rs +++ b/shinkai-bin/shinkai-node/src/llm_provider/providers/claude.rs @@ -124,7 +124,7 @@ impl LLMService for Claude { // Print payload as a pretty JSON string match serde_json::to_string_pretty(&payload) { - Ok(pretty_json) => eprintln!("Payload: {}", pretty_json), + Ok(pretty_json) => eprintln!("cURL Payload: {}", pretty_json), Err(e) => eprintln!("Failed to serialize payload: {:?}", e), }; diff --git a/shinkai-bin/shinkai-node/src/llm_provider/providers/gemini.rs b/shinkai-bin/shinkai-node/src/llm_provider/providers/gemini.rs index 215d32825..b359dced8 100644 --- a/shinkai-bin/shinkai-node/src/llm_provider/providers/gemini.rs +++ b/shinkai-bin/shinkai-node/src/llm_provider/providers/gemini.rs @@ -168,7 +168,7 @@ impl LLMService for Gemini { || std::env::var("LOG_ALL").unwrap_or_default() == "1" { match serde_json::to_string_pretty(&payload) { - Ok(pretty_json) => eprintln!("Payload: {}", pretty_json), + Ok(pretty_json) => eprintln!("cURL Payload: {}", pretty_json), Err(e) => eprintln!("Failed to serialize payload: {:?}", e), }; } diff --git a/shinkai-bin/shinkai-node/src/llm_provider/providers/openai.rs b/shinkai-bin/shinkai-node/src/llm_provider/providers/openai.rs index 2f9ecd646..1984908a1 100644 --- a/shinkai-bin/shinkai-node/src/llm_provider/providers/openai.rs +++ b/shinkai-bin/shinkai-node/src/llm_provider/providers/openai.rs @@ -110,7 +110,7 @@ impl LLMService for OpenAI { // Print payload as a pretty JSON string match serde_json::to_string_pretty(&payload) { - Ok(pretty_json) => eprintln!("Payload: {}", pretty_json), + Ok(pretty_json) => eprintln!("cURL Payload: {}", pretty_json), Err(e) => eprintln!("Failed to serialize payload: {:?}", e), }; diff --git a/shinkai-bin/shinkai-node/src/llm_provider/providers/openrouter.rs b/shinkai-bin/shinkai-node/src/llm_provider/providers/openrouter.rs index 33662dc27..1490cb2d0 100644 --- a/shinkai-bin/shinkai-node/src/llm_provider/providers/openrouter.rs +++ b/shinkai-bin/shinkai-node/src/llm_provider/providers/openrouter.rs @@ -102,7 +102,7 @@ impl LLMService for OpenRouter { // Print payload as a pretty JSON string match serde_json::to_string_pretty(&payload) { - Ok(pretty_json) => eprintln!("Payload: {}", pretty_json), + Ok(pretty_json) => eprintln!("cURL Payload: {}", pretty_json), Err(e) => eprintln!("Failed to serialize payload: {:?}", e), }; diff --git a/shinkai-libs/shinkai-sqlite/src/inbox_manager.rs b/shinkai-libs/shinkai-sqlite/src/inbox_manager.rs index 235c28c9f..d0164e8aa 100644 --- a/shinkai-libs/shinkai-sqlite/src/inbox_manager.rs +++ b/shinkai-libs/shinkai-sqlite/src/inbox_manager.rs @@ -476,8 +476,6 @@ impl SqliteManager { ))); } - let profile_name = profile_name_identity.full_identity_name.to_string(); - let conn = self.get_connection()?; let mut stmt = conn.prepare("SELECT inbox_name FROM inboxes")?; let mut rows = stmt.query([])?; @@ -486,16 +484,7 @@ impl SqliteManager { while let Some(row) = rows.next()? { let inbox_name: String = row.get(0)?; - if inbox_name.contains(&profile_name) { - inboxes.push(inbox_name); - } else { - // Check if the identity has read permission for the inbox - if let Ok(has_perm) = self.has_permission(&inbox_name, &profile_name_identity, InboxPermission::Read) { - if has_perm { - inboxes.push(inbox_name); - } - } - } + inboxes.push(inbox_name); } Ok(inboxes) From 45fbcccec3afa23f2e641acefccdc9b12b0c00af Mon Sep 17 00:00:00 2001 From: Nico Arqueros Date: Fri, 20 Dec 2024 15:43:07 +0530 Subject: [PATCH 2/6] fix tests --- shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs b/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs index c9ccd4652..ff922f4f7 100644 --- a/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs +++ b/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs @@ -631,7 +631,7 @@ mod tests { #[test] fn test_should_execute_specific_minute() { - let now = Utc::now(); + let now = Local::now(); let next_minute = (now.minute() + 1) % 60; let cron = format!("{} * * * *", next_minute); let task = create_test_cron_task(&cron); @@ -642,7 +642,7 @@ mod tests { #[test] fn test_should_not_execute_past_time() { - let now = Utc::now(); + let now = Local::now(); let past_minute = if now.minute() == 0 { 59 } else { now.minute() - 1 }; let cron = format!("{} * * * *", past_minute); let task = create_test_cron_task(&cron); @@ -667,7 +667,7 @@ mod tests { #[test] fn test_should_execute_within_interval() { - let now = Utc::now(); + let now = Local::now(); let next_minute = (now.minute() + 1) % 60; // Create a cron expression for the next minute, any hour/day/month @@ -688,7 +688,7 @@ mod tests { #[test] fn test_should_not_execute_outside_interval() { - let now = Utc::now(); + let now = Local::now(); let future_minute = (now.minute() + 2) % 60; let cron = format!("{} * * * *", future_minute); let task = create_test_cron_task(&cron); From e1af82f3f9faf97f015137a1597a50fefba7cc03 Mon Sep 17 00:00:00 2001 From: Nico Arqueros Date: Fri, 20 Dec 2024 17:58:29 +0530 Subject: [PATCH 3/6] remove permissions --- .../shinkai-node/tests/it/db_inbox_tests.rs | 229 ------------------ 1 file changed, 229 deletions(-) diff --git a/shinkai-bin/shinkai-node/tests/it/db_inbox_tests.rs b/shinkai-bin/shinkai-node/tests/it/db_inbox_tests.rs index dc9acae7e..b537e240c 100644 --- a/shinkai-bin/shinkai-node/tests/it/db_inbox_tests.rs +++ b/shinkai-bin/shinkai-node/tests/it/db_inbox_tests.rs @@ -971,234 +971,5 @@ async fn db_inbox() { .get_last_unread_messages_from_inbox(inbox_name_value.clone().to_string(), 2, None) .unwrap(); assert_eq!(last_messages_inbox.len(), 1); - - // Test permissions - let subidentity_name = "device1"; - let full_subidentity_name = - ShinkaiName::from_node_and_profile_names(node1_identity_name.to_string(), subidentity_name.to_string()) - .unwrap(); - - let device1_subidentity = StandardIdentity::new( - full_subidentity_name.clone(), - None, - node1_encryption_pk.clone(), - node1_identity_pk.clone(), - Some(node1_subencryption_pk), - Some(node1_subidentity_pk), - StandardIdentityType::Profile, - IdentityPermissions::Standard, - ); - - let _ = shinkai_db.insert_profile(device1_subidentity.clone()); - println!("Inserted profile"); - eprintln!("inbox name: {}", inbox_name_value); - - shinkai_db - .add_permission(&inbox_name_value, &device1_subidentity, InboxPermission::Admin) - .unwrap(); - assert!(shinkai_db - .has_permission(&inbox_name_value, &device1_subidentity, InboxPermission::Admin) - .unwrap()); - - shinkai_db - .remove_permission(&inbox_name_value, &device1_subidentity) - .unwrap(); - assert!(!shinkai_db - .has_permission(&inbox_name_value, &device1_subidentity, InboxPermission::Admin) - .unwrap()); - - let message4 = generate_message_with_text( - "Hello World 6".to_string(), - node1_encryption_sk.clone(), - clone_signature_secret_key(&node1_identity_sk), - node1_subencryption_pk, - "other_inbox".to_string(), - node1_identity_name.to_string(), - "2023-07-02T20:53:34.815Z".to_string(), - ); - let message5 = generate_message_with_text( - "Hello World 7".to_string(), - node1_encryption_sk.clone(), - clone_signature_secret_key(&node1_identity_sk), - node1_subencryption_pk, - "yet_another_inbox".to_string(), - node1_identity_name.to_string(), - "2023-07-02T20:53:34.816Z".to_string(), - ); - shinkai_db - .unsafe_insert_inbox_message(&message4, None, None) - .await - .unwrap(); - shinkai_db - .unsafe_insert_inbox_message(&message5, None, None) - .await - .unwrap(); - - // Test get_inboxes_for_profile - let node1_profile_identity = StandardIdentity::new( - ShinkaiName::from_node_and_profile_names(node1_identity_name.to_string(), node1_subidentity_name.to_string()) - .unwrap(), - None, - node1_encryption_pk.clone(), - node1_identity_pk.clone(), - Some(node1_subencryption_pk), - Some(node1_subidentity_pk), - StandardIdentityType::Profile, - IdentityPermissions::Standard, - ); - let _ = shinkai_db.insert_profile(node1_profile_identity.clone()); - let inboxes = shinkai_db - .get_inboxes_for_profile(node1_profile_identity.clone()) - .unwrap(); - assert_eq!(inboxes.len(), 1); - - let inboxes = shinkai_db - .get_inboxes_for_profile(node1_profile_identity.clone()) - .unwrap(); - assert_eq!(inboxes.len(), 1); - assert!(inboxes.contains(&"inbox::@@node1.shinkai::@@node1.shinkai/main_profile_node1::false".to_string())); - - // Test get_smart_inboxes_for_profile - let smart_inboxes = shinkai_db - .get_all_smart_inboxes_for_profile(node1_profile_identity.clone()) - .unwrap(); - assert_eq!(smart_inboxes.len(), 1); - - // Check if smart_inboxes contain the expected results - let expected_inbox_ids = ["inbox::@@node1.shinkai::@@node1.shinkai/main_profile_node1::false"]; - - for smart_inbox in smart_inboxes { - assert!(expected_inbox_ids.contains(&smart_inbox.inbox_id.as_str())); - assert_eq!(format!("New Inbox: {}", smart_inbox.inbox_id), smart_inbox.custom_name); - - // Check the last_message of each smart_inbox - if let Some(last_message) = smart_inbox.last_message { - match last_message.body { - MessageBody::Unencrypted(ref body) => match body.message_data { - MessageData::Unencrypted(ref data) => match smart_inbox.inbox_id.as_str() { - "inbox::@@node1.shinkai::@@node1.shinkai/main_profile_node1::false" => { - assert_eq!(data.message_raw_content, "Hello World 5"); - } - "inbox::@@node1.shinkai::@@node1.shinkai/other_inbox::false" => { - assert_eq!(data.message_raw_content, "Hello World 6"); - } - "inbox::@@node1.shinkai::@@node1.shinkai/yet_another_inbox::false" => { - assert_eq!(data.message_raw_content, "Hello World 7"); - } - _ => panic!("Unexpected inbox_id"), - }, - _ => panic!("Expected unencrypted message data"), - }, - _ => panic!("Expected unencrypted message body"), - } - assert_eq!(last_message.external_metadata.sender, "@@node1.shinkai"); - assert_eq!(last_message.external_metadata.recipient, "@@node1.shinkai"); - assert_eq!(last_message.encryption, EncryptionMethod::None); - assert_eq!(last_message.version, ShinkaiVersion::V1_0); - } - } - - // Update the name of one of the inboxes - let inbox_to_update = "inbox::@@node1.shinkai::@@node1.shinkai/main_profile_node1::false"; - let new_name = "New Inbox Name"; - shinkai_db.update_smart_inbox_name(inbox_to_update, new_name).unwrap(); - - // Get smart_inboxes again - let updated_smart_inboxes = shinkai_db - .get_all_smart_inboxes_for_profile(node1_profile_identity) - .unwrap(); - - // Check if the name of the updated inbox has been changed - for smart_inbox in updated_smart_inboxes { - if smart_inbox.inbox_id == inbox_to_update { - eprintln!("Smart inbox: {:?}", smart_inbox); - assert_eq!(smart_inbox.custom_name, new_name); - } - } } -#[tokio::test] -async fn test_permission_errors() { - let node1_identity_name = "@@node1.shinkai"; - let node1_subidentity_name = "main_profile_node1"; - - let (_, node1_identity_pk) = unsafe_deterministic_signature_keypair(0); - let (_, node1_encryption_pk) = unsafe_deterministic_encryption_keypair(0); - - let (_, node1_subidentity_pk) = unsafe_deterministic_signature_keypair(100); - let (_, node1_subencryption_pk) = unsafe_deterministic_encryption_keypair(100); - - let db = setup_test_db(); - let shinkai_db = Arc::new(db); - - // Update local node keys - shinkai_db - .update_local_node_keys( - ShinkaiName::new(node1_identity_name.to_string()).unwrap(), - node1_encryption_pk, - node1_identity_pk, - ) - .unwrap(); - - let subidentity_name = "device1"; - let full_subidentity_name = - ShinkaiName::from_node_and_profile_names(node1_identity_name.to_string(), subidentity_name.to_string()) - .unwrap(); - - let device1_subidentity = StandardIdentity::new( - full_subidentity_name.clone(), - None, - node1_encryption_pk, - node1_identity_pk, - Some(node1_subencryption_pk), - Some(node1_subidentity_pk), - StandardIdentityType::Profile, - IdentityPermissions::Standard, - ); - let _ = shinkai_db.insert_profile(device1_subidentity.clone()); - - // Create a fake identity for tests - let nonexistent_identity = StandardIdentity::new( - ShinkaiName::from_node_and_profile_names(node1_identity_name.to_string(), "nonexistent_identity".to_string()) - .unwrap(), - None, - node1_encryption_pk, - node1_identity_pk, - Some(node1_subencryption_pk), - Some(node1_subidentity_pk), - StandardIdentityType::Profile, - IdentityPermissions::Standard, - ); - - // Test 1: Adding a permission to a nonexistent inbox should result in an error - let result = shinkai_db.add_permission("nonexistent_inbox", &device1_subidentity, InboxPermission::Admin); - assert!(result.is_err()); - - // Test 2: Adding a permission for a nonexistent identity should result in an error - let result = shinkai_db.add_permission( - "job_inbox::not_existent::false", - &nonexistent_identity, - InboxPermission::Admin, - ); - assert!(result.is_err()); - - // Test 3: Removing a permission from a nonexistent inbox should result in an error - let result = shinkai_db.remove_permission("job_inbox::not_existent::false", &device1_subidentity); - assert!(result.is_err()); - - // Test 4: Removing a permission for a nonexistent identity should result in an error - let result = shinkai_db.remove_permission("existing_inbox", &nonexistent_identity); - assert!(result.is_err()); - - // Test 5: Checking permission of a nonexistent inbox should result in an error - let result: Result = shinkai_db.has_permission( - "job_inbox::not_existent::false", - &device1_subidentity, - InboxPermission::Admin, - ); - assert!(result.is_err()); - - // Test 6: Checking permission for a nonexistent identity should result in an error - let result = shinkai_db.has_permission("existing_inbox", &nonexistent_identity, InboxPermission::Admin); - assert!(result.is_err()); -} From 7be7553ea5d2061ff80f7c4cac1054f045177153 Mon Sep 17 00:00:00 2001 From: Nico Arqueros Date: Fri, 20 Dec 2024 18:33:47 +0530 Subject: [PATCH 4/6] updates --- .../src/cron_tasks/cron_manager.rs | 41 ++++-- .../shinkai-sqlite/src/cron_task_manager.rs | 40 ++++-- .../shinkai-sqlite/src/inbox_manager.rs | 132 ------------------ shinkai-libs/shinkai-sqlite/src/lib.rs | 1 + 4 files changed, 56 insertions(+), 158 deletions(-) diff --git a/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs b/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs index ff922f4f7..3a1c38716 100644 --- a/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs +++ b/shinkai-bin/shinkai-node/src/cron_tasks/cron_manager.rs @@ -326,10 +326,16 @@ impl CronManager { job_creation_info, llm_provider, } => { + // Clone job_creation_info and set is_hidden to true if not defined + let mut job_creation_info_clone = job_creation_info.clone(); + if job_creation_info_clone.is_hidden.is_none() { + job_creation_info_clone.is_hidden = Some(true); + } + let job_id = job_manager .lock() .await - .process_job_creation(job_creation_info, &shinkai_profile, &llm_provider) + .process_job_creation(job_creation_info_clone, &shinkai_profile, &llm_provider) .await?; // Update the job configuration @@ -411,10 +417,10 @@ impl CronManager { result } - async fn log_success_to_sqlite(db: &Arc, task_id: i64) { + async fn log_success_to_sqlite(db: &Arc, task_id: i64, job_id: Option) { let execution_time = Local::now().to_rfc3339(); let db = db; - if let Err(err) = db.add_cron_task_execution(task_id, &execution_time, true, None) { + if let Err(err) = db.add_cron_task_execution(task_id, &execution_time, true, None, job_id) { eprintln!("Failed to log success to SQLite: {}", err); } } @@ -434,11 +440,12 @@ impl CronManager { let bearer = match db.read_api_v2_key() { Ok(Some(token)) => token, Ok(None) => { - Self::log_error_to_sqlite(&db, task_id, "Bearer token not found").await; + Self::log_error_to_sqlite(&db, task_id, "Bearer token not found", None).await; return Ok(()); } Err(err) => { - Self::log_error_to_sqlite(&db, task_id, &format!("Failed to retrieve bearer token: {}", err)).await; + Self::log_error_to_sqlite(&db, task_id, &format!("Failed to retrieve bearer token: {}", err), None) + .await; return Ok(()); } }; @@ -453,7 +460,7 @@ impl CronManager { identity_manager_clone, job_manager_clone, bearer, - job_message_clone, + job_message_clone.clone(), encryption_secret_key_clone, encryption_public_key_clone, signing_secret_key_clone, @@ -461,25 +468,37 @@ impl CronManager { ) .await { - Self::log_error_to_sqlite(&db, task_id, &format!("Failed to send job message: {}", err)).await; + Self::log_error_to_sqlite( + &db, + task_id, + &format!("Failed to send job message: {}", err), + Some(job_message_clone.job_id), + ) + .await; return Ok(()); } // Handle the response only if sending was successful if let Err(err) = res_rx.recv().await { - Self::log_error_to_sqlite(&db, task_id, &format!("Failed to receive response: {}", err)).await; + Self::log_error_to_sqlite( + &db, + task_id, + &format!("Failed to receive response: {}", err), + Some(job_message_clone.job_id), + ) + .await; } else { // Log success if the response is received successfully - Self::log_success_to_sqlite(&db, task_id).await; + Self::log_success_to_sqlite(&db, task_id, Some(job_message_clone.job_id)).await; } Ok(()) } - async fn log_error_to_sqlite(db: &Arc, task_id: i64, error_message: &str) { + async fn log_error_to_sqlite(db: &Arc, task_id: i64, error_message: &str, job_id: Option) { let execution_time = Local::now().to_rfc3339(); let db = db; - if let Err(err) = db.add_cron_task_execution(task_id, &execution_time, false, Some(error_message)) { + if let Err(err) = db.add_cron_task_execution(task_id, &execution_time, false, Some(error_message), job_id) { eprintln!("Failed to log error to SQLite: {}", err); } } diff --git a/shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs b/shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs index 14597a91e..a1f64838a 100644 --- a/shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs +++ b/shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs @@ -125,22 +125,23 @@ impl SqliteManager { execution_time: &str, success: bool, error_message: Option<&str>, + job_id: Option, ) -> Result { let conn = self.get_connection()?; conn.execute( - "INSERT INTO cron_task_executions (task_id, execution_time, success, error_message) VALUES (?1, ?2, ?3, ?4)", - params![task_id, execution_time, success as i32, error_message], + "INSERT INTO cron_task_executions (task_id, execution_time, success, error_message, job_id) VALUES (?1, ?2, ?3, ?4, ?5)", + params![task_id, execution_time, success as i32, error_message, job_id.as_deref()], )?; Ok(conn.last_insert_rowid()) } // Get all execution records - pub fn get_all_cron_task_executions(&self) -> Result)>, SqliteManagerError> { + pub fn get_all_cron_task_executions(&self) -> Result, Option)>, SqliteManagerError> { let conn = self.get_connection()?; let mut stmt = - conn.prepare("SELECT task_id, execution_time, success, error_message FROM cron_task_executions")?; + conn.prepare("SELECT task_id, execution_time, success, error_message, job_id FROM cron_task_executions")?; let execution_iter = stmt.query_map([], |row| { - Ok((row.get(0)?, row.get(1)?, row.get::<_, i32>(2)? != 0, row.get(3)?)) + Ok((row.get(0)?, row.get(1)?, row.get::<_, i32>(2)? != 0, row.get(3)?, row.get(4)?)) })?; execution_iter @@ -152,16 +153,16 @@ impl SqliteManager { pub fn get_cron_task_executions( &self, task_id: i64, - ) -> Result)>, SqliteManagerError> { + ) -> Result, Option)>, SqliteManagerError> { let conn = self.get_connection()?; let mut stmt = conn.prepare( - "SELECT execution_time, success, error_message + "SELECT execution_time, success, error_message, job_id FROM cron_task_executions WHERE task_id = ?1 ORDER BY execution_time DESC" )?; let execution_iter = stmt.query_map(params![task_id], |row| { - Ok((row.get(0)?, row.get::<_, i32>(1)? != 0, row.get(2)?)) + Ok((row.get(0)?, row.get::<_, i32>(1)? != 0, row.get(2)?, row.get(3)?)) })?; execution_iter @@ -173,10 +174,10 @@ impl SqliteManager { pub fn get_cron_task_execution( &self, execution_id: i64, - ) -> Result)>, SqliteManagerError> { + ) -> Result, Option)>, SqliteManagerError> { let conn = self.get_connection()?; let mut stmt = conn.prepare( - "SELECT task_id, execution_time, success, error_message FROM cron_task_executions WHERE execution_id = ?1", + "SELECT task_id, execution_time, success, error_message, job_id FROM cron_task_executions WHERE execution_id = ?1", )?; let mut rows = stmt.query(params![execution_id])?; @@ -186,6 +187,7 @@ impl SqliteManager { row.get(1)?, row.get::<_, i32>(2)? != 0, row.get(3)?, + row.get(4)?, ))) } else { Ok(None) @@ -398,9 +400,10 @@ mod tests { let execution_time = chrono::Utc::now().to_rfc3339(); let success = true; let error_message: Option<&str> = None; + let job_id = Some("test_job_id".to_string()); let execution_id = manager - .add_cron_task_execution(task_id, &execution_time, success, error_message) + .add_cron_task_execution(task_id, &execution_time, success, error_message, job_id.clone()) .unwrap(); let execution_record = manager.get_cron_task_execution(execution_id).unwrap().unwrap(); @@ -408,6 +411,7 @@ mod tests { assert_eq!(execution_record.1, execution_time); assert_eq!(execution_record.2, success); assert_eq!(execution_record.3, error_message.map(|s| s.to_string())); + assert_eq!(execution_record.4, job_id.map(|s| s.to_string())); } #[test] @@ -435,16 +439,19 @@ mod tests { let execution_time2 = chrono::Utc::now().to_rfc3339(); let success = true; let error_message = None; + let job_id = Some("test_job_id".to_string()); manager - .add_cron_task_execution(task_id, &execution_time1, success, error_message) + .add_cron_task_execution(task_id, &execution_time1, success, error_message, job_id.clone()) .unwrap(); manager - .add_cron_task_execution(task_id, &execution_time2, success, error_message) + .add_cron_task_execution(task_id, &execution_time2, success, error_message, job_id.clone()) .unwrap(); let all_executions = manager.get_all_cron_task_executions().unwrap(); assert_eq!(all_executions.len(), 2); + assert_eq!(all_executions[0].4, job_id.clone().map(|s| s.to_string())); + assert_eq!(all_executions[1].4, job_id.map(|s| s.to_string())); } #[test] @@ -472,15 +479,18 @@ mod tests { let execution_time2 = chrono::Utc::now().to_rfc3339(); let success = true; let error_message = None; + let job_id = Some("test_job_id".to_string()); manager - .add_cron_task_execution(task_id, &execution_time1, success, error_message) + .add_cron_task_execution(task_id, &execution_time1, success, error_message, job_id.clone()) .unwrap(); manager - .add_cron_task_execution(task_id, &execution_time2, success, error_message) + .add_cron_task_execution(task_id, &execution_time2, success, error_message, job_id.clone()) .unwrap(); let task_executions = manager.get_cron_task_executions(task_id).unwrap(); assert_eq!(task_executions.len(), 2); + assert_eq!(task_executions[0].3, job_id.clone().map(|s| s.to_string())); + assert_eq!(task_executions[1].3, job_id.clone().map(|s| s.to_string())); } } diff --git a/shinkai-libs/shinkai-sqlite/src/inbox_manager.rs b/shinkai-libs/shinkai-sqlite/src/inbox_manager.rs index d0164e8aa..b1482a27a 100644 --- a/shinkai-libs/shinkai-sqlite/src/inbox_manager.rs +++ b/shinkai-libs/shinkai-sqlite/src/inbox_manager.rs @@ -1316,138 +1316,6 @@ mod tests { .get_last_unread_messages_from_inbox(inbox_name_value.clone().to_string(), 2, None) .unwrap(); assert_eq!(last_messages_inbox.len(), 1); - - // Test permissions - let subidentity_name = "device1"; - let full_subidentity_name = - ShinkaiName::from_node_and_profile_names(node1_identity_name.to_string(), subidentity_name.to_string()) - .unwrap(); - - let device1_subidentity = StandardIdentity::new( - full_subidentity_name.clone(), - None, - node1_encryption_pk.clone(), - node1_identity_pk.clone(), - Some(node1_subencryption_pk), - Some(node1_subidentity_pk), - StandardIdentityType::Profile, - IdentityPermissions::Standard, - ); - - let _ = db.insert_profile(device1_subidentity.clone()); - println!("Inserted profile"); - eprintln!("inbox name: {}", inbox_name_value); - - db.add_permission(&inbox_name_value, &device1_subidentity, InboxPermission::Admin) - .unwrap(); - assert!(db - .has_permission(&inbox_name_value, &device1_subidentity, InboxPermission::Admin) - .unwrap()); - - db.remove_permission(&inbox_name_value, &device1_subidentity).unwrap(); - assert!(!db - .has_permission(&inbox_name_value, &device1_subidentity, InboxPermission::Admin) - .unwrap()); - - let message4 = generate_message_with_text( - "Hello World 6".to_string(), - node1_encryption_sk.clone(), - clone_signature_secret_key(&node1_identity_sk), - node1_subencryption_pk, - "other_inbox".to_string(), - node1_identity_name.to_string(), - "2023-07-02T20:53:34.815Z".to_string(), - ); - let message5 = generate_message_with_text( - "Hello World 7".to_string(), - node1_encryption_sk.clone(), - clone_signature_secret_key(&node1_identity_sk), - node1_subencryption_pk, - "yet_another_inbox".to_string(), - node1_identity_name.to_string(), - "2023-07-02T20:53:34.816Z".to_string(), - ); - db.unsafe_insert_inbox_message(&message4, None, None).await.unwrap(); - db.unsafe_insert_inbox_message(&message5, None, None).await.unwrap(); - - // Test get_inboxes_for_profile - let node1_profile_identity = StandardIdentity::new( - ShinkaiName::from_node_and_profile_names( - node1_identity_name.to_string(), - node1_subidentity_name.to_string(), - ) - .unwrap(), - None, - node1_encryption_pk.clone(), - node1_identity_pk.clone(), - Some(node1_subencryption_pk), - Some(node1_subidentity_pk), - StandardIdentityType::Profile, - IdentityPermissions::Standard, - ); - let _ = db.insert_profile(node1_profile_identity.clone()); - let inboxes = db.get_inboxes_for_profile(node1_profile_identity.clone()).unwrap(); - assert_eq!(inboxes.len(), 1); - - let inboxes = db.get_inboxes_for_profile(node1_profile_identity.clone()).unwrap(); - assert_eq!(inboxes.len(), 1); - assert!(inboxes.contains(&"inbox::@@node1.shinkai::@@node1.shinkai/main_profile_node1::false".to_string())); - - // Test get_smart_inboxes_for_profile - let smart_inboxes = db - .get_all_smart_inboxes_for_profile(node1_profile_identity.clone()) - .unwrap(); - assert_eq!(smart_inboxes.len(), 1); - - // Check if smart_inboxes contain the expected results - let expected_inbox_ids = ["inbox::@@node1.shinkai::@@node1.shinkai/main_profile_node1::false"]; - - for smart_inbox in smart_inboxes { - assert!(expected_inbox_ids.contains(&smart_inbox.inbox_id.as_str())); - assert_eq!(format!("New Inbox: {}", smart_inbox.inbox_id), smart_inbox.custom_name); - - // Check the last_message of each smart_inbox - if let Some(last_message) = smart_inbox.last_message { - match last_message.body { - MessageBody::Unencrypted(ref body) => match body.message_data { - MessageData::Unencrypted(ref data) => match smart_inbox.inbox_id.as_str() { - "inbox::@@node1.shinkai::@@node1.shinkai/main_profile_node1::false" => { - assert_eq!(data.message_raw_content, "Hello World 5"); - } - "inbox::@@node1.shinkai::@@node1.shinkai/other_inbox::false" => { - assert_eq!(data.message_raw_content, "Hello World 6"); - } - "inbox::@@node1.shinkai::@@node1.shinkai/yet_another_inbox::false" => { - assert_eq!(data.message_raw_content, "Hello World 7"); - } - _ => panic!("Unexpected inbox_id"), - }, - _ => panic!("Expected unencrypted message data"), - }, - _ => panic!("Expected unencrypted message body"), - } - assert_eq!(last_message.external_metadata.sender, "@@node1.shinkai"); - assert_eq!(last_message.external_metadata.recipient, "@@node1.shinkai"); - assert_eq!(last_message.encryption, EncryptionMethod::None); - assert_eq!(last_message.version, ShinkaiVersion::V1_0); - } - } - - // Update the name of one of the inboxes - let inbox_to_update = "inbox::@@node1.shinkai::@@node1.shinkai/main_profile_node1::false"; - let new_name = "New Inbox Name"; - db.update_smart_inbox_name(inbox_to_update, new_name).unwrap(); - - // Get smart_inboxes again - let updated_smart_inboxes = db.get_all_smart_inboxes_for_profile(node1_profile_identity).unwrap(); - - // Check if the name of the updated inbox has been changed - for smart_inbox in updated_smart_inboxes { - if smart_inbox.inbox_id == inbox_to_update { - eprintln!("Smart inbox: {:?}", smart_inbox); - assert_eq!(smart_inbox.custom_name, new_name); - } - } } // For benchmarking purposes diff --git a/shinkai-libs/shinkai-sqlite/src/lib.rs b/shinkai-libs/shinkai-sqlite/src/lib.rs index 18303e2e0..078e29423 100644 --- a/shinkai-libs/shinkai-sqlite/src/lib.rs +++ b/shinkai-libs/shinkai-sqlite/src/lib.rs @@ -733,6 +733,7 @@ impl SqliteManager { execution_time TEXT NOT NULL, success INTEGER NOT NULL CHECK (success IN (0, 1)), error_message TEXT, + job_id TEXT, FOREIGN KEY(task_id) REFERENCES cron_tasks(task_id) );", [], From 83c417f8a60a03cd176cef0fa62b10a5c65a679a Mon Sep 17 00:00:00 2001 From: Nico Arqueros Date: Fri, 20 Dec 2024 23:38:04 +0530 Subject: [PATCH 5/6] fix deletion --- .../shinkai-node/src/network/v2_api/api_v2_commands_cron.rs | 1 + shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_cron.rs b/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_cron.rs index 9f8711856..131ea88e7 100644 --- a/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_cron.rs +++ b/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_cron.rs @@ -168,6 +168,7 @@ impl Node { .into_iter() .map(|log| { json!({ + "job_id": log.3.as_ref().map_or("", |j| j), "task_id": task_id.to_string(), "execution_time": log.0, "success": log.1, diff --git a/shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs b/shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs index a1f64838a..22fcf2d2c 100644 --- a/shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs +++ b/shinkai-libs/shinkai-sqlite/src/cron_task_manager.rs @@ -32,7 +32,13 @@ impl SqliteManager { pub fn remove_cron_task(&self, task_id: i64) -> Result<(), SqliteManagerError> { let conn = self.get_connection()?; + + // First, delete all related execution records + conn.execute("DELETE FROM cron_task_executions WHERE task_id = ?1", params![task_id])?; + + // Then, delete the cron task conn.execute("DELETE FROM cron_tasks WHERE task_id = ?1", params![task_id])?; + Ok(()) } From ffc78bfa853aee10e596738f758881226ed10be8 Mon Sep 17 00:00:00 2001 From: Nico Arqueros Date: Sun, 22 Dec 2024 13:12:33 +0530 Subject: [PATCH 6/6] bump code runner --- Cargo.lock | 4 +- Cargo.toml | 2 +- .../shinkai-node/src/managers/tool_router.rs | 84 +++++++++---------- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b01c7004c..a6d53baca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6847,9 +6847,9 @@ dependencies = [ [[package]] name = "shinkai_tools_runner" -version = "0.9.5" +version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c57bb4f0f17afa717610da76132fade98dd41b01ef9d46a4323b53c66ab728" +checksum = "ed2fe0b6aa10cd6dcfafd165c2691d494ef14bb1243ed2881ddd2d2dd1b7f787" dependencies = [ "anyhow", "base64-simd", diff --git a/Cargo.toml b/Cargo.toml index d0802962a..13194ccf2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ chrono = "0.4" serde_json = "1.0.117" anyhow = "1.0" blake3 = "1.2.0" -shinkai_tools_runner = "0.9.5" +shinkai_tools_runner = "0.9.6" serde = "1.0.188" base64 = "0.22.0" reqwest = "0.11.27" diff --git a/shinkai-bin/shinkai-node/src/managers/tool_router.rs b/shinkai-bin/shinkai-node/src/managers/tool_router.rs index 5e5a60764..34495f25e 100644 --- a/shinkai-bin/shinkai-node/src/managers/tool_router.rs +++ b/shinkai-bin/shinkai-node/src/managers/tool_router.rs @@ -69,49 +69,7 @@ impl ToolRouter { .map_err(|e| ToolError::DatabaseError(e.to_string()))?; } - // Import tools - async fn import_tools_from_directory(db: Arc) -> Result<(), ToolError> { - let url = env::var("SHINKAI_TOOLS_DIRECTORY_URL") - .map_err(|_| ToolError::MissingConfigError("SHINKAI_TOOLS_DIRECTORY_URL not set".to_string()))?; - - let response = reqwest::get(url) - .await - .map_err(|e| ToolError::RequestError(e))?; - - if response.status() != 200 { - return Err(ToolError::ExecutionError(format!("Import tools request returned a non OK status: {}", response.status()))); - } - - let tools: Vec = response - .json() - .await - .map_err(|e| ToolError::ParseError(format!("Failed to parse tools directory: {}", e)))?; - - for tool in tools { - let tool_url = tool["file"] - .as_str() - .ok_or_else(|| ToolError::ParseError("Missing or invalid file URL in tool definition".to_string()))?; - - let tool_name = tool["name"] - .as_str() - .unwrap_or("unknown"); - - match Node::v2_api_import_tool_internal( - db.clone(), - fetch_node_environment(), - tool_url.to_string(), - ) - .await - { - Ok(_) => println!("Successfully imported tool {}", tool_name), - Err(e) => eprintln!("Failed to import tool {}: {:#?}", tool_name, e), - } - } - - Ok(()) - } - - if let Err(e) = import_tools_from_directory(self.sqlite_manager.clone()).await { + if let Err(e) = Self::import_tools_from_directory(self.sqlite_manager.clone()).await { eprintln!("Error importing tools from directory: {}", e); } @@ -142,6 +100,40 @@ impl ToolRouter { Ok(()) } + async fn import_tools_from_directory(db: Arc) -> Result<(), ToolError> { + let url = env::var("SHINKAI_TOOLS_DIRECTORY_URL") + .map_err(|_| ToolError::MissingConfigError("SHINKAI_TOOLS_DIRECTORY_URL not set".to_string()))?; + + let response = reqwest::get(url).await.map_err(|e| ToolError::RequestError(e))?; + + if response.status() != 200 { + return Err(ToolError::ExecutionError(format!( + "Import tools request returned a non OK status: {}", + response.status() + ))); + } + + let tools: Vec = response + .json() + .await + .map_err(|e| ToolError::ParseError(format!("Failed to parse tools directory: {}", e)))?; + + for tool in tools { + let tool_url = tool["file"] + .as_str() + .ok_or_else(|| ToolError::ParseError("Missing or invalid file URL in tool definition".to_string()))?; + + let tool_name = tool["name"].as_str().unwrap_or("unknown"); + + match Node::v2_api_import_tool_internal(db.clone(), fetch_node_environment(), tool_url.to_string()).await { + Ok(_) => println!("Successfully imported tool {}", tool_name), + Err(e) => eprintln!("Failed to import tool {}: {:#?}", tool_name, e), + } + } + + Ok(()) + } + pub async fn add_static_prompts(&self, _generator: &Box) -> Result<(), ToolError> { // Check if ONLY_TESTING_PROMPTS is set if env::var("ONLY_TESTING_PROMPTS").unwrap_or_default() == "1" @@ -224,9 +216,15 @@ impl ToolRouter { { for (name, definition) in tools { + // Skip tools that start with "demo" if not only_testing_js_tools + if !only_testing_js_tools && name.starts_with("demo") { + continue; + } + // Skip tools that are not in the allowed list if only_testing_js_tools is true if only_testing_js_tools && !allowed_tools.contains(&name.as_str()) { continue; // Skip tools that are not in the allowed list } + println!("Adding JS tool: {}", name); let toolkit = JSToolkit::new(&name, vec![definition.clone()]);