From 22f86a4f0dd5ca23b4ea50ce2b85f5e3edea9b20 Mon Sep 17 00:00:00 2001 From: Eddie Date: Tue, 24 Dec 2024 11:03:05 -0300 Subject: [PATCH] check config before running tools --- .../shinkai-node/src/managers/tool_router.rs | 8 ++++++- .../tool_execution/execution_coordinator.rs | 6 ++++- .../tool_execution/execution_deno_dynamic.rs | 6 +++-- .../execution_header_generator.rs | 24 ++++++++++++++++++- .../execution_python_dynamic.rs | 6 +++-- 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/shinkai-bin/shinkai-node/src/managers/tool_router.rs b/shinkai-bin/shinkai-node/src/managers/tool_router.rs index ad631a5f9..041c7364f 100644 --- a/shinkai-bin/shinkai-node/src/managers/tool_router.rs +++ b/shinkai-bin/shinkai-node/src/managers/tool_router.rs @@ -8,7 +8,7 @@ use crate::llm_provider::execution::chains::inference_chain_trait::{FunctionCall use crate::network::v2_api::api_v2_commands_app_files::get_app_folder_path; use crate::network::Node; use crate::tools::tool_definitions::definition_generation::{generate_tool_definitions, get_rust_tools}; -use crate::tools::tool_execution::execution_header_generator::generate_execution_environment; +use crate::tools::tool_execution::execution_header_generator::{check_tool_config, generate_execution_environment}; use crate::utils::environment::fetch_node_environment; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -622,6 +622,8 @@ async def run(c: CONFIG, p: INPUTS) -> OUTPUT: .await .map_err(|e| ToolError::ExecutionError(e.to_string()))?; + check_tool_config(shinkai_tool.tool_router_key().clone(), python_tool.config.clone()).await?; + let folder = get_app_folder_path(node_env.clone(), context.full_job().job_id().to_string()); let mounts = Node::v2_api_list_app_files_internal(folder.clone(), true); if let Err(e) = mounts { @@ -701,6 +703,8 @@ async def run(c: CONFIG, p: INPUTS) -> OUTPUT: .await .map_err(|e| ToolError::ExecutionError(e.to_string()))?; + check_tool_config(shinkai_tool.tool_router_key().clone(), deno_tool.config.clone()).await?; + let folder = get_app_folder_path(node_env.clone(), context.full_job().job_id().to_string()); let mounts = Node::v2_api_list_app_files_internal(folder.clone(), true); if let Err(e) = mounts { @@ -1057,6 +1061,8 @@ async def run(c: CONFIG, p: INPUTS) -> OUTPUT: .await .map_err(|e| ToolError::ExecutionError(e.to_string()))?; + check_tool_config(shinkai_tool.tool_router_key().clone(), function_config_vec.clone()).await?; + let result = js_tool .run( env, diff --git a/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_coordinator.rs b/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_coordinator.rs index b95371c18..ea550381b 100644 --- a/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_coordinator.rs +++ b/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_coordinator.rs @@ -2,7 +2,7 @@ use crate::llm_provider::job_manager::JobManager; use crate::tools::tool_definitions::definition_generation::generate_tool_definitions; use crate::tools::tool_execution::execution_custom::execute_custom_tool; use crate::tools::tool_execution::execution_deno_dynamic::{check_deno_tool, execute_deno_tool}; -use crate::tools::tool_execution::execution_header_generator::generate_execution_environment; +use crate::tools::tool_execution::execution_header_generator::{check_tool_config, generate_execution_environment}; use crate::tools::tool_execution::execution_python_dynamic::execute_python_tool; use crate::utils::environment::fetch_node_environment; @@ -172,6 +172,8 @@ pub async fn execute_tool_cmd( ) .await?; + check_tool_config(tool_router_key.clone(), python_tool.config.clone()).await?; + let node_env = fetch_node_environment(); let node_storage_path = node_env .node_storage_path @@ -215,6 +217,8 @@ pub async fn execute_tool_cmd( ) .await?; + check_tool_config(tool_router_key.clone(), deno_tool.config.clone()).await?; + let node_env = fetch_node_environment(); let node_storage_path = node_env .node_storage_path diff --git a/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_deno_dynamic.rs b/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_deno_dynamic.rs index 092a56f28..a5059d614 100644 --- a/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_deno_dynamic.rs +++ b/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_deno_dynamic.rs @@ -9,7 +9,7 @@ use shinkai_tools_primitives::tools::parameters::Parameters; use shinkai_tools_primitives::tools::tool_config::{OAuth, ToolConfig}; use shinkai_tools_primitives::tools::tool_output_arg::ToolOutputArg; -use super::execution_header_generator::generate_execution_environment; +use super::execution_header_generator::{check_tool_config, generate_execution_environment}; use crate::utils::environment::fetch_node_environment; use shinkai_sqlite::SqliteManager; use std::sync::Arc; @@ -35,7 +35,7 @@ pub async fn execute_deno_tool( author: "system".to_string(), js_code: code, tools: None, - config: vec![], + config: extra_config.clone(), oauth: oauth.clone(), description: "Deno runtime execution".to_string(), keywords: vec![], @@ -62,6 +62,8 @@ pub async fn execute_deno_tool( ) .await?; + check_tool_config("code-execution".to_string(), tool.config.clone()).await?; + let node_env = fetch_node_environment(); let node_storage_path = node_env .node_storage_path diff --git a/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_header_generator.rs b/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_header_generator.rs index fdf21f9de..12e424636 100644 --- a/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_header_generator.rs +++ b/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_header_generator.rs @@ -1,7 +1,10 @@ use std::{collections::HashMap, sync::Arc}; use shinkai_sqlite::SqliteManager; -use shinkai_tools_primitives::tools::{error::ToolError, tool_config::OAuth}; +use shinkai_tools_primitives::tools::{ + error::ToolError, + tool_config::{OAuth, ToolConfig}, +}; use super::execution_coordinator::handle_oauth; @@ -29,3 +32,22 @@ pub async fn generate_execution_environment( Ok(envs) } + +pub async fn check_tool_config(tool_router_key: String, tool_config: Vec) -> Result<(), ToolError> { + for config in tool_config { + println!("config: {:?}", config); + match config { + ToolConfig::BasicConfig(config) => { + if config.key_value.is_none() && config.required { + let fix_redirect_url = format!("shinkai://config?tool={}", urlencoding::encode(&tool_router_key)); + return Err(ToolError::MissingConfigError(format!( + "\n\nCannot run tool, config is for \"{}\" is missing.\n\nClick the link to update the tool config and try again.\n\n{}", + config.key_name, fix_redirect_url + ))); + } + } + } + } + + Ok(()) +} diff --git a/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_python_dynamic.rs b/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_python_dynamic.rs index 81036e92f..d7cef768b 100644 --- a/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_python_dynamic.rs +++ b/shinkai-bin/shinkai-node/src/tools/tool_execution/execution_python_dynamic.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, path::PathBuf}; -use super::execution_header_generator::generate_execution_environment; +use super::execution_header_generator::{check_tool_config, generate_execution_environment}; use crate::utils::environment::fetch_node_environment; use serde_json::{Map, Value}; use shinkai_message_primitives::schemas::shinkai_name::ShinkaiName; @@ -36,7 +36,7 @@ pub async fn execute_python_tool( author: "system".to_string(), py_code: code, tools: None, - config: vec![], + config: extra_config.clone(), description: "Python runtime execution".to_string(), keywords: vec![], input_args: Parameters::new(), @@ -62,6 +62,8 @@ pub async fn execute_python_tool( ) .await?; + check_tool_config("code-execution".to_string(), tool.config.clone()).await?; + let node_env = fetch_node_environment(); let node_storage_path = node_env .node_storage_path