dynamo-parsers 5.1.3

Reasoning and tool-calling parsers for OpenAI-compatible inference output.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use serde_json::{Map, Number, Value};
use uuid::Uuid;

use super::super::ToolDefinition;
use super::super::config::MiniMaxM3ParserConfig;
use super::parsed_value::{coerce_integer_literal, raw_number_literal};
use super::response::{CalledFunction, ToolCallResponse, ToolCallType};

const MIN_PARTIAL_TOOL_CALL_START_LEN: usize = 3;

// Used by the streaming jail to detect a complete or split MiniMax M3 tool-call opener.
pub fn detect_tool_call_start_minimax_m3(chunk: &str, config: &MiniMaxM3ParserConfig) -> bool {
    let tool_call_start = tool_call_start(config);
    let invoke_start = invoke_start(config);

    for token in [tool_call_start.as_str(), invoke_start.as_str()] {
        if chunk.contains(token) || chunk_ends_with_partial_m3_marker(chunk, token) {
            return true;
        }
    }

    false
}

fn chunk_ends_with_partial_m3_marker(chunk: &str, marker: &str) -> bool {
    for (i, _) in marker.char_indices().skip(1) {
        if i < MIN_PARTIAL_TOOL_CALL_START_LEN {
            continue;
        }
        if chunk.ends_with(&marker[..i]) {
            return true;
        }
    }

    false
}

// Used by streaming jail to split the completed tool-call block from trailing content.
pub fn find_tool_call_end_position_minimax_m3(
    chunk: &str,
    config: &MiniMaxM3ParserConfig,
) -> usize {
    let tool_call_end = tool_call_end(config);
    chunk
        .find(tool_call_end.as_str())
        .map(|pos| pos + tool_call_end.len())
        .unwrap_or(chunk.len())
}

// Main entry point: strips normal prefix text and turns M3 tool markup into tool-call responses.
pub fn try_tool_call_parse_minimax_m3(
    message: &str,
    config: &MiniMaxM3ParserConfig,
    tools: Option<&[ToolDefinition]>,
) -> anyhow::Result<(Vec<ToolCallResponse>, Option<String>)> {
    let Some(start_pos) = message.find(tool_call_start(config).as_str()) else {
        if let Some(marker_idx) = first_orphan_minimax_m3_marker_index(message, config) {
            if let Some((prefix, calls)) = recover_orphan_invokes_in_span(message, config, tools)? {
                return Ok((calls, Some(prefix)));
            }
            return Ok((vec![], Some(message[..marker_idx].trim_end().to_string())));
        }
        return Ok((vec![], Some(message.to_string())));
    };

    let pre_block_span = &message[..start_pos];
    let (prefix, mut calls) = if let Some((prefix, recovered)) =
        recover_orphan_invokes_in_span(pre_block_span, config, tools)?
    {
        (prefix, recovered)
    } else if let Some(marker_idx) = first_orphan_minimax_m3_marker_index(pre_block_span, config) {
        (
            pre_block_span[..marker_idx].trim_end().to_string(),
            Vec::new(),
        )
    } else {
        (pre_block_span.to_string(), Vec::new())
    };

    let block_start = start_pos + tool_call_start(config).len();
    let tool_call_end = tool_call_end(config);
    let (block, complete) =
        if let Some(end_rel) = message[block_start..].find(tool_call_end.as_str()) {
            let block_end = block_start + end_rel;
            (&message[block_start..block_end], true)
        } else {
            (&message[block_start..], false)
        };

    if !complete && !config.allow_eof_recovery {
        return Ok((vec![], Some(prefix)));
    }

    calls.extend(parse_invokes(block, config, tools)?);
    Ok((calls, Some(prefix)))
}

// Builds the configured outer tool-call start marker.
fn tool_call_start(config: &MiniMaxM3ParserConfig) -> String {
    format!("{}<{}>", config.namespace_token, config.tool_call_tag)
}

// Builds the configured outer tool-call end marker.
fn tool_call_end(config: &MiniMaxM3ParserConfig) -> String {
    format!("{}</{}>", config.namespace_token, config.tool_call_tag)
}

// Builds the marker that introduces an individual function invocation before attributes.
fn invoke_start(config: &MiniMaxM3ParserConfig) -> String {
    format!("{}<invoke", config.namespace_token)
}

// Builds the marker that closes an individual function invocation.
fn invoke_end(config: &MiniMaxM3ParserConfig) -> String {
    format!("{}</invoke>", config.namespace_token)
}

// Builds the shared namespace prefix that starts any M3 XML-ish tag.
fn parameter_start(config: &MiniMaxM3ParserConfig) -> String {
    format!("{}<", config.namespace_token)
}

fn first_orphan_minimax_m3_marker_index(
    text: &str,
    config: &MiniMaxM3ParserConfig,
) -> Option<usize> {
    [
        tool_call_end(config),
        invoke_start(config),
        invoke_end(config),
        parameter_start(config),
    ]
    .iter()
    .filter_map(|marker| text.find(marker.as_str()))
    .min()
}

fn recover_orphan_invokes_in_span(
    span: &str,
    config: &MiniMaxM3ParserConfig,
    tools: Option<&[ToolDefinition]>,
) -> anyhow::Result<Option<(String, Vec<ToolCallResponse>)>> {
    let Some(marker_idx) = first_orphan_minimax_m3_marker_index(span, config) else {
        return Ok(None);
    };

    let marker_tail = &span[marker_idx..];
    if !marker_tail.starts_with(invoke_start(config).as_str()) {
        return Ok(None);
    }
    if !marker_tail.contains(tool_call_end(config).as_str()) && !config.allow_eof_recovery {
        return Ok(None);
    }

    let calls = parse_invokes(marker_tail, config, tools)?;
    if calls.is_empty() {
        return Ok(None);
    }

    Ok(Some((span[..marker_idx].trim_end().to_string(), calls)))
}

// Extracts one or more `<invoke name="...">` blocks from the outer tool-call block.
fn parse_invokes(
    block: &str,
    config: &MiniMaxM3ParserConfig,
    tools: Option<&[ToolDefinition]>,
) -> anyhow::Result<Vec<ToolCallResponse>> {
    let invoke_start = invoke_start(config);
    let invoke_end = invoke_end(config);
    let mut calls = Vec::new();
    let mut cursor = 0;

    while let Some(start_rel) = block[cursor..].find(invoke_start.as_str()) {
        let tag_attrs_start = cursor + start_rel + invoke_start.len();
        let Some(tag_end_rel) = block[tag_attrs_start..].find('>') else {
            break;
        };
        let tag_attrs = &block[tag_attrs_start..tag_attrs_start + tag_end_rel];
        let function_name = parse_invoke_name(tag_attrs);
        let body_start = tag_attrs_start + tag_end_rel + 1;
        let Some(body_end_rel) = block[body_start..].find(invoke_end.as_str()) else {
            break;
        };
        let body_end = body_start + body_end_rel;
        let function_body = &block[body_start..body_end];

        if let Some(function_name) = function_name
            && !function_name.is_empty()
        {
            let arguments = parse_parameters(&function_name, function_body, config, tools)?;
            calls.push(ToolCallResponse {
                id: format!("call-{}", Uuid::new_v4()),
                tp: ToolCallType::Function,
                function: CalledFunction {
                    name: function_name,
                    arguments: serde_json::to_string(&Value::Object(arguments))?,
                },
            });
        }

        cursor = body_end + invoke_end.len();
    }

    Ok(calls)
}

// Reads the `name` attribute from `<invoke ...>` using vLLM-compatible quoting variants.
fn parse_invoke_name(tag_attrs: &str) -> Option<String> {
    let attrs = tag_attrs.trim_start();
    let after_name = attrs.strip_prefix("name")?.trim_start();
    let value = after_name.strip_prefix('=')?.trim_start();

    if let Some(value) = value.strip_prefix('"') {
        return value.find('"').map(|end| value[..end].trim().to_string());
    }
    if let Some(value) = value.strip_prefix('\'') {
        return value.find('\'').map(|end| value[..end].trim().to_string());
    }

    let end = value.find(char::is_whitespace).unwrap_or(value.len());
    if end == 0 {
        None
    } else {
        Some(value[..end].trim().to_string())
    }
}

// Extracts MiniMax M3 parameter tags, where each parameter name is the tag name itself.
fn parse_parameters(
    function_name: &str,
    body: &str,
    config: &MiniMaxM3ParserConfig,
    tools: Option<&[ToolDefinition]>,
) -> anyhow::Result<Map<String, Value>> {
    let parameter_start = parameter_start(config);
    let param_config = get_arguments_config(function_name, tools);
    let mut parameters = Map::new();
    let mut cursor = 0;

    while let Some(start_rel) = body[cursor..].find(parameter_start.as_str()) {
        let start = cursor + start_rel + parameter_start.len();
        if body[start..].starts_with('/') {
            cursor = start + 1;
            continue;
        }

        let Some(name_end_rel) = body[start..].find('>') else {
            break;
        };
        let parameter_name = &body[start..start + name_end_rel];
        if parameter_name.is_empty() || parameter_name.contains(char::is_whitespace) {
            cursor = start + name_end_rel + 1;
            continue;
        }

        let value_start = start + name_end_rel + 1;
        let parameter_end = format!("{}</{}>", config.namespace_token, parameter_name);
        let Some(value_end_rel) = body[value_start..].find(parameter_end.as_str()) else {
            break;
        };
        let value_end = value_start + value_end_rel;
        let raw_value = &body[value_start..value_end];
        let schema = param_config.get(parameter_name);
        let value = parse_parameter_value(raw_value, schema, config);
        insert_parameter(&mut parameters, parameter_name.to_string(), value);

        cursor = value_end + parameter_end.len();
    }

    Ok(parameters)
}

// Preserves duplicate XML tags by collecting repeated values into arrays.
fn insert_parameter(parameters: &mut Map<String, Value>, key: String, value: Value) {
    if let Some(existing) = parameters.remove(&key) {
        let merged = match existing {
            Value::Array(mut values) => {
                values.push(value);
                Value::Array(values)
            }
            existing => Value::Array(vec![existing, value]),
        };
        parameters.insert(key, merged);
    } else {
        parameters.insert(key, value);
    }
}

// Chooses scalar conversion or nested XML parsing based on whether the value contains M3 tags.
fn parse_parameter_value(
    raw: &str,
    schema: Option<&Value>,
    config: &MiniMaxM3ParserConfig,
) -> Value {
    if raw.contains(parameter_start(config).as_str()) {
        parse_nested_minimax_xml(raw, schema.cloned(), config)
    } else {
        convert_scalar_value(raw, schema)
    }
}

// Parses nested parameter bodies such as arrays of `<item>` objects into JSON values.
fn parse_nested_minimax_xml(
    raw: &str,
    schema: Option<Value>,
    config: &MiniMaxM3ParserConfig,
) -> Value {
    let chunks: Vec<&str> = raw.split(config.namespace_token.as_str()).collect();
    let leading_text = chunks.first().copied().unwrap_or_default();
    let root_value = if schema_has_type(schema.as_ref(), "array")
        && chunks
            .get(1)
            .is_some_and(|chunk| chunk.starts_with("<item>"))
    {
        Some(StackValue::Array(Vec::new()))
    } else {
        Some(StackValue::Object(Map::new()))
    };
    let mut stack = vec![StackItem {
        tag: None,
        value: root_value,
        texts: if leading_text.is_empty() {
            Vec::new()
        } else {
            vec![leading_text.to_string()]
        },
        schema,
    }];

    for (chunk_index, chunk) in chunks.iter().enumerate().skip(1) {
        if chunk.starts_with("</") {
            let (tag, trailing_text) = split_end_tag_chunk(chunk);
            while stack.len() > 1 {
                let item = stack.pop().expect("stack has child item");
                let matched = item.tag.as_deref() == Some(tag.as_str());
                stack
                    .last_mut()
                    .expect("stack has parent item")
                    .append(item);
                if matched {
                    break;
                }
            }
            if !trailing_text.is_empty() {
                stack
                    .last_mut()
                    .expect("stack has current item")
                    .append_text(trailing_text);
            }
        } else if chunk.starts_with('<') {
            let (tag, trailing_text) = split_start_tag_chunk(chunk);
            if tag.is_empty() {
                continue;
            }
            let child_schema = stack
                .last()
                .expect("stack has current item")
                .schema_for_child(tag.as_str());
            let child_value = if schema_has_type(child_schema.as_ref(), "array")
                && chunks
                    .get(chunk_index + 1)
                    .is_some_and(|next| next.starts_with("<item>"))
            {
                Some(StackValue::Array(Vec::new()))
            } else if schema_has_type(child_schema.as_ref(), "object") {
                Some(StackValue::Object(Map::new()))
            } else {
                None
            };
            stack.push(StackItem {
                tag: Some(tag),
                value: child_value,
                texts: if trailing_text.is_empty() {
                    Vec::new()
                } else {
                    vec![trailing_text.to_string()]
                },
                schema: child_schema,
            });
        } else if !chunk.is_empty() {
            stack
                .last_mut()
                .expect("stack has current item")
                .append_text(chunk);
        }
    }

    while stack.len() > 1 {
        let item = stack.pop().expect("stack has child item");
        stack
            .last_mut()
            .expect("stack has parent item")
            .append(item);
    }

    stack.pop().expect("root item exists").into_value()
}

// Splits a start-tag chunk into its tag name and any text after `>`.
fn split_start_tag_chunk(chunk: &str) -> (String, &str) {
    let Some(gt) = chunk.find('>') else {
        return (chunk.trim_start_matches('<').to_string(), "");
    };
    (chunk[1..gt].to_string(), &chunk[gt + 1..])
}

// Splits an end-tag chunk into its tag name and any text after `>`.
fn split_end_tag_chunk(chunk: &str) -> (String, &str) {
    let Some(gt) = chunk.find('>') else {
        return (chunk.trim_start_matches("</").to_string(), "");
    };
    (chunk[2..gt].to_string(), &chunk[gt + 1..])
}

#[derive(Debug)]
enum StackValue {
    Object(Map<String, Value>),
    Array(Vec<Value>),
}

#[derive(Debug)]
struct StackItem {
    tag: Option<String>,
    value: Option<StackValue>,
    texts: Vec<String>,
    schema: Option<Value>,
}

impl StackItem {
    // Converts a stack node into the JSON value it represents.
    fn into_value(self) -> Value {
        match self.value {
            None => convert_scalar_value(self.texts.join("").as_str(), self.schema.as_ref()),
            Some(StackValue::Object(mut map)) => {
                if !self.texts.is_empty() {
                    let mut text_key = "$text".to_string();
                    while map.contains_key(&text_key) {
                        text_key = format!("${text_key}");
                    }
                    map.insert(text_key, Value::String(self.texts.join("")));
                }
                Value::Object(map)
            }
            Some(StackValue::Array(values)) => Value::Array(values),
        }
    }

    // Attaches a completed child node to the current object, array, or implicit object.
    fn append(&mut self, item: StackItem) {
        let key = item.tag.clone().unwrap_or_default();
        let value = item.into_value();
        match self.value.as_mut() {
            None => {
                let mut map = Map::new();
                map.insert(key, value);
                self.value = Some(StackValue::Object(map));
            }
            Some(StackValue::Object(map)) => insert_parameter(map, key, value),
            Some(StackValue::Array(values)) => values.push(value),
        }
    }

    // Adds text to the current node, coercing array items through item schema when available.
    fn append_text(&mut self, text: &str) {
        if let Some(StackValue::Array(values)) = self.value.as_mut() {
            let item_schema = schema_array_item(self.schema.as_ref());
            values.push(convert_scalar_value(text, item_schema.as_ref()));
        } else {
            self.texts.push(text.to_string());
        }
    }

    // Finds the schema that should be used for a nested child tag.
    fn schema_for_child(&self, tag: &str) -> Option<Value> {
        if tag == "item"
            && let Some(item_schema) = schema_array_item(self.schema.as_ref())
        {
            return Some(item_schema);
        }

        let schema = self.schema.as_ref()?;
        if let Some(child_schema) = schema
            .get("properties")
            .and_then(|properties| properties.get(tag))
        {
            return Some(child_schema.clone());
        }

        schema
            .get("additionalProperties")
            .filter(|additional| additional.is_object())
            .cloned()
    }
}

// Looks up the selected tool's parameter schema so parsed strings can be type-coerced.
fn get_arguments_config(func_name: &str, tools: Option<&[ToolDefinition]>) -> Map<String, Value> {
    let Some(tools) = tools else {
        return Map::new();
    };

    for tool in tools {
        if tool.name == func_name {
            let Some(params) = &tool.parameters else {
                return Map::new();
            };
            if let Some(properties) = params.get("properties").and_then(Value::as_object) {
                return properties.clone();
            }
            if let Some(params_obj) = params.as_object() {
                return params_obj.clone();
            }
            return Map::new();
        }
    }

    tracing::warn!("Tool '{}' is not defined in the tools list.", func_name);
    Map::new()
}

// Converts a scalar XML text value into the schema-expected JSON type when possible.
fn convert_scalar_value(raw: &str, schema: Option<&Value>) -> Value {
    let value = html_unescape(raw);
    let trimmed = value.trim();
    if trimmed.eq_ignore_ascii_case("null") {
        return Value::Null;
    }

    let Some(schema) = schema else {
        return Value::String(value);
    };

    if schema_has_type(Some(schema), "string") || schema_has_type(Some(schema), "enum") {
        return Value::String(value);
    }
    if schema_has_type(Some(schema), "integer") {
        return coerce_integer_literal(trimmed)
            .and_then(|parsed| serde_json::to_value(parsed).ok())
            .unwrap_or(Value::String(value));
    }
    if schema_has_type(Some(schema), "number") {
        if let Some(parsed) = coerce_integer_literal(trimmed)
            && let Ok(json) = serde_json::to_value(parsed)
        {
            return json;
        }
        if let Ok(number) = trimmed.parse::<f64>()
            && let Some(number) = Number::from_f64(number)
        {
            return Value::Number(number);
        }
        if let Some(parsed) = raw_number_literal(trimmed)
            && let Ok(json) = serde_json::to_value(parsed)
        {
            return json;
        }
        return Value::String(value);
    }
    if schema_has_type(Some(schema), "boolean") {
        return match trimmed.to_ascii_lowercase().as_str() {
            "true" => Value::Bool(true),
            "1" => Value::Bool(true),
            "false" => Value::Bool(false),
            "0" => Value::Bool(false),
            _ => Value::String(value),
        };
    }
    if schema_has_type(Some(schema), "object") {
        if trimmed.is_empty() {
            return Value::Object(Map::new());
        }
        if let Ok(json) = serde_json::from_str::<Value>(trimmed) {
            return json;
        }
    }
    if schema_has_type(Some(schema), "array") {
        if trimmed.is_empty() {
            return Value::Array(Vec::new());
        }
        if let Ok(json) = serde_json::from_str::<Value>(trimmed) {
            return json;
        }
    }

    Value::String(value)
}

// Checks JSON Schema `type`, `anyOf`, and `oneOf` for a target primitive/container type.
fn schema_has_type(schema: Option<&Value>, expected: &str) -> bool {
    let Some(schema) = schema else {
        return false;
    };
    if let Some(ty) = schema.get("type") {
        if ty.as_str() == Some(expected) {
            return true;
        }
        if let Some(types) = ty.as_array()
            && types.iter().any(|ty| ty.as_str() == Some(expected))
        {
            return true;
        }
    }
    for key in ["anyOf", "oneOf"] {
        if let Some(options) = schema.get(key).and_then(Value::as_array)
            && options
                .iter()
                .any(|option| schema_has_type(Some(option), expected))
        {
            return true;
        }
    }
    false
}

// Finds the schema for array elements, including schemas wrapped in `anyOf` or `oneOf`.
fn schema_array_item(schema: Option<&Value>) -> Option<Value> {
    schema
        .and_then(|schema| schema.get("items"))
        .cloned()
        .or_else(|| {
            schema.and_then(|schema| {
                for key in ["anyOf", "oneOf"] {
                    if let Some(options) = schema.get(key).and_then(Value::as_array) {
                        for option in options {
                            if let Some(items) = option.get("items") {
                                return Some(items.clone());
                            }
                        }
                    }
                }
                None
            })
        })
}

// Decodes common XML/HTML entities so tool arguments receive the intended literal text.
fn html_unescape(s: &str) -> String {
    s.replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&amp;", "&")
        .replace("&quot;", "\"")
        .replace("&#x27;", "'")
        .replace("&#39;", "'")
}

#[cfg(test)]
mod tests {
    use super::*;

    fn recovery_config() -> MiniMaxM3ParserConfig {
        MiniMaxM3ParserConfig {
            allow_eof_recovery: true,
            ..Default::default()
        }
    }

    fn call_name_and_args(call: &ToolCallResponse) -> (String, Value) {
        (
            call.function.name.clone(),
            serde_json::from_str(&call.function.arguments).expect("valid JSON arguments"),
        )
    }

    #[test]
    fn detect_tool_call_start_minimax_m3_rejects_short_suffix_false_positives() {
        let config = MiniMaxM3ParserConfig::default();

        assert!(!detect_tool_call_start_minimax_m3(
            "See reference [1]",
            &config
        ));
        assert!(!detect_tool_call_start_minimax_m3(
            "ordinary text ending in ]",
            &config
        ));
        assert!(!detect_tool_call_start_minimax_m3(
            "ordinary text ending in ]<",
            &config
        ));
        assert!(detect_tool_call_start_minimax_m3(
            "split opener ]<]",
            &config
        ));
        assert!(detect_tool_call_start_minimax_m3(
            "full opener ]<]minimax[>[<tool_call>",
            &config
        ));
        assert!(detect_tool_call_start_minimax_m3(
            "bare invoke ]<]minimax[>[<invoke",
            &config
        ));
        assert!(detect_tool_call_start_minimax_m3(
            "split bare invoke ]<]minimax[>[<inv",
            &config
        ));
    }

    #[test]
    fn recovers_complete_bare_invoke_without_outer_tool_call() {
        let config = recovery_config();
        let input = r#"prefix ]<]minimax[>[<invoke name="get_weather">
]<]minimax[>[<location>NYC]<]minimax[>[</location>
]<]minimax[>[</invoke>
]<]minimax[>[</invoke>"#;

        let (calls, normal_text) = try_tool_call_parse_minimax_m3(input, &config, None).unwrap();

        assert_eq!(normal_text.as_deref(), Some("prefix"));
        assert_eq!(calls.len(), 1);
        let (name, args) = call_name_and_args(&calls[0]);
        assert_eq!(name, "get_weather");
        assert_eq!(args["location"], "NYC");
    }

    #[test]
    fn non_recovery_path_strips_bare_invoke_without_claiming_call() {
        let config = MiniMaxM3ParserConfig::default();
        let input = r#"prefix ]<]minimax[>[<invoke name="get_weather">
]<]minimax[>[<location>NYC]<]minimax[>[</location>
]<]minimax[>[</invoke>"#;

        let (calls, normal_text) = try_tool_call_parse_minimax_m3(input, &config, None).unwrap();

        assert!(calls.is_empty());
        assert_eq!(normal_text.as_deref(), Some("prefix"));
    }

    #[test]
    fn strips_incomplete_bare_invoke_marker_tail() {
        let config = recovery_config();
        let input = r#"prefix ]<]minimax[>[<invoke name="get_weather">
]<]minimax[>[<location>NY"#;

        let (calls, normal_text) = try_tool_call_parse_minimax_m3(input, &config, None).unwrap();

        assert!(calls.is_empty());
        assert_eq!(normal_text.as_deref(), Some("prefix"));
    }

    #[test]
    fn parses_nested_arguments_with_custom_namespace_token() {
        let config = MiniMaxM3ParserConfig {
            namespace_token: "NS|".to_string(),
            ..recovery_config()
        };
        let input = r#"NS|<tool_call>
NS|<invoke name="create_order">
NS|<shipping>NS|<city>SingaporeNS|</city>NS|<zip>18956NS|</zip>NS|</shipping>
NS|</invoke>
NS|</tool_call>"#;
        let tools = vec![ToolDefinition {
            name: "create_order".to_string(),
            parameters: Some(serde_json::json!({
                "type": "object",
                "properties": {
                    "shipping": {
                        "type": "object",
                        "properties": {
                            "city": { "type": "string" },
                            "zip": { "type": "integer" }
                        }
                    }
                }
            })),
            strict: None,
        }];

        let (calls, normal_text) =
            try_tool_call_parse_minimax_m3(input, &config, Some(&tools)).unwrap();

        assert_eq!(normal_text.as_deref(), Some(""));
        assert_eq!(calls.len(), 1);
        let (name, args) = call_name_and_args(&calls[0]);
        assert_eq!(name, "create_order");
        assert_eq!(args["shipping"]["city"], "Singapore");
        assert_eq!(args["shipping"]["zip"], 18956);
    }
}