import { ClarkStateNames } from "../state-machine/clark-fsm.js";
/** A stop condition that checks if the last step used a specific tool and got a successful result.
 * A tool result is considered successful if it exists and its output.success is not explicitly false.
 * Tools that don't return a success field are treated as successful (backwards compatibility).
 *
 * @param toolName - name of the tool to condition on
 * @param maxAttempts - stop the loop if the tool has been called this many times without success
 */
export function hasToolSuccess(toolName, maxAttempts = 5) {
    return ({ steps }) => {
        const hasSuccess = steps[steps.length - 1]?.toolResults?.some((toolResult) => toolResult.toolName === toolName &&
            toolResult.output?.success !==
                false) ?? false;
        if (hasSuccess) {
            return true;
        }
        const attempts = steps
            .flatMap((step) => step.toolCalls || [])
            .filter((toolCall) => toolCall.toolName === toolName).length;
        return attempts >= maxAttempts;
    };
}
export function isAwaitingUser(clark) {
    return () => {
        return clark.state === ClarkStateNames.AwaitingUser;
    };
}
export const needsPermissionToExecute = (clark) => {
    return async () => {
        return clark.context.pendingToolPermissionRequest !== undefined;
    };
};
//# sourceMappingURL=stop-condition.js.map