dsh-dev-modes 1.1.4 → 1.1.9
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/helpers/window-quiet-watch.ps1 +220 -0
- package/package.json +1 -1
- package/quiet-run/index.js +120 -5
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
<#
|
|
2
|
+
window-quiet-watch.ps1 — 静默运行的后台执行器。
|
|
3
|
+
|
|
4
|
+
为什么需要它:光靠提示词让模型"记得把窗口移走"不可靠。这个脚本由插件在启动时
|
|
5
|
+
拉起,持续监视屏幕上新出现的窗口;只要当前会话的「静默运行」是开启状态,就把
|
|
6
|
+
新窗口立刻移到可视区之外,完全不依赖模型是否照做。
|
|
7
|
+
|
|
8
|
+
用法(一般由插件自动启动,不需要手动跑):
|
|
9
|
+
powershell.exe -NoProfile -ExecutionPolicy Bypass -File window-quiet-watch.ps1 `
|
|
10
|
+
-StateUrl "http://127.0.0.1:59898/dsh-quiet-run/state"
|
|
11
|
+
|
|
12
|
+
行为:
|
|
13
|
+
- 每 IntervalMs 毫秒问一次开关状态(quiet 为 true 才动手)
|
|
14
|
+
- 只处理"上一轮没有、这一轮新出现"的窗口,已经在屏幕上的不动
|
|
15
|
+
- 跳过系统外壳窗口(任务栏、开始菜单、输入法、桌面等),避免误伤
|
|
16
|
+
- 移走的窗口记到 $env:TEMP\dsh-quiet-windows.json,可用 window-quiet.ps1 -Action show 还原
|
|
17
|
+
#>
|
|
18
|
+
param(
|
|
19
|
+
[string]$FlagFile,
|
|
20
|
+
[string]$StateUrl,
|
|
21
|
+
[int]$IntervalMs = 700,
|
|
22
|
+
[int]$X = 20000,
|
|
23
|
+
[int]$Y = 20000
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
$ErrorActionPreference = 'Continue'
|
|
27
|
+
|
|
28
|
+
$sig = @'
|
|
29
|
+
using System;
|
|
30
|
+
using System.Text;
|
|
31
|
+
using System.Collections.Generic;
|
|
32
|
+
using System.Runtime.InteropServices;
|
|
33
|
+
public class QWatchWin {
|
|
34
|
+
public delegate bool EnumProc(IntPtr h, IntPtr l);
|
|
35
|
+
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc cb, IntPtr l);
|
|
36
|
+
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h);
|
|
37
|
+
[DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetWindowTextW(IntPtr h, StringBuilder s, int n);
|
|
38
|
+
[DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetClassNameW(IntPtr h, StringBuilder s, int n);
|
|
39
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
|
|
40
|
+
[DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr h, IntPtr after, int x, int y, int cx, int cy, uint flags);
|
|
41
|
+
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
|
|
42
|
+
public struct RECT { public int Left, Top, Right, Bottom; }
|
|
43
|
+
|
|
44
|
+
public static List<string> List() {
|
|
45
|
+
var acc = new List<string>();
|
|
46
|
+
EnumWindows((h, l) => {
|
|
47
|
+
if (!IsWindowVisible(h)) return true;
|
|
48
|
+
var sb = new StringBuilder(1024);
|
|
49
|
+
GetWindowTextW(h, sb, 1024);
|
|
50
|
+
var title = sb.ToString();
|
|
51
|
+
if (title.Length == 0) return true;
|
|
52
|
+
var cb = new StringBuilder(256);
|
|
53
|
+
GetClassNameW(h, cb, 256);
|
|
54
|
+
uint pid; GetWindowThreadProcessId(h, out pid);
|
|
55
|
+
RECT r; GetWindowRect(h, out r);
|
|
56
|
+
acc.Add(h.ToInt64() + "|" + pid + "|" + r.Left + "|" + r.Top + "|" + (r.Right - r.Left) + "|" + (r.Bottom - r.Top) + "|" + cb.ToString() + "|" + title);
|
|
57
|
+
return true;
|
|
58
|
+
}, IntPtr.Zero);
|
|
59
|
+
return acc;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public static bool Move(IntPtr h, int x, int y, int cx, int cy) {
|
|
63
|
+
return SetWindowPos(h, IntPtr.Zero, x, y, cx, cy, 0x0014);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
'@
|
|
67
|
+
|
|
68
|
+
if (-not ('QWatchWin' -as [type])) {
|
|
69
|
+
Add-Type -TypeDefinition $sig -ErrorAction Stop
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
$logFile = Join-Path $env:TEMP 'dsh-quiet-watch.log'
|
|
73
|
+
$stateFile = Join-Path $env:TEMP 'dsh-quiet-windows.json'
|
|
74
|
+
$pidFile = Join-Path (Split-Path -Parent $FlagFile) 'watcher.pid'
|
|
75
|
+
|
|
76
|
+
function Log([string]$message) {
|
|
77
|
+
$line = (Get-Date).ToString('HH:mm:ss.fff') + ' ' + $message
|
|
78
|
+
try { Add-Content -Path $logFile -Value $line -Encoding UTF8 } catch { }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
# 单实例:插件每次热重载都会尝试拉起监视器,这里由脚本自己保证只有一个
|
|
82
|
+
try {
|
|
83
|
+
if (Test-Path $pidFile) {
|
|
84
|
+
$old = (Get-Content $pidFile -Raw -Encoding UTF8).Trim()
|
|
85
|
+
if ($old -and $old -ne "$PID") {
|
|
86
|
+
$alive = Get-Process -Id ([int]$old) -ErrorAction SilentlyContinue
|
|
87
|
+
if ($alive) {
|
|
88
|
+
Log "已有监视器在运行(PID $old),本实例退出"
|
|
89
|
+
exit 0
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
[System.IO.File]::WriteAllText($pidFile, "$PID", (New-Object System.Text.UTF8Encoding($false)))
|
|
94
|
+
} catch { }
|
|
95
|
+
|
|
96
|
+
# 不该被移动的窗口:系统外壳、输入法、桌面、以及我们自己的进程
|
|
97
|
+
$skipClasses = @(
|
|
98
|
+
'Shell_TrayWnd', 'Shell_SecondaryTrayWnd', 'Progman', 'WorkerW', 'NotifyIconOverflowWindow',
|
|
99
|
+
'Windows.UI.Core.CoreWindow', 'ApplicationFrameWindow_Hidden', 'DV2ControlHost',
|
|
100
|
+
'MsgrIMEWindowClass', 'IME', 'ConsoleWindowClass'
|
|
101
|
+
)
|
|
102
|
+
$skipProcessNames = @(
|
|
103
|
+
'explorer', 'SearchHost', 'StartMenuExperienceHost', 'ShellExperienceHost', 'TextInputHost',
|
|
104
|
+
'ApplicationFrameHost', 'LockApp', 'Widgets', 'SystemSettings', 'powershell', 'pwsh',
|
|
105
|
+
'conhost', 'WindowsTerminal'
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
function Get-WindowSnapshot {
|
|
109
|
+
$rows = @()
|
|
110
|
+
foreach ($line in [QWatchWin]::List()) {
|
|
111
|
+
$p = $line -split '\|', 8
|
|
112
|
+
$rows += [pscustomobject]@{
|
|
113
|
+
handle = [int64]$p[0]; pid = [int]$p[1]
|
|
114
|
+
left = [int]$p[2]; top = [int]$p[3]; width = [int]$p[4]; height = [int]$p[5]
|
|
115
|
+
klass = $p[6]; title = $p[7]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return $rows
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function Should-Skip($w) {
|
|
122
|
+
if ($skipClasses -contains $w.klass) { return $true }
|
|
123
|
+
try {
|
|
124
|
+
$proc = Get-Process -Id $w.pid -ErrorAction Stop
|
|
125
|
+
if ($skipProcessNames -contains $proc.ProcessName) { return $true }
|
|
126
|
+
} catch { }
|
|
127
|
+
# 窗口比 40x40 小的多为工具提示类,不处理
|
|
128
|
+
if ($w.width -lt 40 -or $w.height -lt 40) { return $true }
|
|
129
|
+
return $false
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function Save-Restore($w) {
|
|
133
|
+
$list = New-Object System.Collections.ArrayList
|
|
134
|
+
if (Test-Path $stateFile) {
|
|
135
|
+
try {
|
|
136
|
+
$raw = Get-Content $stateFile -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
137
|
+
foreach ($item in @($raw)) { if ($item) { [void]$list.Add($item) } }
|
|
138
|
+
} catch { }
|
|
139
|
+
}
|
|
140
|
+
$record = [pscustomobject]@{
|
|
141
|
+
handle = $w.handle; pid = $w.pid; title = $w.title
|
|
142
|
+
left = $w.left; top = $w.top; width = $w.width; height = $w.height
|
|
143
|
+
}
|
|
144
|
+
[void]$list.Add($record)
|
|
145
|
+
$body = ($list | ForEach-Object { $_ | ConvertTo-Json -Depth 5 -Compress }) -join ','
|
|
146
|
+
[System.IO.File]::WriteAllText($stateFile, "[$body]", (New-Object System.Text.UTF8Encoding($false)))
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
Log "watcher 启动,标志文件 '$FlagFile',状态源 '$StateUrl',间隔 ${IntervalMs}ms"
|
|
150
|
+
|
|
151
|
+
# 初始快照:已经打开的窗口视为"已知",绝不移动它们
|
|
152
|
+
$known = New-Object System.Collections.Generic.HashSet[int64]
|
|
153
|
+
foreach ($w in Get-WindowSnapshot) { [void]$known.Add($w.handle) }
|
|
154
|
+
|
|
155
|
+
function Read-Quiet {
|
|
156
|
+
# 优先读插件写的标志文件(不依赖端口),文件太旧就当没开
|
|
157
|
+
if ($FlagFile -and (Test-Path $FlagFile)) {
|
|
158
|
+
try {
|
|
159
|
+
$age = (Get-Date) - (Get-Item $FlagFile).LastWriteTime
|
|
160
|
+
if ($age.TotalSeconds -le 30) {
|
|
161
|
+
$flag = Get-Content $FlagFile -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
162
|
+
return ($flag.quiet -eq $true)
|
|
163
|
+
}
|
|
164
|
+
} catch { }
|
|
165
|
+
return $false
|
|
166
|
+
}
|
|
167
|
+
if ($StateUrl) {
|
|
168
|
+
try {
|
|
169
|
+
$state = Invoke-RestMethod -Uri $StateUrl -TimeoutSec 3 -ErrorAction Stop
|
|
170
|
+
return ($state.quiet -eq $true)
|
|
171
|
+
} catch {
|
|
172
|
+
return $false
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return $false
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
$quiet = $false
|
|
179
|
+
$tick = 0
|
|
180
|
+
$staleTicks = 0
|
|
181
|
+
while ($true) {
|
|
182
|
+
$tick++
|
|
183
|
+
|
|
184
|
+
# 宿主已经不写标志文件了(DSH 退出/崩溃),连续 10 分钟没动静就自行退出,别留孤儿进程
|
|
185
|
+
if ($FlagFile -and (Test-Path $FlagFile)) {
|
|
186
|
+
if (((Get-Date) - (Get-Item $FlagFile).LastWriteTime).TotalMinutes -gt 10) {
|
|
187
|
+
$staleTicks++
|
|
188
|
+
if ($staleTicks -gt 20) {
|
|
189
|
+
Log '标志文件长期未更新,监视器退出'
|
|
190
|
+
exit 0
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
$staleTicks = 0
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
$quiet = Read-Quiet
|
|
198
|
+
|
|
199
|
+
$current = Get-WindowSnapshot
|
|
200
|
+
$currentHandles = New-Object System.Collections.Generic.HashSet[int64]
|
|
201
|
+
foreach ($w in $current) {
|
|
202
|
+
[void]$currentHandles.Add($w.handle)
|
|
203
|
+
if ($known.Contains($w.handle)) { continue }
|
|
204
|
+
# 新出现的窗口
|
|
205
|
+
if (Should-Skip $w) { continue }
|
|
206
|
+
if (-not $quiet) { continue }
|
|
207
|
+
if ($w.left -ge 10000 -or $w.top -ge 10000) { continue }
|
|
208
|
+
$ok = [QWatchWin]::Move([IntPtr]$w.handle, $X, $Y, $w.width, $w.height)
|
|
209
|
+
if ($ok) {
|
|
210
|
+
Save-Restore $w
|
|
211
|
+
Log "已移出:[PID $($w.pid)] $($w.title)"
|
|
212
|
+
} else {
|
|
213
|
+
Log "移动失败(可能全屏独占):[PID $($w.pid)] $($w.title)"
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
# 忘掉已关闭的窗口,避免集合无限增长
|
|
218
|
+
$known = $currentHandles
|
|
219
|
+
Start-Sleep -Milliseconds $IntervalMs
|
|
220
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-dev-modes",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Three DeepSeek Harness Agent presets in one bundle — plus the per-session isolation and quiet-run switches: software development, game development, and read-only discussion, with the matching delivery and engine workflows bundled as skills.",
|
|
6
6
|
"author": "qingfengshangnanzhi",
|
package/quiet-run/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import os from 'node:os';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
4
5
|
import { fileURLToPath } from 'node:url';
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -34,11 +35,117 @@ function logRequest(entry) {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
/** 与包一起分发的窗口搬运脚本(绝对路径,写进规则里让模型直接可用)。 */
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
const PKG_ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
39
|
+
const HELPER = path.join(PKG_ROOT, 'helpers', 'window-quiet.ps1');
|
|
40
|
+
const WATCHER = path.join(PKG_ROOT, 'helpers', 'window-quiet-watch.ps1');
|
|
41
|
+
const WATCHER_PID_FILE = path.join(STORE_DIR, 'watcher.pid');
|
|
42
|
+
/**
|
|
43
|
+
* 给后台监视器看的标志文件:宿主进程里读不到 web 端口,所以不让 watcher 走 HTTP,
|
|
44
|
+
* 直接由插件在每次读写开关时落一个 { quiet } 文件,watcher 轮询它即可。
|
|
45
|
+
*/
|
|
46
|
+
const WATCHER_FLAG_FILE = path.join(STORE_DIR, 'watch-flag.json');
|
|
47
|
+
const WATCHER_DEBUG_FILE = path.join(STORE_DIR, 'watcher-debug.log');
|
|
48
|
+
|
|
49
|
+
function appendWatcherDebug(text) {
|
|
50
|
+
try {
|
|
51
|
+
fs.mkdirSync(STORE_DIR, { recursive: true });
|
|
52
|
+
fs.appendFileSync(WATCHER_DEBUG_FILE, `${new Date().toISOString()} ${text}\n`, 'utf8');
|
|
53
|
+
} catch {
|
|
54
|
+
/* ignore */
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function writeWatchFlag(sessionId, quiet) {
|
|
59
|
+
try {
|
|
60
|
+
fs.mkdirSync(STORE_DIR, { recursive: true });
|
|
61
|
+
fs.writeFileSync(
|
|
62
|
+
WATCHER_FLAG_FILE,
|
|
63
|
+
JSON.stringify({ sessionId: sessionId ?? null, quiet: Boolean(quiet), t: Date.now() }),
|
|
64
|
+
'utf8',
|
|
65
|
+
);
|
|
66
|
+
} catch {
|
|
67
|
+
/* ignore */
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 拉起后台监视器:静默开启时自动把新窗口移出可视区。
|
|
73
|
+
*
|
|
74
|
+
* 为什么要有它:只靠提示词让模型"记得移窗口"不可靠——模型可能没照做、
|
|
75
|
+
* 命令可能失败、执行的人可能根本不是本插件所在的会话。监视器由插件自己
|
|
76
|
+
* 启动,直接读开关状态,不依赖任何模型决策。
|
|
77
|
+
*/
|
|
78
|
+
function ensureWatcher() {
|
|
79
|
+
try {
|
|
80
|
+
// 已有监视器在跑就复用,避免热重载后堆出一堆进程
|
|
81
|
+
try {
|
|
82
|
+
const existing = parseInt(fs.readFileSync(WATCHER_PID_FILE, 'utf8').trim(), 10);
|
|
83
|
+
if (existing > 0) {
|
|
84
|
+
process.kill(existing, 0);
|
|
85
|
+
return existing;
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
/* 没有旧进程或已退出 */
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!fs.existsSync(WATCHER)) return null;
|
|
92
|
+
|
|
93
|
+
const child = spawn(
|
|
94
|
+
'cmd.exe',
|
|
95
|
+
[
|
|
96
|
+
'/c',
|
|
97
|
+
'start',
|
|
98
|
+
'""',
|
|
99
|
+
'/min',
|
|
100
|
+
'powershell.exe',
|
|
101
|
+
'-NoProfile',
|
|
102
|
+
'-ExecutionPolicy',
|
|
103
|
+
'Bypass',
|
|
104
|
+
'-File',
|
|
105
|
+
WATCHER,
|
|
106
|
+
'-FlagFile',
|
|
107
|
+
WATCHER_FLAG_FILE,
|
|
108
|
+
],
|
|
109
|
+
{ detached: true, stdio: 'ignore', windowsHide: true },
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
// 启动失败时把原因写下来,否则只能看到一个永远不起作用的开关
|
|
113
|
+
child.on('error', (err) => {
|
|
114
|
+
diag.watcherError = String((err && err.message) || err);
|
|
115
|
+
appendWatcherDebug('spawn error: ' + diag.watcherError);
|
|
116
|
+
});
|
|
117
|
+
child.on('exit', (code) => {
|
|
118
|
+
appendWatcherDebug('cmd launcher exited: ' + code);
|
|
119
|
+
});
|
|
120
|
+
child.unref();
|
|
121
|
+
try {
|
|
122
|
+
fs.mkdirSync(STORE_DIR, { recursive: true });
|
|
123
|
+
fs.writeFileSync(WATCHER_PID_FILE, String(child.pid), 'utf8');
|
|
124
|
+
} catch {
|
|
125
|
+
/* ignore */
|
|
126
|
+
}
|
|
127
|
+
return child.pid;
|
|
128
|
+
} catch (err) {
|
|
129
|
+
noteError('ensureWatcher', err);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function stopWatcher() {
|
|
135
|
+
try {
|
|
136
|
+
const pid = parseInt(fs.readFileSync(WATCHER_PID_FILE, 'utf8').trim(), 10);
|
|
137
|
+
if (pid > 0) {
|
|
138
|
+
try {
|
|
139
|
+
process.kill(pid);
|
|
140
|
+
} catch {
|
|
141
|
+
/* ignore */
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
fs.rmSync(WATCHER_PID_FILE, { force: true });
|
|
145
|
+
} catch {
|
|
146
|
+
/* ignore */
|
|
147
|
+
}
|
|
148
|
+
}
|
|
42
149
|
|
|
43
150
|
const QUIET_RULE = [
|
|
44
151
|
'## 静默运行(本会话已开启)',
|
|
@@ -194,6 +301,11 @@ function readJsonBody(req) {
|
|
|
194
301
|
export function apply(ctx) {
|
|
195
302
|
load();
|
|
196
303
|
|
|
304
|
+
// 后台监视器:静默开启时自动搬走新窗口,不依赖模型是否照做。
|
|
305
|
+
// 由脚本自己保证单实例,插件这边只管确保有人被拉起(热重载不会堆进程)。
|
|
306
|
+
const watcherPid = ensureWatcher();
|
|
307
|
+
diag.watcherPid = watcherPid;
|
|
308
|
+
|
|
197
309
|
const register = (agent) => {
|
|
198
310
|
try {
|
|
199
311
|
const sessionId = sessionIdOf(agent);
|
|
@@ -254,6 +366,8 @@ export function apply(ctx) {
|
|
|
254
366
|
const url = new URL(req.url || '/', 'http://127.0.0.1');
|
|
255
367
|
const asked = url.searchParams.get('sessionId');
|
|
256
368
|
const sessionId = asked || activeSessionId;
|
|
369
|
+
const quietNow = sessionId ? isQuiet(sessionId) : true;
|
|
370
|
+
if (sessionId) writeWatchFlag(sessionId, quietNow);
|
|
257
371
|
logRequest({ kind: 'state', asked: asked ?? null, resolved: sessionId, fromActive: !asked });
|
|
258
372
|
if (!sessionId) {
|
|
259
373
|
sendJson(res, 200, { sessionId: null, quiet: true, ready: false, diag });
|
|
@@ -292,6 +406,7 @@ export function apply(ctx) {
|
|
|
292
406
|
}
|
|
293
407
|
const quiet = Boolean(body.quiet);
|
|
294
408
|
const saved = setQuiet(sessionId, quiet);
|
|
409
|
+
writeWatchFlag(sessionId, quiet);
|
|
295
410
|
sendJson(res, 200, { sessionId, quiet, saved });
|
|
296
411
|
},
|
|
297
412
|
});
|