222 lines
11 KiB
Python
222 lines
11 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""MCP (JSON-RPC 2.0) 处理: 与传输方式无关, handle(msg) -> 响应 dict 或 None(通知)。"""
|
||
import json
|
||
import math
|
||
import threading
|
||
import time
|
||
|
||
from . import VERSION
|
||
from .aalc_task import TASKS
|
||
from .engine import find_hits
|
||
|
||
MAX_WAIT = 300.0
|
||
WAIT_INTERVAL = 1.0
|
||
INPUT_TOOLS = {'click', 'click_text', 'click_image', 'keypress', 'type_text', 'drag'}
|
||
TASK_TOOLS = {'run_task', 'task_status', 'stop_task'}
|
||
|
||
|
||
def _obj(props=None, required=()):
|
||
s = {'type': 'object', 'properties': props or {}}
|
||
if required:
|
||
s['required'] = list(required)
|
||
return s
|
||
|
||
|
||
_STR = {'type': 'string'}
|
||
_NUM = {'type': 'number'}
|
||
_TIMEOUT = {'type': 'number', 'default': 30, 'description': '秒, 上限 300'}
|
||
_IMG = {'name': {'type': 'string', 'description': 'AALC 模板图相对路径, 如 battle/turn_assets.png'},
|
||
'threshold': {'type': 'number', 'default': 0.8},
|
||
'model': {'type': 'string', 'enum': ['clam', 'normal', 'aggressive'], 'default': 'normal',
|
||
'description': 'clam=只在模板原位置找, normal=扩大范围, aggressive=全屏'}}
|
||
|
||
TOOLS = [
|
||
{'name': 'get_state', 'description': '截图 + OCR, 返回游戏窗口内全部文字(含坐标/置信度)。决策前先调这个, 不要猜坐标',
|
||
'inputSchema': _obj()},
|
||
{'name': 'click_text', 'description': '按文字点击: OCR 定位后点中心。子串匹配、忽略空格, 完全相同的优先; index 选第几个命中',
|
||
'inputSchema': _obj({'text': _STR, 'index': {'type': 'number', 'default': 0}}, ['text'])},
|
||
{'name': 'wait_for_text', 'description': '轮询 OCR 直到文字出现', 'inputSchema': _obj({'text': _STR, 'timeout': _TIMEOUT}, ['text'])},
|
||
{'name': 'wait_for_text_gone', 'description': '轮询 OCR 直到文字消失', 'inputSchema': _obj({'text': _STR, 'timeout': _TIMEOUT}, ['text'])},
|
||
{'name': 'find_image', 'description': '用 AALC 模板图在当前画面里找图标/按钮(比 OCR 稳, 适合没有文字的按钮)',
|
||
'inputSchema': _obj(_IMG, ['name'])},
|
||
{'name': 'click_image', 'description': '找到 AALC 模板图并点击它', 'inputSchema': _obj(_IMG, ['name'])},
|
||
{'name': 'list_images', 'description': '列出 AALC 模板图(按前缀过滤, 如 mirror/ battle/ home/)',
|
||
'inputSchema': _obj({'prefix': {'type': 'string', 'default': ''}})},
|
||
{'name': 'screenshot', 'description': '截游戏窗口, 返回 PNG 路径和尺寸', 'inputSchema': _obj()},
|
||
{'name': 'click', 'description': '按窗口截图像素坐标点击(优先用 click_text / click_image)',
|
||
'inputSchema': _obj({'x': _NUM, 'y': _NUM, 'times': {'type': 'number', 'default': 1}}, ['x', 'y'])},
|
||
{'name': 'keypress', 'description': '按一次键: a-z 0-9 enter esc space tab shift ctrl alt up down left right',
|
||
'inputSchema': _obj({'key': _STR}, ['key'])},
|
||
{'name': 'drag', 'description': '在游戏客户区保持左键按下并缓慢拖动,适用于卡包下拉;duration 是移动秒数,0.1–10,默认 1。先 get_state 核对坐标',
|
||
'inputSchema': _obj({'from_x': _NUM, 'from_y': _NUM, 'to_x': _NUM, 'to_y': _NUM,
|
||
'duration': {'type': 'number', 'default': 1.0, 'minimum': 0.1, 'maximum': 10}},
|
||
['from_x', 'from_y', 'to_x', 'to_y'])},
|
||
{'name': 'type_text', 'description': '向游戏窗口输入文本', 'inputSchema': _obj({'text': _STR}, ['text'])},
|
||
{'name': 'run_task', 'description': (
|
||
'后台运行 AALC 现成的完整任务流程(会自动启动游戏、处理战斗/掉线重试)。立即返回, 用 task_status 看进度。'
|
||
'all=按 AALC config.yaml 勾选的全部任务; daily=经验本+纽本(exp/thread 次数); reward=领奖励; '
|
||
'enkephalin=狂气换体力(times); mirror=镜牢(count 次数)。任务运行期间禁止手动点击类工具'),
|
||
'inputSchema': _obj({'task': {'type': 'string', 'enum': sorted(TASKS)},
|
||
'exp': _NUM, 'thread': _NUM, 'times': _NUM, 'count': _NUM}, ['task'])},
|
||
{'name': 'task_status', 'description': '查看任务状态(idle/running/succeeded/failed/stopped)和日志尾部',
|
||
'inputSchema': _obj({'lines': {'type': 'number', 'default': 30}})},
|
||
{'name': 'stop_task', 'description': '强制停止当前任务(杀掉任务进程)', 'inputSchema': _obj()},
|
||
]
|
||
|
||
|
||
class Server:
|
||
|
||
def __init__(self, engine, tasks, engine_error=None):
|
||
self.engine = engine
|
||
self.engine_error = engine_error
|
||
self.tasks = tasks
|
||
# 游戏只有一个: 所有工具调用串行, 防止两个客户端同时点
|
||
self.lock = threading.Lock()
|
||
|
||
# ---- JSON-RPC ----
|
||
def handle(self, msg):
|
||
if not isinstance(msg, dict):
|
||
return _error(None, -32600, 'request must be a JSON object')
|
||
method, mid = msg.get('method'), msg.get('id')
|
||
if method == 'initialize':
|
||
return _ok(mid, {'protocolVersion': '2024-11-05',
|
||
'capabilities': {'tools': {'listChanged': False}},
|
||
'serverInfo': {'name': 'lc-mcp', 'version': VERSION}})
|
||
if method == 'ping':
|
||
return _ok(mid, {})
|
||
if method == 'tools/list':
|
||
return _ok(mid, {'tools': TOOLS})
|
||
if method == 'tools/call':
|
||
params = msg.get('params') or {}
|
||
result = self.call_tool(params.get('name'), params.get('arguments') or {})
|
||
return _ok(mid, {'content': [{'type': 'text', 'text': json.dumps(result, ensure_ascii=False)}],
|
||
'isError': not result.get('ok', False)})
|
||
if mid is None:
|
||
return None # 通知 (notifications/*) 不回
|
||
return _error(mid, -32601, 'method not found: %s' % method)
|
||
|
||
def call_tool(self, name, args):
|
||
try:
|
||
if name in TASK_TOOLS: # 不抢游戏锁: wait_for_text 期间也能查任务/停任务
|
||
return self._task_tool(name, args)
|
||
with self.lock:
|
||
return self._call(name, args)
|
||
except (KeyError, TypeError, ValueError) as e:
|
||
return {'ok': False, 'error': 'bad arguments: %s: %s' % (type(e).__name__, e)}
|
||
except Exception as e:
|
||
return {'ok': False, 'error': '%s: %s' % (type(e).__name__, e)}
|
||
|
||
# ---- 工具 ----
|
||
def _task_tool(self, name, a):
|
||
if name == 'run_task':
|
||
task = a['task']
|
||
if task not in TASKS:
|
||
raise ValueError('unknown task %r' % task)
|
||
params = {k: int(a[k]) for k in TASKS[task] if a.get(k) is not None}
|
||
return self.tasks.start(task, params)
|
||
if name == 'task_status':
|
||
return self.tasks.status(a.get('lines', 30))
|
||
return self.tasks.stop()
|
||
|
||
def _call(self, name, a):
|
||
if not any(t['name'] == name for t in TOOLS):
|
||
return {'ok': False, 'error': 'unknown tool %s' % name}
|
||
if self.engine is None:
|
||
return {'ok': False, 'error': 'AALC 引擎不可用: %s' % self.engine_error}
|
||
if name in INPUT_TOOLS and self.tasks.running():
|
||
return {'ok': False, 'error': 'AALC 任务 %s 正在运行, 手动输入会打乱它; 先 stop_task' % self.tasks.info['task']}
|
||
e = self.engine
|
||
|
||
if name == 'screenshot':
|
||
return dict(e.screenshot(), ok=True)
|
||
if name == 'get_state':
|
||
shot, items = e.ocr()
|
||
return {'ok': True, 'screen': shot, 'items': items}
|
||
if name == 'click_text':
|
||
text = _text(a)
|
||
shot, items = e.ocr()
|
||
hits = find_hits(text, items)
|
||
if not hits:
|
||
return {'ok': False, 'error': 'text not found: %s' % text, 'screen': shot, 'items': items}
|
||
idx = int(a.get('index', 0))
|
||
if not 0 <= idx < len(hits):
|
||
return {'ok': False, 'error': 'index %d out of range (%d hits)' % (idx, len(hits)), 'hits': hits[:10]}
|
||
h = hits[idx]
|
||
e.click(h['x'], h['y'])
|
||
return {'ok': True, 'clicked': h, 'other_hits': len(hits) - 1}
|
||
if name in ('wait_for_text', 'wait_for_text_gone'):
|
||
return self._wait(_text(a), float(a.get('timeout', 30)), name == 'wait_for_text')
|
||
if name in ('find_image', 'click_image'):
|
||
pos = e.find_image(a['name'], float(a.get('threshold', 0.8)), a.get('model', 'normal'))
|
||
if pos is None:
|
||
return {'ok': name == 'find_image', 'found': False, 'name': a['name']}
|
||
if name == 'click_image':
|
||
e.click(*pos)
|
||
return {'ok': True, 'found': True, 'name': a['name'], 'x': pos[0], 'y': pos[1]}
|
||
if name == 'list_images':
|
||
imgs = e.list_images(a.get('prefix', ''))
|
||
return {'ok': True, 'count': len(imgs), 'images': imgs[:300], 'truncated': len(imgs) > 300}
|
||
if name == 'click':
|
||
e.click(int(a['x']), int(a['y']), max(1, int(a.get('times', 1))))
|
||
return {'ok': True, 'clicked': [int(a['x']), int(a['y'])]}
|
||
if name == 'keypress':
|
||
e.keypress(str(a['key']))
|
||
return {'ok': True, 'key': str(a['key'])}
|
||
if name == 'drag':
|
||
duration = float(a.get('duration', 1.0))
|
||
if not math.isfinite(duration) or not 0.1 <= duration <= 10:
|
||
raise ValueError('duration must be between 0.1 and 10 seconds')
|
||
points = [int(a[k]) for k in ('from_x', 'from_y', 'to_x', 'to_y')]
|
||
e.drag(*points, duration=duration)
|
||
return {'ok': True, 'from': points[:2], 'to': points[2:], 'duration': duration}
|
||
if name == 'type_text':
|
||
e.type_text(str(a['text']))
|
||
return {'ok': True, 'typed': str(a['text'])}
|
||
return {'ok': False, 'error': 'unknown tool %s' % name}
|
||
|
||
def _wait(self, text, timeout, want_present):
|
||
deadline = time.monotonic() + max(0.0, min(timeout, MAX_WAIT))
|
||
items, last_err = [], None
|
||
while True:
|
||
try:
|
||
shot, items = self.engine.ocr()
|
||
hits = find_hits(text, items)
|
||
if bool(hits) == want_present:
|
||
return {'ok': True, 'present': want_present, 'hits': hits[:5], 'screen': shot}
|
||
except Exception as ex:
|
||
last_err = '%s: %s' % (type(ex).__name__, ex)
|
||
if time.monotonic() + WAIT_INTERVAL > deadline:
|
||
break
|
||
time.sleep(WAIT_INTERVAL)
|
||
r = {'ok': False, 'error': 'timeout waiting %r (want present=%s)' % (text, want_present),
|
||
'last_items': items[:15]}
|
||
if last_err:
|
||
r['last_error'] = last_err
|
||
return r
|
||
|
||
|
||
def _text(a):
|
||
text = str(a.get('text', ''))
|
||
if not text.strip():
|
||
raise ValueError('text must be non-empty')
|
||
return text
|
||
|
||
|
||
def _ok(mid, result):
|
||
return {'jsonrpc': '2.0', 'id': mid, 'result': result}
|
||
|
||
|
||
def _error(mid, code, message):
|
||
return {'jsonrpc': '2.0', 'id': mid, 'error': {'code': code, 'message': message}}
|
||
|
||
|
||
def parse_line(line):
|
||
"""一行 -> (msg, error_response)。空行返回 (None, None)。"""
|
||
line = line.strip()
|
||
if not line:
|
||
return None, None
|
||
try:
|
||
return json.loads(line), None
|
||
except ValueError as e:
|
||
return None, _error(None, -32700, 'parse error: %s' % e)
|