use crate::request::{FunctionCall, ToolCall};
use crate::response::COMPLETION_ID_PREFIX;
pub const OPEN_TAG: &str = "<tool_call>";
pub const CLOSE_TAG: &str = "</tool_call>";
pub const OPEN_TAG_ID: u32 = 151_657;
pub const CLOSE_TAG_ID: u32 = 151_658;
pub const CALL_KIND: &str = "function";
pub const CALL_ID_PREFIX: &str = "call_";
const NO_ARGUMENTS: &str = "{}";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedCall {
pub name: String,
pub arguments: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Extracted {
pub content: String,
pub calls: Vec<ParsedCall>,
}
impl Extracted {
pub fn none(reply: &str) -> Self {
Extracted {
content: reply.to_owned(),
calls: Vec::new(),
}
}
}
pub fn extract(reply: &str) -> Extracted {
let mut content = String::new();
let mut calls = Vec::new();
let mut cursor = 0;
while let Some(open) = reply.get(cursor..).and_then(|rest| rest.find(OPEN_TAG)) {
let open = cursor + open;
let body_start = open + OPEN_TAG.len();
let Some(close) = find_close(reply, body_start) else {
break;
};
let body = reply.get(body_start..close).unwrap_or_default();
match parse_block(body) {
Some(call) => {
content.push_str(reply.get(cursor..open).unwrap_or_default());
calls.push(call);
}
None => content.push_str(
reply
.get(cursor..close + CLOSE_TAG.len())
.unwrap_or_default(),
),
}
cursor = close + CLOSE_TAG.len();
}
content.push_str(reply.get(cursor..).unwrap_or_default());
if !calls.is_empty() {
content = content.trim().to_owned();
}
Extracted { content, calls }
}
pub fn call_id(completion_id: &str, index: usize) -> String {
let suffix = completion_id
.strip_prefix(COMPLETION_ID_PREFIX)
.unwrap_or(completion_id);
format!("{CALL_ID_PREFIX}{suffix}_{index}")
}
pub fn wire_call(completion_id: &str, index: usize, call: &ParsedCall) -> ToolCall {
ToolCall {
id: call_id(completion_id, index),
kind: CALL_KIND.to_owned(),
function: FunctionCall {
name: call.name.clone(),
arguments: call.arguments.clone(),
},
}
}
pub fn wire_calls(completion_id: &str, calls: &[ParsedCall]) -> Vec<ToolCall> {
calls
.iter()
.enumerate()
.map(|(index, call)| wire_call(completion_id, index, call))
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Step {
Nothing,
Content(String),
Call {
index: usize,
call: ParsedCall,
},
}
#[derive(Debug, Clone)]
pub struct CallStream {
enabled: bool,
open: Option<String>,
emitted: usize,
}
impl CallStream {
pub fn new(enabled: bool) -> Self {
CallStream {
enabled,
open: None,
emitted: 0,
}
}
pub fn emitted(&self) -> usize {
self.emitted
}
pub fn push(&mut self, id: u32, text: &str) -> Step {
if !self.enabled {
return Step::Content(text.to_owned());
}
match id {
OPEN_TAG_ID if self.open.is_none() => {
self.open = Some(String::new());
Step::Nothing
}
CLOSE_TAG_ID if self.open.is_some() => self.close(),
_ => match &mut self.open {
Some(body) => {
body.push_str(text);
Step::Nothing
}
None => Step::Content(text.to_owned()),
},
}
}
pub fn flush(&mut self) -> Option<String> {
self.open.take().map(|body| format!("{OPEN_TAG}{body}"))
}
fn close(&mut self) -> Step {
let body = self.open.take().unwrap_or_default();
match parse_block(&body) {
Some(call) => {
let index = self.emitted;
self.emitted += 1;
Step::Call { index, call }
}
None => Step::Content(format!("{OPEN_TAG}{body}{CLOSE_TAG}")),
}
}
}
fn find_close(reply: &str, from: usize) -> Option<usize> {
let bytes = reply.as_bytes();
let mut i = from;
while i < bytes.len() {
match bytes[i] {
b'"' => i = end_of_string(bytes, i),
b'<' if bytes[i..].starts_with(CLOSE_TAG.as_bytes()) => return Some(i),
_ => i += 1,
}
}
reply
.get(from..)
.and_then(|rest| rest.find(CLOSE_TAG))
.map(|at| from + at)
}
fn parse_block(body: &str) -> Option<ParsedCall> {
let open = body.find('{')?;
let mut name = None;
let mut arguments = None;
for (key, value) in members(body, open) {
match key.as_str() {
"name" if name.is_none() => name = Some(value),
"arguments" if arguments.is_none() => arguments = Some(value),
_ => {}
}
}
let name = unquote(name?);
if name.is_empty() {
return None;
}
Some(ParsedCall {
name,
arguments: arguments.map_or_else(|| NO_ARGUMENTS.to_owned(), str::to_owned),
})
}
fn members(text: &str, open: usize) -> Vec<(String, &str)> {
let bytes = text.as_bytes();
let mut out = Vec::new();
let mut i = open + 1;
loop {
i = skip_space(bytes, i);
if bytes.get(i) != Some(&b'"') {
return out;
}
let key_end = end_of_string(bytes, i);
let key = unquote(text.get(i..key_end).unwrap_or_default());
i = skip_space(bytes, key_end);
if bytes.get(i) != Some(&b':') {
return out;
}
i = skip_space(bytes, i + 1);
if i >= bytes.len() {
return out;
}
let value_end = end_of_value(bytes, i);
out.push((key, text.get(i..value_end).unwrap_or_default()));
i = skip_space(bytes, value_end);
if bytes.get(i) != Some(&b',') {
return out;
}
i += 1;
}
}
fn end_of_value(bytes: &[u8], start: usize) -> usize {
match bytes.get(start) {
Some(b'"') => end_of_string(bytes, start),
Some(b'{') => end_of_nested(bytes, start, b'{', b'}'),
Some(b'[') => end_of_nested(bytes, start, b'[', b']'),
_ => {
let mut i = start;
while i < bytes.len() && !matches!(bytes[i], b',' | b'}' | b']') && !is_space(bytes[i])
{
i += 1;
}
i
}
}
}
fn end_of_nested(bytes: &[u8], start: usize, open: u8, close: u8) -> usize {
let mut depth = 0usize;
let mut i = start;
while i < bytes.len() {
let byte = bytes[i];
if byte == b'"' {
i = end_of_string(bytes, i);
continue;
}
if byte == open {
depth += 1;
} else if byte == close {
depth = depth.saturating_sub(1);
if depth == 0 {
return i + 1;
}
}
i += 1;
}
bytes.len()
}
fn end_of_string(bytes: &[u8], start: usize) -> usize {
let mut i = start + 1;
while i < bytes.len() {
match bytes[i] {
b'\\' => i += 2,
b'"' => return i + 1,
_ => i += 1,
}
}
bytes.len()
}
fn is_space(byte: u8) -> bool {
matches!(byte, b' ' | b'\t' | b'\n' | b'\r')
}
fn skip_space(bytes: &[u8], mut i: usize) -> usize {
while i < bytes.len() && is_space(bytes[i]) {
i += 1;
}
i
}
fn unquote(raw: &str) -> String {
if let Ok(text) = serde_json::from_str::<String>(raw) {
return text;
}
raw.strip_prefix('"')
.map_or(raw, |rest| rest.strip_suffix('"').unwrap_or(rest))
.to_owned()
}
#[cfg(test)]
mod tests {
use super::*;
fn block(arguments: &str) -> String {
format!("<tool_call>\n{{\"name\": \"read\", \"arguments\": {arguments}}}\n</tool_call>")
}
#[test]
fn a_reply_with_no_calls_is_returned_byte_for_byte() {
for reply in [
"",
"4",
" Sure — the answer is 4.\n\n",
"a < b and c > d",
"here is a tag: <tool>",
] {
let out = extract(reply);
assert!(out.calls.is_empty(), "{reply:?} produced {:?}", out.calls);
assert_eq!(out.content, reply, "a plain reply must not be rewritten");
}
}
#[test]
fn one_call_becomes_one_parsed_call_and_empty_content() {
let out = extract(&block(r#"{"path": "a.txt"}"#));
assert_eq!(out.content, "");
assert_eq!(
out.calls,
vec![ParsedCall {
name: "read".to_owned(),
arguments: r#"{"path": "a.txt"}"#.to_owned(),
}]
);
}
#[test]
fn two_calls_in_one_turn_are_both_extracted_in_order() {
let reply = format!(
"{}\n{}",
block(r#"{"path": "a"}"#),
block(r#"{"path": "b"}"#)
);
let out = extract(&reply);
assert_eq!(out.calls.len(), 2);
assert_eq!(out.calls[0].arguments, r#"{"path": "a"}"#);
assert_eq!(out.calls[1].arguments, r#"{"path": "b"}"#);
assert_eq!(out.content, "");
}
#[test]
fn prose_before_between_and_after_survives() {
let reply = format!(
"I will read both.\n{}\nand the other one\n{}\nThen I will compare them.",
block(r#"{"path": "a"}"#),
block(r#"{"path": "b"}"#)
);
let out = extract(&reply);
assert_eq!(out.calls.len(), 2);
assert_eq!(
out.content,
"I will read both.\n\nand the other one\n\nThen I will compare them."
);
}
#[test]
fn an_empty_arguments_object_is_preserved_as_written() {
let out = extract(&block("{}"));
assert_eq!(out.calls[0].arguments, "{}");
}
#[test]
fn a_block_with_no_arguments_key_gets_an_empty_object() {
let out = extract("<tool_call>\n{\"name\": \"now\"}\n</tool_call>");
assert_eq!(
out.calls,
vec![ParsedCall {
name: "now".to_owned(),
arguments: "{}".to_owned(),
}]
);
}
#[test]
fn malformed_json_with_a_recoverable_name_still_becomes_a_call() {
let out = extract(&block(r#"{"path": "a.txt",}"#));
assert_eq!(out.calls[0].name, "read");
assert_eq!(
out.calls[0].arguments, r#"{"path": "a.txt",}"#,
"the model's bytes reach the client unaltered, invalid or not"
);
assert_eq!(out.content, "");
}
#[test]
fn a_block_missing_its_closing_brace_keeps_what_arguments_had() {
let out = extract(
"<tool_call>\n{\"name\": \"read\", \"arguments\": {\"path\": \"a\"\n</tool_call>",
);
assert_eq!(out.calls[0].name, "read");
assert_eq!(out.calls[0].arguments, "{\"path\": \"a\"\n");
}
#[test]
fn a_block_with_no_recoverable_name_is_content_markers_and_all() {
for body in [
"{\"arguments\": {\"path\": \"a\"}}",
"{\"name\": \"\"}",
"just some words",
"",
] {
let reply = format!("<tool_call>{body}</tool_call>");
let out = extract(&reply);
assert!(out.calls.is_empty(), "{body:?} became a call");
assert_eq!(out.content, reply, "the block must survive as text");
}
}
#[test]
fn an_unterminated_block_is_content() {
let reply = "Reading it.\n<tool_call>\n{\"name\": \"read\", \"argum";
let out = extract(reply);
assert!(out.calls.is_empty());
assert_eq!(out.content, reply);
}
#[test]
fn a_stray_closing_marker_is_content() {
let reply = "the tag is </tool_call>, as shown";
let out = extract(reply);
assert!(out.calls.is_empty());
assert_eq!(out.content, reply);
}
#[test]
fn nested_braces_inside_arguments_are_balanced_not_guessed() {
let arguments =
r#"{"filter": {"path": "a.txt", "range": {"from": 1, "to": 9}}, "raw": true}"#;
let out = extract(&block(arguments));
assert_eq!(out.calls[0].arguments, arguments);
}
#[test]
fn a_brace_inside_a_string_argument_does_not_end_the_value() {
let arguments = r#"{"pattern": "^\\{.*\\}$", "note": "a } on its own"}"#;
let out = extract(&block(arguments));
assert_eq!(out.calls[0].arguments, arguments);
}
#[test]
fn a_closing_marker_inside_a_string_argument_does_not_close_the_block() {
let arguments = r#"{"text": "wrap it in </tool_call> tags"}"#;
let out = extract(&block(arguments));
assert_eq!(out.calls.len(), 1, "the block closed too early");
assert_eq!(out.calls[0].arguments, arguments);
assert_eq!(out.content, "");
}
#[test]
fn an_escaped_quote_does_not_end_the_string_scan() {
let arguments = r#"{"text": "he said \"</tool_call>\" out loud"}"#;
let out = extract(&block(arguments));
assert_eq!(out.calls.len(), 1);
assert_eq!(out.calls[0].arguments, arguments);
}
#[test]
fn an_unbalanced_quote_does_not_hide_the_terminator() {
let reply =
"<tool_call>\n{\"name\": \"read\", \"arguments\": {\"path: \"a}\n</tool_call>\nDone.";
let out = extract(reply);
assert_eq!(out.calls.len(), 1, "the terminator was lost");
assert_eq!(out.calls[0].name, "read");
assert_eq!(out.content, "Done.");
}
#[test]
fn an_escaped_name_is_unescaped_and_its_arguments_are_not() {
let reply = r#"<tool_call>{"name": "read", "arguments": {"q": "a\nb"}}</tool_call>"#;
let out = extract(reply);
assert_eq!(out.calls[0].name, "read");
assert_eq!(
out.calls[0].arguments, r#"{"q": "a\nb"}"#,
"arguments are bytes, not a decoded value"
);
}
#[test]
fn non_ascii_text_around_and_inside_a_block_is_safe() {
let reply = format!(
"Je vais lire → {}\n…voilà",
block(r#"{"path": "café.txt", "note": "naïve"}"#)
);
let out = extract(&reply);
assert_eq!(
out.calls[0].arguments,
r#"{"path": "café.txt", "note": "naïve"}"#
);
assert_eq!(out.content, "Je vais lire → \n…voilà");
}
#[test]
fn argument_bytes_are_preserved_exactly() {
let arguments = "{ \"b\":2,\n \"a\" : 1 }";
let out = extract(&block(arguments));
assert_eq!(out.calls[0].arguments, arguments);
}
#[test]
fn ids_are_derived_from_the_completion_id_and_the_index() {
let calls = vec![
ParsedCall {
name: "read".to_owned(),
arguments: "{}".to_owned(),
},
ParsedCall {
name: "write".to_owned(),
arguments: r#"{"path":"a"}"#.to_owned(),
},
];
let wire = wire_calls("chatcmpl-18f3a0001", &calls);
assert_eq!(wire[0].id, "call_18f3a0001_0");
assert_eq!(wire[1].id, "call_18f3a0001_1");
assert!(wire.iter().all(|call| call.kind == "function"));
assert_eq!(wire[1].function.name, "write");
assert_eq!(wire[1].function.arguments, r#"{"path":"a"}"#);
assert_eq!(wire_calls("chatcmpl-18f3a0001", &calls), wire);
assert_eq!(call_id("x", 3), "call_x_3");
}
const TEXT: u32 = 7;
fn drive(enabled: bool, tokens: &[(u32, &str)]) -> (String, Vec<(usize, ParsedCall)>) {
let mut stream = CallStream::new(enabled);
let mut content = String::new();
let mut calls = Vec::new();
for (id, text) in tokens {
match stream.push(*id, text) {
Step::Nothing => {}
Step::Content(text) => content.push_str(&text),
Step::Call { index, call } => calls.push((index, call)),
}
}
if let Some(rest) = stream.flush() {
content.push_str(&rest);
}
assert_eq!(stream.flush(), None, "flush is not idempotent");
assert_eq!(stream.emitted(), calls.len());
(content, calls)
}
fn streamed(body: &str) -> Vec<(u32, &str)> {
vec![
(OPEN_TAG_ID, OPEN_TAG),
(TEXT, body),
(CLOSE_TAG_ID, CLOSE_TAG),
]
}
#[test]
fn a_streamed_block_recovers_what_the_buffered_path_recovers() {
let body = "\n{\"name\": \"glob\", \"arguments\": {\"pattern\": \"*/.rs\"}}\n";
let (content, calls) = drive(true, &streamed(body));
assert_eq!(content, "");
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].0, 0);
assert_eq!(
calls[0].1,
extract(&format!("{OPEN_TAG}{body}{CLOSE_TAG}")).calls[0]
);
assert_eq!(calls[0].1.name, "glob");
assert_eq!(calls[0].1.arguments, r#"{"pattern": "*/.rs"}"#);
}
#[test]
fn a_body_split_across_tokens_is_reassembled_whole() {
let mut tokens = vec![(OPEN_TAG_ID, OPEN_TAG)];
for piece in [
"\n{\"na",
"me\": \"re",
"ad\", \"argu",
"ments\": {\"path\": \"a\"}}\n",
] {
tokens.push((TEXT, piece));
}
tokens.push((CLOSE_TAG_ID, CLOSE_TAG));
let (content, calls) = drive(true, &tokens);
assert_eq!(content, "");
assert_eq!(calls[0].1.name, "read");
assert_eq!(calls[0].1.arguments, r#"{"path": "a"}"#);
}
#[test]
fn prose_before_between_and_after_streams_as_content() {
let mut tokens = vec![(TEXT, "I will "), (TEXT, "read both.\n")];
tokens.extend(streamed(r#"{"name": "read", "arguments": {"path": "a"}}"#));
tokens.push((TEXT, "\nand the other\n"));
tokens.extend(streamed(r#"{"name": "read", "arguments": {"path": "b"}}"#));
tokens.push((TEXT, "\nDone."));
let (content, calls) = drive(true, &tokens);
assert_eq!(content, "I will read both.\n\nand the other\n\nDone.");
assert_eq!(calls.len(), 2);
assert_eq!(calls[0].0, 0);
assert_eq!(calls[1].0, 1);
assert_eq!(calls[0].1.arguments, r#"{"path": "a"}"#);
assert_eq!(calls[1].1.arguments, r#"{"path": "b"}"#);
}
#[test]
fn an_unterminated_streamed_block_flushes_as_content() {
let tokens = [
(TEXT, "Reading it.\n"),
(OPEN_TAG_ID, OPEN_TAG),
(TEXT, "\n{\"name\": \"read\", \"argum"),
];
let (content, calls) = drive(true, &tokens);
assert!(calls.is_empty());
assert_eq!(
content,
"Reading it.\n<tool_call>\n{\"name\": \"read\", \"argum"
);
}
#[test]
fn a_streamed_block_without_tools_is_ordinary_text() {
let body = r#"{"name": "read", "arguments": {}}"#;
let (content, calls) = drive(false, &streamed(body));
assert!(calls.is_empty());
assert_eq!(content, format!("{OPEN_TAG}{body}{CLOSE_TAG}"));
}
#[test]
fn a_streamed_block_with_no_recoverable_name_is_content_markers_and_all() {
for body in ["{\"arguments\": {}}", "{\"name\": \"\"}", "words", ""] {
let (content, calls) = drive(true, &streamed(body));
assert!(calls.is_empty(), "{body:?} became a call");
assert_eq!(content, format!("{OPEN_TAG}{body}{CLOSE_TAG}"));
}
}
#[test]
fn a_stray_streamed_terminator_is_content() {
let tokens = [
(TEXT, "the tag is "),
(CLOSE_TAG_ID, CLOSE_TAG),
(TEXT, ", as shown"),
];
let (content, calls) = drive(true, &tokens);
assert!(calls.is_empty());
assert_eq!(content, "the tag is </tool_call>, as shown");
}
#[test]
fn a_nested_opener_is_body_text() {
let tokens = [
(OPEN_TAG_ID, OPEN_TAG),
(TEXT, "{\"name\": \"echo\", \"arguments\": {\"t\": \""),
(OPEN_TAG_ID, OPEN_TAG),
(TEXT, "\"}}"),
(CLOSE_TAG_ID, CLOSE_TAG),
];
let (content, calls) = drive(true, &tokens);
assert_eq!(content, "");
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].1.arguments, "{\"t\": \"<tool_call>\"}");
}
#[test]
fn malformed_json_in_a_streamed_block_still_yields_a_call() {
let (content, calls) = drive(
true,
&streamed(r#"{"name": "read", "arguments": {"p": 1,}}"#),
);
assert_eq!(content, "");
assert_eq!(calls[0].1.name, "read");
assert_eq!(calls[0].1.arguments, r#"{"p": 1,}"#);
}
#[test]
fn streamed_call_ids_match_the_buffered_paths() {
let body = r#"{"name": "read", "arguments": {}}"#;
let mut tokens = streamed(body);
tokens.extend(streamed(body));
let (_, calls) = drive(true, &tokens);
let parsed: Vec<ParsedCall> = calls.iter().map(|(_, call)| call.clone()).collect();
let buffered = wire_calls("chatcmpl-18f3a0001", &parsed);
let streamed: Vec<ToolCall> = calls
.iter()
.map(|(index, call)| wire_call("chatcmpl-18f3a0001", *index, call))
.collect();
assert_eq!(streamed, buffered);
assert_eq!(streamed[0].id, "call_18f3a0001_0");
assert_eq!(streamed[1].id, "call_18f3a0001_1");
}
#[test]
fn the_marker_ids_are_the_qwen3_added_tokens() {
assert_eq!(OPEN_TAG_ID, 151_657);
assert_eq!(CLOSE_TAG_ID, 151_658);
assert_ne!(OPEN_TAG_ID, CLOSE_TAG_ID);
}
#[test]
fn a_call_round_trips_back_into_the_block_the_model_emitted() {
let reply = block(r#"{"path": "a.txt"}"#);
let out = extract(&reply);
let wire = wire_calls("chatcmpl-1", &out.calls);
let rendered = format!(
"<tool_call>\n{{\"name\": \"{}\", \"arguments\": {}}}\n</tool_call>",
wire[0].function.name, wire[0].function.arguments
);
assert_eq!(rendered, reply);
}
}