b9ee546bc1
Co-authored-by: Cursor <cursoragent@cursor.com>
71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
from typing import Any
|
|
|
|
from app.browser_manager import BrowserManager, BrowserSession
|
|
|
|
|
|
async def handle_input(
|
|
manager: BrowserManager,
|
|
session: BrowserSession,
|
|
payload: dict[str, Any],
|
|
) -> dict[str, Any] | None:
|
|
action = payload.get("action")
|
|
if not action:
|
|
return None
|
|
|
|
manager.touch(session)
|
|
page = session.page
|
|
|
|
if action == "click":
|
|
x = float(payload.get("x", 0))
|
|
y = float(payload.get("y", 0))
|
|
button = payload.get("button", "left")
|
|
await page.mouse.click(x, y, button=button)
|
|
return {"type": "ack", "action": action}
|
|
|
|
if action == "dblclick":
|
|
x = float(payload.get("x", 0))
|
|
y = float(payload.get("y", 0))
|
|
await page.mouse.dblclick(x, y)
|
|
return {"type": "ack", "action": action}
|
|
|
|
if action == "mousemove":
|
|
x = float(payload.get("x", 0))
|
|
y = float(payload.get("y", 0))
|
|
await page.mouse.move(x, y)
|
|
return None
|
|
|
|
if action == "wheel":
|
|
delta_x = float(payload.get("deltaX", 0))
|
|
delta_y = float(payload.get("deltaY", 0))
|
|
await page.mouse.wheel(delta_x, delta_y)
|
|
return {"type": "ack", "action": action}
|
|
|
|
if action == "keydown":
|
|
key = payload.get("key")
|
|
if not key:
|
|
return None
|
|
await page.keyboard.down(key)
|
|
return {"type": "ack", "action": action}
|
|
|
|
if action == "keyup":
|
|
key = payload.get("key")
|
|
if not key:
|
|
return None
|
|
await page.keyboard.up(key)
|
|
return {"type": "ack", "action": action}
|
|
|
|
if action == "type":
|
|
text = payload.get("text", "")
|
|
if text:
|
|
await page.keyboard.type(text)
|
|
return {"type": "ack", "action": action}
|
|
|
|
if action == "press":
|
|
key = payload.get("key")
|
|
if not key:
|
|
return None
|
|
await page.keyboard.press(key)
|
|
return {"type": "ack", "action": action}
|
|
|
|
return {"type": "error", "message": f"未知操作: {action}"}
|