Skip to content

Commit

Permalink
better paths
Browse files Browse the repository at this point in the history
  • Loading branch information
acedward committed Dec 23, 2024
1 parent d4316f5 commit b6afbfe
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>()
.to_lowercase()
}

#[utoipa::path(
post,
path = "/v2/app_file",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()));
}
_ => {}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
Expand Down
42 changes: 27 additions & 15 deletions shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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::<String>()
.to_lowercase()
}

#[utoipa::path(
get,
path = "/v2/tool_definitions",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit b6afbfe

Please sign in to comment.