Skip to main content

hyprstream_core/api/
tools.rs

1//! Tool calling: format detection and per-model response parsing.
2//!
3//! Prompt formatting is handled by HuggingFace chat templates (the `tools` variable
4//! is passed to the template engine). This module handles the *output* side:
5//! detecting and parsing tool calls from model responses in various formats.
6
7use 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// =============================================================================
15// ToolCallFormat — enum-dispatched per-model parsing
16// =============================================================================
17
18/// Tool-call output format used by a model family.
19///
20/// Selected from the model's architecture string at request time.
21/// Parsing is stateless (free functions), so an enum + match is simpler than a trait.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum ToolCallFormat {
24    /// Qwen3 XML: `<tool_call>{"name":…,"arguments":…}</tool_call>`
25    Qwen3Xml,
26    /// Qwen3.5 XML parameter format:
27    /// `<tool_call><function=NAME><parameter=KEY>value</parameter>…</function></tool_call>`
28    Qwen35XmlParam,
29    /// Llama 3.1+: `<|python_tag|>` prefix + JSON `{"name":…,"parameters":…}`
30    LlamaJson,
31    /// Mistral: `[TOOL_CALLS]` prefix + JSON array
32    MistralJson,
33    /// Model does not support tool calling.
34    None,
35}
36
37impl ToolCallFormat {
38    /// Select the tool-call format from the model architecture.
39    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    /// Select the tool-call format from an architecture name string.
52    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    /// Infer the tool-call format from a model reference string (e.g. "qwen3.5-9b:main").
63    ///
64    /// Uses substring matching against known model family names. Falls back to `None`
65    /// if the model family cannot be determined from the name.
66    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
86// =============================================================================
87// Format-aware dispatch functions
88// =============================================================================
89
90/// Check if text contains tool calls in the given format.
91pub 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
101/// Parse tool calls from model output using the given format.
102pub 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
115/// Extract text content from response, removing tool call markers for the given format.
116pub 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
126// =============================================================================
127// Legacy API (backwards-compatible, delegates to Qwen3 format)
128// =============================================================================
129
130/// Check if text contains Qwen3 tool calls.
131///
132/// Kept for backwards compatibility. Prefer `has_tool_calls_for_format()`.
133pub fn has_tool_calls(text: &str) -> bool {
134    has_tool_calls_for_format(ToolCallFormat::Qwen3Xml, text)
135}
136
137/// Extract text content from response, removing Qwen3 tool call XML tags.
138///
139/// Kept for backwards compatibility. Prefer `extract_text_content_for_format()`.
140pub fn extract_text_content(text: &str) -> String {
141    extract_qwen3_text_content(text)
142}
143
144// =============================================================================
145// Qwen3 XML parser
146// =============================================================================
147
148/// Parse Qwen3 XML tool calls from model output and convert to OpenAI format.
149///
150/// Uses delimiter-based extraction instead of a single regex so that nested
151/// JSON braces inside `arguments` are handled correctly.  Handles malformed
152/// output where the model emits duplicate `<tool_call>` tags by searching
153/// backward from `</tool_call>` to find the nearest `<tool_call>` (and also
154/// skipping blocks that don't contain valid JSON).
155pub 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
204/// Extract text content from Qwen3 response, removing `<tool_call>…</tool_call>` blocks.
205fn 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
213// =============================================================================
214// Qwen3.5 XML parameter parser
215// =============================================================================
216
217/// Detect whether text contains Qwen3.5 XML parameter-style tool calls.
218///
219/// The format uses `<tool_call>` blocks containing `<function=NAME>` tags with
220/// `<parameter=KEY>value</parameter>` children, as opposed to the Qwen3 format
221/// which embeds a JSON object inside `<tool_call>`.
222///
223/// Detection requires both `<tool_call>` and `<function=` to disambiguate from
224/// Qwen3's JSON-in-XML format.
225pub fn has_qwen35_xml_tool_calls(text: &str) -> bool {
226    text.contains("<tool_call>") && text.contains("<function=")
227}
228
229/// Parse Qwen3.5 XML parameter-style tool calls from model output.
230///
231/// # Format
232///
233/// ```text
234/// <tool_call>
235/// <function=get_weather>
236/// <parameter=location>NYC</parameter>
237/// <parameter=unit>celsius</parameter>
238/// </function>
239/// </tool_call>
240/// ```
241///
242/// Multiple `<tool_call>` blocks may appear in a single response.  Parameter
243/// values can span multiple lines and are whitespace-trimmed.  Malformed blocks
244/// (missing function name, unparseable parameters) are silently skipped.
245///
246/// Returns an empty `Vec` (not an error) when no tool calls are found.
247pub fn parse_qwen35_xml_tool_calls(text: &str) -> Result<Vec<ToolCall>, anyhow::Error> {
248    // Regexes compiled once and cached.
249    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            // Collect parameter key-value pairs into a JSON object.
276            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                // Try to parse value as JSON first (numbers, booleans, objects,
284                // arrays).  Fall back to a plain JSON string on failure.
285                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
306/// Extract text content from a Qwen3.5 XML parameter response, removing all
307/// `<tool_call>…</tool_call>` blocks.
308fn 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
316// =============================================================================
317// Llama JSON parser
318// =============================================================================
319
320/// Parse Llama 3.1+ tool calls.
321///
322/// Format: `<|python_tag|>{"name": "func", "parameters": {...}}`
323/// May contain multiple JSON objects separated by newlines.
324pub fn parse_llama_tool_calls(text: &str) -> Result<Vec<ToolCall>, anyhow::Error> {
325    let mut tool_calls = Vec::new();
326
327    // Find everything after <|python_tag|>
328    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    // Try parsing each line as a JSON tool call
334    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        // Llama uses "parameters" instead of "arguments"
351        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
371/// Extract text content from Llama response, removing everything after `<|python_tag|>`.
372fn 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
379// =============================================================================
380// Mistral JSON parser
381// =============================================================================
382
383/// Parse Mistral tool calls.
384///
385/// Format: `[TOOL_CALLS] [{"name": "func", "arguments": {...}}]`
386pub fn parse_mistral_tool_calls(text: &str) -> Result<Vec<ToolCall>, anyhow::Error> {
387    let mut tool_calls = Vec::new();
388
389    // Find everything after [TOOL_CALLS]
390    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    // Try parsing as a JSON array
396    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
423/// Extract text content from Mistral response, removing everything after `[TOOL_CALLS]`.
424fn 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// =============================================================================
432// Tests
433// =============================================================================
434
435#[cfg(test)]
436#[allow(clippy::unwrap_used)]
437mod tests {
438    use super::*;
439
440    // --- ToolCallFormat selection ---
441
442    #[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    // --- Qwen3 XML tests (existing, preserved) ---
499
500    #[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    // --- Qwen3.5 XML parameter format tests ---
564
565    #[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        // Model might emit values on the same line (no newlines inside parameter tags)
694        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        // <tool_call> block with no <function=...> — should be skipped
731        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        // Unclosed <tool_call> — regex won't match, returns empty
739        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        // Qwen3 format (JSON inside tool_call) should NOT match the Qwen3.5 detector
777        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        // Qwen3.5 format should match
785        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        // 1. Detect
800        assert!(has_tool_calls_for_format(
801            ToolCallFormat::Qwen35XmlParam,
802            model_output
803        ));
804
805        // 2. Parse
806        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        // 3. Extract text
816        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        // Qwen3Xml also matches on <tool_call> but that's by design (format is
834        // selected at request time from the model architecture, not auto-detected).
835    }
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    // --- Llama JSON tests ---
861
862    #[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    // --- Mistral JSON tests ---
894
895    #[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    // --- Format-aware dispatch tests ---
924
925    #[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    // --- Tool call round-trip tests ---
938
939    #[test]
940    fn test_tool_call_round_trip_qwen3() {
941        // Simulate model response with tool call
942        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        // 1. Detect tool calls
945        assert!(has_tool_calls_for_format(
946            ToolCallFormat::Qwen3Xml,
947            model_output
948        ));
949
950        // 2. Parse tool calls
951        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        // 3. Extract text content (without tool markers)
962        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        // 4. Build next-turn messages (simulating client constructing history)
967        // Verify the parsed tool call can be used to construct the next turn
968        let tool_response = r#"{"temperature": 72, "condition": "sunny"}"#;
969        let call_id = calls[0].id.clone();
970
971        // Verify the conversation structure pieces are correct
972        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        // Assistant message should have no text content (tool-call-only)
976        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        // Llama 3.1 uses <|python_tag|> format with "parameters" field
983        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        // Llama uses "parameters" not "arguments" — verify it's normalized to arguments
995        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        // Mistral uses [TOOL_CALLS] JSON array format
1002        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        // Each call should have a unique ID
1029        assert_ne!(calls[0].id, calls[1].id);
1030
1031        // Verify different arguments
1032        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        // Even if the text contains tool call markers, ToolCallFormat::None should not detect them
1041        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        // parse should return empty or error
1049        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    // --- from_model_ref tests ---
1056
1057    #[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}