1use once_cell::sync::Lazy;
8use regex::Regex;
9use serde_json::Value;
10
11use crate::runtime::model_config::ModelArchitecture;
12use hyprstream_rpc_std::inference_client::{ToolCall, ToolCallFunction};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum ToolCallFormat {
24 Qwen3Xml,
26 Qwen35XmlParam,
29 LlamaJson,
31 MistralJson,
33 None,
35}
36
37impl ToolCallFormat {
38 pub fn from_architecture(arch: &ModelArchitecture) -> Self {
40 match arch {
41 ModelArchitecture::Qwen => Self::Qwen3Xml,
42 ModelArchitecture::Qwen3_5 => Self::Qwen35XmlParam,
43 ModelArchitecture::Llama => Self::LlamaJson,
44 ModelArchitecture::Mistral => Self::MistralJson,
45 ModelArchitecture::Gemma | ModelArchitecture::Janus | ModelArchitecture::Unknown(_) => {
46 Self::None
47 }
48 }
49 }
50
51 pub fn from_architecture_str(arch: &str) -> Self {
53 match arch.to_lowercase().as_str() {
54 "qwen" | "qwen2" | "qwen3" => Self::Qwen3Xml,
55 "qwen3_5" | "qwen3_5_text" | "qwen3_5_moe" => Self::Qwen35XmlParam,
56 "llama" | "llama2" | "llama3" => Self::LlamaJson,
57 "mistral" | "mixtral" => Self::MistralJson,
58 _ => Self::None,
59 }
60 }
61
62 pub fn from_model_ref(model_ref: &str) -> Self {
67 let name = model_ref
68 .split(':')
69 .next()
70 .unwrap_or(model_ref)
71 .to_lowercase();
72 if name.contains("qwen3.5") || name.contains("qwen3_5") {
73 Self::Qwen35XmlParam
74 } else if name.contains("qwen") {
75 Self::Qwen3Xml
76 } else if name.contains("llama") {
77 Self::LlamaJson
78 } else if name.contains("mistral") || name.contains("mixtral") {
79 Self::MistralJson
80 } else {
81 Self::None
82 }
83 }
84}
85
86pub fn has_tool_calls_for_format(format: ToolCallFormat, text: &str) -> bool {
92 match format {
93 ToolCallFormat::Qwen3Xml => text.contains("<tool_call>"),
94 ToolCallFormat::Qwen35XmlParam => has_qwen35_xml_tool_calls(text),
95 ToolCallFormat::LlamaJson => text.contains("<|python_tag|>"),
96 ToolCallFormat::MistralJson => text.contains("[TOOL_CALLS]"),
97 ToolCallFormat::None => false,
98 }
99}
100
101pub fn parse_tool_calls_for_format(
103 format: ToolCallFormat,
104 text: &str,
105) -> Result<Vec<ToolCall>, anyhow::Error> {
106 match format {
107 ToolCallFormat::Qwen3Xml => parse_qwen3_tool_calls(text),
108 ToolCallFormat::Qwen35XmlParam => parse_qwen35_xml_tool_calls(text),
109 ToolCallFormat::LlamaJson => parse_llama_tool_calls(text),
110 ToolCallFormat::MistralJson => parse_mistral_tool_calls(text),
111 ToolCallFormat::None => Ok(vec![]),
112 }
113}
114
115pub fn extract_text_content_for_format(format: ToolCallFormat, text: &str) -> String {
117 match format {
118 ToolCallFormat::Qwen3Xml => extract_qwen3_text_content(text),
119 ToolCallFormat::Qwen35XmlParam => extract_qwen35_xml_text_content(text),
120 ToolCallFormat::LlamaJson => extract_llama_text_content(text),
121 ToolCallFormat::MistralJson => extract_mistral_text_content(text),
122 ToolCallFormat::None => text.to_owned(),
123 }
124}
125
126pub fn has_tool_calls(text: &str) -> bool {
134 has_tool_calls_for_format(ToolCallFormat::Qwen3Xml, text)
135}
136
137pub fn extract_text_content(text: &str) -> String {
141 extract_qwen3_text_content(text)
142}
143
144pub fn parse_qwen3_tool_calls(text: &str) -> Result<Vec<ToolCall>, anyhow::Error> {
156 let mut tool_calls = Vec::new();
157 let mut search_from = 0;
158
159 while let Some(pos) = text[search_from..].find("</tool_call>") {
160 let close_tag = search_from + pos;
161
162 let region = &text[search_from..close_tag];
163 let open_tag = match region.rfind("<tool_call>") {
164 Some(pos) => search_from + pos,
165 None => {
166 search_from = close_tag + "</tool_call>".len();
167 continue;
168 }
169 };
170
171 let inner = text[open_tag + "<tool_call>".len()..close_tag].trim();
172 search_from = close_tag + "</tool_call>".len();
173
174 if inner.is_empty() {
175 continue;
176 }
177
178 let call_data: Value = match serde_json::from_str(inner) {
179 Ok(v) => v,
180 Err(_) => continue,
181 };
182
183 let name = match call_data["name"].as_str() {
184 Some(n) => n.to_owned(),
185 None => continue,
186 };
187
188 let arguments = call_data["arguments"].clone();
189 let arguments_str = serde_json::to_string(&arguments)?;
190
191 tool_calls.push(ToolCall {
192 id: format!("call_{}", uuid::Uuid::new_v4()),
193 tool_type: "function".to_owned(),
194 function: ToolCallFunction {
195 name,
196 arguments: arguments_str,
197 },
198 });
199 }
200
201 Ok(tool_calls)
202}
203
204fn extract_qwen3_text_content(text: &str) -> String {
206 static TOOL_CALL_REGEX: Lazy<Regex> = Lazy::new(|| {
207 #[allow(clippy::unwrap_used)]
208 Regex::new(r#"(?s)<tool_call>.*?</tool_call>"#).unwrap()
209 });
210 TOOL_CALL_REGEX.replace_all(text, "").trim().to_owned()
211}
212
213pub fn has_qwen35_xml_tool_calls(text: &str) -> bool {
226 text.contains("<tool_call>") && text.contains("<function=")
227}
228
229pub fn parse_qwen35_xml_tool_calls(text: &str) -> Result<Vec<ToolCall>, anyhow::Error> {
248 static TOOL_CALL_RE: Lazy<Regex> = Lazy::new(|| {
250 #[allow(clippy::unwrap_used)]
251 Regex::new(r"(?s)<tool_call>(.*?)</tool_call>").unwrap()
252 });
253 static FUNCTION_RE: Lazy<Regex> = Lazy::new(|| {
254 #[allow(clippy::unwrap_used)]
255 Regex::new(r"(?s)<function=([^>]+)>(.*?)</function>").unwrap()
256 });
257 static PARAM_RE: Lazy<Regex> = Lazy::new(|| {
258 #[allow(clippy::unwrap_used)]
259 Regex::new(r"(?s)<parameter=([^>]+)>(.*?)</parameter>").unwrap()
260 });
261
262 let mut tool_calls = Vec::new();
263
264 for tc_cap in TOOL_CALL_RE.captures_iter(text) {
265 let tc_inner = &tc_cap[1];
266
267 for fn_cap in FUNCTION_RE.captures_iter(tc_inner) {
268 let fn_name = fn_cap[1].trim();
269 if fn_name.is_empty() {
270 continue;
271 }
272
273 let fn_body = &fn_cap[2];
274
275 let mut args = serde_json::Map::new();
277 for param_cap in PARAM_RE.captures_iter(fn_body) {
278 let key = param_cap[1].trim().to_owned();
279 let value = param_cap[2].trim().to_owned();
280 if key.is_empty() {
281 continue;
282 }
283 let json_val =
286 serde_json::from_str::<Value>(&value).unwrap_or(Value::String(value));
287 args.insert(key, json_val);
288 }
289
290 let arguments_str = serde_json::to_string(&Value::Object(args))?;
291
292 tool_calls.push(ToolCall {
293 id: format!("call_{}", uuid::Uuid::new_v4()),
294 tool_type: "function".to_owned(),
295 function: ToolCallFunction {
296 name: fn_name.to_owned(),
297 arguments: arguments_str,
298 },
299 });
300 }
301 }
302
303 Ok(tool_calls)
304}
305
306fn extract_qwen35_xml_text_content(text: &str) -> String {
309 static TOOL_CALL_RE: Lazy<Regex> = Lazy::new(|| {
310 #[allow(clippy::unwrap_used)]
311 Regex::new(r"(?s)<tool_call>.*?</tool_call>").unwrap()
312 });
313 TOOL_CALL_RE.replace_all(text, "").trim().to_owned()
314}
315
316pub fn parse_llama_tool_calls(text: &str) -> Result<Vec<ToolCall>, anyhow::Error> {
325 let mut tool_calls = Vec::new();
326
327 let tool_text = match text.find("<|python_tag|>") {
329 Some(pos) => &text[pos + "<|python_tag|>".len()..],
330 None => return Ok(tool_calls),
331 };
332
333 for line in tool_text.lines() {
335 let trimmed = line.trim();
336 if trimmed.is_empty() || !trimmed.starts_with('{') {
337 continue;
338 }
339
340 let call_data: Value = match serde_json::from_str(trimmed) {
341 Ok(v) => v,
342 Err(_) => continue,
343 };
344
345 let name = match call_data["name"].as_str() {
346 Some(n) => n.to_owned(),
347 None => continue,
348 };
349
350 let arguments = if call_data.get("parameters").is_some() {
352 call_data["parameters"].clone()
353 } else {
354 call_data["arguments"].clone()
355 };
356 let arguments_str = serde_json::to_string(&arguments)?;
357
358 tool_calls.push(ToolCall {
359 id: format!("call_{}", uuid::Uuid::new_v4()),
360 tool_type: "function".to_owned(),
361 function: ToolCallFunction {
362 name,
363 arguments: arguments_str,
364 },
365 });
366 }
367
368 Ok(tool_calls)
369}
370
371fn extract_llama_text_content(text: &str) -> String {
373 match text.find("<|python_tag|>") {
374 Some(pos) => text[..pos].trim().to_owned(),
375 None => text.to_owned(),
376 }
377}
378
379pub fn parse_mistral_tool_calls(text: &str) -> Result<Vec<ToolCall>, anyhow::Error> {
387 let mut tool_calls = Vec::new();
388
389 let tool_text = match text.find("[TOOL_CALLS]") {
391 Some(pos) => text[pos + "[TOOL_CALLS]".len()..].trim(),
392 None => return Ok(tool_calls),
393 };
394
395 let calls: Vec<Value> = match serde_json::from_str(tool_text) {
397 Ok(v) => v,
398 Err(_) => return Ok(tool_calls),
399 };
400
401 for call_data in calls {
402 let name = match call_data["name"].as_str() {
403 Some(n) => n.to_owned(),
404 None => continue,
405 };
406
407 let arguments = call_data["arguments"].clone();
408 let arguments_str = serde_json::to_string(&arguments)?;
409
410 tool_calls.push(ToolCall {
411 id: format!("call_{}", uuid::Uuid::new_v4()),
412 tool_type: "function".to_owned(),
413 function: ToolCallFunction {
414 name,
415 arguments: arguments_str,
416 },
417 });
418 }
419
420 Ok(tool_calls)
421}
422
423fn extract_mistral_text_content(text: &str) -> String {
425 match text.find("[TOOL_CALLS]") {
426 Some(pos) => text[..pos].trim().to_owned(),
427 None => text.to_owned(),
428 }
429}
430
431#[cfg(test)]
436#[allow(clippy::unwrap_used)]
437mod tests {
438 use super::*;
439
440 #[test]
443 fn test_format_from_architecture() {
444 assert_eq!(
445 ToolCallFormat::from_architecture(&ModelArchitecture::Qwen),
446 ToolCallFormat::Qwen3Xml
447 );
448 assert_eq!(
449 ToolCallFormat::from_architecture(&ModelArchitecture::Qwen3_5),
450 ToolCallFormat::Qwen35XmlParam
451 );
452 assert_eq!(
453 ToolCallFormat::from_architecture(&ModelArchitecture::Llama),
454 ToolCallFormat::LlamaJson
455 );
456 assert_eq!(
457 ToolCallFormat::from_architecture(&ModelArchitecture::Mistral),
458 ToolCallFormat::MistralJson
459 );
460 assert_eq!(
461 ToolCallFormat::from_architecture(&ModelArchitecture::Gemma),
462 ToolCallFormat::None
463 );
464 }
465
466 #[test]
467 fn test_format_from_architecture_str() {
468 assert_eq!(
469 ToolCallFormat::from_architecture_str("qwen"),
470 ToolCallFormat::Qwen3Xml
471 );
472 assert_eq!(
473 ToolCallFormat::from_architecture_str("qwen3"),
474 ToolCallFormat::Qwen3Xml
475 );
476 assert_eq!(
477 ToolCallFormat::from_architecture_str("qwen3_5"),
478 ToolCallFormat::Qwen35XmlParam
479 );
480 assert_eq!(
481 ToolCallFormat::from_architecture_str("qwen3_5_moe"),
482 ToolCallFormat::Qwen35XmlParam
483 );
484 assert_eq!(
485 ToolCallFormat::from_architecture_str("llama3"),
486 ToolCallFormat::LlamaJson
487 );
488 assert_eq!(
489 ToolCallFormat::from_architecture_str("mistral"),
490 ToolCallFormat::MistralJson
491 );
492 assert_eq!(
493 ToolCallFormat::from_architecture_str("unknown"),
494 ToolCallFormat::None
495 );
496 }
497
498 #[test]
501 fn test_parse_tool_calls() {
502 let text = r#"Let me search for that.
503<tool_call>
504{"name": "search_web", "arguments": {"query": "rust programming"}}
505</tool_call>"#;
506
507 let calls = parse_qwen3_tool_calls(text).unwrap();
508 assert_eq!(calls.len(), 1);
509 assert_eq!(calls[0].function.name, "search_web");
510 }
511
512 #[test]
513 fn test_parse_tool_calls_nested_json() {
514 let text = r#"<tool_call>
515{"name": "execute_command", "arguments": {"command": "ls -la", "options": {"timeout": 30, "env": {"HOME": "/root"}}}}
516</tool_call>"#;
517
518 let calls = parse_qwen3_tool_calls(text).unwrap();
519 assert_eq!(calls.len(), 1);
520 assert_eq!(calls[0].function.name, "execute_command");
521 let args: serde_json::Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
522 assert_eq!(args["command"], "ls -la");
523 assert_eq!(args["options"]["timeout"], 30);
524 assert_eq!(args["options"]["env"]["HOME"], "/root");
525 }
526
527 #[test]
528 fn test_parse_multiple_tool_calls() {
529 let text = r#"I'll do both.
530<tool_call>
531{"name": "search", "arguments": {"q": "hello"}}
532</tool_call>
533<tool_call>
534{"name": "fetch", "arguments": {"url": "https://example.com"}}
535</tool_call>"#;
536
537 let calls = parse_qwen3_tool_calls(text).unwrap();
538 assert_eq!(calls.len(), 2);
539 assert_eq!(calls[0].function.name, "search");
540 assert_eq!(calls[1].function.name, "fetch");
541 }
542
543 #[test]
544 fn test_parse_tool_calls_duplicate_opening_tag() {
545 let text = "<tool_call>\n\n<tool_call>\n{\"name\": \"search_web\", \"arguments\": {\"query\": \"Windermere\"}}\n</tool_call>";
546
547 let calls = parse_qwen3_tool_calls(text).unwrap();
548 assert_eq!(calls.len(), 1);
549 assert_eq!(calls[0].function.name, "search_web");
550 }
551
552 #[test]
553 fn test_extract_text() {
554 let text = r#"Here is the answer: <tool_call>
555{"name": "test", "arguments": {}}
556</tool_call> More text."#;
557
558 let extracted = extract_text_content(text);
559 assert!(!extracted.contains("<tool_call>"));
560 assert!(extracted.contains("Here is the answer:"));
561 }
562
563 #[test]
566 fn test_qwen35_xml_basic() {
567 let text = r#"<tool_call>
568<function=get_weather>
569<parameter=location>NYC</parameter>
570<parameter=unit>celsius</parameter>
571</function>
572</tool_call>"#;
573
574 assert!(has_qwen35_xml_tool_calls(text));
575 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
576 assert_eq!(calls.len(), 1);
577 assert_eq!(calls[0].tool_type, "function");
578 assert_eq!(calls[0].function.name, "get_weather");
579 assert!(calls[0].id.starts_with("call_"));
580
581 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
582 assert_eq!(args["location"], "NYC");
583 assert_eq!(args["unit"], "celsius");
584 }
585
586 #[test]
587 fn test_qwen35_xml_multiple_tool_calls() {
588 let text = r#"I'll check both locations.
589
590<tool_call>
591<function=get_weather>
592<parameter=location>NYC</parameter>
593</function>
594</tool_call>
595
596<tool_call>
597<function=get_weather>
598<parameter=location>LA</parameter>
599</function>
600</tool_call>"#;
601
602 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
603 assert_eq!(calls.len(), 2);
604 assert_eq!(calls[0].function.name, "get_weather");
605 assert_eq!(calls[1].function.name, "get_weather");
606 assert_ne!(calls[0].id, calls[1].id, "IDs must be unique");
607
608 let args0: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
609 let args1: Value = serde_json::from_str(&calls[1].function.arguments).unwrap();
610 assert_eq!(args0["location"], "NYC");
611 assert_eq!(args1["location"], "LA");
612 }
613
614 #[test]
615 fn test_qwen35_xml_multiline_value() {
616 let text = r#"<tool_call>
617<function=execute_code>
618<parameter=code>
619def hello():
620 print("hello world")
621 return 42
622</parameter>
623</function>
624</tool_call>"#;
625
626 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
627 assert_eq!(calls.len(), 1);
628 assert_eq!(calls[0].function.name, "execute_code");
629
630 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
631 let code = args["code"].as_str().unwrap();
632 assert!(code.contains("def hello():"));
633 assert!(code.contains("return 42"));
634 }
635
636 #[test]
637 fn test_qwen35_xml_numeric_and_bool_values() {
638 let text = r#"<tool_call>
639<function=configure>
640<parameter=timeout>30</parameter>
641<parameter=verbose>true</parameter>
642<parameter=ratio>0.75</parameter>
643<parameter=name>my-config</parameter>
644</function>
645</tool_call>"#;
646
647 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
648 assert_eq!(calls.len(), 1);
649
650 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
651 assert_eq!(args["timeout"], 30);
652 assert_eq!(args["verbose"], true);
653 assert_eq!(args["ratio"], 0.75);
654 assert_eq!(args["name"], "my-config");
655 }
656
657 #[test]
658 fn test_qwen35_xml_json_object_value() {
659 let text = r#"<tool_call>
660<function=create_item>
661<parameter=data>{"key": "value", "nested": [1,2,3]}</parameter>
662</function>
663</tool_call>"#;
664
665 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
666 assert_eq!(calls.len(), 1);
667
668 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
669 assert_eq!(args["data"]["key"], "value");
670 assert_eq!(
671 args["data"]["nested"],
672 Value::Array(vec![Value::from(1), Value::from(2), Value::from(3),])
673 );
674 }
675
676 #[test]
677 fn test_qwen35_xml_no_parameters() {
678 let text = r#"<tool_call>
679<function=get_time>
680</function>
681</tool_call>"#;
682
683 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
684 assert_eq!(calls.len(), 1);
685 assert_eq!(calls[0].function.name, "get_time");
686
687 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
688 assert!(args.as_object().unwrap().is_empty());
689 }
690
691 #[test]
692 fn test_qwen35_xml_inline_parameter_values() {
693 let text = "<tool_call>\n<function=search>\n<parameter=query>rust programming</parameter>\n</function>\n</tool_call>";
695
696 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
697 assert_eq!(calls.len(), 1);
698 assert_eq!(calls[0].function.name, "search");
699
700 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
701 assert_eq!(args["query"], "rust programming");
702 }
703
704 #[test]
705 fn test_qwen35_xml_whitespace_trimming() {
706 let text = r#"<tool_call>
707<function=greet>
708<parameter=name>
709 Alice
710</parameter>
711</function>
712</tool_call>"#;
713
714 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
715 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
716 assert_eq!(args["name"], "Alice");
717 }
718
719 #[test]
720 fn test_qwen35_xml_no_tool_calls() {
721 let text = "Just a normal response with no tool calls.";
722 assert!(!has_qwen35_xml_tool_calls(text));
723
724 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
725 assert!(calls.is_empty());
726 }
727
728 #[test]
729 fn test_qwen35_xml_malformed_no_function() {
730 let text = "<tool_call>\nsome garbage\n</tool_call>";
732 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
733 assert!(calls.is_empty());
734 }
735
736 #[test]
737 fn test_qwen35_xml_malformed_unclosed_tool_call() {
738 let text = "<tool_call>\n<function=test>\n<parameter=x>1</parameter>\n</function>";
740 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
741 assert!(calls.is_empty());
742 }
743
744 #[test]
745 fn test_qwen35_xml_text_before_and_after() {
746 let text = r#"Let me look that up for you.
747
748<tool_call>
749<function=search_web>
750<parameter=query>Rust language</parameter>
751</function>
752</tool_call>
753
754I'll have the results shortly."#;
755
756 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
757 assert_eq!(calls.len(), 1);
758 assert_eq!(calls[0].function.name, "search_web");
759
760 let extracted = extract_qwen35_xml_text_content(text);
761 assert!(extracted.contains("Let me look that up"));
762 assert!(extracted.contains("results shortly"));
763 assert!(!extracted.contains("<tool_call>"));
764 assert!(!extracted.contains("<function="));
765 }
766
767 #[test]
768 fn test_qwen35_xml_extract_text_no_tool_calls() {
769 let text = "No tool calls here.";
770 let extracted = extract_qwen35_xml_text_content(text);
771 assert_eq!(extracted, "No tool calls here.");
772 }
773
774 #[test]
775 fn test_qwen35_xml_detection_distinguishes_from_qwen3() {
776 let qwen3 =
778 "<tool_call>\n{\"name\": \"search\", \"arguments\": {\"q\": \"hello\"}}\n</tool_call>";
779 assert!(
780 !has_qwen35_xml_tool_calls(qwen3),
781 "Qwen3 JSON format should not trigger Qwen3.5 detector"
782 );
783
784 let qwen35 = "<tool_call>\n<function=search>\n<parameter=q>hello</parameter>\n</function>\n</tool_call>";
786 assert!(has_qwen35_xml_tool_calls(qwen35));
787 }
788
789 #[test]
790 fn test_qwen35_xml_round_trip() {
791 let model_output = "I'll check the weather for you.\n\n\
792 <tool_call>\n\
793 <function=get_weather>\n\
794 <parameter=location>NYC</parameter>\n\
795 <parameter=unit>fahrenheit</parameter>\n\
796 </function>\n\
797 </tool_call>";
798
799 assert!(has_tool_calls_for_format(
801 ToolCallFormat::Qwen35XmlParam,
802 model_output
803 ));
804
805 let calls =
807 parse_tool_calls_for_format(ToolCallFormat::Qwen35XmlParam, model_output).unwrap();
808 assert_eq!(calls.len(), 1);
809 assert_eq!(calls[0].function.name, "get_weather");
810 assert!(calls[0].id.starts_with("call_"));
811 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
812 assert_eq!(args["location"], "NYC");
813 assert_eq!(args["unit"], "fahrenheit");
814
815 let text = extract_text_content_for_format(ToolCallFormat::Qwen35XmlParam, model_output);
817 assert!(text.contains("I'll check the weather"));
818 assert!(!text.contains("<tool_call>"));
819 assert!(!text.contains("<function="));
820 }
821
822 #[test]
823 fn test_qwen35_xml_dispatch_has_tool_calls() {
824 let qwen35_text = "<tool_call>\n<function=test>\n</function>\n</tool_call>";
825 assert!(has_tool_calls_for_format(
826 ToolCallFormat::Qwen35XmlParam,
827 qwen35_text
828 ));
829 assert!(!has_tool_calls_for_format(
830 ToolCallFormat::None,
831 qwen35_text
832 ));
833 }
836
837 #[test]
838 fn test_qwen35_xml_different_functions_in_one_response() {
839 let text = r#"<tool_call>
840<function=get_weather>
841<parameter=location>NYC</parameter>
842</function>
843</tool_call>
844
845<tool_call>
846<function=search_web>
847<parameter=query>restaurants near Times Square</parameter>
848</function>
849</tool_call>"#;
850
851 let calls = parse_qwen35_xml_tool_calls(text).unwrap();
852 assert_eq!(calls.len(), 2);
853 assert_eq!(calls[0].function.name, "get_weather");
854 assert_eq!(calls[1].function.name, "search_web");
855
856 let args1: Value = serde_json::from_str(&calls[1].function.arguments).unwrap();
857 assert_eq!(args1["query"], "restaurants near Times Square");
858 }
859
860 #[test]
863 fn test_parse_llama_tool_calls() {
864 let text =
865 "<|python_tag|>{\"name\": \"get_weather\", \"parameters\": {\"city\": \"London\"}}";
866
867 let calls = parse_llama_tool_calls(text).unwrap();
868 assert_eq!(calls.len(), 1);
869 assert_eq!(calls[0].function.name, "get_weather");
870 let args: Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
871 assert_eq!(args["city"], "London");
872 }
873
874 #[test]
875 fn test_llama_has_tool_calls() {
876 assert!(has_tool_calls_for_format(
877 ToolCallFormat::LlamaJson,
878 "text <|python_tag|>{...}"
879 ));
880 assert!(!has_tool_calls_for_format(
881 ToolCallFormat::LlamaJson,
882 "no tool calls here"
883 ));
884 }
885
886 #[test]
887 fn test_llama_extract_text() {
888 let text = "Here is my answer.<|python_tag|>{\"name\": \"test\", \"parameters\": {}}";
889 let extracted = extract_llama_text_content(text);
890 assert_eq!(extracted, "Here is my answer.");
891 }
892
893 #[test]
896 fn test_parse_mistral_tool_calls() {
897 let text = "[TOOL_CALLS] [{\"name\": \"search\", \"arguments\": {\"query\": \"rust\"}}]";
898
899 let calls = parse_mistral_tool_calls(text).unwrap();
900 assert_eq!(calls.len(), 1);
901 assert_eq!(calls[0].function.name, "search");
902 }
903
904 #[test]
905 fn test_mistral_has_tool_calls() {
906 assert!(has_tool_calls_for_format(
907 ToolCallFormat::MistralJson,
908 "[TOOL_CALLS] [...]"
909 ));
910 assert!(!has_tool_calls_for_format(
911 ToolCallFormat::MistralJson,
912 "no tools"
913 ));
914 }
915
916 #[test]
917 fn test_mistral_extract_text() {
918 let text = "Let me help.[TOOL_CALLS] [{\"name\": \"test\", \"arguments\": {}}]";
919 let extracted = extract_mistral_text_content(text);
920 assert_eq!(extracted, "Let me help.");
921 }
922
923 #[test]
926 fn test_dispatch_has_tool_calls() {
927 assert!(has_tool_calls_for_format(
928 ToolCallFormat::Qwen3Xml,
929 "<tool_call>...</tool_call>"
930 ));
931 assert!(!has_tool_calls_for_format(
932 ToolCallFormat::None,
933 "<tool_call>...</tool_call>"
934 ));
935 }
936
937 #[test]
940 fn test_tool_call_round_trip_qwen3() {
941 let model_output = "I'll check the weather for you.\n\n<tool_call>\n{\"name\": \"get_weather\", \"arguments\": {\"location\": \"NYC\"}}\n</tool_call>";
943
944 assert!(has_tool_calls_for_format(
946 ToolCallFormat::Qwen3Xml,
947 model_output
948 ));
949
950 let calls = parse_tool_calls_for_format(ToolCallFormat::Qwen3Xml, model_output).unwrap();
952 assert_eq!(calls.len(), 1);
953 assert_eq!(calls[0].function.name, "get_weather");
954 assert!(
955 calls[0].id.starts_with("call_"),
956 "ID should be auto-generated"
957 );
958 let args: serde_json::Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
959 assert_eq!(args["location"], "NYC");
960
961 let text = extract_text_content_for_format(ToolCallFormat::Qwen3Xml, model_output);
963 assert!(text.contains("I'll check the weather"));
964 assert!(!text.contains("<tool_call>"));
965
966 let tool_response = r#"{"temperature": 72, "condition": "sunny"}"#;
969 let call_id = calls[0].id.clone();
970
971 assert_eq!(calls[0].function.name, "get_weather");
973 assert!(!call_id.is_empty(), "tool call ID should be non-empty");
974 assert!(!tool_response.is_empty());
975 assert!(text.contains("I'll check the weather"));
977 assert!(!text.contains("<tool_call>"));
978 }
979
980 #[test]
981 fn test_tool_call_round_trip_llama() {
982 let model_output = "<|python_tag|>\n{\"name\": \"get_weather\", \"parameters\": {\"location\": \"Tokyo\"}}";
984
985 assert!(has_tool_calls_for_format(
986 ToolCallFormat::LlamaJson,
987 model_output
988 ));
989
990 let calls = parse_tool_calls_for_format(ToolCallFormat::LlamaJson, model_output).unwrap();
991 assert_eq!(calls.len(), 1);
992 assert_eq!(calls[0].function.name, "get_weather");
993
994 let args: serde_json::Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
996 assert_eq!(args["location"], "Tokyo");
997 }
998
999 #[test]
1000 fn test_tool_call_round_trip_mistral() {
1001 let model_output = "Let me check.\n[TOOL_CALLS][{\"name\": \"get_weather\", \"arguments\": {\"location\": \"London\"}}]";
1003
1004 assert!(has_tool_calls_for_format(
1005 ToolCallFormat::MistralJson,
1006 model_output
1007 ));
1008
1009 let calls = parse_tool_calls_for_format(ToolCallFormat::MistralJson, model_output).unwrap();
1010 assert_eq!(calls.len(), 1);
1011 assert_eq!(calls[0].function.name, "get_weather");
1012
1013 let text = extract_text_content_for_format(ToolCallFormat::MistralJson, model_output);
1014 assert_eq!(text, "Let me check.");
1015 }
1016
1017 #[test]
1018 fn test_multiple_tool_calls_in_single_response() {
1019 let model_output = "I'll check both locations.\n\n\
1020 <tool_call>\n{\"name\": \"get_weather\", \"arguments\": {\"location\": \"NYC\"}}\n</tool_call>\n\
1021 <tool_call>\n{\"name\": \"get_weather\", \"arguments\": {\"location\": \"LA\"}}\n</tool_call>";
1022
1023 let calls = parse_tool_calls_for_format(ToolCallFormat::Qwen3Xml, model_output).unwrap();
1024 assert_eq!(calls.len(), 2);
1025 assert_eq!(calls[0].function.name, "get_weather");
1026 assert_eq!(calls[1].function.name, "get_weather");
1027
1028 assert_ne!(calls[0].id, calls[1].id);
1030
1031 let args0: serde_json::Value = serde_json::from_str(&calls[0].function.arguments).unwrap();
1033 let args1: serde_json::Value = serde_json::from_str(&calls[1].function.arguments).unwrap();
1034 assert_eq!(args0["location"], "NYC");
1035 assert_eq!(args1["location"], "LA");
1036 }
1037
1038 #[test]
1039 fn test_no_tool_calls_when_format_none() {
1040 let text_with_markers = "Here's an example: <tool_call>{\"name\": \"test\"}</tool_call>";
1042
1043 assert!(!has_tool_calls_for_format(
1044 ToolCallFormat::None,
1045 text_with_markers
1046 ));
1047
1048 let result = parse_tool_calls_for_format(ToolCallFormat::None, text_with_markers);
1050 if let Ok(calls) = result {
1051 assert!(calls.is_empty());
1052 }
1053 }
1054
1055 #[test]
1058 fn test_from_model_ref() {
1059 assert_eq!(
1060 ToolCallFormat::from_model_ref("qwen3.5-9b:main"),
1061 ToolCallFormat::Qwen35XmlParam
1062 );
1063 assert_eq!(
1064 ToolCallFormat::from_model_ref("Qwen3.5-9B-FP8:main"),
1065 ToolCallFormat::Qwen35XmlParam
1066 );
1067 assert_eq!(
1068 ToolCallFormat::from_model_ref("qwen3-8b:main"),
1069 ToolCallFormat::Qwen3Xml
1070 );
1071 assert_eq!(
1072 ToolCallFormat::from_model_ref("qwen2.5-72b:main"),
1073 ToolCallFormat::Qwen3Xml
1074 );
1075 assert_eq!(
1076 ToolCallFormat::from_model_ref("llama-3.1-8b:main"),
1077 ToolCallFormat::LlamaJson
1078 );
1079 assert_eq!(
1080 ToolCallFormat::from_model_ref("mistral-7b:main"),
1081 ToolCallFormat::MistralJson
1082 );
1083 assert_eq!(
1084 ToolCallFormat::from_model_ref("mixtral-8x7b:main"),
1085 ToolCallFormat::MistralJson
1086 );
1087 assert_eq!(
1088 ToolCallFormat::from_model_ref("gemma-2b:main"),
1089 ToolCallFormat::None
1090 );
1091 assert_eq!(
1092 ToolCallFormat::from_model_ref("unknown-model"),
1093 ToolCallFormat::None
1094 );
1095 }
1096}