title.go
1 package session 2 3 import ( 4 "fmt" 5 "strings" 6 "unicode/utf8" 7 ) 8 9 // AgentTitle returns a fixed title for a named agent's long-lived session. 10 func AgentTitle(agentName string) string { 11 return fmt.Sprintf("%s conversation", agentName) 12 } 13 14 // Title creates a short, readable title from user input. 15 // Truncates to 50 chars at a word boundary, strips leading/trailing whitespace 16 // and newlines, and ensures single-line output. 17 func Title(input string) string { 18 // Take first line only 19 if idx := strings.IndexAny(input, "\n\r"); idx >= 0 { 20 input = input[:idx] 21 } 22 input = strings.TrimSpace(input) 23 if input == "" { 24 return "New session" 25 } 26 const maxRunes = 50 27 if utf8.RuneCountInString(input) <= maxRunes { 28 return input 29 } 30 // Truncate at rune boundary 31 runes := []rune(input) 32 truncated := string(runes[:maxRunes]) 33 if lastSpace := strings.LastIndex(truncated, " "); lastSpace > len(truncated)/2 { 34 truncated = truncated[:lastSpace] 35 } 36 return truncated + "..." 37 }