parsers.rs 124 KB
Newer Older
1
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
// SPDX-License-Identifier: Apache-2.0

4
use super::ToolDefinition;
5
use super::config::{ParserConfig, ToolCallConfig};
6
7
8
use super::dsml::{
    detect_tool_call_start_dsml, find_tool_call_end_position_dsml, try_tool_call_parse_dsml,
};
9
10
11
12
13
14
15
16
17
18
19
use super::harmony::{
    detect_tool_call_start_harmony, find_tool_call_end_position_harmony,
    parse_tool_calls_harmony_complete,
};
use super::json::{
    detect_tool_call_start_json, find_tool_call_end_position_json, try_tool_call_parse_json,
};
use super::pythonic::{
    detect_tool_call_start_pythonic, find_tool_call_end_position_pythonic,
    try_tool_call_parse_pythonic,
};
20
use super::response::ToolCallResponse;
21
use super::xml::{
22
23
    detect_tool_call_start_glm47, detect_tool_call_start_xml, find_tool_call_end_position_glm47,
    find_tool_call_end_position_xml, try_tool_call_parse_glm47, try_tool_call_parse_xml,
24
};
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::collections::HashMap;
use std::sync::OnceLock;

static PARSER_MAP: OnceLock<HashMap<&'static str, ToolCallConfig>> = OnceLock::new();

// Always update this parsermap when adding a new parser
pub fn get_tool_parser_map() -> &'static HashMap<&'static str, ToolCallConfig> {
    PARSER_MAP.get_or_init(|| {
        let mut map = HashMap::new();
        map.insert("hermes", ToolCallConfig::hermes());
        map.insert("nemotron_deci", ToolCallConfig::nemotron_deci());
        map.insert("llama3_json", ToolCallConfig::llama3_json());
        map.insert("mistral", ToolCallConfig::mistral());
        map.insert("phi4", ToolCallConfig::phi4());
        map.insert("pythonic", ToolCallConfig::pythonic());
        map.insert("harmony", ToolCallConfig::harmony());
41
        map.insert("deepseek_v3", ToolCallConfig::deepseek_v3());
42
        map.insert("deepseek_v3_1", ToolCallConfig::deepseek_v3_1());
43
        map.insert("deepseek_v3_2", ToolCallConfig::deepseek_v3_2());
44
        map.insert("qwen3_coder", ToolCallConfig::qwen3_coder());
45
        map.insert("jamba", ToolCallConfig::jamba());
46
        map.insert("minimax_m2", ToolCallConfig::minimax_m2());
47
        map.insert("glm47", ToolCallConfig::glm47());
48
        map.insert("default", ToolCallConfig::default());
49
        map.insert("nemotron_nano", ToolCallConfig::qwen3_coder()); // nemotron nano follows qwen3_coder format
50
51
52
53
54
55
56
        map
    })
}

pub fn get_available_tool_parsers() -> Vec<&'static str> {
    get_tool_parser_map().keys().copied().collect()
}
57

58
pub async fn try_tool_call_parse(
59
60
    message: &str,
    config: &ToolCallConfig,
61
    tools: Option<&[ToolDefinition]>,
62
) -> anyhow::Result<(Vec<ToolCallResponse>, Option<String>)> {
63
    // Use match statement (Rust's switch statement) to call the appropriate parser
64
65
    match &config.parser_config {
        ParserConfig::Json(json_config) => {
66
            let (results, normal_content) = try_tool_call_parse_json(message, json_config, tools)?;
67
68
            Ok((results, normal_content))
        }
69
        ParserConfig::Harmony(json_config) => {
70
            let (results, normal_content) =
71
                parse_tool_calls_harmony_complete(message, json_config, tools).await?;
72
            Ok((results, normal_content))
73
        }
74
        ParserConfig::Pythonic => {
75
            let (results, normal_content) = try_tool_call_parse_pythonic(message, tools)?;
76
            Ok((results, normal_content))
77
        }
78
        ParserConfig::Typescript => {
79
80
            anyhow::bail!("Typescript parser not implemented");
        }
81
        ParserConfig::Xml(xml_config) => {
82
            let (results, normal_content) = try_tool_call_parse_xml(message, xml_config, tools)?;
83
            Ok((results, normal_content))
84
        }
85
86
87
88
        ParserConfig::Dsml(dsml_config) => {
            let (results, normal_content) = try_tool_call_parse_dsml(message, dsml_config)?;
            Ok((results, normal_content))
        }
89
90
91
92
93
        ParserConfig::Glm47(glm47_config) => {
            let (results, normal_content) =
                try_tool_call_parse_glm47(message, glm47_config, tools)?;
            Ok((results, normal_content))
        }
94
95
96
    }
}

97
// Base Detector to call for all tool parsing
98
pub async fn detect_and_parse_tool_call(
99
100
    message: &str,
    parser_str: Option<&str>,
101
    tools: Option<&[ToolDefinition]>,
102
) -> anyhow::Result<(Vec<ToolCallResponse>, Option<String>)> {
103
104
    // Get the tool parser map
    let parser_map = get_tool_parser_map();
105
106
107
108
109
110
111
112

    // Handle None or empty string by defaulting to "default"
    let parser_key = match parser_str {
        Some(s) if !s.is_empty() => s,
        _ => "default", // None or empty string
    };

    match parser_map.get(parser_key) {
113
        Some(config) => {
114
            let (results, normal_content) = try_tool_call_parse(message, config, tools).await?;
115
116
            Ok((results, normal_content))
        }
117
118
119
120
121
        None => anyhow::bail!(
            "Parser '{}' is not implemented. Available parsers: {:?}",
            parser_key,
            get_available_tool_parsers()
        ),
122
123
124
    }
}

125
126
127
128
129
130
131
132
pub fn detect_tool_call_start(chunk: &str, parser_str: Option<&str>) -> anyhow::Result<bool> {
    let parser_map = get_tool_parser_map();
    let parser_key = match parser_str {
        Some(s) if !s.is_empty() => s,
        _ => "default", // None or empty string
    };

    match parser_map.get(parser_key) {
133
134
135
136
        Some(config) => match &config.parser_config {
            ParserConfig::Json(json_config) => Ok(detect_tool_call_start_json(chunk, json_config)),
            ParserConfig::Harmony(json_config) => {
                Ok(detect_tool_call_start_harmony(chunk, json_config, false))
137
            }
138
139
            ParserConfig::Pythonic => Ok(detect_tool_call_start_pythonic(chunk)),
            ParserConfig::Typescript => {
140
141
                anyhow::bail!("Typescript parser not implemented");
            }
142
            ParserConfig::Xml(xml_config) => Ok(detect_tool_call_start_xml(chunk, xml_config)),
143
            ParserConfig::Dsml(dsml_config) => Ok(detect_tool_call_start_dsml(chunk, dsml_config)),
144
145
146
            ParserConfig::Glm47(glm47_config) => {
                Ok(detect_tool_call_start_glm47(chunk, glm47_config))
            }
147
148
149
150
151
152
153
154
155
        },
        None => anyhow::bail!(
            "Parser '{}' is not implemented. Available parsers: {:?}",
            parser_key,
            get_available_tool_parsers()
        ),
    }
}

156
157
158
159
160
161
162
163
pub fn find_tool_call_end_position(chunk: &str, parser_str: Option<&str>) -> usize {
    let parser_map = get_tool_parser_map();
    let parser_key = match parser_str {
        Some(s) if !s.is_empty() => s,
        _ => "default",
    };

    match parser_map.get(parser_key) {
164
165
        Some(config) => match &config.parser_config {
            ParserConfig::Json(json_config) => {
166
167
168
169
170
171
                // For "default", use "nemotron_deci" as the effective parser; otherwise, use the provided parser_key
                let effective_parser = if parser_key == "default" {
                    "nemotron_deci"
                } else {
                    parser_key
                };
172
                find_tool_call_end_position_json(chunk, effective_parser, json_config)
173
            }
174
175
176
177
178
            ParserConfig::Harmony(json_config) => {
                find_tool_call_end_position_harmony(chunk, json_config)
            }
            ParserConfig::Pythonic => find_tool_call_end_position_pythonic(chunk),
            ParserConfig::Typescript => {
179
180
181
                // Typescript parser not implemented
                chunk.len()
            }
182
            ParserConfig::Xml(xml_config) => find_tool_call_end_position_xml(chunk, xml_config),
183
            ParserConfig::Dsml(dsml_config) => find_tool_call_end_position_dsml(chunk, dsml_config),
184
185
186
            ParserConfig::Glm47(glm47_config) => {
                find_tool_call_end_position_glm47(chunk, glm47_config)
            }
187
188
189
190
191
192
193
        },
        None => {
            // Unknown parser, return full content length
            chunk.len()
        }
    }
}
194
195
196
197
// Tests
// cargo test postprocessor::tool_calling::parsers
#[cfg(test)]
mod tests {
198
    use super::super::config::JsonParserConfig;
199
200
201
202
203
204
205
    use super::*;

    fn extract_name_and_args(call: ToolCallResponse) -> (String, serde_json::Value) {
        let args: serde_json::Value = serde_json::from_str(&call.function.arguments).unwrap();
        (call.function.name, args)
    }

206
207
208
209
210
211
212
213
214
215
216
217
218
219
    #[test]
    fn test_get_available_tool_parsers() {
        let parsers = get_available_tool_parsers();
        assert!(!parsers.is_empty());
        // Update this list when adding a new parser
        let available_parsers = [
            "hermes",
            "llama3_json",
            "harmony",
            "nemotron_deci",
            "mistral",
            "phi4",
            "default",
            "pythonic",
220
            "deepseek_v3",
221
            "deepseek_v3_1",
222
            "deepseek_v3_2",
223
            "qwen3_coder",
224
            "jamba",
225
            "nemotron_nano",
226
            "minimax_m2",
227
            "glm47",
228
229
230
231
232
233
        ];
        for parser in available_parsers {
            assert!(parsers.contains(&parser));
        }
    }

234
235
    #[tokio::test]
    async fn parses_single_parameters_object() {
236
        let input = r#"{ "name": "hello", "parameters": { "x": 1, "y": 2 } }"#;
237
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::default(), None)
238
239
            .await
            .unwrap();
240
        assert_eq!(content, Some("".to_string()));
241
242
243
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
244
245
246
247
248
        assert_eq!(name, "hello");
        assert_eq!(args["x"], 1);
        assert_eq!(args["y"], 2);
    }

249
250
    #[tokio::test]
    async fn parses_single_arguments_object() {
251
        let input = r#"{ "name": "world", "arguments": { "a": "abc", "b": 42 } }"#;
252
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::default(), None)
253
254
            .await
            .unwrap();
255
        assert_eq!(content, Some("".to_string()));
256
257
258
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
259
260
261
262
263
        assert_eq!(name, "world");
        assert_eq!(args["a"], "abc");
        assert_eq!(args["b"], 42);
    }

264
265
    #[tokio::test]
    async fn parses_vec_of_parameters() {
266
        let input = r#"[{ "name": "first", "parameters": { "a": 1 } }, { "name": "second", "parameters": { "b": 2 } }]"#;
267
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::default(), None)
268
269
            .await
            .unwrap();
270
        assert_eq!(content, Some("".to_string()));
271
272
273
274
275
276
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "first");
        assert_eq!(args["a"], 1);
        let (name, args) = extract_name_and_args(result[1].clone());
277
278
279
280
        assert_eq!(name, "second");
        assert_eq!(args["b"], 2);
    }

281
282
    #[tokio::test]
    async fn parses_vec_of_arguments() {
283
        let input = r#"[{ "name": "alpha", "arguments": { "a": "x" } }, { "name": "omega", "arguments": { "z": "y" } }]"#;
284
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::default(), None)
285
286
            .await
            .unwrap();
287
        assert_eq!(content, Some("".to_string()));
288
289
290
291
292
293
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "alpha");
        assert_eq!(args["a"], "x");
        let (name, args) = extract_name_and_args(result[1].clone());
294
295
296
297
        assert_eq!(name, "omega");
        assert_eq!(args["z"], "y");
    }

298
299
    #[tokio::test]
    async fn parses_toolcall_wrapped_payload() {
300
301
        let input =
            r#"<TOOLCALL>[{ "name": "wrapped", "parameters": { "foo": "bar" } }]</TOOLCALL>"#;
302
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::default(), None)
303
304
            .await
            .unwrap();
305
        assert_eq!(content, Some("".to_string()));
306
307
308
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
309
310
311
312
        assert_eq!(name, "wrapped");
        assert_eq!(args["foo"], "bar");
    }

313
314
    #[tokio::test]
    async fn parses_python_tag_prefixed_payload() {
315
        let input = r#"<|python_tag|>{ "name": "pyfunc", "arguments": { "k": "v" } }"#;
316
        let (result, content) = try_tool_call_parse(
317
318
            input,
            &ToolCallConfig {
319
                parser_config: ParserConfig::Json(JsonParserConfig {
320
321
322
                    tool_call_start_tokens: vec!["<|python_tag|>".to_string()],
                    tool_call_end_tokens: vec!["".to_string()],
                    ..Default::default()
323
                }),
324
            },
325
            None,
326
        )
327
        .await
328
        .unwrap();
329
        assert_eq!(content, Some("".to_string()));
330
331
332
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
333
334
335
336
        assert_eq!(name, "pyfunc");
        assert_eq!(args["k"], "v");
    }

337
338
    #[tokio::test]
    async fn returns_none_on_invalid_input() {
339
        let input = r#"not even json"#;
340
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::default(), None)
341
342
            .await
            .unwrap();
343
        assert_eq!(content, Some("not even json".to_string()));
344
        assert!(result.is_empty());
345
346
    }

347
348
    #[tokio::test]
    async fn returns_none_on_valid_json_wrong_shape() {
349
        let input = r#"{ "foo": "bar" }"#;
350
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::default(), None)
351
352
            .await
            .unwrap();
353
        assert_eq!(content, Some("{ \"foo\": \"bar\" }".to_string()));
354
        assert!(result.is_empty());
355
356
357
    }

    // Tests for real model outputs - disabled by default
358
359
    #[tokio::test]
    async fn test_nvidia_llama3_nemotron_super_49b_simple() {
360
361
362
363
364
        let input = r#"<think>
Okay, the user is asking for the weather in San Francisco in Fahrenheit. Let me check the tools available.
</think>

<TOOLCALL>[{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}]</TOOLCALL>"#;
365
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
366
367
            .await
            .unwrap();
368
369
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
370
371
372
373
374
375
376
        assert_eq!(content, Some("<think>\nOkay, the user is asking for the weather in San Francisco in Fahrenheit. Let me check the tools available.\n</think>".to_string()));
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

377
378
    #[tokio::test]
    async fn test_nvidia_llama3_nemotron_super_49b_simple_with_no_think() {
379
        let input = r#"<TOOLCALL>[{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}]</TOOLCALL>"#;
380
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
381
382
            .await
            .unwrap();
383
384
385
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        assert_eq!(content, Some("".to_string()));
386
        let (name, args) = extract_name_and_args(result[0].clone());
387
388
389
390
391
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

392
393
    #[tokio::test]
    async fn test_nvidia_llama3_nemotron_super_49b_with_function_array() {
394
395
396
397
398
399
        let input = r#"<think>
Okay, the user is asking for the weather in San Francisco in Fahrenheit. Let me check the tools available.
</think>

<TOOLCALL>[{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}, {"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}]</TOOLCALL>"#;
        let config = ToolCallConfig::nemotron_deci();
400
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
401
        assert_eq!(content, Some("<think>\nOkay, the user is asking for the weather in San Francisco in Fahrenheit. Let me check the tools available.\n</think>".to_string()));
402
403
404
405
406
407
408
409
410
411
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
412
413
    }

414
415
    #[tokio::test]
    async fn test_nvidia_llama3_nemotron_super_49b_with_function_array_with_new_lines() {
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
        let input = r#"<think>
Okay, the user is asking for the weather in San Francisco in Fahrenheit. Let me check the tools available.
</think>

<TOOLCALL>
[{"name": "get_weather",
 "arguments": {"location": "San Francisco, CA",
  "unit": "fahrenheit"}},
  {"name": "get_weather",
   "arguments":
  {"location": "New York, NY",
  "unit": "fahrenheit"}}]
  </TOOLCALL>
  "#;
        let config = ToolCallConfig::nemotron_deci();
431
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
432
        assert_eq!(content, Some("<think>\nOkay, the user is asking for the weather in San Francisco in Fahrenheit. Let me check the tools available.\n</think>".to_string()));
433
434
435
436
437
438
439
440
441
442
443
444
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

445
446
    #[tokio::test]
    async fn test_qwen_qwq_32b_simple() {
447
448
449
        let input = r#"<tool_call>
{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}
</tool_call>"#;
450
        let (result, content) = detect_and_parse_tool_call(input, Some("hermes"), None)
451
452
            .await
            .unwrap();
453
        assert_eq!(content, Some("".to_string()));
454
455
456
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
457
458
459
460
461
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

462
463
    #[tokio::test]
    async fn test_qwen_qwq_32b_simple_with_normal_text() {
464
465
466
        let input = r#"Hey How are you? <tool_call>
{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}
</tool_call>"#;
467
        let (result, content) = detect_and_parse_tool_call(input, Some("hermes"), None)
468
469
            .await
            .unwrap();
470
471
472
473
474
        assert_eq!(content, Some("Hey How are you?".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
    }

475
476
    #[tokio::test]
    async fn test_nousresearch_hermes3_llama31_8b_simple() {
477
478
479
        let input = r#"<tool_call>
{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}
</tool_call>"#;
480
        let (result, content) = detect_and_parse_tool_call(input, Some("hermes"), None)
481
482
            .await
            .unwrap();
483
        assert_eq!(content, Some("".to_string()));
484
485
486
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
487
488
489
490
491
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

492
493
    #[tokio::test]
    async fn test_qwen_qwq_32b_multiple_tool_calls() {
494
495
496
497
498
499
500
501
        let input = r#"<tool_call>
{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}
</tool_call>
<tool_call>
{"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}
</tool_call>
"#;
        let config = ToolCallConfig::hermes();
502
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
503
504
505
506
507
508
509
510
511
512
513
514
515
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

516
517
    #[tokio::test]
    async fn test_qwen_qwq_32b_multiple_tool_calls_with_normal_text() {
518
519
520
521
522
523
524
525
        let input = r#"Hey How are you? <tool_call>
{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}
</tool_call>
<tool_call>
{"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}
</tool_call>
"#;
        let config = ToolCallConfig::hermes();
526
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
527
        assert_eq!(content, Some("Hey How are you?".to_string()));
528
529
530
531
532
533
534
535
536
537
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
538
539
    }

540
541
    #[tokio::test]
    async fn test_qwen_qwq_32b_multiple_tool_calls_with_new_lines() {
542
543
544
545
546
547
548
549
550
551
552
553
        let input = r#"<tool_call>
{"name": "get_weather",
"arguments": {"location": "San Francisco, CA",
"unit": "fahrenheit"}}
</tool_call>
<tool_call>
{"name": "get_weather", "arguments":
{"location": "New York, NY", "unit":
"fahrenheit"}}
</tool_call>
"#;
        let config = ToolCallConfig::hermes();
554
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
555
        assert_eq!(content, Some("".to_string()));
556
557
558
559
560
561
562
563
564
565
566
567
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

568
    #[tokio::test]
569
    #[ignore]
570
    async fn test_ibm_granite_40_tiny_preview_simple() {
571
572
        let input = r#"[{"arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}, "name": "get_weather"}]"#;
        let config = ToolCallConfig {
573
            parser_config: ParserConfig::Json(JsonParserConfig {
574
575
576
577
                tool_call_start_tokens: vec![],
                tool_call_end_tokens: vec![],
                arguments_keys: vec!["arguments".to_string()],
                ..Default::default()
578
            }),
579
        };
580
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
581
        assert_eq!(content, Some("".to_string()));
582
583
584
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
585
586
587
588
589
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

590
591
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_simple() {
592
        let input = r#" [{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}]"#;
593
        let config = ToolCallConfig::mistral();
594
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
595
596
597
598
599
600
601
602
603
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

604
605
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_simple_with_normal_text() {
606
607
        let input = r#"Hey How are you? [{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}]"#;
        let config = ToolCallConfig::mistral();
608
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
609
        assert_eq!(content, Some("Hey How are you?".to_string()));
610
611
612
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
613
614
615
616
617
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

618
619
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_simple_with_new_lines() {
620
621
622
623
624
625
        let input = r#"
        [{"name": "get_weather",
        "arguments": {"location":
        "San Francisco, CA",
        "unit": "fahrenheit"}}]
        "#;
626
        let config = ToolCallConfig::mistral();
627
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
628
        assert_eq!(content, Some("".to_string()));
629
630
631
632
633
634
635
636
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

637
638
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_multiple() {
639
640
        let input = r#" [{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}, {"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}]"#;
        let config = ToolCallConfig::mistral();
641
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
642
643
644
645
646
647
648
649
650
651
652
653
654
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

655
656
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_multiple_with_normal_text() {
657
658
        let input = r#"Hey How are you? [{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}, {"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}]"#;
        let config = ToolCallConfig::mistral();
659
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
660
        assert_eq!(content, Some("Hey How are you?".to_string()));
661
662
663
664
665
666
667
668
669
670
671
672
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

673
674
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_multiple_with_new_lines() {
675
676
        let input = r#"
        [{"name": "get_weather",
677
678
679
680
681
682
        "arguments": {"location":
        "San Francisco, CA",
        "unit": "fahrenheit"}},
        {"name": "get_weather", "arguments":
        {"location": "New York, NY", "unit":
        "fahrenheit"}}]
683
684
        "#;
        let config = ToolCallConfig::mistral();
685
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
686
        assert_eq!(content, Some("".to_string()));
687
688
689
690
691
692
693
694
695
696
697
698
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

699
700
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_single_with_start_token() {
701
702
        let input = r#"[TOOL_CALLS] [{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}]"#;
        let config = ToolCallConfig::mistral();
703
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
704
705
706
707
708
709
710
711
712
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

713
714
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_single_with_start_token_with_normal_text() {
715
716
        let input = r#"Hey How are you? [TOOL_CALLS] [{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}]"#;
        let config = ToolCallConfig::mistral();
717
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
718
        assert_eq!(content, Some("Hey How are you?".to_string()));
719
720
721
722
723
724
725
726
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

727
728
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_single_with_start_tokenwith_new_lines() {
729
730
731
732
733
734
735
736
        let input = r#"
        [TOOL_CALLS]
        [{"name": "get_weather",
        "arguments": {"location":
        "San Francisco, CA",
        "unit": "fahrenheit"}}]
        "#;
        let config = ToolCallConfig::mistral();
737
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
738
        assert_eq!(content, Some("".to_string()));
739
740
741
742
743
744
745
746
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

747
748
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_single_with_start_token_multiple() {
749
750
        let input = r#"[TOOL_CALLS] [{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}, {"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}]"#;
        let config = ToolCallConfig::mistral();
751
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
752
753
754
755
756
757
758
759
760
761
762
763
764
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

765
766
767
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_single_with_start_token_multiple_with_normal_text()
     {
768
769
        let input = r#"Hey How are you? [TOOL_CALLS] [{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}, {"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}]"#;
        let config = ToolCallConfig::mistral();
770
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
771
        assert_eq!(content, Some("Hey How are you?".to_string()));
772
773
774
775
776
777
778
779
780
781
782
783
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

784
785
786
    #[tokio::test]
    async fn test_mistralai_mistral_7b_instruct_v03_single_with_start_token_multiple_with_new_lines()
     {
787
788
789
790
791
792
793
794
795
796
797
        let input = r#"
        [TOOL_CALLS]
        [{"name": "get_weather",
        "arguments": {"location":
        "San Francisco, CA",
        "unit": "fahrenheit"}},
        {"name": "get_weather", "arguments":
        {"location": "New York, NY", "unit":
        "fahrenheit"}}]
        "#;
        let config = ToolCallConfig::mistral();
798
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
799
        assert_eq!(content, Some("".to_string()));
800
801
802
803
804
805
806
807
808
809
810
811
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

812
813
    #[tokio::test]
    async fn test_meta_llama_llama31_8b_instruct_simple() {
814
        let input = r#"{"name": "get_weather", "parameters": {"location": "San Francisco, CA", "unit": "fahrenheit"}}"#;
815
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::mistral(), None)
816
817
            .await
            .unwrap();
818
819
820
821
822
823
824
825
826
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

827
828
    #[tokio::test]
    async fn test_meta_llama_llama31_8b_instruct_simple_with_normal_text() {
829
        let input = r#"Hey How are you? {"name": "get_weather", "parameters": {"location": "San Francisco, CA", "unit": "fahrenheit"}}"#;
830
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::mistral(), None)
831
832
            .await
            .unwrap();
833
        assert_eq!(content, Some("Hey How are you?".to_string()));
834
835
836
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
837
838
839
840
841
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

842
843
    #[tokio::test]
    async fn test_meta_llama_llama31_8b_instruct_with_new_lines() {
844
845
846
847
        let input = r#"
        {"name": "get_weather",
        "parameters": {"location": "San Francisco, CA", "unit": "fahrenheit"}}
        "#;
848
        let (result, content) = detect_and_parse_tool_call(input, Some("llama3_json"), None)
849
850
            .await
            .unwrap();
851
        assert_eq!(content, Some("".to_string()));
852
853
854
855
856
857
858
859
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

860
861
    #[tokio::test]
    async fn test_meta_llama_llama31_8b_instruct_with_python_tag() {
862
        let input = r#"<|python_tag|>{ "name": "get_weather", "parameters": {"location": "San Francisco, CA", "unit": "fahrenheit" } }"#;
863
        let (result, content) = detect_and_parse_tool_call(input, Some("llama3_json"), None)
864
865
            .await
            .unwrap();
866
867
868
869
870
871
872
873
874
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

875
876
    #[tokio::test]
    async fn test_meta_llama_llama31_8b_instruct_with_python_tag_with_normal_text() {
877
        let input = r#"Hey How are you? <|python_tag|>{ "name": "get_weather", "parameters": {"location": "San Francisco, CA", "unit": "fahrenheit" } }"#;
878
        let (result, content) = detect_and_parse_tool_call(input, Some("llama3_json"), None)
879
880
            .await
            .unwrap();
881
        assert_eq!(content, Some("Hey How are you?".to_string()));
882
883
884
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
885
886
887
888
889
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

890
891
    #[tokio::test]
    async fn test_meta_llama_llama31_8b_instruct_with_python_tag_with_new_lines() {
892
893
894
895
        let input = r#"
        <|python_tag|>
        {"name": "get_weather", "parameters": {"location": "San Francisco, CA", "unit": "fahrenheit"}}
        "#;
896
        let (result, content) = detect_and_parse_tool_call(input, Some("llama3_json"), None)
897
898
            .await
            .unwrap();
899
        assert_eq!(content, Some("".to_string()));
900
901
902
903
904
905
906
907
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

908
909
    #[tokio::test]
    async fn test_meta_llama_llama31_8b_instruct_with_python_tag_multiple_with_new_lines() {
910
911
912
913
914
915
        let input = r#"
        <|python_tag|>
        {"name": "get_weather", "parameters": {"location": "San Francisco, CA", "unit": "fahrenheit" }}
        <|python_tag|>
        {"name": "get_weather", "parameters": {"location": "New York, NY", "unit": "fahrenheit" }}
        "#;
916
        let (result, content) = detect_and_parse_tool_call(input, Some("llama3_json"), None)
917
918
            .await
            .unwrap();
919
        assert_eq!(content, Some("".to_string()));
920
921
922
923
924
925
926
927
928
929
930
931
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

932
933
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_error_handling() {
934
935
        // Unknown parser string should return an error
        let input = r#"{"name": "get_weather", "arguments": {"location": "San Francisco, CA"}}"#;
936
        let result = detect_and_parse_tool_call(input, Some("unknown_parser"), None).await;
937
938
939
940
941
942
943
944
945
946
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("is not implemented"),
            "Unexpected error message: {}",
            err
        );

        // Known parser, but invalid input (not JSON) should return Ok(None)
        let input = "not a json";
947
        let (result, content) = detect_and_parse_tool_call(input, Some("hermes"), None)
948
949
            .await
            .unwrap();
950
951
        assert_eq!(content, Some("not a json".to_string()));
        assert!(result.is_empty());
952
953
954

        // Known parser, but valid JSON with wrong shape should return Ok(None)
        let input = r#"{"foo": "bar"}"#;
955
        let (result, content) = detect_and_parse_tool_call(input, Some("hermes"), None)
956
957
            .await
            .unwrap();
958
959
        assert_eq!(content, Some(r#"{"foo": "bar"}"#.to_string()));
        assert!(result.is_empty());
960
961
    }

962
    #[tokio::test]
963
    #[ignore]
964
    async fn test_internlm_internlm2_5_7b_chat_simple() {
965
966
967
968
969
        let input = r#"San Francisco's weather is known for its mild climate with plenty of fog, especially along the coast. Here's an overview of the weather in Fahrenheit:

- **Summer (June to August)**: Average highs range from the mid-60s to low 70s Fahrenheit, with cooler mornings and evenings. Coastal areas may be cooler than inland spots.

Remember, San Francisco weather can be quite unpredictable, particularly with its famous fog, which can significantly lower temperatures. Always check a local weather forecast for the most accurate and up-to-date information."#;
970
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::default(), None)
971
972
            .await
            .unwrap();
973
        assert_eq!(content, Some(input.to_string()));
974
        assert!(result.is_empty()); // This model doesn't produce tool calls
975
976
    }

977
978
    #[tokio::test]
    async fn test_ai21labs_ai21_jamba_15_mini_simple() {
979
980
981
982
        let input = r#"<tool_calls>[
{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}
]</tool_calls>"#;
        let config = ToolCallConfig::jamba();
983
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
984
        assert_eq!(content, Some("".to_string()));
985
986
987
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
988
989
990
991
992
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

993
994
995
996
997
998
999
    #[tokio::test]
    async fn test_ai21labs_ai21_jamba_15_mini_multiple() {
        let input = r#"<tool_calls>[
{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}},
{"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "celsius"}}
]</tool_calls>"#;
        let config = ToolCallConfig::jamba();
1000
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);

        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");

        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "celsius");
    }

1016
    #[tokio::test]
1017
    #[ignore]
1018
    async fn test_salesforce_llama_xlam_2_8b_fc_r_simple() {
1019
1020
        let input = r#"[{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}]"#;
        let config = ToolCallConfig {
1021
            parser_config: ParserConfig::Json(JsonParserConfig {
1022
1023
1024
1025
                tool_call_start_tokens: vec![],
                tool_call_end_tokens: vec![],
                arguments_keys: vec!["arguments".to_string()],
                ..Default::default()
1026
            }),
1027
        };
1028
        let (result, content) = try_tool_call_parse(input, &config, None).await.unwrap();
1029
        assert_eq!(content, Some("".to_string()));
1030
1031
1032
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
1033
1034
1035
1036
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }
1037

1038
1039
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_nemotron_deci() {
1040
        let input = r#"<TOOLCALL>[{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}]</TOOLCALL>"#;
1041
        let (result, content) = detect_and_parse_tool_call(input, None, None).await.unwrap();
1042
        assert_eq!(content, Some("".to_string()));
1043
1044
1045
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
1046
1047
1048
1049
1050
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

1051
1052
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_nemotron_deci_multiple() {
1053
        let input = r#"<TOOLCALL>[{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}, {"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}]</TOOLCALL>"#;
1054
        let (result, content) = detect_and_parse_tool_call(input, None, None).await.unwrap();
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

1068
1069
1070
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_nemotron_deci_multiple_with_normal_text()
     {
1071
        let input = r#"Hey How are you? <TOOLCALL>[{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit"}}, {"name": "get_weather", "arguments": {"location": "New York, NY", "unit": "fahrenheit"}}]</TOOLCALL>"#;
1072
        let (result, content) = detect_and_parse_tool_call(input, None, None).await.unwrap();
1073
        assert_eq!(content, Some("Hey How are you?".to_string()));
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
        assert!(!result.is_empty());
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York, NY");
        assert_eq!(args["unit"], "fahrenheit");
    }

1086
1087
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_llama3_json_with_python_tag() {
1088
        let input = r#"<|python_tag|>{ "name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit" } }"#;
1089
        let (result, content) = detect_and_parse_tool_call(input, None, None).await.unwrap();
1090
1091
1092
1093
1094
1095
1096
1097
1098
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

1099
1100
1101
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_llama3_json_with_python_tag_with_normal_text()
     {
1102
        let input = r#"Hey How are you? <|python_tag|>{ "name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit" } }"#;
1103
        let (result, content) = detect_and_parse_tool_call(input, None, None).await.unwrap();
1104
        assert_eq!(content, Some("Hey How are you?".to_string()));
1105
1106
1107
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
1108
1109
1110
1111
1112
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

1113
1114
1115
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_llama3_json_with_python_tag_with_new_lines()
     {
1116
1117
1118
1119
1120
1121
1122
1123
        let input = r#"
        <|python_tag|>
        {"name":
        "get_weather",
         "arguments":
          {"location": "San Francisco, CA",
          "unit": "fahrenheit" }}
        "#;
1124
        let (result, content) = detect_and_parse_tool_call(input, None, None).await.unwrap();
1125
        assert_eq!(content, Some("".to_string()));
1126
1127
1128
1129
1130
1131
1132
1133
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

1134
1135
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_llama3_json_without_python_tag_multiple_with_new_lines()
1136
     {
1137
1138
1139
1140
1141
        let input = r#"
        {"name": "get_weather", "arguments":
         {"location": "San Francisco, CA",
          "unit": "fahrenheit" }}
        "#;
1142
        let (result, content) = detect_and_parse_tool_call(input, None, None).await.unwrap();
1143
        assert_eq!(content, Some("".to_string()));
1144
1145
1146
1147
1148
1149
1150
1151
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

1152
1153
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_llama3_json_without_python_tag() {
1154
        let input = r#"{ "name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit" } }"#;
1155
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::mistral(), None)
1156
1157
            .await
            .unwrap();
1158
1159
1160
1161
1162
1163
1164
1165
1166
        assert_eq!(content, Some("".to_string()));
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }

1167
1168
    #[tokio::test]
    async fn test_detect_and_parse_tool_call_default_parser_llama3_json_without_python_tag_with_normal_text()
1169
1170
     {
        let input = r#"Hey How are you? { "name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "fahrenheit" } }"#;
1171
        let (result, content) = try_tool_call_parse(input, &ToolCallConfig::mistral(), None)
1172
1173
            .await
            .unwrap();
1174
        assert_eq!(content, Some("Hey How are you?".to_string()));
1175
1176
1177
        assert!(!result.is_empty());
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
1178
1179
1180
1181
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "fahrenheit");
    }
1182

1183
1184
    #[tokio::test]
    async fn test_phi4_single_function_call() {
1185
1186
        let input =
            r#"functools[{"name": "get_country_capital", "arguments": {"country": "Poland"}}]"#;
1187
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1188
1189
            .await
            .unwrap();
1190
1191
1192
1193
1194
1195
1196
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_country_capital");
        assert_eq!(args["country"], "Poland");
    }

1197
1198
    #[tokio::test]
    async fn test_phi4_single_function_call_with_normal_text() {
1199
        let input = r#"Hey How are you? functools[{"name": "get_country_capital", "arguments": {"country": "Poland"}}]"#;
1200
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1201
1202
            .await
            .unwrap();
1203
        assert_eq!(content, Some("Hey How are you?".to_string()));
1204
1205
1206
1207
1208
1209
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_country_capital");
        assert_eq!(args["country"], "Poland");
    }

1210
1211
    #[tokio::test]
    async fn test_phi4_multiple_function_calls_simple_arguments() {
1212
1213
1214
1215
        let input = r#"functools[
  {"name": "get_country_capital", "arguments": {"country": "Poland"}},
  {"name": "get_population", "arguments": {"city": "Warsaw"}}
]"#;
1216
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1217
1218
            .await
            .unwrap();
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 2);

        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "get_country_capital");
        assert_eq!(args1["country"], "Poland");

        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "get_population");
        assert_eq!(args2["city"], "Warsaw");
    }

1231
1232
    #[tokio::test]
    async fn test_phi4_multiple_function_calls_simple_arguments_with_normal_text() {
1233
1234
1235
1236
        let input = r#"Hey How are you? functools[
  {"name": "get_country_capital", "arguments": {"country": "Poland"}},
  {"name": "get_population", "arguments": {"city": "Warsaw"}}
]"#;
1237
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1238
1239
            .await
            .unwrap();
1240
        assert_eq!(content, Some("Hey How are you?".to_string()));
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
        assert_eq!(result.len(), 2);

        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "get_country_capital");
        assert_eq!(args1["country"], "Poland");

        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "get_population");
        assert_eq!(args2["city"], "Warsaw");
    }

1252
1253
    #[tokio::test]
    async fn test_phi4_single_function_call_nested_json_arguments() {
1254
1255
1256
        let input = r#"functools[{"name": "get_weather_forecast", "arguments":
        {"location": {"city": "San Francisco",
        "state": "CA"}, "date": "2023-10-05"}}]"#;
1257
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1258
1259
            .await
            .unwrap();
1260
1261
1262
1263
1264
1265
1266
1267
1268
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather_forecast");
        assert_eq!(args["date"], "2023-10-05");
        assert_eq!(args["location"]["city"], "San Francisco");
        assert_eq!(args["location"]["state"], "CA");
    }

1269
1270
    #[tokio::test]
    async fn test_phi4_single_function_call_nested_json_arguments_with_normal_text() {
1271
1272
1273
        let input = r#"Hey How are you? functools[{"name": "get_weather_forecast", "arguments":
        {"location": {"city": "San Francisco",
        "state": "CA"}, "date": "2023-10-05"}}]"#;
1274
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1275
1276
            .await
            .unwrap();
1277
        assert_eq!(content, Some("Hey How are you?".to_string()));
1278
1279
1280
1281
1282
1283
1284
1285
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather_forecast");
        assert_eq!(args["date"], "2023-10-05");
        assert_eq!(args["location"]["city"], "San Francisco");
        assert_eq!(args["location"]["state"], "CA");
    }

1286
1287
    #[tokio::test]
    async fn test_phi4_function_call_with_parameters_instead_of_arguments() {
1288
1289
        let input = r#"functools[{"name": "calculate_distance",
         "parameters": {"from": "New York", "to": "Los Angeles"}}]"#;
1290
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1291
1292
            .await
            .unwrap();
1293
1294
1295
1296
1297
1298
1299
1300
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "calculate_distance");
        assert_eq!(args["from"], "New York");
        assert_eq!(args["to"], "Los Angeles");
    }

1301
1302
    #[tokio::test]
    async fn test_phi4_function_call_with_parameters_instead_of_arguments_with_normal_text() {
1303
1304
        let input = r#"Hey How are you? functools[{"name": "calculate_distance",
         "parameters": {"from": "New York", "to": "Los Angeles"}}]"#;
1305
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1306
1307
            .await
            .unwrap();
1308
        assert_eq!(content, Some("Hey How are you?".to_string()));
1309
1310
1311
1312
1313
1314
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "calculate_distance");
        assert_eq!(args["from"], "New York");
        assert_eq!(args["to"], "Los Angeles");
    }
1315

1316
1317
1318
1319
1320
    #[tokio::test]
    async fn test_phi4_token_leak_reproduction() {
        // Reproduce the issue where "functools" appears in content field
        // This might happen when there's malformed JSON or parsing issues
        let input = r#"functools{"name": "get_weather","arguments":{"location":"San Francisco"}}"#;
1321
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
            .await
            .unwrap();
        // Content should be empty, not contain "functools"
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco");
    }

    #[tokio::test]
    async fn test_phi4_token_leak_edge_case() {
        // Test the case where only the token appears without JSON
        // This case is less critical but shouldn't leak the full token
        let input = r#"functools"#;
1337
        let (result, _content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
            .await
            .unwrap();
        // Content may contain the token if no valid JSON follows, but shouldn't crash
        // The important thing is that no tool calls are returned
        assert_eq!(result.len(), 0); // No tool calls found
        // Content behavior is less critical for this edge case
    }

    #[tokio::test]
    async fn test_phi4_token_with_invalid_json() {
        // Test the case where token is followed by invalid JSON
        let input = r#"functools{invalid json}"#;
1350
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
            .await
            .unwrap();
        // Content should be empty, not contain "functools" or leak the token
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 0); // No tool calls found due to invalid JSON
    }

    #[tokio::test]
    async fn test_phi4_streaming_partial_tokens() {
        // Test that our fix handles the actual streaming scenario described by the user
        // Where "fun", "ct", "ools" arrive as separate chunks

        // Test that "fun" is detected as a potential tool call start (for streaming jailing)
        let config = super::get_tool_parser_map().get("phi4").unwrap();
1365
1366
1367
1368
        let json_config = match &config.parser_config {
            super::super::config::ParserConfig::Json(cfg) => cfg,
            _ => panic!("Expected JSON parser config"),
        };
1369
1370
1371
1372

        // Test detection of partial tokens
        use super::super::json::detect_tool_call_start_json;
        assert!(
1373
            detect_tool_call_start_json("fun", json_config),
1374
1375
1376
            "'fun' should be detected as potential start"
        );
        assert!(
1377
            detect_tool_call_start_json("f", json_config),
1378
1379
1380
            "'f' should be detected as potential start"
        );
        assert!(
1381
            detect_tool_call_start_json("func", json_config),
1382
1383
1384
            "'func' should be detected as potential start"
        );
        assert!(
1385
            detect_tool_call_start_json("functo", json_config),
1386
1387
1388
1389
1390
            "'functo' should be detected as potential start"
        );

        // Test that unrelated text is not detected
        assert!(
1391
            !detect_tool_call_start_json("hello", json_config),
1392
1393
1394
            "'hello' should not be detected"
        );
        assert!(
1395
            !detect_tool_call_start_json("xyz", json_config),
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
            "'xyz' should not be detected"
        );
    }

    #[tokio::test]
    async fn test_phi4_false_positive_words() {
        // Test that words like "funk" or text starting with "func" but not "functools"
        // are correctly treated as normal content, not tool calls

        let input = r#"funk music is great"#;
1406
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
            .await
            .unwrap();
        // Should be treated as normal content, not tool call
        assert_eq!(
            result.len(),
            0,
            "No tool calls should be found in 'funk music is great'"
        );
        assert_eq!(
            content,
            Some("funk music is great".to_string()),
            "Content should contain the original text"
        );
    }

    #[tokio::test]
    async fn test_phi4_partial_but_complete_words() {
        // Test words that start with "func" but are not "functools"

        let input = r#"The function works well"#;
1427
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
            .await
            .unwrap();
        assert_eq!(
            result.len(),
            0,
            "No tool calls should be found in 'The function works well'"
        );
        assert_eq!(content, Some("The function works well".to_string()));

        let input = r#"functional programming"#;
1438
        let (result, content) = detect_and_parse_tool_call(input, Some("phi4"), None)
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
            .await
            .unwrap();
        assert_eq!(
            result.len(),
            0,
            "No tool calls should be found in 'functional programming'"
        );
        assert_eq!(content, Some("functional programming".to_string()));
    }

    #[tokio::test]
    async fn test_phi4_funk_variations() {
        // Test various "funk" related words to ensure they're not treated as tool calls

        let test_cases = vec![
            "funk",
            "funky",
            "funktion", // German word for function
            "funked",
            "I love funk music",
            "This is funky stuff",
        ];

        for test_input in test_cases {
1463
            let (result, content) = detect_and_parse_tool_call(test_input, Some("phi4"), None)
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
                .await
                .unwrap();
            assert_eq!(
                result.len(),
                0,
                "No tool calls should be found in '{}'",
                test_input
            );
            assert_eq!(
                content,
                Some(test_input.to_string()),
                "Content should match input for '{}'",
                test_input
            );
        }
    }

    #[tokio::test]
    async fn test_phi4_func_but_not_functools() {
        // Test words starting with "func" that are complete words, not partial "functools"

        let test_cases = vec![
            "func()",  // Programming syntax
            "funcdef", // Python keyword variant
            "functions are useful",
            "functionally speaking",
        ];

        for test_input in test_cases {
1493
            let (result, content) = detect_and_parse_tool_call(test_input, Some("phi4"), None)
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
                .await
                .unwrap();
            assert_eq!(
                result.len(),
                0,
                "No tool calls should be found in '{}'",
                test_input
            );
            assert_eq!(
                content,
                Some(test_input.to_string()),
                "Content should match input for '{}'",
                test_input
            );
        }
    }

1511
1512
    #[tokio::test]
    async fn test_pythonic_parser_basic_with_constants() {
1513
        let input = r#"[get_weather(location="San Francisco", unit="fahrenheit"), get_weather(location="New York", unit="fahrenheit")]"#;
1514
        let (result, content) = detect_and_parse_tool_call(input, Some("pythonic"), None)
1515
1516
            .await
            .unwrap();
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York");
        assert_eq!(args["unit"], "fahrenheit");
    }

1529
    #[tokio::test]
1530
    #[ignore]
1531
    async fn test_pythonic_parser_with_constants_and_normal_text() {
1532
        let input = r#"Hey How are you? [get_weather(location="San Francisco", unit="fahrenheit"), get_weather(location="New York", unit="fahrenheit")]"#;
1533
        let (result, content) = detect_and_parse_tool_call(input, Some("pythonic"), None)
1534
1535
            .await
            .unwrap();
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
        assert_eq!(content, Some("Hey How are you?".to_string()));
        assert_eq!(result.len(), 2);

        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco");
        assert_eq!(args["unit"], "fahrenheit");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "New York");
        assert_eq!(args["unit"], "fahrenheit");
    }
1548

1549
1550
    #[tokio::test]
    async fn test_harmony_parser_basic() {
1551
        let input = r#"
1552
        <|channel|>analysis<|message|>Need to use function get_current_weather.<|end|><|start|>assistant<|channel|>commentary to=functions.get_current_weather <|constrain|>json<|message|>{"location":"San Francisco", "unit":"fahrenheit"}"#;
1553
        let (result, content) = detect_and_parse_tool_call(input, Some("harmony"), None)
1554
1555
            .await
            .unwrap();
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
        assert_eq!(
            content,
            Some("Need to use function get_current_weather.".to_string())
        );
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_current_weather");
        assert_eq!(args["location"], "San Francisco");
        assert_eq!(args["unit"], "fahrenheit");
    }
1566

1567
1568
1569
1570
1571
1572
1573
1574
1575
    #[tokio::test]
    async fn test_deepseek_v3_parser_basic() {
        let input = r#"<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_current_weather
```json
{"location": "Tokyo"}
```<|tool▁call▁end|><|tool▁call▁begin|>function<|tool▁sep|>get_current_weather
```json
{"location": "Paris"}
```<|tool▁call▁end|><|tool▁calls▁end|><|end▁of▁sentence|>"#;
1576
        let (result, content) = detect_and_parse_tool_call(input, Some("deepseek_v3"), None)
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
            .await
            .unwrap();
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_current_weather");
        assert_eq!(args["location"], "Tokyo");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_current_weather");
        assert_eq!(args["location"], "Paris");
    }

1589
1590
    #[tokio::test]
    async fn test_deepseek_v3_1_parser_basic() {
1591
        let input = r#"<|tool▁calls▁begin|><|tool▁call▁begin|>get_current_weather<|tool▁sep|>{"location": "Tokyo"}<|tool▁call▁end|><|tool▁call▁begin|>get_current_weather<|tool▁sep|>{"location": "Paris"}<|tool▁call▁end|><|tool▁calls▁end|><|end▁of▁sentence|>"#;
1592
        let (result, content) = detect_and_parse_tool_call(input, Some("deepseek_v3_1"), None)
1593
1594
            .await
            .unwrap();
1595
1596
1597
1598
1599
1600
1601
1602
1603
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 2);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_current_weather");
        assert_eq!(args["location"], "Tokyo");
        let (name, args) = extract_name_and_args(result[1].clone());
        assert_eq!(name, "get_current_weather");
        assert_eq!(args["location"], "Paris");
    }
1604

1605
1606
1607
1608
1609
1610
1611
1612
    #[tokio::test]
    async fn test_deepseek_v3_2_single_tool_call() {
        let input = r#"<|DSML|function_calls>
<|DSML|invoke name="get_datetime">
<|DSML|parameter name="timezone" string="true">Asia/Shanghai</|DSML|parameter>
</|DSML|invoke>
</|DSML|function_calls>"#;

1613
1614
1615
1616
        let (tool_calls, normal_text) =
            detect_and_parse_tool_call(input, Some("deepseek_v3_2"), None)
                .await
                .expect("Failed to parse");
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639

        assert_eq!(tool_calls.len(), 1);
        assert_eq!(tool_calls[0].function.name, "get_datetime");
        assert_eq!(normal_text, Some("".to_string()));

        let args: serde_json::Value =
            serde_json::from_str(&tool_calls[0].function.arguments).unwrap();
        assert_eq!(args["timezone"], "Asia/Shanghai");
    }

    #[tokio::test]
    async fn test_deepseek_v3_2_multiple_tool_calls() {
        let input = r#"<|DSML|function_calls>
<|DSML|invoke name="get_weather">
<|DSML|parameter name="location" string="true">Hangzhou</|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">Beijing</|DSML|parameter>
<|DSML|parameter name="date" string="true">2024-01-16</|DSML|parameter>
</|DSML|invoke>
</|DSML|function_calls>"#;

1640
        let (tool_calls, _) = detect_and_parse_tool_call(input, Some("deepseek_v3_2"), None)
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
            .await
            .expect("Failed to parse");

        assert_eq!(tool_calls.len(), 2);
        assert_eq!(tool_calls[0].function.name, "get_weather");
        assert_eq!(tool_calls[1].function.name, "get_weather");

        let args0: serde_json::Value =
            serde_json::from_str(&tool_calls[0].function.arguments).unwrap();
        assert_eq!(args0["location"], "Hangzhou");
        assert_eq!(args0["date"], "2024-01-16");

        let args1: serde_json::Value =
            serde_json::from_str(&tool_calls[1].function.arguments).unwrap();
        assert_eq!(args1["location"], "Beijing");
    }

    #[tokio::test]
    async fn test_deepseek_v3_2_mixed_parameter_types() {
        let input = r#"<|DSML|function_calls>
<|DSML|invoke name="search">
<|DSML|parameter name="query" string="true">search agent benchmark 2024</|DSML|parameter>
<|DSML|parameter name="topn" string="false">10</|DSML|parameter>
<|DSML|parameter name="source" string="true">web</|DSML|parameter>
</|DSML|invoke>
</|DSML|function_calls>"#;

1668
        let (tool_calls, _) = detect_and_parse_tool_call(input, Some("deepseek_v3_2"), None)
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
            .await
            .expect("Failed to parse");

        assert_eq!(tool_calls.len(), 1);
        assert_eq!(tool_calls[0].function.name, "search");

        let args: serde_json::Value =
            serde_json::from_str(&tool_calls[0].function.arguments).unwrap();
        assert_eq!(args["query"], "search agent benchmark 2024");
        assert_eq!(args["topn"], 10); // Should be number, not string
        assert_eq!(args["source"], "web");
    }

1682
1683
    #[tokio::test]
    async fn test_hermes_parser_without_new_line() {
1684
1685
        let input = r#"<tool_call>{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "celsius"}}</tool_call>"
        "#;
1686
        let (result, content) = detect_and_parse_tool_call(input, Some("hermes"), None)
1687
1688
            .await
            .unwrap();
1689
1690
1691
1692
1693
1694
1695
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco, CA");
        assert_eq!(args["unit"], "celsius");
    }
1696
}
1697

1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
// Comprehensive parallel tool calling tests based on the examples provided
#[cfg(test)]
mod parallel_tool_calling_tests {
    use super::*;

    fn extract_name_and_args(call: ToolCallResponse) -> (String, serde_json::Value) {
        let args: serde_json::Value = serde_json::from_str(&call.function.arguments).unwrap();
        (call.function.name, args)
    }

    /// Helper function to validate parallel tool call results for weather queries
    fn validate_weather_tool_calls(result: &[ToolCallResponse], expected_cities: &[(&str, &str)]) {
        assert_eq!(
            result.len(),
            expected_cities.len(),
            "Expected {} tool calls, got {}",
            expected_cities.len(),
            result.len()
        );

        for (i, (expected_city, expected_state)) in expected_cities.iter().enumerate() {
            let (name, args) = extract_name_and_args(result[i].clone());
            assert_eq!(
                name, "get_current_weather",
                "Tool call {} should be get_current_weather",
                i
            );
            assert_eq!(
                args["city"], *expected_city,
                "Tool call {} city should be {}",
                i, expected_city
            );
            assert_eq!(
                args["state"], *expected_state,
                "Tool call {} state should be {}",
                i, expected_state
            );
            assert_eq!(
                args["unit"], "fahrenheit",
                "Tool call {} unit should be fahrenheit",
                i
            );

            // Validate tool call ID format (should be at least 9 characters)
            assert!(
                result[i].id.len() >= 9,
                "Tool call {} ID should be at least 9 characters",
                i
            );

            // Validate tool call type
            assert_eq!(
                result[i].tp,
                crate::tool_calling::response::ToolCallType::Function,
                "Tool call {} type should be 'function'",
                i
            );
        }
    }

    // =============================================================================
1759
    // 1. NEMOTRON/DECI TOOL PARSER FORMAT (JSON Array in XML tags)
1760
1761
1762
    // =============================================================================

    #[tokio::test]
1763
1764
    async fn test_parallel_nemotron_format_two_cities() {
        let input = r#" <TOOLCALL>[
1765
1766
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}}
1767
]</TOOLCALL>"#;
1768

1769
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
1770
1771
1772
1773
1774
1775
1776
1777
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        validate_weather_tool_calls(&result, &[("Dallas", "TX"), ("Orlando", "FL")]);
    }

    #[tokio::test]
1778
    async fn test_parallel_nemotron_format_three_cities() {
1779
1780
1781
1782
1783
1784
        let input = r#"<TOOLCALL>[
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "fahrenheit"}}
]</TOOLCALL>"#;

1785
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        validate_weather_tool_calls(
            &result,
            &[("Dallas", "TX"), ("Orlando", "FL"), ("Seattle", "WA")],
        );
    }

    #[tokio::test]
1797
    async fn test_parallel_nemotron_format_with_normal_text() {
1798
1799
1800
1801
1802
        let input = r#"I'll help you get the weather for both cities. <TOOLCALL>[
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}}
]</TOOLCALL>"#;

1803
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
            .await
            .unwrap();

        assert_eq!(
            content,
            Some("I'll help you get the weather for both cities.".to_string())
        );
        validate_weather_tool_calls(&result, &[("Dallas", "TX"), ("Orlando", "FL")]);
    }

1814
1815
1816
    // =================================================
    // 2. QWEN3CODER TOOL PARSER FORMAT (XML-style tags)
    // =================================================
1817
1818
1819

    #[tokio::test]
    async fn test_parallel_qwen3coder_format_two_cities() {
1820
        let input = r#"<tool_call>
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
<function=get_current_weather>
<parameter=city>
Dallas
</parameter>
<parameter=state>
TX
</parameter>
<parameter=unit>
fahrenheit
</parameter>
</function>
</tool_call>
<tool_call>
<function=get_current_weather>
<parameter=city>
Orlando
</parameter>
<parameter=state>
FL
</parameter>
<parameter=unit>
fahrenheit
</parameter>
</function>
</tool_call>"#;

1847
        let (result, content) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        validate_weather_tool_calls(&result, &[("Dallas", "TX"), ("Orlando", "FL")]);
    }

    // =============================================================================
    // 3. xLAM TOOL PARSER FORMAT (Pure JSON Array) - Testing via mistral parser
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_xlam_format_pure_json() {
        let input = r#"[{"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}}, {"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}}]"#;

1863
        let (result, content) = detect_and_parse_tool_call(input, Some("mistral"), None)
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        validate_weather_tool_calls(&result, &[("Dallas", "TX"), ("Orlando", "FL")]);
    }

    #[tokio::test]
    async fn test_parallel_xlam_format_with_whitespace() {
        let input = r#"[
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}}
]"#;

1878
        let (result, content) = detect_and_parse_tool_call(input, Some("mistral"), None)
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        validate_weather_tool_calls(&result, &[("Dallas", "TX"), ("Orlando", "FL")]);
    }

    // =============================================================================
    // 4. MINIMAX TOOL PARSER FORMAT (Multi-line JSON in XML tags)
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_minimax_format() {
        let _input = r#"<tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}}
{"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}}
</tool_calls>"#;

        // This would need a specialized parser, but we can test with a modified hermes approach
        // For now, test with nemotron_deci which handles similar XML wrapping
        let input_nemotron_format = r#"<TOOLCALL>[
{"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
{"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}}
]</TOOLCALL>"#;

        let (result, content) =
1905
            detect_and_parse_tool_call(input_nemotron_format, Some("nemotron_deci"), None)
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
                .await
                .unwrap();

        assert_eq!(content, Some("".to_string()));
        validate_weather_tool_calls(&result, &[("Dallas", "TX"), ("Orlando", "FL")]);
    }

    // =============================================================================
    // 5. HARMONY TOOL PARSER FORMAT (Multiple Tool Calls with Harmony Encoding)
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_harmony_format_multiple_tools() {
        // Test with harmony parser for multiple tool calls
1920
        let input = r#"<|channel|>commentary to=functions.get_current_weather <|constrain|>json<|message|>{"city": "Dallas", "state": "TX", "unit": "fahrenheit"}<|call|><|start|>assistant<|channel|>commentary to=functions.get_current_weather <|constrain|>json<|message|>{"city": "Orlando", "state": "FL", "unit": "fahrenheit"}<|call|>"#;
1921

1922
        let (result, _content) = detect_and_parse_tool_call(input, Some("harmony"), None)
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
            .await
            .unwrap();

        // Harmony parser might handle this differently, so we check for at least one tool call
        assert!(!result.is_empty(), "Should parse at least one tool call");

        // Validate first tool call
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_current_weather");
        assert!(args.get("city").is_some() || args.get("location").is_some());
    }

    // =============================================================================
    // 6. MIXED TOOL TYPES PARALLEL CALLING
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_mixed_tool_types() {
        let input = r#"<TOOLCALL>[
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
    {"name": "web_search", "arguments": {"query": "Orlando Florida attractions", "max_results": 5}}
]</TOOLCALL>"#;

1946
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 2);

        // Validate first tool call (weather)
        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "get_current_weather");
        assert_eq!(args1["city"], "Dallas");
        assert_eq!(args1["state"], "TX");
        assert_eq!(args1["unit"], "fahrenheit");

        // Validate second tool call (web search)
        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "web_search");
        assert_eq!(args2["query"], "Orlando Florida attractions");
        assert_eq!(args2["max_results"], 5);
    }

    // =============================================================================
    // 7. EDGE CASES AND ERROR HANDLING
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_malformed_second_call() {
        let input = r#"<TOOLCALL>[
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Orlando", "invalid_field": 123}}
]</TOOLCALL>"#;

1978
        let (result, _content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
            .await
            .unwrap();

        // Should still parse the valid first call
        assert!(
            !result.is_empty(),
            "Should parse at least the valid tool call"
        );

        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_current_weather");
        assert_eq!(args["city"], "Dallas");
    }

    #[tokio::test]
    async fn test_parallel_empty_array() {
        let input = r#"<TOOLCALL>[]</TOOLCALL>"#;

1997
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
            .await
            .unwrap();

        assert_eq!(
            result.len(),
            0,
            "Empty array should result in no tool calls"
        );
        assert_eq!(content, Some("".to_string()));
    }

    #[tokio::test]
    async fn test_parallel_single_call_in_array() {
        let input = r#"<TOOLCALL>[
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}}
]</TOOLCALL>"#;

2015
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        validate_weather_tool_calls(&result, &[("Dallas", "TX")]);
    }

    // =============================================================================
    // 8. LARGE SCALE PARALLEL CALLS
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_five_cities() {
        let input = r#"<TOOLCALL>[
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Denver", "state": "CO", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Miami", "state": "FL", "unit": "fahrenheit"}}
]</TOOLCALL>"#;

2038
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        validate_weather_tool_calls(
            &result,
            &[
                ("Dallas", "TX"),
                ("Orlando", "FL"),
                ("Seattle", "WA"),
                ("Denver", "CO"),
                ("Miami", "FL"),
            ],
        );
    }

    // =============================================================================
    // 9. COMPLEX ARGUMENTS PARALLEL CALLS
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_complex_arguments() {
        let input = r#"<TOOLCALL>[
    {
        "name": "get_weather_forecast",
        "arguments": {
            "location": {"city": "Dallas", "state": "TX", "country": "USA"},
            "days": 7,
            "units": "fahrenheit",
            "include_hourly": true,
            "alerts": ["severe_weather", "temperature_extreme"]
        }
    },
    {
        "name": "get_air_quality",
        "arguments": {
            "coordinates": {"lat": 32.7767, "lon": -96.7970},
            "metrics": ["pm2.5", "pm10", "ozone", "no2"],
            "radius_km": 50
        }
    }
]</TOOLCALL>"#;

2082
        let (result, content) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
            .await
            .unwrap();

        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 2);

        // Validate first tool call (weather forecast)
        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "get_weather_forecast");
        assert_eq!(args1["location"]["city"], "Dallas");
        assert_eq!(args1["days"], 7);
        assert_eq!(args1["include_hourly"], true);

        // Validate second tool call (air quality)
        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "get_air_quality");
        assert_eq!(args2["coordinates"]["lat"], 32.7767);
        assert_eq!(args2["radius_km"], 50);
    }

    // =============================================================================
    // 10. VALIDATION HELPERS AND UTILITIES
    // =============================================================================

    /// Helper function to validate tool call IDs are unique and properly formatted
    fn validate_tool_call_ids(result: &[ToolCallResponse]) {
        let mut ids = std::collections::HashSet::new();
        for (i, tool_call) in result.iter().enumerate() {
            assert!(
                tool_call.id.len() >= 9,
                "Tool call {} ID '{}' should be at least 9 characters",
                i,
                tool_call.id
            );

            assert!(
                ids.insert(&tool_call.id),
                "Tool call {} ID '{}' is not unique",
                i,
                tool_call.id
            );
        }
    }

    /// Helper function to validate tool call structure and OpenAI compatibility
    fn validate_openai_compatibility(result: &[ToolCallResponse]) {
        for (i, tool_call) in result.iter().enumerate() {
            // Validate type is "function"
            assert_eq!(
                tool_call.tp,
                crate::tool_calling::response::ToolCallType::Function,
                "Tool call {} type should be 'function', got '{:?}'",
                i,
                tool_call.tp
            );

            // Validate function name is not empty
            assert!(
                !tool_call.function.name.is_empty(),
                "Tool call {} function name should not be empty",
                i
            );

            // Validate arguments are valid JSON
            let _: serde_json::Value = serde_json::from_str(&tool_call.function.arguments)
                .unwrap_or_else(|_| panic!("Tool call {} arguments should be valid JSON", i));
        }
    }

    #[tokio::test]
    async fn test_parallel_tool_call_id_uniqueness() {
        let input = r#"<TOOLCALL>[
    {"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}},
    {"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}},
    {"name": "web_search", "arguments": {"query": "weather forecast", "max_results": 3}}
]</TOOLCALL>"#;

2160
        let (result, _) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
            .await
            .unwrap();

        assert_eq!(result.len(), 3);
        validate_tool_call_ids(&result);
        validate_openai_compatibility(&result);
    }

    #[tokio::test]
    async fn test_parallel_openai_compatibility_validation() {
        let input = r#"[TOOL_CALLS][
    {"name": "function_one", "arguments": {"param1": "value1", "param2": 42}},
    {"name": "function_two", "arguments": {"param3": true, "param4": [1, 2, 3]}},
    {"name": "function_three", "arguments": {"param5": {"nested": "object"}}}
][/TOOL_CALLS]"#;

2177
        let (result, _) = detect_and_parse_tool_call(input, Some("mistral"), None)
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
            .await
            .unwrap();

        assert_eq!(result.len(), 3);
        validate_openai_compatibility(&result);

        // Verify all functions have different names
        let names: std::collections::HashSet<_> =
            result.iter().map(|tc| &tc.function.name).collect();
        assert_eq!(names.len(), 3, "All function names should be unique");
    }

    // =============================================================================
    // 11. PERFORMANCE AND STRESS TESTS
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_performance_many_small_calls() {
        let mut tool_calls = Vec::new();
        for i in 0..20 {
            tool_calls.push(format!(
                r#"{{"name": "get_data_{}", "arguments": {{"id": {}, "type": "test"}}}}"#,
                i, i
            ));
        }

        let input = format!("<TOOLCALL>[{}]</TOOLCALL>", tool_calls.join(","));

        let start = std::time::Instant::now();
2207
        let (result, _) = detect_and_parse_tool_call(&input, Some("nemotron_deci"), None)
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
            .await
            .unwrap();
        let duration = start.elapsed();

        assert_eq!(result.len(), 20);
        assert!(
            duration < std::time::Duration::from_millis(100),
            "Parsing 20 tool calls should take less than 100ms, took {:?}",
            duration
        );

        validate_tool_call_ids(&result);
        validate_openai_compatibility(&result);
    }

    #[tokio::test]
    async fn test_parallel_large_arguments() {
        let large_data = "x".repeat(1000); // 1KB of data
        let input = format!(
            r#"<TOOLCALL>[
    {{"name": "process_large_data", "arguments": {{"data": "{}", "size": 1000}}}},
    {{"name": "backup_data", "arguments": {{"backup_data": "{}", "timestamp": "2024-01-01T00:00:00Z"}}}}
]</TOOLCALL>"#,
            large_data, large_data
        );

2234
        let (result, _) = detect_and_parse_tool_call(&input, Some("nemotron_deci"), None)
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
            .await
            .unwrap();

        assert_eq!(result.len(), 2);

        // Validate large arguments are preserved
        for tool_call in &result {
            let args: serde_json::Value =
                serde_json::from_str(&tool_call.function.arguments).unwrap();
            if tool_call.function.name == "process_large_data" {
                assert_eq!(args["data"].as_str().unwrap().len(), 1000);
                assert_eq!(args["size"], 1000);
            }
        }
    }

    // =============================================================================
    // 12. ADDITIONAL EDGE CASES AND ERROR SCENARIOS
    // =============================================================================

    #[tokio::test]
    async fn test_parallel_unicode_and_special_characters() {
        let input = r#"<TOOLCALL>[
    {"name": "translate_text", "arguments": {"text": "Hello 世界! 🌍", "from": "en", "to": "zh"}},
    {"name": "analyze_emoji", "arguments": {"emoji": "🚀💫⭐", "context": "space exploration"}},
    {"name": "process_unicode", "arguments": {"data": "café naïve résumé", "encoding": "utf-8"}}
]</TOOLCALL>"#;

2263
        let (result, _) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
            .await
            .unwrap();

        assert_eq!(result.len(), 3);

        // Validate Unicode characters are preserved
        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "translate_text");
        assert_eq!(args1["text"], "Hello 世界! 🌍");

        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "analyze_emoji");
        assert_eq!(args2["emoji"], "🚀💫⭐");

        let (name3, args3) = extract_name_and_args(result[2].clone());
        assert_eq!(name3, "process_unicode");
        assert_eq!(args3["data"], "café naïve résumé");
    }

    #[tokio::test]
    async fn test_parallel_json_escaping_and_quotes() {
2285
2286
        // Test that complex JSON with escaping doesn't crash the parser
        // We don't validate the exact escaped content, just that parsing succeeds
2287
2288
2289
2290
2291
2292
        let input = r#"<TOOLCALL>[
    {"name": "process_json", "arguments": {"json_string": "{\"key\": \"value with \\\"quotes\\\"\"}", "format": "strict"}},
    {"name": "handle_paths", "arguments": {"windows_path": "C:\\Users\\Test\\Documents\\file.txt", "unix_path": "/home/user/file.txt"}},
    {"name": "regex_pattern", "arguments": {"pattern": "\\d{3}-\\d{3}-\\d{4}", "test_string": "Phone: 123-456-7890"}}
]</TOOLCALL>"#;

2293
        let (result, _) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2294
2295
2296
            .await
            .unwrap();

2297
        // Just verify parsing succeeds and we get the expected number of tool calls
2298
2299
        assert_eq!(result.len(), 3);

2300
2301
        // Verify function names are correct
        let (name1, _args1) = extract_name_and_args(result[0].clone());
2302
2303
        assert_eq!(name1, "process_json");

2304
        let (name2, _args2) = extract_name_and_args(result[1].clone());
2305
2306
        assert_eq!(name2, "handle_paths");

2307
        let (name3, _args3) = extract_name_and_args(result[2].clone());
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
        assert_eq!(name3, "regex_pattern");
    }

    #[tokio::test]
    async fn test_parallel_mixed_argument_types() {
        let input = r#"<TOOLCALL>[
    {"name": "type_test", "arguments": {"string": "text", "number": 42, "float": 2.718281828459045, "boolean": true, "null_value": null}},
    {"name": "array_test", "arguments": {"empty_array": [], "string_array": ["a", "b", "c"], "mixed_array": [1, "two", true, null]}},
    {"name": "object_test", "arguments": {"empty_object": {}, "nested": {"level1": {"level2": {"value": "deep"}}}}}
]</TOOLCALL>"#;

2319
        let (result, _) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
            .await
            .unwrap();

        assert_eq!(result.len(), 3);

        // Validate different argument types are preserved
        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "type_test");
        assert_eq!(args1["string"], "text");
        assert_eq!(args1["number"], 42);
        assert_eq!(args1["float"], std::f64::consts::E);
        assert_eq!(args1["boolean"], true);
        assert!(args1["null_value"].is_null());

        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "array_test");
        assert!(args2["empty_array"].is_array());
        assert_eq!(args2["empty_array"].as_array().unwrap().len(), 0);
        assert_eq!(args2["string_array"].as_array().unwrap().len(), 3);
        assert_eq!(args2["mixed_array"].as_array().unwrap().len(), 4);

        let (name3, args3) = extract_name_and_args(result[2].clone());
        assert_eq!(name3, "object_test");
        assert!(args3["empty_object"].is_object());
        assert_eq!(args3["nested"]["level1"]["level2"]["value"], "deep");
    }

    #[tokio::test]
    async fn test_parallel_whitespace_variations() {
        // Test with various whitespace patterns
        let input = r#"<TOOLCALL>[
    {
        "name": "spaced_function",
        "arguments": {
            "param1": "value1",
            "param2": "value2"
        }
    },
    {"name":"compact_function","arguments":{"param":"value"}},
    {
      "name"  :  "weird_spacing",
      "arguments"  :  {
        "key"  :  "value"
      }
    }
]</TOOLCALL>"#;

2367
        let (result, _) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
            .await
            .unwrap();

        assert_eq!(result.len(), 3);
        validate_openai_compatibility(&result);

        // All should parse correctly despite different whitespace
        let names: Vec<_> = result.iter().map(|tc| &tc.function.name).collect();
        assert!(names.contains(&&"spaced_function".to_string()));
        assert!(names.contains(&&"compact_function".to_string()));
        assert!(names.contains(&&"weird_spacing".to_string()));
    }

    #[tokio::test]
    async fn test_parallel_cross_parser_compatibility() {
        // Test the same parallel tool calls across different parsers
        let base_calls = r#"[
    {"name": "get_weather", "arguments": {"city": "Dallas", "unit": "fahrenheit"}},
    {"name": "get_weather", "arguments": {"city": "Orlando", "unit": "fahrenheit"}}
]"#;

        // Test with different parser formats
        let test_cases = vec![
            (
                format!("<TOOLCALL>{}</TOOLCALL>", base_calls),
                "nemotron_deci",
            ),
            (
                format!("[TOOL_CALLS]{}[/TOOL_CALLS]", base_calls),
                "mistral",
            ),
            (base_calls.to_string(), "mistral"), // Raw JSON
        ];

        for (input, parser) in test_cases {
2403
            let (result, _) = detect_and_parse_tool_call(&input, Some(parser), None)
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
                .await
                .unwrap_or_else(|e| panic!("Failed to parse with {}: {}", parser, e));
            assert_eq!(
                result.len(),
                2,
                "Parser {} should produce 2 tool calls",
                parser
            );

            for tool_call in &result {
                assert_eq!(tool_call.function.name, "get_weather");
                let args: serde_json::Value =
                    serde_json::from_str(&tool_call.function.arguments).unwrap();
                assert!(args["city"].is_string());
                assert_eq!(args["unit"], "fahrenheit");
            }
        }
    }

    #[tokio::test]
    async fn test_parallel_boundary_conditions() {
        // Test with exactly 1 tool call in array (boundary between single and parallel)
        let input_single = r#"<TOOLCALL>[
    {"name": "single_call", "arguments": {"test": true}}
]</TOOLCALL>"#;

2430
        let (result, _) = detect_and_parse_tool_call(input_single, Some("nemotron_deci"), None)
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].function.name, "single_call");

        // Test with maximum reasonable number of parallel calls
        let mut many_calls = Vec::new();
        for i in 0..50 {
            many_calls.push(format!(
                r#"{{"name": "call_{}", "arguments": {{"index": {}}}}}"#,
                i, i
            ));
        }

        let input_many = format!("<TOOLCALL>[{}]</TOOLCALL>", many_calls.join(","));

2448
        let (result, _) = detect_and_parse_tool_call(&input_many, Some("nemotron_deci"), None)
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
            .await
            .unwrap();

        assert_eq!(result.len(), 50);
        validate_tool_call_ids(&result);

        // Verify all calls are present and correctly indexed
        for (i, tool_call) in result.iter().enumerate() {
            assert_eq!(tool_call.function.name, format!("call_{}", i));
            let args: serde_json::Value =
                serde_json::from_str(&tool_call.function.arguments).unwrap();
            assert_eq!(args["index"], i);
        }
    }

    #[tokio::test]
    async fn test_parallel_malformed_recovery() {
        // Test parser's ability to recover from malformed entries
        let input = r#"<TOOLCALL>[
    {"name": "good_call_1", "arguments": {"param": "value1"}},
    {"malformed": "missing_name_and_arguments"},
    {"name": "good_call_2", "arguments": {"param": "value2"}},
    {"name": "missing_args"},
    {"name": "good_call_3", "arguments": {"param": "value3"}},
    "completely_invalid_json",
    {"name": "good_call_4", "arguments": {"param": "value4"}}
]</TOOLCALL>"#;

2477
        let (result, _) = detect_and_parse_tool_call(input, Some("nemotron_deci"), None)
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
            .await
            .unwrap();

        // Should recover and parse the valid entries
        assert!(
            !result.is_empty(),
            "Should parse at least some valid tool calls"
        );

        // Count valid tool calls that were successfully parsed
        let valid_calls: Vec<_> = result
            .iter()
            .filter(|tc| tc.function.name.starts_with("good_call"))
            .collect();

        assert!(
            valid_calls.len() >= 2,
            "Should parse at least 2 valid tool calls"
        );

        // Verify the valid ones are correct
        for tool_call in valid_calls {
            assert!(tool_call.function.name.starts_with("good_call"));
            let args: serde_json::Value =
                serde_json::from_str(&tool_call.function.arguments).unwrap();
            assert!(args["param"].is_string());
        }
    }
}

2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
#[cfg(test)]
// Just e2e tests to test the flow. Detailed tests are covered in the individual parsers
mod detect_parser_tests {
    use super::*;

    #[test]
    fn test_e2e_detect_tool_call_start_harmony() {
        let text = r#"<|start|>assistant<|channel|>commentary to=functions.get_current_weather <|constrain|>json"#;
        let result = detect_tool_call_start(text, Some("harmony")).unwrap();
        assert!(result);
    }

    #[test]
    fn test_e2e_detect_tool_call_start_hermes() {
        let text = r#"{"name": "get_current_weather", "parameters": {"location": "Tokyo"}}"#;
        let result = detect_tool_call_start(text, Some("hermes")).unwrap();
        assert!(result);
    }

    #[test]
    fn test_e2e_detect_tool_call_start_pythonic() {
        let text = r#"foo(a=1, b=2), bar(x=3)]"#;
        let result = detect_tool_call_start(text, Some("pythonic")).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_e2e_detect_tool_call_start_nemotron_deci() {
        let text = r#"<TOOLCALL>[{"name": "get_current_weather", "parameters": {"location": "Tokyo"}}]</TOOLCALL>"#;
        let result = detect_tool_call_start(text, Some("nemotron_deci")).unwrap();
        assert!(result);
    }

    #[test]
    fn test_e2e_detect_tool_call_start_phi4() {
        let text =
            r#"functools{"name": "get_current_weather", "parameters": {"location": "Tokyo"}}"#;
        let result = detect_tool_call_start(text, Some("phi4")).unwrap();
        assert!(result);
    }

    #[test]
    fn test_e2e_detect_tool_call_start_llama3_json() {
        let text = r#"<|python_tag|>{ "name": "get_current_weather", "parameters": {"location": "Tokyo"}}"#;
        let result = detect_tool_call_start(text, Some("llama3_json")).unwrap();
        assert!(result);
    }

    #[test]
    fn test_e2e_detect_tool_call_start_mistral() {
        let text =
            r#"[TOOL_CALLS]{"name": "get_current_weather", "parameters": {"location": "Tokyo"}}"#;
        let result = detect_tool_call_start(text, Some("mistral")).unwrap();
        assert!(result);
    }

2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
    // DeepSeek V3
    #[test]
    fn test_e2e_detect_incomplete_tool_call_start_deepseek_v3() {
        let text = r#"<|tool▁call▁begin|>function<|tool▁sep|>get_current_weather
```json
{"location": "Tokyo"}
```<|tool▁call▁end|>"#;
        let result = detect_tool_call_start(text, Some("deepseek_v3")).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_e2e_detect_tool_call_start_deepseek_v3() {
        let text = r#"<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_current_weather
```json
{"location": "Tokyo"}
```<|tool▁call▁end|>"#;
        let result = detect_tool_call_start(text, Some("deepseek_v3")).unwrap();
        assert!(result);
    }

    // DeepSeek V3.1
2586
    #[test]
2587
    fn test_e2e_detect_incomplete_tool_call_start_deepseek_v3_1() {
2588
        let text = r#"<|tool▁call▁begin|>get_current_weather<|tool▁sep|>{"location": "Tokyo"}<|tool▁call▁end|>"#;
2589
        let result = detect_tool_call_start(text, Some("deepseek_v3_1")).unwrap();
2590
        assert!(!result);
2591
2592
2593
    }

    #[test]
2594
    fn test_e2e_detect_tool_call_start_deepseek_v3_1() {
2595
        let text = r#"<|tool▁calls▁begin|><|tool▁call▁begin|>get_current_weather<|tool▁sep|>{"location": "Tokyo"}<|tool▁call▁end|>"#;
2596
2597
2598
        let result = detect_tool_call_start(text, Some("deepseek_v3_1")).unwrap();
        assert!(result);
    }
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633

    #[test]
    fn test_e2e_detect_tool_call_start_xml() {
        let text = r#"<tool_call><function=get_weather><parameter=city>Dallas</parameter></function></tool_call>"#;
        let result = detect_tool_call_start(text, Some("qwen3_coder")).unwrap();
        assert!(result);
    }

    #[test]
    fn test_e2e_detect_tool_call_start_xml_partial() {
        let text = r#"<tool_c"#; // Partial start token
        let result = detect_tool_call_start(text, Some("qwen3_coder")).unwrap();
        assert!(result);
    }
}

// Xml parser tests
#[cfg(test)]
mod xml_parser_tests {
    use super::*;

    fn extract_name_and_args(call: ToolCallResponse) -> (String, serde_json::Value) {
        let args: serde_json::Value = serde_json::from_str(&call.function.arguments).unwrap();
        (call.function.name, args)
    }

    #[tokio::test]
    async fn test_qwen3_coder_simple_tool_call() {
        let input = r#"<tool_call>
<function=execute_bash>
<parameter=command>
pwd && ls
</parameter>
</function>
</tool_call>"#;
2634
        let (result, content) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
            .await
            .unwrap();
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "execute_bash");
        assert_eq!(args["command"], "pwd && ls");
    }

    #[tokio::test]
    async fn test_qwen3_coder_multiple_parameters() {
        let input = r#"<tool_call>
<function=get_current_weather>
<parameter=city>
Dallas
</parameter>
<parameter=state>
TX
</parameter>
<parameter=unit>
fahrenheit
</parameter>
</function>
</tool_call>"#;
2659
        let (result, content) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
            .await
            .unwrap();
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_current_weather");
        assert_eq!(args["city"], "Dallas");
        assert_eq!(args["state"], "TX");
        assert_eq!(args["unit"], "fahrenheit");
    }

    #[tokio::test]
    async fn test_qwen3_coder_with_normal_text() {
        let input = r#"I'll help you check the weather. <tool_call>
<function=get_current_weather>
<parameter=city>
San Francisco
</parameter>
<parameter=unit>
fahrenheit
</parameter>
</function>
</tool_call> Let me get that information for you."#;
2683
        let (result, content) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
            .await
            .unwrap();
        assert_eq!(
            content,
            Some(
                "I'll help you check the weather.  Let me get that information for you."
                    .to_string()
            )
        );
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_current_weather");
        assert_eq!(args["city"], "San Francisco");
        assert_eq!(args["unit"], "fahrenheit");
    }

    #[tokio::test]
    async fn test_qwen3_coder_parallel_tool_calls() {
        let input = r#"<tool_call>
<function=get_current_weather>
<parameter=city>
Dallas
</parameter>
<parameter=state>
TX
</parameter>
<parameter=unit>
fahrenheit
</parameter>
</function>
</tool_call>
<tool_call>
<function=get_current_weather>
<parameter=city>
Orlando
</parameter>
<parameter=state>
FL
</parameter>
<parameter=unit>
fahrenheit
</parameter>
</function>
</tool_call>"#;
2728
        let (result, content) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
            .await
            .unwrap();
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 2);

        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "get_current_weather");
        assert_eq!(args1["city"], "Dallas");
        assert_eq!(args1["state"], "TX");
        assert_eq!(args1["unit"], "fahrenheit");

        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "get_current_weather");
        assert_eq!(args2["city"], "Orlando");
        assert_eq!(args2["state"], "FL");
        assert_eq!(args2["unit"], "fahrenheit");
    }

    #[tokio::test]
    async fn test_qwen3_coder_json_parameter_value() {
        let input = r#"<tool_call>
<function=process_data>
<parameter=config>
{"timeout": 30, "retries": 3}
</parameter>
</function>
</tool_call>"#;
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
        let tools = vec![ToolDefinition {
            name: "process_data".to_string(),
            parameters: Some(serde_json::json!({
                "properties": {
                    "config": {
                        "type": "array"
                    }
                }
            })),
        }];
        let (result, content) =
            detect_and_parse_tool_call(input, Some("qwen3_coder"), Some(&tools))
                .await
                .unwrap();
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "process_data");
        assert!(args["config"].is_object());
        assert_eq!(args["config"]["timeout"], 30);
        assert_eq!(args["config"]["retries"], 3);
    }

    #[tokio::test]
    async fn test_qwen3_coder_numeric_parameters() {
        let input = r#"<tool_call>
<function=calculate>
<parameter=x>
42
</parameter>
<parameter=y>
3.15
</parameter>
<parameter=enabled>
true
</parameter>
</function>
</tool_call>"#;
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
        let tools = vec![ToolDefinition {
            name: "calculate".to_string(),
            parameters: Some(serde_json::json!({
                "properties": {
                    "x": {"type": "int"},
                    "y": {"type": "float"},
                    "enabled": {"type": "bool"},
                }
            })),
        }];
        let (result, _) = detect_and_parse_tool_call(input, Some("qwen3_coder"), Some(&tools))
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "calculate");
        assert_eq!(args["x"], 42);
        assert_eq!(args["y"], 3.15);
        assert_eq!(args["enabled"], true);
    }

    #[tokio::test]
    async fn test_qwen3_coder_no_tool_calls() {
        let input = "This is just normal text without any tool calls.";
2818
        let (result, content) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
2819
2820
2821
2822
2823
2824
2825
2826
2827
            .await
            .unwrap();
        assert_eq!(result.len(), 0);
        assert_eq!(content, Some(input.to_string()));
    }

    #[tokio::test]
    async fn test_qwen3_coder_compact_format() {
        let input = r#"<tool_call><function=search><parameter=query>rust programming</parameter><parameter=limit>10</parameter></function></tool_call>"#;
2828
        let (result, content) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
2829
2830
2831
2832
2833
2834
2835
            .await
            .unwrap();
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "search");
        assert_eq!(args["query"], "rust programming");
2836
        assert_eq!(args["limit"], "10");
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
    }

    #[tokio::test]
    async fn test_qwen3_coder_html_entities() {
        let input = r#"<tool_call>
<function=print_message>
<parameter=text>
&lt;div&gt;Hello &amp; Welcome&lt;/div&gt;
</parameter>
</function>
</tool_call>"#;
2848
        let (result, _) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "print_message");
        assert_eq!(args["text"], "<div>Hello & Welcome</div>");
    }

    #[tokio::test]
    async fn test_qwen3_coder_three_parallel_calls() {
        let input = r#"<tool_call>
<function=get_current_weather>
<parameter=city>
Dallas
</parameter>
</function>
</tool_call>
<tool_call>
<function=get_current_weather>
<parameter=city>
Orlando
</parameter>
</function>
</tool_call>
<tool_call>
<function=get_current_weather>
<parameter=city>
Seattle
</parameter>
</function>
</tool_call>"#;
2880
        let (result, content) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
            .await
            .unwrap();
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 3);

        let cities = ["Dallas", "Orlando", "Seattle"];
        for (i, expected_city) in cities.iter().enumerate() {
            let (name, args) = extract_name_and_args(result[i].clone());
            assert_eq!(name, "get_current_weather");
            assert_eq!(args["city"], *expected_city);
        }
    }

    #[tokio::test]
    async fn test_qwen3_coder_mixed_tool_types() {
        let input = r#"<tool_call>
<function=get_current_weather>
<parameter=city>
Dallas
</parameter>
<parameter=unit>
fahrenheit
</parameter>
</function>
</tool_call>
<tool_call>
<function=web_search>
<parameter=query>
weather forecasting
</parameter>
<parameter=max_results>
5
</parameter>
</function>
</tool_call>"#;
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
        let tools = vec![ToolDefinition {
            name: "web_search".to_string(),
            parameters: Some(serde_json::json!({
                "properties": {
                    "max_results": {
                        "type": "uint"
                    }
                }
            })),
        }];
        let (result, content) =
            detect_and_parse_tool_call(input, Some("qwen3_coder"), Some(&tools))
                .await
                .unwrap();
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 2);

        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "get_current_weather");
        assert_eq!(args1["city"], "Dallas");
        assert_eq!(args1["unit"], "fahrenheit");

        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "web_search");
        assert_eq!(args2["query"], "weather forecasting");
        assert_eq!(args2["max_results"], 5);
    }

    #[tokio::test]
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
    async fn test_qwen3_coder_array_parameter_value_without_tool_definition() {
        let input = r#"<tool_call>
<function=process_list>
<parameter=items>
[1, 2, 3, 4, 5]
</parameter>
</function>
</tool_call>"#;
        let (result, _) = detect_and_parse_tool_call(input, Some("qwen3_coder"), None)
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "process_list");
        // The default is to return it as a string.
        assert_eq!(args["items"], serde_json::json!("[1, 2, 3, 4, 5]"));
    }

    #[tokio::test]
    async fn test_qwen3_coder_array_parameter_value_with_tool_definition() {
2965
2966
2967
2968
2969
2970
2971
        let input = r#"<tool_call>
<function=process_list>
<parameter=items>
[1, 2, 3, 4, 5]
</parameter>
</function>
</tool_call>"#;
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
        let tools = vec![ToolDefinition {
            name: "process_list".to_string(),
            parameters: Some(serde_json::json!({
                "properties": {
                    "items": {
                        "type": "array"
                    }
                }
            })),
        }];
        let (result, _) = detect_and_parse_tool_call(input, Some("qwen3_coder"), Some(&tools))
2983
2984
2985
2986
2987
2988
2989
2990
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "process_list");
        assert!(args["items"].is_array());
        assert_eq!(args["items"], serde_json::json!([1, 2, 3, 4, 5]));
    }
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159

    // MiniMax-M2.1 parser tests
    #[tokio::test]
    async fn test_minimax_m2_simple_tool_call() {
        let input = r#"<minimax:tool_call>
<invoke name="get_weather">
<parameter name="location">San Francisco</parameter>
<parameter name="unit">celsius</parameter>
</invoke>
</minimax:tool_call>"#;
        let (result, content) = detect_and_parse_tool_call(input, Some("minimax_m2"), None)
            .await
            .unwrap();
        assert_eq!(content, Some("".to_string()));
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "San Francisco");
        assert_eq!(args["unit"], "celsius");
    }

    #[tokio::test]
    async fn test_minimax_m2_multiple_tool_calls() {
        let input = r#"<minimax:tool_call>
<invoke name="search_web">
<parameter name="query_tag">["technology", "events"]</parameter>
<parameter name="query_list">["OpenAI", "latest", "release"]</parameter>
</invoke>
<invoke name="search_web">
<parameter name="query_tag">["technology", "events"]</parameter>
<parameter name="query_list">["Gemini", "latest", "release"]</parameter>
</invoke>
</minimax:tool_call>"#;
        let tools = vec![ToolDefinition {
            name: "search_web".to_string(),
            parameters: Some(serde_json::json!({
                "properties": {
                    "query_tag": {"type": "array"},
                    "query_list": {"type": "array"}
                }
            })),
        }];
        let (result, _) = detect_and_parse_tool_call(input, Some("minimax_m2"), Some(&tools))
            .await
            .unwrap();
        assert_eq!(result.len(), 2);

        // First call
        let (name1, args1) = extract_name_and_args(result[0].clone());
        assert_eq!(name1, "search_web");
        assert!(args1["query_tag"].is_array());
        assert_eq!(
            args1["query_tag"],
            serde_json::json!(["technology", "events"])
        );
        assert!(args1["query_list"].is_array());
        assert_eq!(
            args1["query_list"],
            serde_json::json!(["OpenAI", "latest", "release"])
        );

        // Second call
        let (name2, args2) = extract_name_and_args(result[1].clone());
        assert_eq!(name2, "search_web");
        assert!(args2["query_tag"].is_array());
        assert_eq!(
            args2["query_tag"],
            serde_json::json!(["technology", "events"])
        );
        assert!(args2["query_list"].is_array());
        assert_eq!(
            args2["query_list"],
            serde_json::json!(["Gemini", "latest", "release"])
        );
    }

    #[tokio::test]
    async fn test_minimax_m2_with_normal_text() {
        let input = r#"I'll help you check the weather. <minimax:tool_call>
<invoke name="get_weather">
<parameter name="location">Tokyo</parameter>
<parameter name="unit">fahrenheit</parameter>
</invoke>
</minimax:tool_call> Let me get that information for you."#;
        let (result, content) = detect_and_parse_tool_call(input, Some("minimax_m2"), None)
            .await
            .unwrap();
        assert!(content.is_some());
        assert!(
            content
                .unwrap()
                .contains("I'll help you check the weather.")
        );
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "Tokyo");
        assert_eq!(args["unit"], "fahrenheit");
    }

    #[tokio::test]
    async fn test_minimax_m2_empty_parameters() {
        let input = r#"<minimax:tool_call>
<invoke name="get_time">
</invoke>
</minimax:tool_call>"#;
        let (result, _) = detect_and_parse_tool_call(input, Some("minimax_m2"), None)
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "get_time");
        assert_eq!(args, serde_json::json!({}));
    }

    #[tokio::test]
    async fn test_minimax_m2_with_type_conversion() {
        let input = r#"<minimax:tool_call>
<invoke name="process_data">
<parameter name="count">42</parameter>
<parameter name="temperature">98.6</parameter>
<parameter name="enabled">true</parameter>
</invoke>
</minimax:tool_call>"#;
        let tools = vec![ToolDefinition {
            name: "process_data".to_string(),
            parameters: Some(serde_json::json!({
                "properties": {
                    "count": {"type": "integer"},
                    "temperature": {"type": "number"},
                    "enabled": {"type": "boolean"}
                }
            })),
        }];
        let (result, _) = detect_and_parse_tool_call(input, Some("minimax_m2"), Some(&tools))
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "process_data");
        assert_eq!(args["count"], 42);
        assert_eq!(args["temperature"], 98.6);
        assert_eq!(args["enabled"], true);
    }

    #[tokio::test]
    async fn test_minimax_m2_array_parameter() {
        let input = r#"<minimax:tool_call>
<invoke name="batch_process">
<parameter name="items">[1, 2, 3, 4, 5]</parameter>
</invoke>
</minimax:tool_call>"#;
        let tools = vec![ToolDefinition {
            name: "batch_process".to_string(),
            parameters: Some(serde_json::json!({
                "properties": {
                    "items": {"type": "array"}
                }
            })),
        }];
        let (result, _) = detect_and_parse_tool_call(input, Some("minimax_m2"), Some(&tools))
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        let (name, args) = extract_name_and_args(result[0].clone());
        assert_eq!(name, "batch_process");
        assert!(args["items"].is_array());
        assert_eq!(args["items"], serde_json::json!([1, 2, 3, 4, 5]));
    }
3160
}