|
| 1 | +from beartype import beartype |
| 2 | +from typing import TypedDict |
| 3 | +from enum import IntEnum |
| 4 | + |
| 5 | + |
| 6 | +class Action(TypedDict): |
| 7 | + action_type: int |
| 8 | + element_id: int |
| 9 | + element_name: str |
| 10 | + url: str |
| 11 | + fill_text: str |
| 12 | + |
| 13 | + |
| 14 | +class ActionTypes(IntEnum): |
| 15 | + NONE = 0 |
| 16 | + CLICK = 1 |
| 17 | + GOTO = 2 |
| 18 | + GOOGLE_SEARCH = 3 |
| 19 | + FILL_FORM = 4 |
| 20 | + SWITCH_TAB = 5 |
| 21 | + GO_BACK = 6 |
| 22 | + FILL_SEARCH = 7 |
| 23 | + SELECT_OPTION = 8 |
| 24 | + HOVER = 9 |
| 25 | + SCROLL_DOWN = 10 |
| 26 | + SCROLL_UP = 11 |
| 27 | + CACHE_DATA = 12 |
| 28 | + GET_FINAL_ANSWER = 13 |
| 29 | + |
| 30 | +@beartype |
| 31 | +def create_cache_data_action(elementid: int,fill_text: str) -> Action: |
| 32 | + return { |
| 33 | + "action_type": ActionTypes.CACHE_DATA, |
| 34 | + "element_id": elementid, |
| 35 | + "url": "", |
| 36 | + "fill_text": fill_text, |
| 37 | + "element_name": "" |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +@beartype |
| 42 | +def create_get_final_answer(elementid: int,fill_text: str) -> Action: |
| 43 | + return { |
| 44 | + "action_type": ActionTypes.GET_FINAL_ANSWER, |
| 45 | + "element_id": elementid, |
| 46 | + "url": "", |
| 47 | + "fill_text": fill_text, |
| 48 | + "element_name": "" |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +@beartype |
| 53 | +def create_click_action(elementid: int) -> Action: |
| 54 | + return { |
| 55 | + "action_type": ActionTypes.CLICK, |
| 56 | + "element_id": elementid, |
| 57 | + "url": "", |
| 58 | + "fill_text": "", |
| 59 | + "element_name": "" |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | +@beartype |
| 64 | +def create_goto_action(elementid: int, url: str) -> Action: |
| 65 | + return { |
| 66 | + "action_type": ActionTypes.GOTO, |
| 67 | + "element_id": elementid, |
| 68 | + "url": url, |
| 69 | + "fill_text": "", |
| 70 | + "element_name": "" |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +@beartype |
| 75 | +def create_none_action(elementid: int) -> Action: |
| 76 | + return { |
| 77 | + "action_type": ActionTypes.NONE, |
| 78 | + "element_id": elementid, |
| 79 | + "url": "", |
| 80 | + "fill_text": "", |
| 81 | + "element_name": "" |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +@beartype |
| 86 | +def create_fill_action(elementid: int, fill_text: str) -> Action: |
| 87 | + return { |
| 88 | + "action_type": ActionTypes.FILL_FORM, |
| 89 | + "element_id": elementid, |
| 90 | + "url": "", |
| 91 | + "fill_text": fill_text, |
| 92 | + "element_name": "" |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +@beartype |
| 97 | +def create_fill_search_action(elementid: int, fill_text: str) -> Action: |
| 98 | + return { |
| 99 | + "action_type": ActionTypes.FILL_SEARCH, |
| 100 | + "element_id": elementid, |
| 101 | + "url": "", |
| 102 | + "fill_text": fill_text, |
| 103 | + "element_name": "" |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +@beartype |
| 108 | +def create_search_action(elementid: int, text: str) -> Action: |
| 109 | + return { |
| 110 | + "action_type": ActionTypes.GOOGLE_SEARCH, |
| 111 | + "element_id": elementid, |
| 112 | + "url": "https://www.google.com", |
| 113 | + "fill_text": text, |
| 114 | + "element_name": "" |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | +@beartype |
| 119 | +def create_go_back_action(elementid: int) -> Action: |
| 120 | + return { |
| 121 | + "action_type": ActionTypes.GO_BACK, |
| 122 | + "element_id": elementid, |
| 123 | + "url": "", |
| 124 | + "fill_text": "", |
| 125 | + "element_name": "" |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | +@beartype |
| 130 | +def create_select_option_action(elementid: int, target_value: str) -> Action: |
| 131 | + return { |
| 132 | + "action_type": ActionTypes.SELECT_OPTION, |
| 133 | + "element_id": elementid, |
| 134 | + "url": "", |
| 135 | + "fill_text": target_value, |
| 136 | + "element_name": "" |
| 137 | + } |
| 138 | + |
| 139 | +@beartype |
| 140 | +def create_hover_action(elementid: int) -> Action: |
| 141 | + return { |
| 142 | + "action_type": ActionTypes.HOVER, |
| 143 | + "element_id": elementid, |
| 144 | + "url": "", |
| 145 | + "fill_text": "", |
| 146 | + "element_name": "" |
| 147 | + } |
| 148 | + |
| 149 | +@beartype |
| 150 | +def create_scroll_down_action(elementid: int) -> Action: |
| 151 | + return { |
| 152 | + "action_type": ActionTypes.SCROLL_DOWN, |
| 153 | + "element_id": elementid, |
| 154 | + "url": "", |
| 155 | + "fill_text": "", |
| 156 | + "element_name": "" |
| 157 | + } |
| 158 | + |
| 159 | +@beartype |
| 160 | +def create_scroll_up_action(elementid: int) -> Action: |
| 161 | + return { |
| 162 | + "action_type": ActionTypes.SCROLL_UP, |
| 163 | + "element_id": elementid, |
| 164 | + "url": "", |
| 165 | + "fill_text": "", |
| 166 | + "element_name": "" |
| 167 | + } |
| 168 | + |
| 169 | +@beartype |
| 170 | +def create_action(elementid: int, action_type: str, action_input: str) -> Action: |
| 171 | + if action_type == "click": |
| 172 | + return create_click_action(elementid=elementid) |
| 173 | + elif action_type == "fill_form": |
| 174 | + return create_fill_action(elementid=elementid, fill_text=action_input) |
| 175 | + elif action_type == "fill_search": |
| 176 | + return create_fill_search_action(elementid=elementid, fill_text=action_input) |
| 177 | + elif action_type == "goto": |
| 178 | + return create_goto_action(elementid=elementid, url=action_input) |
| 179 | + elif action_type == "google_search": |
| 180 | + return create_search_action(elementid=elementid, text=action_input) |
| 181 | + elif action_type == "go_back": |
| 182 | + return create_go_back_action(elementid=elementid) |
| 183 | + elif action_type == "select_option": |
| 184 | + return create_select_option_action(elementid=elementid, target_value=action_input) |
| 185 | + elif action_type == "hover": |
| 186 | + return create_hover_action(elementid=elementid) |
| 187 | + elif action_type == "scroll_down": |
| 188 | + return create_scroll_down_action(elementid=elementid) |
| 189 | + elif action_type == "scroll_up": |
| 190 | + return create_scroll_up_action(elementid=elementid) |
| 191 | + elif action_type == "cache_storage": |
| 192 | + return create_cache_data_action(elementid=elementid,fill_text=action_input) |
| 193 | + elif action_type == "get_final_answer": |
| 194 | + return create_get_final_answer(elementid=elementid,fill_text=action_input) |
| 195 | + else: |
| 196 | + return create_none_action(elementid=elementid) |
| 197 | + |
| 198 | + |
| 199 | +__all__ = [ |
| 200 | + "Action", |
| 201 | + "ActionTypes", |
| 202 | + "create_click_action", |
| 203 | + "create_fill_action", |
| 204 | + "create_none_action", |
| 205 | + "create_goto_action", |
| 206 | + "create_search_action", |
| 207 | + "create_go_back_action", |
| 208 | + "create_fill_search_action", |
| 209 | + "create_select_option_action", |
| 210 | + "create_hover_action", |
| 211 | + "create_scroll_down_action", |
| 212 | + "create_scroll_up_action", |
| 213 | + "create_cache_data_action", |
| 214 | + "create_get_final_answer", |
| 215 | + "create_action" |
| 216 | +] |
0 commit comments