(
py: Python<'_>,
tool_calls_json: String,
node_tools: Vec<String>,
)
| 1013 | /// Execute tool calls from workflow |
| 1014 | #[pyfunction] |
| 1015 | pub(crate) fn execute_workflow_tool_calls( |
| 1016 | py: Python<'_>, |
| 1017 | tool_calls_json: String, |
| 1018 | node_tools: Vec<String>, |
| 1019 | ) -> PyResult<String> { |
| 1020 | // Parse the tool calls JSON |
| 1021 | let tool_calls: Vec<serde_json::Value> = |
| 1022 | serde_json::from_str(&tool_calls_json).map_err(|e| { |
| 1023 | PyErr::new::<pyo3::exceptions::PyValueError, _>(format!( |
| 1024 | "Failed to parse tool calls JSON: {}", |
| 1025 | e |
| 1026 | )) |
| 1027 | })?; |
| 1028 | |
| 1029 | let mut results = Vec::new(); |
| 1030 | |
| 1031 | for tool_call in tool_calls { |
| 1032 | if let (Some(tool_name), Some(parameters)) = ( |
| 1033 | tool_call.get("tool_name").and_then(|v| v.as_str()), |
| 1034 | tool_call.get("parameters").and_then(|v| v.as_object()), |
| 1035 | ) { |
| 1036 | // Validate tool availability |
| 1037 | if !node_tools.contains(&tool_name.to_string()) { |
| 1038 | results.push(format!( |
| 1039 | "{}: Error - Tool '{}' not available for this node", |
| 1040 | tool_name, tool_name |
| 1041 | )); |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | // Execute tool using the global registry |
| 1046 | let tool_result = TOOL_REGISTRY.with(|registry| { |
| 1047 | let registry = registry.borrow(); |
| 1048 | if let Some(tool_func) = registry.get(tool_name) { |
| 1049 | // Convert parameters to Python kwargs |
| 1050 | let kwargs = pyo3::types::PyDict::new(py); |
| 1051 | |
| 1052 | // Convert each parameter from JSON to Python |
| 1053 | for (key, value) in parameters { |
| 1054 | match json_to_python_value(value, py) { |
| 1055 | Ok(py_value) => { |
| 1056 | if let Err(e) = kwargs.set_item(key, py_value) { |
| 1057 | return Ok::<String, PyErr>(format!( |
| 1058 | "{}: Error - Failed to set parameter '{}': {}", |
| 1059 | tool_name, key, e |
| 1060 | )); |
| 1061 | } |
| 1062 | } |
| 1063 | Err(e) => { |
| 1064 | return Ok::<String, PyErr>(format!( |
| 1065 | "{}: Error - Failed to convert parameter '{}': {}", |
| 1066 | tool_name, key, e |
| 1067 | )); |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | // Execute the tool function |
nothing calls this directly
no test coverage detected