[Rust Frontend] Introduce unified parser interface & combined parser (#46583)
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
[package]
|
||||
name = "vllm-parser"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[features]
|
||||
test-util = []
|
||||
|
||||
[dependencies]
|
||||
easy-ext.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
thiserror.workspace = true
|
||||
thiserror-ext.workspace = true
|
||||
vllm-tokenizer.workspace = true
|
||||
winnow.workspace = true
|
||||
xgrammar-structural-tag.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_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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,155 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use vllm_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{Gemma4ToolParser, Tool, ToolParser};
|
||||
|
||||
mod utils;
|
||||
use utils::feed_parser;
|
||||
|
||||
const CHUNK_CHARS: usize = 7;
|
||||
const LONG_NORMAL_TEXT_REPEATS: usize = 2048;
|
||||
const LONG_TOOL_ARGUMENT_REPEATS: usize = 256;
|
||||
|
||||
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 long_tool_argument_fixture() -> String {
|
||||
let line =
|
||||
"<section><p>Literal } and <tool_call|> marker-shaped text inside content.</p></section>\n";
|
||||
format!(
|
||||
concat!(
|
||||
"I will write the file.\n",
|
||||
"<|tool_call>",
|
||||
"call:write_file{{",
|
||||
"path:<|\"|>index.html<|\"|>,",
|
||||
"content:<|\"|>{}<|\"|>",
|
||||
"}}",
|
||||
"<tool_call|>",
|
||||
"Done."
|
||||
),
|
||||
line.repeat(LONG_TOOL_ARGUMENT_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_tool_argument = long_tool_argument_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_tool_argument",
|
||||
&tools,
|
||||
&long_tool_argument,
|
||||
CHUNK_CHARS,
|
||||
"I will write the file.\nDone.",
|
||||
1,
|
||||
);
|
||||
|
||||
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_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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,161 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main};
|
||||
use tool_parser::parsers::QwenCoderParser as ExternalQwenCoderParser;
|
||||
use vllm_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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;
|
||||
const LONG_TOOL_BODY_REPEATS: usize = 8192;
|
||||
|
||||
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 long_tool_call_fixture() -> String {
|
||||
let location = "x".repeat(LONG_TOOL_BODY_REPEATS);
|
||||
format!(
|
||||
"<tool_call>\n\
|
||||
<function=get_weather>\n\
|
||||
<parameter=location>{location}</parameter>\n\
|
||||
</function>\n\
|
||||
</tool_call>"
|
||||
)
|
||||
}
|
||||
|
||||
fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> {
|
||||
Qwen3CoderToolParser::create(tools).expect("Qwen Coder parser should initialize")
|
||||
}
|
||||
|
||||
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();
|
||||
let long_tool_call = long_tool_call_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,
|
||||
);
|
||||
|
||||
run_stream_group(
|
||||
c,
|
||||
"qwen3_coder/long_tool_call_body",
|
||||
&tools,
|
||||
&long_tool_call,
|
||||
CHUNK_CHARS,
|
||||
"",
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
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_parser::tool::test_utils::{split_by_chars, test_tools};
|
||||
use vllm_parser::tool::{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_parser::tool::test_utils::collect_stream;
|
||||
use vllm_parser::tool::{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,19 @@
|
||||
[package]
|
||||
name = "vllm-tool-parser-py"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[lib]
|
||||
name = "_rust_tool_parser"
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
pyo3.workspace = true
|
||||
pythonize = { workspace = true, features = ["serde_json"] }
|
||||
serde_json.workspace = true
|
||||
thiserror-ext.workspace = true
|
||||
vllm-parser.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,394 @@
|
||||
//! Thin PyO3 bindings for `vllm_parser::tool`.
|
||||
//!
|
||||
//! This crate exposes the Rust tool parser trait and data shapes to Python
|
||||
//! while keeping parser state, grammar, and schema-aware argument conversion in
|
||||
//! Rust. Python callers should use this module as a typed bridge and keep any
|
||||
//! vLLM protocol adaptation outside the binding.
|
||||
|
||||
use pyo3::exceptions::PyValueError;
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::types::{PyAny, PyModule};
|
||||
use pythonize::{depythonize, pythonize};
|
||||
use serde_json::Value;
|
||||
use thiserror_ext::AsReport as _;
|
||||
use vllm_parser::tool::{Tool, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
|
||||
macro_rules! tool_parser_factory {
|
||||
($($parser:ident),+ $(,)?) => {
|
||||
fn create_tool_parser(
|
||||
name: &str,
|
||||
tools: &[Tool],
|
||||
) -> PyResult<Box<dyn ToolParser>> {
|
||||
match name {
|
||||
$(
|
||||
stringify!($parser) => {
|
||||
<vllm_parser::tool::$parser as ToolParser>::create(tools)
|
||||
}
|
||||
)+
|
||||
_ => {
|
||||
return Err(PyValueError::new_err(format!(
|
||||
"unsupported tool parser `{name}`"
|
||||
)));
|
||||
}
|
||||
}
|
||||
.map_err(|error| PyValueError::new_err(error.to_report_string()))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Export a tool parser to Python by registering it here.
|
||||
tool_parser_factory! {
|
||||
MinimaxM3ToolParser,
|
||||
|
||||
// Below are the parsers just for testing purposes on Python side.
|
||||
DeepSeekV4ToolParser,
|
||||
KimiK2ToolParser,
|
||||
}
|
||||
|
||||
#[pyclass(name = "Tool", module = "vllm._rust_tool_parser", skip_from_py_object)]
|
||||
#[derive(Clone)]
|
||||
struct PyTool(Tool);
|
||||
|
||||
#[pymethods]
|
||||
impl PyTool {
|
||||
#[new]
|
||||
#[pyo3(signature = (name, description, parameters, strict=None))]
|
||||
fn new(
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
parameters: &Bound<'_, PyAny>,
|
||||
strict: Option<bool>,
|
||||
) -> PyResult<Self> {
|
||||
let parameters = depythonize::<Value>(parameters).map_err(|error| {
|
||||
PyValueError::new_err(format!(
|
||||
"failed to convert tool parameters from Python to JSON: {error}"
|
||||
))
|
||||
})?;
|
||||
Ok(Self(Tool {
|
||||
name,
|
||||
description,
|
||||
parameters,
|
||||
strict,
|
||||
}))
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn name(&self) -> &str {
|
||||
&self.0.name
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn description(&self) -> Option<&str> {
|
||||
self.0.description.as_deref()
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn parameters(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
|
||||
pythonize(py, &self.0.parameters).map(Bound::unbind).map_err(|error| {
|
||||
PyValueError::new_err(format!(
|
||||
"failed to convert tool parameters from JSON to Python: {error}"
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn strict(&self) -> Option<bool> {
|
||||
self.0.strict
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(
|
||||
name = "ToolCallDelta",
|
||||
module = "vllm._rust_tool_parser",
|
||||
skip_from_py_object
|
||||
)]
|
||||
#[derive(Clone)]
|
||||
struct PyToolCallDelta(ToolCallDelta);
|
||||
|
||||
#[pymethods]
|
||||
impl PyToolCallDelta {
|
||||
#[new]
|
||||
#[pyo3(signature = (tool_index, name, arguments))]
|
||||
fn new(tool_index: usize, name: Option<String>, arguments: String) -> Self {
|
||||
Self(ToolCallDelta {
|
||||
tool_index,
|
||||
name,
|
||||
arguments,
|
||||
})
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn tool_index(&self) -> usize {
|
||||
self.0.tool_index
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn name(&self) -> Option<&str> {
|
||||
self.0.name.as_deref()
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn arguments(&self) -> &str {
|
||||
&self.0.arguments
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(
|
||||
name = "ToolParserOutput",
|
||||
module = "vllm._rust_tool_parser",
|
||||
skip_from_py_object
|
||||
)]
|
||||
#[derive(Clone)]
|
||||
struct PyToolParserOutput(ToolParserOutput);
|
||||
|
||||
#[pymethods]
|
||||
impl PyToolParserOutput {
|
||||
#[new]
|
||||
#[pyo3(signature = (normal_text="", calls=None))]
|
||||
fn new(py: Python<'_>, normal_text: &str, calls: Option<Vec<Py<PyToolCallDelta>>>) -> Self {
|
||||
let calls =
|
||||
calls.unwrap_or_default().iter().map(|call| call.borrow(py).0.clone()).collect();
|
||||
Self(ToolParserOutput {
|
||||
normal_text: normal_text.to_owned(),
|
||||
calls,
|
||||
})
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn normal_text(&self) -> &str {
|
||||
&self.0.normal_text
|
||||
}
|
||||
|
||||
#[getter]
|
||||
fn calls(&self) -> Vec<PyToolCallDelta> {
|
||||
self.0.calls.iter().cloned().map(PyToolCallDelta).collect()
|
||||
}
|
||||
|
||||
fn append(&mut self, other: PyRef<'_, PyToolParserOutput>) {
|
||||
self.0.append(other.0.clone());
|
||||
}
|
||||
|
||||
fn coalesce_calls(&self) -> Self {
|
||||
Self(self.0.clone().coalesce_calls())
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(name = "ToolParser", module = "vllm._rust_tool_parser", unsendable)]
|
||||
struct PyToolParser(Box<dyn ToolParser>);
|
||||
|
||||
impl PyToolParser {
|
||||
fn parse_into_output(&mut self, chunk: &str, output: &mut PyToolParserOutput) -> PyResult<()> {
|
||||
self.0
|
||||
.parse_into(chunk, &mut output.0)
|
||||
.map_err(|error| PyValueError::new_err(error.to_report_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl PyToolParser {
|
||||
#[new]
|
||||
fn new(py: Python<'_>, parser_name: &str, tools: Vec<Py<PyTool>>) -> PyResult<Self> {
|
||||
let tools = tools.iter().map(|tool| tool.borrow(py).0.clone()).collect::<Vec<_>>();
|
||||
create_tool_parser(parser_name, &tools).map(Self)
|
||||
}
|
||||
|
||||
fn parse_into(
|
||||
&mut self,
|
||||
chunk: &str,
|
||||
mut output: PyRefMut<'_, PyToolParserOutput>,
|
||||
) -> PyResult<()> {
|
||||
self.parse_into_output(chunk, &mut output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> PyResult<PyToolParserOutput> {
|
||||
self.0
|
||||
.finish()
|
||||
.map(PyToolParserOutput)
|
||||
.map_err(|error| PyValueError::new_err(error.to_report_string()))
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.0.reset()
|
||||
}
|
||||
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
self.0.preserve_special_tokens()
|
||||
}
|
||||
|
||||
fn tool_call_id(&self, tool_index: usize) -> Option<&str> {
|
||||
self.0.tool_call_id(tool_index)
|
||||
}
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
fn _rust_tool_parser(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_class::<PyTool>()?;
|
||||
m.add_class::<PyToolCallDelta>()?;
|
||||
m.add_class::<PyToolParserOutput>()?;
|
||||
m.add_class::<PyToolParser>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn with_python<R>(f: impl for<'py> FnOnce(Python<'py>) -> R) -> R {
|
||||
Python::initialize();
|
||||
Python::attach(f)
|
||||
}
|
||||
|
||||
fn tool_schema() -> Value {
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_id": {"type": "integer"},
|
||||
"shipping": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {"type": "string"},
|
||||
"zip": {"type": "integer"}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn build_call() -> String {
|
||||
r#"<|DSML|tool_calls>
|
||||
<|DSML|invoke name="create_order">
|
||||
<|DSML|parameter name="user_id" string="false">42</|DSML|parameter>
|
||||
<|DSML|parameter name="shipping" string="false">{"city":"Singapore","zip":18956}</|DSML|parameter>
|
||||
</|DSML|invoke>
|
||||
</|DSML|tool_calls>"#
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
fn make_py_tool(py: Python<'_>) -> PyResult<Py<PyTool>> {
|
||||
let parameters = pythonize(py, &tool_schema()).map_err(|error| {
|
||||
PyValueError::new_err(format!(
|
||||
"failed to convert test schema from JSON to Python: {error}"
|
||||
))
|
||||
})?;
|
||||
Py::new(
|
||||
py,
|
||||
PyTool::new(
|
||||
"create_order".to_owned(),
|
||||
Some("Create an order".to_owned()),
|
||||
¶meters,
|
||||
None,
|
||||
)?,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_round_trips_typed_fields() {
|
||||
with_python(|py| {
|
||||
let tool = make_py_tool(py)?;
|
||||
let borrowed = tool.borrow(py);
|
||||
assert_eq!(borrowed.name(), "create_order");
|
||||
assert_eq!(borrowed.description(), Some("Create an order"));
|
||||
assert_eq!(borrowed.strict(), None);
|
||||
|
||||
let parameters = borrowed.parameters(py)?;
|
||||
let parameters = depythonize::<Value>(parameters.bind(py))?;
|
||||
assert_eq!(parameters, tool_schema());
|
||||
PyResult::Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_append_and_coalesce_calls() {
|
||||
with_python(|py| {
|
||||
let first = Py::new(
|
||||
py,
|
||||
PyToolCallDelta::new(0, Some("create_order".to_owned()), "{\"a\"".to_owned()),
|
||||
)?;
|
||||
let second = Py::new(py, PyToolCallDelta::new(0, None, ":1}".to_owned()))?;
|
||||
let mut output = PyToolParserOutput::new(py, "text", Some(vec![first]));
|
||||
let other = Py::new(py, PyToolParserOutput::new(py, "", Some(vec![second])))?;
|
||||
output.append(other.borrow(py));
|
||||
|
||||
let coalesced = output.coalesce_calls();
|
||||
assert_eq!(coalesced.normal_text(), "text");
|
||||
let calls = coalesced.calls();
|
||||
assert_eq!(calls.len(), 1);
|
||||
assert_eq!(calls[0].tool_index(), 0);
|
||||
assert_eq!(calls[0].name(), Some("create_order"));
|
||||
assert_eq!(calls[0].arguments(), "{\"a\":1}");
|
||||
PyResult::Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parser_parse_finish_and_preserve_special_tokens() {
|
||||
with_python(|py| {
|
||||
let tool = make_py_tool(py)?;
|
||||
let mut parser = PyToolParser::new(py, "DeepSeekV4ToolParser", vec![tool])?;
|
||||
assert!(parser.preserve_special_tokens());
|
||||
|
||||
let mut output = PyToolParserOutput::new(py, "", None);
|
||||
parser.parse_into_output(&build_call(), &mut output)?;
|
||||
let finish = Py::new(py, parser.finish()?)?;
|
||||
output.append(finish.borrow(py));
|
||||
let output = output.coalesce_calls();
|
||||
|
||||
assert_eq!(output.normal_text(), "");
|
||||
let calls = output.calls();
|
||||
assert_eq!(calls.len(), 1);
|
||||
assert_eq!(calls[0].name(), Some("create_order"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(calls[0].arguments()).unwrap(),
|
||||
json!({
|
||||
"user_id": 42,
|
||||
"shipping": {
|
||||
"city": "Singapore",
|
||||
"zip": 18956
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
assert_eq!(parser.reset(), "");
|
||||
PyResult::Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parser_exposes_model_emitted_tool_call_ids() {
|
||||
with_python(|py| {
|
||||
let tool = make_py_tool(py)?;
|
||||
let mut parser = PyToolParser::new(py, "KimiK2ToolParser", vec![tool])?;
|
||||
|
||||
let input = "<|tool_calls_section_begin|>\
|
||||
<|tool_call_begin|>functions.create_order:0<|tool_call_argument_begin|>\
|
||||
{\"user_id\":42}<|tool_call_end|>\
|
||||
<|tool_calls_section_end|>";
|
||||
let mut output = PyToolParserOutput::new(py, "", None);
|
||||
parser.parse_into_output(input, &mut output)?;
|
||||
|
||||
assert_eq!(parser.tool_call_id(0), Some("functions.create_order:0"));
|
||||
assert_eq!(parser.tool_call_id(1), None);
|
||||
PyResult::Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parser_errors_for_unknown_name() {
|
||||
with_python(|py| {
|
||||
let tool = make_py_tool(py)?;
|
||||
let error = match PyToolParser::new(py, "missing", vec![tool]) {
|
||||
Ok(_) => panic!("missing parser name unexpectedly succeeded"),
|
||||
Err(error) => error,
|
||||
};
|
||||
let message = format!("{error}");
|
||||
assert!(message.contains("unsupported tool parser `missing`"));
|
||||
PyResult::Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
//! Streaming parsers for chat completions.
|
||||
|
||||
pub mod reasoning;
|
||||
pub mod tool;
|
||||
pub mod unified;
|
||||
@@ -0,0 +1,45 @@
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningParser, Result};
|
||||
|
||||
/// Reasoning parser for Cohere Command models that use explicit START/END tags.
|
||||
pub struct CohereCmdReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
}
|
||||
|
||||
impl CohereCmdReasoningParser {
|
||||
/// Create a Cohere Command parser backed by the shared delimited state
|
||||
/// machine.
|
||||
pub fn new(tokenizer: DynTokenizer) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(
|
||||
tokenizer,
|
||||
"<|START_THINKING|>",
|
||||
"<|END_THINKING|>",
|
||||
false,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for CohereCmdReasoningParser {
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tokenizer)?))
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.push(delta))
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.finish())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningParser, Result};
|
||||
|
||||
/// Reasoning parser for DeepSeek R1 style outputs.
|
||||
///
|
||||
/// DeepSeek R1 may begin generating directly inside a reasoning span and only
|
||||
/// emit the closing `</think>` delimiter, so the no-boundary fallback defaults
|
||||
/// to `in_reasoning = true`.
|
||||
pub struct DeepSeekR1ReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
}
|
||||
|
||||
impl DeepSeekR1ReasoningParser {
|
||||
/// Create a DeepSeek R1 parser backed by the shared delimited state
|
||||
/// machine.
|
||||
pub fn new(tokenizer: DynTokenizer) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(tokenizer, "<think>", "</think>", true)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for DeepSeekR1ReasoningParser {
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tokenizer)?))
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.push(delta))
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.finish())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
use vllm_tokenizer::{DynTokenizer, Tokenizer};
|
||||
|
||||
use super::{ReasoningDelta, ReasoningError, Result};
|
||||
|
||||
/// Shared incremental state machine for tag-delimited reasoning protocols.
|
||||
///
|
||||
/// This helper is intentionally not a public parser type. Model-family parser
|
||||
/// wrappers own one `DelimitedReasoningParser` internally and expose the
|
||||
/// request-facing [`super::ReasoningParser`] trait.
|
||||
///
|
||||
/// The shared state machine stays generic by deriving its initial
|
||||
/// `current_in_reasoning` state from the prompt token boundary instead of
|
||||
/// hardcoding model-family conventions. That means families with the same
|
||||
/// delimiters can often reuse this implementation even if their chat templates
|
||||
/// prefill different prompts.
|
||||
pub(crate) struct DelimitedReasoningParser {
|
||||
tokenizer: DynTokenizer,
|
||||
current_in_reasoning: bool,
|
||||
buffer: String,
|
||||
start_token: String,
|
||||
end_token: String,
|
||||
start_token_id: u32,
|
||||
end_token_id: u32,
|
||||
default_in_reasoning: bool,
|
||||
}
|
||||
|
||||
impl DelimitedReasoningParser {
|
||||
/// Create one delimited parser state machine.
|
||||
///
|
||||
/// `default_in_reasoning` is only used when prompt initialization sees no
|
||||
/// reasoning boundary token at all. If the prompt contains either the
|
||||
/// start or end delimiter, that prompt boundary always wins.
|
||||
pub(crate) fn new(
|
||||
tokenizer: DynTokenizer,
|
||||
start_token: &'static str,
|
||||
end_token: &'static str,
|
||||
default_in_reasoning: bool,
|
||||
) -> Result<Self> {
|
||||
let start_token_id =
|
||||
tokenizer.token_to_id(start_token).ok_or_else(|| ReasoningError::MissingToken {
|
||||
token: start_token.to_string(),
|
||||
})?;
|
||||
let end_token_id =
|
||||
tokenizer.token_to_id(end_token).ok_or_else(|| ReasoningError::MissingToken {
|
||||
token: end_token.to_string(),
|
||||
})?;
|
||||
|
||||
Ok(Self {
|
||||
tokenizer,
|
||||
current_in_reasoning: default_in_reasoning,
|
||||
buffer: String::new(),
|
||||
start_token: start_token.to_string(),
|
||||
end_token: end_token.to_string(),
|
||||
start_token_id,
|
||||
end_token_id,
|
||||
default_in_reasoning,
|
||||
})
|
||||
}
|
||||
|
||||
/// Initialize the starting state from prompt token IDs.
|
||||
pub(crate) fn initialize(&mut self, prompt_token_ids: &[u32]) {
|
||||
self.current_in_reasoning = last_reasoning_boundary(
|
||||
prompt_token_ids,
|
||||
self.start_token_id,
|
||||
self.end_token_id,
|
||||
self.tokenizer.as_ref(),
|
||||
)
|
||||
.unwrap_or(self.default_in_reasoning);
|
||||
}
|
||||
|
||||
/// Return whether the parser is currently inside a reasoning section.
|
||||
pub(crate) fn in_reasoning(&self) -> bool {
|
||||
self.current_in_reasoning
|
||||
}
|
||||
|
||||
/// Parse one decoded text delta and return its reasoning/content split.
|
||||
pub(crate) fn push(&mut self, delta: &str) -> ReasoningDelta {
|
||||
self.buffer.push_str(delta);
|
||||
|
||||
let partial_suffix_len = self.partial_suffix_len(&self.buffer);
|
||||
let stable_len = self.buffer.len() - partial_suffix_len;
|
||||
let pending_suffix = self.buffer.split_off(stable_len);
|
||||
let stable_text = std::mem::replace(&mut self.buffer, pending_suffix);
|
||||
|
||||
self.parse_stable_text(&stable_text)
|
||||
}
|
||||
|
||||
/// Flush any buffered partial delimiter suffix at end of stream.
|
||||
pub(crate) fn finish(&mut self) -> ReasoningDelta {
|
||||
let stable_text = std::mem::take(&mut self.buffer);
|
||||
self.parse_stable_text(&stable_text)
|
||||
}
|
||||
|
||||
/// Parse text that is known not to end with a partial delimiter suffix.
|
||||
fn parse_stable_text(&mut self, mut stable: &str) -> ReasoningDelta {
|
||||
let mut delta = ReasoningDelta::default();
|
||||
|
||||
while !stable.is_empty() {
|
||||
if self.current_in_reasoning {
|
||||
if let Some(end_idx) = stable.find(&self.end_token) {
|
||||
delta.push_reasoning(&stable[..end_idx]);
|
||||
stable = &stable[end_idx + self.end_token.len()..];
|
||||
self.current_in_reasoning = false;
|
||||
} else {
|
||||
delta.push_reasoning(stable);
|
||||
break;
|
||||
}
|
||||
} else if let Some(start_idx) = stable.find(&self.start_token) {
|
||||
delta.push_content(&stable[..start_idx]);
|
||||
stable = &stable[start_idx + self.start_token.len()..];
|
||||
self.current_in_reasoning = true;
|
||||
} else {
|
||||
delta.push_content(stable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
delta
|
||||
}
|
||||
|
||||
/// Return the longest trailing suffix that could still complete a
|
||||
/// delimiter.
|
||||
fn partial_suffix_len(&self, text: &str) -> usize {
|
||||
let mut best = 0;
|
||||
for idx in text.char_indices().map(|(idx, _)| idx).skip(1) {
|
||||
let suffix = &text[idx..];
|
||||
if self.start_token.starts_with(suffix) && self.start_token != suffix {
|
||||
best = best.max(text.len() - idx);
|
||||
}
|
||||
if self.end_token.starts_with(suffix) && self.end_token != suffix {
|
||||
best = best.max(text.len() - idx);
|
||||
}
|
||||
}
|
||||
|
||||
if self.start_token.starts_with(text) && self.start_token != text {
|
||||
best = best.max(text.len());
|
||||
}
|
||||
if self.end_token.starts_with(text) && self.end_token != text {
|
||||
best = best.max(text.len());
|
||||
}
|
||||
|
||||
best
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine the reasoning state implied by the last prompt boundary, if any.
|
||||
fn last_reasoning_boundary(
|
||||
prompt_token_ids: &[u32],
|
||||
start_token_id: u32,
|
||||
end_token_id: u32,
|
||||
tokenizer: &dyn Tokenizer,
|
||||
) -> Option<bool> {
|
||||
for token_id in prompt_token_ids.iter().rev() {
|
||||
if *token_id == start_token_id {
|
||||
return Some(true);
|
||||
}
|
||||
if *token_id == end_token_id {
|
||||
return Some(false);
|
||||
}
|
||||
if tokenizer.is_special_id(*token_id) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningParser, Result};
|
||||
|
||||
const THOUGHT_PREFIX: &str = "thought\n";
|
||||
|
||||
/// Reasoning parser for Google Gemma4 thinking models.
|
||||
///
|
||||
/// Gemma4 emits reasoning inside `<|channel> ... <channel|>` spans and adds a
|
||||
/// structural `thought\n` label at the beginning of the reasoning channel.
|
||||
/// This parser keeps the delimiter handling in the shared delimited parser and
|
||||
/// only layers on Gemma4-specific request adjustment plus prefix stripping.
|
||||
///
|
||||
/// Original Python implementation:
|
||||
/// <https://github.com/vllm-project/vllm/blob/18b1c77211d8f6fe800bcfb89524d2b598708032/vllm/reasoning/gemma4_reasoning_parser.py#L23>
|
||||
pub struct Gemma4ReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
reasoning_text: String,
|
||||
prefix_stripped: bool,
|
||||
}
|
||||
|
||||
impl Gemma4ReasoningParser {
|
||||
/// Create a Gemma4 parser.
|
||||
pub fn new(tokenizer: DynTokenizer) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(tokenizer, "<|channel>", "<channel|>", false)?,
|
||||
reasoning_text: String::new(),
|
||||
prefix_stripped: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Apply Gemma4's `thought\n` stripping rule to one reasoning delta.
|
||||
///
|
||||
/// Early reasoning text is buffered until we can decide whether it begins
|
||||
/// with the structural channel label.
|
||||
fn strip_thought_prefix(&mut self, reasoning: &str) -> Option<String> {
|
||||
if self.prefix_stripped {
|
||||
return Some(reasoning.to_string());
|
||||
}
|
||||
|
||||
self.reasoning_text.push_str(reasoning);
|
||||
|
||||
if self.reasoning_text.starts_with(THOUGHT_PREFIX) {
|
||||
let prefix_len = THOUGHT_PREFIX.len();
|
||||
let previous_len = self.reasoning_text.len() - reasoning.len();
|
||||
if previous_len >= prefix_len {
|
||||
self.reasoning_text.clear();
|
||||
self.prefix_stripped = true;
|
||||
return Some(reasoning.to_string());
|
||||
}
|
||||
|
||||
let prefix_chars_in_delta = prefix_len - previous_len;
|
||||
let stripped = &reasoning[prefix_chars_in_delta.min(reasoning.len())..];
|
||||
if stripped.is_empty() {
|
||||
if self.reasoning_text.len() >= prefix_len {
|
||||
self.reasoning_text.clear();
|
||||
self.prefix_stripped = true;
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
self.reasoning_text.clear();
|
||||
self.prefix_stripped = true;
|
||||
return Some(stripped.to_string());
|
||||
}
|
||||
|
||||
if THOUGHT_PREFIX.starts_with(&self.reasoning_text) {
|
||||
return None;
|
||||
}
|
||||
|
||||
self.prefix_stripped = true;
|
||||
Some(std::mem::take(&mut self.reasoning_text))
|
||||
}
|
||||
|
||||
/// Apply Gemma4-specific reasoning post-processing to one parsed delta.
|
||||
fn post_process(&mut self, mut result: ReasoningDelta) -> ReasoningDelta {
|
||||
if let Some(reasoning) = result.reasoning.take() {
|
||||
result.reasoning =
|
||||
self.strip_thought_prefix(&reasoning).filter(|text| !text.is_empty());
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for Gemma4ReasoningParser {
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tokenizer)?))
|
||||
}
|
||||
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids);
|
||||
self.reasoning_text.clear();
|
||||
self.prefix_stripped = false;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> Result<ReasoningDelta> {
|
||||
let result = self.inner.push(delta);
|
||||
Ok(self.post_process(result))
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
let result = self.inner.finish();
|
||||
Ok(self.post_process(result))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use vllm_tokenizer::Tokenizer;
|
||||
|
||||
use super::Gemma4ReasoningParser;
|
||||
use crate::reasoning::ReasoningParser;
|
||||
|
||||
struct FakeTokenizer;
|
||||
|
||||
impl Tokenizer for FakeTokenizer {
|
||||
fn encode(
|
||||
&self,
|
||||
text: &str,
|
||||
_add_special_tokens: bool,
|
||||
) -> vllm_tokenizer::Result<Vec<u32>> {
|
||||
Ok(text.chars().map(u32::from).collect())
|
||||
}
|
||||
|
||||
fn decode(
|
||||
&self,
|
||||
token_ids: &[u32],
|
||||
_skip_special_tokens: bool,
|
||||
) -> vllm_tokenizer::Result<String> {
|
||||
Ok(token_ids
|
||||
.iter()
|
||||
.map(|token_id| char::from_u32(*token_id).unwrap_or('\u{FFFD}'))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn token_to_id(&self, token: &str) -> Option<u32> {
|
||||
match token {
|
||||
"<|channel>" => Some(1000),
|
||||
"<channel|>" => Some(1001),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_streaming(output: &[&str]) -> (Option<String>, Option<String>) {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Gemma4ReasoningParser::new(tokenizer).unwrap();
|
||||
let mut reasoning = String::new();
|
||||
let mut content = String::new();
|
||||
|
||||
for delta in output {
|
||||
let result = parser.push(delta).unwrap();
|
||||
if let Some(next) = result.reasoning {
|
||||
reasoning.push_str(&next);
|
||||
}
|
||||
if let Some(next) = result.content {
|
||||
content.push_str(&next);
|
||||
}
|
||||
}
|
||||
|
||||
let final_delta = parser.finish().unwrap();
|
||||
if let Some(next) = final_delta.reasoning {
|
||||
reasoning.push_str(&next);
|
||||
}
|
||||
if let Some(next) = final_delta.content {
|
||||
content.push_str(&next);
|
||||
}
|
||||
|
||||
(
|
||||
(!reasoning.is_empty()).then_some(reasoning),
|
||||
(!content.is_empty()).then_some(content),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_reasoning_streaming_handles_channel_delimited_outputs() {
|
||||
let cases = [
|
||||
(
|
||||
"no_reasoning",
|
||||
vec!["This is content"],
|
||||
None,
|
||||
Some("This is content"),
|
||||
),
|
||||
(
|
||||
"reasoning_and_content",
|
||||
vec!["<|channel>This is a reasoning section<channel|>This is the rest"],
|
||||
Some("This is a reasoning section"),
|
||||
Some("This is the rest"),
|
||||
),
|
||||
(
|
||||
"complete_reasoning",
|
||||
vec!["<|channel>This is a reasoning section<channel|>"],
|
||||
Some("This is a reasoning section"),
|
||||
None,
|
||||
),
|
||||
(
|
||||
"multiple_lines",
|
||||
vec!["<|channel>This\nThat<channel|>This is the rest\nThat"],
|
||||
Some("This\nThat"),
|
||||
Some("This is the rest\nThat"),
|
||||
),
|
||||
(
|
||||
"no_end",
|
||||
vec!["<|channel>This is a reasoning section"],
|
||||
Some("This is a reasoning section"),
|
||||
None,
|
||||
),
|
||||
("empty", vec![""], None, None),
|
||||
(
|
||||
"newline_around_reasoning",
|
||||
vec!["Before\n<|channel>This is a reasoning section<channel|>\nThis is the rest"],
|
||||
Some("This is a reasoning section"),
|
||||
Some("Before\n\nThis is the rest"),
|
||||
),
|
||||
(
|
||||
"thought_prefix",
|
||||
vec!["<|channel>thought\nActual reasoning here<channel|>Final answer"],
|
||||
Some("Actual reasoning here"),
|
||||
Some("Final answer"),
|
||||
),
|
||||
(
|
||||
"thought_prefix_only",
|
||||
vec!["<|channel>thought\n<channel|>"],
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
"thought_prefix_multiline",
|
||||
vec!["<|channel>thought\nLine1\nLine2<channel|>Answer"],
|
||||
Some("Line1\nLine2"),
|
||||
Some("Answer"),
|
||||
),
|
||||
(
|
||||
"thought_prefix_diverge",
|
||||
vec!["<|channel>thousand reasons<channel|>Done"],
|
||||
Some("thousand reasons"),
|
||||
Some("Done"),
|
||||
),
|
||||
];
|
||||
|
||||
for (name, output, expected_reasoning, expected_content) in cases {
|
||||
let (reasoning, content) = run_streaming(&output);
|
||||
assert_eq!(reasoning.as_deref(), expected_reasoning, "{name}");
|
||||
assert_eq!(content.as_deref(), expected_content, "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_strips_thought_prefix_even_when_split_across_deltas() {
|
||||
let (reasoning, content) =
|
||||
run_streaming(&["<|channel>thou", "ght", "\nabc", "<channel|>done"]);
|
||||
assert_eq!(reasoning.as_deref(), Some("abc"));
|
||||
assert_eq!(content.as_deref(), Some("done"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_preserves_special_tokens() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let parser = Gemma4ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
assert!(parser.preserve_special_tokens());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningParser, Result};
|
||||
|
||||
/// Reasoning parser for legacy Kimi models that use Unicode thinking tags.
|
||||
pub struct KimiReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
}
|
||||
|
||||
impl KimiReasoningParser {
|
||||
/// Create a Kimi parser backed by the shared delimited state machine.
|
||||
pub fn new(tokenizer: DynTokenizer) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(tokenizer, "◁think▷", "◁/think▷", false)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for KimiReasoningParser {
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tokenizer)?))
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.push(delta))
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.finish())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningParser, Result};
|
||||
|
||||
const M3_THINK_START: &str = "<mm:think>";
|
||||
const M3_THINK_END: &str = "</mm:think>";
|
||||
|
||||
/// Reasoning parser for MiniMax M3 style outputs.
|
||||
///
|
||||
/// MiniMax M3 uses `<mm:think>...</mm:think>` delimiters. Its chat template may
|
||||
/// prefill either delimiter depending on the requested thinking mode, so the
|
||||
/// shared delimited parser derives the starting state from the rendered prompt.
|
||||
pub struct MiniMaxM3ReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
/// True until the first response text is classified. Only this position may
|
||||
/// drop a stray `</mm:think>` emitted at the start of a response.
|
||||
at_response_start: bool,
|
||||
/// Holds an initial suffix like `</mm` while it may still complete into the
|
||||
/// leading closer on a later chunk.
|
||||
leading_end_buffer: String,
|
||||
}
|
||||
|
||||
impl MiniMaxM3ReasoningParser {
|
||||
/// Create a MiniMax M3 parser backed by the shared delimited state machine.
|
||||
pub fn new(tokenizer: DynTokenizer) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(tokenizer, M3_THINK_START, M3_THINK_END, false)?,
|
||||
at_response_start: true,
|
||||
leading_end_buffer: String::new(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Drop a response-leading `</mm:think>` while preserving later unmatched
|
||||
/// closers as ordinary content.
|
||||
fn push_inner(&mut self, delta: &str) -> ReasoningDelta {
|
||||
if self.at_response_start && !self.inner.in_reasoning() {
|
||||
self.leading_end_buffer.push_str(delta);
|
||||
let buffered = std::mem::take(&mut self.leading_end_buffer);
|
||||
|
||||
if buffered.is_empty() {
|
||||
return ReasoningDelta::default();
|
||||
}
|
||||
if let Some(rest) = buffered.strip_prefix(M3_THINK_END) {
|
||||
self.at_response_start = false;
|
||||
return self.inner.push(rest);
|
||||
}
|
||||
if M3_THINK_END.starts_with(buffered.as_str()) {
|
||||
self.leading_end_buffer = buffered;
|
||||
return ReasoningDelta::default();
|
||||
}
|
||||
|
||||
self.at_response_start = false;
|
||||
return self.inner.push(&buffered);
|
||||
}
|
||||
|
||||
self.inner.push(delta)
|
||||
}
|
||||
}
|
||||
|
||||
fn append_delta(target: &mut ReasoningDelta, delta: ReasoningDelta) {
|
||||
if let Some(reasoning) = delta.reasoning {
|
||||
target.push_reasoning(&reasoning);
|
||||
}
|
||||
if let Some(content) = delta.content {
|
||||
target.push_content(&content);
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for MiniMaxM3ReasoningParser {
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tokenizer)?))
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids);
|
||||
self.at_response_start = true;
|
||||
self.leading_end_buffer.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> Result<ReasoningDelta> {
|
||||
Ok(self.push_inner(delta))
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
let mut delta = ReasoningDelta::default();
|
||||
if !self.leading_end_buffer.is_empty() {
|
||||
let pending = std::mem::take(&mut self.leading_end_buffer);
|
||||
self.at_response_start = false;
|
||||
append_delta(&mut delta, self.inner.push(&pending));
|
||||
}
|
||||
append_delta(&mut delta, self.inner.finish());
|
||||
Ok(delta)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
//! Streaming reasoning parsers for chat completions.
|
||||
//!
|
||||
//! The key design choice here is that parser initialization prefers the
|
||||
//! *actual rendered prompt state* over model-family conventions. When a stream
|
||||
//! starts, each parser receives the prompt token IDs and inspects the last
|
||||
//! reasoning boundary that is already present in the prompt. In practice this
|
||||
//! is a more faithful signal than hardcoding assumptions such as "this model
|
||||
//! always starts in reasoning" or "this model always emits `<think>` itself".
|
||||
//!
|
||||
//! That prompt-first initialization lets multiple model families share the
|
||||
//! same incremental parser implementation even when older Python parsers split
|
||||
//! them apart. If two families use the same textual delimiters and differ
|
||||
//! mostly in how their chat templates prefill `<think>` / `</think>`, they can
|
||||
//! usually reuse one parser here because the prompt token IDs already tell us
|
||||
//! which state the stream is entering with.
|
||||
|
||||
mod cohere_cmd;
|
||||
mod deepseek_r1;
|
||||
mod delimited;
|
||||
mod gemma4;
|
||||
mod kimi;
|
||||
mod minimax_m3;
|
||||
mod qwen3;
|
||||
mod seed_oss;
|
||||
mod step3p5;
|
||||
|
||||
use thiserror::Error;
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
pub use self::cohere_cmd::CohereCmdReasoningParser;
|
||||
pub use self::deepseek_r1::DeepSeekR1ReasoningParser;
|
||||
pub(crate) use self::delimited::DelimitedReasoningParser;
|
||||
pub use self::gemma4::Gemma4ReasoningParser;
|
||||
pub use self::kimi::KimiReasoningParser;
|
||||
pub use self::minimax_m3::MiniMaxM3ReasoningParser;
|
||||
pub use self::qwen3::Qwen3ReasoningParser;
|
||||
pub use self::seed_oss::SeedOssReasoningParser;
|
||||
pub use self::step3p5::Step3p5ReasoningParser;
|
||||
|
||||
/// DeepSeek V3 currently shares the standard `<think>...</think>` parser.
|
||||
pub type DeepSeekV3ReasoningParser = Qwen3ReasoningParser;
|
||||
/// DeepSeek V4 currently shares the standard `<think>...</think>` parser.
|
||||
pub type DeepSeekV4ReasoningParser = Qwen3ReasoningParser;
|
||||
/// GLM45 currently shares the standard `<think>...</think>` parser.
|
||||
pub type Glm45ReasoningParser = Qwen3ReasoningParser;
|
||||
/// Kimi K2 currently shares the standard `<think>...</think>` parser.
|
||||
// TODO: kimi k2 may implicitly end reasoning by starting a tool call section
|
||||
// using <|tool_calls_section_begin|>, we should support that.
|
||||
pub type KimiK2ReasoningParser = Qwen3ReasoningParser;
|
||||
/// MiniMax M2 currently shares the standard `<think>...</think>` parser.
|
||||
pub type MiniMaxM2ReasoningParser = Qwen3ReasoningParser;
|
||||
/// Nemotron V3 currently shares the standard `<think>...</think>` parser.
|
||||
pub type NemotronV3ReasoningParser = Qwen3ReasoningParser;
|
||||
/// Step3 currently shares the standard `<think>...</think>` parser.
|
||||
pub type Step3ReasoningParser = Qwen3ReasoningParser;
|
||||
|
||||
/// Result alias for reasoning parser operations.
|
||||
pub type Result<T> = std::result::Result<T, ReasoningError>;
|
||||
|
||||
/// One parsed streaming delta split into reasoning and visible content.
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
pub struct ReasoningDelta {
|
||||
pub reasoning: Option<String>,
|
||||
pub content: Option<String>,
|
||||
}
|
||||
|
||||
impl ReasoningDelta {
|
||||
/// Return true when this delta carries neither reasoning nor content text.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.reasoning.is_none() && self.content.is_none()
|
||||
}
|
||||
|
||||
/// Append text to the reasoning portion, creating it on first use.
|
||||
pub(crate) fn push_reasoning(&mut self, text: &str) {
|
||||
if text.is_empty() {
|
||||
return;
|
||||
}
|
||||
match &mut self.reasoning {
|
||||
Some(existing) => existing.push_str(text),
|
||||
None => self.reasoning = Some(text.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Append text to the visible content portion, creating it on first use.
|
||||
pub(crate) fn push_content(&mut self, text: &str) {
|
||||
if text.is_empty() {
|
||||
return;
|
||||
}
|
||||
match &mut self.content {
|
||||
Some(existing) => existing.push_str(text),
|
||||
None => self.content = Some(text.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Incremental parser that splits decoded text deltas into reasoning and
|
||||
/// content.
|
||||
pub trait ReasoningParser: Send {
|
||||
/// Construct a boxed parser instance for one request stream.
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static;
|
||||
|
||||
/// Initialize parser state from prompt token IDs before output deltas
|
||||
/// arrive.
|
||||
fn initialize(&mut self, _prompt_token_ids: &[u32]) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Return whether decoded output must preserve tokenizer special tokens.
|
||||
///
|
||||
/// Some model families emit reasoning 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, delta: &str) -> Result<ReasoningDelta>;
|
||||
|
||||
/// Flush any buffered partial delimiter state at end of stream.
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
Ok(ReasoningDelta::default())
|
||||
}
|
||||
}
|
||||
|
||||
/// Errors produced while creating or running reasoning parsers.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ReasoningError {
|
||||
#[error("tokenizer is missing reasoning delimiter token `{token}`")]
|
||||
MissingToken { token: String },
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,43 @@
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningParser, Result};
|
||||
|
||||
/// Reasoning parser for the Qwen3/Qwen3.5 family.
|
||||
///
|
||||
/// This parser uses standard `<think>...</think>` delimiters and defaults to
|
||||
/// waiting for an explicit start token when prompt initialization finds no
|
||||
/// reasoning boundary.
|
||||
pub struct Qwen3ReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
}
|
||||
|
||||
impl Qwen3ReasoningParser {
|
||||
/// Create a Qwen3 parser backed by the shared delimited state machine.
|
||||
pub fn new(tokenizer: DynTokenizer) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(tokenizer, "<think>", "</think>", false)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for Qwen3ReasoningParser {
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tokenizer)?))
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.push(delta))
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.finish())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningParser, Result};
|
||||
|
||||
/// Reasoning parser for SeedOSS models using `<seed:think>`/`</seed:think>`
|
||||
/// delimiters.
|
||||
pub struct SeedOssReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
}
|
||||
|
||||
impl SeedOssReasoningParser {
|
||||
/// Create a SeedOSS parser backed by the shared delimited state machine.
|
||||
pub fn new(tokenizer: DynTokenizer) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(
|
||||
tokenizer,
|
||||
"<seed:think>",
|
||||
"</seed:think>",
|
||||
false,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for SeedOssReasoningParser {
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tokenizer)?))
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.push(delta))
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
Ok(self.inner.finish())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::SeedOssReasoningParser;
|
||||
use crate::reasoning::{ReasoningParser, tests::FakeTokenizer};
|
||||
|
||||
#[test]
|
||||
fn without_prompt_markers_expects_start_token() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = SeedOssReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("implicit reasoning</seed:think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(
|
||||
delta.content.as_deref(),
|
||||
Some("implicit reasoning</seed:think>answer")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn picks_up_prompt_start_boundary() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = SeedOssReasoningParser::new(tokenizer).unwrap();
|
||||
// Prompt prefills `<seed:think>` (id 10), opening reasoning before the stream.
|
||||
parser.initialize(&[10]).unwrap();
|
||||
|
||||
let delta = parser.push("reason</seed:think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn respects_prompt_end_boundary() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = SeedOssReasoningParser::new(tokenizer).unwrap();
|
||||
// Prompt already closed reasoning with `</seed:think>` (id 11).
|
||||
parser.initialize(&[11]).unwrap();
|
||||
|
||||
let delta = parser.push("answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_explicit_start_token() {
|
||||
// An explicit start delimiter must not leak into reasoning text.
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = SeedOssReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("<seed:think>reason</seed:think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streams_explicit_start_token_across_pushes() {
|
||||
// Start token, reasoning body, end token, and content arrive in separate
|
||||
// streaming deltas.
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = SeedOssReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let mut reasoning = String::new();
|
||||
let mut content = String::new();
|
||||
for delta_str in [
|
||||
"<seed:think>",
|
||||
"Some ",
|
||||
"reasoning ",
|
||||
"content",
|
||||
"</seed:think>",
|
||||
"Final ",
|
||||
"answer",
|
||||
] {
|
||||
let delta = parser.push(delta_str).unwrap();
|
||||
if let Some(r) = delta.reasoning {
|
||||
reasoning.push_str(&r);
|
||||
}
|
||||
if let Some(c) = delta.content {
|
||||
content.push_str(&c);
|
||||
}
|
||||
}
|
||||
assert_eq!(reasoning, "Some reasoning content");
|
||||
assert_eq!(content, "Final answer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_partial_delimiters_across_pushes() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = SeedOssReasoningParser::new(tokenizer).unwrap();
|
||||
parser.initialize(&[10]).unwrap();
|
||||
|
||||
// Closing delimiter `</seed:think>` arrives in two halves.
|
||||
let first = parser.push("reason</seed:").unwrap();
|
||||
assert_eq!(first.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(first.content, None);
|
||||
|
||||
let second = parser.push("think>answer").unwrap();
|
||||
assert_eq!(second.reasoning, None);
|
||||
assert_eq!(second.content.as_deref(), Some("answer"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningParser, Result};
|
||||
|
||||
/// Reasoning parser for Step3p5 outputs.
|
||||
///
|
||||
/// Step3p5 uses standard `<think>`/`</think>` delimiters but emits a `\n`
|
||||
/// immediately before and/or after `</think>`. The parser drops these framing
|
||||
/// newlines on both sides of the boundary, holding a trailing `\n` from
|
||||
/// reasoning across pushes until either more reasoning text or `</think>`
|
||||
/// arrives, and dropping a leading `\n` from the first content delta after
|
||||
/// the boundary.
|
||||
pub struct Step3p5ReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
/// `\n` at end of last reasoning delta, held in case `</think>` follows.
|
||||
pending_reasoning_newline: bool,
|
||||
/// Last push ended on `</think>` without emitting content; the next
|
||||
/// content delta's leading `\n` should be dropped.
|
||||
just_ended_reasoning: bool,
|
||||
}
|
||||
|
||||
impl Step3p5ReasoningParser {
|
||||
/// Create a Step3p5 parser backed by the shared delimited state machine.
|
||||
pub fn new(tokenizer: DynTokenizer) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(tokenizer, "<think>", "</think>", false)?,
|
||||
pending_reasoning_newline: false,
|
||||
just_ended_reasoning: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Drop framing newlines around `</think>` and track held-newline state.
|
||||
fn process(
|
||||
&mut self,
|
||||
mut inner_delta: ReasoningDelta,
|
||||
was_in_reasoning: bool,
|
||||
now_in_reasoning: bool,
|
||||
) -> ReasoningDelta {
|
||||
// A `<think>...</think>` round-trip in one push still counts as a
|
||||
// transition: the inner emits reasoning while ending in content mode.
|
||||
let transitioned =
|
||||
!now_in_reasoning && (was_in_reasoning || inner_delta.reasoning.is_some());
|
||||
|
||||
// Replay or drop a previously-held trailing reasoning newline.
|
||||
if self.pending_reasoning_newline {
|
||||
if let Some(reasoning) = inner_delta.reasoning.as_mut() {
|
||||
reasoning.insert(0, '\n');
|
||||
self.pending_reasoning_newline = false;
|
||||
} else if transitioned {
|
||||
// The held `\n` was the one right before `</think>`: drop it.
|
||||
self.pending_reasoning_newline = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Hold back a trailing reasoning `\n` until we know if `</think>` follows.
|
||||
if let Some(reasoning) = inner_delta.reasoning.as_mut()
|
||||
&& reasoning.ends_with('\n')
|
||||
{
|
||||
reasoning.pop();
|
||||
if !transitioned {
|
||||
self.pending_reasoning_newline = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Drop a leading `\n` of content emitted right after `</think>`.
|
||||
if let Some(content) = inner_delta.content.as_mut()
|
||||
&& (transitioned || self.just_ended_reasoning)
|
||||
&& content.starts_with('\n')
|
||||
{
|
||||
content.remove(0);
|
||||
}
|
||||
|
||||
self.just_ended_reasoning = transitioned && inner_delta.content.is_none();
|
||||
|
||||
if inner_delta.reasoning.as_deref() == Some("") {
|
||||
inner_delta.reasoning = None;
|
||||
}
|
||||
if inner_delta.content.as_deref() == Some("") {
|
||||
inner_delta.content = None;
|
||||
}
|
||||
|
||||
inner_delta
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for Step3p5ReasoningParser {
|
||||
fn create(tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tokenizer)?))
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> Result<ReasoningDelta> {
|
||||
let was = self.inner.in_reasoning();
|
||||
let inner_delta = self.inner.push(delta);
|
||||
let now = self.inner.in_reasoning();
|
||||
Ok(self.process(inner_delta, was, now))
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ReasoningDelta> {
|
||||
let was = self.inner.in_reasoning();
|
||||
let inner_delta = self.inner.finish();
|
||||
let now = self.inner.in_reasoning();
|
||||
let mut delta = self.process(inner_delta, was, now);
|
||||
|
||||
// Emit a still-held newline rather than silently dropping it.
|
||||
if self.pending_reasoning_newline {
|
||||
match delta.reasoning.as_mut() {
|
||||
Some(existing) => existing.push('\n'),
|
||||
None => delta.reasoning = Some("\n".to_string()),
|
||||
}
|
||||
self.pending_reasoning_newline = false;
|
||||
}
|
||||
|
||||
Ok(delta)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::Step3p5ReasoningParser;
|
||||
use crate::reasoning::{ReasoningParser, tests::FakeTokenizer};
|
||||
|
||||
#[test]
|
||||
fn picks_up_prompt_start_boundary() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
// Prompt prefills `<think>` (id 1), opening reasoning before the stream.
|
||||
parser.initialize(&[1]).unwrap();
|
||||
|
||||
let delta = parser.push("This is a reasoning section</think>This is the rest").unwrap();
|
||||
assert_eq!(
|
||||
delta.reasoning.as_deref(),
|
||||
Some("This is a reasoning section")
|
||||
);
|
||||
assert_eq!(delta.content.as_deref(), Some("This is the rest"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_unterminated_reasoning() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let pushed = parser.push("<think>reason without end").unwrap();
|
||||
assert_eq!(pushed.reasoning.as_deref(), Some("reason without end"));
|
||||
assert_eq!(pushed.content, None);
|
||||
|
||||
let flushed = parser.finish().unwrap();
|
||||
assert!(flushed.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_empty_input() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let pushed = parser.push("").unwrap();
|
||||
assert!(pushed.is_empty());
|
||||
let flushed = parser.finish().unwrap();
|
||||
assert!(flushed.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complex_newline_pattern_trims_only_single_framing_newline_each_side() {
|
||||
// Only the immediately-adjacent framing `\n` is dropped on each side of
|
||||
// `</think>`; surrounding newlines remain part of reasoning/content.
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
parser.initialize(&[1]).unwrap();
|
||||
|
||||
let delta = parser
|
||||
.push("\n This is a \n reasoning section\n\n\n</think>\n\nThis is the rest")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
delta.reasoning.as_deref(),
|
||||
Some("\n This is a \n reasoning section\n\n")
|
||||
);
|
||||
assert_eq!(delta.content.as_deref(), Some("\nThis is the rest"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drops_framing_newlines_in_single_push() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("<think>reason\n</think>\nanswer").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drops_framing_newlines_across_pushes() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
// The trailing `\n` from the first push is held until we know whether
|
||||
// `</think>` follows.
|
||||
let first = parser.push("<think>reason\n").unwrap();
|
||||
assert_eq!(first.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(first.content, None);
|
||||
|
||||
// `</think>` arrives standalone; the held newline should be dropped.
|
||||
let second = parser.push("</think>").unwrap();
|
||||
assert!(second.is_empty());
|
||||
|
||||
// The leading newline of the first content delta is dropped.
|
||||
let third = parser.push("\nanswer").unwrap();
|
||||
assert_eq!(third.reasoning, None);
|
||||
assert_eq!(third.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replays_held_newline_when_more_reasoning_follows() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let first = parser.push("<think>reason\n").unwrap();
|
||||
assert_eq!(first.reasoning.as_deref(), Some("reason"));
|
||||
|
||||
let second = parser.push("more reason").unwrap();
|
||||
assert_eq!(second.reasoning.as_deref(), Some("\nmore reason"));
|
||||
assert_eq!(second.content, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finish_flushes_held_newline_in_unterminated_stream() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let first = parser.push("<think>reason\n").unwrap();
|
||||
assert_eq!(first.reasoning.as_deref(), Some("reason"));
|
||||
|
||||
let flushed = parser.finish().unwrap();
|
||||
assert_eq!(flushed.reasoning.as_deref(), Some("\n"));
|
||||
assert_eq!(flushed.content, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_inner_newlines_in_reasoning() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("<think>line1\nline2</think>tail").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("line1\nline2"));
|
||||
assert_eq!(delta.content.as_deref(), Some("tail"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trims_only_one_trailing_reasoning_newline() {
|
||||
// Only the single framing newline immediately before `</think>` is
|
||||
// dropped; earlier newlines in the reasoning body are preserved.
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("<think>reason\n\n</think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason\n"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drops_only_first_content_newline_after_transition() {
|
||||
// The leading-`\n` drop applies only to the first content delta after
|
||||
// `</think>`; later deltas pass through untouched.
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let first = parser.push("<think>reason</think>").unwrap();
|
||||
assert_eq!(first.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(first.content, None);
|
||||
|
||||
let second = parser.push("\nfirst").unwrap();
|
||||
assert_eq!(second.reasoning, None);
|
||||
assert_eq!(second.content.as_deref(), Some("first"));
|
||||
|
||||
// A `\n` arriving in a later content delta must NOT be dropped.
|
||||
let third = parser.push("\nsecond").unwrap();
|
||||
assert_eq!(third.reasoning, None);
|
||||
assert_eq!(third.content.as_deref(), Some("\nsecond"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_through_clean_boundary_without_framing_newlines() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("<think>reason</think>tail").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("tail"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_empty_reasoning_section() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Step3p5ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("<think></think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use vllm_tokenizer::Tokenizer;
|
||||
|
||||
use super::{
|
||||
DeepSeekR1ReasoningParser, DelimitedReasoningParser, MiniMaxM3ReasoningParser,
|
||||
Qwen3ReasoningParser, ReasoningParser,
|
||||
};
|
||||
|
||||
pub(crate) struct FakeTokenizer;
|
||||
|
||||
impl Tokenizer for FakeTokenizer {
|
||||
fn encode(&self, text: &str, _add_special_tokens: bool) -> vllm_tokenizer::Result<Vec<u32>> {
|
||||
Ok(text.chars().map(u32::from).collect())
|
||||
}
|
||||
|
||||
fn decode(
|
||||
&self,
|
||||
token_ids: &[u32],
|
||||
_skip_special_tokens: bool,
|
||||
) -> vllm_tokenizer::Result<String> {
|
||||
Ok(token_ids
|
||||
.iter()
|
||||
.map(|token_id| char::from_u32(*token_id).unwrap_or('\u{FFFD}'))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn token_to_id(&self, token: &str) -> Option<u32> {
|
||||
match token {
|
||||
"<think>" => Some(1),
|
||||
"</think>" => Some(2),
|
||||
"<|START_THINKING|>" => Some(3),
|
||||
"<|END_THINKING|>" => Some(4),
|
||||
"◁think▷" => Some(5),
|
||||
"◁/think▷" => Some(6),
|
||||
"<mm:think>" => Some(8),
|
||||
"</mm:think>" => Some(9),
|
||||
"<seed:think>" => Some(10),
|
||||
"</seed:think>" => Some(11),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_special_id(&self, token_id: u32) -> bool {
|
||||
token_id == 7
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delimited_content_only_stream() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser =
|
||||
DelimitedReasoningParser::new(tokenizer, "<think>", "</think>", false).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
parser.push("plain content").content.as_deref(),
|
||||
Some("plain content")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delimited_single_chunk_with_reasoning_and_content() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser =
|
||||
DelimitedReasoningParser::new(tokenizer, "<think>", "</think>", false).unwrap();
|
||||
|
||||
let delta = parser.push("<think>reason</think>answer");
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delimited_partial_tokens_across_chunks() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser =
|
||||
DelimitedReasoningParser::new(tokenizer, "<think>", "</think>", false).unwrap();
|
||||
|
||||
assert!(parser.push("<thi").is_empty());
|
||||
let delta = parser.push("nk>reason</think>answer");
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delimited_finish_flushes_buffer() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser =
|
||||
DelimitedReasoningParser::new(tokenizer, "<think>", "</think>", false).unwrap();
|
||||
parser.initialize(&[1]);
|
||||
|
||||
let delta = parser.push("unfinished</thi");
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("unfinished"));
|
||||
let final_delta = parser.finish();
|
||||
assert_eq!(final_delta.reasoning.as_deref(), Some("</thi"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen3_without_prompt_markers_expects_start_token() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Qwen3ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("reason</think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("reason</think>answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen3_prompt_end_marker_starts_in_content() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Qwen3ReasoningParser::new(tokenizer).unwrap();
|
||||
parser.initialize(&[2]).unwrap();
|
||||
|
||||
let delta = parser.push("answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen3_tolerates_old_and_new_formats() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
|
||||
let mut old_parser = Qwen3ReasoningParser::new(tokenizer.clone()).unwrap();
|
||||
let old = old_parser.push("<think>reason</think>answer").unwrap();
|
||||
assert_eq!(old.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(old.content.as_deref(), Some("answer"));
|
||||
|
||||
let mut new_parser = Qwen3ReasoningParser::new(tokenizer).unwrap();
|
||||
new_parser.initialize(&[1]).unwrap();
|
||||
let new = new_parser.push("reason</think>answer").unwrap();
|
||||
assert_eq!(new.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(new.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen3_stops_scanning_at_last_special_token() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = Qwen3ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
parser.initialize(&[1, 7]).unwrap();
|
||||
|
||||
let delta = parser.push("answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_r1_defaults_to_reasoning_without_prompt_boundary() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = DeepSeekR1ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("reason</think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_r1_stops_scanning_at_last_special_token() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = DeepSeekR1ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
parser.initialize(&[2, 7]).unwrap();
|
||||
|
||||
let delta = parser.push("reason</think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_handles_explicit_think_delimiters() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = MiniMaxM3ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("<mm:think>reason</mm:think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_drops_leading_end_marker() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = MiniMaxM3ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("</mm:think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_preserves_non_leading_end_marker() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = MiniMaxM3ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
let delta = parser.push("XXX</mm:think>YYY").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("XXX</mm:think>YYY"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_drops_split_leading_end_marker() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = MiniMaxM3ReasoningParser::new(tokenizer).unwrap();
|
||||
|
||||
assert!(parser.push("</mm").unwrap().is_empty());
|
||||
let delta = parser.push(":think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_uses_prompt_prefilled_start_marker() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = MiniMaxM3ReasoningParser::new(tokenizer).unwrap();
|
||||
parser.initialize(&[8]).unwrap();
|
||||
|
||||
let delta = parser.push("reason</mm:think>answer").unwrap();
|
||||
assert_eq!(delta.reasoning.as_deref(), Some("reason"));
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_uses_prompt_prefilled_end_marker() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let mut parser = MiniMaxM3ReasoningParser::new(tokenizer).unwrap();
|
||||
parser.initialize(&[9]).unwrap();
|
||||
|
||||
let delta = parser.push("answer").unwrap();
|
||||
assert_eq!(delta.reasoning, None);
|
||||
assert_eq!(delta.content.as_deref(), Some("answer"));
|
||||
}
|
||||
@@ -0,0 +1,445 @@
|
||||
use super::{DeepSeekDsmlToolParser, DsmlTokens};
|
||||
use crate::tool::{Result, StructuralTagModel, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
/// 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 {
|
||||
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 structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::DeepSeekV32)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.0.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.0.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.0.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::DeepSeekV32ToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserTestExt as _};
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"get_weather",
|
||||
&[("location", "SF"), ("date", "2024-01-16")],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"location": "SF",
|
||||
"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 output = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Thinking... ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_converts_schema_types() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = 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!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = 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!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": "5.0",
|
||||
"flag": "true",
|
||||
"payload": "{\"nested\":true}",
|
||||
"items": "[1,2]",
|
||||
"empty": "null",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_parse_complete_preserves_raw_closing_tag_text_in_parameter_value() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = 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>(&output.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 output = 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!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_preserves_prefix_text() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = 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!(output.normal_text, "Thinking... ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_without_tool_call_emits_text_incrementally() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = collect_stream(&mut parser, &["Hello, ", "world!"]);
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_extracts_multiple_tool_calls_in_order() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = 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!(output.calls.len(), 2);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[1].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[1].tool_index, 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = 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!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "Beijing" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_truncated_parameter_does_not_leak_eos() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
parser.parse_chunk("<|DSML|function_calls>\n").unwrap();
|
||||
parser.parse_chunk("<|DSML|invoke name=\"get_weather\">\n").unwrap();
|
||||
parser
|
||||
.parse_chunk("<|DSML|parameter name=\"location\" string=\"true\">Tokyo")
|
||||
.unwrap();
|
||||
parser.parse_chunk("<|end▁of▁sentence|>").unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
assert!(error.to_report_string().contains("incomplete DeepSeek DSML tool call"));
|
||||
}
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_drops_eos_after_complete_tool_calls() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = 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!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_ignores_text_after_complete_tool_calls() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
let output = 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!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v32_streaming_does_not_emit_incomplete_invoke() {
|
||||
let mut parser = DeepSeekV32ToolParser::new(&test_tools());
|
||||
parser.parse_chunk("<|DSML|function_calls>\n").unwrap();
|
||||
parser.parse_chunk("<|DSML|invoke name=\"get_weather\">\n").unwrap();
|
||||
parser
|
||||
.parse_chunk(
|
||||
"<|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,148 @@
|
||||
use super::{DeepSeekDsmlToolParser, DsmlTokens};
|
||||
use crate::tool::{Result, StructuralTagModel, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
/// 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 {
|
||||
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 structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::DeepSeekV4)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.0.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.0.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.0.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::DeepSeekV4ToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, test_tools};
|
||||
use crate::tool::{StructuralTagModel, ToolParser, ToolParserTestExt as _};
|
||||
|
||||
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_exposes_structural_tag_model() {
|
||||
let parser = DeepSeekV4ToolParser::new(&test_tools());
|
||||
|
||||
assert_eq!(
|
||||
parser.structural_tag_model(),
|
||||
Some(StructuralTagModel::DeepSeekV4)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v4_parse_complete_reuses_dsml_parser_with_tool_calls_token() {
|
||||
let mut parser = DeepSeekV4ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"get_weather",
|
||||
&[("location", "SF"), ("date", "2024-01-16")],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"location": "SF",
|
||||
"date": "2024-01-16"
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v4_streaming_handles_tool_calls_token_split_across_chunks() {
|
||||
let mut parser = DeepSeekV4ToolParser::new(&test_tools());
|
||||
let output = 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!(output.normal_text, "Thinking... ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "Beijing" })
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
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::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParserOutput};
|
||||
use crate::tool::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, PartialEq, Eq)]
|
||||
enum DsmlMode {
|
||||
Text,
|
||||
ToolBlock { invoke_end_scan: MarkerScanState },
|
||||
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, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
DsmlEvent::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
DsmlEvent::ToolCallsStart => {
|
||||
self.mode = DsmlMode::ToolBlock {
|
||||
invoke_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
DsmlEvent::Invoke { name, raw_params } => {
|
||||
let mut arguments = serde_json::Map::with_capacity(raw_params.len());
|
||||
for param in raw_params {
|
||||
let value = if param.is_string {
|
||||
serde_json::Value::String(param.value)
|
||||
} else {
|
||||
self.tool_parameters.convert_param_with_schema(
|
||||
&name,
|
||||
¶m.name,
|
||||
param.value,
|
||||
)
|
||||
};
|
||||
arguments.insert(param.name, value);
|
||||
}
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
output.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(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = DsmlMode::Text;
|
||||
self.emitted_invoke_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
// Extract tool calls from streaming model output.
|
||||
//
|
||||
// Uses a buffer-until-complete-invoke strategy: text is buffered until
|
||||
// a complete invoke block is available, then parsed and emitted in one
|
||||
// shot.
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_dsml_event(input, &mut self.mode, self.tokens)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match self.mode {
|
||||
DsmlMode::Text => output.normal_text.push_str(&self.buffer),
|
||||
DsmlMode::Done => {}
|
||||
DsmlMode::ToolBlock { .. } => {
|
||||
return Err(parsing_failed!("incomplete DeepSeek DSML tool call"));
|
||||
}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a DSML event for the current parser mode.
|
||||
fn parse_next_dsml_event(
|
||||
input: &mut DsmlInput<'_>,
|
||||
mode: &mut DsmlMode,
|
||||
tokens: DsmlTokens,
|
||||
) -> ModalResult<DsmlEvent> {
|
||||
match mode {
|
||||
DsmlMode::Text => parse_text_event(input, tokens),
|
||||
DsmlMode::ToolBlock { invoke_end_scan } => {
|
||||
parse_tool_block_event(input, tokens, invoke_end_scan)
|
||||
}
|
||||
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,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<DsmlEvent> {
|
||||
ws0.void().parse_next(input)?;
|
||||
alt((
|
||||
|input: &mut DsmlInput<'_>| invoke_event(input, invoke_end_scan),
|
||||
|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<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<DsmlEvent> {
|
||||
let (name, body) = seq!(
|
||||
_: literal(INVOKE_START),
|
||||
_: ws1,
|
||||
dsml_name_attr,
|
||||
_: ws0,
|
||||
_: ">",
|
||||
take_until_marker(INVOKE_END, invoke_end_scan),
|
||||
_: 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(str::to_string),
|
||||
_: 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,240 @@
|
||||
use super::{DeepSeekJsonFormat, DeepSeekJsonToolParser};
|
||||
use crate::tool::{Result, StructuralTagModel, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
/// 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 {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::DeepSeekR1)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.0.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.0.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.0.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::DeepSeekV3ToolParser;
|
||||
use crate::tool::deepseek_json::{
|
||||
TOOL_CALL_SEPARATOR, TOOL_CALL_START, TOOL_CALLS_END, TOOL_CALLS_START, V3_ARGUMENT_END,
|
||||
V3_JSON_START,
|
||||
};
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_parse_complete_extracts_raw_json_arguments() {
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
let arguments = r#"{ "location": "Tokyo", "days": "3" }"#;
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"Let me check.\n{} trailing text",
|
||||
tool_section(&[v3_tool_call("get_weather", arguments)])
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check.\n");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo",}"#;
|
||||
let output = parser
|
||||
.parse_complete(&tool_section(&[v3_tool_call("get_weather", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.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 output = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
output.append(next);
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(
|
||||
output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "hello ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.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 output = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v3_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = DeepSeekV3ToolParser::new(&test_tools());
|
||||
parser
|
||||
.parse_chunk(&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.parse_chunk(&input).unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
use super::{DeepSeekJsonFormat, DeepSeekJsonToolParser};
|
||||
use crate::tool::{Result, StructuralTagModel, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
/// 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 {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::DeepSeekV31)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.0.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.0.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.0.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::DeepSeekV31ToolParser;
|
||||
use crate::tool::deepseek_json::{
|
||||
TOOL_CALL_END, TOOL_CALL_SEPARATOR, TOOL_CALL_START, TOOL_CALLS_END, TOOL_CALLS_START,
|
||||
};
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
fn v31_tool_call(function_name: &str, arguments: &str) -> String {
|
||||
format!("{TOOL_CALL_START}{function_name}{TOOL_CALL_SEPARATOR}{arguments}{TOOL_CALL_END}")
|
||||
}
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_parse_complete_extracts_raw_json_arguments() {
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
let arguments = r#"{ "location": "Tokyo", "days": "3" }"#;
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"Let me check.{} trailing text",
|
||||
tool_section(&[v31_tool_call("get_weather", arguments)])
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check.");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo",}"#;
|
||||
let output = parser
|
||||
.parse_complete(&tool_section(&[v31_tool_call("get_weather", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.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 output = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
output.append(next);
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(
|
||||
output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "hello ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.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 output = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[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 output = collect_stream(&mut parser, &[&input]);
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_v31_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = DeepSeekV31ToolParser::new(&test_tools());
|
||||
parser
|
||||
.parse_chunk(&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.parse_chunk(&input).unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
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, ToolParserOutput};
|
||||
|
||||
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,
|
||||
output: &mut ToolParserOutput,
|
||||
) -> Result<()> {
|
||||
match event {
|
||||
DeepSeekJsonEvent::Text { len: consumed_len } => {
|
||||
output.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(),
|
||||
};
|
||||
output.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()
|
||||
));
|
||||
};
|
||||
output.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(())
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
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, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match &self.mode {
|
||||
DeepSeekJsonMode::Text => output.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()
|
||||
));
|
||||
}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = DeepSeekJsonMode::Text;
|
||||
self.active_tool_index = None;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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::tool::error"))]
|
||||
pub enum ToolParserError {
|
||||
#[error("tool parser parsing failed: {message}")]
|
||||
ParsingFailed { message: String },
|
||||
}
|
||||
@@ -0,0 +1,826 @@
|
||||
use serde_json::{Map, Number, Value};
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::{alt, delimited, eof, opt, separated, seq, terminated};
|
||||
use winnow::error::{ContextError, ErrMode, ModalResult};
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::{Partial, Stream};
|
||||
use winnow::token::{literal, take_till, take_until};
|
||||
|
||||
use super::utils::{incomplete, parse_buffered_event, partial_prefix_len, safe_text_len};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::tool::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 },
|
||||
ToolCallStart,
|
||||
ToolCallHeader { name: String },
|
||||
ToolCall { args: Map<String, Value> },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
struct Gemma4ArgsScanState {
|
||||
scanned_len: usize,
|
||||
in_string: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
enum Gemma4Mode {
|
||||
#[default]
|
||||
Text,
|
||||
Header,
|
||||
ToolCall {
|
||||
name: String,
|
||||
args_scan: Gemma4ArgsScanState,
|
||||
},
|
||||
}
|
||||
|
||||
/// 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,
|
||||
mode: Gemma4Mode,
|
||||
emitted_tool_count: usize,
|
||||
}
|
||||
|
||||
impl Gemma4ToolParser {
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: Gemma4Mode::default(),
|
||||
emitted_tool_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_event(&mut self, event: Gemma4Event, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
Gemma4Event::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
Gemma4Event::ToolCallStart => self.mode = Gemma4Mode::Header,
|
||||
Gemma4Event::ToolCallHeader { name } => {
|
||||
self.mode = Gemma4Mode::ToolCall {
|
||||
name,
|
||||
args_scan: Gemma4ArgsScanState::default(),
|
||||
};
|
||||
}
|
||||
Gemma4Event::ToolCall { args } => {
|
||||
let mode = std::mem::replace(&mut self.mode, Gemma4Mode::Text);
|
||||
let Gemma4Mode::ToolCall { name, .. } = mode else {
|
||||
return Err(parsing_failed!(
|
||||
"Gemma4 arguments without an active tool call"
|
||||
));
|
||||
};
|
||||
let arguments = serde_json::to_string(&args)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
output.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
let raw = match std::mem::replace(&mut self.mode, Gemma4Mode::Text) {
|
||||
Gemma4Mode::Text => std::mem::take(&mut self.buffer),
|
||||
Gemma4Mode::Header => {
|
||||
format!("{}{}", TOOL_CALL_START, std::mem::take(&mut self.buffer))
|
||||
}
|
||||
Gemma4Mode::ToolCall { name, .. } => {
|
||||
format!(
|
||||
"{}{}{}{{{}",
|
||||
TOOL_CALL_START,
|
||||
CALL_PREFIX,
|
||||
name,
|
||||
std::mem::take(&mut self.buffer)
|
||||
)
|
||||
}
|
||||
};
|
||||
self.mode = Gemma4Mode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
raw
|
||||
}
|
||||
}
|
||||
|
||||
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 parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = {
|
||||
parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_gemma4_event(input, &mut self.mode)
|
||||
})?
|
||||
} {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
|
||||
match &self.mode {
|
||||
Gemma4Mode::Text => output.normal_text.push_str(&self.buffer),
|
||||
Gemma4Mode::Header | Gemma4Mode::ToolCall { .. } => {
|
||||
return Err(parsing_failed!("incomplete Gemma4 tool call"));
|
||||
}
|
||||
}
|
||||
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
Gemma4ToolParser::reset(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse one Gemma4 event from buffered streaming input.
|
||||
fn parse_next_gemma4_event(
|
||||
input: &mut Gemma4Input<'_>,
|
||||
mode: &mut Gemma4Mode,
|
||||
) -> ModalResult<Gemma4Event> {
|
||||
match mode {
|
||||
Gemma4Mode::Text => parse_text_event(input),
|
||||
Gemma4Mode::Header => tool_call_header_event(input),
|
||||
Gemma4Mode::ToolCall { args_scan, .. } => tool_call_args_event(input, args_scan),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 text-mode event.
|
||||
fn parse_text_event(input: &mut Gemma4Input<'_>) -> ModalResult<Gemma4Event> {
|
||||
alt((tool_call_start_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 tool-call start marker.
|
||||
fn tool_call_start_event(input: &mut Gemma4Input<'_>) -> ModalResult<Gemma4Event> {
|
||||
literal(TOOL_CALL_START).value(Gemma4Event::ToolCallStart).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 tool-call header.
|
||||
fn tool_call_header_event(input: &mut Gemma4Input<'_>) -> ModalResult<Gemma4Event> {
|
||||
let (name,) = seq!(
|
||||
_: literal(CALL_PREFIX),
|
||||
gemma4_tool_name,
|
||||
_: literal("{"),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
Ok(Gemma4Event::ToolCallHeader { name })
|
||||
}
|
||||
|
||||
/// Parse complete Gemma4 tool-call arguments.
|
||||
fn tool_call_args_event(
|
||||
input: &mut Gemma4Input<'_>,
|
||||
args_scan: &mut Gemma4ArgsScanState,
|
||||
) -> ModalResult<Gemma4Event> {
|
||||
let raw_args = gemma4_raw_args_until_tool_call_end(input, args_scan)?;
|
||||
let Some(args_input) = raw_args.strip_suffix('}') else {
|
||||
return Err(ErrMode::Cut(ContextError::new()));
|
||||
};
|
||||
let args = parse_gemma4_args(args_input)?;
|
||||
|
||||
Ok(Gemma4Event::ToolCall { 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 raw Gemma4 arguments through the first end marker outside a Gemma string.
|
||||
fn gemma4_raw_args_until_tool_call_end<'i>(
|
||||
input: &mut Gemma4Input<'i>,
|
||||
state: &mut Gemma4ArgsScanState,
|
||||
) -> ModalResult<&'i str> {
|
||||
let text = **input;
|
||||
if state.scanned_len > text.len() {
|
||||
return incomplete();
|
||||
}
|
||||
|
||||
loop {
|
||||
let rest = &text[state.scanned_len..];
|
||||
if state.in_string {
|
||||
let Some(string_delim) = rest.find(STRING_DELIM) else {
|
||||
state.scanned_len = safe_scan_len(text, state.scanned_len, &[STRING_DELIM]);
|
||||
return incomplete();
|
||||
};
|
||||
|
||||
state.scanned_len += string_delim + STRING_DELIM.len();
|
||||
state.in_string = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
let next_string_delim = rest.find(STRING_DELIM);
|
||||
let next_tool_call_end = rest.find(TOOL_CALL_END);
|
||||
match (next_string_delim, next_tool_call_end) {
|
||||
(Some(string_delim), Some(tool_call_end)) if tool_call_end < string_delim => {
|
||||
let end = state.scanned_len + tool_call_end;
|
||||
state.scanned_len = end + TOOL_CALL_END.len();
|
||||
input.next_slice(state.scanned_len);
|
||||
return Ok(&text[..end]);
|
||||
}
|
||||
(Some(string_delim), _) => {
|
||||
state.scanned_len += string_delim + STRING_DELIM.len();
|
||||
state.in_string = true;
|
||||
}
|
||||
(None, Some(tool_call_end)) => {
|
||||
let end = state.scanned_len + tool_call_end;
|
||||
state.scanned_len = end + TOOL_CALL_END.len();
|
||||
input.next_slice(state.scanned_len);
|
||||
return Ok(&text[..end]);
|
||||
}
|
||||
(None, None) => {
|
||||
state.scanned_len =
|
||||
safe_scan_len(text, state.scanned_len, &[STRING_DELIM, TOOL_CALL_END]);
|
||||
return incomplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the scan length while holding back a split marker prefix.
|
||||
fn safe_scan_len(text: &str, start: usize, markers: &[&str]) -> usize {
|
||||
let max_partial = markers
|
||||
.iter()
|
||||
.map(|marker| partial_prefix_len(&text[start..], marker))
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
text.len() - max_partial
|
||||
}
|
||||
|
||||
/// Parse complete Gemma4 custom key-value arguments.
|
||||
fn parse_gemma4_args(args: &str) -> ModalResult<Map<String, Value>> {
|
||||
let mut input = args;
|
||||
terminated(gemma4_args, eof).parse_next(&mut input)
|
||||
}
|
||||
|
||||
/// Parse Gemma4's custom key-value argument object content.
|
||||
fn gemma4_args(input: &mut &str) -> 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 &str) -> 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 &str) -> 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 &str) -> 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 &'i str) -> 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 &str) -> ModalResult<Map<String, Value>> {
|
||||
delimited(literal("{"), gemma4_args, literal("}")).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a Gemma4 array value.
|
||||
fn gemma4_array_value(input: &mut &str) -> ModalResult<Vec<Value>> {
|
||||
delimited(literal("["), gemma4_array_content, literal("]")).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse Gemma4 array content.
|
||||
fn gemma4_array_content(input: &mut &str) -> 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 &str) -> 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 &str) -> 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 super::{
|
||||
Gemma4ToolParser, ToolCallDelta, ToolParser, ToolParserOutput, gemma4_array_content,
|
||||
parse_gemma4_args,
|
||||
};
|
||||
use crate::tool::{Tool, ToolParserTestExt as _};
|
||||
|
||||
fn parse_gemma4_array(array: &str) -> super::Result<Vec<Value>> {
|
||||
let mut input = array;
|
||||
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]) -> ToolParserOutput {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
let mut output = ToolParserOutput::default();
|
||||
for chunk in chunks {
|
||||
output.append(parser.parse_chunk(chunk).unwrap());
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
output.coalesce_calls()
|
||||
}
|
||||
|
||||
fn first_call(output: &ToolParserOutput) -> &ToolCallDelta {
|
||||
output.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 output = parser
|
||||
.parse_complete("<|tool_call>call:get_weather{location:<|\"|>London<|\"|>}<tool_call|>")
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).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 output = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:get_weather{",
|
||||
"location:<|\"|>Paris",
|
||||
", France",
|
||||
"<|\"|>}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "location": "Paris, France" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_text_before_and_after_tool_call() {
|
||||
let output = collect_stream(&[
|
||||
"Let me check ",
|
||||
"the weather. ",
|
||||
"<|tool_call>",
|
||||
"call:get_weather{",
|
||||
"location:<|\"|>London<|\"|>}",
|
||||
"<tool_call|><",
|
||||
"div>",
|
||||
]);
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check the weather. <div>");
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "location": "London" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_waits_for_complete_tool_call() {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
let mut output = ToolParserOutput::default();
|
||||
|
||||
for chunk in [
|
||||
"<|tool_call>",
|
||||
"call:get_weather{",
|
||||
"location:<|\"|>Paris<|\"|>}",
|
||||
] {
|
||||
output.append(parser.parse_chunk(chunk).unwrap());
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
output.append(parser.parse_chunk("<tool_call|>").unwrap());
|
||||
let output = output.coalesce_calls();
|
||||
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "location": "Paris" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_boolean_split_across_chunks() {
|
||||
let output = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:search{input:{all:tru",
|
||||
"e}}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("search"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "input": { "all": true } })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_false_split_across_chunks() {
|
||||
let output = collect_stream(&["<|tool_call>", "call:set{flag:fals", "e}", "<tool_call|>"]);
|
||||
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("set"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "flag": false })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_number_split_across_chunks() {
|
||||
let output = collect_stream(&["<|tool_call>", "call:set{count:4", "2}", "<tool_call|>"]);
|
||||
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("set"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "count": 42 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_split_string_delimiter() {
|
||||
let output = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:todowrite{",
|
||||
"content:<|\"|>Buy milk<|",
|
||||
"\"|>}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("todowrite"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "content": "Buy milk" })
|
||||
);
|
||||
assert!(!first_call(&output).arguments.contains("<|"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_split_tool_call_end_marker() {
|
||||
let output = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:get_weather{location:<|\"|>Paris<|\"|>}<tool",
|
||||
"_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "location": "Paris" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_end_marker_literal_inside_string() {
|
||||
let output = collect_stream(&[
|
||||
"<|tool_call>",
|
||||
"call:todowrite{",
|
||||
"content:<|\"|>literal }<tool_call|> inside",
|
||||
"<|\"|>}",
|
||||
"<tool_call|>",
|
||||
]);
|
||||
|
||||
assert_eq!(first_call(&output).name.as_deref(), Some("todowrite"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({ "content": "literal }<tool_call|> inside" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_streaming_handles_html_argument_without_duplication() {
|
||||
let output = 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(&output).name.as_deref(), Some("write_file"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).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 output = 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(&output).name.as_deref(), Some("Edit"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&first_call(&output).arguments).unwrap(),
|
||||
json!({
|
||||
"file_path": "src/env.py",
|
||||
"old_string": "old_val",
|
||||
"new_string": "new_val",
|
||||
"replace_all": false,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
first_call(&output).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 output = parser.parse_chunk("<").unwrap();
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(output.normal_text, "<");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_finish_rejects_complete_args_without_end_marker() {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
for chunk in ["<|tool_call>", "call:get_status{}"] {
|
||||
parser.parse_chunk(chunk).unwrap();
|
||||
}
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
assert!(error.to_report_string().contains("incomplete Gemma4 tool call"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_reset_preserves_internally_buffered_arguments() {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
for chunk in [
|
||||
"<|tool_call>",
|
||||
"call:write_file{",
|
||||
"content:<|\"|>hello ",
|
||||
"world<|\"|>",
|
||||
] {
|
||||
parser.parse_chunk(chunk).unwrap();
|
||||
}
|
||||
|
||||
let raw = parser.reset();
|
||||
|
||||
assert_eq!(
|
||||
raw,
|
||||
"<|tool_call>call:write_file{content:<|\"|>hello world<|\"|>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemma4_reset_preserves_completed_arguments_after_parse_error() {
|
||||
let mut parser = Gemma4ToolParser::new(&test_tools());
|
||||
let input = "<|tool_call>call:set{broken}<tool_call|>";
|
||||
|
||||
let _error = parser.parse_chunk(input).unwrap_err();
|
||||
let raw = parser.reset();
|
||||
|
||||
assert_eq!(raw, input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
use super::{GlmXmlToolParser, Separator};
|
||||
use crate::tool::{Result, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
/// 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 {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.0.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.0.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.0.reset()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
use super::{GlmXmlToolParser, Separator};
|
||||
use crate::tool::{Result, StructuralTagModel, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
/// 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 structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::Glm47)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.0.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.0.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.0.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::Glm47MoeToolParser;
|
||||
use crate::tool::ToolParserTestExt as _;
|
||||
use crate::tool::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 output = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me search for that.\n");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[1].name.as_deref(), Some("add"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = 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>(&output.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 output = parser.parse_complete("<tool_call>add</tool_call>").unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("add"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
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::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParserOutput};
|
||||
use crate::tool::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, PartialEq, Eq)]
|
||||
enum GlmMode {
|
||||
Text,
|
||||
ToolCall { tool_call_end_scan: MarkerScanState },
|
||||
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, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
GlmEvent::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
GlmEvent::ToolCallStart => {
|
||||
self.mode = GlmMode::ToolCall {
|
||||
tool_call_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
GlmEvent::ToolCall { name, raw_params } => {
|
||||
self.mode = GlmMode::AfterToolCall;
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
output.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
GlmEvent::IgnoredRest => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = GlmMode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_glm_event(input, &mut self.mode, self.separator)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
if !self.buffer.is_empty() {
|
||||
match self.mode {
|
||||
GlmMode::Text => output.normal_text.push_str(&self.buffer),
|
||||
GlmMode::ToolCall { .. } => {
|
||||
return Err(parsing_failed!("incomplete GLM MoE tool call"));
|
||||
}
|
||||
GlmMode::AfterToolCall => {}
|
||||
}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a GLM event for the current parser mode.
|
||||
fn parse_next_glm_event(
|
||||
input: &mut GlmInput<'_>,
|
||||
mode: &mut GlmMode,
|
||||
separator: Separator,
|
||||
) -> ModalResult<GlmEvent> {
|
||||
match mode {
|
||||
GlmMode::Text => parse_text_event(input),
|
||||
GlmMode::ToolCall { tool_call_end_scan } => {
|
||||
tool_call_event(input, separator, tool_call_end_scan)
|
||||
}
|
||||
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,
|
||||
tool_call_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<GlmEvent> {
|
||||
let (body,) = seq!(
|
||||
take_until_marker(TOOL_CALL_END, tool_call_end_scan),
|
||||
_: 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),
|
||||
_: literal(ARG_VALUE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok((key.trim().to_string(), value.to_string()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::Glm45MoeToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserTestExt as _};
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.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 output = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me search for that.\n");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[1].name.as_deref(), Some("add"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[1].arguments).unwrap(),
|
||||
json!({"x": 1, "y": 2})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_parse_complete_preserves_raw_closing_tag_text_in_arg_value() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
let output = 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>(&output.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 output = collect_stream(&mut parser, &["hello ", "world"]);
|
||||
|
||||
assert_eq!(output.normal_text, "hello world");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_streaming_preserves_prefix_text() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
|
||||
let output = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"Prefix ",
|
||||
&glm45_tool_call("get_weather", &[("city", "Hangzhou")]),
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(output.normal_text, "Prefix ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_streaming_handles_start_token_split_across_chunks() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
let output = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"hello <tool",
|
||||
"_call>get_weather\n",
|
||||
"<arg_key>city</arg_key><arg_value>Paris</arg_value></tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(output.normal_text, "hello ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.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 output = parser.parse_chunk("<tool_call>get_weather\n<arg_key>city</arg_key>").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glm45_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = Glm45MoeToolParser::new(&test_tools());
|
||||
|
||||
parser.parse_chunk("<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.parse_chunk("<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 output = collect_stream(
|
||||
&mut parser,
|
||||
&[&format!(
|
||||
"{}<|endoftext|>",
|
||||
glm45_tool_call("get_weather", &[("city", "Paris")])
|
||||
)],
|
||||
);
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,581 @@
|
||||
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, rest, take_until};
|
||||
|
||||
use super::parameters::ToolSchemas;
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::tool::{StructuralTagModel, Tool};
|
||||
|
||||
const TOOL_CALLS_START: &str = "<tool_calls>";
|
||||
const TOOL_CALLS_END: &str = "</tool_calls>";
|
||||
const TOOL_CALL_START: &str = "<tool_call>";
|
||||
const TOOL_CALL_END: &str = "</tool_call>";
|
||||
const TOOL_SEP: &str = "<tool_sep>";
|
||||
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 HyV3Input<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum HyV3Mode {
|
||||
Text,
|
||||
ToolBlock { tool_call_end_scan: MarkerScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum HyV3Event {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolBlockStart,
|
||||
ToolCall {
|
||||
name: String,
|
||||
raw_params: Vec<(String, String)>,
|
||||
},
|
||||
ToolBlockEnd,
|
||||
IgnoredRest,
|
||||
}
|
||||
|
||||
/// Tool parser for HY3 XML-style tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <tool_calls>
|
||||
/// <tool_call>get_weather<tool_sep>
|
||||
/// <arg_key>city</arg_key><arg_value>Beijing</arg_value>
|
||||
/// </tool_call>
|
||||
/// </tool_calls>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are emitted only after a full `<tool_call>` block is parsed.
|
||||
/// HY3 marker tokens are added-vocabulary tokens rather than tokenizer special
|
||||
/// tokens, so the default `preserve_special_tokens() == false` is sufficient.
|
||||
pub struct HyV3ToolParser {
|
||||
buffer: String,
|
||||
mode: HyV3Mode,
|
||||
emitted_tool_count: usize,
|
||||
tool_parameters: ToolSchemas,
|
||||
}
|
||||
|
||||
impl HyV3ToolParser {
|
||||
/// Create a HY3 tool parser.
|
||||
fn new(tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: HyV3Mode::Text,
|
||||
emitted_tool_count: 0,
|
||||
tool_parameters: ToolSchemas::from_tools(tools),
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed HY3 event to parser state and output.
|
||||
fn apply_event(&mut self, event: HyV3Event, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
HyV3Event::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
HyV3Event::ToolBlockStart => {
|
||||
self.mode = HyV3Mode::ToolBlock {
|
||||
tool_call_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
HyV3Event::ToolCall { name, raw_params } => {
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
output.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
HyV3Event::ToolBlockEnd => self.mode = HyV3Mode::Done,
|
||||
HyV3Event::IgnoredRest => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for HyV3ToolParser {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::HyV3)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_hy_v3_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match self.mode {
|
||||
HyV3Mode::Text => output.normal_text.push_str(&self.buffer),
|
||||
HyV3Mode::ToolBlock { .. } => return Err(parsing_failed!("incomplete HY3 tool call")),
|
||||
HyV3Mode::Done => {}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = HyV3Mode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a HY3 event for the current parser mode.
|
||||
fn parse_next_hy_v3_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
mode: &mut HyV3Mode,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
match mode {
|
||||
HyV3Mode::Text => parse_text_event(input),
|
||||
HyV3Mode::ToolBlock { tool_call_end_scan } => {
|
||||
parse_tool_block_event(input, tool_call_end_scan)
|
||||
}
|
||||
HyV3Mode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode HY3 event.
|
||||
fn parse_text_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
alt((tool_block_start_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a HY3 tool-block start marker.
|
||||
fn tool_block_start_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
literal(TOOL_CALLS_START).value(HyV3Event::ToolBlockStart).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next HY3 marker.
|
||||
fn safe_text_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
safe_text_len(input, TOOL_CALLS_START).map(|len| HyV3Event::Text { len })
|
||||
}
|
||||
|
||||
/// Parse one event inside a HY3 tool block.
|
||||
fn parse_tool_block_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
tool_call_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
alt((tool_block_end_event, |input: &mut HyV3Input<'_>| {
|
||||
tool_call_event(input, tool_call_end_scan)
|
||||
}))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a HY3 tool-block end marker.
|
||||
fn tool_block_end_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
(ws0, literal(TOOL_CALLS_END)).value(HyV3Event::ToolBlockEnd).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a complete HY3 tool-call block.
|
||||
fn tool_call_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
tool_call_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
let (name, body) = seq!(
|
||||
_: ws0,
|
||||
_: literal(TOOL_CALL_START),
|
||||
take_until(0.., TOOL_SEP),
|
||||
_: literal(TOOL_SEP),
|
||||
take_until_marker(TOOL_CALL_END, tool_call_end_scan),
|
||||
_: literal(TOOL_CALL_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
let raw_params = parse_tool_call_params(body)?;
|
||||
|
||||
Ok(HyV3Event::ToolCall {
|
||||
name: name.trim().to_string(),
|
||||
raw_params,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse all parameter blocks inside a complete HY3 tool call.
|
||||
fn parse_tool_call_params(tool_call_body: &str) -> ModalResult<Vec<(String, String)>> {
|
||||
let mut input = tool_call_body;
|
||||
delimited(ws0, repeat(0.., terminated(parameter, ws0)), eof).parse_next(&mut input)
|
||||
}
|
||||
|
||||
/// Parse a HY3 argument key/value block.
|
||||
fn parameter(input: &mut &str) -> ModalResult<(String, String)> {
|
||||
let (name, value) = seq!(
|
||||
_: literal(ARG_KEY_START),
|
||||
take_until(0.., ARG_KEY_END),
|
||||
_: literal(ARG_KEY_END),
|
||||
_: ws0,
|
||||
_: literal(ARG_VALUE_START),
|
||||
take_until(0.., ARG_VALUE_END),
|
||||
_: literal(ARG_VALUE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok((name.trim().to_string(), value.to_string()))
|
||||
}
|
||||
|
||||
/// Parse ignored rest after the HY3 tool block ends.
|
||||
fn ignored_rest_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
rest.value(HyV3Event::IgnoredRest).parse_next(input)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::{HyV3ToolParser, ToolParser};
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
fn build_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("\n");
|
||||
format!("<tool_call>{function_name}<tool_sep>{params}</tool_call>")
|
||||
}
|
||||
|
||||
fn build_tool_calls(tool_calls: &[String]) -> String {
|
||||
format!("<tool_calls>\n{}\n</tool_calls>", tool_calls.join("\n"))
|
||||
}
|
||||
|
||||
fn parsed_arguments(output: &ToolParserOutput, index: usize) -> Value {
|
||||
serde_json::from_str(&output.calls[index].arguments).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_does_not_preserve_special_tokens() {
|
||||
let parser = HyV3ToolParser::new(&test_tools());
|
||||
|
||||
assert!(!parser.preserve_special_tokens());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_without_tool_call_keeps_text() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let output = parser.parse_complete("This is a plain response.").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "This is a plain response.");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_zero_arg_inline_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
"<tool_calls><tool_call>get_current_date<tool_sep></tool_call></tool_calls>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_current_date"));
|
||||
assert_eq!(parsed_arguments(&output, 0), json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_zero_arg_newline_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
"<tool_calls>\n<tool_call>get_current_date<tool_sep>\n</tool_call>\n</tool_calls>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_current_date"));
|
||||
assert_eq!(parsed_arguments(&output, 0), json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_arguments_on_same_line() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
"<tool_calls><tool_call>get_weather<tool_sep><arg_key>city</arg_key><arg_value>Beijing</arg_value><arg_key>date</arg_key><arg_value>2026-03-30</arg_value></tool_call></tool_calls>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
parsed_arguments(&output, 0),
|
||||
json!({ "city": "Beijing", "date": "2026-03-30" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_arguments_with_newlines() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_calls(&[build_tool_call(
|
||||
"get_weather",
|
||||
&[("city", "Beijing"), ("date", "2026-03-30")],
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
parsed_arguments(&output, 0),
|
||||
json!({ "city": "Beijing", "date": "2026-03-30" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_preserves_prefix_and_ignores_trailing_text() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"Checking.{} trailing text",
|
||||
build_tool_calls(&[build_tool_call("get_current_date", &[])])
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Checking.");
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_current_date"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_multiple_tool_calls_in_one_block() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_calls(&[
|
||||
build_tool_call(
|
||||
"get_weather",
|
||||
&[("city", "Beijing"), ("date", "2026-03-30")],
|
||||
),
|
||||
build_tool_call(
|
||||
"get_weather",
|
||||
&[("city", "Hangzhou"), ("date", "2026-03-30")],
|
||||
),
|
||||
]))
|
||||
.unwrap();
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
normal_text: "",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"city\":\"Beijing\",\"date\":\"2026-03-30\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"city\":\"Hangzhou\",\"date\":\"2026-03-30\"}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_converts_schema_types() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_calls(&[build_tool_call(
|
||||
"convert",
|
||||
&[
|
||||
("whole", "5.3"),
|
||||
("flag", "true"),
|
||||
("payload", r#"{"k":1}"#),
|
||||
("items", "[1,2]"),
|
||||
],
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
parsed_arguments(&output, 0),
|
||||
json!({
|
||||
"whole": 5.3,
|
||||
"flag": true,
|
||||
"payload": { "k": 1 },
|
||||
"items": [1, 2]
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_without_tool_call_emits_text_incrementally() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let mut output = ToolParserOutput::default();
|
||||
|
||||
output.append(parser.parse_chunk("This is ").unwrap());
|
||||
output.append(parser.parse_chunk("a plain ").unwrap());
|
||||
output.append(parser.parse_chunk("response.").unwrap());
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(output.normal_text, "This is a plain response.");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_extracts_zero_arg_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"<tool_calls>",
|
||||
"\n<tool_call>",
|
||||
"get_current_date",
|
||||
"<tool_sep>",
|
||||
"\n</tool_call>",
|
||||
"\n</tool_calls>",
|
||||
];
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_current_date"));
|
||||
assert_eq!(parsed_arguments(&output, 0), json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_extracts_arguments() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"<tool_calls>",
|
||||
"\n<tool_call>",
|
||||
"get_weather",
|
||||
"<tool_sep>",
|
||||
"\n<arg_key>city</arg_key>",
|
||||
"\n<arg_value>Beijing</arg_value>",
|
||||
"\n<arg_key>date</arg_key>",
|
||||
"\n<arg_value>2026-03-30</arg_value>",
|
||||
"\n</tool_call>",
|
||||
"\n</tool_calls>",
|
||||
];
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
parsed_arguments(&output, 0),
|
||||
json!({ "city": "Beijing", "date": "2026-03-30" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_preserves_prefix_text() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"Checking.",
|
||||
"<tool_calls>",
|
||||
"\n<tool_call>",
|
||||
"get_current_date",
|
||||
"<tool_sep>",
|
||||
"\n</tool_call>",
|
||||
"\n</tool_calls>",
|
||||
];
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "Checking.");
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_current_date"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_extracts_multiple_tool_calls_in_one_block() {
|
||||
let input = build_tool_calls(&[
|
||||
build_tool_call(
|
||||
"get_weather",
|
||||
&[("city", "Beijing"), ("date", "2026-03-30")],
|
||||
),
|
||||
build_tool_call(
|
||||
"get_weather",
|
||||
&[("city", "Hangzhou"), ("date", "2026-03-30")],
|
||||
),
|
||||
]);
|
||||
let chunks = split_by_chars(&input, 9);
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(parsed_arguments(&output, 0)["city"], json!("Beijing"));
|
||||
assert_eq!(parsed_arguments(&output, 1)["city"], json!("Hangzhou"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_handles_start_marker_split_across_chunks() {
|
||||
let input = format!(
|
||||
"hello {}",
|
||||
build_tool_calls(&[build_tool_call("get_weather", &[("city", "Beijing")])])
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "hello ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(parsed_arguments(&output, 0), json!({ "city": "Beijing" }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_does_not_emit_incomplete_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let mut output = ToolParserOutput::default();
|
||||
|
||||
parser
|
||||
.parse_into(
|
||||
"<tool_calls><tool_call>get_weather<tool_sep><arg_key>city</arg_key><arg_value>Bei",
|
||||
&mut output,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
parser.parse_chunk("<tool_calls><tool_call>get_weather<tool_sep>").unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete HY3 tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_malformed_tool_call_fails_fast() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools());
|
||||
let error = parser
|
||||
.parse_complete(
|
||||
"<tool_calls><tool_call>get_weather<tool_sep><arg_key>city</arg_key><arg_value>Beijing</tool_call></tool_calls>",
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(error.to_report_string().starts_with("tool parser parsing failed:"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,495 @@
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::{alt, peek, seq};
|
||||
use winnow::error::{ContextError, ErrMode, ModalResult, StrContext};
|
||||
use winnow::prelude::*;
|
||||
use winnow::token::{any, literal};
|
||||
|
||||
use super::{
|
||||
JsonToolCallConfig, JsonToolCallEvent, JsonToolCallWhitespace, JsonToolInput,
|
||||
tool_call_header_event,
|
||||
};
|
||||
use crate::tool::utils::{
|
||||
JsonObjectScanState, json_str, parse_buffered_event, safe_text_len, take_json_object,
|
||||
};
|
||||
use crate::tool::{Result, Tool, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
|
||||
const TOOL_CALL_START: &str = "<tool_call>";
|
||||
const TOOL_CALL_END: &str = "</tool_call>";
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum Granite4Mode {
|
||||
Text,
|
||||
Header,
|
||||
/// Parsing the arguments value:
|
||||
/// `None` until the first byte decides object vs string;
|
||||
/// `Some` while streaming an object value.
|
||||
Args {
|
||||
json_scan: Option<JsonObjectScanState>,
|
||||
},
|
||||
/// Arguments done; consume the object's closing `}` and `</tool_call>`.
|
||||
Close,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum Granite4Event {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolCallStart,
|
||||
ToolCallHeader {
|
||||
function_name: String,
|
||||
},
|
||||
/// Verbatim bytes of an object-valued arguments payload; `complete` once the
|
||||
/// object scan reaches its closing brace.
|
||||
ObjectArgsDelta {
|
||||
len: usize,
|
||||
complete: bool,
|
||||
},
|
||||
/// Decoded contents of a string-valued arguments payload.
|
||||
StringArgs {
|
||||
decoded: String,
|
||||
},
|
||||
ToolCallEnd,
|
||||
}
|
||||
|
||||
/// Tool parser for Granite 4 `<tool_call>…</tool_call>` JSON tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <tool_call>{"name": "get_weather", "arguments": {"city": "Boston"}}</tool_call>
|
||||
/// ```
|
||||
///
|
||||
/// Parallel calls are repeated `<tool_call>…</tool_call>` blocks with ordinary
|
||||
/// content interleaved between them. This reuses the shared JSON helpers for
|
||||
/// everything except one Granite 4 specific step (`args_event`): the `arguments`
|
||||
/// value may be a JSON object (kept verbatim) **or** a JSON string whose decoded
|
||||
/// contents are the arguments (the `# test granite behavior` case in Python).
|
||||
pub struct Granite4ToolParser {
|
||||
buffer: String,
|
||||
mode: Granite4Mode,
|
||||
active_tool_index: Option<usize>,
|
||||
emitted_tool_count: usize,
|
||||
}
|
||||
|
||||
impl Granite4ToolParser {
|
||||
/// Create a Granite 4 tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: Granite4Mode::Text,
|
||||
active_tool_index: None,
|
||||
emitted_tool_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed Granite 4 event to parser state and output.
|
||||
fn apply_event(&mut self, event: Granite4Event, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
Granite4Event::Text { len } => output.normal_text.push_str(&self.buffer[..len]),
|
||||
Granite4Event::ToolCallStart => self.mode = Granite4Mode::Header,
|
||||
Granite4Event::ToolCallHeader { function_name } => {
|
||||
let tool_index = self.emitted_tool_count;
|
||||
self.emitted_tool_count += 1;
|
||||
self.active_tool_index = Some(tool_index);
|
||||
self.mode = Granite4Mode::Args { json_scan: None };
|
||||
output.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: Some(function_name),
|
||||
arguments: String::new(),
|
||||
});
|
||||
}
|
||||
Granite4Event::ObjectArgsDelta { len, complete } => {
|
||||
let arguments = self.buffer[..len].to_string();
|
||||
self.push_arguments(arguments, output)?;
|
||||
if complete {
|
||||
self.mode = Granite4Mode::Close;
|
||||
}
|
||||
}
|
||||
Granite4Event::StringArgs { decoded } => {
|
||||
self.push_arguments(decoded, output)?;
|
||||
self.mode = Granite4Mode::Close;
|
||||
}
|
||||
Granite4Event::ToolCallEnd => {
|
||||
self.active_tool_index = None;
|
||||
self.mode = Granite4Mode::Text;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Append one arguments delta to the active tool call.
|
||||
fn push_arguments(&self, arguments: String, output: &mut ToolParserOutput) -> Result<()> {
|
||||
let Some(tool_index) = self.active_tool_index else {
|
||||
return Err(parsing_failed!(
|
||||
"Granite4 arguments without an active tool call"
|
||||
));
|
||||
};
|
||||
output.calls.push(ToolCallDelta {
|
||||
tool_index,
|
||||
name: None,
|
||||
arguments,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = Granite4Mode::Text;
|
||||
self.active_tool_index = None;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Granite4ToolParser {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_granite4_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match &self.mode {
|
||||
Granite4Mode::Text => output.normal_text.push_str(&self.buffer),
|
||||
Granite4Mode::Header | Granite4Mode::Args { .. } | Granite4Mode::Close => {
|
||||
return Err(parsing_failed!("incomplete Granite4 tool call"));
|
||||
}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
Granite4ToolParser::reset(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Granite 4 event for the current parser mode.
|
||||
fn parse_next_granite4_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
mode: &mut Granite4Mode,
|
||||
) -> ModalResult<Granite4Event> {
|
||||
match mode {
|
||||
Granite4Mode::Text => text_event(input),
|
||||
Granite4Mode::Header => header_event(input),
|
||||
Granite4Mode::Args { json_scan } => args_event(input, json_scan),
|
||||
Granite4Mode::Close => close_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse content text or the start of a `<tool_call>` block. *(reuses `safe_text_len`)*
|
||||
fn text_event(input: &mut JsonToolInput<'_>) -> ModalResult<Granite4Event> {
|
||||
alt((
|
||||
|input: &mut JsonToolInput<'_>| {
|
||||
seq!(_: literal(TOOL_CALL_START), _: ws0)
|
||||
.value(Granite4Event::ToolCallStart)
|
||||
.parse_next(input)
|
||||
},
|
||||
|input: &mut JsonToolInput<'_>| {
|
||||
safe_text_len(input, TOOL_CALL_START).map(|len| Granite4Event::Text { len })
|
||||
},
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse the `{"name":"X","arguments":` header before the value. *(reuses `tool_call_header_event`)*
|
||||
fn header_event(input: &mut JsonToolInput<'_>) -> ModalResult<Granite4Event> {
|
||||
const CONFIG: JsonToolCallConfig = JsonToolCallConfig {
|
||||
parser_name: "Granite4",
|
||||
start_marker: "",
|
||||
end_marker: "",
|
||||
marker_whitespace: JsonToolCallWhitespace::Optional,
|
||||
delimiter: None,
|
||||
name_key: "name",
|
||||
arguments_key: &["arguments"],
|
||||
};
|
||||
|
||||
match tool_call_header_event(input, CONFIG)? {
|
||||
JsonToolCallEvent::ToolCallHeader { function_name } => {
|
||||
Ok(Granite4Event::ToolCallHeader { function_name })
|
||||
}
|
||||
_ => unreachable!("tool_call_header_event only emits ToolCallHeader"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse one arguments-value event.
|
||||
///
|
||||
/// GRANITE 4 SPECIFIC - the sole behavior that differs from the shared
|
||||
/// `<tool_call>` JSON parsers. The value is either a JSON object (kept verbatim,
|
||||
/// streamed incrementally via `take_json_object`) or an escaped JSON string
|
||||
/// (decoded whole via `json_str`). The string form is why we cannot just forward
|
||||
/// raw arg bytes like the sibling parsers do: an escaped string only resolves
|
||||
/// once seen whole and unescaped.
|
||||
fn args_event(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
json_scan: &mut Option<JsonObjectScanState>,
|
||||
) -> ModalResult<Granite4Event> {
|
||||
if let Some(scan) = json_scan {
|
||||
let len = take_json_object(input, scan)?;
|
||||
return Ok(Granite4Event::ObjectArgsDelta {
|
||||
len,
|
||||
complete: scan.complete(),
|
||||
});
|
||||
}
|
||||
|
||||
match peek(any).parse_next(input)? {
|
||||
'{' => {
|
||||
let mut scan = JsonObjectScanState::default();
|
||||
let len = take_json_object(input, &mut scan)?;
|
||||
let complete = scan.complete();
|
||||
*json_scan = Some(scan);
|
||||
Ok(Granite4Event::ObjectArgsDelta { len, complete })
|
||||
}
|
||||
'"' => Ok(Granite4Event::StringArgs {
|
||||
decoded: json_str(input)?,
|
||||
}),
|
||||
_ => {
|
||||
let mut error = ContextError::new();
|
||||
error.push(StrContext::Label("Granite4 arguments"));
|
||||
Err(ErrMode::Cut(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the tool-call object's closing `}` and the `</tool_call>` end marker.
|
||||
fn close_event(input: &mut JsonToolInput<'_>) -> ModalResult<Granite4Event> {
|
||||
seq!(_: ws0, _: literal("}"), _: ws0, _: literal(TOOL_CALL_END))
|
||||
.value(Granite4Event::ToolCallEnd)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::Granite4ToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
#[test]
|
||||
fn granite4_parse_complete_without_tool_call_keeps_text() {
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
let output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_parse_complete_object_args() {
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
r#"<tool_call>{"name":"get_weather","arguments":{"city":"Boston"}}</tool_call>"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, r#"{"city":"Boston"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_parse_complete_string_args() {
|
||||
// GRANITE4-SPECIFIC: `arguments` may be a pre-serialized JSON string; its
|
||||
// decoded contents become the arguments.
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
r#"<tool_call>{"name":"get_weather","arguments":"{\"city\":\"Boston\"}"}</tool_call>"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, r#"{"city":"Boston"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_extracts_interleaved_content_and_mixed_args() {
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
r#"before <tool_call>{"name":"find_bbox","arguments":"{\"x\":1}"}</tool_call> middle <tool_call>{"name":"get_weather","arguments":{"city":"Boston"}}</tool_call> after"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
normal_text: "before middle after",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"find_bbox",
|
||||
),
|
||||
arguments: "{\"x\":1}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"city\":\"Boston\"}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_streaming_handles_split_markers() {
|
||||
let input = r#"hello <tool_call>{"name":"get_weather","arguments":{"city":"Tokyo"}}</tool_call> bye"#;
|
||||
let chunks = split_by_chars(input, 5);
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "hello bye");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, r#"{"city":"Tokyo"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_streaming_emits_object_argument_deltas() {
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
r#"<tool_call>{"name":"get_weather","arguments":"#,
|
||||
r#"{"city":"#,
|
||||
r#""Beijing""#,
|
||||
r#"}"#,
|
||||
r#"}</tool_call>"#,
|
||||
];
|
||||
|
||||
let mut output = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
output.append(next);
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, [r#"{"city":"#, r#""Beijing""#, r#"}"#]);
|
||||
assert_eq!(
|
||||
output.coalesce_calls().calls[0].arguments,
|
||||
r#"{"city":"Beijing"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_string_args_split_across_chunks() {
|
||||
let input = r#"<tool_call>{"name":"f","arguments":"{\"a\":1}"}</tool_call>"#;
|
||||
let chunks = split_by_chars(input, 3);
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("f"));
|
||||
assert_eq!(output.calls[0].arguments, r#"{"a":1}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_streaming_handles_marker_and_json_whitespace() {
|
||||
// Granite spaces the markers (`<tool_call> {…} </tool_call>`) and the JSON
|
||||
// (`"name": …`). Since `args_event` has no leading `ws0`, this guards that
|
||||
// the header consumes the whitespace before the arguments value.
|
||||
let input = concat!(
|
||||
"Here goes the bbox call: \n",
|
||||
r#"<tool_call> {"name": "find_bbox", "arguments": "{\"coordinates\": [[23.54, 43.1], [-12.2, 54.3], [4, 5]], \"coordinate_type\": \"latlong\"}"} </tool_call>"#,
|
||||
" Now the stock price call: \n ",
|
||||
r#"<tool_call> {"name": "get_stock_price", "arguments": {"symbol": "AAPL", "start_date": "2021-01-01", "end_date": "2021-12-31"}} </tool_call>"#,
|
||||
" Now another bbox call: \n ",
|
||||
r#"<tool_call> {"name": "find_bbox", "arguments": "{\"coordinates\": [[23.54, 43.1], [-12.2, 54.3], [4, 5]], \"coordinate_type\": \"latlong\"}"} </tool_call>"#,
|
||||
" See? I'm a helpful assistant.",
|
||||
);
|
||||
let chunks = split_by_chars(input, 3);
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
normal_text: "Here goes the bbox call: \n Now the stock price call: \n Now another bbox call: \n See? I'm a helpful assistant.",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"find_bbox",
|
||||
),
|
||||
arguments: "{\"coordinates\": [[23.54, 43.1], [-12.2, 54.3], [4, 5]], \"coordinate_type\": \"latlong\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"get_stock_price",
|
||||
),
|
||||
arguments: "{\"symbol\": \"AAPL\", \"start_date\": \"2021-01-01\", \"end_date\": \"2021-12-31\"}",
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 2,
|
||||
name: Some(
|
||||
"find_bbox",
|
||||
),
|
||||
arguments: "{\"coordinates\": [[23.54, 43.1], [-12.2, 54.3], [4, 5]], \"coordinate_type\": \"latlong\"}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]].assert_debug_eq(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
parser
|
||||
.parse_chunk(r#"<tool_call>{"name":"get_weather","arguments":{"city""#)
|
||||
.unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete Granite4 tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_rejects_non_object_non_string_args() {
|
||||
let mut parser = Granite4ToolParser::new(&test_tools());
|
||||
let error = parser
|
||||
.parse_chunk(r#"<tool_call>{"name":"f","arguments":42}</tool_call>"#)
|
||||
.unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: invalid Granite4 arguments"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn granite4_preserve_special_tokens_is_false() {
|
||||
let parser = Granite4ToolParser::new(&test_tools());
|
||||
assert!(!parser.preserve_special_tokens());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::tool::{Result, StructuralTagModel, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
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 {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::Hermes)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.inner.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.inner.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.inner.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::HermesToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_parse_complete_extracts_raw_json_arguments() {
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
let arguments = r#"{ "location": "Tokyo", "days": "3" }"#;
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"Let me check.\n{}",
|
||||
build_tool_call("get_weather", arguments)
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check.\n");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_accepts_newline_after_tool_call_start() {
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
r#"<tool_call>
|
||||
{"name":"get_weather","arguments":{}}</tool_call>"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo",}"#;
|
||||
let output = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap();
|
||||
|
||||
assert_eq!(output.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 output = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
output.append(next);
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(output.normal_text, "preface suffix");
|
||||
assert_eq!(
|
||||
output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "hello ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = HermesToolParser::new(&test_tools());
|
||||
parser
|
||||
.parse_chunk(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,345 @@
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::tool::{Result, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
const INTERNLM2_CONFIG: JsonToolCallConfig = JsonToolCallConfig {
|
||||
parser_name: "InternLM2",
|
||||
start_marker: "<|action_start|><|plugin|>",
|
||||
end_marker: "<|action_end|>",
|
||||
marker_whitespace: JsonToolCallWhitespace::Optional,
|
||||
delimiter: None,
|
||||
name_key: "name",
|
||||
// The Python parser's `get_arguments()` accepts either `parameters` or
|
||||
// `arguments` and prefers `parameters` when both are present. This Rust
|
||||
// parser uses first-encountered semantics because the header parser only
|
||||
// permits one args key per tool-call object; if a future model emits
|
||||
// both keys in the same object, the Rust port will accept the first one
|
||||
// and reject the trailing one as a syntax error rather than silently
|
||||
// shadowing it.
|
||||
arguments_key: &["parameters", "arguments"],
|
||||
};
|
||||
|
||||
/// Tool parser for InternLM2 special-token wrapped JSON tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
/// ```text
|
||||
/// <|action_start|><|plugin|>{"name": "get_weather", "parameters": {"location":"Tokyo"}}<|action_end|>
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are already OpenAI-style JSON text, so they are streamed as raw
|
||||
/// argument deltas without schema conversion or JSON normalization.
|
||||
///
|
||||
/// # Divergences from the Python reference
|
||||
///
|
||||
/// This Rust port intentionally diverges from
|
||||
/// `vllm/tool_parsers/internlm2_tool_parser.py` in two user-visible ways:
|
||||
///
|
||||
/// - **Parallel tool calls are supported.** Python silently drops every `<|action_start|>` block
|
||||
/// after the first (`current_tool_id > 0` returns an empty delta); this parser emits every
|
||||
/// well-formed block with incrementing `tool_index`. Models that legitimately emit multiple
|
||||
/// action blocks therefore produce more tool calls under Rust than under Python.
|
||||
/// - **End-marker bytes inside JSON string values are preserved.** Python does
|
||||
/// `action.split("<|action_end|>")[0]` which truncates regardless of JSON context; this parser
|
||||
/// scans matched braces and quotes so a literal `<|action_end|>` inside an arguments string is
|
||||
/// forwarded intact.
|
||||
/// - **Only whitespace is allowed before the `{`.** Python's non-streaming
|
||||
/// `action[action.find("{"):]` drops any bytes before the first `{`, but its streaming path has
|
||||
/// no equivalent and the model format always emits `<|plugin|>{...`; this parser allows only
|
||||
/// whitespace there, matching the other JSON parsers in this crate.
|
||||
/// - **Truncated tool calls error rather than silently dropping.** Python's streaming wrapper
|
||||
/// swallows mid-stream errors with `except Exception: return None` (logging a traceback) while
|
||||
/// its non-streaming path raises `JSONDecodeError`; this parser returns an `incomplete InternLM2
|
||||
/// tool call` error from `finish()`, matching the other JSON parsers and Python's non-streaming
|
||||
/// behavior.
|
||||
///
|
||||
/// # Known unaddressed divergences (TODO)
|
||||
///
|
||||
/// The following Python behaviors are NOT yet matched. They are deferred to
|
||||
/// follow-up work because they require non-local changes to the shared
|
||||
/// `JsonToolCallParser` core that would affect Hermes / Llama / Mistral /
|
||||
/// Qwen as well. If a real-world InternLM2 deployment hits one of these,
|
||||
/// prioritize the corresponding fix.
|
||||
///
|
||||
/// - **Arguments value type.** The shared core requires the arguments value to be a JSON object
|
||||
/// (`take_json_object` rejects anything not starting with `{`). Python's
|
||||
/// `json.dumps(action_dict.get("parameters", ...))` accepts `null`, arrays, strings, and numbers
|
||||
/// and round-trips them verbatim. Models that legitimately emit `"parameters":null` will hard-
|
||||
/// fail under Rust.
|
||||
/// - **Unknown arguments key.** Python falls back to `{}` via `action_dict.get("parameters",
|
||||
/// action_dict.get("arguments", {}))` when neither key is present; the Rust header parser raises
|
||||
/// `parsing failed: invalid InternLM2` for any unrecognized key. A model that emits a typo (e.g.
|
||||
/// `"params"`) breaks the whole response.
|
||||
/// - **Field order independence.** The header parser requires the JSON keys to appear in the order
|
||||
/// `name` then arguments key. Python's `json.loads` + `dict.get` is order-independent, so a model
|
||||
/// emitting `{"parameters":{...},"name":"foo"}` parses in Python but fails in Rust.
|
||||
pub struct Internlm2ToolParser {
|
||||
inner: JsonToolCallParser,
|
||||
}
|
||||
|
||||
impl Internlm2ToolParser {
|
||||
/// Create an InternLM2 tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
inner: JsonToolCallParser::new(INTERNLM2_CONFIG),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Internlm2ToolParser {
|
||||
/// Create a boxed InternLM2 tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Preserve special-token markers while decoding, since
|
||||
/// `<|action_start|>`, `<|plugin|>`, and `<|action_end|>` are tokenizer
|
||||
/// special tokens in InternLM2 models.
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// Feed one decoded text chunk through the InternLM2 parser.
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.inner.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
/// Flush any buffered partial state at end of stream.
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.inner.finish()
|
||||
}
|
||||
|
||||
/// Clear parser state and return currently uncommitted buffered text.
|
||||
fn reset(&mut self) -> String {
|
||||
self.inner.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::Internlm2ToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
const ACTION_START: &str = "<|action_start|><|plugin|>";
|
||||
const ACTION_END: &str = "<|action_end|>";
|
||||
|
||||
fn build_tool_call(function_name: &str, args_key: &str, arguments: &str) -> String {
|
||||
format!(
|
||||
r#"{ACTION_START}{{"name":"{function_name}","{args_key}":{arguments}}}{ACTION_END}"#
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internlm2_parse_complete_without_tool_call_keeps_text() {
|
||||
let mut parser = Internlm2ToolParser::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 internlm2_parse_complete_extracts_parameters_key() {
|
||||
let mut parser = Internlm2ToolParser::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", "parameters", 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 internlm2_parse_complete_extracts_arguments_key_fallback() {
|
||||
let mut parser = Internlm2ToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo"}"#;
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_call("get_weather", "arguments", arguments))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internlm2_accepts_whitespace_after_plugin_marker() {
|
||||
let mut parser = Internlm2ToolParser::new(&test_tools());
|
||||
let result = parser
|
||||
.parse_complete(&format!(
|
||||
r#"{ACTION_START}
|
||||
{{"name":"get_weather","parameters":{{}}}}{ACTION_END}"#
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internlm2_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = Internlm2ToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo",}"#;
|
||||
let result = parser
|
||||
.parse_complete(&build_tool_call("get_weather", "parameters", arguments))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internlm2_streaming_emits_argument_deltas() {
|
||||
let mut parser = Internlm2ToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"preface <|action",
|
||||
"_start|><|plugin|>",
|
||||
r#"{"name":"get_weather","parameters":"#,
|
||||
r#"{"location":"#,
|
||||
r#""Beijing""#,
|
||||
r#"}"#,
|
||||
r#"}<|action_end|> suffix"#,
|
||||
];
|
||||
|
||||
let mut result = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
result.append(next);
|
||||
}
|
||||
result.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(
|
||||
observed_arguments,
|
||||
[r#"{"location":"#, r#""Beijing""#, r#"}"#]
|
||||
);
|
||||
assert_eq!(result.normal_text, "preface suffix");
|
||||
assert_eq!(
|
||||
result.coalesce_calls().calls[0].arguments,
|
||||
r#"{"location":"Beijing"}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internlm2_streaming_handles_split_markers() {
|
||||
let input = format!(
|
||||
"hello {}",
|
||||
build_tool_call("get_weather", "parameters", r#"{"location":"Tokyo"}"#)
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = Internlm2ToolParser::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 internlm2_streaming_extracts_multiple_blocks() {
|
||||
let input = format!(
|
||||
"{}{}",
|
||||
build_tool_call("get_weather", "parameters", r#"{"location":"Shanghai"}"#),
|
||||
build_tool_call("add", "arguments", r#"{"x":1,"y":2}"#),
|
||||
);
|
||||
let chunks = split_by_chars(&input, 7);
|
||||
let mut parser = Internlm2ToolParser::new(&test_tools());
|
||||
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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 internlm2_keeps_end_marker_literal_inside_json_string() {
|
||||
let mut parser = Internlm2ToolParser::new(&test_tools());
|
||||
let arguments = format!(r#"{{"text":"literal {ACTION_END} inside"}}"#);
|
||||
let input = build_tool_call("echo", "parameters", &arguments);
|
||||
|
||||
let result = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internlm2_finish_errors_on_truncated_tool_call() {
|
||||
let mut parser = Internlm2ToolParser::new(&test_tools());
|
||||
let pre_finish = parser
|
||||
.parse_chunk(&format!(
|
||||
r#"{ACTION_START}{{"name":"get_weather","parameters":{{"location""#
|
||||
))
|
||||
.unwrap();
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
assert_eq!(
|
||||
pre_finish.calls[0].name.as_deref(),
|
||||
Some("get_weather"),
|
||||
"name delta is still emitted from parse_chunk() before truncation",
|
||||
);
|
||||
assert!(
|
||||
error.to_report_string().contains("incomplete InternLM2 tool call"),
|
||||
"finish() reports the truncated tool call as incomplete: {}",
|
||||
error.to_report_string(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internlm2_unknown_arguments_key_fails() {
|
||||
let mut parser = Internlm2ToolParser::new(&test_tools());
|
||||
let input = build_tool_call("get_weather", "params", r#"{"location":"Tokyo"}"#);
|
||||
|
||||
let error = parser.parse_chunk(&input).unwrap_err();
|
||||
|
||||
expect![[r#"
|
||||
tool parser parsing failed: invalid InternLM2
|
||||
expected `parameters`, `arguments`"#]]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internlm2_preserve_special_tokens_is_true() {
|
||||
let parser = Internlm2ToolParser::new(&test_tools());
|
||||
assert!(parser.preserve_special_tokens());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,492 @@
|
||||
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::tool::utils::{JsonObjectScanState, parse_buffered_event};
|
||||
use crate::tool::{Result, StructuralTagModel, Tool, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
|
||||
#[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, output: &mut ToolParserOutput) -> 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(),
|
||||
};
|
||||
output.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"
|
||||
));
|
||||
};
|
||||
output.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(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = LlamaJsonMode::Start;
|
||||
self.active_tool_index = None;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Llama3JsonToolParser {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::Llama)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
if !self.commit_start() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if matches!(self.mode, LlamaJsonMode::Passthrough) {
|
||||
output.normal_text.push_str(&self.buffer);
|
||||
self.buffer.clear();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_llama_json_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match &self.mode {
|
||||
LlamaJsonMode::Start | LlamaJsonMode::Passthrough => {
|
||||
output.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"));
|
||||
}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
Llama3JsonToolParser::reset(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_passthrough_never_reenters_tool_parsing() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let mut output = parser.parse_chunk("plain text first ").unwrap();
|
||||
output.append(
|
||||
parser
|
||||
.parse_chunk(&build_tool_call("get_weather", r#"{"location":"Tokyo"}"#))
|
||||
.unwrap(),
|
||||
);
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(
|
||||
output.normal_text,
|
||||
r#"plain text first {"name":"get_weather","parameters":{"location":"Tokyo"}}"#
|
||||
);
|
||||
assert!(output.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 output = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, input);
|
||||
assert!(output.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 output = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, input);
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_extracts_raw_parameters_object() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let arguments = r#"{ "location": "Tokyo", "days": 3 }"#;
|
||||
let output = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn 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 output = parser.parse_complete(&input).unwrap();
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[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 output = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
output.append(next);
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(
|
||||
output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(
|
||||
output.calls[0].arguments,
|
||||
r#"{"location":"Dallas","state":"TX"}"#
|
||||
);
|
||||
assert_eq!(output.calls[1].name.as_deref(), Some("add"));
|
||||
assert_eq!(output.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 output = parser.parse_complete(&build_tool_call("convert", arguments)).unwrap();
|
||||
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_keeps_trailing_whitespace_after_tool_call() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"{}\n\t ",
|
||||
build_tool_call("get_weather", r#"{"location":"Tokyo"}"#)
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn llama_json_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = Llama3JsonToolParser::new(&test_tools());
|
||||
parser.parse_chunk(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.parse_chunk(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
|
||||
.parse_chunk(&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,241 @@
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::tool::{Result, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
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 {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.inner.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.inner.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.inner.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::MistralToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_parse_complete_extracts_raw_json_arguments() {
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
let arguments = r#"{ "location": "Tokyo", "days": "3" }"#;
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"Let me check.\n{}",
|
||||
build_tool_calls(&[build_tool_call("get_weather", arguments)])
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check.\n");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_parse_complete_extracts_pretty_multiple_tool_calls() {
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
let output = 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#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo",}"#;
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_calls(&[build_tool_call(
|
||||
"get_weather",
|
||||
arguments,
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.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 output = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
output.append(next);
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(output.normal_text, "preface suffix");
|
||||
assert_eq!(
|
||||
output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "hello ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_keeps_array_bracket_literal_inside_json_string() {
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
let arguments = r#"{"text":"Array notation: arr[0] = value[1]"}"#;
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_calls(&[build_tool_call("echo", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mistral_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = MistralToolParser::new(&test_tools());
|
||||
parser
|
||||
.parse_chunk(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
|
||||
.parse_chunk(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,503 @@
|
||||
//! Shared parser core for JSON tool calls wrapped by text markers.
|
||||
|
||||
pub use granite4::Granite4ToolParser;
|
||||
pub use hermes::HermesToolParser;
|
||||
pub use internlm2::Internlm2ToolParser;
|
||||
pub use llama::Llama3JsonToolParser;
|
||||
pub use mistral::MistralToolParser;
|
||||
pub use phi4mini::Phi4MiniJsonToolParser;
|
||||
pub use qwen::Qwen3XmlToolParser;
|
||||
|
||||
mod granite4;
|
||||
mod hermes;
|
||||
mod internlm2;
|
||||
mod llama;
|
||||
mod mistral;
|
||||
mod phi4mini;
|
||||
mod qwen;
|
||||
|
||||
use winnow::ascii::multispace0 as ws0;
|
||||
use winnow::combinator::{alt, seq};
|
||||
use winnow::error::{AddContext, ModalResult, StrContext, StrContextValue};
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::{Partial, Stream};
|
||||
use winnow::token::literal;
|
||||
|
||||
use super::utils::{
|
||||
JsonObjectScanState, json_str, parse_buffered_event, safe_text_len, take_json_object,
|
||||
};
|
||||
use super::{Result, ToolCallDelta, ToolParserOutput};
|
||||
|
||||
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,
|
||||
/// Candidate JSON keys naming the arguments payload, tried in order.
|
||||
/// Most parsers use a single key like `["arguments"]`, but some accept
|
||||
/// multiple (e.g. InternLM2 accepts `parameters` or `arguments`).
|
||||
arguments_key: &'static [&'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,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
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, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match &self.mode {
|
||||
JsonToolCallMode::Text => output.normal_text.push_str(&self.buffer),
|
||||
JsonToolCallMode::Header | JsonToolCallMode::Arguments { .. } => {
|
||||
return Err(parsing_failed!(
|
||||
"incomplete {} tool call",
|
||||
self.config.parser_name
|
||||
));
|
||||
}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
/// Apply one parsed JSON tool-call event to parser state and output.
|
||||
fn apply_event(
|
||||
&mut self,
|
||||
event: JsonToolCallEvent,
|
||||
output: &mut ToolParserOutput,
|
||||
) -> Result<()> {
|
||||
match event {
|
||||
JsonToolCallEvent::Text { len: consumed_len } => {
|
||||
output.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(),
|
||||
};
|
||||
output.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
|
||||
));
|
||||
};
|
||||
output.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(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = JsonToolCallMode::Text;
|
||||
self.active_tool_index = None;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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_arguments_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 a JSON object key accepting any of `candidates`.
|
||||
///
|
||||
/// The full quoted key is consumed and compared against the candidate list,
|
||||
/// so this works correctly under partial input regardless of key lengths.
|
||||
///
|
||||
/// On mismatch, each candidate is attached as its own `Expected` context so the
|
||||
/// error enumerates every valid key ("expected `a`, expected `b`"). Because
|
||||
/// `StrContextValue::StringLiteral` carries a single `&'static str`, the
|
||||
/// contexts are added in a loop over `candidates` rather than through chained
|
||||
/// `.context(...)` calls, which keeps the diagnostics complete for any number
|
||||
/// of candidates.
|
||||
fn json_arguments_key(
|
||||
input: &mut JsonToolInput<'_>,
|
||||
candidates: &'static [&'static str],
|
||||
) -> ModalResult<()> {
|
||||
let start = input.checkpoint();
|
||||
json_str
|
||||
.verify(|key: &String| candidates.contains(&key.as_str()))
|
||||
.void()
|
||||
.parse_next(input)
|
||||
.map_err(|err| {
|
||||
err.map(|context_error| {
|
||||
candidates.iter().fold(context_error, |context_error, candidate| {
|
||||
context_error.add_context(
|
||||
&*input,
|
||||
&start,
|
||||
StrContext::Expected(StrContextValue::StringLiteral(candidate)),
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// 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::tool::ToolParserOutput;
|
||||
|
||||
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]) -> ToolParserOutput {
|
||||
let mut output = ToolParserOutput::default();
|
||||
for chunk in chunks {
|
||||
parser.parse_into(chunk, &mut output).unwrap();
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
output.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 output = collect_chunks(&mut parser, &[&input]);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[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 output = collect_chunks(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[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 output = collect_chunks(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
normal_text: " trailing text",
|
||||
calls: [
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"location\":\"Shanghai\"}",
|
||||
},
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::tool::{Result, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
const PHI4MINI_CONFIG: JsonToolCallConfig = JsonToolCallConfig {
|
||||
parser_name: "Phi4Mini",
|
||||
start_marker: "functools[",
|
||||
end_marker: "]",
|
||||
marker_whitespace: JsonToolCallWhitespace::Optional,
|
||||
delimiter: Some(","),
|
||||
name_key: "name",
|
||||
// Accept both key variants emitted by Phi-4 Mini tool-call templates.
|
||||
arguments_key: &["arguments", "parameters"],
|
||||
};
|
||||
|
||||
/// Tool parser for phi-4-mini models.
|
||||
///
|
||||
/// Example tool-call content:
|
||||
///
|
||||
/// ```text
|
||||
/// functools[{"name": "get_weather", "arguments": {"location": "Tokyo"}}]
|
||||
/// ```
|
||||
///
|
||||
/// phi-4-mini emits an array of tool-call objects wrapped in a `functools[..]`
|
||||
/// envelope. Each object names the function with `name` and carries its
|
||||
/// arguments under `arguments` (preferred) or `parameters`. Arguments are
|
||||
/// already OpenAI-style JSON text, so they are streamed as raw argument deltas
|
||||
/// without schema conversion or JSON normalization.
|
||||
pub struct Phi4MiniJsonToolParser {
|
||||
inner: JsonToolCallParser,
|
||||
}
|
||||
|
||||
impl Phi4MiniJsonToolParser {
|
||||
/// Create a phi-4-mini tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
inner: JsonToolCallParser::new(PHI4MINI_CONFIG),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Phi4MiniJsonToolParser {
|
||||
/// Create a boxed phi-4-mini tool parser.
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
/// Feed one decoded text chunk through the phi-4-mini parser.
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.inner.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
/// Flush any buffered partial state at end of stream.
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.inner.finish()
|
||||
}
|
||||
|
||||
/// Clear parser state and return currently uncommitted buffered text.
|
||||
fn reset(&mut self) -> String {
|
||||
self.inner.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::Phi4MiniJsonToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserTestExt as _};
|
||||
|
||||
/// Build one phi-4-mini tool-call object: `{"name":..,"<args_key>":<args>}`.
|
||||
fn build_call(function_name: &str, args_key: &str, arguments: &str) -> String {
|
||||
format!(r#"{{"name":"{function_name}","{args_key}":{arguments}}}"#)
|
||||
}
|
||||
|
||||
/// Wrap tool-call objects in the `functools[..]` envelope.
|
||||
fn wrap(calls: &[String]) -> String {
|
||||
format!("functools[{}]", calls.join(","))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phi4mini_parse_complete_without_tool_call_keeps_text() {
|
||||
let mut parser = Phi4MiniJsonToolParser::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 phi4mini_parse_complete_extracts_arguments_key() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo","days":"3"}"#;
|
||||
let result = parser
|
||||
.parse_complete(&wrap(&[build_call("get_weather", "arguments", 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 phi4mini_parse_complete_falls_back_to_parameters_key() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo"}"#;
|
||||
let result = parser
|
||||
.parse_complete(&wrap(&[build_call("get_weather", "parameters", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phi4mini_extracts_multiple_comma_delimited_calls() {
|
||||
let input = wrap(&[
|
||||
build_call("get_weather", "arguments", r#"{"location":"Shanghai"}"#),
|
||||
build_call("add", "arguments", r#"{"x":1,"y":2}"#),
|
||||
]);
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
|
||||
let result = parser.parse_complete(&input).unwrap();
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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);
|
||||
}
|
||||
|
||||
/// The shared JSON core scans matched braces, so bracket-bearing argument
|
||||
/// values are forwarded intact.
|
||||
#[test]
|
||||
fn phi4mini_array_valued_arguments_are_not_truncated() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let arguments = r#"{"items":[1,2],"flag":true}"#;
|
||||
let result = parser
|
||||
.parse_complete(&wrap(&[build_call("convert", "arguments", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
/// Preface text before a tool call is preserved as normal_text, consistent
|
||||
/// with the other JSON parsers in this crate.
|
||||
#[test]
|
||||
fn phi4mini_preserves_text_before_tool_call() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let input = format!(
|
||||
"Let me check.\n{}",
|
||||
wrap(&[build_call(
|
||||
"get_weather",
|
||||
"arguments",
|
||||
r#"{"location":"Tokyo"}"#
|
||||
)])
|
||||
);
|
||||
|
||||
let result = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(result.normal_text, "Let me check.\n");
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phi4mini_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo",}"#;
|
||||
let result = parser
|
||||
.parse_complete(&wrap(&[build_call("get_weather", "arguments", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
/// The bundled `tool_chat_template_phi4_mini.jinja` emits objects with
|
||||
/// whitespace after `:` and `,` (e.g. `{"name": "f", "arguments": {..}}`).
|
||||
/// Confirm the parser handles that real model format and preserves the
|
||||
/// inner argument spacing verbatim.
|
||||
#[test]
|
||||
fn phi4mini_accepts_real_model_whitespace_format() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let input = r#"functools[{"name": "get_weather", "arguments": {"location": "Tokyo"}}]"#;
|
||||
|
||||
let result = parser.parse_complete(input).unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(result.calls[0].arguments, r#"{"location": "Tokyo"}"#);
|
||||
}
|
||||
|
||||
/// Argument deltas are streamed through the shared JSON core.
|
||||
#[test]
|
||||
fn phi4mini_streaming_emits_argument_deltas() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let chunks = [
|
||||
"preface functo",
|
||||
"ols[",
|
||||
r#"{"name":"get_weather","arguments":"#,
|
||||
r#"{"location":"#,
|
||||
r#""Beijing""#,
|
||||
r#"}"#,
|
||||
r#"}]"#,
|
||||
" suffix",
|
||||
];
|
||||
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(result.normal_text, "preface suffix");
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(result.calls[0].arguments, r#"{"location":"Beijing"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phi4mini_streaming_handles_split_markers() {
|
||||
let input = format!(
|
||||
"hello {}",
|
||||
wrap(&[build_call(
|
||||
"get_weather",
|
||||
"arguments",
|
||||
r#"{"location":"Tokyo"}"#
|
||||
)])
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = Phi4MiniJsonToolParser::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 phi4mini_finish_errors_on_truncated_tool_call() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let _ = parser
|
||||
.parse_chunk(r#"functools[{"name":"get_weather","arguments":{"location""#)
|
||||
.unwrap();
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
assert!(
|
||||
error.to_report_string().contains("incomplete Phi4Mini tool call"),
|
||||
"finish() reports the truncated tool call as incomplete: {}",
|
||||
error.to_report_string(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phi4mini_preserve_special_tokens_is_false() {
|
||||
let parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
assert!(!parser.preserve_special_tokens());
|
||||
}
|
||||
|
||||
/// The brace-scanning core handles nested arrays and objects in arguments.
|
||||
#[test]
|
||||
fn phi4mini_parses_nested_arrays_and_objects() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let arguments =
|
||||
r#"{"array_field":["a","b","c"],"object_field":{"nested":"value"},"empty_object":{}}"#;
|
||||
let result = parser
|
||||
.parse_complete(&wrap(&[build_call("convert", "arguments", arguments)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.calls.len(), 1);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("convert"));
|
||||
assert_eq!(result.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
/// The chat template emits parallel calls as `},\n {` (comma + newline +
|
||||
/// indent). Confirm the `Optional` marker whitespace and `,` delimiter
|
||||
/// parse the real multi-call layout.
|
||||
#[test]
|
||||
fn phi4mini_parses_parallel_calls_in_template_format() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let input = concat!(
|
||||
"functools[\n",
|
||||
" {\"name\": \"get_weather\", \"arguments\": {\"city\": \"Tokyo\"}},\n",
|
||||
" {\"name\": \"add\", \"arguments\": {\"x\": 1, \"y\": 2}}\n",
|
||||
"]"
|
||||
);
|
||||
|
||||
let result = parser.parse_complete(input).unwrap();
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
/// The shared core requires an object after the start marker.
|
||||
#[test]
|
||||
fn phi4mini_empty_array_errors() {
|
||||
let mut parser = Phi4MiniJsonToolParser::new(&test_tools());
|
||||
let error = parser.parse_complete("functools[]").unwrap_err();
|
||||
|
||||
assert!(
|
||||
error.to_report_string().contains("invalid Phi4Mini"),
|
||||
"empty functools[] should error: {}",
|
||||
error.to_report_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
use super::{JsonToolCallConfig, JsonToolCallParser, JsonToolCallWhitespace};
|
||||
use crate::tool::{Result, StructuralTagModel, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
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 {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::Qwen3)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.inner.parse_into(chunk, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
self.inner.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.inner.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::Qwen3XmlToolParser;
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParser, ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_parse_complete_extracts_raw_json_arguments() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let arguments = r#"{ "location": "Tokyo", "days": "3" }"#;
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"Let me check.\n{}",
|
||||
build_tool_call("get_weather", arguments)
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check.\n");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"Tokyo",}"#;
|
||||
let output = parser.parse_complete(&build_tool_call("get_weather", arguments)).unwrap();
|
||||
|
||||
assert_eq!(output.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 output = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
output.append(next);
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Beijing\"", "}"]);
|
||||
assert_eq!(
|
||||
output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "hello ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].arguments, r#"{"location":"Tokyo"}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_keeps_end_marker_literal_inside_json_string() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let arguments = r#"{"text":"literal </tool_call> inside"}"#;
|
||||
let output = parser.parse_complete(&build_tool_call("echo", arguments)).unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_decodes_escaped_function_name() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
r#"<tool_call>
|
||||
{"name":"say_\"hi","arguments":{}}
|
||||
</tool_call>"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.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 output = parser.parse_complete(input).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, input);
|
||||
assert!(output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_xml_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||
parser
|
||||
.parse_chunk(
|
||||
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
|
||||
.parse_chunk(
|
||||
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,596 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
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, ToolParser, ToolParserOutput};
|
||||
use crate::tool::{StructuralTagModel, 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 {
|
||||
tool_call_id: String,
|
||||
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>,
|
||||
call_ids: BTreeMap<usize, String>,
|
||||
}
|
||||
|
||||
impl KimiK2ToolParser {
|
||||
/// Create a Kimi K2 tool parser.
|
||||
fn new(_tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: KimiK2Mode::Text,
|
||||
active_tool_index: None,
|
||||
call_ids: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed Kimi K2 event to parser state and output.
|
||||
fn apply_event(&mut self, event: KimiK2Event, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
KimiK2Event::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
KimiK2Event::ToolCallsStart => self.mode = KimiK2Mode::ToolBlock,
|
||||
KimiK2Event::ToolCallStart => self.mode = KimiK2Mode::Header,
|
||||
KimiK2Event::ToolCallHeader {
|
||||
tool_call_id,
|
||||
function_name,
|
||||
function_index,
|
||||
} => {
|
||||
let tool_index = function_index;
|
||||
self.active_tool_index = Some(tool_index);
|
||||
self.mode = KimiK2Mode::Arguments {
|
||||
json_scan: JsonObjectScanState::default(),
|
||||
};
|
||||
self.call_ids.insert(tool_index, tool_call_id);
|
||||
output.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"
|
||||
));
|
||||
};
|
||||
output.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(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = KimiK2Mode::Text;
|
||||
self.active_tool_index = None;
|
||||
self.call_ids.clear();
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for KimiK2ToolParser {
|
||||
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 structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::Kimi)
|
||||
}
|
||||
|
||||
fn tool_call_id(&self, tool_index: usize) -> Option<&str> {
|
||||
self.call_ids.get(&tool_index).map(String::as_str)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
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, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match &self.mode {
|
||||
KimiK2Mode::Text => output.normal_text.push_str(&self.buffer),
|
||||
KimiK2Mode::ToolBlock | KimiK2Mode::Done => {}
|
||||
KimiK2Mode::Header | KimiK2Mode::Arguments { .. } => {
|
||||
return Err(parsing_failed!("incomplete Kimi K2 tool call"));
|
||||
}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
KimiK2ToolParser::reset(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 (raw_header, _) = (
|
||||
take_until(1.., TOOL_CALL_ARGUMENT_START),
|
||||
literal(TOOL_CALL_ARGUMENT_START),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
let tool_call_id = raw_header.trim().to_string();
|
||||
let mut header_input = raw_header;
|
||||
let (header, _, _) = (tool_header, ws0, eof).parse_next(&mut header_input)?;
|
||||
|
||||
Ok(KimiK2Event::ToolCallHeader {
|
||||
tool_call_id,
|
||||
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::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_parse_complete_extracts_raw_json_arguments() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
let arguments = r#"{ "location": "NYC", "days": "3" }"#;
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"Checking. {} trailing text",
|
||||
build_tool_section(&[build_tool_call("get_weather", 0, arguments)])
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Checking. ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].arguments, arguments);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_does_not_validate_or_normalize_arguments() {
|
||||
let mut parser = KimiK2ToolParser::new(&test_tools());
|
||||
let arguments = r#"{"location":"NYC",}"#;
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_section(&[build_tool_call(
|
||||
"get_weather",
|
||||
0,
|
||||
arguments,
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.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 output = ToolParserOutput::default();
|
||||
let mut observed_arguments = Vec::new();
|
||||
for chunk in chunks {
|
||||
let next = parser.parse_chunk(chunk).unwrap();
|
||||
observed_arguments.extend(
|
||||
next.calls
|
||||
.iter()
|
||||
.filter(|call| call.name.is_none())
|
||||
.map(|call| call.arguments.clone()),
|
||||
);
|
||||
output.append(next);
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(observed_arguments, ["{\"location\":", "\"Paris\"", "}"]);
|
||||
let output = output.coalesce_calls();
|
||||
assert_eq!(output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text, "hello ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.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 output = parser.parse_complete(&input).unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
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(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kimi_k2_preserves_model_generated_tool_call_ids() {
|
||||
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}"#),
|
||||
]);
|
||||
|
||||
for chunk in split_by_chars(&input, 7) {
|
||||
parser.parse_chunk(chunk).unwrap();
|
||||
}
|
||||
|
||||
// IDs are available after parsing but before finish(), which calls reset().
|
||||
assert_eq!(parser.tool_call_id(0), Some("functions.get_weather:0"));
|
||||
assert_eq!(parser.tool_call_id(1), Some("functions.add:1"));
|
||||
parser.finish().unwrap();
|
||||
assert_eq!(parser.tool_call_id(0), None);
|
||||
}
|
||||
|
||||
#[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 output = parser.parse_chunk(&input).unwrap().coalesce_calls();
|
||||
|
||||
assert_eq!(output.calls[0].tool_index, 42);
|
||||
assert_eq!(parser.tool_call_id(42), Some("api.tools.search:42"));
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("search"));
|
||||
assert_eq!(output.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
|
||||
.parse_chunk(&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.parse_chunk(&input).unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,598 @@
|
||||
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::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::tool::{StructuralTagModel, 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, PartialEq, Eq)]
|
||||
enum MinimaxM2Mode {
|
||||
Text,
|
||||
ToolBlock { invoke_end_scan: MarkerScanState },
|
||||
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, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
MinimaxM2Event::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
MinimaxM2Event::ToolBlockStart => {
|
||||
self.mode = MinimaxM2Mode::ToolBlock {
|
||||
invoke_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
MinimaxM2Event::Invoke { name, raw_params } => {
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
output.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(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = MinimaxM2Mode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for MinimaxM2ToolParser {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::Minimax)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_minimax_m2_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match self.mode {
|
||||
MinimaxM2Mode::Text => {
|
||||
output.normal_text.push_str(&self.buffer);
|
||||
}
|
||||
MinimaxM2Mode::ToolBlock { .. } => {
|
||||
return Err(parsing_failed!("incomplete MiniMax M2 tool call"));
|
||||
}
|
||||
MinimaxM2Mode::Done => {}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
MinimaxM2ToolParser::reset(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M2 event for the current parser mode.
|
||||
fn parse_next_minimax_m2_event(
|
||||
input: &mut MinimaxM2Input<'_>,
|
||||
mode: &mut MinimaxM2Mode,
|
||||
) -> ModalResult<MinimaxM2Event> {
|
||||
match mode {
|
||||
MinimaxM2Mode::Text => parse_text_event(input),
|
||||
MinimaxM2Mode::ToolBlock { invoke_end_scan } => {
|
||||
parse_tool_block_event(input, invoke_end_scan)
|
||||
}
|
||||
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<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<MinimaxM2Event> {
|
||||
alt((tool_block_end_event, |input: &mut MinimaxM2Input<'_>| {
|
||||
invoke_event(input, invoke_end_scan)
|
||||
}))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M2 tool-block end marker.
|
||||
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<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<MinimaxM2Event> {
|
||||
let (name, body) = seq!(
|
||||
_: ws0,
|
||||
_: literal(INVOKE_START),
|
||||
_: (ws1, literal("name=")),
|
||||
partial_attr_value,
|
||||
_: literal(">"),
|
||||
take_until_marker(INVOKE_END, invoke_end_scan),
|
||||
_: literal(INVOKE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
let raw_params = parse_invoke_params(body)?;
|
||||
|
||||
Ok(MinimaxM2Event::Invoke {
|
||||
name: name.trim().to_string(),
|
||||
raw_params,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse all parameter blocks inside a complete MiniMax M2 invoke body.
|
||||
fn parse_invoke_params(invoke_body: &str) -> ModalResult<Vec<(String, String)>> {
|
||||
let mut input = invoke_body;
|
||||
delimited(ws0, repeat(0.., terminated(parameter, ws0)), eof).parse_next(&mut input)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M2 parameter block.
|
||||
fn parameter(input: &mut &str) -> ModalResult<(String, String)> {
|
||||
let (name, value) = seq!(
|
||||
_: literal(PARAMETER_START),
|
||||
_: (ws1, literal("name=")),
|
||||
attr_value,
|
||||
_: literal(">"),
|
||||
take_until(0.., PARAMETER_END),
|
||||
_: literal(PARAMETER_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok((name.trim().to_string(), value.to_string()))
|
||||
}
|
||||
|
||||
/// Parse a quoted or unquoted XML attribute value.
|
||||
fn attr_value<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
alt((
|
||||
delimited(literal("\""), take_until(1.., "\""), literal("\"")),
|
||||
delimited(literal("'"), take_until(1.., "'"), literal("'")),
|
||||
take_until(1.., ">"),
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a quoted or unquoted XML attribute value from partial streaming input.
|
||||
fn partial_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::tool::ToolParserTestExt as _;
|
||||
use crate::tool::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 output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[(
|
||||
"get_weather",
|
||||
vec![("city", "Seattle"), ("days", "5")],
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "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 output = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check. ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_extracts_multiple_invokes() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[
|
||||
("get_weather", vec![("city", "Seattle")]),
|
||||
("get_weather", vec![("city", "NYC")]),
|
||||
]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[1].tool_index, 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[1].arguments).unwrap(),
|
||||
json!({ "city": "NYC" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_converts_schema_types() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output = 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>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": 5.0,
|
||||
"flag": true,
|
||||
"payload": { "nested": true },
|
||||
"items": [1, 2],
|
||||
"empty": "42",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_preserves_raw_entities_in_parameter_value() {
|
||||
// The MiniMax-M2 chat template renders string parameter values RAW (no
|
||||
// XML escaping), so a value the user wants to be the literal text
|
||||
// "Tom & Jerry <3" is emitted verbatim. The parser must preserve
|
||||
// it; xml_unescape currently decodes it, corrupting the bytes.
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[(
|
||||
"get_weather",
|
||||
vec![("city", "Tom & Jerry <3")],
|
||||
)]))
|
||||
.unwrap();
|
||||
let args: Value = serde_json::from_str(&output.calls[0].arguments).unwrap();
|
||||
assert_eq!(args["city"], json!("Tom & Jerry <3"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_parse_complete_preserves_raw_closing_tag_text_in_parameter_value() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output = 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>(&output.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 output = 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>(&output.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 output = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"<minimax:tool_call>",
|
||||
r#"<invoke name="get_weather">"#,
|
||||
r#"<parameter name="city">Seattle</parameter>"#,
|
||||
"</invoke></minimax:tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_preserves_prefix_text() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output = 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!(output.normal_text, "Let me check. ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_without_tool_call_emits_text_incrementally() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output = collect_stream(&mut parser, &["Hello, ", "world!"]);
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert!(output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[1].tool_index, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_handles_template_whitespace_and_split_parameters() {
|
||||
let text = concat!(
|
||||
"I will call the tools.\n",
|
||||
"<minimax:tool_call>\n",
|
||||
"<invoke name=\"get_weather\">\n",
|
||||
"<parameter name=\"city\">Seattle</parameter>\n",
|
||||
"</invoke>\n",
|
||||
"<invoke name=\"get_weather\">\n",
|
||||
"<parameter name=\"city\">NYC</parameter>\n",
|
||||
"</invoke>\n",
|
||||
"</minimax:tool_call>",
|
||||
);
|
||||
let chunks = split_by_chars(text, 7);
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let result = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(result.normal_text, "I will call the tools.\n");
|
||||
assert_eq!(result.calls.len(), 2);
|
||||
assert_eq!(result.calls[0].tool_index, 0);
|
||||
assert_eq!(result.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(result.calls[1].tool_index, 1);
|
||||
assert_eq!(result.calls[1].name.as_deref(), Some("get_weather"));
|
||||
}
|
||||
|
||||
#[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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_streaming_does_not_emit_incomplete_tool_call() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
let output =
|
||||
parser.parse_chunk(r#"<minimax:tool_call><invoke name="get_weather">"#).unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m2_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = MinimaxM2ToolParser::new(&test_tools());
|
||||
parser.parse_chunk(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.parse_chunk("<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.parse_chunk("<minimax:tool_call><bad></minimax:tool_call>").unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,900 @@
|
||||
use winnow::ascii::{multispace0 as ws0, multispace1 as ws1};
|
||||
use winnow::combinator::{alt, delimited, seq};
|
||||
use winnow::error::{ContextError, ErrMode};
|
||||
use winnow::prelude::*;
|
||||
use winnow::stream::Partial;
|
||||
use winnow::token::{literal, rest, take_until};
|
||||
|
||||
use super::parameters::{ParamElement, ParamInput, ToolSchemas};
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::tool::Tool;
|
||||
|
||||
const NAMESPACE: &str = "]<]minimax[>[";
|
||||
const TOOL_CALL_START: &str = "]<]minimax[>[<tool_call>";
|
||||
const TOOL_CALL_END: &str = "]<]minimax[>[</tool_call>";
|
||||
const INVOKE_START: &str = "]<]minimax[>[<invoke";
|
||||
const INVOKE_END: &str = "]<]minimax[>[</invoke>";
|
||||
const ELEMENT_START: &str = "]<]minimax[>[<";
|
||||
const ELEMENT_END_START: &str = "]<]minimax[>[</";
|
||||
const MIXED_TEXT_FIELD: &str = "$text";
|
||||
|
||||
type MinimaxM3Input<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum MinimaxM3Mode {
|
||||
Text,
|
||||
ToolBlock { invoke_end_scan: MarkerScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum MinimaxM3Event {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
ToolBlockStart,
|
||||
Invoke {
|
||||
name: String,
|
||||
params: Vec<(String, ParamInput)>,
|
||||
},
|
||||
ToolBlockEnd,
|
||||
IgnoredRest,
|
||||
}
|
||||
|
||||
/// Tool parser for MiniMax M3 namespace-delimited XML-style tool calls.
|
||||
///
|
||||
/// Example tool call content with recursive parameters:
|
||||
///
|
||||
/// ```text
|
||||
/// ]<]minimax[>[<tool_call>
|
||||
/// ]<]minimax[>[<invoke name="create_order">
|
||||
/// ]<]minimax[>[<user_id>42]<]minimax[>[</user_id>
|
||||
/// ]<]minimax[>[<shipping>
|
||||
/// ]<]minimax[>[<city>Singapore]<]minimax[>[</city>
|
||||
/// ]<]minimax[>[<zip>018956]<]minimax[>[</zip>
|
||||
/// ]<]minimax[>[</shipping>
|
||||
/// ]<]minimax[>[<items>
|
||||
/// ]<]minimax[>[<item>
|
||||
/// ]<]minimax[>[<sku>book-001]<]minimax[>[</sku>
|
||||
/// ]<]minimax[>[<qty>2]<]minimax[>[</qty>
|
||||
/// ]<]minimax[>[</item>
|
||||
/// ]<]minimax[>[</items>
|
||||
/// ]<]minimax[>[</invoke>
|
||||
/// ]<]minimax[>[</tool_call>
|
||||
/// ```
|
||||
///
|
||||
/// With a schema where `shipping` is an object and `items` is an array of
|
||||
/// objects, recursive parameter conversion produces:
|
||||
///
|
||||
/// ```json
|
||||
/// {
|
||||
/// "user_id": 42,
|
||||
/// "shipping": {
|
||||
/// "city": "Singapore",
|
||||
/// "zip": 18956
|
||||
/// },
|
||||
/// "items": [
|
||||
/// {
|
||||
/// "sku": "book-001",
|
||||
/// "qty": 2
|
||||
/// }
|
||||
/// ]
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// MiniMax M3 emits the namespace marker `]<]minimax[>[` before each structural
|
||||
/// tag. Arguments are emitted only after a full `<invoke>` block is parsed.
|
||||
pub struct MinimaxM3ToolParser {
|
||||
buffer: String,
|
||||
mode: MinimaxM3Mode,
|
||||
emitted_tool_count: usize,
|
||||
tool_parameters: ToolSchemas,
|
||||
}
|
||||
|
||||
impl MinimaxM3ToolParser {
|
||||
/// Create a MiniMax M3 tool parser.
|
||||
pub fn new(tools: &[Tool]) -> Self {
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: MinimaxM3Mode::Text,
|
||||
emitted_tool_count: 0,
|
||||
tool_parameters: ToolSchemas::from_tools(tools),
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed MiniMax M3 event to parser state and output.
|
||||
fn apply_event(&mut self, event: MinimaxM3Event, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
MinimaxM3Event::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
MinimaxM3Event::ToolBlockStart => {
|
||||
self.mode = MinimaxM3Mode::ToolBlock {
|
||||
invoke_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
MinimaxM3Event::Invoke { name, params } => {
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
output.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
MinimaxM3Event::ToolBlockEnd => self.mode = MinimaxM3Mode::Done,
|
||||
MinimaxM3Event::IgnoredRest => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for MinimaxM3ToolParser {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_minimax_m3_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match self.mode {
|
||||
MinimaxM3Mode::Text => {
|
||||
output.normal_text.push_str(&self.buffer);
|
||||
}
|
||||
MinimaxM3Mode::ToolBlock { .. } => {
|
||||
if !self.buffer.trim_start().is_empty() {
|
||||
return Err(parsing_failed!("incomplete MiniMax M3 tool call"));
|
||||
}
|
||||
}
|
||||
MinimaxM3Mode::Done => {}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = MinimaxM3Mode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M3 event for the current parser mode.
|
||||
fn parse_next_minimax_m3_event(
|
||||
input: &mut MinimaxM3Input<'_>,
|
||||
mode: &mut MinimaxM3Mode,
|
||||
) -> ModalResult<MinimaxM3Event> {
|
||||
match mode {
|
||||
MinimaxM3Mode::Text => parse_text_event(input),
|
||||
MinimaxM3Mode::ToolBlock { invoke_end_scan } => {
|
||||
parse_tool_block_event(input, invoke_end_scan)
|
||||
}
|
||||
MinimaxM3Mode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode MiniMax M3 event.
|
||||
fn parse_text_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3Event> {
|
||||
alt((tool_block_start_event, safe_text_event)).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M3 tool-block start marker.
|
||||
fn tool_block_start_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3Event> {
|
||||
literal(TOOL_CALL_START).value(MinimaxM3Event::ToolBlockStart).parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next MiniMax M3 marker.
|
||||
fn safe_text_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3Event> {
|
||||
safe_text_len(input, TOOL_CALL_START).map(|len| MinimaxM3Event::Text { len })
|
||||
}
|
||||
|
||||
/// Parse one event inside a MiniMax M3 tool block.
|
||||
fn parse_tool_block_event(
|
||||
input: &mut MinimaxM3Input<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<MinimaxM3Event> {
|
||||
alt((tool_block_end_event, |input: &mut MinimaxM3Input<'_>| {
|
||||
invoke_event(input, invoke_end_scan)
|
||||
}))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M3 tool-block end marker.
|
||||
fn tool_block_end_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3Event> {
|
||||
(ws0, literal(TOOL_CALL_END))
|
||||
.value(MinimaxM3Event::ToolBlockEnd)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a complete MiniMax M3 invoke block.
|
||||
fn invoke_event(
|
||||
input: &mut MinimaxM3Input<'_>,
|
||||
invoke_end_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<MinimaxM3Event> {
|
||||
let (name, body) = seq!(
|
||||
_: ws0,
|
||||
_: literal(INVOKE_START),
|
||||
_: (ws1, literal("name=")),
|
||||
partial_attr_value,
|
||||
_: literal(">"),
|
||||
take_until_marker(INVOKE_END, invoke_end_scan),
|
||||
_: literal(INVOKE_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
let params = parse_invoke_params(body)?;
|
||||
|
||||
Ok(MinimaxM3Event::Invoke {
|
||||
name: name.trim().to_string(),
|
||||
params,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse all parameter elements inside a complete MiniMax M3 invoke body.
|
||||
fn parse_invoke_params(invoke_body: &str) -> ModalResult<Vec<(String, ParamInput)>> {
|
||||
let mut input = invoke_body;
|
||||
let mut elements = Vec::new();
|
||||
|
||||
loop {
|
||||
let _ = ws0.parse_next(&mut input)?;
|
||||
if input.is_empty() {
|
||||
break;
|
||||
}
|
||||
if input.starts_with(ELEMENT_START) {
|
||||
elements.push(parameter_element(&mut input)?);
|
||||
continue;
|
||||
}
|
||||
if input.starts_with(NAMESPACE) {
|
||||
return malformed();
|
||||
}
|
||||
// Be tolerant: ordinary text at an invokeparameter boundary ends this invoke.
|
||||
// Keep parsed parameters and drop the remaining invoke body.
|
||||
break;
|
||||
}
|
||||
|
||||
Ok(elements.into_iter().map(|element| (element.name, element.value)).collect())
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M3 parameter element.
|
||||
fn parameter_element(input: &mut &str) -> ModalResult<ParamElement> {
|
||||
let name = open_element_tag(input)?.to_string();
|
||||
let value = element_body(input, &name)?;
|
||||
close_element_tag(input, &name)?;
|
||||
Ok(ParamElement { name, value })
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M3 opening element tag.
|
||||
fn open_element_tag<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
let name = seq!(
|
||||
_: literal(ELEMENT_START),
|
||||
take_until(1.., ">"),
|
||||
_: literal(">"),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
let name = name.0;
|
||||
if name.starts_with('/') || name.trim().is_empty() {
|
||||
return malformed();
|
||||
}
|
||||
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
/// Parse a MiniMax M3 closing element tag.
|
||||
fn close_element_tag(input: &mut &str, name: &str) -> ModalResult<()> {
|
||||
literal(ELEMENT_END_START).void().parse_next(input)?;
|
||||
literal(name).void().parse_next(input)?;
|
||||
literal(">").void().parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse the body of one MiniMax M3 element.
|
||||
fn element_body(input: &mut &str, closing_name: &str) -> ModalResult<ParamInput> {
|
||||
let close_tag = format!("{ELEMENT_END_START}{closing_name}>");
|
||||
let mut text = String::new();
|
||||
let mut elements = Vec::new();
|
||||
|
||||
loop {
|
||||
text.push_str(text_until_namespace(input)?);
|
||||
|
||||
if input.starts_with(&close_tag) {
|
||||
// Close tag reached, end of element body.
|
||||
break;
|
||||
}
|
||||
if input.starts_with(ELEMENT_START) {
|
||||
// Child element start reached, parse child element recursively.
|
||||
elements.push(parameter_element(input)?);
|
||||
continue;
|
||||
}
|
||||
if input.starts_with(NAMESPACE) {
|
||||
// Unexpected namespace marker.
|
||||
return malformed();
|
||||
}
|
||||
}
|
||||
|
||||
if elements.is_empty() {
|
||||
Ok(ParamInput::Text(text))
|
||||
} else {
|
||||
if !text.trim().is_empty() {
|
||||
push_mixed_text_element(&mut elements, text);
|
||||
}
|
||||
Ok(ParamInput::Elements(elements))
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse text until the next MiniMax M3 namespace marker.
|
||||
fn text_until_namespace<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
|
||||
take_until(0.., NAMESPACE).parse_next(input)
|
||||
}
|
||||
|
||||
/// Preserve mixed text content under a reserved object field.
|
||||
///
|
||||
/// By default, the field name is `$text`, but if that collides with an existing
|
||||
/// child element name, prepend `$` until there is no collision.
|
||||
fn push_mixed_text_element(elements: &mut Vec<ParamElement>, text: String) {
|
||||
let mut name = MIXED_TEXT_FIELD.to_string();
|
||||
while elements.iter().any(|element| element.name == name) {
|
||||
name.insert(0, '$');
|
||||
}
|
||||
elements.push(ParamElement {
|
||||
name,
|
||||
value: ParamInput::Text(text),
|
||||
});
|
||||
}
|
||||
|
||||
/// Parse a quoted or unquoted XML attribute value from partial streaming input.
|
||||
fn partial_attr_value<'i>(input: &mut MinimaxM3Input<'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 M3 tool block ends.
|
||||
fn ignored_rest_event(input: &mut MinimaxM3Input<'_>) -> ModalResult<MinimaxM3Event> {
|
||||
rest.value(MinimaxM3Event::IgnoredRest).parse_next(input)
|
||||
}
|
||||
|
||||
fn malformed<T>() -> ModalResult<T> {
|
||||
Err(ErrMode::Cut(ContextError::new()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::expect;
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::{
|
||||
ELEMENT_END_START, ELEMENT_START, INVOKE_END, INVOKE_START, MinimaxM3ToolParser,
|
||||
TOOL_CALL_END, TOOL_CALL_START, ToolParser,
|
||||
};
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{Tool, ToolParserTestExt as _};
|
||||
|
||||
fn element(name: &str, body: &str) -> String {
|
||||
format!("{ELEMENT_START}{name}>{body}{ELEMENT_END_START}{name}>")
|
||||
}
|
||||
|
||||
fn invoke(function_name: &str, body: &str) -> String {
|
||||
format!("{INVOKE_START} name=\"{function_name}\">{body}{INVOKE_END}")
|
||||
}
|
||||
|
||||
fn build_tool_block(invokes: &[(&str, String)]) -> String {
|
||||
let invokes = invokes
|
||||
.iter()
|
||||
.map(|(function_name, body)| invoke(function_name, body))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
format!("{TOOL_CALL_START}\n{invokes}\n{TOOL_CALL_END}")
|
||||
}
|
||||
|
||||
fn m3_test_tools() -> Vec<Tool> {
|
||||
let mut tools = test_tools();
|
||||
tools.push(Tool {
|
||||
name: "create_order".to_string(),
|
||||
description: None,
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_id": { "type": "integer" },
|
||||
"urgent": { "type": "boolean" },
|
||||
"note": { "type": "string" },
|
||||
"shipping": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": { "type": "string" },
|
||||
"zip": { "type": "integer" }
|
||||
}
|
||||
},
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sku": { "type": "string" },
|
||||
"qty": { "type": "integer" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"metadata": {
|
||||
"type": "object",
|
||||
"additionalProperties": { "type": "integer" }
|
||||
},
|
||||
"duplicate_demo": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"tag": { "type": "string" }
|
||||
}
|
||||
},
|
||||
"schema_mismatch_array": {
|
||||
"type": "array",
|
||||
"items": { "type": "integer" }
|
||||
}
|
||||
}
|
||||
}),
|
||||
strict: None,
|
||||
});
|
||||
tools
|
||||
}
|
||||
|
||||
fn order_arguments() -> String {
|
||||
let shipping = element(
|
||||
"shipping",
|
||||
&format!(
|
||||
"{}{}",
|
||||
element("city", "Singapore"),
|
||||
element("zip", "018956")
|
||||
),
|
||||
);
|
||||
let first_item = element(
|
||||
"item",
|
||||
&format!("{}{}", element("sku", "book-001"), element("qty", "2")),
|
||||
);
|
||||
let second_item = element(
|
||||
"item",
|
||||
&format!("{}{}", element("sku", "pen-007"), element("qty", "5")),
|
||||
);
|
||||
let items = element("items", &format!("{first_item}{second_item}"));
|
||||
let metadata = element(
|
||||
"metadata",
|
||||
&format!("{}{}", element("score", "42"), element("rank", "7")),
|
||||
);
|
||||
let duplicate_demo = element(
|
||||
"duplicate_demo",
|
||||
&format!("{}{}", element("tag", "a"), element("tag", "b")),
|
||||
);
|
||||
let schema_mismatch_array = element(
|
||||
"schema_mismatch_array",
|
||||
&format!("{}{}", element("x", "1"), element("x", "2")),
|
||||
);
|
||||
|
||||
[
|
||||
element("user_id", "42"),
|
||||
element("urgent", "true"),
|
||||
element("note", "Please leave at front desk."),
|
||||
shipping,
|
||||
items,
|
||||
metadata,
|
||||
duplicate_demo,
|
||||
schema_mismatch_array,
|
||||
element(
|
||||
"unknown_struct",
|
||||
&format!("{}{}", element("a", "1"), element("a", "2")),
|
||||
),
|
||||
]
|
||||
.join("")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_parse_complete_without_tool_call_keeps_text() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[(
|
||||
"get_weather",
|
||||
format!("{}{}", element("city", "Seattle"), element("days", "5")),
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle", "days": 5 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_parse_complete_preserves_prefix_and_ignores_trailing_text() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = format!(
|
||||
"Let me check. {} This trailing text is ignored.",
|
||||
build_tool_block(&[("get_weather", element("city", "Seattle"))])
|
||||
);
|
||||
let output = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check. ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_parse_complete_extracts_multiple_invokes() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[
|
||||
("get_weather", element("city", "Seattle")),
|
||||
("get_weather", element("city", "NYC")),
|
||||
]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[1].tool_index, 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[1].arguments).unwrap(),
|
||||
json!({ "city": "NYC" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_invoke_body_junk_drops_rest_of_invoke() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[(
|
||||
"get_weather",
|
||||
[
|
||||
element("city", "Seattle"),
|
||||
"I need to use the city above.".to_string(),
|
||||
element("days", "5"),
|
||||
]
|
||||
.join(""),
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_parse_complete_converts_schema_types() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[(
|
||||
"convert",
|
||||
[
|
||||
element("whole", "5.0"),
|
||||
element("flag", "true"),
|
||||
element("payload", r#"{"nested":true}"#),
|
||||
element("items", "[1,2]"),
|
||||
element("empty", "42"),
|
||||
]
|
||||
.join(""),
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"whole": 5.0,
|
||||
"flag": true,
|
||||
"payload": { "nested": true },
|
||||
"items": [1, 2],
|
||||
"empty": "42",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_parse_complete_converts_nested_arguments() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[("create_order", order_arguments())]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"user_id": 42,
|
||||
"urgent": true,
|
||||
"note": "Please leave at front desk.",
|
||||
"shipping": {
|
||||
"city": "Singapore",
|
||||
"zip": 18956
|
||||
},
|
||||
"items": [
|
||||
{
|
||||
"sku": "book-001",
|
||||
"qty": 2
|
||||
},
|
||||
{
|
||||
"sku": "pen-007",
|
||||
"qty": 5
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"score": 42,
|
||||
"rank": 7
|
||||
},
|
||||
"duplicate_demo": {
|
||||
"tag": ["a", "b"]
|
||||
},
|
||||
"schema_mismatch_array": [1, 2],
|
||||
"unknown_struct": {
|
||||
"a": ["1", "2"]
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_parse_complete_handles_multiline_leaf_parameters() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_block(&[(
|
||||
"calculate_area",
|
||||
[
|
||||
element("shape", "\nrectangle\n"),
|
||||
element("dimensions", r#"{"width":10,"height":20}"#),
|
||||
element("precision", "2"),
|
||||
]
|
||||
.join(""),
|
||||
)]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"shape": "\nrectangle\n",
|
||||
"dimensions": { "width": 10, "height": 20 },
|
||||
"precision": 2,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_streaming_extracts_single_tool_call() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
TOOL_CALL_START,
|
||||
&invoke("get_weather", &element("city", "Seattle")),
|
||||
TOOL_CALL_END,
|
||||
],
|
||||
);
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_streaming_preserves_prefix_text() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"Let me check. ",
|
||||
TOOL_CALL_START,
|
||||
&invoke("get_weather", &element("city", "Seattle")),
|
||||
TOOL_CALL_END,
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(output.normal_text, "Let me check. ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_streaming_without_tool_call_emits_text_incrementally() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = collect_stream(&mut parser, &["Hello, ", "world!"]);
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_streaming_handles_marker_split_across_chunks() {
|
||||
let text = build_tool_block(&[("get_weather", element("city", "Seattle"))]);
|
||||
let chunks = split_by_chars(&text, 3);
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert!(output.normal_text.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_streaming_extracts_multiple_invokes_in_order() {
|
||||
let text = build_tool_block(&[
|
||||
("get_weather", element("city", "Seattle")),
|
||||
("get_weather", element("city", "NYC")),
|
||||
]);
|
||||
let chunks = split_by_chars(&text, 7);
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[1].tool_index, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_streaming_does_not_emit_incomplete_tool_call() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser
|
||||
.parse_chunk(&format!(
|
||||
"{TOOL_CALL_START}{INVOKE_START} name=\"get_weather\">"
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_streaming_ignores_text_after_tool_block() {
|
||||
let text = format!(
|
||||
"{} ignored",
|
||||
build_tool_block(&[("get_weather", element("city", "Seattle"))])
|
||||
);
|
||||
let chunks = split_by_chars(&text, 5);
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
parser
|
||||
.parse_chunk(&format!(
|
||||
"{TOOL_CALL_START}{INVOKE_START} name=\"get_weather\">"
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert!(parser.finish().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_finish_recovers_after_bare_tool_block_start() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
parser.parse_chunk(TOOL_CALL_START).unwrap();
|
||||
|
||||
let output = parser.finish().unwrap();
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_finish_recovers_completed_invoke_with_whitespace_tail() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"{}\n{}\n \n",
|
||||
TOOL_CALL_START,
|
||||
invoke("get_weather", &element("city", "Seattle"))
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Seattle" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_finish_fails_partial_outer_end_marker() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
parser
|
||||
.parse_chunk(&format!(
|
||||
"{}\n{}\n{}",
|
||||
TOOL_CALL_START,
|
||||
invoke("get_weather", &element("city", "Seattle")),
|
||||
&TOOL_CALL_END[..3]
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert!(parser.finish().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_malformed_tool_call_fails_fast() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let error = parser
|
||||
.parse_chunk(&format!(
|
||||
"{TOOL_CALL_START}{ELEMENT_START}bad>{TOOL_CALL_END}"
|
||||
))
|
||||
.unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: "].assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_mixed_content_is_preserved_as_text_field() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let body = element(
|
||||
"payload",
|
||||
&format!("text before {} text after", element("child", "value")),
|
||||
);
|
||||
let output = parser.parse_complete(&build_tool_block(&[("convert", body)])).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"payload": {
|
||||
"child": "value",
|
||||
"$text": "text before text after"
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimax_m3_mixed_text_field_avoids_child_name_collision() {
|
||||
let mut parser = MinimaxM3ToolParser::new(&m3_test_tools());
|
||||
let body = element(
|
||||
"payload",
|
||||
&format!(
|
||||
"text{}{}",
|
||||
element("$text", "child text"),
|
||||
element("child", "value")
|
||||
),
|
||||
);
|
||||
let output = parser.parse_complete(&build_tool_block(&[("convert", body)])).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"payload": {
|
||||
"$text": "child text",
|
||||
"$$text": "text",
|
||||
"child": "value"
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
//! Streaming tool parsers for chat completions.
|
||||
|
||||
#[macro_use]
|
||||
pub(crate) mod error;
|
||||
mod deepseek_dsml;
|
||||
pub(crate) mod deepseek_json;
|
||||
mod gemma4;
|
||||
mod glm_xml;
|
||||
mod hy_v3;
|
||||
mod json;
|
||||
mod kimi_k2;
|
||||
mod minimax_m2;
|
||||
mod minimax_m3;
|
||||
mod parameters;
|
||||
mod qwen_coder;
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub mod test_utils;
|
||||
pub(crate) 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 hy_v3::HyV3ToolParser;
|
||||
pub use json::{
|
||||
Granite4ToolParser, HermesToolParser, Internlm2ToolParser, Llama3JsonToolParser,
|
||||
MistralToolParser, Phi4MiniJsonToolParser, Qwen3XmlToolParser,
|
||||
};
|
||||
pub use kimi_k2::KimiK2ToolParser;
|
||||
pub use minimax_m2::MinimaxM2ToolParser;
|
||||
pub use minimax_m3::MinimaxM3ToolParser;
|
||||
pub use qwen_coder::Qwen3CoderToolParser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
pub use xgrammar_structural_tag::Model as StructuralTagModel;
|
||||
|
||||
/// 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 ToolParserOutput {
|
||||
/// Plain assistant text that is not part of any tool call.
|
||||
pub normal_text: String,
|
||||
/// Tool-call updates extracted from this input.
|
||||
pub calls: Vec<ToolCallDelta>,
|
||||
}
|
||||
|
||||
impl ToolParserOutput {
|
||||
/// Append another parser output onto this one.
|
||||
///
|
||||
/// Note that this does not attempt to merge multiple deltas for the same
|
||||
/// tool call into one complete item. Call `coalesce_calls()` after if
|
||||
/// that behavior is desired.
|
||||
pub 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 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
|
||||
}
|
||||
|
||||
/// Return the xgrammar structural-tag model used for strict tool calling.
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Return the parser-provided ID for a tool call by index, if the model
|
||||
/// emitted one.
|
||||
fn tool_call_id(&self, _tool_index: usize) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Feed one decoded text delta into the parser, appending committed output
|
||||
/// into `output`.
|
||||
///
|
||||
/// If this returns an error, any output already appended to `output`
|
||||
/// remains committed parser output. The parser must keep its uncommitted
|
||||
/// buffer intact so callers may recover it with `reset()`.
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()>;
|
||||
|
||||
/// Flush any buffered partial state at end of stream.
|
||||
///
|
||||
/// This operation is atomic: on error no partial output is returned and the
|
||||
/// parser's buffered state is left intact.
|
||||
fn finish(&mut self) -> Result<ToolParserOutput>;
|
||||
|
||||
/// Clear parser state and return currently uncommitted buffered text.
|
||||
///
|
||||
/// Callers may use this to recover any text that failed to parse after an error
|
||||
/// and output it as normal text.
|
||||
fn reset(&mut self) -> String;
|
||||
}
|
||||
|
||||
/// Extension methods for easily testing `ToolParser` implementations.
|
||||
///
|
||||
/// These helpers do not handle partial parsing or error recovery, so they are
|
||||
/// not intended for use in production code paths.
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
#[easy_ext::ext(ToolParserTestExt)]
|
||||
impl<T: ToolParser + ?Sized> T {
|
||||
/// Feed one decoded text delta and return only if the whole chunk parses.
|
||||
///
|
||||
/// If parsing fails, partial committed output is discarded by this helper.
|
||||
/// Prefer `parse_into` for more fine-grained control in error recovery.
|
||||
pub fn parse_chunk(&mut self, chunk: &str) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
self.parse_into(chunk, &mut output)?;
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
/// Parse complete tool calls from final output.
|
||||
///
|
||||
/// This default implementation reuses the incremental parser lifecycle by
|
||||
/// feeding the full output through `parse_chunk()` and then calling `finish()`.
|
||||
///
|
||||
/// If parsing fails, partial committed output is discarded by this helper.
|
||||
/// Prefer `parse_into` for more fine-grained control in error recovery.
|
||||
pub fn parse_complete(&mut self, text: &str) -> Result<ToolParserOutput> {
|
||||
let mut output = self.parse_chunk(text)?;
|
||||
output.append(self.finish()?);
|
||||
Ok(output.coalesce_calls())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,883 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde_json::{Map, Number, Value};
|
||||
|
||||
use crate::tool::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>,
|
||||
}
|
||||
|
||||
/// Parameter input for schema-aware conversion.
|
||||
///
|
||||
/// It can be either a raw text string, or a structured input with named child elements.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(super) enum ParamInput {
|
||||
Text(String),
|
||||
#[allow(dead_code)]
|
||||
Elements(Vec<ParamElement>),
|
||||
}
|
||||
|
||||
impl From<String> for ParamInput {
|
||||
fn from(value: String) -> Self {
|
||||
Self::Text(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// One named structured parameter child.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(super) struct ParamElement {
|
||||
pub name: String,
|
||||
pub value: ParamInput,
|
||||
}
|
||||
|
||||
/// Normalized JSON parameter type used for raw string coercion.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(super) enum JsonParamType {
|
||||
String,
|
||||
Integer,
|
||||
Number,
|
||||
Boolean,
|
||||
Object {
|
||||
properties: BTreeMap<String, JsonParamType>,
|
||||
additional_properties: Option<Box<JsonParamType>>,
|
||||
},
|
||||
Array {
|
||||
items: Option<Box<JsonParamType>>,
|
||||
},
|
||||
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 parameter values for one named tool.
|
||||
///
|
||||
/// Unknown tool names use an empty schema, so all parameters fall back to
|
||||
/// strings or object-like JSON for structured inputs.
|
||||
pub(super) fn convert_params_with_schema<P>(
|
||||
&self,
|
||||
function_name: &str,
|
||||
params: Vec<(String, P)>,
|
||||
) -> Map<String, Value>
|
||||
where
|
||||
P: Into<ParamInput>,
|
||||
{
|
||||
let tool_schema = self.tools.get(function_name).unwrap_or(ToolSchema::empty());
|
||||
let mut converted = Map::with_capacity(params.len());
|
||||
for (name, value) in params {
|
||||
let value = tool_schema.convert(&name, value.into());
|
||||
converted.insert(name, value);
|
||||
}
|
||||
converted
|
||||
}
|
||||
|
||||
/// Convert one parameter value for one named tool.
|
||||
pub(super) fn convert_param_with_schema<P>(
|
||||
&self,
|
||||
function_name: &str,
|
||||
name: &str,
|
||||
value: P,
|
||||
) -> Value
|
||||
where
|
||||
P: Into<ParamInput>,
|
||||
{
|
||||
let tool_schema = self.tools.get(function_name).unwrap_or(ToolSchema::empty());
|
||||
tool_schema.convert(name, value.into())
|
||||
}
|
||||
}
|
||||
|
||||
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 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, or object-like JSON for structured input.
|
||||
fn convert(&self, name: &str, input: ParamInput) -> Value {
|
||||
convert_with_optional_schema(self.params.get(name), &input)
|
||||
}
|
||||
}
|
||||
|
||||
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, schema);
|
||||
}
|
||||
|
||||
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_else(|| Self::object_from_schema(Some(schema)));
|
||||
return Some(param_type);
|
||||
}
|
||||
|
||||
// Typically, these types are already handled by checking the "type" field, but
|
||||
// we can also infer them from their characteristic fields if "type" is missing.
|
||||
if schema.contains_key("enum") {
|
||||
return Some(Self::String);
|
||||
}
|
||||
if schema.contains_key("items") {
|
||||
return Some(Self::array_from_schema(Some(schema)));
|
||||
}
|
||||
if schema.contains_key("properties") || schema.contains_key("additionalProperties") {
|
||||
return Some(Self::object_from_schema(Some(schema)));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Normalize a JSON schema `type` value.
|
||||
fn from_type_value(type_value: &Value, schema: &Map<String, Value>) -> Option<Self> {
|
||||
match type_value {
|
||||
Value::String(kind) => Self::from_type_name(kind, Some(schema)),
|
||||
Value::Array(kinds) => {
|
||||
let types = kinds
|
||||
.iter()
|
||||
.filter_map(Value::as_str)
|
||||
.filter_map(|kind| Self::from_type_name(kind, Some(schema)))
|
||||
.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, schema: Option<&Map<String, Value>>) -> 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" | "double" => Some(Self::Number),
|
||||
"boolean" | "bool" | "binary" => Some(Self::Boolean),
|
||||
"object" | "dict" | "map" => Some(Self::object_from_schema(schema)),
|
||||
"array" | "arr" | "list" | "sequence" => Some(Self::array_from_schema(schema)),
|
||||
"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_from_schema(schema)),
|
||||
_ if kind.starts_with("list") => Some(Self::array_from_schema(schema)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalize object schema fields.
|
||||
fn object_from_schema(schema: Option<&Map<String, Value>>) -> Self {
|
||||
let properties = schema
|
||||
.and_then(|schema| schema.get("properties"))
|
||||
.and_then(Value::as_object)
|
||||
.map(|properties| {
|
||||
properties
|
||||
.iter()
|
||||
.filter_map(|(name, schema)| {
|
||||
Self::from_schema(schema).map(|param_type| (name.clone(), param_type))
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let additional_properties =
|
||||
schema.and_then(|schema| schema.get("additionalProperties")).and_then(|schema| {
|
||||
if schema.is_object() {
|
||||
Self::from_schema(schema).map(Box::new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
Self::Object {
|
||||
properties,
|
||||
additional_properties,
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalize array schema fields.
|
||||
fn array_from_schema(schema: Option<&Map<String, Value>>) -> Self {
|
||||
let items = schema
|
||||
.and_then(|schema| schema.get("items"))
|
||||
.and_then(Self::from_schema)
|
||||
.map(Box::new);
|
||||
|
||||
Self::Array { items }
|
||||
}
|
||||
|
||||
/// 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 parameter input to a normalized JSON value.
|
||||
fn convert_with_optional_schema(param_type: Option<&JsonParamType>, input: &ParamInput) -> Value {
|
||||
// For literal `null`, always convert to JSON null value.
|
||||
if let ParamInput::Text(value) = input
|
||||
&& value.eq_ignore_ascii_case("null")
|
||||
{
|
||||
return Value::Null;
|
||||
}
|
||||
|
||||
// If we have a schema, try to convert the value using it.
|
||||
if let Some(param_type) = param_type
|
||||
&& let Some(value) = try_convert_value(param_type, input)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
// We don't have a schema, or conversion failed, use fallback logic.
|
||||
match input {
|
||||
ParamInput::Text(value) => Value::String(value.clone()),
|
||||
ParamInput::Elements(elements) => {
|
||||
// Convert structured input to object without a schema.
|
||||
Value::Object(convert_elements_to_object(elements, &BTreeMap::new(), None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert one parameter input to a normalized JSON type.
|
||||
fn try_convert_value(param_type: &JsonParamType, input: &ParamInput) -> Option<Value> {
|
||||
match input {
|
||||
ParamInput::Text(value) => try_convert_text_value(param_type, value),
|
||||
ParamInput::Elements(elements) => try_convert_elements_value(param_type, elements),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert one raw string value to a normalized JSON type.
|
||||
fn try_convert_text_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 => try_convert_number(value),
|
||||
JsonParamType::Boolean => try_convert_boolean(value),
|
||||
JsonParamType::Object { .. } if value.is_empty() => Some(Value::Object(Map::new())),
|
||||
JsonParamType::Array { .. } if value.is_empty() => Some(Value::Array(Vec::new())),
|
||||
JsonParamType::Object { .. } | JsonParamType::Array { .. } => {
|
||||
// For composite types with string input, simply interpret the string as JSON.
|
||||
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| try_convert_text_value(param_type, value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert one structured parameter input to a normalized JSON type.
|
||||
fn try_convert_elements_value(
|
||||
param_type: &JsonParamType,
|
||||
elements: &[ParamElement],
|
||||
) -> Option<Value> {
|
||||
match param_type {
|
||||
JsonParamType::Object {
|
||||
properties,
|
||||
additional_properties,
|
||||
} => Some(Value::Object(convert_elements_to_object(
|
||||
elements,
|
||||
properties,
|
||||
additional_properties.as_deref(),
|
||||
))),
|
||||
JsonParamType::Array { items } => Some(Value::Array(
|
||||
// Collect all child elements into an array, regardless of their names.
|
||||
elements
|
||||
.iter()
|
||||
.map(|element| convert_with_optional_schema(items.as_deref(), &element.value))
|
||||
.collect(),
|
||||
)),
|
||||
JsonParamType::OneOf(types) => types
|
||||
.iter()
|
||||
.find_map(|param_type| try_convert_elements_value(param_type, elements)),
|
||||
|
||||
// Primitive types can't be converted from structured input.
|
||||
JsonParamType::String
|
||||
| JsonParamType::Integer
|
||||
| JsonParamType::Number
|
||||
| JsonParamType::Boolean
|
||||
| JsonParamType::Null => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert structured elements to an object, using field schemas when present.
|
||||
fn convert_elements_to_object(
|
||||
elements: &[ParamElement],
|
||||
properties: &BTreeMap<String, JsonParamType>,
|
||||
additional_properties: Option<&JsonParamType>,
|
||||
) -> Map<String, Value> {
|
||||
let mut object = Map::with_capacity(elements.len());
|
||||
for element in elements {
|
||||
let param_type = properties.get(&element.name).or(additional_properties);
|
||||
let value = convert_with_optional_schema(param_type, &element.value);
|
||||
insert_object_value(&mut object, element.name.clone(), value);
|
||||
}
|
||||
object
|
||||
}
|
||||
|
||||
/// Insert an object field while preserving duplicate keys as arrays.
|
||||
fn insert_object_value(object: &mut Map<String, Value>, key: String, value: Value) {
|
||||
if let Some(existing) = object.get_mut(&key) {
|
||||
match existing {
|
||||
// Collect values under the same key into an array.
|
||||
Value::Array(values) => values.push(value),
|
||||
existing => {
|
||||
let first = std::mem::replace(existing, Value::Null);
|
||||
*existing = Value::Array(vec![first, value]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
object.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert one raw string value to a JSON number.
|
||||
fn try_convert_number(value: &str) -> Option<Value> {
|
||||
serde_json::from_str::<Number>(value)
|
||||
.or_else(|_| value.parse::<i64>().map(Number::from))
|
||||
.or_else(|_| value.parse::<f64>().ok().and_then(Number::from_f64).ok_or(()))
|
||||
.ok()
|
||||
.map(Value::Number)
|
||||
}
|
||||
|
||||
/// Convert one raw string value to a boolean.
|
||||
fn try_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::{Value, json};
|
||||
|
||||
use super::{ParamElement, ParamInput, ToolSchema, ToolSchemas};
|
||||
use crate::tool::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", text("42")), json!("42"));
|
||||
assert_eq!(params.convert("count", text("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", text("42")), json!("42"));
|
||||
assert_eq!(params.convert("unknown_type", text("42")), json!("42"));
|
||||
assert_eq!(params.convert("known", text("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" },
|
||||
"ratio": { "type": "double" },
|
||||
"enabled": { "type": "boolean" },
|
||||
"payload": { "type": "object" },
|
||||
"mapping": { "type": "map" },
|
||||
"items": { "type": "array" },
|
||||
"names": { "type": "list" },
|
||||
"nothing": { "type": "null" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(params.convert("text", text("42")), json!("42"));
|
||||
assert_eq!(params.convert("count", text("42")), json!(42));
|
||||
assert_eq!(params.convert("size", text("5.0")), json!(5.0));
|
||||
assert_eq!(params.convert("ratio", text("2.5")), json!(2.5));
|
||||
assert_eq!(params.convert("enabled", text("1")), json!(true));
|
||||
assert_eq!(
|
||||
params.convert("payload", text(r#"{"k":1}"#)),
|
||||
json!({ "k": 1 })
|
||||
);
|
||||
assert_eq!(
|
||||
params.convert("mapping", text(r#"{"k":1}"#)),
|
||||
json!({ "k": 1 })
|
||||
);
|
||||
assert_eq!(params.convert("items", text("[1,2]")), json!([1, 2]));
|
||||
assert_eq!(
|
||||
params.convert("names", text(r#"["a","b"]"#)),
|
||||
json!(["a", "b"])
|
||||
);
|
||||
assert_eq!(params.convert("nothing", text("null")), json!(null));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn number_conversion_preserves_json_number_spelling_with_legacy_fallback() {
|
||||
let params = ToolSchema::from_schema(&json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"value": { "type": "number" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(converted_number_text(¶ms, "5"), "5");
|
||||
assert_eq!(converted_number_text(¶ms, "5.0"), "5.0");
|
||||
assert_eq!(converted_number_text(¶ms, "5."), "5.0");
|
||||
assert_eq!(converted_number_text(¶ms, "+1"), "1");
|
||||
assert_eq!(converted_number_text(¶ms, "+1.0"), "1.0");
|
||||
|
||||
// TODO: we cannot preserve the original number precision by enabling `serde_json`'s
|
||||
// `arbitrary_precision` feature, otherwise the test
|
||||
// `serialized_json_numbers_do_not_leak_serde_private_representation` will fail.
|
||||
// See issue: https://github.com/mitsuhiko/minijinja/issues/641
|
||||
|
||||
// assert_eq!(converted_number_text(¶ms, "5.00"), "5.00");
|
||||
// assert_eq!(converted_number_text(¶ms, "1e0"), "1e+0");
|
||||
// assert_eq!(
|
||||
// converted_number_text(¶ms, "9223372036854775807.5"),
|
||||
// "9223372036854775807.5"
|
||||
// );
|
||||
}
|
||||
|
||||
fn converted_number_text(params: &ToolSchema, value: &str) -> String {
|
||||
serde_json::to_string(¶ms.convert("value", text(value))).unwrap()
|
||||
}
|
||||
|
||||
fn text(value: &str) -> ParamInput {
|
||||
ParamInput::Text(value.to_string())
|
||||
}
|
||||
|
||||
fn elem(name: &str, value: ParamInput) -> ParamElement {
|
||||
ParamElement {
|
||||
name: name.to_string(),
|
||||
value,
|
||||
}
|
||||
}
|
||||
|
||||
fn elements(elements: Vec<ParamElement>) -> ParamInput {
|
||||
ParamInput::Elements(elements)
|
||||
}
|
||||
|
||||
#[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", text("x")), json!("x"));
|
||||
assert_eq!(params.convert("i", text("7")), json!(7));
|
||||
assert_eq!(params.convert("n", text("7.5")), json!(7.5));
|
||||
assert_eq!(params.convert("b", text("true")), json!(true));
|
||||
assert_eq!(params.convert("a", text("[1]")), json!([1]));
|
||||
assert_eq!(params.convert("o", text(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", text("42")), json!(42));
|
||||
assert_eq!(string_first.convert("value", text("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", text("42")), json!(42));
|
||||
assert_eq!(
|
||||
params.convert("fallback_object", text(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", text("a")), json!("a"));
|
||||
assert_eq!(params.convert("items", text("[1,2]")), json!([1, 2]));
|
||||
assert_eq!(
|
||||
params.convert("payload", text(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)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_structured_inputs_with_recursive_schema() {
|
||||
let schemas = ToolSchemas::from_tools(&[test_tool(
|
||||
"create_order",
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_id": { "type": "integer" },
|
||||
"urgent": { "type": "boolean" },
|
||||
"note": { "type": "string" },
|
||||
"nil": { "type": "string" },
|
||||
"shipping": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": { "type": "string" },
|
||||
"zip": { "type": "integer" }
|
||||
}
|
||||
},
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sku": { "type": "string" },
|
||||
"qty": { "type": "integer" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"metadata": {
|
||||
"type": "object",
|
||||
"additionalProperties": { "type": "integer" }
|
||||
},
|
||||
"duplicate_demo": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"tag": { "type": "string" }
|
||||
}
|
||||
},
|
||||
"schema_mismatch_array": {
|
||||
"type": "array",
|
||||
"items": { "type": "integer" }
|
||||
},
|
||||
"closed_object": {
|
||||
"type": "object",
|
||||
"additionalProperties": false
|
||||
},
|
||||
"open_object": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
},
|
||||
"payload_text": { "type": "object" },
|
||||
"items_text": { "type": "array" }
|
||||
}
|
||||
}),
|
||||
)]);
|
||||
|
||||
let converted = schemas.convert_params_with_schema(
|
||||
"create_order",
|
||||
vec![
|
||||
("user_id".to_string(), text("42")),
|
||||
("urgent".to_string(), text("true")),
|
||||
("note".to_string(), text("Please leave at front desk.")),
|
||||
("nil".to_string(), text("NULL")),
|
||||
(
|
||||
"shipping".to_string(),
|
||||
elements(vec![
|
||||
elem("city", text("Singapore")),
|
||||
elem("zip", text("018956")),
|
||||
]),
|
||||
),
|
||||
(
|
||||
"items".to_string(),
|
||||
elements(vec![
|
||||
elem(
|
||||
"item1",
|
||||
elements(vec![elem("sku", text("book-001")), elem("qty", text("2"))]),
|
||||
),
|
||||
elem(
|
||||
"item2",
|
||||
elements(vec![elem("sku", text("pen-007")), elem("qty", text("5"))]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
(
|
||||
"metadata".to_string(),
|
||||
elements(vec![elem("score", text("42")), elem("rank", text("7"))]),
|
||||
),
|
||||
(
|
||||
"duplicate_demo".to_string(),
|
||||
elements(vec![elem("tag", text("a")), elem("tag", text("b"))]),
|
||||
),
|
||||
(
|
||||
"closed_object".to_string(),
|
||||
elements(vec![elem("unknown", text("x"))]),
|
||||
),
|
||||
(
|
||||
"open_object".to_string(),
|
||||
elements(vec![elem("unknown", text("y"))]),
|
||||
),
|
||||
("payload_text".to_string(), text(r#"{"x":1}"#)),
|
||||
("items_text".to_string(), text("[1,2]")),
|
||||
(
|
||||
"unknown_struct".to_string(),
|
||||
elements(vec![
|
||||
elem("a", text("1")),
|
||||
elem("a", text("2")),
|
||||
elem("nil", text("null")),
|
||||
]),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Value::Object(converted),
|
||||
json!({
|
||||
"user_id": 42,
|
||||
"urgent": true,
|
||||
"note": "Please leave at front desk.",
|
||||
"nil": null,
|
||||
"shipping": {
|
||||
"city": "Singapore",
|
||||
"zip": 18956
|
||||
},
|
||||
"items": [
|
||||
{
|
||||
"sku": "book-001",
|
||||
"qty": 2
|
||||
},
|
||||
{
|
||||
"sku": "pen-007",
|
||||
"qty": 5
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"score": 42,
|
||||
"rank": 7
|
||||
},
|
||||
"duplicate_demo": {
|
||||
"tag": ["a", "b"]
|
||||
},
|
||||
"closed_object": {
|
||||
"unknown": "x"
|
||||
},
|
||||
"open_object": {
|
||||
"unknown": "y"
|
||||
},
|
||||
"payload_text": {
|
||||
"x": 1
|
||||
},
|
||||
"items_text": [1, 2],
|
||||
"unknown_struct": {
|
||||
"a": ["1", "2"],
|
||||
"nil": null
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,717 @@
|
||||
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::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, StructuralTagModel, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::tool::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, PartialEq, Eq)]
|
||||
enum QwenCoderMode {
|
||||
Text,
|
||||
ToolCall { end_marker_scan: MarkerScanState },
|
||||
}
|
||||
|
||||
#[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, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
QwenCoderEvent::Text { len: consumed_len } => {
|
||||
output.normal_text.push_str(&self.buffer[..consumed_len]);
|
||||
}
|
||||
QwenCoderEvent::ToolCallStart => {
|
||||
self.mode = QwenCoderMode::ToolCall {
|
||||
end_marker_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
QwenCoderEvent::ToolCall { name, raw_params } => {
|
||||
self.mode = QwenCoderMode::Text;
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
|
||||
output.calls.push(ToolCallDelta {
|
||||
tool_index: self.emitted_tool_count,
|
||||
name: Some(name),
|
||||
arguments,
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = QwenCoderMode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for Qwen3CoderToolParser {
|
||||
fn create(tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self::new(tools)))
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
Some(StructuralTagModel::Qwen3Coder)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_qwen_coder_event(input, &mut self.mode)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
if !self.buffer.is_empty() {
|
||||
if matches!(self.mode, QwenCoderMode::ToolCall { .. })
|
||||
|| self.buffer.starts_with(TOOL_CALL_START)
|
||||
{
|
||||
return Err(parsing_failed!("incomplete Qwen Coder tool call"));
|
||||
}
|
||||
output.normal_text.push_str(&self.buffer);
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
Qwen3CoderToolParser::reset(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a Qwen Coder event for the current parser mode.
|
||||
fn parse_next_qwen_coder_event(
|
||||
input: &mut QwenCoderInput<'_>,
|
||||
mode: &mut QwenCoderMode,
|
||||
) -> ModalResult<QwenCoderEvent> {
|
||||
match mode {
|
||||
QwenCoderMode::Text => parse_text_event(input),
|
||||
QwenCoderMode::ToolCall { end_marker_scan } => tool_call_event(input, end_marker_scan),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<'_>,
|
||||
end_marker_scan: &mut MarkerScanState,
|
||||
) -> ModalResult<QwenCoderEvent> {
|
||||
let (body,) = seq!(
|
||||
_: ws0,
|
||||
take_until_marker(TOOL_CALL_END, end_marker_scan),
|
||||
_: 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),
|
||||
_: literal(PARAMETER_END),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
|
||||
Ok((name.to_string(), value.to_string()))
|
||||
}
|
||||
|
||||
/// 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, StructuralTagModel, ToolParser};
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
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_exposes_structural_tag_model() {
|
||||
let parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
|
||||
assert_eq!(
|
||||
parser.structural_tag_model(),
|
||||
Some(StructuralTagModel::Qwen3Coder)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_without_tool_call_keeps_text() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = parser.parse_complete("Hello, world!").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_extracts_single_tool_call() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"get_weather",
|
||||
&[("location", "SF"), ("date", "2026-04-29")],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({
|
||||
"location": "SF",
|
||||
"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 output = parser.parse_complete(&output).unwrap();
|
||||
|
||||
assert_eq!(output.normal_text, "Thinking... ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_converts_schema_types() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"convert",
|
||||
&[
|
||||
("whole", "5.0"),
|
||||
("flag", "true"),
|
||||
("payload", r#"{"nested":true}"#),
|
||||
("items", "[1,2]"),
|
||||
("empty", "42"),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = parser.parse_complete(&build_tool_call("get_weather", &[])).unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_parse_complete_handles_upstream_multiline_typed_params() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = 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!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("calculate_area"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"convert",
|
||||
&[(
|
||||
"payload",
|
||||
r#"{"nested":{"value":[1,2,3],"child":{"enabled":true}}}"#,
|
||||
)],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = 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!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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_preserves_raw_closing_tag_text_in_parameter_value() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"get_weather",
|
||||
&[
|
||||
(
|
||||
"location",
|
||||
"杭州 </parameter></function></tool_call>",
|
||||
),
|
||||
("date", "2026-05-08"),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = parser
|
||||
.parse_complete(&build_tool_call(
|
||||
"update_record",
|
||||
&[("data", r#"{"key":"value","count":42}"#)],
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"<tool_call>\n",
|
||||
"<function=get_weather>\n",
|
||||
"<parameter=location>SF</parameter>\n",
|
||||
"</function>\n",
|
||||
"</tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_preserves_prefix_text() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = collect_stream(
|
||||
&mut parser,
|
||||
&[
|
||||
"Thinking... ",
|
||||
"<tool_call>\n",
|
||||
"<function=get_weather>\n",
|
||||
"<parameter=location>SF</parameter>\n",
|
||||
"</function>\n",
|
||||
"</tool_call>",
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(output.normal_text, "Thinking... ");
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_without_tool_call_emits_text_incrementally() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = collect_stream(&mut parser, &["Hello, ", "world!"]);
|
||||
|
||||
assert_eq!(output.normal_text, "Hello, world!");
|
||||
assert!(output.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 output = collect_stream(&mut parser, &[&text]);
|
||||
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[1].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(output.calls[0].tool_index, 0);
|
||||
assert_eq!(output.calls[1].tool_index, 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(
|
||||
output.normal_text,
|
||||
"I'll check two cities.Between calls.Done."
|
||||
);
|
||||
assert_eq!(output.calls.len(), 2);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "city": "Dallas", "state": "TX" })
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.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 output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_handles_end_token_split_across_chunks() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_chunk(
|
||||
"<tool_call>\n\
|
||||
<function=get_weather>\n\
|
||||
<parameter=location>SF</parameter>\n\
|
||||
</function>\n\
|
||||
</tool",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert!(output.calls.is_empty());
|
||||
|
||||
let mut output = output;
|
||||
output.append(parser.parse_chunk("_call>").unwrap());
|
||||
output.append(parser.finish().unwrap());
|
||||
let output = output.coalesce_calls();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "SF" })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_buffers_long_body_until_end_marker() {
|
||||
let long_location = format!("SF-{}", "x".repeat(8192));
|
||||
let text = build_tool_call("get_weather", &[("location", &long_location)]);
|
||||
let split_at = text.len() - "_call>".len();
|
||||
let (body_with_partial_end, end_suffix) = text.split_at(split_at);
|
||||
let chunks = split_by_chars(body_with_partial_end, 31);
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let mut output = ToolParserOutput::default();
|
||||
|
||||
assert_eq!(end_suffix, "_call>");
|
||||
|
||||
for chunk in chunks {
|
||||
let chunk_output = parser.parse_chunk(chunk).unwrap();
|
||||
assert!(chunk_output.normal_text.is_empty());
|
||||
assert!(chunk_output.calls.is_empty());
|
||||
output.append(chunk_output);
|
||||
}
|
||||
|
||||
output.append(parser.parse_chunk(end_suffix).unwrap());
|
||||
output.append(parser.finish().unwrap());
|
||||
let output = output.coalesce_calls();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert_eq!(output.calls.len(), 1);
|
||||
assert_eq!(output.calls[0].name.as_deref(), Some("get_weather"));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": long_location })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_streaming_does_not_emit_incomplete_tool_call() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
let output = parser
|
||||
.parse_chunk("<tool_call>\n<function=get_weather>\n<parameter=location>SF</parameter>")
|
||||
.unwrap();
|
||||
|
||||
assert!(output.normal_text.is_empty());
|
||||
assert!(output.calls.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qwen_coder_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = Qwen3CoderToolParser::new(&test_tools());
|
||||
parser
|
||||
.parse_chunk("<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.parse_chunk("<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
|
||||
.parse_chunk(
|
||||
"<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 output = 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>(&output.calls[0].arguments).unwrap(),
|
||||
json!({ "location": "Hangzhou" })
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
use serde_json::json;
|
||||
|
||||
use super::{ToolParser, ToolParserOutput};
|
||||
use crate::tool::{Tool, ToolParserTestExt as _};
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// Panics if there are any parsing errors along the way.
|
||||
pub fn collect_stream<T: ToolParser + ?Sized>(parser: &mut T, chunks: &[&str]) -> ToolParserOutput {
|
||||
let mut output = ToolParserOutput::default();
|
||||
for chunk in chunks {
|
||||
output.append(parser.parse_chunk(chunk).unwrap());
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
output.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,109 @@
|
||||
use super::{Result, Tool, ToolCallDelta, ToolParser, ToolParserOutput};
|
||||
use crate::tool::ToolParserTestExt as _;
|
||||
|
||||
struct DefaultParser;
|
||||
|
||||
impl ToolParser for DefaultParser {
|
||||
fn create(_tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self))
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, _chunk: &str, _output: &mut ToolParserOutput) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
Ok(ToolParserOutput::default())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[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_parse_chunk_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 parse_into(&mut self, _chunk: &str, output: &mut ToolParserOutput) -> Result<()> {
|
||||
output.normal_text.push_str("prefix ");
|
||||
output.calls.extend([
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some("weather".to_string()),
|
||||
arguments: "{\"location\":".to_string(),
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: None,
|
||||
arguments: "\"Paris\"".to_string(),
|
||||
},
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some("time".to_string()),
|
||||
arguments: "{\"timezone\":".to_string(),
|
||||
},
|
||||
]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
Ok(ToolParserOutput {
|
||||
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(),
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
let mut parser = StreamingParser;
|
||||
let output = parser.parse_complete("ignored").unwrap();
|
||||
assert_eq!(output.normal_text, "prefix suffix");
|
||||
assert_eq!(
|
||||
output.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,648 @@
|
||||
//! Shared helpers for tool parsers.
|
||||
|
||||
use winnow::Parser;
|
||||
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)
|
||||
}
|
||||
|
||||
/// Streaming scan state for a buffered marker search [`take_until_marker`],
|
||||
/// so that we don't have to rescan the whole buffered prefix when resuming.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(super) struct MarkerScanState {
|
||||
scan_start: usize,
|
||||
}
|
||||
|
||||
impl MarkerScanState {
|
||||
pub(super) fn reset(&mut self) {
|
||||
self.scan_start = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse text until `marker`, resuming from the last safe scan checkpoint.
|
||||
///
|
||||
/// This is the streaming-buffered variant of `winnow::token::take_until(0..,
|
||||
/// marker)`: it returns the slice before `marker` and leaves `marker` for the
|
||||
/// caller to consume. On incomplete input, it stores the earliest byte offset
|
||||
/// that can still match `marker` and returns `Incomplete` without consuming
|
||||
/// input, so the next parse can avoid rescanning the whole buffered prefix.
|
||||
///
|
||||
/// Use this for outer parser states that keep the full buffered input across
|
||||
/// chunks while waiting for a closing marker. Plain `take_until` is still a
|
||||
/// better fit for one-shot parsers over a complete body, and for `1..` cases
|
||||
/// where an empty slice before the marker should be rejected.
|
||||
pub(super) fn take_until_marker<'i, 'a>(
|
||||
marker: &'a str,
|
||||
state: &'a mut MarkerScanState,
|
||||
) -> impl Parser<Partial<&'i str>, &'i str, ErrMode<ContextError>> + 'a {
|
||||
move |input: &mut Partial<&'i str>| take_until_marker_(input, marker, state)
|
||||
}
|
||||
|
||||
fn take_until_marker_<'i>(
|
||||
input: &mut Partial<&'i str>,
|
||||
marker: &str,
|
||||
state: &mut MarkerScanState,
|
||||
) -> ModalResult<&'i str> {
|
||||
debug_assert!(!marker.is_empty());
|
||||
|
||||
let text = **input;
|
||||
if text.is_empty() {
|
||||
return incomplete();
|
||||
}
|
||||
|
||||
// Normal updates store a char boundary; this keeps stale or misused state from panicking.
|
||||
let scan_start = floor_char_boundary(text, state.scan_start);
|
||||
|
||||
if let Some(offset) = text[scan_start..].find(marker) {
|
||||
let marker_start = scan_start + offset;
|
||||
let body = &text[..marker_start];
|
||||
input.next_slice(marker_start);
|
||||
state.reset();
|
||||
return Ok(body);
|
||||
}
|
||||
|
||||
let keep_len = partial_prefix_len(text, marker);
|
||||
state.scan_start = text.len() - keep_len;
|
||||
incomplete()
|
||||
}
|
||||
|
||||
fn floor_char_boundary(text: &str, index: usize) -> usize {
|
||||
let mut index = index.min(text.len());
|
||||
while !text.is_char_boundary(index) {
|
||||
index -= 1;
|
||||
}
|
||||
index
|
||||
}
|
||||
|
||||
/// Streaming lexical state for a top-level JSON object.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(super) struct JsonObjectScanState {
|
||||
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 expect_test::expect;
|
||||
use winnow::Parser;
|
||||
use winnow::error::ErrMode;
|
||||
use winnow::stream::{Offset, Partial, Stream};
|
||||
|
||||
use super::{
|
||||
JsonObjectScanState, MarkerScanState, json_str, partial_prefix_len, safe_text_len,
|
||||
take_json_object, take_until_marker,
|
||||
};
|
||||
|
||||
#[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 take_until_marker_stops_before_marker() {
|
||||
let mut state = MarkerScanState::default();
|
||||
let mut input = Partial::new("body</end>tail");
|
||||
let checkpoint = input.checkpoint();
|
||||
|
||||
let body = take_until_marker("</end>", &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "body");
|
||||
assert_eq!(input.offset_from(&checkpoint), "body".len());
|
||||
assert_eq!(*input, "</end>tail");
|
||||
assert_eq!(state, MarkerScanState::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_resumes_after_split_marker() {
|
||||
let mut state = MarkerScanState::default();
|
||||
let mut input = Partial::new("body</to");
|
||||
|
||||
let error = take_until_marker("</tool_call>", &mut state)
|
||||
.parse_next(&mut input)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
assert_eq!(state.scan_start, "body".len());
|
||||
|
||||
let mut input = Partial::new("body</tool_call>tail");
|
||||
let body = take_until_marker("</tool_call>", &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "body");
|
||||
assert_eq!(*input, "</tool_call>tail");
|
||||
assert_eq!(state, MarkerScanState::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_advances_checkpoint_for_long_prefix() {
|
||||
let mut state = MarkerScanState::default();
|
||||
let text = format!("{}{}", "x".repeat(1024), "</too");
|
||||
let mut input = Partial::new(text.as_str());
|
||||
|
||||
let error = take_until_marker("</tool_call>", &mut state)
|
||||
.parse_next(&mut input)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
assert_eq!(state.scan_start, 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_keeps_unicode_marker_boundaries() {
|
||||
let marker = "<|DSML|function_calls>";
|
||||
let mut state = MarkerScanState::default();
|
||||
let mut input = Partial::new("prefix <|DSML|fun");
|
||||
|
||||
let error = take_until_marker(marker, &mut state).parse_next(&mut input).unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
assert_eq!(state.scan_start, "prefix ".len());
|
||||
assert!("prefix <|DSML|fun".is_char_boundary(state.scan_start));
|
||||
|
||||
let mut input = Partial::new("prefix <|DSML|function_calls>tail");
|
||||
let body = take_until_marker(marker, &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "prefix ");
|
||||
assert_eq!(*input, "<|DSML|function_calls>tail");
|
||||
assert_eq!(state, MarkerScanState::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_floors_stale_checkpoint_to_char_boundary() {
|
||||
let mut state = MarkerScanState { scan_start: 1 };
|
||||
let mut input = Partial::new("é</end>");
|
||||
|
||||
let body = take_until_marker("</end>", &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "é");
|
||||
assert_eq!(*input, "</end>");
|
||||
assert_eq!(state, MarkerScanState::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_until_marker_handles_overlapping_prefixes() {
|
||||
let mut state = MarkerScanState::default();
|
||||
let mut input = Partial::new("xxaba");
|
||||
|
||||
let error = take_until_marker("ababa", &mut state).parse_next(&mut input).unwrap_err();
|
||||
|
||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||
assert_eq!(state.scan_start, 2);
|
||||
|
||||
let mut input = Partial::new("xxababa!");
|
||||
let body = take_until_marker("ababa", &mut state).parse_next(&mut input).unwrap();
|
||||
|
||||
assert_eq!(body, "xx");
|
||||
assert_eq!(*input, "ababa!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_json_object_consumes_simple_object() {
|
||||
let mut state = JsonObjectScanState::default();
|
||||
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(_)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,346 @@
|
||||
//! Adapter that combines reasoning and tool parsers.
|
||||
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use crate::reasoning::ReasoningParser;
|
||||
use crate::tool::{StructuralTagModel, Tool, ToolParser, ToolParserOutput};
|
||||
|
||||
use super::{Result, UnifiedParser, UnifiedParserError, UnifiedParserOutput};
|
||||
|
||||
/// Unified parser that composes existing reasoning and tool parsers.
|
||||
pub struct CombinedParser {
|
||||
reasoning: Option<Box<dyn ReasoningParser>>,
|
||||
tool: Option<Box<dyn ToolParser>>,
|
||||
}
|
||||
|
||||
impl CombinedParser {
|
||||
/// Create a combined parser from optional reasoning and tool parsers.
|
||||
pub fn new(
|
||||
reasoning: Option<Box<dyn ReasoningParser>>,
|
||||
tool: Option<Box<dyn ToolParser>>,
|
||||
) -> Self {
|
||||
Self { reasoning, tool }
|
||||
}
|
||||
|
||||
/// Create a text-only combined parser.
|
||||
pub fn plain_text_only() -> Self {
|
||||
Self {
|
||||
reasoning: None,
|
||||
tool: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_tool(&mut self, content: &str, output: &mut UnifiedParserOutput) -> Result<()> {
|
||||
let Some(tool) = self.tool.as_mut() else {
|
||||
output.push_text(content.to_string());
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// Preserve any tool output that was already produced before the error.
|
||||
let mut tool_output = ToolParserOutput::default();
|
||||
let result = tool.parse_into(content, &mut tool_output);
|
||||
output.append_tool_output(tool_output);
|
||||
result?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn flush_tool(&mut self) -> Result<UnifiedParserOutput> {
|
||||
let Some(tool) = self.tool.as_mut() else {
|
||||
return Ok(UnifiedParserOutput::default());
|
||||
};
|
||||
|
||||
let output = tool.finish()?;
|
||||
let mut unified = UnifiedParserOutput::default();
|
||||
unified.append_tool_output(output);
|
||||
Ok(unified)
|
||||
}
|
||||
}
|
||||
|
||||
impl UnifiedParser for CombinedParser {
|
||||
fn create(_tools: &[Tool], _tokenizer: DynTokenizer) -> Result<Box<dyn UnifiedParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Err(UnifiedParserError::CombinedParserConstructor)
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
if let Some(reasoning) = self.reasoning.as_mut() {
|
||||
reasoning.initialize(prompt_token_ids)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
self.reasoning.as_ref().is_some_and(|parser| parser.preserve_special_tokens())
|
||||
|| self.tool.as_ref().is_some_and(|parser| parser.preserve_special_tokens())
|
||||
}
|
||||
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
self.tool.as_ref().and_then(|parser| parser.structural_tag_model())
|
||||
}
|
||||
|
||||
fn tool_call_id(&self, tool_index: usize) -> Option<&str> {
|
||||
self.tool.as_ref().and_then(|parser| parser.tool_call_id(tool_index))
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, delta: &str, output: &mut UnifiedParserOutput) -> Result<()> {
|
||||
let Some(reasoning) = self.reasoning.as_mut() else {
|
||||
return self.parse_tool(delta, output);
|
||||
};
|
||||
|
||||
let reasoning_delta = reasoning.push(delta)?;
|
||||
if let Some(reasoning) = reasoning_delta.reasoning {
|
||||
output.push_reasoning(reasoning);
|
||||
}
|
||||
if let Some(content) = reasoning_delta.content {
|
||||
self.parse_tool(&content, output)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<UnifiedParserOutput> {
|
||||
let mut output = UnifiedParserOutput::default();
|
||||
if let Some(reasoning) = self.reasoning.as_mut() {
|
||||
let reasoning_delta = reasoning.finish()?;
|
||||
if let Some(reasoning) = reasoning_delta.reasoning {
|
||||
output.push_reasoning(reasoning);
|
||||
}
|
||||
if let Some(content) = reasoning_delta.content {
|
||||
self.parse_tool(&content, &mut output)?;
|
||||
}
|
||||
}
|
||||
output.append(self.flush_tool()?);
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.tool.as_mut().map_or_else(String::new, |parser| parser.reset())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use vllm_tokenizer::Tokenizer;
|
||||
|
||||
use super::CombinedParser;
|
||||
use crate::reasoning::{Qwen3ReasoningParser, ReasoningDelta, ReasoningParser};
|
||||
use crate::tool::{Qwen3XmlToolParser, Tool, ToolParser};
|
||||
use crate::unified::{UnifiedParser, UnifiedParserEvent, UnifiedParserOutput};
|
||||
|
||||
struct FakeTokenizer;
|
||||
|
||||
impl Tokenizer for FakeTokenizer {
|
||||
fn encode(
|
||||
&self,
|
||||
text: &str,
|
||||
_add_special_tokens: bool,
|
||||
) -> vllm_tokenizer::Result<Vec<u32>> {
|
||||
Ok(text.chars().map(u32::from).collect())
|
||||
}
|
||||
|
||||
fn decode(
|
||||
&self,
|
||||
token_ids: &[u32],
|
||||
_skip_special_tokens: bool,
|
||||
) -> vllm_tokenizer::Result<String> {
|
||||
Ok(token_ids
|
||||
.iter()
|
||||
.map(|token_id| char::from_u32(*token_id).unwrap_or('\u{FFFD}'))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn token_to_id(&self, token: &str) -> Option<u32> {
|
||||
match token {
|
||||
"<think>" => Some(1),
|
||||
"</think>" => Some(2),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_tools() -> Vec<Tool> {
|
||||
vec![Tool {
|
||||
name: "get_weather".to_string(),
|
||||
description: None,
|
||||
parameters: serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": { "type": "string" }
|
||||
},
|
||||
}),
|
||||
strict: None,
|
||||
}]
|
||||
}
|
||||
|
||||
fn collect(parser: &mut dyn UnifiedParser, chunks: &[&str]) -> UnifiedParserOutput {
|
||||
let mut output = UnifiedParserOutput::default();
|
||||
for chunk in chunks {
|
||||
parser.parse_into(chunk, &mut output).unwrap();
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
output
|
||||
}
|
||||
|
||||
struct PreserveReasoningParser;
|
||||
|
||||
impl ReasoningParser for PreserveReasoningParser {
|
||||
fn create(
|
||||
_tokenizer: vllm_tokenizer::DynTokenizer,
|
||||
) -> crate::reasoning::Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self))
|
||||
}
|
||||
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn push(&mut self, delta: &str) -> crate::reasoning::Result<ReasoningDelta> {
|
||||
Ok(ReasoningDelta {
|
||||
reasoning: None,
|
||||
content: Some(delta.to_string()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct PreserveToolParser;
|
||||
|
||||
impl ToolParser for PreserveToolParser {
|
||||
fn create(_tools: &[Tool]) -> crate::tool::Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self))
|
||||
}
|
||||
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn parse_into(
|
||||
&mut self,
|
||||
chunk: &str,
|
||||
output: &mut crate::tool::ToolParserOutput,
|
||||
) -> crate::tool::Result<()> {
|
||||
output.normal_text.push_str(chunk);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> crate::tool::Result<crate::tool::ToolParserOutput> {
|
||||
Ok(crate::tool::ToolParserOutput::default())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
struct PartialThenErrorToolParser;
|
||||
|
||||
impl ToolParser for PartialThenErrorToolParser {
|
||||
fn create(_tools: &[Tool]) -> crate::tool::Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Ok(Box::new(Self))
|
||||
}
|
||||
|
||||
fn parse_into(
|
||||
&mut self,
|
||||
_chunk: &str,
|
||||
output: &mut crate::tool::ToolParserOutput,
|
||||
) -> crate::tool::Result<()> {
|
||||
output.normal_text.push_str("committed");
|
||||
Err(crate::tool::ToolParserError::ParsingFailed {
|
||||
message: "synthetic failure".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> crate::tool::Result<crate::tool::ToolParserOutput> {
|
||||
Ok(crate::tool::ToolParserOutput::default())
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combined_parser_emits_reasoning_and_text() {
|
||||
let tokenizer = Arc::new(FakeTokenizer);
|
||||
let reasoning = Qwen3ReasoningParser::create(tokenizer).unwrap();
|
||||
let mut parser = CombinedParser::new(Some(reasoning), None);
|
||||
|
||||
let output = collect(&mut parser, &["<think>work</think>answer"]);
|
||||
|
||||
assert_eq!(
|
||||
output.events,
|
||||
vec![
|
||||
UnifiedParserEvent::Reasoning("work".to_string()),
|
||||
UnifiedParserEvent::Text("answer".to_string()),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combined_parser_emits_tool_calls_from_visible_content() {
|
||||
let tool = Qwen3XmlToolParser::create(&test_tools()).unwrap();
|
||||
let mut parser = CombinedParser::new(None, Some(tool));
|
||||
assert!(matches!(
|
||||
parser.structural_tag_model(),
|
||||
Some(crate::tool::StructuralTagModel::Qwen3)
|
||||
));
|
||||
|
||||
let output = collect(
|
||||
&mut parser,
|
||||
&[r#"<tool_call>
|
||||
{"name":"get_weather","arguments":{"location":"Paris"}}
|
||||
</tool_call>"#],
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
output.events,
|
||||
vec![
|
||||
UnifiedParserEvent::ToolCall(crate::tool::ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some("get_weather".to_string()),
|
||||
arguments: String::new(),
|
||||
}),
|
||||
UnifiedParserEvent::ToolCall(crate::tool::ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: None,
|
||||
arguments: r#"{"location":"Paris"}"#.to_string(),
|
||||
}),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combined_parser_preserves_tool_output_on_parse_error() {
|
||||
let mut parser = CombinedParser::new(None, Some(Box::new(PartialThenErrorToolParser)));
|
||||
let mut output = UnifiedParserOutput::default();
|
||||
|
||||
let error = parser.parse_into("bad", &mut output).unwrap_err();
|
||||
|
||||
assert!(matches!(error, crate::unified::UnifiedParserError::Tool(_)));
|
||||
assert_eq!(
|
||||
output.events,
|
||||
vec![UnifiedParserEvent::Text("committed".to_string())]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combined_parser_preserves_special_tokens_when_either_inner_parser_needs_it() {
|
||||
let mut parser = CombinedParser::new(Some(Box::new(PreserveReasoningParser)), None);
|
||||
assert!(parser.preserve_special_tokens());
|
||||
|
||||
parser = CombinedParser::new(None, Some(Box::new(PreserveToolParser)));
|
||||
assert!(parser.preserve_special_tokens());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
//! Unified parser interface for reasoning and tool-call deltas.
|
||||
|
||||
mod combined;
|
||||
|
||||
use thiserror::Error;
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
pub use combined::CombinedParser;
|
||||
|
||||
use crate::reasoning::ReasoningError;
|
||||
use crate::tool::{StructuralTagModel, Tool, ToolCallDelta, ToolParserError, ToolParserOutput};
|
||||
|
||||
/// Result alias for unified parser operations.
|
||||
pub type Result<T> = std::result::Result<T, UnifiedParserError>;
|
||||
|
||||
/// One parsed event emitted by a unified parser.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum UnifiedParserEvent {
|
||||
/// Normal assistant-visible text.
|
||||
Text(String),
|
||||
/// Reasoning text hidden from the normal content stream.
|
||||
Reasoning(String),
|
||||
/// A tool-call update extracted from visible assistant text.
|
||||
ToolCall(ToolCallDelta),
|
||||
}
|
||||
|
||||
/// Result of advancing unified parsing with one assistant-text input.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct UnifiedParserOutput {
|
||||
/// Ordered parser events committed by this input.
|
||||
pub events: Vec<UnifiedParserEvent>,
|
||||
}
|
||||
|
||||
impl UnifiedParserOutput {
|
||||
/// Append one visible text event if `delta` is non-empty.
|
||||
pub fn push_text(&mut self, delta: String) {
|
||||
if delta.is_empty() {
|
||||
return;
|
||||
}
|
||||
self.events.push(UnifiedParserEvent::Text(delta));
|
||||
}
|
||||
|
||||
/// Append one reasoning text event if `delta` is non-empty.
|
||||
pub fn push_reasoning(&mut self, delta: String) {
|
||||
if delta.is_empty() {
|
||||
return;
|
||||
}
|
||||
self.events.push(UnifiedParserEvent::Reasoning(delta));
|
||||
}
|
||||
|
||||
/// Append parsed tool parser output as unified events.
|
||||
pub fn append_tool_output(&mut self, output: ToolParserOutput) {
|
||||
// TODO: make ToolParserOutput carry ordered events and remove this text-first flattening.
|
||||
self.push_text(output.normal_text);
|
||||
self.events.extend(output.calls.into_iter().map(UnifiedParserEvent::ToolCall));
|
||||
}
|
||||
|
||||
/// Append another parser output onto this one.
|
||||
pub fn append(&mut self, mut other: Self) {
|
||||
self.events.append(&mut other.events);
|
||||
}
|
||||
}
|
||||
|
||||
/// Incremental parser that extracts reasoning and tool-call events from assistant output.
|
||||
pub trait UnifiedParser: Send {
|
||||
/// Construct a boxed parser instance for one request stream.
|
||||
fn create(tools: &[Tool], tokenizer: DynTokenizer) -> Result<Box<dyn UnifiedParser>>
|
||||
where
|
||||
Self: Sized + 'static;
|
||||
|
||||
/// Initialize parser state from prompt token IDs before output deltas arrive.
|
||||
fn initialize(&mut self, _prompt_token_ids: &[u32]) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Return whether decoded output must preserve tokenizer special tokens.
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Return the xgrammar structural-tag model used for strict tool calling.
|
||||
fn structural_tag_model(&self) -> Option<StructuralTagModel> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Return the parser-provided ID for a tool call by index, if the model emitted one.
|
||||
fn tool_call_id(&self, _tool_index: usize) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Feed one decoded text delta into the parser, appending committed output into `output`.
|
||||
fn parse_into(&mut self, delta: &str, output: &mut UnifiedParserOutput) -> Result<()>;
|
||||
|
||||
/// Flush any buffered parser state at end of stream.
|
||||
fn finish(&mut self) -> Result<UnifiedParserOutput> {
|
||||
Ok(UnifiedParserOutput::default())
|
||||
}
|
||||
|
||||
/// Clear parser state and return currently uncommitted buffered text.
|
||||
fn reset(&mut self) -> String {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Errors produced while creating or running unified parsers.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum UnifiedParserError {
|
||||
#[error("combined parser is constructed from split parser instances")]
|
||||
CombinedParserConstructor,
|
||||
#[error(transparent)]
|
||||
Reasoning(#[from] ReasoningError),
|
||||
#[error(transparent)]
|
||||
Tool(#[from] ToolParserError),
|
||||
}
|
||||
Reference in New Issue
Block a user