Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
export function redactPhone(phone: string): string {
if (!phone || phone.length < 6) return '***';
const visible = phone.slice(0, 4);
const end = phone.slice(-2);
return `${visible}***${end}`;
}
export function redactUuid(uuid: string): string {
if (!uuid || uuid.length < 8) return '***';
return `${uuid.slice(0, 4)}***`;
}
export function redactIdentity(phone: string, uuid?: string): string {
const phonePart = redactPhone(phone);
if (uuid) {
return `${phonePart} (${redactUuid(uuid)})`;
}
return phonePart;
}
/**
* Secret redaction for logs and tool-result echoes.
* Provider errors embed response bodies that contain API-key fragments;
* command output can contain environment secrets — anything written to
* persistent logs or session transcripts passes through this first.
*/
const SECRET_PATTERNS: RegExp[] = [
// OpenAI / Anthropic / DeepSeek style keys
/\bsk-(?:proj-|ant-|svcacct-)?[A-Za-z0-9_-]{16,}\b/g,
// GitHub tokens
/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}\b/g,
/\bgithub_pat_[A-Za-z0-9_]{20,}\b/g,
// AWS
/\bAKIA[0-9A-Z]{16}\b/g,
// Slack tokens
/\bxox[bapr]-[A-Za-z0-9-]{10,}\b/g,
// Bearer / authorization headers
/\b(?:Bearer|token|authorization)\s+[:=]?\s*[A-Za-z0-9._~+/-]{24,}/gi,
// Generic key=value secrets in config output
/\b(api[_-]?key|secret|access[_-]?token|refresh[_-]?token)\s*[:=]\s*["']?[A-Za-z0-9._~+/=-]{12,}/gi,
];
// Masked whole: a password has no recognizable prefix worth keeping, and a short
// one would be mostly shown by maskMatch. Covers yaml/env values and Basic headers.
const FULL_MASK_PATTERNS: RegExp[] = [
/(?<=\bpassword\s*[:=]\s*["']?)[^\s"']+/gi,
/(?<=\bBasic\s+)[A-Za-z0-9+/]{8,}={0,2}/g,
];
/** Keep enough of the key to identify it, mask the rest. */
function maskMatch(match: string): string {
if (match.length <= 8) return '****';
return `${match.slice(0, 4)}****${match.slice(-4)}`;
}
const registeredSecrets = new Set<string>();
export function registerSecretValue(value: string): void {
if (typeof value === 'string' && value.length >= 8) registeredSecrets.add(value);
}
export function redactSecrets(text: string): string {
if (!text) return text;
let out = text;
for (const secret of registeredSecrets) {
if (out.includes(secret)) out = out.split(secret).join('****');
}
for (const pattern of SECRET_PATTERNS) {
out = out.replace(pattern, maskMatch);
}
for (const pattern of FULL_MASK_PATTERNS) {
out = out.replace(pattern, '****');
}
return out;
}
/**
* Recursively redact strings inside an arbitrary object (error properties,
* nested bodies) for safe logging. Non-destructive: returns a redacted copy.
*/
export function redactDeep(value: unknown, depth = 0): unknown {
if (depth > 6) return '[depth-limit]';
if (typeof value === 'string') return redactSecrets(value);
if (Array.isArray(value)) return value.map((v) => redactDeep(v, depth + 1));
if (value && typeof value === 'object') {
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
out[k] = redactDeep(v, depth + 1);
}
return out;
}
return value;
}
export function userFacingAiError(error: unknown): string {
const message = error instanceof Error ? error.message : typeof error === 'string' ? error : '';
const normalized = message.toLowerCase();
if (/context(?:_|\s|-)?length|too many tokens|maximum context|request too large|payload too large/.test(normalized)) {
return 'This request is too large for the selected model. Shorten the conversation or choose another model.';
}
// Before the rate-limit rule: a budget refusal is our own decision, not the provider's, and
// "please try again shortly" is exactly wrong advice — a ceiling does not lift by itself.
if (/daily token budget|token budget|budget (?:exhausted|exceeded)|exceeded (?:my|its) (?:daily )?budget/.test(normalized)) {
return 'Mercury has used its whole daily token budget, so it stopped answering. Raise the budget, or wait for the daily reset.';
}
if (/rate.?limit|too many requests|quota|capacity|overloaded|\b429\b/.test(normalized)) {
return 'The selected model is temporarily busy. Please try again shortly.';
}
if (/timeout|timed out|deadline|stalled/.test(normalized)) {
return 'The request timed out before Mercury could finish. Please try again.';
}
if (/api.?key|authenticat|unauthori[sz]ed|forbidden|credential|\b401\b|\b403\b/.test(normalized)) {
return 'Mercury could not connect to the selected model. Please try another model or contact support.';
}
if (/model.+(?:not found|unavailable|unsupported|invalid)|no provider available|provider mismatch|\b404\b/.test(normalized)) {
return 'The selected model is currently unavailable. Please choose another model or try again later.';
}
if (/content.?policy|safety|moderation|blocked|refused/.test(normalized)) {
return 'The selected model could not process this request. Revise it and try again.';
}
return 'Mercury could not complete this request. Please try again shortly.';
}