lingshu-skill 5.5.0 → 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -0
- package/README.md +71 -1
- package/SKILL.md +16 -1
- package/assets/panel.html +30 -0
- package/docs//350/277/233/345/261/225/344/270/216/345/276/205/345/212/236.md +15 -0
- package/package.json +1 -1
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +10 -9
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +40 -0
- package/scripts/astrbot_plugin_lingshu/engine/README.md +71 -1
- package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +16 -1
- package/scripts/astrbot_plugin_lingshu/engine/assets/panel.html +30 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +33 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/core.py +3 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/desktop_shortcut.py +7 -1
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_launcher.py +178 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_server.py +94 -3
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_settings.py +3 -0
- package/scripts/cli.py +33 -0
- package/scripts/core.py +3 -0
- package/scripts/desktop_shortcut.py +7 -1
- package/scripts/panel_launcher.py +178 -0
- package/scripts/panel_server.py +94 -3
- package/scripts/panel_settings.py +3 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""灵枢面板启动器:单实例 + 已在跑就只开浏览器 + 出错有可见提示。
|
|
3
|
+
|
|
4
|
+
解决两个真实困扰(用户反馈):
|
|
5
|
+
1. 再点一次快捷方式 → 端口被占 → 隐藏的 pythonw 里 raise SystemExit(1),
|
|
6
|
+
**用户什么都看不到**,还以为没反应;
|
|
7
|
+
2. 关了网页后服务仍在后台跑,只能去任务管理器杀。
|
|
8
|
+
|
|
9
|
+
行为:
|
|
10
|
+
· 先探测 `GET /api/ping`:
|
|
11
|
+
- 已是我们 → **只打开浏览器**,退出 0(绝不起第二个进程);
|
|
12
|
+
- 端口有响应但不是灵枢 → 弹窗说清"端口被别的程序占用了",退出 1;
|
|
13
|
+
- 没响应 → 起服务(serve 里带空闲自退)。
|
|
14
|
+
· 隐藏启动(pythonw)没有控制台,所以出错用 Windows MessageBox 弹出来——
|
|
15
|
+
用 ctypes 调 user32,**零依赖**。
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import json
|
|
20
|
+
import os
|
|
21
|
+
import sys
|
|
22
|
+
import time
|
|
23
|
+
import urllib.error
|
|
24
|
+
import urllib.request
|
|
25
|
+
import webbrowser
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
|
|
28
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
29
|
+
|
|
30
|
+
import brain # noqa: E402,F401 ← 合并命名空间
|
|
31
|
+
import core # noqa: E402
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _msgbox(text: str, title: str = "灵枢面板", error: bool = True) -> None:
|
|
35
|
+
"""隐藏进程里没有控制台,用系统弹窗把话说出去(ctypes 零依赖)。
|
|
36
|
+
|
|
37
|
+
`LINGSHU_NO_MSGBOX=1` 时只写 stderr——自动化/测试里弹窗会**阻塞等点击**,
|
|
38
|
+
把进程永久挂住。
|
|
39
|
+
"""
|
|
40
|
+
if os.environ.get("LINGSHU_NO_MSGBOX", "").strip() in ("1", "true", "yes"):
|
|
41
|
+
print(f"[{title}] {text}", file=sys.stderr)
|
|
42
|
+
return
|
|
43
|
+
try:
|
|
44
|
+
import ctypes
|
|
45
|
+
icon = 0x10 if error else 0x40 # MB_ICONERROR / MB_ICONINFORMATION
|
|
46
|
+
ctypes.windll.user32.MessageBoxW(None, str(text), str(title), icon)
|
|
47
|
+
except Exception:
|
|
48
|
+
# 非 Windows / 弹窗失败:至少写 stderr(有控制台时能看到)
|
|
49
|
+
print(f"[{title}] {text}", file=sys.stderr)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _open_browser(url: str, timeout: float = 3.0) -> None:
|
|
53
|
+
"""打开浏览器,但**绝不拖住进程**。
|
|
54
|
+
|
|
55
|
+
实测:没有默认浏览器关联时,`webbrowser.open()` 会一直阻塞——
|
|
56
|
+
启动器就永久留在进程表里(用户看到的是"点了没反应、又多了个进程")。
|
|
57
|
+
所以放 daemon 线程、最多等 `timeout` 秒,然后照常退出。
|
|
58
|
+
"""
|
|
59
|
+
import threading
|
|
60
|
+
done = threading.Event()
|
|
61
|
+
|
|
62
|
+
def _go():
|
|
63
|
+
try:
|
|
64
|
+
webbrowser.open(url)
|
|
65
|
+
except Exception:
|
|
66
|
+
pass
|
|
67
|
+
finally:
|
|
68
|
+
done.set()
|
|
69
|
+
|
|
70
|
+
threading.Thread(target=_go, name="open-browser", daemon=True).start()
|
|
71
|
+
done.wait(timeout)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _port() -> int:
|
|
75
|
+
"""与 panel_server 同口径:config.panel_port,退回 8765。"""
|
|
76
|
+
try:
|
|
77
|
+
p = int(core.load_config().get("panel_port") or 8765)
|
|
78
|
+
return p if 1 <= p <= 65535 else 8765
|
|
79
|
+
except Exception:
|
|
80
|
+
return 8765
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _ping(port: int, timeout: float = 1.5) -> dict | None:
|
|
84
|
+
"""探测本机面板。返回 {ok:True,...} 表示是灵枢;None 表示没响应。"""
|
|
85
|
+
url = f"http://127.0.0.1:{port}/api/ping"
|
|
86
|
+
try:
|
|
87
|
+
with urllib.request.urlopen(url, timeout=timeout) as r:
|
|
88
|
+
data = json.loads(r.read().decode("utf-8", "replace"))
|
|
89
|
+
if isinstance(data, dict) and data.get("app") == "lingshu-panel":
|
|
90
|
+
return data
|
|
91
|
+
return {"ok": True, "app": str(data.get("app") or "unknown")}
|
|
92
|
+
except Exception:
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _wait_for_app(port: int, tries: int = 8, gap: float = 0.35) -> dict | None:
|
|
97
|
+
"""带重试地探测:连点时另一个实例可能正在 bind,稍等就能看到它。"""
|
|
98
|
+
for _ in range(max(1, tries)):
|
|
99
|
+
info = _ping(port)
|
|
100
|
+
if info and info.get("app") == "lingshu-panel":
|
|
101
|
+
return info
|
|
102
|
+
time.sleep(gap)
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _port_occupied(port: int) -> bool:
|
|
107
|
+
"""端口有没有人在听(不关心是谁)。"""
|
|
108
|
+
import socket
|
|
109
|
+
s = socket.socket()
|
|
110
|
+
s.settimeout(0.8)
|
|
111
|
+
try:
|
|
112
|
+
s.connect(("127.0.0.1", port))
|
|
113
|
+
return True
|
|
114
|
+
except Exception:
|
|
115
|
+
return False
|
|
116
|
+
finally:
|
|
117
|
+
try:
|
|
118
|
+
s.close()
|
|
119
|
+
except Exception:
|
|
120
|
+
pass
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def main() -> int:
|
|
124
|
+
port = _port()
|
|
125
|
+
url = f"http://127.0.0.1:{port}"
|
|
126
|
+
|
|
127
|
+
info = _wait_for_app(port, tries=3, gap=0.2)
|
|
128
|
+
if info and info.get("app") == "lingshu-panel":
|
|
129
|
+
# 已经在跑:只把浏览器叫到前面来,不起第二个进程
|
|
130
|
+
_open_browser(url)
|
|
131
|
+
return 0
|
|
132
|
+
if _port_occupied(port):
|
|
133
|
+
# 竞态:连点时端口可能刚被**同伴**抢到,而我们第一轮探测太早没看到。
|
|
134
|
+
# 先多等一会儿再看——只有确认不是我们自己的面板,才报"被别的程序占用"。
|
|
135
|
+
again = _wait_for_app(port, tries=12, gap=0.3)
|
|
136
|
+
if again and again.get("app") == "lingshu-panel":
|
|
137
|
+
_open_browser(url)
|
|
138
|
+
return 0
|
|
139
|
+
_msgbox(
|
|
140
|
+
f"端口 {port} 已被别的程序占用,面板起不来。\n\n"
|
|
141
|
+
f"两种办法:\n"
|
|
142
|
+
f" 1) 关掉占用该端口的程序;\n"
|
|
143
|
+
f" 2) 用 brain.py config set panel_port 8766 换个端口。",
|
|
144
|
+
"灵枢面板 · 端口被占用")
|
|
145
|
+
return 1
|
|
146
|
+
|
|
147
|
+
# 起服务(serve 内部带空闲自退 + 打开浏览器)
|
|
148
|
+
# 竞态下 bind 失败是预期行为(另一个同伴刚抢到),让 serve 别往 stderr 吐假警报。
|
|
149
|
+
os.environ["LINGSHU_QUIET_PORT"] = "1"
|
|
150
|
+
try:
|
|
151
|
+
import panel_server
|
|
152
|
+
panel_server.serve(port, open_browser=True)
|
|
153
|
+
return 0
|
|
154
|
+
except (SystemExit, OSError) as exc:
|
|
155
|
+
# **竞态**:连点两次时两边都会 bind 失败一次。先别急着报错——
|
|
156
|
+
# 带重试再探一下,如果这会儿是我们自己的面板在服务,就静默开浏览器退出。
|
|
157
|
+
# (直接弹窗会让用户看到假的"启动失败",而且 MessageBox 会阻塞进程。)
|
|
158
|
+
again = _wait_for_app(port, tries=10, gap=0.35)
|
|
159
|
+
if again and again.get("app") == "lingshu-panel":
|
|
160
|
+
_open_browser(url)
|
|
161
|
+
return 0
|
|
162
|
+
if _port_occupied(port):
|
|
163
|
+
_msgbox(
|
|
164
|
+
f"端口 {port} 已被别的程序占用,面板起不来。\n\n"
|
|
165
|
+
f"两种办法:\n"
|
|
166
|
+
f" 1) 关掉占用该端口的程序;\n"
|
|
167
|
+
f" 2) 用 brain.py config set panel_port 8766 换个端口。",
|
|
168
|
+
"灵枢面板 · 端口被占用")
|
|
169
|
+
return 1
|
|
170
|
+
_msgbox(f"面板启动失败:\n{type(exc).__name__}: {exc}", "灵枢面板 · 启动失败")
|
|
171
|
+
return int(getattr(exc, "code", 1) or 2)
|
|
172
|
+
except Exception as exc: # noqa: BLE001
|
|
173
|
+
_msgbox(f"面板启动失败:\n{type(exc).__name__}: {exc}", "灵枢面板 · 启动失败")
|
|
174
|
+
return 2
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
if __name__ == "__main__":
|
|
178
|
+
raise SystemExit(main())
|
|
@@ -70,6 +70,24 @@ _WRITE_ENDPOINTS = {
|
|
|
70
70
|
# 本机随机 token:页面加载时注入,之后每个 API 请求都要带上。
|
|
71
71
|
# 注意:token 会随首页响应下发,本机进程 GET / 即可提取,所以它防的不是本机进程,
|
|
72
72
|
# 而是浏览器上下文里的误用(恶意网页跨站调用本机端口);跨站防护主要靠 Host/Origin 校验。
|
|
73
|
+
# 最后活动时间:任何 HTTP 请求都会刷新它;空闲监控线程据此决定是否自动退出。
|
|
74
|
+
# 用 list 包一层是为了在闭包里改值(模块级 float 改不了)。
|
|
75
|
+
_LAST_ACTIVITY = [time.time()]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _touch_activity() -> None:
|
|
79
|
+
_LAST_ACTIVITY[0] = time.time()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _idle_minutes() -> int:
|
|
83
|
+
"""空闲多少分钟自动退出;0 = 永不自动退出。坏配置当 30。"""
|
|
84
|
+
try:
|
|
85
|
+
v = int(brain.load_config().get("panel_idle_minutes", 30))
|
|
86
|
+
return v if v >= 0 else 30
|
|
87
|
+
except Exception:
|
|
88
|
+
return 30
|
|
89
|
+
|
|
90
|
+
|
|
73
91
|
TOKEN = os.environ.get("LINGSHU_PANEL_TOKEN") or secrets.token_urlsafe(24)
|
|
74
92
|
# 环境变量给了空串/空白时不能退化成空 token(否则任何请求都能过鉴权)——直接拒启
|
|
75
93
|
if not TOKEN.strip():
|
|
@@ -583,6 +601,7 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
583
601
|
|
|
584
602
|
# -------------------------------------------------- routes
|
|
585
603
|
def do_GET(self):
|
|
604
|
+
_touch_activity()
|
|
586
605
|
# 与 do_POST 对齐的兜底:任意一条数据损坏都不能让整个接口断连无响应
|
|
587
606
|
try:
|
|
588
607
|
self._route_get()
|
|
@@ -639,6 +658,14 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
639
658
|
pass
|
|
640
659
|
self._send(404, b"icon missing", "text/plain; charset=utf-8")
|
|
641
660
|
return
|
|
661
|
+
if path == "/api/ping":
|
|
662
|
+
# 启动器/页面心跳用。**故意不校验 token**:调用方在探测阶段还不知道 token,
|
|
663
|
+
# 而且它只回"活着 + 端口",不含任何记忆内容。
|
|
664
|
+
_touch_activity()
|
|
665
|
+
self._json({"ok": True, "app": "lingshu-panel",
|
|
666
|
+
"port": int(self.server.server_address[1]),
|
|
667
|
+
"pid": os.getpid()})
|
|
668
|
+
return
|
|
642
669
|
if path in ("/favicon.ico", "/apple-touch-icon.png"):
|
|
643
670
|
# 浏览器自动请求。优先返回真图标(assets/lingshu.ico,由 make_icon.py 生成),
|
|
644
671
|
# 没有才退回透明 1x1 PNG(避免控制台刷 404)。
|
|
@@ -892,6 +919,7 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
892
919
|
self._json({"error": "not found"}, 404)
|
|
893
920
|
|
|
894
921
|
def do_POST(self):
|
|
922
|
+
_touch_activity()
|
|
895
923
|
if not self._origin_ok() or not self._token_ok():
|
|
896
924
|
# 拒绝前没读请求体:keep-alive 连接里残留的 body 会被下一轮当成请求行解析,
|
|
897
925
|
# 造成连接错乱——断开连接最干净
|
|
@@ -1036,6 +1064,21 @@ def _api_data_restore(self, path, data):
|
|
|
1036
1064
|
self._json(r)
|
|
1037
1065
|
|
|
1038
1066
|
|
|
1067
|
+
def _api_shutdown(self, path, data):
|
|
1068
|
+
"""面板「退出服务」按钮调这里:优雅关掉监听,进程随即退出。
|
|
1069
|
+
|
|
1070
|
+
先返回响应再关(否则前端拿不到 ok);关的是 serve_forever() 那个循环。
|
|
1071
|
+
"""
|
|
1072
|
+
def _later():
|
|
1073
|
+
time.sleep(0.2) # 让响应先写出去
|
|
1074
|
+
try:
|
|
1075
|
+
self.server.shutdown()
|
|
1076
|
+
except Exception:
|
|
1077
|
+
pass
|
|
1078
|
+
threading.Thread(target=_later, daemon=True).start()
|
|
1079
|
+
self._json({"ok": True, "message": "面板服务正在退出"})
|
|
1080
|
+
|
|
1081
|
+
|
|
1039
1082
|
def _api_config(self, path, data):
|
|
1040
1083
|
"""改一项配置:{key, value}。只允许 panel_settings 白名单里的键,
|
|
1041
1084
|
并按 schema 校验类型/范围(配错一个键就能让注入崩掉,不能任写)。"""
|
|
@@ -2668,6 +2711,8 @@ _POST_ROUTES = {
|
|
|
2668
2711
|
# 数据页
|
|
2669
2712
|
"/api/data/backup": _api_data_backup,
|
|
2670
2713
|
"/api/data/restore": _api_data_restore,
|
|
2714
|
+
# 生命周期:面板自退(页面「退出服务」按钮)
|
|
2715
|
+
"/api/shutdown": _api_shutdown,
|
|
2671
2716
|
# 设置页
|
|
2672
2717
|
"/api/config": _api_config,
|
|
2673
2718
|
# 人格 / 心智 / 情绪
|
|
@@ -2749,6 +2794,20 @@ def _startup_update_check() -> None:
|
|
|
2749
2794
|
threading.Thread(target=_job, name="startup-update-check", daemon=True).start()
|
|
2750
2795
|
|
|
2751
2796
|
|
|
2797
|
+
class _ExclusiveServer(ThreadingHTTPServer):
|
|
2798
|
+
"""禁止端口复用。
|
|
2799
|
+
|
|
2800
|
+
Python 的 HTTPServer 默认 `allow_reuse_address = True` → 它会设 SO_REUSEADDR,
|
|
2801
|
+
而 **Windows 上 SO_REUSEADDR 的语义是"允许抢占已占用端口"**(Unix 是"允许
|
|
2802
|
+
TIME_WAIT 上重绑",完全不同)。实测后果:连点两次快捷方式 → 两个独立面板
|
|
2803
|
+
**同时监听同一端口**,请求随机落到其中一个,两个进程都常驻不退。
|
|
2804
|
+
|
|
2805
|
+
置 False 后第二次 bind 直接 EADDRINUSE,启动器据此判定"已有实例"并静默退出。
|
|
2806
|
+
"""
|
|
2807
|
+
|
|
2808
|
+
allow_reuse_address = False
|
|
2809
|
+
|
|
2810
|
+
|
|
2752
2811
|
def serve(port: int = 8765, open_browser: bool = True) -> None:
|
|
2753
2812
|
brain.ensure_home()
|
|
2754
2813
|
brain._resolve_readonly(False) # 面板也要探测只读:数据目录不可写时 POST 写接口返回 403
|
|
@@ -2757,16 +2816,48 @@ def serve(port: int = 8765, open_browser: bool = True) -> None:
|
|
|
2757
2816
|
# → 无感接入 embed 配置;模型没拉 → 后台自动 pull(整个生命周期只一次)。
|
|
2758
2817
|
_ollama_auto_probe_async()
|
|
2759
2818
|
try:
|
|
2760
|
-
httpd =
|
|
2819
|
+
httpd = _ExclusiveServer(("127.0.0.1", port), Handler)
|
|
2761
2820
|
except OSError as exc:
|
|
2762
|
-
|
|
2763
|
-
|
|
2821
|
+
# 启动器(panel_launcher)会先探测再启动,竞态下这里的"端口被占"是**预期**的
|
|
2822
|
+
# ——它拿到 EADDRINUSE 后会重试探、发现是同伴就静默退出。所以由它设
|
|
2823
|
+
# LINGSHU_QUIET_PORT=1 把这条压掉,避免往日志里写假警报。
|
|
2824
|
+
if os.environ.get("LINGSHU_QUIET_PORT", "").strip() not in ("1", "true", "yes"):
|
|
2825
|
+
print(f"[错误] 端口 {port} 被占用或不可用:{exc}", file=sys.stderr)
|
|
2826
|
+
print("换一个端口试试:python scripts/panel_server.py --port 8766",
|
|
2827
|
+
file=sys.stderr)
|
|
2764
2828
|
raise SystemExit(1) from exc
|
|
2765
2829
|
httpd.daemon_threads = True # 退出时不等挂住的请求线程,防止 Ctrl+C 后进程残留
|
|
2766
2830
|
url = f"http://127.0.0.1:{port}"
|
|
2767
2831
|
print(f"灵枢 · 记忆面板:{url}")
|
|
2768
2832
|
print(f"记忆库:{brain.HOME}")
|
|
2769
2833
|
print("Ctrl+C 停止")
|
|
2834
|
+
|
|
2835
|
+
# 空闲自退(借 Jupyter shutdown_no_activity_timeout 的思路):
|
|
2836
|
+
# 页面每 30 秒发一次 /api/ping 保活;用户关掉网页后心跳停,
|
|
2837
|
+
# 超过 panel_idle_minutes(默认 30,0=永不退)没任何请求就优雅退出——
|
|
2838
|
+
# 这样"关了网页服务还在后台跑"的问题就不存在了。
|
|
2839
|
+
def _idle_watch():
|
|
2840
|
+
idle = _idle_minutes()
|
|
2841
|
+
if idle <= 0:
|
|
2842
|
+
return
|
|
2843
|
+
limit = idle * 60
|
|
2844
|
+
while True:
|
|
2845
|
+
time.sleep(30)
|
|
2846
|
+
idle_for = time.time() - _LAST_ACTIVITY[0]
|
|
2847
|
+
if idle_for > limit:
|
|
2848
|
+
try:
|
|
2849
|
+
brain.log_event(
|
|
2850
|
+
"panel", f"面板空闲 {int(idle_for // 60)} 分钟(>{idle}),自动退出")
|
|
2851
|
+
except Exception:
|
|
2852
|
+
pass
|
|
2853
|
+
print(f"\n空闲 {int(idle_for // 60)} 分钟,自动退出")
|
|
2854
|
+
try:
|
|
2855
|
+
httpd.shutdown()
|
|
2856
|
+
except Exception:
|
|
2857
|
+
pass
|
|
2858
|
+
return
|
|
2859
|
+
|
|
2860
|
+
threading.Thread(target=_idle_watch, name="panel-idle", daemon=True).start()
|
|
2770
2861
|
if open_browser:
|
|
2771
2862
|
threading.Timer(0.8, lambda: webbrowser.open(url)).start()
|
|
2772
2863
|
try:
|
|
@@ -76,6 +76,9 @@ CONFIG_SCHEMA: list[dict[str, Any]] = [
|
|
|
76
76
|
"hint": "送进 rerank 的候选条数,越大越准也越慢"},
|
|
77
77
|
|
|
78
78
|
# —— 面板与宿主 ——
|
|
79
|
+
{"key": "panel_idle_minutes", "group": "面板与宿主", "label": "空闲自动退出(分钟)",
|
|
80
|
+
"kind": "int", "min": 0, "max": 1440, "step": 5,
|
|
81
|
+
"hint": "关掉网页后,多少分钟没请求就自动退出服务;0=永不退出(需重启面板生效)"},
|
|
79
82
|
{"key": "panel_port", "group": "面板与宿主", "label": "面板端口",
|
|
80
83
|
"kind": "int", "min": 1024, "max": 65535, "step": 1, "restart": True,
|
|
81
84
|
"hint": "改完需要重启面板才生效"},
|
package/scripts/cli.py
CHANGED
|
@@ -4100,6 +4100,33 @@ def _main(argv=None) -> int:
|
|
|
4100
4100
|
return ("runtime-liveness", "WARN", ";".join(parts))
|
|
4101
4101
|
return ("runtime-liveness", "PASS", ";".join(parts))
|
|
4102
4102
|
|
|
4103
|
+
def _doctor_desktop_shortcut() -> tuple:
|
|
4104
|
+
"""桌面「灵枢面板」快捷方式还在不在。
|
|
4105
|
+
|
|
4106
|
+
用户可能不小心删了/改名了/换了桌面目录。丢了不是错误,但**必须能被发现**——
|
|
4107
|
+
否则用户只会觉得"面板打不开了",而 AI 也不知道从哪修。
|
|
4108
|
+
"""
|
|
4109
|
+
try:
|
|
4110
|
+
import desktop_shortcut as _ds
|
|
4111
|
+
except Exception as exc: # noqa: BLE001
|
|
4112
|
+
return ("desktop-shortcut", "WARN", f"检查失败:{exc}")
|
|
4113
|
+
try:
|
|
4114
|
+
exists = _ds.shortcut_exists()
|
|
4115
|
+
except Exception as exc: # noqa: BLE001
|
|
4116
|
+
return ("desktop-shortcut", "WARN", f"检查失败:{exc}")
|
|
4117
|
+
if exists:
|
|
4118
|
+
return ("desktop-shortcut", "PASS", "桌面「灵枢面板」快捷方式在位")
|
|
4119
|
+
try:
|
|
4120
|
+
enabled = bool(core.load_config().get("desktop_shortcut", True))
|
|
4121
|
+
except Exception:
|
|
4122
|
+
enabled = True
|
|
4123
|
+
if not enabled:
|
|
4124
|
+
return ("desktop-shortcut", "PASS",
|
|
4125
|
+
"快捷方式不在,但配置已关闭自动创建(desktop_shortcut=false)")
|
|
4126
|
+
return ("desktop-shortcut", "WARN",
|
|
4127
|
+
"桌面「灵枢面板」快捷方式不见了(被删/改名/换了桌面目录)。"
|
|
4128
|
+
"恢复:python scripts/brain.py shortcut --ensure")
|
|
4129
|
+
|
|
4103
4130
|
def _doctor_failures() -> tuple:
|
|
4104
4131
|
"""v3.56:被吞异常的可数账(failures.jsonl → 按类聚合,近七天)。
|
|
4105
4132
|
|
|
@@ -4333,6 +4360,12 @@ def _main(argv=None) -> int:
|
|
|
4333
4360
|
except Exception as e:
|
|
4334
4361
|
checks.append(("runtime-liveness", "WARN", f"自检失败:{e}"))
|
|
4335
4362
|
|
|
4363
|
+
# 14. 桌面快捷方式是否还在(用户可能不小心删了)
|
|
4364
|
+
try:
|
|
4365
|
+
checks.append(_doctor_desktop_shortcut())
|
|
4366
|
+
except Exception as e:
|
|
4367
|
+
checks.append(("desktop-shortcut", "WARN", f"自检失败:{e}"))
|
|
4368
|
+
|
|
4336
4369
|
fails = sum(1 for _, s, _ in checks if s == "FAIL")
|
|
4337
4370
|
warns = sum(1 for _, s, _ in checks if s == "WARN")
|
|
4338
4371
|
if args.json:
|
package/scripts/core.py
CHANGED
|
@@ -337,6 +337,9 @@ DEFAULT_CONFIG = {
|
|
|
337
337
|
"safety_snapshots": True, # 关掉就没有快照/回滚保护(不建议)
|
|
338
338
|
"safety_verify": True, # 写后校验(JSON 可解析 + 层级自洽)
|
|
339
339
|
"safety_keep": 20, # 保留最近多少份快照;更旧的移入 .safety/.pruned/(不删)
|
|
340
|
+
# 面板空闲多少分钟自动退出(0 = 永不)。页面每 30 秒发心跳保活,
|
|
341
|
+
# 关掉网页后心跳停 → 到点自退,避免"关了网页服务还在后台跑"。
|
|
342
|
+
"panel_idle_minutes": 30,
|
|
340
343
|
"auto_update_interval_hours": 24,
|
|
341
344
|
"session_state_cache": False,
|
|
342
345
|
"session_state_ttl": 300,
|
|
@@ -51,8 +51,14 @@ def _desktop_dir() -> Path:
|
|
|
51
51
|
|
|
52
52
|
|
|
53
53
|
def _panel_script() -> Path:
|
|
54
|
+
"""启动器(不是 panel_server 本人):它先探测单实例,已在跑就只开浏览器。
|
|
55
|
+
|
|
56
|
+
直接指 panel_server 的话,重复点击会因端口占用在隐藏窗口里 SystemExit(1),
|
|
57
|
+
用户什么都看不到。
|
|
58
|
+
"""
|
|
54
59
|
root = Path(__file__).resolve().parent
|
|
55
|
-
|
|
60
|
+
launcher = root / "panel_launcher.py"
|
|
61
|
+
return launcher if launcher.is_file() else root / "panel_server.py"
|
|
56
62
|
|
|
57
63
|
|
|
58
64
|
def _icon_path() -> Path | None:
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""灵枢面板启动器:单实例 + 已在跑就只开浏览器 + 出错有可见提示。
|
|
3
|
+
|
|
4
|
+
解决两个真实困扰(用户反馈):
|
|
5
|
+
1. 再点一次快捷方式 → 端口被占 → 隐藏的 pythonw 里 raise SystemExit(1),
|
|
6
|
+
**用户什么都看不到**,还以为没反应;
|
|
7
|
+
2. 关了网页后服务仍在后台跑,只能去任务管理器杀。
|
|
8
|
+
|
|
9
|
+
行为:
|
|
10
|
+
· 先探测 `GET /api/ping`:
|
|
11
|
+
- 已是我们 → **只打开浏览器**,退出 0(绝不起第二个进程);
|
|
12
|
+
- 端口有响应但不是灵枢 → 弹窗说清"端口被别的程序占用了",退出 1;
|
|
13
|
+
- 没响应 → 起服务(serve 里带空闲自退)。
|
|
14
|
+
· 隐藏启动(pythonw)没有控制台,所以出错用 Windows MessageBox 弹出来——
|
|
15
|
+
用 ctypes 调 user32,**零依赖**。
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import json
|
|
20
|
+
import os
|
|
21
|
+
import sys
|
|
22
|
+
import time
|
|
23
|
+
import urllib.error
|
|
24
|
+
import urllib.request
|
|
25
|
+
import webbrowser
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
|
|
28
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
29
|
+
|
|
30
|
+
import brain # noqa: E402,F401 ← 合并命名空间
|
|
31
|
+
import core # noqa: E402
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _msgbox(text: str, title: str = "灵枢面板", error: bool = True) -> None:
|
|
35
|
+
"""隐藏进程里没有控制台,用系统弹窗把话说出去(ctypes 零依赖)。
|
|
36
|
+
|
|
37
|
+
`LINGSHU_NO_MSGBOX=1` 时只写 stderr——自动化/测试里弹窗会**阻塞等点击**,
|
|
38
|
+
把进程永久挂住。
|
|
39
|
+
"""
|
|
40
|
+
if os.environ.get("LINGSHU_NO_MSGBOX", "").strip() in ("1", "true", "yes"):
|
|
41
|
+
print(f"[{title}] {text}", file=sys.stderr)
|
|
42
|
+
return
|
|
43
|
+
try:
|
|
44
|
+
import ctypes
|
|
45
|
+
icon = 0x10 if error else 0x40 # MB_ICONERROR / MB_ICONINFORMATION
|
|
46
|
+
ctypes.windll.user32.MessageBoxW(None, str(text), str(title), icon)
|
|
47
|
+
except Exception:
|
|
48
|
+
# 非 Windows / 弹窗失败:至少写 stderr(有控制台时能看到)
|
|
49
|
+
print(f"[{title}] {text}", file=sys.stderr)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _open_browser(url: str, timeout: float = 3.0) -> None:
|
|
53
|
+
"""打开浏览器,但**绝不拖住进程**。
|
|
54
|
+
|
|
55
|
+
实测:没有默认浏览器关联时,`webbrowser.open()` 会一直阻塞——
|
|
56
|
+
启动器就永久留在进程表里(用户看到的是"点了没反应、又多了个进程")。
|
|
57
|
+
所以放 daemon 线程、最多等 `timeout` 秒,然后照常退出。
|
|
58
|
+
"""
|
|
59
|
+
import threading
|
|
60
|
+
done = threading.Event()
|
|
61
|
+
|
|
62
|
+
def _go():
|
|
63
|
+
try:
|
|
64
|
+
webbrowser.open(url)
|
|
65
|
+
except Exception:
|
|
66
|
+
pass
|
|
67
|
+
finally:
|
|
68
|
+
done.set()
|
|
69
|
+
|
|
70
|
+
threading.Thread(target=_go, name="open-browser", daemon=True).start()
|
|
71
|
+
done.wait(timeout)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _port() -> int:
|
|
75
|
+
"""与 panel_server 同口径:config.panel_port,退回 8765。"""
|
|
76
|
+
try:
|
|
77
|
+
p = int(core.load_config().get("panel_port") or 8765)
|
|
78
|
+
return p if 1 <= p <= 65535 else 8765
|
|
79
|
+
except Exception:
|
|
80
|
+
return 8765
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _ping(port: int, timeout: float = 1.5) -> dict | None:
|
|
84
|
+
"""探测本机面板。返回 {ok:True,...} 表示是灵枢;None 表示没响应。"""
|
|
85
|
+
url = f"http://127.0.0.1:{port}/api/ping"
|
|
86
|
+
try:
|
|
87
|
+
with urllib.request.urlopen(url, timeout=timeout) as r:
|
|
88
|
+
data = json.loads(r.read().decode("utf-8", "replace"))
|
|
89
|
+
if isinstance(data, dict) and data.get("app") == "lingshu-panel":
|
|
90
|
+
return data
|
|
91
|
+
return {"ok": True, "app": str(data.get("app") or "unknown")}
|
|
92
|
+
except Exception:
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _wait_for_app(port: int, tries: int = 8, gap: float = 0.35) -> dict | None:
|
|
97
|
+
"""带重试地探测:连点时另一个实例可能正在 bind,稍等就能看到它。"""
|
|
98
|
+
for _ in range(max(1, tries)):
|
|
99
|
+
info = _ping(port)
|
|
100
|
+
if info and info.get("app") == "lingshu-panel":
|
|
101
|
+
return info
|
|
102
|
+
time.sleep(gap)
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _port_occupied(port: int) -> bool:
|
|
107
|
+
"""端口有没有人在听(不关心是谁)。"""
|
|
108
|
+
import socket
|
|
109
|
+
s = socket.socket()
|
|
110
|
+
s.settimeout(0.8)
|
|
111
|
+
try:
|
|
112
|
+
s.connect(("127.0.0.1", port))
|
|
113
|
+
return True
|
|
114
|
+
except Exception:
|
|
115
|
+
return False
|
|
116
|
+
finally:
|
|
117
|
+
try:
|
|
118
|
+
s.close()
|
|
119
|
+
except Exception:
|
|
120
|
+
pass
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def main() -> int:
|
|
124
|
+
port = _port()
|
|
125
|
+
url = f"http://127.0.0.1:{port}"
|
|
126
|
+
|
|
127
|
+
info = _wait_for_app(port, tries=3, gap=0.2)
|
|
128
|
+
if info and info.get("app") == "lingshu-panel":
|
|
129
|
+
# 已经在跑:只把浏览器叫到前面来,不起第二个进程
|
|
130
|
+
_open_browser(url)
|
|
131
|
+
return 0
|
|
132
|
+
if _port_occupied(port):
|
|
133
|
+
# 竞态:连点时端口可能刚被**同伴**抢到,而我们第一轮探测太早没看到。
|
|
134
|
+
# 先多等一会儿再看——只有确认不是我们自己的面板,才报"被别的程序占用"。
|
|
135
|
+
again = _wait_for_app(port, tries=12, gap=0.3)
|
|
136
|
+
if again and again.get("app") == "lingshu-panel":
|
|
137
|
+
_open_browser(url)
|
|
138
|
+
return 0
|
|
139
|
+
_msgbox(
|
|
140
|
+
f"端口 {port} 已被别的程序占用,面板起不来。\n\n"
|
|
141
|
+
f"两种办法:\n"
|
|
142
|
+
f" 1) 关掉占用该端口的程序;\n"
|
|
143
|
+
f" 2) 用 brain.py config set panel_port 8766 换个端口。",
|
|
144
|
+
"灵枢面板 · 端口被占用")
|
|
145
|
+
return 1
|
|
146
|
+
|
|
147
|
+
# 起服务(serve 内部带空闲自退 + 打开浏览器)
|
|
148
|
+
# 竞态下 bind 失败是预期行为(另一个同伴刚抢到),让 serve 别往 stderr 吐假警报。
|
|
149
|
+
os.environ["LINGSHU_QUIET_PORT"] = "1"
|
|
150
|
+
try:
|
|
151
|
+
import panel_server
|
|
152
|
+
panel_server.serve(port, open_browser=True)
|
|
153
|
+
return 0
|
|
154
|
+
except (SystemExit, OSError) as exc:
|
|
155
|
+
# **竞态**:连点两次时两边都会 bind 失败一次。先别急着报错——
|
|
156
|
+
# 带重试再探一下,如果这会儿是我们自己的面板在服务,就静默开浏览器退出。
|
|
157
|
+
# (直接弹窗会让用户看到假的"启动失败",而且 MessageBox 会阻塞进程。)
|
|
158
|
+
again = _wait_for_app(port, tries=10, gap=0.35)
|
|
159
|
+
if again and again.get("app") == "lingshu-panel":
|
|
160
|
+
_open_browser(url)
|
|
161
|
+
return 0
|
|
162
|
+
if _port_occupied(port):
|
|
163
|
+
_msgbox(
|
|
164
|
+
f"端口 {port} 已被别的程序占用,面板起不来。\n\n"
|
|
165
|
+
f"两种办法:\n"
|
|
166
|
+
f" 1) 关掉占用该端口的程序;\n"
|
|
167
|
+
f" 2) 用 brain.py config set panel_port 8766 换个端口。",
|
|
168
|
+
"灵枢面板 · 端口被占用")
|
|
169
|
+
return 1
|
|
170
|
+
_msgbox(f"面板启动失败:\n{type(exc).__name__}: {exc}", "灵枢面板 · 启动失败")
|
|
171
|
+
return int(getattr(exc, "code", 1) or 2)
|
|
172
|
+
except Exception as exc: # noqa: BLE001
|
|
173
|
+
_msgbox(f"面板启动失败:\n{type(exc).__name__}: {exc}", "灵枢面板 · 启动失败")
|
|
174
|
+
return 2
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
if __name__ == "__main__":
|
|
178
|
+
raise SystemExit(main())
|