/**
 * Tool call classification — maps raw provider tool names to categories.
 *
 * Raw tool names from providers are noisy and inconsistent (`WebSearch` vs
 * `web_search` vs `Browser.search`). This module normalizes every tool call
 * into one of six standard categories for cross-model comparison.
 *
 * @see docs/design-docs/architecture-overhaul/observability-telemetry.md
 */
// ---------------------------------------------------------------------------
// Default tool name → category mapping
// ---------------------------------------------------------------------------
const DEFAULT_TOOL_CATEGORIES = {
    // Search tools
    Grep: "search",
    WebSearch: "search",
    grep: "search",
    search: "search",
    semantic_search: "search",
    web_search: "search",
    // Read tools
    Glob: "read",
    Read: "read",
    WebFetch: "read",
    cat: "read",
    curl: "read",
    file_read: "read",
    read_file: "read",
    web_fetch: "read",
    // Write tools
    Edit: "write",
    FileEdit: "write",
    Write: "write",
    file_write: "write",
    patch: "write",
    write_file: "write",
    // Execute tools
    Bash: "execute",
    RunCode: "execute",
    bash: "execute",
    exec: "execute",
    python: "execute",
    run_code: "execute",
    shell: "execute",
    // Navigate tools
    "Browser.navigate": "navigate",
    FollowLink: "navigate",
    browse: "navigate",
    follow_link: "navigate",
    navigate: "navigate",
    open_url: "navigate",
    // Communicate tools
    AskUser: "communicate",
    TodoRead: "communicate",
    TodoWrite: "communicate",
    ask_user: "communicate",
    submit_response: "communicate",
};
// ---------------------------------------------------------------------------
// Heuristic patterns (fallback when name not in lookup table)
// ---------------------------------------------------------------------------
const HEURISTIC_PATTERNS = [
    [/search|find|query|lookup|grep/i, "search"],
    [/read|fetch|get|load|cat|view/i, "read"],
    [/write|create|edit|update|patch|save|put|post/i, "write"],
    [/exec|run|bash|shell|python|code|command/i, "execute"],
    [/navigate|browse|open|follow|link|url/i, "navigate"],
    [/ask|user|chat|message|submit|todo|response/i, "communicate"],
];
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Classify a tool call by its raw name.
 *
 * Resolution order:
 * 1. Exact match in custom overrides (if provided)
 * 2. Exact match in default tool categories
 * 3. Heuristic pattern matching on the name
 * 4. Falls back to "execute" (safest default for unknown tools)
 *
 * @param name - Raw tool name from the provider
 * @param customMappings - Optional custom tool → category overrides
 * @returns The classified category
 */
export function classifyToolCall(name, customMappings) {
    // 1. Custom overrides
    if (customMappings?.[name]) {
        return customMappings[name];
    }
    // 2. Default lookup
    if (DEFAULT_TOOL_CATEGORIES[name]) {
        return DEFAULT_TOOL_CATEGORIES[name];
    }
    // 3. Heuristic matching
    for (const [pattern, category] of HEURISTIC_PATTERNS) {
        if (pattern.test(name)) {
            return category;
        }
    }
    // 4. Unknown → execute (safest default)
    return "execute";
}
/**
 * Classify multiple tool calls, returning the category for each.
 * Also tracks unrecognized names for the caller to log warnings.
 */
export function classifyToolCalls(names, customMappings) {
    const categories = [];
    const unrecognized = [];
    for (const name of names) {
        const category = classifyToolCall(name, customMappings);
        categories.push(category);
        // Track names that required heuristic or default fallback
        if (!DEFAULT_TOOL_CATEGORIES[name] && !customMappings?.[name]) {
            unrecognized.push(name);
        }
    }
    return { categories, unrecognized };
}
