@kangyishuai/dsh-session-delete 0.2.0 → 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 +7 -7
- package/README.zh-CN.md +7 -7
- package/lib/agent-tool.js +173 -0
- package/lib/client.js +78 -0
- package/lib/index.js +3 -2
- package/lib/operations.js +59 -5
- package/lib/service.js +114 -2
- package/locale/en.json +5 -1
- package/locale/zh.json +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
Unofficial plugin for [DeepSeek Harness (DSH)](https://github.com/deepseek-ai). Not affiliated with DeepSeek.
|
|
8
8
|
|
|
9
|
-
> **Status: v0.
|
|
9
|
+
> **Status: v0.3, early.** Verified on DSH 0.2.1-rc.5 with `pnpm smoke:session-delete` (23 end-to-end assertions) plus a manual pass in the desktop GUI. Treat it as young software and keep backups of anything you cannot lose.
|
|
10
10
|
|
|
11
11
|
## What it does
|
|
12
12
|
|
|
@@ -38,6 +38,8 @@ Restart the DSH desktop app afterwards. A fresh install adds a host half, so rel
|
|
|
38
38
|
|
|
39
39
|
## Usage
|
|
40
40
|
|
|
41
|
+
**Agent tool**: models can call `session_delete`, but only in two steps — `action=plan` returns the impact summary plus a single-use token (2 minutes), and `action=delete` needs that token. It refuses to delete the calling session, and a changed session invalidates the token. Deleting from the UI never requires the tool.
|
|
42
|
+
|
|
41
43
|
**Keyboard**: Tab between controls, Enter/Space to activate, Esc to close a dialog. In the trash panel, ↑/↓/Home/End move focus between rows. `⌘/Ctrl + Alt + T` opens the trash. There is no one-key delete shortcut — deletion always goes through the menu and a confirmation dialog.
|
|
42
44
|
|
|
43
45
|
**Refresh**: the trash panel has no refresh button. It loads on open and re-syncs every 5 seconds, when the window becomes visible again, and after every restore or permanent delete.
|
|
@@ -49,7 +51,7 @@ Everything starts from a session row in the sidebar:
|
|
|
49
51
|
2. The confirmation dialog shows:
|
|
50
52
|
- how much disk space the session occupies;
|
|
51
53
|
- whether the session is live (running turn, subagents, background jobs) — a live session is stopped first, not refused;
|
|
52
|
-
- how many sub-sessions it has (
|
|
54
|
+
- how many sub-sessions it has, with a checkbox to delete them together (off by default; each child goes to trash separately);
|
|
53
55
|
- the references that will be cleaned: archived set, pinned set, workspace membership, projection cache.
|
|
54
56
|
3. Confirm. The session moves to the trash and disappears from the sidebar.
|
|
55
57
|
4. **回收站…** (Trash…) in the same menu opens the trash panel, where you can:
|
|
@@ -95,7 +97,7 @@ v0.1 declares no `engines.dsh` range. Instead there are three layers of defense:
|
|
|
95
97
|
|
|
96
98
|
| Plugin | Verified on DSH | Notes |
|
|
97
99
|
| ------ | ------------------------ | ------------------------------------------------------------------------------------------- |
|
|
98
|
-
| 0.
|
|
100
|
+
| 0.3.0 | **0.2.1-rc.5** (desktop) | `pnpm smoke:session-delete`: 21 assertions green; manual GUI pass on the same host version. |
|
|
99
101
|
|
|
100
102
|
After a DSH upgrade: restart, then open the plugin status (settings → plugins). If it reports read-only mode, the host API changed — upgrade the plugin or report the version pair; don't force it.
|
|
101
103
|
|
|
@@ -110,13 +112,11 @@ After a DSH upgrade: restart, then open the plugin status (settings → plugins)
|
|
|
110
112
|
| Purge left something behind | Residue that the post-delete receipt lists | Read the receipt and the audit log, clean the listed path by hand, and open an issue with the receipt |
|
|
111
113
|
| The trash panel is empty after a delete | The plugin resolved a different data root than your DSH (`DSH_HOME` / config) | Look at `~/.dsh/session-trash/` on disk and check the plugin's `dataRoot` config |
|
|
112
114
|
|
|
113
|
-
## Roadmap (
|
|
115
|
+
## Roadmap (later releases)
|
|
114
116
|
|
|
115
|
-
The following are planned and
|
|
117
|
+
The following are planned and not implemented yet:
|
|
116
118
|
|
|
117
119
|
- batch deletion (multi-select);
|
|
118
|
-
- cascade deletion with a checkbox per sub-session;
|
|
119
|
-
- an agent tool (plan first, then delete);
|
|
120
120
|
- an undo toast after a delete.
|
|
121
121
|
|
|
122
122
|
## License
|
package/README.zh-CN.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
非官方 [DeepSeek Harness (DSH)](https://github.com/deepseek-ai) 插件,与 DeepSeek 无关。
|
|
8
8
|
|
|
9
|
-
> **状态:v0.
|
|
9
|
+
> **状态:v0.3,早期可用。** 已在 DSH 0.2.1-rc.5 上用 `pnpm smoke:session-delete`(23 项端到端断言)加一轮桌面版人工验收。仍是年轻软件,重要数据请自行备份。
|
|
10
10
|
|
|
11
11
|
## 做什么
|
|
12
12
|
|
|
@@ -38,6 +38,8 @@ dsh plugin --profile desktop add /tmp/kangyishuai-dsh-session-delete-*.tgz
|
|
|
38
38
|
|
|
39
39
|
## 使用
|
|
40
40
|
|
|
41
|
+
**Agent 工具**:模型可以调用 `session_delete`,但必须两段式——`action=plan` 返回影响面摘要和一次性 token(2 分钟有效),`action=delete` 必须带这个 token。它拒绝删除调用者自己的会话,会话在此期间变化会让 token 失效。通过 UI 删除不需要这个工具。
|
|
42
|
+
|
|
41
43
|
**键盘**:Tab 在各按钮间移动,Enter/Space 触发,Esc 关闭对话框;回收站面板里可用 ↑/↓/Home/End 在条目间移动焦点;`⌘/Ctrl + Alt + T` 直接打开回收站。删除始终需要菜单 + 确认框,没有一键删除的快捷键。
|
|
42
44
|
|
|
43
45
|
**刷新**:回收站面板没有刷新按钮——打开时自动加载,之后每 5 秒、窗口重新可见、以及每次恢复/彻底删除后自动同步。
|
|
@@ -49,7 +51,7 @@ dsh plugin --profile desktop add /tmp/kangyishuai-dsh-session-delete-*.tgz
|
|
|
49
51
|
2. 确认框会展示:
|
|
50
52
|
- 这条会话在磁盘上占多少体积;
|
|
51
53
|
- 会话是否 live(有没有在跑的轮次、子代理、后台任务)——live 会话会先被停下再删,而不是拒绝;
|
|
52
|
-
-
|
|
54
|
+
- 有几个子会话,并给一个勾选框决定是否一并删除(默认不勾;每个子会话单独进回收站);
|
|
53
55
|
- 将被清理的引用:归档集合、置顶集合、工作区归属、投影缓存。
|
|
54
56
|
3. 确认。会话进入回收站,侧栏里的那一行消失。
|
|
55
57
|
4. 同一个菜单里的「回收站…」打开回收站面板,可以:
|
|
@@ -95,7 +97,7 @@ v0.1 没有声明 `engines.dsh` 版本范围,改用三层防线:
|
|
|
95
97
|
|
|
96
98
|
| 插件版本 | 已验证的 DSH 版本 | 说明 |
|
|
97
99
|
| -------- | ------------------------ | -------------------------------------------------------------------------------- |
|
|
98
|
-
| 0.
|
|
100
|
+
| 0.3.0 | **0.2.1-rc.5**(桌面版) | `pnpm smoke:session-delete` 21 项断言全绿,并在同一版本桌面版做过人工 GUI 验收。 |
|
|
99
101
|
|
|
100
102
|
DSH 升级之后:重启,然后看插件的状态条(设置 → 插件)。如果显示只读模式,说明宿主 API 变了——升级插件,或者把两个版本号一起报上来,别硬用。
|
|
101
103
|
|
|
@@ -110,13 +112,11 @@ DSH 升级之后:重启,然后看插件的状态条(设置 → 插件)
|
|
|
110
112
|
| 彻底删除后还残留东西 | 删除回执里列出的残留 | 对回执和审计日志,按回执里的路径手动清理,并把回执附在 issue 里 |
|
|
111
113
|
| 删完之后回收站是空的 | 插件解析到的数据根目录和你的 DSH 不是同一个(`DSH_HOME` / 配置) | 去磁盘上看 `~/.dsh/session-trash/`,再核对插件的 `dataRoot` 配置 |
|
|
112
114
|
|
|
113
|
-
##
|
|
115
|
+
## 计划中(后续版本)
|
|
114
116
|
|
|
115
|
-
|
|
117
|
+
下面这些是计划,尚未实现:
|
|
116
118
|
|
|
117
119
|
- 批量删除(多选);
|
|
118
|
-
- 子会话级联删除(逐个子会话勾选);
|
|
119
|
-
- agent 工具(先 plan 再删);
|
|
120
120
|
- 删除后的撤销 toast。
|
|
121
121
|
|
|
122
122
|
## License
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 模型工具的两段式控制器:先 plan 拿一次性 token,再 delete。
|
|
3
|
+
// 只做状态机与校验,不直接依赖 @deepseek-ai/*,方便单测。
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
|
|
6
|
+
const DEFAULT_TTL_MS = 120_000;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {object} options
|
|
10
|
+
* @param {{ plan: (sessionId: string) => Promise<any>, trash: (sessionId: string, request: any) => Promise<any> }} options.ops
|
|
11
|
+
* @param {() => number} [options.now]
|
|
12
|
+
* @param {number} [options.ttlMs]
|
|
13
|
+
*/
|
|
14
|
+
export function createAgentToolController({ ops, now = Date.now, ttlMs = DEFAULT_TTL_MS }) {
|
|
15
|
+
/** @type {Map<string, { sessionId: string, digest: string, cascade: boolean, expiresAt: number }>} */
|
|
16
|
+
const tokens = new Map();
|
|
17
|
+
|
|
18
|
+
/** @param {string} token */
|
|
19
|
+
function takeToken(token) {
|
|
20
|
+
const entry = tokens.get(token);
|
|
21
|
+
if (!entry) return { error: "invalid-token" };
|
|
22
|
+
tokens.delete(token);
|
|
23
|
+
if (entry.expiresAt < now()) return { error: "token-expired" };
|
|
24
|
+
return { entry };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 第一步:只读预览 + 发 token。
|
|
29
|
+
* @param {{ sessionId: string, cascade?: boolean }} args
|
|
30
|
+
*/
|
|
31
|
+
async function plan(args) {
|
|
32
|
+
const plan = await ops.plan(args.sessionId);
|
|
33
|
+
if (plan.blockers.length > 0) {
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
code: "blocked",
|
|
37
|
+
sessionId: plan.sessionId,
|
|
38
|
+
blockers: plan.blockers,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const token = randomUUID();
|
|
42
|
+
tokens.set(token, {
|
|
43
|
+
sessionId: plan.sessionId,
|
|
44
|
+
digest: plan.digest,
|
|
45
|
+
cascade: args.cascade === true,
|
|
46
|
+
expiresAt: now() + ttlMs,
|
|
47
|
+
});
|
|
48
|
+
// 简单上限,防止模型反复 plan 把内存撑大
|
|
49
|
+
while (tokens.size > 32) {
|
|
50
|
+
const oldest = tokens.keys().next().value;
|
|
51
|
+
if (oldest === undefined) break;
|
|
52
|
+
tokens.delete(oldest);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
ok: true,
|
|
56
|
+
sessionId: plan.sessionId,
|
|
57
|
+
title: plan.title,
|
|
58
|
+
sizeBytes: plan.sizeBytes,
|
|
59
|
+
live: plan.live,
|
|
60
|
+
children: plan.children.map((/** @type {any} */ child) => ({
|
|
61
|
+
id: child.id,
|
|
62
|
+
title: child.title,
|
|
63
|
+
})),
|
|
64
|
+
cascade: args.cascade === true,
|
|
65
|
+
digest: plan.digest,
|
|
66
|
+
token,
|
|
67
|
+
expiresAt: new Date(now() + ttlMs).toISOString(),
|
|
68
|
+
hint: "call this tool again with action=delete and the token to execute; the token is single-use and expires in 2 minutes",
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 第二步:带 token 执行删除。
|
|
74
|
+
* @param {{ sessionId: string, token?: string, cascade?: boolean, callerSessionId?: string }} args
|
|
75
|
+
*/
|
|
76
|
+
async function remove(args) {
|
|
77
|
+
if (args.callerSessionId && args.callerSessionId === args.sessionId) {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
code: "caller-session",
|
|
81
|
+
message: "refusing to delete the session that is calling this tool",
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (!args.token)
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
code: "token-required",
|
|
88
|
+
message: "call action=plan first to obtain a token",
|
|
89
|
+
};
|
|
90
|
+
const taken = takeToken(args.token);
|
|
91
|
+
if (taken.error)
|
|
92
|
+
return {
|
|
93
|
+
ok: false,
|
|
94
|
+
code: taken.error,
|
|
95
|
+
message: "token is invalid or expired; run action=plan again",
|
|
96
|
+
};
|
|
97
|
+
const entry = /** @type {any} */ (taken.entry);
|
|
98
|
+
if (entry.sessionId !== args.sessionId) {
|
|
99
|
+
return { ok: false, code: "token-mismatch", message: "token was issued for another session" };
|
|
100
|
+
}
|
|
101
|
+
const plan = await ops.plan(args.sessionId);
|
|
102
|
+
if (plan.blockers.length > 0) {
|
|
103
|
+
return { ok: false, code: "blocked", blockers: plan.blockers };
|
|
104
|
+
}
|
|
105
|
+
if (plan.digest !== entry.digest) {
|
|
106
|
+
return {
|
|
107
|
+
ok: false,
|
|
108
|
+
code: "stale-plan",
|
|
109
|
+
message: "session changed after the preview; run action=plan again",
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const result = await ops.trash(args.sessionId, {
|
|
113
|
+
digest: plan.digest,
|
|
114
|
+
cascade: args.cascade === true || entry.cascade === true,
|
|
115
|
+
operator: "agent",
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
ok: true,
|
|
119
|
+
sessionId: args.sessionId,
|
|
120
|
+
entryId: result.entryId,
|
|
121
|
+
receiptId: result.receiptId,
|
|
122
|
+
cascaded: result.cascaded,
|
|
123
|
+
removed: result.removed,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return { plan, remove, size: () => tokens.size };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 生成给模型看的纯文本摘要。
|
|
132
|
+
* @param {any} planResult
|
|
133
|
+
* @returns {string}
|
|
134
|
+
*/
|
|
135
|
+
export function summarizePlanForModel(planResult) {
|
|
136
|
+
if (!planResult.ok) {
|
|
137
|
+
const blockers = (planResult.blockers ?? [])
|
|
138
|
+
.map((/** @type {any} */ item) => item.code)
|
|
139
|
+
.join(", ");
|
|
140
|
+
return (
|
|
141
|
+
"Delete preview failed: " +
|
|
142
|
+
(planResult.code || "blocked") +
|
|
143
|
+
(blockers ? " (" + blockers + ")" : "")
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
const parts = [
|
|
147
|
+
"Delete preview for " + (planResult.title || planResult.sessionId),
|
|
148
|
+
"size " +
|
|
149
|
+
(typeof planResult.sizeBytes === "number" ? planResult.sizeBytes + " bytes" : "unknown"),
|
|
150
|
+
];
|
|
151
|
+
if (planResult.live) parts.push("session is live");
|
|
152
|
+
if (planResult.children.length > 0) parts.push(planResult.children.length + " child session(s)");
|
|
153
|
+
parts.push("token " + planResult.token + " expires " + planResult.expiresAt);
|
|
154
|
+
return parts.join(" | ");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 把内部错误归一成模型能理解的返回:session/not-found → not-found。
|
|
159
|
+
* @param {unknown} error
|
|
160
|
+
* @returns {{ ok: false, code: string, message: string }}
|
|
161
|
+
*/
|
|
162
|
+
export function normalizeToolError(error) {
|
|
163
|
+
const raw = /** @type {any} */ (error);
|
|
164
|
+
const code =
|
|
165
|
+
typeof raw?.code === "string" && raw.code.startsWith("session/")
|
|
166
|
+
? raw.code.slice("session/".length)
|
|
167
|
+
: "error";
|
|
168
|
+
return {
|
|
169
|
+
ok: false,
|
|
170
|
+
code,
|
|
171
|
+
message: error instanceof Error ? error.message : String(error),
|
|
172
|
+
};
|
|
173
|
+
}
|
package/lib/client.js
CHANGED
|
@@ -55,6 +55,7 @@ window.__ModuleLoader__.load({
|
|
|
55
55
|
"confirm.size": "会话日志约 {size}",
|
|
56
56
|
"confirm.live": "该会话仍在本机打开,删除前会先停止它的工作。",
|
|
57
57
|
"confirm.children": "会一并带上 {n} 个子代理会话",
|
|
58
|
+
"confirm.cascade": "同时删除这 {n} 个子会话(各自进入回收站,需分别恢复)",
|
|
58
59
|
"confirm.refs": "将清理:{list}",
|
|
59
60
|
"confirm.trashPath": "回收站位置:{path}",
|
|
60
61
|
"confirm.blocked": "暂时无法删除:{reason}",
|
|
@@ -76,6 +77,9 @@ window.__ModuleLoader__.load({
|
|
|
76
77
|
"已存 {stored} · live {live} · 归档 {archived} · 回收站 {trash} · 待处理 {pending}",
|
|
77
78
|
"health.ok": "未发现失效引用。",
|
|
78
79
|
"health.items": "{n} 条",
|
|
80
|
+
"health.agentTool": "模型工具 session_delete:{state}",
|
|
81
|
+
"health.agentToolOn": "已注册",
|
|
82
|
+
"health.agentToolOff": "未注册(宿主未提供 tools 运行时)",
|
|
79
83
|
"health.unavailable": "无法体检:{reason}",
|
|
80
84
|
"health.clean": "清理失效引用({n})",
|
|
81
85
|
"health.cleanConfirm": "确认清理 {n} 条?只动登记表,不碰会话文件",
|
|
@@ -105,6 +109,7 @@ window.__ModuleLoader__.load({
|
|
|
105
109
|
"confirm.size": "Session log is about {size}",
|
|
106
110
|
"confirm.live": "This session is still open; its work is stopped first.",
|
|
107
111
|
"confirm.children": "{n} subagent session(s) come along",
|
|
112
|
+
"confirm.cascade": "Also delete these {n} child session(s) (each goes to trash separately)",
|
|
108
113
|
"confirm.refs": "Will clean: {list}",
|
|
109
114
|
"confirm.trashPath": "Trash location: {path}",
|
|
110
115
|
"confirm.blocked": "Cannot delete right now: {reason}",
|
|
@@ -126,6 +131,9 @@ window.__ModuleLoader__.load({
|
|
|
126
131
|
"stored {stored} · live {live} · archived {archived} · trash {trash} · pending {pending}",
|
|
127
132
|
"health.ok": "No dangling references found.",
|
|
128
133
|
"health.items": "{n} item(s)",
|
|
134
|
+
"health.agentTool": "Agent tool session_delete: {state}",
|
|
135
|
+
"health.agentToolOn": "registered",
|
|
136
|
+
"health.agentToolOff": "not registered (host has no tools runtime)",
|
|
129
137
|
"health.unavailable": "Cannot run the check: {reason}",
|
|
130
138
|
"health.clean": "Clean dangling references ({n})",
|
|
131
139
|
"health.cleanConfirm": "Clean {n} dangling reference(s)? Bookkeeping only, no session files.",
|
|
@@ -467,12 +475,16 @@ window.__ModuleLoader__.load({
|
|
|
467
475
|
var busy = busyState[0];
|
|
468
476
|
var setBusy = busyState[1];
|
|
469
477
|
var requestId = request ? request.sessionId : null;
|
|
478
|
+
var cascadeState = React.useState(false);
|
|
479
|
+
var cascade = cascadeState[0];
|
|
480
|
+
var setCascade = cascadeState[1];
|
|
470
481
|
|
|
471
482
|
React.useEffect(() => {
|
|
472
483
|
if (!requestId) {
|
|
473
484
|
setPlan(null);
|
|
474
485
|
setError(null);
|
|
475
486
|
setBusy(false);
|
|
487
|
+
setCascade(false);
|
|
476
488
|
return undefined;
|
|
477
489
|
}
|
|
478
490
|
var alive = true;
|
|
@@ -505,6 +517,7 @@ window.__ModuleLoader__.load({
|
|
|
505
517
|
sessionId: request.sessionId,
|
|
506
518
|
digest: plan.digest,
|
|
507
519
|
title: request.title || null,
|
|
520
|
+
cascade: cascade && plan.children.length > 0,
|
|
508
521
|
})
|
|
509
522
|
.then((/** @type {any} */ result) => {
|
|
510
523
|
props.requestStore.set(null);
|
|
@@ -541,6 +554,42 @@ window.__ModuleLoader__.load({
|
|
|
541
554
|
request.title
|
|
542
555
|
? h("div", { style: { fontWeight: 600, marginBottom: "10px" } }, request.title)
|
|
543
556
|
: null,
|
|
557
|
+
plan && plan.children.length > 0
|
|
558
|
+
? h(
|
|
559
|
+
"label",
|
|
560
|
+
{
|
|
561
|
+
style: {
|
|
562
|
+
display: "flex",
|
|
563
|
+
alignItems: "flex-start",
|
|
564
|
+
gap: "8px",
|
|
565
|
+
margin: "8px 0 4px",
|
|
566
|
+
cursor: "pointer",
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
h("input", {
|
|
570
|
+
type: "checkbox",
|
|
571
|
+
checked: cascade,
|
|
572
|
+
disabled: busy,
|
|
573
|
+
onChange: (/** @type {any} */ event) => setCascade(Boolean(event.target.checked)),
|
|
574
|
+
style: { marginTop: "2px" },
|
|
575
|
+
}),
|
|
576
|
+
h(
|
|
577
|
+
"span",
|
|
578
|
+
null,
|
|
579
|
+
props.t("confirm.cascade", { n: plan.children.length }),
|
|
580
|
+
h(
|
|
581
|
+
"span",
|
|
582
|
+
{
|
|
583
|
+
style: { display: "block", color: T.faint, fontSize: "12px", marginTop: "2px" },
|
|
584
|
+
},
|
|
585
|
+
plan.children
|
|
586
|
+
.slice(0, 4)
|
|
587
|
+
.map((/** @type {any} */ child) => child.title || child.id)
|
|
588
|
+
.join(" · ") + (plan.children.length > 4 ? " …" : ""),
|
|
589
|
+
),
|
|
590
|
+
),
|
|
591
|
+
)
|
|
592
|
+
: null,
|
|
544
593
|
lines.map((/** @type {any} */ line, /** @type {any} */ index) =>
|
|
545
594
|
h("div", { key: "line-" + index, style: { marginBottom: "4px" } }, line),
|
|
546
595
|
),
|
|
@@ -893,6 +942,17 @@ window.__ModuleLoader__.load({
|
|
|
893
942
|
props.t("health.inventory", scan.inventory || {}),
|
|
894
943
|
),
|
|
895
944
|
);
|
|
945
|
+
content.push(
|
|
946
|
+
h(
|
|
947
|
+
"div",
|
|
948
|
+
{ key: "scan-agent-tool", style: { color: T.dim, fontSize: "12px" } },
|
|
949
|
+
props.t("health.agentTool", {
|
|
950
|
+
state: scan.agentTool
|
|
951
|
+
? props.t("health.agentToolOn")
|
|
952
|
+
: props.t("health.agentToolOff"),
|
|
953
|
+
}),
|
|
954
|
+
),
|
|
955
|
+
);
|
|
896
956
|
if (scan.findings.length === 0) {
|
|
897
957
|
content.push(
|
|
898
958
|
h("div", { key: "scan-ok", style: { padding: "8px 0" } }, props.t("health.ok")),
|
|
@@ -1326,6 +1386,24 @@ window.__ModuleLoader__.load({
|
|
|
1326
1386
|
void refreshRows(result && result.sessionId ? result.sessionId : undefined);
|
|
1327
1387
|
tickStore.set(tickStore.getSnapshot() + 1);
|
|
1328
1388
|
};
|
|
1389
|
+
// 外部变更(agent 工具、另一个窗口、CLI)不会通知本页面,
|
|
1390
|
+
// 定期拉一次会话列表;页面重新可见时也立刻拉一次。
|
|
1391
|
+
ctx.effect(() => {
|
|
1392
|
+
const pull = () => {
|
|
1393
|
+
if (document.visibilityState === "visible") {
|
|
1394
|
+
void refreshRows(undefined);
|
|
1395
|
+
// 回收站条目也可能被 agent 工具/另一个窗口改变,顺带刷新页脚计数
|
|
1396
|
+
tickStore.set(tickStore.getSnapshot() + 1);
|
|
1397
|
+
}
|
|
1398
|
+
};
|
|
1399
|
+
const timer = setInterval(pull, 10_000);
|
|
1400
|
+
document.addEventListener("visibilitychange", pull);
|
|
1401
|
+
return () => {
|
|
1402
|
+
clearInterval(timer);
|
|
1403
|
+
document.removeEventListener("visibilitychange", pull);
|
|
1404
|
+
};
|
|
1405
|
+
}, "session-delete: poll external session removals");
|
|
1406
|
+
|
|
1329
1407
|
const onPurged = (/** @type {any} */ sessionId) => {
|
|
1330
1408
|
void refreshRows(sessionId || undefined);
|
|
1331
1409
|
};
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
-
// host 半入口:Cordis
|
|
3
|
-
//
|
|
2
|
+
// host 半入口:Cordis 插件(类形态)。Remote 方法必须定义在导出的这个类自身的原型上,
|
|
3
|
+
// 否则 Typert 网关不会 claim 端点(不要包一层子类)。
|
|
4
|
+
// 模型工具 session_delete 在 SessionDeleteService 构造函数里注册。
|
|
4
5
|
import { SessionDeleteService } from "./service.js";
|
|
5
6
|
|
|
6
7
|
export { SessionDeleteService };
|
package/lib/operations.js
CHANGED
|
@@ -105,11 +105,49 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
const activity = normalizeActivity(rawActivity);
|
|
108
|
+
|
|
109
|
+
// 子会话 = live 活动里的 subagent + 持久层 header.parentSession 血缘(任意深度)。
|
|
110
|
+
// 后者需要 persistence.list():对当前格式是 O(1) 级别,对历史格式可能按会话数扫描,
|
|
111
|
+
// 因此只在真的删除/预览时调用,不参与面板等批量路径。
|
|
112
|
+
/** @type {Map<string, { id: string, title: string | null }>} */
|
|
113
|
+
const childMap = new Map();
|
|
108
114
|
const subagents = activity.find((group) => group.kind === "subagent");
|
|
109
|
-
|
|
110
|
-
id,
|
|
111
|
-
|
|
112
|
-
|
|
115
|
+
(subagents?.ids ?? []).forEach((id, index) => {
|
|
116
|
+
childMap.set(id, { id, title: subagents?.labels[index] ?? null });
|
|
117
|
+
});
|
|
118
|
+
if (caps.checks["persistence.list"] && caps.services.persistence?.list) {
|
|
119
|
+
try {
|
|
120
|
+
const all = (await caps.services.persistence.list()) ?? [];
|
|
121
|
+
/** @type {Map<string, { id: string, title: string | null }[]>} */
|
|
122
|
+
const byParent = new Map();
|
|
123
|
+
for (const entry of all) {
|
|
124
|
+
const id = String(entry?.id ?? entry?.header?.id ?? "");
|
|
125
|
+
const parent = entry?.header?.parentSession;
|
|
126
|
+
if (!id || typeof parent !== "string" || parent.length === 0) continue;
|
|
127
|
+
const list = byParent.get(parent) ?? [];
|
|
128
|
+
list.push({
|
|
129
|
+
id,
|
|
130
|
+
title: typeof entry?.header?.title === "string" ? entry.header.title : null,
|
|
131
|
+
});
|
|
132
|
+
byParent.set(parent, list);
|
|
133
|
+
}
|
|
134
|
+
const queue = [sessionId];
|
|
135
|
+
const seen = new Set([sessionId]);
|
|
136
|
+
while (queue.length > 0) {
|
|
137
|
+
const current = /** @type {string} */ (queue.shift());
|
|
138
|
+
for (const child of byParent.get(current) ?? []) {
|
|
139
|
+
if (seen.has(child.id)) continue;
|
|
140
|
+
seen.add(child.id);
|
|
141
|
+
childMap.set(child.id, child);
|
|
142
|
+
queue.push(child.id);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
} catch (error) {
|
|
146
|
+
notes.push("lineage unavailable: " + messageOf(error));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
childMap.delete(sessionId);
|
|
150
|
+
const children = [...childMap.values()];
|
|
113
151
|
|
|
114
152
|
const located = await locateSession({
|
|
115
153
|
dataRoot,
|
|
@@ -199,7 +237,7 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
199
237
|
|
|
200
238
|
/**
|
|
201
239
|
* @param {string} sessionIdRaw
|
|
202
|
-
* @param {{ digest?: string, title?: string | null, operator?: string }} request
|
|
240
|
+
* @param {{ digest?: string, title?: string | null, operator?: string, cascade?: boolean, child?: boolean }} request
|
|
203
241
|
*/
|
|
204
242
|
async function trash(sessionIdRaw, request = {}) {
|
|
205
243
|
const loaded = await makePlan(sessionIdRaw);
|
|
@@ -228,6 +266,20 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
228
266
|
});
|
|
229
267
|
}
|
|
230
268
|
if (!location) throw failure("session-delete/location-unknown", "没有可移动的会话目录");
|
|
269
|
+
|
|
270
|
+
/** @type {{ id: string, ok: boolean, entryId?: string, error?: string }[]} */
|
|
271
|
+
const cascaded = [];
|
|
272
|
+
if (request.cascade === true && request.child !== true) {
|
|
273
|
+
for (const child of plan.children) {
|
|
274
|
+
try {
|
|
275
|
+
const result = await trash(child.id, { operator: request.operator, child: true });
|
|
276
|
+
cascaded.push({ id: child.id, ok: true, entryId: result.entryId });
|
|
277
|
+
} catch (error) {
|
|
278
|
+
cascaded.push({ id: child.id, ok: false, error: messageOf(error) });
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
231
283
|
if (inFlight.has(plan.sessionId)) {
|
|
232
284
|
throw failure("session-delete/busy", "这个会话正在被另一个删除操作处理。");
|
|
233
285
|
}
|
|
@@ -335,6 +387,7 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
335
387
|
digest: plan.digest,
|
|
336
388
|
live: plan.live,
|
|
337
389
|
children: plan.children,
|
|
390
|
+
cascaded,
|
|
338
391
|
removed,
|
|
339
392
|
checks,
|
|
340
393
|
});
|
|
@@ -342,6 +395,7 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
342
395
|
ok: true,
|
|
343
396
|
entryId,
|
|
344
397
|
receiptId,
|
|
398
|
+
cascaded,
|
|
345
399
|
sessionId: plan.sessionId,
|
|
346
400
|
digest: plan.digest,
|
|
347
401
|
title: plan.title ?? (typeof request.title === "string" ? request.title : null),
|
package/lib/service.js
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
// Typert Remote 包装层:把 operations 的方法暴露给浏览器,一条 /api 通道。
|
|
3
3
|
// declareRemote 的写法参考 Jaylor-Wang/dsh-tool-session-delete(MIT),见根目录 NOTICE。
|
|
4
4
|
import { Remote, RemoteError, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
5
|
+
import {
|
|
6
|
+
createAgentToolController,
|
|
7
|
+
normalizeToolError,
|
|
8
|
+
summarizePlanForModel,
|
|
9
|
+
} from "./agent-tool.js";
|
|
5
10
|
import { createOperations } from "./operations.js";
|
|
6
11
|
import { resolveDataRoot } from "./paths.js";
|
|
7
12
|
|
|
@@ -51,6 +56,9 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
51
56
|
this.config = config ?? {};
|
|
52
57
|
/** @type {any} */
|
|
53
58
|
this._ops = null;
|
|
59
|
+
/** @type {boolean} */
|
|
60
|
+
this.agentToolRegistered = false;
|
|
61
|
+
this.installAgentTool();
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
/**
|
|
@@ -73,6 +81,108 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
73
81
|
return this.ops.health();
|
|
74
82
|
}
|
|
75
83
|
|
|
84
|
+
/**
|
|
85
|
+
* 注册模型工具:两段式 plan → token → delete。
|
|
86
|
+
* 必须等 ctx.tools 就绪:插件构造时 tools 运行时可能还没挂载,
|
|
87
|
+
* 用 ctx.inject 延迟到服务可用(并在服务重建时重新注册)。
|
|
88
|
+
*/
|
|
89
|
+
installAgentTool() {
|
|
90
|
+
const register = (/** @type {any} */ scopedCtx) => {
|
|
91
|
+
void this.registerAgentTool(scopedCtx);
|
|
92
|
+
};
|
|
93
|
+
try {
|
|
94
|
+
if (typeof this.ctx?.inject === "function") {
|
|
95
|
+
this.ctx.inject(["tools"], register);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
register(this.ctx);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
this.ctx?.logger?.warn?.(
|
|
101
|
+
"session-delete: cannot schedule agent tool registration: " +
|
|
102
|
+
(error instanceof Error ? error.message : String(error)),
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {any} scopedCtx 注入 tools 之后的上下文
|
|
109
|
+
*/
|
|
110
|
+
async registerAgentTool(scopedCtx) {
|
|
111
|
+
try {
|
|
112
|
+
const tools = scopedCtx?.tools;
|
|
113
|
+
if (!tools || typeof tools.register !== "function") return;
|
|
114
|
+
const module = await import("@deepseek-ai/dsh-tools");
|
|
115
|
+
const defineTool = module.defineTool;
|
|
116
|
+
if (typeof defineTool !== "function") return;
|
|
117
|
+
const controller = createAgentToolController({
|
|
118
|
+
ops: {
|
|
119
|
+
plan: (/** @type {string} */ sessionId) => this.ops.plan(sessionId),
|
|
120
|
+
trash: (/** @type {string} */ sessionId, /** @type {any} */ request) =>
|
|
121
|
+
this.ops.trash(sessionId, request),
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
tools.register(
|
|
125
|
+
defineTool({
|
|
126
|
+
name: "session_delete",
|
|
127
|
+
description:
|
|
128
|
+
"Delete a DSH session permanently but recoverably (moves it to the local trash). " +
|
|
129
|
+
"Two steps are mandatory: call with action=plan to preview what will be removed and obtain a single-use token, " +
|
|
130
|
+
"then call again with action=delete and that token. Never delete the caller's own session. " +
|
|
131
|
+
"The token expires in 2 minutes and becomes invalid if the session changes.",
|
|
132
|
+
parameters: {
|
|
133
|
+
action: {
|
|
134
|
+
type: "string",
|
|
135
|
+
required: true,
|
|
136
|
+
enum: ["plan", "delete"],
|
|
137
|
+
description: "plan = read-only preview and token; delete = execute with the token",
|
|
138
|
+
},
|
|
139
|
+
sessionId: { type: "string", required: true, description: "Target session id" },
|
|
140
|
+
token: { type: "string", description: "Required for action=delete; from action=plan" },
|
|
141
|
+
cascade: {
|
|
142
|
+
type: "boolean",
|
|
143
|
+
description: "Also delete child (subagent) sessions; default false",
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
output: {
|
|
147
|
+
schema: { type: "object", additionalProperties: true },
|
|
148
|
+
render: (/** @type {any} */ _args, /** @type {any} */ value) => [
|
|
149
|
+
{ type: "text", text: JSON.stringify(value, null, 2) },
|
|
150
|
+
],
|
|
151
|
+
},
|
|
152
|
+
async execute(/** @type {any} */ args, /** @type {any} */ exec) {
|
|
153
|
+
const agent = exec?.agent;
|
|
154
|
+
const callerSessionId =
|
|
155
|
+
agent?.session?.id ?? agent?.session?.header?.id ?? agent?.sessionId ?? undefined;
|
|
156
|
+
try {
|
|
157
|
+
if (args.action === "plan") {
|
|
158
|
+
const result = await controller.plan({
|
|
159
|
+
sessionId: args.sessionId,
|
|
160
|
+
cascade: args.cascade,
|
|
161
|
+
});
|
|
162
|
+
return Object.assign({}, result, { summary: summarizePlanForModel(result) });
|
|
163
|
+
}
|
|
164
|
+
return await controller.remove({
|
|
165
|
+
sessionId: args.sessionId,
|
|
166
|
+
token: args.token,
|
|
167
|
+
cascade: args.cascade,
|
|
168
|
+
callerSessionId: typeof callerSessionId === "string" ? callerSessionId : undefined,
|
|
169
|
+
});
|
|
170
|
+
} catch (error) {
|
|
171
|
+
return normalizeToolError(error);
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
}),
|
|
175
|
+
);
|
|
176
|
+
this.agentToolRegistered = true;
|
|
177
|
+
this.ctx.logger?.info?.("session-delete: agent tool registered: session_delete");
|
|
178
|
+
} catch (error) {
|
|
179
|
+
this.ctx.logger?.warn?.(
|
|
180
|
+
"session-delete: agent tool unavailable: " +
|
|
181
|
+
(error instanceof Error ? error.message : String(error)),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
76
186
|
/**
|
|
77
187
|
* 只读:返回完整影响面(plan/dry-run 的数据源)。
|
|
78
188
|
* @param {{ sessionId: string }} request
|
|
@@ -87,13 +197,14 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
87
197
|
|
|
88
198
|
/**
|
|
89
199
|
* 把会话移入回收站。
|
|
90
|
-
* @param {{ sessionId: string, digest?: string, title?: string | null }} request
|
|
200
|
+
* @param {{ sessionId: string, digest?: string, title?: string | null, cascade?: boolean }} request
|
|
91
201
|
*/
|
|
92
202
|
async trash(request) {
|
|
93
203
|
try {
|
|
94
204
|
return await this.ops.trash(request?.sessionId, {
|
|
95
205
|
digest: request?.digest,
|
|
96
206
|
title: request?.title,
|
|
207
|
+
cascade: request?.cascade === true,
|
|
97
208
|
operator: "ui",
|
|
98
209
|
});
|
|
99
210
|
} catch (error) {
|
|
@@ -159,7 +270,8 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
159
270
|
/** 全库体检:只读。 */
|
|
160
271
|
async healthScan() {
|
|
161
272
|
try {
|
|
162
|
-
|
|
273
|
+
const report = await this.ops.scanHealth();
|
|
274
|
+
return Object.assign({}, report, { agentTool: this.agentToolRegistered === true });
|
|
163
275
|
} catch (error) {
|
|
164
276
|
rethrow(error);
|
|
165
277
|
}
|
package/locale/en.json
CHANGED
|
@@ -44,6 +44,10 @@
|
|
|
44
44
|
"health.cleanDone": "Cleaned {n}",
|
|
45
45
|
"panel.tabReceipts": "Receipts",
|
|
46
46
|
"receipts.empty": "No operation receipts yet.",
|
|
47
|
-
"receipts.restored": "restored"
|
|
47
|
+
"receipts.restored": "restored",
|
|
48
|
+
"confirm.cascade": "Also delete these {n} child session(s) (each goes to trash separately)",
|
|
49
|
+
"health.agentTool": "Agent tool session_delete: {state}",
|
|
50
|
+
"health.agentToolOn": "registered",
|
|
51
|
+
"health.agentToolOff": "not registered (host has no tools runtime)"
|
|
48
52
|
}
|
|
49
53
|
}
|
package/locale/zh.json
CHANGED
|
@@ -44,6 +44,10 @@
|
|
|
44
44
|
"health.cleanDone": "已清理 {n}",
|
|
45
45
|
"panel.tabReceipts": "回执",
|
|
46
46
|
"receipts.empty": "还没有操作回执。",
|
|
47
|
-
"receipts.restored": "已移回原位置"
|
|
47
|
+
"receipts.restored": "已移回原位置",
|
|
48
|
+
"confirm.cascade": "同时删除这 {n} 个子会话(各自进入回收站,需分别恢复)",
|
|
49
|
+
"health.agentTool": "模型工具 session_delete:{state}",
|
|
50
|
+
"health.agentToolOn": "已注册",
|
|
51
|
+
"health.agentToolOff": "未注册(宿主未提供 tools 运行时)"
|
|
48
52
|
}
|
|
49
53
|
}
|