[Rust Frontend] Move code from vllm-frontend-rs (#43283)
Signed-off-by: Bugen Zhao <i@bugenzhao.com> Signed-off-by: Nick Hill <nickhill123@gmail.com> Signed-off-by: Eric Curtin <eric.curtin@docker.com> Signed-off-by: Dev-X25874 <283057883+Dev-X25874@users.noreply.github.com> Signed-off-by: Will.hou <1205157517@qq.com> Signed-off-by: Will.hou <willamhou@ceresman.com> Co-authored-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Eric Curtin <eric.curtin@docker.com> Co-authored-by: Dev-X25874 <283057883+Dev-X25874@users.noreply.github.com> Co-authored-by: Will.hou <1205157517@qq.com> Co-authored-by: Will.hou <willamhou@ceresman.com> Please see https://github.com/Inferact/vllm-frontend-rs for full original commit history.
This commit is contained in:
co-authored by
Nick Hill
Eric Curtin
Dev-X25874
Will.hou
Will.hou
parent
39d5fa96a7
commit
39910f2b25
@@ -0,0 +1,75 @@
|
||||
[package]
|
||||
name = "vllm-tool-parser"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[features]
|
||||
test-util = []
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
thiserror.workspace = true
|
||||
thiserror-ext.workspace = true
|
||||
winnow.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
criterion.workspace = true
|
||||
expect-test.workspace = true
|
||||
futures.workspace = true
|
||||
openai-protocol.workspace = true
|
||||
tool-parser.workspace = true
|
||||
|
||||
[[bench]]
|
||||
name = "deepseek_v3"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "deepseek_v31"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "deepseek_v32"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "qwen3_coder"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "qwen3_xml"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "llama3_json"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "minimax_m2"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "glm45_moe"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "kimi_k2"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[[bench]]
|
||||
name = "gemma4"
|
||||
harness = false
|
||||
required-features = ["test-util"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,130 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::DeepSeekParser as ExternalDeepSeekParser;
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{DeepSeekV3ToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will check two cities before answering.\n",
|
||||
"<|tool▁calls▁begin|>",
|
||||
"<|tool▁call▁begin|>function<|tool▁sep|>get_weather\n",
|
||||
"```json\n",
|
||||
"{\"location\":\"Hangzhou\",\"days\":3}",
|
||||
"\n```<|tool▁call▁end|>",
|
||||
"<|tool▁call▁begin|>function<|tool▁sep|>get_weather\n",
|
||||
"```json\n",
|
||||
"{\"location\":\"San Francisco\",\"days\":2}",
|
||||
"\n```<|tool▁call▁end|>",
|
||||
"<|tool▁calls▁end|>",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no DeepSeek V3 tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
DeepSeekV3ToolParser::create(tools).expect("DeepSeek V3 parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_native_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
let openai_tools = openai_tools(tools);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("native_reuse_parser", |b| {
|
||||
let mut parser = native_parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("native_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| native_parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.bench_function("external_reuse_parser", |b| {
|
||||
let mut parser = ExternalDeepSeekParser::new();
|
||||
b.iter(|| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("external_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
ExternalDeepSeekParser::new,
|
||||
|mut parser| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_deepseek_v3(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"deepseek_v3/mixed_text_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"deepseek_v3/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_deepseek_v3);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,128 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::DeepSeek31Parser as ExternalDeepSeek31Parser;
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{DeepSeekV31ToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will check two cities before answering.\n",
|
||||
"<|tool▁calls▁begin|>",
|
||||
"<|tool▁call▁begin|>get_weather<|tool▁sep|>",
|
||||
"{\"location\":\"Hangzhou\",\"days\":3}",
|
||||
"<|tool▁call▁end|>",
|
||||
"<|tool▁call▁begin|>get_weather<|tool▁sep|>",
|
||||
"{\"location\":\"San Francisco\",\"days\":2}",
|
||||
"<|tool▁call▁end|>",
|
||||
"<|tool▁calls▁end|>",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no DeepSeek V3.1 tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
DeepSeekV31ToolParser::create(tools).expect("DeepSeek V3.1 parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_native_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
let openai_tools = openai_tools(tools);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("native_reuse_parser", |b| {
|
||||
let mut parser = native_parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("native_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| native_parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.bench_function("external_reuse_parser", |b| {
|
||||
let mut parser = ExternalDeepSeek31Parser::new();
|
||||
b.iter(|| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("external_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
ExternalDeepSeek31Parser::new,
|
||||
|mut parser| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_deepseek_v31(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"deepseek_v31/mixed_text_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"deepseek_v31/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_deepseek_v31);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,113 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{DeepSeekV32ToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::feed_parser;
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will check two cities before answering.\n",
|
||||
"<|DSML|function_calls>\n",
|
||||
"<|DSML|invoke name=\"get_weather\">\n",
|
||||
"<|DSML|parameter name=\"location\" string=\"true\">Hangzhou</|DSML|parameter>\n",
|
||||
"<|DSML|parameter name=\"date\" string=\"true\">2026-04-28</|DSML|parameter>\n",
|
||||
"<|DSML|parameter name=\"unit\" string=\"true\">celsius</|DSML|parameter>\n",
|
||||
"<|DSML|parameter name=\"days\" string=\"false\">3</|DSML|parameter>\n",
|
||||
"</|DSML|invoke>\n",
|
||||
"<|DSML|invoke name=\"get_weather\">\n",
|
||||
"<|DSML|parameter name=\"location\" string=\"true\">San Francisco</|DSML|parameter>\n",
|
||||
"<|DSML|parameter name=\"date\" string=\"true\">2026-04-28</|DSML|parameter>\n",
|
||||
"<|DSML|parameter name=\"unit\" string=\"true\">fahrenheit</|DSML|parameter>\n",
|
||||
"<|DSML|parameter name=\"days\" string=\"false\">2</|DSML|parameter>\n",
|
||||
"</|DSML|invoke>\n",
|
||||
"</|DSML|function_calls>",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no DSML tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
DeepSeekV32ToolParser::create(tools).expect("DeepSeek V3.2 parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("reuse_parser", |b| {
|
||||
let mut parser = parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_deepseek_v32(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"deepseek_v32/mixed_text_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"deepseek_v32/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_deepseek_v32);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,125 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{Gemma4ToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::feed_parser;
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will inspect the data before answering.\n",
|
||||
"<|tool_call>",
|
||||
"call:convert{",
|
||||
"whole:114.514,",
|
||||
"flag:true,",
|
||||
"empty:<|\"|><|\"|>,",
|
||||
"payload:{",
|
||||
"name:<|\"|>demo<|\"|>,",
|
||||
"count:42,",
|
||||
"enabled:false,",
|
||||
"missing:null,",
|
||||
"nested:{level:2,label:<|\"|>deep<|\"|>},",
|
||||
"tags:[<|\"|>red<|\"|>,<|\"|>blue<|\"|>,3,true,null,{kind:<|\"|>leaf<|\"|>}]",
|
||||
"},",
|
||||
"items:[",
|
||||
"<|\"|>alpha<|\"|>,",
|
||||
"{key:<|\"|>value<|\"|>,score:0.75},",
|
||||
"[1,2,3]",
|
||||
"]",
|
||||
"}",
|
||||
"<tool_call|>",
|
||||
"<|tool_call>",
|
||||
"call:update_record{",
|
||||
"data:{id:7,active:true,notes:[<|\"|>keep<|\"|>,<|\"|>review<|\"|>]}",
|
||||
"}",
|
||||
"<tool_call|>",
|
||||
" Finished.",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no Gemma4 tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
Gemma4ToolParser::create(tools).expect("Gemma4 parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("reuse_parser", |b| {
|
||||
let mut parser = parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_gemma4(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"gemma4/mixed_complex_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will inspect the data before answering.\n Finished.",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"gemma4/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_gemma4);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,210 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::Glm4MoeParser as ExternalGlm4MoeParser;
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{Glm45MoeToolParser, Glm47MoeToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const GLM45_PARSER_NAME: &str = "glm45";
|
||||
const GLM47_PARSER_NAME: &str = "glm47";
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn glm45_mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will check two cities before answering.\n",
|
||||
"<tool_call>get_weather\n",
|
||||
"<arg_key>city</arg_key>\n",
|
||||
"<arg_value>Hangzhou</arg_value>\n",
|
||||
"<arg_key>date</arg_key>\n",
|
||||
"<arg_value>2026-05-07</arg_value>\n",
|
||||
"<arg_key>unit</arg_key>\n",
|
||||
"<arg_value>celsius</arg_value>\n",
|
||||
"<arg_key>days</arg_key>\n",
|
||||
"<arg_value>3</arg_value>\n",
|
||||
"</tool_call>\n",
|
||||
"<tool_call>get_weather\n",
|
||||
"<arg_key>city</arg_key>\n",
|
||||
"<arg_value>San Francisco</arg_value>\n",
|
||||
"<arg_key>date</arg_key>\n",
|
||||
"<arg_value>2026-05-07</arg_value>\n",
|
||||
"<arg_key>unit</arg_key>\n",
|
||||
"<arg_value>fahrenheit</arg_value>\n",
|
||||
"<arg_key>days</arg_key>\n",
|
||||
"<arg_value>2</arg_value>\n",
|
||||
"</tool_call>",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn glm47_mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will check two cities before answering.\n",
|
||||
"<tool_call>get_weather",
|
||||
"<arg_key>city</arg_key>",
|
||||
"<arg_value>Hangzhou</arg_value>",
|
||||
"<arg_key>date</arg_key>",
|
||||
"<arg_value>2026-05-07</arg_value>",
|
||||
"<arg_key>unit</arg_key>",
|
||||
"<arg_value>celsius</arg_value>",
|
||||
"<arg_key>days</arg_key>",
|
||||
"<arg_value>3</arg_value>",
|
||||
"</tool_call>",
|
||||
"<tool_call>get_weather",
|
||||
"<arg_key>city</arg_key>",
|
||||
"<arg_value>San Francisco</arg_value>",
|
||||
"<arg_key>date</arg_key>",
|
||||
"<arg_value>2026-05-07</arg_value>",
|
||||
"<arg_key>unit</arg_key>",
|
||||
"<arg_value>fahrenheit</arg_value>",
|
||||
"<arg_key>days</arg_key>",
|
||||
"<arg_value>2</arg_value>",
|
||||
"</tool_call>",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no GLM MoE tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn native_parser(name: &str, tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
match name {
|
||||
GLM45_PARSER_NAME => Glm45MoeToolParser::create(tools),
|
||||
GLM47_PARSER_NAME => Glm47MoeToolParser::create(tools),
|
||||
_ => unreachable!("unexpected GLM parser name"),
|
||||
}
|
||||
.expect("GLM MoE parser should initialize")
|
||||
}
|
||||
|
||||
fn external_parser(name: &str) -> ExternalGlm4MoeParser {
|
||||
match name {
|
||||
GLM45_PARSER_NAME => ExternalGlm4MoeParser::glm45(),
|
||||
GLM47_PARSER_NAME => ExternalGlm4MoeParser::glm47(),
|
||||
_ => unreachable!("unexpected GLM parser name"),
|
||||
}
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
parser_name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_native_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
let openai_tools = openai_tools(tools);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("native_reuse_parser", |b| {
|
||||
let mut parser = native_parser(parser_name, tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("native_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| native_parser(parser_name, tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.bench_function("external_reuse_parser", |b| {
|
||||
let mut parser = external_parser(parser_name);
|
||||
b.iter(|| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("external_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| external_parser(parser_name),
|
||||
|mut parser| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_glm45_moe(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let glm45_mixed_text = glm45_mixed_fixture();
|
||||
let glm47_mixed_text = glm47_mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"glm45/mixed_text_tool_call",
|
||||
GLM45_PARSER_NAME,
|
||||
&tools,
|
||||
&glm45_mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"glm47/mixed_text_tool_call",
|
||||
GLM47_PARSER_NAME,
|
||||
&tools,
|
||||
&glm47_mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"glm45/long_normal_text",
|
||||
GLM45_PARSER_NAME,
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"glm47/long_normal_text",
|
||||
GLM47_PARSER_NAME,
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_glm45_moe);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,149 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::KimiK2Parser as ExternalKimiK2Parser;
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{KimiK2ToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will check two cities before answering.\n",
|
||||
"<|tool_calls_section_begin|>",
|
||||
"<|tool_call_begin|>functions.get_weather:0",
|
||||
"<|tool_call_argument_begin|>{\"location\":\"Hangzhou\",\"days\":3}",
|
||||
"<|tool_call_end|>",
|
||||
"<|tool_call_begin|>functions.get_weather:1",
|
||||
"<|tool_call_argument_begin|>{\"location\":\"San Francisco\",\"days\":2}",
|
||||
"<|tool_call_end|>",
|
||||
"<|tool_calls_section_end|>",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn mixed_chunks() -> Vec<&'static str> {
|
||||
vec![
|
||||
"I will check two cities before answering.\n",
|
||||
"<|tool_calls_section_begin|>",
|
||||
"<|tool_call_begin|>functions.get_weather:0",
|
||||
"<|tool_call_argument_begin|>",
|
||||
"{\"location\":",
|
||||
"\"Hangzhou\",",
|
||||
"\"days\":3}",
|
||||
"<|tool_call_end|>",
|
||||
"<|tool_call_begin|>functions.get_weather:1",
|
||||
"<|tool_call_argument_begin|>",
|
||||
"{\"location\":",
|
||||
"\"San Francisco\",",
|
||||
"\"days\":2}",
|
||||
"<|tool_call_end|>",
|
||||
"<|tool_calls_section_end|>",
|
||||
]
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no Kimi K2 tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
KimiK2ToolParser::create(tools).expect("Kimi K2 parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunks: &[&str],
|
||||
expected_normal_text: &str,
|
||||
expected_native_calls_len: usize,
|
||||
) {
|
||||
let openai_tools = openai_tools(tools);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("native_reuse_parser", |b| {
|
||||
let mut parser = native_parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("native_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| native_parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.bench_function("external_reuse_parser", |b| {
|
||||
let mut parser = ExternalKimiK2Parser::new();
|
||||
b.iter(|| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(chunks));
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("external_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
ExternalKimiK2Parser::new,
|
||||
|mut parser| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(chunks));
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_kimi_k2(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let mixed_chunks = mixed_chunks();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
let long_normal_chunks = split_by_chars(&long_normal_text, CHUNK_CHARS);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"kimi_k2/mixed_text_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
&mixed_chunks,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"kimi_k2/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
&long_normal_chunks,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_kimi_k2);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,128 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::LlamaParser as ExternalLlamaParser;
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{Llama3JsonToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn tool_call(function_name: &str, parameters: &str) -> String {
|
||||
format!(r#"{{"name":"{function_name}","parameters":{parameters}}}"#)
|
||||
}
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
format!(
|
||||
"{}; {}",
|
||||
tool_call("get_weather", r#"{"location":"Hangzhou","days":3}"#),
|
||||
tool_call(
|
||||
"convert",
|
||||
r#"{"whole":42.5,"flag":true,"payload":{"nested":["x",null]},"items":[1,2,3],"empty":""}"#
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no Llama JSON tool call at the root.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
Llama3JsonToolParser::create(tools).expect("Llama JSON parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_native_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
let openai_tools = openai_tools(tools);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("native_reuse_parser", |b| {
|
||||
let mut parser = native_parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("native_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| native_parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.bench_function("external_reuse_parser", |b| {
|
||||
let mut parser = ExternalLlamaParser::new();
|
||||
b.iter(|| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("external_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
ExternalLlamaParser::new,
|
||||
|mut parser| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_llama3_json(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"llama3_json/mixed_text_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"llama3_json/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_llama3_json);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,136 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::MinimaxM2Parser as ExternalMinimaxM2Parser;
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{MinimaxM2ToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will check two cities before answering.\n",
|
||||
"<minimax:tool_call>",
|
||||
"<invoke name=\"get_weather\">",
|
||||
"<parameter name=\"city\">Hangzhou</parameter>",
|
||||
"<parameter name=\"date\">2026-04-30</parameter>",
|
||||
"<parameter name=\"unit\">celsius</parameter>",
|
||||
"<parameter name=\"days\">3</parameter>",
|
||||
"</invoke>",
|
||||
"<invoke name=\"get_weather\">",
|
||||
"<parameter name=\"city\">San Francisco</parameter>",
|
||||
"<parameter name=\"date\">2026-04-30</parameter>",
|
||||
"<parameter name=\"unit\">fahrenheit</parameter>",
|
||||
"<parameter name=\"days\">2</parameter>",
|
||||
"</invoke>",
|
||||
"</minimax:tool_call>",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no MiniMax M2 tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
MinimaxM2ToolParser::create(tools).expect("MiniMax M2 parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_native_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
let openai_tools = openai_tools(tools);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("native_reuse_parser", |b| {
|
||||
let mut parser = native_parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("native_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| native_parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.bench_function("external_reuse_parser", |b| {
|
||||
let mut parser = ExternalMinimaxM2Parser::new();
|
||||
b.iter(|| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("external_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
ExternalMinimaxM2Parser::new,
|
||||
|mut parser| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_minimax_m2(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"minimax_m2/mixed_text_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"minimax_m2/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_minimax_m2);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,138 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::QwenCoderParser as ExternalQwenCoderParser;
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{Qwen3CoderToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
concat!(
|
||||
"I will check two cities before answering.\n",
|
||||
"<tool_call>\n",
|
||||
"<function=get_weather>\n",
|
||||
"<parameter=location>Hangzhou</parameter>\n",
|
||||
"<parameter=date>2026-04-29</parameter>\n",
|
||||
"<parameter=unit>celsius</parameter>\n",
|
||||
"<parameter=days>3</parameter>\n",
|
||||
"</function>\n",
|
||||
"</tool_call>\n",
|
||||
"<tool_call>\n",
|
||||
"<function=get_weather>\n",
|
||||
"<parameter=location>San Francisco</parameter>\n",
|
||||
"<parameter=date>2026-04-29</parameter>\n",
|
||||
"<parameter=unit>fahrenheit</parameter>\n",
|
||||
"<parameter=days>2</parameter>\n",
|
||||
"</function>\n",
|
||||
"</tool_call>",
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no Qwen Coder tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
Qwen3CoderToolParser::create(tools).expect("Qwen Coder parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_native_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
let openai_tools = openai_tools(tools);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("native_reuse_parser", |b| {
|
||||
let mut parser = native_parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("native_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| native_parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.bench_function("external_reuse_parser", |b| {
|
||||
let mut parser = ExternalQwenCoderParser::new();
|
||||
b.iter(|| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("external_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
ExternalQwenCoderParser::new,
|
||||
|mut parser| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_qwen3_coder(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"qwen3_coder/mixed_text_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"qwen3_coder/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_qwen3_coder);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,125 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::QwenParser as ExternalQwenParser;
|
||||
use vllm_tool_parser::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_tool_parser::{Qwen3XmlToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::{feed_external_parser, feed_parser, openai_tools};
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
|
||||
fn tool_call(function_name: &str, arguments: &str) -> String {
|
||||
format!("<tool_call>\n{{\"name\":\"{function_name}\",\"arguments\":{arguments}}}\n</tool_call>")
|
||||
}
|
||||
|
||||
fn mixed_fixture() -> String {
|
||||
format!(
|
||||
"I will check two cities before answering.\n{}{}",
|
||||
tool_call("get_weather", r#"{"location":"Hangzhou","days":3}"#),
|
||||
tool_call("get_weather", r#"{"location":"San Francisco","days":2}"#),
|
||||
)
|
||||
}
|
||||
|
||||
fn long_normal_text_fixture() -> String {
|
||||
let line = "This is ordinary assistant text with no Qwen XML tool markers at all.\n";
|
||||
line.repeat(LONG_NORMAL_TEXT_REPEATS)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
Qwen3XmlToolParser::create(tools).expect("Qwen XML parser should initialize")
|
||||
}
|
||||
|
||||
fn run_stream_group(
|
||||
c: &mut Criterion,
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
text: &str,
|
||||
chunk_chars: usize,
|
||||
expected_normal_text: &str,
|
||||
expected_native_calls_len: usize,
|
||||
) {
|
||||
let chunks = split_by_chars(text, chunk_chars);
|
||||
let openai_tools = openai_tools(tools);
|
||||
|
||||
let mut group = c.benchmark_group(name);
|
||||
group.sample_size(50);
|
||||
group.warm_up_time(Duration::from_millis(300));
|
||||
group.measurement_time(Duration::from_secs(2));
|
||||
group.throughput(Throughput::Bytes(text.len() as u64));
|
||||
|
||||
group.bench_function("native_reuse_parser", |b| {
|
||||
let mut parser = native_parser(tools);
|
||||
b.iter(|| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("native_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
|| native_parser(tools),
|
||||
|mut parser| {
|
||||
let result = feed_parser(&mut *parser, black_box(&chunks));
|
||||
debug_assert_eq!(result.0, expected_normal_text);
|
||||
debug_assert_eq!(result.1, expected_native_calls_len);
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.bench_function("external_reuse_parser", |b| {
|
||||
let mut parser = ExternalQwenParser::new();
|
||||
b.iter(|| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
black_box(result);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("external_create_parser", |b| {
|
||||
b.iter_batched(
|
||||
ExternalQwenParser::new,
|
||||
|mut parser| {
|
||||
let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks));
|
||||
black_box(result);
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_qwen3_xml(c: &mut Criterion) {
|
||||
let tools = test_tools();
|
||||
let mixed_text = mixed_fixture();
|
||||
let long_normal_text = long_normal_text_fixture();
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"qwen3_xml/mixed_text_tool_call",
|
||||
&tools,
|
||||
&mixed_text,
|
||||
CHUNK_CHARS,
|
||||
"I will check two cities before answering.\n",
|
||||
2,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"qwen3_xml/long_normal_text",
|
||||
&tools,
|
||||
&long_normal_text,
|
||||
CHUNK_CHARS,
|
||||
&long_normal_text,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_qwen3_xml);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,49 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use futures::FutureExt as _;
|
||||
use openai_protocol::common::{Function as OpenAiFunction, Tool as OpenAiTool};
|
||||
use tool_parser::traits::ToolParser as ExternalToolParser;
|
||||
use vllm_tool_parser::test_utils::collect_stream;
|
||||
use vllm_tool_parser::{Tool, ToolParser};
|
||||
|
||||
pub(super) fn openai_tools(tools: &[Tool]) -> Vec<OpenAiTool> {
|
||||
tools
|
||||
.iter()
|
||||
.map(|tool| OpenAiTool {
|
||||
tool_type: "function".to_string(),
|
||||
function: OpenAiFunction {
|
||||
name: tool.name.clone(),
|
||||
description: tool.description.clone(),
|
||||
parameters: tool.parameters.clone(),
|
||||
strict: tool.strict,
|
||||
},
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(super) fn feed_parser(parser: &mut dyn ToolParser, chunks: &[&str]) -> (String, usize) {
|
||||
let result = collect_stream(parser, chunks);
|
||||
(result.normal_text, result.calls.len())
|
||||
}
|
||||
|
||||
pub(super) fn feed_external_parser(
|
||||
parser: &mut impl ExternalToolParser,
|
||||
tools: &[OpenAiTool],
|
||||
chunks: &[&str],
|
||||
) -> (String, usize) {
|
||||
ExternalToolParser::reset(parser);
|
||||
|
||||
let mut normal_text = String::new();
|
||||
let mut calls_len = 0;
|
||||
for chunk in chunks {
|
||||
let delta = parser
|
||||
.parse_incremental(chunk, tools)
|
||||
.now_or_never()
|
||||
.expect("external parser should not suspend")
|
||||
.expect("chunk should parse");
|
||||
normal_text.push_str(&delta.normal_text);
|
||||
calls_len += delta.calls.len();
|
||||
}
|
||||
calls_len += parser.get_unstreamed_tool_args().unwrap_or_default().len();
|
||||
(normal_text, calls_len)
|
||||
}
|
||||
@@ -0,0 +1,439 @@
|
||||
use super::{DeepSeekDsmlToolParser, DsmlTokens};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
/// Tool parser for DeepSeek V3.2 models.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <|DSML|function_calls>
|
||||
/// <|DSML|invoke name="get_weather">
|
||||
/// <|DSML|parameter name="location" string="true">杭州</|DSML|parameter>
|
||||
/// <|DSML|parameter name="date" string="true">2024-01-16</|DSML|parameter>
|
||||
/// </|DSML|invoke>
|
||||
/// <|DSML|invoke name="get_weather">
|
||||
/// <|DSML|parameter name="location" string="true">北京</|DSML|parameter>
|
||||
/// <|DSML|parameter name="date" string="true">2024-01-16</|DSML|parameter>
|
||||
/// </|DSML|invoke>
|
||||
/// </|DSML|function_calls>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are emitted only after a full `invoke` block is parsed.
|
||||
///
|
||||
/// DeepSeek V3.2 relies on DSML markers such as `|DSML|`, which are
|
||||
/// represented as special tokens in the tokenizer and therefore must be
|
||||
/// preserved during decode for parsing to work.
|
||||
pub struct DeepSeekV32ToolParser(DeepSeekDsmlToolParser);
|
||||
|
||||
impl DeepSeekV32ToolParser {
|
||||
/// Create a DeepSeek V3.2 tool parser.
|
||||
pub(super) fn new(tools: &[Tool]) -> Self {
|
||||
Self(DeepSeekDsmlToolParser::new(tools, DsmlTokens::V32))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for DeepSeekV32ToolParser {
|
||||
/// Create a boxed DeepSeek V3.2 tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
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<ToolParseResult> {
|
||||
self.0.push(chunk)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.0.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::DeepSeekV32ToolParser;
|
||||
use crate::ToolParser;
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
|
||||
fn build_tool_call(function_name: &str, params: &[(&str, &str)]) -> String {
|
||||
let params = params
|
||||
.iter()
|
||||
.map(|(name, value)| {
|
||||
format!(
|
||||
r#"<|DSML|parameter name="{name}" string="true">{value}</|DSML|parameter>"#
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
format!(
|
||||
"<|DSML|function_calls>\n<|DSML|invoke name=\"{function_name}\">\n{params}\n</|DSML|invoke>\n</|DSML|function_calls>"
|
||||
)
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let result = 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_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"location": "SF",
|
||||
"date": "2024-01-16"
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_preserves_prefix_text() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = format!(
|
||||
"Thinking... {}",
|
||||
build_tool_call("get_weather", &[("location", "NYC")])
|
||||
);
|
||||
let result = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(result.normal_text, "Thinking... ");
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_converts_schema_types() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(
|
||||
"<|DSML|function_calls>\n\
|
||||
<|DSML|invoke name=\"convert\">\n\
|
||||
<|DSML|parameter name=\"whole\" string=\"false\">5.0</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"flag\" string=\"false\">true</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"payload\" string=\"false\">{\"nested\":true}</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"items\" string=\"false\">[1,2]</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"empty\" string=\"false\">null</|DSML|parameter>\n\
|
||||
</|DSML|invoke>\n\
|
||||
</|DSML|function_calls>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": 5.0,
|
||||
"flag": true,
|
||||
"payload": { "nested": true },
|
||||
"items": [1, 2],
|
||||
"empty": null,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_string_attr_overrides_schema_types() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(
|
||||
"<|DSML|function_calls>\n\
|
||||
<|DSML|invoke name=\"convert\">\n\
|
||||
<|DSML|parameter name=\"whole\" string=\"true\">5.0</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"flag\" string=\"true\">true</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"payload\" string=\"true\">{\"nested\":true}</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"items\" string=\"true\">[1,2]</|DSML|parameter>\n\
|
||||
<|DSML|parameter name=\"empty\" string=\"true\">null</|DSML|parameter>\n\
|
||||
</|DSML|invoke>\n\
|
||||
</|DSML|function_calls>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": "5.0",
|
||||
"flag": "true",
|
||||
"payload": "{\"nested\":true}",
|
||||
"items": "[1,2]",
|
||||
"empty": "null",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_unescapes_literal_closing_tags_in_parameter_value() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"get_weather",
|
||||
&[
|
||||
(
|
||||
"location",
|
||||
"Hangzhou </|DSML|parameter></|DSML|invoke></|DSML|function_calls>",
|
||||
),
|
||||
("date", "2026-05-08"),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"location": "Hangzhou </|DSML|parameter></|DSML|invoke></|DSML|function_calls>",
|
||||
"date": "2026-05-08",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_extracts_single_tool_call() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"<|DSML|function_calls>\n",
|
||||
"<|DSML|invoke name=\"get_weather\">\n",
|
||||
"<|DSML|parameter name=\"location\" string=\"true\">SF</|DSML|parameter>\n",
|
||||
"</|DSML|invoke>\n",
|
||||
"</|DSML|function_calls>",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_preserves_prefix_text() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"Thinking... ",
|
||||
"<|DSML|function_calls>\n",
|
||||
"<|DSML|invoke name=\"get_weather\">\n",
|
||||
"<|DSML|parameter name=\"location\" string=\"true\">SF</|DSML|parameter>\n",
|
||||
"</|DSML|invoke>\n",
|
||||
"</|DSML|function_calls>",
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(result.normal_text, "Thinking... ");
|
||||
assert_eq!(result.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!"]);
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.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(
|
||||
&mut parser,
|
||||
&[&format!(
|
||||
"{}\n{}",
|
||||
build_tool_call("get_weather", &[("location", "SF")])
|
||||
.trim_end_matches("</|DSML|function_calls>"),
|
||||
"<|DSML|invoke name=\"get_weather\">\n<|DSML|parameter name=\"location\" string=\"true\">NYC</|DSML|parameter>\n</|DSML|invoke>\n</|DSML|function_calls>"
|
||||
)],
|
||||
);
|
||||
|
||||
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!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[1].arguments).unwrap(),
|
||||
json!({ "location": "NYC" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_handles_start_token_split_across_chunks() {
|
||||
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);
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_handles_bpe_chunked_dsml_opener() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"<|DSML|",
|
||||
"function",
|
||||
"_c",
|
||||
"all",
|
||||
"s",
|
||||
">\n",
|
||||
"<|DSML|",
|
||||
"invoke",
|
||||
" name=\"",
|
||||
"get_weather",
|
||||
"\">\n",
|
||||
"<|DSML|",
|
||||
"parameter",
|
||||
" name=\"location\" string=\"true\">",
|
||||
"Beijing",
|
||||
"</|DSML|",
|
||||
"parameter>\n",
|
||||
"</|DSML|",
|
||||
"invoke>\n",
|
||||
"</|DSML|",
|
||||
"function_calls>",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "Beijing" })
|
||||
);
|
||||
}
|
||||
|
||||
#[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
|
||||
.push("<|DSML|parameter name=\"location\" string=\"true\">Tokyo")
|
||||
.unwrap();
|
||||
parser.push("<|end▁of▁sentence|>").unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
assert!(error.to_report_string().contains("incomplete DeepSeek DSML tool call"));
|
||||
}
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_drops_eos_after_complete_tool_calls() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"<|DSML|function_calls>\n",
|
||||
"<|DSML|invoke name=\"get_weather\">\n",
|
||||
"<|DSML|parameter name=\"location\" string=\"true\">SF</|DSML|parameter>\n",
|
||||
"</|DSML|invoke>\n",
|
||||
"</|DSML|function_calls><|end▁of▁sentence|>",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.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(
|
||||
&mut parser,
|
||||
&[
|
||||
"<|DSML|function_calls>\n",
|
||||
"<|DSML|invoke name=\"get_weather\">\n",
|
||||
"<|DSML|parameter name=\"location\" string=\"true\">SF</|DSML|parameter>\n",
|
||||
"</|DSML|invoke>\n",
|
||||
"</|DSML|function_calls>",
|
||||
"trailing text",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert_eq!(result.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
|
||||
.push("<|DSML|parameter name=\"location\" string=\"true\">SF</|DSML|parameter>\n")
|
||||
.unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
assert!(error.to_report_string().contains("incomplete DeepSeek DSML tool call"));
|
||||
}
|
||||
#[test]
|
||||
fn deepseek_v32_parser_state_resets_after_finish() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let first = parser
|
||||
.parse_complete(&build_tool_call("get_weather", &[("location", "SF")]))
|
||||
.unwrap();
|
||||
let second = parser
|
||||
.parse_complete(&build_tool_call("get_weather", &[("location", "NYC")]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(first.calls.len(), 1);
|
||||
assert_eq!(second.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&second.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "NYC" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_matches_parse_complete() {
|
||||
let full_text = build_tool_call("add", &[("x", "3"), ("y", "4")]);
|
||||
let chunks = split_by_chars(&full_text, 7);
|
||||
let mut streaming_parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let streamed = collect_stream(&mut streaming_parser, &chunks);
|
||||
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let complete = parser.parse_complete(&full_text).unwrap();
|
||||
|
||||
assert_eq!(streamed.normal_text, complete.normal_text);
|
||||
assert_eq!(streamed.calls, complete.calls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
use super::{DeepSeekDsmlToolParser, DsmlTokens};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
/// Tool parser for DeepSeek V4 models.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <|DSML|tool_calls>
|
||||
/// <|DSML|invoke name="get_weather">
|
||||
/// <|DSML|parameter name="location" string="true">杭州</|DSML|parameter>
|
||||
/// <|DSML|parameter name="date" string="true">2024-01-16</|DSML|parameter>
|
||||
/// </|DSML|invoke>
|
||||
/// <|DSML|invoke name="get_weather">
|
||||
/// <|DSML|parameter name="location" string="true">北京</|DSML|parameter>
|
||||
/// <|DSML|parameter name="date" string="true">2024-01-16</|DSML|parameter>
|
||||
/// </|DSML|invoke>
|
||||
/// </|DSML|tool_calls>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are emitted only after a full `invoke` block is parsed.
|
||||
///
|
||||
/// V4 reuses the V3.2 DSML invoke/parameter grammar but wraps calls in
|
||||
/// `<|DSML|tool_calls>` instead of `<|DSML|function_calls>`.
|
||||
///
|
||||
/// DeepSeek V4 relies on DSML markers such as `|DSML|`, which are
|
||||
/// represented as special tokens in the tokenizer and therefore must be
|
||||
/// preserved during decode for parsing to work.
|
||||
pub struct DeepSeekV4ToolParser(DeepSeekDsmlToolParser);
|
||||
|
||||
impl DeepSeekV4ToolParser {
|
||||
/// Create a DeepSeek V4 tool parser.
|
||||
fn new(tools: &[Tool]) -> Self {
|
||||
Self(DeepSeekDsmlToolParser::new(tools, DsmlTokens::V4))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for DeepSeekV4ToolParser {
|
||||
/// Create a boxed DeepSeek V4 tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
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<ToolParseResult> {
|
||||
self.0.push(chunk)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.0.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{DeepSeekV4ToolParser, ToolParser};
|
||||
use crate::test_utils::{collect_stream, test_tools};
|
||||
|
||||
fn build_tool_call(function_name: &str, params: &[(&str, &str)]) -> String {
|
||||
let params = params
|
||||
.iter()
|
||||
.map(|(name, value)| {
|
||||
format!(
|
||||
r#"<|DSML|parameter name="{name}" string="true">{value}</|DSML|parameter>"#
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
format!(
|
||||
"<|DSML|tool_calls>\n<|DSML|invoke name=\"{function_name}\">\n{params}\n</|DSML|invoke>\n</|DSML|tool_calls>"
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v4_parse_complete_reuses_dsml_parser_with_tool_calls_token() {
|
||||
let mut parser = DeepSeekV4ToolParser::new(&test_tools());
|
||||
let result = 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_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"location": "SF",
|
||||
"date": "2024-01-16"
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v4_streaming_handles_tool_calls_token_split_across_chunks() {
|
||||
let mut parser = DeepSeekV4ToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"Thinking... ",
|
||||
"<|DSML|",
|
||||
"tool",
|
||||
"_calls>\n",
|
||||
"<|DSML|invoke name=\"get_weather\">\n",
|
||||
"<|DSML|parameter name=\"location\" string=\"true\">Beijing</|DSML|parameter>\n",
|
||||
"</|DSML|invoke>\n",
|
||||
"</|DSML|",
|
||||
"tool_calls>",
|
||||
],
|
||||
);
|
||||
|
||||
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!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "Beijing" })
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
use winnow::ascii::{multispace0 as ws0, multispace1 as ws1};
|
||||
use winnow::combinator::{alt, delimited, eof, repeat, seq, terminated};
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::Partial;
|
||||
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 crate::Tool;
|
||||
|
||||
mod deepseek_v32;
|
||||
mod deepseek_v4;
|
||||
|
||||
pub use deepseek_v4::DeepSeekV4ToolParser;
|
||||
pub use deepseek_v32::DeepSeekV32ToolParser;
|
||||
|
||||
const INVOKE_START: &str = "<|DSML|invoke";
|
||||
const INVOKE_END: &str = "</|DSML|invoke>";
|
||||
const PARAMETER_START: &str = "<|DSML|parameter";
|
||||
const PARAMETER_END: &str = "</|DSML|parameter>";
|
||||
|
||||
type DsmlInput<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct DsmlTokens {
|
||||
tool_calls_start: &'static str,
|
||||
tool_calls_end: &'static str,
|
||||
}
|
||||
|
||||
impl DsmlTokens {
|
||||
const V32: Self = Self {
|
||||
tool_calls_start: "<|DSML|function_calls>",
|
||||
tool_calls_end: "</|DSML|function_calls>",
|
||||
};
|
||||
const V4: Self = Self {
|
||||
tool_calls_start: "<|DSML|tool_calls>",
|
||||
tool_calls_end: "</|DSML|tool_calls>",
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum DsmlMode {
|
||||
Text,
|
||||
ToolBlock,
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum DsmlEvent {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolCallsStart,
|
||||
Invoke {
|
||||
name: String,
|
||||
raw_params: Vec<DsmlParameter>,
|
||||
},
|
||||
ToolCallsEnd,
|
||||
IgnoredRest,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct DsmlParameter {
|
||||
name: String,
|
||||
value: String,
|
||||
is_string: bool,
|
||||
}
|
||||
|
||||
/// Tool parser core for DeepSeek DSML tool calls.
|
||||
struct DeepSeekDsmlToolParser {
|
||||
buffer: String,
|
||||
mode: DsmlMode,
|
||||
emitted_invoke_count: usize,
|
||||
tool_parameters: ToolSchemas,
|
||||
tokens: DsmlTokens,
|
||||
}
|
||||
|
||||
impl DeepSeekDsmlToolParser {
|
||||
/// Create a parser with DSML tokens for one DeepSeek format.
|
||||
fn new(tools: &[Tool], tokens: DsmlTokens) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: DsmlMode::Text,
|
||||
emitted_invoke_count: 0,
|
||||
tool_parameters: ToolSchemas::from_tools(tools),
|
||||
tokens,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed DSML event to parser state and output.
|
||||
fn apply_event(&mut self, event: DsmlEvent, result: &mut ToolParseResult) -> Result<()> {
|
||||
match event {
|
||||
DsmlEvent::Text { len: consumed_len } => {
|
||||
result.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
DsmlEvent::ToolCallsStart => self.mode = DsmlMode::ToolBlock,
|
||||
DsmlEvent::Invoke { name, raw_params } => {
|
||||
let mut arguments = serde_json::Map::with_capacity(raw_params.len());
|
||||
for param in raw_params {
|
||||
let value = if param.is_string {
|
||||
serde_json::Value::String(param.value)
|
||||
} else {
|
||||
self.tool_parameters.convert_param_with_schema(
|
||||
&name,
|
||||
¶m.name,
|
||||
¶m.value,
|
||||
)
|
||||
};
|
||||
arguments.insert(param.name, value);
|
||||
}
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_invoke_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_invoke_count += 1;
|
||||
}
|
||||
DsmlEvent::ToolCallsEnd => self.mode = DsmlMode::Done,
|
||||
DsmlEvent::IgnoredRest => {}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset all streaming state.
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.mode = DsmlMode::Text;
|
||||
self.emitted_invoke_count = 0;
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the DSML parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
// 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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::default();
|
||||
match self.mode {
|
||||
DsmlMode::Text => result.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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a DSML event for the current parser mode.
|
||||
fn parse_next_dsml_event(
|
||||
input: &mut DsmlInput<'_>,
|
||||
mode: DsmlMode,
|
||||
tokens: DsmlTokens,
|
||||
) -> ModalResult<DsmlEvent> {
|
||||
match mode {
|
||||
DsmlMode::Text => parse_text_event(input, tokens),
|
||||
DsmlMode::ToolBlock => parse_tool_block_event(input, tokens),
|
||||
DsmlMode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode DSML event.
|
||||
fn parse_text_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResult<DsmlEvent> {
|
||||
alt((
|
||||
|input: &mut DsmlInput<'_>| tool_calls_start_event(input, tokens),
|
||||
|input: &mut DsmlInput<'_>| safe_text_event(input, tokens),
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a tool-block DSML event.
|
||||
fn parse_tool_block_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResult<DsmlEvent> {
|
||||
ws0.void().parse_next(input)?;
|
||||
alt((invoke_event, |input: &mut DsmlInput<'_>| {
|
||||
tool_calls_end_event(input, tokens)
|
||||
}))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a DSML function-calls start marker.
|
||||
fn tool_calls_start_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResult<DsmlEvent> {
|
||||
literal(tokens.tool_calls_start)
|
||||
.value(DsmlEvent::ToolCallsStart)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a DSML function-calls end marker.
|
||||
fn tool_calls_end_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResult<DsmlEvent> {
|
||||
literal(tokens.tool_calls_end).value(DsmlEvent::ToolCallsEnd).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a trailing rest after DSML function calls.
|
||||
fn ignored_rest_event(input: &mut DsmlInput<'_>) -> ModalResult<DsmlEvent> {
|
||||
rest.value(DsmlEvent::IgnoredRest).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next DSML marker.
|
||||
fn safe_text_event(input: &mut DsmlInput<'_>, tokens: DsmlTokens) -> ModalResult<DsmlEvent> {
|
||||
safe_text_len(input, tokens.tool_calls_start).map(|len| DsmlEvent::Text { len })
|
||||
}
|
||||
|
||||
/// Parse a DSML invoke block.
|
||||
fn invoke_event(input: &mut DsmlInput<'_>) -> ModalResult<DsmlEvent> {
|
||||
let (name, body) = seq!(
|
||||
_: literal(INVOKE_START),
|
||||
_: ws1,
|
||||
dsml_name_attr,
|
||||
_: ws0,
|
||||
_: ">",
|
||||
take_until(0.., INVOKE_END),
|
||||
_: literal(INVOKE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
let raw_params = parse_invoke_params(body)?;
|
||||
Ok(DsmlEvent::Invoke {
|
||||
name: name.to_string(),
|
||||
raw_params,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a DSML invoke body.
|
||||
fn parse_invoke_params(invoke_body: &str) -> ModalResult<Vec<DsmlParameter>> {
|
||||
let mut input = invoke_body;
|
||||
delimited(ws0, repeat(0.., terminated(parse_parameter, ws0)), eof).parse_next(&mut input)
|
||||
}
|
||||
|
||||
/// Parse a DSML parameter block.
|
||||
fn parse_parameter(input: &mut &str) -> ModalResult<DsmlParameter> {
|
||||
seq! {DsmlParameter {
|
||||
_: literal(PARAMETER_START),
|
||||
_: ws1,
|
||||
name: name_attr.map(|name: &str| name.to_string()),
|
||||
_: ws1,
|
||||
is_string: string_attr.map(|value| value == "true"),
|
||||
_: ws0,
|
||||
_: ">",
|
||||
value: take_until(0.., PARAMETER_END).map(xml_unescape).map(|value| value.into_owned()),
|
||||
_: literal(PARAMETER_END),
|
||||
}}
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a name attribute.
|
||||
fn name_attr<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
delimited("name=\"", take_until(1.., "\""), "\"").parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a string attribute.
|
||||
fn string_attr<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
delimited("string=\"", alt(("true", "false")), "\"").parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a DSML name attribute.
|
||||
fn dsml_name_attr<'i>(input: &mut DsmlInput<'i>) -> ModalResult<&'i str> {
|
||||
delimited("name=\"", take_until(1.., "\""), "\"").parse_next(input)
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
use super::{DeepSeekJsonFormat, DeepSeekJsonToolParser};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
/// Tool parser for DeepSeek V3 JSON-fenced tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ````text
|
||||
/// <|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_weather
|
||||
/// ```json
|
||||
/// {"location":"Tokyo"}
|
||||
/// ```<|tool▁call▁end|><|tool▁calls▁end|>
|
||||
/// ````
|
||||
///
|
||||
/// Arguments are already OpenAI-style JSON text inside the markdown fence, so
|
||||
/// they are streamed as raw argument deltas without schema conversion or JSON
|
||||
/// normalization.
|
||||
pub struct DeepSeekV3ToolParser(DeepSeekJsonToolParser);
|
||||
|
||||
impl DeepSeekV3ToolParser {
|
||||
/// Create a DeepSeek V3 tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self(DeepSeekJsonToolParser::new(DeepSeekJsonFormat::V3))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for DeepSeekV3ToolParser {
|
||||
/// Create a boxed DeepSeek V3 tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the DeepSeek V3 parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
self.0.push(chunk)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.0.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::DeepSeekV3ToolParser;
|
||||
use crate::deepseek_json::{
|
||||
TOOL_CALL_SEPARATOR, TOOL_CALL_START, TOOL_CALLS_END, TOOL_CALLS_START, V3_ARGUMENT_END,
|
||||
V3_JSON_START,
|
||||
};
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::{ToolParseResult, ToolParser};
|
||||
|
||||
fn v3_tool_call(function_name: &str, arguments: &str) -> String {
|
||||
format!(
|
||||
"{TOOL_CALL_START}function{TOOL_CALL_SEPARATOR}{function_name}{V3_JSON_START}{arguments}{V3_ARGUMENT_END}"
|
||||
)
|
||||
}
|
||||
|
||||
fn tool_section(tool_calls: &[String]) -> String {
|
||||
format!("{TOOL_CALLS_START}{}{TOOL_CALLS_END}", tool_calls.join(""))
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.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
|
||||
.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);
|
||||
}
|
||||
|
||||
#[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
|
||||
.parse_complete(&tool_section(&[v3_tool_call("get_weather", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_streaming_emits_argument_deltas() {
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
TOOL_CALLS_START,
|
||||
TOOL_CALL_START,
|
||||
"function",
|
||||
TOOL_CALL_SEPARATOR,
|
||||
"get_weather",
|
||||
V3_JSON_START,
|
||||
"{\"location\":",
|
||||
"\"Beijing\"",
|
||||
"}",
|
||||
V3_ARGUMENT_END,
|
||||
TOOL_CALLS_END,
|
||||
];
|
||||
|
||||
let mut result = ToolParseResult::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.push(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
result.append(next);
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(
|
||||
result.coalesce_calls().calls[0].arguments,
|
||||
r#"{"location":"Beijing"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_streaming_handles_split_markers() {
|
||||
let input = format!(
|
||||
"hello {}",
|
||||
tool_section(&[v3_tool_call("get_weather", r#"{"location":"Tokyo"}"#)])
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
|
||||
let result = 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"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_keeps_fenced_end_marker_literal_inside_json_string() {
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
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();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_streaming_extracts_multiple_tool_calls() {
|
||||
let input = tool_section(&[
|
||||
v3_tool_call("get_weather", r#"{"location":"Shanghai"}"#),
|
||||
v3_tool_call("add", r#"{"x":1,"y":2}"#),
|
||||
]);
|
||||
let chunks = split_by_chars(&input, 7);
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\":1,\"y\":2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
parser
|
||||
.push(&format!(
|
||||
"{TOOL_CALLS_START}{TOOL_CALL_START}function{TOOL_CALL_SEPARATOR}get_weather{V3_JSON_START}{{\"location\""
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete DeepSeek V3 tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_malformed_type_fails_fast() {
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
let input = format!(
|
||||
"{TOOL_CALLS_START}{TOOL_CALL_START}tool{TOOL_CALL_SEPARATOR}get_weather{V3_JSON_START}{{}}"
|
||||
);
|
||||
|
||||
let error = parser.push(&input).unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
use super::{DeepSeekJsonFormat, DeepSeekJsonToolParser};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
/// Tool parser for DeepSeek V3.1 raw JSON tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <|tool▁calls▁begin|><|tool▁call▁begin|>get_weather<|tool▁sep|>{"location":"Tokyo"}<|tool▁call▁end|><|tool▁calls▁end|>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are already OpenAI-style JSON text, so they are streamed as raw
|
||||
/// argument deltas without schema conversion or JSON normalization.
|
||||
pub struct DeepSeekV31ToolParser(DeepSeekJsonToolParser);
|
||||
|
||||
impl DeepSeekV31ToolParser {
|
||||
/// Create a DeepSeek V3.1 tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self(DeepSeekJsonToolParser::new(DeepSeekJsonFormat::V31))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for DeepSeekV31ToolParser {
|
||||
/// Create a boxed DeepSeek V3.1 tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the DeepSeek V3.1 parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
self.0.push(chunk)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.0.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::DeepSeekV31ToolParser;
|
||||
use crate::deepseek_json::{
|
||||
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};
|
||||
|
||||
fn v31_tool_call(function_name: &str, arguments: &str) -> String {
|
||||
format!("{TOOL_CALL_START}{function_name}{TOOL_CALL_SEPARATOR}{arguments}{TOOL_CALL_END}")
|
||||
}
|
||||
|
||||
fn tool_section(tool_calls: &[String]) -> String {
|
||||
format!("{TOOL_CALLS_START}{}{TOOL_CALLS_END}", tool_calls.join(""))
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.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
|
||||
.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);
|
||||
}
|
||||
|
||||
#[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
|
||||
.parse_complete(&tool_section(&[v31_tool_call("get_weather", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_streaming_emits_argument_deltas() {
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
TOOL_CALLS_START,
|
||||
TOOL_CALL_START,
|
||||
"get_weather",
|
||||
TOOL_CALL_SEPARATOR,
|
||||
"{\"location\":",
|
||||
"\"Beijing\"",
|
||||
"}",
|
||||
TOOL_CALL_END,
|
||||
TOOL_CALLS_END,
|
||||
];
|
||||
|
||||
let mut result = ToolParseResult::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.push(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
result.append(next);
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(
|
||||
result.coalesce_calls().calls[0].arguments,
|
||||
r#"{"location":"Beijing"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_streaming_handles_split_markers() {
|
||||
let input = format!(
|
||||
"hello {}",
|
||||
tool_section(&[v31_tool_call("get_weather", r#"{"location":"Tokyo"}"#)])
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
|
||||
let result = 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"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_keeps_end_marker_literal_inside_json_string() {
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
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();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_streaming_extracts_multiple_tool_calls() {
|
||||
let input = tool_section(&[
|
||||
v31_tool_call("get_weather", r#"{"location":"Shanghai"}"#),
|
||||
v31_tool_call("add", r#"{"x":1,"y":2}"#),
|
||||
]);
|
||||
let chunks = split_by_chars(&input, 7);
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\":1,\"y\":2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_streaming_drops_eos_after_complete_tool_calls() {
|
||||
let input = format!(
|
||||
"{}<|end▁of▁sentence|>",
|
||||
tool_section(&[v31_tool_call("get_weather", r#"{"location":"Tokyo"}"#)])
|
||||
);
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
|
||||
let result = 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"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
parser
|
||||
.push(&format!(
|
||||
"{TOOL_CALLS_START}{TOOL_CALL_START}get_weather{TOOL_CALL_SEPARATOR}{{\"location\""
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete DeepSeek V3.1 tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_malformed_empty_name_fails_fast() {
|
||||
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();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
mod deepseek_v3;
|
||||
mod deepseek_v31;
|
||||
|
||||
pub use deepseek_v3::DeepSeekV3ToolParser;
|
||||
pub use deepseek_v31::DeepSeekV31ToolParser;
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::{alt, seq};
|
||||
use winnow::prelude::*;
|
||||
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};
|
||||
|
||||
pub(super) const TOOL_CALLS_START: &str = "<|tool▁calls▁begin|>";
|
||||
pub(super) const TOOL_CALLS_END: &str = "<|tool▁calls▁end|>";
|
||||
pub(super) const TOOL_CALL_START: &str = "<|tool▁call▁begin|>";
|
||||
pub(super) const TOOL_CALL_END: &str = "<|tool▁call▁end|>";
|
||||
pub(super) const TOOL_CALL_SEPARATOR: &str = "<|tool▁sep|>";
|
||||
pub(super) const V3_JSON_START: &str = "\n```json\n";
|
||||
pub(super) const V3_ARGUMENT_END: &str = "\n```<|tool▁call▁end|>";
|
||||
|
||||
type DeepSeekJsonInput<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum DeepSeekJsonFormat {
|
||||
V3,
|
||||
V31,
|
||||
}
|
||||
|
||||
impl DeepSeekJsonFormat {
|
||||
/// Return the parser name used in diagnostics.
|
||||
const fn parser_name(self) -> &'static str {
|
||||
match self {
|
||||
Self::V3 => "DeepSeek V3",
|
||||
Self::V31 => "DeepSeek V3.1",
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the marker that closes the raw JSON arguments payload.
|
||||
const fn argument_end_marker(self) -> &'static str {
|
||||
match self {
|
||||
Self::V3 => V3_ARGUMENT_END,
|
||||
Self::V31 => TOOL_CALL_END,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum DeepSeekJsonMode {
|
||||
Text,
|
||||
ToolBlock,
|
||||
Header,
|
||||
Arguments { json_scan: JsonObjectScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum DeepSeekJsonEvent {
|
||||
Text { len: usize },
|
||||
ToolCallsStart,
|
||||
ToolCallStart,
|
||||
ToolCallHeader { function_name: String },
|
||||
Arguments { len: usize },
|
||||
ToolCallEnd,
|
||||
ToolCallsEnd,
|
||||
IgnoredRest,
|
||||
}
|
||||
|
||||
/// Tool parser core for DeepSeek JSON-argument tool calls.
|
||||
struct DeepSeekJsonToolParser {
|
||||
buffer: String,
|
||||
mode: DeepSeekJsonMode,
|
||||
active_tool_index: Option<usize>,
|
||||
emitted_tool_count: usize,
|
||||
format: DeepSeekJsonFormat,
|
||||
}
|
||||
|
||||
impl DeepSeekJsonToolParser {
|
||||
/// Create a parser for one DeepSeek JSON-argument format.
|
||||
fn new(format: DeepSeekJsonFormat) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: DeepSeekJsonMode::Text,
|
||||
active_tool_index: None,
|
||||
emitted_tool_count: 0,
|
||||
format,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed DeepSeek JSON event to parser state and output.
|
||||
fn apply_event(
|
||||
&mut self,
|
||||
event: DeepSeekJsonEvent,
|
||||
result: &mut ToolParseResult,
|
||||
) -> Result<()> {
|
||||
match event {
|
||||
DeepSeekJsonEvent::Text { len: consumed_len } => {
|
||||
result.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
DeepSeekJsonEvent::ToolCallsStart => self.mode = DeepSeekJsonMode::ToolBlock,
|
||||
DeepSeekJsonEvent::ToolCallStart => self.mode = DeepSeekJsonMode::Header,
|
||||
DeepSeekJsonEvent::ToolCallHeader { function_name } => {
|
||||
let tool_index = self.emitted_tool_count;
|
||||
self.emitted_tool_count += 1;
|
||||
self.active_tool_index = Some(tool_index);
|
||||
self.mode = DeepSeekJsonMode::Arguments {
|
||||
json_scan: JsonObjectScanState::default(),
|
||||
};
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: Some(function_name),
|
||||
arguments: String::new(),
|
||||
});
|
||||
}
|
||||
DeepSeekJsonEvent::Arguments { len: consumed_len } => {
|
||||
let Some(tool_index) = self.active_tool_index else {
|
||||
return Err(parsing_failed!(
|
||||
"{} arguments without an active tool call",
|
||||
self.format.parser_name()
|
||||
));
|
||||
};
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: None,
|
||||
arguments: self.buffer[..consumed_len].to_string(),
|
||||
});
|
||||
}
|
||||
DeepSeekJsonEvent::ToolCallEnd => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = DeepSeekJsonMode::ToolBlock;
|
||||
}
|
||||
DeepSeekJsonEvent::ToolCallsEnd => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = DeepSeekJsonMode::Done;
|
||||
}
|
||||
DeepSeekJsonEvent::IgnoredRest => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the DeepSeek JSON parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::default();
|
||||
match &self.mode {
|
||||
DeepSeekJsonMode::Text => result.normal_text.push_str(&self.buffer),
|
||||
DeepSeekJsonMode::ToolBlock | DeepSeekJsonMode::Done => {}
|
||||
DeepSeekJsonMode::Header | DeepSeekJsonMode::Arguments { .. } => {
|
||||
return Err(parsing_failed!(
|
||||
"incomplete {} tool call",
|
||||
self.format.parser_name()
|
||||
));
|
||||
}
|
||||
}
|
||||
self.reset();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Reset all streaming state.
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.mode = DeepSeekJsonMode::Text;
|
||||
self.active_tool_index = None;
|
||||
self.emitted_tool_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek JSON event for the current parser mode.
|
||||
fn parse_next_deepseek_json_event(
|
||||
input: &mut DeepSeekJsonInput<'_>,
|
||||
mode: &mut DeepSeekJsonMode,
|
||||
format: DeepSeekJsonFormat,
|
||||
) -> ModalResult<DeepSeekJsonEvent> {
|
||||
match mode {
|
||||
DeepSeekJsonMode::Text => parse_text_event(input),
|
||||
DeepSeekJsonMode::ToolBlock => parse_tool_block_event(input),
|
||||
DeepSeekJsonMode::Header => tool_call_header_event(input, format),
|
||||
DeepSeekJsonMode::Arguments { json_scan } => {
|
||||
parse_arguments_event(input, format, json_scan)
|
||||
}
|
||||
DeepSeekJsonMode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode DeepSeek JSON event.
|
||||
fn parse_text_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
alt((tool_calls_start_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse one event inside the DeepSeek tool-calls section.
|
||||
fn parse_tool_block_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
ws0.void().parse_next(input)?;
|
||||
alt((tool_calls_end_event, tool_call_start_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse one event inside a DeepSeek tool-call arguments payload.
|
||||
fn parse_arguments_event(
|
||||
input: &mut DeepSeekJsonInput<'_>,
|
||||
format: DeepSeekJsonFormat,
|
||||
json_scan: &mut JsonObjectScanState,
|
||||
) -> ModalResult<DeepSeekJsonEvent> {
|
||||
if json_scan.complete() {
|
||||
tool_call_end_event(input, format)
|
||||
} else {
|
||||
argument_delta_event(input, json_scan)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek tool-calls start marker.
|
||||
fn tool_calls_start_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
literal(TOOL_CALLS_START)
|
||||
.value(DeepSeekJsonEvent::ToolCallsStart)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek tool-calls end marker.
|
||||
fn tool_calls_end_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
literal(TOOL_CALLS_END).value(DeepSeekJsonEvent::ToolCallsEnd).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek tool-call start marker.
|
||||
fn tool_call_start_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
literal(TOOL_CALL_START)
|
||||
.value(DeepSeekJsonEvent::ToolCallStart)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek tool-call end marker.
|
||||
fn tool_call_end_event(
|
||||
input: &mut DeepSeekJsonInput<'_>,
|
||||
format: DeepSeekJsonFormat,
|
||||
) -> ModalResult<DeepSeekJsonEvent> {
|
||||
literal(format.argument_end_marker())
|
||||
.value(DeepSeekJsonEvent::ToolCallEnd)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek tool-call header before the JSON arguments payload.
|
||||
fn tool_call_header_event(
|
||||
input: &mut DeepSeekJsonInput<'_>,
|
||||
format: DeepSeekJsonFormat,
|
||||
) -> ModalResult<DeepSeekJsonEvent> {
|
||||
match format {
|
||||
DeepSeekJsonFormat::V3 => v3_tool_call_header_event(input),
|
||||
DeepSeekJsonFormat::V31 => v31_tool_call_header_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek V3 tool-call header.
|
||||
fn v3_tool_call_header_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
let name = seq!(
|
||||
_: literal("function"),
|
||||
_: literal(TOOL_CALL_SEPARATOR),
|
||||
take_until(1.., V3_JSON_START),
|
||||
_: literal(V3_JSON_START),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok(DeepSeekJsonEvent::ToolCallHeader {
|
||||
function_name: name.0.trim().to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek V3.1 tool-call header.
|
||||
fn v31_tool_call_header_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
let (name, _) = (
|
||||
take_until(1.., TOOL_CALL_SEPARATOR),
|
||||
literal(TOOL_CALL_SEPARATOR),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok(DeepSeekJsonEvent::ToolCallHeader {
|
||||
function_name: name.trim().to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a DeepSeek raw JSON arguments delta.
|
||||
fn argument_delta_event(
|
||||
input: &mut DeepSeekJsonInput<'_>,
|
||||
json_scan: &mut JsonObjectScanState,
|
||||
) -> ModalResult<DeepSeekJsonEvent> {
|
||||
take_json_object(input, json_scan).map(|len| DeepSeekJsonEvent::Arguments { len })
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next DeepSeek tool-calls section.
|
||||
fn safe_text_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
safe_text_len(input, TOOL_CALLS_START).map(|len| DeepSeekJsonEvent::Text { len })
|
||||
}
|
||||
|
||||
/// Parse ignored rest after the DeepSeek tool-calls section ends.
|
||||
fn ignored_rest_event(input: &mut DeepSeekJsonInput<'_>) -> ModalResult<DeepSeekJsonEvent> {
|
||||
rest.value(DeepSeekJsonEvent::IgnoredRest).parse_next(input)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
use thiserror::Error;
|
||||
use thiserror_ext::Macro;
|
||||
|
||||
/// Result alias for tool parser operations.
|
||||
pub type Result<T> = std::result::Result<T, ToolParserError>;
|
||||
|
||||
/// Errors produced while creating or running tool parsers.
|
||||
#[derive(Debug, Error, Macro)]
|
||||
#[thiserror_ext(macro(path = "crate::error"))]
|
||||
pub enum ToolParserError {
|
||||
#[error("tool parser parsing failed: {message}")]
|
||||
ParsingFailed { message: String },
|
||||
}
|
||||
@@ -0,0 +1,653 @@
|
||||
use serde_json::{Map, Number, Value};
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::{alt, delimited, opt, separated, seq, terminated};
|
||||
use winnow::error::{ContextError, ErrMode, ModalResult};
|
||||
use winnow::prelude::*;
|
||||
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 crate::Tool;
|
||||
|
||||
const TOOL_CALL_START: &str = "<|tool_call>";
|
||||
const TOOL_CALL_END: &str = "<tool_call|>";
|
||||
const STRING_DELIM: &str = "<|\"|>";
|
||||
const CALL_PREFIX: &str = "call:";
|
||||
|
||||
type Gemma4Input<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
enum Gemma4Event {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolCall {
|
||||
name: String,
|
||||
args: Map<String, Value>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Tool parser for Google Gemma4 models.
|
||||
///
|
||||
/// Original Python implementation:
|
||||
/// <https://github.com/vllm-project/vllm/blob/bf45e6d0a558da2b8d7b60efb07b4aa394f3b60b/vllm/tool_parsers/gemma4_tool_parser.py>
|
||||
///
|
||||
/// Handles the Gemma4 function call format:
|
||||
///
|
||||
/// `<|tool_call>call:func_name{key:<|"|>value<|"|>}<tool_call|>`
|
||||
///
|
||||
/// Arguments are emitted only after a full Gemma4 tool call is parsed.
|
||||
pub struct Gemma4ToolParser {
|
||||
buffer: String,
|
||||
emitted_tool_count: usize,
|
||||
}
|
||||
|
||||
impl Gemma4ToolParser {
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
emitted_tool_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_event(&mut self, event: Gemma4Event, result: &mut ToolParseResult) -> Result<()> {
|
||||
match event {
|
||||
Gemma4Event::Text { len: consumed_len } => {
|
||||
result.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 {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.emitted_tool_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Gemma4ToolParser {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::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);
|
||||
}
|
||||
|
||||
self.reset();
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse one Gemma4 event from buffered streaming input.
|
||||
fn parse_next_gemma4_event(input: &mut Gemma4Input<'_>) -> ModalResult<Gemma4Event> {
|
||||
alt((tool_call_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a complete Gemma4 tool call.
|
||||
// TODO: incremental parsing arguments to reduce scanning from O(n^2) to O(n).
|
||||
fn tool_call_event(input: &mut Gemma4Input<'_>) -> ModalResult<Gemma4Event> {
|
||||
let (name, args) = seq!(
|
||||
_: literal(TOOL_CALL_START),
|
||||
_: literal(CALL_PREFIX),
|
||||
gemma4_tool_name,
|
||||
_: literal("{"),
|
||||
gemma4_args,
|
||||
_: literal("}"),
|
||||
_: literal(TOOL_CALL_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok(Gemma4Event::ToolCall { name, args })
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 tool name.
|
||||
fn gemma4_tool_name(input: &mut Gemma4Input<'_>) -> ModalResult<String> {
|
||||
let name = take_until(1.., "{").parse_next(input)?.trim();
|
||||
if name.is_empty() {
|
||||
return Err(ErrMode::Cut(ContextError::new()));
|
||||
}
|
||||
Ok(name.to_string())
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next Gemma4 marker.
|
||||
fn safe_text_event(input: &mut Gemma4Input<'_>) -> ModalResult<Gemma4Event> {
|
||||
safe_text_len(input, TOOL_CALL_START).map(|len| Gemma4Event::Text { len })
|
||||
}
|
||||
|
||||
/// Parse Gemma4's custom key-value argument object content.
|
||||
fn gemma4_args(input: &mut Gemma4Input<'_>) -> ModalResult<Map<String, Value>> {
|
||||
let pairs: Vec<(String, Value)> = delimited(
|
||||
ws0,
|
||||
terminated(
|
||||
separated(0.., gemma4_pair, comma_separator),
|
||||
opt(comma_separator),
|
||||
),
|
||||
ws0,
|
||||
)
|
||||
.parse_next(input)?;
|
||||
Ok(pairs.into_iter().collect())
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 key-value pair.
|
||||
fn gemma4_pair(input: &mut Gemma4Input<'_>) -> ModalResult<(String, Value)> {
|
||||
let (key, value) = seq!(
|
||||
_: ws0,
|
||||
gemma4_key,
|
||||
_: ws0,
|
||||
_: literal(":"),
|
||||
_: ws0,
|
||||
gemma4_value,
|
||||
)
|
||||
.parse_next(input)?;
|
||||
Ok((key, value))
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 bare key.
|
||||
fn gemma4_key(input: &mut Gemma4Input<'_>) -> ModalResult<String> {
|
||||
let key = take_till(1.., |char: char| char == ':').parse_next(input)?.trim();
|
||||
if key.is_empty() {
|
||||
return Err(ErrMode::Cut(ContextError::new()));
|
||||
}
|
||||
Ok(key.to_string())
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 value.
|
||||
fn gemma4_value(input: &mut Gemma4Input<'_>) -> ModalResult<Value> {
|
||||
alt((
|
||||
gemma4_string.map(|value: &str| Value::String(value.to_string())),
|
||||
gemma4_object.map(Value::Object),
|
||||
gemma4_array_value.map(Value::Array),
|
||||
gemma4_bare_value,
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 string delimited by `<|"|>`.
|
||||
fn gemma4_string<'i>(input: &mut Gemma4Input<'i>) -> ModalResult<&'i str> {
|
||||
delimited(
|
||||
literal(STRING_DELIM),
|
||||
take_until(0.., STRING_DELIM),
|
||||
literal(STRING_DELIM),
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a nested Gemma4 object.
|
||||
fn gemma4_object(input: &mut Gemma4Input<'_>) -> ModalResult<Map<String, Value>> {
|
||||
delimited(literal("{"), gemma4_args, literal("}")).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 array value.
|
||||
fn gemma4_array_value(input: &mut Gemma4Input<'_>) -> ModalResult<Vec<Value>> {
|
||||
delimited(literal("["), gemma4_array_content, literal("]")).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse Gemma4 array content.
|
||||
fn gemma4_array_content(input: &mut Gemma4Input<'_>) -> ModalResult<Vec<Value>> {
|
||||
delimited(
|
||||
ws0,
|
||||
terminated(
|
||||
separated(0.., gemma4_value, comma_separator),
|
||||
opt(comma_separator),
|
||||
),
|
||||
ws0,
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 bare scalar.
|
||||
fn gemma4_bare_value(input: &mut Gemma4Input<'_>) -> ModalResult<Value> {
|
||||
take_till(1.., |char: char| matches!(char, ',' | '}' | ']'))
|
||||
.map(parse_gemma4_scalar)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 comma separator.
|
||||
fn comma_separator(input: &mut Gemma4Input<'_>) -> ModalResult<()> {
|
||||
delimited(ws0, literal(","), ws0).void().parse_next(input)
|
||||
}
|
||||
|
||||
fn parse_gemma4_scalar(value: &str) -> Value {
|
||||
let value = value.trim();
|
||||
if value.is_empty() {
|
||||
return Value::String(String::new());
|
||||
}
|
||||
if value == "true" {
|
||||
return Value::Bool(true);
|
||||
}
|
||||
if value == "false" {
|
||||
return Value::Bool(false);
|
||||
}
|
||||
if matches!(value, "null" | "none" | "nil" | "NULL" | "None" | "NIL") {
|
||||
return Value::Null;
|
||||
}
|
||||
if value.contains('.') {
|
||||
if let Ok(parsed) = value.parse::<f64>()
|
||||
&& let Some(number) = Number::from_f64(parsed)
|
||||
{
|
||||
return Value::Number(number);
|
||||
}
|
||||
} else if let Ok(parsed) = value.parse::<i64>() {
|
||||
return Value::Number(Number::from(parsed));
|
||||
}
|
||||
|
||||
Value::String(value.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
use winnow::combinator::{eof, terminated};
|
||||
use winnow::error::ErrMode;
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::Partial;
|
||||
|
||||
use super::{
|
||||
Gemma4ToolParser, ToolCallDelta, ToolParseResult, ToolParser, gemma4_args,
|
||||
gemma4_array_content,
|
||||
};
|
||||
use crate::Tool;
|
||||
|
||||
fn parse_gemma4_args(args: &str) -> super::Result<serde_json::Map<String, Value>> {
|
||||
let mut input = Partial::new(args);
|
||||
let _ = input.complete();
|
||||
match terminated(gemma4_args, eof).parse_next(&mut input) {
|
||||
Ok(value) => Ok(value),
|
||||
Err(ErrMode::Incomplete(_)) => Err(parsing_failed!("incomplete Gemma4 arguments")),
|
||||
Err(ErrMode::Backtrack(error) | ErrMode::Cut(error)) => {
|
||||
Err(parsing_failed!("{}", error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_gemma4_array(array: &str) -> super::Result<Vec<Value>> {
|
||||
let mut input = Partial::new(array);
|
||||
let _ = input.complete();
|
||||
match terminated(gemma4_array_content, eof).parse_next(&mut input) {
|
||||
Ok(value) => Ok(value),
|
||||
Err(ErrMode::Incomplete(_)) => Err(parsing_failed!("incomplete Gemma4 array")),
|
||||
Err(ErrMode::Backtrack(error) | ErrMode::Cut(error)) => {
|
||||
Err(parsing_failed!("{}", error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_tools() -> Vec<Tool> {
|
||||
vec![
|
||||
Tool {
|
||||
name: "get_weather".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object" }),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "get_time".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object" }),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "write_file".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object" }),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "Edit".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object" }),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "search".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object" }),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "set".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object" }),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "get_status".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object" }),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "todowrite".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object" }),
|
||||
strict: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn collect_stream(chunks: &[&str]) -> ToolParseResult {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
let mut result = ToolParseResult::default();
|
||||
for chunk in chunks {
|
||||
result.append(parser.push(chunk).unwrap());
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
result.coalesce_calls()
|
||||
}
|
||||
|
||||
fn first_call(result: &ToolParseResult) -> &ToolCallDelta {
|
||||
result.calls.first().expect("expected one tool call")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_parse_args_handles_scalars_and_nested_values() {
|
||||
let parsed = parse_gemma4_args(
|
||||
"name:<|\"|>test<|\"|>,count:42,active:true,score:114.514,nested:{inner:<|\"|>value<|\"|>},items:[<|\"|>a<|\"|>,<|\"|>b<|\"|>]",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
Value::Object(parsed),
|
||||
json!({
|
||||
"name": "test",
|
||||
"count": 42,
|
||||
"active": true,
|
||||
"score": 114.514,
|
||||
"nested": { "inner": "value" },
|
||||
"items": ["a", "b"],
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_parse_args_handles_empty_arguments() {
|
||||
let parsed = parse_gemma4_args("").unwrap();
|
||||
assert_eq!(Value::Object(parsed), json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_parse_array_handles_bare_values() {
|
||||
let parsed = parse_gemma4_array("42,true,114.514").unwrap();
|
||||
assert_eq!(Value::Array(parsed), json!([42, true, 114.514]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete("<|tool_call>call:get_weather{location:<|\"|>London<|\"|>}<tool_call|>")
|
||||
.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_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({ "location": "London" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_parse_complete_rejects_incomplete_tool_call() {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
let error = parser
|
||||
.parse_complete("<|tool_call>call:get_weather{location:<|\"|>London")
|
||||
.unwrap_err();
|
||||
|
||||
assert!(error.to_report_string().contains("incomplete Gemma4 tool call"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_basic_single_tool_call() {
|
||||
let result = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:get_weather{",
|
||||
"location:<|\"|>Paris",
|
||||
", France",
|
||||
"<|\"|>}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({ "location": "Paris, France" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_text_before_and_after_tool_call() {
|
||||
let result = collect_stream(&[
|
||||
"Let me check ",
|
||||
"the weather. ",
|
||||
"<|tool_call>",
|
||||
"call:get_weather{",
|
||||
"location:<|\"|>London<|\"|>}",
|
||||
"<tool_call|><",
|
||||
"div>",
|
||||
]);
|
||||
|
||||
assert_eq!(result.normal_text, "Let me check the weather. <div>");
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({ "location": "London" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_waits_for_complete_tool_call() {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
let mut result = ToolParseResult::default();
|
||||
|
||||
for chunk in [
|
||||
"<|tool_call>",
|
||||
"call:get_weather{",
|
||||
"location:<|\"|>Paris<|\"|>}",
|
||||
] {
|
||||
result.append(parser.push(chunk).unwrap());
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
result.append(parser.push("<tool_call|>").unwrap());
|
||||
let result = result.coalesce_calls();
|
||||
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({ "location": "Paris" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_boolean_split_across_chunks() {
|
||||
let result = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:search{input:{all:tru",
|
||||
"e}}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("search"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).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}", "<tool_call|>"]);
|
||||
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("set"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).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}", "<tool_call|>"]);
|
||||
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("set"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({ "count": 42 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_split_string_delimiter() {
|
||||
let result = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:todowrite{",
|
||||
"content:<|\"|>Buy milk<|",
|
||||
"\"|>}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("todowrite"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({ "content": "Buy milk" })
|
||||
);
|
||||
assert!(!first_call(&result).arguments.contains("<|"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_end_marker_literal_inside_string() {
|
||||
let result = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:todowrite{",
|
||||
"content:<|\"|>literal }<tool_call|> inside",
|
||||
"<|\"|>}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("todowrite"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({ "content": "literal }<tool_call|> inside" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_html_argument_without_duplication() {
|
||||
let result = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:write_file{",
|
||||
"path:<|\"|>index.html<|\"|>,",
|
||||
"content:<|\"|><!DOCTYPE html>\n<",
|
||||
"html lang=\"zh-CN\">\n<",
|
||||
"head>\n <",
|
||||
"meta charset=\"UTF-8\">\n <",
|
||||
"meta name=\"viewport\" content=\"width=device-width\">\n",
|
||||
"<|\"|>}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("write_file"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({
|
||||
"path": "index.html",
|
||||
"content": "<!DOCTYPE html>\n<html lang=\"zh-CN\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width\">\n",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_trailing_bare_bool_is_not_duplicated() {
|
||||
let result = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:Edit{",
|
||||
"file_path:<|\"|>src/env.py<|\"|>,",
|
||||
"old_string:<|\"|>old_val<|\"|>,",
|
||||
"new_string:<|\"|>new_val<|\"|>,",
|
||||
"replace_all:",
|
||||
"false}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&result).name.as_deref(), Some("Edit"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&result).arguments).unwrap(),
|
||||
json!({
|
||||
"file_path": "src/env.py",
|
||||
"old_string": "old_val",
|
||||
"new_string": "new_val",
|
||||
"replace_all": false,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
first_call(&result).arguments.matches("replace_all").count(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[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());
|
||||
|
||||
assert_eq!(result.normal_text, "<");
|
||||
assert!(result.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();
|
||||
}
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
assert!(error.to_report_string().contains("incomplete Gemma4 tool call"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
use super::{GlmXmlToolParser, Separator};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
/// Tool parser for GLM-4.5/4.6 MoE XML-style tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <tool_call>get_weather
|
||||
/// <arg_key>city</arg_key>
|
||||
/// <arg_value>Hangzhou</arg_value>
|
||||
/// </tool_call>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are emitted only after a full `tool_call` block is parsed.
|
||||
pub struct Glm45MoeToolParser(GlmXmlToolParser);
|
||||
|
||||
impl Glm45MoeToolParser {
|
||||
/// Create a GLM-4.5/4.6 MoE tool parser.
|
||||
pub(super) fn new(tools: &[Tool]) -> Self {
|
||||
Self(GlmXmlToolParser::new(tools, Separator::Newline))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Glm45MoeToolParser {
|
||||
/// Create a boxed GLM-4.5/4.6 MoE tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the GLM MoE parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
self.0.push(chunk)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.0.finish()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
use super::{GlmXmlToolParser, Separator};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
/// Tool parser for GLM-4.7 MoE XML-style tool calls.
|
||||
///
|
||||
/// GLM-4.7 reuses the GLM-4.5 parser with a more flexible function-name
|
||||
/// separator, so the name may be followed by whitespace, a newline, or the
|
||||
/// first `<arg_key>` tag directly.
|
||||
pub struct Glm47MoeToolParser(GlmXmlToolParser);
|
||||
|
||||
impl Glm47MoeToolParser {
|
||||
fn new(tools: &[Tool]) -> Self {
|
||||
Self(GlmXmlToolParser::new(tools, Separator::Flexible))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Glm47MoeToolParser {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
self.0.push(chunk)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.0.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{Glm47MoeToolParser, ToolParser};
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
|
||||
fn glm47_tool_call(function_name: &str, params: &[(&str, &str)]) -> String {
|
||||
let params = params
|
||||
.iter()
|
||||
.map(|(name, value)| format!("<arg_key>{name}</arg_key><arg_value>{value}</arg_value>"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("");
|
||||
format!("<tool_call>{function_name}{params}</tool_call>")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm47_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = Glm47MoeToolParser::new(&test_tools());
|
||||
let output = format!(
|
||||
"Let me search for that.\n{}",
|
||||
glm47_tool_call(
|
||||
"get_weather",
|
||||
&[("city", "Beijing"), ("date", "2024-12-25")]
|
||||
)
|
||||
);
|
||||
|
||||
let result = 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!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({"city": "Beijing", "date": "2024-12-25"})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm47_streaming_extracts_multiple_tool_calls() {
|
||||
let mut parser = Glm47MoeToolParser::new(&test_tools());
|
||||
let output = format!(
|
||||
"{}{}",
|
||||
glm47_tool_call("get_weather", &[("city", "Shanghai")]),
|
||||
glm47_tool_call("add", &[("x", "1"), ("y", "2")])
|
||||
);
|
||||
|
||||
let chunks = split_by_chars(&output, 7);
|
||||
let result = 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!(
|
||||
serde_json::from_str::<Value>(&result.calls[1].arguments).unwrap(),
|
||||
json!({"x": 1, "y": 2})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm47_parse_complete_converts_schema_types() {
|
||||
let mut parser = Glm47MoeToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&glm47_tool_call(
|
||||
"convert",
|
||||
&[
|
||||
("whole", "42"),
|
||||
("flag", "true"),
|
||||
("payload", r#"{"nested":{"key":"value"}}"#),
|
||||
("items", "[1, 2, 3]"),
|
||||
("empty", ""),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": 42,
|
||||
"flag": true,
|
||||
"payload": {"nested": {"key": "value"}},
|
||||
"items": [1, 2, 3],
|
||||
"empty": ""
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm47_parse_complete_extracts_zero_argument_call() {
|
||||
let mut parser = Glm47MoeToolParser::new(&test_tools());
|
||||
|
||||
let result = parser.parse_complete("<tool_call>add</tool_call>").unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("add"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,435 @@
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::{alt, eof, repeat, seq, terminated};
|
||||
use winnow::prelude::*;
|
||||
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, xml_unescape};
|
||||
use super::{Result, ToolCallDelta, ToolParseResult};
|
||||
use crate::Tool;
|
||||
|
||||
mod glm45_moe;
|
||||
mod glm47_moe;
|
||||
|
||||
pub use glm45_moe::Glm45MoeToolParser;
|
||||
pub use glm47_moe::Glm47MoeToolParser;
|
||||
|
||||
const TOOL_CALL_START: &str = "<tool_call>";
|
||||
const TOOL_CALL_END: &str = "</tool_call>";
|
||||
const ARG_KEY_START: &str = "<arg_key>";
|
||||
const ARG_KEY_END: &str = "</arg_key>";
|
||||
const ARG_VALUE_START: &str = "<arg_value>";
|
||||
const ARG_VALUE_END: &str = "</arg_value>";
|
||||
|
||||
type GlmInput<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum GlmMode {
|
||||
Text,
|
||||
ToolCall,
|
||||
AfterToolCall,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Separator {
|
||||
/// GLM-4.5/4.6 format: function name must end at a newline before
|
||||
/// arguments.
|
||||
Newline,
|
||||
/// GLM-4.7 format: function name may end at whitespace or directly before
|
||||
/// `<arg_key>`.
|
||||
Flexible,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum GlmEvent {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolCallStart,
|
||||
ToolCall {
|
||||
name: String,
|
||||
raw_params: Vec<(String, String)>,
|
||||
},
|
||||
IgnoredRest,
|
||||
}
|
||||
|
||||
/// Tool parser core for GLM XML-style tool calls.
|
||||
struct GlmXmlToolParser {
|
||||
buffer: String,
|
||||
mode: GlmMode,
|
||||
emitted_tool_count: usize,
|
||||
tool_parameters: ToolSchemas,
|
||||
separator: Separator,
|
||||
}
|
||||
|
||||
impl GlmXmlToolParser {
|
||||
/// Create a GLM XML tool parser with a function-name separator.
|
||||
fn new(tools: &[Tool], separator: Separator) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: GlmMode::Text,
|
||||
emitted_tool_count: 0,
|
||||
tool_parameters: ToolSchemas::from_tools(tools),
|
||||
separator,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed GLM event to parser state and output.
|
||||
fn apply_event(&mut self, event: GlmEvent, result: &mut ToolParseResult) -> Result<()> {
|
||||
match event {
|
||||
GlmEvent::Text { len: consumed_len } => {
|
||||
result.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
GlmEvent::ToolCallStart => self.mode = GlmMode::ToolCall,
|
||||
GlmEvent::ToolCall { name, raw_params } => {
|
||||
self.mode = GlmMode::AfterToolCall;
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
GlmEvent::IgnoredRest => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset all streaming state.
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.mode = GlmMode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the GLM MoE parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::default();
|
||||
if !self.buffer.is_empty() {
|
||||
match self.mode {
|
||||
GlmMode::Text => result.normal_text.push_str(&self.buffer),
|
||||
GlmMode::ToolCall => return Err(parsing_failed!("incomplete GLM MoE tool call")),
|
||||
GlmMode::AfterToolCall => {}
|
||||
}
|
||||
}
|
||||
self.reset();
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a GLM event for the current parser mode.
|
||||
fn parse_next_glm_event(
|
||||
input: &mut GlmInput<'_>,
|
||||
mode: GlmMode,
|
||||
separator: Separator,
|
||||
) -> ModalResult<GlmEvent> {
|
||||
match mode {
|
||||
GlmMode::Text => parse_text_event(input),
|
||||
GlmMode::ToolCall => tool_call_event(input, separator),
|
||||
GlmMode::AfterToolCall => after_tool_call_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode GLM event.
|
||||
fn parse_text_event(input: &mut GlmInput<'_>) -> ModalResult<GlmEvent> {
|
||||
alt((tool_call_start_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a GLM tool-call start marker.
|
||||
fn tool_call_start_event(input: &mut GlmInput<'_>) -> ModalResult<GlmEvent> {
|
||||
literal(TOOL_CALL_START).value(GlmEvent::ToolCallStart).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next GLM marker.
|
||||
fn safe_text_event(input: &mut GlmInput<'_>) -> ModalResult<GlmEvent> {
|
||||
safe_text_len(input, TOOL_CALL_START).map(|len| GlmEvent::Text { len })
|
||||
}
|
||||
|
||||
/// Parse text after a completed GLM tool call.
|
||||
fn after_tool_call_event(input: &mut GlmInput<'_>) -> ModalResult<GlmEvent> {
|
||||
ws0.void().parse_next(input)?;
|
||||
alt((tool_call_start_event, ignored_rest_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a trailing rest after GLM tool calls.
|
||||
fn ignored_rest_event(input: &mut GlmInput<'_>) -> ModalResult<GlmEvent> {
|
||||
rest.value(GlmEvent::IgnoredRest).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a complete GLM tool call.
|
||||
fn tool_call_event(input: &mut GlmInput<'_>, separator: Separator) -> ModalResult<GlmEvent> {
|
||||
let (body,) = seq!(
|
||||
take_until(0.., TOOL_CALL_END),
|
||||
_: literal(TOOL_CALL_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
parse_tool_call_body(body, separator)
|
||||
}
|
||||
|
||||
/// Parse a GLM tool-call body.
|
||||
fn parse_tool_call_body(body: &str, separator: Separator) -> ModalResult<GlmEvent> {
|
||||
let mut input = body;
|
||||
let (name, raw_params) = match separator {
|
||||
Separator::Newline => seq!(
|
||||
_: ws0,
|
||||
parse_newline_separated_function_name,
|
||||
parse_parameters,
|
||||
_: ws0,
|
||||
_: eof,
|
||||
)
|
||||
.parse_next(&mut input)?,
|
||||
Separator::Flexible => seq!(
|
||||
_: ws0,
|
||||
parse_flexible_function_name,
|
||||
parse_parameters,
|
||||
_: ws0,
|
||||
_: eof,
|
||||
)
|
||||
.parse_next(&mut input)?,
|
||||
};
|
||||
|
||||
Ok(GlmEvent::ToolCall {
|
||||
name: name.to_string(),
|
||||
raw_params,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a GLM-4.5 newline-separated function name.
|
||||
fn parse_newline_separated_function_name<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
terminated(take_until(1.., "\n"), "\n").map(str::trim).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a GLM-4.7 whitespace-or-tag-separated function name.
|
||||
fn parse_flexible_function_name<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
terminated(
|
||||
take_while(1.., |ch: char| !ch.is_whitespace() && ch != '<'),
|
||||
ws0,
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse GLM argument key-value pairs.
|
||||
fn parse_parameters(input: &mut &str) -> ModalResult<Vec<(String, String)>> {
|
||||
repeat(0.., terminated(parse_parameter, ws0)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a GLM argument key-value pair.
|
||||
fn parse_parameter(input: &mut &str) -> ModalResult<(String, String)> {
|
||||
let (key, value) = seq!(
|
||||
_: literal(ARG_KEY_START),
|
||||
take_until(1.., ARG_KEY_END),
|
||||
_: literal(ARG_KEY_END),
|
||||
_: ws0,
|
||||
_: literal(ARG_VALUE_START),
|
||||
take_until(0.., ARG_VALUE_END).map(str::trim).map(xml_unescape),
|
||||
_: literal(ARG_VALUE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok((key.trim().to_string(), value.into_owned()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::Glm45MoeToolParser;
|
||||
use crate::ToolParser;
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
|
||||
fn glm45_tool_call(function_name: &str, params: &[(&str, &str)]) -> String {
|
||||
let params = params
|
||||
.iter()
|
||||
.map(|(name, value)| {
|
||||
format!("<arg_key>{name}</arg_key>\n<arg_value>{value}</arg_value>")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
format!("<tool_call>{function_name}\n{params}\n</tool_call>")
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
let output = format!(
|
||||
"Let me search for that.\n{}",
|
||||
glm45_tool_call(
|
||||
"get_weather",
|
||||
&[("city", "Beijing"), ("date", "2024-12-25")]
|
||||
)
|
||||
);
|
||||
|
||||
let result = 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!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({"city": "Beijing", "date": "2024-12-25"})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_streaming_extracts_multiple_tool_calls() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
let output = format!(
|
||||
"{}\n{}",
|
||||
glm45_tool_call("get_weather", &[("city", "Shanghai")]),
|
||||
glm45_tool_call("add", &[("x", "1"), ("y", "2")])
|
||||
);
|
||||
|
||||
let chunks = split_by_chars(&output, 11);
|
||||
let result = 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!(
|
||||
serde_json::from_str::<Value>(&result.calls[1].arguments).unwrap(),
|
||||
json!({"x": 1, "y": 2})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_parse_complete_unescapes_literal_closing_tags_in_arg_value() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&glm45_tool_call(
|
||||
"get_weather",
|
||||
&[
|
||||
("city", "Paris </arg_value></tool_call>"),
|
||||
("date", "2026-05-08"),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"city": "Paris </arg_value></tool_call>",
|
||||
"date": "2026-05-08",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_streaming_without_tool_call_emits_text_incrementally() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(&mut parser, &["hello ", "world"]);
|
||||
|
||||
assert_eq!(result.normal_text, "hello world");
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_streaming_preserves_prefix_text() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"Prefix ",
|
||||
&glm45_tool_call("get_weather", &[("city", "Hangzhou")]),
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(result.normal_text, "Prefix ");
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_streaming_handles_start_token_split_across_chunks() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"hello <tool",
|
||||
"_call>get_weather\n",
|
||||
"<arg_key>city</arg_key><arg_value>Paris</arg_value></tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(result.normal_text, "hello ");
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_streaming_does_not_emit_incomplete_tool_call() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
|
||||
let result = parser.push("<tool_call>get_weather\n<arg_key>city</arg_key>").unwrap();
|
||||
|
||||
assert_eq!(result.normal_text, "");
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
|
||||
parser.push("<tool_call>get_weather\n<arg_key>city</arg_key>").unwrap();
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
assert!(error.as_report().to_string().contains("incomplete GLM MoE tool call"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_malformed_tool_call_fails_fast() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
|
||||
let error = parser.push("<tool_call>get_weather<arg_key>city</arg_key><arg_value>Paris</arg_value></tool_call>").unwrap_err();
|
||||
|
||||
assert!(error.as_report().to_string().contains("tool parser parsing failed"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_streaming_ignores_trailing_text_after_tool_calls() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[&format!(
|
||||
"{}<|endoftext|>",
|
||||
glm45_tool_call("get_weather", &[("city", "Paris")])
|
||||
)],
|
||||
);
|
||||
|
||||
assert_eq!(result.normal_text, "");
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
const HERMES_CONFIG: JsonToolCallConfig = JsonToolCallConfig {
|
||||
parser_name: "Hermes",
|
||||
start_marker: "<tool_call>",
|
||||
end_marker: "</tool_call>",
|
||||
marker_whitespace: JsonToolCallWhitespace::Optional,
|
||||
delimiter: None,
|
||||
name_key: "name",
|
||||
arguments_key: "arguments",
|
||||
};
|
||||
|
||||
/// Tool parser for Hermes XML-wrapped JSON tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <tool_call>{"name": "get_weather", "arguments": {"location":"Tokyo"}}</tool_call>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are already OpenAI-style JSON text, so they are streamed as raw
|
||||
/// argument deltas without schema conversion or JSON normalization.
|
||||
///
|
||||
/// Note: parallel calls are represented as repeated
|
||||
/// `<tool_call>...</tool_call>` blocks, not as multiple calls inside one tag.
|
||||
pub struct HermesToolParser {
|
||||
inner: JsonToolCallParser,
|
||||
}
|
||||
|
||||
impl HermesToolParser {
|
||||
/// Create a Hermes tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
inner: JsonToolCallParser::new(HERMES_CONFIG),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for HermesToolParser {
|
||||
/// Create a boxed Hermes tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the Hermes parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
self.inner.push(chunk)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.inner.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::HermesToolParser;
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::{ToolParseResult, ToolParser};
|
||||
|
||||
fn build_tool_call(function_name: &str, arguments: &str) -> String {
|
||||
format!(r#"<tool_call>{{"name":"{function_name}","arguments":{arguments}}}</tool_call>"#)
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.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
|
||||
.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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_accepts_newline_after_tool_call_start() {
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(
|
||||
r#"<tool_call>
|
||||
{"name":"get_weather","arguments":{}}</tool_call>"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.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();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_streaming_emits_argument_deltas() {
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"preface <tool",
|
||||
"_call>{\"name\":\"get_weather\",\"arguments\":",
|
||||
"{\"location\":",
|
||||
"\"Beijing\"",
|
||||
"}",
|
||||
"}</tool_call> suffix",
|
||||
];
|
||||
|
||||
let mut result = ToolParseResult::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.push(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
result.append(next);
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(result.normal_text, "preface suffix");
|
||||
assert_eq!(
|
||||
result.coalesce_calls().calls[0].arguments,
|
||||
r#"{"location":"Beijing"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_streaming_handles_split_markers() {
|
||||
let input = format!(
|
||||
"hello {}",
|
||||
build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
|
||||
let result = 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"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_streaming_extracts_multiple_tool_calls() {
|
||||
let input = format!(
|
||||
"{}{}",
|
||||
build_tool_call("get_weather", r#"{"location":"Shanghai"}"#),
|
||||
build_tool_call("add", r#"{"x":1,"y":2}"#),
|
||||
);
|
||||
let chunks = split_by_chars(&input, 7);
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\":1,\"y\":2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
parser
|
||||
.push(r#"<tool_call>{"name":"get_weather","arguments":{"location""#)
|
||||
.unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete Hermes tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,487 @@
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::seq;
|
||||
use winnow::error::{ModalResult, StrContext};
|
||||
use winnow::prelude::*;
|
||||
use winnow::token::literal;
|
||||
|
||||
use super::{
|
||||
JsonToolCallConfig, JsonToolCallEvent, JsonToolCallWhitespace, JsonToolInput,
|
||||
argument_delta_event, tool_call_header_event,
|
||||
};
|
||||
use crate::utils::{JsonObjectScanState, parse_buffered_event};
|
||||
use crate::{Result, Tool, ToolCallDelta, ToolParseResult, ToolParser};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum LlamaJsonMode {
|
||||
Start,
|
||||
Header,
|
||||
Arguments { json_scan: JsonObjectScanState },
|
||||
AfterCall,
|
||||
Passthrough,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum LlamaJsonEvent {
|
||||
ToolCallHeader { function_name: String },
|
||||
Arguments { len: usize },
|
||||
ToolCallClose,
|
||||
Separator,
|
||||
}
|
||||
|
||||
/// Tool parser for strict Llama JSON-template tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// {"name":"get_weather","parameters":{"location":"Tokyo"}}; {"name":"add","parameters":{"x":1,"y":2}}
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are already OpenAI-style JSON text, so they are streamed as raw
|
||||
/// argument deltas without schema conversion or JSON normalization.
|
||||
///
|
||||
/// Natural text at the beginning of the stream permanently disables tool
|
||||
/// parsing for that assistant output.
|
||||
pub struct Llama3JsonToolParser {
|
||||
buffer: String,
|
||||
mode: LlamaJsonMode,
|
||||
active_tool_index: Option<usize>,
|
||||
emitted_tool_count: usize,
|
||||
}
|
||||
|
||||
impl Llama3JsonToolParser {
|
||||
/// Create a Llama JSON tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: LlamaJsonMode::Start,
|
||||
active_tool_index: None,
|
||||
emitted_tool_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Commit the stream to JSON parsing or permanent passthrough.
|
||||
fn commit_start(&mut self) -> bool {
|
||||
if !matches!(self.mode, LlamaJsonMode::Start) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if self.buffer.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if self.buffer.starts_with('{') {
|
||||
self.mode = LlamaJsonMode::Header;
|
||||
} else {
|
||||
self.mode = LlamaJsonMode::Passthrough;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Apply one parsed Llama JSON event to parser state and output.
|
||||
fn apply_event(&mut self, event: LlamaJsonEvent, result: &mut ToolParseResult) -> Result<()> {
|
||||
match event {
|
||||
LlamaJsonEvent::ToolCallHeader { function_name } => {
|
||||
let tool_index = self.emitted_tool_count;
|
||||
self.emitted_tool_count += 1;
|
||||
self.active_tool_index = Some(tool_index);
|
||||
self.mode = LlamaJsonMode::Arguments {
|
||||
json_scan: JsonObjectScanState::default(),
|
||||
};
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: Some(function_name),
|
||||
arguments: String::new(),
|
||||
});
|
||||
}
|
||||
LlamaJsonEvent::Arguments { len: consumed_len } => {
|
||||
let Some(tool_index) = self.active_tool_index else {
|
||||
return Err(parsing_failed!(
|
||||
"Llama JSON arguments without an active tool call"
|
||||
));
|
||||
};
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: None,
|
||||
arguments: self.buffer[..consumed_len].to_string(),
|
||||
});
|
||||
}
|
||||
LlamaJsonEvent::ToolCallClose => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = LlamaJsonMode::AfterCall;
|
||||
}
|
||||
LlamaJsonEvent::Separator => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = LlamaJsonMode::Header;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset all streaming state.
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.mode = LlamaJsonMode::Start;
|
||||
self.active_tool_index = None;
|
||||
self.emitted_tool_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Llama3JsonToolParser {
|
||||
/// Create a boxed Llama JSON tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the Llama JSON parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
self.buffer.push_str(chunk);
|
||||
let mut result = ToolParseResult::default();
|
||||
|
||||
if !self.commit_start() {
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
if matches!(self.mode, LlamaJsonMode::Passthrough) {
|
||||
result.normal_text.push_str(&self.buffer);
|
||||
self.buffer.clear();
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::default();
|
||||
match &self.mode {
|
||||
LlamaJsonMode::Start | LlamaJsonMode::Passthrough => {
|
||||
result.normal_text.push_str(&self.buffer);
|
||||
}
|
||||
LlamaJsonMode::AfterCall if self.buffer.trim().is_empty() => {}
|
||||
LlamaJsonMode::Header | LlamaJsonMode::Arguments { .. } => {
|
||||
return Err(parsing_failed!("incomplete Llama JSON tool call"));
|
||||
}
|
||||
LlamaJsonMode::AfterCall => {
|
||||
return Err(parsing_failed!("invalid Llama JSON"));
|
||||
}
|
||||
}
|
||||
self.reset();
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Llama JSON event for the current parser mode.
|
||||
fn parse_next_llama_json_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
mode: &mut LlamaJsonMode,
|
||||
) -> ModalResult<LlamaJsonEvent> {
|
||||
match mode {
|
||||
LlamaJsonMode::Start | LlamaJsonMode::Passthrough => {
|
||||
unreachable!("Llama JSON parser driver must commit before parsing events")
|
||||
}
|
||||
LlamaJsonMode::Header => llama_tool_call_header_event(input),
|
||||
LlamaJsonMode::Arguments { json_scan } => parse_llama_arguments_event(input, json_scan),
|
||||
LlamaJsonMode::AfterCall => after_call_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Llama JSON tool-call header.
|
||||
fn llama_tool_call_header_event(input: &mut JsonToolInput<'_>) -> ModalResult<LlamaJsonEvent> {
|
||||
const CONFIG: JsonToolCallConfig = JsonToolCallConfig {
|
||||
parser_name: "Llama JSON",
|
||||
start_marker: "",
|
||||
end_marker: "",
|
||||
marker_whitespace: JsonToolCallWhitespace::Optional,
|
||||
delimiter: Some(";"),
|
||||
name_key: "name",
|
||||
arguments_key: "parameters",
|
||||
};
|
||||
|
||||
match tool_call_header_event(input, CONFIG)? {
|
||||
JsonToolCallEvent::ToolCallHeader { function_name } => {
|
||||
Ok(LlamaJsonEvent::ToolCallHeader { function_name })
|
||||
}
|
||||
_ => unreachable!("tool_call_header_event only emits ToolCallHeader"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse one event inside a Llama JSON arguments payload.
|
||||
fn parse_llama_arguments_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
json_scan: &mut JsonObjectScanState,
|
||||
) -> ModalResult<LlamaJsonEvent> {
|
||||
if json_scan.complete() {
|
||||
tool_call_close_event(input)
|
||||
} else {
|
||||
match argument_delta_event(input, json_scan)? {
|
||||
JsonToolCallEvent::Arguments { len } => Ok(LlamaJsonEvent::Arguments { len }),
|
||||
_ => unreachable!("argument_delta_event only emits Arguments"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the outer closing brace for one Llama JSON tool call.
|
||||
fn tool_call_close_event(input: &mut JsonToolInput<'_>) -> ModalResult<LlamaJsonEvent> {
|
||||
literal("}").value(LlamaJsonEvent::ToolCallClose).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a semicolon separator after one Llama JSON tool call.
|
||||
fn after_call_event(input: &mut JsonToolInput<'_>) -> ModalResult<LlamaJsonEvent> {
|
||||
seq!(
|
||||
_: ws0,
|
||||
_: literal(";"),
|
||||
_: ws0,
|
||||
)
|
||||
.value(LlamaJsonEvent::Separator)
|
||||
.context(StrContext::Label("Llama JSON"))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::Llama3JsonToolParser;
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::{ToolParseResult, ToolParser};
|
||||
|
||||
fn build_tool_call(function_name: &str, parameters: &str) -> String {
|
||||
format!(r#"{{"name":"{function_name}","parameters":{parameters}}}"#)
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.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(),
|
||||
);
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(
|
||||
result.normal_text,
|
||||
r#"plain text first {"name":"get_weather","parameters":{"location":"Tokyo"}}"#
|
||||
);
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_does_not_support_python_tag_prefix() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let input = format!(
|
||||
"<|python_tag|>{}",
|
||||
build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)
|
||||
);
|
||||
let result = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(result.normal_text, input);
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_rejects_leading_whitespace_before_tool_call() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let input = format!(
|
||||
"\n {}",
|
||||
build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)
|
||||
);
|
||||
let result = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(result.normal_text, input);
|
||||
assert!(result.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();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_rejects_arguments_key() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let error = parser
|
||||
.parse_complete(r#"{"name":"get_weather","arguments":{"location":"Tokyo"}}"#)
|
||||
.unwrap_err();
|
||||
|
||||
expect![[r#"
|
||||
tool parser parsing failed: invalid Llama JSON
|
||||
expected `parameters`"#]]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_extracts_multiple_semicolon_separated_calls() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let input = format!(
|
||||
"{} \n; {}",
|
||||
build_tool_call("get_weather", r#"{"location":"Shanghai"}"#),
|
||||
build_tool_call("add", r#"{"x":1,"y":2}"#),
|
||||
);
|
||||
let result = parser.parse_complete(&input).unwrap();
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\":1,\"y\":2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_streaming_emits_argument_deltas() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"{\"name\":\"get_weather\",\"parameters\":",
|
||||
"{\"location\":",
|
||||
"\"Beijing\"",
|
||||
"}}",
|
||||
];
|
||||
|
||||
let mut result = ToolParseResult::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.push(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
result.append(next);
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(
|
||||
result.coalesce_calls().calls[0].arguments,
|
||||
r#"{"location":"Beijing"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_streaming_handles_split_objects_and_separator() {
|
||||
let input = format!(
|
||||
"{};{}",
|
||||
build_tool_call("get_weather", r#"{"location":"Dallas","state":"TX"}"#),
|
||||
build_tool_call("add", r#"{"x":4,"y":5}"#),
|
||||
);
|
||||
let chunks = split_by_chars(&input, 6);
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(result.normal_text, "");
|
||||
assert_eq!(result.calls.len(), 2);
|
||||
assert_eq!(
|
||||
result.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}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_handles_nested_multiline_and_escaped_string_parameters() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let arguments = r#"{
|
||||
"payload": {"items": [1, {"value": "literal { brace } and \"quote\""}]},
|
||||
"flag": true
|
||||
}"#;
|
||||
let result = parser.parse_complete(&build_tool_call("convert", arguments)).unwrap();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_keeps_trailing_whitespace_after_tool_call() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let result = 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);
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete Llama JSON tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
expect![[r#"
|
||||
tool parser parsing failed: invalid Llama JSON
|
||||
expected `name`"#]]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_trailing_non_separator_content_errors() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let error = parser
|
||||
.push(&format!(
|
||||
"{} trailing",
|
||||
build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)
|
||||
))
|
||||
.unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: invalid Llama JSON"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
const MISTRAL_CONFIG: JsonToolCallConfig = JsonToolCallConfig {
|
||||
parser_name: "Mistral",
|
||||
start_marker: "[TOOL_CALLS] [",
|
||||
end_marker: "]",
|
||||
marker_whitespace: JsonToolCallWhitespace::Optional,
|
||||
delimiter: Some(","),
|
||||
name_key: "name",
|
||||
arguments_key: "arguments",
|
||||
};
|
||||
|
||||
/// Tool parser for Mistral JSON-array tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// [TOOL_CALLS] [{"name": "get_weather", "arguments": {"location":"Tokyo"}}]
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are already OpenAI-style JSON text, so they are streamed as raw
|
||||
/// argument deltas without schema conversion or JSON normalization.
|
||||
pub struct MistralToolParser {
|
||||
inner: JsonToolCallParser,
|
||||
}
|
||||
|
||||
impl MistralToolParser {
|
||||
/// Create a Mistral tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
inner: JsonToolCallParser::new(MISTRAL_CONFIG),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for MistralToolParser {
|
||||
/// Create a boxed Mistral tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the Mistral parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
self.inner.push(chunk)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.inner.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::MistralToolParser;
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::{ToolParseResult, ToolParser};
|
||||
|
||||
fn build_tool_call(function_name: &str, arguments: &str) -> String {
|
||||
format!(r#"{{"name":"{function_name}","arguments":{arguments}}}"#)
|
||||
}
|
||||
|
||||
fn build_tool_calls(tool_calls: &[String]) -> String {
|
||||
format!("[TOOL_CALLS] [{}]", tool_calls.join(","))
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.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
|
||||
.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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_parse_complete_extracts_pretty_multiple_tool_calls() {
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(
|
||||
r#"I'll help.
|
||||
[TOOL_CALLS] [
|
||||
{"name": "get_weather", "arguments": {"city": "Tokyo", "units": "celsius"}}
|
||||
,
|
||||
{"name": "add", "arguments": {"x": 1, "y": 2}}
|
||||
]"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "I'll help.\n",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"city\": \"Tokyo\", \"units\": \"celsius\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\": 1, \"y\": 2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo",}"#;
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_calls(&[build_tool_call(
|
||||
"get_weather",
|
||||
arguments,
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_streaming_emits_argument_deltas() {
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"preface [TOOL",
|
||||
"_CALLS] [{\"name\":\"get_weather\",\"arguments\":",
|
||||
"{\"location\":",
|
||||
"\"Beijing\"",
|
||||
"}",
|
||||
"}] suffix",
|
||||
];
|
||||
|
||||
let mut result = ToolParseResult::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.push(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
result.append(next);
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(result.normal_text, "preface suffix");
|
||||
assert_eq!(
|
||||
result.coalesce_calls().calls[0].arguments,
|
||||
r#"{"location":"Beijing"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_streaming_handles_split_markers() {
|
||||
let input = format!(
|
||||
"hello {}",
|
||||
build_tool_calls(&[build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)])
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
|
||||
let result = 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"}"#);
|
||||
}
|
||||
|
||||
#[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
|
||||
.parse_complete(&build_tool_calls(&[build_tool_call("echo", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.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""#)
|
||||
.unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete Mistral tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
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"}]"#)
|
||||
.unwrap_err();
|
||||
|
||||
expect![[r#"
|
||||
tool parser parsing failed: invalid Mistral
|
||||
expected `name`"#]]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,465 @@
|
||||
//! Shared parser core for JSON tool calls wrapped by text markers.
|
||||
|
||||
pub use hermes::HermesToolParser;
|
||||
pub use llama::Llama3JsonToolParser;
|
||||
pub use mistral::MistralToolParser;
|
||||
pub use qwen::Qwen3XmlToolParser;
|
||||
|
||||
mod hermes;
|
||||
mod llama;
|
||||
mod mistral;
|
||||
mod qwen;
|
||||
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::{alt, seq};
|
||||
use winnow::error::{ModalResult, StrContext, StrContextValue};
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::Partial;
|
||||
use winnow::token::literal;
|
||||
|
||||
use super::utils::{
|
||||
JsonObjectScanState, json_str, parse_buffered_event, safe_text_len, take_json_object,
|
||||
};
|
||||
use super::{Result, ToolCallDelta, ToolParseResult};
|
||||
|
||||
type JsonToolInput<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct JsonToolCallConfig {
|
||||
parser_name: &'static str,
|
||||
start_marker: &'static str,
|
||||
end_marker: &'static str,
|
||||
marker_whitespace: JsonToolCallWhitespace,
|
||||
delimiter: Option<&'static str>,
|
||||
name_key: &'static str,
|
||||
arguments_key: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum JsonToolCallWhitespace {
|
||||
Optional,
|
||||
Exact(&'static str),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum JsonToolCallMode {
|
||||
Text,
|
||||
Header,
|
||||
Arguments { json_scan: JsonObjectScanState },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum JsonToolCallEvent {
|
||||
Text { len: usize },
|
||||
ToolCallStart,
|
||||
ToolCallHeader { function_name: String },
|
||||
Arguments { len: usize },
|
||||
ToolCallDelimiter,
|
||||
ToolCallEnd,
|
||||
}
|
||||
|
||||
/// Tool parser core for marker-wrapped JSON tool calls.
|
||||
#[derive(Debug)]
|
||||
struct JsonToolCallParser {
|
||||
config: JsonToolCallConfig,
|
||||
buffer: String,
|
||||
mode: JsonToolCallMode,
|
||||
active_tool_index: Option<usize>,
|
||||
emitted_tool_count: usize,
|
||||
}
|
||||
|
||||
impl JsonToolCallParser {
|
||||
/// Create a marker-wrapped JSON tool-call parser.
|
||||
fn new(config: JsonToolCallConfig) -> Self {
|
||||
Self {
|
||||
config,
|
||||
buffer: String::new(),
|
||||
mode: JsonToolCallMode::Text,
|
||||
active_tool_index: None,
|
||||
emitted_tool_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the JSON tool-call parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::default();
|
||||
match &self.mode {
|
||||
JsonToolCallMode::Text => result.normal_text.push_str(&self.buffer),
|
||||
JsonToolCallMode::Header | JsonToolCallMode::Arguments { .. } => {
|
||||
return Err(parsing_failed!(
|
||||
"incomplete {} tool call",
|
||||
self.config.parser_name
|
||||
));
|
||||
}
|
||||
}
|
||||
self.reset();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Apply one parsed JSON tool-call event to parser state and output.
|
||||
fn apply_event(
|
||||
&mut self,
|
||||
event: JsonToolCallEvent,
|
||||
result: &mut ToolParseResult,
|
||||
) -> Result<()> {
|
||||
match event {
|
||||
JsonToolCallEvent::Text { len: consumed_len } => {
|
||||
result.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
JsonToolCallEvent::ToolCallStart => self.mode = JsonToolCallMode::Header,
|
||||
JsonToolCallEvent::ToolCallHeader { function_name } => {
|
||||
let tool_index = self.emitted_tool_count;
|
||||
self.emitted_tool_count += 1;
|
||||
self.active_tool_index = Some(tool_index);
|
||||
self.mode = JsonToolCallMode::Arguments {
|
||||
json_scan: JsonObjectScanState::default(),
|
||||
};
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: Some(function_name),
|
||||
arguments: String::new(),
|
||||
});
|
||||
}
|
||||
JsonToolCallEvent::Arguments { len: consumed_len } => {
|
||||
let Some(tool_index) = self.active_tool_index else {
|
||||
return Err(parsing_failed!(
|
||||
"{} arguments without an active tool call",
|
||||
self.config.parser_name
|
||||
));
|
||||
};
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: None,
|
||||
arguments: self.buffer[..consumed_len].to_string(),
|
||||
});
|
||||
}
|
||||
JsonToolCallEvent::ToolCallDelimiter => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = JsonToolCallMode::Header;
|
||||
}
|
||||
JsonToolCallEvent::ToolCallEnd => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = JsonToolCallMode::Text;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset all streaming state.
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.mode = JsonToolCallMode::Text;
|
||||
self.active_tool_index = None;
|
||||
self.emitted_tool_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a JSON tool-call event for the current parser mode.
|
||||
fn parse_next_json_tool_call_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
mode: &mut JsonToolCallMode,
|
||||
config: JsonToolCallConfig,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
match mode {
|
||||
JsonToolCallMode::Text => parse_text_event(input, config),
|
||||
JsonToolCallMode::Header => tool_call_header_event(input, config),
|
||||
JsonToolCallMode::Arguments { json_scan } => {
|
||||
parse_arguments_event(input, json_scan, config)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode JSON tool-call event.
|
||||
fn parse_text_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
config: JsonToolCallConfig,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
alt((
|
||||
|input: &mut JsonToolInput<'_>| tool_call_start_event(input, config),
|
||||
|input: &mut JsonToolInput<'_>| safe_text_event(input, config),
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a marker-wrapped JSON tool-call start marker.
|
||||
fn tool_call_start_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
config: JsonToolCallConfig,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
seq!(
|
||||
_: literal(config.start_marker),
|
||||
_: |input: &mut JsonToolInput<'_>| marker_whitespace(input, config),
|
||||
)
|
||||
.value(JsonToolCallEvent::ToolCallStart)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a marker-wrapped JSON tool-call header before the raw arguments
|
||||
/// payload.
|
||||
fn tool_call_header_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
config: JsonToolCallConfig,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
let (function_name,) = seq!(
|
||||
_: ws0,
|
||||
_: literal("{"),
|
||||
_: ws0,
|
||||
_: |input: &mut JsonToolInput<'_>| json_key(input, config.name_key),
|
||||
_: ws0,
|
||||
_: literal(":"),
|
||||
_: ws0,
|
||||
json_str,
|
||||
_: ws0,
|
||||
_: literal(","),
|
||||
_: ws0,
|
||||
_: |input: &mut JsonToolInput<'_>| json_key(input, config.arguments_key),
|
||||
_: ws0,
|
||||
_: literal(":"),
|
||||
_: ws0,
|
||||
)
|
||||
.context(StrContext::Label(config.parser_name))
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok(JsonToolCallEvent::ToolCallHeader { function_name })
|
||||
}
|
||||
|
||||
/// Parse a configured JSON object key.
|
||||
fn json_key(input: &mut JsonToolInput<'_>, key: &'static str) -> ModalResult<()> {
|
||||
seq!(
|
||||
_: literal("\""),
|
||||
_: literal(key).context(StrContext::Expected(StrContextValue::StringLiteral(key))),
|
||||
_: literal("\""),
|
||||
)
|
||||
.void()
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse one event inside a marker-wrapped JSON tool-call arguments payload.
|
||||
fn parse_arguments_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
json_scan: &mut JsonObjectScanState,
|
||||
config: JsonToolCallConfig,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
if json_scan.complete() {
|
||||
tool_call_close_event(input, config)
|
||||
} else {
|
||||
argument_delta_event(input, json_scan)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a raw JSON arguments delta.
|
||||
fn argument_delta_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
json_scan: &mut JsonObjectScanState,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
take_json_object(input, json_scan).map(|len| JsonToolCallEvent::Arguments { len })
|
||||
}
|
||||
|
||||
/// Parse a marker-wrapped JSON tool-call close marker.
|
||||
fn tool_call_close_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
config: JsonToolCallConfig,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
let _ = literal("}").parse_next(input)?;
|
||||
|
||||
match config.delimiter {
|
||||
Some(delimiter) => alt((
|
||||
|input: &mut JsonToolInput<'_>| tool_call_end_event(input, config),
|
||||
|input: &mut JsonToolInput<'_>| tool_call_delimiter_event(input, delimiter),
|
||||
))
|
||||
.parse_next(input),
|
||||
None => tool_call_end_event(input, config),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a marker-wrapped JSON tool-call end marker.
|
||||
fn tool_call_end_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
config: JsonToolCallConfig,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
seq!(
|
||||
_: |input: &mut JsonToolInput<'_>| marker_whitespace(input, config),
|
||||
_: literal(config.end_marker),
|
||||
)
|
||||
.value(JsonToolCallEvent::ToolCallEnd)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a delimiter between JSON tool calls inside one marker block.
|
||||
fn tool_call_delimiter_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
delimiter: &'static str,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
seq!(
|
||||
_: ws0,
|
||||
_: literal(delimiter),
|
||||
_: ws0,
|
||||
)
|
||||
.value(JsonToolCallEvent::ToolCallDelimiter)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse configured whitespace around a marker-wrapped JSON tool call.
|
||||
fn marker_whitespace(input: &mut JsonToolInput<'_>, config: JsonToolCallConfig) -> ModalResult<()> {
|
||||
match config.marker_whitespace {
|
||||
JsonToolCallWhitespace::Optional => ws0.void().parse_next(input),
|
||||
JsonToolCallWhitespace::Exact(whitespace) => literal(whitespace).void().parse_next(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next marker-wrapped JSON tool call.
|
||||
fn safe_text_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
config: JsonToolCallConfig,
|
||||
) -> ModalResult<JsonToolCallEvent> {
|
||||
safe_text_len(input, config.start_marker).map(|len| JsonToolCallEvent::Text { len })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::ToolParseResult;
|
||||
|
||||
const DELIMITED_CONFIG: JsonToolCallConfig = JsonToolCallConfig {
|
||||
parser_name: "Delimited JSON",
|
||||
start_marker: "<tool_calls>",
|
||||
end_marker: "</tool_calls>",
|
||||
marker_whitespace: JsonToolCallWhitespace::Optional,
|
||||
delimiter: Some("<"),
|
||||
name_key: "function",
|
||||
arguments_key: "parameters",
|
||||
};
|
||||
|
||||
fn build_tool_call(function_name: &str, arguments: &str) -> String {
|
||||
format!(r#"{{"function":"{function_name}","parameters":{arguments}}}"#)
|
||||
}
|
||||
|
||||
fn build_tool_calls(tool_calls: &[String]) -> String {
|
||||
format!("<tool_calls>{}</tool_calls>", tool_calls.join(" <\n"))
|
||||
}
|
||||
|
||||
fn collect_chunks(parser: &mut JsonToolCallParser, chunks: &[&str]) -> ToolParseResult {
|
||||
let mut result = ToolParseResult::default();
|
||||
for chunk in chunks {
|
||||
result.append(parser.push(chunk).unwrap());
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
result.coalesce_calls()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_tool_call_delimiter_extracts_multiple_calls_in_one_block() {
|
||||
let input = build_tool_calls(&[
|
||||
build_tool_call("get_weather", r#"{"location":"Shanghai"}"#),
|
||||
build_tool_call("add", r#"{"x":1,"y":2}"#),
|
||||
]);
|
||||
let mut parser = JsonToolCallParser::new(DELIMITED_CONFIG);
|
||||
|
||||
let result = collect_chunks(&mut parser, &[&input]);
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\":1,\"y\":2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_tool_call_delimiter_can_arrive_in_later_chunk() {
|
||||
let mut parser = JsonToolCallParser::new(DELIMITED_CONFIG);
|
||||
let chunks = [
|
||||
r#"<tool_calls>{"function":"get_weather","parameters":{"location":"Shanghai"}}"#,
|
||||
" <\n",
|
||||
r#"{"function":"add","parameters":{"x":1,"y":2}}"#,
|
||||
"</tool_calls>",
|
||||
];
|
||||
|
||||
let result = collect_chunks(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\":1,\"y\":2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_tool_call_end_marker_wins_over_delimiter_prefix() {
|
||||
let mut parser = JsonToolCallParser::new(DELIMITED_CONFIG);
|
||||
let chunks = [
|
||||
r#"<tool_calls>{"function":"get_weather","parameters":{"location":"Shanghai"}}"#,
|
||||
" ",
|
||||
"</tool_calls> trailing text",
|
||||
];
|
||||
|
||||
let result = collect_chunks(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: " trailing text",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::{Result, Tool, ToolParseResult, ToolParser};
|
||||
|
||||
const QWEN_XML_CONFIG: JsonToolCallConfig = JsonToolCallConfig {
|
||||
parser_name: "Qwen XML",
|
||||
start_marker: "<tool_call>",
|
||||
end_marker: "</tool_call>",
|
||||
marker_whitespace: JsonToolCallWhitespace::Exact("\n"),
|
||||
delimiter: None,
|
||||
name_key: "name",
|
||||
arguments_key: "arguments",
|
||||
};
|
||||
|
||||
/// Tool parser for Qwen XML-wrapped JSON tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <tool_call>
|
||||
/// {"name": "get_weather", "arguments": {"location":"Tokyo"}}
|
||||
/// </tool_call>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are already OpenAI-style JSON text, so they are streamed as raw
|
||||
/// argument deltas without schema conversion or JSON normalization.
|
||||
///
|
||||
/// Note: parallel calls are represented as repeated
|
||||
/// `<tool_call>...</tool_call>` blocks, not as multiple calls inside one tag.
|
||||
pub struct Qwen3XmlToolParser {
|
||||
inner: JsonToolCallParser,
|
||||
}
|
||||
|
||||
impl Qwen3XmlToolParser {
|
||||
/// Create a Qwen XML tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
inner: JsonToolCallParser::new(QWEN_XML_CONFIG),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Qwen3XmlToolParser {
|
||||
/// Create a boxed Qwen XML tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the Qwen XML parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
self.inner.push(chunk)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
self.inner.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::Qwen3XmlToolParser;
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::{ToolParseResult, ToolParser};
|
||||
|
||||
fn build_tool_call(function_name: &str, arguments: &str) -> String {
|
||||
format!(
|
||||
"<tool_call>\n{{\"name\": \"{function_name}\", \"arguments\": {arguments}}}\n</tool_call>"
|
||||
)
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.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
|
||||
.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);
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_streaming_emits_argument_deltas() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"<tool_call>",
|
||||
"\n{\"name\": \"get_weather\", \"arguments\": ",
|
||||
"{\"location\":",
|
||||
"\"Beijing\"",
|
||||
"}",
|
||||
"}\n</tool_call>",
|
||||
];
|
||||
|
||||
let mut result = ToolParseResult::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.push(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
result.append(next);
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(
|
||||
result.coalesce_calls().calls[0].arguments,
|
||||
r#"{"location":"Beijing"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_streaming_handles_split_markers() {
|
||||
let input = format!(
|
||||
"hello {}",
|
||||
build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
|
||||
let result = 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"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_keeps_end_marker_literal_inside_json_string() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let arguments = r#"{"text":"literal </tool_call> inside"}"#;
|
||||
let result = parser.parse_complete(&build_tool_call("echo", arguments)).unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_decodes_escaped_function_name() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(
|
||||
r#"<tool_call>
|
||||
{"name":"say_\"hi","arguments":{}}
|
||||
</tool_call>"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("say_\"hi"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_requires_newline_after_tool_call_start() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let input = r#"<tool_call>{"name":"get_weather","arguments":{}}
|
||||
</tool_call>"#;
|
||||
|
||||
let result = parser.parse_complete(input).unwrap();
|
||||
|
||||
assert_eq!(result.normal_text, input);
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_requires_newline_before_tool_call_end() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let error = parser
|
||||
.parse_complete(
|
||||
r#"<tool_call>
|
||||
{"name":"get_weather","arguments":{}}</tool_call>"#,
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(error.to_report_string().starts_with("tool parser parsing failed:"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_streaming_extracts_multiple_tool_calls() {
|
||||
let input = format!(
|
||||
"{}{}",
|
||||
build_tool_call("get_weather", r#"{"location":"Shanghai"}"#),
|
||||
build_tool_call("add", r#"{"x":1,"y":2}"#),
|
||||
);
|
||||
let chunks = split_by_chars(&input, 7);
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\":1,\"y\":2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
parser
|
||||
.push(
|
||||
r#"<tool_call>
|
||||
{"name":"get_weather","arguments":{"location""#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete Qwen XML tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_malformed_field_order_fails_fast() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let error = parser
|
||||
.push(
|
||||
r#"<tool_call>
|
||||
{"arguments":{},"name":"get_weather"}
|
||||
</tool_call>"#,
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
expect![[r#"
|
||||
tool parser parsing failed: invalid Qwen XML
|
||||
expected `name`"#]]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,560 @@
|
||||
use winnow::ascii::{digit1, multispace0 as ws0};
|
||||
use winnow::combinator::{alt, eof, repeat, seq};
|
||||
use winnow::prelude::*;
|
||||
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 crate::Tool;
|
||||
|
||||
const TOOL_CALLS_START: &str = "<|tool_calls_section_begin|>";
|
||||
const TOOL_CALLS_END: &str = "<|tool_calls_section_end|>";
|
||||
const TOOL_CALL_START: &str = "<|tool_call_begin|>";
|
||||
const TOOL_CALL_END: &str = "<|tool_call_end|>";
|
||||
const TOOL_CALL_ARGUMENT_START: &str = "<|tool_call_argument_begin|>";
|
||||
|
||||
type KimiK2Input<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum KimiK2Mode {
|
||||
Text,
|
||||
ToolBlock,
|
||||
Header,
|
||||
Arguments { json_scan: JsonObjectScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum KimiK2Event {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolCallsStart,
|
||||
ToolCallStart,
|
||||
ToolCallHeader {
|
||||
function_name: String,
|
||||
function_index: usize,
|
||||
},
|
||||
Arguments {
|
||||
len: usize,
|
||||
},
|
||||
ToolCallEnd,
|
||||
ToolCallsEnd,
|
||||
IgnoredRest,
|
||||
}
|
||||
|
||||
/// Tool parser for Kimi K2 token-delimited tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <|tool_calls_section_begin|>
|
||||
/// <|tool_call_begin|>functions.get_weather:0<|tool_call_argument_begin|>{"location":"NYC"}<|tool_call_end|>
|
||||
/// <|tool_calls_section_end|>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are already OpenAI-style JSON text, so they are streamed as raw
|
||||
/// argument deltas without schema conversion or JSON normalization.
|
||||
pub struct KimiK2ToolParser {
|
||||
buffer: String,
|
||||
mode: KimiK2Mode,
|
||||
active_tool_index: Option<usize>,
|
||||
}
|
||||
|
||||
impl KimiK2ToolParser {
|
||||
/// Create a Kimi K2 tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: KimiK2Mode::Text,
|
||||
active_tool_index: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed Kimi K2 event to parser state and output.
|
||||
fn apply_event(&mut self, event: KimiK2Event, result: &mut ToolParseResult) -> Result<()> {
|
||||
match event {
|
||||
KimiK2Event::Text { len: consumed_len } => {
|
||||
result.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
KimiK2Event::ToolCallsStart => self.mode = KimiK2Mode::ToolBlock,
|
||||
KimiK2Event::ToolCallStart => self.mode = KimiK2Mode::Header,
|
||||
KimiK2Event::ToolCallHeader {
|
||||
function_name,
|
||||
function_index,
|
||||
} => {
|
||||
let tool_index = function_index;
|
||||
self.active_tool_index = Some(tool_index);
|
||||
self.mode = KimiK2Mode::Arguments {
|
||||
json_scan: JsonObjectScanState::default(),
|
||||
};
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: Some(function_name),
|
||||
arguments: String::new(),
|
||||
});
|
||||
}
|
||||
KimiK2Event::Arguments { len: consumed_len } => {
|
||||
let Some(tool_index) = self.active_tool_index else {
|
||||
return Err(parsing_failed!(
|
||||
"Kimi K2 arguments without an active tool call"
|
||||
));
|
||||
};
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: None,
|
||||
arguments: self.buffer[..consumed_len].to_string(),
|
||||
});
|
||||
}
|
||||
KimiK2Event::ToolCallEnd => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = KimiK2Mode::ToolBlock;
|
||||
}
|
||||
KimiK2Event::ToolCallsEnd => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = KimiK2Mode::Done;
|
||||
}
|
||||
KimiK2Event::IgnoredRest => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset all streaming state.
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.mode = KimiK2Mode::Text;
|
||||
self.active_tool_index = None;
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for KimiK2ToolParser {
|
||||
/// Create a boxed Kimi K2 tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
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<ToolParseResult> {
|
||||
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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::default();
|
||||
match &self.mode {
|
||||
KimiK2Mode::Text => result.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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 event for the current parser mode.
|
||||
fn parse_next_kimi_k2_event(
|
||||
input: &mut KimiK2Input<'_>,
|
||||
mode: &mut KimiK2Mode,
|
||||
) -> ModalResult<KimiK2Event> {
|
||||
match mode {
|
||||
KimiK2Mode::Text => parse_text_event(input),
|
||||
KimiK2Mode::ToolBlock => parse_tool_block_event(input),
|
||||
KimiK2Mode::Header => tool_call_header_event(input),
|
||||
KimiK2Mode::Arguments { json_scan } => parse_arguments_event(input, json_scan),
|
||||
KimiK2Mode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode Kimi K2 event.
|
||||
fn parse_text_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
alt((tool_calls_start_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse one event inside the Kimi K2 tool-calls section.
|
||||
fn parse_tool_block_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
alt((tool_calls_end_event, tool_call_start_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse one event inside a Kimi K2 tool-call arguments payload.
|
||||
fn parse_arguments_event(
|
||||
input: &mut KimiK2Input<'_>,
|
||||
json_scan: &mut JsonObjectScanState,
|
||||
) -> ModalResult<KimiK2Event> {
|
||||
if json_scan.complete() {
|
||||
tool_call_end_event(input)
|
||||
} else {
|
||||
argument_delta_event(input, json_scan)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 tool-calls section start marker.
|
||||
fn tool_calls_start_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
literal(TOOL_CALLS_START).value(KimiK2Event::ToolCallsStart).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 tool-calls section end marker.
|
||||
fn tool_calls_end_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
(ws0, literal(TOOL_CALLS_END))
|
||||
.value(KimiK2Event::ToolCallsEnd)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 tool-call start marker.
|
||||
fn tool_call_start_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
(ws0, literal(TOOL_CALL_START))
|
||||
.value(KimiK2Event::ToolCallStart)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 tool-call end marker.
|
||||
fn tool_call_end_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
literal(TOOL_CALL_END).value(KimiK2Event::ToolCallEnd).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 tool-call header before the argument marker.
|
||||
fn tool_call_header_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
let (header, _) = (
|
||||
take_until(1.., TOOL_CALL_ARGUMENT_START),
|
||||
literal(TOOL_CALL_ARGUMENT_START),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
let mut header_input = header;
|
||||
let (header, _, _) = (tool_header, ws0, eof).parse_next(&mut header_input)?;
|
||||
|
||||
Ok(KimiK2Event::ToolCallHeader {
|
||||
function_name: header.function_name,
|
||||
function_index: header.function_index,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 raw JSON arguments delta.
|
||||
fn argument_delta_event(
|
||||
input: &mut KimiK2Input<'_>,
|
||||
json_scan: &mut JsonObjectScanState,
|
||||
) -> ModalResult<KimiK2Event> {
|
||||
take_json_object(input, json_scan).map(|len| KimiK2Event::Arguments { len })
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next Kimi K2 tool-calls section.
|
||||
fn safe_text_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
safe_text_len(input, TOOL_CALLS_START).map(|len| KimiK2Event::Text { len })
|
||||
}
|
||||
|
||||
/// Parse ignored rest after the Kimi K2 tool-calls section ends.
|
||||
fn ignored_rest_event(input: &mut KimiK2Input<'_>) -> ModalResult<KimiK2Event> {
|
||||
rest.value(KimiK2Event::IgnoredRest).parse_next(input)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct KimiK2ToolHeader {
|
||||
function_name: String,
|
||||
function_index: usize,
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 tool-call header.
|
||||
fn tool_header(input: &mut &str) -> ModalResult<KimiK2ToolHeader> {
|
||||
let (function_name, function_index) = seq!(
|
||||
_: ws0,
|
||||
_: namespace_prefix,
|
||||
tool_name_segment,
|
||||
_: literal(":"),
|
||||
tool_call_index,
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok(KimiK2ToolHeader {
|
||||
function_name: function_name.to_string(),
|
||||
function_index,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse Kimi K2 namespace segments before the final tool name.
|
||||
fn namespace_prefix(input: &mut &str) -> ModalResult<()> {
|
||||
repeat(0.., namespace_segment).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 namespace segment.
|
||||
fn namespace_segment<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
let (segment, _) = (tool_name_segment, literal(".")).parse_next(input)?;
|
||||
Ok(segment)
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 tool name segment.
|
||||
fn tool_name_segment<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
take_while(1.., |ch: char| {
|
||||
!ch.is_whitespace() && ch != '<' && ch != ':' && ch != '.'
|
||||
})
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Kimi K2 tool-call index.
|
||||
fn tool_call_index(input: &mut &str) -> ModalResult<usize> {
|
||||
digit1.parse_to().parse_next(input)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::{
|
||||
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};
|
||||
|
||||
fn build_tool_call(function_name: &str, index: usize, arguments: &str) -> String {
|
||||
format!(
|
||||
"{TOOL_CALL_START}functions.{function_name}:{index}{TOOL_CALL_ARGUMENT_START}{arguments}{TOOL_CALL_END}"
|
||||
)
|
||||
}
|
||||
|
||||
fn build_tool_section(tool_calls: &[String]) -> String {
|
||||
format!("{TOOL_CALLS_START}{}{TOOL_CALLS_END}", tool_calls.join(""))
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.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
|
||||
.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);
|
||||
}
|
||||
|
||||
#[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
|
||||
.parse_complete(&build_tool_section(&[build_tool_call(
|
||||
"get_weather",
|
||||
0,
|
||||
arguments,
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_streaming_emits_argument_deltas() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
TOOL_CALLS_START,
|
||||
TOOL_CALL_START,
|
||||
"functions.get_weather:0",
|
||||
TOOL_CALL_ARGUMENT_START,
|
||||
"{\"location\":",
|
||||
"\"Paris\"",
|
||||
"}",
|
||||
TOOL_CALL_END,
|
||||
TOOL_CALLS_END,
|
||||
];
|
||||
|
||||
let mut result = ToolParseResult::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.push(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
result.append(next);
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Paris\"", "}"]);
|
||||
let result = result.coalesce_calls();
|
||||
assert_eq!(result.calls[0].arguments, r#"{"location":"Paris"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_streaming_holds_back_split_markers() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"hello <|tool_calls",
|
||||
"_section_begin|>",
|
||||
TOOL_CALL_START,
|
||||
"functions.get_weather:0",
|
||||
TOOL_CALL_ARGUMENT_START,
|
||||
r#"{"location":"NYC"}"#,
|
||||
"<|tool_call",
|
||||
"_end|>",
|
||||
TOOL_CALLS_END,
|
||||
];
|
||||
|
||||
let result = 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"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_keeps_end_marker_literal_inside_json_string() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
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();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_streaming_keeps_split_end_marker_literal_inside_json_string() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
TOOL_CALLS_START,
|
||||
TOOL_CALL_START,
|
||||
"functions.echo:0",
|
||||
TOOL_CALL_ARGUMENT_START,
|
||||
r#"{"text":"literal <|tool"#,
|
||||
r#"_call_end|> inside"}"#,
|
||||
TOOL_CALL_END,
|
||||
TOOL_CALLS_END,
|
||||
];
|
||||
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
result.calls[0].arguments,
|
||||
r#"{"text":"literal <|tool_call_end|> inside"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_streaming_extracts_multiple_tool_calls() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
let input = build_tool_section(&[
|
||||
build_tool_call("get_weather", 0, r#"{"location":"Shanghai"}"#),
|
||||
build_tool_call("add", 1, r#"{"x":1,"y":2}"#),
|
||||
]);
|
||||
|
||||
let chunks = split_by_chars(&input, 7);
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParseResult {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"add",
|
||||
),
|
||||
arguments: "{\"x\":1,\"y\":2}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_accepts_non_functions_header_prefix() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
let input = format!(
|
||||
"{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();
|
||||
|
||||
assert_eq!(result.calls[0].tool_index, 42);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("search"));
|
||||
assert_eq!(result.calls[0].arguments, "{}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_tool_header_parses_namespace_function_and_index() {
|
||||
let mut input = "api.tools.search:42";
|
||||
let header = tool_header(&mut input).unwrap();
|
||||
|
||||
expect![[r#"
|
||||
KimiK2ToolHeader {
|
||||
function_name: "search",
|
||||
function_index: 42,
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&header);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
parser
|
||||
.push(&format!(
|
||||
"{TOOL_CALLS_START}{TOOL_CALL_START}functions.get_weather:0{TOOL_CALL_ARGUMENT_START}{{\"location\""
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete Kimi K2 tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_malformed_header_fails_fast() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
let input =
|
||||
format!("{TOOL_CALLS_START}{TOOL_CALL_START}get_weather{TOOL_CALL_ARGUMENT_START}{{}}");
|
||||
|
||||
let error = parser.push(&input).unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
//! Streaming tool parsers for chat completions.
|
||||
|
||||
#[macro_use]
|
||||
mod error;
|
||||
mod deepseek_dsml;
|
||||
mod deepseek_json;
|
||||
mod gemma4;
|
||||
mod glm_xml;
|
||||
mod json;
|
||||
mod kimi_k2;
|
||||
mod minimax_m2;
|
||||
mod parameters;
|
||||
mod qwen_coder;
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub mod test_utils;
|
||||
mod utils;
|
||||
|
||||
use std::collections::{BTreeMap, btree_map};
|
||||
|
||||
pub use deepseek_dsml::{DeepSeekV4ToolParser, DeepSeekV32ToolParser};
|
||||
pub use deepseek_json::{DeepSeekV3ToolParser, DeepSeekV31ToolParser};
|
||||
pub use error::{Result, ToolParserError};
|
||||
pub use gemma4::Gemma4ToolParser;
|
||||
pub use glm_xml::{Glm45MoeToolParser, Glm47MoeToolParser};
|
||||
pub use json::{HermesToolParser, Llama3JsonToolParser, MistralToolParser, Qwen3XmlToolParser};
|
||||
pub use kimi_k2::KimiK2ToolParser;
|
||||
pub use minimax_m2::MinimaxM2ToolParser;
|
||||
pub use qwen_coder::Qwen3CoderToolParser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
/// One function-style tool made available to the model.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Tool {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub parameters: Value,
|
||||
pub strict: Option<bool>,
|
||||
}
|
||||
|
||||
/// One tool-call update emitted while parsing assistant text.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ToolCallDelta {
|
||||
/// Stable parser-local tool index for this call within one assistant turn.
|
||||
pub tool_index: usize,
|
||||
/// Function name, present on the first update for one tool call.
|
||||
pub name: Option<String>,
|
||||
/// Arguments text contributed by this update.
|
||||
pub arguments: String,
|
||||
}
|
||||
|
||||
/// Result of advancing tool parsing with one assistant-text input.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct ToolParseResult {
|
||||
/// 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<ToolCallDelta>,
|
||||
}
|
||||
|
||||
impl ToolParseResult {
|
||||
/// Append another parser result 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) {
|
||||
self.normal_text.push_str(&other.normal_text);
|
||||
self.calls.append(&mut other.calls);
|
||||
}
|
||||
|
||||
/// Merge multiple deltas for the same tool call into one complete item.
|
||||
///
|
||||
/// This is primarily used by the default `parse_complete()` implementation,
|
||||
/// 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 {
|
||||
let mut merged = BTreeMap::<usize, ToolCallDelta>::new();
|
||||
let mut order = Vec::new();
|
||||
|
||||
for call in self.calls {
|
||||
match merged.entry(call.tool_index) {
|
||||
btree_map::Entry::Vacant(entry) => {
|
||||
order.push(call.tool_index);
|
||||
entry.insert(call);
|
||||
}
|
||||
btree_map::Entry::Occupied(mut entry) => {
|
||||
let existing = entry.get_mut();
|
||||
if existing.name.is_none() {
|
||||
existing.name = call.name;
|
||||
}
|
||||
existing.arguments.push_str(&call.arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.calls =
|
||||
order.into_iter().filter_map(|tool_index| merged.remove(&tool_index)).collect();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Incremental parser that extracts tool calls from assistant output.
|
||||
pub trait ToolParser: Send {
|
||||
/// Construct a boxed parser instance for one request stream.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static;
|
||||
|
||||
/// Return whether decoded output must preserve tokenizer special tokens.
|
||||
///
|
||||
/// Some model families emit tool-call sentinels as special tokens. Those
|
||||
/// parsers need `skip_special_tokens = false` while parsing is enabled.
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Feed one decoded text delta into the parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult>;
|
||||
|
||||
/// Flush any buffered partial state at end of stream.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
Ok(ToolParseResult::default())
|
||||
}
|
||||
|
||||
/// 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<ToolParseResult> {
|
||||
let mut result = self.push(output)?;
|
||||
result.append(self.finish()?);
|
||||
Ok(result.coalesce_calls())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,519 @@
|
||||
use winnow::ascii::{multispace0 as ws0, multispace1 as ws1};
|
||||
use winnow::combinator::{alt, delimited, repeat, seq, terminated};
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::Partial;
|
||||
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 crate::Tool;
|
||||
|
||||
const TOOL_CALL_START: &str = "<minimax:tool_call>";
|
||||
const TOOL_CALL_END: &str = "</minimax:tool_call>";
|
||||
const INVOKE_START: &str = "<invoke";
|
||||
const INVOKE_END: &str = "</invoke>";
|
||||
const PARAMETER_START: &str = "<parameter";
|
||||
const PARAMETER_END: &str = "</parameter>";
|
||||
|
||||
type MinimaxM2Input<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum MinimaxM2Mode {
|
||||
Text,
|
||||
ToolBlock,
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum MinimaxM2Event {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolBlockStart,
|
||||
Invoke {
|
||||
name: String,
|
||||
raw_params: Vec<(String, String)>,
|
||||
},
|
||||
ToolBlockEnd,
|
||||
IgnoredRest,
|
||||
}
|
||||
|
||||
/// Tool parser for MiniMax M2 XML-style tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <minimax:tool_call><invoke name="get_weather">
|
||||
/// <parameter name="city">Seattle</parameter>
|
||||
/// </invoke></minimax:tool_call>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are emitted only after a full `<invoke>` block is parsed.
|
||||
pub struct MinimaxM2ToolParser {
|
||||
buffer: String,
|
||||
mode: MinimaxM2Mode,
|
||||
emitted_tool_count: usize,
|
||||
tool_parameters: ToolSchemas,
|
||||
}
|
||||
|
||||
impl MinimaxM2ToolParser {
|
||||
/// Create a MiniMax M2 tool parser.
|
||||
fn new(tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: MinimaxM2Mode::Text,
|
||||
emitted_tool_count: 0,
|
||||
tool_parameters: ToolSchemas::from_tools(tools),
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed MiniMax M2 event to parser state and output.
|
||||
fn apply_event(&mut self, event: MinimaxM2Event, result: &mut ToolParseResult) -> Result<()> {
|
||||
match event {
|
||||
MinimaxM2Event::Text { len: consumed_len } => {
|
||||
result.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
MinimaxM2Event::ToolBlockStart => self.mode = MinimaxM2Mode::ToolBlock,
|
||||
MinimaxM2Event::Invoke { name, raw_params } => {
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
MinimaxM2Event::ToolBlockEnd => self.mode = MinimaxM2Mode::Done,
|
||||
MinimaxM2Event::IgnoredRest => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset all streaming state.
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.mode = MinimaxM2Mode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for MinimaxM2ToolParser {
|
||||
/// Create a boxed MiniMax M2 tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the MiniMax M2 parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::default();
|
||||
match self.mode {
|
||||
MinimaxM2Mode::Text => {
|
||||
result.normal_text.push_str(&self.buffer);
|
||||
}
|
||||
MinimaxM2Mode::ToolBlock => {
|
||||
return Err(parsing_failed!("incomplete MiniMax M2 tool call"));
|
||||
}
|
||||
MinimaxM2Mode::Done => {}
|
||||
}
|
||||
self.reset();
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M2 event for the current parser mode.
|
||||
fn parse_next_minimax_m2_event(
|
||||
input: &mut MinimaxM2Input<'_>,
|
||||
mode: MinimaxM2Mode,
|
||||
) -> ModalResult<MinimaxM2Event> {
|
||||
match mode {
|
||||
MinimaxM2Mode::Text => parse_text_event(input),
|
||||
MinimaxM2Mode::ToolBlock => parse_tool_block_event(input),
|
||||
MinimaxM2Mode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode MiniMax M2 event.
|
||||
fn parse_text_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event> {
|
||||
alt((tool_block_start_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M2 tool-block start marker.
|
||||
fn tool_block_start_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event> {
|
||||
literal(TOOL_CALL_START).value(MinimaxM2Event::ToolBlockStart).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next MiniMax M2 marker.
|
||||
fn safe_text_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event> {
|
||||
safe_text_len(input, TOOL_CALL_START).map(|len| MinimaxM2Event::Text { len })
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M2 tool-block end marker.
|
||||
fn tool_block_end_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event> {
|
||||
(ws0, literal(TOOL_CALL_END))
|
||||
.value(MinimaxM2Event::ToolBlockEnd)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a complete MiniMax M2 invoke block.
|
||||
fn invoke_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event> {
|
||||
let (name, raw_params) = seq!(
|
||||
_: ws0,
|
||||
_: literal(INVOKE_START),
|
||||
_: (ws1, literal("name=")),
|
||||
attr_value,
|
||||
_: literal(">"),
|
||||
repeat(0.., terminated(parameter, ws0)),
|
||||
_: literal(INVOKE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok(MinimaxM2Event::Invoke {
|
||||
name: name.trim().to_string(),
|
||||
raw_params,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M2 parameter block.
|
||||
fn parameter(input: &mut MinimaxM2Input<'_>) -> ModalResult<(String, String)> {
|
||||
let (name, value) = seq!(
|
||||
_: literal(PARAMETER_START),
|
||||
_: (ws1, literal("name=")),
|
||||
attr_value,
|
||||
_: literal(">"),
|
||||
take_until(0.., PARAMETER_END).map(xml_unescape),
|
||||
_: literal(PARAMETER_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok((name.trim().to_string(), value.into_owned()))
|
||||
}
|
||||
|
||||
/// Parse a quoted or unquoted XML attribute value.
|
||||
fn attr_value<'i>(input: &mut MinimaxM2Input<'i>) -> ModalResult<&'i str> {
|
||||
alt((
|
||||
delimited(literal("\""), take_until(1.., "\""), literal("\"")),
|
||||
delimited(literal("'"), take_until(1.., "'"), literal("'")),
|
||||
take_until(1.., ">"),
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse ignored rest after the MiniMax M2 tool block ends.
|
||||
fn ignored_rest_event(input: &mut MinimaxM2Input<'_>) -> ModalResult<MinimaxM2Event> {
|
||||
rest.value(MinimaxM2Event::IgnoredRest).parse_next(input)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::{MinimaxM2ToolParser, TOOL_CALL_END, TOOL_CALL_START, ToolParser};
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
|
||||
fn build_tool_block(invokes: &[(&str, Vec<(&str, &str)>)]) -> String {
|
||||
let invokes = invokes
|
||||
.iter()
|
||||
.map(|(function_name, params)| {
|
||||
let params = params
|
||||
.iter()
|
||||
.map(|(name, value)| format!(r#"<parameter name="{name}">{value}</parameter>"#))
|
||||
.collect::<Vec<_>>()
|
||||
.join("");
|
||||
format!(r#"<invoke name="{function_name}">{params}</invoke>"#)
|
||||
})
|
||||
.collect::<String>();
|
||||
format!("{TOOL_CALL_START}{invokes}{TOOL_CALL_END}")
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = 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_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle", "days": 5 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_preserves_prefix_and_ignores_trailing_text() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output = format!(
|
||||
"Let me check. {} This trailing text is ignored.",
|
||||
build_tool_block(&[("get_weather", vec![("city", "Seattle")])])
|
||||
);
|
||||
let result = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(result.normal_text, "Let me check. ");
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_extracts_multiple_invokes() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = 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!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[1].arguments).unwrap(),
|
||||
json!({ "city": "NYC" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_converts_schema_types() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_block(&[(
|
||||
"convert",
|
||||
vec![
|
||||
("whole", "5.0"),
|
||||
("flag", "true"),
|
||||
("payload", r#"{"nested":true}"#),
|
||||
("items", "[1,2]"),
|
||||
("empty", "42"),
|
||||
],
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": 5.0,
|
||||
"flag": true,
|
||||
"payload": { "nested": true },
|
||||
"items": [1, 2],
|
||||
"empty": "42",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_unescapes_literal_closing_tags_in_parameter_value() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_block(&[(
|
||||
"get_weather",
|
||||
vec![
|
||||
(
|
||||
"city",
|
||||
"Seattle </parameter></invoke></minimax:tool_call>",
|
||||
),
|
||||
("days", "5"),
|
||||
],
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"city": "Seattle </parameter></invoke></minimax:tool_call>",
|
||||
"days": 5,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_handles_multiline_parameters() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(
|
||||
"<minimax:tool_call>\
|
||||
<invoke name=\"calculate_area\">\
|
||||
<parameter name=\"shape\">\nrectangle\n</parameter>\
|
||||
<parameter name=\"dimensions\">{\"width\":10,\n\"height\":20}</parameter>\
|
||||
<parameter name=\"precision\">2</parameter>\
|
||||
</invoke>\
|
||||
</minimax:tool_call>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"shape": "\nrectangle\n",
|
||||
"dimensions": { "width": 10, "height": 20 },
|
||||
"precision": 2,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_extracts_single_tool_call() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"<minimax:tool_call>",
|
||||
r#"<invoke name="get_weather">"#,
|
||||
r#"<parameter name="city">Seattle</parameter>"#,
|
||||
"</invoke></minimax:tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_preserves_prefix_text() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"Let me check. ",
|
||||
"<minimax:tool_call>",
|
||||
r#"<invoke name="get_weather"><parameter name="city">Seattle</parameter></invoke>"#,
|
||||
"</minimax:tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(result.normal_text, "Let me check. ");
|
||||
assert_eq!(result.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!"]);
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_handles_marker_split_across_chunks() {
|
||||
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);
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert!(result.normal_text.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_extracts_multiple_invokes_in_order() {
|
||||
let text = build_tool_block(&[
|
||||
("get_weather", vec![("city", "Seattle")]),
|
||||
("get_weather", vec![("city", "NYC")]),
|
||||
]);
|
||||
let chunks = split_by_chars(&text, 7);
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = 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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_ignores_text_after_tool_block() {
|
||||
let text = format!(
|
||||
"{} ignored",
|
||||
build_tool_block(&[("get_weather", vec![("city", "Seattle")])])
|
||||
);
|
||||
let chunks = split_by_chars(&text, 5);
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert_eq!(result.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#"<minimax:tool_call><invoke name="get_weather">"#).unwrap();
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
parser.push(r#"<minimax:tool_call><invoke name="get_weather">"#).unwrap();
|
||||
|
||||
assert!(parser.finish().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_finish_fails_after_bare_tool_block_start() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
parser.push("<minimax:tool_call>").unwrap();
|
||||
|
||||
assert!(parser.finish().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_malformed_tool_call_fails_fast() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let error = parser.push("<minimax:tool_call><bad></minimax:tool_call>").unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,508 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde_json::{Number, Value};
|
||||
|
||||
use crate::Tool;
|
||||
|
||||
/// Normalized parameter schemas for all tools in one request.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(super) struct ToolSchemas {
|
||||
tools: BTreeMap<String, ToolSchema>,
|
||||
}
|
||||
|
||||
/// Normalized parameter schema for one tool.
|
||||
///
|
||||
/// This is a minimal subset of JSON Schema with some normalization heuristics
|
||||
/// to support common schema patterns and upstream schema variations, focused on
|
||||
/// coercing raw string parameter values into more specific JSON types for
|
||||
/// downstream tool call execution.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(super) struct ToolSchema {
|
||||
params: BTreeMap<String, JsonParamType>,
|
||||
}
|
||||
|
||||
/// Normalized JSON parameter type used for raw string coercion.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(super) enum JsonParamType {
|
||||
String,
|
||||
Integer,
|
||||
Number,
|
||||
Boolean,
|
||||
Object,
|
||||
Array,
|
||||
Null,
|
||||
OneOf(Vec<JsonParamType>),
|
||||
}
|
||||
|
||||
impl ToolSchemas {
|
||||
/// Normalize OpenAI-style tool parameter JSON schemas for one request.
|
||||
pub(super) fn from_tools(tools: &[Tool]) -> Self {
|
||||
let tools = tools
|
||||
.iter()
|
||||
.map(|tool| (tool.name.clone(), ToolSchema::from_schema(&tool.parameters)))
|
||||
.collect();
|
||||
|
||||
Self { tools }
|
||||
}
|
||||
|
||||
/// Convert raw string parameter values for one named tool.
|
||||
///
|
||||
/// Unknown tool names use an empty schema, so all parameters fall back to
|
||||
/// strings.
|
||||
pub(super) fn convert_params_with_schema(
|
||||
&self,
|
||||
function_name: &str,
|
||||
params: Vec<(String, String)>,
|
||||
) -> serde_json::Map<String, Value> {
|
||||
let tool_schema = self.tools.get(function_name).unwrap_or(ToolSchema::empty());
|
||||
let mut converted = serde_json::Map::with_capacity(params.len());
|
||||
for (name, value) in params {
|
||||
let value = tool_schema.convert(&name, &value);
|
||||
converted.insert(name, value);
|
||||
}
|
||||
converted
|
||||
}
|
||||
|
||||
/// Convert one raw string parameter value for one named tool.
|
||||
pub(super) fn convert_param_with_schema(
|
||||
&self,
|
||||
function_name: &str,
|
||||
name: &str,
|
||||
value: &str,
|
||||
) -> Value {
|
||||
let tool_schema = self.tools.get(function_name).unwrap_or(ToolSchema::empty());
|
||||
tool_schema.convert(name, value)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolSchema {
|
||||
/// Return an empty schema with no parameter information, which causes all
|
||||
/// parameters to be treated as strings.
|
||||
const fn empty() -> &'static Self {
|
||||
static EMPTY: ToolSchema = ToolSchema {
|
||||
params: BTreeMap::new(),
|
||||
};
|
||||
&EMPTY
|
||||
}
|
||||
|
||||
/// Normalize an OpenAI-style tool parameters JSON schema.
|
||||
fn from_schema(parameters: &Value) -> Self {
|
||||
let Some(properties) = parameters.get("properties").and_then(Value::as_object) else {
|
||||
return Self::default();
|
||||
};
|
||||
|
||||
let params = properties
|
||||
.iter()
|
||||
.filter_map(|(name, schema)| {
|
||||
JsonParamType::from_schema(schema).map(|param_type| (name.clone(), param_type))
|
||||
})
|
||||
.collect();
|
||||
|
||||
Self { params }
|
||||
}
|
||||
|
||||
/// Convert one raw parameter value using its normalized schema type.
|
||||
///
|
||||
/// If the parameter name is unknown, or we don't have a schema for it, or
|
||||
/// the value fails to convert, this falls back to returning the raw
|
||||
/// string as a JSON string value.
|
||||
fn convert(&self, name: &str, value: &str) -> Value {
|
||||
if value.eq_ignore_ascii_case("null") {
|
||||
return Value::Null;
|
||||
}
|
||||
|
||||
let Some(param_type) = self.params.get(name) else {
|
||||
return Value::String(value.to_string());
|
||||
};
|
||||
|
||||
convert_value(param_type, value).unwrap_or_else(|| Value::String(value.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl JsonParamType {
|
||||
/// Normalize one parameter property schema.
|
||||
fn from_schema(schema: &Value) -> Option<Self> {
|
||||
let schema = schema.as_object()?;
|
||||
|
||||
if let Some(type_value) = schema.get("type") {
|
||||
return Self::from_type_value(type_value);
|
||||
}
|
||||
|
||||
if let Some(composite) = schema.get("anyOf").or_else(|| schema.get("oneOf")) {
|
||||
let param_type = composite
|
||||
.as_array()
|
||||
.map(|schemas| schemas.iter().filter_map(Self::from_schema).collect::<Vec<_>>())
|
||||
.filter(|types| !types.is_empty())
|
||||
.map(Self::one_of)
|
||||
.unwrap_or(Self::Object);
|
||||
return Some(param_type);
|
||||
}
|
||||
|
||||
if schema.contains_key("enum") {
|
||||
return Some(Self::String);
|
||||
}
|
||||
if schema.contains_key("items") {
|
||||
return Some(Self::Array);
|
||||
}
|
||||
if schema.contains_key("properties") {
|
||||
return Some(Self::Object);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Normalize a JSON schema `type` value.
|
||||
fn from_type_value(type_value: &Value) -> Option<Self> {
|
||||
match type_value {
|
||||
Value::String(kind) => Self::from_type_name(kind),
|
||||
Value::Array(kinds) => {
|
||||
let types = kinds
|
||||
.iter()
|
||||
.filter_map(Value::as_str)
|
||||
.filter_map(Self::from_type_name)
|
||||
.collect::<Vec<_>>();
|
||||
if types.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(Self::one_of(types))
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalize one JSON schema type name.
|
||||
fn from_type_name(kind: &str) -> Option<Self> {
|
||||
let kind = kind.trim().to_ascii_lowercase();
|
||||
match kind.as_str() {
|
||||
"string" | "str" | "text" | "varchar" | "char" | "enum" => Some(Self::String),
|
||||
"integer" | "int" => Some(Self::Integer),
|
||||
"number" | "float" => Some(Self::Number),
|
||||
"boolean" | "bool" | "binary" => Some(Self::Boolean),
|
||||
"object" => Some(Self::Object),
|
||||
"array" | "arr" | "sequence" => Some(Self::Array),
|
||||
"null" => Some(Self::Null),
|
||||
_ if kind.starts_with("int")
|
||||
|| kind.starts_with("uint")
|
||||
|| kind.starts_with("long")
|
||||
|| kind.starts_with("short")
|
||||
|| kind.starts_with("unsigned") =>
|
||||
{
|
||||
Some(Self::Integer)
|
||||
}
|
||||
_ if kind.starts_with("num") || kind.starts_with("float") => Some(Self::Number),
|
||||
_ if kind.starts_with("dict") => Some(Self::Object),
|
||||
_ if kind.starts_with("list") => Some(Self::Array),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Collapse a candidate type list into one normalized type.
|
||||
fn one_of(mut types: Vec<Self>) -> Self {
|
||||
if types.len() == 1 {
|
||||
types.remove(0)
|
||||
} else {
|
||||
Self::OneOf(types)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert one raw string value to a normalized JSON type.
|
||||
fn convert_value(param_type: &JsonParamType, value: &str) -> Option<Value> {
|
||||
match param_type {
|
||||
JsonParamType::String => Some(Value::String(value.to_string())),
|
||||
JsonParamType::Integer => value.parse::<i64>().ok().map(Number::from).map(Value::Number),
|
||||
JsonParamType::Number => convert_number(value),
|
||||
JsonParamType::Boolean => convert_boolean(value),
|
||||
JsonParamType::Object | JsonParamType::Array => serde_json::from_str(value).ok(),
|
||||
JsonParamType::Null => value.eq_ignore_ascii_case("null").then_some(Value::Null),
|
||||
JsonParamType::OneOf(types) => {
|
||||
types.iter().find_map(|param_type| convert_value(param_type, value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert one raw string value to a JSON number.
|
||||
fn convert_number(value: &str) -> Option<Value> {
|
||||
if let Ok(parsed) = value.parse::<i64>() {
|
||||
return Some(Value::Number(Number::from(parsed)));
|
||||
}
|
||||
Number::from_f64(value.parse::<f64>().ok()?).map(Value::Number)
|
||||
}
|
||||
|
||||
/// Convert one raw string value to a boolean.
|
||||
fn convert_boolean(value: &str) -> Option<Value> {
|
||||
match value.trim().to_ascii_lowercase().as_str() {
|
||||
"true" | "1" => Some(Value::Bool(true)),
|
||||
"false" | "0" => Some(Value::Bool(false)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::{ToolSchema, ToolSchemas};
|
||||
use crate::Tool;
|
||||
|
||||
fn test_tool(name: &str, parameters: serde_json::Value) -> Tool {
|
||||
Tool {
|
||||
name: name.to_string(),
|
||||
description: None,
|
||||
parameters,
|
||||
strict: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_schema_converts_everything_as_string() {
|
||||
let params = ToolSchema::from_schema(&json!({ "type": "object" }));
|
||||
|
||||
assert_eq!(params.convert("count", "42"), json!("42"));
|
||||
assert_eq!(params.convert("count", "null"), json!(null));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skips_unknown_property_schema_and_unknown_type() {
|
||||
let params = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"unknown_schema": true,
|
||||
"unknown_type": { "type": "mystery" },
|
||||
"known": { "type": "integer" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(params.convert("unknown_schema", "42"), json!("42"));
|
||||
assert_eq!(params.convert("unknown_type", "42"), json!("42"));
|
||||
assert_eq!(params.convert("known", "42"), json!(42));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_supported_types() {
|
||||
let params = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"text": { "type": "string" },
|
||||
"count": { "type": "integer" },
|
||||
"size": { "type": "number" },
|
||||
"enabled": { "type": "boolean" },
|
||||
"payload": { "type": "object" },
|
||||
"items": { "type": "array" },
|
||||
"nothing": { "type": "null" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(params.convert("text", "42"), json!("42"));
|
||||
assert_eq!(params.convert("count", "42"), json!(42));
|
||||
assert_eq!(params.convert("size", "5.0"), json!(5.0));
|
||||
assert_eq!(params.convert("enabled", "1"), json!(true));
|
||||
assert_eq!(params.convert("payload", r#"{"k":1}"#), json!({ "k": 1 }));
|
||||
assert_eq!(params.convert("items", "[1,2]"), json!([1, 2]));
|
||||
assert_eq!(params.convert("nothing", "null"), json!(null));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn number_conversion_parses_int_then_float() {
|
||||
let params = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"value": { "type": "number" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(params.convert("value", "5"), json!(5));
|
||||
assert_eq!(params.convert("value", "5.0"), json!(5.0));
|
||||
assert_eq!(params.convert("value", "5."), json!(5.0));
|
||||
assert_eq!(params.convert("value", "+1"), json!(1));
|
||||
assert_eq!(params.convert("value", "+1.0"), json!(1.0));
|
||||
assert_eq!(
|
||||
params.convert("value", "9223372036854775807.5"),
|
||||
json!(9223372036854775808.0)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_upstream_aliases() {
|
||||
let params = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"s": { "type": "varchar" },
|
||||
"i": { "type": "unsigned_int" },
|
||||
"n": { "type": "float64" },
|
||||
"b": { "type": "binary" },
|
||||
"a": { "type": "sequence" },
|
||||
"o": { "type": "dict" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(params.convert("s", "x"), json!("x"));
|
||||
assert_eq!(params.convert("i", "7"), json!(7));
|
||||
assert_eq!(params.convert("n", "7.5"), json!(7.5));
|
||||
assert_eq!(params.convert("b", "true"), json!(true));
|
||||
assert_eq!(params.convert("a", "[1]"), json!([1]));
|
||||
assert_eq!(params.convert("o", r#"{"x":1}"#), json!({ "x": 1 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_union_type_order() {
|
||||
let integer_first = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"value": { "type": ["integer", "string"] }
|
||||
}
|
||||
}));
|
||||
let string_first = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"value": { "type": ["string", "integer"] }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(integer_first.convert("value", "42"), json!(42));
|
||||
assert_eq!(string_first.convert("value", "42"), json!("42"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_composite_schemas() {
|
||||
let params = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"choice": {
|
||||
"anyOf": [
|
||||
{ "type": "integer" },
|
||||
{ "type": "string" }
|
||||
]
|
||||
},
|
||||
"fallback_object": {
|
||||
"oneOf": [
|
||||
{ "type": "mystery" }
|
||||
]
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(params.convert("choice", "42"), json!(42));
|
||||
assert_eq!(
|
||||
params.convert("fallback_object", r#"{"x":1}"#),
|
||||
json!({ "x": 1 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infers_type_from_schema_shape_without_type() {
|
||||
let params = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"choice": { "enum": ["a", "b"] },
|
||||
"items": { "items": { "type": "integer" } },
|
||||
"payload": { "properties": { "x": { "type": "integer" } } }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(params.convert("choice", "a"), json!("a"));
|
||||
assert_eq!(params.convert("items", "[1,2]"), json!([1, 2]));
|
||||
assert_eq!(params.convert("payload", r#"{"x":1}"#), json!({ "x": 1 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_params_for_known_tool() {
|
||||
let schemas = ToolSchemas::from_tools(&[test_tool(
|
||||
"search",
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": { "type": "string" },
|
||||
"topn": { "type": "integer" }
|
||||
}
|
||||
}),
|
||||
)]);
|
||||
|
||||
let converted = schemas.convert_params_with_schema(
|
||||
"search",
|
||||
vec![
|
||||
("query".to_string(), "rust".to_string()),
|
||||
("topn".to_string(), "5".to_string()),
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(converted.get("query"), Some(&json!("rust")));
|
||||
assert_eq!(converted.get("topn"), Some(&json!(5)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_params_falls_back_to_string_for_failed_coercion() {
|
||||
let schemas = ToolSchemas::from_tools(&[test_tool(
|
||||
"convert",
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"whole": { "type": "number" },
|
||||
"flag": { "type": "boolean" },
|
||||
"payload": { "type": "object" },
|
||||
"items": { "type": "array" },
|
||||
"missing_type": {}
|
||||
}
|
||||
}),
|
||||
)]);
|
||||
|
||||
let converted = schemas.convert_params_with_schema(
|
||||
"convert",
|
||||
vec![
|
||||
("whole".to_string(), "not-a-number".to_string()),
|
||||
("flag".to_string(), "maybe".to_string()),
|
||||
("payload".to_string(), "not-json".to_string()),
|
||||
("items".to_string(), "not-json".to_string()),
|
||||
("missing_type".to_string(), "42".to_string()),
|
||||
("unknown_param".to_string(), "42".to_string()),
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(converted.get("whole"), Some(&json!("not-a-number")));
|
||||
assert_eq!(converted.get("flag"), Some(&json!("maybe")));
|
||||
assert_eq!(converted.get("payload"), Some(&json!("not-json")));
|
||||
assert_eq!(converted.get("items"), Some(&json!("not-json")));
|
||||
assert_eq!(converted.get("missing_type"), Some(&json!("42")));
|
||||
assert_eq!(converted.get("unknown_param"), Some(&json!("42")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_params_preserves_null_for_known_param() {
|
||||
let schemas = ToolSchemas::from_tools(&[test_tool(
|
||||
"convert",
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"value": { "type": "string" }
|
||||
}
|
||||
}),
|
||||
)]);
|
||||
|
||||
let converted = schemas
|
||||
.convert_params_with_schema("convert", vec![("value".to_string(), "NULL".to_string())]);
|
||||
|
||||
assert_eq!(converted.get("value"), Some(&json!(null)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_tool_converts_values_without_schema() {
|
||||
let schemas = ToolSchemas::from_tools(&[test_tool(
|
||||
"search",
|
||||
json!({ "type": "object", "properties": {} }),
|
||||
)]);
|
||||
|
||||
let converted = schemas.convert_params_with_schema(
|
||||
"missing",
|
||||
vec![
|
||||
("query".to_string(), "rust".to_string()),
|
||||
("topn".to_string(), "5".to_string()),
|
||||
("nullish".to_string(), "null".to_string()),
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(converted.get("query"), Some(&json!("rust")));
|
||||
assert_eq!(converted.get("topn"), Some(&json!("5")));
|
||||
assert_eq!(converted.get("nullish"), Some(&json!(null)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,632 @@
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::{alt, delimited, eof, repeat, seq, terminated};
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::Partial;
|
||||
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 crate::Tool;
|
||||
|
||||
const TOOL_CALL_START: &str = "<tool_call>";
|
||||
const TOOL_CALL_END: &str = "</tool_call>";
|
||||
const FUNCTION_START: &str = "<function=";
|
||||
const FUNCTION_END: &str = "</function>";
|
||||
const PARAMETER_START: &str = "<parameter=";
|
||||
const PARAMETER_END: &str = "</parameter>";
|
||||
|
||||
type QwenCoderInput<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum QwenCoderMode {
|
||||
Text,
|
||||
ToolCall,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum QwenCoderEvent {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolCallStart,
|
||||
ToolCall {
|
||||
name: String,
|
||||
raw_params: Vec<(String, String)>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Tool parser for Qwen Coder XML-style tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <tool_call>
|
||||
/// <function=get_weather>
|
||||
/// <parameter=location>杭州</parameter>
|
||||
/// </function>
|
||||
/// </tool_call>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are emitted only after a full `tool_call` block is parsed.
|
||||
///
|
||||
/// Note: parallel calls are represented as repeated
|
||||
/// `<tool_call>...</tool_call>` blocks, not as multiple calls inside one tag.
|
||||
pub struct Qwen3CoderToolParser {
|
||||
buffer: String,
|
||||
mode: QwenCoderMode,
|
||||
emitted_tool_count: usize,
|
||||
tool_parameters: ToolSchemas,
|
||||
}
|
||||
|
||||
impl Qwen3CoderToolParser {
|
||||
/// Create a Qwen Coder tool parser.
|
||||
fn new(tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: QwenCoderMode::Text,
|
||||
emitted_tool_count: 0,
|
||||
tool_parameters: ToolSchemas::from_tools(tools),
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed Qwen Coder event to parser state and output.
|
||||
fn apply_event(&mut self, event: QwenCoderEvent, result: &mut ToolParseResult) -> Result<()> {
|
||||
match event {
|
||||
QwenCoderEvent::Text { len: consumed_len } => {
|
||||
result.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
QwenCoderEvent::ToolCallStart => self.mode = QwenCoderMode::ToolCall,
|
||||
QwenCoderEvent::ToolCall { name, raw_params } => {
|
||||
self.mode = QwenCoderMode::Text;
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
result.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset all streaming state.
|
||||
fn reset(&mut self) {
|
||||
self.buffer.clear();
|
||||
self.mode = QwenCoderMode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Qwen3CoderToolParser {
|
||||
/// Create a boxed Qwen Coder tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Push one decoded text chunk through the Qwen Coder parser.
|
||||
fn push(&mut self, chunk: &str) -> Result<ToolParseResult> {
|
||||
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.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Flush buffered text and reset parser state.
|
||||
fn finish(&mut self) -> Result<ToolParseResult> {
|
||||
let mut result = ToolParseResult::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);
|
||||
}
|
||||
self.reset();
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Qwen Coder event for the current parser mode.
|
||||
fn parse_next_qwen_coder_event(
|
||||
input: &mut QwenCoderInput<'_>,
|
||||
mode: QwenCoderMode,
|
||||
) -> ModalResult<QwenCoderEvent> {
|
||||
match mode {
|
||||
QwenCoderMode::Text => parse_text_event(input),
|
||||
QwenCoderMode::ToolCall => tool_call_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode Qwen Coder event.
|
||||
fn parse_text_event(input: &mut QwenCoderInput<'_>) -> ModalResult<QwenCoderEvent> {
|
||||
alt((tool_call_start_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Qwen Coder tool-call start marker.
|
||||
fn tool_call_start_event(input: &mut QwenCoderInput<'_>) -> ModalResult<QwenCoderEvent> {
|
||||
literal(TOOL_CALL_START).value(QwenCoderEvent::ToolCallStart).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next Qwen Coder marker.
|
||||
fn safe_text_event(input: &mut QwenCoderInput<'_>) -> ModalResult<QwenCoderEvent> {
|
||||
safe_text_len(input, TOOL_CALL_START).map(|len| QwenCoderEvent::Text { len })
|
||||
}
|
||||
|
||||
/// Parse a complete Qwen Coder tool call.
|
||||
fn tool_call_event(input: &mut QwenCoderInput<'_>) -> ModalResult<QwenCoderEvent> {
|
||||
let (body,) = seq!(
|
||||
_: ws0,
|
||||
take_until(0.., TOOL_CALL_END),
|
||||
_: literal(TOOL_CALL_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
parse_tool_call_body(body)
|
||||
}
|
||||
|
||||
/// Parse a Qwen Coder function block.
|
||||
fn function_event(input: &mut &str) -> ModalResult<QwenCoderEvent> {
|
||||
let (name, raw_params) = seq!(
|
||||
_: literal(FUNCTION_START),
|
||||
take_until(1.., ">"),
|
||||
_: ">",
|
||||
_: ws0,
|
||||
repeat(0.., terminated(parameter, ws0)),
|
||||
_: literal(FUNCTION_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok(QwenCoderEvent::ToolCall {
|
||||
name: name.to_string(),
|
||||
raw_params,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a Qwen Coder parameter block.
|
||||
fn parameter(input: &mut &str) -> ModalResult<(String, String)> {
|
||||
let (name, value) = seq!(
|
||||
_: literal(PARAMETER_START),
|
||||
take_until(1.., ">"),
|
||||
_: ">",
|
||||
take_until(0.., PARAMETER_END).map(trim_one_wrapping_newline).map(xml_unescape),
|
||||
_: literal(PARAMETER_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok((name.to_string(), value.into_owned()))
|
||||
}
|
||||
|
||||
/// Parse a Qwen Coder tool-call body.
|
||||
fn parse_tool_call_body(body: &str) -> ModalResult<QwenCoderEvent> {
|
||||
let mut input = body;
|
||||
delimited(ws0, function_event, (ws0, eof)).parse_next(&mut input)
|
||||
}
|
||||
|
||||
/// Trim a single leading and trailing newline from a parameter value.
|
||||
fn trim_one_wrapping_newline(value: &str) -> &str {
|
||||
let value = value.strip_prefix('\n').unwrap_or(value);
|
||||
value.strip_suffix('\n').unwrap_or(value)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::{Qwen3CoderToolParser, ToolParser};
|
||||
use crate::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
|
||||
fn build_tool_call(function_name: &str, params: &[(&str, &str)]) -> String {
|
||||
let params = params
|
||||
.iter()
|
||||
.map(|(name, value)| format!("<parameter={name}>{value}</parameter>"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
format!("<tool_call>\n<function={function_name}>\n{params}\n</function>\n</tool_call>")
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = 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_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"location": "SF",
|
||||
"date": "2026-04-29"
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_preserves_prefix_text() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = format!(
|
||||
"Thinking... {}",
|
||||
build_tool_call("get_weather", &[("location", "NYC")])
|
||||
);
|
||||
let result = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(result.normal_text, "Thinking... ");
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_converts_schema_types() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"convert",
|
||||
&[
|
||||
("whole", "5.0"),
|
||||
("flag", "true"),
|
||||
("payload", r#"{"nested":true}"#),
|
||||
("items", "[1,2]"),
|
||||
("empty", "42"),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": 5.0,
|
||||
"flag": true,
|
||||
"payload": { "nested": true },
|
||||
"items": [1, 2],
|
||||
"empty": "42",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_handles_upstream_multiline_typed_params() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(
|
||||
"<tool_call>\n\
|
||||
<function=calculate_area>\n\
|
||||
<parameter=shape>\n\
|
||||
rectangle\n\
|
||||
</parameter>\n\
|
||||
<parameter=dimensions>\n\
|
||||
{\"width\": 10,\n\
|
||||
\"height\": 20}\n\
|
||||
</parameter>\n\
|
||||
<parameter=precision>\n\
|
||||
2\n\
|
||||
</parameter>\n\
|
||||
</function>\n\
|
||||
</tool_call>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("calculate_area"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"shape": "rectangle",
|
||||
"dimensions": { "width": 10, "height": 20 },
|
||||
"precision": 2,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_handles_nested_json_parameter() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"convert",
|
||||
&[(
|
||||
"payload",
|
||||
r#"{"nested":{"value":[1,2,3],"child":{"enabled":true}}}"#,
|
||||
)],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"payload": {
|
||||
"nested": {
|
||||
"value": [1, 2, 3],
|
||||
"child": { "enabled": true },
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_preserves_xml_like_parameter_values() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"process",
|
||||
&[
|
||||
(
|
||||
"html_content",
|
||||
r#"<div class="test"><span>Hello</span></div>"#,
|
||||
),
|
||||
("xml_snippet", r#"<root><child attr="value"/></root>"#),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"html_content": r#"<div class="test"><span>Hello</span></div>"#,
|
||||
"xml_snippet": r#"<root><child attr="value"/></root>"#,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_unescapes_literal_closing_tags_in_parameter_value() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"get_weather",
|
||||
&[
|
||||
(
|
||||
"location",
|
||||
"杭州 </parameter></function></tool_call>",
|
||||
),
|
||||
("date", "2026-05-08"),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"location": "杭州 </parameter></function></tool_call>",
|
||||
"date": "2026-05-08",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_does_not_double_encode_anyof_object() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"update_record",
|
||||
&[("data", r#"{"key":"value","count":42}"#)],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"data": { "key": "value", "count": 42 },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_extracts_single_tool_call() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"<tool_call>\n",
|
||||
"<function=get_weather>\n",
|
||||
"<parameter=location>SF</parameter>\n",
|
||||
"</function>\n",
|
||||
"</tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_preserves_prefix_text() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"Thinking... ",
|
||||
"<tool_call>\n",
|
||||
"<function=get_weather>\n",
|
||||
"<parameter=location>SF</parameter>\n",
|
||||
"</function>\n",
|
||||
"</tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(result.normal_text, "Thinking... ");
|
||||
assert_eq!(result.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!"]);
|
||||
|
||||
assert_eq!(result.normal_text, "Hello, world!");
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_extracts_multiple_tool_calls_in_order() {
|
||||
let text = format!(
|
||||
"{}\n{}",
|
||||
build_tool_call("get_weather", &[("location", "SF")]),
|
||||
build_tool_call("get_weather", &[("location", "NYC")])
|
||||
);
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = 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!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[1].arguments).unwrap(),
|
||||
json!({ "location": "NYC" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_preserves_text_between_tool_calls() {
|
||||
let text = format!(
|
||||
"I'll check two cities.{}Between calls.{}Done.",
|
||||
build_tool_call("get_weather", &[("city", "Dallas"), ("state", "TX")]),
|
||||
build_tool_call("get_weather", &[("city", "Orlando"), ("state", "FL")])
|
||||
);
|
||||
let chunks = split_by_chars(&text, 5);
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(
|
||||
result.normal_text,
|
||||
"I'll check two cities.Between calls.Done."
|
||||
);
|
||||
assert_eq!(result.calls.len(), 2);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Dallas", "state": "TX" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[1].arguments).unwrap(),
|
||||
json!({ "city": "Orlando", "state": "FL" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_handles_start_token_split_across_chunks() {
|
||||
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);
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_does_not_emit_incomplete_tool_call() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.push("<tool_call>\n<function=get_weather>\n<parameter=location>SF</parameter>")
|
||||
.unwrap();
|
||||
|
||||
assert!(result.normal_text.is_empty());
|
||||
assert!(result.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
parser
|
||||
.push("<tool_call>\n<function=get_weather>\n<parameter=location>SF</parameter>")
|
||||
.unwrap();
|
||||
|
||||
assert!(parser.finish().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_malformed_tool_call_fails_fast() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let error = parser.push("<tool_call>\n<bad>\n</tool_call>").unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_missing_parameter_end_fails_fast_after_function_end() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let error = parser
|
||||
.push(
|
||||
"<tool_call>\n<function=get_weather>\n<parameter=location>SF</function>\n</tool_call>",
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_function_body_trims_one_wrapping_newline() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(
|
||||
"<tool_call>\n<function=get_weather>\n<parameter=location>\nHangzhou\n</parameter>\n</function>\n</tool_call>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&result.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "Hangzhou" })
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
use serde_json::json;
|
||||
|
||||
use super::{ToolParseResult, ToolParser};
|
||||
use crate::Tool;
|
||||
|
||||
/// Build a reusable set of function tools for parser unit tests.
|
||||
pub fn test_tools() -> Vec<Tool> {
|
||||
vec![
|
||||
Tool {
|
||||
name: "get_weather".to_string(),
|
||||
description: None,
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": { "type": "string" },
|
||||
"city": { "type": "string" },
|
||||
"state": { "type": "string" },
|
||||
"unit": { "type": "string" },
|
||||
"date": { "type": "string" },
|
||||
"days": { "type": "integer" }
|
||||
}
|
||||
}),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "add".to_string(),
|
||||
description: None,
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"x": { "type": "integer" },
|
||||
"y": { "type": "integer" }
|
||||
}
|
||||
}),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "convert".to_string(),
|
||||
description: None,
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"whole": { "type": "number" },
|
||||
"flag": { "type": "boolean" },
|
||||
"payload": { "type": "object" },
|
||||
"items": { "type": "array" },
|
||||
"empty": { "type": "string" }
|
||||
}
|
||||
}),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "calculate_area".to_string(),
|
||||
description: None,
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"shape": { "type": "string" },
|
||||
"dimensions": { "type": "object" },
|
||||
"precision": { "type": "integer" }
|
||||
}
|
||||
}),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "update_record".to_string(),
|
||||
description: None,
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"anyOf": [
|
||||
{ "type": "object" },
|
||||
{ "type": "null" }
|
||||
]
|
||||
}
|
||||
}
|
||||
}),
|
||||
strict: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// Push chunks through a streaming parser and coalesce its tool-call deltas.
|
||||
pub fn collect_stream<T: ToolParser + ?Sized>(parser: &mut T, chunks: &[&str]) -> ToolParseResult {
|
||||
let mut result = ToolParseResult::default();
|
||||
for chunk in chunks {
|
||||
result.append(parser.push(chunk).unwrap());
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
result.coalesce_calls()
|
||||
}
|
||||
|
||||
/// Split text into chunks containing at most `chunk_chars` Unicode scalar
|
||||
/// values.
|
||||
pub fn split_by_chars(text: &str, chunk_chars: usize) -> Vec<&str> {
|
||||
let mut chunks = Vec::new();
|
||||
let mut start = 0;
|
||||
let mut count = 0;
|
||||
|
||||
for (index, _) in text.char_indices() {
|
||||
if count == chunk_chars {
|
||||
chunks.push(&text[start..index]);
|
||||
start = index;
|
||||
count = 0;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
|
||||
if start < text.len() {
|
||||
chunks.push(&text[start..]);
|
||||
}
|
||||
|
||||
chunks
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
use super::{Result, Tool, ToolCallDelta, ToolParseResult, ToolParser};
|
||||
|
||||
struct DefaultParser;
|
||||
|
||||
impl ToolParser for DefaultParser {
|
||||
fn create(_tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self))
|
||||
}
|
||||
|
||||
fn push(&mut self, _chunk: &str) -> Result<ToolParseResult> {
|
||||
Ok(ToolParseResult::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_parser_does_not_preserve_special_tokens_by_default() {
|
||||
let parser = DefaultParser;
|
||||
|
||||
assert!(!parser.preserve_special_tokens());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_parse_complete_delegates_through_push_and_finish() {
|
||||
struct StreamingParser;
|
||||
|
||||
impl ToolParser for StreamingParser {
|
||||
fn create(_tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self))
|
||||
}
|
||||
|
||||
fn push(&mut self, _chunk: &str) -> Result<ToolParseResult> {
|
||||
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 finish(&mut self) -> Result<ToolParseResult> {
|
||||
Ok(ToolParseResult {
|
||||
normal_text: "suffix".to_string(),
|
||||
calls: vec![
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: None,
|
||||
arguments: "}".to_string(),
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: None,
|
||||
arguments: "\"UTC\"}".to_string(),
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let mut parser = StreamingParser;
|
||||
let result = parser.parse_complete("ignored").unwrap();
|
||||
assert_eq!(result.normal_text, "prefix suffix");
|
||||
assert_eq!(
|
||||
result.calls,
|
||||
vec![
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some("weather".to_string()),
|
||||
arguments: "{\"location\":\"Paris\"}".to_string(),
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some("time".to_string()),
|
||||
arguments: "{\"timezone\":\"UTC\"}".to_string(),
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,581 @@
|
||||
//! Shared helpers for tool parsers.
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use winnow::error::{ContextError, ErrMode, ModalResult, Needed, StrContext, StrContextValue};
|
||||
use winnow::stream::{Offset, Partial, Stream};
|
||||
|
||||
use super::Result;
|
||||
|
||||
/// Return the byte length of the longest proper prefix of `token` that is also
|
||||
/// a suffix of `buffer`.
|
||||
///
|
||||
/// Streaming parsers use this to keep only the trailing fragment that might
|
||||
/// still grow into a full marker after the next decoded chunk arrives.
|
||||
///
|
||||
/// The returned length is always a valid UTF-8 boundary in `token`, so callers
|
||||
/// can safely slice `&token[..len]` even when markers contain non-ASCII
|
||||
/// characters such as DeepSeek's DSML delimiters.
|
||||
pub(super) fn partial_prefix_len(buffer: &str, token: &str) -> usize {
|
||||
let Some(first_byte) = token.as_bytes().first().copied() else {
|
||||
return 0;
|
||||
};
|
||||
|
||||
let max_len = buffer.len().min(token.len().saturating_sub(1));
|
||||
let tail_start = buffer.len() - max_len;
|
||||
let buffer_bytes = buffer.as_bytes();
|
||||
let token_bytes = token.as_bytes();
|
||||
|
||||
// Scan from the longest possible suffix to preserve overlapping prefixes.
|
||||
for index in tail_start..buffer.len() {
|
||||
if buffer_bytes[index] != first_byte {
|
||||
continue;
|
||||
}
|
||||
|
||||
let len = buffer.len() - index;
|
||||
if buffer.is_char_boundary(index)
|
||||
&& token.is_char_boundary(len)
|
||||
&& token_bytes[..len] == buffer_bytes[index..]
|
||||
{
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next marker.
|
||||
///
|
||||
/// Returns the text length in bytes, and advances the input.
|
||||
pub(super) fn safe_text_len(input: &mut Partial<&str>, marker: &str) -> ModalResult<usize> {
|
||||
let text = **input;
|
||||
if text.is_empty() {
|
||||
return incomplete();
|
||||
}
|
||||
|
||||
if let Some(start_idx) = text.find(marker) {
|
||||
input.next_slice(start_idx);
|
||||
return Ok(start_idx);
|
||||
}
|
||||
|
||||
let keep_len = partial_prefix_len(text, marker);
|
||||
let emit_len = text.len().saturating_sub(keep_len);
|
||||
if emit_len == 0 {
|
||||
return incomplete();
|
||||
}
|
||||
|
||||
input.next_slice(emit_len);
|
||||
Ok(emit_len)
|
||||
}
|
||||
|
||||
/// Decode XML/HTML entities in XML-style parameter values.
|
||||
pub(super) fn xml_unescape(value: &str) -> Cow<'_, str> {
|
||||
if !value.as_bytes().contains(&b'&') {
|
||||
return Cow::Borrowed(value);
|
||||
}
|
||||
|
||||
let mut output: Option<String> = None;
|
||||
let mut copied_len = 0;
|
||||
let mut rest = value;
|
||||
|
||||
while let Some(ampersand) = rest.find('&') {
|
||||
let before_ampersand = &rest[..ampersand];
|
||||
let after_ampersand = &rest[ampersand + '&'.len_utf8()..];
|
||||
if let Some(semicolon) = after_ampersand.find(';') {
|
||||
let entity = &after_ampersand[..semicolon];
|
||||
if let Some(decoded) = decode_xml_entity(entity) {
|
||||
match &mut output {
|
||||
Some(output) => output.push_str(before_ampersand),
|
||||
None => {
|
||||
let mut new_output = String::with_capacity(value.len());
|
||||
new_output.push_str(&value[..copied_len + ampersand]);
|
||||
output = Some(new_output);
|
||||
}
|
||||
}
|
||||
let output = output.as_mut().expect("output is initialized above");
|
||||
output.push(decoded);
|
||||
let consumed_len = ampersand + '&'.len_utf8() + semicolon + ';'.len_utf8();
|
||||
copied_len += consumed_len;
|
||||
rest = &rest[consumed_len..];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(output) = &mut output {
|
||||
output.push_str(before_ampersand);
|
||||
output.push('&');
|
||||
}
|
||||
let consumed_len = ampersand + '&'.len_utf8();
|
||||
copied_len += consumed_len;
|
||||
rest = after_ampersand;
|
||||
}
|
||||
|
||||
if let Some(mut output) = output {
|
||||
output.push_str(rest);
|
||||
Cow::Owned(output)
|
||||
} else {
|
||||
Cow::Borrowed(value)
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_xml_entity(entity: &str) -> Option<char> {
|
||||
match entity {
|
||||
"amp" => Some('&'),
|
||||
"lt" => Some('<'),
|
||||
"gt" => Some('>'),
|
||||
"quot" => Some('"'),
|
||||
"apos" => Some('\''),
|
||||
entity if entity.starts_with("#x") || entity.starts_with("#X") => {
|
||||
u32::from_str_radix(&entity[2..], 16).ok().and_then(char::from_u32)
|
||||
}
|
||||
entity if entity.starts_with('#') => {
|
||||
entity[1..].parse::<u32>().ok().and_then(char::from_u32)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Streaming lexical state for a top-level JSON object.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(super) struct JsonObjectScanState {
|
||||
object_depth: usize,
|
||||
array_depth: usize,
|
||||
in_string: bool,
|
||||
escape: bool,
|
||||
phase: JsonObjectScanPhase,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
enum JsonObjectScanPhase {
|
||||
#[default]
|
||||
Initial,
|
||||
Scanning,
|
||||
Complete,
|
||||
}
|
||||
|
||||
impl JsonObjectScanState {
|
||||
/// Returns whether the top-level JSON object has closed.
|
||||
pub(super) const fn complete(&self) -> bool {
|
||||
matches!(self.phase, JsonObjectScanPhase::Complete)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a raw top-level JSON object argument prefix.
|
||||
///
|
||||
/// The returned length is safe to emit as raw argument text. This scans only
|
||||
/// lexical boundaries from `{` through the matching `}`, preserving
|
||||
/// malformed-but-balanced JSON without deserializing or normalizing it.
|
||||
pub(super) fn take_json_object(
|
||||
input: &mut Partial<&str>,
|
||||
state: &mut JsonObjectScanState,
|
||||
) -> ModalResult<usize> {
|
||||
let text = **input;
|
||||
if text.is_empty() {
|
||||
return incomplete();
|
||||
}
|
||||
if state.complete() {
|
||||
return Err(json_scan_error(
|
||||
"JSON object argument",
|
||||
StrContextValue::Description("active JSON object scan"),
|
||||
));
|
||||
}
|
||||
|
||||
let bytes = text.as_bytes();
|
||||
let just_started = matches!(state.phase, JsonObjectScanPhase::Initial);
|
||||
if just_started {
|
||||
if bytes[0] != b'{' {
|
||||
return Err(json_scan_error(
|
||||
"JSON object argument",
|
||||
StrContextValue::CharLiteral('{'),
|
||||
));
|
||||
}
|
||||
state.phase = JsonObjectScanPhase::Scanning;
|
||||
state.object_depth = 1;
|
||||
}
|
||||
|
||||
let mut index = usize::from(just_started);
|
||||
|
||||
while index < bytes.len() {
|
||||
let byte = bytes[index];
|
||||
index += 1;
|
||||
|
||||
if state.in_string {
|
||||
if state.escape {
|
||||
state.escape = false;
|
||||
} else if byte == b'\\' {
|
||||
state.escape = true;
|
||||
} else if byte == b'"' {
|
||||
state.in_string = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
match byte {
|
||||
b'"' => state.in_string = true,
|
||||
b'{' => state.object_depth += 1,
|
||||
b'}' => {
|
||||
state.object_depth = state.object_depth.checked_sub(1).ok_or_else(|| {
|
||||
json_scan_error(
|
||||
"JSON object argument",
|
||||
StrContextValue::Description("balanced object braces"),
|
||||
)
|
||||
})?;
|
||||
if state.object_depth == 0 && state.array_depth == 0 {
|
||||
state.phase = JsonObjectScanPhase::Complete;
|
||||
input.next_slice(index);
|
||||
return Ok(index);
|
||||
}
|
||||
if state.object_depth == 0 {
|
||||
return Err(json_scan_error(
|
||||
"JSON object argument",
|
||||
StrContextValue::Description(
|
||||
"nested arrays to close before the top-level object",
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
b'[' => state.array_depth += 1,
|
||||
b']' => {
|
||||
state.array_depth = state.array_depth.checked_sub(1).ok_or_else(|| {
|
||||
json_scan_error(
|
||||
"JSON object argument",
|
||||
StrContextValue::Description("balanced array brackets"),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
input.next_slice(text.len());
|
||||
Ok(text.len())
|
||||
}
|
||||
|
||||
/// Parse a JSON string literal.
|
||||
pub(super) fn json_str(input: &mut Partial<&str>) -> ModalResult<String> {
|
||||
let text = **input;
|
||||
if text.is_empty() {
|
||||
return incomplete();
|
||||
}
|
||||
|
||||
let bytes = text.as_bytes();
|
||||
if bytes[0] != b'"' {
|
||||
return Err(json_scan_error(
|
||||
"JSON string",
|
||||
StrContextValue::CharLiteral('"'),
|
||||
));
|
||||
}
|
||||
|
||||
let mut escape = false;
|
||||
let mut index = 1;
|
||||
while index < bytes.len() {
|
||||
let byte = bytes[index];
|
||||
index += 1;
|
||||
|
||||
if escape {
|
||||
escape = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
match byte {
|
||||
b'\\' => escape = true,
|
||||
b'"' => {
|
||||
let raw = &text[..index];
|
||||
let value = serde_json::from_str::<String>(raw).map_err(|_| {
|
||||
json_scan_error(
|
||||
"JSON string",
|
||||
StrContextValue::Description("valid JSON string"),
|
||||
)
|
||||
})?;
|
||||
input.next_slice(index);
|
||||
return Ok(value);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
incomplete()
|
||||
}
|
||||
|
||||
fn json_scan_error(label: &'static str, expected: StrContextValue) -> ErrMode<ContextError> {
|
||||
let mut error = ContextError::new();
|
||||
error.push(StrContext::Label(label));
|
||||
error.push(StrContext::Expected(expected));
|
||||
ErrMode::Cut(error)
|
||||
}
|
||||
|
||||
/// Parse one event from a buffered streaming input.
|
||||
///
|
||||
/// Returns:
|
||||
/// - `Ok(Some((event, consumed_len)))` if an event was successfully parsed, along with the number
|
||||
/// of bytes consumed from the buffer.
|
||||
/// - `Ok(None)` if the buffer does not contain a full event yet, and more data is needed.
|
||||
/// - `Err` if a parsing error occurred.
|
||||
pub(super) fn parse_buffered_event<E>(
|
||||
buffer: &str,
|
||||
parse: impl FnOnce(&mut Partial<&str>) -> ModalResult<E>,
|
||||
) -> Result<Option<(E, usize)>> {
|
||||
let mut input = Partial::new(buffer);
|
||||
let checkpoint = input.checkpoint();
|
||||
let event = match parse(&mut input) {
|
||||
Ok(event) => event,
|
||||
Err(ErrMode::Incomplete(_)) => return Ok(None),
|
||||
Err(ErrMode::Backtrack(e) | ErrMode::Cut(e)) => {
|
||||
// TODO: enrich context for error reporting
|
||||
return Err(parsing_failed!("{}", e));
|
||||
}
|
||||
};
|
||||
let consumed_len = input.offset_from(&checkpoint);
|
||||
if consumed_len == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
Ok(Some((event, consumed_len)))
|
||||
}
|
||||
|
||||
/// Returns an error indicating that we need more data to continue parsing.
|
||||
pub(super) fn incomplete<T>() -> ModalResult<T> {
|
||||
Err(ErrMode::Incomplete(Needed::Unknown))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::borrow::Cow;
|
||||
|
||||
use expect_test::expect;
|
||||
use winnow::error::ErrMode;
|
||||
use winnow::stream::{Offset, Partial, Stream};
|
||||
|
||||
use super::{
|
||||
JsonObjectScanState, json_str, partial_prefix_len, safe_text_len, take_json_object,
|
||||
xml_unescape,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn partial_prefix_len_handles_ascii_markers() {
|
||||
assert_eq!(
|
||||
partial_prefix_len("hello<|tool", "<|tool_call>"),
|
||||
"<|tool".len()
|
||||
);
|
||||
assert_eq!(partial_prefix_len("hello world", "<|tool_call>"), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partial_prefix_len_prefers_longest_overlapping_prefix() {
|
||||
assert_eq!(partial_prefix_len("chunk ending in aba", "ababa"), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partial_prefix_len_handles_unicode_markers() {
|
||||
let token = "<|DSML|function_calls>";
|
||||
assert_eq!(
|
||||
partial_prefix_len("prefix <|DSML|fun", token),
|
||||
"<|DSML|fun".len()
|
||||
);
|
||||
assert_eq!(partial_prefix_len("prefix <|DSML", token), "<|DSML".len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn safe_text_len_stops_before_marker() {
|
||||
let mut input = Partial::new("hello<tool_call>");
|
||||
let checkpoint = input.checkpoint();
|
||||
|
||||
let len = safe_text_len(&mut input, "<tool_call>").unwrap();
|
||||
|
||||
assert_eq!(len, "hello".len());
|
||||
assert_eq!(input.offset_from(&checkpoint), "hello".len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn safe_text_len_holds_back_partial_marker() {
|
||||
let mut input = Partial::new("hello<tool");
|
||||
let checkpoint = input.checkpoint();
|
||||
|
||||
let len = safe_text_len(&mut input, "<tool_call>").unwrap();
|
||||
|
||||
assert_eq!(len, "hello".len());
|
||||
assert_eq!(input.offset_from(&checkpoint), "hello".len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn safe_text_len_reports_incomplete_for_only_partial_marker() {
|
||||
let mut input = Partial::new("<tool");
|
||||
|
||||
let error = safe_text_len(&mut input, "<tool_call>").unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xml_unescape_decodes_common_entities() {
|
||||
assert_eq!(
|
||||
xml_unescape("<tag attr="value">Tom & Jerry's</tag>"),
|
||||
r#"<tag attr="value">Tom & Jerry's</tag>"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xml_unescape_decodes_numeric_entities() {
|
||||
assert_eq!(xml_unescape("<tag>😀"), "<tag>😀");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xml_unescape_preserves_unknown_and_incomplete_entities() {
|
||||
let output = xml_unescape("Tom & Jerry &unknown; &");
|
||||
|
||||
assert!(matches!(output, Cow::Borrowed(_)));
|
||||
assert_eq!(output, "Tom & Jerry &unknown; &");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xml_unescape_borrows_when_no_entity_is_present() {
|
||||
let input = "plain text";
|
||||
let output = xml_unescape(input);
|
||||
|
||||
assert!(matches!(output, Cow::Borrowed(_)));
|
||||
assert_eq!(output, input);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_consumes_simple_object() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
let buffer = r#"{"location":"Paris"}<end>"#;
|
||||
let mut input = Partial::new(buffer);
|
||||
let checkpoint = input.checkpoint();
|
||||
|
||||
let len = take_json_object(&mut input, &mut state).unwrap();
|
||||
|
||||
assert_eq!(len, r#"{"location":"Paris"}"#.len());
|
||||
assert_eq!(input.offset_from(&checkpoint), len);
|
||||
assert!(state.complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_tracks_nested_values_and_strings() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
let arguments = r#"{"nested":{"items":[{"text":"} <|tool_call_end|> \" \\"}]}}"#;
|
||||
let buffer = format!("{arguments}<end>");
|
||||
let mut input = Partial::new(buffer.as_str());
|
||||
|
||||
let len = take_json_object(&mut input, &mut state).unwrap();
|
||||
|
||||
assert_eq!(len, arguments.len());
|
||||
assert!(state.complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_rejects_leading_whitespace() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
let mut input = Partial::new(" {\"x\":1}");
|
||||
|
||||
let error = take_json_object(&mut input, &mut state).unwrap_err();
|
||||
|
||||
let ErrMode::Cut(error) = error else {
|
||||
panic!("expected cut error");
|
||||
};
|
||||
expect![[r#"
|
||||
invalid JSON object argument
|
||||
expected `{`"#]]
|
||||
.assert_eq(&error.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_leaves_trailing_whitespace_to_caller() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
let mut input = Partial::new("{\"x\":1}\n<end>");
|
||||
let checkpoint = input.checkpoint();
|
||||
|
||||
let len = take_json_object(&mut input, &mut state).unwrap();
|
||||
|
||||
assert_eq!(len, "{\"x\":1}".len());
|
||||
assert_eq!(input.offset_from(&checkpoint), len);
|
||||
assert!(state.complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_continues_across_chunks() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
let chunks = [
|
||||
r#"{"text":"literal "#,
|
||||
r#"<|tool_call_end|>"#,
|
||||
r#" inside"}<end>"#,
|
||||
];
|
||||
let mut collected = String::new();
|
||||
|
||||
for chunk in chunks {
|
||||
let mut input = Partial::new(chunk);
|
||||
let len = take_json_object(&mut input, &mut state).unwrap();
|
||||
collected.push_str(&chunk[..len]);
|
||||
}
|
||||
|
||||
assert_eq!(collected, r#"{"text":"literal <|tool_call_end|> inside"}"#);
|
||||
assert!(state.complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_rejects_non_object_top_level() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
let mut input = Partial::new(r#"[{"x":1}]"#);
|
||||
|
||||
let error = take_json_object(&mut input, &mut state).unwrap_err();
|
||||
|
||||
let ErrMode::Cut(error) = error else {
|
||||
panic!("expected cut error");
|
||||
};
|
||||
expect![[r#"
|
||||
invalid JSON object argument
|
||||
expected `{`"#]]
|
||||
.assert_eq(&error.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_reports_unbalanced_array() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
let mut input = Partial::new(r#"{"x":]}"#);
|
||||
|
||||
let error = take_json_object(&mut input, &mut state).unwrap_err();
|
||||
|
||||
let ErrMode::Cut(error) = error else {
|
||||
panic!("expected cut error");
|
||||
};
|
||||
expect![[r#"
|
||||
invalid JSON object argument
|
||||
expected balanced array brackets"#]]
|
||||
.assert_eq(&error.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_reports_top_level_close_before_nested_array() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
let mut input = Partial::new(r#"{"x":[}"#);
|
||||
|
||||
let error = take_json_object(&mut input, &mut state).unwrap_err();
|
||||
|
||||
let ErrMode::Cut(error) = error else {
|
||||
panic!("expected cut error");
|
||||
};
|
||||
expect![[r#"
|
||||
invalid JSON object argument
|
||||
expected nested arrays to close before the top-level object"#]]
|
||||
.assert_eq(&error.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_str_decodes_escaped_content() {
|
||||
let mut input = Partial::new(r#""say_\"hi\u0021" rest"#);
|
||||
|
||||
let value = json_str(&mut input).unwrap();
|
||||
|
||||
assert_eq!(value, "say_\"hi!");
|
||||
assert_eq!(*input, " rest");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_str_reports_incomplete_escaped_string() {
|
||||
let mut input = Partial::new(r#""say_\"#);
|
||||
|
||||
let error = json_str(&mut input).unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user