@wangyf397/session_del 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/README.md +73 -2
- package/client.js +251 -0
- package/cordis.patch.yml +5 -0
- package/icon.svg +19 -0
- package/index.js +537 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +50 -4
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# session_del
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
彻底删除 DeepSeek Harness 的历史对话。
|
|
4
|
+
|
|
5
|
+
DSH 自带的能力只有「归档」:归档只是把会话从侧栏隐藏,日志还在磁盘上,投影缓存、归档记录也都还在。
|
|
6
|
+
`session_del` 做的是真正的删除(clean delete),三个入口共用同一套操作:
|
|
7
|
+
|
|
8
|
+
| 入口 | 用法 |
|
|
9
|
+
|---|---|
|
|
10
|
+
| 侧栏菜单 | 会话行「…」→ **彻底删除会话** → 再点一次确认 → 删除 |
|
|
11
|
+
| 人类命令 | `/session-del list`、`/session-del delete <会话ID…>` |
|
|
12
|
+
| Agent 工具 | `session_del({ action: "list" })`、`session_del({ action: "delete", session_ids: [...] })` |
|
|
13
|
+
|
|
14
|
+
## 删除什么
|
|
15
|
+
|
|
16
|
+
1. **会话存储目录** `<sessionsRoot>/<projectKey(cwd)>/<encodeSegment(id)>/`
|
|
17
|
+
—— 会话日志(`session.vN.jsonl[.zstd]`)与会话锁。路径编码与官方 JSONL 后端逐位一致;
|
|
18
|
+
如果后端配置了自定义 `root`,会从运行中的配置读出来,不写死 `~/.dsh/sessions`。
|
|
19
|
+
2. **投影缓存** `<storagesRoot>/session_projcache/sessions/<id>.json` —— 列表里显示过的标题、统计等缓存行。
|
|
20
|
+
3. **工作区注册表**里的归档 / 置顶记录(通过 `workspaceRegistry` 的公开 API 清除)。
|
|
21
|
+
|
|
22
|
+
## 刻意不删
|
|
23
|
+
|
|
24
|
+
- `attachments/` 里的附件:按内容寻址的共享存储,同一份对象可能被别的会话引用,删了会连带破坏其它会话。
|
|
25
|
+
- 工作区里的排序占位 id:没有公开 API;下次启动按磁盘重新索引时会自动忽略,不影响任何显示。
|
|
26
|
+
- 工作区目录里的文件:只删会话记录,不碰你的代码和文档。
|
|
27
|
+
|
|
28
|
+
## 安全约束
|
|
29
|
+
|
|
30
|
+
- 只删 `sessionPersistence.stat()` 能查到的会话——id 必须真实存在,杜绝拼错 id 误删。
|
|
31
|
+
- 只删「basename 等于 `encodeSegment(id)`、且目录里确实有会话日志」的目录,并且要求位于会话根目录之内。
|
|
32
|
+
- **正在加载/运行中的会话一律拒绝**(包括你当前正在看的那个):请先切到别的会话再删。
|
|
33
|
+
- 删除不可恢复,UI 侧有二次确认。
|
|
34
|
+
|
|
35
|
+
## 安装
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
# 从 npm
|
|
39
|
+
dsh plugin --profile web add @wangyf397/session_del
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
或在 Web 的 **Plugins → 添加** 里填包名安装;Agent 也可以直接 `plugin_manager install_bundle`。
|
|
43
|
+
|
|
44
|
+
从源码目录安装(开发用):
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
dsh plugin --profile web add link:/Users/wangyf/code/dsh-plugins/session_del
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
安装后重启/刷新页面:侧栏每个会话的「…」菜单里会多出「彻底删除会话」。
|
|
51
|
+
|
|
52
|
+
## 已知限制
|
|
53
|
+
|
|
54
|
+
- 删除通过 `ctx.remote.commands.execute(sessionId, "/session-del delete …")` 落到 Host,
|
|
55
|
+
因此需要一个**当前打开的会话**作为命令的执行者(正在看的那个会话本身删不掉)。
|
|
56
|
+
命令会在当前会话里留下两条 log-only 的 `command/run`、`command/done` 记录,便于审计。
|
|
57
|
+
- 侧栏菜单采用「点两下 + 行内反馈」:第一下变成「再点一次,彻底删除」,第二下执行,
|
|
58
|
+
成功/失败信息都显示在那一行里(不弹窗、不写全局 DOM/样式)。
|
|
59
|
+
- 侧栏列表由客户端会话列表驱动:删除成功后该行要刷新页面才会消失;磁盘与索引在删除当刻就干净了。
|
|
60
|
+
- 工作区注册表里那条会话的排序占位 id 没有公开删除 API:显示时已被过滤,下次该工作区有任何变更时
|
|
61
|
+
会连带清掉。
|
|
62
|
+
|
|
63
|
+
## 发布
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
cd /Users/wangyf/code/dsh-plugins/session_del
|
|
67
|
+
npm publish --access public --registry=https://registry.npmjs.org
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
(`publishConfig` 已指定官方 registry 与 public;`engines.dsh` 限制在 `^0.2.1-rc.5`。)
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
package/client.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* session_del — Client half.
|
|
3
|
+
*
|
|
4
|
+
* 在侧栏每个会话的「…」菜单里加一行「彻底删除会话」。点一下变成确认态,
|
|
5
|
+
* 再点一下才真正执行;执行结果(成功 / Host 拒绝的原因)直接显示在这一行里。
|
|
6
|
+
*
|
|
7
|
+
* 删除动作通过 ctx.remote.commands.execute(sessionId, "/session-del delete <id>")
|
|
8
|
+
* 交给 Host 的 /session-del 命令完成——UI 与 Agent 工具共用同一套操作。
|
|
9
|
+
*
|
|
10
|
+
* 不引用任何 @deepseek-ai 客户端包,也不写全局 DOM/样式:视觉全部用内联样式
|
|
11
|
+
* 和主题 token(--dsw-alias-*),把出错面压到最小。
|
|
12
|
+
*
|
|
13
|
+
* @module @wangyf397/session_del/client
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
window.__ModuleLoader__.load({
|
|
17
|
+
id: '@wangyf397/session_del',
|
|
18
|
+
factory(require) {
|
|
19
|
+
const React = require('react')
|
|
20
|
+
const h = React.createElement
|
|
21
|
+
|
|
22
|
+
/** 运行时 locale 命名空间(同时用于插件元数据)。 */
|
|
23
|
+
const NS = 'session_del'
|
|
24
|
+
|
|
25
|
+
/** 中英文案。 */
|
|
26
|
+
const zh = {
|
|
27
|
+
'menu.delete': '彻底删除会话',
|
|
28
|
+
'menu.confirm': '再点一次,彻底删除',
|
|
29
|
+
'menu.busy': '正在删除…',
|
|
30
|
+
'menu.done': '已彻底删除',
|
|
31
|
+
'menu.failed': '删除失败:{reason}',
|
|
32
|
+
'error.noRunner': '没有可执行删除的会话,请先打开任意会话',
|
|
33
|
+
'error.noRemote': '无法连接 Host(ctx.remote.commands 不可用)',
|
|
34
|
+
'error.unknownCommand': 'Host 上没有 /session-del 命令(插件未生效?)',
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const en = {
|
|
38
|
+
'menu.delete': 'Delete session permanently',
|
|
39
|
+
'menu.confirm': 'Click again to delete permanently',
|
|
40
|
+
'menu.busy': 'Deleting…',
|
|
41
|
+
'menu.done': 'Deleted permanently',
|
|
42
|
+
'menu.failed': 'Delete failed: {reason}',
|
|
43
|
+
'error.noRunner': 'No session available to run the deletion; open any session first',
|
|
44
|
+
'error.noRemote': 'Cannot reach the Host (ctx.remote.commands is unavailable)',
|
|
45
|
+
'error.unknownCommand': 'The Host has no /session-del command (is the plugin active?)',
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 当前主视图里的会话 id:命令需要一个 agent 作为执行者。 */
|
|
49
|
+
function mainViewSessionId(state, excludeId) {
|
|
50
|
+
const byId = state?.byId ?? {}
|
|
51
|
+
for (const row of Object.values(byId)) {
|
|
52
|
+
if (!row || String(row.id) === String(excludeId)) continue
|
|
53
|
+
if ((row.retainedBy?.mainView ?? 0) > 0) return String(row.id)
|
|
54
|
+
}
|
|
55
|
+
return undefined
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function TrashIcon() {
|
|
59
|
+
return h(
|
|
60
|
+
'svg',
|
|
61
|
+
{
|
|
62
|
+
viewBox: '0 0 16 16',
|
|
63
|
+
width: 14,
|
|
64
|
+
height: 14,
|
|
65
|
+
'aria-hidden': true,
|
|
66
|
+
fill: 'none',
|
|
67
|
+
stroke: 'currentColor',
|
|
68
|
+
strokeWidth: 1.2,
|
|
69
|
+
strokeLinecap: 'round',
|
|
70
|
+
strokeLinejoin: 'round',
|
|
71
|
+
},
|
|
72
|
+
h('path', { d: 'M3 4.6h10' }),
|
|
73
|
+
h('path', { d: 'M6.4 4.6V3.3c0-.4.3-.7.7-.7h1.8c.4 0 .7.3.7.7v1.3' }),
|
|
74
|
+
h('path', { d: 'M4.5 4.6l.5 7.9c0 .5.4.9.9.9h4.2c.5 0 .9-.4.9-.9l.5-7.9' }),
|
|
75
|
+
h('path', { d: 'M6.8 7.1v3.9M9.2 7.1v3.9' })
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 从命令执行结果里取出要展示的文字。
|
|
81
|
+
* @param {object|undefined} result ctx.remote.commands.execute 的返回值
|
|
82
|
+
* @param {Function} t 翻译函数
|
|
83
|
+
* @returns {{ok: boolean, text: string}}
|
|
84
|
+
*/
|
|
85
|
+
function readResult(result, t) {
|
|
86
|
+
if (!result || result.ok !== true) {
|
|
87
|
+
const message = result?.error?.message ?? t('error.noRemote')
|
|
88
|
+
const code = result?.error?.code ? ` (${result.error.code})` : ''
|
|
89
|
+
return { ok: false, text: `${message}${code}` }
|
|
90
|
+
}
|
|
91
|
+
if (result.value === undefined) return { ok: false, text: t('error.unknownCommand') }
|
|
92
|
+
const execution = result.value.result ?? {}
|
|
93
|
+
return { ok: execution.kind === 'success', text: String(execution.text ?? '') }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 侧栏会话菜单里的一行:idle → confirm → busy → done/failed。
|
|
98
|
+
* @param {object} props sessionId / displayTitle / useMenuOpenState / useSessions / t
|
|
99
|
+
*/
|
|
100
|
+
function DeleteMenuItem(props) {
|
|
101
|
+
const { sessionId, displayTitle, useMenuOpenState, useSessions, t } = props
|
|
102
|
+
const translate = typeof t === 'function' ? t : (key, vars) => (vars ? `${key} ${JSON.stringify(vars)}` : key)
|
|
103
|
+
const [, setMenuOpen] = useMenuOpenState()
|
|
104
|
+
const sessionState = useSessions((state) => state)
|
|
105
|
+
|
|
106
|
+
const [phase, setPhase] = React.useState('idle')
|
|
107
|
+
const [reason, setReason] = React.useState('')
|
|
108
|
+
const [hover, setHover] = React.useState(false)
|
|
109
|
+
const [focused, setFocused] = React.useState(false)
|
|
110
|
+
const timerRef = React.useRef(null)
|
|
111
|
+
|
|
112
|
+
React.useEffect(
|
|
113
|
+
() => () => {
|
|
114
|
+
if (timerRef.current !== null) clearTimeout(timerRef.current)
|
|
115
|
+
},
|
|
116
|
+
[]
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
/** 执行删除。 */
|
|
120
|
+
const run = async () => {
|
|
121
|
+
const remote = ctxRef?.get?.('remote') ?? ctxRef?.remote
|
|
122
|
+
if (!remote?.commands?.execute) {
|
|
123
|
+
setPhase('failed')
|
|
124
|
+
setReason(translate('error.noRemote'))
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
const runner = mainViewSessionId(sessionState, sessionId)
|
|
128
|
+
if (!runner) {
|
|
129
|
+
setPhase('failed')
|
|
130
|
+
setReason(translate('error.noRunner'))
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
setPhase('busy')
|
|
134
|
+
try {
|
|
135
|
+
const result = await remote.commands.execute(runner, `/session-del delete ${String(sessionId)}`, [])
|
|
136
|
+
const outcome = readResult(result, translate)
|
|
137
|
+
if (outcome.ok) {
|
|
138
|
+
setPhase('done')
|
|
139
|
+
setReason('')
|
|
140
|
+
timerRef.current = setTimeout(() => {
|
|
141
|
+
timerRef.current = null
|
|
142
|
+
setMenuOpen(false)
|
|
143
|
+
}, 1200)
|
|
144
|
+
} else {
|
|
145
|
+
setPhase('failed')
|
|
146
|
+
setReason(outcome.text || 'error')
|
|
147
|
+
}
|
|
148
|
+
} catch (error) {
|
|
149
|
+
setPhase('failed')
|
|
150
|
+
setReason(String(error?.message ?? error))
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const label =
|
|
155
|
+
phase === 'confirm'
|
|
156
|
+
? translate('menu.confirm')
|
|
157
|
+
: phase === 'busy'
|
|
158
|
+
? translate('menu.busy')
|
|
159
|
+
: phase === 'done'
|
|
160
|
+
? translate('menu.done')
|
|
161
|
+
: phase === 'failed'
|
|
162
|
+
? translate('menu.failed', { reason })
|
|
163
|
+
: translate('menu.delete')
|
|
164
|
+
|
|
165
|
+
const danger = phase === 'confirm' || phase === 'failed'
|
|
166
|
+
const busy = phase === 'busy'
|
|
167
|
+
|
|
168
|
+
const style = {
|
|
169
|
+
display: 'flex',
|
|
170
|
+
alignItems: 'center',
|
|
171
|
+
gap: 8,
|
|
172
|
+
boxSizing: 'border-box',
|
|
173
|
+
width: '100%',
|
|
174
|
+
minWidth: 0,
|
|
175
|
+
height: 28,
|
|
176
|
+
padding: '0 8px',
|
|
177
|
+
margin: 0,
|
|
178
|
+
border: 0,
|
|
179
|
+
borderRadius: 6,
|
|
180
|
+
background: hover && !busy ? 'color-mix(in srgb, var(--dsw-alias-label-primary) 8%, transparent)' : 'transparent',
|
|
181
|
+
color: danger ? 'var(--dsw-alias-state-error-primary)' : 'var(--dsw-alias-label-primary)',
|
|
182
|
+
fontFamily: 'inherit',
|
|
183
|
+
fontSize: 13,
|
|
184
|
+
fontWeight: 400,
|
|
185
|
+
lineHeight: '18px',
|
|
186
|
+
textAlign: 'left',
|
|
187
|
+
cursor: busy ? 'default' : 'pointer',
|
|
188
|
+
opacity: busy ? 0.6 : 1,
|
|
189
|
+
outline: focused ? 'var(--dsw-focus-ring-width, 2px) solid var(--dsw-alias-brand-primary)' : 'none',
|
|
190
|
+
outlineOffset: focused ? -2 : 0,
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const labelStyle = {
|
|
194
|
+
minWidth: 0,
|
|
195
|
+
overflow: 'hidden',
|
|
196
|
+
whiteSpace: 'nowrap',
|
|
197
|
+
textOverflow: 'ellipsis',
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return h(
|
|
201
|
+
'button',
|
|
202
|
+
{
|
|
203
|
+
type: 'button',
|
|
204
|
+
role: 'menuitem',
|
|
205
|
+
style,
|
|
206
|
+
title: [displayTitle, phase === 'failed' ? reason : undefined].filter(Boolean).join(' — ') || undefined,
|
|
207
|
+
disabled: busy,
|
|
208
|
+
'aria-label': translate('menu.delete'),
|
|
209
|
+
onMouseEnter: () => setHover(true),
|
|
210
|
+
onMouseLeave: () => setHover(false),
|
|
211
|
+
onFocus: () => setFocused(true),
|
|
212
|
+
onBlur: () => setFocused(false),
|
|
213
|
+
onClick: () => {
|
|
214
|
+
if (phase === 'idle') {
|
|
215
|
+
setPhase('confirm')
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
if (phase === 'confirm' || phase === 'failed') {
|
|
219
|
+
void run()
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
h('span', { key: 'icon', style: { display: 'inline-flex', flex: 'none', width: 14, height: 14 } }, TrashIcon()),
|
|
224
|
+
h('span', { key: 'label', style: labelStyle }, label)
|
|
225
|
+
)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** 当前 client 上下文(供点击时按需取用 ctx.remote)。 */
|
|
229
|
+
let ctxRef = null
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
inject: ['slots', 'locale'],
|
|
233
|
+
apply(ctx) {
|
|
234
|
+
ctxRef = ctx
|
|
235
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'session_del: dictionaries')
|
|
236
|
+
|
|
237
|
+
ctx.slots.inject('sidebar.workspaces.session.menu.item', () =>
|
|
238
|
+
ctx.slots.register(
|
|
239
|
+
{
|
|
240
|
+
name: 'sidebar.workspaces.session.menu.item',
|
|
241
|
+
id: 'session_del',
|
|
242
|
+
order: 600,
|
|
243
|
+
locale: NS,
|
|
244
|
+
},
|
|
245
|
+
DeleteMenuItem
|
|
246
|
+
)
|
|
247
|
+
)
|
|
248
|
+
},
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
})
|
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="session_del">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="sd-bg" x1="0" y1="0" x2="64" y2="64" gradientUnits="userSpaceOnUse">
|
|
4
|
+
<stop offset="0" stop-color="#3b4a63"/>
|
|
5
|
+
<stop offset="1" stop-color="#1b2432"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect width="64" height="64" rx="14" fill="url(#sd-bg)"/>
|
|
9
|
+
<g fill="none" stroke="#ff8b7a" stroke-width="2.6" stroke-linecap="round" stroke-linejoin="round">
|
|
10
|
+
<path d="M20 22h24"/>
|
|
11
|
+
<path d="M27.5 22v-3.2c0-1.2 1-2.2 2.2-2.2h4.6c1.2 0 2.2 1 2.2 2.2V22"/>
|
|
12
|
+
<path d="M24 22l1.6 22c.1 1.6 1.4 2.8 3 2.8h6.8c1.6 0 2.9-1.2 3-2.8L40 22"/>
|
|
13
|
+
</g>
|
|
14
|
+
<g fill="#e8eef7">
|
|
15
|
+
<rect x="29.2" y="29" width="9.6" height="2.4" rx="1.2"/>
|
|
16
|
+
<rect x="29.2" y="35" width="9.6" height="2.4" rx="1.2"/>
|
|
17
|
+
<rect x="29.2" y="41" width="6" height="2.4" rx="1.2"/>
|
|
18
|
+
</g>
|
|
19
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* session_del — Host half.
|
|
3
|
+
*
|
|
4
|
+
* 「彻底删除历史对话」:把一个会话从磁盘上真正删除,而不是只归档隐藏。
|
|
5
|
+
*
|
|
6
|
+
* 删除范围(clean delete):
|
|
7
|
+
* 1. 会话存储目录 <sessionsRoot>/<projectKey(cwd)>/<encodeSegment(id)>/ —— 会话日志与锁文件;
|
|
8
|
+
* 2. 投影缓存 <storagesRoot>/session_projcache/sessions/<id>.json;
|
|
9
|
+
* 3. 工作区注册表里的归档 / 置顶记录(通过 workspaceRegistry 的公开 API)。
|
|
10
|
+
*
|
|
11
|
+
* 刻意不做:附件对象(attachments/ 是内容寻址的共享存储,可能被其它会话引用)、
|
|
12
|
+
* 工作区里的排序占位 id(没有公开 API,且下次启动时按存储重新索引会自动忽略)。
|
|
13
|
+
*
|
|
14
|
+
* 安全约束:
|
|
15
|
+
* - 只有 sessionPersistence 能 stat 到的会话才会被删(id 必须真实存在);
|
|
16
|
+
* - 运行中的会话(live agent / live session)一律拒绝;
|
|
17
|
+
* - 只删除 basename 等于 encodeSegment(id)、且确实含会话日志文件的目录。
|
|
18
|
+
*
|
|
19
|
+
* 对外提供三处入口,共用同一套操作:
|
|
20
|
+
* - Agent 工具 `session_del`(action: list | delete)
|
|
21
|
+
* - 人类命令 `/session-del list | delete <会话ID...>`
|
|
22
|
+
* - Web UI 的会话「…」菜单(经由 commands.execute 调用上面的命令)
|
|
23
|
+
*
|
|
24
|
+
* @module @wangyf397/session_del
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { existsSync } from 'node:fs'
|
|
28
|
+
import { readdir, rm, stat, unlink } from 'node:fs/promises'
|
|
29
|
+
import { homedir } from 'node:os'
|
|
30
|
+
import { join, resolve, sep } from 'node:path'
|
|
31
|
+
|
|
32
|
+
/** 工具 / 命令 / 菜单共用的名字。 */
|
|
33
|
+
const PLUGIN = 'session_del'
|
|
34
|
+
/** 会话存储后端与 JSON 存储后端的包名:用于从运行中的配置里读出真实根目录。 */
|
|
35
|
+
const SESSIONS_BACKEND = '@deepseek-ai/dsh-session-persistence-jsonl'
|
|
36
|
+
const STORAGE_BACKEND = '@deepseek-ai/dsh-storage-json'
|
|
37
|
+
/** 投影缓存所在的 storage domain。 */
|
|
38
|
+
const PROJECTION_DOMAIN = 'session_projcache'
|
|
39
|
+
/** 会话日志文件名(历史世代是 session.vN.jsonl[.zstd])。 */
|
|
40
|
+
const LOG_FILE = /^session(?:\.v\d+)?\.jsonl(?:\.zstd)?$/
|
|
41
|
+
|
|
42
|
+
const TOOL_NAME = 'session_del'
|
|
43
|
+
const COMMAND_NAME = 'session-del'
|
|
44
|
+
|
|
45
|
+
/* ------------------------------------------------------------------ */
|
|
46
|
+
/* 会话存储路径:与 @deepseek-ai/dsh-session-persistence-jsonl 保持一致的编码 */
|
|
47
|
+
/* ------------------------------------------------------------------ */
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 把任意字符串编码成一个安全的路径片段(与官方 JSONL 后端完全一致的算法:
|
|
51
|
+
* 只保留 [A-Za-z0-9._-],其余按 UTF-16 码元写成 ~XXXX)。
|
|
52
|
+
* @param {string} raw 原始片段(会话 id)。
|
|
53
|
+
* @returns {string} 单层安全路径片段。
|
|
54
|
+
*/
|
|
55
|
+
function encodeSegment(raw) {
|
|
56
|
+
if (raw.length === 0) throw new Error('cannot encode an empty path segment')
|
|
57
|
+
if (raw === '.') return '~002E'
|
|
58
|
+
if (raw === '..') return '~002E~002E'
|
|
59
|
+
let out = ''
|
|
60
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
61
|
+
const code = raw.charCodeAt(i)
|
|
62
|
+
const ch = String.fromCharCode(code)
|
|
63
|
+
if (ch !== '~' && /^[A-Za-z0-9._-]$/.test(ch)) out += ch
|
|
64
|
+
else out += `~${code.toString(16).toUpperCase().padStart(4, '0')}`
|
|
65
|
+
}
|
|
66
|
+
return out
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 项目目录名(与官方后端一致的 projectKey):分隔符折叠成 '-',不安全码元转义,
|
|
71
|
+
* 截断到 251 个字符。
|
|
72
|
+
* @param {string} cwd 会话的工作目录。
|
|
73
|
+
* @returns {string} 项目目录名。
|
|
74
|
+
*/
|
|
75
|
+
function projectKey(cwd) {
|
|
76
|
+
if (cwd.length === 0) throw new Error('cannot encode an empty project path')
|
|
77
|
+
let readable = ''
|
|
78
|
+
let separatorRun = false
|
|
79
|
+
for (let i = 0; i < cwd.length; i += 1) {
|
|
80
|
+
const code = cwd.charCodeAt(i)
|
|
81
|
+
const ch = String.fromCharCode(code)
|
|
82
|
+
if (ch === '/' || ch === '\\' || ch === ':') {
|
|
83
|
+
if (!separatorRun) readable += '-'
|
|
84
|
+
separatorRun = true
|
|
85
|
+
} else if (ch !== '~' && /^[A-Za-z0-9._-]$/.test(ch)) {
|
|
86
|
+
readable += ch
|
|
87
|
+
separatorRun = false
|
|
88
|
+
} else {
|
|
89
|
+
readable += `~${code.toString(16).toUpperCase().padStart(4, '0')}`
|
|
90
|
+
separatorRun = false
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return `--${(readable.replace(/^-+/, '') || 'root').slice(0, 251)}--`
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** DSH home:$DSH_HOME 优先,否则 ~/.dsh。 */
|
|
97
|
+
function dshHome() {
|
|
98
|
+
const configured = process.env.DSH_HOME?.trim()
|
|
99
|
+
return configured ? resolve(configured) : join(homedir(), '.dsh')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 从运行中的配置里读取某个插件行的根目录(例如会话存储、JSON 存储的实际 root),
|
|
104
|
+
* 这样自定义了 root 的部署也能删到正确的目录。
|
|
105
|
+
* @param {object} ctx 插件上下文。
|
|
106
|
+
* @param {string} packageName 插件包名。
|
|
107
|
+
* @param {string} key 配置字段名。
|
|
108
|
+
* @returns {string | undefined} 绝对路径。
|
|
109
|
+
*/
|
|
110
|
+
function configuredRoot(ctx, packageName, key) {
|
|
111
|
+
try {
|
|
112
|
+
const editor = ctx.get('configEditor')
|
|
113
|
+
if (!editor || typeof editor.configuration !== 'function') return undefined
|
|
114
|
+
for (const row of editor.configuration()) {
|
|
115
|
+
const entry = row?.entry
|
|
116
|
+
const name = entry?.name ?? entry?.options?.name
|
|
117
|
+
if (name !== packageName) continue
|
|
118
|
+
for (const layer of [row.override, row.inherited]) {
|
|
119
|
+
const value = layer?.[key]
|
|
120
|
+
if (typeof value === 'string' && value.trim().length > 0) return resolve(value.trim())
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} catch {
|
|
124
|
+
/* 配置读取只是优化项:失败就退回默认目录。 */
|
|
125
|
+
}
|
|
126
|
+
return undefined
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 会话日志根目录。 */
|
|
130
|
+
function sessionsRoot(ctx) {
|
|
131
|
+
return configuredRoot(ctx, SESSIONS_BACKEND, 'root') ?? join(dshHome(), 'sessions')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** JSON 存储根目录(投影缓存等 domain 都挂在它下面)。 */
|
|
135
|
+
function storagesRoot(ctx) {
|
|
136
|
+
return configuredRoot(ctx, STORAGE_BACKEND, 'root') ?? join(dshHome(), 'storages')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 判断路径是否为目录。 */
|
|
140
|
+
async function isDirectory(path) {
|
|
141
|
+
try {
|
|
142
|
+
return (await stat(path)).isDirectory()
|
|
143
|
+
} catch {
|
|
144
|
+
return false
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** 目录里是否真的躺着会话日志(删除前的最后一道确认)。 */
|
|
149
|
+
async function looksLikeSessionDir(path) {
|
|
150
|
+
try {
|
|
151
|
+
const entries = await readdir(path)
|
|
152
|
+
return entries.some((entry) => LOG_FILE.test(entry))
|
|
153
|
+
} catch {
|
|
154
|
+
return false
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 定位一个会话的存储目录:先按 header.cwd 精确计算,再退化为扫描所有项目目录,
|
|
160
|
+
* 兼容自定义 root / cwd 不可解析等情况。找不到就返回 undefined。
|
|
161
|
+
* @param {object} ctx 插件上下文。
|
|
162
|
+
* @param {object} header 会话 header。
|
|
163
|
+
* @returns {Promise<string | undefined>} 会话目录绝对路径。
|
|
164
|
+
*/
|
|
165
|
+
async function locateSessionDir(ctx, header) {
|
|
166
|
+
const root = sessionsRoot(ctx)
|
|
167
|
+
const segment = encodeSegment(String(header.id))
|
|
168
|
+
const candidates = []
|
|
169
|
+
if (typeof header.cwd === 'string' && header.cwd.length > 0) {
|
|
170
|
+
candidates.push(join(root, projectKey(header.cwd), segment))
|
|
171
|
+
}
|
|
172
|
+
candidates.push(join(root, '_no-cwd', segment))
|
|
173
|
+
for (const candidate of candidates) {
|
|
174
|
+
if (await isDirectory(candidate)) return candidate
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
178
|
+
if (!entry.isDirectory()) continue
|
|
179
|
+
const candidate = join(root, entry.name, segment)
|
|
180
|
+
if (await isDirectory(candidate)) return candidate
|
|
181
|
+
}
|
|
182
|
+
} catch {
|
|
183
|
+
/* root 不存在:下面统一按“找不到”处理。 */
|
|
184
|
+
}
|
|
185
|
+
return undefined
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* ------------------------------------------------------------------ */
|
|
189
|
+
/* 删除操作 */
|
|
190
|
+
/* ------------------------------------------------------------------ */
|
|
191
|
+
|
|
192
|
+
/** 删除投影缓存文件(后缀名与 id 一致;两种写法都试,容忍差异)。 */
|
|
193
|
+
async function removeProjectionCache(ctx, id) {
|
|
194
|
+
const dir = join(storagesRoot(ctx), PROJECTION_DOMAIN, 'sessions')
|
|
195
|
+
let removed = 0
|
|
196
|
+
for (const name of new Set([`${id}.json`, `${encodeSegment(String(id))}.json`])) {
|
|
197
|
+
try {
|
|
198
|
+
await unlink(join(dir, name))
|
|
199
|
+
removed += 1
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (error?.code !== 'ENOENT') throw error
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return removed
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** 清掉工作区注册表里的归档 / 置顶记录(排序占位没有公开 API,启动时会被重新索引忽略)。 */
|
|
208
|
+
async function forgetRegistry(ctx, id) {
|
|
209
|
+
const registry = ctx.get('workspaceRegistry')
|
|
210
|
+
if (!registry) return
|
|
211
|
+
for (const method of ['unarchiveSession', 'unpinSession']) {
|
|
212
|
+
try {
|
|
213
|
+
await registry[method]?.(id)
|
|
214
|
+
} catch (error) {
|
|
215
|
+
ctx.logger?.warn?.(`${PLUGIN}: ${method}('${id}') failed: ${String(error)}`)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** 人类可读的字节数。 */
|
|
221
|
+
function formatBytes(bytes) {
|
|
222
|
+
if (typeof bytes !== 'number' || !Number.isFinite(bytes) || bytes <= 0) return undefined
|
|
223
|
+
const units = ['B', 'KB', 'MB', 'GB']
|
|
224
|
+
let value = bytes
|
|
225
|
+
let unit = 0
|
|
226
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
227
|
+
value /= 1024
|
|
228
|
+
unit += 1
|
|
229
|
+
}
|
|
230
|
+
return `${value >= 10 || unit === 0 ? Math.round(value) : value.toFixed(1)}${units[unit]}`
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** 本地时间(不带时区偏移,便于阅读)。 */
|
|
234
|
+
function formatTime(ms) {
|
|
235
|
+
if (typeof ms !== 'number' || !Number.isFinite(ms)) return '?'
|
|
236
|
+
const date = new Date(ms)
|
|
237
|
+
const pad = (n) => String(n).padStart(2, '0')
|
|
238
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** 读取一条会话的标题(投影缓存里的零 IO 视图;没有就算了)。 */
|
|
242
|
+
function cachedTitle(ctx, header) {
|
|
243
|
+
try {
|
|
244
|
+
const cache = ctx.get('sessionProjectionCache')
|
|
245
|
+
const values = cache?.cachedSnapshot?.(header, ['title'])?.values
|
|
246
|
+
const raw = values?.title
|
|
247
|
+
if (typeof raw === 'string') return raw
|
|
248
|
+
if (raw && typeof raw === 'object' && typeof raw.title === 'string') return raw.title
|
|
249
|
+
} catch {
|
|
250
|
+
/* 标题只是方便辨认,读不到不影响任何操作。 */
|
|
251
|
+
}
|
|
252
|
+
return undefined
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** 会话存储服务(inject 保证它存在;这里用 ctx.get 取实例,便于测试替身)。 */
|
|
256
|
+
function persistenceOf(ctx) {
|
|
257
|
+
return ctx.get('sessionPersistence')
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 列出所有已持久化的会话(按创建时间倒序)。
|
|
262
|
+
* @param {object} ctx 插件上下文。
|
|
263
|
+
* @returns {Promise<Array<object>>} 会话行。
|
|
264
|
+
*/
|
|
265
|
+
async function listSessions(ctx) {
|
|
266
|
+
const snapshots = await persistenceOf(ctx).list()
|
|
267
|
+
const registry = ctx.get('workspaceRegistry')
|
|
268
|
+
const archived = new Set((registry?.archivedSessionIds ?? []).map(String))
|
|
269
|
+
const pinned = new Set((registry?.pinnedSessionIds ?? []).map(String))
|
|
270
|
+
const live = new Set((ctx.get('sessions')?.list() ?? []).map((session) => String(session.id)))
|
|
271
|
+
const rows = snapshots.map((snapshot) => {
|
|
272
|
+
const header = snapshot.header
|
|
273
|
+
const id = String(header.id)
|
|
274
|
+
return {
|
|
275
|
+
sessionId: id,
|
|
276
|
+
title: cachedTitle(ctx, header),
|
|
277
|
+
cwd: header.cwd,
|
|
278
|
+
createdAt: header.createdAt,
|
|
279
|
+
sizeBytes: snapshot.sizeBytes,
|
|
280
|
+
eventCount: snapshot.eventCount,
|
|
281
|
+
archived: archived.has(id),
|
|
282
|
+
pinned: pinned.has(id),
|
|
283
|
+
live: live.has(id),
|
|
284
|
+
parentSessionId: header.parentSession ? String(header.parentSession) : undefined,
|
|
285
|
+
origin: header.origin,
|
|
286
|
+
}
|
|
287
|
+
})
|
|
288
|
+
rows.sort((left, right) => (right.createdAt ?? 0) - (left.createdAt ?? 0))
|
|
289
|
+
return rows
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* 彻底删除一个会话。
|
|
294
|
+
* @param {object} ctx 插件上下文。
|
|
295
|
+
* @param {string} rawId 会话 id。
|
|
296
|
+
* @returns {Promise<object>} 单条结果。
|
|
297
|
+
*/
|
|
298
|
+
async function deleteOne(ctx, rawId) {
|
|
299
|
+
const id = String(rawId ?? '').trim()
|
|
300
|
+
if (id.length === 0) return { sessionId: id, status: 'invalid', reason: '空的会话 ID' }
|
|
301
|
+
|
|
302
|
+
const persistence = persistenceOf(ctx)
|
|
303
|
+
let snapshot
|
|
304
|
+
try {
|
|
305
|
+
snapshot = await persistence.stat(id)
|
|
306
|
+
} catch (error) {
|
|
307
|
+
return { sessionId: id, status: 'error', reason: `读取会话失败:${String(error?.message ?? error)}` }
|
|
308
|
+
}
|
|
309
|
+
if (snapshot === undefined) {
|
|
310
|
+
return { sessionId: id, status: 'not-found', reason: '没有这个已持久化的会话' }
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const liveSession = ctx.get('sessions')?.get(id)
|
|
314
|
+
const liveAgent = ctx.get('agents')?.get(id)
|
|
315
|
+
if (liveSession !== undefined || liveAgent !== undefined) {
|
|
316
|
+
return {
|
|
317
|
+
sessionId: id,
|
|
318
|
+
status: 'active',
|
|
319
|
+
reason: '会话仍处于加载/运行状态。请先切到别的会话(或重启 DSH)让它冷下来再删除。',
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const dir = await locateSessionDir(ctx, snapshot.header)
|
|
324
|
+
if (dir === undefined) {
|
|
325
|
+
return {
|
|
326
|
+
sessionId: id,
|
|
327
|
+
status: 'error',
|
|
328
|
+
reason: `找不到会话存储目录(sessionsRoot=${sessionsRoot(ctx)})`,
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const root = sessionsRoot(ctx)
|
|
332
|
+
const insideRoot = resolve(dir).startsWith(root + sep)
|
|
333
|
+
if (!insideRoot || !(await looksLikeSessionDir(dir))) {
|
|
334
|
+
return {
|
|
335
|
+
sessionId: id,
|
|
336
|
+
status: 'error',
|
|
337
|
+
reason: `拒绝删除可疑目录:${dir}`,
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const sizeBytes = snapshot.sizeBytes
|
|
342
|
+
const title = cachedTitle(ctx, snapshot.header)
|
|
343
|
+
await rm(dir, { recursive: true, force: true })
|
|
344
|
+
|
|
345
|
+
let cacheFiles = 0
|
|
346
|
+
try {
|
|
347
|
+
cacheFiles = await removeProjectionCache(ctx, id)
|
|
348
|
+
} catch (error) {
|
|
349
|
+
ctx.logger?.warn?.(`${PLUGIN}: projection cache cleanup failed for '${id}': ${String(error)}`)
|
|
350
|
+
}
|
|
351
|
+
await forgetRegistry(ctx, id)
|
|
352
|
+
|
|
353
|
+
let gone = false
|
|
354
|
+
try {
|
|
355
|
+
gone = (await persistence.stat(id)) === undefined
|
|
356
|
+
} catch {
|
|
357
|
+
gone = !existsSync(dir)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return {
|
|
361
|
+
sessionId: id,
|
|
362
|
+
status: gone ? 'deleted' : 'partial',
|
|
363
|
+
title,
|
|
364
|
+
sizeBytes,
|
|
365
|
+
freed: formatBytes(sizeBytes),
|
|
366
|
+
cacheFiles,
|
|
367
|
+
dir,
|
|
368
|
+
reason: gone ? undefined : '会话目录已删除,但存储层仍报告该会话存在',
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* 批量删除。
|
|
374
|
+
* @param {object} ctx 插件上下文。
|
|
375
|
+
* @param {readonly string[]} ids 会话 id 列表。
|
|
376
|
+
* @returns {Promise<Array<object>>} 每条的结果。
|
|
377
|
+
*/
|
|
378
|
+
async function deleteSessions(ctx, ids) {
|
|
379
|
+
const results = []
|
|
380
|
+
for (const id of ids) results.push(await deleteOne(ctx, id))
|
|
381
|
+
return results
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/* ------------------------------------------------------------------ */
|
|
385
|
+
/* 文本渲染 */
|
|
386
|
+
/* ------------------------------------------------------------------ */
|
|
387
|
+
|
|
388
|
+
/** 一行会话摘要。 */
|
|
389
|
+
function sessionLine(row, index) {
|
|
390
|
+
const flags = [row.live ? '运行中' : undefined, row.archived ? '已归档' : undefined, row.pinned ? '已置顶' : undefined]
|
|
391
|
+
.filter(Boolean)
|
|
392
|
+
.join(' / ')
|
|
393
|
+
const parts = [
|
|
394
|
+
`${index + 1}. ${row.sessionId}`,
|
|
395
|
+
row.title ? `“${row.title}”` : undefined,
|
|
396
|
+
formatTime(row.createdAt),
|
|
397
|
+
formatBytes(row.sizeBytes),
|
|
398
|
+
row.cwd,
|
|
399
|
+
flags || undefined,
|
|
400
|
+
].filter(Boolean)
|
|
401
|
+
return parts.join(' | ')
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** 渲染 list 结果。 */
|
|
405
|
+
function renderList(rows) {
|
|
406
|
+
if (rows.length === 0) return '没有任何已持久化的历史会话。'
|
|
407
|
+
const lines = [
|
|
408
|
+
`共 ${rows.length} 个历史会话(按创建时间倒序;“运行中”的会话要先切走再删):`,
|
|
409
|
+
...rows.map(sessionLine),
|
|
410
|
+
]
|
|
411
|
+
return lines.join('\n')
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** 渲染删除结果。 */
|
|
415
|
+
function renderResults(results) {
|
|
416
|
+
const ok = results.filter((row) => row.status === 'deleted').length
|
|
417
|
+
const failed = results.length - ok
|
|
418
|
+
const lines = [`删除完成:成功 ${ok} 个,失败/跳过 ${failed} 个。`]
|
|
419
|
+
for (const row of results) {
|
|
420
|
+
if (row.status === 'deleted') {
|
|
421
|
+
lines.push(`- ${row.sessionId}: 已彻底删除${row.freed ? `,释放 ${row.freed}` : ''}${row.title ? `(“${row.title}”)` : ''}`)
|
|
422
|
+
} else {
|
|
423
|
+
lines.push(`- ${row.sessionId}: ${row.status}${row.reason ? ` — ${row.reason}` : ''}`)
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return lines.join('\n')
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/* ------------------------------------------------------------------ */
|
|
430
|
+
/* Agent 工具 */
|
|
431
|
+
/* ------------------------------------------------------------------ */
|
|
432
|
+
|
|
433
|
+
/** 构造 `session_del` 工具定义(不依赖任何 @deepseek-ai 运行时包)。 */
|
|
434
|
+
function createTool(ctx) {
|
|
435
|
+
return {
|
|
436
|
+
name: TOOL_NAME,
|
|
437
|
+
description:
|
|
438
|
+
'Cleanly delete persisted Harness conversations (sessions) from disk: the session log directory, its projection cache, and its archive/pin records. ' +
|
|
439
|
+
'Use action "list" to see every stored session with its size, working directory, title and flags; use action "delete" with exact session_ids to remove them permanently. ' +
|
|
440
|
+
'Irreversible. A session that is loaded or running is refused — switch away from it first. Attachment objects and workspace files are never touched.',
|
|
441
|
+
parameters: {
|
|
442
|
+
type: 'object',
|
|
443
|
+
properties: {
|
|
444
|
+
action: {
|
|
445
|
+
type: 'string',
|
|
446
|
+
enum: ['list', 'delete'],
|
|
447
|
+
description: 'list: inspect stored sessions. delete: permanently remove the given session_ids.',
|
|
448
|
+
},
|
|
449
|
+
session_ids: {
|
|
450
|
+
type: 'array',
|
|
451
|
+
items: { type: 'string' },
|
|
452
|
+
description: 'Exact session ids to delete. Required for action "delete"; copy them from action "list".',
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
required: ['action'],
|
|
456
|
+
additionalProperties: false,
|
|
457
|
+
},
|
|
458
|
+
output: {
|
|
459
|
+
schema: {
|
|
460
|
+
type: 'object',
|
|
461
|
+
properties: {
|
|
462
|
+
text: { type: 'string', description: 'Human-readable report of the operation.' },
|
|
463
|
+
},
|
|
464
|
+
required: ['text'],
|
|
465
|
+
additionalProperties: false,
|
|
466
|
+
},
|
|
467
|
+
render: (_args, value) => [{ type: 'text', text: String(value?.text ?? '') }],
|
|
468
|
+
},
|
|
469
|
+
async execute(args, exec) {
|
|
470
|
+
const action = String(args?.action ?? 'list').toLowerCase()
|
|
471
|
+
if (action === 'list') return { text: renderList(await listSessions(ctx)) }
|
|
472
|
+
if (action !== 'delete') return { text: `未知 action: ${action}(只支持 list / delete)` }
|
|
473
|
+
const ids = Array.isArray(args?.session_ids) ? args.session_ids.map(String) : []
|
|
474
|
+
if (ids.length === 0) return { text: 'action "delete" 需要 session_ids(先用 action "list" 找到要删的会话 id)。' }
|
|
475
|
+
if (exec?.signal?.aborted) return { text: '已取消。' }
|
|
476
|
+
return { text: renderResults(await deleteSessions(ctx, ids)) }
|
|
477
|
+
},
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/* ------------------------------------------------------------------ */
|
|
482
|
+
/* 人类命令 */
|
|
483
|
+
/* ------------------------------------------------------------------ */
|
|
484
|
+
|
|
485
|
+
const COMMAND_USAGE =
|
|
486
|
+
'用法:/session-del list(列出历史会话) | /session-del delete <会话ID…>(彻底删除,可用空格或逗号分隔多个)'
|
|
487
|
+
|
|
488
|
+
/** 构造 `/session-del` 命令定义。 */
|
|
489
|
+
function createCommand(ctx) {
|
|
490
|
+
return {
|
|
491
|
+
name: COMMAND_NAME,
|
|
492
|
+
description: '彻底删除历史会话(会话日志 + 投影缓存 + 归档/置顶记录):list 查看,delete <会话ID…> 删除。',
|
|
493
|
+
input: { hint: 'list | delete <session-id...>' },
|
|
494
|
+
handler: async (invocation) => {
|
|
495
|
+
const raw = String(invocation?.rawInput ?? '').trim()
|
|
496
|
+
const tokens = raw.split(/[\s,,]+/).filter((token) => token.length > 0)
|
|
497
|
+
const action = (tokens.shift() ?? 'list').toLowerCase()
|
|
498
|
+
|
|
499
|
+
if (action === 'list' || action === 'ls') {
|
|
500
|
+
return { kind: 'success', text: renderList(await listSessions(ctx)) }
|
|
501
|
+
}
|
|
502
|
+
if (action === 'delete' || action === 'del' || action === 'rm') {
|
|
503
|
+
if (tokens.length === 0) return { kind: 'error', text: COMMAND_USAGE }
|
|
504
|
+
try {
|
|
505
|
+
const results = await deleteSessions(ctx, tokens)
|
|
506
|
+
const ok = results.some((row) => row.status === 'deleted')
|
|
507
|
+
return { kind: ok ? 'success' : 'error', text: renderResults(results) }
|
|
508
|
+
} catch (error) {
|
|
509
|
+
return { kind: 'error', text: `${PLUGIN}: 删除失败:${String(error?.message ?? error)}` }
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return { kind: 'error', text: `未知操作 “${action}”。\n${COMMAND_USAGE}` }
|
|
513
|
+
},
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/* ------------------------------------------------------------------ */
|
|
518
|
+
/* 插件入口 */
|
|
519
|
+
/* ------------------------------------------------------------------ */
|
|
520
|
+
|
|
521
|
+
export const inject = ['sessionPersistence']
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* 注册工具与命令。两者都只是同一套操作的两个入口。
|
|
525
|
+
* @param {object} ctx Cordis 插件上下文。
|
|
526
|
+
*/
|
|
527
|
+
export function apply(ctx) {
|
|
528
|
+
const tools = ctx.get('tools')
|
|
529
|
+
if (tools) {
|
|
530
|
+
ctx.effect(() => tools.register(createTool(ctx)), `${PLUGIN}: tool`)
|
|
531
|
+
}
|
|
532
|
+
const commands = ctx.get('commands')
|
|
533
|
+
if (commands) {
|
|
534
|
+
ctx.effect(() => commands.register(createCommand(ctx)), `${PLUGIN}: command`)
|
|
535
|
+
}
|
|
536
|
+
ctx.logger?.info?.(`${PLUGIN}: ready (sessionsRoot=${sessionsRoot(ctx)})`)
|
|
537
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "session_del",
|
|
4
|
+
"description": "Permanently delete chat history. One row in every session's \"…\" menu, the /session-del command, and the session_del tool remove a session's log directory, its projection cache and its archive/pin records — not just hide it behind an archive flag."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wangyf397/session_del",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "彻底删除 DeepSeek Harness 历史对话:侧栏会话「…」菜单 + /session-del 命令 + session_del 工具,删除会话日志、投影缓存与归档/置顶记录(不是归档隐藏)。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./client": "./client.js",
|
|
10
|
+
"./locale/*.json": "./locale/*.json",
|
|
11
|
+
"./icon": "./icon.svg",
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"client.js",
|
|
17
|
+
"cordis.patch.yml",
|
|
18
|
+
"locale/*.json",
|
|
19
|
+
"icon.svg",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"dsh": "^0.2.1-rc.5"
|
|
24
|
+
},
|
|
25
|
+
"dsh": {
|
|
26
|
+
"bundle": {
|
|
27
|
+
"patch": "./cordis.patch.yml"
|
|
28
|
+
},
|
|
29
|
+
"client": {
|
|
30
|
+
"platform": "web",
|
|
31
|
+
"immediately": true,
|
|
32
|
+
"inject": [
|
|
33
|
+
"@deepseek-ai/dsh-client-ui-workspace"
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public",
|
|
39
|
+
"registry": "https://registry.npmjs.org"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"dsh",
|
|
43
|
+
"deepseek-harness",
|
|
44
|
+
"cordis",
|
|
45
|
+
"plugin",
|
|
46
|
+
"session",
|
|
47
|
+
"session-delete",
|
|
48
|
+
"privacy"
|
|
49
|
+
],
|
|
50
|
+
"author": "wangyf397",
|
|
51
|
+
"license": "MIT"
|
|
52
|
+
}
|