def action2create_function()

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


def action2create_function(action: BrowserAction) -> str:
    # this used to be a match statement, changed for 3.9 compatibility
    action_type = action.data["action_type"]
    if action_type == BrowserActionTypes.NONE:
        return "create_none_action()"

    # mouse wheel and keyboard action
    elif action_type == BrowserActionTypes.SCROLL:
        direction = "up" if "up" in action.data["direction"] else "down"
        return f"create_scroll_action({repr(direction)})"
    elif action_type == BrowserActionTypes.KEY_PRESS:
        return f"create_key_press_action({repr(action.data['key_comb'])})"
    # inter-page actions
    elif action_type == BrowserActionTypes.PAGE_FOCUS:
        return f"create_page_focus_action({action.data['page_number']})"
    elif action_type == BrowserActionTypes.NEW_TAB:
        return "create_new_tab_action()"
    elif action_type == BrowserActionTypes.GO_BACK:
        return "create_go_back_action()"
    elif action_type == BrowserActionTypes.GO_FORWARD:
        return "create_go_forward_action()"
    elif action_type == BrowserActionTypes.GOTO_URL:
        return f"create_goto_url_action({repr(action.data['url'])})"
    elif action_type == BrowserActionTypes.PAGE_CLOSE:
        return "create_page_close_action()"

    # low-level keyboard and mouse actions
    elif action_type == BrowserActionTypes.MOUSE_CLICK:
        return f"create_mouse_click_action({action.data['coords'][0]}, {action.data['coords'][1]})"
    elif action_type == BrowserActionTypes.MOUSE_HOVER:
        return f"create_mouse_hover_action({action.data['coords'][0]}, {action.data['coords'][1]})"
    elif action_type == BrowserActionTypes.KEYBOARD_TYPE:
        return (
            f"create_keyboard_type_action({list(map(lambda x: _id2key[x], action.data['text']))})"
        )

    # mid-level keyboard and mouse actions
    elif action_type == BrowserActionTypes.CLICK:
        args = []
        args.append(f"element_id={repr(action.data['element_id'])}")
        args.append(f"element_role={repr(_id2role[action.data['element_role']])}")
        args.append(f"element_name={repr(action.data['element_name'])}")
        args.append(f"pw_code={repr(action.data['pw_code'])}")
        args_str = ", ".join(args)
        return f"create_click_action({args_str})"
    elif action_type == BrowserActionTypes.HOVER:
        args = []
        args.append(f"element_id={repr(action.data['element_id'])}")
        args.append(f"element_role={repr(_id2role[action.data['element_role']])}")
        args.append(f"element_name={repr(action.data['element_name'])}")
        args.append(f"pw_code={repr(action.data['pw_code'])}")
        args_str = ", ".join(args)
        return f"create_hover_action({args_str})"
    elif action_type == BrowserActionTypes.TYPE:
        args = []
        text = "".join(map(lambda x: _id2key[x], action.data["text"]))
        args.append(f"text={repr(text)}")
        args.append(f"element_id={repr(action.data['element_id'])}")
        args.append(f"element_role={repr(_id2role[action.data['element_role']])}")
        args.append(f"element_name={repr(action.data['element_name'])}")
        args.append(f"pw_code={repr(action.data['pw_code'])}")
        args_str = ", ".join(args)
        return f"create_type_action({args_str})"

    # high-level actions, only support locators from playwright
    elif action_type == BrowserActionTypes.CHECK:
        return f"create_check_action(pw_code={repr(action.data['pw_code'])})"
    elif action_type == BrowserActionTypes.SELECT_OPTION:
        return f"create_select_option_action(pw_code={repr(action.data['pw_code'])})"
    elif action_type == BrowserActionTypes.STOP:
        return f'create_stop_action({repr(action.data["answer"])})'
    else:
        raise ValueError(f"Invalid action type: {action.data['action_type']}")