def browser_is_equivalent()

in evals/elsuite/multistep_web_tasks/webarena/browser_env/actions.py [0:0]


def browser_is_equivalent(a_action: BrowserAction, b_action: BrowserAction) -> bool:
    """Return True if two actions are equal."""
    a, b = a_action.data, b_action.data
    a_action_type, b_action_type = a["action_type"], b["action_type"]
    if a_action_type != b_action_type:
        return False

    # used to be match statement
    if a_action_type == BrowserActionTypes.NONE:
        return True
    elif a_action_type == BrowserActionTypes.SCROLL:
        da = "up" if "up" in a["direction"] else "down"
        db = "up" if "up" in b["direction"] else "down"
        return da == db
    elif a_action_type == BrowserActionTypes.KEY_PRESS:
        return a["key_comb"] == b["key_comb"]
    elif a_action_type in [BrowserActionTypes.MOUSE_CLICK, BrowserActionTypes.MOUSE_HOVER]:
        return np.allclose(a["coords"], b["coords"])
    elif a_action_type == BrowserActionTypes.KEYBOARD_TYPE:
        return a["text"] == b["text"]
    elif a_action_type in [
        BrowserActionTypes.CLICK,
        BrowserActionTypes.HOVER,
        BrowserActionTypes.TYPE,
    ]:  # TODO: can be further optimized
        if a["element_id"] and b["element_id"]:
            return a["element_id"] == b["element_id"]
        elif a["element_role"] and b["element_role"]:
            return a["element_role"] == b["element_role"] and a["element_name"] == b["element_name"]
        elif a["pw_code"] and b["pw_code"]:
            return a["pw_code"] == b["pw_code"]
        else:
            return False
    elif a_action_type == BrowserActionTypes.PAGE_FOCUS:
        return a["page_number"] == b["page_number"]
    elif a_action_type == BrowserActionTypes.NEW_TAB:
        return True
    elif a_action_type == BrowserActionTypes.GO_BACK:
        return True
    elif a_action_type == BrowserActionTypes.GO_FORWARD:
        return True
    elif a_action_type == BrowserActionTypes.GOTO_URL:
        return a["url"] == b["url"]
    elif a_action_type == BrowserActionTypes.PAGE_CLOSE:
        return True
    elif a_action_type in [BrowserActionTypes.CHECK, BrowserActionTypes.SELECT_OPTION]:
        return a["pw_code"] == b["pw_code"]
    elif a_action_type == BrowserActionTypes.STOP:
        return a["answer"] == b["answer"]
    else:
        raise ValueError(f"Unknown action type: {a['action_type']}")