refactor: enhance XML tool call parsing to support nested structures, CDATA, and repeated tags

This commit is contained in:
CJACK
2026-04-19 19:58:45 +08:00
parent 26d195f2a6
commit 0f2b5fee23
16 changed files with 550 additions and 140 deletions

View File

@@ -12,7 +12,7 @@ func TestMessagesPrepareBasic(t *testing.T) {
if got == "" {
t.Fatal("expected non-empty prompt")
}
if got != "<begin▁of▁sentence><User>Hello<Assistant></think>" {
if got != "<begin▁of▁sentence><User>Hello<Assistant>" {
t.Fatalf("unexpected prompt: %q", got)
}
}
@@ -32,10 +32,10 @@ func TestMessagesPrepareRoles(t *testing.T) {
if !contains(got, "<begin▁of▁sentence>") {
t.Fatalf("expected begin marker in %q", got)
}
if !contains(got, "<User>Hi<Assistant></think>Hello<end▁of▁sentence>") {
if !contains(got, "<User>Hi<Assistant>Hello<end▁of▁sentence>") {
t.Fatalf("expected user/assistant separation in %q", got)
}
if !contains(got, "<Assistant></think>Hello<end▁of▁sentence><Tool>Search results<end▁of▁toolresults>") {
if !contains(got, "<Assistant>Hello<end▁of▁sentence><Tool>Search results<end▁of▁toolresults>") {
t.Fatalf("expected assistant/tool separation in %q", got)
}
if !contains(got, "<Tool>Search results<end▁of▁toolresults><User>How are you") {
@@ -77,7 +77,7 @@ func TestMessagesPrepareArrayTextVariants(t *testing.T) {
},
}
got := MessagesPrepare(messages)
if got != "<begin▁of▁sentence><User>line1\nline2<Assistant></think>" {
if got != "<begin▁of▁sentence><User>line1\nline2<Assistant>" {
t.Fatalf("unexpected content from text variants: %q", got)
}
}

View File

@@ -195,9 +195,12 @@ func TestMessagesPrepareAssistantMarkers(t *testing.T) {
if strings.Count(got, "<end▁of▁sentence>") != 1 {
t.Fatalf("expected one end_of_sentence (assistant only), got %q", got)
}
if !strings.Contains(got, "<Assistant></think>Hello!<end▁of▁sentence>") {
if !strings.Contains(got, "<Assistant>Hello!<end▁of▁sentence>") {
t.Fatalf("expected assistant EOS suffix, got %q", got)
}
if strings.Contains(got, "<think>") || strings.Contains(got, "</think>") {
t.Fatalf("did not expect think tags in prompt, got %q", got)
}
if strings.Contains(got, "<system_instructions>") {
t.Fatalf("did not expect legacy system marker, got %q", got)
}