From 4e1b83194847d75bf7dc2c56858b191ddc90e614 Mon Sep 17 00:00:00 2001 From: ooodc <549496103@qq.com> Date: Mon, 27 Apr 2026 17:15:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=86=85=E5=AE=B9=E6=A0=BC=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E7=A9=BA=E5=8F=82=E6=95=B0=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=B9=B6=E6=94=B9=E8=BF=9B=E5=8F=82=E6=95=B0=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bus/message.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/bus/message.rs b/src/bus/message.rs index b0980dd..10db019 100644 --- a/src/bus/message.rs +++ b/src/bus/message.rs @@ -468,21 +468,10 @@ impl OutboundMessage { } pub(crate) fn format_tool_call_content(tool_name: &str, tool_arguments: &serde_json::Value) -> String { - let mut lines = vec![format!("### {}", tool_name)]; - match tool_arguments { - serde_json::Value::Object(map) if !map.is_empty() => { - let mut entries: Vec<_> = map.iter().collect(); - entries.sort_by(|(left, _), (right, _)| left.cmp(right)); - for (key, value) in entries { - lines.push(format!("- {}: {}", key, format_tool_argument_value(value))); - } - } - serde_json::Value::Object(_) => {} - other => lines.push(format!("- args: {}", format_tool_argument_value(other))), + serde_json::Value::Object(map) if map.is_empty() => tool_name.to_string(), + other => format!("{}\nargs: {}", tool_name, format_tool_arguments_json(other)), } - - lines.join("\n") } fn format_tool_result_content(tool_name: &str, content: &str) -> String { @@ -497,6 +486,28 @@ fn format_tool_argument_value(value: &serde_json::Value) -> String { } } +fn format_tool_arguments_json(value: &serde_json::Value) -> String { + match value { + serde_json::Value::Object(map) => { + let mut entries: Vec<_> = map.iter().collect(); + entries.sort_by(|(left, _), (right, _)| left.cmp(right)); + let body = entries + .into_iter() + .map(|(key, value)| { + format!( + "{}:{}", + serde_json::to_string(key).unwrap_or_else(|_| format!("\"{}\"", key)), + serde_json::to_string(value).unwrap_or_else(|_| value.to_string()), + ) + }) + .collect::>() + .join(","); + format!("{{{}}}", body) + } + other => format_tool_argument_value(other), + } +} + // ============================================================================ // Helpers // ============================================================================ @@ -545,9 +556,9 @@ mod tests { assert_eq!(outbound[0].event_kind, OutboundEventKind::ToolCall); assert_eq!(outbound[0].tool_name.as_deref(), Some("calculator")); assert_eq!(outbound[0].tool_arguments.as_ref().unwrap()["expression"], "1 + 1"); - assert_eq!(outbound[0].content, "### calculator\n- expression: 1 + 1"); + assert_eq!(outbound[0].content, "calculator\nargs: {\"expression\":\"1 + 1\"}"); assert_eq!(outbound[1].tool_name.as_deref(), Some("file_read")); - assert_eq!(outbound[1].content, "### file_read\n- path: README.md"); + assert_eq!(outbound[1].content, "file_read\nargs: {\"path\":\"README.md\"}"); } #[test]