From b6afbfe052ea4a5eb5b8f34ef1c35c7248c0e660 Mon Sep 17 00:00:00 2001 From: Eddie Date: Mon, 23 Dec 2024 19:08:46 -0300 Subject: [PATCH] better paths --- .../src/api_v2/api_v2_handlers_app_files.rs | 37 +++++++++++----- .../src/api_v2/api_v2_handlers_tools.rs | 42 ++++++++++++------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_app_files.rs b/shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_app_files.rs index 08e1bf909..74001e0e7 100644 --- a/shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_app_files.rs +++ b/shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_app_files.rs @@ -64,6 +64,20 @@ pub fn app_files_routes( .or(delete_file_route) } +pub fn safe_folder_name(tool_router_key: &str) -> String { + tool_router_key + .chars() + .map(|c| { + if c.is_ascii_alphanumeric() || c == '-' || c == '_' { + c + } else { + '_' + } + }) + .collect::() + .to_lowercase() +} + #[utoipa::path( post, path = "/v2/app_file", @@ -95,6 +109,7 @@ pub async fn upload_file_handler( bytes.extend_from_slice(chunk.chunk()); } file_name = String::from_utf8_lossy(&bytes).into_owned(); + file_name = safe_folder_name(&file_name); } "file" => { // Read file data @@ -125,8 +140,8 @@ pub async fn upload_file_handler( sender .send(NodeCommand::V2ApiUploadAppFile { bearer, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), file_name, file_data: file_data.unwrap(), res: res_sender, @@ -207,7 +222,7 @@ pub async fn update_file_handler( while let Ok(Some(chunk)) = stream.try_next().await { bytes.extend_from_slice(chunk.chunk()); } - new_name = Some(String::from_utf8_lossy(&bytes).into_owned()); + new_name = Some(safe_folder_name(&String::from_utf8_lossy(&bytes).into_owned())); } _ => {} } @@ -226,8 +241,8 @@ pub async fn update_file_handler( sender .send(NodeCommand::V2ApiUpdateAppFile { bearer, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), file_name, new_name, file_data, @@ -287,8 +302,8 @@ pub async fn get_file_handler( sender .send(NodeCommand::V2ApiGetAppFile { bearer, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), file_name, res: res_sender, }) @@ -336,8 +351,8 @@ pub async fn list_files_handler( sender .send(NodeCommand::V2ApiListAppFiles { bearer, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), res: res_sender, }) .await @@ -394,8 +409,8 @@ pub async fn delete_file_handler( sender .send(NodeCommand::V2ApiDeleteAppFile { bearer, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), file_name, res: res_sender, }) diff --git a/shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_tools.rs b/shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_tools.rs index 893b213d3..397701f70 100644 --- a/shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_tools.rs +++ b/shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_tools.rs @@ -7,9 +7,8 @@ use utoipa::{OpenApi, ToSchema}; use warp::Filter; use reqwest::StatusCode; use std::collections::HashMap; -use bytes::Bytes; use futures::TryStreamExt; -use warp::multipart::{FormData, Part}; +use warp::multipart::{FormData}; use bytes::Buf; use crate::{node_api_router::APIError, node_commands::NodeCommand}; @@ -53,7 +52,6 @@ pub fn tool_routes( .and(warp::body::json()) .and_then(add_shinkai_tool_handler); - let tool_execution_route = warp::path("tool_execution") .and(warp::post()) .and(with_sender(node_commands_sender.clone())) @@ -222,6 +220,20 @@ pub fn tool_routes( .or(remove_tool_route) } +pub fn safe_folder_name(tool_router_key: &str) -> String { + tool_router_key + .chars() + .map(|c| { + if c.is_ascii_alphanumeric() || c == '-' || c == '_' { + c + } else { + '_' + } + }) + .collect::() + .to_lowercase() +} + #[utoipa::path( get, path = "/v2/tool_definitions", @@ -343,8 +355,8 @@ pub async fn tool_execution_handler( bearer, tool_router_key: payload.tool_router_key.clone(), parameters, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), llm_provider: payload.llm_provider.clone(), extra_config, mounts: payload.mounts, @@ -741,8 +753,8 @@ pub async fn set_playground_tool_handler( .send(NodeCommand::V2ApiSetPlaygroundTool { bearer, payload, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), res: res_sender, }) .await @@ -1031,8 +1043,8 @@ pub async fn code_execution_handler( parameters, extra_config, oauth: payload.oauth, - tool_id: tool_id, - app_id: app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), llm_provider: payload.llm_provider, mounts: payload.mounts, res: res_sender, @@ -1447,8 +1459,8 @@ pub async fn tool_asset_handler( sender .send(NodeCommand::V2ApiUploadToolAsset { bearer, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), file_name, file_data, res: res_sender, @@ -1492,8 +1504,8 @@ pub async fn list_tool_asset_handler( sender .send(NodeCommand::V2ApiListToolAssets { bearer, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), res: res_sender, }) .await @@ -1550,8 +1562,8 @@ pub async fn delete_tool_asset_handler( sender .send(NodeCommand::V2ApiDeleteToolAsset { bearer, - tool_id, - app_id, + tool_id: safe_folder_name(&tool_id), + app_id: safe_folder_name(&app_id), file_name, res: res_sender, })