dsh-disk-cleanup 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -2
- package/cordis.patch.yml +24 -0
- package/icon.svg +11 -0
- package/lib/acl.js +122 -0
- package/lib/cleanup.js +207 -0
- package/lib/client.js +194 -0
- package/lib/config.js +116 -0
- package/lib/index.d.ts +33 -0
- package/lib/index.js +434 -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 +85 -4
package/README.md
CHANGED
|
@@ -1,3 +1,114 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-disk-cleanup
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
DSH 插件:为 shell 命令注入临时目录与依赖缓存变量,给 scratch 目录补齐写权限,在系统提示里说明临时文件落点,并自动清理过期的临时文件。
|
|
4
|
+
|
|
5
|
+
DSH 本身没有「临时文件目录」这个概念;模型写出的探针脚本默认都落在工作区内。这个插件提供统一的环境注入、scratch 目录授权、一段系统提示和一份过期清理,让临时文件有地方可去、也不会越攒越多。
|
|
6
|
+
|
|
7
|
+
## 它做什么
|
|
8
|
+
|
|
9
|
+
四条机制:
|
|
10
|
+
|
|
11
|
+
| 机制 | 覆盖的情况 | 做法 |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| shell 环境注入 | 脚本内部用 `tempfile` / `[System.IO.Path]::GetTempPath()` 之类 | 在 `pwsh` / `bash` 命令串前设置 `TMP` / `TEMP` / `TMPDIR`,指向 `<DSH_HOME>/cache/scratch` |
|
|
14
|
+
| 权限补齐 | workspace-write 下 scratch 目录对受限子进程不可写 | 给会话注册可写根 + 给目录补访问控制项 |
|
|
15
|
+
| 系统提示 | 模型不知道该往哪写 | 加一段祈使句,点明 scratch 位置、临时文件写那儿、用户文件留在工作区;路径每次构建时现算,只读会话不出这段 |
|
|
16
|
+
| 过期清理 | scratch 只增不减,旧探针脚本一直留着 | 按保留天数删掉 `<DSH_HOME>/cache/scratch` 里过期的条目,并清掉残留的空目录,插件加载时与之后定期各扫一次 |
|
|
17
|
+
|
|
18
|
+
不改写任何工具入参的路径:模型给什么路径就写什么路径。要不要把临时文件放进 scratch 由提示词引导,不由插件替模型决定。
|
|
19
|
+
|
|
20
|
+
## 落点规则
|
|
21
|
+
|
|
22
|
+
| 权限模式 | scratch 目录 | 怎么让它可写 |
|
|
23
|
+
|---|---|---|
|
|
24
|
+
| workspace-write | `<DSH_HOME>/cache/scratch` | 给会话注册可写根(文件工具)+ 给目录补访问控制项(子进程) |
|
|
25
|
+
| danger-full-access | `<DSH_HOME>/cache/scratch` | 不需要,本来就能写 |
|
|
26
|
+
| read-only | 不启用 | —— |
|
|
27
|
+
| 任意可写模式 + 配置了 `scratchDir` | 配置里给的那个目录 | 同 workspace-write 的两步;这个目录不参与自动清理 |
|
|
28
|
+
|
|
29
|
+
`<DSH_HOME>` 默认 `~/.dsh`,跟随 `DSH_HOME` 环境变量。**所有可写模式都用同一个位置**,工作区只留用户要的文件。
|
|
30
|
+
|
|
31
|
+
workspace-write 下写到工作区外面要两步授权,插件在会话创建时就做好:
|
|
32
|
+
|
|
33
|
+
1. `sandboxPolicy.registerWritableRoot(session, dir)` —— 让文件工具能写这个目录。
|
|
34
|
+
2. `AclWriteGrant`(Windows)—— 用工作区自己的安全标识给目录加一条可写项,受限子进程也能写。
|
|
35
|
+
|
|
36
|
+
第 2 步只在 Windows 上做,复用工作区的安全标识,不加新身份、也不放宽到别的目录。补上的访问控制项是常驻的:插件卸载时只释放授权对象,不删这一项,否则下一个会话写 scratch 会被挡回去。任何一步失败就整体退到 `<工作区>/cache/scratch`。
|
|
37
|
+
|
|
38
|
+
## 过期清理
|
|
39
|
+
|
|
40
|
+
清 `<DSH_HOME>/cache/scratch` 里的过期条目与空目录,另外删 `<DSH_HOME>` 下一级的空目录:
|
|
41
|
+
|
|
42
|
+
- **删什么**:`cache/scratch` 下的顶层条目,最后修改时间早于「现在减保留天数」的才删;目录整个删掉,含里面的内容。
|
|
43
|
+
- **空目录**:`cache/scratch` 里自底向上删空目录(父目录因此变空也一并删),另外删 `<DSH_HOME>` 下一级的空目录;都只删没有任何条目的目录。
|
|
44
|
+
- **不删什么**:只处理 `<DSH_HOME>/cache/scratch` 与 `<DSH_HOME>` 下一级的空目录,不往其它子目录里递归;配置里自己指了 `scratchDir` 的,那个目录也不自动清,里面的东西未必可丢。
|
|
45
|
+
- **怎么算过期**:看条目自己的最后修改时间,不看访问时间(Windows 默认不更新访问时间)。目录里某个子文件刚被写过,目录的修改时间会一并刷新,正在用的工作目录不会被误删。
|
|
46
|
+
- **何时清**:插件加载时一次,之后定期再扫,进程重启等于重新起算。
|
|
47
|
+
- **单个条目删不掉**(Windows 上被别的进程占着)就跳过,不中断整批。
|
|
48
|
+
|
|
49
|
+
在插件**详情页**的「过期清理」卡片里可以关掉自动清理,也可以改保留天数(1–365):天数留空恢复默认的 7 天。
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
## 安装
|
|
53
|
+
|
|
54
|
+
在 DSH 的插件管理页里安装,或者用命令行装:
|
|
55
|
+
|
|
56
|
+
```powershell
|
|
57
|
+
dsh plugin --profile <profile> add dsh-disk-cleanup
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
安装后重启 DSH 生效。
|
|
61
|
+
|
|
62
|
+
## 配置
|
|
63
|
+
|
|
64
|
+
插件详情页(插件页点开某个插件后的那一页)上那张「过期清理」卡片管 `autoCleanup` 与 `cleanupMaxAgeDays`;其余字段在 profile 的 `cordis.patch.yml` 里覆盖:
|
|
65
|
+
|
|
66
|
+
```yaml
|
|
67
|
+
- id: dsh-disk-cleanup
|
|
68
|
+
name: 'dsh-disk-cleanup'
|
|
69
|
+
config:
|
|
70
|
+
enabled: true
|
|
71
|
+
scratchDir: '' # 留空 = 默认暂存目录并自动清理;自定义目录不清理
|
|
72
|
+
injectShellTemp: true # pwsh/bash 的 TMP/TEMP 指向 scratch
|
|
73
|
+
grantProcessWrite: true # 给 scratch 补访问控制项,让子进程也能写
|
|
74
|
+
promptSection: true # 往系统提示里加一段说明
|
|
75
|
+
autoCleanup: true # 启动时清掉过期的临时文件(插件页也能改)
|
|
76
|
+
cleanupMaxAgeDays: 7 # 保留天数,1–365(插件页也能改)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 测试
|
|
80
|
+
|
|
81
|
+
```powershell
|
|
82
|
+
node --test "test/*.test.mjs"
|
|
83
|
+
node test/e2e-shell.mjs
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- `plugin` / `scratch` / `cleanup`:插件装配、纯函数与目录清理,覆盖 shell 包装、环境变量注入、可写根登记、提示词 section、过期判定、空目录清理与节流。
|
|
87
|
+
- `e2e-shell`:生成的前缀真的交给 pwsh 执行,确认 TMP/TEMP 落到 scratch。
|
|
88
|
+
- `schema-probe`:核对 `Config` 能被真的 schemastery 构建,且默认值与实际生效的一致;要在装了本插件的 profile 目录里跑。
|
|
89
|
+
- `client-probe`:核对客户端产物 `lib/client.js` 能注册到插件页槽位、保存时写哪几个字段。
|
|
90
|
+
|
|
91
|
+
改 `src/client/index.jsx` 之后要重新生成 `lib/client.js`:
|
|
92
|
+
|
|
93
|
+
```powershell
|
|
94
|
+
node scripts/build.mjs
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## 工具包装
|
|
98
|
+
|
|
99
|
+
包装只覆盖 `pwsh` 与 `bash`,并处理两个情况:
|
|
100
|
+
|
|
101
|
+
- **目标工具可能比本插件晚注册**(bundle 顺序不定)。`install()` 只给此刻已注册的工具装包装,只有真的注册成功才记进「已装」集合;还没出现的留白,由 `tools/change` 事件补装。
|
|
102
|
+
- **注册包装会触发 `tools/change`**,监听器回头再调 `install()` 就是重入,会在同一作用域重复注册同名工具并抛错。用 `installing` WeakSet 挡住。
|
|
103
|
+
|
|
104
|
+
## 边界
|
|
105
|
+
|
|
106
|
+
- 拿不到 `sandboxPolicy` 服务,或当前会话是 read-only → 完全不动:不注入、不建目录。
|
|
107
|
+
- 可写根或访问控制项补不上 → 整体退到 `<工作区>/cache/scratch`,不混用两处。
|
|
108
|
+
- 非 Windows 平台 → 跳过访问控制项那一步;可写根注册那半仍然生效。
|
|
109
|
+
- 原工具被别的插件关掉 → 跳过该工具,其余照常。
|
|
110
|
+
- 原 `execute` 抛错 → 原样透传,不包装、不吞异常。
|
|
111
|
+
- `run_code` 不覆盖(工具名被宿主保留,注册会抛错),也不在覆盖范围内。
|
|
112
|
+
- 挂载后会话一建立就会建 scratch 目录(`agent/created` 时一次性建好),不等第一次写文件。
|
|
113
|
+
- `sandbox_permissions` / `justification` 原样透传,权限升级链路不断。
|
|
114
|
+
- 客户端半边注册失败(比如宿主没有槽位服务)只影响插件页的卡片,shell 注入、授权与清理照常。
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# dsh-disk-cleanup 组合包补丁:把插件行插入 profile。
|
|
2
|
+
# 本补丁按 id 覆盖;用户可在自己 profile 的 cordis.patch.yml 里覆盖本行整个 config。
|
|
3
|
+
# 插件页上的「过期清理」卡片只改 autoCleanup 与 cleanupMaxAgeDays,其余字段在这里改。
|
|
4
|
+
#
|
|
5
|
+
# 示例(用户 profile 的 cordis.patch.yml):
|
|
6
|
+
# - id: dsh-disk-cleanup
|
|
7
|
+
# name: 'dsh-disk-cleanup'
|
|
8
|
+
# config:
|
|
9
|
+
# enabled: true
|
|
10
|
+
# # scratchDir: '' # 留空 = 默认暂存目录并自动清理;自定义目录不清理
|
|
11
|
+
# injectShellTemp: true # pwsh/bash 的 TMP/TEMP 指向 scratch
|
|
12
|
+
# grantProcessWrite: true # 给 scratch 补访问控制项,让子进程也能写
|
|
13
|
+
# promptSection: true # 往系统提示里加一段 scratch 说明
|
|
14
|
+
# autoCleanup: true # 启动时清理 scratch(插件页也能改)
|
|
15
|
+
# cleanupMaxAgeDays: 7 # 保留天数(插件页也能改)
|
|
16
|
+
#
|
|
17
|
+
- insert:
|
|
18
|
+
- id: dsh-disk-cleanup
|
|
19
|
+
name: 'dsh-disk-cleanup'
|
|
20
|
+
config:
|
|
21
|
+
enabled: true
|
|
22
|
+
injectShellTemp: true
|
|
23
|
+
grantProcessWrite: true
|
|
24
|
+
promptSection: true
|
package/icon.svg
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36" fill="none">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="diskCleanupBlue" x1="6" y1="7" x2="30" y2="30" gradientUnits="userSpaceOnUse">
|
|
4
|
+
<stop stop-color="#69B9FF"/>
|
|
5
|
+
<stop offset="1" stop-color="#324DE2"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<path d="M6 12.2A2.7 2.7 0 0 1 8.7 9.5h5.9l2.5 2.9h10.2a2.7 2.7 0 0 1 2.7 2.7v12.2a2.7 2.7 0 0 1-2.7 2.7H8.7a2.7 2.7 0 0 1-2.7-2.7V12.2Z" fill="url(#diskCleanupBlue)"/>
|
|
9
|
+
<circle cx="18" cy="21" r="6" fill="#FFFFFF"/>
|
|
10
|
+
<path d="M18 17.4v3.8l2.5 1.6" stroke="#324DE2" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
|
|
11
|
+
</svg>
|
package/lib/acl.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 给受限子进程补写权限(仅 Windows)。
|
|
3
|
+
*
|
|
4
|
+
* workspace-write 模式下,子进程拿的是受限令牌,能写的只有工作区根和它自己的
|
|
5
|
+
* 私有临时目录。要让子进程也能写 scratch,必须把同一个工作区写标识加到该目录的
|
|
6
|
+
* 访问控制列表上——令牌里已经有这个标识,加上就能通过。
|
|
7
|
+
* @module dsh-disk-cleanup/acl
|
|
8
|
+
*/
|
|
9
|
+
import { createRequire } from 'node:module'
|
|
10
|
+
import { realpathSync } from 'node:fs'
|
|
11
|
+
import { dirname, join } from 'node:path'
|
|
12
|
+
import { pathToFileURL } from 'node:url'
|
|
13
|
+
|
|
14
|
+
/** 内部包名。它只随 DSH 安装提供,不在 profile 的依赖里。 */
|
|
15
|
+
const ACL_PACKAGE = '@deepseek-ai/dsh-sandbox-windows-acl'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 按 DSH 的安装位置解析内部包。
|
|
19
|
+
* @returns {string | undefined} 包内入口的绝对路径;找不到时 undefined。
|
|
20
|
+
*/
|
|
21
|
+
function resolveAclPackage() {
|
|
22
|
+
const installRoot = dirname(process.execPath)
|
|
23
|
+
const appRoot = join(installRoot, 'resources', 'app.asar', 'dsh', 'node_modules')
|
|
24
|
+
try {
|
|
25
|
+
const require = createRequire(join(appRoot, 'noop.js'))
|
|
26
|
+
return require.resolve(ACL_PACKAGE)
|
|
27
|
+
} catch {
|
|
28
|
+
return undefined
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 已加载的包导出;null 表示确认不可用。 */
|
|
33
|
+
let aclModule
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 加载 ACL 包(按需,只尝试一次)。
|
|
37
|
+
* @returns {Promise<object | undefined>} 包导出;不可用时 undefined。
|
|
38
|
+
*/
|
|
39
|
+
async function loadAcl() {
|
|
40
|
+
if (aclModule !== undefined) return aclModule ?? undefined
|
|
41
|
+
if (process.platform !== 'win32') {
|
|
42
|
+
aclModule = null
|
|
43
|
+
return undefined
|
|
44
|
+
}
|
|
45
|
+
const entry = resolveAclPackage()
|
|
46
|
+
if (entry === undefined) {
|
|
47
|
+
aclModule = null
|
|
48
|
+
return undefined
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
const mod = await import(pathToFileURL(entry).href)
|
|
52
|
+
const usable = typeof mod.AclWriteGrant?.create === 'function' && typeof mod.workspaceWriteSid === 'function'
|
|
53
|
+
aclModule = usable ? mod : null
|
|
54
|
+
} catch {
|
|
55
|
+
aclModule = null
|
|
56
|
+
}
|
|
57
|
+
return aclModule ?? undefined
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 解析成符号链接展开后的物理路径(访问控制列表比较的就是它)。
|
|
62
|
+
* @param {string} target - 原始路径。
|
|
63
|
+
* @returns {string} 物理路径;解析失败时原样返回。
|
|
64
|
+
*/
|
|
65
|
+
export function physicalPath(target) {
|
|
66
|
+
try {
|
|
67
|
+
return realpathSync.native(target)
|
|
68
|
+
} catch {
|
|
69
|
+
return target
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 已经授权过的目录。键是工作区与目录的物理路径,同一工作区的多个会话共用一份授权。
|
|
75
|
+
* @type {Map<string, {grant: object}>}
|
|
76
|
+
*/
|
|
77
|
+
const grants = new Map()
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 给一个目录加上工作区写权限,让受限子进程能写进去。
|
|
81
|
+
*
|
|
82
|
+
* 幂等:同一组工作区与目录只建一次授权。
|
|
83
|
+
* 访问控制项按 standing 建,和 DSH 自己对可写目录的处理一致——撤销后下一个会话
|
|
84
|
+
* 就会被挡住,而这层授权本就是为了复用而存在,所以卸载时不撤销。revokeAll 只
|
|
85
|
+
* 释放授权对象持有的标识,不动目录上的访问控制项。
|
|
86
|
+
* @param {string} workspaceRoot - 会话工作区根。
|
|
87
|
+
* @param {string} directory - 需要可写的目录。
|
|
88
|
+
* @returns {Promise<boolean>} 是否可用(Windows 之外或内部包不可用时为 false)。
|
|
89
|
+
*/
|
|
90
|
+
export async function grantProcessWrite(workspaceRoot, directory) {
|
|
91
|
+
const acl = await loadAcl()
|
|
92
|
+
if (acl === undefined) return false
|
|
93
|
+
const root = physicalPath(workspaceRoot)
|
|
94
|
+
const dir = physicalPath(directory)
|
|
95
|
+
const key = JSON.stringify([root, dir])
|
|
96
|
+
if (grants.has(key)) return true
|
|
97
|
+
try {
|
|
98
|
+
const grant = acl.AclWriteGrant.create(acl.workspaceWriteSid(root))
|
|
99
|
+
grant.add(dir, true)
|
|
100
|
+
grants.set(key, { grant })
|
|
101
|
+
return true
|
|
102
|
+
} catch {
|
|
103
|
+
return false
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 释放全部授权对象。插件卸载时调用;单个失败不影响其余。
|
|
109
|
+
* 目录上的访问控制项是 standing 的,dispose 不会撤销它们(见 AclWriteGrant 的约定),
|
|
110
|
+
* 所以这里只是释放资源,不会让之后的会话失去写 scratch 的能力。
|
|
111
|
+
* @returns {void}
|
|
112
|
+
*/
|
|
113
|
+
export function revokeAll() {
|
|
114
|
+
for (const [key, entry] of grants) {
|
|
115
|
+
try {
|
|
116
|
+
entry.grant.dispose()
|
|
117
|
+
} catch {
|
|
118
|
+
/* 单个撤销失败不阻塞其余 */
|
|
119
|
+
}
|
|
120
|
+
grants.delete(key)
|
|
121
|
+
}
|
|
122
|
+
}
|
package/lib/cleanup.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 临时目录的清理:scratch 的过期条目,以及残留的空目录。
|
|
3
|
+
*
|
|
4
|
+
* 只删 scratch 自己的条目:临时文件都是「写完就不再需要」的一次性产物,
|
|
5
|
+
* 删掉不会影响工作区里的项目。
|
|
6
|
+
* 空目录清理只认「目录为空」,不猜是谁留下的,也不删有内容的目录。
|
|
7
|
+
* @module dsh-disk-cleanup/cleanup
|
|
8
|
+
*/
|
|
9
|
+
import { lstat, readdir, rm, rmdir, stat } from 'node:fs/promises'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
|
|
12
|
+
/** 一天有多少毫秒。 */
|
|
13
|
+
const DAY_MS = 24 * 60 * 60 * 1000
|
|
14
|
+
|
|
15
|
+
/** 启动之后自动清理的最小间隔。 */
|
|
16
|
+
export const CLEANUP_INTERVAL_MS = 6 * 60 * 60 * 1000
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 算出某个时刻的过期分界线。
|
|
20
|
+
*
|
|
21
|
+
* 用「条目自己的最后修改时间」判断,不用访问时间:Windows 默认关闭最后访问时间的
|
|
22
|
+
* 更新,读过的文件会一直显示成很久没动过。
|
|
23
|
+
* @param {number} maxAgeDays - 保留天数。
|
|
24
|
+
* @param {number} [now] - 参照时刻(毫秒)。
|
|
25
|
+
* @returns {number} 早于这个时刻的条目该删。
|
|
26
|
+
*/
|
|
27
|
+
export function cutoffTime(maxAgeDays, now = Date.now()) {
|
|
28
|
+
return now - maxAgeDays * DAY_MS
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 列出目录下的直接条目(不看内容)。
|
|
33
|
+
* @param {string} dir - 目录绝对路径。
|
|
34
|
+
* @returns {Promise<Array<{name: string, path: string, directory: boolean, mtimeMs: number}>>} 条目列表。
|
|
35
|
+
*/
|
|
36
|
+
async function listEntries(dir) {
|
|
37
|
+
const names = await readdir(dir)
|
|
38
|
+
const entries = []
|
|
39
|
+
for (const name of names) {
|
|
40
|
+
const path = join(dir, name)
|
|
41
|
+
try {
|
|
42
|
+
const info = await stat(path)
|
|
43
|
+
entries.push({ name, path, directory: info.isDirectory(), mtimeMs: info.mtimeMs })
|
|
44
|
+
} catch {
|
|
45
|
+
/* 单个条目读不到就跳过,不影响整批 */
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return entries
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 清掉 scratch 目录里超过保留期的条目。
|
|
53
|
+
*
|
|
54
|
+
* 只删顶层条目,且只删「条目本身最后一次改动早于分界线」的:目录里某个子文件
|
|
55
|
+
* 刚被写过,目录的修改时间会一并刷新,正在用的工作目录不会被误删。
|
|
56
|
+
* 单个条目删不掉(Windows 上文件被别的进程占着)就跳过,不中断整批。
|
|
57
|
+
* @param {object} options - 清理输入。
|
|
58
|
+
* @param {string} options.dir - scratch 目录。
|
|
59
|
+
* @param {number} options.maxAgeDays - 保留天数。
|
|
60
|
+
* @param {boolean} [options.dryRun] - 只统计不删除。
|
|
61
|
+
* @param {number} [options.now] - 参照时刻(毫秒),测试用。
|
|
62
|
+
* @returns {Promise<{removed: number, failed: number, kept: number}>} 统计结果;目录不存在时全为 0。
|
|
63
|
+
*/
|
|
64
|
+
export async function cleanScratch({ dir, maxAgeDays, dryRun = false, now = Date.now() }) {
|
|
65
|
+
const result = { removed: 0, failed: 0, kept: 0 }
|
|
66
|
+
if (typeof dir !== 'string' || dir === '') return result
|
|
67
|
+
let entries
|
|
68
|
+
try {
|
|
69
|
+
entries = await listEntries(dir)
|
|
70
|
+
} catch (error) {
|
|
71
|
+
// 目录不存在是常态(还没人写过临时文件),其余错误要知道,否则清理会静默失效。
|
|
72
|
+
if (error?.code === 'ENOENT') return result
|
|
73
|
+
result.failed += 1
|
|
74
|
+
return result
|
|
75
|
+
}
|
|
76
|
+
const cutoff = cutoffTime(maxAgeDays, now)
|
|
77
|
+
for (const entry of entries) {
|
|
78
|
+
if (entry.mtimeMs >= cutoff) {
|
|
79
|
+
result.kept += 1
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
82
|
+
if (dryRun) {
|
|
83
|
+
result.removed += 1
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
await rm(entry.path, { recursive: true, force: true })
|
|
88
|
+
result.removed += 1
|
|
89
|
+
} catch {
|
|
90
|
+
result.failed += 1
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return result
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 建一个「按间隔节流」的执行器:调用 execute 时,距离上次真正执行不足间隔就跳过。
|
|
98
|
+
*
|
|
99
|
+
* 不用常驻定时器:插件每次加载先跑一次,之后由调用方的周期触发兜底,
|
|
100
|
+
* 进程重启也只会重新跑一次,不会因为错过时间点堆起补偿。
|
|
101
|
+
* @param {number} intervalMs - 最小间隔(毫秒)。
|
|
102
|
+
* @returns {(run: () => Promise<unknown>) => Promise<boolean>} 执行器;真的执行了返回 true。
|
|
103
|
+
*/
|
|
104
|
+
export function throttled(intervalMs) {
|
|
105
|
+
let last = 0
|
|
106
|
+
let running = false
|
|
107
|
+
return async (run) => {
|
|
108
|
+
if (running) return false
|
|
109
|
+
const now = Date.now()
|
|
110
|
+
if (last !== 0 && now - last < intervalMs) return false
|
|
111
|
+
running = true
|
|
112
|
+
last = now
|
|
113
|
+
try {
|
|
114
|
+
await run()
|
|
115
|
+
return true
|
|
116
|
+
} finally {
|
|
117
|
+
running = false
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 清掉目录下的空目录,只删「一个条目都没有」的目录。
|
|
124
|
+
*
|
|
125
|
+
* recursive 为真时自底向上:空的子目录先删,父目录因此变空后一并删;
|
|
126
|
+
* 为假时只看直接子目录,不往里走。两种都不删 dir 本身。
|
|
127
|
+
* 用 rmdir 而不是 rm:目录此刻若被写入了内容,rmdir 会失败,不会连内容一起删。
|
|
128
|
+
* @param {object} options - 清理输入。
|
|
129
|
+
* @param {string} options.dir - 待清理的目录根。
|
|
130
|
+
* @param {boolean} [options.recursive] - 是否递归进子目录。
|
|
131
|
+
* @param {boolean} [options.dryRun] - 只统计不删除。
|
|
132
|
+
* @returns {Promise<{removed: number, failed: number, kept: number}>} 统计结果;目录不存在时全为 0。
|
|
133
|
+
*/
|
|
134
|
+
export async function cleanEmptyDirs({ dir, recursive = false, dryRun = false }) {
|
|
135
|
+
const result = { removed: 0, failed: 0, kept: 0 }
|
|
136
|
+
if (typeof dir !== 'string' || dir === '') return result
|
|
137
|
+
await pruneChildren(dir, recursive, dryRun, result)
|
|
138
|
+
return result
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 删掉 dir 下所有空的子目录,返回 dir 清理后是否也空了。
|
|
143
|
+
*
|
|
144
|
+
* 返回值一路往上传,dryRun 下也能算出「子目录删掉后父目录跟着变空」的连锁,
|
|
145
|
+
* 而不是只看目录此刻的内容。
|
|
146
|
+
* @param {string} dir - 当前层目录。
|
|
147
|
+
* @param {boolean} recursive - 是否继续往子目录里走。
|
|
148
|
+
* @param {boolean} dryRun - 只统计不删除。
|
|
149
|
+
* @param {{removed: number, failed: number, kept: number}} result - 累计统计。
|
|
150
|
+
* @returns {Promise<boolean>} dir 清理后是否为空;读不到时按非空处理。
|
|
151
|
+
*/
|
|
152
|
+
async function pruneChildren(dir, recursive, dryRun, result) {
|
|
153
|
+
let names
|
|
154
|
+
try {
|
|
155
|
+
names = await readdir(dir)
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (error?.code !== 'ENOENT') result.failed += 1
|
|
158
|
+
return false
|
|
159
|
+
}
|
|
160
|
+
let hasContent = false
|
|
161
|
+
for (const name of names) {
|
|
162
|
+
const target = join(dir, name)
|
|
163
|
+
let info
|
|
164
|
+
try {
|
|
165
|
+
info = await lstat(target)
|
|
166
|
+
} catch {
|
|
167
|
+
hasContent = true
|
|
168
|
+
continue
|
|
169
|
+
}
|
|
170
|
+
// 符号链接和 junction 不进也不删,否则会递归到别的目录树里。
|
|
171
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
172
|
+
hasContent = true
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
175
|
+
let empty
|
|
176
|
+
if (recursive) {
|
|
177
|
+
empty = await pruneChildren(target, true, dryRun, result)
|
|
178
|
+
} else {
|
|
179
|
+
let left
|
|
180
|
+
try {
|
|
181
|
+
left = await readdir(target)
|
|
182
|
+
empty = left.length === 0
|
|
183
|
+
} catch {
|
|
184
|
+
hasContent = true
|
|
185
|
+
result.kept += 1
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (!empty) {
|
|
190
|
+
hasContent = true
|
|
191
|
+
result.kept += 1
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
if (dryRun) {
|
|
195
|
+
result.removed += 1
|
|
196
|
+
continue
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
await rmdir(target)
|
|
200
|
+
result.removed += 1
|
|
201
|
+
} catch {
|
|
202
|
+
result.failed += 1
|
|
203
|
+
hasContent = true
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return !hasContent
|
|
207
|
+
}
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// 由 scripts/build.mjs 生成,不要手改。
|
|
2
|
+
window.__ModuleLoader__.load({
|
|
3
|
+
id: "dsh-disk-cleanup",
|
|
4
|
+
factory: function (require) {
|
|
5
|
+
var module = { exports: {} };
|
|
6
|
+
var exports = module.exports;
|
|
7
|
+
// ---- index.jsx ----
|
|
8
|
+
/**
|
|
9
|
+
* dsh-disk-cleanup 的浏览器半边:插件详情页上的「过期清理」配置卡片。
|
|
10
|
+
*
|
|
11
|
+
* 宿主按 schema 自动生成的表单只有字段、没有说明与文案,所以宿主侧关掉自动表单
|
|
12
|
+
* (见 lib/index.js 的 settings.configure),由本文件在 plugins.bundle.config 槽里
|
|
13
|
+
* 渲染一张自己的卡片。
|
|
14
|
+
*
|
|
15
|
+
* 槽位 key 与配置表单的命名空间都用包名:宿主按包名判断插件详情页要不要出配置区,
|
|
16
|
+
* 也按同名的 profile 条目读配置。
|
|
17
|
+
*
|
|
18
|
+
* 这张卡片只碰 autoCleanup(是否自动清理)与 cleanupMaxAgeDays(保留天数)两个
|
|
19
|
+
* volatile 字段,其余字段仍走 cordis.patch.yml。
|
|
20
|
+
*
|
|
21
|
+
* @module dsh-disk-cleanup/client
|
|
22
|
+
*/
|
|
23
|
+
const { Switch, SettingsForm, SettingsFormModel, settingsNumberField, SettingsValueField } = require("@deepseek-ai/dsh-client-ui-primitives")
|
|
24
|
+
const { jsx, jsxs } = require("react/jsx-runtime")
|
|
25
|
+
/*
|
|
26
|
+
* 客户端插件的身份(name / inject)由入口在 factory 里挂到 module.exports 上:
|
|
27
|
+
* 宿主 ModuleLoader 读的就是这个导出对象,闭包产物里不能写 export。
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** 包名:既是 plugins.bundle.config 的槽位 key,也是配置表单的命名空间。 */
|
|
31
|
+
const PLUGIN_PACKAGE = 'dsh-disk-cleanup'
|
|
32
|
+
/** 本地化命名空间。 */
|
|
33
|
+
const NS = 'settings.diskCleanup'
|
|
34
|
+
|
|
35
|
+
/** 保留天数默认值,与 lib/config.js 的 schema 默认值保持一致;输入框留空时置灰显示。 */
|
|
36
|
+
const DEFAULT_MAX_AGE_DAYS = '7'
|
|
37
|
+
|
|
38
|
+
/** 卡片里最小的必要样式:开关行左右排布、说明文字弱化。 */
|
|
39
|
+
const STYLE = {
|
|
40
|
+
row: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '12px' },
|
|
41
|
+
label: { fontWeight: 600 },
|
|
42
|
+
hint: { margin: '4px 0 12px', opacity: 0.7, fontSize: '12px' },
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 文案。宿主界面是中文,随宿主语言只给一份中文。 */
|
|
46
|
+
const MESSAGES = {
|
|
47
|
+
zh: {
|
|
48
|
+
title: '过期清理',
|
|
49
|
+
description: '自动清理 scratch 里过期的临时文件与残留的空目录。',
|
|
50
|
+
on: '开启后自动清理过期的临时文件与残留的空目录。',
|
|
51
|
+
off: '关闭后不再自动清理。',
|
|
52
|
+
maxAge: '保留天数',
|
|
53
|
+
maxAgeHint: 'scratch 里超过这么多天没改动过的条目会被删除。',
|
|
54
|
+
invalid: '请填一个正整数,或留空使用默认值。',
|
|
55
|
+
overridden: '已覆盖',
|
|
56
|
+
reset: '恢复默认',
|
|
57
|
+
save: '保存',
|
|
58
|
+
saving: '保存中…',
|
|
59
|
+
saveFailed: '宿主没有接受这些值,已保留以便重试。',
|
|
60
|
+
readonly: '当前配置为只读,无法修改。',
|
|
61
|
+
unavailable: '该插件当前未加载,暂时无法配置。',
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 布尔字段:开关写 'true' / 'false' 文本,再由 parse 变成真正的布尔值。
|
|
67
|
+
* @param {string} field - 字段名。
|
|
68
|
+
* @returns {object} 字段转换规则。
|
|
69
|
+
*/
|
|
70
|
+
function booleanField(field) {
|
|
71
|
+
return {
|
|
72
|
+
field,
|
|
73
|
+
format: value => String(value !== false),
|
|
74
|
+
parse: text => ({ kind: 'set', value: text === 'true' }),
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 表单外框的文案。
|
|
80
|
+
* @param {(key: string) => string} t - 翻译函数。
|
|
81
|
+
* @returns {object} SettingsForm 需要的标签。
|
|
82
|
+
*/
|
|
83
|
+
function formLabels(t) {
|
|
84
|
+
return {
|
|
85
|
+
unavailable: t('unavailable'),
|
|
86
|
+
readOnly: t('readonly'),
|
|
87
|
+
saveFailed: t('saveFailed'),
|
|
88
|
+
save: t('save'),
|
|
89
|
+
saving: t('saving'),
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 设置卡片。槽位注入 hooks.cleanupCard 与表单动作,形状与 dsh-rewind 的卡片一致。
|
|
95
|
+
* @param {object} props - 槽位注入的属性。
|
|
96
|
+
* @returns {object | null} 卡片元素;摘要视图不出内容。
|
|
97
|
+
*/
|
|
98
|
+
function CleanupCard(props) {
|
|
99
|
+
const { t, useCleanupCard } = props
|
|
100
|
+
const state = useCleanupCard(snapshot => snapshot)
|
|
101
|
+
if (props.view === 'summary') return null
|
|
102
|
+
const disabled = !state.writable || state.saving
|
|
103
|
+
const enabled = state.enabled.text === 'true'
|
|
104
|
+
return jsxs(SettingsForm, {
|
|
105
|
+
labels: formLabels(t),
|
|
106
|
+
state,
|
|
107
|
+
onSave: props.save,
|
|
108
|
+
onDiscard: props.discard,
|
|
109
|
+
children: [
|
|
110
|
+
jsxs('div', {
|
|
111
|
+
style: STYLE.row,
|
|
112
|
+
children: [
|
|
113
|
+
jsx('span', { style: STYLE.label, children: t('title') }),
|
|
114
|
+
jsx(Switch, {
|
|
115
|
+
checked: enabled,
|
|
116
|
+
label: t('title'),
|
|
117
|
+
disabled,
|
|
118
|
+
onChange: next => { props.edit('autoCleanup', String(next)) },
|
|
119
|
+
}),
|
|
120
|
+
],
|
|
121
|
+
}, 'toggle'),
|
|
122
|
+
jsx('p', { style: STYLE.hint, children: t(enabled ? 'on' : 'off') }, 'hint'),
|
|
123
|
+
jsx(SettingsValueField, {
|
|
124
|
+
id: 'plugin-config-disk-cleanup-maxage',
|
|
125
|
+
label: t('maxAge'),
|
|
126
|
+
hint: t('maxAgeHint'),
|
|
127
|
+
overriddenLabel: t('overridden'),
|
|
128
|
+
resetLabel: t('reset'),
|
|
129
|
+
invalidLabel: t('invalid'),
|
|
130
|
+
numeric: true,
|
|
131
|
+
placeholder: DEFAULT_MAX_AGE_DAYS,
|
|
132
|
+
disabled,
|
|
133
|
+
...state.maxAgeDays,
|
|
134
|
+
onEdit: text => { props.edit('cleanupMaxAgeDays', text) },
|
|
135
|
+
onReset: () => { props.resetField('cleanupMaxAgeDays') },
|
|
136
|
+
}, 'maxage'),
|
|
137
|
+
],
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 表单模型与卡片状态的接线。
|
|
143
|
+
*/
|
|
144
|
+
class CleanupCardController {
|
|
145
|
+
/** @param {object} scope - 宿主该条目的配置表单。 */
|
|
146
|
+
constructor(scope) {
|
|
147
|
+
this.form = new SettingsFormModel(scope, [
|
|
148
|
+
booleanField('autoCleanup'),
|
|
149
|
+
settingsNumberField('cleanupMaxAgeDays'),
|
|
150
|
+
])
|
|
151
|
+
this.store = this.form.bind(() => ({
|
|
152
|
+
...this.form.shell(),
|
|
153
|
+
enabled: this.form.field('autoCleanup'),
|
|
154
|
+
maxAgeDays: this.form.field('cleanupMaxAgeDays'),
|
|
155
|
+
}))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** @returns {object} 槽位注入的 hooks 与动作。 */
|
|
159
|
+
inject() {
|
|
160
|
+
return { hooks: { cleanupCard: this.store }, ...this.form.actions() }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
dispose() {
|
|
164
|
+
this.form.dispose()
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 客户端插件入口。
|
|
170
|
+
* @param {object} ctx - 客户端根上下文。
|
|
171
|
+
*/
|
|
172
|
+
function apply(ctx) {
|
|
173
|
+
ctx.effect(() => ctx.locale.register(NS, MESSAGES), 'dsh-disk-cleanup 文案')
|
|
174
|
+
const t = ctx.locale.bind(NS)
|
|
175
|
+
const card = new CleanupCardController(ctx.configForms.get(PLUGIN_PACKAGE))
|
|
176
|
+
ctx.effect(() => () => {
|
|
177
|
+
try {
|
|
178
|
+
card.dispose()
|
|
179
|
+
} catch { /* 宿主回收表单服务后,表单已经自己关掉了 */ }
|
|
180
|
+
}, 'dsh-disk-cleanup 表单')
|
|
181
|
+
ctx.effect(() => ctx.configForms.whileServed([PLUGIN_PACKAGE], () => ctx.slots.inject('plugins.bundle.config', () => ctx.slots.register({
|
|
182
|
+
name: 'plugins.bundle.config',
|
|
183
|
+
key: PLUGIN_PACKAGE,
|
|
184
|
+
locale: NS,
|
|
185
|
+
inject: () => card.inject(),
|
|
186
|
+
}, CleanupCard))), 'dsh-disk-cleanup 卡片')
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
module.exports.name = 'dsh-disk-cleanup'
|
|
190
|
+
module.exports.inject = ['slots', 'locale', 'configForms']
|
|
191
|
+
module.exports.apply = apply
|
|
192
|
+
return module.exports;
|
|
193
|
+
},
|
|
194
|
+
});
|