Fix background MCP input and add mirror dungeon feedback records
This commit is contained in:
@@ -0,0 +1,367 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""Closed-loop Limbus Company Mirror Dungeon driver.
|
||||||
|
|
||||||
|
IO goes through the user's lc-mcp (AALC) bridge: OCR state + background
|
||||||
|
click/keypress/drag. Decisions come from deepseek-v4.1-flash on the zen API.
|
||||||
|
|
||||||
|
Usage: python lc_agent.py <config.json>
|
||||||
|
|
||||||
|
config.json:
|
||||||
|
{
|
||||||
|
"steps": 200,
|
||||||
|
"model": "deepseek-v4.1-flash",
|
||||||
|
"apiKey": "oc_sk_...",
|
||||||
|
"baseUrl": "https://opencode.ai/zen/go/v1",
|
||||||
|
"logFile": "D:\\codex\\limbus_tools\\lc_agent_log.jsonl",
|
||||||
|
"shotDir": "D:\\codex\\limbus_tools\\lc_shots"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
import base64
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import urllib.request
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(r"D:\codex\lc-mcp")
|
||||||
|
BRIDGE = ROOT / "lc_mcp" / "bridge.py"
|
||||||
|
STOP_FILE = Path(r"{source_root}\STOP")
|
||||||
|
|
||||||
|
SKY = {
|
||||||
|
"tool": "click|click_text|keypress|drag|wait_for_text|screenshot|get_state",
|
||||||
|
}
|
||||||
|
|
||||||
|
TOOLS_DOC = """可用工具(只输出一个 JSON):
|
||||||
|
{"tool":"battle","args":{"rounds":8}} // 战斗宏:自动 p → enter 循环 rounds 轮,战斗中优先用这个
|
||||||
|
{"tool":"keys","args":{"keys":["enter","enter"],"delay":0.8}} // 顺序按几个键
|
||||||
|
{"tool":"keypress","args":{"key":"enter"}} // 单键: enter/esc/space/p/a-z/up/down/left/right
|
||||||
|
{"tool":"click_text","args":{"text":"进入"}} // 按 OCR 文字点击(有文字时最稳)
|
||||||
|
{"tool":"click_image","args":{"name":"mirror/road_in_mir/event.png"}} // 按 AALC 模板图点击(没文字的图标/按钮/地图节点用这个,最稳)
|
||||||
|
{"tool":"find_image","args":{"name":"mirror/road_in_mir/shop.png"}} // 只查找模板图,返回 found/x/y
|
||||||
|
{"tool":"list_images","args":{"prefix":"mirror/"}} // 列出可选模板图
|
||||||
|
{"tool":"click","args":{"x":960,"y":540}} // 按客户区坐标点击(1920x1080),最后手段
|
||||||
|
{"tool":"drag","args":{"from_x":600,"from_y":330,"to_x":600,"to_y":640,"duration":1.2}} // 长按拖拽,卡包选择用
|
||||||
|
{"tool":"wait_for_text","args":{"text":"SKIP","timeout":20}}
|
||||||
|
{"tool":"get_state"} // 重新观察
|
||||||
|
{"tool":"done","args":{"reason":"..."}} // 整局通关结算出现,或确实需要人类接管
|
||||||
|
|
||||||
|
常用模板图(AALC 自带):
|
||||||
|
mirror/road_in_mir/event.png(事件节点) shop.png(商店) battle.png(普通战斗) risky_encounter.png(危险战斗)
|
||||||
|
mirror/road_in_mir/enter_assets.png(进入按钮) up.png / down.png / mid.png(地图上下选节点)
|
||||||
|
mirror/road_in_mir/acquire_ego_gift_refuse_assets.png(观测「拒绝饰品」) refuse_gift_assets.png
|
||||||
|
mirror/road_in_mir/select_encounter_reward_card_assets.png mirror/get_reward_card/get_reward_card_confirm_assets.png
|
||||||
|
mirror/road_in_mir/clear_floor.png mirror/road_in_mir/to_window_assets.png
|
||||||
|
"""
|
||||||
|
|
||||||
|
SYSTEM = f"""你是《边狱公司》(Limbus Company) 镜像迷宫速通的**执行器**。你看到的是游戏窗口的 OCR 文字清单 + 截图,要输出**下一步一个操作**,追求整局最快通关。
|
||||||
|
|
||||||
|
{TOOLS_DOC}
|
||||||
|
|
||||||
|
决策规则(按优先级):
|
||||||
|
0. **工具选择**:有图标没文字时优先 click_image(模板图);有文字时用 click_text;click 坐标是最后手段。
|
||||||
|
1. **战斗中**(OCR 出现 WAVE / TURN / 胜率 / 伤害 / MAX):立刻用 {{"tool":"battle","args":{{"rounds":8}}}}。不要按 space(会暂停)。战斗动画中也可以继续发,游戏会排队。
|
||||||
|
2. **剧情/对话**(出现 SKIP 按钮):用 click_text 点 "SKIP" 跳过整段。只有多段文本才用 enter。
|
||||||
|
3. **判定事件**("应该让谁上前"/"选择一位罪人来进行判定"):点带「极高」标记的出战罪人卡(底部一排,卡上有 极高/高/低)。用 click 点那张卡的中下部。
|
||||||
|
4. **事件选项**:避开额外战斗和判定;优先能直接结束的选项。选完出现 SKIP 就 skip。
|
||||||
|
5. **奖励界面**:优先含「破裂」的 E.G.O 饰品;避开「体力上限」「防御等级」增加的苦难;有「选择 N/M」按钮时选完再点它。
|
||||||
|
5b. **额外E.G.O饰品观测**(画面中央单张饰品卡 + 右下「拒绝饰品」/「选择」两个按钮):**用 click_image("mirror/road_in_mir/acquire_ego_gift_refuse_assets.png")** 或 click_text "拒绝饰品"。用户明确要求:不要额外观测饰品。
|
||||||
|
5c. 如果「选择」按钮点了没用,说明还没选中卡片或该按钮不可用 —— 改点「拒绝饰品」或按 esc,不要在同一个按钮上反复点。
|
||||||
|
6. **卡包选择**(标题「选择N层主题卡包」):必须拖拽。用 drag 从目标卡包中心向下拖(如 from(600,330) to(600,640), duration 1.2)。选哪个包都行,速度优先。
|
||||||
|
7. **地图**:先用 find_image 找 mirror/road_in_mir/event.png(事件)或 shop.png(商店),找到就 click_image 点它,减少战斗;找不到事件/商店就点 battle.png / risky_encounter.png。节点详情面板出现后用 click_text 点 "进入"。
|
||||||
|
8. **商店**:先看有没有我方人格(辛克莱/鸿璐/希斯克利夫/奥提斯/格里高尔/良秀/浮士德)的「替换XX的技能」,有就买;成捆的符咒还没 ++ 就先「强化饰品」。然后点 "离开" → "确认"。
|
||||||
|
9. **任何弹窗**:优先 click_text 点 "确认"/"继续"/"进入"。
|
||||||
|
10. 连续 3 步画面没变说明方法错了,换一个明显不同的操作(换工具或换坐标)。同一个动作别重复超过 2 次。
|
||||||
|
11. 只输出 JSON,不要解释。
|
||||||
|
|
||||||
|
已知:HARD 难度;队伍是破裂纯 P;关键饰品 成捆的符咒(要 ++)、干巴柴紧致鸡胸肉、荆棘索套、荧光灯、标准负载电池。
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class Bridge:
|
||||||
|
def __init__(self):
|
||||||
|
self.proc = subprocess.Popen(
|
||||||
|
[sys.executable if False else r"D:\codex\lc-mcp\vendor\aalc\.venv\Scripts\python.exe",
|
||||||
|
"-X", "utf8", str(BRIDGE)],
|
||||||
|
cwd=str(ROOT), env=dict(os.environ, PYTHONUTF8="1"),
|
||||||
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
||||||
|
text=True, encoding="utf-8", bufsize=1,
|
||||||
|
)
|
||||||
|
self.n = 0
|
||||||
|
self.send({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {
|
||||||
|
"protocolVersion": "2024-11-05", "capabilities": {},
|
||||||
|
"clientInfo": {"name": "dsh-lc-agent", "version": "1"}}})
|
||||||
|
self.await_id(1)
|
||||||
|
self.send({"jsonrpc": "2.0", "method": "notifications/initialized"})
|
||||||
|
|
||||||
|
def send(self, obj):
|
||||||
|
self.proc.stdin.write(json.dumps(obj, ensure_ascii=False) + "\n")
|
||||||
|
self.proc.stdin.flush()
|
||||||
|
|
||||||
|
def await_id(self, mid):
|
||||||
|
while True:
|
||||||
|
line = self.proc.stdout.readline()
|
||||||
|
if not line:
|
||||||
|
raise RuntimeError("bridge closed")
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
msg = json.loads(line)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
if msg.get("id") == mid:
|
||||||
|
return msg
|
||||||
|
|
||||||
|
def call(self, tool, args=None, timeout=None):
|
||||||
|
self.n += 1
|
||||||
|
mid = 1000 + self.n
|
||||||
|
self.send({"jsonrpc": "2.0", "id": mid, "method": "tools/call",
|
||||||
|
"params": {"name": tool, "arguments": args or {}}})
|
||||||
|
msg = self.await_id(mid)
|
||||||
|
if msg is None:
|
||||||
|
return {"ok": False, "error": "no response"}
|
||||||
|
if "error" in msg:
|
||||||
|
return {"ok": False, "error": msg["error"]}
|
||||||
|
res = msg["result"]
|
||||||
|
try:
|
||||||
|
return json.loads(res["content"][0]["text"])
|
||||||
|
except Exception:
|
||||||
|
return {"ok": False, "raw": res}
|
||||||
|
|
||||||
|
|
||||||
|
def shrink(path, out_dir, max_w=1280):
|
||||||
|
"""Downscale the AALC PNG to a small JPEG data URL for the vision model."""
|
||||||
|
try:
|
||||||
|
from PIL import Image
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
im = Image.open(path).convert("RGB")
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
if im.width > max_w:
|
||||||
|
im = im.resize((max_w, int(im.height * max_w / im.width)), Image.LANCZOS)
|
||||||
|
os.makedirs(out_dir, exist_ok=True)
|
||||||
|
out = os.path.join(out_dir, "model_view.jpg")
|
||||||
|
im.save(out, "JPEG", quality=72)
|
||||||
|
with open(out, "rb") as f:
|
||||||
|
return "data:image/jpeg;base64," + base64.b64encode(f.read()).decode("ascii")
|
||||||
|
|
||||||
|
|
||||||
|
def summarize(items, limit=90):
|
||||||
|
lines = []
|
||||||
|
for it in items[:limit]:
|
||||||
|
lines.append(f'{it["text"]} @({it["x"]},{it["y"]})')
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def ask(cfg, image_url, ocr_text, recent, step, stuck):
|
||||||
|
nudge = ""
|
||||||
|
if stuck:
|
||||||
|
nudge = ("\n\n⚠️ 最近几步画面完全没变化,说明当前操作方式无效:"
|
||||||
|
"立刻换一个**完全不同**的工具或目标(例如 keypress esc / click_text 拒绝饰品 / "
|
||||||
|
"click_text 确认 / click_text 进入 / drag 拖卡包),不要再点同一个按钮。\n")
|
||||||
|
body = {
|
||||||
|
"model": cfg["model"],
|
||||||
|
"temperature": 0,
|
||||||
|
"max_tokens": 900,
|
||||||
|
"reasoning_effort": "minimal",
|
||||||
|
"messages": [
|
||||||
|
{"role": "system", "content": SYSTEM},
|
||||||
|
{"role": "user", "content": [
|
||||||
|
{"type": "text", "text": (
|
||||||
|
f"第 {step} 步。\n最近操作:{' | '.join(recent[-5:]) or '无'}\n"
|
||||||
|
f"OCR 文字清单(text @x,y):\n{ocr_text}{nudge}\n\n输出下一步 JSON。")},
|
||||||
|
{"type": "image_url", "image_url": {"url": image_url, "detail": "low"}},
|
||||||
|
]},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||||||
|
req = urllib.request.Request(
|
||||||
|
cfg["baseUrl"].rstrip("/") + "/chat/completions", data=data,
|
||||||
|
headers={"Authorization": "Bearer " + cfg["apiKey"],
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"x-opencode-session": cfg.get("sessionId", "dsh-lc-agent"),
|
||||||
|
# Cloudflare in front of the zen API returns 1010 without a browser UA.
|
||||||
|
"User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
||||||
|
"(KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36")},
|
||||||
|
method="POST")
|
||||||
|
with urllib.request.urlopen(req, timeout=120) as resp:
|
||||||
|
payload = json.loads(resp.read().decode("utf-8"))
|
||||||
|
msg = (payload.get("choices") or [{}])[0].get("message") or {}
|
||||||
|
return msg.get("content") or "", msg.get("reasoning_content") or ""
|
||||||
|
|
||||||
|
|
||||||
|
def parse_action(text):
|
||||||
|
raw = text.replace("```json", "\n").replace("```", "\n")
|
||||||
|
for i, ch in enumerate(raw):
|
||||||
|
if ch != "{":
|
||||||
|
continue
|
||||||
|
depth = 0
|
||||||
|
instr = False
|
||||||
|
esc = False
|
||||||
|
for j in range(i, len(raw)):
|
||||||
|
c = raw[j]
|
||||||
|
if instr:
|
||||||
|
if esc:
|
||||||
|
esc = False
|
||||||
|
elif c == "\\":
|
||||||
|
esc = True
|
||||||
|
elif c == '"':
|
||||||
|
instr = False
|
||||||
|
continue
|
||||||
|
if c == '"':
|
||||||
|
instr = True
|
||||||
|
elif c == "{":
|
||||||
|
depth += 1
|
||||||
|
elif c == "}":
|
||||||
|
depth -= 1
|
||||||
|
if depth == 0:
|
||||||
|
try:
|
||||||
|
obj = json.loads(raw[i:j + 1])
|
||||||
|
if isinstance(obj, dict) and isinstance(obj.get("tool"), str):
|
||||||
|
return obj
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
break
|
||||||
|
raise ValueError("no action JSON")
|
||||||
|
|
||||||
|
|
||||||
|
def run_action(br, action):
|
||||||
|
tool = action["tool"]
|
||||||
|
args = action.get("args") or {}
|
||||||
|
if tool == "battle":
|
||||||
|
rounds = max(1, min(int(args.get("rounds") or 6), 20))
|
||||||
|
done = 0
|
||||||
|
for _ in range(rounds):
|
||||||
|
br.call("keypress", {"key": "p"})
|
||||||
|
time.sleep(0.6)
|
||||||
|
br.call("keypress", {"key": "enter"})
|
||||||
|
time.sleep(1.0)
|
||||||
|
done += 1
|
||||||
|
return {"did": "battle", "rounds": done}
|
||||||
|
if tool == "keys":
|
||||||
|
keys = [k for k in (args.get("keys") or []) if isinstance(k, str)][:20]
|
||||||
|
delay = float(args.get("delay") or 0.8)
|
||||||
|
for k in keys:
|
||||||
|
br.call("keypress", {"key": k})
|
||||||
|
time.sleep(delay)
|
||||||
|
return {"did": "keys", "keys": keys}
|
||||||
|
if tool == "battle_until_text":
|
||||||
|
return {"did": "noop"}
|
||||||
|
res = br.call(tool, args)
|
||||||
|
res["did"] = tool
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
cfg = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
|
||||||
|
if cfg.get('decisionEngine') == 'jev':
|
||||||
|
from jev_mirror_agent import main as jev_main
|
||||||
|
return jev_main()
|
||||||
|
log_path = Path(cfg["logFile"])
|
||||||
|
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
recent = []
|
||||||
|
br = Bridge()
|
||||||
|
step = 0
|
||||||
|
last_sig = None
|
||||||
|
stuck = 0
|
||||||
|
model_errs = 0
|
||||||
|
while step < int(cfg["steps"]):
|
||||||
|
step += 1
|
||||||
|
if STOP_FILE.exists():
|
||||||
|
print(json.dumps({"status": "stopped-by-file", "step": step}, ensure_ascii=False))
|
||||||
|
return
|
||||||
|
t0 = time.time()
|
||||||
|
state = br.call("get_state")
|
||||||
|
if not state.get("ok"):
|
||||||
|
print(json.dumps({"step": step, "status": "state-error", "state": state}, ensure_ascii=False))
|
||||||
|
time.sleep(2)
|
||||||
|
continue
|
||||||
|
items = state.get("items") or []
|
||||||
|
shot = (state.get("screen") or {}).get("path")
|
||||||
|
|
||||||
|
# Progress watchdog: if the OCR text stops changing, the current approach
|
||||||
|
# is not reaching the game. Nudge the model, then force an escape.
|
||||||
|
sig = "|".join(it.get("text", "") for it in items)
|
||||||
|
if sig == last_sig:
|
||||||
|
stuck += 1
|
||||||
|
else:
|
||||||
|
stuck = 0
|
||||||
|
last_sig = sig
|
||||||
|
if stuck >= 4:
|
||||||
|
br.call("keypress", {"key": "esc"})
|
||||||
|
time.sleep(0.8)
|
||||||
|
rec = {"step": step, "status": "watchdog-esc", "stuck": stuck, "shot": shot,
|
||||||
|
"at": time.strftime("%Y-%m-%dT%H:%M:%S")}
|
||||||
|
log_path.open("a", encoding="utf-8").write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||||||
|
print(json.dumps(rec, ensure_ascii=False))
|
||||||
|
stuck = 0
|
||||||
|
last_sig = None
|
||||||
|
continue
|
||||||
|
|
||||||
|
img = shrink(shot, cfg.get("shotDir", r"{source_root}\lc_shots")) if shot else None
|
||||||
|
if not img:
|
||||||
|
print(json.dumps({"step": step, "status": "no-image"}, ensure_ascii=False))
|
||||||
|
time.sleep(2)
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
content, reasoning = ask(cfg, img, summarize(items), recent, step, stuck >= 2)
|
||||||
|
try:
|
||||||
|
action = parse_action(content or reasoning)
|
||||||
|
except Exception:
|
||||||
|
# One stricter retry: the model sometimes answers in prose or
|
||||||
|
# spends the whole budget on reasoning.
|
||||||
|
content2, reasoning2 = ask(
|
||||||
|
cfg, img,
|
||||||
|
summarize(items) + "\n\n上一次回复没有给出 JSON。现在**只输出一行 JSON**,以 {\"tool\": 开头。",
|
||||||
|
recent, step, False)
|
||||||
|
try:
|
||||||
|
action = parse_action(content2 or reasoning2)
|
||||||
|
except Exception:
|
||||||
|
raise ValueError(f"no action JSON (raw={((content or reasoning) or '')[:200]!r})")
|
||||||
|
except Exception as e:
|
||||||
|
model_errs += 1
|
||||||
|
rec = {"step": step, "status": "model-error", "error": f"{type(e).__name__}: {e}",
|
||||||
|
"model_errs": model_errs, "shot": shot}
|
||||||
|
log_path.open("a", encoding="utf-8").write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||||||
|
print(json.dumps(rec, ensure_ascii=False))
|
||||||
|
if model_errs >= 3:
|
||||||
|
# Don't stall on a flaky model: nudge the game forward so the next
|
||||||
|
# observation has new information to work with.
|
||||||
|
model_errs = 0
|
||||||
|
try:
|
||||||
|
br.call("keypress", {"key": "enter"})
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
time.sleep(1.5)
|
||||||
|
continue
|
||||||
|
model_errs = 0
|
||||||
|
try:
|
||||||
|
result = run_action(br, action)
|
||||||
|
except Exception as e:
|
||||||
|
rec = {"step": step, "status": "exec-error", "action": action,
|
||||||
|
"error": f"{type(e).__name__}: {e}", "shot": shot}
|
||||||
|
log_path.open("a", encoding="utf-8").write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||||||
|
print(json.dumps(rec, ensure_ascii=False))
|
||||||
|
time.sleep(2)
|
||||||
|
continue
|
||||||
|
rec = {"step": step, "action": action, "result": result,
|
||||||
|
"ms": int((time.time() - t0) * 1000), "shot": shot,
|
||||||
|
"at": time.strftime("%Y-%m-%dT%H:%M:%S")}
|
||||||
|
log_path.open("a", encoding="utf-8").write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||||||
|
print(json.dumps({"step": step, "action": json.dumps(action, ensure_ascii=False),
|
||||||
|
"did": result.get("did"), "ms": rec["ms"], "shot": shot}, ensure_ascii=False))
|
||||||
|
sys.stdout.flush()
|
||||||
|
recent.append(json.dumps(action, ensure_ascii=False))
|
||||||
|
if action.get("tool") == "done":
|
||||||
|
return
|
||||||
|
time.sleep(float(cfg.get("settle", 0.4)))
|
||||||
|
print(json.dumps({"status": "step-budget-exhausted", "step": step}, ensure_ascii=False))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user