Wrap a Python callable `(str, Json) -> Json` for tool request intercepts.
(py_fn: Py<PyAny>)
| 238 | |
| 239 | /// Wrap a Python callable `(str, Json) -> Json` for tool request intercepts. |
| 240 | pub fn wrap_py_tool_request_intercept_fn(py_fn: Py<PyAny>) -> ToolInterceptFn { |
| 241 | Arc::new(move |name: &str, args: Json| { |
| 242 | Python::attach(|py| { |
| 243 | let py_args = json_to_py(py, &args).map_err(|e| { |
| 244 | FlowError::Internal(format!("tool callback json_to_py failed for '{name}': {e}")) |
| 245 | })?; |
| 246 | let result = py_fn.call1(py, (name, py_args)).map_err(|e| { |
| 247 | FlowError::Internal(format!("Python tool callable failed for '{name}': {e}")) |
| 248 | })?; |
| 249 | py_to_json(result.bind(py)).map_err(|e| { |
| 250 | FlowError::Internal(format!("tool callback py_to_json failed for '{name}': {e}")) |
| 251 | }) |
| 252 | }) |
| 253 | }) |
| 254 | } |
| 255 | |
| 256 | /// Wrap a Python callable `(Json) -> Json` for tool execution intercepts. |
| 257 | /// Supports both sync and async Python callables. If the callable returns a |