refactor: enhance AgentLoopEngine with confirmed MCP reply handling and add tests for tool result summarization
This commit is contained in:
@@ -98,5 +98,27 @@ func appendNonBlankSegment(input []string, value string) []string {
|
||||
if value == "" {
|
||||
return input
|
||||
}
|
||||
key := canonicalToolResultSegment(value)
|
||||
for _, existing := range input {
|
||||
if canonicalToolResultSegment(existing) == key {
|
||||
return input
|
||||
}
|
||||
}
|
||||
return append(input, value)
|
||||
}
|
||||
|
||||
func canonicalToolResultSegment(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
var payload any
|
||||
if err := json.Unmarshal([]byte(value), &payload); err != nil {
|
||||
return value
|
||||
}
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return value
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package tooling
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"agent-desk/internal/ai/mcps"
|
||||
)
|
||||
|
||||
func TestBuildReducedToolResultSummaryDeduplicatesStructuredAndTextContent(t *testing.T) {
|
||||
result := &mcps.ToolCallResult{
|
||||
StructuredContent: map[string]any{
|
||||
"timestamp": "2026-07-28 11:51:52",
|
||||
"timezone": "Local",
|
||||
},
|
||||
Content: []mcps.ToolResultContent{{
|
||||
Type: "text",
|
||||
Text: `{"timezone":"Local","timestamp":"2026-07-28 11:51:52"}`,
|
||||
}},
|
||||
}
|
||||
|
||||
summary := BuildReducedToolResultSummary(result)
|
||||
if strings.Count(summary, "timestamp") != 1 {
|
||||
t.Fatalf("duplicate MCP result was not removed: %q", summary)
|
||||
}
|
||||
if summary != `{"timestamp":"2026-07-28 11:51:52","timezone":"Local"}` {
|
||||
t.Fatalf("unexpected reduced result: %q", summary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildReducedToolResultSummaryKeepsDistinctSegments(t *testing.T) {
|
||||
result := &mcps.ToolCallResult{
|
||||
StructuredContent: map[string]any{"status": "ok"},
|
||||
Content: []mcps.ToolResultContent{{
|
||||
Type: "text",
|
||||
Text: "additional context",
|
||||
}},
|
||||
}
|
||||
|
||||
summary := BuildReducedToolResultSummary(result)
|
||||
if !strings.Contains(summary, `{"status":"ok"}`) || !strings.Contains(summary, "additional context") {
|
||||
t.Fatalf("distinct MCP result segments were lost: %q", summary)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user