import { assertValidProviderTurnActionPolicy } from "./provider-action-policy.js";
export function isProviderToolSelectableByConstraint(constraint, toolName) {
    if (constraint.forbiddenToolNames.includes(toolName)) {
        return false;
    }
    if (constraint.selectableToolNames && !constraint.selectableToolNames.includes(toolName)) {
        return false;
    }
    if (constraint.visibleToolNames && !constraint.visibleToolNames.includes(toolName)) {
        return false;
    }
    return true;
}
export function resolveProviderVisibleTools(availableTools, requiredToolConstraint, _messages = []) {
    return resolveProviderToolVisibility(availableTools, requiredToolConstraint).visibleTools;
}
export function resolveProviderToolVisibility(availableTools, requiredToolConstraint, _messages = []) {
    const forbiddenToolNames = new Set(requiredToolConstraint?.forbiddenToolNames ?? []);
    const visibleToolNames = resolveVisibleToolBoundary(requiredToolConstraint);
    const visibleToolNameSet = visibleToolNames ? new Set(visibleToolNames) : undefined;
    const hiddenTools = [];
    const visibleTools = [];
    for (const tool of availableTools) {
        if (forbiddenToolNames.has(tool.name)) {
            hiddenTools.push({ name: tool.name, reason: "explicit-forbidden-tool" });
            continue;
        }
        if (visibleToolNameSet && !visibleToolNameSet.has(tool.name)) {
            hiddenTools.push({ name: tool.name, reason: "explicit-tool-allow-list" });
            continue;
        }
        visibleTools.push(tool);
    }
    return {
        hiddenTools,
        policy: forbiddenToolNames.size > 0 || visibleToolNameSet
            ? "restricted-by-user-tool-set"
            : "full-surface",
        visibleTools,
    };
}
function resolveVisibleToolBoundary(constraint) {
    if (!constraint) {
        return undefined;
    }
    if (constraint.visibleToolNames) {
        return constraint.visibleToolNames;
    }
    if (constraint.reason === "explicit-exclusive-tool-set") {
        return constraint.selectableToolNames;
    }
    return undefined;
}
export function resolveProviderTurnActionPolicy(input) {
    let policy;
    if (input.requestKind === "generate") {
        policy = {
            allowedActions: ["respond"],
            directResponseAllowed: true,
        };
        assertValidProviderTurnActionPolicy(policy);
        return policy;
    }
    if (input.allowPlanUpdate) {
        policy = {
            allowedActions: input.visibleTools.length > 0 ? ["tool", "plan_update"] : ["plan_update"],
            directResponseAllowed: false,
            required: {
                kind: "plan_update",
                reason: "plan-authoring-turn",
            },
        };
        assertValidProviderTurnActionPolicy(policy);
        return policy;
    }
    if (input.allowPhaseResult) {
        policy = {
            allowedActions: input.visibleTools.length > 0 ? ["tool", "phase_result"] : ["phase_result"],
            directResponseAllowed: false,
            required: {
                kind: "phase_result",
                phaseName: "current",
                reason: "phase-completion-turn",
            },
        };
        assertValidProviderTurnActionPolicy(policy);
        return policy;
    }
    policy = {
        allowedActions: input.visibleTools.length > 0 ? ["respond", "tool"] : ["respond"],
        directResponseAllowed: true,
    };
    assertValidProviderTurnActionPolicy(policy);
    return policy;
}
//# sourceMappingURL=tool-visibility-policy.js.map