|
11 | 11 |
|
12 | 12 | from macosagent.agents.wechat_agent.controller.registry.service import Registry
|
13 | 13 | from macosagent.agents.wechat_agent.wechat.context import WechatContext
|
14 |
| -from macosagent.agents.wechat_agent.controller.views import ClickElementAction, InputAction, DoneAction, ScrollAction, PasteAction, CopyAction, RightClickElementAction, SendAction |
| 14 | +from macosagent.agents.wechat_agent.controller.views import ClickElementAction, InputAction, DoneAction, ScrollAction, PasteAction, CopyAction, RightClickElementAction, SendAction, ExtractContent |
15 | 15 | from macosagent.agents.wechat_agent.agent.views import ActionResult, ActionModel
|
16 | 16 | #from macosagent.agents.wechat_agent.agent.llm import create_gpt_4o_tool
|
17 | 17 |
|
@@ -136,27 +136,67 @@ async def inputs(params: InputAction, context: WechatContext):
|
136 | 136 | return ActionResult(is_done=False, success=False, extracted_content=str(e), include_in_memory=True)
|
137 | 137 |
|
138 | 138 | @self.registry.action(
|
139 |
| - "Scroll element", |
| 139 | + "Scroll element up", |
140 | 140 | param_model=ScrollAction,
|
141 | 141 | )
|
142 |
| - async def scroll(params: ScrollAction, context: WechatContext): |
| 142 | + async def scroll_up(params: ScrollAction, context: WechatContext): |
143 | 143 | index = params.index
|
144 | 144 | element = [item for item in context.state.accessibility_tree_json if item["id"] == index]
|
145 | 145 | x, y, w, h = element[0]["bbox"]
|
146 | 146 | offset = context.state.offset
|
147 |
| - x = int(x + offset[0] + w/2) |
148 |
| - y = int(y + offset[1] + h/2) |
149 |
| - logger.info(f"Scroll element {element[0]}") |
| 147 | + x = int(x + offset[0] + w / 2) |
| 148 | + y = int(y + offset[1] + h / 2) |
150 | 149 | try:
|
151 |
| - amount = params.amount if params.amount is not None else 0 # Default to scrolling one page if amount is None |
152 |
| - logger.info(f"Scrolling {amount} pixels") |
153 |
| - # pyautogui.click(x=x, y=y, clicks=1, interval=0.1, button='left') |
| 150 | + amount = -abs(params.amount) if params.amount is not None else -10 |
| 151 | + logger.info(f"Scroll element {element[0]} up") |
154 | 152 | pyautogui.moveTo(x, y, duration=0.1)
|
155 |
| - pyautogui.scroll(amount) # Use pyautogui to scroll by 'amount' pixels |
156 |
| - return ActionResult(is_done=False, success=True, extracted_content=f"🌀 Scrolled {amount} pixels", include_in_memory=True) |
| 153 | + pyautogui.scroll(amount) |
| 154 | + return ActionResult( |
| 155 | + is_done=False, |
| 156 | + success=True, |
| 157 | + extracted_content=f"🌀 Scrolled up {abs(amount)} pixels", |
| 158 | + include_in_memory=True, |
| 159 | + ) |
157 | 160 | except Exception as e:
|
158 |
| - logger.error(f"Error scrolling: {e}") |
159 |
| - return ActionResult(is_done=False, success=False, extracted_content=str(e), include_in_memory=True) |
| 161 | + logger.error(f"Error scrolling up: {e}") |
| 162 | + return ActionResult( |
| 163 | + is_done=False, |
| 164 | + success=False, |
| 165 | + extracted_content=str(e), |
| 166 | + include_in_memory=True, |
| 167 | + ) |
| 168 | + |
| 169 | + @self.registry.action( |
| 170 | + "Scroll element down", |
| 171 | + param_model=ScrollAction, |
| 172 | + ) |
| 173 | + async def scroll_down(params: ScrollAction, context: WechatContext): |
| 174 | + index = params.index |
| 175 | + element = [item for item in context.state.accessibility_tree_json if item["id"] == index] |
| 176 | + x, y, w, h = element[0]["bbox"] |
| 177 | + offset = context.state.offset |
| 178 | + x = int(x + offset[0] + w / 2) |
| 179 | + y = int(y + offset[1] + h / 2) |
| 180 | + try: |
| 181 | + amount = abs(params.amount) if params.amount is not None else 10 |
| 182 | + logger.info(f"Scroll element {element[0]} down") |
| 183 | + pyautogui.moveTo(x, y, duration=0.1) |
| 184 | + pyautogui.scroll(amount) |
| 185 | + return ActionResult( |
| 186 | + is_done=False, |
| 187 | + success=True, |
| 188 | + extracted_content=f"🌀 Scrolled down {abs(amount)} pixels", |
| 189 | + include_in_memory=True, |
| 190 | + ) |
| 191 | + except Exception as e: |
| 192 | + logger.error(f"Error scrolling down: {e}") |
| 193 | + return ActionResult( |
| 194 | + is_done=False, |
| 195 | + success=False, |
| 196 | + extracted_content=str(e), |
| 197 | + include_in_memory=True, |
| 198 | + ) |
| 199 | + |
160 | 200 |
|
161 | 201 | @self.registry.action(
|
162 | 202 | "Paste clipboard text",
|
@@ -237,6 +277,23 @@ async def send(params: SendAction, context: WechatContext):
|
237 | 277 | logger.error(f"Error pasting text: {e}")
|
238 | 278 | return ActionResult(is_done=False, success=False, extracted_content=str(e), include_in_memory=True)
|
239 | 279 |
|
| 280 | + @self.registry.action( |
| 281 | + "Extract content", |
| 282 | + param_model=ExtractContent, |
| 283 | + ) |
| 284 | + async def extract_content(params: ExtractContent, context: WechatContext): |
| 285 | + try: |
| 286 | + target = params.target |
| 287 | + content = params.content |
| 288 | + |
| 289 | + logger.info(f"Extracted content based on the requirement '{target}': {content}") |
| 290 | + |
| 291 | + return ActionResult(is_done=False, success=True, extracted_content=f"📋 Extracted content based on the requirement '{target}': {content}", include_in_memory=True) |
| 292 | + |
| 293 | + except Exception as e: |
| 294 | + logger.error(f"Error extracting content based on the requirement '{target}': {e}") |
| 295 | + return ActionResult(is_done=False, success=False, extracted_content=str(e), include_in_memory=True) |
| 296 | + |
240 | 297 | # @self.registry.action(
|
241 | 298 | # "Modify clipboard text",
|
242 | 299 | # param_model=ModifyClipboardTextAction,
|
|
0 commit comments