Files
clickthrough/skill/clickthrough_skill.py
2026-04-05 19:15:12 +02:00

65 lines
1.8 KiB
Python

from dataclasses import dataclass
from typing import Any, Dict
import httpx
@dataclass
class ActionPlan:
grid_id: str
target_cell: str | None
action: str
text: str | None = None
class ClickthroughSkill:
"""Lightweight wrapper around the Clickthrough HTTP API."""
def __init__(self, server_url: str = "http://localhost:8000") -> None:
self._client = httpx.Client(base_url=server_url, timeout=10)
def describe_grid(
self,
screenshot_base64: str,
width: int,
height: int,
rows: int = 4,
columns: int = 4,
) -> Dict[str, Any]:
payload = {
"width": width,
"height": height,
"rows": rows,
"columns": columns,
"screenshot_base64": screenshot_base64,
"memo": "agent-powered grid",
}
response = self._client.post("/grid/init", json=payload)
response.raise_for_status()
return response.json()
def plan_action(self, plan: ActionPlan) -> Dict[str, Any]:
payload = {
"grid_id": plan.grid_id,
"action": plan.action,
"target_cell": plan.target_cell,
"text": plan.text,
"comment": "skill-generated plan",
}
response = self._client.post("/grid/action", json=payload)
response.raise_for_status()
return response.json()
if __name__ == "__main__":
import base64
dummy = base64.b64encode(b"fake-screenshot").decode()
skill = ClickthroughSkill()
grid = skill.describe_grid(dummy, width=800, height=600)
print("Grid cells:", len(grid.get("cells", [])))
if grid.get("cells"):
first_cell = grid["cells"][0]["cell_id"]
result = skill.plan_action(ActionPlan(grid_id=grid["grid_id"], target_cell=first_cell, action="click"))
print("Action result:", result)