From a88a52c89d83c6242705d0586a738a7c7315b23d Mon Sep 17 00:00:00 2001 From: Eddie Date: Mon, 30 Dec 2024 16:03:10 -0300 Subject: [PATCH] tool match w/version --- .../api_v2_commands_ext_agent_offers.rs | 2 +- .../network/v2_api/api_v2_commands_tools.rs | 14 +++++++++-- .../src/shinkai_tool_manager.rs | 25 ++++++++++++------- .../src/tools/shinkai_tool.rs | 8 +++--- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_ext_agent_offers.rs b/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_ext_agent_offers.rs index 4e331033d..ced676111 100644 --- a/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_ext_agent_offers.rs +++ b/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_ext_agent_offers.rs @@ -157,7 +157,7 @@ impl Node { } // Get the tool from the database - match db.tool_exists(&tool_offering.tool_key) { + match db.tool_exists(&tool_offering.tool_key, None) { Ok(exists) => { if !exists { let api_error = APIError { diff --git a/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_tools.rs b/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_tools.rs index 3704d5484..80b43f462 100644 --- a/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_tools.rs +++ b/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_tools.rs @@ -604,8 +604,18 @@ impl Node { } // Create a longer-lived binding for the db clone - - match db.tool_exists(&shinkai_tool.tool_router_key().to_string_without_version()) { + let version = shinkai_tool.version_indexable(); + if version.is_err() { + let api_error = APIError { + code: StatusCode::INTERNAL_SERVER_ERROR.as_u16(), + error: "Internal Server Error".to_string(), + message: format!("Failed to get version: {}", version.err().unwrap()), + }; + let _ = res.send(Err(api_error)).await; + return Ok(()); + } + let version = Some(version.unwrap()); + match db.tool_exists(&shinkai_tool.tool_router_key().to_string_without_version(), version) { Ok(true) => { // Tool already exists, update it match db.update_tool(shinkai_tool).await { diff --git a/shinkai-libs/shinkai-sqlite/src/shinkai_tool_manager.rs b/shinkai-libs/shinkai-sqlite/src/shinkai_tool_manager.rs index f27e2676c..256790fdb 100644 --- a/shinkai-libs/shinkai-sqlite/src/shinkai_tool_manager.rs +++ b/shinkai-libs/shinkai-sqlite/src/shinkai_tool_manager.rs @@ -490,20 +490,27 @@ impl SqliteManager { } /// Checks if a tool exists in the shinkai_tools table by its tool_key - pub fn tool_exists(&self, tool_key: &str) -> Result { + pub fn tool_exists(&self, tool_key: &str, version: Option) -> Result { let conn = self.get_connection()?; - let exists: bool = conn - .query_row( + let exists = match version { + Some(version) => conn.query_row( + "SELECT EXISTS(SELECT 1 FROM shinkai_tools WHERE tool_key = ?1 AND version = ?2)", + params![tool_key.to_lowercase(), version.get_version_number()], + |row| row.get(0), + ), + None => conn.query_row( "SELECT EXISTS(SELECT 1 FROM shinkai_tools WHERE tool_key = ?1)", params![tool_key.to_lowercase()], |row| row.get(0), - ) - .map_err(|e| { + ), + }; + match exists { + Ok(exists) => Ok(exists), + Err(e) => { eprintln!("Database error: {}", e); - SqliteManagerError::DatabaseError(e) - })?; - - Ok(exists) + Err(SqliteManagerError::DatabaseError(e)) + } + } } /// Checks if there are any JS tools in the shinkai_tools table diff --git a/shinkai-libs/shinkai-tools-primitives/src/tools/shinkai_tool.rs b/shinkai-libs/shinkai-tools-primitives/src/tools/shinkai_tool.rs index 0c4ba16d3..be01c2654 100644 --- a/shinkai-libs/shinkai-tools-primitives/src/tools/shinkai_tool.rs +++ b/shinkai-libs/shinkai-tools-primitives/src/tools/shinkai_tool.rs @@ -361,11 +361,13 @@ impl ShinkaiTool { matches!(self, ShinkaiTool::Network(_, _)) } + pub fn version_indexable(&self) -> Result { + IndexableVersion::from_string(&self.version()) + } + /// Returns the version number using IndexableVersion pub fn version_number(&self) -> Result { - let version_str = self.version(); - - let indexable_version = IndexableVersion::from_string(&version_str)?; + let indexable_version = self.version_indexable()?; Ok(indexable_version.get_version_number()) } }