From 2f47a8a273885ef7827b3a2bc6544f29f3347e48 Mon Sep 17 00:00:00 2001 From: ooodc <549496103@qq.com> Date: Wed, 22 Apr 2026 08:42:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(ws):=20=E7=A7=BB=E9=99=A4=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=BB=93=E6=9E=9C=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E4=BC=98=E5=8C=96=E6=B6=88=E6=81=AF=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bus/message.rs | 18 +++------------- src/gateway/ws.rs | 54 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/bus/message.rs b/src/bus/message.rs index 959e384..0d99009 100644 --- a/src/bus/message.rs +++ b/src/bus/message.rs @@ -319,15 +319,7 @@ impl OutboundMessage { )] } } - "tool" => vec![Self::tool_result( - channel.to_string(), - chat_id.to_string(), - message.tool_call_id.clone().unwrap_or_else(|| message.id.clone()), - message.tool_name.clone().unwrap_or_else(|| "tool".to_string()), - message.content.clone(), - reply_to, - metadata.clone(), - )], + "tool" => Vec::new(), _ => Vec::new(), } } @@ -404,7 +396,7 @@ mod tests { } #[test] - fn test_from_chat_message_maps_tool_result() { + fn test_from_chat_message_omits_tool_result() { let message = ChatMessage::tool("call-9", "calculator", "2"); let outbound = OutboundMessage::from_chat_message( @@ -415,10 +407,6 @@ mod tests { &message, ); - assert_eq!(outbound.len(), 1); - assert_eq!(outbound[0].event_kind, OutboundEventKind::ToolResult); - assert_eq!(outbound[0].tool_call_id.as_deref(), Some("call-9")); - assert_eq!(outbound[0].tool_name.as_deref(), Some("calculator")); - assert!(outbound[0].content.contains("工具结果: calculator")); + assert!(outbound.is_empty()); } } diff --git a/src/gateway/ws.rs b/src/gateway/ws.rs index 9d37879..1c17c1d 100644 --- a/src/gateway/ws.rs +++ b/src/gateway/ws.rs @@ -172,17 +172,7 @@ fn ws_outbound_from_chat_message(message: &ChatMessage) -> Vec { }] } } - "tool" => vec![WsOutbound::ToolResult { - id: message.id.clone(), - tool_call_id: message.tool_call_id.clone().unwrap_or_else(|| message.id.clone()), - tool_name: message.tool_name.clone().unwrap_or_else(|| "tool".to_string()), - content: format!( - "工具结果: {}\n\n{}", - message.tool_name.clone().unwrap_or_else(|| "tool".to_string()), - message.content, - ), - role: message.role.clone(), - }], + "tool" => Vec::new(), _ => Vec::new(), } } @@ -379,3 +369,45 @@ async fn handle_inbound( } } } + +#[cfg(test)] +mod tests { + use super::ws_outbound_from_chat_message; + use crate::bus::ChatMessage; + use crate::providers::ToolCall; + use crate::protocol::WsOutbound; + use serde_json::json; + + #[test] + fn test_ws_outbound_from_chat_message_expands_tool_calls() { + let message = ChatMessage::assistant_with_tool_calls( + "", + vec![ToolCall { + id: "call-1".to_string(), + name: "calculator".to_string(), + arguments: json!({"expression": "1 + 1"}), + }], + ); + + let outbound = ws_outbound_from_chat_message(&message); + + assert_eq!(outbound.len(), 1); + match &outbound[0] { + WsOutbound::ToolCall { tool_call_id, tool_name, arguments, .. } => { + assert_eq!(tool_call_id, "call-1"); + assert_eq!(tool_name, "calculator"); + assert_eq!(arguments["expression"], "1 + 1"); + } + other => panic!("unexpected outbound variant: {:?}", other), + } + } + + #[test] + fn test_ws_outbound_from_chat_message_omits_tool_results() { + let message = ChatMessage::tool("call-1", "calculator", "2"); + + let outbound = ws_outbound_from_chat_message(&message); + + assert!(outbound.is_empty()); + } +}