import type { TFunction } from "i18next"; export function redactActivityText(value: string): string { return value .replace(/(https?:\/\/)[^/@\s]+@/gi, "$1@") .replace(/\b(Bearer)\s+[A-Za-z0-9._~+/=-]+/gi, "$1 ") .replace( /(^|[\s;])((?:[A-Z0-9_]*)(?:API[_-]?KEY|TOKEN|SECRET|PASSWORD|PASS|AUTH)(?:[A-Z0-9_]*))=(?:"[^"]*"|'[^']*'|[^\s]+)/gim, "$1$2=", ) .replace( /(--(?:api-?key|access-?token|token|secret|password)(?:=|\s+))(?:"[^"]*"|'[^']*'|[^\s]+)/gi, "$1", ) .replace(/([?&](?:api_?key|access_?token|token|secret|password)=)[^&\s]+/gi, "$1") .replace( /(["']?authorization["']?\s*[:=]\s*["']?)[^"'\r\n,;}]+/gi, "$1", ) .replace( /(["']?(?:api[_-]?key|access[_-]?token|token|secret|password)["']?\s*[:=]\s*)["']?[^"'\s,&;}]+["']?/gi, "$1", ) .replace(/\b(?:sk(?:-proj)?|xox[baprs]?|xapp)[-_][A-Za-z0-9._-]{8,}\b/gi, "") .replace(/\bgh[pousr]_[A-Za-z0-9]{12,}\b/g, "") .replace(/\bAKIA[A-Z0-9]{16}\b/g, "") .replace(/\b\d{6,12}:[A-Za-z0-9_-]{20,}\b/g, ""); } export function redactShellCommand(command: string): string { return redactActivityText(command).replaceAll("", "••••"); } export function compactActivityPath(value: string): string { return value .replace(/\/Users\/[^/\s"']+/g, "~") .replace(/\/home\/[^/\s"']+/g, "~") .replace(/\/private\/tmp\/[^\s"']+/g, "/tmp/…") .replace(/\/var\/folders\/[^\s"']+/g, "/var/folders/…"); } export function safeActivityDetail(value: string, maxLength = 96): string { return truncateMiddle( compactActivityPath(redactActivityText(value)) .replace(/\/\.nanobot\/tool-results\/[^\s"']+/g, "/.nanobot/tool-results/…") .replace(/\s+/g, " ") .replace(/^["']|["']$/g, "") .trim(), maxLength, ); } export function formatActivityTarget(t: TFunction, action: string, target: string): string { return target ? t("message.agentActivity.actionTarget", { action, target }) : action; } export function summarizeShellCommand(command: string, t: TFunction): string { const lines = redactShellCommand(command.replace(/\r\n/g, "\n")) .split("\n") .map((line) => line.trim()) .filter(Boolean); const firstLine = compactActivityPath(lines[0] || t("message.agentActivity.command")); const firstPreview = truncateMiddle(firstLine, 92); return lines.length <= 1 ? firstPreview : `${firstPreview} · ${t("message.agentActivity.scriptLines", { count: lines.length })}`; } function truncateMiddle(value: string, maxLength: number): string { if (value.length <= maxLength) return value; const head = Math.ceil((maxLength - 1) * 0.62); const tail = Math.floor((maxLength - 1) * 0.38); return `${value.slice(0, head)}…${value.slice(-tail)}`; }