fix: avoid taking generic <name> as xml tool name

This commit is contained in:
CJACK.
2026-04-04 01:52:57 +08:00
parent c6340354ec
commit 0847091864
2 changed files with 14 additions and 1 deletions

View File

@@ -22,7 +22,6 @@ var toolUseToolNameBodyPattern = regexp.MustCompile(`(?is)<tool_use>\s*<tool_nam
var xmlToolNamePatterns = []*regexp.Regexp{
regexp.MustCompile(`(?is)<(?:[a-z0-9_:-]+:)?tool_name\b[^>]*>(.*?)</(?:[a-z0-9_:-]+:)?tool_name>`),
regexp.MustCompile(`(?is)<(?:[a-z0-9_:-]+:)?function_name\b[^>]*>(.*?)</(?:[a-z0-9_:-]+:)?function_name>`),
regexp.MustCompile(`(?is)<(?:[a-z0-9_:-]+:)?name\b[^>]*>(.*?)</(?:[a-z0-9_:-]+:)?name>`),
}
func parseXMLToolCalls(text string) []ParsedToolCall {

View File

@@ -191,6 +191,20 @@ func TestParseToolCallsSupportsXMLParametersJSONWithAmpersandCommand(t *testing.
}
}
func TestParseToolCallsDoesNotTreatParameterNameTagAsToolName(t *testing.T) {
text := `<tool_call><tool name="execute_command"><parameters><name>file.txt</name><command>pwd</command></parameters></tool></tool_call>`
calls := ParseToolCalls(text, []string{"execute_command"})
if len(calls) != 1 {
t.Fatalf("expected 1 call, got %#v", calls)
}
if calls[0].Name != "execute_command" {
t.Fatalf("expected tool name execute_command, got %q", calls[0].Name)
}
if calls[0].Input["name"] != "file.txt" {
t.Fatalf("expected parameter name preserved, got %#v", calls[0].Input)
}
}
func TestParseToolCallsPrefersJSONPayloadOverIncidentalXMLInString(t *testing.T) {
text := `{"tool_calls":[{"name":"search","input":{"q":"latest <tool_call><tool_name>wrong</tool_name><parameters>{\"x\":1}</parameters></tool_call>"}}]}`
calls := ParseToolCallsDetailed(text, []string{"search"}).Calls