dsh-app-controls 0.5.1 → 0.6.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/README.md +30 -18
- package/assets/dsh-tray.png +0 -0
- package/lib/client.js +332 -8
- package/lib/index.js +246 -11
- package/lib/settings.js +21 -0
- package/lib/update.js +640 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -2
- package/tray/dsh-tray.py +432 -39
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-app-controls",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "DeepSeek Harness Web GUI controls,
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "DeepSeek Harness Web GUI controls, single-instance DeepSeek-style KDE tray icon, same-window navigation, safe update deferral with task warnings, remembered window size, and cache cleanup.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"cordis.patch.yml",
|
|
17
17
|
"locale",
|
|
18
18
|
"icon.svg",
|
|
19
|
+
"assets",
|
|
19
20
|
"tray/dsh-tray.py"
|
|
20
21
|
],
|
|
21
22
|
"icon": "./icon.svg",
|
package/tray/dsh-tray.py
CHANGED
|
@@ -17,10 +17,12 @@ import json
|
|
|
17
17
|
import os
|
|
18
18
|
import re
|
|
19
19
|
import shutil
|
|
20
|
+
import socket
|
|
20
21
|
import subprocess
|
|
21
22
|
import sys
|
|
22
23
|
import time
|
|
23
24
|
from pathlib import Path
|
|
25
|
+
from urllib.parse import urlsplit
|
|
24
26
|
|
|
25
27
|
from PyQt5.QtCore import QLineF, QObject, QPointF, QRectF, QTimer, QUrl, Qt
|
|
26
28
|
from PyQt5.QtGui import QColor, QGuiApplication, QIcon, QPainter, QPalette, QPen, QPixmap
|
|
@@ -39,6 +41,10 @@ URL = os.environ.get("DSH_WEB_URL", "http://127.0.0.1:3080/")
|
|
|
39
41
|
AUTOSTART = HOME / ".config/autostart/dsh-tray.desktop"
|
|
40
42
|
LOCK_FILE = STATE_DIR / "tray.lock"
|
|
41
43
|
ICON_SVG = PLUGIN_DIR / "icon.svg"
|
|
44
|
+
SETTINGS_FILE = STATE_DIR / "settings.json"
|
|
45
|
+
WHALE_ASSET = PLUGIN_DIR / "assets" / "dsh-tray.png"
|
|
46
|
+
TRAY_ICON_STYLES = ("deepseek", "classic", "custom")
|
|
47
|
+
DEFAULT_TRAY_ICON_STYLE = "deepseek"
|
|
42
48
|
|
|
43
49
|
STATUS_INTERVAL_MS = 2000
|
|
44
50
|
UPDATE_INTERVAL_MS = 30 * 60 * 1000
|
|
@@ -46,6 +52,8 @@ THEME_INTERVAL_MS = 3000
|
|
|
46
52
|
REQUEST_TIMEOUT_MS = 4000
|
|
47
53
|
CHECK_TIMEOUT_MS = 30000
|
|
48
54
|
START_TIMEOUT_SECONDS = 45
|
|
55
|
+
RESTART_POLL_MS = 250
|
|
56
|
+
RESTART_TIMEOUT_SECONDS = 30
|
|
49
57
|
DSH_PATCH = HOME / ".dsh/profiles/web/cordis.patch.yml"
|
|
50
58
|
|
|
51
59
|
_STATE_COLORS = {
|
|
@@ -67,6 +75,99 @@ def log(message: str) -> None:
|
|
|
67
75
|
pass
|
|
68
76
|
|
|
69
77
|
|
|
78
|
+
def read_tray_icon_settings(path: Path | None = None) -> tuple[str, Path]:
|
|
79
|
+
"""Read the tray icon choice from the shared settings file.
|
|
80
|
+
|
|
81
|
+
The settings file is owned by the host plugin, so this reader is
|
|
82
|
+
deliberately forgiving: a missing file, invalid JSON or an unknown style
|
|
83
|
+
all fall back to the DeepSeek whale. ``custom`` with a missing or
|
|
84
|
+
unreadable image also falls back to the whale, matching the Web UI.
|
|
85
|
+
"""
|
|
86
|
+
style = DEFAULT_TRAY_ICON_STYLE
|
|
87
|
+
custom = ""
|
|
88
|
+
try:
|
|
89
|
+
data = json.loads((path or SETTINGS_FILE).read_text(encoding="utf-8"))
|
|
90
|
+
except Exception:
|
|
91
|
+
return style, Path()
|
|
92
|
+
if isinstance(data, dict):
|
|
93
|
+
value = data.get("trayIcon")
|
|
94
|
+
if isinstance(value, dict):
|
|
95
|
+
candidate = value.get("style")
|
|
96
|
+
if candidate in TRAY_ICON_STYLES:
|
|
97
|
+
style = candidate
|
|
98
|
+
raw_custom = value.get("customPath")
|
|
99
|
+
if isinstance(raw_custom, str):
|
|
100
|
+
custom = raw_custom.strip()
|
|
101
|
+
if style != "custom" or not custom:
|
|
102
|
+
return (DEFAULT_TRAY_ICON_STYLE if style == "custom" else style), Path()
|
|
103
|
+
resolved = Path(os.path.expandvars(os.path.expanduser(custom)))
|
|
104
|
+
if not resolved.is_absolute():
|
|
105
|
+
resolved = HOME / resolved
|
|
106
|
+
try:
|
|
107
|
+
if not resolved.is_file() or not os.access(resolved, os.R_OK):
|
|
108
|
+
return DEFAULT_TRAY_ICON_STYLE, Path()
|
|
109
|
+
except OSError:
|
|
110
|
+
return DEFAULT_TRAY_ICON_STYLE, Path()
|
|
111
|
+
return "custom", resolved
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def read_update_defer_minutes(path: Path | None = None) -> int:
|
|
115
|
+
"""Read the shared default defer duration from settings.json."""
|
|
116
|
+
try:
|
|
117
|
+
data = json.loads((path or SETTINGS_FILE).read_text(encoding="utf-8"))
|
|
118
|
+
except Exception:
|
|
119
|
+
return 60
|
|
120
|
+
value = data.get("update") if isinstance(data, dict) else None
|
|
121
|
+
minutes = value.get("deferMinutes") if isinstance(value, dict) else None
|
|
122
|
+
try:
|
|
123
|
+
number = int(minutes)
|
|
124
|
+
except (TypeError, ValueError):
|
|
125
|
+
return 60
|
|
126
|
+
return number if 5 <= number <= 7 * 24 * 60 else 60
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def find_other_tray_processes() -> list[int]:
|
|
130
|
+
"""Return PIDs of other ``dsh-tray.py`` processes for this user.
|
|
131
|
+
|
|
132
|
+
``main()`` uses this before taking the lock so a second tray (for example
|
|
133
|
+
after an update, a manual start or a login race) exits immediately instead
|
|
134
|
+
of showing duplicate icons. Only the real script invocation counts:
|
|
135
|
+
one-shot ``python -m py_compile``/``-c`` commands and ``--check`` runs are
|
|
136
|
+
ignored, and the calling process is never reported. The scan lives here,
|
|
137
|
+
not in ``Tray.__init__``, so offscreen tests can instantiate ``Tray``.
|
|
138
|
+
"""
|
|
139
|
+
own_pid = os.getpid()
|
|
140
|
+
found: list[int] = []
|
|
141
|
+
try:
|
|
142
|
+
entries = os.listdir("/proc")
|
|
143
|
+
except OSError:
|
|
144
|
+
return found
|
|
145
|
+
for entry in entries:
|
|
146
|
+
if not entry.isdigit():
|
|
147
|
+
continue
|
|
148
|
+
pid = int(entry)
|
|
149
|
+
if pid == own_pid:
|
|
150
|
+
continue
|
|
151
|
+
try:
|
|
152
|
+
if os.stat(f"/proc/{pid}").st_uid != os.getuid():
|
|
153
|
+
continue
|
|
154
|
+
raw = Path(f"/proc/{pid}/cmdline").read_bytes()
|
|
155
|
+
except OSError:
|
|
156
|
+
continue
|
|
157
|
+
args = [part.decode("utf-8", "replace") for part in raw.split(b"\0") if part]
|
|
158
|
+
if not args:
|
|
159
|
+
continue
|
|
160
|
+
executable = Path(args[0]).name.lower()
|
|
161
|
+
if "python" not in executable:
|
|
162
|
+
continue
|
|
163
|
+
if "-m" in args or "-c" in args or "--check" in args:
|
|
164
|
+
continue
|
|
165
|
+
if any(Path(arg).name == "dsh-tray.py" for arg in args[1:]):
|
|
166
|
+
if pid not in found:
|
|
167
|
+
found.append(pid)
|
|
168
|
+
return found
|
|
169
|
+
|
|
170
|
+
|
|
70
171
|
def dsh_theme_preference() -> str:
|
|
71
172
|
"""Read the DSH Web profile's theme preference without requiring PyYAML."""
|
|
72
173
|
try:
|
|
@@ -191,10 +292,14 @@ class Tray(QSystemTrayIcon):
|
|
|
191
292
|
self.starting = False
|
|
192
293
|
self.start_deadline = 0.0
|
|
193
294
|
self.restart_pending = False
|
|
295
|
+
self.restart_waiting = False
|
|
296
|
+
self.restart_deadline = 0.0
|
|
194
297
|
self.tray_update = False
|
|
195
298
|
self.suppress_stop_notice = False
|
|
196
299
|
self.last_job_finished = None
|
|
197
300
|
self.last_notified_update = None
|
|
301
|
+
self.update_defer_until = 0.0
|
|
302
|
+
self.defer_notice_scheduled = False
|
|
198
303
|
self.requests = set()
|
|
199
304
|
self._request_id = 0
|
|
200
305
|
self.state = "stopped"
|
|
@@ -203,6 +308,11 @@ class Tray(QSystemTrayIcon):
|
|
|
203
308
|
self._theme_pref = None
|
|
204
309
|
self.check_result = None
|
|
205
310
|
self.check_result_deadline = 0.0
|
|
311
|
+
self.icon_style = DEFAULT_TRAY_ICON_STYLE
|
|
312
|
+
self.custom_icon_path = Path()
|
|
313
|
+
self._whale_source = None
|
|
314
|
+
self._icon_settings_key = None
|
|
315
|
+
self._custom_icon_fallback_logged = False
|
|
206
316
|
|
|
207
317
|
self.dark = self._resolve_dark()
|
|
208
318
|
self.menu = QMenu()
|
|
@@ -302,12 +412,15 @@ class Tray(QSystemTrayIcon):
|
|
|
302
412
|
dark = True if preference == "dark" else False if preference == "light" else base.lightness() < 128
|
|
303
413
|
key = (preference, dark, base.name())
|
|
304
414
|
if not force and key == self._theme_key:
|
|
415
|
+
self._sync_icon_settings()
|
|
305
416
|
return
|
|
306
417
|
self._theme_pref = preference
|
|
307
418
|
self.dark = dark
|
|
308
419
|
self._theme_key = key
|
|
420
|
+
self._sync_icon_settings()
|
|
309
421
|
self.menu.setStyleSheet(self._menu_style())
|
|
310
422
|
self._apply_action_icons()
|
|
423
|
+
self.setIcon(self._make_icon(self.state))
|
|
311
424
|
self._refresh_menu()
|
|
312
425
|
log(f"tray theme: {preference} ({'dark' if dark else 'light'})")
|
|
313
426
|
except Exception as error:
|
|
@@ -316,6 +429,25 @@ class Tray(QSystemTrayIcon):
|
|
|
316
429
|
def _sync_theme(self) -> None:
|
|
317
430
|
self._apply_theme()
|
|
318
431
|
|
|
432
|
+
def _sync_icon_settings(self) -> None:
|
|
433
|
+
"""Pick up trayIcon changes written by the Web settings panel."""
|
|
434
|
+
try:
|
|
435
|
+
stat = SETTINGS_FILE.stat()
|
|
436
|
+
key = (stat.st_mtime_ns, stat.st_size)
|
|
437
|
+
except OSError:
|
|
438
|
+
key = None
|
|
439
|
+
if key == self._icon_settings_key:
|
|
440
|
+
return
|
|
441
|
+
self._icon_settings_key = key
|
|
442
|
+
style, custom = read_tray_icon_settings()
|
|
443
|
+
if style == self.icon_style and custom == self.custom_icon_path:
|
|
444
|
+
return
|
|
445
|
+
self.icon_style = style
|
|
446
|
+
self.custom_icon_path = custom
|
|
447
|
+
self._custom_icon_fallback_logged = False
|
|
448
|
+
self.setIcon(self._make_icon(self.state))
|
|
449
|
+
log(f"tray icon style: {style}" + (f" ({custom})" if style == "custom" else ""))
|
|
450
|
+
|
|
319
451
|
def _menu_icon(self, kind: str, color: str | None = None) -> QIcon:
|
|
320
452
|
c = QColor(color or self._colors()["text"])
|
|
321
453
|
size = 18
|
|
@@ -478,14 +610,9 @@ class Tray(QSystemTrayIcon):
|
|
|
478
610
|
self.checking = False
|
|
479
611
|
if response.get("ok") and response.get("update"):
|
|
480
612
|
self.update = response["update"]
|
|
613
|
+
self._note_update_defer(self.update)
|
|
481
614
|
if self.update.get("updateAvailable"):
|
|
482
|
-
|
|
483
|
-
if candidate != self.last_notified_update:
|
|
484
|
-
self.last_notified_update = candidate
|
|
485
|
-
self._notify(
|
|
486
|
-
"有可用更新",
|
|
487
|
-
f"DeepSeek Harness {candidate}(当前 {self.update.get('current')})",
|
|
488
|
-
)
|
|
615
|
+
self._maybe_notify_update()
|
|
489
616
|
elif mode == "manual":
|
|
490
617
|
self._set_check_result("已是最新版本")
|
|
491
618
|
elif mode == "manual":
|
|
@@ -507,6 +634,86 @@ class Tray(QSystemTrayIcon):
|
|
|
507
634
|
self.check_result = None
|
|
508
635
|
self._refresh_menu()
|
|
509
636
|
|
|
637
|
+
def _note_update_defer(self, value) -> None:
|
|
638
|
+
if not isinstance(value, dict):
|
|
639
|
+
return
|
|
640
|
+
until = value.get("updateDeferUntil")
|
|
641
|
+
if isinstance(until, (int, float)) and until > 0:
|
|
642
|
+
self.update_defer_until = max(self.update_defer_until, float(until))
|
|
643
|
+
# A deferral set elsewhere (for example the Web dialog) should
|
|
644
|
+
# allow a fresh notification when it expires.
|
|
645
|
+
self.last_notified_update = None
|
|
646
|
+
|
|
647
|
+
def _update_deferred(self) -> bool:
|
|
648
|
+
return self.update_defer_until > time.time()
|
|
649
|
+
|
|
650
|
+
def _defer_time_text(self) -> str:
|
|
651
|
+
if not self._update_deferred():
|
|
652
|
+
return ""
|
|
653
|
+
return time.strftime("%H:%M", time.localtime(self.update_defer_until))
|
|
654
|
+
|
|
655
|
+
def _maybe_notify_update(self) -> None:
|
|
656
|
+
if not isinstance(self.update, dict) or not self.update.get("updateAvailable"):
|
|
657
|
+
return
|
|
658
|
+
# The startup check can finish before KDE has attached the tray icon;
|
|
659
|
+
# wait for the icon to be visible so the notification is not lost.
|
|
660
|
+
if not self.isVisible():
|
|
661
|
+
QTimer.singleShot(2000, self._maybe_notify_update)
|
|
662
|
+
return
|
|
663
|
+
if self._update_deferred():
|
|
664
|
+
remain = max(1.0, self.update_defer_until - time.time())
|
|
665
|
+
if not self.defer_notice_scheduled:
|
|
666
|
+
self.defer_notice_scheduled = True
|
|
667
|
+
QTimer.singleShot(int(remain * 1000) + 500, self._on_defer_expired)
|
|
668
|
+
return
|
|
669
|
+
candidate = self.update.get("candidate")
|
|
670
|
+
if candidate == self.last_notified_update:
|
|
671
|
+
return
|
|
672
|
+
self.last_notified_update = candidate
|
|
673
|
+
self._notify(
|
|
674
|
+
"有可用更新",
|
|
675
|
+
f"DeepSeek Harness {candidate}(当前 {self.update.get('current')})\n"
|
|
676
|
+
"右键托盘图标可立即更新,或先推迟更新。",
|
|
677
|
+
)
|
|
678
|
+
|
|
679
|
+
def _on_defer_expired(self) -> None:
|
|
680
|
+
self.defer_notice_scheduled = False
|
|
681
|
+
self.update_defer_until = 0.0
|
|
682
|
+
self._maybe_notify_update()
|
|
683
|
+
self._refresh_menu()
|
|
684
|
+
|
|
685
|
+
def _defer_update(self, minutes: int = 60) -> None:
|
|
686
|
+
def done(response: dict) -> None:
|
|
687
|
+
if not response.get("ok"):
|
|
688
|
+
self._notify("无法推迟更新", response.get("message") or response.get("error") or "未知错误")
|
|
689
|
+
return
|
|
690
|
+
until = response.get("updateDeferUntil")
|
|
691
|
+
if isinstance(until, (int, float)) and until > 0:
|
|
692
|
+
self.update_defer_until = float(until)
|
|
693
|
+
self.defer_notice_scheduled = False
|
|
694
|
+
self.last_notified_update = None
|
|
695
|
+
self._notify("已推迟更新", f"将在 {minutes} 分钟后再提醒你。")
|
|
696
|
+
self._refresh_menu()
|
|
697
|
+
|
|
698
|
+
self._request({"command": "defer-update", "minutes": minutes}, done, timeout_ms=CHECK_TIMEOUT_MS)
|
|
699
|
+
|
|
700
|
+
def _activity_text(self) -> str:
|
|
701
|
+
update = self.update if isinstance(self.update, dict) else {}
|
|
702
|
+
activity = update.get("activity")
|
|
703
|
+
if not isinstance(activity, dict) or activity.get("available") is False:
|
|
704
|
+
return "暂时无法确认是否有任务在运行,更新前请留意。"
|
|
705
|
+
if not activity.get("active"):
|
|
706
|
+
return "当前没有检测到正在运行的任务。"
|
|
707
|
+
parts = []
|
|
708
|
+
if isinstance(activity.get("turns"), int) and activity["turns"] > 0:
|
|
709
|
+
parts.append(f"{activity['turns']} 个进行中的对话")
|
|
710
|
+
if isinstance(activity.get("jobs"), int) and activity["jobs"] > 0:
|
|
711
|
+
parts.append(f"{activity['jobs']} 个后台任务")
|
|
712
|
+
count = activity.get("count")
|
|
713
|
+
if not isinstance(count, int) or count <= 0:
|
|
714
|
+
count = len(activity.get("items") or [])
|
|
715
|
+
return f"检测到 {count} 个正在运行的任务({'、'.join(parts)})。更新会重启本地服务,这些任务会被中断。"
|
|
716
|
+
|
|
510
717
|
# ── status handling ─────────────────────────────────────────────────────
|
|
511
718
|
def _on_status(self, response: dict) -> None:
|
|
512
719
|
self.polling = False
|
|
@@ -520,6 +727,10 @@ class Tray(QSystemTrayIcon):
|
|
|
520
727
|
self.job = response.get("job") if isinstance(response.get("job"), dict) else None
|
|
521
728
|
if isinstance(response.get("update"), dict):
|
|
522
729
|
self.update = response["update"]
|
|
730
|
+
self._note_update_defer(self.update)
|
|
731
|
+
self.update_defer_until = float(response.get("updateDeferUntil") or 0)
|
|
732
|
+
if self.update_defer_until > time.time():
|
|
733
|
+
self.last_notified_update = None
|
|
523
734
|
if not had_status:
|
|
524
735
|
log("connected to DeepSeek Harness control socket")
|
|
525
736
|
|
|
@@ -539,16 +750,7 @@ class Tray(QSystemTrayIcon):
|
|
|
539
750
|
self.suppress_stop_notice = True
|
|
540
751
|
self.tray_update = False
|
|
541
752
|
|
|
542
|
-
|
|
543
|
-
isinstance(self.update, dict)
|
|
544
|
-
and self.update.get("updateAvailable")
|
|
545
|
-
and self.update.get("candidate") != self.last_notified_update
|
|
546
|
-
):
|
|
547
|
-
self.last_notified_update = self.update.get("candidate")
|
|
548
|
-
self._notify(
|
|
549
|
-
"有可用更新",
|
|
550
|
-
f"DeepSeek Harness {self.update.get('candidate')}(当前 {self.update.get('current')})",
|
|
551
|
-
)
|
|
753
|
+
self._maybe_notify_update()
|
|
552
754
|
self._refresh_menu()
|
|
553
755
|
|
|
554
756
|
def _handle_unreachable(self) -> None:
|
|
@@ -567,8 +769,7 @@ class Tray(QSystemTrayIcon):
|
|
|
567
769
|
self.suppress_stop_notice = False
|
|
568
770
|
self._apply_state("stopped")
|
|
569
771
|
if self.restart_pending:
|
|
570
|
-
self.
|
|
571
|
-
self.launch()
|
|
772
|
+
self._begin_restart_wait()
|
|
572
773
|
self._refresh_menu()
|
|
573
774
|
|
|
574
775
|
# ── actions ─────────────────────────────────────────────────────────────
|
|
@@ -579,16 +780,33 @@ class Tray(QSystemTrayIcon):
|
|
|
579
780
|
self.launch()
|
|
580
781
|
|
|
581
782
|
def open_plugins(self) -> None:
|
|
582
|
-
|
|
583
|
-
self._notify("服务未运行", "请先点击托盘图标启动 DeepSeek Harness。")
|
|
584
|
-
return
|
|
585
|
-
self._run_launcher("dsh-app-controls/plugins")
|
|
783
|
+
self._open_view("plugins")
|
|
586
784
|
|
|
587
785
|
def open_settings(self) -> None:
|
|
786
|
+
self._open_view("settings")
|
|
787
|
+
|
|
788
|
+
def _open_view(self, view: str) -> None:
|
|
789
|
+
"""Switch the existing Web window to ``view`` instead of opening one.
|
|
790
|
+
|
|
791
|
+
The host owns the browser window, so while the service is running the
|
|
792
|
+
tray asks it to change views over the control socket and only then
|
|
793
|
+
focuses the app through the launcher. The launcher is called without
|
|
794
|
+
a hash so an existing Chrome app window is reused. If the service is
|
|
795
|
+
stopped, or the host predates the ``open-view`` command, fall back to
|
|
796
|
+
the launcher hash so the page still opens.
|
|
797
|
+
"""
|
|
588
798
|
if self.status is None:
|
|
589
|
-
self.
|
|
799
|
+
self._run_launcher(f"dsh-app-controls/{view}")
|
|
590
800
|
return
|
|
591
|
-
|
|
801
|
+
|
|
802
|
+
def done(response: dict) -> None:
|
|
803
|
+
if response.get("ok"):
|
|
804
|
+
self._run_launcher()
|
|
805
|
+
return
|
|
806
|
+
log(f"open-view {view} failed: {response.get('error') or response.get('message')}")
|
|
807
|
+
self._run_launcher(f"dsh-app-controls/{view}")
|
|
808
|
+
|
|
809
|
+
self._request({"command": "open-view", "view": view}, done, timeout_ms=1500)
|
|
592
810
|
|
|
593
811
|
def launch(self) -> None:
|
|
594
812
|
if self.starting:
|
|
@@ -638,6 +856,46 @@ class Tray(QSystemTrayIcon):
|
|
|
638
856
|
except Exception as error:
|
|
639
857
|
self._notify("无法打开窗口", str(error))
|
|
640
858
|
|
|
859
|
+
def _service_port_open(self) -> bool:
|
|
860
|
+
"""Whether the Web service's TCP port is still accepting connections."""
|
|
861
|
+
parts = urlsplit(URL)
|
|
862
|
+
host = parts.hostname or "127.0.0.1"
|
|
863
|
+
port = parts.port or (443 if parts.scheme == "https" else 80)
|
|
864
|
+
try:
|
|
865
|
+
with socket.create_connection((host, port), timeout=0.2):
|
|
866
|
+
return True
|
|
867
|
+
except OSError:
|
|
868
|
+
return False
|
|
869
|
+
|
|
870
|
+
def _begin_restart_wait(self) -> None:
|
|
871
|
+
if self.restart_waiting or not self.restart_pending:
|
|
872
|
+
return
|
|
873
|
+
self.restart_waiting = True
|
|
874
|
+
self.restart_deadline = time.time() + RESTART_TIMEOUT_SECONDS
|
|
875
|
+
log("restart: waiting for the old service to release the port")
|
|
876
|
+
QTimer.singleShot(RESTART_POLL_MS, self._restart_tick)
|
|
877
|
+
|
|
878
|
+
def _restart_tick(self) -> None:
|
|
879
|
+
if not self.restart_waiting:
|
|
880
|
+
return
|
|
881
|
+
if not self.restart_pending:
|
|
882
|
+
self.restart_waiting = False
|
|
883
|
+
return
|
|
884
|
+
if not self._service_port_open():
|
|
885
|
+
self.restart_waiting = False
|
|
886
|
+
self.restart_pending = False
|
|
887
|
+
log("restart: old service stopped; launching a new one")
|
|
888
|
+
self.launch()
|
|
889
|
+
return
|
|
890
|
+
if time.time() >= self.restart_deadline:
|
|
891
|
+
self.restart_waiting = False
|
|
892
|
+
self.restart_pending = False
|
|
893
|
+
log("restart: timed out waiting for the old service to stop")
|
|
894
|
+
self._notify("重启失败", "旧服务没有及时退出,请查看日志后重试。")
|
|
895
|
+
self._apply_state("stopped")
|
|
896
|
+
return
|
|
897
|
+
QTimer.singleShot(RESTART_POLL_MS, self._restart_tick)
|
|
898
|
+
|
|
641
899
|
def stop_dsh(self) -> None:
|
|
642
900
|
if self.status is None:
|
|
643
901
|
return
|
|
@@ -652,12 +910,16 @@ class Tray(QSystemTrayIcon):
|
|
|
652
910
|
return
|
|
653
911
|
self.suppress_stop_notice = True
|
|
654
912
|
self.restart_pending = False
|
|
913
|
+
self.restart_waiting = False
|
|
655
914
|
self._apply_state("stopping")
|
|
656
915
|
self._request({"command": "quit"}, self._on_quit_response)
|
|
657
916
|
|
|
658
917
|
def restart_dsh(self) -> None:
|
|
659
918
|
if self.status is None:
|
|
660
|
-
self.
|
|
919
|
+
if not self.restart_waiting:
|
|
920
|
+
self.launch()
|
|
921
|
+
return
|
|
922
|
+
if self.restart_waiting:
|
|
661
923
|
return
|
|
662
924
|
self.suppress_stop_notice = True
|
|
663
925
|
self.restart_pending = True
|
|
@@ -665,22 +927,47 @@ class Tray(QSystemTrayIcon):
|
|
|
665
927
|
self._request({"command": "quit"}, self._on_quit_response)
|
|
666
928
|
|
|
667
929
|
def _on_quit_response(self, response: dict) -> None:
|
|
668
|
-
if
|
|
669
|
-
|
|
930
|
+
if response.get("ok"):
|
|
931
|
+
return
|
|
932
|
+
message = response.get("message") or response.get("error") or "未知错误"
|
|
933
|
+
self.restart_waiting = False
|
|
934
|
+
if self.restart_pending:
|
|
935
|
+
self.restart_pending = False
|
|
936
|
+
self._notify("重启失败", message)
|
|
937
|
+
else:
|
|
938
|
+
self._notify("停止失败", message)
|
|
670
939
|
|
|
671
940
|
def on_update_action(self) -> None:
|
|
672
941
|
if self.job and self.job.get("active"):
|
|
673
942
|
return
|
|
674
943
|
if isinstance(self.update, dict) and self.update.get("updateAvailable"):
|
|
675
944
|
candidate = self.update.get("candidate")
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
945
|
+
activity = self.update.get("activity") if isinstance(self.update.get("activity"), dict) else {}
|
|
946
|
+
active = bool(activity.get("active"))
|
|
947
|
+
minutes = read_update_defer_minutes()
|
|
948
|
+
defer_label = "1 小时" if minutes == 60 else f"{minutes} 分钟"
|
|
949
|
+
box = QMessageBox()
|
|
950
|
+
box.setWindowTitle("更新 DeepSeek Harness")
|
|
951
|
+
box.setIcon(QMessageBox.Warning if active else QMessageBox.Information)
|
|
952
|
+
box.setText(f"发现新版本 {candidate}(当前 {self.update.get('current')})。")
|
|
953
|
+
box.setInformativeText(
|
|
954
|
+
f"{self._activity_text()}\n\n"
|
|
955
|
+
"更新会下载新版本并重启本地服务,正在进行的对话或后台任务会被中断。\n"
|
|
956
|
+
f"你可以立即更新,或推迟 {defer_label} 后再处理。"
|
|
682
957
|
)
|
|
683
|
-
|
|
958
|
+
notes = self.update.get("releaseNotes")
|
|
959
|
+
if isinstance(notes, dict) and isinstance(notes.get("notes"), str) and notes["notes"].strip():
|
|
960
|
+
box.setDetailedText(notes["notes"].strip()[:4000])
|
|
961
|
+
update_button = box.addButton("立即更新", QMessageBox.AcceptRole)
|
|
962
|
+
defer_button = box.addButton(f"推迟 {defer_label}", QMessageBox.ActionRole)
|
|
963
|
+
box.addButton("取消", QMessageBox.RejectRole)
|
|
964
|
+
box.setDefaultButton(defer_button if active else update_button)
|
|
965
|
+
box.exec_()
|
|
966
|
+
clicked = box.clickedButton()
|
|
967
|
+
if clicked is defer_button:
|
|
968
|
+
self._defer_update(minutes)
|
|
969
|
+
return
|
|
970
|
+
if clicked is not update_button:
|
|
684
971
|
return
|
|
685
972
|
self.tray_update = True
|
|
686
973
|
self._request({"command": "update"}, self._on_update_start, timeout_ms=CHECK_TIMEOUT_MS)
|
|
@@ -770,6 +1057,96 @@ for (var pi = 0; pi < ps.length; pi++) {
|
|
|
770
1057
|
self._refresh_menu()
|
|
771
1058
|
|
|
772
1059
|
def _make_icon(self, state: str) -> QIcon:
|
|
1060
|
+
if self.icon_style == "custom":
|
|
1061
|
+
icon = self._make_custom_icon(state)
|
|
1062
|
+
if icon is not None:
|
|
1063
|
+
return icon
|
|
1064
|
+
if self.icon_style == "classic":
|
|
1065
|
+
return self._make_classic_icon(state)
|
|
1066
|
+
return self._make_deepseek_icon(state)
|
|
1067
|
+
|
|
1068
|
+
def _whale_pixmap(self) -> QPixmap | None:
|
|
1069
|
+
if self._whale_source is not None:
|
|
1070
|
+
return self._whale_source
|
|
1071
|
+
pixmap = QPixmap(str(WHALE_ASSET))
|
|
1072
|
+
if pixmap.isNull():
|
|
1073
|
+
log(f"DeepSeek tray asset is missing: {WHALE_ASSET}")
|
|
1074
|
+
return None
|
|
1075
|
+
self._whale_source = pixmap
|
|
1076
|
+
return pixmap
|
|
1077
|
+
|
|
1078
|
+
@staticmethod
|
|
1079
|
+
def _tint_pixmap(source: QPixmap, color: QColor) -> QPixmap:
|
|
1080
|
+
result = QPixmap(source.size())
|
|
1081
|
+
result.fill(Qt.transparent)
|
|
1082
|
+
painter = QPainter(result)
|
|
1083
|
+
painter.drawPixmap(0, 0, source)
|
|
1084
|
+
painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
|
|
1085
|
+
painter.fillRect(result.rect(), color)
|
|
1086
|
+
painter.end()
|
|
1087
|
+
return result
|
|
1088
|
+
|
|
1089
|
+
def _draw_status_dot(self, pixmap: QPixmap, state: str) -> None:
|
|
1090
|
+
dot = QColor(_STATE_COLORS.get(state, _STATE_COLORS["running"]))
|
|
1091
|
+
ring = QColor(self._colors()["bg"])
|
|
1092
|
+
painter = QPainter(pixmap)
|
|
1093
|
+
painter.setRenderHint(QPainter.Antialiasing)
|
|
1094
|
+
painter.setPen(QPen(ring, 3))
|
|
1095
|
+
painter.setBrush(dot)
|
|
1096
|
+
painter.drawEllipse(QRectF(43, 43, 17, 17))
|
|
1097
|
+
painter.end()
|
|
1098
|
+
|
|
1099
|
+
def _make_deepseek_icon(self, state: str) -> QIcon:
|
|
1100
|
+
mask = self._whale_pixmap()
|
|
1101
|
+
if mask is None:
|
|
1102
|
+
return self._make_classic_icon(state)
|
|
1103
|
+
size = 64
|
|
1104
|
+
canvas = QPixmap(size, size)
|
|
1105
|
+
canvas.fill(Qt.transparent)
|
|
1106
|
+
painter = QPainter(canvas)
|
|
1107
|
+
painter.setRenderHint(QPainter.SmoothPixmapTransform)
|
|
1108
|
+
scaled = mask.scaled(58, 58, Qt.KeepAspectRatio, Qt.SmoothTransformation)
|
|
1109
|
+
painter.drawPixmap((size - scaled.width()) // 2, (size - scaled.height()) // 2, scaled)
|
|
1110
|
+
painter.end()
|
|
1111
|
+
# The bundled asset is a white alpha mask, tinted with the current
|
|
1112
|
+
# theme text colour so it stays visible on both light and dark panels.
|
|
1113
|
+
tinted = self._tint_pixmap(canvas, QColor(self._colors()["text"]))
|
|
1114
|
+
self._draw_status_dot(tinted, state)
|
|
1115
|
+
icon = QIcon()
|
|
1116
|
+
icon.addPixmap(tinted, QIcon.Normal)
|
|
1117
|
+
icon.addPixmap(tinted, QIcon.Active)
|
|
1118
|
+
icon.addPixmap(tinted, QIcon.Selected)
|
|
1119
|
+
return icon
|
|
1120
|
+
|
|
1121
|
+
def _make_custom_icon(self, state: str) -> QIcon | None:
|
|
1122
|
+
path = self.custom_icon_path
|
|
1123
|
+
try:
|
|
1124
|
+
if not path or not path.is_file():
|
|
1125
|
+
return None
|
|
1126
|
+
source = QPixmap(str(path))
|
|
1127
|
+
except Exception:
|
|
1128
|
+
source = QPixmap()
|
|
1129
|
+
if source.isNull():
|
|
1130
|
+
if not self._custom_icon_fallback_logged:
|
|
1131
|
+
self._custom_icon_fallback_logged = True
|
|
1132
|
+
log(f"custom tray icon is not readable: {path}; using DeepSeek icon")
|
|
1133
|
+
return None
|
|
1134
|
+
size = 64
|
|
1135
|
+
canvas = QPixmap(size, size)
|
|
1136
|
+
canvas.fill(Qt.transparent)
|
|
1137
|
+
painter = QPainter(canvas)
|
|
1138
|
+
painter.setRenderHint(QPainter.SmoothPixmapTransform)
|
|
1139
|
+
scaled = source.scaled(58, 58, Qt.KeepAspectRatio, Qt.SmoothTransformation)
|
|
1140
|
+
painter.drawPixmap((size - scaled.width()) // 2, (size - scaled.height()) // 2, scaled)
|
|
1141
|
+
painter.end()
|
|
1142
|
+
self._draw_status_dot(canvas, state)
|
|
1143
|
+
icon = QIcon()
|
|
1144
|
+
icon.addPixmap(canvas, QIcon.Normal)
|
|
1145
|
+
icon.addPixmap(canvas, QIcon.Active)
|
|
1146
|
+
icon.addPixmap(canvas, QIcon.Selected)
|
|
1147
|
+
return icon
|
|
1148
|
+
|
|
1149
|
+
def _make_classic_icon(self, state: str) -> QIcon:
|
|
773
1150
|
pixmap = QPixmap(64, 64)
|
|
774
1151
|
pixmap.fill(Qt.transparent)
|
|
775
1152
|
painter = QPainter(pixmap)
|
|
@@ -803,7 +1180,10 @@ for (var pi = 0; pi < ps.length; pi++) {
|
|
|
803
1180
|
if self.job and self.job.get("active"):
|
|
804
1181
|
lines.append(f"更新中 · {self.job.get('percent', 0)}%")
|
|
805
1182
|
elif isinstance(self.update, dict) and self.update.get("updateAvailable"):
|
|
806
|
-
|
|
1183
|
+
if self._update_deferred():
|
|
1184
|
+
lines.append(f"可更新到 {self.update.get('candidate')}(已推迟到 {self._defer_time_text()})")
|
|
1185
|
+
else:
|
|
1186
|
+
lines.append(f"可更新到 {self.update.get('candidate')}")
|
|
807
1187
|
lines.append("左键打开 · 右键菜单")
|
|
808
1188
|
return "\n".join(lines)
|
|
809
1189
|
|
|
@@ -839,7 +1219,10 @@ for (var pi = 0; pi < ps.length; pi++) {
|
|
|
839
1219
|
self.update_action.setText(f"正在更新 {self.job.get('percent', 0)}%")
|
|
840
1220
|
self.update_action.setEnabled(False)
|
|
841
1221
|
elif isinstance(self.update, dict) and self.update.get("updateAvailable"):
|
|
842
|
-
|
|
1222
|
+
if self._update_deferred():
|
|
1223
|
+
self.update_action.setText(f"更新到 {self.update.get('candidate')}({self._defer_time_text()} 再提醒)")
|
|
1224
|
+
else:
|
|
1225
|
+
self.update_action.setText(f"更新到 {self.update.get('candidate')}")
|
|
843
1226
|
self.update_action.setEnabled(True)
|
|
844
1227
|
elif self.check_result:
|
|
845
1228
|
self.update_action.setText(self.check_result)
|
|
@@ -850,8 +1233,9 @@ for (var pi = 0; pi < ps.length; pi++) {
|
|
|
850
1233
|
|
|
851
1234
|
self.status_action.setIcon(self._status_icon())
|
|
852
1235
|
self.restart_action.setText("重启服务" if running else "启动服务")
|
|
853
|
-
|
|
854
|
-
self.
|
|
1236
|
+
# Navigation stays available while stopped: it launches/focuses the app.
|
|
1237
|
+
self.plugins_action.setEnabled(True)
|
|
1238
|
+
self.settings_action.setEnabled(True)
|
|
855
1239
|
self.stop_action.setEnabled(running)
|
|
856
1240
|
self.copy_action.setEnabled(running)
|
|
857
1241
|
self.logs_action.setEnabled(True)
|
|
@@ -876,6 +1260,15 @@ def run_check() -> int:
|
|
|
876
1260
|
def main() -> int:
|
|
877
1261
|
if "--check" in sys.argv:
|
|
878
1262
|
return run_check()
|
|
1263
|
+
# A fresh tray is only needed when this user has no other tray helper.
|
|
1264
|
+
# Checking before the lock/pid means a duplicate exits without touching
|
|
1265
|
+
# tray.pid, so the host never sees a stale or foreign pid.
|
|
1266
|
+
others = find_other_tray_processes()
|
|
1267
|
+
if others:
|
|
1268
|
+
message = "dsh-tray is already running (pid {})".format(", ".join(str(pid) for pid in sorted(others)))
|
|
1269
|
+
log(message)
|
|
1270
|
+
print(message, file=sys.stderr)
|
|
1271
|
+
return 0
|
|
879
1272
|
STATE_DIR.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
880
1273
|
lock = open(LOCK_FILE, "w", encoding="utf-8")
|
|
881
1274
|
try:
|