# -*- coding: utf-8 -*- """补上:Actions 的初始化字段、剪贴板历史、服务端 13 个新命令的处理。""" from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\windows\lanremote") ACTIONS = ROOT / "actions.py" SERVER = ROOT / "server.py" # ---- Actions 初始化 ---- INIT_OLD = ''' self._audio_alias: str | None = None self._image_window = None self._lock = threading.RLock()''' INIT_NEW = ''' self._audio_alias: str | None = None self._image_window = None self._lock = threading.RLock() # 剪贴板历史(最近 20 条) self._clip_history: list[dict] = [] # CPU 占用要两次采样才算得出来 self._last_cpu_total = 0 self._last_cpu_idle = 0''' # ---- clip_set 记历史 ---- CLIP_OLD = ''' self.ui.ui_call(do) return f"剪贴板已更新({len(text)} 字符)"''' CLIP_NEW = ''' self.ui.ui_call(do) self.store.set("last_clip", text) self._remember_clip(text) return f"剪贴板已更新({len(text)} 字符)"''' # ---- 服务端命令处理 ---- SERVER_ANCHOR = ''' if cmd == P.CMD_SET_VOICE:''' SERVER_NEW = ''' if cmd == P.CMD_SHUTDOWN_TIMER: cancel = bool(msg.get("cancel")) try: minutes = int(msg.get("minutes") or 0) except (TypeError, ValueError): minutes = 0 note = self.actions.shutdown_timer(minutes, cancel) self._log(f"[电源] {note}") return {"ok": True, "note": note} if cmd == P.CMD_MONITOR: note = self.actions.monitor_power(bool(msg.get("on"))) self._log(f"[显示] {note}") return {"ok": True, "note": note} if cmd == P.CMD_OPEN_URL: note = self.actions.open_url(str(msg.get("url") or "")) self._log(f"[网址] {note}") return {"ok": True, "note": note} if cmd == P.CMD_QUICK: note = self.actions.quick_launch(str(msg.get("app") or "")) self._log(f"[快捷] {note}") return {"ok": True, "note": note} if cmd == P.CMD_OPEN_FOLDER: note = self.actions.open_folder(str(msg.get("folder") or "")) self._log(f"[文件夹] {note}") return {"ok": True, "note": note} if cmd == P.CMD_PROC_LIST: procs = self.actions.list_processes() return {"ok": True, "names": [f"{p['name']} ({p['mem_kb'] // 1024} MB)" for p in procs], "pids": [p["pid"] for p in procs]} if cmd == P.CMD_PROC_KILL: try: pid = int(msg.get("pid") or 0) except (TypeError, ValueError): pid = 0 note = self.actions.kill_process(pid, str(msg.get("name") or "")) self._log(f"[进程] {note}") return {"ok": True, "note": note} if cmd == P.CMD_BATTERY: info = self.actions.battery() if not info.get("present"): return {"ok": True, "note": info.get("note") or "没有电池(台式机)"} detail = f"{info['percent']}%" if info.get("charging"): detail += "(充电中)" return {"ok": True, "note": detail, **info} if cmd == P.CMD_DISK: drives = self.actions.disk() lines = [f"{d['drive']} {d['free_gb']}/{d['total_gb']} GB 可用" f"(已用 {d['used_percent']}%)" for d in drives] return {"ok": True, "note": " | ".join(lines) or "读不到磁盘", "drives": drives} if cmd == P.CMD_PERF: info = self.actions.perf() note = (f"CPU {info.get('cpu_percent')}% " f"内存 {info.get('mem_percent')}%" f"({info.get('mem_used_gb')}/{info.get('mem_total_gb')} GB)") return {"ok": True, "note": note, **info} if cmd == P.CMD_CLIP_HISTORY: items = self.actions.clip_history() return {"ok": True, "note": f"共 {len(items)} 条", "texts": [i.get("text", "") for i in items], "times": [i.get("at", "") for i in items]} if cmd == P.CMD_SET_WALLPAPER: note = self.actions.set_wallpaper(str(msg.get("path") or "")) self._log(f"[壁纸] {note}") return {"ok": True, "note": note} if cmd == P.CMD_SAY_CLIP: note = self.actions.say_clipboard() self._log(f"[朗读] 剪贴板内容") return {"ok": True, "note": note} if cmd == P.CMD_SET_VOICE:''' def main() -> int: ok = True text = ACTIONS.read_text(encoding="utf-8") for old, new, tag in ((INIT_OLD, INIT_NEW, "初始化字段"), (CLIP_OLD, CLIP_NEW, "剪贴板历史")): if old not in text: print(f" [失败] actions.py {tag} 锚点未匹配") ok = False else: text = text.replace(old, new, 1) print(f" actions.py / {tag}: OK") if ok: ACTIONS.write_text(text, encoding="utf-8") stext = SERVER.read_text(encoding="utf-8") if SERVER_ANCHOR not in stext: print(" [失败] server.py 锚点未匹配") ok = False else: stext = stext.replace(SERVER_ANCHOR, SERVER_NEW, 1) SERVER.write_text(stext, encoding="utf-8") print(" server.py / 13 个新命令: OK") print("\n语法检查:") import ast for name, path in (("actions.py", ACTIONS), ("server.py", SERVER)): try: ast.parse(path.read_text(encoding="utf-8")) print(f" {name}: OK") except SyntaxError as exc: print(f" {name}: 语法错误 {exc}") ok = False return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())