From 63bd30e82b4fdefef502c6ef2df8326fe65c97f6 Mon Sep 17 00:00:00 2001 From: instax-dutta Date: Tue, 3 Mar 2026 18:03:08 +0530 Subject: [PATCH] fix: address PR review comments for TLDR feature - Add TLDRIncludeMessage config option for privacy in group chats - Add i18n.go with function-based message templates - Deduplicate executed tools to prevent unbounded growth - Replace strings.Builder with fmt.Sprintf for cleaner code - Move deduplication to single pass before generateTLDR call --- pkg/agent/i18n.go | 25 +++++++++++++++++++++++++ pkg/agent/loop.go | 44 +++++++++++++++++--------------------------- pkg/config/config.go | 1 + 3 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 pkg/agent/i18n.go diff --git a/pkg/agent/i18n.go b/pkg/agent/i18n.go new file mode 100644 index 000000000..08d66369d --- /dev/null +++ b/pkg/agent/i18n.go @@ -0,0 +1,25 @@ +package agent + +import "fmt" + +func tldrWithToolsMessage(toolCount int, tools string, iteration int) string { + return fmt.Sprintf("Processed %d tool(s): %s. (%d iteration%s)", toolCount, tools, iteration, pluralize(iteration)) +} + +func tldrNoToolsMessage(iteration int) string { + return fmt.Sprintf("Processed your request (%d iteration%s)", iteration, pluralize(iteration)) +} + +func tldrWithMessage(msg string) string { + if len(msg) <= 50 { + return fmt.Sprintf(". Message: \"%s\"", msg) + } + return fmt.Sprintf(". Message: \"%s...\"", msg[:50]) +} + +func pluralize(n int) string { + if n > 1 { + return "s" + } + return "" +} diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index cb297b7e1..40dfeacd7 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -12,7 +12,6 @@ import ( "errors" "fmt" "path/filepath" - "strconv" "strings" "sync" "sync/atomic" @@ -529,7 +528,16 @@ func (al *AgentLoop) runAgentLoop(ctx context.Context, agent *AgentInstance, opt // 5. Handle empty response if finalContent == "" { - finalContent = generateTLDR(opts.UserMessage, executedTools, iteration) + // Deduplicate tools for TLDR + seen := make(map[string]bool) + uniqueTools := make([]string, 0, len(executedTools)) + for _, t := range executedTools { + if !seen[t] { + seen[t] = true + uniqueTools = append(uniqueTools, t) + } + } + finalContent = generateTLDR(opts.UserMessage, uniqueTools, iteration, al.cfg.Agents.Defaults.TLDRIncludeMessage) } // 6. Save final assistant message to session @@ -1358,36 +1366,18 @@ func extractParentPeer(msg bus.InboundMessage) *routing.RoutePeer { // generateTLDR generates a summary when the LLM returns an empty response. // It provides context about what was processed instead of a generic message. -func generateTLDR(userMessage string, executedTools []string, iteration int) string { - var sb strings.Builder +func generateTLDR(userMessage string, executedTools []string, iteration int, includeMessage bool) string { + var result string if len(executedTools) > 0 { - sb.WriteString("Processed ") - sb.WriteString(strconv.Itoa(len(executedTools))) - sb.WriteString(" tool(s): ") - sb.WriteString(strings.Join(executedTools, ", ")) - sb.WriteString(".") + result = tldrWithToolsMessage(len(executedTools), strings.Join(executedTools, ", "), iteration) } else { - sb.WriteString("Processed your request") + result = tldrNoToolsMessage(iteration) } - sb.WriteString(" (") - sb.WriteString(strconv.Itoa(iteration)) - sb.WriteString(" iteration") - if iteration > 1 { - sb.WriteString("s") - } - sb.WriteString(")") - - if len(userMessage) > 50 { - sb.WriteString(". Message: \"") - sb.WriteString(utils.Truncate(userMessage, 50)) - sb.WriteString("...\"") - } else if userMessage != "" { - sb.WriteString(". Message: \"") - sb.WriteString(userMessage) - sb.WriteString("\"") + if includeMessage && len(userMessage) > 0 { + result += tldrWithMessage(userMessage) } - return sb.String() + return result } diff --git a/pkg/config/config.go b/pkg/config/config.go index c4c175495..bd7e4a609 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -180,6 +180,7 @@ type AgentDefaults struct { MaxTokens int `json:"max_tokens" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOKENS"` Temperature *float64 `json:"temperature,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_TEMPERATURE"` MaxToolIterations int `json:"max_tool_iterations" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOOL_ITERATIONS"` + TLDRIncludeMessage bool `json:"tldr_include_message" env:"PICOCLAW_AGENTS_DEFAULTS_TLDR_INCLUDE_MESSAGE"` } // GetModelName returns the effective model name for the agent defaults.