function normalized(text) {
    return text.toLowerCase();
}
function matchesIntent(tool, signals) {
    const haystack = normalized(`${tool.name} ${tool.description ?? ''}`);
    return signals.some((signal) => haystack.includes(normalized(signal)));
}
function groupNamesForTool(toolName, groups) {
    return Object.entries(groups)
        .filter(([, tools]) => tools.includes(toolName))
        .map(([group]) => group);
}
export function toolsFromPolicyGroups(policy) {
    return [...new Set(Object.values(policy.tools.groups).flat())].map((name) => ({ name }));
}
export function recommendRelevantTools(input) {
    const unsupported = new Set(input.hostUnsupportedTools ?? []);
    const actionSignals = [input.action, ...input.intentSignals].map(normalized);
    const recommended = [];
    const avoided = [];
    for (const tool of input.tools) {
        const groups = groupNamesForTool(tool.name, input.policy.tools.groups);
        const isUnsupported = unsupported.has(tool.name);
        const isRelevant = !isUnsupported &&
            (matchesIntent(tool, actionSignals) ||
                groups.some((group) => actionSignals.includes(normalized(group))));
        if (isRelevant && recommended.length < input.policy.tools.maxRecommended) {
            recommended.push({
                decision: 'recommend',
                target: tool.name,
                reason: 'Tool matches the current action, intent signals, or policy tool group.',
                evidence: [
                    { source: 'policy', key: 'tools.groups', value: groups.join(',') },
                    { source: 'runtime', key: 'intentSignals', value: input.intentSignals.join(',') },
                ],
                confidence: groups.length > 0 ? 'high' : 'medium',
            });
            continue;
        }
        avoided.push({
            decision: 'avoid',
            target: tool.name,
            reason: isUnsupported
                ? 'Host capability marks this tool unsupported.'
                : 'Tool does not match the current action or intent and can add avoidable tool metadata overhead.',
            evidence: [
                {
                    source: isUnsupported ? 'host-capability' : 'policy',
                    key: isUnsupported ? 'unsupportedTools' : 'tools.groups',
                },
            ],
            confidence: isUnsupported ? 'high' : 'medium',
        });
    }
    return { recommended, avoided };
}
//# sourceMappingURL=tool-relevance.js.map