Address PR review: fenced-stream guard and multi ANTML calls

This commit is contained in:
CJACK.
2026-03-07 17:45:43 +08:00
parent 25e40cc3a6
commit 0ce3fd22a7
4 changed files with 71 additions and 4 deletions

View File

@@ -32,8 +32,8 @@ func parseXMLToolCalls(text string) []ParsedToolCall {
if call, ok := parseFunctionCallTagStyle(text); ok {
return []ParsedToolCall{call}
}
if call, ok := parseAntmlFunctionCallStyle(text); ok {
return []ParsedToolCall{call}
if calls := parseAntmlFunctionCallStyles(text); len(calls) > 0 {
return calls
}
if call, ok := parseInvokeFunctionCallStyle(text); ok {
return []ParsedToolCall{call}
@@ -140,8 +140,24 @@ func parseFunctionCallTagStyle(text string) (ParsedToolCall, bool) {
return ParsedToolCall{Name: name, Input: input}, true
}
func parseAntmlFunctionCallStyle(text string) (ParsedToolCall, bool) {
m := antmlFunctionCallPattern.FindStringSubmatch(text)
func parseAntmlFunctionCallStyles(text string) []ParsedToolCall {
matches := antmlFunctionCallPattern.FindAllStringSubmatch(text, -1)
if len(matches) == 0 {
return nil
}
out := make([]ParsedToolCall, 0, len(matches))
for _, m := range matches {
if call, ok := parseSingleAntmlFunctionCallMatch(m); ok {
out = append(out, call)
}
}
if len(out) == 0 {
return nil
}
return out
}
func parseSingleAntmlFunctionCallMatch(m []string) (ParsedToolCall, bool) {
if len(m) < 3 {
return ParsedToolCall{}, false
}

View File

@@ -260,3 +260,14 @@ func TestParseToolCallsSupportsAntmlFunctionAttributeWithParametersTag(t *testin
t.Fatalf("expected command argument, got %#v", calls[0].Input)
}
}
func TestParseToolCallsSupportsMultipleAntmlFunctionCalls(t *testing.T) {
text := `<antml:function_calls><antml:function_call id="1" function="Bash"><antml:parameters>{"command":"pwd"}</antml:parameters></antml:function_call><antml:function_call id="2" function="Read"><antml:parameters>{"file_path":"README.md"}</antml:parameters></antml:function_call></antml:function_calls>`
calls := ParseToolCalls(text, []string{"bash", "read"})
if len(calls) != 2 {
t.Fatalf("expected 2 calls, got %#v", calls)
}
if calls[0].Name != "bash" || calls[1].Name != "read" {
t.Fatalf("expected canonical names [bash read], got %#v", calls)
}
}