Execute a tool by name, bypassing the LLM.
(
&self,
py: Python<'_>,
name: String,
args: &Bound<'_, pyo3::types::PyDict>,
)
| 1906 | |
| 1907 | /// Execute a tool by name, bypassing the LLM. |
| 1908 | fn tool( |
| 1909 | &self, |
| 1910 | py: Python<'_>, |
| 1911 | name: String, |
| 1912 | args: &Bound<'_, pyo3::types::PyDict>, |
| 1913 | ) -> PyResult<PyToolResult> { |
| 1914 | let json_str = py_dict_to_json(args)?; |
| 1915 | let json_value: serde_json::Value = serde_json::from_str(&json_str) |
| 1916 | .map_err(|e| PyValueError::new_err(format!("Invalid JSON args: {e}")))?; |
| 1917 | |
| 1918 | let session = self.inner.clone(); |
| 1919 | let result = py |
| 1920 | .allow_threads(move || get_runtime().block_on(session.tool(&name, json_value))) |
| 1921 | .map_err(|e| PyRuntimeError::new_err(format!("Tool execution failed: {e}")))?; |
| 1922 | |
| 1923 | Ok(PyToolResult { |
| 1924 | name: result.name, |
| 1925 | output: result.output, |
| 1926 | exit_code: result.exit_code, |
| 1927 | metadata_json: result.metadata.as_ref().map(serde_json::Value::to_string), |
| 1928 | error_kind_json: result |
| 1929 | .error_kind |
| 1930 | .as_ref() |
| 1931 | .and_then(|k| serde_json::to_string(k).ok()), |
| 1932 | }) |
| 1933 | } |
| 1934 | |
| 1935 | /// Delegate a bounded task with the compact object-shaped API. |
| 1936 | fn task(&self, py: Python<'_>, options: &Bound<'_, PyDict>) -> PyResult<PyToolResult> { |