Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check config before running tools #754

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion shinkai-bin/shinkai-node/src/managers/tool_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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![],
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<ToolConfig>) -> 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(())
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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
Expand Down