@kangyishuai/dsh-session-delete 0.2.0 → 0.4.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 +14 -13
- package/README.zh-CN.md +16 -13
- package/lib/agent-tool.js +173 -0
- package/lib/capability.js +10 -4
- package/lib/client.js +274 -1
- package/lib/index.js +3 -2
- package/lib/operations.js +127 -5
- package/lib/service.js +141 -2
- package/locale/en.json +14 -1
- package/locale/zh.json +14 -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.4, early.** Verified on DSH 0.2.1-rc.5 with `pnpm smoke:session-delete` (24 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:
|
|
@@ -87,15 +89,17 @@ Archive is a bookmark, not a deletion: DSH adds the session id to the workspace'
|
|
|
87
89
|
|
|
88
90
|
v0.1 declares no `engines.dsh` range. Instead there are three layers of defense:
|
|
89
91
|
|
|
90
|
-
| Layer
|
|
91
|
-
|
|
|
92
|
-
| L1 version gate
|
|
93
|
-
| L2 startup self-check
|
|
94
|
-
|
|
92
|
+
| Layer | When it runs | What it does | What you see |
|
|
93
|
+
| --------------------- | -------------------- | ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
94
|
+
| L1 version gate | install / activation | `engines.dsh` version range. **Not declared in v0.1**, so this layer is currently inactive. | nothing |
|
|
95
|
+
| L2 startup self-check | plugin activation | Probes every host capability it depends on and grades them api / internal / unknown. | **Degraded mode**: a host without `workspaceRegistry` (for example the headless composition) still allows deletion — the archive gate and workspace bookkeeping steps are skipped and reported in the receipt. Only a missing persistence seam puts the plugin in read-only mode. |
|
|
96
|
+
|
|
97
|
+
**Read-only mode**: plan, audit and trash listing keep working; delete, restore and purge are disabled. |
|
|
98
|
+
| L3 per-operation refusal | every operation | Each operation re-checks its own prerequisites; unknown always refuses. | A structured error code plus a human-readable reason, and no half-deleted state. |
|
|
95
99
|
|
|
96
100
|
| Plugin | Verified on DSH | Notes |
|
|
97
101
|
| ------ | ------------------------ | ------------------------------------------------------------------------------------------- |
|
|
98
|
-
| 0.
|
|
102
|
+
| 0.4.0 | **0.2.1-rc.5** (desktop) | `pnpm smoke:session-delete`: 24 assertions green; manual GUI pass on the same host version. |
|
|
99
103
|
|
|
100
104
|
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
105
|
|
|
@@ -110,14 +114,11 @@ After a DSH upgrade: restart, then open the plugin status (settings → plugins)
|
|
|
110
114
|
| 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
115
|
| 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
116
|
|
|
113
|
-
## Roadmap (
|
|
117
|
+
## Roadmap (later releases)
|
|
114
118
|
|
|
115
|
-
The following are planned and
|
|
119
|
+
The following are planned and not implemented yet:
|
|
116
120
|
|
|
117
121
|
- batch deletion (multi-select);
|
|
118
|
-
- cascade deletion with a checkbox per sub-session;
|
|
119
|
-
- an agent tool (plan first, then delete);
|
|
120
|
-
- an undo toast after a delete.
|
|
121
122
|
|
|
122
123
|
## License
|
|
123
124
|
|
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.4,早期可用。** 已在 DSH 0.2.1-rc.5 上用 `pnpm smoke:session-delete`(24 项端到端断言)加一轮桌面版人工验收。仍是年轻软件,重要数据请自行备份。
|
|
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. 同一个菜单里的「回收站…」打开回收站面板,可以:
|
|
@@ -87,15 +89,19 @@ dsh plugin --profile desktop add /tmp/kangyishuai-dsh-session-delete-*.tgz
|
|
|
87
89
|
|
|
88
90
|
v0.1 没有声明 `engines.dsh` 版本范围,改用三层防线:
|
|
89
91
|
|
|
90
|
-
| 层
|
|
91
|
-
|
|
|
92
|
-
| L1 版本闸门
|
|
93
|
-
| L2 启动自检
|
|
94
|
-
|
|
92
|
+
| 层 | 触发时机 | 做什么 | 你会看到什么 |
|
|
93
|
+
| ----------- | ----------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
94
|
+
| L1 版本闸门 | 安装 / 激活 | `engines.dsh` 版本范围。**v0.1 尚未声明**,这一层目前不生效。 | 无 |
|
|
95
|
+
| L2 启动自检 | 插件激活时 | 探测所有依赖的宿主能力,分成 api / internal / unknown 三档。 | 进入**降级模式**:宿主没有 `workspaceRegistry`(例如 headless 组合)时仍然允许删除——归档闸门与工作区记账步骤会被跳过并写进回执。只有持久化本体缺失时才进入只读模式。 |
|
|
96
|
+
|
|
97
|
+
**只读模式**:plan、审计、回收站列表还能用;删除、恢复、彻底删除被禁用。 |
|
|
98
|
+
| L3 执行期拒绝 | 每次操作 | 每个操作自己再校验前置能力,unknown 一律拒绝。 | 结构化错误码 + 人类可读原因,磁盘上不会出现删了一半的状态。 |
|
|
95
99
|
|
|
96
100
|
| 插件版本 | 已验证的 DSH 版本 | 说明 |
|
|
97
101
|
| -------- | ------------------------ | -------------------------------------------------------------------------------- |
|
|
98
|
-
| 0.
|
|
102
|
+
| 0.4.0 | **0.2.1-rc.5**(桌面版) | `pnpm smoke:session-delete` 24 项断言全绿,并在同一版本桌面版做过人工 GUI 验收。 |
|
|
103
|
+
|
|
104
|
+
目前只在 **macOS** 上验证过。Windows 相关路径(`session.lock` 的 EBUSY/EPERM 重试、宿主在那儿用的命名信号量)代码里有,但**还没实测**。
|
|
99
105
|
|
|
100
106
|
DSH 升级之后:重启,然后看插件的状态条(设置 → 插件)。如果显示只读模式,说明宿主 API 变了——升级插件,或者把两个版本号一起报上来,别硬用。
|
|
101
107
|
|
|
@@ -110,14 +116,11 @@ DSH 升级之后:重启,然后看插件的状态条(设置 → 插件)
|
|
|
110
116
|
| 彻底删除后还残留东西 | 删除回执里列出的残留 | 对回执和审计日志,按回执里的路径手动清理,并把回执附在 issue 里 |
|
|
111
117
|
| 删完之后回收站是空的 | 插件解析到的数据根目录和你的 DSH 不是同一个(`DSH_HOME` / 配置) | 去磁盘上看 `~/.dsh/session-trash/`,再核对插件的 `dataRoot` 配置 |
|
|
112
118
|
|
|
113
|
-
##
|
|
119
|
+
## 计划中(后续版本)
|
|
114
120
|
|
|
115
|
-
|
|
121
|
+
下面这些是计划,尚未实现:
|
|
116
122
|
|
|
117
123
|
- 批量删除(多选);
|
|
118
|
-
- 子会话级联删除(逐个子会话勾选);
|
|
119
|
-
- agent 工具(先 plan 再删);
|
|
120
|
-
- 删除后的撤销 toast。
|
|
121
124
|
|
|
122
125
|
## License
|
|
123
126
|
|
|
@@ -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/capability.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// 宿主能力探测。整个插件对宿主内部契约的依赖只在这里出现一次,
|
|
3
3
|
// 这样 DSH 升级后需要改的地方永远只有一个文件(见 docs/adr/0005)。
|
|
4
4
|
|
|
5
|
-
/** @typedef {"api" | "internal" | "unknown"} CapabilityTier */
|
|
5
|
+
/** @typedef {"api" | "internal" | "minimal" | "unknown"} CapabilityTier */
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @typedef {object} Capabilities
|
|
@@ -33,10 +33,12 @@ function callable(target, key) {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
36
|
+
* 探测本插件依赖的全部宿主能力,并按四档给出结论:
|
|
37
37
|
* - api:官方文档里有的一等公民能力齐全;
|
|
38
38
|
* - internal:核心可用,但有依赖社区契约的部件(会降级执行并告警);
|
|
39
|
-
* -
|
|
39
|
+
* - minimal:缺少 workspaceRegistry(如 headless 组合),删除仍可做,
|
|
40
|
+
* 归档闸门/工作区记账等步骤跳过并在回执里标记;
|
|
41
|
+
* - unknown:持久化本体缺失,破坏性操作必须拒绝。
|
|
40
42
|
*
|
|
41
43
|
* @param {any} ctx host 上下文。
|
|
42
44
|
* @returns {Capabilities}
|
|
@@ -75,7 +77,10 @@ export function probeCapabilities(ctx) {
|
|
|
75
77
|
|
|
76
78
|
/** @type {CapabilityTier} */
|
|
77
79
|
let tier = "api";
|
|
78
|
-
|
|
80
|
+
// 删除本身只需要持久化读取 + 文件系统;工作区/归档只是闸门与记账,
|
|
81
|
+
// 缺失时降级为 minimal(跳过对应步骤并报告),不应整体禁用。
|
|
82
|
+
const essential = ["persistence.stat"];
|
|
83
|
+
const workspaceGate = ["workspace.archive", "workspace.unarchive"];
|
|
79
84
|
const convenient = [
|
|
80
85
|
"persistence.list",
|
|
81
86
|
"persistence.locate",
|
|
@@ -86,6 +91,7 @@ export function probeCapabilities(ctx) {
|
|
|
86
91
|
"event.session-activity",
|
|
87
92
|
];
|
|
88
93
|
if (essential.some((k) => !table[k])) tier = "unknown";
|
|
94
|
+
else if (workspaceGate.some((k) => !table[k])) tier = "minimal";
|
|
89
95
|
else if (convenient.some((k) => !table[k])) tier = "internal";
|
|
90
96
|
|
|
91
97
|
return {
|
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}",
|
|
@@ -69,6 +70,15 @@ window.__ModuleLoader__.load({
|
|
|
69
70
|
"panel.tabAudit": "审计",
|
|
70
71
|
"panel.tabHealth": "体检",
|
|
71
72
|
"panel.tabReceipts": "回执",
|
|
73
|
+
"panel.tabBatch": "批量",
|
|
74
|
+
"batch.empty": "没有可删除的会话。",
|
|
75
|
+
"batch.selectAll": "全选",
|
|
76
|
+
"batch.clear": "清空",
|
|
77
|
+
"batch.delete": "批量删除({n})",
|
|
78
|
+
"batch.confirm": "确认删除 {n} 个?各自进回收站",
|
|
79
|
+
"batch.running": "正在删除…",
|
|
80
|
+
"batch.done": "完成:{ok}/{total} 成功",
|
|
81
|
+
"batch.live": "运行中",
|
|
72
82
|
"receipts.empty": "还没有操作回执。",
|
|
73
83
|
"receipts.restored": "已移回原位置",
|
|
74
84
|
"health.loading": "正在检查…",
|
|
@@ -76,6 +86,9 @@ window.__ModuleLoader__.load({
|
|
|
76
86
|
"已存 {stored} · live {live} · 归档 {archived} · 回收站 {trash} · 待处理 {pending}",
|
|
77
87
|
"health.ok": "未发现失效引用。",
|
|
78
88
|
"health.items": "{n} 条",
|
|
89
|
+
"health.agentTool": "模型工具 session_delete:{state}",
|
|
90
|
+
"health.agentToolOn": "已注册",
|
|
91
|
+
"health.agentToolOff": "未注册(宿主未提供 tools 运行时)",
|
|
79
92
|
"health.unavailable": "无法体检:{reason}",
|
|
80
93
|
"health.clean": "清理失效引用({n})",
|
|
81
94
|
"health.cleanConfirm": "确认清理 {n} 条?只动登记表,不碰会话文件",
|
|
@@ -105,6 +118,7 @@ window.__ModuleLoader__.load({
|
|
|
105
118
|
"confirm.size": "Session log is about {size}",
|
|
106
119
|
"confirm.live": "This session is still open; its work is stopped first.",
|
|
107
120
|
"confirm.children": "{n} subagent session(s) come along",
|
|
121
|
+
"confirm.cascade": "Also delete these {n} child session(s) (each goes to trash separately)",
|
|
108
122
|
"confirm.refs": "Will clean: {list}",
|
|
109
123
|
"confirm.trashPath": "Trash location: {path}",
|
|
110
124
|
"confirm.blocked": "Cannot delete right now: {reason}",
|
|
@@ -119,6 +133,15 @@ window.__ModuleLoader__.load({
|
|
|
119
133
|
"panel.tabAudit": "Audit",
|
|
120
134
|
"panel.tabHealth": "Health",
|
|
121
135
|
"panel.tabReceipts": "Receipts",
|
|
136
|
+
"panel.tabBatch": "Batch",
|
|
137
|
+
"batch.empty": "No sessions to delete.",
|
|
138
|
+
"batch.selectAll": "Select all",
|
|
139
|
+
"batch.clear": "Clear",
|
|
140
|
+
"batch.delete": "Delete {n} selected",
|
|
141
|
+
"batch.confirm": "Delete {n} session(s)? Each goes to trash",
|
|
142
|
+
"batch.running": "Deleting…",
|
|
143
|
+
"batch.done": "Done: {ok}/{total} succeeded",
|
|
144
|
+
"batch.live": "live",
|
|
122
145
|
"receipts.empty": "No operation receipts yet.",
|
|
123
146
|
"receipts.restored": "restored",
|
|
124
147
|
"health.loading": "Checking…",
|
|
@@ -126,6 +149,9 @@ window.__ModuleLoader__.load({
|
|
|
126
149
|
"stored {stored} · live {live} · archived {archived} · trash {trash} · pending {pending}",
|
|
127
150
|
"health.ok": "No dangling references found.",
|
|
128
151
|
"health.items": "{n} item(s)",
|
|
152
|
+
"health.agentTool": "Agent tool session_delete: {state}",
|
|
153
|
+
"health.agentToolOn": "registered",
|
|
154
|
+
"health.agentToolOff": "not registered (host has no tools runtime)",
|
|
129
155
|
"health.unavailable": "Cannot run the check: {reason}",
|
|
130
156
|
"health.clean": "Clean dangling references ({n})",
|
|
131
157
|
"health.cleanConfirm": "Clean {n} dangling reference(s)? Bookkeeping only, no session files.",
|
|
@@ -467,12 +493,16 @@ window.__ModuleLoader__.load({
|
|
|
467
493
|
var busy = busyState[0];
|
|
468
494
|
var setBusy = busyState[1];
|
|
469
495
|
var requestId = request ? request.sessionId : null;
|
|
496
|
+
var cascadeState = React.useState(false);
|
|
497
|
+
var cascade = cascadeState[0];
|
|
498
|
+
var setCascade = cascadeState[1];
|
|
470
499
|
|
|
471
500
|
React.useEffect(() => {
|
|
472
501
|
if (!requestId) {
|
|
473
502
|
setPlan(null);
|
|
474
503
|
setError(null);
|
|
475
504
|
setBusy(false);
|
|
505
|
+
setCascade(false);
|
|
476
506
|
return undefined;
|
|
477
507
|
}
|
|
478
508
|
var alive = true;
|
|
@@ -505,6 +535,7 @@ window.__ModuleLoader__.load({
|
|
|
505
535
|
sessionId: request.sessionId,
|
|
506
536
|
digest: plan.digest,
|
|
507
537
|
title: request.title || null,
|
|
538
|
+
cascade: cascade && plan.children.length > 0,
|
|
508
539
|
})
|
|
509
540
|
.then((/** @type {any} */ result) => {
|
|
510
541
|
props.requestStore.set(null);
|
|
@@ -541,6 +572,42 @@ window.__ModuleLoader__.load({
|
|
|
541
572
|
request.title
|
|
542
573
|
? h("div", { style: { fontWeight: 600, marginBottom: "10px" } }, request.title)
|
|
543
574
|
: null,
|
|
575
|
+
plan && plan.children.length > 0
|
|
576
|
+
? h(
|
|
577
|
+
"label",
|
|
578
|
+
{
|
|
579
|
+
style: {
|
|
580
|
+
display: "flex",
|
|
581
|
+
alignItems: "flex-start",
|
|
582
|
+
gap: "8px",
|
|
583
|
+
margin: "8px 0 4px",
|
|
584
|
+
cursor: "pointer",
|
|
585
|
+
},
|
|
586
|
+
},
|
|
587
|
+
h("input", {
|
|
588
|
+
type: "checkbox",
|
|
589
|
+
checked: cascade,
|
|
590
|
+
disabled: busy,
|
|
591
|
+
onChange: (/** @type {any} */ event) => setCascade(Boolean(event.target.checked)),
|
|
592
|
+
style: { marginTop: "2px" },
|
|
593
|
+
}),
|
|
594
|
+
h(
|
|
595
|
+
"span",
|
|
596
|
+
null,
|
|
597
|
+
props.t("confirm.cascade", { n: plan.children.length }),
|
|
598
|
+
h(
|
|
599
|
+
"span",
|
|
600
|
+
{
|
|
601
|
+
style: { display: "block", color: T.faint, fontSize: "12px", marginTop: "2px" },
|
|
602
|
+
},
|
|
603
|
+
plan.children
|
|
604
|
+
.slice(0, 4)
|
|
605
|
+
.map((/** @type {any} */ child) => child.title || child.id)
|
|
606
|
+
.join(" · ") + (plan.children.length > 4 ? " …" : ""),
|
|
607
|
+
),
|
|
608
|
+
),
|
|
609
|
+
)
|
|
610
|
+
: null,
|
|
544
611
|
lines.map((/** @type {any} */ line, /** @type {any} */ index) =>
|
|
545
612
|
h("div", { key: "line-" + index, style: { marginBottom: "4px" } }, line),
|
|
546
613
|
),
|
|
@@ -609,6 +676,30 @@ window.__ModuleLoader__.load({
|
|
|
609
676
|
var receiptsState = React.useState(null);
|
|
610
677
|
var receipts = receiptsState[0];
|
|
611
678
|
var setReceipts = receiptsState[1];
|
|
679
|
+
var batchListState = React.useState(null);
|
|
680
|
+
var batchList = batchListState[0];
|
|
681
|
+
var setBatchList = batchListState[1];
|
|
682
|
+
var batchSelectedState = React.useState({});
|
|
683
|
+
var batchSelected = batchSelectedState[0];
|
|
684
|
+
var setBatchSelected = batchSelectedState[1];
|
|
685
|
+
var batchBusyState = React.useState(false);
|
|
686
|
+
var batchBusy = batchBusyState[0];
|
|
687
|
+
var setBatchBusy = batchBusyState[1];
|
|
688
|
+
var batchMsgState = React.useState("");
|
|
689
|
+
var batchMsg = batchMsgState[0];
|
|
690
|
+
var setBatchMsg = batchMsgState[1];
|
|
691
|
+
|
|
692
|
+
const loadBatch = React.useCallback(() => {
|
|
693
|
+
props
|
|
694
|
+
.call("listSessions")
|
|
695
|
+
.then((/** @type {any} */ value) => {
|
|
696
|
+
setBatchList(value && value.sessions ? value.sessions : []);
|
|
697
|
+
})
|
|
698
|
+
.catch((/** @type {any} */ reason) => {
|
|
699
|
+
setBatchList([]);
|
|
700
|
+
setBatchMsg(errorText(reason));
|
|
701
|
+
});
|
|
702
|
+
}, []);
|
|
612
703
|
|
|
613
704
|
const loadReceipts = React.useCallback(() => {
|
|
614
705
|
props
|
|
@@ -794,10 +885,163 @@ window.__ModuleLoader__.load({
|
|
|
794
885
|
},
|
|
795
886
|
props.t("panel.tabReceipts"),
|
|
796
887
|
),
|
|
888
|
+
h(
|
|
889
|
+
Btn,
|
|
890
|
+
{
|
|
891
|
+
onClick: () => {
|
|
892
|
+
setTab("batch");
|
|
893
|
+
loadBatch();
|
|
894
|
+
},
|
|
895
|
+
},
|
|
896
|
+
props.t("panel.tabBatch"),
|
|
897
|
+
),
|
|
797
898
|
),
|
|
798
899
|
);
|
|
799
900
|
|
|
800
|
-
if (tab === "
|
|
901
|
+
if (tab === "batch") {
|
|
902
|
+
if (!batchList) {
|
|
903
|
+
content.push(
|
|
904
|
+
h(
|
|
905
|
+
"div",
|
|
906
|
+
{ key: "batch-loading", style: { color: T.dim, padding: "8px 0" } },
|
|
907
|
+
props.t("health.loading"),
|
|
908
|
+
),
|
|
909
|
+
);
|
|
910
|
+
} else if (batchList.length === 0) {
|
|
911
|
+
content.push(
|
|
912
|
+
h(
|
|
913
|
+
"div",
|
|
914
|
+
{ key: "batch-empty", style: { color: T.dim, padding: "8px 0" } },
|
|
915
|
+
props.t("batch.empty"),
|
|
916
|
+
),
|
|
917
|
+
);
|
|
918
|
+
} else {
|
|
919
|
+
const selectedIds = Object.keys(batchSelected).filter(
|
|
920
|
+
(id) => /** @type {any} */ (batchSelected)[id],
|
|
921
|
+
);
|
|
922
|
+
content.push(
|
|
923
|
+
h(
|
|
924
|
+
"div",
|
|
925
|
+
{
|
|
926
|
+
key: "batch-actions",
|
|
927
|
+
style: { display: "flex", gap: "8px", alignItems: "center", marginBottom: "8px" },
|
|
928
|
+
},
|
|
929
|
+
h(
|
|
930
|
+
Btn,
|
|
931
|
+
{
|
|
932
|
+
onClick: () => {
|
|
933
|
+
/** @type {Record<string, boolean>} */
|
|
934
|
+
const all = {};
|
|
935
|
+
for (const item of /** @type {any[]} */ (batchList)) all[item.id] = true;
|
|
936
|
+
setBatchSelected(all);
|
|
937
|
+
},
|
|
938
|
+
},
|
|
939
|
+
props.t("batch.selectAll"),
|
|
940
|
+
),
|
|
941
|
+
h(Btn, { onClick: () => setBatchSelected({}) }, props.t("batch.clear")),
|
|
942
|
+
confirmPurge === "__batch__"
|
|
943
|
+
? h(
|
|
944
|
+
Btn,
|
|
945
|
+
{
|
|
946
|
+
danger: true,
|
|
947
|
+
disabled: batchBusy || selectedIds.length === 0,
|
|
948
|
+
onClick: () => {
|
|
949
|
+
setBatchBusy(true);
|
|
950
|
+
setBatchMsg("");
|
|
951
|
+
props
|
|
952
|
+
.call("trashMany", { sessionIds: selectedIds })
|
|
953
|
+
.then((/** @type {any} */ result) => {
|
|
954
|
+
setBatchBusy(false);
|
|
955
|
+
setConfirmPurge(null);
|
|
956
|
+
setBatchMsg(
|
|
957
|
+
props.t("batch.done", { ok: result.succeeded, total: result.total }),
|
|
958
|
+
);
|
|
959
|
+
for (const item of result.results) {
|
|
960
|
+
if (item.ok) props.onPurged?.(item.id);
|
|
961
|
+
}
|
|
962
|
+
setBatchSelected({});
|
|
963
|
+
loadBatch();
|
|
964
|
+
refresh();
|
|
965
|
+
props.bump?.();
|
|
966
|
+
})
|
|
967
|
+
.catch((/** @type {any} */ reason) => {
|
|
968
|
+
setBatchBusy(false);
|
|
969
|
+
setBatchMsg(errorText(reason));
|
|
970
|
+
});
|
|
971
|
+
},
|
|
972
|
+
},
|
|
973
|
+
props.t("batch.confirm", { n: selectedIds.length }),
|
|
974
|
+
)
|
|
975
|
+
: h(
|
|
976
|
+
Btn,
|
|
977
|
+
{
|
|
978
|
+
danger: true,
|
|
979
|
+
disabled: batchBusy || selectedIds.length === 0,
|
|
980
|
+
onClick: () => setConfirmPurge("__batch__"),
|
|
981
|
+
},
|
|
982
|
+
batchBusy
|
|
983
|
+
? props.t("batch.running")
|
|
984
|
+
: props.t("batch.delete", { n: selectedIds.length }),
|
|
985
|
+
),
|
|
986
|
+
),
|
|
987
|
+
);
|
|
988
|
+
batchList.forEach((/** @type {any} */ item, /** @type {any} */ index) => {
|
|
989
|
+
const checked = Boolean(/** @type {any} */ (batchSelected)[item.id]);
|
|
990
|
+
content.push(
|
|
991
|
+
h(
|
|
992
|
+
"label",
|
|
993
|
+
{
|
|
994
|
+
key: "batch-" + index,
|
|
995
|
+
style: {
|
|
996
|
+
display: "flex",
|
|
997
|
+
alignItems: "baseline",
|
|
998
|
+
gap: "8px",
|
|
999
|
+
borderTop: "1px solid " + T.border,
|
|
1000
|
+
padding: "6px 0",
|
|
1001
|
+
fontSize: "12px",
|
|
1002
|
+
cursor: "pointer",
|
|
1003
|
+
},
|
|
1004
|
+
},
|
|
1005
|
+
h("input", {
|
|
1006
|
+
type: "checkbox",
|
|
1007
|
+
checked: checked,
|
|
1008
|
+
disabled: batchBusy,
|
|
1009
|
+
onChange: () =>
|
|
1010
|
+
setBatchSelected((/** @type {any} */ current) => ({
|
|
1011
|
+
...current,
|
|
1012
|
+
[item.id]: !checked,
|
|
1013
|
+
})),
|
|
1014
|
+
style: { marginTop: "2px" },
|
|
1015
|
+
}),
|
|
1016
|
+
h(
|
|
1017
|
+
"span",
|
|
1018
|
+
{
|
|
1019
|
+
title: item.id,
|
|
1020
|
+
style: {
|
|
1021
|
+
flex: 1,
|
|
1022
|
+
minWidth: 0,
|
|
1023
|
+
overflow: "hidden",
|
|
1024
|
+
textOverflow: "ellipsis",
|
|
1025
|
+
whiteSpace: "nowrap",
|
|
1026
|
+
},
|
|
1027
|
+
},
|
|
1028
|
+
item.title || item.id,
|
|
1029
|
+
),
|
|
1030
|
+
item.live ? h("span", { style: { color: T.danger } }, props.t("batch.live")) : null,
|
|
1031
|
+
),
|
|
1032
|
+
);
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
if (batchMsg) {
|
|
1036
|
+
content.push(
|
|
1037
|
+
h(
|
|
1038
|
+
"div",
|
|
1039
|
+
{ key: "batch-msg", style: { color: T.dim, marginTop: "8px", fontSize: "12px" } },
|
|
1040
|
+
batchMsg,
|
|
1041
|
+
),
|
|
1042
|
+
);
|
|
1043
|
+
}
|
|
1044
|
+
} else if (tab === "receipts") {
|
|
801
1045
|
if (!receipts || receipts.length === 0) {
|
|
802
1046
|
content.push(
|
|
803
1047
|
h(
|
|
@@ -893,6 +1137,17 @@ window.__ModuleLoader__.load({
|
|
|
893
1137
|
props.t("health.inventory", scan.inventory || {}),
|
|
894
1138
|
),
|
|
895
1139
|
);
|
|
1140
|
+
content.push(
|
|
1141
|
+
h(
|
|
1142
|
+
"div",
|
|
1143
|
+
{ key: "scan-agent-tool", style: { color: T.dim, fontSize: "12px" } },
|
|
1144
|
+
props.t("health.agentTool", {
|
|
1145
|
+
state: scan.agentTool
|
|
1146
|
+
? props.t("health.agentToolOn")
|
|
1147
|
+
: props.t("health.agentToolOff"),
|
|
1148
|
+
}),
|
|
1149
|
+
),
|
|
1150
|
+
);
|
|
896
1151
|
if (scan.findings.length === 0) {
|
|
897
1152
|
content.push(
|
|
898
1153
|
h("div", { key: "scan-ok", style: { padding: "8px 0" } }, props.t("health.ok")),
|
|
@@ -1326,6 +1581,24 @@ window.__ModuleLoader__.load({
|
|
|
1326
1581
|
void refreshRows(result && result.sessionId ? result.sessionId : undefined);
|
|
1327
1582
|
tickStore.set(tickStore.getSnapshot() + 1);
|
|
1328
1583
|
};
|
|
1584
|
+
// 外部变更(agent 工具、另一个窗口、CLI)不会通知本页面,
|
|
1585
|
+
// 定期拉一次会话列表;页面重新可见时也立刻拉一次。
|
|
1586
|
+
ctx.effect(() => {
|
|
1587
|
+
const pull = () => {
|
|
1588
|
+
if (document.visibilityState === "visible") {
|
|
1589
|
+
void refreshRows(undefined);
|
|
1590
|
+
// 回收站条目也可能被 agent 工具/另一个窗口改变,顺带刷新页脚计数
|
|
1591
|
+
tickStore.set(tickStore.getSnapshot() + 1);
|
|
1592
|
+
}
|
|
1593
|
+
};
|
|
1594
|
+
const timer = setInterval(pull, 10_000);
|
|
1595
|
+
document.addEventListener("visibilitychange", pull);
|
|
1596
|
+
return () => {
|
|
1597
|
+
clearInterval(timer);
|
|
1598
|
+
document.removeEventListener("visibilitychange", pull);
|
|
1599
|
+
};
|
|
1600
|
+
}, "session-delete: poll external session removals");
|
|
1601
|
+
|
|
1329
1602
|
const onPurged = (/** @type {any} */ sessionId) => {
|
|
1330
1603
|
void refreshRows(sessionId || undefined);
|
|
1331
1604
|
};
|
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),
|
|
@@ -826,6 +880,72 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
826
880
|
};
|
|
827
881
|
}
|
|
828
882
|
|
|
883
|
+
/** 列出会话(批量删除用):只读,字段刻意保持最小。 */
|
|
884
|
+
async function listSessions() {
|
|
885
|
+
if (!caps.checks["persistence.list"]) {
|
|
886
|
+
throw failure("session-delete/unsupported-host", "当前宿主没有 sessionPersistence.list");
|
|
887
|
+
}
|
|
888
|
+
const list = (await caps.services.persistence.list()) ?? [];
|
|
889
|
+
const out = list.map((/** @type {any} */ entry) => {
|
|
890
|
+
const id = String(entry?.id ?? entry?.header?.id ?? "");
|
|
891
|
+
return {
|
|
892
|
+
id,
|
|
893
|
+
title: typeof entry?.header?.title === "string" ? entry.header.title : null,
|
|
894
|
+
cwd: typeof entry?.header?.cwd === "string" ? entry.header.cwd : null,
|
|
895
|
+
live: caps.services.sessions?.get?.(id) !== undefined,
|
|
896
|
+
};
|
|
897
|
+
});
|
|
898
|
+
out.sort((/** @type {any} */ a, /** @type {any} */ b) =>
|
|
899
|
+
String(b.title ?? b.id).localeCompare(String(a.title ?? a.id)),
|
|
900
|
+
);
|
|
901
|
+
return { sessions: out.filter((/** @type {any} */ item) => item.id.length > 0) };
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* 批量删除(顺序执行,逐条返回结果;单条失败不影响其它)。
|
|
906
|
+
* @param {{ sessionIds?: string[], cascade?: boolean, operator?: string }} request
|
|
907
|
+
*/
|
|
908
|
+
async function trashMany(request = {}) {
|
|
909
|
+
const ids = Array.isArray(request.sessionIds) ? request.sessionIds : [];
|
|
910
|
+
if (ids.length === 0) throw failure("session-delete/invalid-request", "没有选择任何会话");
|
|
911
|
+
if (ids.length > 50) throw failure("session-delete/invalid-request", "一次最多 50 个会话");
|
|
912
|
+
/** @type {{ id: string, ok: boolean, entryId?: string, receiptId?: string, code?: string, error?: string }[]} */
|
|
913
|
+
const results = [];
|
|
914
|
+
for (const id of ids) {
|
|
915
|
+
try {
|
|
916
|
+
const result = await trash(id, {
|
|
917
|
+
cascade: request.cascade === true,
|
|
918
|
+
operator: request.operator ?? "ui",
|
|
919
|
+
});
|
|
920
|
+
results.push({ id, ok: true, entryId: result.entryId, receiptId: result.receiptId });
|
|
921
|
+
} catch (error) {
|
|
922
|
+
const raw = /** @type {any} */ (error);
|
|
923
|
+
results.push({
|
|
924
|
+
id,
|
|
925
|
+
ok: false,
|
|
926
|
+
code: typeof raw?.code === "string" ? raw.code : "error",
|
|
927
|
+
error: messageOf(error),
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
const okCount = results.filter((item) => item.ok).length;
|
|
932
|
+
const receiptId = await writeReceipt(dataRoot, {
|
|
933
|
+
action: "trash-many",
|
|
934
|
+
at: new Date().toISOString(),
|
|
935
|
+
requested: ids.length,
|
|
936
|
+
succeeded: okCount,
|
|
937
|
+
failed: results.length - okCount,
|
|
938
|
+
results,
|
|
939
|
+
});
|
|
940
|
+
return {
|
|
941
|
+
ok: okCount === results.length,
|
|
942
|
+
total: results.length,
|
|
943
|
+
succeeded: okCount,
|
|
944
|
+
results,
|
|
945
|
+
receiptId,
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
|
|
829
949
|
/** @param {number} [limit] */
|
|
830
950
|
async function receipts(limit = 50) {
|
|
831
951
|
return { entries: await listReceipts(dataRoot, limit) };
|
|
@@ -834,6 +954,8 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
834
954
|
return {
|
|
835
955
|
caps,
|
|
836
956
|
health,
|
|
957
|
+
listSessions,
|
|
958
|
+
trashMany,
|
|
837
959
|
receipts,
|
|
838
960
|
scanHealth,
|
|
839
961
|
cleanHealth,
|
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) {
|
|
@@ -144,6 +255,31 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
144
255
|
}
|
|
145
256
|
}
|
|
146
257
|
|
|
258
|
+
/** 批量删除的候选列表(只读)。 */
|
|
259
|
+
async listSessions() {
|
|
260
|
+
try {
|
|
261
|
+
return await this.ops.listSessions();
|
|
262
|
+
} catch (error) {
|
|
263
|
+
rethrow(error);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 批量删除。
|
|
269
|
+
* @param {{ sessionIds?: string[], cascade?: boolean }} request
|
|
270
|
+
*/
|
|
271
|
+
async trashMany(request) {
|
|
272
|
+
try {
|
|
273
|
+
return await this.ops.trashMany({
|
|
274
|
+
sessionIds: request?.sessionIds,
|
|
275
|
+
cascade: request?.cascade === true,
|
|
276
|
+
operator: "ui",
|
|
277
|
+
});
|
|
278
|
+
} catch (error) {
|
|
279
|
+
rethrow(error);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
147
283
|
/**
|
|
148
284
|
* 最近的操作回执。
|
|
149
285
|
* @param {{ limit?: number }} [request]
|
|
@@ -159,7 +295,8 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
159
295
|
/** 全库体检:只读。 */
|
|
160
296
|
async healthScan() {
|
|
161
297
|
try {
|
|
162
|
-
|
|
298
|
+
const report = await this.ops.scanHealth();
|
|
299
|
+
return Object.assign({}, report, { agentTool: this.agentToolRegistered === true });
|
|
163
300
|
} catch (error) {
|
|
164
301
|
rethrow(error);
|
|
165
302
|
}
|
|
@@ -196,6 +333,8 @@ declareRemote(SessionDeleteService, "list");
|
|
|
196
333
|
declareRemote(SessionDeleteService, "restore");
|
|
197
334
|
declareRemote(SessionDeleteService, "purge");
|
|
198
335
|
declareRemote(SessionDeleteService, "audit");
|
|
336
|
+
declareRemote(SessionDeleteService, "listSessions");
|
|
337
|
+
declareRemote(SessionDeleteService, "trashMany");
|
|
199
338
|
declareRemote(SessionDeleteService, "receipts");
|
|
200
339
|
declareRemote(SessionDeleteService, "healthScan");
|
|
201
340
|
declareRemote(SessionDeleteService, "cleanHealth");
|
package/locale/en.json
CHANGED
|
@@ -44,6 +44,19 @@
|
|
|
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)",
|
|
52
|
+
"panel.tabBatch": "Batch",
|
|
53
|
+
"batch.empty": "No sessions to delete.",
|
|
54
|
+
"batch.selectAll": "Select all",
|
|
55
|
+
"batch.clear": "Clear",
|
|
56
|
+
"batch.delete": "Delete {n} selected",
|
|
57
|
+
"batch.confirm": "Delete {n} session(s)? Each goes to trash",
|
|
58
|
+
"batch.running": "Deleting…",
|
|
59
|
+
"batch.done": "Done: {ok}/{total} succeeded",
|
|
60
|
+
"batch.live": "live"
|
|
48
61
|
}
|
|
49
62
|
}
|
package/locale/zh.json
CHANGED
|
@@ -44,6 +44,19 @@
|
|
|
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 运行时)",
|
|
52
|
+
"panel.tabBatch": "批量",
|
|
53
|
+
"batch.empty": "没有可删除的会话。",
|
|
54
|
+
"batch.selectAll": "全选",
|
|
55
|
+
"batch.clear": "清空",
|
|
56
|
+
"batch.delete": "批量删除({n})",
|
|
57
|
+
"batch.confirm": "确认删除 {n} 个?各自进回收站",
|
|
58
|
+
"batch.running": "正在删除…",
|
|
59
|
+
"batch.done": "完成:{ok}/{total} 成功",
|
|
60
|
+
"batch.live": "运行中"
|
|
48
61
|
}
|
|
49
62
|
}
|