Skip to content

Commit

Permalink
Merge branch 'main' into fix/tool-router-keys-serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
guillevalin authored Dec 30, 2024
2 parents e35e074 + 424b984 commit 71ec3c3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 16 additions & 9 deletions shinkai-libs/shinkai-sqlite/src/shinkai_tool_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, SqliteManagerError> {
pub fn tool_exists(&self, tool_key: &str, version: Option<IndexableVersion>) -> Result<bool, SqliteManagerError> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,13 @@ impl ShinkaiTool {
matches!(self, ShinkaiTool::Network(_, _))
}

pub fn version_indexable(&self) -> Result<IndexableVersion, String> {
IndexableVersion::from_string(&self.version())
}

/// Returns the version number using IndexableVersion
pub fn version_number(&self) -> Result<u64, String> {
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())
}
}
Expand Down

0 comments on commit 71ec3c3

Please sign in to comment.