2026-07-25 12:04:06 +08:00
|
|
|
package tooling
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-08-28 22:23:13 +08:00
|
|
|
"regexp"
|
2026-07-25 12:04:06 +08:00
|
|
|
"strings"
|
|
|
|
|
"unicode"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const maxCustomerReplyRunes = 8000
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
const restrictedNetworkPolicyFallback = "当前网络状态请以实际使用情况为准。如无法联网,请使用“智能检测”或联系人工客服。"
|
|
|
|
|
|
|
|
|
|
var restrictedNetworkPolicyPattern = regexp.MustCompile(`(?i)限速|降速|速率限制|带宽限制|speed[ _-]?limit|throttl|traffic[ _-]?shap|(?:^|[^a-z0-9])\d+(?:\.\d+)?\s*(?:k|m|g)?bps(?:[^a-z0-9]|$)`)
|
|
|
|
|
|
2026-07-25 12:04:06 +08:00
|
|
|
// NormalizeCustomerReply applies the final plain-text boundary before an AI
|
|
|
|
|
// response enters a customer conversation. It rejects likely credential
|
|
|
|
|
// assignments instead of masking them, because a masked secret is not useful
|
|
|
|
|
// customer-facing content.
|
|
|
|
|
func NormalizeCustomerReply(value string) (string, error) {
|
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
if value == "" {
|
|
|
|
|
return "", fmt.Errorf("ai reply is empty")
|
|
|
|
|
}
|
|
|
|
|
if secretAssignmentPattern.MatchString(value) {
|
|
|
|
|
return "", fmt.Errorf("ai reply contains sensitive credential data")
|
|
|
|
|
}
|
|
|
|
|
var builder strings.Builder
|
|
|
|
|
for _, r := range value {
|
|
|
|
|
if unicode.IsControl(r) && r != '\n' && r != '\t' {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
builder.WriteRune(r)
|
|
|
|
|
}
|
|
|
|
|
value = strings.TrimSpace(builder.String())
|
|
|
|
|
for strings.Contains(value, "\n\n\n") {
|
|
|
|
|
value = strings.ReplaceAll(value, "\n\n\n", "\n\n")
|
|
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
value = redactRestrictedNetworkPolicy(value)
|
2026-07-25 12:04:06 +08:00
|
|
|
if value == "" {
|
|
|
|
|
return "", fmt.Errorf("ai reply is empty")
|
|
|
|
|
}
|
|
|
|
|
if len([]rune(value)) > maxCustomerReplyRunes {
|
|
|
|
|
return "", fmt.Errorf("ai reply exceeds maximum length")
|
|
|
|
|
}
|
|
|
|
|
return value, nil
|
|
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
|
|
|
|
|
// redactRestrictedNetworkPolicy is a final customer-visible safety boundary.
|
|
|
|
|
// The model may still ignore its system prompt, so any line that confirms,
|
|
|
|
|
// denies, or quantifies an internal network speed policy is removed before the
|
|
|
|
|
// message is persisted. Other useful lines are preserved.
|
|
|
|
|
func redactRestrictedNetworkPolicy(value string) string {
|
|
|
|
|
if !restrictedNetworkPolicyPattern.MatchString(value) {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
lines := strings.Split(value, "\n")
|
|
|
|
|
safe := make([]string, 0, len(lines)+1)
|
|
|
|
|
redacted := false
|
|
|
|
|
for _, line := range lines {
|
|
|
|
|
if restrictedNetworkPolicyPattern.MatchString(line) {
|
|
|
|
|
redacted = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
safe = append(safe, line)
|
|
|
|
|
}
|
|
|
|
|
if redacted {
|
|
|
|
|
safe = append(safe, restrictedNetworkPolicyFallback)
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(strings.Join(safe, "\n"))
|
|
|
|
|
}
|