opencode-cursor-agent/internal/sanitize/sanitize.go

49 lines
1.9 KiB
Go

// Package sanitize strips third-party AI branding, telemetry headers, and
// identifying metadata from prompts before they are forwarded to the Cursor
// CLI. Without this, the Cursor agent sees "You are Claude Code..." style
// system prompts and behaves confusingly (trying to use tools it doesn't
// own, reasoning about being "Anthropic's CLI", etc.).
//
// Ported from cursor-api-proxy/src/lib/sanitize.ts.
package sanitize
import "regexp"
type rule struct {
pattern *regexp.Regexp
replace string
}
// Note: Go regexp is RE2, no lookbehind/lookahead, but these rules don't need any.
// (?i) enables case-insensitive for that single rule.
var rules = []rule{
// Strip x-anthropic-billing-header line (injected by Claude Code CLI).
{regexp.MustCompile(`(?i)x-anthropic-billing-header:[^\n]*\n?`), ""},
// Strip individual telemetry tokens that may appear in headers or text.
{regexp.MustCompile(`(?i)\bcc_version=[^\s;,\n]+[;,]?\s*`), ""},
{regexp.MustCompile(`(?i)\bcc_entrypoint=[^\s;,\n]+[;,]?\s*`), ""},
{regexp.MustCompile(`(?i)\bcch=[a-f0-9]+[;,]?\s*`), ""},
// Replace "Claude Code" product name with "Cursor" (case-sensitive on purpose).
{regexp.MustCompile(`\bClaude Code\b`), "Cursor"},
// Replace full Anthropic CLI description. Handle both straight and curly apostrophes.
{regexp.MustCompile(`(?i)Anthropic['\x{2019}]s official CLI for Claude`), "Cursor AI assistant"},
// Replace remaining Anthropic brand references.
{regexp.MustCompile(`\bAnthropic\b`), "Cursor"},
// Known Anthropic domains.
{regexp.MustCompile(`(?i)anthropic\.com`), "cursor.com"},
{regexp.MustCompile(`(?i)claude\.ai`), "cursor.sh"},
// Normalise leftover leading semicolons/whitespace at start of content.
{regexp.MustCompile(`^[;,\s]+`), ""},
}
// Text applies all sanitization rules to s.
func Text(s string) string {
if s == "" {
return s
}
for _, r := range rules {
s = r.pattern.ReplaceAllString(s, r.replace)
}
return s
}