@kangyishuai/dsh-session-delete 0.4.9 → 0.5.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 +21 -16
- package/README.zh-CN.md +15 -10
- package/lib/client.js +1564 -788
- package/lib/operations.js +231 -7
- package/lib/plan.js +24 -0
- package/lib/preferences.js +90 -0
- package/lib/service.js +60 -0
- package/locale/en.json +70 -44
- package/locale/zh.json +71 -45
- package/package.json +4 -2
package/lib/operations.js
CHANGED
|
@@ -9,7 +9,8 @@ import { describeCapabilities, probeCapabilities } from "./capability.js";
|
|
|
9
9
|
import { beginOperation, finishOperation, listPending } from "./journal.js";
|
|
10
10
|
import { directorySize, locateSession } from "./locate.js";
|
|
11
11
|
import { auditPath, requireSessionId, sessionsRoot, trashRoot } from "./paths.js";
|
|
12
|
-
import { buildPlan, planExecutable, summarizePlan } from "./plan.js";
|
|
12
|
+
import { buildPlan, digestOfWorkspace, planExecutable, summarizePlan } from "./plan.js";
|
|
13
|
+
import { readPreferences, writePreferences } from "./preferences.js";
|
|
13
14
|
import { listReceipts, writeReceipt } from "./receipts.js";
|
|
14
15
|
import { buildRefs } from "./refs.js";
|
|
15
16
|
import { listEntries, moveIntoTrash, purgeEntry, readEntry, restoreEntry } from "./trash.js";
|
|
@@ -21,6 +22,14 @@ const pkg = nodeRequire("../package.json");
|
|
|
21
22
|
const SETTLE_MS = 200;
|
|
22
23
|
/** 移走之后复查原路径的时间点;活句柄迟到写入会在这个窗口里重建目录。 */
|
|
23
24
|
const REVIVE_CHECK_MS = 1500;
|
|
25
|
+
/**
|
|
26
|
+
* 单次批量 RPC 的条目上限。界面按 BATCH_CHUNK 分批调用,这里只是防御性硬上限:
|
|
27
|
+
* 0.4.x 的上限是 50,用户回收站 80 条时「全选 → 彻底删除」会被整单拒绝,
|
|
28
|
+
* 表现成「点了没反应」——所以上限要远大于一次常见的批量规模。
|
|
29
|
+
*/
|
|
30
|
+
const MAX_BATCH_IDS = 200;
|
|
31
|
+
/** 客户端每次批量请求的条数,同时也是这里建议的单批规模。 */
|
|
32
|
+
export const BATCH_CHUNK = 20;
|
|
24
33
|
|
|
25
34
|
/** @param {unknown} error @returns {string} */
|
|
26
35
|
function messageOf(error) {
|
|
@@ -78,7 +87,11 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
78
87
|
* 收集一份完整事实并生成 Plan。
|
|
79
88
|
* @param {unknown} sessionIdRaw
|
|
80
89
|
*/
|
|
81
|
-
|
|
90
|
+
/**
|
|
91
|
+
* @param {unknown} sessionIdRaw
|
|
92
|
+
* @param {{ all?: unknown[] }} [options] all = 预加载的 persistence.list()(工作区批量复用)
|
|
93
|
+
*/
|
|
94
|
+
async function makePlan(sessionIdRaw, options = {}) {
|
|
82
95
|
const sessionId = requireSessionId(sessionIdRaw);
|
|
83
96
|
/** @type {string[]} */
|
|
84
97
|
const notes = [];
|
|
@@ -117,7 +130,8 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
117
130
|
});
|
|
118
131
|
if (caps.checks["persistence.list"] && caps.services.persistence?.list) {
|
|
119
132
|
try {
|
|
120
|
-
|
|
133
|
+
// 工作区批量会把同一个列表复用给每个会话,否则 N 个会话要拉 N 次全量列表。
|
|
134
|
+
const all = options.all ?? (await caps.services.persistence.list()) ?? [];
|
|
121
135
|
/** @type {Map<string, { id: string, title: string | null }[]>} */
|
|
122
136
|
const byParent = new Map();
|
|
123
137
|
for (const entry of all) {
|
|
@@ -237,10 +251,11 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
237
251
|
|
|
238
252
|
/**
|
|
239
253
|
* @param {string} sessionIdRaw
|
|
240
|
-
* @param {{ digest?: string, title?: string | null, operator?: string, cascade?: boolean, child?: boolean
|
|
254
|
+
* @param {{ digest?: string, title?: string | null, operator?: string, cascade?: boolean, child?: boolean,
|
|
255
|
+
* all?: unknown[] }} request all = 预加载的 persistence.list()(工作区批量复用)
|
|
241
256
|
*/
|
|
242
257
|
async function trash(sessionIdRaw, request = {}) {
|
|
243
|
-
const loaded = await makePlan(sessionIdRaw);
|
|
258
|
+
const loaded = await makePlan(sessionIdRaw, { all: request.all });
|
|
244
259
|
const { plan, location, header } = loaded;
|
|
245
260
|
if (request.digest && request.digest !== plan.digest) {
|
|
246
261
|
throw failure(
|
|
@@ -859,6 +874,24 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
859
874
|
return { ok: errors.length === 0, cleaned: counts, total, errors, receiptId };
|
|
860
875
|
}
|
|
861
876
|
|
|
877
|
+
/**
|
|
878
|
+
* 读取界面偏好(目前只有确认框里「同时删除子会话」的初值)。
|
|
879
|
+
* 坏文件/坏字段由 preferences.js 统一回落默认值,这里不抛。
|
|
880
|
+
* @returns {Promise<{ version: number, cascadeChildren: boolean }>}
|
|
881
|
+
*/
|
|
882
|
+
async function getPrefs() {
|
|
883
|
+
return readPreferences(dataRoot, logger);
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* 合并写入界面偏好。
|
|
888
|
+
* @param {Record<string, unknown>} patch
|
|
889
|
+
* @returns {Promise<{ version: number, cascadeChildren: boolean }>}
|
|
890
|
+
*/
|
|
891
|
+
async function setPrefs(patch = {}) {
|
|
892
|
+
return writePreferences(dataRoot, patch, logger);
|
|
893
|
+
}
|
|
894
|
+
|
|
862
895
|
async function health() {
|
|
863
896
|
const pending = await listPending(dataRoot);
|
|
864
897
|
return {
|
|
@@ -880,7 +913,12 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
880
913
|
async function restoreMany(request = {}) {
|
|
881
914
|
const ids = Array.isArray(request.entryIds) ? request.entryIds : [];
|
|
882
915
|
if (ids.length === 0) throw failure("session-delete/invalid-request", "没有选择任何条目");
|
|
883
|
-
if (ids.length >
|
|
916
|
+
if (ids.length > MAX_BATCH_IDS) {
|
|
917
|
+
throw failure(
|
|
918
|
+
"session-delete/invalid-request",
|
|
919
|
+
`一次最多 ${MAX_BATCH_IDS} 条(界面会自动分批)`,
|
|
920
|
+
);
|
|
921
|
+
}
|
|
884
922
|
/** @type {{ entryId: string, ok: boolean, sessionId?: string, error?: string }[]} */
|
|
885
923
|
const results = [];
|
|
886
924
|
for (const entryId of ids) {
|
|
@@ -902,7 +940,12 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
902
940
|
async function purgeMany(request = {}) {
|
|
903
941
|
const ids = Array.isArray(request.entryIds) ? request.entryIds : [];
|
|
904
942
|
if (ids.length === 0) throw failure("session-delete/invalid-request", "没有选择任何条目");
|
|
905
|
-
if (ids.length >
|
|
943
|
+
if (ids.length > MAX_BATCH_IDS) {
|
|
944
|
+
throw failure(
|
|
945
|
+
"session-delete/invalid-request",
|
|
946
|
+
`一次最多 ${MAX_BATCH_IDS} 条(界面会自动分批)`,
|
|
947
|
+
);
|
|
948
|
+
}
|
|
906
949
|
/** @type {{ entryId: string, ok: boolean, bytes?: number, error?: string }[]} */
|
|
907
950
|
const results = [];
|
|
908
951
|
for (const entryId of ids) {
|
|
@@ -917,6 +960,183 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
917
960
|
return { ok: okCount === results.length, total: results.length, succeeded: okCount, results };
|
|
918
961
|
}
|
|
919
962
|
|
|
963
|
+
/**
|
|
964
|
+
* 工作区记账里的成员会话。侧栏的工作区分组就是这份清单一族(组内 = 这些 id 里
|
|
965
|
+
* 还能在会话列表里找到的那些),所以「删这个工作区的会话」= 这里返回的 id。
|
|
966
|
+
* 优先读 host workspaceRegistry(活状态);没有该服务时回落到 workspace.json(只读兜底)。
|
|
967
|
+
* @param {unknown} workspaceIdRaw
|
|
968
|
+
* @returns {Promise<{ workspaceId: string, title: string | null, sessionIds: string[] }>}
|
|
969
|
+
*/
|
|
970
|
+
async function workspaceMembers(workspaceIdRaw) {
|
|
971
|
+
const workspaceId = typeof workspaceIdRaw === "string" ? workspaceIdRaw.trim() : "";
|
|
972
|
+
if (workspaceId.length === 0) {
|
|
973
|
+
throw failure("session-delete/invalid-request", "缺少工作区 id");
|
|
974
|
+
}
|
|
975
|
+
const registry = caps.services.workspace;
|
|
976
|
+
if (typeof registry?.list === "function") {
|
|
977
|
+
try {
|
|
978
|
+
const rows = registry.list() ?? [];
|
|
979
|
+
const found = rows.find((/** @type {any} */ row) => String(row?.id) === workspaceId);
|
|
980
|
+
if (found) {
|
|
981
|
+
return {
|
|
982
|
+
workspaceId,
|
|
983
|
+
title: typeof found.title === "string" ? found.title : null,
|
|
984
|
+
sessionIds: Array.isArray(found.sessionIds) ? found.sessionIds.map(String) : [],
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
} catch (error) {
|
|
988
|
+
logger.warn(`session-delete: workspaceRegistry.list failed: ${messageOf(error)}`);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
// 文件兜底:老版本/headless 组合里没有 workspaceRegistry。
|
|
992
|
+
const state = await readWorkspaceState();
|
|
993
|
+
const fromFile = state.workspaces.find((row) => row.id === workspaceId);
|
|
994
|
+
if (fromFile) {
|
|
995
|
+
return { workspaceId, title: null, sessionIds: fromFile.sessionIds.map(String) };
|
|
996
|
+
}
|
|
997
|
+
throw failure("session-delete/not-found", `找不到工作区 ${workspaceId}`, { workspaceId });
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/**
|
|
1001
|
+
* 工作区级影响面(dry-run)。逐个会话算 plan,跳过已经不存在的(登记表幽灵),
|
|
1002
|
+
* 聚合成一份带 digest 的预览,供确认框一次性展示。
|
|
1003
|
+
* @param {unknown} workspaceIdRaw
|
|
1004
|
+
* @returns {Promise<any>}
|
|
1005
|
+
*/
|
|
1006
|
+
async function planWorkspace(workspaceIdRaw) {
|
|
1007
|
+
const workspace = await workspaceMembers(workspaceIdRaw);
|
|
1008
|
+
/** @type {unknown[] | undefined} */
|
|
1009
|
+
let all;
|
|
1010
|
+
if (caps.checks["persistence.list"] && caps.services.persistence?.list) {
|
|
1011
|
+
try {
|
|
1012
|
+
all = (await caps.services.persistence.list()) ?? [];
|
|
1013
|
+
} catch (error) {
|
|
1014
|
+
logger.warn(`session-delete: persistence.list failed: ${messageOf(error)}`);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
/** @type {{ sessionId: string, title: string | null, sizeBytes: number | null, live: boolean,
|
|
1018
|
+
* children: { id: string, title: string | null }[], blockers: { code: string, message: string }[],
|
|
1019
|
+
* digest: string }[]} */
|
|
1020
|
+
const sessions = [];
|
|
1021
|
+
/** @type {string[]} */
|
|
1022
|
+
const missing = [];
|
|
1023
|
+
for (const sessionId of workspace.sessionIds) {
|
|
1024
|
+
try {
|
|
1025
|
+
const { plan } = await makePlan(sessionId, { all });
|
|
1026
|
+
sessions.push({
|
|
1027
|
+
sessionId: plan.sessionId,
|
|
1028
|
+
title: plan.title,
|
|
1029
|
+
sizeBytes: plan.sizeBytes,
|
|
1030
|
+
live: plan.live,
|
|
1031
|
+
children: plan.children,
|
|
1032
|
+
blockers: plan.blockers,
|
|
1033
|
+
digest: plan.digest,
|
|
1034
|
+
});
|
|
1035
|
+
} catch (error) {
|
|
1036
|
+
// 登记表里指向已不存在的会话:跳过而不是整单失败(体检会单独报告它们)。
|
|
1037
|
+
if (/** @type {any} */ (error)?.code === "session/not-found") {
|
|
1038
|
+
missing.push(sessionId);
|
|
1039
|
+
continue;
|
|
1040
|
+
}
|
|
1041
|
+
throw error;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
let sizeBytes = 0;
|
|
1045
|
+
let live = 0;
|
|
1046
|
+
let children = 0;
|
|
1047
|
+
let blocked = 0;
|
|
1048
|
+
for (const session of sessions) {
|
|
1049
|
+
if (typeof session.sizeBytes === "number") sizeBytes += session.sizeBytes;
|
|
1050
|
+
if (session.live) live += 1;
|
|
1051
|
+
children += session.children.length;
|
|
1052
|
+
if (session.blockers.length > 0) blocked += 1;
|
|
1053
|
+
}
|
|
1054
|
+
return {
|
|
1055
|
+
workspaceId: workspace.workspaceId,
|
|
1056
|
+
title: workspace.title,
|
|
1057
|
+
sessions,
|
|
1058
|
+
total: sessions.length,
|
|
1059
|
+
missing,
|
|
1060
|
+
sizeBytes,
|
|
1061
|
+
live,
|
|
1062
|
+
children,
|
|
1063
|
+
blocked,
|
|
1064
|
+
digest: digestOfWorkspace({ workspaceId: workspace.workspaceId, sessions }),
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* 工作区级批量删除:逐条走既有的 trash(各自的归档闸门、停止、引用清理、审计与回执),
|
|
1070
|
+
* 汇总部分失败而不是中途放弃——一个卡住的会话不应该让其余会话留在磁盘上。
|
|
1071
|
+
* @param {unknown} workspaceIdRaw
|
|
1072
|
+
* @param {{ digest?: string, cascade?: boolean, operator?: string }} [request]
|
|
1073
|
+
* @returns {Promise<any>}
|
|
1074
|
+
*/
|
|
1075
|
+
async function trashWorkspace(workspaceIdRaw, request = {}) {
|
|
1076
|
+
const preview = await planWorkspace(workspaceIdRaw);
|
|
1077
|
+
if (request.digest && request.digest !== preview.digest) {
|
|
1078
|
+
throw failure(
|
|
1079
|
+
"session-delete/stale-plan",
|
|
1080
|
+
"这个工作区的会话在确认期间发生了变化,请重新查看影响面。",
|
|
1081
|
+
{ workspaceId: preview.workspaceId, expected: preview.digest, actual: request.digest },
|
|
1082
|
+
);
|
|
1083
|
+
}
|
|
1084
|
+
const cascade = request.cascade === true;
|
|
1085
|
+
const operator = request.operator ?? "ui";
|
|
1086
|
+
/** @type {unknown[] | undefined} */
|
|
1087
|
+
let all;
|
|
1088
|
+
if (caps.checks["persistence.list"] && caps.services.persistence?.list) {
|
|
1089
|
+
try {
|
|
1090
|
+
all = (await caps.services.persistence.list()) ?? [];
|
|
1091
|
+
} catch (error) {
|
|
1092
|
+
logger.warn(`session-delete: persistence.list failed: ${messageOf(error)}`);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
/** @type {{ sessionId: string, title: string | null, ok: boolean, entryId?: string, children?: number, error?: string }[]} */
|
|
1096
|
+
const results = [];
|
|
1097
|
+
for (const session of preview.sessions) {
|
|
1098
|
+
try {
|
|
1099
|
+
const result = await trash(session.sessionId, {
|
|
1100
|
+
digest: session.digest,
|
|
1101
|
+
cascade,
|
|
1102
|
+
operator,
|
|
1103
|
+
all,
|
|
1104
|
+
});
|
|
1105
|
+
results.push({
|
|
1106
|
+
sessionId: session.sessionId,
|
|
1107
|
+
title: session.title,
|
|
1108
|
+
ok: true,
|
|
1109
|
+
entryId: result.entryId,
|
|
1110
|
+
children: Array.isArray(result.cascaded) ? result.cascaded.length : 0,
|
|
1111
|
+
});
|
|
1112
|
+
} catch (error) {
|
|
1113
|
+
results.push({
|
|
1114
|
+
sessionId: session.sessionId,
|
|
1115
|
+
title: session.title,
|
|
1116
|
+
ok: false,
|
|
1117
|
+
error: messageOf(error),
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
const okCount = results.filter((item) => item.ok).length;
|
|
1122
|
+
// 一次批量一条汇总审计:逐条的 trash 记录已经在 trash() 里写过了。
|
|
1123
|
+
await audit
|
|
1124
|
+
.append({
|
|
1125
|
+
action: "trash",
|
|
1126
|
+
result: okCount === results.length ? "ok" : "failed",
|
|
1127
|
+
operator,
|
|
1128
|
+
detail: `workspace ${preview.workspaceId}: ${okCount}/${results.length} 个会话,含 ${preview.children} 个子会话,cascade=${cascade}`,
|
|
1129
|
+
})
|
|
1130
|
+
.catch(() => {});
|
|
1131
|
+
return {
|
|
1132
|
+
ok: okCount === results.length,
|
|
1133
|
+
workspaceId: preview.workspaceId,
|
|
1134
|
+
total: results.length,
|
|
1135
|
+
succeeded: okCount,
|
|
1136
|
+
results,
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
|
|
920
1140
|
/** @param {number} [limit] */
|
|
921
1141
|
async function receipts(limit = 50) {
|
|
922
1142
|
return { entries: await listReceipts(dataRoot, limit) };
|
|
@@ -937,5 +1157,9 @@ export function createOperations({ ctx, dataRoot, logger }) {
|
|
|
937
1157
|
purge,
|
|
938
1158
|
reconcile,
|
|
939
1159
|
auditLog,
|
|
1160
|
+
getPrefs,
|
|
1161
|
+
setPrefs,
|
|
1162
|
+
planWorkspace,
|
|
1163
|
+
trashWorkspace,
|
|
940
1164
|
};
|
|
941
1165
|
}
|
package/lib/plan.js
CHANGED
|
@@ -95,6 +95,30 @@ export function digestOf(plan) {
|
|
|
95
95
|
return sha256(stableStringify(canonical)).slice(0, 32);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
/**
|
|
99
|
+
* 工作区批量的摘要:覆盖工作区身份与**每个**会话的删除后果(体积/活体/子会话/阻断),
|
|
100
|
+
* 不覆盖标题与时间戳——改名不应该让用户重新确认一遍。
|
|
101
|
+
* 排序后再哈希,保证同一组事实永远得到同一摘要。
|
|
102
|
+
* @param {{ workspaceId: string, sessions: { sessionId: string, sizeBytes?: number | null, live?: boolean,
|
|
103
|
+
* children?: { id: string }[], blockers?: { code: string }[] }[] }} input
|
|
104
|
+
* @returns {string}
|
|
105
|
+
*/
|
|
106
|
+
export function digestOfWorkspace(input) {
|
|
107
|
+
const canonical = {
|
|
108
|
+
workspaceId: input.workspaceId,
|
|
109
|
+
sessions: [...input.sessions]
|
|
110
|
+
.sort((a, b) => a.sessionId.localeCompare(b.sessionId))
|
|
111
|
+
.map((session) => ({
|
|
112
|
+
sessionId: session.sessionId,
|
|
113
|
+
sizeBytes: typeof session.sizeBytes === "number" ? session.sizeBytes : null,
|
|
114
|
+
live: session.live === true,
|
|
115
|
+
children: (session.children ?? []).map((child) => child.id).sort(),
|
|
116
|
+
blockers: (session.blockers ?? []).map((blocker) => blocker.code).sort(),
|
|
117
|
+
})),
|
|
118
|
+
};
|
|
119
|
+
return sha256(stableStringify(canonical)).slice(0, 32);
|
|
120
|
+
}
|
|
121
|
+
|
|
98
122
|
/**
|
|
99
123
|
* 生成 Plan。纯函数:不读盘、不调宿主服务。
|
|
100
124
|
* @param {PlanInput} input
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 界面偏好的持久化:目前只有一项 —— 删除确认框里「同时删除子会话」的初值。
|
|
3
|
+
// 为什么落在宿主侧而不是 localStorage:DSH Web 以 127.0.0.1:<随机端口> 作为 origin,
|
|
4
|
+
// 端口一变 localStorage 就是另一份,用户会看到「明明上次勾了却又没勾」。
|
|
5
|
+
// 放的规则:坏文件、坏字段一律静默回落到默认值 —— 偏好永远不该阻断一次删除。
|
|
6
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { requireInside, trashRoot } from "./paths.js";
|
|
9
|
+
|
|
10
|
+
/** 偏好文件版本号。加字段是兼容的;改语义必须递增,读到不认识的版本直接回落默认值。 */
|
|
11
|
+
export const PREFERENCES_VERSION = 1;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 默认偏好。级联删除默认关闭:ADR 0003 要求「删不删子会话由用户决定」,
|
|
15
|
+
* 记忆只是把用户上一次的决定带过来,不能凭空把默认值改成 true。
|
|
16
|
+
* @type {{ version: number, cascadeChildren: boolean }}
|
|
17
|
+
*/
|
|
18
|
+
export const DEFAULT_PREFERENCES = Object.freeze({
|
|
19
|
+
version: PREFERENCES_VERSION,
|
|
20
|
+
cascadeChildren: false,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 偏好文件路径(就在回收站根下,跟审计日志、回执放一起)。
|
|
25
|
+
* @param {string} dataRoot
|
|
26
|
+
* @returns {string}
|
|
27
|
+
*/
|
|
28
|
+
export function preferencesPath(dataRoot) {
|
|
29
|
+
return requireInside(dataRoot, join(trashRoot(dataRoot), "preferences.json"));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 把任意输入收敛成一份合法偏好。只认显式 `true`,其余一切(缺失、类型错、老版本)都算 false。
|
|
34
|
+
* @param {unknown} value
|
|
35
|
+
* @returns {{ version: number, cascadeChildren: boolean }}
|
|
36
|
+
*/
|
|
37
|
+
export function normalizePreferences(value) {
|
|
38
|
+
const record = value && typeof value === "object" ? /** @type {any} */ (value) : {};
|
|
39
|
+
const version = Number.isInteger(record.version) ? record.version : PREFERENCES_VERSION;
|
|
40
|
+
const known = version === PREFERENCES_VERSION;
|
|
41
|
+
return {
|
|
42
|
+
version: PREFERENCES_VERSION,
|
|
43
|
+
cascadeChildren: known && record.cascadeChildren === true,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 读取偏好。文件不存在是正常情况(首次使用);损坏时告警并回落默认值。
|
|
49
|
+
* @param {string} dataRoot
|
|
50
|
+
* @param {{ warn?: (message: string) => void }} [logger]
|
|
51
|
+
* @returns {Promise<{ version: number, cascadeChildren: boolean }>}
|
|
52
|
+
*/
|
|
53
|
+
export async function readPreferences(dataRoot, logger) {
|
|
54
|
+
const file = preferencesPath(dataRoot);
|
|
55
|
+
let raw;
|
|
56
|
+
try {
|
|
57
|
+
raw = await readFile(file, "utf8");
|
|
58
|
+
} catch (error) {
|
|
59
|
+
const code = /** @type {any} */ (error)?.code;
|
|
60
|
+
if (code !== "ENOENT") {
|
|
61
|
+
logger?.warn?.(`session-delete: cannot read ${file}: ${String(error)}`);
|
|
62
|
+
}
|
|
63
|
+
return { ...DEFAULT_PREFERENCES };
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
return normalizePreferences(JSON.parse(raw));
|
|
67
|
+
} catch (error) {
|
|
68
|
+
logger?.warn?.(`session-delete: ignoring malformed ${file}: ${String(error)}`);
|
|
69
|
+
return { ...DEFAULT_PREFERENCES };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 写入偏好:合并补丁 → 校验 → 原子写(临时文件 + rename)。
|
|
75
|
+
* 原子写是必要的:半写文件会让下一次读取回落到默认值,等于用户的选择被静默丢弃。
|
|
76
|
+
* @param {string} dataRoot
|
|
77
|
+
* @param {Record<string, unknown>} patch
|
|
78
|
+
* @param {{ warn?: (message: string) => void }} [logger]
|
|
79
|
+
* @returns {Promise<{ version: number, cascadeChildren: boolean }>}
|
|
80
|
+
*/
|
|
81
|
+
export async function writePreferences(dataRoot, patch, logger) {
|
|
82
|
+
const current = await readPreferences(dataRoot, logger);
|
|
83
|
+
const next = normalizePreferences({ ...current, ...(patch ?? {}) });
|
|
84
|
+
const file = preferencesPath(dataRoot);
|
|
85
|
+
await mkdir(trashRoot(dataRoot), { recursive: true });
|
|
86
|
+
const tmp = `${file}.tmp`;
|
|
87
|
+
await writeFile(tmp, `${JSON.stringify(next, null, 2)}\n`, "utf8");
|
|
88
|
+
await rename(tmp, file);
|
|
89
|
+
return next;
|
|
90
|
+
}
|
package/lib/service.js
CHANGED
|
@@ -183,6 +183,34 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
+
/**
|
|
187
|
+
* 界面偏好:确认框里「同时删除子会话」的初值。
|
|
188
|
+
* 存宿主侧而不是 localStorage —— Web 的 origin 带随机端口,换端口就丢。
|
|
189
|
+
*/
|
|
190
|
+
async getPrefs() {
|
|
191
|
+
try {
|
|
192
|
+
return await this.ops.getPrefs();
|
|
193
|
+
} catch (error) {
|
|
194
|
+
rethrow(error);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 合并写入界面偏好。
|
|
200
|
+
* @param {{ cascadeChildren?: boolean }} [request]
|
|
201
|
+
*/
|
|
202
|
+
async setPrefs(request) {
|
|
203
|
+
try {
|
|
204
|
+
return await this.ops.setPrefs({
|
|
205
|
+
...(typeof request?.cascadeChildren === "boolean"
|
|
206
|
+
? { cascadeChildren: request.cascadeChildren }
|
|
207
|
+
: {}),
|
|
208
|
+
});
|
|
209
|
+
} catch (error) {
|
|
210
|
+
rethrow(error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
186
214
|
/**
|
|
187
215
|
* 只读:返回完整影响面(plan/dry-run 的数据源)。
|
|
188
216
|
* @param {{ sessionId: string }} request
|
|
@@ -195,6 +223,34 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
195
223
|
}
|
|
196
224
|
}
|
|
197
225
|
|
|
226
|
+
/**
|
|
227
|
+
* 工作区级影响面:一次返回该工作区全部会话的体积/活体/子会话与聚合 digest。
|
|
228
|
+
* @param {{ workspaceId: string }} request
|
|
229
|
+
*/
|
|
230
|
+
async planWorkspace(request) {
|
|
231
|
+
try {
|
|
232
|
+
return await this.ops.planWorkspace(request?.workspaceId);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
rethrow(error);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 把整个工作区的会话逐条移入回收站(汇总部分失败)。
|
|
240
|
+
* @param {{ workspaceId: string, digest?: string, cascade?: boolean }} request
|
|
241
|
+
*/
|
|
242
|
+
async trashWorkspace(request) {
|
|
243
|
+
try {
|
|
244
|
+
return await this.ops.trashWorkspace(request?.workspaceId, {
|
|
245
|
+
digest: request?.digest,
|
|
246
|
+
cascade: request?.cascade === true,
|
|
247
|
+
operator: "ui",
|
|
248
|
+
});
|
|
249
|
+
} catch (error) {
|
|
250
|
+
rethrow(error);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
198
254
|
/**
|
|
199
255
|
* 把会话移入回收站。
|
|
200
256
|
* @param {{ sessionId: string, digest?: string, title?: string | null, cascade?: boolean }} request
|
|
@@ -326,7 +382,11 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
326
382
|
}
|
|
327
383
|
|
|
328
384
|
declareRemote(SessionDeleteService, "health");
|
|
385
|
+
declareRemote(SessionDeleteService, "getPrefs");
|
|
386
|
+
declareRemote(SessionDeleteService, "setPrefs");
|
|
329
387
|
declareRemote(SessionDeleteService, "plan");
|
|
388
|
+
declareRemote(SessionDeleteService, "planWorkspace");
|
|
389
|
+
declareRemote(SessionDeleteService, "trashWorkspace");
|
|
330
390
|
declareRemote(SessionDeleteService, "trash");
|
|
331
391
|
declareRemote(SessionDeleteService, "list");
|
|
332
392
|
declareRemote(SessionDeleteService, "restore");
|
package/locale/en.json
CHANGED
|
@@ -1,59 +1,85 @@
|
|
|
1
1
|
{
|
|
2
2
|
"sessionDelete": {
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"confirm.desc": "The session moves to the local trash and can be restored.",
|
|
8
|
-
"confirm.size": "Session log is about {size}",
|
|
9
|
-
"confirm.live": "This session is still open; its work is stopped first.",
|
|
10
|
-
"confirm.children": "{n} subagent session(s) come along",
|
|
11
|
-
"confirm.refs": "Will clean: {list}",
|
|
12
|
-
"confirm.trashPath": "Trash location: {path}",
|
|
3
|
+
"batch.done": "Done: {ok}/{total} succeeded",
|
|
4
|
+
"batch.failed": "{n} failed; kept selected",
|
|
5
|
+
"batch.progress": "{done}/{total} done",
|
|
6
|
+
"batch.stop": "Stop",
|
|
13
7
|
"confirm.blocked": "Cannot delete right now: {reason}",
|
|
14
8
|
"confirm.cancel": "Cancel",
|
|
9
|
+
"confirm.cascade": "Also delete these {n} child session(s) (each goes to trash separately)",
|
|
10
|
+
"confirm.children": "{n} subagent session(s)",
|
|
15
11
|
"confirm.confirm": "Move to trash",
|
|
16
|
-
"confirm.
|
|
12
|
+
"confirm.desc": "Moves to the local trash and can be restored.",
|
|
13
|
+
"confirm.detail.children": "Child sessions: {list}",
|
|
14
|
+
"confirm.detail.recoverable": "Recoverable: restore from the trash back to its original place",
|
|
15
|
+
"confirm.detail.refs": "References: {list}",
|
|
16
|
+
"confirm.detail.trashPath": "Trash: {path}",
|
|
17
|
+
"confirm.detail.workspaceSessions": "Sessions: {list}",
|
|
18
|
+
"confirm.details": "Details",
|
|
17
19
|
"confirm.done": "Moved to trash",
|
|
20
|
+
"confirm.live": "running; its work stops first",
|
|
21
|
+
"confirm.loading": "Reading what will be removed…",
|
|
22
|
+
"confirm.pending": "Moving to trash…",
|
|
23
|
+
"confirm.refs": "Cleans {n} reference(s)",
|
|
24
|
+
"confirm.size": "about {size}",
|
|
25
|
+
"confirm.title": "Delete session?",
|
|
18
26
|
"confirm.undo": "Undo",
|
|
19
|
-
"confirm.
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
27
|
+
"confirm.workspace.blocked": "{n} cannot be deleted right now and will be reported",
|
|
28
|
+
"confirm.workspace.confirm": "Move {n} to trash",
|
|
29
|
+
"confirm.workspace.live": "{n} running; their work stops first",
|
|
30
|
+
"confirm.workspace.missing": "{n} stale bookkeeping entr(ies) ignored",
|
|
31
|
+
"confirm.workspace.summary": "{n} session(s) · about {size}",
|
|
32
|
+
"confirm.workspace.title": "Delete every session in \"{name}\"?",
|
|
33
|
+
"footer.trash": "Trash",
|
|
34
|
+
"menu.delete": "Delete session…",
|
|
35
|
+
"menu.trash": "Trash…",
|
|
36
|
+
"menu.workspaceTrash": "Delete all sessions in this workspace…",
|
|
37
|
+
"panel.cancelConfirm": "Cancel",
|
|
38
|
+
"panel.close": "Close",
|
|
23
39
|
"panel.empty": "The trash is empty.",
|
|
24
|
-
"panel.
|
|
25
|
-
"panel.restore": "Restore",
|
|
40
|
+
"panel.pending": "{n} unfinished operation(s)",
|
|
26
41
|
"panel.purge": "Delete for good",
|
|
27
|
-
"panel.purgeConfirm": "
|
|
42
|
+
"panel.purgeConfirm": "Cannot be undone — confirm",
|
|
43
|
+
"panel.readOnly": "Read-only: this DSH version is missing required APIs; deletion is disabled.",
|
|
44
|
+
"panel.restore": "Restore",
|
|
28
45
|
"panel.size": "{size}",
|
|
29
|
-
"panel.
|
|
46
|
+
"panel.title": "Session trash",
|
|
47
|
+
"panel.trashedAt": "{time}",
|
|
30
48
|
"panel.unknown": "{n} unrecognized director(ies) kept",
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
49
|
+
"settings.audit.empty": "No audit entries yet.",
|
|
50
|
+
"settings.audit.path": "Log file: {path}",
|
|
51
|
+
"settings.audit.title": "Audit log",
|
|
52
|
+
"settings.cascade.desc": "The next delete confirmation starts from your last choice.",
|
|
53
|
+
"settings.cascade.reset": "Reset to default (unchecked)",
|
|
54
|
+
"settings.cascade.resetDone": "Reset",
|
|
55
|
+
"settings.cascade.title": "Remember \"also delete child sessions\"",
|
|
56
|
+
"settings.group.delete": "Deletion preferences",
|
|
57
|
+
"settings.group.maintenance": "Maintenance",
|
|
58
|
+
"settings.group.trash": "Trash",
|
|
59
|
+
"settings.health.clean": "Clean {n}",
|
|
60
|
+
"settings.health.cleanConfirm": "Clean {n} dangling reference(s)? Bookkeeping only.",
|
|
61
|
+
"settings.health.cleaned": "Cleaned {n}",
|
|
62
|
+
"settings.health.desc": "Checks bookkeeping that points at sessions that no longer exist (read-only).",
|
|
63
|
+
"settings.health.items": "{n} to clean",
|
|
64
|
+
"settings.health.ok": "No dangling references found.",
|
|
65
|
+
"settings.health.title": "Health",
|
|
66
|
+
"settings.health.unavailable": "Cannot run the check: {reason}",
|
|
67
|
+
"settings.loading": "Loading…",
|
|
68
|
+
"settings.nav": "Session trash",
|
|
69
|
+
"settings.receipts.empty": "No operation receipts yet.",
|
|
70
|
+
"settings.receipts.title": "Receipts",
|
|
71
|
+
"settings.trash.open": "Open trash",
|
|
72
|
+
"settings.trash.summary": "{n} session(s) · {size}",
|
|
73
|
+
"shortcut.busy": "A delete confirmation is already open",
|
|
74
|
+
"shortcut.delete": "Delete current session…",
|
|
75
|
+
"shortcut.noSession": "No current session to delete",
|
|
76
|
+
"shortcut.workspace": "Delete every session in the current workspace…",
|
|
34
77
|
"toast.failed": "Operation failed: {message}",
|
|
35
|
-
"
|
|
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)",
|
|
52
|
-
"batch.done": "Done: {ok}/{total} succeeded",
|
|
53
|
-
"trash.selectAll": "Select all",
|
|
54
|
-
"trash.clear": "Clear",
|
|
55
|
-
"trash.restoreMany": "Restore {n}",
|
|
78
|
+
"toast.workspace.done": "Moved {ok}/{total} session(s) to the trash",
|
|
56
79
|
"trash.purgeMany": "Delete {n} permanently",
|
|
57
|
-
"trash.purgeManyConfirm": "Permanently delete {n}? This cannot be undone"
|
|
80
|
+
"trash.purgeManyConfirm": "Permanently delete {n}? This cannot be undone",
|
|
81
|
+
"trash.restoreMany": "Restore ({n})",
|
|
82
|
+
"trash.selectAll": "Select all",
|
|
83
|
+
"trash.selectCount": "{n}/{total} selected"
|
|
58
84
|
}
|
|
59
85
|
}
|