@6starlong/dsh-disk-cleanup 0.0.0-stage → 0.4.4
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 +67 -2
- package/cordis.patch.yml +24 -0
- package/icon.svg +11 -0
- package/lib/acl.js +122 -0
- package/lib/attachments.js +310 -0
- package/lib/cleanup.js +207 -0
- package/lib/client.js +194 -0
- package/lib/config.js +117 -0
- package/lib/index.d.ts +33 -0
- package/lib/index.js +462 -0
- package/lib/prompt.js +23 -0
- package/lib/schema.js +125 -0
- package/lib/scratch.js +57 -0
- package/lib/shell-env.js +90 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +86 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-disk-cleanup —— 给临时文件一个专用目录。
|
|
3
|
+
*
|
|
4
|
+
* 四条机制:
|
|
5
|
+
* - shell 环境注入:在 pwsh / bash 命令串前把 TMP / TEMP / TMPDIR 指向 scratch,
|
|
6
|
+
* 让脚本里的临时文件默认落在工作区之外。
|
|
7
|
+
* - 权限补齐:会话一创建就给 scratch 追加可写根(文件工具)和访问控制
|
|
8
|
+
* 列表授权(受限子进程),不依赖 shell 命令是否执行。
|
|
9
|
+
* - 系统提示:说明临时文件写 scratch、用户文件留在工作区。
|
|
10
|
+
* - 清理:插件加载时扫一遍 scratch、请求图片缓存与无主原件,清掉过期条目和空目录,之后定时检查。
|
|
11
|
+
* @module dsh-disk-cleanup
|
|
12
|
+
*/
|
|
13
|
+
import { Config, resolveConfig } from './config.js'
|
|
14
|
+
import { CLEANUP_INTERVAL_MS, cleanEmptyDirs, cleanScratch, throttled } from './cleanup.js'
|
|
15
|
+
import { cleanAttachmentLeftovers } from './attachments.js'
|
|
16
|
+
import { dshHome, ensureDir, scratchDirFor, sharedScratchDir } from './scratch.js'
|
|
17
|
+
import { injectShellTemp } from './shell-env.js'
|
|
18
|
+
import { grantProcessWrite, revokeAll } from './acl.js'
|
|
19
|
+
import { scratchPromptText } from './prompt.js'
|
|
20
|
+
|
|
21
|
+
/** Cordis 插件身份(与包名一致)。 */
|
|
22
|
+
export const name = '@6starlong/dsh-disk-cleanup'
|
|
23
|
+
/** 必需服务:工具注册表与 agent 注册表。 */
|
|
24
|
+
export const inject = ['tools', 'agents']
|
|
25
|
+
|
|
26
|
+
/** 插件配置 schema:宿主用它渲染设置页表单。 */
|
|
27
|
+
export { Config }
|
|
28
|
+
|
|
29
|
+
/** agent → 已装好包装的工具名。用来避免重复注册,也用来记下「当时还没加载」的工具。 */
|
|
30
|
+
const installed = new WeakMap()
|
|
31
|
+
|
|
32
|
+
/** 已替换过 execute 的活工具对象;防止 tools/change 重复包裹导致 execute 层层嵌套。 */
|
|
33
|
+
const wrappedTools = new WeakSet()
|
|
34
|
+
|
|
35
|
+
/** 正在安装中的 agent。注册包装会触发 tools/change,不挡住就会重入、重复注册同一个工具名并抛错。 */
|
|
36
|
+
const installing = new WeakSet()
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 插件入口。
|
|
40
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx - 宿主上下文。
|
|
41
|
+
* @param {unknown} config - cordis.patch.yml 里的 config。
|
|
42
|
+
*/
|
|
43
|
+
export function apply(ctx, config) {
|
|
44
|
+
const cfg = resolveConfig(config)
|
|
45
|
+
if (!cfg.enabled) return
|
|
46
|
+
|
|
47
|
+
/** 可选服务:拿不到就退化(只保留能用的那部分),绝不因此报错。 */
|
|
48
|
+
let sandboxPolicy
|
|
49
|
+
ctx.inject(['sandboxPolicy'], (child) => { sandboxPolicy = child.sandboxPolicy })
|
|
50
|
+
|
|
51
|
+
// 设置页由本插件自己的卡片渲染(客户端半边注册在 plugins.bundle.config 槽),
|
|
52
|
+
// 关掉宿主按 schema 自动生成的那份,免得同一份配置出现两套控件。
|
|
53
|
+
ctx.inject(['settings'], (child) => {
|
|
54
|
+
try {
|
|
55
|
+
child.effect(() => child.settings.configure({ auto: false }, ctx.fiber), 'dsh-disk-cleanup 设置卡片')
|
|
56
|
+
} catch { /* 宿主没有设置服务时保持默认,不影响其余机制 */ }
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 清理入口。插件加载时跑一次,之后每 CLEANUP_INTERVAL_MS 最多再跑一次;
|
|
62
|
+
* 没有常驻定时器,进程重启等于重新起算;附件首轮只多写一个标记文件。
|
|
63
|
+
* @returns {Promise<void>}
|
|
64
|
+
*/
|
|
65
|
+
const runCleanup = throttled(CLEANUP_INTERVAL_MS)
|
|
66
|
+
/**
|
|
67
|
+
* 执行一次清理(受节流控制)。
|
|
68
|
+
*
|
|
69
|
+
* 只清共享 scratch:配置里自己指了别处的用户,目录里的东西未必可丢,不替他决定。
|
|
70
|
+
* 附件清理也只受总开关控制:请求图片缓存按保留期清,无主原件先比对全部会话。
|
|
71
|
+
* DSH home 下一级的空目录与它们无关,单独清。
|
|
72
|
+
* @returns {Promise<void>}
|
|
73
|
+
*/
|
|
74
|
+
async function cleanup() {
|
|
75
|
+
if (!cfg.autoCleanup) return
|
|
76
|
+
try {
|
|
77
|
+
await runCleanup(async () => {
|
|
78
|
+
if (cfg.scratchDir === '') {
|
|
79
|
+
const scratch = sharedScratchDir()
|
|
80
|
+
await cleanScratch({ dir: scratch, maxAgeDays: cfg.cleanupMaxAgeDays })
|
|
81
|
+
await cleanEmptyDirs({ dir: scratch, recursive: true })
|
|
82
|
+
}
|
|
83
|
+
const home = dshHome()
|
|
84
|
+
const attachments = await cleanAttachmentLeftovers({
|
|
85
|
+
home,
|
|
86
|
+
maxAgeDays: cfg.cleanupMaxAgeDays,
|
|
87
|
+
})
|
|
88
|
+
if (attachments.first && attachments.objects.candidates > 0) {
|
|
89
|
+
console.info(
|
|
90
|
+
`[dsh-disk-cleanup] 首次扫描发现 ${attachments.objects.candidates} 个无主原件,本次只记录不删除。`
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
await cleanEmptyDirs({ dir: home })
|
|
94
|
+
})
|
|
95
|
+
} catch {
|
|
96
|
+
/* 清理只是收尾,失败了不能连带插件加载失败 */
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 会话 → 已完成的授权。键里带模式与目录,模式一变就重新授权。
|
|
102
|
+
* @type {WeakMap<object, {key: string, promise: Promise<boolean>}>}
|
|
103
|
+
*/
|
|
104
|
+
const prepared = new WeakMap()
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 会话 → 实际生效的落点。退路可能改掉 scratch 位置,系统提示要跟着改口。
|
|
108
|
+
* @type {WeakMap<object, object>}
|
|
109
|
+
*/
|
|
110
|
+
const effective = new WeakMap()
|
|
111
|
+
|
|
112
|
+
/** session → 已登记过的可写目录,避免同一目录重复注册。 */
|
|
113
|
+
const writableRoots = new WeakMap()
|
|
114
|
+
|
|
115
|
+
/** registerWritableRoot 返回的撤销函数,插件卸载时统一撤销。 */
|
|
116
|
+
const rootDisposers = new Set()
|
|
117
|
+
|
|
118
|
+
/** agent → 已经登记过的目录,避免同一 agent 重复登记。 */
|
|
119
|
+
const agentRoots = new WeakMap()
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 当前驱动链里的会话;工具执行上下文拿不到 agent 时用它兜底。
|
|
123
|
+
* @returns {object | undefined} 会话对象。
|
|
124
|
+
*/
|
|
125
|
+
function currentSession() {
|
|
126
|
+
try {
|
|
127
|
+
return ctx.agents?.currentInitiator?.()?.session
|
|
128
|
+
} catch {
|
|
129
|
+
return undefined
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 取一个可选服务:优先用注入回调拿到的,其次直接问上下文。
|
|
135
|
+
* 注入回调是异步触发的,落点计算可能早于它,所以这里必须能自己取到。
|
|
136
|
+
* @param {string} serviceName - 服务名。
|
|
137
|
+
* @param {unknown} injected - 注入回调里拿到的实例。
|
|
138
|
+
* @returns {unknown} 服务实例,取不到时为 undefined。
|
|
139
|
+
*/
|
|
140
|
+
function service(serviceName, injected) {
|
|
141
|
+
if (injected !== undefined) return injected
|
|
142
|
+
try {
|
|
143
|
+
return typeof ctx.get === 'function' ? ctx.get(serviceName) : undefined
|
|
144
|
+
} catch {
|
|
145
|
+
return undefined
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 读一个会话解析后的沙箱策略。
|
|
151
|
+
* @param {object | undefined} session - 会话对象。
|
|
152
|
+
* @returns {{mode?: string, workspaceRoot?: string}} 策略;取不到时为空对象。
|
|
153
|
+
*/
|
|
154
|
+
function policyFor(session) {
|
|
155
|
+
const policyService = service('sandboxPolicy', sandboxPolicy)
|
|
156
|
+
try {
|
|
157
|
+
return policyService?.resolve({ session }) ?? {}
|
|
158
|
+
} catch {
|
|
159
|
+
return {}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 算出一个会话的落点信息。纯计算,不建目录、不授权。
|
|
165
|
+
*
|
|
166
|
+
* 每次调用都重新读策略:模式可能在会话中途变化,缓存住旧模式会把临时文件
|
|
167
|
+
* 送进当时并不允许写的目录。
|
|
168
|
+
* @param {object | undefined} session - 会话对象。
|
|
169
|
+
* @returns {{mode?: string, workspaceRoot?: string, scratchDir?: string}} 落点信息。
|
|
170
|
+
*/
|
|
171
|
+
function resolveState(session) {
|
|
172
|
+
const policy = policyFor(session)
|
|
173
|
+
const mode = policy.mode
|
|
174
|
+
const workspaceRoot = policy.workspaceRoot ?? session?.header?.cwd
|
|
175
|
+
const state = { mode, workspaceRoot, scratchDir: undefined }
|
|
176
|
+
if (mode === undefined || mode === 'read-only') return state
|
|
177
|
+
|
|
178
|
+
const scratchDir = scratchDirFor({ mode, workspaceRoot, configured: cfg.scratchDir })
|
|
179
|
+
if (scratchDir === undefined) return state
|
|
180
|
+
state.scratchDir = scratchDir
|
|
181
|
+
|
|
182
|
+
return state
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 让一个目录可写:建出来,并补上文件工具与受限子进程各自的权限。
|
|
187
|
+
* @param {object} session - 会话对象。
|
|
188
|
+
* @param {string} mode - 沙箱模式。
|
|
189
|
+
* @param {string | undefined} workspaceRoot - 工作区根。
|
|
190
|
+
* @param {string} dir - 目标目录。
|
|
191
|
+
* @returns {Promise<boolean>} 是否可用。
|
|
192
|
+
*/
|
|
193
|
+
async function makeWritable(session, mode, workspaceRoot, dir) {
|
|
194
|
+
try {
|
|
195
|
+
await ensureDir(dir)
|
|
196
|
+
} catch {
|
|
197
|
+
return false
|
|
198
|
+
}
|
|
199
|
+
// 文件工具的可写根与「是否给子进程补访问控制项」是两件事,分开判断。
|
|
200
|
+
let rooted = true
|
|
201
|
+
const policyService = service('sandboxPolicy', sandboxPolicy)
|
|
202
|
+
try {
|
|
203
|
+
if (typeof policyService?.registerWritableRoot !== 'function') rooted = false
|
|
204
|
+
else {
|
|
205
|
+
let seen = writableRoots.get(session)
|
|
206
|
+
if (seen === undefined) {
|
|
207
|
+
seen = new Set()
|
|
208
|
+
writableRoots.set(session, seen)
|
|
209
|
+
}
|
|
210
|
+
if (!seen.has(dir)) {
|
|
211
|
+
const dispose = policyService.registerWritableRoot(session, dir)
|
|
212
|
+
seen.add(dir)
|
|
213
|
+
if (typeof dispose === 'function') rootDisposers.add(dispose)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
} catch {
|
|
217
|
+
rooted = false
|
|
218
|
+
}
|
|
219
|
+
// 完全权限下没有围栏,不需要给受限子进程补访问控制项。
|
|
220
|
+
if (mode === 'danger-full-access') return rooted
|
|
221
|
+
const processWritable = cfg.grantProcessWrite
|
|
222
|
+
? await grantProcessWrite(workspaceRoot, dir)
|
|
223
|
+
: true
|
|
224
|
+
return rooted && processWritable
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 落实落点:建目录并补权限。首次真正要用时才调用——
|
|
229
|
+
* 会话创建那一刻策略还没落定,提前算会把临时文件送进错误的目录。
|
|
230
|
+
* @param {object | undefined} session - 会话对象。
|
|
231
|
+
* @param {object} state - resolveState 的结果,可能被就地修正为退路目录。
|
|
232
|
+
* @returns {Promise<boolean>} 是否可用。
|
|
233
|
+
*/
|
|
234
|
+
function activate(session, state) {
|
|
235
|
+
if (session === undefined || session === null) return Promise.resolve(false)
|
|
236
|
+
if (state.scratchDir === undefined) return Promise.resolve(false)
|
|
237
|
+
const key = [state.mode, state.scratchDir].join('|')
|
|
238
|
+
const cached = prepared.get(session)
|
|
239
|
+
if (cached !== undefined && cached.key === key) return cached.promise
|
|
240
|
+
const promise = (async () => {
|
|
241
|
+
const dirs = [state.scratchDir]
|
|
242
|
+
let ok = true
|
|
243
|
+
for (const dir of dirs) {
|
|
244
|
+
if (!(await makeWritable(session, state.mode, state.workspaceRoot, dir))) {
|
|
245
|
+
ok = false
|
|
246
|
+
break
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (ok) {
|
|
250
|
+
effective.set(session, state)
|
|
251
|
+
return true
|
|
252
|
+
}
|
|
253
|
+
// 共享目录拿不到权限时退回工作区里的 cache/scratch:至少写入能成。
|
|
254
|
+
if (state.mode !== 'workspace-write' || typeof state.workspaceRoot !== 'string') return false
|
|
255
|
+
const fallback = scratchDirFor({
|
|
256
|
+
mode: state.mode,
|
|
257
|
+
workspaceRoot: state.workspaceRoot,
|
|
258
|
+
configured: cfg.scratchDir,
|
|
259
|
+
sharedCache: false,
|
|
260
|
+
})
|
|
261
|
+
if (fallback === undefined) return false
|
|
262
|
+
if (!(await makeWritable(session, state.mode, state.workspaceRoot, fallback))) return false
|
|
263
|
+
state.scratchDir = fallback
|
|
264
|
+
effective.set(session, state)
|
|
265
|
+
return true
|
|
266
|
+
})()
|
|
267
|
+
prepared.set(session, { key, promise })
|
|
268
|
+
return promise
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 会话创建完成后立即把 scratch 登记为可写目录。
|
|
273
|
+
*
|
|
274
|
+
* 不能等到 shell 命令执行:文件工具(write / edit)不经过 shell 包装,
|
|
275
|
+
* 而 resume / fork 之后原先的登记会失效,agent/created 会再次触发。
|
|
276
|
+
* 登记只是把目录加进可写允许列表,不改变权限模式;明确只读时跳过。
|
|
277
|
+
* @param {object} agent - agent 实例(真实宿主里带 session)。
|
|
278
|
+
* @returns {Promise<boolean>} 是否登记成功。
|
|
279
|
+
*/
|
|
280
|
+
function prepareAgent(agent) {
|
|
281
|
+
const session = agent?.session
|
|
282
|
+
if (session === undefined || session === null) return Promise.resolve(false)
|
|
283
|
+
const policy = policyFor(session)
|
|
284
|
+
if (policy.mode === 'read-only') return Promise.resolve(false)
|
|
285
|
+
const workspaceRoot = policy.workspaceRoot ?? session?.header?.cwd
|
|
286
|
+
// 目录位置与模式无关;只在补访问控制项时区分完全权限(无需给受限进程授权)。
|
|
287
|
+
const mode = policy.mode === 'danger-full-access' ? 'danger-full-access' : 'workspace-write'
|
|
288
|
+
const scratchDir = scratchDirFor({
|
|
289
|
+
mode,
|
|
290
|
+
workspaceRoot,
|
|
291
|
+
configured: cfg.scratchDir,
|
|
292
|
+
})
|
|
293
|
+
if (scratchDir === undefined) return Promise.resolve(false)
|
|
294
|
+
let seen = agentRoots.get(agent)
|
|
295
|
+
if (seen === undefined) {
|
|
296
|
+
seen = new Set()
|
|
297
|
+
agentRoots.set(agent, seen)
|
|
298
|
+
}
|
|
299
|
+
if (seen.has(scratchDir)) return Promise.resolve(true)
|
|
300
|
+
// 不让同一 agent 并发重复走一遍授权;失败则连标记一起撤掉,留给下一次重试。
|
|
301
|
+
const promise = makeWritable(session, mode, workspaceRoot, scratchDir)
|
|
302
|
+
seen.add(scratchDir)
|
|
303
|
+
void promise.then((ok) => { if (!ok) seen.delete(scratchDir) }, () => { seen.delete(scratchDir) })
|
|
304
|
+
return promise
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 包装 shell 工具:在命令串前注入临时目录。
|
|
309
|
+
* 查工具要带 agent 作用域,否则只读到全局层,工具恒为 undefined。
|
|
310
|
+
* 直接替换活对象的 execute,而不是 register 同名工具——dsh-ops 已用带 when
|
|
311
|
+
* 的条件注册占用了 ops_bash 工具名,再 register 会重名冲突抛错。
|
|
312
|
+
* @param {object} agentCtx - agent 作用域上下文。
|
|
313
|
+
* @param {object} agent - 当前 agent,作为工具视图的作用域。
|
|
314
|
+
* @param {'pwsh' | 'bash' | 'ops_bash'} shell - 工具名;非 pwsh 一律按 bash 处理。
|
|
315
|
+
* @returns {boolean} 是否包装成功。
|
|
316
|
+
*/
|
|
317
|
+
function wrapShell(agentCtx, agent, shell) {
|
|
318
|
+
if (!cfg.injectShellTemp) return false
|
|
319
|
+
const live = agentCtx.tools.get(shell, agent)
|
|
320
|
+
if (live === undefined || live === null || typeof live !== 'object') return false
|
|
321
|
+
if (typeof live.execute !== 'function' || wrappedTools.has(live)) return wrappedTools.has(live)
|
|
322
|
+
// 工具定义对象就是注册进 registry 的原对象,替换 execute 即改调用行为。
|
|
323
|
+
const original = live.execute
|
|
324
|
+
live.execute = async function (args, exec) {
|
|
325
|
+
const input = args !== null && typeof args === 'object' ? args : {}
|
|
326
|
+
if (typeof input.command !== 'string' || input.command === '') return original(args, exec)
|
|
327
|
+
const execAgent = exec?.agent ?? exec?.caller
|
|
328
|
+
const session = execAgent?.session ?? currentSession()
|
|
329
|
+
const state = resolveState(session)
|
|
330
|
+
if (state.scratchDir === undefined) return original(args, exec)
|
|
331
|
+
if (!(await activate(session, state))) return original(args, exec)
|
|
332
|
+
// activate 成功即保证 scratchDir 有值。
|
|
333
|
+
const command = injectShellTemp(shell, input.command, state.scratchDir)
|
|
334
|
+
if (command === input.command) return original(args, exec)
|
|
335
|
+
return original({ ...input, command }, exec)
|
|
336
|
+
}
|
|
337
|
+
wrappedTools.add(live)
|
|
338
|
+
return true
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** 只包装 shell 工具;文件工具的落点由模型自己决定。 */
|
|
342
|
+
const TARGETS = ['pwsh', 'bash', 'ops_bash']
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* 给一个 agent 装上全部包装;已经装过的跳过。
|
|
346
|
+
* @param {object} agent - agent 实例。
|
|
347
|
+
* @returns {Promise<void>}
|
|
348
|
+
*/
|
|
349
|
+
async function install(agent) {
|
|
350
|
+
if (agent === undefined || agent === null) return
|
|
351
|
+
if (installing.has(agent)) return
|
|
352
|
+
installing.add(agent)
|
|
353
|
+
try {
|
|
354
|
+
await installNow(agent)
|
|
355
|
+
} finally {
|
|
356
|
+
installing.delete(agent)
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* install 的实际内容,由 install 负责重入保护。
|
|
362
|
+
* 目标工具当时还没注册时留待 tools/change 重试,不会永久漏掉。
|
|
363
|
+
* @param {object} agent - agent 实例。
|
|
364
|
+
* @returns {Promise<void>}
|
|
365
|
+
*/
|
|
366
|
+
async function installNow(agent) {
|
|
367
|
+
const agentCtx = agent.ctx
|
|
368
|
+
if (agentCtx === undefined || typeof agentCtx.inject !== 'function') return
|
|
369
|
+
let done = installed.get(agent)
|
|
370
|
+
if (done === undefined) {
|
|
371
|
+
done = new Set()
|
|
372
|
+
installed.set(agent, done)
|
|
373
|
+
}
|
|
374
|
+
const ready = TARGETS.filter(
|
|
375
|
+
toolName => !done.has(toolName) && ctx.tools.get(toolName, agent) !== undefined
|
|
376
|
+
)
|
|
377
|
+
if (ready.length === 0) return
|
|
378
|
+
// 真实的 agent 上下文要求先声明依赖才能读 ctx.tools;注册仍落在该 agent 的作用域层。
|
|
379
|
+
await new Promise((resolve) => {
|
|
380
|
+
agentCtx.inject(['tools'], (child) => {
|
|
381
|
+
// 只有真的注册成功才记进 done;目标工具还没出现时留白,等 tools/change 再补。
|
|
382
|
+
for (const toolName of ready) {
|
|
383
|
+
if (wrapShell(child, agent, toolName)) done.add(toolName)
|
|
384
|
+
}
|
|
385
|
+
resolve()
|
|
386
|
+
})
|
|
387
|
+
})
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// agent/created 是串行且被 await 的;返回 Promise 保证输入放行前
|
|
391
|
+
// 包装已装好、scratch 已登记为可写目录。
|
|
392
|
+
ctx.on('agent/created', ({ agent }) =>
|
|
393
|
+
Promise.all([install(agent), prepareAgent(agent)]).then(() => undefined))
|
|
394
|
+
// 工具注册表变化时补装:目标工具可能比本插件晚加载。
|
|
395
|
+
ctx.on('tools/change', () => {
|
|
396
|
+
let list
|
|
397
|
+
try { list = ctx.agents?.list() } catch { list = undefined }
|
|
398
|
+
if (!Array.isArray(list)) return
|
|
399
|
+
for (const agent of list) {
|
|
400
|
+
void install(agent)
|
|
401
|
+
void prepareAgent(agent)
|
|
402
|
+
}
|
|
403
|
+
})
|
|
404
|
+
// agents 服务可能晚于本插件注册,等它就绪后补装一遍。
|
|
405
|
+
ctx.inject(['agents'], (child) => {
|
|
406
|
+
for (const agent of child.agents.list()) {
|
|
407
|
+
void install(agent)
|
|
408
|
+
void prepareAgent(agent)
|
|
409
|
+
}
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
// 启动就跑一次清理,之后每 CLEANUP_INTERVAL_MS 再触发一次。
|
|
413
|
+
// 用原生定时器而不是 ctx.interval:后者要注入 cordis 的 timer 服务,
|
|
414
|
+
// 宿主没装就会整个插件起不来,为一次清理不值得。
|
|
415
|
+
// 定时器随插件的 effect 一起清掉,节流器保证两次真正落地间隔不小于间隔值。
|
|
416
|
+
void cleanup()
|
|
417
|
+
ctx.effect(() => {
|
|
418
|
+
const timer = setInterval(() => { void cleanup() }, CLEANUP_INTERVAL_MS)
|
|
419
|
+
// 不阻止进程退出:清理本身不紧急,宿主该退就退。
|
|
420
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
421
|
+
return () => clearInterval(timer)
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
if (cfg.promptSection) {
|
|
425
|
+
ctx.inject(['systemPrompt'], (child) => {
|
|
426
|
+
// 排在 write 工具的说明之后;取不到宿主顺序时用固定兜底值。
|
|
427
|
+
let order = 1201
|
|
428
|
+
try {
|
|
429
|
+
const baseOrder = child.systemPrompt.getSectionOrder('TOOL_WRITE')
|
|
430
|
+
if (Number.isFinite(baseOrder)) order = baseOrder + 1
|
|
431
|
+
} catch {
|
|
432
|
+
/* 宿主未提供顺序表时保留兜底值 */
|
|
433
|
+
}
|
|
434
|
+
child.systemPrompt.section({
|
|
435
|
+
name: '@6starlong/dsh-disk-cleanup',
|
|
436
|
+
order,
|
|
437
|
+
text: (context) => {
|
|
438
|
+
const session = context?.agent?.session ?? currentSession()
|
|
439
|
+
// 只读会话没有可写落点,讲了也是假的:整段不注入。
|
|
440
|
+
if (policyFor(session).mode === 'read-only') return ''
|
|
441
|
+
const fresh = resolveState(session)
|
|
442
|
+
// 退路可能把 scratch 挪到了工作区,提示要跟着改口;但模式变了(比如切成只读)就先以新模式为准。
|
|
443
|
+
const applied = session === undefined ? undefined : effective.get(session)
|
|
444
|
+
const state = fresh.scratchDir === undefined ? fresh : applied ?? fresh
|
|
445
|
+
return scratchPromptText({ scratchDir: state.scratchDir })
|
|
446
|
+
},
|
|
447
|
+
})
|
|
448
|
+
})
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// 插件卸载时撤销可写根登记、释放授权对象。目录上的访问控制项是 standing 的,
|
|
452
|
+
// 留着给之后的会话复用——撤销了下一个会话就会被挡住。
|
|
453
|
+
if (typeof ctx.effect === 'function') {
|
|
454
|
+
ctx.effect(() => () => {
|
|
455
|
+
for (const dispose of rootDisposers) {
|
|
456
|
+
try { dispose() } catch { /* 单个撤销失败不阻塞其余 */ }
|
|
457
|
+
}
|
|
458
|
+
rootDisposers.clear()
|
|
459
|
+
revokeAll()
|
|
460
|
+
})
|
|
461
|
+
}
|
|
462
|
+
}
|
package/lib/prompt.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 系统提示里那一段说明(软约束,配合硬机制用)。
|
|
3
|
+
* @module dsh-disk-cleanup/prompt
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 生成提示文本。
|
|
8
|
+
*
|
|
9
|
+
* 只说「写哪里」和「为什么别放工作区」,不解释保留天数、定时器这类内部策略:
|
|
10
|
+
* 模型既改不了也用不上,写进提示只是噪音。
|
|
11
|
+
* @param {object} options - 生成输入。
|
|
12
|
+
* @param {string} [options.scratchDir] - 当前会话的 scratch 目录。
|
|
13
|
+
* @returns {string} 一段提示。
|
|
14
|
+
*/
|
|
15
|
+
export function scratchPromptText({ scratchDir } = {}) {
|
|
16
|
+
const lines = [
|
|
17
|
+
'Write temporary files (probe scripts, one-off checks, scratch drafts) to the scratch directory'
|
|
18
|
+
+ (scratchDir === undefined ? '' : ` (${scratchDir})`)
|
|
19
|
+
+ ': the shell\'s TMP/TEMP point there, and these files may be cleaned up later.',
|
|
20
|
+
'Files the user asked for stay in the workspace: give them a real relative path (for example src/main.ts or report.md) and they are written exactly where you say.',
|
|
21
|
+
]
|
|
22
|
+
return lines.join(' ')
|
|
23
|
+
}
|
package/lib/schema.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 取 schemastery 实例,并给出一条加载不到时的退路。
|
|
3
|
+
*
|
|
4
|
+
* 设置页靠插件导出的 Config schema 渲染表单,而 schema 必须用真的 schemastery
|
|
5
|
+
* 构建出来。包随 DSH 一起分发,插件以 link 或独立 node_modules 安装时解析规则
|
|
6
|
+
* 不保证覆盖到它,所以这里显式兜底:先按普通模块解析,再按 DSH 安装位置找。
|
|
7
|
+
* @module dsh-disk-cleanup/schema
|
|
8
|
+
*/
|
|
9
|
+
import { createRequire } from 'node:module'
|
|
10
|
+
import { dirname, join } from 'node:path'
|
|
11
|
+
import { pathToFileURL } from 'node:url'
|
|
12
|
+
|
|
13
|
+
/** schemastery 的包名。 */
|
|
14
|
+
const SCHEMA_PACKAGE = '@deepseek-ai/schemastery'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 按 DSH 的安装位置解析平台包。
|
|
18
|
+
* @returns {string | undefined} 包内入口的绝对路径;找不到时 undefined。
|
|
19
|
+
*/
|
|
20
|
+
function fromInstallRoot() {
|
|
21
|
+
const appRoot = join(dirname(process.execPath), 'resources', 'app.asar', 'dsh', 'node_modules')
|
|
22
|
+
try {
|
|
23
|
+
return createRequire(join(appRoot, 'noop.js')).resolve(SCHEMA_PACKAGE)
|
|
24
|
+
} catch {
|
|
25
|
+
return undefined
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 取 schemastery 模块。
|
|
31
|
+
* @returns {Promise<object | undefined>} 模块导出;哪里都找不到时 undefined。
|
|
32
|
+
*/
|
|
33
|
+
async function load() {
|
|
34
|
+
try {
|
|
35
|
+
return await import(SCHEMA_PACKAGE)
|
|
36
|
+
} catch {
|
|
37
|
+
/* 平台包不在解析路径里,继续按安装位置找 */
|
|
38
|
+
}
|
|
39
|
+
const entry = fromInstallRoot()
|
|
40
|
+
if (entry === undefined) return undefined
|
|
41
|
+
try {
|
|
42
|
+
return await import(pathToFileURL(entry).href)
|
|
43
|
+
} catch {
|
|
44
|
+
return undefined
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 空壳构建器:任何方法都返回它自己,方法链走得通、什么也不产生。
|
|
50
|
+
* 包彻底找不到时就用它,插件的配置因此退回手写默认值,其余机制照常工作。
|
|
51
|
+
* @returns {object} 只支持链式调用的假构建器。
|
|
52
|
+
*/
|
|
53
|
+
function inertZ() {
|
|
54
|
+
return new Proxy(function z() {}, {
|
|
55
|
+
apply() {
|
|
56
|
+
return inertZ()
|
|
57
|
+
},
|
|
58
|
+
get() {
|
|
59
|
+
return () => inertZ()
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 真的 schemastery;模块求值阶段就要用,所以同步解析一次。 */
|
|
65
|
+
let loaded
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 同步取构建器;ESM 里没有同步 import,所以用 CommonJS 的 require。
|
|
69
|
+
* 插件装在 profile 的 node_modules 下时,宿主与平台包是同级依赖,
|
|
70
|
+
* 用 profile 的 require(即 process.cwd() 那一级)能解析到;插件被单独
|
|
71
|
+
* 装在别处时解析不到,由下面的异步兜底接手。
|
|
72
|
+
* @returns {Function | undefined} 构建器;解析不到时 undefined。
|
|
73
|
+
*/
|
|
74
|
+
function syncLoad() {
|
|
75
|
+
for (const base of [import.meta.url, join(process.cwd(), 'noop.js')]) {
|
|
76
|
+
try {
|
|
77
|
+
// 这个包是 CommonJS:require 回来的是构建器本身(不是带 default 的命名空间)。
|
|
78
|
+
const mod = createRequire(base)(SCHEMA_PACKAGE)
|
|
79
|
+
if (typeof mod === 'function') return mod
|
|
80
|
+
if (typeof mod?.default === 'function') return mod.default
|
|
81
|
+
} catch {
|
|
82
|
+
/* 这一级解析不到,换下一级 */
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
loaded = syncLoad()
|
|
89
|
+
|
|
90
|
+
if (typeof loaded !== 'function') {
|
|
91
|
+
load().then((mod) => {
|
|
92
|
+
if (typeof mod?.default === 'function') loaded = mod.default
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 当前生效的构建器;异步兜底加载到真包后也由它跟上。 */
|
|
97
|
+
function builder() {
|
|
98
|
+
return typeof loaded === 'function' ? loaded : inertZ()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 本插件使用的 schemastery 实例。
|
|
103
|
+
* 函数一律补绑到构建器上,取属性时同步转发。
|
|
104
|
+
*/
|
|
105
|
+
export const z = new Proxy(inertZ(), {
|
|
106
|
+
// 顶层方法要能直接调用:z() 就是构建器本身,z.object(...) 之类也走这里。
|
|
107
|
+
apply(target, thisArg, args) {
|
|
108
|
+
return Reflect.apply(builder(), thisArg, args)
|
|
109
|
+
},
|
|
110
|
+
get(target, key) {
|
|
111
|
+
// Promise 解析会读 then:不挡住的话,任何 await 都会把 z 当成 thenable。
|
|
112
|
+
if (key === 'then') return undefined
|
|
113
|
+
const source = builder()
|
|
114
|
+
const value = source[key]
|
|
115
|
+
return typeof value === 'function' ? value.bind(source) : value
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 判断真的 schemastery 是否已经就位。
|
|
121
|
+
* @returns {boolean} 构建器来自真包时为 true。
|
|
122
|
+
*/
|
|
123
|
+
export function schemaReady() {
|
|
124
|
+
return typeof loaded === 'function'
|
|
125
|
+
}
|
package/lib/scratch.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 权限模式 → scratch 目录;目录创建。
|
|
3
|
+
* @module dsh-disk-cleanup/scratch
|
|
4
|
+
*/
|
|
5
|
+
import { mkdir } from 'node:fs/promises'
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 解析 DSH 的 home 目录:优先环境变量 DSH_HOME,否则 ~/.dsh。
|
|
11
|
+
* @returns {string} 绝对路径。
|
|
12
|
+
*/
|
|
13
|
+
export function dshHome() {
|
|
14
|
+
const fromEnv = process.env.DSH_HOME
|
|
15
|
+
if (typeof fromEnv === 'string' && fromEnv.trim() !== '') return fromEnv.trim()
|
|
16
|
+
return join(homedir(), '.dsh')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 共享的 scratch 目录:DSH home 下的 cache/scratch。
|
|
21
|
+
* @returns {string} 绝对路径。
|
|
22
|
+
*/
|
|
23
|
+
export function sharedScratchDir() {
|
|
24
|
+
return join(dshHome(), 'cache', 'scratch')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 按权限模式给出 scratch 目录。
|
|
29
|
+
*
|
|
30
|
+
* 两种可写模式都优先用 DSH home 下的共享目录,工作区因此不会长出临时文件;
|
|
31
|
+
* 共享目录写不进去时(无法追加可写根)退到工作区里的 cache/scratch。
|
|
32
|
+
* @param {object} options - 解析输入。
|
|
33
|
+
* @param {string} [options.mode] - 沙箱模式:workspace-write / danger-full-access / read-only。
|
|
34
|
+
* @param {string} [options.workspaceRoot] - 会话工作区根。
|
|
35
|
+
* @param {string} [options.configured] - 配置里显式指定的目录,优先于模式推导。
|
|
36
|
+
* @param {boolean} [options.sharedCache] - 是否可以用 DSH home 下的共享目录。
|
|
37
|
+
* @returns {string | undefined} 绝对路径;read-only 或无可用根时返回 undefined。
|
|
38
|
+
*/
|
|
39
|
+
export function scratchDirFor({ mode, workspaceRoot, configured, sharedCache = true } = {}) {
|
|
40
|
+
if (typeof configured === 'string' && configured.trim() !== '') return configured.trim()
|
|
41
|
+
if (mode === 'read-only') return undefined
|
|
42
|
+
const shared = sharedScratchDir()
|
|
43
|
+
if (sharedCache) return shared
|
|
44
|
+
if (typeof workspaceRoot === 'string' && workspaceRoot.trim() !== '') {
|
|
45
|
+
return join(workspaceRoot, 'cache', 'scratch')
|
|
46
|
+
}
|
|
47
|
+
return shared
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 确保目录存在(幂等)。
|
|
52
|
+
* @param {string} dir - 目录绝对路径。
|
|
53
|
+
* @returns {Promise<void>} 完成信号。
|
|
54
|
+
*/
|
|
55
|
+
export async function ensureDir(dir) {
|
|
56
|
+
await mkdir(dir, { recursive: true })
|
|
57
|
+
}
|