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
This commit is contained in:
parent
ad070267fd
commit
63bd30e82b
3 changed files with 43 additions and 27 deletions
25
pkg/agent/i18n.go
Normal file
25
pkg/agent/i18n.go
Normal file
|
|
@ -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 ""
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue