Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions agent/compaction/compaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,44 @@ func TestToolResultStrategy_CollapsesOldToolGroups(t *testing.T) {
}
}

func TestDefaultToolCallFormatter_ResultTypes(t *testing.T) {
for _, tc := range []struct {
name string
result any
want string
}{
{name: "raw JSON object", result: json.RawMessage(`{"status":"shipped"}`), want: "[Tool Calls]\nlookup:\n - {\"status\":\"shipped\"}"},
{name: "raw JSON array", result: json.RawMessage(`["first","second"]`), want: "[Tool Calls]\nlookup:\n - [\"first\",\"second\"]"},
{name: "raw JSON null", result: json.RawMessage(`null`), want: "[Tool Calls]\nlookup:\n - null"},
{name: "empty raw JSON", result: json.RawMessage{}, want: "[Tool Calls]\nlookup:"},
{name: "nil raw JSON", result: json.RawMessage(nil), want: "[Tool Calls]\nlookup:"},
{name: "string", result: "shipped", want: "[Tool Calls]\nlookup:\n - shipped"},
{name: "number", result: 42, want: "[Tool Calls]\nlookup:\n - 42"},
} {
t.Run(tc.name, func(t *testing.T) {
group := &compaction.MessageGroup{
Messages: []*message.Message{
{
Role: message.RoleAssistant,
Contents: []message.Content{
&message.FunctionCallContent{CallID: "call-1", Name: "lookup"},
},
},
{
Role: message.RoleTool,
Contents: []message.Content{
&message.FunctionResultContent{CallID: "call-1", Result: tc.result},
},
},
},
}
if got := compaction.DefaultToolCallFormatter(group); got != tc.want {
t.Fatalf("formatter output = %q, want %q", got, tc.want)
}
})
}
}

// TestDefaultToolCallFormatter_DedupsRepeatedNamesWithEmptyResults guards the
// tool-name deduplication when repeated calls to the same tool produce empty
// results. The name must still be listed exactly once, matching the behavior
Expand Down
7 changes: 6 additions & 1 deletion agent/compaction/toolresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package compaction

import (
"context"
"encoding/json"
"fmt"
"slices"
"strings"
Expand Down Expand Up @@ -120,7 +121,11 @@ func DefaultToolCallFormatter(group *MessageGroup) string {
case *message.FunctionCallContent:
functionCalls = append(functionCalls, call{id: typed.CallID, name: typed.Name})
case *message.FunctionResultContent:
resultsByCallID[typed.CallID] = fmt.Sprint(typed.Result)
if raw, ok := typed.Result.(json.RawMessage); ok {
resultsByCallID[typed.CallID] = string(raw)
} else {
resultsByCallID[typed.CallID] = fmt.Sprint(typed.Result)
}
hasFunctionResult = true
}
}
Expand Down
Loading