forked from ggml-org/llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-chat-auto-parser.cpp
More file actions
1969 lines (1646 loc) · 86.3 KB
/
test-chat-auto-parser.cpp
File metadata and controls
1969 lines (1646 loc) · 86.3 KB
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "chat-auto-parser-helpers.h"
#include "chat-auto-parser.h"
#include "chat-peg-parser.h"
#include "chat.h"
#include "peg-parser.h"
#include "testing.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace autoparser;
static void test_calculate_diff_split_basic(testing & t);
static void test_calculate_diff_split_identical(testing & t);
static void test_calculate_diff_split_common_prefix(testing & t);
static void test_calculate_diff_split_common_suffix(testing & t);
static void test_calculate_diff_split_common_both(testing & t);
static void test_calculate_diff_split_empty_cases(testing & t);
static void test_calculate_diff_split_no_common(testing & t);
static void test_calculate_diff_split_single_char(testing & t);
static void test_calculate_diff_split_overlaps(testing & t);
static void test_calculate_diff_split_tag_boundaries(testing & t);
static void test_calculate_diff_split_generation_prompt(testing & t);
static void test_calculate_diff_split(testing & t);
static void test_until_common_prefix_basic(testing & t);
static void test_until_common_prefix(testing & t);
static void test_after_common_suffix_basic(testing & t);
static void test_after_common_suffix(testing & t);
static void test_analyze_tool_call_pure_json(testing & t);
static void test_analyze_tool_call_function_name_markers(testing & t);
static void test_analyze_tool_call_full_markers(testing & t);
static void test_analyze_tool_call_edge_cases(testing & t);
static void test_compare_variants_basic(testing & t);
static void test_compare_variants_messages_modifier(testing & t);
static void test_compare_variants_tools_modifier(testing & t);
static void test_compare_variants_both_modifiers(testing & t);
static void test_compare_variants_template_failure(testing & t);
static void test_compare_variants_identity(testing & t);
static void test_compare_variants(testing & t);
// Seed-OSS template tool calling analysis tests
static void test_seed_oss_tool_analysis(testing & t);
static void test_seed_oss_tool_presence(testing & t);
static void test_seed_oss_call_count(testing & t);
static void test_seed_oss_function_names(testing & t);
static void test_seed_oss_argument_count(testing & t);
static void test_seed_oss_args_presence(testing & t);
static void test_seed_oss_tool_with_reasoning(testing & t);
// Nemotron template analysis tests
static void test_nemotron_analysis(testing & t);
static void test_nemotron_reasoning_detection(testing & t);
static void test_nemotron_tool_format(testing & t);
// CohereForAI template analysis tests
static void test_cohere_reasoning_detection(testing & t);
static void test_cohere_analysis(testing & t);
// SmolLM3 template analysis tests
static void test_smollm3_analysis(testing & t);
// Marker separation
static void test_marker_separation(testing & t);
// standard_json_tools format tests
static void test_standard_json_tools_formats(testing & t);
static void test_standard_json_tools_openai(testing & t);
static void test_standard_json_tools_cohere(testing & t);
static void test_standard_json_tools_function_key(testing & t);
// normalize_quotes_to_json tests
static void test_normalize_quotes_to_json(testing & t);
static void test_normalize_quotes_with_embedded_quotes(testing & t);
// TAG_WITH_TAGGED argument parsing tests
static void test_tagged_args_with_embedded_quotes(testing & t);
int main(int argc, char * argv[]) {
testing t(std::cout);
t.verbose = true;
// usage: test-chat-auto-parser-helpers [filter_regex]
if (argc > 1) {
t.set_filter(argv[1]);
}
t.test("diff_split", test_calculate_diff_split);
t.test("common_prefix", test_until_common_prefix);
t.test("common_suffix", test_after_common_suffix);
t.test("compare_variants", test_compare_variants);
t.test("segments", test_marker_separation);
t.test("seed_oss_diffs", test_seed_oss_tool_analysis);
t.test("cohere", test_cohere_analysis);
t.test("nemotron", test_nemotron_analysis);
t.test("smollm3", test_smollm3_analysis);
t.test("standard_json_tools", test_standard_json_tools_formats);
t.test("normalize_quotes_to_json", test_normalize_quotes_to_json);
t.test("tagged_args_embedded_quotes", test_tagged_args_with_embedded_quotes);
return t.summary();
}
static void test_marker_separation(testing & t) {
auto single_square_marker = segmentize_markers("pre_marker[marker]post_marker");
auto single_diag_marker = segmentize_markers("pre_marker<marker>post_marker");
auto paired_markers = segmentize_markers("<hello>world</hello>");
auto double_different_markers = segmentize_markers("<hello>[hello]<world>[world]");
auto in_between = segmentize_markers("im<blue>daba<dee>da[hey]");
t.test("single_square_marker", [&] (testing & t) {
t.assert_equal("first is text", segment_type::TEXT, single_square_marker[0].type);
t.assert_equal("second is marker", segment_type::MARKER, single_square_marker[1].type);
t.assert_equal("last is text", segment_type::TEXT, single_square_marker[2].type);
t.assert_equal("first is 'pre_marker'", "pre_marker", single_square_marker[0].value);
t.assert_equal("second is '[marker]'", "[marker]", single_square_marker[1].value);
t.assert_equal("last is 'post_marker'", "post_marker", single_square_marker[2].value);
});
t.test("single_diagonal_marker", [&] (testing & t) {
t.assert_equal("first is text", segment_type::TEXT, single_diag_marker[0].type);
t.assert_equal("second is marker", segment_type::MARKER, single_diag_marker[1].type);
t.assert_equal("last is text", segment_type::TEXT, single_diag_marker[2].type);
t.assert_equal("first is 'pre_marker'", "pre_marker", single_diag_marker[0].value);
t.assert_equal("second is '<marker>'", "<marker>", single_diag_marker[1].value);
t.assert_equal("last is 'post_marker'", "post_marker", single_diag_marker[2].value);
});
t.test("paired_markers", [&] (testing & t) {
t.assert_equal("first is marker", segment_type::MARKER, paired_markers[0].type);
t.assert_equal("second is text", segment_type::TEXT, paired_markers[1].type);
t.assert_equal("third is marker", segment_type::MARKER, paired_markers[2].type);
t.assert_equal("first is '<hello>'", "<hello>", paired_markers[0].value);
t.assert_equal("second is 'world'", "world", paired_markers[1].value);
t.assert_equal("third is '</hello>'", "</hello>", paired_markers[2].value);
});
t.test("double_different_markers", [&] (testing & t) {
t.assert_equal("first is marker", segment_type::MARKER, double_different_markers[0].type);
t.assert_equal("second is marker", segment_type::MARKER, double_different_markers[1].type);
t.assert_equal("third is marker", segment_type::MARKER, double_different_markers[2].type);
t.assert_equal("fourth is marker", segment_type::MARKER, double_different_markers[3].type);
t.assert_equal("first is '<hello>'", "<hello>", double_different_markers[0].value);
t.assert_equal("second is '[hello]'", "[hello]", double_different_markers[1].value);
t.assert_equal("third is '<world>'", "<world>", double_different_markers[2].value);
t.assert_equal("fourth is '[world]'", "[world]", double_different_markers[3].value);
});
t.test("in_between", [&] (testing & t) {
t.assert_equal("first is text", segment_type::TEXT, in_between[0].type);
t.assert_equal("second is marker", segment_type::MARKER, in_between[1].type);
t.assert_equal("third is text", segment_type::TEXT, in_between[2].type);
t.assert_equal("fourth is marker", segment_type::MARKER, in_between[3].type);
t.assert_equal("fifth is text", segment_type::TEXT, in_between[4].type);
t.assert_equal("sixth is marker", segment_type::MARKER, in_between[5].type);
t.assert_equal("first is 'im'", "im", in_between[0].value);
t.assert_equal("second is '<blue>'", "<blue>", in_between[1].value);
t.assert_equal("third is 'daba'", "daba", in_between[2].value);
t.assert_equal("fourth is '<dee>'", "<dee>", in_between[3].value);
t.assert_equal("fifth is 'da'", "da", in_between[4].value);
t.assert_equal("sixth is '[hey]'", "[hey]", in_between[5].value);
});
}
static void test_calculate_diff_split(testing & t) {
t.test("calculate_diff_split basic", test_calculate_diff_split_basic);
t.test("calculate_diff_split identical", test_calculate_diff_split_identical);
t.test("calculate_diff_split common prefix", test_calculate_diff_split_common_prefix);
t.test("calculate_diff_split common suffix", test_calculate_diff_split_common_suffix);
t.test("calculate_diff_split common both", test_calculate_diff_split_common_both);
t.test("calculate_diff_split empty cases", test_calculate_diff_split_empty_cases);
t.test("calculate_diff_split no common", test_calculate_diff_split_no_common);
t.test("calculate_diff_split single char", test_calculate_diff_split_single_char);
t.test("calculate_diff_split overlaps", test_calculate_diff_split_overlaps);
t.test("calculate_diff_split tag boundaries", test_calculate_diff_split_tag_boundaries);
t.test("calculate_diff_split generation prompt", test_calculate_diff_split_generation_prompt);
}
static void test_calculate_diff_split_basic(testing & t) {
diff_split result = calculate_diff_split("hello world", "hello test");
t.assert_equal("prefix should be 'hello '", "hello ", result.prefix);
t.assert_equal("left should be 'world'", "world", result.left);
t.assert_equal("right should be 'test'", "test", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("abc", "xyz");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'abc'", "abc", result.left);
t.assert_equal("right should be 'xyz'", "xyz", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("prefixA suffix", "prefixB suffix");
t.assert_equal("prefix should be 'prefix'", "prefix", result.prefix);
t.assert_equal("left should be 'A'", "A", result.left);
t.assert_equal("right should be 'B'", "B", result.right);
t.assert_equal("suffix should be ' suffix'", " suffix", result.suffix);
}
static void test_calculate_diff_split_identical(testing & t) {
diff_split result = calculate_diff_split("hello", "hello");
t.assert_equal("prefix should be 'hello'", "hello", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("", "");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("a", "a");
t.assert_equal("prefix should be 'a'", "a", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("<row><row><row><your><boat><gently>", "<row><row><row><your><boat><gently>");
t.assert_equal("prefix should be '<row><row><row><your><boat><gently>'", "<row><row><row><your><boat><gently>", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
}
static void test_calculate_diff_split_common_prefix(testing & t) {
diff_split result = calculate_diff_split("abcdef", "abcxyz");
t.assert_equal("prefix should be 'abc'", "abc", result.prefix);
t.assert_equal("left should be 'def'", "def", result.left);
t.assert_equal("right should be 'xyz'", "xyz", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("same", "sameagain");
t.assert_equal("prefix should be 'same'", "same", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be 'again'", "again", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("test", "testing");
t.assert_equal("prefix should be 'test'", "test", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be 'ing'", "ing", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
}
static void test_calculate_diff_split_common_suffix(testing & t) {
diff_split result = calculate_diff_split("123end", "456end");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be '123'", "123", result.left);
t.assert_equal("right should be '456'", "456", result.right);
t.assert_equal("suffix should be 'end'", "end", result.suffix);
result = calculate_diff_split("start", "end");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'start'", "start", result.left);
t.assert_equal("right should be 'end'", "end", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("abcsuffix", "xyzsuffix");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'abc'", "abc", result.left);
t.assert_equal("right should be 'xyz'", "xyz", result.right);
t.assert_equal("suffix should be 'suffix'", "suffix", result.suffix);
}
static void test_calculate_diff_split_common_both(testing & t) {
diff_split result = calculate_diff_split("helloXworld", "helloYworld");
t.assert_equal("prefix should be 'hello'", "hello", result.prefix);
t.assert_equal("left should be 'X'", "X", result.left);
t.assert_equal("right should be 'Y'", "Y", result.right);
t.assert_equal("suffix should be 'world'", "world", result.suffix);
result = calculate_diff_split("ABCmiddleXYZ", "ABCdifferentXYZ");
t.assert_equal("prefix should be 'ABC'", "ABC", result.prefix);
t.assert_equal("left should be 'middle'", "middle", result.left);
t.assert_equal("right should be 'different'", "different", result.right);
t.assert_equal("suffix should be 'XYZ'", "XYZ", result.suffix);
result = calculate_diff_split("startAend", "startBend");
t.assert_equal("prefix should be 'start'", "start", result.prefix);
t.assert_equal("left should be 'A'", "A", result.left);
t.assert_equal("right should be 'B'", "B", result.right);
t.assert_equal("suffix should be 'end'", "end", result.suffix);
// Edge case: common prefix and suffix overlap
result = calculate_diff_split("aa", "ab");
t.assert_equal("prefix should be 'a'", "a", result.prefix);
t.assert_equal("left should be 'a'", "a", result.left);
t.assert_equal("right should be 'b'", "b", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
}
static void test_calculate_diff_split_empty_cases(testing & t) {
// Empty left, non-empty right
diff_split result = calculate_diff_split("", "hello");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be 'hello'", "hello", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Non-empty left, empty right
result = calculate_diff_split("hello", "");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'hello'", "hello", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Both empty
result = calculate_diff_split("", "");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Left single char, empty right
result = calculate_diff_split("a", "");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'a'", "a", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Empty left, right single char
result = calculate_diff_split("", "a");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be 'a'", "a", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
}
static void test_calculate_diff_split_no_common(testing & t) {
diff_split result = calculate_diff_split("abc", "xyz");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'abc'", "abc", result.left);
t.assert_equal("right should be 'xyz'", "xyz", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("left", "right");
// The algorithm finds "t" as a common suffix since both strings end with 't'
// This is the algorithm's actual behavior - it finds maximal common suffix
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'lef'", "lef", result.left);
t.assert_equal("right should be 'righ'", "righ", result.right);
t.assert_equal("suffix should be 't'", "t", result.suffix);
result = calculate_diff_split("123", "456");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be '123'", "123", result.left);
t.assert_equal("right should be '456'", "456", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
}
static void test_calculate_diff_split_single_char(testing & t) {
diff_split result = calculate_diff_split("a", "b");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'a'", "a", result.left);
t.assert_equal("right should be 'b'", "b", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("a", "a");
t.assert_equal("prefix should be 'a'", "a", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("a", "ab");
t.assert_equal("prefix should be 'a'", "a", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be 'b'", "b", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("ab", "a");
t.assert_equal("prefix should be 'a'", "a", result.prefix);
t.assert_equal("left should be 'b'", "b", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
}
static void test_calculate_diff_split_overlaps(testing & t) {
// One string is substring of another
diff_split result = calculate_diff_split("test", "testing");
t.assert_equal("prefix should be 'test'", "test", result.prefix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be 'ing'", "ing", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
result = calculate_diff_split("testing", "test");
t.assert_equal("prefix should be 'test'", "test", result.prefix);
t.assert_equal("left should be 'ing'", "ing", result.left);
t.assert_equal("right should be empty", "", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Similar strings with one extra char at start
result = calculate_diff_split("Xtest", "Ytest");
// The algorithm finds "test" as a common suffix since both strings end with "test"
// This is the algorithm's actual behavior - it finds maximal common suffix
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'X'", "X", result.left);
t.assert_equal("right should be 'Y'", "Y", result.right);
t.assert_equal("suffix should be 'test'", "test", result.suffix);
// Similar strings with one extra char at end
result = calculate_diff_split("testX", "testY");
t.assert_equal("prefix should be 'test'", "test", result.prefix);
t.assert_equal("left should be 'X'", "X", result.left);
t.assert_equal("right should be 'Y'", "Y", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Strings that are reverses
result = calculate_diff_split("abc", "cba");
t.assert_equal("prefix should be empty", "", result.prefix);
t.assert_equal("left should be 'abc'", "abc", result.left);
t.assert_equal("right should be 'cba'", "cba", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
}
static void test_calculate_diff_split_tag_boundaries(testing & t) {
// Test with unclosed XML tags
diff_split result = calculate_diff_split("test<tag", "test>content");
// The fix_tag_boundaries should move incomplete tags appropriately
t.assert_true("prefix should start with 'test'", result.prefix.find("test") == 0);
t.assert_true("should handle tag boundaries", result.left != "" || result.right != "" || result.suffix != "");
// Test with unclosed brackets
result = calculate_diff_split("test[", "test]value");
t.assert_true("should handle bracket boundaries", result.left != "" || result.right != "" || result.suffix != "");
// Test with partial tags on both sides
result = calculate_diff_split("prefix<tag>", "prefix</tag>suffix");
// fix_tag_boundaries moves the incomplete '<' from prefix to left/right
t.assert_equal("prefix should be 'prefix'", "prefix", result.prefix);
t.assert_equal("left should be '<tag>'", "<tag>", result.left);
t.assert_equal("right should be '</tag>suffix'", "</tag>suffix", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Test with complex nested tags
result = calculate_diff_split("prefix<div>content</div>", "prefix<div>different</div>");
// Algorithm finds "ent</div>" as a common suffix because both strings end with it
// This is the actual algorithm behavior, though not semantically ideal
t.assert_equal("prefix should be 'prefix<div>'", "prefix<div>", result.prefix);
t.assert_equal("left should be 'cont'", "cont", result.left);
t.assert_equal("right should be 'differ'", "differ", result.right);
t.assert_equal("suffix should be 'ent</div>'", "ent</div>", result.suffix);
// Test with unclosed angle bracket
result = calculate_diff_split("Hello <world>", "Hello test");
t.assert_equal("prefix should be 'Hello '", "Hello ", result.prefix);
t.assert_true("left should contain '<world>'", result.left.find("<world>") != std::string::npos);
t.assert_equal("right should be 'test'", "test", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Test with unclosed square bracket
result = calculate_diff_split("test [array]", "test other");
t.assert_equal("prefix should be 'test '", "test ", result.prefix);
t.assert_true("left should contain '[array]'", result.left.find("[array]") != std::string::npos);
t.assert_equal("right should be 'other'", "other", result.right);
t.assert_equal("suffix should be empty", "", result.suffix);
// Test empty prefix and suffix with tags
result = calculate_diff_split("<tag>left</tag>", "<tag>righ</tag>");
t.assert_equal("prefix should be '<tag>'", "<tag>", result.prefix);
t.assert_equal("left should be 'left'", "left", result.left);
t.assert_equal("right should be 'righ'", "righ", result.right);
t.assert_equal("suffix should be '</tag>'", "</tag>", result.suffix);
{
// real case from template tests, simplified
std::string left = "PREFIX</think>Sure";
std::string right = "PREFIX<think>Lemme think</think>Sure";
result = calculate_diff_split(left, right);
t.assert_equal("prefix should be PREFIX", "PREFIX", result.prefix);
t.assert_equal("suffix should be </think>Sure", "</think>Sure", result.suffix);
t.assert_equal("left should be empty", "", result.left);
t.assert_equal("right should be <think>Lemme think", "<think>Lemme think", result.right);
}
{
// Real case: special tokens with |> boundary issue
// The suffix starts with |> which should be moved to complete <|END_RESPONSE and <|END_ACTION
std::string prefix = "SOME_PREFIX";
std::string suffix = "|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>";
std::string left_diff = "<|START_RESPONSE|>Let me help you.<|END_RESPONSE";
std::string right_diff =
"<|START_THINKING|><|END_THINKING|><|START_ACTION|>[\n"
" {\"tool_call_id\": \"0\", \"tool_name\": \"test_function_name\", "
"\"parameters\": {\"param1\": \"value1\", \"param2\": \"value2\"}}\n"
"]<|END_ACTION";
std::string left = prefix + left_diff + suffix;
std::string right = prefix + right_diff + suffix;
result = calculate_diff_split(left, right);
t.assert_equal("special token prefix", prefix, result.prefix);
// The |> should be moved from suffix to complete the tokens
t.assert_equal("special token left", "<|START_RESPONSE|>Let me help you.<|END_RESPONSE|>", result.left);
t.assert_true("special token right ends with |>", result.right.find("<|END_ACTION|>") != std::string::npos);
t.assert_equal("special token suffix", "<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>",
result.suffix);
}
}
static void test_calculate_diff_split_generation_prompt(testing & t) {
// ChatML thinking template: left is a prefix of right, generation_prompt is the appended part.
// The trailing \n in left matches the trailing \n in the generation_prompt, causing
// the suffix matcher to steal it and rotate the diff result.
{
// Simplified reproduction: left ends with \n, right = left + "<|im_start|>assistant\n<think>\n"
std::string left = "<|im_start|>user\nHello<|im_end|>\n";
std::string right = left + "<|im_start|>assistant\n<think>\n";
diff_split result = calculate_diff_split(left, right);
t.assert_equal("chatml prefix", left, result.prefix);
t.assert_equal("chatml left", "", result.left);
t.assert_equal("chatml right should be generation prompt",
"<|im_start|>assistant\n<think>\n", result.right);
t.assert_equal("chatml suffix", "", result.suffix);
}
{
// More realistic: longer conversation ending with tool_response
std::string common =
"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
"<|im_start|>user\nSearch for files<|im_end|>\n"
"<|im_start|>assistant\n<think>\nLet me search.\n</think>\n\n"
"<tool_call>\n<function=search>\n</function>\n</tool_call><|im_end|>\n"
"<|im_start|>user\n<tool_response>\nNo files found\n</tool_response><|im_end|>\n";
std::string left = common;
std::string right = common + "<|im_start|>assistant\n<think>\n";
diff_split result = calculate_diff_split(left, right);
t.assert_equal("tool_response left", "", result.left);
t.assert_equal("tool_response right should be generation prompt",
"<|im_start|>assistant\n<think>\n", result.right);
}
}
static void test_until_common_prefix(testing & t) {
t.test("until_common_prefix basic", test_until_common_prefix_basic);
}
static void test_until_common_prefix_basic(testing & t) {
// Test case from the user request
std::string result = until_common_prefix("<function name=foo><arg name=bar>", "<arg name=bar>", "<arg name=baz>");
t.assert_equal("untilCommonPrefix should return '<function name=foo>'", "<function name=foo>", result);
// Additional test cases to ensure robustness
// Test with different common prefix lengths
result = until_common_prefix("prefix<test>suffix", "<test>different", "<test>other");
t.assert_equal("should return 'prefix'", "prefix", result);
// Test when common prefix is at the start
result = until_common_prefix("<common>rest", "<common>left", "<common>right");
t.assert_equal("should return empty string when common prefix at start", "", result);
// Test when there's no common prefix
result = until_common_prefix("something", "left", "right");
t.assert_equal("should return empty string when no common prefix", "", result);
// Test with empty strings
result = until_common_prefix("test", "", "right");
t.assert_equal("should return empty string when left is empty", "", result);
// Test with longer common prefix
result = until_common_prefix("abcXYZ<shared_prefix>rest", "<shared_prefix>left", "<shared_prefix>right");
t.assert_equal("should return 'abcXYZ'", "abcXYZ", result);
}
static void test_after_common_suffix(testing & t) {
t.test("after_common_suffix basic", test_after_common_suffix_basic);
}
static void test_after_common_suffix_basic(testing & t) {
// Test case from the user request
std::string result = after_common_suffix("<function name=foo><arg name=bar>100</arg></function>",
"<arg name=bar>100</arg>",
"<arg name=baz>535</arg>");
t.assert_equal("afterCommonSuffix should return '</function>'", "</function>", result);
// Test when common suffix is at the end
result = after_common_suffix("rest<common>", "left<common>", "right<common>");
t.assert_equal("should return empty string when common suffix at end", "", result);
// Test with empty strings
result = after_common_suffix("test", "left", "");
t.assert_equal("should return empty string when right is empty", "", result);
// Test case with XML-like structure similar to the main example
result = after_common_suffix("<outer><inner>value</inner></outer>",
"<inner>value</inner>",
"<inner>different</inner>");
t.assert_equal("should return '</outer>'", "</outer>", result);
// Test with longer common suffix appearing at the end of full
result = after_common_suffix("prefix<shared>rest</shared>", "prefix<shared>left</shared>", "prefix<shared>right</shared>");
t.assert_equal("should return '' when common suffix is at end of full", "", result);
// Test with common suffix appearing in middle but not at end
result = after_common_suffix("<tag>content</tag><extra>", "<tag>value</tag>", "<tag>other</tag>");
t.assert_equal("should return '<extra>' when common suffix appears before end", "<extra>", result);
// Test with multi-character common suffix at the very end of full
result = after_common_suffix("start<middle>end</middle>", "prefix<middle>left</middle>", "prefix<middle>right</middle>");
t.assert_equal("should return '' when common suffix </middle> is at end of full", "", result);
}
static void test_compare_variants(testing & t) {
t.test("compare_variants basic", test_compare_variants_basic);
t.test("compare_variants messages modifier", test_compare_variants_messages_modifier);
t.test("compare_variants tools modifier", test_compare_variants_tools_modifier);
t.test("compare_variants both modifiers", test_compare_variants_both_modifiers);
t.test("compare_variants template failure", test_compare_variants_template_failure);
t.test("compare_variants identity", test_compare_variants_identity);
}
static void test_compare_variants_basic(testing & t) {
// Create a simple template that just echoes messages
common_chat_template tmpl("{{ messages[0]['content'] }}", "", "");
template_params params;
params.messages = json::array({
json {{"role", "user"}, {"content", "Hello"}}
});
auto modifier = [](template_params & p) {
p.messages[0]["content"] = "World";
};
auto result = ::compare_variants(tmpl, params, modifier);
if (!t.assert_true("result should have value", result.has_value())) {
return;
}
// The template might not output anything if messages is empty or format is different
// Check that we get a valid result
t.assert_true("prefix or left should have content", !result->diff.prefix.empty() || !result->diff.left.empty());
}
static void test_compare_variants_messages_modifier(testing & t) {
// Test with messages modifier only
common_chat_template tmpl("{% for message in messages %}{{ message['role'] }}:{{ message['content'] }}{% endfor %}", "", "");
template_params params;
params.messages = json::array({
json {{"role", "user"}, {"content", "A"}}
});
auto modifier = [](template_params & p) {
p.messages[0]["content"] = "B";
};
std::optional<compare_variants_result> result = ::compare_variants(tmpl, params, modifier);
if (!t.assert_true("result should have value", result.has_value())) {
return;
}
t.assert_equal("left should be 'A'", "A", result->diff.left);
t.assert_equal("right should be 'B'", "B", result->diff.right);
}
static void test_compare_variants_tools_modifier(testing & t) {
// Test with tools modifier only
common_chat_template tmpl(
"{% for tool in tools %}{{ tool['name'] }}{% endfor %}", "", "");
template_params params;
params.tools = json::array({
json {{"name", "foo"}}
});
auto modifier = [](template_params & p) {
p.tools[0]["name"] = "bar";
};
auto result = ::compare_variants(tmpl, params, modifier);
if (!t.assert_true("result should have value", result.has_value())) {
return;
}
t.assert_equal("left should be 'foo'", "foo", result->diff.left);
t.assert_equal("right should be 'bar'", "bar", result->diff.right);
}
static void test_compare_variants_both_modifiers(testing & t) {
// Test with both messages and tools modifiers using the for loop approach
common_chat_template tmpl(
"{% for message in messages %}{{ message['role'] }}:{{ message['content'] }}{% endfor %}", "", "");
template_params params;
params.messages = json::array({
json {{"role", "user"}, {"content", "A"}}
});
auto modifier = [](template_params & p) {
p.messages[0]["content"] = "B";
p.messages[0]["role"] = "newuser";
};
auto result = ::compare_variants(tmpl, params, modifier);
if (!t.assert_true("result should have value", result.has_value())) {
return;
}
t.assert_equal("left should be 'user:A'", "user:A", result->diff.left);
t.assert_equal("right should be 'newuser:B'", "newuser:B", result->diff.right);
}
static void test_compare_variants_template_failure(testing & t) {
// Test with template that causes failure during application (not construction)
// We use a valid template syntax but one that will fail during application
common_chat_template tmpl("{{ messages[0]['nonexistent_field'] }}", "", "");
template_params params;
params.messages = json::array({
json {{"role", "user"}, {"content", "Hello"}}
});
auto modifier = [](template_params & p) {
p.messages[0]["content"] = "World";
};
auto result = ::compare_variants(tmpl, params, modifier);
t.assert_true("result should be nullopt on template failure", !result.has_value());
}
static void test_compare_variants_identity(testing & t) {
// Test with identity modifier (no change)
common_chat_template tmpl("{{ messages[0]['content'] }}", "", "");
template_params params;
params.messages = json::array({
json {{"role", "user"}, {"content", "Hello"}}
});
// No modifier - should use identity
auto result = ::compare_variants(tmpl, params, nullptr);
if (!t.assert_true("result should have value", result.has_value())) {
return;
}
t.assert_equal("prefix should be 'Hello'", "Hello", result->diff.prefix);
t.assert_equal("left should be empty", "", result->diff.left);
t.assert_equal("right should be empty", "", result->diff.right);
t.assert_equal("suffix should be empty", "", result->diff.suffix);
}
// ============================================================================
// Seed-OSS Template Tool Calling Analysis Tests
// ============================================================================
static void test_seed_oss_tool_analysis(testing & t) {
t.test("Seed-OSS tool presence", test_seed_oss_tool_presence);
t.test("Seed-OSS call count", test_seed_oss_call_count);
t.test("Seed-OSS function names", test_seed_oss_function_names);
t.test("Seed-OSS argument count", test_seed_oss_argument_count);
t.test("Seed-OSS args presence", test_seed_oss_args_presence);
t.test("Seed-OSS tool with reasoning", test_seed_oss_tool_with_reasoning);
}
// Helper to load Seed-OSS template
static common_chat_template load_seed_oss_template(testing & t) {
std::string template_path = "models/templates/ByteDance-Seed-OSS.jinja";
std::ifstream fin(template_path, std::ios::binary);
std::ostringstream buf;
if (fin.is_open()) {
buf << fin.rdbuf();
}
std::string template_source = buf.str();
common_chat_template tmpl(template_source, "", "");
t.assert_true("Seed-OSS template loaded successfully", template_source.length() > 0);
return tmpl;
}
// Helper to build tool call JSON
static json build_tool_call(const std::string & name, const json & args, const std::string & id = "call_001") {
return json{
{"id", id},
{"type", "function"},
{"function", json{
{"name", name},
{"arguments", args}
}}
};
}
// Helper to build tools definition
static json build_tools_definition() {
json parameters_schema = json::object();
parameters_schema["type"] = "object";
parameters_schema["properties"] = json::object();
parameters_schema["properties"]["param1"] = json::object({
{"type", "string"},
{"description", "First parameter"}
});
parameters_schema["properties"]["param2"] = json::object({
{"type", "string"},
{"description", "Second parameter"}
});
parameters_schema["required"] = json::array({"param1", "param2"});
return json::array({
json{
{"type", "function"},
{"function", json{
{"name", "test_function_name"},
{"description", "A test function for debugging"},
{"parameters", parameters_schema}
}}
}
});
}
// T1: Compare with/without tool call (user, assistant)
static void test_seed_oss_tool_presence(testing & t) {
common_chat_template tmpl = load_seed_oss_template(t);
json assistant_no_tools = json{
{"role", "assistant"},
{"content", "Let me help you."}
};
json assistant_with_tools = json{
{"role", "assistant"},
{"content", nullptr},
{"tool_calls", json::array({
build_tool_call("test_function_name", json::object({{"param1", "value1"}, {"param2", "value2"}}))
})}
};
json user_msg = json{
{"role", "user"},
{"content", "Hello, please help me."}
};
template_params params_no_tools;
params_no_tools.messages = json::array({user_msg, assistant_no_tools});
params_no_tools.tools = build_tools_definition();
params_no_tools.add_generation_prompt = false;
params_no_tools.enable_thinking = true;
template_params params_with_tools;
params_with_tools.messages = json::array({user_msg, assistant_with_tools});
params_with_tools.tools = build_tools_definition();
params_with_tools.add_generation_prompt = false;
params_with_tools.enable_thinking = true;
auto result = ::compare_variants(tmpl, params_no_tools,
[&](template_params & p) {
p.messages = params_with_tools.messages;
});
if (!t.assert_true("T1 result should have value", result.has_value())) {
return;
}
const auto & diff = result->diff;
t.assert_true("T1 prefix should contain system", diff.prefix.find("system") != std::string::npos);
t.assert_true("T1 prefix should contain user", diff.prefix.find("user") != std::string::npos);
t.assert_true("T1 prefix should contain assistant", diff.prefix.find("assistant") != std::string::npos);
// Left should be the assistant content without tool
t.assert_equal("T1 left should contain 'Let me help you.'", "Let me help you.", diff.left);
// Right should contain the tool call markers
t.assert_true("T1 right should contain tool_call begin", diff.right.find("<seed:tool_call>") != std::string::npos);
t.assert_true("T1 right should contain function tag", diff.right.find("<function=test_function_name>") != std::string::npos);
t.assert_true("T1 right should contain parameter=param1", diff.right.find("<parameter=param1>") != std::string::npos);
t.assert_true("T1 right should contain parameter=param2", diff.right.find("<parameter=param2>") != std::string::npos);
t.assert_true("T1 right should contain value1", diff.right.find("value1") != std::string::npos);
t.assert_true("T1 right should contain value2", diff.right.find("value2") != std::string::npos);
t.assert_true("T1 right should contain tool_call end", diff.right.find("</seed:tool_call>") != std::string::npos);
// Suffix should be the eos token
t.assert_equal("T1 suffix should be '<seed:eos>'", "<seed:eos>", diff.suffix);
}
// T2: Compare one vs two tool calls
static void test_seed_oss_call_count(testing & t) {
common_chat_template tmpl = load_seed_oss_template(t);
json assistant_one_call = json{
{"role", "assistant"},
{"content", nullptr},
{"tool_calls", json::array({
build_tool_call("test_function_name", json::object({{"param1", "value1"}, {"param2", "value2"}}))
})}
};
json assistant_two_calls = json{
{"role", "assistant"},
{"content", nullptr},
{"tool_calls", json::array({
build_tool_call("test_function_name", json::object({{"param1", "value1"}, {"param2", "value2"}})),
build_tool_call("test_function_name", json::object({{"param1", "value3"}, {"param2", "value4"}}), "call_002")
})}
};
json user_msg = json{
{"role", "user"},
{"content", "Hello, please help me."}
};
template_params params_one;
params_one.messages = json::array({user_msg, assistant_one_call});
params_one.tools = build_tools_definition();
params_one.add_generation_prompt = false;
params_one.enable_thinking = true;
auto result = ::compare_variants(tmpl, params_one,
[&](template_params & p) {
p.messages = json::array({user_msg, assistant_two_calls});
});
if (!t.assert_true("T2 result should have value", result.has_value())) {
return;
}
const auto & diff = result->diff;
// Prefix should include the first tool call
t.assert_true("T2 prefix should contain first tool_call begin", diff.prefix.find("<seed:tool_call>") != std::string::npos);
t.assert_true("T2 prefix should contain first function", diff.prefix.find("<function=test_function_name>") != std::string::npos);
t.assert_true("T2 prefix should contain value1", diff.prefix.find("value1") != std::string::npos);
t.assert_true("T2 prefix should contain value2", diff.prefix.find("value2") != std::string::npos);
t.assert_true("T2 prefix should contain first tool_call end", diff.prefix.find("</seed:tool_call>") != std::string::npos);
// Left should be empty (no second tool call in variant A)
t.assert_equal("T2 left should be empty", "", diff.left);
// Right should contain the second tool call
t.assert_true("T2 right should contain second tool_call begin", diff.right.find("<seed:tool_call>") != std::string::npos);
t.assert_true("T2 right should contain second function", diff.right.find("<function=test_function_name>") != std::string::npos);
t.assert_true("T2 right should contain value3", diff.right.find("value3") != std::string::npos);
t.assert_true("T2 right should contain value4", diff.right.find("value4") != std::string::npos);
t.assert_true("T2 right should contain second tool_call end", diff.right.find("</seed:tool_call>") != std::string::npos);
// Suffix should end with the eos token
t.assert_equal("T2 suffix should end with '<seed:eos>'", "<seed:eos>", diff.suffix.substr(diff.suffix.length() - 10, 10));
}
// T3: Compare different function names
static void test_seed_oss_function_names(testing & t) {
common_chat_template tmpl = load_seed_oss_template(t);
// Build tools with two different function names
json parameters_schema = json::object();
parameters_schema["type"] = "object";
parameters_schema["properties"] = json::object();
parameters_schema["properties"]["arg1"] = json::object({
{"type", "string"},
{"description", "Argument 1"}
});
parameters_schema["required"] = json::array({"arg1"});
json tools = json::array({
json{
{"type", "function"},
{"function", json{
{"name", "func_alpha"},
{"description", "First function"},
{"parameters", parameters_schema}
}}
},
json{
{"type", "function"},
{"function", json{
{"name", "func_beta"},
{"description", "Second function"},
{"parameters", parameters_schema}
}}
}
});
json assistant_func_alpha = json{
{"role", "assistant"},
{"content", nullptr},
{"tool_calls", json::array({
build_tool_call("func_alpha", json::object({{"arg1", "test_value"}}))
})}
};
json assistant_func_beta = json{
{"role", "assistant"},
{"content", nullptr},
{"tool_calls", json::array({
build_tool_call("func_beta", json::object({{"arg1", "test_value"}}))
})}
};
json user_msg = json{
{"role", "user"},
{"content", "Hello"}
};