| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use async_trait::async_trait; |
| 4 | use deepseek_protocol::{ToolKind, ToolOutput, ToolPayload}; |
| 5 | use deepseek_tools::{ |
| 6 | ToolCall, ToolCallSource, ToolHandler, ToolInvocation, ToolRegistry, ToolSpec, |
| 7 | }; |
| 8 | use serde_json::json; |
| 9 | |
| 10 | struct EchoHandler; |
| 11 | |
| 12 | #[async_trait] |
| 13 | impl ToolHandler for EchoHandler { |
| 14 | fn kind(&self) -> ToolKind { |
| 15 | ToolKind::Function |
| 16 | } |
| 17 | |
| 18 | fn is_mutating(&self) -> bool { |
| 19 | false |
| 20 | } |
| 21 | |
| 22 | async fn handle( |
| 23 | &self, |
| 24 | invocation: ToolInvocation, |
| 25 | ) -> std::result::Result<ToolOutput, deepseek_tools::FunctionCallError> { |
| 26 | Ok(ToolOutput::Function { |
| 27 | body: Some(json!({ |
| 28 | "tool": invocation.tool_name, |
| 29 | "call_id": invocation.call_id |
| 30 | })), |
| 31 | success: true, |
| 32 | }) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #[tokio::test] |
| 37 | async fn dispatches_function_tool_with_parallel_flag() { |
| 38 | let mut registry = ToolRegistry::default(); |
| 39 | registry |
| 40 | .register( |
| 41 | ToolSpec { |
| 42 | name: "echo".to_string(), |
| 43 | input_schema: json!({"type":"object"}), |
| 44 | output_schema: json!({"type":"object"}), |
| 45 | supports_parallel_tool_calls: true, |
| 46 | timeout_ms: Some(1000), |
| 47 | }, |
| 48 | Arc::new(EchoHandler), |
| 49 | ) |
| 50 | .expect("register tool"); |
| 51 | |
| 52 | let output = registry |
| 53 | .dispatch( |
| 54 | ToolCall { |
| 55 | name: "echo".to_string(), |
| 56 | payload: ToolPayload::Function { |
| 57 | arguments: "{\"message\":\"hi\"}".to_string(), |
| 58 | }, |
| 59 | source: ToolCallSource::Direct, |
| 60 | raw_tool_call_id: Some("call-1".to_string()), |
| 61 | }, |
| 62 | true, |
| 63 | ) |
| 64 | .await |
| 65 | .expect("dispatch tool"); |
| 66 | match output { |
| 67 | ToolOutput::Function { success, .. } => assert!(success), |
| 68 | other => panic!("unexpected output: {other:?}"), |
| 69 | } |
| 70 | } |
| 71 |