Add files via upload
This commit is contained in:
1 parent
fb46b5e6a1
commit
067f5289b2
26 files changed
+4189
-3033
No files matched your search
+33
-32
@@ -1,6 +1,7 @@
|
||||
#computer_control.py
|
||||
import io
|
||||
import json
|
||||
import platform
|
||||
import re
|
||||
import string
|
||||
import subprocess
|
||||
@@ -39,13 +40,17 @@ def _load_config() -> dict:
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
def _get_os() -> str:
|
||||
import platform
|
||||
s = platform.system().lower()
|
||||
if s == "darwin":
|
||||
return "mac"
|
||||
return s # "windows" or "linux"
|
||||
def _platform_os() -> str:
|
||||
return {"Windows": "windows", "Darwin": "mac", "Linux": "linux"}.get(
|
||||
platform.system(), "linux"
|
||||
)
|
||||
|
||||
def _get_os() -> str:
|
||||
return _load_config().get("os_system", _platform_os()).lower()
|
||||
|
||||
|
||||
def _get_api_key() -> str:
|
||||
return _load_config().get("gemini_api_key", "")
|
||||
|
||||
_SAFE_SCREENSHOT_ROOTS = (
|
||||
Path.home(),
|
||||
@@ -160,7 +165,8 @@ def _smart_type(text: str, clear_first: bool = True) -> str:
|
||||
if len(text) > 20 and _PYPERCLIP:
|
||||
pyperclip.copy(text)
|
||||
time.sleep(0.1)
|
||||
pyautogui.hotkey("ctrl", "v")
|
||||
paste_key = "command" if _get_os() == "mac" else "ctrl"
|
||||
pyautogui.hotkey(paste_key, "v")
|
||||
return f"Smart-typed (clipboard): {text[:60]}{'…' if len(text) > 60 else ''}"
|
||||
|
||||
pyautogui.typewrite(text, interval=0.04)
|
||||
@@ -222,7 +228,8 @@ def _clipboard_paste(text: str) -> str:
|
||||
pyperclip.copy(text)
|
||||
time.sleep(0.1)
|
||||
_require_pyautogui()
|
||||
pyautogui.hotkey("ctrl", "v")
|
||||
paste_key = "command" if _get_os() == "mac" else "ctrl"
|
||||
pyautogui.hotkey(paste_key, "v")
|
||||
return f"Pasted: {text[:60]}{'…' if len(text) > 60 else ''}"
|
||||
return "pyperclip not available"
|
||||
|
||||
@@ -237,7 +244,8 @@ def _screenshot(save_path: str | None = None) -> str:
|
||||
|
||||
def _clear_field() -> str:
|
||||
_require_pyautogui()
|
||||
pyautogui.hotkey("ctrl", "a")
|
||||
select_key = "command" if _get_os() == "mac" else "ctrl"
|
||||
pyautogui.hotkey(select_key, "a")
|
||||
time.sleep(0.1)
|
||||
pyautogui.press("delete")
|
||||
return "Field cleared"
|
||||
@@ -298,27 +306,23 @@ def _focus_window(title: str) -> str:
|
||||
return f"focus_window: unknown OS '{os_name}'"
|
||||
|
||||
def _screen_find(description: str) -> tuple[int, int] | None:
|
||||
api_key = _get_api_key()
|
||||
if not api_key:
|
||||
print("[ComputerControl] ⚠️ No API key for screen_find")
|
||||
return None
|
||||
|
||||
try:
|
||||
import base64
|
||||
import requests as _req
|
||||
import json as _json
|
||||
|
||||
cfg_path = Path(__file__).resolve().parent.parent / "config" / "api_keys.json"
|
||||
try:
|
||||
cfg = _json.loads(cfg_path.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
cfg = {}
|
||||
|
||||
ollama_url = cfg.get("llm_url", "http://localhost:11434").rstrip("/")
|
||||
vision_model = cfg.get("vision_model") or cfg.get("llm_model", "llava")
|
||||
from google import genai
|
||||
from google.genai import types as gtypes
|
||||
|
||||
_require_pyautogui()
|
||||
w, h = pyautogui.size()
|
||||
img = pyautogui.screenshot()
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="PNG")
|
||||
b64 = base64.b64encode(buf.getvalue()).decode("ascii")
|
||||
image_bytes = buf.getvalue()
|
||||
|
||||
client = genai.Client(api_key=api_key)
|
||||
prompt = (
|
||||
f"This is a screenshot of a {w}×{h} pixel screen. "
|
||||
f"Locate the UI element described as: '{description}'. "
|
||||
@@ -326,18 +330,15 @@ def _screen_find(description: str) -> tuple[int, int] | None:
|
||||
f"If the element is not visible, reply: NOT_FOUND"
|
||||
)
|
||||
|
||||
resp = _req.post(
|
||||
f"{ollama_url}/api/chat",
|
||||
json={
|
||||
"model": vision_model,
|
||||
"stream": False,
|
||||
"messages": [{"role": "user", "content": prompt, "images": [b64]}],
|
||||
},
|
||||
timeout=30,
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.5-flash-lite",
|
||||
contents=[
|
||||
gtypes.Part.from_bytes(data=image_bytes, mime_type="image/png"),
|
||||
prompt,
|
||||
],
|
||||
)
|
||||
resp.raise_for_status()
|
||||
text = (resp.json().get("message", {}).get("content") or "").strip()
|
||||
|
||||
text = (response.text or "").strip()
|
||||
if "NOT_FOUND" in text.upper():
|
||||
return None
|
||||
|
||||
|
||||
Reference in new issue
Block a user