feat: AI 目标设备必选 + 设备任务占用锁——①前端新增「🎯 目标设备」选择器(在线设备含型号,任务运行中的设备禁选),发送必须选定设备,AI 只操作你指定的设备;②/api/agent/run 校验:serial 不在池/离线/任务 running-connecting → 拒绝(409 提示任务名);③MCP 11 个写工具加 busy 锁(_ensure_device_free,5s 缓存):任务运行中的设备 AI 一律拒绝,AI 不与任务抢设备(外部 MCP 客户端同样受保护);④运行状态端点 GET /api/agent/run + 前端 8s 轮询:多人/多窗口能看到运行中任务(发起时间/任务/设备)并可停止(stop 加确认);未选设备直接 400 引导请选择设备,去掉模型自己乱挑设备的行为
This commit is contained in:
@@ -52,6 +52,32 @@ def _check_write():
|
||||
raise PlatformError("write_disabled", "写操作未启用(MCP_ALLOW_WRITE=1 开启)")
|
||||
|
||||
|
||||
# 设备任务占用锁:serial -> (ts, worker_status, task_job)。AI 写操作前检查,
|
||||
# 任务 running/connecting 的设备拒绝操作(AI 不与任务抢设备)。5s 缓存。
|
||||
_busy_cache = {}
|
||||
|
||||
|
||||
def _ensure_device_free(serial):
|
||||
"""写操作前检查设备是否有任务在跑(worker running/connecting → 拒绝)。"""
|
||||
import time
|
||||
now = time.time()
|
||||
c = _busy_cache.get(serial)
|
||||
if not c or now - c[0] > 5:
|
||||
try:
|
||||
devs = platform().list_devices()
|
||||
info = next((d for d in devs if d.get("serial") == serial), {})
|
||||
c = (now, info.get("worker_status") or "idle",
|
||||
info.get("task_job") or "")
|
||||
_busy_cache[serial] = c
|
||||
except Exception:
|
||||
return # 状态查询失败不阻塞(操作失败会另行报错)
|
||||
if c[1] in ("running", "connecting"):
|
||||
raise PlatformError(
|
||||
"device_busy",
|
||||
f"设备正在执行任务「{c[2] or '未知'}」——AI 不与任务抢设备,"
|
||||
f"任务结束后才能操作(可在平台任务页先停止任务)")
|
||||
|
||||
|
||||
def _to_native(serial, x, y):
|
||||
"""截图坐标 → 设备原生坐标(按最近一次截图的比例换算)。"""
|
||||
c = _coord.get(serial)
|
||||
@@ -138,6 +164,7 @@ def de_tap(serial: str, x: int, y: int) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
if x < 0 or y < 0:
|
||||
raise PlatformError("invalid_param", "坐标不能为负")
|
||||
nx, ny = _to_native(serial, x, y)
|
||||
@@ -160,6 +187,7 @@ def de_swipe(serial: str, x1: int, y1: int, x2: int, y2: int,
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
nx1, ny1 = _to_native(serial, x1, y1)
|
||||
nx2, ny2 = _to_native(serial, x2, y2)
|
||||
platform().swipe(serial, nx1, ny1, nx2, ny2, duration)
|
||||
@@ -217,6 +245,7 @@ def de_tap_element(serial: str, by: str, value: str, index: int = 1) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
if by not in ("text", "id", "desc", "text_contains", "desc_contains"):
|
||||
raise PlatformError("invalid_param",
|
||||
"by 可选 text/id/desc/text_contains/desc_contains")
|
||||
@@ -270,6 +299,7 @@ def de_wake(serial: str) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
platform().wake(serial)
|
||||
except PlatformError as e:
|
||||
return _err(e)
|
||||
@@ -283,6 +313,7 @@ def de_press_key(serial: str, key: str) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
if key not in _KEYS:
|
||||
raise PlatformError("invalid_param", f"不支持的按键: {key}(可选 {_KEYS})")
|
||||
platform().press_key(serial, key)
|
||||
@@ -299,6 +330,7 @@ def de_open_app(serial: str, package: str) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
if not package:
|
||||
raise PlatformError("invalid_param", "缺少包名")
|
||||
from mcp_server import direct_ops
|
||||
@@ -317,6 +349,7 @@ def de_stop_app(serial: str, package: str) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
if not package:
|
||||
raise PlatformError("invalid_param", "缺少包名")
|
||||
from mcp_server import direct_ops
|
||||
@@ -350,6 +383,7 @@ def de_type_text(serial: str, text: str) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
if not text:
|
||||
raise PlatformError("invalid_param", "内容为空")
|
||||
from mcp_server import direct_ops
|
||||
@@ -370,6 +404,7 @@ def de_set_clipboard(serial: str, text: str) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
if not text:
|
||||
raise PlatformError("invalid_param", "内容为空")
|
||||
from mcp_server import direct_ops
|
||||
@@ -390,6 +425,7 @@ def de_sleep(serial: str) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
platform().sleep(serial)
|
||||
except PlatformError as e:
|
||||
return _err(e)
|
||||
@@ -429,6 +465,7 @@ def de_tap_text(serial: str, text: str) -> dict:
|
||||
try:
|
||||
_check_write()
|
||||
serial = _check_serial(serial)
|
||||
_ensure_device_free(serial)
|
||||
if not text or len(text) > 100:
|
||||
raise PlatformError("invalid_param", "text 不能为空且 ≤100 字符")
|
||||
res = platform().tap_text(serial, text)
|
||||
|
||||
Reference in New Issue
Block a user