dsh-dev-modes 1.0.0 → 1.1.1
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/LICENSE +21 -0
- package/README.md +20 -0
- package/client.js +168 -0
- package/cordis.patch.yml +8 -1
- package/package.json +18 -3
- package/quiet-run/index.js +271 -0
- package/session-lock/index.js +265 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 huang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -18,6 +18,26 @@
|
|
|
18
18
|
|
|
19
19
|
如果 `~/.dsh/skills/` 里还留着同名副本,运行时会优先使用本包注册的版本;可以删掉副本以免困惑。
|
|
20
20
|
|
|
21
|
+
## 自带开关(1.1.0 起)
|
|
22
|
+
|
|
23
|
+
本包另外挂载两个**宿主级**插件,所以对**所有** Agent 预设生效(包括内置的 Standard / PTC / Minimal / Creator),不只是本包的三个模式。输入框旁边并排两个按钮:
|
|
24
|
+
|
|
25
|
+
| 按钮 | 作用 | 默认 |
|
|
26
|
+
|---|---|---|
|
|
27
|
+
| 🔒 禁止读其它会话 | 每个会话独立:锁定时禁止读取 `~/.dsh/sessions/` 等其它会话文件 | 锁定 |
|
|
28
|
+
| 🔇 静默运行 | 每个会话独立:启动游戏窗口/编辑器时必须挪到屏幕外或改用无窗口方式,不挡前台、不抢焦点 | 静默 |
|
|
29
|
+
|
|
30
|
+
点一下切换,对**当前会话**生效,下一次模型请求立刻起作用。
|
|
31
|
+
|
|
32
|
+
状态存储(升级后原有设置保留):
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
$DSH_HOME/storages/dsh-session-lock/policies.json
|
|
36
|
+
$DSH_HOME/storages/dsh-quiet-run/state.json
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
> 这两个是**提示词级约束**,不是强制拦截:模型被告知规则并遵守,但宿主不会物理阻止。窗口挪出屏幕后仍占用 GPU/CPU。
|
|
40
|
+
|
|
21
41
|
## 安装
|
|
22
42
|
|
|
23
43
|
```text
|
package/client.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: 'dsh-dev-modes',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const React = require('react');
|
|
5
|
+
const h = React.createElement;
|
|
6
|
+
|
|
7
|
+
const baseBtn = {
|
|
8
|
+
display: 'inline-flex',
|
|
9
|
+
alignItems: 'center',
|
|
10
|
+
justifyContent: 'center',
|
|
11
|
+
gap: 4,
|
|
12
|
+
height: 28,
|
|
13
|
+
padding: '0 8px',
|
|
14
|
+
border: '1px solid rgba(128,128,128,0.35)',
|
|
15
|
+
borderRadius: 8,
|
|
16
|
+
background: 'transparent',
|
|
17
|
+
color: 'inherit',
|
|
18
|
+
font: 'inherit',
|
|
19
|
+
fontSize: 13,
|
|
20
|
+
lineHeight: 1,
|
|
21
|
+
cursor: 'pointer',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
async function getJson(url) {
|
|
25
|
+
const res = await fetch(url, { cache: 'no-store' });
|
|
26
|
+
if (!res.ok) throw new Error('HTTP ' + res.status);
|
|
27
|
+
return await res.json();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function postJson(url, body) {
|
|
31
|
+
const res = await fetch(url, {
|
|
32
|
+
method: 'POST',
|
|
33
|
+
headers: { 'content-type': 'application/json' },
|
|
34
|
+
body: JSON.stringify(body),
|
|
35
|
+
});
|
|
36
|
+
if (!res.ok) {
|
|
37
|
+
let detail = '';
|
|
38
|
+
try {
|
|
39
|
+
const j = await res.json();
|
|
40
|
+
detail = (j && j.error) || '';
|
|
41
|
+
} catch (e) {}
|
|
42
|
+
throw new Error('HTTP ' + res.status + ' ' + detail);
|
|
43
|
+
}
|
|
44
|
+
return await res.json();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function stateUrl(apiPath, sessionId) {
|
|
48
|
+
return apiPath + '/state' + (sessionId ? '?sessionId=' + encodeURIComponent(sessionId) : '');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 一个开关按钮。
|
|
53
|
+
* sessionId 来自插槽标准 props —— 必须带上,否则宿主只能猜"当前会话",
|
|
54
|
+
* 多开窗口时会写到别的会话去(表现就是"点了没反应 / 关不掉")。
|
|
55
|
+
*/
|
|
56
|
+
function SwitchButton({ apiPath, sessionId, swKey, iconOn, iconOff, labelOn, labelOff, titleOn, titleOff }) {
|
|
57
|
+
const [value, setValue] = React.useState(true);
|
|
58
|
+
const [ready, setReady] = React.useState(false);
|
|
59
|
+
const [busy, setBusy] = React.useState(false);
|
|
60
|
+
const [note, setNote] = React.useState('');
|
|
61
|
+
|
|
62
|
+
const refresh = React.useCallback(async () => {
|
|
63
|
+
try {
|
|
64
|
+
const s = await getJson(stateUrl(apiPath, sessionId));
|
|
65
|
+
setReady(Boolean(s.ready));
|
|
66
|
+
setValue(s[swKey] !== false);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
setReady(false);
|
|
69
|
+
}
|
|
70
|
+
}, [apiPath, sessionId, swKey]);
|
|
71
|
+
|
|
72
|
+
React.useEffect(() => {
|
|
73
|
+
refresh();
|
|
74
|
+
const id = setInterval(refresh, 5000);
|
|
75
|
+
return () => clearInterval(id);
|
|
76
|
+
}, [refresh]);
|
|
77
|
+
|
|
78
|
+
const onClick = async () => {
|
|
79
|
+
if (busy || !ready) return;
|
|
80
|
+
setBusy(true);
|
|
81
|
+
setNote('');
|
|
82
|
+
const next = !value;
|
|
83
|
+
try {
|
|
84
|
+
await postJson(apiPath + '/set', { sessionId, [swKey]: next });
|
|
85
|
+
setValue(next);
|
|
86
|
+
setNote(next ? '已开启' : '已关闭');
|
|
87
|
+
setTimeout(() => setNote(''), 3000);
|
|
88
|
+
setTimeout(refresh, 300);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
setNote('切换失败:' + ((e && e.message) || String(e)));
|
|
91
|
+
setTimeout(() => setNote(''), 6000);
|
|
92
|
+
} finally {
|
|
93
|
+
setBusy(false);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const icon = !ready ? '⏳' : value ? iconOn : iconOff;
|
|
98
|
+
const label = !ready ? '未就绪' : value ? labelOn : labelOff;
|
|
99
|
+
const title = !ready
|
|
100
|
+
? sessionId
|
|
101
|
+
? '本会话尚未建立,稍后再试'
|
|
102
|
+
: '还没有会话,稍后再试'
|
|
103
|
+
: value
|
|
104
|
+
? titleOn
|
|
105
|
+
: titleOff;
|
|
106
|
+
|
|
107
|
+
return h(
|
|
108
|
+
'div',
|
|
109
|
+
{ style: { display: 'inline-flex', alignItems: 'center', gap: 6 } },
|
|
110
|
+
h(
|
|
111
|
+
'button',
|
|
112
|
+
{
|
|
113
|
+
type: 'button',
|
|
114
|
+
onClick,
|
|
115
|
+
disabled: busy || !ready,
|
|
116
|
+
title,
|
|
117
|
+
style: Object.assign({}, baseBtn, busy || !ready ? { opacity: 0.55 } : null),
|
|
118
|
+
},
|
|
119
|
+
h('span', { style: { fontSize: 14 } }, icon),
|
|
120
|
+
h('span', { style: { fontSize: 12, opacity: 0.85 } }, label),
|
|
121
|
+
),
|
|
122
|
+
note ? h('span', { style: { fontSize: 12, opacity: 0.75, whiteSpace: 'nowrap' } }, note) : null,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function DevModeSwitch(props) {
|
|
127
|
+
const sessionId = props && props.sessionId;
|
|
128
|
+
return h(
|
|
129
|
+
'div',
|
|
130
|
+
{ style: { display: 'inline-flex', alignItems: 'center', gap: 8 } },
|
|
131
|
+
h(SwitchButton, {
|
|
132
|
+
apiPath: '/dsh-session-lock',
|
|
133
|
+
sessionId,
|
|
134
|
+
swKey: 'locked',
|
|
135
|
+
iconOn: '🔒',
|
|
136
|
+
iconOff: '🔓',
|
|
137
|
+
labelOn: '禁止读其它会话',
|
|
138
|
+
labelOff: '允许读其它会话',
|
|
139
|
+
titleOn: '当前会话已锁定:不会读取其它会话的内容。点击允许。',
|
|
140
|
+
titleOff: '当前会话已允许:可以读取其它会话。点击锁定。',
|
|
141
|
+
}),
|
|
142
|
+
h(SwitchButton, {
|
|
143
|
+
apiPath: '/dsh-quiet-run',
|
|
144
|
+
sessionId,
|
|
145
|
+
swKey: 'quiet',
|
|
146
|
+
iconOn: '🔇',
|
|
147
|
+
iconOff: '🔊',
|
|
148
|
+
labelOn: '静默运行',
|
|
149
|
+
labelOff: '可见运行',
|
|
150
|
+
titleOn: '静默运行:启动游戏或编辑器时会挪到屏幕外,不挡你、不抢焦点。点击改为可见。',
|
|
151
|
+
titleOff: '可见运行:允许窗口出现在前台。点击改回静默。',
|
|
152
|
+
}),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
inject: ['slots'],
|
|
158
|
+
apply(ctx) {
|
|
159
|
+
ctx.slots.inject('conversation.composer.dock', () =>
|
|
160
|
+
ctx.slots.register(
|
|
161
|
+
{ name: 'conversation.composer.dock', id: 'dsh-dev-modes', order: 4 },
|
|
162
|
+
DevModeSwitch,
|
|
163
|
+
),
|
|
164
|
+
);
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
});
|
package/cordis.patch.yml
CHANGED
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
- Use subagents for breadth (research, parallel review), then integrate and verify their findings yourself; delegation never replaces verification.
|
|
29
29
|
- Be plain about risk: call out breaking changes, migrations, security-relevant edits, and follow-ups instead of hiding them.
|
|
30
30
|
- Never commit, push, force-push, rewrite history, or change credentials unless the user explicitly asks. Ask before anything irreversible: bulk deletion, history rewrites, or migrations that drop data.
|
|
31
|
+
- Version control: before the first change, check whether the working directory is a Git repository. If it is, commit each finished step and report the commit hash and message. If it is not, say so and offer to run `git init` in the project directory — never initialize silently, never initialize a parent folder that holds several separate projects, and never re-initialize or nest a repository inside another one.
|
|
31
32
|
|
|
32
33
|
Write code, tests, and comments in the repository's conventions; write prose in the user's language.
|
|
33
34
|
- id: agent-instructions
|
|
@@ -171,6 +172,7 @@
|
|
|
171
172
|
- Use subagents for breadth (asset audits, parallel research), then integrate and verify their findings yourself; delegation never replaces verification.
|
|
172
173
|
- Be plain about risk: call out save-format, balance, input, and export-breaking changes, plus what remains unverified.
|
|
173
174
|
- Never commit, push, force-push, rewrite history, or change signing identity or credentials unless the user explicitly asks. Ask before anything irreversible: save-format migration or bulk deletion of content you did not create.
|
|
175
|
+
- Version control: before the first change, check whether the working directory is a Git repository. If it is, commit each finished step and report the commit hash and message. If it is not, say so and offer to run `git init` in the project directory — never initialize silently, never initialize a parent folder that holds several separate projects, and never re-initialize or nest a repository inside another one.
|
|
174
176
|
|
|
175
177
|
Load the game-dev-delivery skill before any game change larger than a one-line fix; it is this mode's checklist. Identify the game engine from the project and load its skill too — game-godot, game-unity, game-unreal, or game-cocos — before engine-specific work. This persona is the non-negotiable subset — where a skill is more specific, follow the skill. Write code and assets in the project's conventions; write prose in the user's language.
|
|
176
178
|
- id: agent-instructions
|
|
@@ -358,7 +360,12 @@
|
|
|
358
360
|
- id: tool-skill
|
|
359
361
|
name: '@deepseek-ai/dsh-tool-skill'
|
|
360
362
|
|
|
361
|
-
#
|
|
363
|
+
# 本包自带的宿主级插件:注册技能、会话隔离开关、静默运行开关。
|
|
364
|
+
# 放在预设声明之外,所以对全部模式生效(含内置预设)。
|
|
362
365
|
- insert:
|
|
363
366
|
- id: dev-modes-skills
|
|
364
367
|
name: 'dsh-dev-modes'
|
|
368
|
+
- id: dev-modes-session-lock
|
|
369
|
+
name: 'dsh-dev-modes/session-lock'
|
|
370
|
+
- id: dev-modes-quiet-run
|
|
371
|
+
name: 'dsh-dev-modes/quiet-run'
|
package/package.json
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-dev-modes",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "Three DeepSeek Harness Agent presets in one bundle: software development, game development, and read-only discussion, with the matching delivery and engine workflows bundled as skills.",
|
|
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",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./index.js",
|
|
10
|
+
"./client": "./client.js",
|
|
11
|
+
"./session-lock": "./session-lock/index.js",
|
|
12
|
+
"./quiet-run": "./quiet-run/index.js",
|
|
10
13
|
"./locale/*.json": "./locale/*.json",
|
|
11
14
|
"./icon": "./icon.svg"
|
|
12
15
|
},
|
|
13
16
|
"files": [
|
|
14
17
|
"index.js",
|
|
18
|
+
"client.js",
|
|
19
|
+
"session-lock",
|
|
20
|
+
"quiet-run",
|
|
15
21
|
"cordis.patch.yml",
|
|
16
22
|
"skills",
|
|
17
23
|
"locale/*.json",
|
|
@@ -23,7 +29,9 @@
|
|
|
23
29
|
"deepseek-harness",
|
|
24
30
|
"agent-preset",
|
|
25
31
|
"software-development",
|
|
26
|
-
"game-development"
|
|
32
|
+
"game-development",
|
|
33
|
+
"quiet",
|
|
34
|
+
"session-isolation"
|
|
27
35
|
],
|
|
28
36
|
"engines": {
|
|
29
37
|
"dsh": ">=0.2.1-rc.2"
|
|
@@ -31,6 +39,13 @@
|
|
|
31
39
|
"dsh": {
|
|
32
40
|
"bundle": {
|
|
33
41
|
"patch": "./cordis.patch.yml"
|
|
42
|
+
},
|
|
43
|
+
"client": {
|
|
44
|
+
"platform": "web",
|
|
45
|
+
"immediately": true,
|
|
46
|
+
"inject": [
|
|
47
|
+
"@deepseek-ai/dsh-client-ui-conversation"
|
|
48
|
+
]
|
|
34
49
|
}
|
|
35
50
|
}
|
|
36
51
|
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 每个会话独立的「静默运行」开关(并入 dsh-dev-modes)。
|
|
7
|
+
*
|
|
8
|
+
* 静默(默认):启动任何 GUI 都必须挪到屏幕外或改用无窗口方式,不挡用户、不抢焦点。
|
|
9
|
+
* 可见:用户允许窗口出现在前台。
|
|
10
|
+
*
|
|
11
|
+
* 存储:$DSH_HOME/storages/dsh-quiet-run/state.json
|
|
12
|
+
* { "<sessionId>": { "quiet": true, "updatedAt": 1700000000000 } }
|
|
13
|
+
*
|
|
14
|
+
* 目录名沿用独立插件时期的名字,升级后原有设置继续生效。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const HOME = process.env.DSH_HOME || path.join(os.homedir(), '.dsh');
|
|
18
|
+
const STORE_DIR = path.join(HOME, 'storages', 'dsh-quiet-run');
|
|
19
|
+
const STORE_FILE = path.join(STORE_DIR, 'state.json');
|
|
20
|
+
|
|
21
|
+
const QUIET_RULE = [
|
|
22
|
+
'## 静默运行(本会话已开启)',
|
|
23
|
+
'',
|
|
24
|
+
'用户可能正在看视频或打字。启动任何 GUI 都不得挡住他、不得抢走焦点。',
|
|
25
|
+
'',
|
|
26
|
+
'- **优先无窗口**:构建、测试、lint、类型检查、逻辑验证一律用无界面方式(headless / batchmode / 纯命令行)',
|
|
27
|
+
'- **必须开窗口**(游戏窗口、编辑器、浏览器预览)时:启动后立刻移到屏幕外(例如坐标 10000,10000),或用最小化启动;**禁止全屏**',
|
|
28
|
+
'- 不要主动激活、置顶或聚焦任何新窗口;不要调用会抓取全屏的截图方式',
|
|
29
|
+
'- 需要用户用肉眼确认效果时,先把窗口移回可见区域之前**先问他**,得到同意再显示',
|
|
30
|
+
'- 预计超过 1 分钟的构建或测试(首次导入、cook、导出、编译大项目)先说明再跑',
|
|
31
|
+
'',
|
|
32
|
+
'如果你无法保证窗口不会出现在前台,就改用无窗口方式,或先问用户。',
|
|
33
|
+
].join('\n');
|
|
34
|
+
|
|
35
|
+
const VISIBLE_RULE = [
|
|
36
|
+
'## 可见运行(本会话已开启)',
|
|
37
|
+
'',
|
|
38
|
+
'用户允许窗口出现在前台,可以正常启动并显示 GUI。',
|
|
39
|
+
'',
|
|
40
|
+
'仍然不要主动抢焦点:窗口按需启动即可,不必刻意隐藏。',
|
|
41
|
+
].join('\n');
|
|
42
|
+
|
|
43
|
+
/** sessionId -> { quiet: boolean, updatedAt: number } */
|
|
44
|
+
let states = {};
|
|
45
|
+
const agents = new Map();
|
|
46
|
+
let activeSessionId = null;
|
|
47
|
+
|
|
48
|
+
const diag = {
|
|
49
|
+
pluginLoaded: true,
|
|
50
|
+
createdEvents: 0,
|
|
51
|
+
listedAtStartup: 0,
|
|
52
|
+
errors: [],
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
function noteError(where, err) {
|
|
56
|
+
if (diag.errors.length > 10) diag.errors.shift();
|
|
57
|
+
diag.errors.push(where + ': ' + String((err && err.message) || err));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function ensureDir() {
|
|
61
|
+
try {
|
|
62
|
+
fs.mkdirSync(STORE_DIR, { recursive: true });
|
|
63
|
+
} catch {
|
|
64
|
+
/* ignore */
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function load() {
|
|
69
|
+
try {
|
|
70
|
+
const parsed = JSON.parse(fs.readFileSync(STORE_FILE, 'utf8'));
|
|
71
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) states = parsed;
|
|
72
|
+
} catch {
|
|
73
|
+
states = {};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function persist() {
|
|
78
|
+
try {
|
|
79
|
+
ensureDir();
|
|
80
|
+
fs.writeFileSync(STORE_FILE, JSON.stringify(states, null, 2), 'utf8');
|
|
81
|
+
return true;
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 默认静默:没有记录的会话按"别挡我"处理。 */
|
|
88
|
+
function isQuiet(sessionId) {
|
|
89
|
+
if (!sessionId) return true;
|
|
90
|
+
const entry = states[sessionId];
|
|
91
|
+
if (!entry || typeof entry.quiet !== 'boolean') return true;
|
|
92
|
+
return entry.quiet;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 提示词里用这个:每次都从磁盘重读。
|
|
97
|
+
* 会话作用域里已注册的变量在插件热重载后不会被新实例覆盖,旧闭包仍在求值,
|
|
98
|
+
* 让双方都读磁盘就不会出现"界面显示已切换、提示词没变"。
|
|
99
|
+
*/
|
|
100
|
+
function isQuietFresh(sessionId) {
|
|
101
|
+
load();
|
|
102
|
+
return isQuiet(sessionId);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function setQuiet(sessionId, quiet) {
|
|
106
|
+
if (!sessionId) return false;
|
|
107
|
+
states[sessionId] = { quiet: Boolean(quiet), updatedAt: Date.now() };
|
|
108
|
+
return persist();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function sessionIdOf(agent) {
|
|
112
|
+
if (!agent) return null;
|
|
113
|
+
const tries = [
|
|
114
|
+
() => agent.session?.id,
|
|
115
|
+
() => agent.session?.sessionId,
|
|
116
|
+
() => agent.sessionId,
|
|
117
|
+
() => agent.session?.session?.id,
|
|
118
|
+
];
|
|
119
|
+
for (const t of tries) {
|
|
120
|
+
try {
|
|
121
|
+
const v = t();
|
|
122
|
+
if (typeof v === 'string' && v) return v;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
noteError('sessionIdOf', err);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function attachPrompt(agent, sessionId) {
|
|
131
|
+
try {
|
|
132
|
+
agent.ctx.systemPrompt.variable('quiet_run_rule', () =>
|
|
133
|
+
isQuietFresh(sessionId) ? QUIET_RULE : VISIBLE_RULE,
|
|
134
|
+
);
|
|
135
|
+
agent.ctx.systemPrompt.section({
|
|
136
|
+
name: 'quiet-run',
|
|
137
|
+
order: 901,
|
|
138
|
+
text: '{{quiet_run_rule}}',
|
|
139
|
+
});
|
|
140
|
+
return true;
|
|
141
|
+
} catch (err) {
|
|
142
|
+
if (!diag.attachPromptNoted) {
|
|
143
|
+
diag.attachPromptNoted = true;
|
|
144
|
+
noteError('attachPrompt', err);
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function readJsonBody(req) {
|
|
151
|
+
return new Promise((resolve) => {
|
|
152
|
+
let data = '';
|
|
153
|
+
req.on('data', (chunk) => {
|
|
154
|
+
data += chunk;
|
|
155
|
+
if (data.length > 1e5) req.destroy();
|
|
156
|
+
});
|
|
157
|
+
req.on('end', () => {
|
|
158
|
+
try {
|
|
159
|
+
resolve(data ? JSON.parse(data) : {});
|
|
160
|
+
} catch {
|
|
161
|
+
resolve({});
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
req.on('error', () => resolve({}));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function apply(ctx) {
|
|
169
|
+
load();
|
|
170
|
+
|
|
171
|
+
const register = (agent) => {
|
|
172
|
+
try {
|
|
173
|
+
const sessionId = sessionIdOf(agent);
|
|
174
|
+
if (!sessionId) return;
|
|
175
|
+
agents.set(sessionId, agent);
|
|
176
|
+
activeSessionId = sessionId;
|
|
177
|
+
attachPrompt(agent, sessionId);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
noteError('register', err);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
ctx.inject(['agents'], (c) => {
|
|
184
|
+
try {
|
|
185
|
+
const live = c.agents.list();
|
|
186
|
+
diag.listedAtStartup = Array.isArray(live) ? live.length : 0;
|
|
187
|
+
for (const item of live || []) register(item?.agent ?? item);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
noteError('agents.list', err);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
ctx.effect(() => {
|
|
194
|
+
const off = ctx.on('agent/created', (payload) => {
|
|
195
|
+
diag.createdEvents += 1;
|
|
196
|
+
register(payload?.agent ?? payload);
|
|
197
|
+
});
|
|
198
|
+
return () => {
|
|
199
|
+
if (typeof off === 'function') off();
|
|
200
|
+
agents.clear();
|
|
201
|
+
};
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// 用户在某会话里发消息时把"当前会话"锁定为用户正在用的那个。
|
|
205
|
+
ctx.effect(() => {
|
|
206
|
+
const off = ctx.on('api-session/activity', (sessionId) => {
|
|
207
|
+
if (typeof sessionId === 'string' && sessionId) activeSessionId = sessionId;
|
|
208
|
+
});
|
|
209
|
+
return () => {
|
|
210
|
+
if (typeof off === 'function') off();
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
ctx.inject(['webServer'], (c) => {
|
|
215
|
+
c.effect(() => {
|
|
216
|
+
const sendJson = (res, status, obj) => {
|
|
217
|
+
res.writeHead(status, {
|
|
218
|
+
'content-type': 'application/json; charset=utf-8',
|
|
219
|
+
'cache-control': 'no-store',
|
|
220
|
+
});
|
|
221
|
+
res.end(JSON.stringify(obj));
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const state = c.webServer.register({
|
|
225
|
+
kind: 'exact',
|
|
226
|
+
path: '/dsh-quiet-run/state',
|
|
227
|
+
handler: async (req, res) => {
|
|
228
|
+
const url = new URL(req.url || '/', 'http://127.0.0.1');
|
|
229
|
+
const asked = url.searchParams.get('sessionId');
|
|
230
|
+
const sessionId = asked || activeSessionId;
|
|
231
|
+
if (!sessionId) {
|
|
232
|
+
sendJson(res, 200, { sessionId: null, quiet: true, ready: false, diag });
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
sendJson(res, 200, {
|
|
236
|
+
sessionId,
|
|
237
|
+
quiet: isQuiet(sessionId),
|
|
238
|
+
ready: true,
|
|
239
|
+
tracked: agents.has(sessionId),
|
|
240
|
+
diag,
|
|
241
|
+
});
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
const toggle = c.webServer.register({
|
|
246
|
+
kind: 'exact',
|
|
247
|
+
path: '/dsh-quiet-run/set',
|
|
248
|
+
handler: async (req, res) => {
|
|
249
|
+
if (req.method !== 'POST') {
|
|
250
|
+
sendJson(res, 405, { error: 'POST only' });
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const body = await readJsonBody(req);
|
|
254
|
+
const sessionId = body.sessionId || activeSessionId;
|
|
255
|
+
if (!sessionId) {
|
|
256
|
+
sendJson(res, 400, { error: 'no active session yet' });
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const quiet = Boolean(body.quiet);
|
|
260
|
+
const saved = setQuiet(sessionId, quiet);
|
|
261
|
+
sendJson(res, 200, { sessionId, quiet, saved });
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
return () => {
|
|
266
|
+
state();
|
|
267
|
+
toggle();
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 每个会话独立的「会话隔离」开关(并入 dsh-dev-modes)。
|
|
7
|
+
*
|
|
8
|
+
* 锁定(默认):该会话的系统提示禁止读取其它会话的文件。
|
|
9
|
+
* 开放:不额外注入规则,回到全局 AGENTS.md 的约束。
|
|
10
|
+
*
|
|
11
|
+
* 存储:$DSH_HOME/storages/dsh-session-lock/policies.json
|
|
12
|
+
* { "<sessionId>": { "locked": true, "updatedAt": 1700000000000 } }
|
|
13
|
+
*
|
|
14
|
+
* 目录名沿用独立插件时期的名字,升级后原有设置继续生效。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const HOME = process.env.DSH_HOME || path.join(os.homedir(), '.dsh');
|
|
18
|
+
const STORE_DIR = path.join(HOME, 'storages', 'dsh-session-lock');
|
|
19
|
+
const STORE_FILE = path.join(STORE_DIR, 'policies.json');
|
|
20
|
+
|
|
21
|
+
const LOCKED_RULE = [
|
|
22
|
+
'## 会话隔离(本会话已锁定)',
|
|
23
|
+
'',
|
|
24
|
+
'本会话被用户设置为**隔离模式**。禁止读取其他会话的任何内容:',
|
|
25
|
+
'',
|
|
26
|
+
'- 不要读取、列举、搜索 `~/.dsh/sessions/` 或 `~/.dsh/storages/session_projcache/sessions/` 下的文件',
|
|
27
|
+
'- 不要用 read / grep / glob / pwsh / read_image 等任何工具去翻看历史会话',
|
|
28
|
+
'- 不要为了补充上下文而自行查找过去的对话',
|
|
29
|
+
'',
|
|
30
|
+
'如果任务确实需要历史信息,先询问用户,由用户决定是否提供。',
|
|
31
|
+
].join('\n');
|
|
32
|
+
|
|
33
|
+
/** sessionId -> { locked: boolean, updatedAt: number } */
|
|
34
|
+
let policies = {};
|
|
35
|
+
/** sessionId -> Agent,用于按会话注册提示词 */
|
|
36
|
+
const agents = new Map();
|
|
37
|
+
let activeSessionId = null;
|
|
38
|
+
|
|
39
|
+
const diag = {
|
|
40
|
+
pluginLoaded: true,
|
|
41
|
+
createdEvents: 0,
|
|
42
|
+
listedAtStartup: 0,
|
|
43
|
+
lastSessionProbe: null,
|
|
44
|
+
errors: [],
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function noteError(where, err) {
|
|
48
|
+
if (diag.errors.length > 10) diag.errors.shift();
|
|
49
|
+
diag.errors.push(where + ': ' + String((err && err.message) || err));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function ensureDir() {
|
|
53
|
+
try {
|
|
54
|
+
fs.mkdirSync(STORE_DIR, { recursive: true });
|
|
55
|
+
} catch {
|
|
56
|
+
/* ignore */
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function load() {
|
|
61
|
+
try {
|
|
62
|
+
const parsed = JSON.parse(fs.readFileSync(STORE_FILE, 'utf8'));
|
|
63
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) policies = parsed;
|
|
64
|
+
} catch {
|
|
65
|
+
policies = {};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function persist() {
|
|
70
|
+
try {
|
|
71
|
+
ensureDir();
|
|
72
|
+
fs.writeFileSync(STORE_FILE, JSON.stringify(policies, null, 2), 'utf8');
|
|
73
|
+
return true;
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 默认锁定:没有记录的会话不得读取其它会话。 */
|
|
80
|
+
function isLocked(sessionId) {
|
|
81
|
+
if (!sessionId) return true;
|
|
82
|
+
const entry = policies[sessionId];
|
|
83
|
+
if (!entry || typeof entry.locked !== 'boolean') return true;
|
|
84
|
+
return entry.locked;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 提示词里用这个:每次都从磁盘重读。
|
|
89
|
+
* 会话作用域里已注册的变量在插件热重载后不会被新实例覆盖,旧闭包仍在求值,
|
|
90
|
+
* 让双方都读磁盘就不会出现"界面显示已切换、提示词没变"。
|
|
91
|
+
*/
|
|
92
|
+
function isLockedFresh(sessionId) {
|
|
93
|
+
load();
|
|
94
|
+
return isLocked(sessionId);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function setLocked(sessionId, locked) {
|
|
98
|
+
if (!sessionId) return false;
|
|
99
|
+
policies[sessionId] = { locked: Boolean(locked), updatedAt: Date.now() };
|
|
100
|
+
return persist();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function sessionIdOf(agent) {
|
|
104
|
+
if (!agent) return null;
|
|
105
|
+
const tries = [
|
|
106
|
+
() => agent.session?.id,
|
|
107
|
+
() => agent.session?.sessionId,
|
|
108
|
+
() => agent.sessionId,
|
|
109
|
+
() => agent.session?.session?.id,
|
|
110
|
+
];
|
|
111
|
+
for (const t of tries) {
|
|
112
|
+
try {
|
|
113
|
+
const v = t();
|
|
114
|
+
if (typeof v === 'string' && v) {
|
|
115
|
+
diag.lastSessionProbe = v;
|
|
116
|
+
return v;
|
|
117
|
+
}
|
|
118
|
+
} catch (err) {
|
|
119
|
+
noteError('sessionIdOf', err);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function attachPrompt(agent, sessionId) {
|
|
126
|
+
try {
|
|
127
|
+
agent.ctx.systemPrompt.variable('session_lock_rule', () =>
|
|
128
|
+
isLockedFresh(sessionId) ? LOCKED_RULE : '',
|
|
129
|
+
);
|
|
130
|
+
agent.ctx.systemPrompt.section({
|
|
131
|
+
name: 'session-lock',
|
|
132
|
+
order: 900,
|
|
133
|
+
text: '{{session_lock_rule}}',
|
|
134
|
+
});
|
|
135
|
+
return true;
|
|
136
|
+
} catch (err) {
|
|
137
|
+
if (!diag.attachPromptNoted) {
|
|
138
|
+
diag.attachPromptNoted = true;
|
|
139
|
+
noteError('attachPrompt', err);
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function readJsonBody(req) {
|
|
146
|
+
return new Promise((resolve) => {
|
|
147
|
+
let data = '';
|
|
148
|
+
req.on('data', (chunk) => {
|
|
149
|
+
data += chunk;
|
|
150
|
+
if (data.length > 1e5) req.destroy();
|
|
151
|
+
});
|
|
152
|
+
req.on('end', () => {
|
|
153
|
+
try {
|
|
154
|
+
resolve(data ? JSON.parse(data) : {});
|
|
155
|
+
} catch {
|
|
156
|
+
resolve({});
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
req.on('error', () => resolve({}));
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function apply(ctx) {
|
|
164
|
+
load();
|
|
165
|
+
|
|
166
|
+
const register = (agent) => {
|
|
167
|
+
try {
|
|
168
|
+
const sessionId = sessionIdOf(agent);
|
|
169
|
+
if (!sessionId) return;
|
|
170
|
+
agents.set(sessionId, agent);
|
|
171
|
+
activeSessionId = sessionId;
|
|
172
|
+
attachPrompt(agent, sessionId);
|
|
173
|
+
} catch (err) {
|
|
174
|
+
noteError('register', err);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
ctx.inject(['agents'], (c) => {
|
|
179
|
+
try {
|
|
180
|
+
const live = c.agents.list();
|
|
181
|
+
diag.listedAtStartup = Array.isArray(live) ? live.length : 0;
|
|
182
|
+
for (const item of live || []) register(item?.agent ?? item);
|
|
183
|
+
} catch (err) {
|
|
184
|
+
noteError('agents.list', err);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
ctx.effect(() => {
|
|
189
|
+
const off = ctx.on('agent/created', (payload) => {
|
|
190
|
+
diag.createdEvents += 1;
|
|
191
|
+
register(payload?.agent ?? payload);
|
|
192
|
+
});
|
|
193
|
+
return () => {
|
|
194
|
+
if (typeof off === 'function') off();
|
|
195
|
+
agents.clear();
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
ctx.effect(() => {
|
|
200
|
+
const off = ctx.on('api-session/activity', (sessionId) => {
|
|
201
|
+
if (typeof sessionId === 'string' && sessionId) activeSessionId = sessionId;
|
|
202
|
+
});
|
|
203
|
+
return () => {
|
|
204
|
+
if (typeof off === 'function') off();
|
|
205
|
+
};
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
ctx.inject(['webServer'], (c) => {
|
|
209
|
+
c.effect(() => {
|
|
210
|
+
const sendJson = (res, status, obj) => {
|
|
211
|
+
res.writeHead(status, {
|
|
212
|
+
'content-type': 'application/json; charset=utf-8',
|
|
213
|
+
'cache-control': 'no-store',
|
|
214
|
+
});
|
|
215
|
+
res.end(JSON.stringify(obj));
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const state = c.webServer.register({
|
|
219
|
+
kind: 'exact',
|
|
220
|
+
path: '/dsh-session-lock/state',
|
|
221
|
+
handler: async (req, res) => {
|
|
222
|
+
const url = new URL(req.url || '/', 'http://127.0.0.1');
|
|
223
|
+
const asked = url.searchParams.get('sessionId');
|
|
224
|
+
const sessionId = asked || activeSessionId;
|
|
225
|
+
if (!sessionId) {
|
|
226
|
+
sendJson(res, 200, { sessionId: null, locked: true, ready: false, diag });
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
sendJson(res, 200, {
|
|
230
|
+
sessionId,
|
|
231
|
+
locked: isLocked(sessionId),
|
|
232
|
+
ready: true,
|
|
233
|
+
tracked: agents.has(sessionId),
|
|
234
|
+
diag,
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const toggle = c.webServer.register({
|
|
240
|
+
kind: 'exact',
|
|
241
|
+
path: '/dsh-session-lock/set',
|
|
242
|
+
handler: async (req, res) => {
|
|
243
|
+
if (req.method !== 'POST') {
|
|
244
|
+
sendJson(res, 405, { error: 'POST only' });
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const body = await readJsonBody(req);
|
|
248
|
+
const sessionId = body.sessionId || activeSessionId;
|
|
249
|
+
if (!sessionId) {
|
|
250
|
+
sendJson(res, 400, { error: 'no active session yet' });
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const locked = Boolean(body.locked);
|
|
254
|
+
const saved = setLocked(sessionId, locked);
|
|
255
|
+
sendJson(res, 200, { sessionId, locked, saved });
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
return () => {
|
|
260
|
+
state();
|
|
261
|
+
toggle();
|
|
262
|
+
};
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
}
|