lingshu-skill 5.8.0 → 5.10.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.
@@ -75,6 +75,11 @@ _WRITE_ENDPOINTS = {
75
75
  _LAST_ACTIVITY = [time.time()]
76
76
 
77
77
 
78
+ # 正在挂着的长轮询请求数:>0 说明还有标签页连着(哪怕它被节流、一条心跳都没发)。
79
+ # 这是"默认 1 分钟自退"能安全的前提——只靠定时器心跳会被浏览器节流误杀。
80
+ _OPEN_ALIVE = [0]
81
+
82
+
78
83
  def _touch_activity() -> None:
79
84
  _LAST_ACTIVITY[0] = time.time()
80
85
 
@@ -658,6 +663,25 @@ class Handler(BaseHTTPRequestHandler):
658
663
  pass
659
664
  self._send(404, b"icon missing", "text/plain; charset=utf-8")
660
665
  return
666
+ if path == "/api/alive":
667
+ # 长轮询保活:按住最多 25 秒再返回,页面收到后立刻再发一条。
668
+ # 它不受浏览器"后台定时器节流"影响(不是 timer),所以标签页还在就一定连着。
669
+ if not self._origin_ok():
670
+ self._json({"error": "forbidden"}, 403)
671
+ return
672
+ _touch_activity()
673
+ _OPEN_ALIVE[0] += 1
674
+ try:
675
+ deadline = time.time() + 25
676
+ while time.time() < deadline:
677
+ if getattr(self.server, "_shutting_down", False):
678
+ break
679
+ time.sleep(0.5)
680
+ finally:
681
+ _OPEN_ALIVE[0] = max(0, _OPEN_ALIVE[0] - 1)
682
+ _touch_activity()
683
+ self._json({"ok": True, "app": "lingshu-panel"})
684
+ return
661
685
  if path == "/api/ping":
662
686
  # 闸门对 ping 放行,所以这里自己补一次 Host/Origin 校验(防 DNS rebinding)
663
687
  if not self._origin_ok():
@@ -2849,8 +2873,11 @@ def serve(port: int = 8765, open_browser: bool = True) -> None:
2849
2873
  return
2850
2874
  limit = idle * 60
2851
2875
  while True:
2852
- time.sleep(30)
2876
+ time.sleep(10)
2853
2877
  idle_for = time.time() - _LAST_ACTIVITY[0]
2878
+ # 还有标签页挂着长轮询 → 页面明明开着,绝不退(防定时器节流误杀)
2879
+ if _OPEN_ALIVE[0] > 0:
2880
+ continue
2854
2881
  if idle_for > limit:
2855
2882
  try:
2856
2883
  brain.log_event(
@@ -78,7 +78,7 @@ CONFIG_SCHEMA: list[dict[str, Any]] = [
78
78
  # —— 面板与宿主 ——
79
79
  {"key": "panel_idle_minutes", "group": "面板与宿主", "label": "空闲自动退出(分钟)",
80
80
  "kind": "int", "min": 0, "max": 1440, "step": 5,
81
- "hint": "关掉网页后,多少分钟没请求就自动退出服务;0=永不退出(需重启面板生效)"},
81
+ "hint": "关掉网页后,多少分钟没请求就自动退出服务(默认 1);0=永不退出(需重启面板生效)"},
82
82
  {"key": "panel_port", "group": "面板与宿主", "label": "面板端口",
83
83
  "kind": "int", "min": 1024, "max": 65535, "step": 1, "restart": True,
84
84
  "hint": "改完需要重启面板才生效"},
package/scripts/core.py CHANGED
@@ -337,9 +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
+ # 面板空闲多少分钟自动退出(0 = 永不)。页面挂着长轮询保活(不受后台定时器节流影响),
341
+ # 关掉网页后连接断开 → 到点自退。默认 1 分钟:关掉尽快回收,不占后台。
342
+ "panel_idle_minutes": 1,
343
343
  "auto_update_interval_hours": 24,
344
344
  "session_state_cache": False,
345
345
  "session_state_ttl": 300,