Files
goclaw/internal/config/normalize.go
T
Viet Tran f3f4c67b36 Initial commit: GoClaw AI agent gateway
Multi-agent AI gateway with WebSocket RPC, HTTP API, and messaging channel integrations.
Go port of OpenClaw with multi-tenant PostgreSQL, per-user isolation, security hardening,
and production observability.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 14:58:07 +07:00

49 lines
1.1 KiB
Go

package config
import (
"regexp"
"strings"
)
const DefaultAgentID = "default"
var (
validIDRe = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,63}$`)
invalidChars = regexp.MustCompile(`[^a-z0-9_-]+`)
leadingDash = regexp.MustCompile(`^-+`)
trailingDash = regexp.MustCompile(`-+$`)
)
// NormalizeAgentID converts a user-provided name into a valid agent ID.
// Matching TS normalizeAgentId() from routing/session-key.ts:
// - Lowercase, max 64 chars
// - Only [a-z0-9_-] allowed
// - Invalid chars replaced with "-"
// - Leading/trailing dashes stripped
// - Empty result defaults to "default"
func NormalizeAgentID(name string) string {
trimmed := strings.TrimSpace(name)
if trimmed == "" {
return DefaultAgentID
}
lower := strings.ToLower(trimmed)
if validIDRe.MatchString(lower) {
return lower
}
// Best-effort: collapse invalid chars to "-"
result := invalidChars.ReplaceAllString(lower, "-")
result = leadingDash.ReplaceAllString(result, "")
result = trailingDash.ReplaceAllString(result, "")
if len(result) > 64 {
result = result[:64]
}
if result == "" {
return DefaultAgentID
}
return result
}