From 396c8fee5071117914104244cc962db805ab0af0 Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Wed, 27 May 2026 18:13:12 +0800 Subject: [PATCH] [Rust Frontend] Align tool parser fallback behavior between streaming & non-streaming paths (#43662) Signed-off-by: Bugen Zhao --- rust/Cargo.lock | 1 + rust/src/chat/src/output/default/tool.rs | 344 +++++++++++++++--- rust/src/chat/src/parser/tool/mod.rs | 2 +- rust/src/chat/src/parser/tool/tests.rs | 14 +- rust/src/tool-parser/Cargo.toml | 1 + .../src/deepseek_dsml/deepseek_v32.rs | 140 +++---- .../src/deepseek_dsml/deepseek_v4.rs | 39 +- rust/src/tool-parser/src/deepseek_dsml/mod.rs | 33 +- .../src/deepseek_json/deepseek_v3.rs | 73 ++-- .../src/deepseek_json/deepseek_v31.rs | 81 +++-- rust/src/tool-parser/src/deepseek_json/mod.rs | 34 +- rust/src/tool-parser/src/gemma4.rs | 148 ++++---- rust/src/tool-parser/src/glm_xml/glm45_moe.rs | 15 +- rust/src/tool-parser/src/glm_xml/glm47_moe.rs | 49 +-- rust/src/tool-parser/src/glm_xml/mod.rs | 102 +++--- rust/src/tool-parser/src/json/hermes.rs | 73 ++-- rust/src/tool-parser/src/json/llama.rs | 131 +++---- rust/src/tool-parser/src/json/mistral.rs | 75 ++-- rust/src/tool-parser/src/json/mod.rs | 64 ++-- rust/src/tool-parser/src/json/qwen.rs | 83 ++--- rust/src/tool-parser/src/kimi_k2.rs | 114 +++--- rust/src/tool-parser/src/lib.rs | 65 +++- rust/src/tool-parser/src/minimax_m2.rs | 139 +++---- rust/src/tool-parser/src/qwen_coder.rs | 178 ++++----- rust/src/tool-parser/src/test_utils.rs | 16 +- rust/src/tool-parser/src/tests.rs | 72 ++-- 26 files changed, 1187 insertions(+), 899 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 5a05d9e0b5..42b3c230cc 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -5850,6 +5850,7 @@ name = "vllm-tool-parser" version = "0.1.0" dependencies = [ "criterion", + "easy-ext", "expect-test", "futures", "openai-protocol", diff --git a/rust/src/chat/src/output/default/tool.rs b/rust/src/chat/src/output/default/tool.rs index 9ab5ee1684..9774f64381 100644 --- a/rust/src/chat/src/output/default/tool.rs +++ b/rust/src/chat/src/output/default/tool.rs @@ -15,7 +15,7 @@ use crate::Result; use crate::error::Error; use crate::event::AssistantBlockKind; use crate::output::generate_tool_call_id; -use crate::parser::tool::{ToolCallDelta, ToolParseResult, ToolParser}; +use crate::parser::tool::{ToolCallDelta, ToolParser, ToolParserOutput}; /// Per-stream tool parsing state. struct ToolState { @@ -57,46 +57,52 @@ impl ToolState { return Ok(events); } - let parse_result = self.parser.push(&delta); + let mut output = ToolParserOutput::default(); + let parse_result = self.parser.parse_into(&delta, &mut output); match parse_result { - Ok(result) => self.process_parse_result(kind, result, &mut events)?, + Ok(()) => self.process_parser_output(kind, output, &mut events)?, Err(error) => { - if !self.parser_failed { - warn!( - error = %error.as_report(), - "tool parser failed; falling back to plain text deltas" - ); - self.parser_failed = true; - } + warn!( + error = %error.as_report(), + "tool parser failed; falling back to plain text deltas" + ); + // Permanently mark this parser as failed. + // TODO: we may consider recovering from parsing errors in the future. + self.parser_failed = true; + + // On parsing failure, we still apply the partial parser output if any, but we close + // any open tool calls and emit the remaining buffered text as a plain-text delta to + // preserve as much of the output as possible. + self.process_parser_output(kind, output, &mut events)?; self.open_call_index = None; - events.push(AssistantEvent::TextDelta { kind, delta }); + push_text_delta(&mut events, kind, self.parser.reset()); } } Ok(events) } - /// Apply one parsed tool result to the current stream state. - fn process_parse_result( + /// Apply one parsed tool output to the current stream state. + fn process_parser_output( &mut self, kind: AssistantBlockKind, - result: ToolParseResult, + output: ToolParserOutput, events: &mut Vec, ) -> Result<()> { // When we are not currently streaming a tool call, preserve plain // text first and then surface any new tool call items. if self.open_call_index.is_none() { - push_text_delta(events, kind, result.normal_text); - self.process_tool_items(result.calls, events)?; + push_text_delta(events, kind, output.normal_text); + self.process_tool_items(output.calls, events)?; } else { // Once a tool call is open, prioritize tool deltas first. If the // parser emits normal text again, close the tool call and resume // plain text output. - self.process_tool_items(result.calls, events)?; - if !result.normal_text.is_empty() { + self.process_tool_items(output.calls, events)?; + if !output.normal_text.is_empty() { self.open_call_index = None; - push_text_delta(events, kind, result.normal_text); + push_text_delta(events, kind, output.normal_text); } } Ok(()) @@ -158,8 +164,8 @@ impl ToolState { } match self.parser.finish() { - Ok(result) => { - self.process_parse_result(AssistantBlockKind::Text, result, &mut events)? + Ok(output) => { + self.process_parser_output(AssistantBlockKind::Text, output, &mut events)? } Err(error) => { warn!( @@ -265,17 +271,24 @@ mod tests { use crate::error::Error; use crate::event::{AssistantBlockKind, AssistantMessageExt as _}; use crate::output::structured::structured_chat_event_stream; - use crate::parser::tool::{ToolParseResult, ToolParser, ToolParserError}; + use crate::parser::tool::{ + DeepSeekV4ToolParser, ToolParser, ToolParserError, ToolParserOutput, + }; use crate::request::ChatTool; - use crate::stream::ChatEventStream; + use crate::stream::{ChatEventStream, CollectedAssistantMessage}; struct FailingParser { fail_next: bool, + buffered: String, } struct ScriptedParser { - push_results: Vec, - finish_result: ToolParseResult, + push_outputs: Vec, + finish_output: ToolParserOutput, + } + + struct PartialThenFailParser { + buffered: String, } impl ToolParser for FailingParser { @@ -283,10 +296,14 @@ mod tests { where Self: Sized + 'static, { - Ok(Box::new(Self { fail_next: false })) + Ok(Box::new(Self { + fail_next: false, + buffered: String::new(), + })) } - fn push(&mut self, _chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, _output: &mut ToolParserOutput) -> Result<()> { + self.buffered.push_str(chunk); if self.fail_next { self.fail_next = false; return Err(ToolParserError::ParsingFailed { @@ -294,7 +311,16 @@ mod tests { }); } - Ok(ToolParseResult::default()) + self.buffered.clear(); + Ok(()) + } + + fn finish(&mut self) -> Result { + Ok(ToolParserOutput::default()) + } + + fn reset(&mut self) -> String { + std::mem::take(&mut self.buffered) } } @@ -304,18 +330,215 @@ mod tests { Self: Sized + 'static, { Ok(Box::new(Self { - push_results: Vec::new(), - finish_result: ToolParseResult::default(), + push_outputs: Vec::new(), + finish_output: ToolParserOutput::default(), })) } - fn push(&mut self, _chunk: &str) -> Result { - Ok(self.push_results.pop().unwrap_or_default()) + fn parse_into(&mut self, _chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + let mut next = self.push_outputs.pop().unwrap_or_default(); + output.normal_text.push_str(&next.normal_text); + output.calls.append(&mut next.calls); + Ok(()) } - fn finish(&mut self) -> Result { - Ok(std::mem::take(&mut self.finish_result)) + fn finish(&mut self) -> Result { + Ok(std::mem::take(&mut self.finish_output)) } + + fn reset(&mut self) -> String { + String::new() + } + } + + impl ToolParser for PartialThenFailParser { + fn create(_tools: &[ChatTool]) -> vllm_tool_parser::Result> + where + Self: Sized + 'static, + { + Ok(Box::new(Self { + buffered: String::new(), + })) + } + + fn parse_into(&mut self, _chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + output.calls.extend([ + crate::parser::tool::ToolCallDelta { + tool_index: 0, + name: Some("get_weather".to_string()), + arguments: String::new(), + }, + crate::parser::tool::ToolCallDelta { + tool_index: 0, + name: None, + arguments: r#"{"location":"SF"}"#.to_string(), + }, + ]); + self.buffered.push_str(" trailing text"); + Err(ToolParserError::ParsingFailed { + message: "boom".to_string(), + }) + } + + fn finish(&mut self) -> Result { + Ok(ToolParserOutput::default()) + } + + fn reset(&mut self) -> String { + std::mem::take(&mut self.buffered) + } + } + + fn deepseek_v4_test_tools() -> Vec { + vec![ + ChatTool { + name: "get_weather".to_string(), + description: None, + parameters: serde_json::json!({ + "type": "object", + "properties": { + "location": { "type": "string" } + } + }), + strict: None, + }, + ChatTool { + name: "add".to_string(), + description: None, + parameters: serde_json::json!({ + "type": "object", + "properties": { + "x": { "type": "integer" }, + "y": { "type": "integer" } + } + }), + strict: None, + }, + ] + } + + async fn collect_deepseek_v4_message(chunks: Vec) -> CollectedAssistantMessage { + let events = chunks + .into_iter() + .map(|delta| { + Ok(ContentEvent::TextDelta { + kind: AssistantBlockKind::Text, + delta, + }) + }) + .chain(std::iter::once(Ok(ContentEvent::Done { + prompt_token_count: 1, + output_token_count: 1, + finish_reason: FinishReason::stop_eos(), + kv_transfer_params: None, + }))); + let parser = DeepSeekV4ToolParser::create(&deepseek_v4_test_tools()).unwrap(); + let assistant_events = tool_event_stream(stream::iter(events), Some(parser)); + let chat_events = structured_chat_event_stream(assistant_events); + + ChatEventStream::new("req_deepseek_v4".to_string(), Box::pin(chat_events)) + .collect_message() + .await + .unwrap() + } + + fn message_tool_projection( + message: &CollectedAssistantMessage, + ) -> (String, Vec<(String, serde_json::Value)>) { + ( + message.message.text(), + message + .message + .tool_calls() + .map(|call| { + ( + call.name.clone(), + serde_json::from_str(&call.arguments).unwrap(), + ) + }) + .collect(), + ) + } + + #[tokio::test] + async fn tool_parser_error_preserves_partial_output_and_flushes_buffer() { + let events = stream::iter(vec![ + Ok(ContentEvent::TextDelta { + kind: AssistantBlockKind::Text, + delta: "ignored".to_string(), + }), + Ok(ContentEvent::Done { + prompt_token_count: 1, + output_token_count: 1, + finish_reason: FinishReason::stop_eos(), + kv_transfer_params: None, + }), + ]); + + let events = tool_event_stream( + events, + Some(Box::new(PartialThenFailParser { + buffered: String::new(), + })), + ) + .collect::>() + .await + .into_iter() + .collect::>>() + .unwrap(); + + assert!(matches!( + &events[0], + AssistantEvent::ToolCallStart { name, .. } if name == "get_weather" + )); + assert!(matches!( + &events[1], + AssistantEvent::ToolCallArgumentsDelta { delta } if delta == r#"{"location":"SF"}"# + )); + assert_eq!( + events[2], + AssistantEvent::TextDelta { + kind: AssistantBlockKind::Text, + delta: " trailing text".to_string(), + } + ); + assert!(matches!(events[3], AssistantEvent::Done { .. })); + } + + #[tokio::test] + async fn real_buffered_parser_error_matches_streaming_and_non_streaming() { + let prefix = "I will check both.\n"; + let first_tool_call = concat!( + "<|DSML|tool_calls>\n", + "<|DSML|invoke name=\"get_weather\">\n", + "<|DSML|parameter name=\"location\" string=\"true\">Tokyo\n", + "", + ); + let malformed_second_tool_call = concat!( + "\n<|DSML|invoke name=\"add\">\n", + "not a parameter\n", + "\n", + "", + ); + let streaming_chunks = vec![ + prefix.to_string(), + first_tool_call.to_string(), + malformed_second_tool_call.to_string(), + ]; + let full_output = streaming_chunks.concat(); + + let streaming = collect_deepseek_v4_message(streaming_chunks).await; + let non_streaming = collect_deepseek_v4_message(vec![full_output]).await; + + let expected = ( + format!("{prefix}{malformed_second_tool_call}"), + vec![( + "get_weather".to_string(), + serde_json::json!({ "location": "Tokyo" }), + )], + ); + assert_eq!(message_tool_projection(&streaming), expected); + assert_eq!(message_tool_projection(&non_streaming), expected); } #[tokio::test] @@ -341,10 +564,15 @@ mod tests { }), ]); - let collected = - tool_event_stream(events, Some(Box::new(FailingParser { fail_next: true }))) - .collect::>() - .await; + let collected = tool_event_stream( + events, + Some(Box::new(FailingParser { + fail_next: true, + buffered: String::new(), + })), + ) + .collect::>() + .await; let events = collected .into_iter() @@ -415,12 +643,18 @@ mod tests { kv_transfer_params: None, }), ]); - let events = tool_event_stream(events, Some(Box::new(FailingParser { fail_next: false }))) - .collect::>() - .await - .into_iter() - .collect::>>() - .unwrap(); + let events = tool_event_stream( + events, + Some(Box::new(FailingParser { + fail_next: false, + buffered: String::new(), + })), + ) + .collect::>() + .await + .into_iter() + .collect::>>() + .unwrap(); assert_eq!( events, @@ -468,7 +702,7 @@ mod tests { ]); let parser = ScriptedParser { - push_results: vec![ToolParseResult { + push_outputs: vec![ToolParserOutput { normal_text: String::new(), calls: vec![ crate::parser::tool::ToolCallDelta { @@ -483,14 +717,14 @@ mod tests { }, ], }], - finish_result: ToolParseResult::default(), + finish_output: ToolParserOutput::default(), }; let err = tool_event_stream(events, Some(Box::new(parser))) .collect::>() .await .into_iter() - .find_map(|result| result.err()) + .find_map(|output| output.err()) .expect("expected invariant error"); assert!(matches!(err, Error::ToolCallStreamInvariant { .. })); @@ -514,8 +748,8 @@ mod tests { ]); let parser = ScriptedParser { - push_results: vec![ - ToolParseResult { + push_outputs: vec![ + ToolParserOutput { normal_text: String::new(), calls: vec![crate::parser::tool::ToolCallDelta { tool_index: 0, @@ -523,11 +757,11 @@ mod tests { arguments: "}".to_string(), }], }, - ToolParseResult { + ToolParserOutput { normal_text: "plain text".to_string(), calls: Vec::new(), }, - ToolParseResult { + ToolParserOutput { normal_text: String::new(), calls: vec![crate::parser::tool::ToolCallDelta { tool_index: 0, @@ -536,14 +770,14 @@ mod tests { }], }, ], - finish_result: ToolParseResult::default(), + finish_output: ToolParserOutput::default(), }; let err = tool_event_stream(events, Some(Box::new(parser))) .collect::>() .await .into_iter() - .find_map(|result| result.err()) + .find_map(|output| output.err()) .expect("expected invariant error"); assert!(matches!( @@ -573,7 +807,7 @@ mod tests { ]); let parser = ScriptedParser { - push_results: vec![ToolParseResult { + push_outputs: vec![ToolParserOutput { normal_text: String::new(), calls: vec![ crate::parser::tool::ToolCallDelta { @@ -588,7 +822,7 @@ mod tests { }, ], }], - finish_result: ToolParseResult::default(), + finish_output: ToolParserOutput::default(), }; let events = tool_event_stream(events, Some(Box::new(parser))) diff --git a/rust/src/chat/src/parser/tool/mod.rs b/rust/src/chat/src/parser/tool/mod.rs index fd1bbedd82..73f52ea01f 100644 --- a/rust/src/chat/src/parser/tool/mod.rs +++ b/rust/src/chat/src/parser/tool/mod.rs @@ -6,7 +6,7 @@ pub use vllm_tool_parser::{ DeepSeekV3ToolParser, DeepSeekV4ToolParser, DeepSeekV31ToolParser, DeepSeekV32ToolParser, Gemma4ToolParser, Glm45MoeToolParser, Glm47MoeToolParser, HermesToolParser, KimiK2ToolParser, Llama3JsonToolParser, MinimaxM2ToolParser, MistralToolParser, Qwen3CoderToolParser, - Qwen3XmlToolParser, ToolCallDelta, ToolParseResult, ToolParser, ToolParserError, + Qwen3XmlToolParser, ToolCallDelta, ToolParser, ToolParserError, ToolParserOutput, }; use crate::parser::ParserFactory; diff --git a/rust/src/chat/src/parser/tool/tests.rs b/rust/src/chat/src/parser/tool/tests.rs index fb2230faeb..ba8fbba25f 100644 --- a/rust/src/chat/src/parser/tool/tests.rs +++ b/rust/src/chat/src/parser/tool/tests.rs @@ -1,6 +1,6 @@ use vllm_tool_parser::Result; -use super::{ToolParseResult, ToolParser, ToolParserFactory, names}; +use super::{ToolParser, ToolParserFactory, ToolParserOutput, names}; use crate::Error; use crate::request::ChatTool; @@ -18,8 +18,16 @@ impl ToolParser for FakeToolParser { true } - fn push(&mut self, _chunk: &str) -> Result { - Ok(ToolParseResult::default()) + fn parse_into(&mut self, _chunk: &str, _output: &mut ToolParserOutput) -> Result<()> { + Ok(()) + } + + fn finish(&mut self) -> Result { + Ok(ToolParserOutput::default()) + } + + fn reset(&mut self) -> String { + String::new() } } diff --git a/rust/src/tool-parser/Cargo.toml b/rust/src/tool-parser/Cargo.toml index e02c397cab..0bc7010b75 100644 --- a/rust/src/tool-parser/Cargo.toml +++ b/rust/src/tool-parser/Cargo.toml @@ -8,6 +8,7 @@ license.workspace = true test-util = [] [dependencies] +easy-ext.workspace = true serde.workspace = true serde_json.workspace = true thiserror.workspace = true diff --git a/rust/src/tool-parser/src/deepseek_dsml/deepseek_v32.rs b/rust/src/tool-parser/src/deepseek_dsml/deepseek_v32.rs index ddc630ceab..1bc487826e 100644 --- a/rust/src/tool-parser/src/deepseek_dsml/deepseek_v32.rs +++ b/rust/src/tool-parser/src/deepseek_dsml/deepseek_v32.rs @@ -1,5 +1,5 @@ use super::{DeepSeekDsmlToolParser, DsmlTokens}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; /// Tool parser for DeepSeek V3.2 models. /// @@ -33,7 +33,6 @@ impl DeepSeekV32ToolParser { } impl ToolParser for DeepSeekV32ToolParser { - /// Create a boxed DeepSeek V3.2 tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -41,20 +40,21 @@ impl ToolParser for DeepSeekV32ToolParser { Ok(Box::new(Self::new(tools))) } - /// Preserve DSML special tokens while decoding. fn preserve_special_tokens(&self) -> bool { true } - /// Push one decoded text chunk through the DSML parser. - fn push(&mut self, chunk: &str) -> Result { - self.0.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.0.parse_into(chunk, output) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.0.finish() } + + fn reset(&mut self) -> String { + self.0.reset() + } } #[cfg(test)] @@ -63,8 +63,8 @@ mod tests { use thiserror_ext::AsReport; use super::DeepSeekV32ToolParser; - use crate::ToolParser; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; + use crate::{ToolParser, ToolParserTestExt as _}; fn build_tool_call(function_name: &str, params: &[(&str, &str)]) -> String { let params = params @@ -84,27 +84,27 @@ mod tests { #[test] fn deepseek_v32_parse_complete_without_tool_call_keeps_text() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn deepseek_v32_parse_complete_extracts_single_tool_call() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "get_weather", &[("location", "SF"), ("date", "2024-01-16")], )) .unwrap(); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF", "date": "2024-01-16" @@ -119,16 +119,16 @@ mod tests { "Thinking... {}", build_tool_call("get_weather", &[("location", "NYC")]) ); - let result = parser.parse_complete(&output).unwrap(); + let output = parser.parse_complete(&output).unwrap(); - assert_eq!(result.normal_text, "Thinking... "); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, "Thinking... "); + assert_eq!(output.calls.len(), 1); } #[test] fn deepseek_v32_parse_complete_converts_schema_types() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete( "<|DSML|function_calls>\n\ <|DSML|invoke name=\"convert\">\n\ @@ -142,9 +142,9 @@ mod tests { ) .unwrap(); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "whole": 5.0, "flag": true, @@ -158,7 +158,7 @@ mod tests { #[test] fn deepseek_v32_parse_complete_string_attr_overrides_schema_types() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete( "<|DSML|function_calls>\n\ <|DSML|invoke name=\"convert\">\n\ @@ -172,9 +172,9 @@ mod tests { ) .unwrap(); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "whole": "5.0", "flag": "true", @@ -188,7 +188,7 @@ mod tests { #[test] fn deepseek_v32_parse_complete_unescapes_literal_closing_tags_in_parameter_value() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "get_weather", &[ @@ -202,7 +202,7 @@ mod tests { .unwrap(); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "Hangzhou ", "date": "2026-05-08", @@ -213,7 +213,7 @@ mod tests { #[test] fn deepseek_v32_streaming_extracts_single_tool_call() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "<|DSML|function_calls>\n", @@ -224,11 +224,11 @@ mod tests { ], ); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF" }) ); } @@ -236,7 +236,7 @@ mod tests { #[test] fn deepseek_v32_streaming_preserves_prefix_text() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "Thinking... ", @@ -248,23 +248,23 @@ mod tests { ], ); - assert_eq!(result.normal_text, "Thinking... "); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, "Thinking... "); + assert_eq!(output.calls.len(), 1); } #[test] fn deepseek_v32_streaming_without_tool_call_emits_text_incrementally() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &["Hello, ", "world!"]); + let output = collect_stream(&mut parser, &["Hello, ", "world!"]); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn deepseek_v32_streaming_extracts_multiple_tool_calls_in_order() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[&format!( "{}\n{}", @@ -274,17 +274,17 @@ mod tests { )], ); - assert_eq!(result.calls.len(), 2); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[1].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[1].tool_index, 1); + assert_eq!(output.calls.len(), 2); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[1].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[1].tool_index, 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF" }) ); assert_eq!( - serde_json::from_str::(&result.calls[1].arguments).unwrap(), + serde_json::from_str::(&output.calls[1].arguments).unwrap(), json!({ "location": "NYC" }) ); } @@ -294,11 +294,11 @@ mod tests { let text = build_tool_call("get_weather", &[("location", "SF")]); let chunks = split_by_chars(&text, 5); let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF" }) ); } @@ -306,7 +306,7 @@ mod tests { #[test] fn deepseek_v32_streaming_handles_bpe_chunked_dsml_opener() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "<|DSML|", @@ -333,11 +333,11 @@ mod tests { ], ); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "Beijing" }) ); } @@ -345,12 +345,12 @@ mod tests { #[test] fn deepseek_v32_streaming_truncated_parameter_does_not_leak_eos() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - parser.push("<|DSML|function_calls>\n").unwrap(); - parser.push("<|DSML|invoke name=\"get_weather\">\n").unwrap(); + parser.parse_chunk("<|DSML|function_calls>\n").unwrap(); + parser.parse_chunk("<|DSML|invoke name=\"get_weather\">\n").unwrap(); parser - .push("<|DSML|parameter name=\"location\" string=\"true\">Tokyo") + .parse_chunk("<|DSML|parameter name=\"location\" string=\"true\">Tokyo") .unwrap(); - parser.push("<|end▁of▁sentence|>").unwrap(); + parser.parse_chunk("<|end▁of▁sentence|>").unwrap(); let error = parser.finish().unwrap_err(); assert!(error.to_report_string().contains("incomplete DeepSeek DSML tool call")); @@ -358,7 +358,7 @@ mod tests { #[test] fn deepseek_v32_streaming_drops_eos_after_complete_tool_calls() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "<|DSML|function_calls>\n", @@ -369,15 +369,15 @@ mod tests { ], ); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); } #[test] fn deepseek_v32_streaming_ignores_text_after_complete_tool_calls() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "<|DSML|function_calls>\n", @@ -389,17 +389,19 @@ mod tests { ], ); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); } #[test] fn deepseek_v32_streaming_does_not_emit_incomplete_invoke() { let mut parser = DeepSeekV32ToolParser::new(&test_tools()); - parser.push("<|DSML|function_calls>\n").unwrap(); - parser.push("<|DSML|invoke name=\"get_weather\">\n").unwrap(); + parser.parse_chunk("<|DSML|function_calls>\n").unwrap(); + parser.parse_chunk("<|DSML|invoke name=\"get_weather\">\n").unwrap(); parser - .push("<|DSML|parameter name=\"location\" string=\"true\">SF\n") + .parse_chunk( + "<|DSML|parameter name=\"location\" string=\"true\">SF\n", + ) .unwrap(); let error = parser.finish().unwrap_err(); diff --git a/rust/src/tool-parser/src/deepseek_dsml/deepseek_v4.rs b/rust/src/tool-parser/src/deepseek_dsml/deepseek_v4.rs index 20fe23b95f..ba01b1b586 100644 --- a/rust/src/tool-parser/src/deepseek_dsml/deepseek_v4.rs +++ b/rust/src/tool-parser/src/deepseek_dsml/deepseek_v4.rs @@ -1,5 +1,5 @@ use super::{DeepSeekDsmlToolParser, DsmlTokens}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; /// Tool parser for DeepSeek V4 models. /// @@ -36,7 +36,6 @@ impl DeepSeekV4ToolParser { } impl ToolParser for DeepSeekV4ToolParser { - /// Create a boxed DeepSeek V4 tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -44,27 +43,29 @@ impl ToolParser for DeepSeekV4ToolParser { Ok(Box::new(Self::new(tools))) } - /// Preserve DSML special tokens while decoding. fn preserve_special_tokens(&self) -> bool { true } - /// Push one decoded text chunk through the DSML parser. - fn push(&mut self, chunk: &str) -> Result { - self.0.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.0.parse_into(chunk, output) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.0.finish() } + + fn reset(&mut self) -> String { + self.0.reset() + } } #[cfg(test)] mod tests { use serde_json::{Value, json}; - use super::{DeepSeekV4ToolParser, ToolParser}; + use super::DeepSeekV4ToolParser; + use crate::ToolParserTestExt as _; use crate::test_utils::{collect_stream, test_tools}; fn build_tool_call(function_name: &str, params: &[(&str, &str)]) -> String { @@ -85,18 +86,18 @@ mod tests { #[test] fn deepseek_v4_parse_complete_reuses_dsml_parser_with_tool_calls_token() { let mut parser = DeepSeekV4ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "get_weather", &[("location", "SF"), ("date", "2024-01-16")], )) .unwrap(); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF", "date": "2024-01-16" @@ -107,7 +108,7 @@ mod tests { #[test] fn deepseek_v4_streaming_handles_tool_calls_token_split_across_chunks() { let mut parser = DeepSeekV4ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "Thinking... ", @@ -122,11 +123,11 @@ mod tests { ], ); - assert_eq!(result.normal_text, "Thinking... "); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.normal_text, "Thinking... "); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "Beijing" }) ); } diff --git a/rust/src/tool-parser/src/deepseek_dsml/mod.rs b/rust/src/tool-parser/src/deepseek_dsml/mod.rs index dda0b2368c..eba36e33db 100644 --- a/rust/src/tool-parser/src/deepseek_dsml/mod.rs +++ b/rust/src/tool-parser/src/deepseek_dsml/mod.rs @@ -6,7 +6,7 @@ use winnow::token::{literal, rest, take_until}; use super::parameters::ToolSchemas; use super::utils::{parse_buffered_event, safe_text_len, xml_unescape}; -use super::{Result, ToolCallDelta, ToolParseResult}; +use super::{Result, ToolCallDelta, ToolParserOutput}; use crate::Tool; mod deepseek_v32; @@ -89,10 +89,10 @@ impl DeepSeekDsmlToolParser { } /// Apply one parsed DSML event to parser state and output. - fn apply_event(&mut self, event: DsmlEvent, result: &mut ToolParseResult) -> Result<()> { + fn apply_event(&mut self, event: DsmlEvent, output: &mut ToolParserOutput) -> Result<()> { match event { DsmlEvent::Text { len: consumed_len } => { - result.normal_text.push_str(&self.buffer[..consumed_len]); + output.normal_text.push_str(&self.buffer[..consumed_len]); } DsmlEvent::ToolCallsStart => self.mode = DsmlMode::ToolBlock, DsmlEvent::Invoke { name, raw_params } => { @@ -112,7 +112,7 @@ impl DeepSeekDsmlToolParser { let arguments = serde_json::to_string(&arguments) .map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index: self.emitted_invoke_count, name: Some(name), arguments, @@ -125,46 +125,41 @@ impl DeepSeekDsmlToolParser { Ok(()) } - /// Reset all streaming state. - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.mode = DsmlMode::Text; self.emitted_invoke_count = 0; + std::mem::take(&mut self.buffer) } - /// Push one decoded text chunk through the DSML parser. - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { // Extract tool calls from streaming model output. // // Uses a buffer-until-complete-invoke strategy: text is buffered until // a complete invoke block is available, then parsed and emitted in one // shot. self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| { parse_next_dsml_event(input, self.mode, self.tokens) })? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); match self.mode { - DsmlMode::Text => result.normal_text.push_str(&self.buffer), + DsmlMode::Text => output.normal_text.push_str(&self.buffer), DsmlMode::Done => {} DsmlMode::ToolBlock => { - self.reset(); return Err(parsing_failed!("incomplete DeepSeek DSML tool call")); } } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) } } diff --git a/rust/src/tool-parser/src/deepseek_json/deepseek_v3.rs b/rust/src/tool-parser/src/deepseek_json/deepseek_v3.rs index 3481951b8c..9c8a2a5c58 100644 --- a/rust/src/tool-parser/src/deepseek_json/deepseek_v3.rs +++ b/rust/src/tool-parser/src/deepseek_json/deepseek_v3.rs @@ -1,5 +1,5 @@ use super::{DeepSeekJsonFormat, DeepSeekJsonToolParser}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; /// Tool parser for DeepSeek V3 JSON-fenced tool calls. /// @@ -25,7 +25,6 @@ impl DeepSeekV3ToolParser { } impl ToolParser for DeepSeekV3ToolParser { - /// Create a boxed DeepSeek V3 tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -33,15 +32,17 @@ impl ToolParser for DeepSeekV3ToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the DeepSeek V3 parser. - fn push(&mut self, chunk: &str) -> Result { - self.0.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.0.parse_into(chunk, output) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.0.finish() } + + fn reset(&mut self) -> String { + self.0.reset() + } } #[cfg(test)] @@ -55,7 +56,7 @@ mod tests { V3_JSON_START, }; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; - use crate::{ToolParseResult, ToolParser}; + use crate::{ToolParser, ToolParserOutput, ToolParserTestExt as _}; fn v3_tool_call(function_name: &str, arguments: &str) -> String { format!( @@ -70,39 +71,39 @@ mod tests { #[test] fn deepseek_v3_parse_complete_without_tool_call_keeps_text() { let mut parser = DeepSeekV3ToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn deepseek_v3_parse_complete_extracts_raw_json_arguments() { let mut parser = DeepSeekV3ToolParser::new(&test_tools()); let arguments = r#"{ "location": "Tokyo", "days": "3" }"#; - let result = parser + let output = parser .parse_complete(&format!( "Let me check.\n{} trailing text", tool_section(&[v3_tool_call("get_weather", arguments)]) )) .unwrap(); - assert_eq!(result.normal_text, "Let me check.\n"); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.normal_text, "Let me check.\n"); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn deepseek_v3_does_not_validate_or_normalize_arguments() { let mut parser = DeepSeekV3ToolParser::new(&test_tools()); let arguments = r#"{"location":"Tokyo",}"#; - let result = parser + let output = parser .parse_complete(&tool_section(&[v3_tool_call("get_weather", arguments)])) .unwrap(); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -122,23 +123,23 @@ mod tests { TOOL_CALLS_END, ]; - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); let mut observed_arguments = Vec::new(); for chunk in chunks { - let next = parser.push(chunk).unwrap(); + let next = parser.parse_chunk(chunk).unwrap(); observed_arguments.extend( next.calls .iter() .filter(|call| call.name.is_none()) .map(|call| call.arguments.clone()), ); - result.append(next); + output.append(next); } - result.append(parser.finish().unwrap()); + output.append(parser.finish().unwrap()); assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]); assert_eq!( - result.coalesce_calls().calls[0].arguments, + output.coalesce_calls().calls[0].arguments, r#"{"location":"Beijing"}"# ); } @@ -152,11 +153,11 @@ mod tests { let chunks = split_by_chars(&input, 5); let mut parser = DeepSeekV3ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, "hello "); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, r#"{"location":"Tokyo"}"#); + assert_eq!(output.normal_text, "hello "); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#); } #[test] @@ -165,10 +166,10 @@ mod tests { let arguments = format!("{{\"text\":\"literal {V3_ARGUMENT_END} inside\"}}"); let input = tool_section(&[v3_tool_call("echo", &arguments)]); - let result = parser.parse_complete(&input).unwrap(); + let output = parser.parse_complete(&input).unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -180,10 +181,10 @@ mod tests { let chunks = split_by_chars(&input, 7); let mut parser = DeepSeekV3ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "", calls: [ ToolCallDelta { @@ -203,14 +204,14 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] fn deepseek_v3_finish_fails_incomplete_tool_call() { let mut parser = DeepSeekV3ToolParser::new(&test_tools()); parser - .push(&format!( + .parse_chunk(&format!( "{TOOL_CALLS_START}{TOOL_CALL_START}function{TOOL_CALL_SEPARATOR}get_weather{V3_JSON_START}{{\"location\"" )) .unwrap(); @@ -228,7 +229,7 @@ mod tests { "{TOOL_CALLS_START}{TOOL_CALL_START}tool{TOOL_CALL_SEPARATOR}get_weather{V3_JSON_START}{{}}" ); - let error = parser.push(&input).unwrap_err(); + let error = parser.parse_chunk(&input).unwrap_err(); expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string()); } diff --git a/rust/src/tool-parser/src/deepseek_json/deepseek_v31.rs b/rust/src/tool-parser/src/deepseek_json/deepseek_v31.rs index bde6bc8fe8..d16ffd7de6 100644 --- a/rust/src/tool-parser/src/deepseek_json/deepseek_v31.rs +++ b/rust/src/tool-parser/src/deepseek_json/deepseek_v31.rs @@ -1,5 +1,5 @@ use super::{DeepSeekJsonFormat, DeepSeekJsonToolParser}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; /// Tool parser for DeepSeek V3.1 raw JSON tool calls. /// @@ -21,7 +21,6 @@ impl DeepSeekV31ToolParser { } impl ToolParser for DeepSeekV31ToolParser { - /// Create a boxed DeepSeek V3.1 tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -29,15 +28,17 @@ impl ToolParser for DeepSeekV31ToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the DeepSeek V3.1 parser. - fn push(&mut self, chunk: &str) -> Result { - self.0.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.0.parse_into(chunk, output) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.0.finish() } + + fn reset(&mut self) -> String { + self.0.reset() + } } #[cfg(test)] @@ -50,7 +51,7 @@ mod tests { TOOL_CALL_END, TOOL_CALL_SEPARATOR, TOOL_CALL_START, TOOL_CALLS_END, TOOL_CALLS_START, }; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; - use crate::{ToolParseResult, ToolParser}; + use crate::{ToolParser, ToolParserOutput, ToolParserTestExt as _}; fn v31_tool_call(function_name: &str, arguments: &str) -> String { format!("{TOOL_CALL_START}{function_name}{TOOL_CALL_SEPARATOR}{arguments}{TOOL_CALL_END}") @@ -63,39 +64,39 @@ mod tests { #[test] fn deepseek_v31_parse_complete_without_tool_call_keeps_text() { let mut parser = DeepSeekV31ToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn deepseek_v31_parse_complete_extracts_raw_json_arguments() { let mut parser = DeepSeekV31ToolParser::new(&test_tools()); let arguments = r#"{ "location": "Tokyo", "days": "3" }"#; - let result = parser + let output = parser .parse_complete(&format!( "Let me check.{} trailing text", tool_section(&[v31_tool_call("get_weather", arguments)]) )) .unwrap(); - assert_eq!(result.normal_text, "Let me check."); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.normal_text, "Let me check."); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn deepseek_v31_does_not_validate_or_normalize_arguments() { let mut parser = DeepSeekV31ToolParser::new(&test_tools()); let arguments = r#"{"location":"Tokyo",}"#; - let result = parser + let output = parser .parse_complete(&tool_section(&[v31_tool_call("get_weather", arguments)])) .unwrap(); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -113,23 +114,23 @@ mod tests { TOOL_CALLS_END, ]; - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); let mut observed_arguments = Vec::new(); for chunk in chunks { - let next = parser.push(chunk).unwrap(); + let next = parser.parse_chunk(chunk).unwrap(); observed_arguments.extend( next.calls .iter() .filter(|call| call.name.is_none()) .map(|call| call.arguments.clone()), ); - result.append(next); + output.append(next); } - result.append(parser.finish().unwrap()); + output.append(parser.finish().unwrap()); assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]); assert_eq!( - result.coalesce_calls().calls[0].arguments, + output.coalesce_calls().calls[0].arguments, r#"{"location":"Beijing"}"# ); } @@ -143,11 +144,11 @@ mod tests { let chunks = split_by_chars(&input, 5); let mut parser = DeepSeekV31ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, "hello "); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, r#"{"location":"Tokyo"}"#); + assert_eq!(output.normal_text, "hello "); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#); } #[test] @@ -156,10 +157,10 @@ mod tests { let arguments = format!(r#"{{"text":"literal {TOOL_CALL_END} inside"}}"#); let input = tool_section(&[v31_tool_call("echo", &arguments)]); - let result = parser.parse_complete(&input).unwrap(); + let output = parser.parse_complete(&input).unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -171,10 +172,10 @@ mod tests { let chunks = split_by_chars(&input, 7); let mut parser = DeepSeekV31ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "", calls: [ ToolCallDelta { @@ -194,7 +195,7 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] @@ -205,18 +206,18 @@ mod tests { ); let mut parser = DeepSeekV31ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &[&input]); + let output = collect_stream(&mut parser, &[&input]); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, r#"{"location":"Tokyo"}"#); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#); } #[test] fn deepseek_v31_finish_fails_incomplete_tool_call() { let mut parser = DeepSeekV31ToolParser::new(&test_tools()); parser - .push(&format!( + .parse_chunk(&format!( "{TOOL_CALLS_START}{TOOL_CALL_START}get_weather{TOOL_CALL_SEPARATOR}{{\"location\"" )) .unwrap(); @@ -232,7 +233,7 @@ mod tests { let mut parser = DeepSeekV31ToolParser::new(&test_tools()); let input = format!("{TOOL_CALLS_START}{TOOL_CALL_START}{TOOL_CALL_SEPARATOR}{{}}"); - let error = parser.push(&input).unwrap_err(); + let error = parser.parse_chunk(&input).unwrap_err(); expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string()); } diff --git a/rust/src/tool-parser/src/deepseek_json/mod.rs b/rust/src/tool-parser/src/deepseek_json/mod.rs index a24d47ae46..0f0d04f042 100644 --- a/rust/src/tool-parser/src/deepseek_json/mod.rs +++ b/rust/src/tool-parser/src/deepseek_json/mod.rs @@ -10,7 +10,7 @@ use winnow::stream::Partial; use winnow::token::{literal, rest, take_until}; use super::utils::{JsonObjectScanState, parse_buffered_event, safe_text_len, take_json_object}; -use super::{Result, ToolCallDelta, ToolParseResult}; +use super::{Result, ToolCallDelta, ToolParserOutput}; pub(super) const TOOL_CALLS_START: &str = "<|tool▁calls▁begin|>"; pub(super) const TOOL_CALLS_END: &str = "<|tool▁calls▁end|>"; @@ -92,11 +92,11 @@ impl DeepSeekJsonToolParser { fn apply_event( &mut self, event: DeepSeekJsonEvent, - result: &mut ToolParseResult, + output: &mut ToolParserOutput, ) -> Result<()> { match event { DeepSeekJsonEvent::Text { len: consumed_len } => { - result.normal_text.push_str(&self.buffer[..consumed_len]); + output.normal_text.push_str(&self.buffer[..consumed_len]); } DeepSeekJsonEvent::ToolCallsStart => self.mode = DeepSeekJsonMode::ToolBlock, DeepSeekJsonEvent::ToolCallStart => self.mode = DeepSeekJsonMode::Header, @@ -107,7 +107,7 @@ impl DeepSeekJsonToolParser { self.mode = DeepSeekJsonMode::Arguments { json_scan: JsonObjectScanState::default(), }; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index, name: Some(function_name), arguments: String::new(), @@ -120,7 +120,7 @@ impl DeepSeekJsonToolParser { self.format.parser_name() )); }; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index, name: None, arguments: self.buffer[..consumed_len].to_string(), @@ -139,26 +139,23 @@ impl DeepSeekJsonToolParser { Ok(()) } - /// Push one decoded text chunk through the DeepSeek JSON parser. - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| { parse_next_deepseek_json_event(input, &mut self.mode, self.format) })? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); match &self.mode { - DeepSeekJsonMode::Text => result.normal_text.push_str(&self.buffer), + DeepSeekJsonMode::Text => output.normal_text.push_str(&self.buffer), DeepSeekJsonMode::ToolBlock | DeepSeekJsonMode::Done => {} DeepSeekJsonMode::Header | DeepSeekJsonMode::Arguments { .. } => { return Err(parsing_failed!( @@ -167,16 +164,15 @@ impl DeepSeekJsonToolParser { )); } } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) } - /// Reset all streaming state. - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.mode = DeepSeekJsonMode::Text; self.active_tool_index = None; self.emitted_tool_count = 0; + std::mem::take(&mut self.buffer) } } diff --git a/rust/src/tool-parser/src/gemma4.rs b/rust/src/tool-parser/src/gemma4.rs index 2eb3608a5b..37625c1114 100644 --- a/rust/src/tool-parser/src/gemma4.rs +++ b/rust/src/tool-parser/src/gemma4.rs @@ -7,7 +7,7 @@ use winnow::stream::Partial; use winnow::token::{literal, take_till, take_until}; use super::utils::{parse_buffered_event, safe_text_len}; -use super::{Result, ToolCallDelta, ToolParseResult, ToolParser}; +use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput}; use crate::Tool; const TOOL_CALL_START: &str = "<|tool_call>"; @@ -51,16 +51,16 @@ impl Gemma4ToolParser { } } - fn apply_event(&mut self, event: Gemma4Event, result: &mut ToolParseResult) -> Result<()> { + fn apply_event(&mut self, event: Gemma4Event, output: &mut ToolParserOutput) -> Result<()> { match event { Gemma4Event::Text { len: consumed_len } => { - result.normal_text.push_str(&self.buffer[..consumed_len]); + output.normal_text.push_str(&self.buffer[..consumed_len]); } Gemma4Event::ToolCall { name, args } => { let arguments = serde_json::to_string(&args) .map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index: self.emitted_tool_count, name: Some(name), arguments, @@ -71,9 +71,9 @@ impl Gemma4ToolParser { Ok(()) } - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.emitted_tool_count = 0; + std::mem::take(&mut self.buffer) } } @@ -89,33 +89,35 @@ impl ToolParser for Gemma4ToolParser { true } - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, parse_next_gemma4_event)? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); if !self.buffer.is_empty() { if self.buffer.starts_with(TOOL_CALL_START) { - self.reset(); return Err(parsing_failed!("incomplete Gemma4 tool call")); } - result.normal_text.push_str(&self.buffer); + output.normal_text.push_str(&self.buffer); } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) + } + + fn reset(&mut self) -> String { + Gemma4ToolParser::reset(self) } } @@ -285,10 +287,10 @@ mod tests { use winnow::stream::Partial; use super::{ - Gemma4ToolParser, ToolCallDelta, ToolParseResult, ToolParser, gemma4_args, + Gemma4ToolParser, ToolCallDelta, ToolParser, ToolParserOutput, gemma4_args, gemma4_array_content, }; - use crate::Tool; + use crate::{Tool, ToolParserTestExt as _}; fn parse_gemma4_args(args: &str) -> super::Result> { let mut input = Partial::new(args); @@ -367,18 +369,18 @@ mod tests { ] } - fn collect_stream(chunks: &[&str]) -> ToolParseResult { + fn collect_stream(chunks: &[&str]) -> ToolParserOutput { let mut parser = Gemma4ToolParser::new(&test_tools()); - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); for chunk in chunks { - result.append(parser.push(chunk).unwrap()); + output.append(parser.parse_chunk(chunk).unwrap()); } - result.append(parser.finish().unwrap()); - result.coalesce_calls() + output.append(parser.finish().unwrap()); + output.coalesce_calls() } - fn first_call(result: &ToolParseResult) -> &ToolCallDelta { - result.calls.first().expect("expected one tool call") + fn first_call(output: &ToolParserOutput) -> &ToolCallDelta { + output.calls.first().expect("expected one tool call") } #[test] @@ -416,15 +418,15 @@ mod tests { #[test] fn gemma4_parse_complete_extracts_single_tool_call() { let mut parser = Gemma4ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete("<|tool_call>call:get_weather{location:<|\"|>London<|\"|>}") .unwrap(); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(first_call(&result).name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(first_call(&output).name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "location": "London" }) ); } @@ -441,7 +443,7 @@ mod tests { #[test] fn gemma4_streaming_basic_single_tool_call() { - let result = collect_stream(&[ + let output = collect_stream(&[ "<|tool_call>", "call:get_weather{", "location:<|\"|>Paris", @@ -450,17 +452,17 @@ mod tests { "", ]); - assert!(result.normal_text.is_empty()); - assert_eq!(first_call(&result).name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(first_call(&output).name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "location": "Paris, France" }) ); } #[test] fn gemma4_streaming_text_before_and_after_tool_call() { - let result = collect_stream(&[ + let output = collect_stream(&[ "Let me check ", "the weather. ", "<|tool_call>", @@ -470,10 +472,10 @@ mod tests { "div>", ]); - assert_eq!(result.normal_text, "Let me check the weather.
"); - assert_eq!(first_call(&result).name.as_deref(), Some("get_weather")); + assert_eq!(output.normal_text, "Let me check the weather.
"); + assert_eq!(first_call(&output).name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "location": "London" }) ); } @@ -481,68 +483,68 @@ mod tests { #[test] fn gemma4_streaming_waits_for_complete_tool_call() { let mut parser = Gemma4ToolParser::new(&test_tools()); - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); for chunk in [ "<|tool_call>", "call:get_weather{", "location:<|\"|>Paris<|\"|>}", ] { - result.append(parser.push(chunk).unwrap()); - assert!(result.calls.is_empty()); + output.append(parser.parse_chunk(chunk).unwrap()); + assert!(output.calls.is_empty()); } - result.append(parser.push("").unwrap()); - let result = result.coalesce_calls(); + output.append(parser.parse_chunk("").unwrap()); + let output = output.coalesce_calls(); - assert_eq!(first_call(&result).name.as_deref(), Some("get_weather")); + assert_eq!(first_call(&output).name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "location": "Paris" }) ); } #[test] fn gemma4_streaming_handles_boolean_split_across_chunks() { - let result = collect_stream(&[ + let output = collect_stream(&[ "<|tool_call>", "call:search{input:{all:tru", "e}}", "", ]); - assert_eq!(first_call(&result).name.as_deref(), Some("search")); + assert_eq!(first_call(&output).name.as_deref(), Some("search")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "input": { "all": true } }) ); } #[test] fn gemma4_streaming_handles_false_split_across_chunks() { - let result = collect_stream(&["<|tool_call>", "call:set{flag:fals", "e}", ""]); + let output = collect_stream(&["<|tool_call>", "call:set{flag:fals", "e}", ""]); - assert_eq!(first_call(&result).name.as_deref(), Some("set")); + assert_eq!(first_call(&output).name.as_deref(), Some("set")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "flag": false }) ); } #[test] fn gemma4_streaming_handles_number_split_across_chunks() { - let result = collect_stream(&["<|tool_call>", "call:set{count:4", "2}", ""]); + let output = collect_stream(&["<|tool_call>", "call:set{count:4", "2}", ""]); - assert_eq!(first_call(&result).name.as_deref(), Some("set")); + assert_eq!(first_call(&output).name.as_deref(), Some("set")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "count": 42 }) ); } #[test] fn gemma4_streaming_handles_split_string_delimiter() { - let result = collect_stream(&[ + let output = collect_stream(&[ "<|tool_call>", "call:todowrite{", "content:<|\"|>Buy milk<|", @@ -550,17 +552,17 @@ mod tests { "", ]); - assert_eq!(first_call(&result).name.as_deref(), Some("todowrite")); + assert_eq!(first_call(&output).name.as_deref(), Some("todowrite")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "content": "Buy milk" }) ); - assert!(!first_call(&result).arguments.contains("<|")); + assert!(!first_call(&output).arguments.contains("<|")); } #[test] fn gemma4_streaming_handles_end_marker_literal_inside_string() { - let result = collect_stream(&[ + let output = collect_stream(&[ "<|tool_call>", "call:todowrite{", "content:<|\"|>literal } inside", @@ -568,16 +570,16 @@ mod tests { "", ]); - assert_eq!(first_call(&result).name.as_deref(), Some("todowrite")); + assert_eq!(first_call(&output).name.as_deref(), Some("todowrite")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "content": "literal } inside" }) ); } #[test] fn gemma4_streaming_handles_html_argument_without_duplication() { - let result = collect_stream(&[ + let output = collect_stream(&[ "<|tool_call>", "call:write_file{", "path:<|\"|>index.html<|\"|>,", @@ -590,9 +592,9 @@ mod tests { "", ]); - assert_eq!(first_call(&result).name.as_deref(), Some("write_file")); + assert_eq!(first_call(&output).name.as_deref(), Some("write_file")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "path": "index.html", "content": "\n\n\n \n \n", @@ -602,7 +604,7 @@ mod tests { #[test] fn gemma4_streaming_trailing_bare_bool_is_not_duplicated() { - let result = collect_stream(&[ + let output = collect_stream(&[ "<|tool_call>", "call:Edit{", "file_path:<|\"|>src/env.py<|\"|>,", @@ -613,9 +615,9 @@ mod tests { "", ]); - assert_eq!(first_call(&result).name.as_deref(), Some("Edit")); + assert_eq!(first_call(&output).name.as_deref(), Some("Edit")); assert_eq!( - serde_json::from_str::(&first_call(&result).arguments).unwrap(), + serde_json::from_str::(&first_call(&output).arguments).unwrap(), json!({ "file_path": "src/env.py", "old_string": "old_val", @@ -624,7 +626,7 @@ mod tests { }) ); assert_eq!( - first_call(&result).arguments.matches("replace_all").count(), + first_call(&output).arguments.matches("replace_all").count(), 1 ); } @@ -632,18 +634,18 @@ mod tests { #[test] fn gemma4_finish_flushes_partial_start_marker_as_text() { let mut parser = Gemma4ToolParser::new(&test_tools()); - let mut result = parser.push("<").unwrap(); - result.append(parser.finish().unwrap()); + let mut output = parser.parse_chunk("<").unwrap(); + output.append(parser.finish().unwrap()); - assert_eq!(result.normal_text, "<"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "<"); + assert!(output.calls.is_empty()); } #[test] fn gemma4_finish_rejects_complete_args_without_end_marker() { let mut parser = Gemma4ToolParser::new(&test_tools()); for chunk in ["<|tool_call>", "call:get_status{}"] { - parser.push(chunk).unwrap(); + parser.parse_chunk(chunk).unwrap(); } let error = parser.finish().unwrap_err(); diff --git a/rust/src/tool-parser/src/glm_xml/glm45_moe.rs b/rust/src/tool-parser/src/glm_xml/glm45_moe.rs index b145671429..2a2d2e0381 100644 --- a/rust/src/tool-parser/src/glm_xml/glm45_moe.rs +++ b/rust/src/tool-parser/src/glm_xml/glm45_moe.rs @@ -1,5 +1,5 @@ use super::{GlmXmlToolParser, Separator}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; /// Tool parser for GLM-4.5/4.6 MoE XML-style tool calls. /// @@ -23,7 +23,6 @@ impl Glm45MoeToolParser { } impl ToolParser for Glm45MoeToolParser { - /// Create a boxed GLM-4.5/4.6 MoE tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -31,13 +30,15 @@ impl ToolParser for Glm45MoeToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the GLM MoE parser. - fn push(&mut self, chunk: &str) -> Result { - self.0.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.0.parse_into(chunk, output) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.0.finish() } + + fn reset(&mut self) -> String { + self.0.reset() + } } diff --git a/rust/src/tool-parser/src/glm_xml/glm47_moe.rs b/rust/src/tool-parser/src/glm_xml/glm47_moe.rs index 7e7538d7c3..3d1c38d55f 100644 --- a/rust/src/tool-parser/src/glm_xml/glm47_moe.rs +++ b/rust/src/tool-parser/src/glm_xml/glm47_moe.rs @@ -1,5 +1,5 @@ use super::{GlmXmlToolParser, Separator}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; /// Tool parser for GLM-4.7 MoE XML-style tool calls. /// @@ -22,20 +22,25 @@ impl ToolParser for Glm47MoeToolParser { Ok(Box::new(Self::new(tools))) } - fn push(&mut self, chunk: &str) -> Result { - self.0.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.0.parse_into(chunk, output) } - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.0.finish() } + + fn reset(&mut self) -> String { + self.0.reset() + } } #[cfg(test)] mod tests { use serde_json::{Value, json}; - use super::{Glm47MoeToolParser, ToolParser}; + use super::Glm47MoeToolParser; + use crate::ToolParserTestExt as _; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; fn glm47_tool_call(function_name: &str, params: &[(&str, &str)]) -> String { @@ -58,13 +63,13 @@ mod tests { ) ); - let result = parser.parse_complete(&output).unwrap(); + let output = parser.parse_complete(&output).unwrap(); - assert_eq!(result.normal_text, "Let me search for that.\n"); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.normal_text, "Let me search for that.\n"); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({"city": "Beijing", "date": "2024-12-25"}) ); } @@ -79,14 +84,14 @@ mod tests { ); let chunks = split_by_chars(&output, 7); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, ""); - assert_eq!(result.calls.len(), 2); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[1].name.as_deref(), Some("add")); + assert_eq!(output.normal_text, ""); + assert_eq!(output.calls.len(), 2); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[1].name.as_deref(), Some("add")); assert_eq!( - serde_json::from_str::(&result.calls[1].arguments).unwrap(), + serde_json::from_str::(&output.calls[1].arguments).unwrap(), json!({"x": 1, "y": 2}) ); } @@ -94,7 +99,7 @@ mod tests { #[test] fn glm47_parse_complete_converts_schema_types() { let mut parser = Glm47MoeToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&glm47_tool_call( "convert", &[ @@ -108,7 +113,7 @@ mod tests { .unwrap(); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "whole": 42, "flag": true, @@ -123,12 +128,12 @@ mod tests { fn glm47_parse_complete_extracts_zero_argument_call() { let mut parser = Glm47MoeToolParser::new(&test_tools()); - let result = parser.parse_complete("add").unwrap(); + let output = parser.parse_complete("add").unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("add")); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("add")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({}) ); } diff --git a/rust/src/tool-parser/src/glm_xml/mod.rs b/rust/src/tool-parser/src/glm_xml/mod.rs index ccb05e8d85..6d657619ba 100644 --- a/rust/src/tool-parser/src/glm_xml/mod.rs +++ b/rust/src/tool-parser/src/glm_xml/mod.rs @@ -6,7 +6,7 @@ use winnow::token::{literal, rest, take_until, take_while}; use super::parameters::ToolSchemas; use super::utils::{parse_buffered_event, safe_text_len, xml_unescape}; -use super::{Result, ToolCallDelta, ToolParseResult}; +use super::{Result, ToolCallDelta, ToolParserOutput}; use crate::Tool; mod glm45_moe; @@ -76,10 +76,10 @@ impl GlmXmlToolParser { } /// Apply one parsed GLM event to parser state and output. - fn apply_event(&mut self, event: GlmEvent, result: &mut ToolParseResult) -> Result<()> { + fn apply_event(&mut self, event: GlmEvent, output: &mut ToolParserOutput) -> Result<()> { match event { GlmEvent::Text { len: consumed_len } => { - result.normal_text.push_str(&self.buffer[..consumed_len]); + output.normal_text.push_str(&self.buffer[..consumed_len]); } GlmEvent::ToolCallStart => self.mode = GlmMode::ToolCall, GlmEvent::ToolCall { name, raw_params } => { @@ -88,7 +88,7 @@ impl GlmXmlToolParser { let arguments = serde_json::to_string(&arguments) .map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index: self.emitted_tool_count, name: Some(name), arguments, @@ -100,40 +100,36 @@ impl GlmXmlToolParser { Ok(()) } - /// Reset all streaming state. - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.mode = GlmMode::Text; self.emitted_tool_count = 0; + std::mem::take(&mut self.buffer) } - /// Push one decoded text chunk through the GLM MoE parser. - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| { parse_next_glm_event(input, self.mode, self.separator) })? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); if !self.buffer.is_empty() { match self.mode { - GlmMode::Text => result.normal_text.push_str(&self.buffer), + GlmMode::Text => output.normal_text.push_str(&self.buffer), GlmMode::ToolCall => return Err(parsing_failed!("incomplete GLM MoE tool call")), GlmMode::AfterToolCall => {} } } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) } } @@ -256,8 +252,8 @@ mod tests { use thiserror_ext::AsReport; use super::Glm45MoeToolParser; - use crate::ToolParser; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; + use crate::{ToolParser, ToolParserTestExt as _}; fn glm45_tool_call(function_name: &str, params: &[(&str, &str)]) -> String { let params = params @@ -273,10 +269,10 @@ mod tests { #[test] fn glm45_parse_complete_without_tool_call_keeps_text() { let mut parser = Glm45MoeToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] @@ -290,13 +286,13 @@ mod tests { ) ); - let result = parser.parse_complete(&output).unwrap(); + let output = parser.parse_complete(&output).unwrap(); - assert_eq!(result.normal_text, "Let me search for that.\n"); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.normal_text, "Let me search for that.\n"); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({"city": "Beijing", "date": "2024-12-25"}) ); } @@ -311,14 +307,14 @@ mod tests { ); let chunks = split_by_chars(&output, 11); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, ""); - assert_eq!(result.calls.len(), 2); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[1].name.as_deref(), Some("add")); + assert_eq!(output.normal_text, ""); + assert_eq!(output.calls.len(), 2); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[1].name.as_deref(), Some("add")); assert_eq!( - serde_json::from_str::(&result.calls[1].arguments).unwrap(), + serde_json::from_str::(&output.calls[1].arguments).unwrap(), json!({"x": 1, "y": 2}) ); } @@ -326,7 +322,7 @@ mod tests { #[test] fn glm45_parse_complete_unescapes_literal_closing_tags_in_arg_value() { let mut parser = Glm45MoeToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&glm45_tool_call( "get_weather", &[ @@ -337,7 +333,7 @@ mod tests { .unwrap(); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "city": "Paris ", "date": "2026-05-08", @@ -349,17 +345,17 @@ mod tests { fn glm45_streaming_without_tool_call_emits_text_incrementally() { let mut parser = Glm45MoeToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &["hello ", "world"]); + let output = collect_stream(&mut parser, &["hello ", "world"]); - assert_eq!(result.normal_text, "hello world"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "hello world"); + assert!(output.calls.is_empty()); } #[test] fn glm45_streaming_preserves_prefix_text() { let mut parser = Glm45MoeToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "Prefix ", @@ -367,14 +363,14 @@ mod tests { ], ); - assert_eq!(result.normal_text, "Prefix "); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, "Prefix "); + assert_eq!(output.calls.len(), 1); } #[test] fn glm45_streaming_handles_start_token_split_across_chunks() { let mut parser = Glm45MoeToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "hello get_weather\ncity").unwrap(); + let output = parser.parse_chunk("get_weather\ncity").unwrap(); - assert_eq!(result.normal_text, ""); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, ""); + assert!(output.calls.is_empty()); } #[test] fn glm45_finish_fails_incomplete_tool_call() { let mut parser = Glm45MoeToolParser::new(&test_tools()); - parser.push("get_weather\ncity").unwrap(); + parser.parse_chunk("get_weather\ncity").unwrap(); let error = parser.finish().unwrap_err(); assert!(error.as_report().to_string().contains("incomplete GLM MoE tool call")); @@ -412,7 +408,7 @@ mod tests { fn glm45_malformed_tool_call_fails_fast() { let mut parser = Glm45MoeToolParser::new(&test_tools()); - let error = parser.push("get_weathercityParis").unwrap_err(); + let error = parser.parse_chunk("get_weathercityParis").unwrap_err(); assert!(error.as_report().to_string().contains("tool parser parsing failed")); } @@ -421,7 +417,7 @@ mod tests { fn glm45_streaming_ignores_trailing_text_after_tool_calls() { let mut parser = Glm45MoeToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[&format!( "{}<|endoftext|>", @@ -429,7 +425,7 @@ mod tests { )], ); - assert_eq!(result.normal_text, ""); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, ""); + assert_eq!(output.calls.len(), 1); } } diff --git a/rust/src/tool-parser/src/json/hermes.rs b/rust/src/tool-parser/src/json/hermes.rs index d57c7de8ef..c8e6a49a7c 100644 --- a/rust/src/tool-parser/src/json/hermes.rs +++ b/rust/src/tool-parser/src/json/hermes.rs @@ -1,5 +1,5 @@ use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; const HERMES_CONFIG: JsonToolCallConfig = JsonToolCallConfig { parser_name: "Hermes", @@ -38,7 +38,6 @@ impl HermesToolParser { } impl ToolParser for HermesToolParser { - /// Create a boxed Hermes tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -46,15 +45,17 @@ impl ToolParser for HermesToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the Hermes parser. - fn push(&mut self, chunk: &str) -> Result { - self.inner.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.inner.parse_into(chunk, output) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.inner.finish() } + + fn reset(&mut self) -> String { + self.inner.reset() + } } #[cfg(test)] @@ -64,7 +65,7 @@ mod tests { use super::HermesToolParser; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; - use crate::{ToolParseResult, ToolParser}; + use crate::{ToolParser, ToolParserOutput, ToolParserTestExt as _}; fn build_tool_call(function_name: &str, arguments: &str) -> String { format!(r#"{{"name":"{function_name}","arguments":{arguments}}}"#) @@ -73,51 +74,51 @@ mod tests { #[test] fn hermes_parse_complete_without_tool_call_keeps_text() { let mut parser = HermesToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn hermes_parse_complete_extracts_raw_json_arguments() { let mut parser = HermesToolParser::new(&test_tools()); let arguments = r#"{ "location": "Tokyo", "days": "3" }"#; - let result = parser + let output = parser .parse_complete(&format!( "Let me check.\n{}", build_tool_call("get_weather", arguments) )) .unwrap(); - assert_eq!(result.normal_text, "Let me check.\n"); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.normal_text, "Let me check.\n"); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn hermes_accepts_newline_after_tool_call_start() { let mut parser = HermesToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete( r#" {"name":"get_weather","arguments":{}}"#, ) .unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); } #[test] fn hermes_does_not_validate_or_normalize_arguments() { let mut parser = HermesToolParser::new(&test_tools()); let arguments = r#"{"location":"Tokyo",}"#; - let result = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap(); + let output = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap(); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -132,24 +133,24 @@ mod tests { "} suffix", ]; - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); let mut observed_arguments = Vec::new(); for chunk in chunks { - let next = parser.push(chunk).unwrap(); + let next = parser.parse_chunk(chunk).unwrap(); observed_arguments.extend( next.calls .iter() .filter(|call| call.name.is_none()) .map(|call| call.arguments.clone()), ); - result.append(next); + output.append(next); } - result.append(parser.finish().unwrap()); + output.append(parser.finish().unwrap()); assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]); - assert_eq!(result.normal_text, "preface suffix"); + assert_eq!(output.normal_text, "preface suffix"); assert_eq!( - result.coalesce_calls().calls[0].arguments, + output.coalesce_calls().calls[0].arguments, r#"{"location":"Beijing"}"# ); } @@ -163,11 +164,11 @@ mod tests { let chunks = split_by_chars(&input, 5); let mut parser = HermesToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, "hello "); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, r#"{"location":"Tokyo"}"#); + assert_eq!(output.normal_text, "hello "); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#); } #[test] @@ -180,10 +181,10 @@ mod tests { let chunks = split_by_chars(&input, 7); let mut parser = HermesToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "", calls: [ ToolCallDelta { @@ -203,14 +204,14 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] fn hermes_finish_fails_incomplete_tool_call() { let mut parser = HermesToolParser::new(&test_tools()); parser - .push(r#"{"name":"get_weather","arguments":{"location""#) + .parse_chunk(r#"{"name":"get_weather","arguments":{"location""#) .unwrap(); let error = parser.finish().unwrap_err(); diff --git a/rust/src/tool-parser/src/json/llama.rs b/rust/src/tool-parser/src/json/llama.rs index 3e579be72f..5ad6918b61 100644 --- a/rust/src/tool-parser/src/json/llama.rs +++ b/rust/src/tool-parser/src/json/llama.rs @@ -9,7 +9,7 @@ use super::{ argument_delta_event, tool_call_header_event, }; use crate::utils::{JsonObjectScanState, parse_buffered_event}; -use crate::{Result, Tool, ToolCallDelta, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolCallDelta, ToolParser, ToolParserOutput}; #[derive(Debug, Clone, PartialEq, Eq)] enum LlamaJsonMode { @@ -78,7 +78,7 @@ impl Llama3JsonToolParser { } /// Apply one parsed Llama JSON event to parser state and output. - fn apply_event(&mut self, event: LlamaJsonEvent, result: &mut ToolParseResult) -> Result<()> { + fn apply_event(&mut self, event: LlamaJsonEvent, output: &mut ToolParserOutput) -> Result<()> { match event { LlamaJsonEvent::ToolCallHeader { function_name } => { let tool_index = self.emitted_tool_count; @@ -87,7 +87,7 @@ impl Llama3JsonToolParser { self.mode = LlamaJsonMode::Arguments { json_scan: JsonObjectScanState::default(), }; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index, name: Some(function_name), arguments: String::new(), @@ -99,7 +99,7 @@ impl Llama3JsonToolParser { "Llama JSON arguments without an active tool call" )); }; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index, name: None, arguments: self.buffer[..consumed_len].to_string(), @@ -117,17 +117,15 @@ impl Llama3JsonToolParser { Ok(()) } - /// Reset all streaming state. - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.mode = LlamaJsonMode::Start; self.active_tool_index = None; self.emitted_tool_count = 0; + std::mem::take(&mut self.buffer) } } impl ToolParser for Llama3JsonToolParser { - /// Create a boxed Llama JSON tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -135,37 +133,34 @@ impl ToolParser for Llama3JsonToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the Llama JSON parser. - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); if !self.commit_start() { - return Ok(result); + return Ok(()); } if matches!(self.mode, LlamaJsonMode::Passthrough) { - result.normal_text.push_str(&self.buffer); + output.normal_text.push_str(&self.buffer); self.buffer.clear(); - return Ok(result); + return Ok(()); } while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| { parse_next_llama_json_event(input, &mut self.mode) })? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); match &self.mode { LlamaJsonMode::Start | LlamaJsonMode::Passthrough => { - result.normal_text.push_str(&self.buffer); + output.normal_text.push_str(&self.buffer); } LlamaJsonMode::AfterCall if self.buffer.trim().is_empty() => {} LlamaJsonMode::Header | LlamaJsonMode::Arguments { .. } => { @@ -175,8 +170,12 @@ impl ToolParser for Llama3JsonToolParser { return Err(parsing_failed!("invalid Llama JSON")); } } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) + } + + fn reset(&mut self) -> String { + Llama3JsonToolParser::reset(self) } } @@ -254,7 +253,7 @@ mod tests { use super::Llama3JsonToolParser; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; - use crate::{ToolParseResult, ToolParser}; + use crate::{ToolParser, ToolParserOutput, ToolParserTestExt as _}; fn build_tool_call(function_name: &str, parameters: &str) -> String { format!(r#"{{"name":"{function_name}","parameters":{parameters}}}"#) @@ -263,26 +262,28 @@ mod tests { #[test] fn llama_json_parse_complete_without_tool_call_keeps_text() { let mut parser = Llama3JsonToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn llama_json_passthrough_never_reenters_tool_parsing() { let mut parser = Llama3JsonToolParser::new(&test_tools()); - let mut result = parser.push("plain text first ").unwrap(); - result.append( - parser.push(&build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)).unwrap(), + let mut output = parser.parse_chunk("plain text first ").unwrap(); + output.append( + parser + .parse_chunk(&build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)) + .unwrap(), ); - result.append(parser.finish().unwrap()); + output.append(parser.finish().unwrap()); assert_eq!( - result.normal_text, + output.normal_text, r#"plain text first {"name":"get_weather","parameters":{"location":"Tokyo"}}"# ); - assert!(result.calls.is_empty()); + assert!(output.calls.is_empty()); } #[test] @@ -292,10 +293,10 @@ mod tests { "<|python_tag|>{}", build_tool_call("get_weather", r#"{"location":"Tokyo"}"#) ); - let result = parser.parse_complete(&input).unwrap(); + let output = parser.parse_complete(&input).unwrap(); - assert_eq!(result.normal_text, input); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, input); + assert!(output.calls.is_empty()); } #[test] @@ -305,22 +306,22 @@ mod tests { "\n {}", build_tool_call("get_weather", r#"{"location":"Tokyo"}"#) ); - let result = parser.parse_complete(&input).unwrap(); + let output = parser.parse_complete(&input).unwrap(); - assert_eq!(result.normal_text, input); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, input); + assert!(output.calls.is_empty()); } #[test] fn llama_json_extracts_raw_parameters_object() { let mut parser = Llama3JsonToolParser::new(&test_tools()); let arguments = r#"{ "location": "Tokyo", "days": 3 }"#; - let result = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap(); + let output = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -344,10 +345,10 @@ mod tests { build_tool_call("get_weather", r#"{"location":"Shanghai"}"#), build_tool_call("add", r#"{"x":1,"y":2}"#), ); - let result = parser.parse_complete(&input).unwrap(); + let output = parser.parse_complete(&input).unwrap(); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "", calls: [ ToolCallDelta { @@ -367,7 +368,7 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] @@ -380,23 +381,23 @@ mod tests { "}}", ]; - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); let mut observed_arguments = Vec::new(); for chunk in chunks { - let next = parser.push(chunk).unwrap(); + let next = parser.parse_chunk(chunk).unwrap(); observed_arguments.extend( next.calls .iter() .filter(|call| call.name.is_none()) .map(|call| call.arguments.clone()), ); - result.append(next); + output.append(next); } - result.append(parser.finish().unwrap()); + output.append(parser.finish().unwrap()); assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]); assert_eq!( - result.coalesce_calls().calls[0].arguments, + output.coalesce_calls().calls[0].arguments, r#"{"location":"Beijing"}"# ); } @@ -411,16 +412,16 @@ mod tests { let chunks = split_by_chars(&input, 6); let mut parser = Llama3JsonToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, ""); - assert_eq!(result.calls.len(), 2); + assert_eq!(output.normal_text, ""); + assert_eq!(output.calls.len(), 2); assert_eq!( - result.calls[0].arguments, + output.calls[0].arguments, r#"{"location":"Dallas","state":"TX"}"# ); - assert_eq!(result.calls[1].name.as_deref(), Some("add")); - assert_eq!(result.calls[1].arguments, r#"{"x":4,"y":5}"#); + assert_eq!(output.calls[1].name.as_deref(), Some("add")); + assert_eq!(output.calls[1].arguments, r#"{"x":4,"y":5}"#); } #[test] @@ -430,29 +431,29 @@ mod tests { "payload": {"items": [1, {"value": "literal { brace } and \"quote\""}]}, "flag": true }"#; - let result = parser.parse_complete(&build_tool_call("convert", arguments)).unwrap(); + let output = parser.parse_complete(&build_tool_call("convert", arguments)).unwrap(); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn llama_json_keeps_trailing_whitespace_after_tool_call() { let mut parser = Llama3JsonToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&format!( "{}\n\t ", build_tool_call("get_weather", r#"{"location":"Tokyo"}"#) )) .unwrap(); - assert_eq!(result.normal_text, ""); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, ""); + assert_eq!(output.calls.len(), 1); } #[test] fn llama_json_finish_fails_incomplete_tool_call() { let mut parser = Llama3JsonToolParser::new(&test_tools()); - parser.push(r#"{"name":"get_weather","parameters":{"location""#).unwrap(); + parser.parse_chunk(r#"{"name":"get_weather","parameters":{"location""#).unwrap(); let error = parser.finish().unwrap_err(); @@ -463,7 +464,7 @@ mod tests { #[test] fn llama_json_malformed_field_order_fails_fast() { let mut parser = Llama3JsonToolParser::new(&test_tools()); - let error = parser.push(r#"{"parameters":{},"name":"get_weather"}"#).unwrap_err(); + let error = parser.parse_chunk(r#"{"parameters":{},"name":"get_weather"}"#).unwrap_err(); expect![[r#" tool parser parsing failed: invalid Llama JSON @@ -475,7 +476,7 @@ mod tests { fn llama_json_trailing_non_separator_content_errors() { let mut parser = Llama3JsonToolParser::new(&test_tools()); let error = parser - .push(&format!( + .parse_chunk(&format!( "{} trailing", build_tool_call("get_weather", r#"{"location":"Tokyo"}"#) )) diff --git a/rust/src/tool-parser/src/json/mistral.rs b/rust/src/tool-parser/src/json/mistral.rs index ac5dea804c..a8756bb2cf 100644 --- a/rust/src/tool-parser/src/json/mistral.rs +++ b/rust/src/tool-parser/src/json/mistral.rs @@ -1,5 +1,5 @@ use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; const MISTRAL_CONFIG: JsonToolCallConfig = JsonToolCallConfig { parser_name: "Mistral", @@ -35,7 +35,6 @@ impl MistralToolParser { } impl ToolParser for MistralToolParser { - /// Create a boxed Mistral tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -43,15 +42,17 @@ impl ToolParser for MistralToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the Mistral parser. - fn push(&mut self, chunk: &str) -> Result { - self.inner.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.inner.parse_into(chunk, output) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.inner.finish() } + + fn reset(&mut self) -> String { + self.inner.reset() + } } #[cfg(test)] @@ -61,7 +62,7 @@ mod tests { use super::MistralToolParser; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; - use crate::{ToolParseResult, ToolParser}; + use crate::{ToolParser, ToolParserOutput, ToolParserTestExt as _}; fn build_tool_call(function_name: &str, arguments: &str) -> String { format!(r#"{{"name":"{function_name}","arguments":{arguments}}}"#) @@ -74,34 +75,34 @@ mod tests { #[test] fn mistral_parse_complete_without_tool_call_keeps_text() { let mut parser = MistralToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn mistral_parse_complete_extracts_raw_json_arguments() { let mut parser = MistralToolParser::new(&test_tools()); let arguments = r#"{ "location": "Tokyo", "days": "3" }"#; - let result = parser + let output = parser .parse_complete(&format!( "Let me check.\n{}", build_tool_calls(&[build_tool_call("get_weather", arguments)]) )) .unwrap(); - assert_eq!(result.normal_text, "Let me check.\n"); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.normal_text, "Let me check.\n"); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn mistral_parse_complete_extracts_pretty_multiple_tool_calls() { let mut parser = MistralToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete( r#"I'll help. [TOOL_CALLS] [ @@ -113,7 +114,7 @@ mod tests { .unwrap(); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "I'll help.\n", calls: [ ToolCallDelta { @@ -133,21 +134,21 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] fn mistral_does_not_validate_or_normalize_arguments() { let mut parser = MistralToolParser::new(&test_tools()); let arguments = r#"{"location":"Tokyo",}"#; - let result = parser + let output = parser .parse_complete(&build_tool_calls(&[build_tool_call( "get_weather", arguments, )])) .unwrap(); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -162,24 +163,24 @@ mod tests { "}] suffix", ]; - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); let mut observed_arguments = Vec::new(); for chunk in chunks { - let next = parser.push(chunk).unwrap(); + let next = parser.parse_chunk(chunk).unwrap(); observed_arguments.extend( next.calls .iter() .filter(|call| call.name.is_none()) .map(|call| call.arguments.clone()), ); - result.append(next); + output.append(next); } - result.append(parser.finish().unwrap()); + output.append(parser.finish().unwrap()); assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]); - assert_eq!(result.normal_text, "preface suffix"); + assert_eq!(output.normal_text, "preface suffix"); assert_eq!( - result.coalesce_calls().calls[0].arguments, + output.coalesce_calls().calls[0].arguments, r#"{"location":"Beijing"}"# ); } @@ -193,30 +194,30 @@ mod tests { let chunks = split_by_chars(&input, 5); let mut parser = MistralToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, "hello "); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, r#"{"location":"Tokyo"}"#); + assert_eq!(output.normal_text, "hello "); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#); } #[test] fn mistral_keeps_array_bracket_literal_inside_json_string() { let mut parser = MistralToolParser::new(&test_tools()); let arguments = r#"{"text":"Array notation: arr[0] = value[1]"}"#; - let result = parser + let output = parser .parse_complete(&build_tool_calls(&[build_tool_call("echo", arguments)])) .unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn mistral_finish_fails_incomplete_tool_call() { let mut parser = MistralToolParser::new(&test_tools()); parser - .push(r#"[TOOL_CALLS] [{"name":"get_weather","arguments":{"location""#) + .parse_chunk(r#"[TOOL_CALLS] [{"name":"get_weather","arguments":{"location""#) .unwrap(); let error = parser.finish().unwrap_err(); @@ -229,7 +230,7 @@ mod tests { fn mistral_malformed_field_order_fails_fast() { let mut parser = MistralToolParser::new(&test_tools()); let error = parser - .push(r#"[TOOL_CALLS] [{"arguments":{},"name":"get_weather"}]"#) + .parse_chunk(r#"[TOOL_CALLS] [{"arguments":{},"name":"get_weather"}]"#) .unwrap_err(); expect![[r#" diff --git a/rust/src/tool-parser/src/json/mod.rs b/rust/src/tool-parser/src/json/mod.rs index ce96fbbde7..4b589b891d 100644 --- a/rust/src/tool-parser/src/json/mod.rs +++ b/rust/src/tool-parser/src/json/mod.rs @@ -20,7 +20,7 @@ use winnow::token::literal; use super::utils::{ JsonObjectScanState, json_str, parse_buffered_event, safe_text_len, take_json_object, }; -use super::{Result, ToolCallDelta, ToolParseResult}; +use super::{Result, ToolCallDelta, ToolParserOutput}; type JsonToolInput<'i> = Partial<&'i str>; @@ -80,27 +80,24 @@ impl JsonToolCallParser { } } - /// Push one decoded text chunk through the JSON tool-call parser. - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); let config = self.config; while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| { parse_next_json_tool_call_event(input, &mut self.mode, config) })? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); match &self.mode { - JsonToolCallMode::Text => result.normal_text.push_str(&self.buffer), + JsonToolCallMode::Text => output.normal_text.push_str(&self.buffer), JsonToolCallMode::Header | JsonToolCallMode::Arguments { .. } => { return Err(parsing_failed!( "incomplete {} tool call", @@ -108,19 +105,19 @@ impl JsonToolCallParser { )); } } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) } /// Apply one parsed JSON tool-call event to parser state and output. fn apply_event( &mut self, event: JsonToolCallEvent, - result: &mut ToolParseResult, + output: &mut ToolParserOutput, ) -> Result<()> { match event { JsonToolCallEvent::Text { len: consumed_len } => { - result.normal_text.push_str(&self.buffer[..consumed_len]); + output.normal_text.push_str(&self.buffer[..consumed_len]); } JsonToolCallEvent::ToolCallStart => self.mode = JsonToolCallMode::Header, JsonToolCallEvent::ToolCallHeader { function_name } => { @@ -130,7 +127,7 @@ impl JsonToolCallParser { self.mode = JsonToolCallMode::Arguments { json_scan: JsonObjectScanState::default(), }; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index, name: Some(function_name), arguments: String::new(), @@ -143,7 +140,7 @@ impl JsonToolCallParser { self.config.parser_name )); }; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index, name: None, arguments: self.buffer[..consumed_len].to_string(), @@ -161,12 +158,11 @@ impl JsonToolCallParser { Ok(()) } - /// Reset all streaming state. - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.mode = JsonToolCallMode::Text; self.active_tool_index = None; self.emitted_tool_count = 0; + std::mem::take(&mut self.buffer) } } @@ -336,7 +332,7 @@ mod tests { use expect_test::expect; use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace}; - use crate::ToolParseResult; + use crate::ToolParserOutput; const DELIMITED_CONFIG: JsonToolCallConfig = JsonToolCallConfig { parser_name: "Delimited JSON", @@ -356,13 +352,13 @@ mod tests { format!("{}", tool_calls.join(" <\n")) } - fn collect_chunks(parser: &mut JsonToolCallParser, chunks: &[&str]) -> ToolParseResult { - let mut result = ToolParseResult::default(); + fn collect_chunks(parser: &mut JsonToolCallParser, chunks: &[&str]) -> ToolParserOutput { + let mut output = ToolParserOutput::default(); for chunk in chunks { - result.append(parser.push(chunk).unwrap()); + parser.parse_into(chunk, &mut output).unwrap(); } - result.append(parser.finish().unwrap()); - result.coalesce_calls() + output.append(parser.finish().unwrap()); + output.coalesce_calls() } #[test] @@ -373,10 +369,10 @@ mod tests { ]); let mut parser = JsonToolCallParser::new(DELIMITED_CONFIG); - let result = collect_chunks(&mut parser, &[&input]); + let output = collect_chunks(&mut parser, &[&input]); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "", calls: [ ToolCallDelta { @@ -396,7 +392,7 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] @@ -409,10 +405,10 @@ mod tests { "", ]; - let result = collect_chunks(&mut parser, &chunks); + let output = collect_chunks(&mut parser, &chunks); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "", calls: [ ToolCallDelta { @@ -432,7 +428,7 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] @@ -444,10 +440,10 @@ mod tests { " trailing text", ]; - let result = collect_chunks(&mut parser, &chunks); + let output = collect_chunks(&mut parser, &chunks); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: " trailing text", calls: [ ToolCallDelta { @@ -460,6 +456,6 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } } diff --git a/rust/src/tool-parser/src/json/qwen.rs b/rust/src/tool-parser/src/json/qwen.rs index 34bb190ec0..f58ca6e0fa 100644 --- a/rust/src/tool-parser/src/json/qwen.rs +++ b/rust/src/tool-parser/src/json/qwen.rs @@ -1,5 +1,5 @@ use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace}; -use crate::{Result, Tool, ToolParseResult, ToolParser}; +use crate::{Result, Tool, ToolParser, ToolParserOutput}; const QWEN_XML_CONFIG: JsonToolCallConfig = JsonToolCallConfig { parser_name: "Qwen XML", @@ -40,7 +40,6 @@ impl Qwen3XmlToolParser { } impl ToolParser for Qwen3XmlToolParser { - /// Create a boxed Qwen XML tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -48,15 +47,17 @@ impl ToolParser for Qwen3XmlToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the Qwen XML parser. - fn push(&mut self, chunk: &str) -> Result { - self.inner.push(chunk) + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + self.inner.parse_into(chunk, output) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { + fn finish(&mut self) -> Result { self.inner.finish() } + + fn reset(&mut self) -> String { + self.inner.reset() + } } #[cfg(test)] @@ -66,7 +67,7 @@ mod tests { use super::Qwen3XmlToolParser; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; - use crate::{ToolParseResult, ToolParser}; + use crate::{ToolParser, ToolParserOutput, ToolParserTestExt as _}; fn build_tool_call(function_name: &str, arguments: &str) -> String { format!( @@ -77,37 +78,37 @@ mod tests { #[test] fn qwen_xml_parse_complete_without_tool_call_keeps_text() { let mut parser = Qwen3XmlToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn qwen_xml_parse_complete_extracts_raw_json_arguments() { let mut parser = Qwen3XmlToolParser::new(&test_tools()); let arguments = r#"{ "location": "Tokyo", "days": "3" }"#; - let result = parser + let output = parser .parse_complete(&format!( "Let me check.\n{}", build_tool_call("get_weather", arguments) )) .unwrap(); - assert_eq!(result.normal_text, "Let me check.\n"); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.normal_text, "Let me check.\n"); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn qwen_xml_does_not_validate_or_normalize_arguments() { let mut parser = Qwen3XmlToolParser::new(&test_tools()); let arguments = r#"{"location":"Tokyo",}"#; - let result = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap(); + let output = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap(); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -122,23 +123,23 @@ mod tests { "}\n", ]; - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); let mut observed_arguments = Vec::new(); for chunk in chunks { - let next = parser.push(chunk).unwrap(); + let next = parser.parse_chunk(chunk).unwrap(); observed_arguments.extend( next.calls .iter() .filter(|call| call.name.is_none()) .map(|call| call.arguments.clone()), ); - result.append(next); + output.append(next); } - result.append(parser.finish().unwrap()); + output.append(parser.finish().unwrap()); assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]); assert_eq!( - result.coalesce_calls().calls[0].arguments, + output.coalesce_calls().calls[0].arguments, r#"{"location":"Beijing"}"# ); } @@ -152,27 +153,27 @@ mod tests { let chunks = split_by_chars(&input, 5); let mut parser = Qwen3XmlToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, "hello "); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, r#"{"location":"Tokyo"}"#); + assert_eq!(output.normal_text, "hello "); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#); } #[test] fn qwen_xml_keeps_end_marker_literal_inside_json_string() { let mut parser = Qwen3XmlToolParser::new(&test_tools()); let arguments = r#"{"text":"literal inside"}"#; - let result = parser.parse_complete(&build_tool_call("echo", arguments)).unwrap(); + let output = parser.parse_complete(&build_tool_call("echo", arguments)).unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn qwen_xml_decodes_escaped_function_name() { let mut parser = Qwen3XmlToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete( r#" {"name":"say_\"hi","arguments":{}} @@ -180,7 +181,7 @@ mod tests { ) .unwrap(); - assert_eq!(result.calls[0].name.as_deref(), Some("say_\"hi")); + assert_eq!(output.calls[0].name.as_deref(), Some("say_\"hi")); } #[test] @@ -189,10 +190,10 @@ mod tests { let input = r#"{"name":"get_weather","arguments":{}} "#; - let result = parser.parse_complete(input).unwrap(); + let output = parser.parse_complete(input).unwrap(); - assert_eq!(result.normal_text, input); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, input); + assert!(output.calls.is_empty()); } #[test] @@ -218,10 +219,10 @@ mod tests { let chunks = split_by_chars(&input, 7); let mut parser = Qwen3XmlToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "", calls: [ ToolCallDelta { @@ -241,14 +242,14 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] fn qwen_xml_finish_fails_incomplete_tool_call() { let mut parser = Qwen3XmlToolParser::new(&test_tools()); parser - .push( + .parse_chunk( r#" {"name":"get_weather","arguments":{"location""#, ) @@ -264,7 +265,7 @@ mod tests { fn qwen_xml_malformed_field_order_fails_fast() { let mut parser = Qwen3XmlToolParser::new(&test_tools()); let error = parser - .push( + .parse_chunk( r#" {"arguments":{},"name":"get_weather"} "#, diff --git a/rust/src/tool-parser/src/kimi_k2.rs b/rust/src/tool-parser/src/kimi_k2.rs index 921b6e0c01..e43ff4afc6 100644 --- a/rust/src/tool-parser/src/kimi_k2.rs +++ b/rust/src/tool-parser/src/kimi_k2.rs @@ -5,7 +5,7 @@ use winnow::stream::Partial; use winnow::token::{literal, rest, take_until, take_while}; use super::utils::{JsonObjectScanState, parse_buffered_event, safe_text_len, take_json_object}; -use super::{Result, ToolCallDelta, ToolParseResult, ToolParser}; +use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput}; use crate::Tool; const TOOL_CALLS_START: &str = "<|tool_calls_section_begin|>"; @@ -73,10 +73,10 @@ impl KimiK2ToolParser { } /// Apply one parsed Kimi K2 event to parser state and output. - fn apply_event(&mut self, event: KimiK2Event, result: &mut ToolParseResult) -> Result<()> { + fn apply_event(&mut self, event: KimiK2Event, output: &mut ToolParserOutput) -> Result<()> { match event { KimiK2Event::Text { len: consumed_len } => { - result.normal_text.push_str(&self.buffer[..consumed_len]); + output.normal_text.push_str(&self.buffer[..consumed_len]); } KimiK2Event::ToolCallsStart => self.mode = KimiK2Mode::ToolBlock, KimiK2Event::ToolCallStart => self.mode = KimiK2Mode::Header, @@ -89,7 +89,7 @@ impl KimiK2ToolParser { self.mode = KimiK2Mode::Arguments { json_scan: JsonObjectScanState::default(), }; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index, name: Some(function_name), arguments: String::new(), @@ -101,7 +101,7 @@ impl KimiK2ToolParser { "Kimi K2 arguments without an active tool call" )); }; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index, name: None, arguments: self.buffer[..consumed_len].to_string(), @@ -120,16 +120,14 @@ impl KimiK2ToolParser { Ok(()) } - /// Reset all streaming state. - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.mode = KimiK2Mode::Text; self.active_tool_index = None; + std::mem::take(&mut self.buffer) } } impl ToolParser for KimiK2ToolParser { - /// Create a boxed Kimi K2 tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -137,38 +135,38 @@ impl ToolParser for KimiK2ToolParser { Ok(Box::new(Self::new(tools))) } - /// Preserve Kimi K2 special-token markers while decoding. fn preserve_special_tokens(&self) -> bool { true } - /// Push one decoded text chunk through the Kimi K2 parser. - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| { parse_next_kimi_k2_event(input, &mut self.mode) })? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); match &self.mode { - KimiK2Mode::Text => result.normal_text.push_str(&self.buffer), + KimiK2Mode::Text => output.normal_text.push_str(&self.buffer), KimiK2Mode::ToolBlock | KimiK2Mode::Done => {} KimiK2Mode::Header | KimiK2Mode::Arguments { .. } => { return Err(parsing_failed!("incomplete Kimi K2 tool call")); } } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) + } + + fn reset(&mut self) -> String { + KimiK2ToolParser::reset(self) } } @@ -323,8 +321,8 @@ mod tests { KimiK2ToolParser, TOOL_CALL_ARGUMENT_START, TOOL_CALL_END, TOOL_CALL_START, TOOL_CALLS_END, TOOL_CALLS_START, ToolParser, tool_header, }; - use crate::ToolParseResult; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; + use crate::{ToolParserOutput, ToolParserTestExt as _}; fn build_tool_call(function_name: &str, index: usize, arguments: &str) -> String { format!( @@ -339,35 +337,35 @@ mod tests { #[test] fn kimi_k2_parse_complete_without_tool_call_keeps_text() { let mut parser = KimiK2ToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn kimi_k2_parse_complete_extracts_raw_json_arguments() { let mut parser = KimiK2ToolParser::new(&test_tools()); let arguments = r#"{ "location": "NYC", "days": "3" }"#; - let result = parser + let output = parser .parse_complete(&format!( "Checking. {} trailing text", build_tool_section(&[build_tool_call("get_weather", 0, arguments)]) )) .unwrap(); - assert_eq!(result.normal_text, "Checking. "); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.normal_text, "Checking. "); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].arguments, arguments); } #[test] fn kimi_k2_does_not_validate_or_normalize_arguments() { let mut parser = KimiK2ToolParser::new(&test_tools()); let arguments = r#"{"location":"NYC",}"#; - let result = parser + let output = parser .parse_complete(&build_tool_section(&[build_tool_call( "get_weather", 0, @@ -375,7 +373,7 @@ mod tests { )])) .unwrap(); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -393,23 +391,23 @@ mod tests { TOOL_CALLS_END, ]; - let mut result = ToolParseResult::default(); + let mut output = ToolParserOutput::default(); let mut observed_arguments = Vec::new(); for chunk in chunks { - let next = parser.push(chunk).unwrap(); + let next = parser.parse_chunk(chunk).unwrap(); observed_arguments.extend( next.calls .iter() .filter(|call| call.name.is_none()) .map(|call| call.arguments.clone()), ); - result.append(next); + output.append(next); } - result.append(parser.finish().unwrap()); + output.append(parser.finish().unwrap()); assert_eq!(observed_arguments, ["{\"location\":", "\"Paris\"", "}"]); - let result = result.coalesce_calls(); - assert_eq!(result.calls[0].arguments, r#"{"location":"Paris"}"#); + let output = output.coalesce_calls(); + assert_eq!(output.calls[0].arguments, r#"{"location":"Paris"}"#); } #[test] @@ -427,11 +425,11 @@ mod tests { TOOL_CALLS_END, ]; - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.normal_text, "hello "); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, r#"{"location":"NYC"}"#); + assert_eq!(output.normal_text, "hello "); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, r#"{"location":"NYC"}"#); } #[test] @@ -440,10 +438,10 @@ mod tests { let arguments = format!(r#"{{"text":"literal {TOOL_CALL_END} inside"}}"#); let input = build_tool_section(&[build_tool_call("echo", 0, &arguments)]); - let result = parser.parse_complete(&input).unwrap(); + let output = parser.parse_complete(&input).unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].arguments, arguments); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].arguments, arguments); } #[test] @@ -460,11 +458,11 @@ mod tests { TOOL_CALLS_END, ]; - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - result.calls[0].arguments, + output.calls[0].arguments, r#"{"text":"literal <|tool_call_end|> inside"}"# ); } @@ -478,10 +476,10 @@ mod tests { ]); let chunks = split_by_chars(&input, 7); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); expect![[r#" - ToolParseResult { + ToolParserOutput { normal_text: "", calls: [ ToolCallDelta { @@ -501,7 +499,7 @@ mod tests { ], } "#]] - .assert_debug_eq(&result); + .assert_debug_eq(&output); } #[test] @@ -511,11 +509,11 @@ mod tests { "{TOOL_CALLS_START}{TOOL_CALL_START}api.tools.search:42{TOOL_CALL_ARGUMENT_START}{{}}{TOOL_CALL_END}{TOOL_CALLS_END}" ); - let result = parser.parse_complete(&input).unwrap(); + let output = parser.parse_complete(&input).unwrap(); - assert_eq!(result.calls[0].tool_index, 42); - assert_eq!(result.calls[0].name.as_deref(), Some("search")); - assert_eq!(result.calls[0].arguments, "{}"); + assert_eq!(output.calls[0].tool_index, 42); + assert_eq!(output.calls[0].name.as_deref(), Some("search")); + assert_eq!(output.calls[0].arguments, "{}"); } #[test] @@ -536,7 +534,7 @@ mod tests { fn kimi_k2_finish_fails_incomplete_tool_call() { let mut parser = KimiK2ToolParser::new(&test_tools()); parser - .push(&format!( + .parse_chunk(&format!( "{TOOL_CALLS_START}{TOOL_CALL_START}functions.get_weather:0{TOOL_CALL_ARGUMENT_START}{{\"location\"" )) .unwrap(); @@ -553,7 +551,7 @@ mod tests { let input = format!("{TOOL_CALLS_START}{TOOL_CALL_START}get_weather{TOOL_CALL_ARGUMENT_START}{{}}"); - let error = parser.push(&input).unwrap_err(); + let error = parser.parse_chunk(&input).unwrap_err(); expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string()); } diff --git a/rust/src/tool-parser/src/lib.rs b/rust/src/tool-parser/src/lib.rs index f7d3411f3b..c0ebc40722 100644 --- a/rust/src/tool-parser/src/lib.rs +++ b/rust/src/tool-parser/src/lib.rs @@ -51,20 +51,20 @@ pub struct ToolCallDelta { /// Result of advancing tool parsing with one assistant-text input. #[derive(Debug, Clone, Default, PartialEq, Eq)] -pub struct ToolParseResult { +pub struct ToolParserOutput { /// Plain assistant text that is not part of any tool call. pub normal_text: String, /// Tool-call updates extracted from this input. pub calls: Vec, } -impl ToolParseResult { - /// Append another parser result onto this one. +impl ToolParserOutput { + /// Append another parser output onto this one. /// /// Note that this does not attempt to merge multiple deltas for the same /// tool call into one complete item. Call `coalesce_calls()` after if /// that behavior is desired. - pub(crate) fn append(&mut self, mut other: Self) { + pub fn append(&mut self, mut other: Self) { self.normal_text.push_str(&other.normal_text); self.calls.append(&mut other.calls); } @@ -75,7 +75,7 @@ impl ToolParseResult { /// which delegates through the incremental parser lifecycle and then /// needs to collapse streaming-style argument fragments into one final /// tool call. - pub(crate) fn coalesce_calls(mut self) -> Self { + pub fn coalesce_calls(mut self) -> Self { let mut merged = BTreeMap::::new(); let mut order = Vec::new(); @@ -116,24 +116,55 @@ pub trait ToolParser: Send { false } - /// Feed one decoded text delta into the parser. - fn push(&mut self, chunk: &str) -> Result; + /// Feed one decoded text delta into the parser, appending committed output + /// into `output`. + /// + /// If this returns an error, any output already appended to `output` + /// remains committed parser output. The parser must keep its uncommitted + /// buffer intact so callers may recover it with `reset()`. + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()>; /// Flush any buffered partial state at end of stream. - fn finish(&mut self) -> Result { - Ok(ToolParseResult::default()) + /// + /// This operation is atomic: on error no partial output is returned and the + /// parser's buffered state is left intact. + fn finish(&mut self) -> Result; + + /// Clear parser state and return currently uncommitted buffered text. + /// + /// Callers may use this to recover any text that failed to parse after an error + /// and output it as normal text. + fn reset(&mut self) -> String; +} + +/// Extension methods for easily testing `ToolParser` implementations. +/// +/// These helpers do not handle partial parsing or error recovery, so they are +/// not intended for use in production code paths. +#[cfg(any(test, feature = "test-util"))] +#[easy_ext::ext(ToolParserTestExt)] +impl T { + /// Feed one decoded text delta and return only if the whole chunk parses. + /// + /// If parsing fails, partial committed output is discarded by this helper. + /// Prefer `parse_into` for more fine-grained control in error recovery. + pub fn parse_chunk(&mut self, chunk: &str) -> Result { + let mut output = ToolParserOutput::default(); + self.parse_into(chunk, &mut output)?; + Ok(output) } /// Parse complete tool calls from final output. /// - /// The default implementation reuses the incremental parser lifecycle by - /// feeding the full output through `push()` and then calling `finish()`. - /// This keeps one source of truth for robust parsers whose incremental - /// state machine is equivalent across arbitrary chunking. - fn parse_complete(&mut self, output: &str) -> Result { - let mut result = self.push(output)?; - result.append(self.finish()?); - Ok(result.coalesce_calls()) + /// This default implementation reuses the incremental parser lifecycle by + /// feeding the full output through `parse_chunk()` and then calling `finish()`. + /// + /// If parsing fails, partial committed output is discarded by this helper. + /// Prefer `parse_into` for more fine-grained control in error recovery. + pub fn parse_complete(&mut self, text: &str) -> Result { + let mut output = self.parse_chunk(text)?; + output.append(self.finish()?); + Ok(output.coalesce_calls()) } } diff --git a/rust/src/tool-parser/src/minimax_m2.rs b/rust/src/tool-parser/src/minimax_m2.rs index 87e61e9979..0e5956de9f 100644 --- a/rust/src/tool-parser/src/minimax_m2.rs +++ b/rust/src/tool-parser/src/minimax_m2.rs @@ -6,7 +6,7 @@ use winnow::token::{literal, rest, take_until}; use super::parameters::ToolSchemas; use super::utils::{parse_buffered_event, safe_text_len, xml_unescape}; -use super::{Result, ToolCallDelta, ToolParseResult, ToolParser}; +use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput}; use crate::Tool; const TOOL_CALL_START: &str = ""; @@ -69,10 +69,10 @@ impl MinimaxM2ToolParser { } /// Apply one parsed MiniMax M2 event to parser state and output. - fn apply_event(&mut self, event: MinimaxM2Event, result: &mut ToolParseResult) -> Result<()> { + fn apply_event(&mut self, event: MinimaxM2Event, output: &mut ToolParserOutput) -> Result<()> { match event { MinimaxM2Event::Text { len: consumed_len } => { - result.normal_text.push_str(&self.buffer[..consumed_len]); + output.normal_text.push_str(&self.buffer[..consumed_len]); } MinimaxM2Event::ToolBlockStart => self.mode = MinimaxM2Mode::ToolBlock, MinimaxM2Event::Invoke { name, raw_params } => { @@ -80,7 +80,7 @@ impl MinimaxM2ToolParser { let arguments = serde_json::to_string(&arguments) .map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index: self.emitted_tool_count, name: Some(name), arguments, @@ -93,16 +93,14 @@ impl MinimaxM2ToolParser { Ok(()) } - /// Reset all streaming state. - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.mode = MinimaxM2Mode::Text; self.emitted_tool_count = 0; + std::mem::take(&mut self.buffer) } } impl ToolParser for MinimaxM2ToolParser { - /// Create a boxed MiniMax M2 tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -110,35 +108,36 @@ impl ToolParser for MinimaxM2ToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the MiniMax M2 parser. - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| { parse_next_minimax_m2_event(input, self.mode) })? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); match self.mode { MinimaxM2Mode::Text => { - result.normal_text.push_str(&self.buffer); + output.normal_text.push_str(&self.buffer); } MinimaxM2Mode::ToolBlock => { return Err(parsing_failed!("incomplete MiniMax M2 tool call")); } MinimaxM2Mode::Done => {} } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) + } + + fn reset(&mut self) -> String { + MinimaxM2ToolParser::reset(self) } } @@ -254,6 +253,7 @@ mod tests { use thiserror_ext::AsReport; use super::{MinimaxM2ToolParser, TOOL_CALL_END, TOOL_CALL_START, ToolParser}; + use crate::ToolParserTestExt as _; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; fn build_tool_block(invokes: &[(&str, Vec<(&str, &str)>)]) -> String { @@ -274,27 +274,27 @@ mod tests { #[test] fn minimax_m2_parse_complete_without_tool_call_keeps_text() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn minimax_m2_parse_complete_extracts_single_tool_call() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_block(&[( "get_weather", vec![("city", "Seattle"), ("days", "5")], )])) .unwrap(); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "city": "Seattle", "days": 5 }) ); } @@ -306,31 +306,31 @@ mod tests { "Let me check. {} This trailing text is ignored.", build_tool_block(&[("get_weather", vec![("city", "Seattle")])]) ); - let result = parser.parse_complete(&output).unwrap(); + let output = parser.parse_complete(&output).unwrap(); - assert_eq!(result.normal_text, "Let me check. "); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, "Let me check. "); + assert_eq!(output.calls.len(), 1); } #[test] fn minimax_m2_parse_complete_extracts_multiple_invokes() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_block(&[ ("get_weather", vec![("city", "Seattle")]), ("get_weather", vec![("city", "NYC")]), ])) .unwrap(); - assert_eq!(result.calls.len(), 2); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[1].tool_index, 1); + assert_eq!(output.calls.len(), 2); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[1].tool_index, 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "city": "Seattle" }) ); assert_eq!( - serde_json::from_str::(&result.calls[1].arguments).unwrap(), + serde_json::from_str::(&output.calls[1].arguments).unwrap(), json!({ "city": "NYC" }) ); } @@ -338,7 +338,7 @@ mod tests { #[test] fn minimax_m2_parse_complete_converts_schema_types() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_block(&[( "convert", vec![ @@ -352,7 +352,7 @@ mod tests { .unwrap(); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "whole": 5.0, "flag": true, @@ -366,7 +366,7 @@ mod tests { #[test] fn minimax_m2_parse_complete_unescapes_literal_closing_tags_in_parameter_value() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_block(&[( "get_weather", vec![ @@ -380,7 +380,7 @@ mod tests { .unwrap(); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "city": "Seattle ", "days": 5, @@ -391,7 +391,7 @@ mod tests { #[test] fn minimax_m2_parse_complete_handles_multiline_parameters() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete( "\ \ @@ -404,7 +404,7 @@ mod tests { .unwrap(); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "shape": "\nrectangle\n", "dimensions": { "width": 10, "height": 20 }, @@ -416,7 +416,7 @@ mod tests { #[test] fn minimax_m2_streaming_extracts_single_tool_call() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "", @@ -426,11 +426,11 @@ mod tests { ], ); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "city": "Seattle" }) ); } @@ -438,7 +438,7 @@ mod tests { #[test] fn minimax_m2_streaming_preserves_prefix_text() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "Let me check. ", @@ -448,17 +448,17 @@ mod tests { ], ); - assert_eq!(result.normal_text, "Let me check. "); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, "Let me check. "); + assert_eq!(output.calls.len(), 1); } #[test] fn minimax_m2_streaming_without_tool_call_emits_text_incrementally() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &["Hello, ", "world!"]); + let output = collect_stream(&mut parser, &["Hello, ", "world!"]); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] @@ -466,10 +466,10 @@ mod tests { let text = build_tool_block(&[("get_weather", vec![("city", "Seattle")])]); let chunks = split_by_chars(&text, 3); let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.calls.len(), 1); - assert!(result.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert!(output.normal_text.is_empty()); } #[test] @@ -480,11 +480,11 @@ mod tests { ]); let chunks = split_by_chars(&text, 7); let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.calls.len(), 2); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[1].tool_index, 1); + assert_eq!(output.calls.len(), 2); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[1].tool_index, 1); } #[test] @@ -520,25 +520,26 @@ mod tests { ); let chunks = split_by_chars(&text, 5); let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); } #[test] fn minimax_m2_streaming_does_not_emit_incomplete_tool_call() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let result = parser.push(r#""#).unwrap(); + let output = + parser.parse_chunk(r#""#).unwrap(); - assert!(result.normal_text.is_empty()); - assert!(result.calls.is_empty()); + assert!(output.normal_text.is_empty()); + assert!(output.calls.is_empty()); } #[test] fn minimax_m2_finish_fails_incomplete_tool_call() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - parser.push(r#""#).unwrap(); + parser.parse_chunk(r#""#).unwrap(); assert!(parser.finish().is_err()); } @@ -546,7 +547,7 @@ mod tests { #[test] fn minimax_m2_finish_fails_after_bare_tool_block_start() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - parser.push("").unwrap(); + parser.parse_chunk("").unwrap(); assert!(parser.finish().is_err()); } @@ -554,7 +555,7 @@ mod tests { #[test] fn minimax_m2_malformed_tool_call_fails_fast() { let mut parser = MinimaxM2ToolParser::new(&test_tools()); - let error = parser.push("").unwrap_err(); + let error = parser.parse_chunk("").unwrap_err(); expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string()); } diff --git a/rust/src/tool-parser/src/qwen_coder.rs b/rust/src/tool-parser/src/qwen_coder.rs index 227315f9bd..c8d21957d7 100644 --- a/rust/src/tool-parser/src/qwen_coder.rs +++ b/rust/src/tool-parser/src/qwen_coder.rs @@ -6,7 +6,7 @@ use winnow::token::{literal, take_until}; use super::parameters::ToolSchemas; use super::utils::{parse_buffered_event, safe_text_len, xml_unescape}; -use super::{Result, ToolCallDelta, ToolParseResult, ToolParser}; +use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput}; use crate::Tool; const TOOL_CALL_START: &str = ""; @@ -71,10 +71,10 @@ impl Qwen3CoderToolParser { } /// Apply one parsed Qwen Coder event to parser state and output. - fn apply_event(&mut self, event: QwenCoderEvent, result: &mut ToolParseResult) -> Result<()> { + fn apply_event(&mut self, event: QwenCoderEvent, output: &mut ToolParserOutput) -> Result<()> { match event { QwenCoderEvent::Text { len: consumed_len } => { - result.normal_text.push_str(&self.buffer[..consumed_len]); + output.normal_text.push_str(&self.buffer[..consumed_len]); } QwenCoderEvent::ToolCallStart => self.mode = QwenCoderMode::ToolCall, QwenCoderEvent::ToolCall { name, raw_params } => { @@ -83,7 +83,7 @@ impl Qwen3CoderToolParser { let arguments = serde_json::to_string(&arguments) .map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?; - result.calls.push(ToolCallDelta { + output.calls.push(ToolCallDelta { tool_index: self.emitted_tool_count, name: Some(name), arguments, @@ -94,16 +94,14 @@ impl Qwen3CoderToolParser { Ok(()) } - /// Reset all streaming state. - fn reset(&mut self) { - self.buffer.clear(); + fn reset(&mut self) -> String { self.mode = QwenCoderMode::Text; self.emitted_tool_count = 0; + std::mem::take(&mut self.buffer) } } impl ToolParser for Qwen3CoderToolParser { - /// Create a boxed Qwen Coder tool parser. fn create(tools: &[Tool]) -> Result> where Self: Sized + 'static, @@ -111,32 +109,33 @@ impl ToolParser for Qwen3CoderToolParser { Ok(Box::new(Self::new(tools))) } - /// Push one decoded text chunk through the Qwen Coder parser. - fn push(&mut self, chunk: &str) -> Result { + fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> { self.buffer.push_str(chunk); - let mut result = ToolParseResult::default(); while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| { parse_next_qwen_coder_event(input, self.mode) })? { - self.apply_event(event, &mut result)?; + self.apply_event(event, output)?; self.buffer.drain(..consumed_len); } - Ok(result) + Ok(()) } - /// Flush buffered text and reset parser state. - fn finish(&mut self) -> Result { - let mut result = ToolParseResult::default(); + fn finish(&mut self) -> Result { + let mut output = ToolParserOutput::default(); if !self.buffer.is_empty() { if self.mode == QwenCoderMode::ToolCall || self.buffer.starts_with(TOOL_CALL_START) { return Err(parsing_failed!("incomplete Qwen Coder tool call")); } - result.normal_text.push_str(&self.buffer); + output.normal_text.push_str(&self.buffer); } - self.reset(); - Ok(result) + let _ = self.reset(); + Ok(output) + } + + fn reset(&mut self) -> String { + Qwen3CoderToolParser::reset(self) } } @@ -229,6 +228,7 @@ mod tests { use thiserror_ext::AsReport; use super::{Qwen3CoderToolParser, ToolParser}; + use crate::ToolParserTestExt as _; use crate::test_utils::{collect_stream, split_by_chars, test_tools}; fn build_tool_call(function_name: &str, params: &[(&str, &str)]) -> String { @@ -243,27 +243,27 @@ mod tests { #[test] fn qwen_coder_parse_complete_without_tool_call_keeps_text() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser.parse_complete("Hello, world!").unwrap(); + let output = parser.parse_complete("Hello, world!").unwrap(); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] fn qwen_coder_parse_complete_extracts_single_tool_call() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "get_weather", &[("location", "SF"), ("date", "2026-04-29")], )) .unwrap(); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF", "date": "2026-04-29" @@ -278,16 +278,16 @@ mod tests { "Thinking... {}", build_tool_call("get_weather", &[("location", "NYC")]) ); - let result = parser.parse_complete(&output).unwrap(); + let output = parser.parse_complete(&output).unwrap(); - assert_eq!(result.normal_text, "Thinking... "); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, "Thinking... "); + assert_eq!(output.calls.len(), 1); } #[test] fn qwen_coder_parse_complete_converts_schema_types() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "convert", &[ @@ -300,9 +300,9 @@ mod tests { )) .unwrap(); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "whole": 5.0, "flag": true, @@ -316,12 +316,12 @@ mod tests { #[test] fn qwen_coder_parse_complete_extracts_empty_arguments() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser.parse_complete(&build_tool_call("get_weather", &[])).unwrap(); + let output = parser.parse_complete(&build_tool_call("get_weather", &[])).unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({}) ); } @@ -329,7 +329,7 @@ mod tests { #[test] fn qwen_coder_parse_complete_handles_upstream_multiline_typed_params() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete( "\n\ \n\ @@ -348,10 +348,10 @@ mod tests { ) .unwrap(); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("calculate_area")); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("calculate_area")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "shape": "rectangle", "dimensions": { "width": 10, "height": 20 }, @@ -363,7 +363,7 @@ mod tests { #[test] fn qwen_coder_parse_complete_handles_nested_json_parameter() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "convert", &[( @@ -373,9 +373,9 @@ mod tests { )) .unwrap(); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "payload": { "nested": { @@ -390,7 +390,7 @@ mod tests { #[test] fn qwen_coder_parse_complete_preserves_xml_like_parameter_values() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "process", &[ @@ -403,9 +403,9 @@ mod tests { )) .unwrap(); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "html_content": r#"
Hello
"#, "xml_snippet": r#""#, @@ -416,7 +416,7 @@ mod tests { #[test] fn qwen_coder_parse_complete_unescapes_literal_closing_tags_in_parameter_value() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "get_weather", &[ @@ -429,9 +429,9 @@ mod tests { )) .unwrap(); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "杭州
", "date": "2026-05-08", @@ -442,16 +442,16 @@ mod tests { #[test] fn qwen_coder_parse_complete_does_not_double_encode_anyof_object() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete(&build_tool_call( "update_record", &[("data", r#"{"key":"value","count":42}"#)], )) .unwrap(); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "data": { "key": "value", "count": 42 }, }) @@ -461,7 +461,7 @@ mod tests { #[test] fn qwen_coder_streaming_extracts_single_tool_call() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "\n", @@ -472,11 +472,11 @@ mod tests { ], ); - assert!(result.normal_text.is_empty()); - assert_eq!(result.calls.len(), 1); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); + assert!(output.normal_text.is_empty()); + assert_eq!(output.calls.len(), 1); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF" }) ); } @@ -484,7 +484,7 @@ mod tests { #[test] fn qwen_coder_streaming_preserves_prefix_text() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = collect_stream( + let output = collect_stream( &mut parser, &[ "Thinking... ", @@ -496,17 +496,17 @@ mod tests { ], ); - assert_eq!(result.normal_text, "Thinking... "); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.normal_text, "Thinking... "); + assert_eq!(output.calls.len(), 1); } #[test] fn qwen_coder_streaming_without_tool_call_emits_text_incrementally() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &["Hello, ", "world!"]); + let output = collect_stream(&mut parser, &["Hello, ", "world!"]); - assert_eq!(result.normal_text, "Hello, world!"); - assert!(result.calls.is_empty()); + assert_eq!(output.normal_text, "Hello, world!"); + assert!(output.calls.is_empty()); } #[test] @@ -517,19 +517,19 @@ mod tests { build_tool_call("get_weather", &[("location", "NYC")]) ); let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &[&text]); + let output = collect_stream(&mut parser, &[&text]); - assert_eq!(result.calls.len(), 2); - assert_eq!(result.calls[0].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[1].name.as_deref(), Some("get_weather")); - assert_eq!(result.calls[0].tool_index, 0); - assert_eq!(result.calls[1].tool_index, 1); + assert_eq!(output.calls.len(), 2); + assert_eq!(output.calls[0].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[1].name.as_deref(), Some("get_weather")); + assert_eq!(output.calls[0].tool_index, 0); + assert_eq!(output.calls[1].tool_index, 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF" }) ); assert_eq!( - serde_json::from_str::(&result.calls[1].arguments).unwrap(), + serde_json::from_str::(&output.calls[1].arguments).unwrap(), json!({ "location": "NYC" }) ); } @@ -543,19 +543,19 @@ mod tests { ); let chunks = split_by_chars(&text, 5); let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); assert_eq!( - result.normal_text, + output.normal_text, "I'll check two cities.Between calls.Done." ); - assert_eq!(result.calls.len(), 2); + assert_eq!(output.calls.len(), 2); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "city": "Dallas", "state": "TX" }) ); assert_eq!( - serde_json::from_str::(&result.calls[1].arguments).unwrap(), + serde_json::from_str::(&output.calls[1].arguments).unwrap(), json!({ "city": "Orlando", "state": "FL" }) ); } @@ -565,11 +565,11 @@ mod tests { let text = build_tool_call("get_weather", &[("location", "SF")]); let chunks = split_by_chars(&text, 3); let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = collect_stream(&mut parser, &chunks); + let output = collect_stream(&mut parser, &chunks); - assert_eq!(result.calls.len(), 1); + assert_eq!(output.calls.len(), 1); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "SF" }) ); } @@ -577,19 +577,19 @@ mod tests { #[test] fn qwen_coder_streaming_does_not_emit_incomplete_tool_call() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser - .push("\n\nSF") + let output = parser + .parse_chunk("\n\nSF") .unwrap(); - assert!(result.normal_text.is_empty()); - assert!(result.calls.is_empty()); + assert!(output.normal_text.is_empty()); + assert!(output.calls.is_empty()); } #[test] fn qwen_coder_finish_fails_incomplete_tool_call() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); parser - .push("\n\nSF") + .parse_chunk("\n\nSF") .unwrap(); assert!(parser.finish().is_err()); @@ -598,7 +598,7 @@ mod tests { #[test] fn qwen_coder_malformed_tool_call_fails_fast() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let error = parser.push("\n\n").unwrap_err(); + let error = parser.parse_chunk("\n\n").unwrap_err(); expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string()); } @@ -607,7 +607,7 @@ mod tests { fn qwen_coder_missing_parameter_end_fails_fast_after_function_end() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); let error = parser - .push( + .parse_chunk( "\n\nSF\n", ) .unwrap_err(); @@ -618,14 +618,14 @@ mod tests { #[test] fn qwen_coder_parse_function_body_trims_one_wrapping_newline() { let mut parser = Qwen3CoderToolParser::new(&test_tools()); - let result = parser + let output = parser .parse_complete( "\n\n\nHangzhou\n\n\n", ) .unwrap(); assert_eq!( - serde_json::from_str::(&result.calls[0].arguments).unwrap(), + serde_json::from_str::(&output.calls[0].arguments).unwrap(), json!({ "location": "Hangzhou" }) ); } diff --git a/rust/src/tool-parser/src/test_utils.rs b/rust/src/tool-parser/src/test_utils.rs index 45dda6ddfd..70178756e4 100644 --- a/rust/src/tool-parser/src/test_utils.rs +++ b/rust/src/tool-parser/src/test_utils.rs @@ -1,7 +1,7 @@ use serde_json::json; -use super::{ToolParseResult, ToolParser}; -use crate::Tool; +use super::{ToolParser, ToolParserOutput}; +use crate::{Tool, ToolParserTestExt as _}; /// Build a reusable set of function tools for parser unit tests. pub fn test_tools() -> Vec { @@ -82,13 +82,15 @@ pub fn test_tools() -> Vec { } /// Push chunks through a streaming parser and coalesce its tool-call deltas. -pub fn collect_stream(parser: &mut T, chunks: &[&str]) -> ToolParseResult { - let mut result = ToolParseResult::default(); +/// +/// Panics if there are any parsing errors along the way. +pub fn collect_stream(parser: &mut T, chunks: &[&str]) -> ToolParserOutput { + let mut output = ToolParserOutput::default(); for chunk in chunks { - result.append(parser.push(chunk).unwrap()); + output.append(parser.parse_chunk(chunk).unwrap()); } - result.append(parser.finish().unwrap()); - result.coalesce_calls() + output.append(parser.finish().unwrap()); + output.coalesce_calls() } /// Split text into chunks containing at most `chunk_chars` Unicode scalar diff --git a/rust/src/tool-parser/src/tests.rs b/rust/src/tool-parser/src/tests.rs index 73e3b7bbf3..fb9c8e62bf 100644 --- a/rust/src/tool-parser/src/tests.rs +++ b/rust/src/tool-parser/src/tests.rs @@ -1,4 +1,5 @@ -use super::{Result, Tool, ToolCallDelta, ToolParseResult, ToolParser}; +use super::{Result, Tool, ToolCallDelta, ToolParser, ToolParserOutput}; +use crate::ToolParserTestExt as _; struct DefaultParser; @@ -10,8 +11,16 @@ impl ToolParser for DefaultParser { Ok(Box::new(Self)) } - fn push(&mut self, _chunk: &str) -> Result { - Ok(ToolParseResult::default()) + fn parse_into(&mut self, _chunk: &str, _output: &mut ToolParserOutput) -> Result<()> { + Ok(()) + } + + fn finish(&mut self) -> Result { + Ok(ToolParserOutput::default()) + } + + fn reset(&mut self) -> String { + String::new() } } @@ -23,7 +32,7 @@ fn tool_parser_does_not_preserve_special_tokens_by_default() { } #[test] -fn default_parse_complete_delegates_through_push_and_finish() { +fn default_parse_complete_delegates_through_parse_chunk_and_finish() { struct StreamingParser; impl ToolParser for StreamingParser { @@ -34,31 +43,30 @@ fn default_parse_complete_delegates_through_push_and_finish() { Ok(Box::new(Self)) } - fn push(&mut self, _chunk: &str) -> Result { - Ok(ToolParseResult { - normal_text: "prefix ".to_string(), - calls: vec![ - ToolCallDelta { - tool_index: 0, - name: Some("weather".to_string()), - arguments: "{\"location\":".to_string(), - }, - ToolCallDelta { - tool_index: 0, - name: None, - arguments: "\"Paris\"".to_string(), - }, - ToolCallDelta { - tool_index: 1, - name: Some("time".to_string()), - arguments: "{\"timezone\":".to_string(), - }, - ], - }) + fn parse_into(&mut self, _chunk: &str, output: &mut ToolParserOutput) -> Result<()> { + output.normal_text.push_str("prefix "); + output.calls.extend([ + ToolCallDelta { + tool_index: 0, + name: Some("weather".to_string()), + arguments: "{\"location\":".to_string(), + }, + ToolCallDelta { + tool_index: 0, + name: None, + arguments: "\"Paris\"".to_string(), + }, + ToolCallDelta { + tool_index: 1, + name: Some("time".to_string()), + arguments: "{\"timezone\":".to_string(), + }, + ]); + Ok(()) } - fn finish(&mut self) -> Result { - Ok(ToolParseResult { + fn finish(&mut self) -> Result { + Ok(ToolParserOutput { normal_text: "suffix".to_string(), calls: vec![ ToolCallDelta { @@ -74,13 +82,17 @@ fn default_parse_complete_delegates_through_push_and_finish() { ], }) } + + fn reset(&mut self) -> String { + String::new() + } } let mut parser = StreamingParser; - let result = parser.parse_complete("ignored").unwrap(); - assert_eq!(result.normal_text, "prefix suffix"); + let output = parser.parse_complete("ignored").unwrap(); + assert_eq!(output.normal_text, "prefix suffix"); assert_eq!( - result.calls, + output.calls, vec![ ToolCallDelta { tool_index: 0,