@kangyishuai/dsh-session-delete 0.1.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/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,40 @@ export class SessionDeleteService extends TypertRemoteService {
144
255
  }
145
256
  }
146
257
 
258
+ /**
259
+ * 最近的操作回执。
260
+ * @param {{ limit?: number }} [request]
261
+ */
262
+ async receipts(request) {
263
+ try {
264
+ return await this.ops.receipts(request?.limit ?? 50);
265
+ } catch (error) {
266
+ rethrow(error);
267
+ }
268
+ }
269
+
270
+ /** 全库体检:只读。 */
271
+ async healthScan() {
272
+ try {
273
+ const report = await this.ops.scanHealth();
274
+ return Object.assign({}, report, { agentTool: this.agentToolRegistered === true });
275
+ } catch (error) {
276
+ rethrow(error);
277
+ }
278
+ }
279
+
280
+ /**
281
+ * 清理体检发现的失效引用(只动登记表)。
282
+ * @param {{ classes?: string[] }} [request]
283
+ */
284
+ async cleanHealth(request) {
285
+ try {
286
+ return await this.ops.cleanHealth({ classes: request?.classes, operator: "ui" });
287
+ } catch (error) {
288
+ rethrow(error);
289
+ }
290
+ }
291
+
147
292
  /**
148
293
  * @param {{ sessionId: string }} request
149
294
  */
@@ -163,4 +308,7 @@ declareRemote(SessionDeleteService, "list");
163
308
  declareRemote(SessionDeleteService, "restore");
164
309
  declareRemote(SessionDeleteService, "purge");
165
310
  declareRemote(SessionDeleteService, "audit");
311
+ declareRemote(SessionDeleteService, "receipts");
312
+ declareRemote(SessionDeleteService, "healthScan");
313
+ declareRemote(SessionDeleteService, "cleanHealth");
166
314
  declareRemote(SessionDeleteService, "reconcile");
package/lib/trash.js CHANGED
@@ -167,7 +167,11 @@ export async function listEntries(dataRoot) {
167
167
  try {
168
168
  names = (await readdir(root, { withFileTypes: true }))
169
169
  .filter(
170
- (entry) => entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "journal",
170
+ (entry) =>
171
+ entry.isDirectory() &&
172
+ !entry.name.startsWith(".") &&
173
+ entry.name !== "journal" &&
174
+ entry.name !== "receipts",
171
175
  )
172
176
  .map((entry) => entry.name);
173
177
  } catch {
package/locale/en.json CHANGED
@@ -32,6 +32,22 @@
32
32
  "panel.readOnly": "Read-only: this DSH version is missing required APIs; deletion is disabled.",
33
33
  "panel.close": "Close",
34
34
  "toast.failed": "Operation failed: {message}",
35
- "footer.trash": "Trash"
35
+ "footer.trash": "Trash",
36
+ "panel.tabHealth": "Health",
37
+ "health.loading": "Checking…",
38
+ "health.inventory": "stored {stored} · live {live} · archived {archived} · trash {trash} · pending {pending}",
39
+ "health.ok": "No dangling references found.",
40
+ "health.items": "{n} item(s)",
41
+ "health.unavailable": "Cannot run the check: {reason}",
42
+ "health.clean": "Clean dangling references ({n})",
43
+ "health.cleanConfirm": "Clean {n} dangling reference(s)? Bookkeeping only, no session files.",
44
+ "health.cleanDone": "Cleaned {n}",
45
+ "panel.tabReceipts": "Receipts",
46
+ "receipts.empty": "No operation receipts yet.",
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)"
36
52
  }
37
53
  }
package/locale/zh.json CHANGED
@@ -32,6 +32,22 @@
32
32
  "panel.readOnly": "只读模式:当前 DSH 版本缺少依赖接口,删除功能已禁用。",
33
33
  "panel.close": "关闭",
34
34
  "toast.failed": "操作失败:{message}",
35
- "footer.trash": "回收站"
35
+ "footer.trash": "回收站",
36
+ "panel.tabHealth": "体检",
37
+ "health.loading": "正在检查…",
38
+ "health.inventory": "已存 {stored} · live {live} · 归档 {archived} · 回收站 {trash} · 待处理 {pending}",
39
+ "health.ok": "未发现失效引用。",
40
+ "health.items": "{n} 条",
41
+ "health.unavailable": "无法体检:{reason}",
42
+ "health.clean": "清理失效引用({n})",
43
+ "health.cleanConfirm": "确认清理 {n} 条?只动登记表,不碰会话文件",
44
+ "health.cleanDone": "已清理 {n}",
45
+ "panel.tabReceipts": "回执",
46
+ "receipts.empty": "还没有操作回执。",
47
+ "receipts.restored": "已移回原位置",
48
+ "confirm.cascade": "同时删除这 {n} 个子会话(各自进入回收站,需分别恢复)",
49
+ "health.agentTool": "模型工具 session_delete:{state}",
50
+ "health.agentToolOn": "已注册",
51
+ "health.agentToolOff": "未注册(宿主未提供 tools 运行时)"
36
52
  }
37
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kangyishuai/dsh-session-delete",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Real session deletion for DSH: trash by default, plan before delete, audit log, residue receipt",
5
5
  "keywords": [
6
6
  "dsh",