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
package/scripts/panel_server.py
CHANGED
|
@@ -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": "改完需要重启面板才生效"},
|