修复 v0.5 审查问题: platform 改名, 协议健壮性, CJK 匹配, VBS 引号, interception API

Browse Source
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yws
2026-10-02 01:39:06 +00:00
co-authored by Claude Opus 5.5
parent 176570e7cd
commit 809a7fb017
12 changed files with 667 additions and 360 deletions
+58
View File
@@ -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))
+34
View File
@@ -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)'}
+20
View File
@@ -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'}
+122
View File
@@ -0,0 +1,122 @@
# -*- coding: utf-8 -*-
"""Windows 截图提供者:交互计划任务(LogonType Interactive)。
OpenSSH 服务会话(session 0)没有交互桌面,截图/前端输入必须在交互会话执行。
任务只注册一次(RunLevel Limited, 截图不需要管理员), 之后每次只改 job.ps1 + Start。
"""
import os
import subprocess
import time
USER = os.environ.get('USERNAME', 'yws-hhh')
BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp')
# 默认放用户目录(只有本人可写); ProgramData 子目录对其他本地用户可写
OUT_DIR = os.environ.get('LCP_SHOT_DIR') or os.path.join(BASE, 'shots')
JOB_PS1 = os.path.join(BASE, 'job.ps1')
RUN_VBS = os.path.join(BASE, 'run.vbs')
TASK = 'LcMCP'
def _ps_quote(s):
"""PowerShell 单引号字面量。"""
return "'" + str(s).replace("'", "''") + "'"
def _vbs_content(job_ps1):
# VBS 字符串里的双引号要写成 ""
cmd = 'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%s"' % job_ps1
return 'CreateObject("WScript.Shell").Run "%s", 0, False\n' % cmd.replace('"', '""')
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 _register_task():
"""注册(或覆盖)交互计划任务。进程生命周期内只做一次。"""
reg = ("$A=New-ScheduledTaskAction -Execute 'wscript.exe' -Argument %s; "
"$P=New-ScheduledTaskPrincipal -UserId %s -LogonType Interactive -RunLevel Limited; "
"$S=New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries "
"-ExecutionTimeLimit (New-TimeSpan -Minutes 2) -MultipleInstances Parallel; "
"Register-ScheduledTask -TaskName %s -Action $A -Principal $P -Settings $S -Force | Out-Null; 'OK'"
% (_ps_quote('"%s"' % RUN_VBS), _ps_quote(USER), _ps_quote(TASK)))
rc, out, err = _pshell(reg)
if 'OK' not in out:
raise RuntimeError('register task fail: %s %s' % (out[-150:], err[-150:]))
def _run_interactive(ps1_code, expect_file, timeout=15):
# 带 BOM: Windows PowerShell 5.1 把无 BOM 的 .ps1 当 ANSI 读, 中文路径会乱码
with open(JOB_PS1, 'w', encoding='utf-8-sig') as f:
f.write('$ErrorActionPreference="Stop"\n' + ps1_code)
try:
if os.path.exists(expect_file):
os.remove(expect_file)
except Exception:
pass
rc, out, err = _pshell("Start-ScheduledTask -TaskName %s; 'OK'" % _ps_quote(TASK))
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-sig') as f:
out = f.read().strip()
except Exception:
out = ''
try:
os.remove(expect_file)
except Exception:
pass
return {"ok": True, "out": out}
time.sleep(0.2)
return {"ok": False, "error": "timeout waiting %s" % expect_file}
# 截图前声明 DPI 感知, 否则缩放 != 100% 时 Bounds 是逻辑尺寸, 截出来是裁切的
_SHOT_PS = r"""
Add-Type -AssemblyName System.Windows.Forms,System.Drawing
Add-Type -Namespace LcMcp -Name Dpi -MemberDefinition '[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();'
[void][LcMcp.Dpi]::SetProcessDPIAware()
$img = %(img)s
$done = %(done)s
try {
$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($img, [System.Drawing.Imaging.ImageFormat]::Png)
$g.Dispose(); $bmp.Dispose()
'DONE' | Set-Content -Encoding UTF8 $done
} catch {
('ERR ' + $_.Exception.Message) | Set-Content -Encoding UTF8 $done
}
"""
class WinScreenshotProvider:
def __init__(self):
os.makedirs(BASE, exist_ok=True)
os.makedirs(OUT_DIR, exist_ok=True)
# WSH 只认 ANSI / UTF-16(BOM), UTF-8 下中文路径会坏
with open(RUN_VBS, 'w', encoding='utf-16') as f:
f.write(_vbs_content(JOB_PS1))
_register_task()
def grab(self):
stamp = time.strftime('%Y%m%d_%H%M%S') + '_%03d' % (int(time.time() * 1000) % 1000)
img_path = os.path.join(OUT_DIR, 'shot_%s.png' % stamp)
done = os.path.join(OUT_DIR, 'done_%s.txt' % stamp)
ps = _SHOT_PS % {'img': _ps_quote(img_path), 'done': _ps_quote(done)}
r = _run_interactive(ps, done, timeout=20)
if not r.get('ok'):
return {'ok': False, 'error': r.get('error', 'screenshot fail')}
if r.get('out', '').startswith('ERR'):
return {'ok': False, 'error': r['out']}
if not os.path.exists(img_path):
return {'ok': False, 'error': 'png not produced'}
return {'ok': True, 'path': img_path.replace('\\', '/')}