From 04831e5166150eaeb0c832c08e3d6800e5797f11 Mon Sep 17 00:00:00 2001 From: ManManavadaria Date: Thu, 19 Feb 2026 19:03:07 +0530 Subject: [PATCH] Fix json strip from text --- pkg/providers/toolcall/tool_call_extract.go | 7 ++- .../toolcall/tool_call_extract_test.go | 47 ++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/pkg/providers/toolcall/tool_call_extract.go b/pkg/providers/toolcall/tool_call_extract.go index 1d9c71723..65edef74e 100644 --- a/pkg/providers/toolcall/tool_call_extract.go +++ b/pkg/providers/toolcall/tool_call_extract.go @@ -136,7 +136,12 @@ func StripJSONObject(text, pattern string) string { return text } - return strings.TrimSpace(text[:start] + text[end:]) + before := strings.TrimRight(text[:start], " \t\n\r") + after := strings.TrimLeft(text[end:], " \t\n\r") + if before != "" && after != "" { + return before + " " + after + } + return before + after } // StripToolCallsFromText removes tool call JSON from response text. diff --git a/pkg/providers/toolcall/tool_call_extract_test.go b/pkg/providers/toolcall/tool_call_extract_test.go index 5807d0847..99fbad1ff 100644 --- a/pkg/providers/toolcall/tool_call_extract_test.go +++ b/pkg/providers/toolcall/tool_call_extract_test.go @@ -198,16 +198,45 @@ func TestParseToolCallArguments(t *testing.T) { } } else { // For other cases, do deep comparison - if gotVal != wantVal { - // Handle nested maps - if gotMap, ok := gotVal.(map[string]interface{}); ok { - if wantMap, ok := wantVal.(map[string]interface{}); ok { - if len(gotMap) != len(wantMap) { - t.Errorf("ParseToolCallArguments()[%q] nested map length = %d, want %d", key, len(gotMap), len(wantMap)) + // Handle nested maps first to avoid panic from direct comparison + if gotMap, ok := gotVal.(map[string]interface{}); ok { + if wantMap, ok := wantVal.(map[string]interface{}); ok { + if len(gotMap) != len(wantMap) { + t.Errorf("ParseToolCallArguments()[%q] nested map length = %d, want %d", key, len(gotMap), len(wantMap)) + } else { + // Recursively check nested map values + for nestedKey, nestedWantVal := range wantMap { + nestedGotVal, ok := gotMap[nestedKey] + if !ok { + t.Errorf("ParseToolCallArguments()[%q][%q] missing key", key, nestedKey) + continue + } + if nestedGotVal != nestedWantVal { + t.Errorf("ParseToolCallArguments()[%q][%q] = %v, want %v", key, nestedKey, nestedGotVal, nestedWantVal) + } } - continue } + continue } + } + // Handle arrays/slices to avoid panic from direct comparison + if gotSlice, ok := gotVal.([]interface{}); ok { + if wantSlice, ok := wantVal.([]interface{}); ok { + if len(gotSlice) != len(wantSlice) { + t.Errorf("ParseToolCallArguments()[%q] array length = %d, want %d", key, len(gotSlice), len(wantSlice)) + } else { + // Compare array elements + for i := range gotSlice { + if gotSlice[i] != wantSlice[i] { + t.Errorf("ParseToolCallArguments()[%q][%d] = %v, want %v", key, i, gotSlice[i], wantSlice[i]) + } + } + } + continue + } + } + // For other values, do direct comparison + if gotVal != wantVal { t.Errorf("ParseToolCallArguments()[%q] = %v, want %v", key, gotVal, wantVal) } } @@ -285,10 +314,6 @@ func TestExtractToolCallsFromText(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := ExtractToolCallsFromText(tt.text) - if tt.want == nil && got != nil { - t.Errorf("ExtractToolCallsFromText() = %v, want nil", got) - return - } if len(got) != tt.wantLen { t.Errorf("ExtractToolCallsFromText() length = %d, want %d", len(got), tt.wantLen) return