dsh-dev-modes 0.0.0-stage → 1.1.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/LICENSE +21 -0
- package/README.md +44 -2
- package/client.js +171 -0
- package/cordis.patch.yml +371 -0
- package/icon.svg +5 -0
- package/index.js +65 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +48 -3
- package/quiet-run/index.js +271 -0
- package/session-lock/index.js +265 -0
- package/skills/game-cocos/SKILL.md +46 -0
- package/skills/game-dev-delivery/SKILL.md +81 -0
- package/skills/game-godot/SKILL.md +38 -0
- package/skills/game-unity/SKILL.md +59 -0
- package/skills/game-unreal/SKILL.md +62 -0
- package/skills/software-delivery/SKILL.md +58 -0
package/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
const SKILLS_DIR = join(dirname(fileURLToPath(import.meta.url)), 'skills');
|
|
6
|
+
|
|
7
|
+
function parseSkill(file) {
|
|
8
|
+
const text = readFileSync(file, 'utf8');
|
|
9
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/u.exec(text);
|
|
10
|
+
if (match === null) return undefined;
|
|
11
|
+
const fields = {};
|
|
12
|
+
for (const line of match[1].split(/\r?\n/u)) {
|
|
13
|
+
const field = /^([a-zA-Z][a-zA-Z0-9_-]*):\s*(.*)$/u.exec(line);
|
|
14
|
+
if (field === null) continue;
|
|
15
|
+
let value = field[2].trim();
|
|
16
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
17
|
+
value = value.slice(1, -1);
|
|
18
|
+
}
|
|
19
|
+
fields[field[1]] = value;
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
name: fields.name,
|
|
23
|
+
description: fields.description,
|
|
24
|
+
whenToUse: fields.whenToUse,
|
|
25
|
+
content: match[2],
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function registerBundledSkills(skills) {
|
|
30
|
+
let entries;
|
|
31
|
+
try {
|
|
32
|
+
entries = readdirSync(SKILLS_DIR, { withFileTypes: true });
|
|
33
|
+
} catch {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
for (const entry of entries) {
|
|
37
|
+
if (!entry.isDirectory()) continue;
|
|
38
|
+
const file = join(SKILLS_DIR, entry.name, 'SKILL.md');
|
|
39
|
+
if (!existsSync(file)) continue;
|
|
40
|
+
try {
|
|
41
|
+
const skill = parseSkill(file);
|
|
42
|
+
if (skill === undefined || !skill.name || !skill.description) continue;
|
|
43
|
+
skills.register({
|
|
44
|
+
name: skill.name,
|
|
45
|
+
description: skill.description,
|
|
46
|
+
...(skill.whenToUse ? { whenToUse: skill.whenToUse } : {}),
|
|
47
|
+
content: skill.content,
|
|
48
|
+
source: 'bundled',
|
|
49
|
+
provider: 'dev-modes',
|
|
50
|
+
});
|
|
51
|
+
} catch {
|
|
52
|
+
// A malformed bundled skill must not break the bundle.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function apply(ctx) {
|
|
58
|
+
ctx.inject(['skills'], (scoped) => {
|
|
59
|
+
try {
|
|
60
|
+
registerBundledSkills(scoped.skills);
|
|
61
|
+
} catch {
|
|
62
|
+
// Registration is best effort; presets keep working without skills.
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Development modes",
|
|
4
|
+
"description": "Three Agent presets in one install: Software development (read the repository, plan, implement in small steps, verify with the project's own tooling), Game development (prototype first, verify by running it; Godot, Unity, Unreal, and Cocos workflows included), and Read-only discussion (read and search only, no file writes or commands). Ships the delivery and engine workflow skills."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-dev-modes",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"type": "module",
|
|
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
|
+
"author": "qingfengshangnanzhi",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.js",
|
|
10
|
+
"./client": "./client.js",
|
|
11
|
+
"./session-lock": "./session-lock/index.js",
|
|
12
|
+
"./quiet-run": "./quiet-run/index.js",
|
|
13
|
+
"./locale/*.json": "./locale/*.json",
|
|
14
|
+
"./icon": "./icon.svg"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"index.js",
|
|
18
|
+
"client.js",
|
|
19
|
+
"session-lock",
|
|
20
|
+
"quiet-run",
|
|
21
|
+
"cordis.patch.yml",
|
|
22
|
+
"skills",
|
|
23
|
+
"locale/*.json",
|
|
24
|
+
"icon.svg",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"keywords": [
|
|
28
|
+
"dsh-plugin",
|
|
29
|
+
"deepseek-harness",
|
|
30
|
+
"agent-preset",
|
|
31
|
+
"software-development",
|
|
32
|
+
"game-development",
|
|
33
|
+
"quiet",
|
|
34
|
+
"session-isolation"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"dsh": ">=0.2.1-rc.2"
|
|
38
|
+
},
|
|
39
|
+
"dsh": {
|
|
40
|
+
"bundle": {
|
|
41
|
+
"patch": "./cordis.patch.yml"
|
|
42
|
+
},
|
|
43
|
+
"client": {
|
|
44
|
+
"platform": "web",
|
|
45
|
+
"immediately": true,
|
|
46
|
+
"inject": [
|
|
47
|
+
"@deepseek-ai/dsh-client-ui-conversation"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
6
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
|
+
}
|