const SHELL_TOOL_ALIASES = new Set(['shell', 'shell_command', 'exec_command']);
const APPLY_PATCH_NAME = 'apply_patch';
export function normalizeToolName(value) {
    return typeof value === 'string' ? value.trim().toLowerCase() : '';
}
function extractToolFunctionName(entry) {
    if (!entry || typeof entry !== 'object') {
        return '';
    }
    const fnName = typeof entry.function?.name === 'string'
        ? entry.function.name
        : undefined;
    if (fnName && fnName.trim().length > 0) {
        return fnName.trim();
    }
    const topName = typeof entry.name === 'string' ? entry.name : '';
    return topName.trim();
}
export function isShellToolName(value) {
    return SHELL_TOOL_ALIASES.has(normalizeToolName(value));
}
export function hasApplyPatchToolDeclared(tools) {
    if (!Array.isArray(tools)) {
        return false;
    }
    return tools.some((entry) => normalizeToolName(extractToolFunctionName(entry)) === APPLY_PATCH_NAME);
}
export function buildShellDescription(toolDisplayName, hasApplyPatch) {
    const label = toolDisplayName && toolDisplayName.trim().length > 0
        ? toolDisplayName.trim()
        : 'shell';
    const base = 'Runs a shell command and returns its output.';
    const workdirLine = `- Always set the \`workdir\` param when using the ${label} function. Avoid using \`cd\` unless absolutely necessary.`;
    const applyPatchLine = '- Prefer apply_patch for editing files instead of shell redirection or here-doc usage.';
    return hasApplyPatch ? `${base}\n${workdirLine}\n${applyPatchLine}` : `${base}\n${workdirLine}`;
}
export function appendApplyPatchReminder(description, hasApplyPatch) {
    if (!hasApplyPatch) {
        return description;
    }
    const trimmed = description?.trim() ?? '';
    if (!trimmed) {
        return buildShellDescription('shell', true);
    }
    if (trimmed.includes('apply_patch')) {
        return trimmed;
    }
    const applyPatchLine = '- Prefer apply_patch for editing files instead of shell redirection or here-doc usage.';
    return `${trimmed}\n${applyPatchLine}`;
}
