[Rust Frontend][Perf] O(n) argument scan in tool parser (#45826)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
b4092176b9
commit
e1a5fc406b
@@ -10,6 +10,7 @@ use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
const LONG_TOOL_BODY_REPEATS: usize = 8192;
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
concat!(
|
||||
@@ -39,6 +40,17 @@ fn long_normal_text_fixture() -> String {
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn long_tool_call_fixture() -> String {
|
||||
let location = "x".repeat(LONG_TOOL_BODY_REPEATS);
|
||||
format!(
|
||||
"<tool_call>\n\
|
||||
<function=get_weather>\n\
|
||||
<parameter=location>{location}</parameter>\n\
|
||||
</function>\n\
|
||||
</tool_call>"
|
||||
)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
Qwen3CoderToolParser::create(tools).expect("Qwen Coder parser should initialize")
|
||||
}
|
||||
@@ -112,6 +124,7 @@ fn bench_qwen3_coder(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
let long_tool_call = long_tool_call_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
@@ -132,6 +145,16 @@ fn bench_qwen3_coder(c: &mut Criterion) {
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"qwen3_coder/long_tool_call_body",
|
||||
&tools,
|
||||
&long_tool_call,
|
||||
CHUNK_CHARS,
|
||||
"",
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_qwen3_coder);
|
||||
|
||||
@@ -5,7 +5,7 @@ use winnow::stream::Partial;
|
||||
use winnow::token::{literal, rest, take_until};
|
||||
|
||||
use super::parameters::ToolSchemas;
|
||||
use super::utils::{parse_buffered_event, safe_text_len};
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParserOutput};
|
||||
use crate::Tool;
|
||||
|
||||
@@ -39,10 +39,10 @@ impl DsmlTokens {
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum DsmlMode {
|
||||
Text,
|
||||
ToolBlock,
|
||||
ToolBlock { invoke_end_scan: MarkerScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
@@ -94,7 +94,11 @@ impl DeepSeekDsmlToolParser {
|
||||
DsmlEvent::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
DsmlEvent::ToolCallsStart => self.mode = DsmlMode::ToolBlock,
|
||||
DsmlEvent::ToolCallsStart => {
|
||||
self.mode = DsmlMode::ToolBlock {
|
||||
invoke_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
DsmlEvent::Invoke { name, raw_params } => {
|
||||
let mut arguments = serde_json::Map::with_capacity(raw_params.len());
|
||||
for param in raw_params {
|
||||
@@ -140,7 +144,7 @@ impl DeepSeekDsmlToolParser {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_dsml_event(input, self.mode, self.tokens)
|
||||
parse_next_dsml_event(input, &mut self.mode, self.tokens)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
@@ -154,7 +158,7 @@ impl DeepSeekDsmlToolParser {
|
||||
match self.mode {
|
||||
DsmlMode::Text => output.normal_text.push_str(&self.buffer),
|
||||
DsmlMode::Done => {}
|
||||
DsmlMode::ToolBlock => {
|
||||
DsmlMode::ToolBlock { .. } => {
|
||||
return Err(parsing_failed!("incomplete DeepSeek DSML tool call"));
|
||||
}
|
||||
}
|
||||
@@ -166,12 +170,14 @@ impl DeepSeekDsmlToolParser {
|
||||
/// Parse a DSML event for the current parser mode.
|
||||
fn parse_next_dsml_event(
|
||||
input: &mut DsmlInput<'_>,
|
||||
mode: DsmlMode,
|
||||
mode: &mut DsmlMode,
|
||||
tokens: DsmlTokens,
|
||||
) -> ModalResult<DsmlEvent> {
|
||||
match mode {
|
||||
DsmlMode::Text => parse_text_event(input, tokens),
|
||||
DsmlMode::ToolBlock => parse_tool_block_event(input, tokens),
|
||||
DsmlMode::ToolBlock { invoke_end_scan } => {
|
||||
parse_tool_block_event(input, tokens, invoke_end_scan)
|
||||
}
|
||||
DsmlMode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
@@ -186,11 +192,16 @@ fn parse_text_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResul
|
||||
}
|
||||
|
||||
/// Parse a tool-block DSML event.
|
||||
fn parse_tool_block_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResult<DsmlEvent> {
|
||||
fn parse_tool_block_event(
|
||||
input: &mut DsmlInput<'_>,
|
||||
tokens: DsmlTokens,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<DsmlEvent> {
|
||||
ws0.void().parse_next(input)?;
|
||||
alt((invoke_event, |input: &mut DsmlInput<'_>| {
|
||||
tool_calls_end_event(input, tokens)
|
||||
}))
|
||||
alt((
|
||||
|input: &mut DsmlInput<'_>| invoke_event(input, invoke_end_scan),
|
||||
|input: &mut DsmlInput<'_>| tool_calls_end_event(input, tokens),
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
@@ -217,14 +228,17 @@ fn safe_text_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResult
|
||||
}
|
||||
|
||||
/// Parse a DSML invoke block.
|
||||
fn invoke_event(input: &mut DsmlInput<'_>) -> ModalResult<DsmlEvent> {
|
||||
fn invoke_event(
|
||||
input: &mut DsmlInput<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<DsmlEvent> {
|
||||
let (name, body) = seq!(
|
||||
_: literal(INVOKE_START),
|
||||
_: ws1,
|
||||
dsml_name_attr,
|
||||
_: ws0,
|
||||
_: ">",
|
||||
take_until(0.., INVOKE_END),
|
||||
take_until_marker(INVOKE_END, invoke_end_scan),
|
||||
_: literal(INVOKE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
@@ -5,7 +5,7 @@ use winnow::stream::Partial;
|
||||
use winnow::token::{literal, rest, take_until, take_while};
|
||||
|
||||
use super::parameters::ToolSchemas;
|
||||
use super::utils::{parse_buffered_event, safe_text_len};
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParserOutput};
|
||||
use crate::Tool;
|
||||
|
||||
@@ -24,10 +24,10 @@ const ARG_VALUE_END: &str = "</arg_value>";
|
||||
|
||||
type GlmInput<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum GlmMode {
|
||||
Text,
|
||||
ToolCall,
|
||||
ToolCall { tool_call_end_scan: MarkerScanState },
|
||||
AfterToolCall,
|
||||
}
|
||||
|
||||
@@ -81,7 +81,11 @@ impl GlmXmlToolParser {
|
||||
GlmEvent::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
GlmEvent::ToolCallStart => self.mode = GlmMode::ToolCall,
|
||||
GlmEvent::ToolCallStart => {
|
||||
self.mode = GlmMode::ToolCall {
|
||||
tool_call_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
GlmEvent::ToolCall { name, raw_params } => {
|
||||
self.mode = GlmMode::AfterToolCall;
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
@@ -110,7 +114,7 @@ impl GlmXmlToolParser {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_glm_event(input, self.mode, self.separator)
|
||||
parse_next_glm_event(input, &mut self.mode, self.separator)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
@@ -124,7 +128,9 @@ impl GlmXmlToolParser {
|
||||
if !self.buffer.is_empty() {
|
||||
match self.mode {
|
||||
GlmMode::Text => output.normal_text.push_str(&self.buffer),
|
||||
GlmMode::ToolCall => return Err(parsing_failed!("incomplete GLM MoE tool call")),
|
||||
GlmMode::ToolCall { .. } => {
|
||||
return Err(parsing_failed!("incomplete GLM MoE tool call"));
|
||||
}
|
||||
GlmMode::AfterToolCall => {}
|
||||
}
|
||||
}
|
||||
@@ -136,12 +142,14 @@ impl GlmXmlToolParser {
|
||||
/// Parse a GLM event for the current parser mode.
|
||||
fn parse_next_glm_event(
|
||||
input: &mut GlmInput<'_>,
|
||||
mode: GlmMode,
|
||||
mode: &mut GlmMode,
|
||||
separator: Separator,
|
||||
) -> ModalResult<GlmEvent> {
|
||||
match mode {
|
||||
GlmMode::Text => parse_text_event(input),
|
||||
GlmMode::ToolCall => tool_call_event(input, separator),
|
||||
GlmMode::ToolCall { tool_call_end_scan } => {
|
||||
tool_call_event(input, separator, tool_call_end_scan)
|
||||
}
|
||||
GlmMode::AfterToolCall => after_tool_call_event(input),
|
||||
}
|
||||
}
|
||||
@@ -173,9 +181,13 @@ fn ignored_rest_event(input: &mut GlmInput<'_>) -> ModalResult<GlmEvent> {
|
||||
}
|
||||
|
||||
/// Parse a complete GLM tool call.
|
||||
fn tool_call_event(input: &mut GlmInput<'_>, separator: Separator) -> ModalResult<GlmEvent> {
|
||||
fn tool_call_event(
|
||||
input: &mut GlmInput<'_>,
|
||||
separator: Separator,
|
||||
tool_call_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<GlmEvent> {
|
||||
let (body,) = seq!(
|
||||
take_until(0.., TOOL_CALL_END),
|
||||
take_until_marker(TOOL_CALL_END, tool_call_end_scan),
|
||||
_: literal(TOOL_CALL_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
@@ -5,7 +5,7 @@ use winnow::stream::Partial;
|
||||
use winnow::token::{literal, rest, take_until};
|
||||
|
||||
use super::parameters::ToolSchemas;
|
||||
use super::utils::{parse_buffered_event, safe_text_len};
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::Tool;
|
||||
|
||||
@@ -21,10 +21,10 @@ const ARG_VALUE_END: &str = "</arg_value>";
|
||||
|
||||
type HyV3Input<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum HyV3Mode {
|
||||
Text,
|
||||
ToolBlock,
|
||||
ToolBlock { tool_call_end_scan: MarkerScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
@@ -81,7 +81,11 @@ impl HyV3ToolParser {
|
||||
HyV3Event::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
HyV3Event::ToolBlockStart => self.mode = HyV3Mode::ToolBlock,
|
||||
HyV3Event::ToolBlockStart => {
|
||||
self.mode = HyV3Mode::ToolBlock {
|
||||
tool_call_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
HyV3Event::ToolCall { name, raw_params } => {
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
@@ -113,7 +117,7 @@ impl ToolParser for HyV3ToolParser {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_hy_v3_event(input, self.mode)
|
||||
parse_next_hy_v3_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
@@ -126,7 +130,7 @@ impl ToolParser for HyV3ToolParser {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match self.mode {
|
||||
HyV3Mode::Text => output.normal_text.push_str(&self.buffer),
|
||||
HyV3Mode::ToolBlock => return Err(parsing_failed!("incomplete HY3 tool call")),
|
||||
HyV3Mode::ToolBlock { .. } => return Err(parsing_failed!("incomplete HY3 tool call")),
|
||||
HyV3Mode::Done => {}
|
||||
}
|
||||
let _ = self.reset();
|
||||
@@ -141,10 +145,15 @@ impl ToolParser for HyV3ToolParser {
|
||||
}
|
||||
|
||||
/// Parse a HY3 event for the current parser mode.
|
||||
fn parse_next_hy_v3_event(input: &mut HyV3Input<'_>, mode: HyV3Mode) -> ModalResult<HyV3Event> {
|
||||
fn parse_next_hy_v3_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
mode: &mut HyV3Mode,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
match mode {
|
||||
HyV3Mode::Text => parse_text_event(input),
|
||||
HyV3Mode::ToolBlock => parse_tool_block_event(input),
|
||||
HyV3Mode::ToolBlock { tool_call_end_scan } => {
|
||||
parse_tool_block_event(input, tool_call_end_scan)
|
||||
}
|
||||
HyV3Mode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
@@ -165,8 +174,14 @@ fn safe_text_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
}
|
||||
|
||||
/// Parse one event inside a HY3 tool block.
|
||||
fn parse_tool_block_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
alt((tool_block_end_event, tool_call_event)).parse_next(input)
|
||||
fn parse_tool_block_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
tool_call_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
alt((tool_block_end_event, |input: &mut HyV3Input<'_>| {
|
||||
tool_call_event(input, tool_call_end_scan)
|
||||
}))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a HY3 tool-block end marker.
|
||||
@@ -175,13 +190,16 @@ fn tool_block_end_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
}
|
||||
|
||||
/// Parse a complete HY3 tool-call block.
|
||||
fn tool_call_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
fn tool_call_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
tool_call_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
let (name, body) = seq!(
|
||||
_: ws0,
|
||||
_: literal(TOOL_CALL_START),
|
||||
take_until(0.., TOOL_SEP),
|
||||
_: literal(TOOL_SEP),
|
||||
take_until(0.., TOOL_CALL_END),
|
||||
take_until_marker(TOOL_CALL_END, tool_call_end_scan),
|
||||
_: literal(TOOL_CALL_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
@@ -5,7 +5,7 @@ use winnow::stream::Partial;
|
||||
use winnow::token::{literal, rest, take_until};
|
||||
|
||||
use super::parameters::ToolSchemas;
|
||||
use super::utils::{parse_buffered_event, safe_text_len};
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::Tool;
|
||||
|
||||
@@ -18,10 +18,10 @@ const PARAMETER_END: &str = "</parameter>";
|
||||
|
||||
type MinimaxM2Input<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum MinimaxM2Mode {
|
||||
Text,
|
||||
ToolBlock,
|
||||
ToolBlock { invoke_end_scan: MarkerScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
@@ -74,7 +74,11 @@ impl MinimaxM2ToolParser {
|
||||
MinimaxM2Event::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
MinimaxM2Event::ToolBlockStart => self.mode = MinimaxM2Mode::ToolBlock,
|
||||
MinimaxM2Event::ToolBlockStart => {
|
||||
self.mode = MinimaxM2Mode::ToolBlock {
|
||||
invoke_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
MinimaxM2Event::Invoke { name, raw_params } => {
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
@@ -112,7 +116,7 @@ impl ToolParser for MinimaxM2ToolParser {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_minimax_m2_event(input, self.mode)
|
||||
parse_next_minimax_m2_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
@@ -127,7 +131,7 @@ impl ToolParser for MinimaxM2ToolParser {
|
||||
MinimaxM2Mode::Text => {
|
||||
output.normal_text.push_str(&self.buffer);
|
||||
}
|
||||
MinimaxM2Mode::ToolBlock => {
|
||||
MinimaxM2Mode::ToolBlock { .. } => {
|
||||
return Err(parsing_failed!("incomplete MiniMax M2 tool call"));
|
||||
}
|
||||
MinimaxM2Mode::Done => {}
|
||||
@@ -144,11 +148,13 @@ impl ToolParser for MinimaxM2ToolParser {
|
||||
/// Parse a MiniMax M2 event for the current parser mode.
|
||||
fn parse_next_minimax_m2_event(
|
||||
input: &mut MinimaxM2Input<'_>,
|
||||
mode: MinimaxM2Mode,
|
||||
mode: &mut MinimaxM2Mode,
|
||||
) -> ModalResult<MinimaxM2Event> {
|
||||
match mode {
|
||||
MinimaxM2Mode::Text => parse_text_event(input),
|
||||
MinimaxM2Mode::ToolBlock => parse_tool_block_event(input),
|
||||
MinimaxM2Mode::ToolBlock { invoke_end_scan } => {
|
||||
parse_tool_block_event(input, invoke_end_scan)
|
||||
}
|
||||
MinimaxM2Mode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
@@ -169,8 +175,14 @@ fn safe_text_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event
|
||||
}
|
||||
|
||||
/// Parse one event inside a MiniMax M2 tool block.
|
||||
fn parse_tool_block_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event> {
|
||||
alt((tool_block_end_event, invoke_event)).parse_next(input)
|
||||
fn parse_tool_block_event(
|
||||
input: &mut MinimaxM2Input<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<MinimaxM2Event> {
|
||||
alt((tool_block_end_event, |input: &mut MinimaxM2Input<'_>| {
|
||||
invoke_event(input, invoke_end_scan)
|
||||
}))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M2 tool-block end marker.
|
||||
@@ -181,14 +193,17 @@ fn tool_block_end_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2
|
||||
}
|
||||
|
||||
/// Parse a complete MiniMax M2 invoke block.
|
||||
fn invoke_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event> {
|
||||
fn invoke_event(
|
||||
input: &mut MinimaxM2Input<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<MinimaxM2Event> {
|
||||
let (name, body) = seq!(
|
||||
_: ws0,
|
||||
_: literal(INVOKE_START),
|
||||
_: (ws1, literal("name=")),
|
||||
partial_attr_value,
|
||||
_: literal(">"),
|
||||
take_until(0.., INVOKE_END),
|
||||
take_until_marker(INVOKE_END, invoke_end_scan),
|
||||
_: literal(INVOKE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
@@ -6,7 +6,7 @@ use winnow::stream::Partial;
|
||||
use winnow::token::{literal, rest, take_until};
|
||||
|
||||
use super::parameters::{ParamElement, ParamInput, ToolSchemas};
|
||||
use super::utils::{parse_buffered_event, safe_text_len};
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::Tool;
|
||||
|
||||
@@ -21,10 +21,10 @@ const MIXED_TEXT_FIELD: &str = "$text";
|
||||
|
||||
type MinimaxM3Input<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum MinimaxM3Mode {
|
||||
Text,
|
||||
ToolBlock,
|
||||
ToolBlock { invoke_end_scan: MarkerScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
@@ -109,7 +109,11 @@ impl MinimaxM3ToolParser {
|
||||
MinimaxM3Event::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
MinimaxM3Event::ToolBlockStart => self.mode = MinimaxM3Mode::ToolBlock,
|
||||
MinimaxM3Event::ToolBlockStart => {
|
||||
self.mode = MinimaxM3Mode::ToolBlock {
|
||||
invoke_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
MinimaxM3Event::Invoke { name, params } => {
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
@@ -141,7 +145,7 @@ impl ToolParser for MinimaxM3ToolParser {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_minimax_m3_event(input, self.mode)
|
||||
parse_next_minimax_m3_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
@@ -156,7 +160,7 @@ impl ToolParser for MinimaxM3ToolParser {
|
||||
MinimaxM3Mode::Text => {
|
||||
output.normal_text.push_str(&self.buffer);
|
||||
}
|
||||
MinimaxM3Mode::ToolBlock => {
|
||||
MinimaxM3Mode::ToolBlock { .. } => {
|
||||
if !self.buffer.trim_start().is_empty() {
|
||||
return Err(parsing_failed!("incomplete MiniMax M3 tool call"));
|
||||
}
|
||||
@@ -177,11 +181,13 @@ impl ToolParser for MinimaxM3ToolParser {
|
||||
/// Parse a MiniMax M3 event for the current parser mode.
|
||||
fn parse_next_minimax_m3_event(
|
||||
input: &mut MinimaxM3Input<'_>,
|
||||
mode: MinimaxM3Mode,
|
||||
mode: &mut MinimaxM3Mode,
|
||||
) -> ModalResult<MinimaxM3Event> {
|
||||
match mode {
|
||||
MinimaxM3Mode::Text => parse_text_event(input),
|
||||
MinimaxM3Mode::ToolBlock => parse_tool_block_event(input),
|
||||
MinimaxM3Mode::ToolBlock { invoke_end_scan } => {
|
||||
parse_tool_block_event(input, invoke_end_scan)
|
||||
}
|
||||
MinimaxM3Mode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
@@ -202,8 +208,14 @@ fn safe_text_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3Event
|
||||
}
|
||||
|
||||
/// Parse one event inside a MiniMax M3 tool block.
|
||||
fn parse_tool_block_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3Event> {
|
||||
alt((tool_block_end_event, invoke_event)).parse_next(input)
|
||||
fn parse_tool_block_event(
|
||||
input: &mut MinimaxM3Input<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<MinimaxM3Event> {
|
||||
alt((tool_block_end_event, |input: &mut MinimaxM3Input<'_>| {
|
||||
invoke_event(input, invoke_end_scan)
|
||||
}))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M3 tool-block end marker.
|
||||
@@ -214,14 +226,17 @@ fn tool_block_end_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3
|
||||
}
|
||||
|
||||
/// Parse a complete MiniMax M3 invoke block.
|
||||
fn invoke_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3Event> {
|
||||
fn invoke_event(
|
||||
input: &mut MinimaxM3Input<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<MinimaxM3Event> {
|
||||
let (name, body) = seq!(
|
||||
_: ws0,
|
||||
_: literal(INVOKE_START),
|
||||
_: (ws1, literal("name=")),
|
||||
partial_attr_value,
|
||||
_: literal(">"),
|
||||
take_until(0.., INVOKE_END),
|
||||
take_until_marker(INVOKE_END, invoke_end_scan),
|
||||
_: literal(INVOKE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
@@ -5,7 +5,7 @@ use winnow::stream::Partial;
|
||||
use winnow::token::{literal, take_until};
|
||||
|
||||
use super::parameters::ToolSchemas;
|
||||
use super::utils::{parse_buffered_event, safe_text_len};
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::Tool;
|
||||
|
||||
@@ -18,10 +18,10 @@ const PARAMETER_END: &str = "</parameter>";
|
||||
|
||||
type QwenCoderInput<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum QwenCoderMode {
|
||||
Text,
|
||||
ToolCall,
|
||||
ToolCall { end_marker_scan: MarkerScanState },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -76,7 +76,11 @@ impl Qwen3CoderToolParser {
|
||||
QwenCoderEvent::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
QwenCoderEvent::ToolCallStart => self.mode = QwenCoderMode::ToolCall,
|
||||
QwenCoderEvent::ToolCallStart => {
|
||||
self.mode = QwenCoderMode::ToolCall {
|
||||
end_marker_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
QwenCoderEvent::ToolCall { name, raw_params } => {
|
||||
self.mode = QwenCoderMode::Text;
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
@@ -113,7 +117,7 @@ impl ToolParser for Qwen3CoderToolParser {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_qwen_coder_event(input, self.mode)
|
||||
parse_next_qwen_coder_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
@@ -125,7 +129,9 @@ impl ToolParser for Qwen3CoderToolParser {
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
if !self.buffer.is_empty() {
|
||||
if self.mode == QwenCoderMode::ToolCall || self.buffer.starts_with(TOOL_CALL_START) {
|
||||
if matches!(self.mode, QwenCoderMode::ToolCall { .. })
|
||||
|| self.buffer.starts_with(TOOL_CALL_START)
|
||||
{
|
||||
return Err(parsing_failed!("incomplete Qwen Coder tool call"));
|
||||
}
|
||||
output.normal_text.push_str(&self.buffer);
|
||||
@@ -142,11 +148,11 @@ impl ToolParser for Qwen3CoderToolParser {
|
||||
/// Parse a Qwen Coder event for the current parser mode.
|
||||
fn parse_next_qwen_coder_event(
|
||||
input: &mut QwenCoderInput<'_>,
|
||||
mode: QwenCoderMode,
|
||||
mode: &mut QwenCoderMode,
|
||||
) -> ModalResult<QwenCoderEvent> {
|
||||
match mode {
|
||||
QwenCoderMode::Text => parse_text_event(input),
|
||||
QwenCoderMode::ToolCall => tool_call_event(input),
|
||||
QwenCoderMode::ToolCall { end_marker_scan } => tool_call_event(input, end_marker_scan),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,10 +172,13 @@ fn safe_text_event(input: &mut QwenCoderInput<'_>) -> ModalResult<QwenCoderEvent
|
||||
}
|
||||
|
||||
/// Parse a complete Qwen Coder tool call.
|
||||
fn tool_call_event(input: &mut QwenCoderInput<'_>) -> ModalResult<QwenCoderEvent> {
|
||||
fn tool_call_event(
|
||||
input: &mut QwenCoderInput<'_>,
|
||||
end_marker_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<QwenCoderEvent> {
|
||||
let (body,) = seq!(
|
||||
_: ws0,
|
||||
take_until(0.., TOOL_CALL_END),
|
||||
take_until_marker(TOOL_CALL_END, end_marker_scan),
|
||||
_: literal(TOOL_CALL_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
@@ -228,8 +237,8 @@ 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};
|
||||
use crate::{ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
fn build_tool_call(function_name: &str, params: &[(&str, &str)]) -> String {
|
||||
let params = params
|
||||
@@ -574,6 +583,68 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_handles_end_token_split_across_chunks() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_chunk(
|
||||
"<tool_call>\n\
|
||||
<function=get_weather>\n\
|
||||
<parameter=location>SF</parameter>\n\
|
||||
</function>\n\
|
||||
</tool",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert!(output.calls.is_empty());
|
||||
|
||||
let mut output = output;
|
||||
output.append(parser.parse_chunk("_call>").unwrap());
|
||||
output.append(parser.finish().unwrap());
|
||||
let output = output.coalesce_calls();
|
||||
|
||||
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::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_buffers_long_body_until_end_marker() {
|
||||
let long_location = format!("SF-{}", "x".repeat(8192));
|
||||
let text = build_tool_call("get_weather", &[("location", &long_location)]);
|
||||
let split_at = text.len() - "_call>".len();
|
||||
let (body_with_partial_end, end_suffix) = text.split_at(split_at);
|
||||
let chunks = split_by_chars(body_with_partial_end, 31);
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let mut output = ToolParserOutput::default();
|
||||
|
||||
assert_eq!(end_suffix, "_call>");
|
||||
|
||||
for chunk in chunks {
|
||||
let chunk_output = parser.parse_chunk(chunk).unwrap();
|
||||
assert!(chunk_output.normal_text.is_empty());
|
||||
assert!(chunk_output.calls.is_empty());
|
||||
output.append(chunk_output);
|
||||
}
|
||||
|
||||
output.append(parser.parse_chunk(end_suffix).unwrap());
|
||||
output.append(parser.finish().unwrap());
|
||||
let output = output.coalesce_calls();
|
||||
|
||||
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::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": long_location })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_does_not_emit_incomplete_tool_call() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Shared helpers for tool parsers.
|
||||
|
||||
use winnow::Parser;
|
||||
use winnow::error::{ContextError, ErrMode, ModalResult, Needed, StrContext, StrContextValue};
|
||||
use winnow::stream::{Offset, Partial, Stream};
|
||||
|
||||
@@ -66,6 +67,74 @@ pub(super) fn safe_text_len(input: &mut Partial<&str>, marker: &str) -> ModalRes
|
||||
Ok(emit_len)
|
||||
}
|
||||
|
||||
/// Streaming scan state for a buffered marker search [`take_until_marker`],
|
||||
/// so that we don't have to rescan the whole buffered prefix when resuming.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(super) struct MarkerScanState {
|
||||
scan_start: usize,
|
||||
}
|
||||
|
||||
impl MarkerScanState {
|
||||
pub(super) fn reset(&mut self) {
|
||||
self.scan_start = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse text until `marker`, resuming from the last safe scan checkpoint.
|
||||
///
|
||||
/// This is the streaming-buffered variant of `winnow::token::take_until(0..,
|
||||
/// marker)`: it returns the slice before `marker` and leaves `marker` for the
|
||||
/// caller to consume. On incomplete input, it stores the earliest byte offset
|
||||
/// that can still match `marker` and returns `Incomplete` without consuming
|
||||
/// input, so the next parse can avoid rescanning the whole buffered prefix.
|
||||
///
|
||||
/// Use this for outer parser states that keep the full buffered input across
|
||||
/// chunks while waiting for a closing marker. Plain `take_until` is still a
|
||||
/// better fit for one-shot parsers over a complete body, and for `1..` cases
|
||||
/// where an empty slice before the marker should be rejected.
|
||||
pub(super) fn take_until_marker<'i, 'a>(
|
||||
marker: &'a str,
|
||||
state: &'a mut MarkerScanState,
|
||||
) -> impl Parser<Partial<&'i str>, &'i str, ErrMode<ContextError>> + 'a {
|
||||
move |input: &mut Partial<&'i str>| take_until_marker_(input, marker, state)
|
||||
}
|
||||
|
||||
fn take_until_marker_<'i>(
|
||||
input: &mut Partial<&'i str>,
|
||||
marker: &str,
|
||||
state: &mut MarkerScanState,
|
||||
) -> ModalResult<&'i str> {
|
||||
debug_assert!(!marker.is_empty());
|
||||
|
||||
let text = **input;
|
||||
if text.is_empty() {
|
||||
return incomplete();
|
||||
}
|
||||
|
||||
// Normal updates store a char boundary; this keeps stale or misused state from panicking.
|
||||
let scan_start = floor_char_boundary(text, state.scan_start);
|
||||
|
||||
if let Some(offset) = text[scan_start..].find(marker) {
|
||||
let marker_start = scan_start + offset;
|
||||
let body = &text[..marker_start];
|
||||
input.next_slice(marker_start);
|
||||
state.reset();
|
||||
return Ok(body);
|
||||
}
|
||||
|
||||
let keep_len = partial_prefix_len(text, marker);
|
||||
state.scan_start = text.len() - keep_len;
|
||||
incomplete()
|
||||
}
|
||||
|
||||
fn floor_char_boundary(text: &str, index: usize) -> usize {
|
||||
let mut index = index.min(text.len());
|
||||
while !text.is_char_boundary(index) {
|
||||
index -= 1;
|
||||
}
|
||||
index
|
||||
}
|
||||
|
||||
/// Streaming lexical state for a top-level JSON object.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(super) struct JsonObjectScanState {
|
||||
@@ -273,11 +342,13 @@ pub(super) fn incomplete<T>() -> ModalResult<T> {
|
||||
mod tests {
|
||||
|
||||
use expect_test::expect;
|
||||
use winnow::Parser;
|
||||
use winnow::error::ErrMode;
|
||||
use winnow::stream::{Offset, Partial, Stream};
|
||||
|
||||
use super::{
|
||||
JsonObjectScanState, json_str, partial_prefix_len, safe_text_len, take_json_object,
|
||||
JsonObjectScanState, MarkerScanState, json_str, partial_prefix_len, safe_text_len,
|
||||
take_json_object, take_until_marker,
|
||||
};
|
||||
|
||||
#[test]
|
||||
@@ -335,6 +406,103 @@ mod tests {
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_stops_before_marker() {
|
||||
let mut state = MarkerScanState::default();
|
||||
let mut input = Partial::new("body</end>tail");
|
||||
let checkpoint = input.checkpoint();
|
||||
|
||||
let body = take_until_marker("</end>", &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "body");
|
||||
assert_eq!(input.offset_from(&checkpoint), "body".len());
|
||||
assert_eq!(*input, "</end>tail");
|
||||
assert_eq!(state, MarkerScanState::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_resumes_after_split_marker() {
|
||||
let mut state = MarkerScanState::default();
|
||||
let mut input = Partial::new("body</to");
|
||||
|
||||
let error = take_until_marker("</tool_call>", &mut state)
|
||||
.parse_next(&mut input)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
assert_eq!(state.scan_start, "body".len());
|
||||
|
||||
let mut input = Partial::new("body</tool_call>tail");
|
||||
let body = take_until_marker("</tool_call>", &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "body");
|
||||
assert_eq!(*input, "</tool_call>tail");
|
||||
assert_eq!(state, MarkerScanState::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_advances_checkpoint_for_long_prefix() {
|
||||
let mut state = MarkerScanState::default();
|
||||
let text = format!("{}{}", "x".repeat(1024), "</too");
|
||||
let mut input = Partial::new(text.as_str());
|
||||
|
||||
let error = take_until_marker("</tool_call>", &mut state)
|
||||
.parse_next(&mut input)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
assert_eq!(state.scan_start, 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_keeps_unicode_marker_boundaries() {
|
||||
let marker = "<|DSML|function_calls>";
|
||||
let mut state = MarkerScanState::default();
|
||||
let mut input = Partial::new("prefix <|DSML|fun");
|
||||
|
||||
let error = take_until_marker(marker, &mut state).parse_next(&mut input).unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
assert_eq!(state.scan_start, "prefix ".len());
|
||||
assert!("prefix <|DSML|fun".is_char_boundary(state.scan_start));
|
||||
|
||||
let mut input = Partial::new("prefix <|DSML|function_calls>tail");
|
||||
let body = take_until_marker(marker, &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "prefix ");
|
||||
assert_eq!(*input, "<|DSML|function_calls>tail");
|
||||
assert_eq!(state, MarkerScanState::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_floors_stale_checkpoint_to_char_boundary() {
|
||||
let mut state = MarkerScanState { scan_start: 1 };
|
||||
let mut input = Partial::new("é</end>");
|
||||
|
||||
let body = take_until_marker("</end>", &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "é");
|
||||
assert_eq!(*input, "</end>");
|
||||
assert_eq!(state, MarkerScanState::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_handles_overlapping_prefixes() {
|
||||
let mut state = MarkerScanState::default();
|
||||
let mut input = Partial::new("xxaba");
|
||||
|
||||
let error = take_until_marker("ababa", &mut state).parse_next(&mut input).unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
assert_eq!(state.scan_start, 2);
|
||||
|
||||
let mut input = Partial::new("xxababa!");
|
||||
let body = take_until_marker("ababa", &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "xx");
|
||||
assert_eq!(*input, "ababa!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_consumes_simple_object() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
|
||||
Reference in New Issue
Block a user