Execute a tool from the global registry
(
py: Python<'_>,
tool_name: &str,
parameters: &serde_json::Map<String, serde_json::Value>,
)
| 941 | |
| 942 | /// Execute a tool from the global registry |
| 943 | fn execute_tool_from_global_registry( |
| 944 | py: Python<'_>, |
| 945 | tool_name: &str, |
| 946 | parameters: &serde_json::Map<String, serde_json::Value>, |
| 947 | ) -> PyResult<String> { |
| 948 | use crate::tools::decorator::get_global_registry; |
| 949 | |
| 950 | // Get the global registry |
| 951 | let global_registry = get_global_registry(); |
| 952 | let global_guard = global_registry.lock().map_err(|e| { |
| 953 | PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!( |
| 954 | "Failed to acquire global registry lock: {}", |
| 955 | e |
| 956 | )) |
| 957 | })?; |
| 958 | |
| 959 | // Check if the tool exists in the global registry |
| 960 | let has_tool = global_guard.has_tool(tool_name).map_err(|e| { |
| 961 | PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!( |
| 962 | "Failed to check tool existence: {}", |
| 963 | e |
| 964 | )) |
| 965 | })?; |
| 966 | |
| 967 | if !has_tool { |
| 968 | return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!( |
| 969 | "Tool '{}' not found in global registry", |
| 970 | tool_name |
| 971 | ))); |
| 972 | } |
| 973 | |
| 974 | // Execute the tool using the global registry's execute method |
| 975 | // Convert parameters to the format expected by the registry |
| 976 | let params_dict = pyo3::types::PyDict::new(py); |
| 977 | for (key, value) in parameters { |
| 978 | match json_to_python_value(value, py) { |
| 979 | Ok(py_value) => { |
| 980 | params_dict.set_item(key, py_value)?; |
| 981 | } |
| 982 | Err(e) => { |
| 983 | return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!( |
| 984 | "Failed to convert parameter '{}': {}", |
| 985 | key, e |
| 986 | ))); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | // Execute the tool through the global registry |
| 992 | match global_guard.execute_tool(tool_name, ¶ms_dict, py) { |
| 993 | Ok(tool_result) => { |
| 994 | if tool_result.success { |
| 995 | Ok(format!("{}: {}", tool_name, tool_result.output)) |
| 996 | } else { |
| 997 | Ok(format!( |
| 998 | "{}: Error - {}", |
| 999 | tool_name, |
| 1000 | tool_result |
no test coverage detected