package com.gzzm.lobster.guardrails;

import java.util.regex.Pattern;

/**
 * InternalInfoSanitizer —— 脱敏内部信息 /
 * Scrubs internal tool names, paths and internal ids from user-visible text.
 */
public class InternalInfoSanitizer {

    /** 内置工具名（不含 MCP 命名空间）/ Built-in tool names. */
    private static final Pattern BUILTIN_TOOLS = Pattern.compile(
            "\\b(list_files|search_files|read_file|write_file|save_to_oa|"
          + "memory_search|memory_write|list_skills|use_skill|"
          + "confirm_action|ask_user|update_plan|read_externalized_content|read_skill_resource|"
          + "oa_list_files|oa_read_file|oa_write_file|oa_get_file_metadata|"
            + "oa_search_knowledge|oa_get_knowledge_detail|"
            + "oa_mail_query_receivers|oa_mail_list|oa_mail_get|oa_mail_save_draft|oa_mail_send)\\b");

    /** 绝对路径（Windows/Unix）/ Absolute file paths. */
    private static final Pattern ABS_PATH = Pattern.compile(
            "(/[\\w\\-./]+|[A-Z]:\\\\[\\w\\-\\\\/.]+)");

    /** 内部 ID 前缀：th_/msg_/run_/pr_/.../ Internal id prefixes. */
    private static final Pattern INTERNAL_ID = Pattern.compile(
            "\\b(th_|msg_|run_|pr_|art_|ws_|res_|mem_|pln_|pli_|aud_|call_)[a-f0-9]{6,}\\b");

    /** 执行脱敏，返回用户友好文本 / Scrub and return user-friendly text. */
    public String sanitize(String text) {
        if (text == null) return null;
        String out = BUILTIN_TOOLS.matcher(text).replaceAll("【内部能力】");
        out = ABS_PATH.matcher(out).replaceAll("…");
        out = INTERNAL_ID.matcher(out).replaceAll("<id>");
        return out;
    }
}
