Parse @agentname patterns in channel message body. Mentioned agents (who are channel members, excluding sender) get mention=true flag in their inbox notification metadata. Channel message metadata includes mentioned_agents list for all recipients. - mentions.go: regex parser with email exclusion, dedup, 22 test cases - BroadcastMessage: metadata now uses json.Marshal, includes mentions - Tests verify mention flag, self-mention exclusion, non-member skip Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package messaging
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// mentionPattern matches @agentname patterns where agent names contain
|
|
// alphanumeric characters, hyphens, and underscores (max 64 chars).
|
|
// It uses a negative lookbehind-style approach: we exclude matches that
|
|
// look like email addresses (preceded by alphanumeric chars).
|
|
var mentionPattern = regexp.MustCompile(`(?:^|[^a-zA-Z0-9.@])@([a-zA-Z0-9][a-zA-Z0-9_-]{0,63})`)
|
|
|
|
// ParseMentions extracts unique @agentname mentions from message body text.
|
|
// It returns a deduplicated list of agent names (without the @ prefix).
|
|
//
|
|
// Rules:
|
|
// - Agent names start with an alphanumeric character
|
|
// - Agent names can contain: alphanumeric, hyphens, underscores (max 64 chars)
|
|
// - Email addresses (e.g., user@example.com) are NOT matched
|
|
// - Duplicate mentions are deduplicated
|
|
// - @ at end of string or @@ are ignored
|
|
func ParseMentions(body string) []string {
|
|
matches := mentionPattern.FindAllStringSubmatch(body, -1)
|
|
if len(matches) == 0 {
|
|
return nil
|
|
}
|
|
|
|
seen := make(map[string]bool, len(matches))
|
|
var result []string
|
|
for _, m := range matches {
|
|
name := strings.ToLower(m[1])
|
|
if !seen[name] {
|
|
seen[name] = true
|
|
result = append(result, name)
|
|
}
|
|
}
|
|
return result
|
|
}
|