fix(tool-feedback): dedupe duplicate content and keep full explanations
This commit is contained in:
14 files changed
+288
-54
No files matched your search
@@ -0,0 +1,39 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/providers"
|
||||
)
|
||||
|
||||
func normalizeToolFeedbackComparisonText(text string) string {
|
||||
text = strings.ReplaceAll(text, "\r\n", "\n")
|
||||
text = strings.ReplaceAll(text, "\r", "\n")
|
||||
text = strings.TrimSpace(text)
|
||||
if text == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Join(strings.Fields(text), " ")
|
||||
}
|
||||
|
||||
func ToolCallExplanationDuplicatesContent(content string, toolCalls []providers.ToolCall) bool {
|
||||
normalizedContent := normalizeToolFeedbackComparisonText(content)
|
||||
if normalizedContent == "" || len(toolCalls) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, tc := range toolCalls {
|
||||
if tc.ExtraContent == nil {
|
||||
continue
|
||||
}
|
||||
explanation := normalizeToolFeedbackComparisonText(tc.ExtraContent.ToolFeedbackExplanation)
|
||||
if explanation == "" {
|
||||
continue
|
||||
}
|
||||
if explanation == normalizedContent {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/providers"
|
||||
)
|
||||
|
||||
func TestToolCallExplanationDuplicatesContent(t *testing.T) {
|
||||
t.Run("exact duplicate", func(t *testing.T) {
|
||||
toolCalls := []providers.ToolCall{{
|
||||
ExtraContent: &providers.ExtraContent{
|
||||
ToolFeedbackExplanation: "Read the file before replying.",
|
||||
},
|
||||
}}
|
||||
|
||||
if !ToolCallExplanationDuplicatesContent("Read the file before replying.", toolCalls) {
|
||||
t.Fatal("expected duplicated content to be detected")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("whitespace normalized duplicate", func(t *testing.T) {
|
||||
toolCalls := []providers.ToolCall{{
|
||||
ExtraContent: &providers.ExtraContent{
|
||||
ToolFeedbackExplanation: "Read the file\nbefore replying.",
|
||||
},
|
||||
}}
|
||||
|
||||
if !ToolCallExplanationDuplicatesContent(" Read the file before replying. ", toolCalls) {
|
||||
t.Fatal("expected whitespace-only differences to be ignored")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("distinct content", func(t *testing.T) {
|
||||
toolCalls := []providers.ToolCall{{
|
||||
ExtraContent: &providers.ExtraContent{
|
||||
ToolFeedbackExplanation: "Read the file before replying.",
|
||||
},
|
||||
}}
|
||||
|
||||
if ToolCallExplanationDuplicatesContent(
|
||||
"I will summarize the findings after reading the file.",
|
||||
toolCalls,
|
||||
) {
|
||||
t.Fatal("expected distinct content to remain visible")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("missing explanation", func(t *testing.T) {
|
||||
toolCalls := []providers.ToolCall{{}}
|
||||
if ToolCallExplanationDuplicatesContent("Read the file before replying.", toolCalls) {
|
||||
t.Fatal("expected empty tool explanations to skip dedupe")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -39,9 +39,6 @@ func BuildVisibleToolCalls(
|
||||
explanation := ""
|
||||
if tc.ExtraContent != nil {
|
||||
explanation = strings.TrimSpace(tc.ExtraContent.ToolFeedbackExplanation)
|
||||
if maxArgsLen > 0 {
|
||||
explanation = Truncate(explanation, maxArgsLen)
|
||||
}
|
||||
}
|
||||
if name == "" && explanation == "" && argsPreview == "" {
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/providers"
|
||||
)
|
||||
|
||||
func TestBuildVisibleToolCalls_DoesNotTruncateExplanation(t *testing.T) {
|
||||
explanation := "Read README.md first to confirm the current project structure before editing the config example."
|
||||
toolCalls := []providers.ToolCall{{
|
||||
ID: "call_1",
|
||||
Type: "function",
|
||||
Function: &providers.FunctionCall{
|
||||
Name: "read_file",
|
||||
Arguments: `{"path":"README.md","start_line":1,"end_line":10,"extra":"abcdefghijklmnopqrstuvwxyz"}`,
|
||||
},
|
||||
ExtraContent: &providers.ExtraContent{
|
||||
ToolFeedbackExplanation: explanation,
|
||||
},
|
||||
}}
|
||||
|
||||
visible := BuildVisibleToolCalls(toolCalls, 20)
|
||||
if len(visible) != 1 {
|
||||
t.Fatalf("len(visible) = %d, want 1", len(visible))
|
||||
}
|
||||
if visible[0].ExtraContent == nil || visible[0].ExtraContent.ToolFeedbackExplanation != explanation {
|
||||
t.Fatalf("visible explanation = %#v, want %q", visible[0].ExtraContent, explanation)
|
||||
}
|
||||
if visible[0].Function == nil || visible[0].Function.Arguments == "" {
|
||||
t.Fatalf("visible function = %#v, want truncated args preview", visible[0].Function)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user