lc-mcp v0.5: 语义化操控(MCP stdio) — OCR文本化+语义点击, win/linux/mac 截图与输入后端
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""平台抽象:截图 + 输入后端,按平台注册。
|
||||
core (MCP协议/OCR/语义工具) 平台无关。"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def detect_platform():
|
||||
p = os.environ.get('LCP_PLATFORM', '')
|
||||
if p:
|
||||
return p.lower()
|
||||
if sys.platform.startswith('win'):
|
||||
return 'win'
|
||||
if sys.platform == 'darwin':
|
||||
return 'mac'
|
||||
return 'linux'
|
||||
|
||||
|
||||
def get_screenshot_provider(platform=None):
|
||||
platform = platform or detect_platform()
|
||||
if platform == 'win':
|
||||
from .win import WinScreenshotProvider
|
||||
return WinScreenshotProvider()
|
||||
if platform == 'linux':
|
||||
from .linux import LinuxScreenshotProvider
|
||||
return LinuxScreenshotProvider()
|
||||
if platform == 'mac':
|
||||
from .mac import MacScreenshotProvider
|
||||
return MacScreenshotProvider()
|
||||
raise ValueError('no screenshot provider for %s' % platform)
|
||||
|
||||
|
||||
def get_input_backend(name=None, platform=None):
|
||||
platform = platform or detect_platform()
|
||||
name = (name or os.environ.get('INPUT_BACKEND', '') or
|
||||
{'win': 'sendinput', 'linux': 'xdotool', 'mac': 'cg'}[platform]).lower()
|
||||
if name == 'mock':
|
||||
from backends.mock import MockBackend
|
||||
return MockBackend()
|
||||
if platform == 'win':
|
||||
if name == 'sendinput':
|
||||
from backends.sendinput_win import SendInputBackend
|
||||
return SendInputBackend()
|
||||
if name == 'interception':
|
||||
from backends.interception_win import InterceptionBackend
|
||||
return InterceptionBackend()
|
||||
if platform == 'linux':
|
||||
if name == 'xdotool':
|
||||
from backends.xdotool_linux import XdotoolBackend
|
||||
return XdotoolBackend()
|
||||
if name == 'uinput':
|
||||
from backends.uinput_linux import UinputBackend
|
||||
return UinputBackend()
|
||||
if platform == 'mac' and name == 'cg':
|
||||
from backends.cg_mac import CGBackend
|
||||
return CGBackend()
|
||||
raise ValueError('no input backend %s for %s' % (name, platform))
|
||||
@@ -0,0 +1,34 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Linux 截图提供者:X11 (PIL ImageGrab / scrot 兜底)。
|
||||
Wayland 下 ImageGrab 不可用,需额外方案(见 README)。
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
|
||||
class LinuxScreenshotProvider:
|
||||
|
||||
def grab(self):
|
||||
# 1) PIL ImageGrab (X11)
|
||||
try:
|
||||
from PIL import ImageGrab
|
||||
img = ImageGrab.grab()
|
||||
path = os.path.join(tempfile.gettempdir(), 'lc_shot_%s.png' % time.strftime('%H%M%S'))
|
||||
img.save(path)
|
||||
return {'ok': True, 'path': path}
|
||||
except Exception:
|
||||
pass
|
||||
# 2) scrot / import (ImageMagick) / gnome-screenshot 兜底
|
||||
for cmd in (['scrot', '-z'], ['import', '-window', 'root'], ['gnome-screenshot', '-f']):
|
||||
if shutil.which(cmd[0]):
|
||||
path = os.path.join(tempfile.gettempdir(), 'lc_shot_%s.png' % time.strftime('%H%M%S'))
|
||||
try:
|
||||
subprocess.run(cmd + [path], check=True, timeout=20)
|
||||
if os.path.exists(path):
|
||||
return {'ok': True, 'path': path}
|
||||
except Exception:
|
||||
continue
|
||||
return {'ok': False, 'error': 'no working screenshot method on this Linux (need X11 + PIL/scrot)'}
|
||||
@@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""macOS 截图提供者:screencapture CLI (系统自带)。"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
|
||||
class MacScreenshotProvider:
|
||||
|
||||
def grab(self):
|
||||
path = os.path.join(tempfile.gettempdir(), 'lc_shot_%s.png' % time.strftime('%H%M%S'))
|
||||
try:
|
||||
subprocess.run(['screencapture', '-x', path], check=True, timeout=20)
|
||||
if os.path.exists(path):
|
||||
return {'ok': True, 'path': path}
|
||||
except Exception as e:
|
||||
return {'ok': False, 'error': 'screencapture fail: %s' % e}
|
||||
return {'ok': False, 'error': 'no png produced'}
|
||||
@@ -0,0 +1,76 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Windows 截图提供者:交互计划任务(LogonType Interactive)。
|
||||
OpenSSH 服务会话(session 0)没有交互桌面,截图/前端输入必须在交互会话执行。
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
USER = os.environ.get('USERNAME', 'yws-hhh')
|
||||
BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp')
|
||||
os.makedirs(BASE, exist_ok=True)
|
||||
OUT_DIR = r'C:\ProgramData\hduty'
|
||||
os.makedirs(OUT_DIR, exist_ok=True)
|
||||
JOB_PS1 = os.path.join(BASE, 'job.ps1')
|
||||
RUN_VBS = os.path.join(BASE, 'run.vbs')
|
||||
TASK = 'LcMCP'
|
||||
|
||||
if not os.path.exists(RUN_VBS):
|
||||
with open(RUN_VBS, 'w', encoding='utf-8') as f:
|
||||
f.write('CreateObject("WScript.Shell").Run "powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%s"", 0, False\n' % JOB_PS1)
|
||||
|
||||
|
||||
def _pshell(code):
|
||||
r = subprocess.run(['powershell.exe', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', code],
|
||||
capture_output=True, text=True, encoding='utf-8', errors='replace', timeout=60)
|
||||
return r.returncode, (r.stdout or ''), (r.stderr or '')
|
||||
|
||||
|
||||
def _run_interactive(ps1_code, expect_file, timeout=15):
|
||||
with open(JOB_PS1, 'w', encoding='utf-8') as f:
|
||||
f.write('$ErrorActionPreference="Stop"\n' + ps1_code)
|
||||
try:
|
||||
if os.path.exists(expect_file):
|
||||
os.remove(expect_file)
|
||||
except Exception:
|
||||
pass
|
||||
reg = ("Unregister-ScheduledTask -TaskName %s -Confirm:$false -ErrorAction SilentlyContinue; "
|
||||
"$A=New-ScheduledTaskAction -Execute 'wscript.exe' -Argument '%s'; "
|
||||
"$P=New-ScheduledTaskPrincipal -UserId '%s' -LogonType Interactive -RunLevel Highest; "
|
||||
"Register-ScheduledTask -TaskName %s -Action $A -Principal $P -Force | Out-Null; "
|
||||
"Start-ScheduledTask -TaskName %s; 'OK'"
|
||||
% (TASK, RUN_VBS.replace("'", "''"), USER, TASK, TASK))
|
||||
rc, out, err = _pshell(reg)
|
||||
if 'OK' not in out:
|
||||
return {"ok": False, "error": "task start fail: %s %s" % (out[-150:], err[-150:])}
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
if os.path.exists(expect_file):
|
||||
try:
|
||||
with open(expect_file, 'r', encoding='utf-8') as f:
|
||||
return {"ok": True, "out": f.read().strip()}
|
||||
except Exception:
|
||||
return {"ok": True, "out": ''}
|
||||
time.sleep(0.4)
|
||||
return {"ok": False, "error": "timeout waiting %s" % expect_file}
|
||||
|
||||
|
||||
class WinScreenshotProvider:
|
||||
|
||||
def grab(self):
|
||||
name = 'shot_%s.png' % time.strftime('%Y%m%d_%H%M%S')
|
||||
img_path = os.path.join(OUT_DIR, name)
|
||||
ps = ("Add-Type -AssemblyName System.Windows.Forms,System.Drawing; "
|
||||
"$b=[System.Windows.Forms.Screen]::PrimaryScreen.Bounds; "
|
||||
"$bmp=New-Object System.Drawing.Bitmap $b.Width,$b.Height; "
|
||||
"$g=[System.Drawing.Graphics]::FromImage($bmp); "
|
||||
"$g.CopyFromScreen($b.X,$b.Y,0,0,$b.Size); "
|
||||
"$bmp.Save('%s'); $g.Dispose(); $bmp.Dispose(); "
|
||||
"$f=Join-Path '%s' 'done.txt'; 'DONE' | Set-Content $f"
|
||||
% (img_path, OUT_DIR))
|
||||
r = _run_interactive(ps, os.path.join(OUT_DIR, 'done.txt'), timeout=20)
|
||||
if not r.get('ok'):
|
||||
return {'ok': False, 'error': r.get('error', 'screenshot fail')}
|
||||
if not os.path.exists(img_path):
|
||||
return {'ok': False, 'error': 'png not produced'}
|
||||
return {'ok': True, 'path': img_path.replace('\\', '/')}
|
||||
Reference in New Issue
Block a user