dsh-session-editor 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/cordis.patch.yml +3 -0
- package/package.json +61 -4
- package//345/233/276/346/240/207.svg +8 -0
- package//345/256/277/344/270/273.js +782 -0
- package//347/225/214/351/235/242.js +804 -0
- package//350/257/273/346/210/221.md +71 -0
- package/README.md +0 -3
|
@@ -0,0 +1,782 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「对话编辑」插件的 Host 半。
|
|
3
|
+
*
|
|
4
|
+
* 两件事,各自一条实现:
|
|
5
|
+
*
|
|
6
|
+
* - 删除某一轮:用一条 `user/message` 替换记录(`surfaceOp: replace`,与压缩功能同一机制)
|
|
7
|
+
* 把该轮从模型可见表面上遮蔽,立即生效且落盘。原始记录随后在该会话不再处于
|
|
8
|
+
* Host 活动状态时从会话日志里物理删除(活动会话持有写句柄,不能在其下方重写日志)。
|
|
9
|
+
* - 删除整个会话:删除会话目录,同样推迟到「不再活动」的时刻。
|
|
10
|
+
*
|
|
11
|
+
* 重写路径不碰任何 zstd 字节:用 `sessionPersistence.create()` 重建日志,再把存活事件
|
|
12
|
+
* 作为一批连续重编号后追加,因此磁盘格式始终是持久化层自己写出的格式。
|
|
13
|
+
* @module dsh-session-editor
|
|
14
|
+
*/
|
|
15
|
+
import fs from 'node:fs/promises'
|
|
16
|
+
import os from 'node:os'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
import { randomUUID } from 'node:crypto'
|
|
19
|
+
|
|
20
|
+
export const name = 'session-editor'
|
|
21
|
+
export const inject = ['commands', 'sessionPersistence', 'workspaceRegistry', 'sessions']
|
|
22
|
+
|
|
23
|
+
/** 占据模型可见表面上某个节点的事件类型。 */
|
|
24
|
+
const 表面事件类型 = new Set([
|
|
25
|
+
'system/message',
|
|
26
|
+
'developer/message',
|
|
27
|
+
'user/message',
|
|
28
|
+
'assistant/message',
|
|
29
|
+
'tool/result',
|
|
30
|
+
])
|
|
31
|
+
|
|
32
|
+
/** 代替被删除那一轮、给模型看的文案。 */
|
|
33
|
+
const 已删除文案 = '(这一轮对话已被用户删除,不要再引用它。)'
|
|
34
|
+
|
|
35
|
+
const 家目录 = process.env.DSH_HOME ?? path.join(os.homedir(), '.dsh')
|
|
36
|
+
const 会话根目录 = path.join(家目录, 'sessions')
|
|
37
|
+
const 状态目录 = path.join(家目录, 'storages', 'session-editor')
|
|
38
|
+
const 队列路径 = path.join(状态目录, 'queue.json')
|
|
39
|
+
|
|
40
|
+
/** 正在重写的会话,避免两次触发交叉执行。 */
|
|
41
|
+
const 正在处理 = new Set()
|
|
42
|
+
|
|
43
|
+
/** 落盘的待办队列,形如 `{ [会话ID]: { 轮次: number[], 命令ID: string[], 全部删除?: true, 移入工作区?: string } }`。 */
|
|
44
|
+
let 队列
|
|
45
|
+
|
|
46
|
+
async function 载入队列() {
|
|
47
|
+
if (队列 !== undefined) return 队列
|
|
48
|
+
try {
|
|
49
|
+
const 文本 = await fs.readFile(队列路径, 'utf8')
|
|
50
|
+
const 解析结果 = JSON.parse(文本)
|
|
51
|
+
队列 = 解析结果 !== null && typeof 解析结果 === 'object' ? 解析结果 : {}
|
|
52
|
+
} catch {
|
|
53
|
+
队列 = {}
|
|
54
|
+
}
|
|
55
|
+
return 队列
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function 保存队列() {
|
|
59
|
+
await fs.mkdir(状态目录, { recursive: true })
|
|
60
|
+
await fs.writeFile(队列路径, JSON.stringify(队列 ?? {}, null, 2), 'utf8')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function 登记任务(会话ID, 补丁) {
|
|
64
|
+
await 载入队列()
|
|
65
|
+
const 条目 = 队列[会话ID] ?? { 轮次: [], 命令ID: [] }
|
|
66
|
+
if (补丁.全部删除 === true) 条目.全部删除 = true
|
|
67
|
+
if (补丁.移入工作区 !== undefined) 条目.移入工作区 = 补丁.移入工作区
|
|
68
|
+
if (补丁.轮次 !== undefined && !条目.轮次.includes(补丁.轮次)) 条目.轮次.push(补丁.轮次)
|
|
69
|
+
if (补丁.命令ID !== undefined) 条目.命令ID.push(补丁.命令ID)
|
|
70
|
+
队列[会话ID] = 条目
|
|
71
|
+
await 保存队列()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function 注销任务(会话ID) {
|
|
75
|
+
await 载入队列()
|
|
76
|
+
if (队列[会话ID] === undefined) return
|
|
77
|
+
delete 队列[会话ID]
|
|
78
|
+
await 保存队列()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 不重新推导 projectKey,直接找出某个会话的存储目录。 */
|
|
82
|
+
async function 查找会话目录(会话ID) {
|
|
83
|
+
let 项目列表
|
|
84
|
+
try {
|
|
85
|
+
项目列表 = await fs.readdir(会话根目录)
|
|
86
|
+
} catch {
|
|
87
|
+
return undefined
|
|
88
|
+
}
|
|
89
|
+
for (const 项目 of 项目列表) {
|
|
90
|
+
const 候选 = path.join(会话根目录, 项目, 会话ID)
|
|
91
|
+
try {
|
|
92
|
+
const 信息 = await fs.stat(候选)
|
|
93
|
+
if (信息.isDirectory()) return 候选
|
|
94
|
+
} catch {
|
|
95
|
+
// 不在这个 projectKey 下。
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return undefined
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 立刻删除会话的磁盘痕迹:目录(含 zstd 日志)与投影缓存。
|
|
103
|
+
* Windows 的写租约是命名内核信号量而非文件句柄,因此目录可以直接删除,
|
|
104
|
+
* 不必等会话从内存中退出。
|
|
105
|
+
* @returns 'deleted' | 'absent' | 'failed'
|
|
106
|
+
*/
|
|
107
|
+
async function 删除磁盘痕迹(ctx, 会话ID) {
|
|
108
|
+
const 目录 = await 查找会话目录(会话ID)
|
|
109
|
+
const 缓存 = path.join(家目录, 'storages', 'session_projcache', 'sessions', `${会话ID}.json`)
|
|
110
|
+
if (目录 === undefined) {
|
|
111
|
+
try {
|
|
112
|
+
await fs.rm(缓存, { force: true })
|
|
113
|
+
} catch {
|
|
114
|
+
// 缓存不存在或不可删,不影响会话目录状态。
|
|
115
|
+
}
|
|
116
|
+
return 'absent'
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
await fs.rm(目录, { recursive: true, force: true })
|
|
120
|
+
} catch (错误) {
|
|
121
|
+
await 记日志(ctx, `删除目录失败 ${会话ID}:${错误?.message ?? 错误}`)
|
|
122
|
+
return 'failed'
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
await fs.rm(缓存, { force: true })
|
|
126
|
+
} catch {
|
|
127
|
+
// 缓存删除失败不阻塞会话删除。
|
|
128
|
+
}
|
|
129
|
+
return 'deleted'
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 通过持久化层读取磁盘日志(不碰 zstd 字节)。 */
|
|
133
|
+
async function 读取日志(ctx, 会话ID) {
|
|
134
|
+
const 句柄 = await ctx.sessionPersistence.open(会话ID, 'read')
|
|
135
|
+
try {
|
|
136
|
+
const 结果 = await 句柄.read(0)
|
|
137
|
+
return {
|
|
138
|
+
header: 句柄.header,
|
|
139
|
+
inheritedEventCount: 句柄.inheritedEventCount,
|
|
140
|
+
events: [...结果.events],
|
|
141
|
+
}
|
|
142
|
+
} finally {
|
|
143
|
+
await 句柄.close()
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** 把表面历史折叠成模型看到的节点序列。 */
|
|
148
|
+
function 折叠表面(事件列表) {
|
|
149
|
+
const 节点 = []
|
|
150
|
+
for (const 事件 of 事件列表) {
|
|
151
|
+
if (!表面事件类型.has(事件.type)) continue
|
|
152
|
+
const 操作 = 事件.surfaceOp ?? 'append'
|
|
153
|
+
if (操作 === 'append') {
|
|
154
|
+
节点.push(事件.seq)
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
const 起 = 节点.indexOf(操作.startSeq)
|
|
158
|
+
const 止 = 节点.indexOf(操作.endSeq)
|
|
159
|
+
if (起 === -1 || 止 === -1) {
|
|
160
|
+
节点.push(事件.seq)
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
节点.splice(起, 止 - 起 + 1, 事件.seq)
|
|
164
|
+
}
|
|
165
|
+
return 节点
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 把某一轮解析成它在日志里的 seq 区间:从开启它的用户消息,到该轮的 `turn/end`。
|
|
170
|
+
* @param 事件列表 - 按 seq 排好的磁盘事件。
|
|
171
|
+
* @param 轮次 - 客户端显示的第几轮。
|
|
172
|
+
* @returns 区间与原始提问文本;该轮不存在时返回 undefined。
|
|
173
|
+
*/
|
|
174
|
+
function 定位轮次(事件列表, 轮次) {
|
|
175
|
+
const 开始下标 = 事件列表.findIndex((事件) => 事件.type === 'turn/start' && 事件.data?.turn === 轮次)
|
|
176
|
+
if (开始下标 < 0) return undefined
|
|
177
|
+
let 结束下标 = -1
|
|
178
|
+
for (let 下标 = 事件列表.length - 1; 下标 > 开始下标; 下标 -= 1) {
|
|
179
|
+
if (事件列表[下标].type === 'turn/end' && 事件列表[下标].data?.turn === 轮次) {
|
|
180
|
+
结束下标 = 下标
|
|
181
|
+
break
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
let 提问下标 = -1
|
|
185
|
+
const 搜索止 = 结束下标 === -1 ? 事件列表.length - 1 : 结束下标
|
|
186
|
+
// 当前格式里用户消息落在 turn/start **之后**(step/start 之后),先向轮内找。
|
|
187
|
+
for (let 下标 = 开始下标; 下标 <= 搜索止; 下标 += 1) {
|
|
188
|
+
const 事件 = 事件列表[下标]
|
|
189
|
+
if (事件.type === 'user/message' && (事件.surfaceOp ?? 'append') === 'append') {
|
|
190
|
+
提问下标 = 下标
|
|
191
|
+
break
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// 兼容旧格式:用户消息在 turn/start 之前。
|
|
195
|
+
if (提问下标 === -1) {
|
|
196
|
+
for (let 下标 = 开始下标 - 1; 下标 >= 0; 下标 -= 1) {
|
|
197
|
+
const 事件 = 事件列表[下标]
|
|
198
|
+
if (事件.type === 'turn/end') break
|
|
199
|
+
if (事件.type === 'user/message' && (事件.surfaceOp ?? 'append') === 'append') {
|
|
200
|
+
提问下标 = 下标
|
|
201
|
+
break
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// 区间始终从 turn/start(或更早的旧格式提问)开始,保证过程/工具事件也落在删除区间内。
|
|
206
|
+
const 起 = 提问下标 === -1 ? 开始下标 : Math.min(开始下标, 提问下标)
|
|
207
|
+
const 止 = 结束下标 === -1 ? 事件列表.length - 1 : 结束下标
|
|
208
|
+
const 提问事件 = 提问下标 === -1 ? undefined : 事件列表[提问下标]
|
|
209
|
+
return {
|
|
210
|
+
轮次,
|
|
211
|
+
startSeq: 事件列表[起].seq,
|
|
212
|
+
endSeq: 事件列表[止].seq,
|
|
213
|
+
promptText: 取出文本(提问事件?.data),
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 用户消息的 seq 定位到它所属的轮次。界面上的按钮挂在用户消息上,
|
|
219
|
+
* 因此只带 seq;区间起点取这条用户消息本身,避免漏删它。
|
|
220
|
+
* @param 事件列表 - 按 seq 排好的磁盘事件。
|
|
221
|
+
* @param 序号 - 用户消息事件的 seq。
|
|
222
|
+
* @returns 轮次、区间与提问文本;找不到所属轮次时返回 undefined。
|
|
223
|
+
*/
|
|
224
|
+
function 按序号取轮次(事件列表, 序号) {
|
|
225
|
+
const 命中下标 = 事件列表.findIndex((事件) => 事件.seq === 序号)
|
|
226
|
+
if (命中下标 < 0) return undefined
|
|
227
|
+
let 开始下标 = -1
|
|
228
|
+
for (let 下标 = 命中下标; 下标 >= 0; 下标 -= 1) {
|
|
229
|
+
if (事件列表[下标].type === 'turn/start') {
|
|
230
|
+
开始下标 = 下标
|
|
231
|
+
break
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (开始下标 < 0) 开始下标 = 事件列表.findIndex((事件) => 事件.type === 'turn/start')
|
|
235
|
+
if (开始下标 < 0) return undefined
|
|
236
|
+
const 这一轮 = 定位轮次(事件列表, 事件列表[开始下标].data?.turn)
|
|
237
|
+
if (这一轮 === undefined) return undefined
|
|
238
|
+
return {
|
|
239
|
+
...这一轮,
|
|
240
|
+
startSeq: Math.min(这一轮.startSeq, 序号),
|
|
241
|
+
promptText: 取出文本(事件列表[命中下标].data),
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 助手消息的消息标识定位到它所属的轮次。图标行挂在回答上,只带 messageId。
|
|
247
|
+
* @param 事件列表 - 按 seq 排好的磁盘事件。
|
|
248
|
+
* @param 消息标识 - 助手消息的 id。
|
|
249
|
+
* @returns 轮次、区间与提问文本;找不到时返回 undefined。
|
|
250
|
+
*/
|
|
251
|
+
function 按消息取轮次(事件列表, 消息标识) {
|
|
252
|
+
const 命中 = 事件列表.find((事件) => 事件.data?.message?.id === 消息标识)
|
|
253
|
+
if (命中 === undefined) return undefined
|
|
254
|
+
const 轮次 = 命中.data?.turn
|
|
255
|
+
if (typeof 轮次 !== 'number') return undefined
|
|
256
|
+
return 定位轮次(事件列表, 轮次)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** 从落盘的消息载荷里取出纯文本。 */
|
|
260
|
+
function 取出文本(数据) {
|
|
261
|
+
const 消息 = 数据?.message ?? 数据
|
|
262
|
+
const 内容 = 消息?.content
|
|
263
|
+
if (!Array.isArray(内容)) return ''
|
|
264
|
+
return 内容
|
|
265
|
+
.filter((块) => 块?.type === 'text' && typeof 块.text === 'string')
|
|
266
|
+
.map((块) => 块.text)
|
|
267
|
+
.join('\n')
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** 按日志里消息的形状构造一条用户消息载荷。 */
|
|
271
|
+
function 构造消息(模板, 文本, 标识) {
|
|
272
|
+
const 内容 = [{ type: 'text', text: 文本 }]
|
|
273
|
+
if (模板 !== undefined && 模板 !== null && typeof 模板 === 'object' && 模板.message !== undefined) {
|
|
274
|
+
return { ...模板, message: { ...模板.message, id: 标识, content: 内容 } }
|
|
275
|
+
}
|
|
276
|
+
const 基底 = 模板 !== null && typeof 模板 === 'object' ? 模板 : {}
|
|
277
|
+
return { ...基底, id: 标识, role: 'user', content: 内容 }
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** 按消息标识解析这一轮的区间;也返回该轮内的全部消息标识与事件时间戳。 */
|
|
281
|
+
function 解析目标轮次(事件列表, 目标) {
|
|
282
|
+
const 这一轮 = 目标.messageId !== undefined
|
|
283
|
+
? 按消息取轮次(事件列表, 目标.messageId)
|
|
284
|
+
: (目标.seq === undefined ? 定位轮次(事件列表, 目标.turn) : 按序号取轮次(事件列表, 目标.seq))
|
|
285
|
+
if (这一轮 === undefined) return undefined
|
|
286
|
+
// 收集该轮里所有消息标识与事件时间戳:客户端据此把这一轮的全部聊天节点
|
|
287
|
+
// (问题、回答、工具调用/结果、过程状态、完成用时等)一起隐藏。
|
|
288
|
+
const 消息ID = []
|
|
289
|
+
const 事件时间 = []
|
|
290
|
+
for (const 事件 of 事件列表) {
|
|
291
|
+
if (事件.seq < 这一轮.startSeq || 事件.seq > 这一轮.endSeq) continue
|
|
292
|
+
if (typeof 事件.time === 'number') 事件时间.push(事件.time)
|
|
293
|
+
const 用户标识 = 事件.type === 'user/message' ? 事件.data?.id : undefined
|
|
294
|
+
if (typeof 用户标识 === 'string') 消息ID.push(用户标识)
|
|
295
|
+
const 助手标识 = 事件.type === 'assistant/message' ? 事件.data?.message?.id : undefined
|
|
296
|
+
if (typeof 助手标识 === 'string') 消息ID.push(助手标识)
|
|
297
|
+
}
|
|
298
|
+
return { ...这一轮, 消息ID, 事件时间 }
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** 只读取某一轮的原文(编辑面板预填用),不产生任何记录。 */
|
|
302
|
+
async function 读取轮次信息(ctx, 会话ID, 目标) {
|
|
303
|
+
const { events } = await 读取日志(ctx, 会话ID)
|
|
304
|
+
if (目标.messageId === undefined && 目标.seq === undefined && 目标.turn === undefined) {
|
|
305
|
+
throw new Error('缺少轮次标识')
|
|
306
|
+
}
|
|
307
|
+
const 这一轮 = 解析目标轮次(events, 目标)
|
|
308
|
+
if (这一轮 === undefined) throw new Error('找不到这条消息所属的轮次')
|
|
309
|
+
return { turn: 这一轮.轮次, prompt: 这一轮.promptText, ids: 这一轮.消息ID, times: 这一轮.事件时间 }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 在模型表面上整轮遮蔽(删除或改写共用),并登记它的物理清除。
|
|
314
|
+
* 替换事件只覆盖表面节点;同一轮的工具调用、步骤与完成状态由返回的
|
|
315
|
+
* 时间戳交给客户端隐藏,并在会话释放时随删除区间一起从磁盘清除。
|
|
316
|
+
*/
|
|
317
|
+
async function 替换轮次(ctx, 会话, 会话ID, 目标, 文本, 命令ID) {
|
|
318
|
+
const { events } = await 读取日志(ctx, 会话ID)
|
|
319
|
+
const 这一轮 = 解析目标轮次(events, 目标)
|
|
320
|
+
if (这一轮 === undefined) throw new Error(`找不到这条消息所属的轮次`)
|
|
321
|
+
const 节点 = 折叠表面(events)
|
|
322
|
+
const 被遮蔽 = 节点.filter((seq) => seq >= 这一轮.startSeq && seq <= 这一轮.endSeq)
|
|
323
|
+
if (被遮蔽.length === 0) throw new Error(`第 ${这一轮.轮次} 轮在当前可见上下文中没有可替换的消息`)
|
|
324
|
+
const 模板 = events.find((事件) => 事件.type === 'user/message')
|
|
325
|
+
会话.append('user/message', 构造消息(模板?.data, 文本, `session-editor-${randomUUID()}`), {
|
|
326
|
+
surfaceOp: { op: 'replace', startSeq: 被遮蔽[0], endSeq: 被遮蔽[被遮蔽.length - 1] },
|
|
327
|
+
sourceEventSeqs: 被遮蔽,
|
|
328
|
+
})
|
|
329
|
+
try {
|
|
330
|
+
if (typeof ctx.sessions?.flush === 'function') await ctx.sessions.flush(会话)
|
|
331
|
+
} catch {
|
|
332
|
+
// 屏障不可用时,交由常规 drain 检查点落盘。
|
|
333
|
+
}
|
|
334
|
+
await 登记任务(会话ID, { 轮次: 这一轮.轮次, 命令ID })
|
|
335
|
+
// 这一轮的提问事件就是区间起点(旧格式里可能早于 turn/start)。
|
|
336
|
+
const 提问事件 = events.find((事件) => 事件.seq === 这一轮.startSeq)
|
|
337
|
+
return {
|
|
338
|
+
turn: 这一轮.轮次,
|
|
339
|
+
messageId: typeof 提问事件?.data?.id === 'string' ? 提问事件.data.id : undefined,
|
|
340
|
+
startSeq: 这一轮.startSeq,
|
|
341
|
+
endSeq: 这一轮.endSeq,
|
|
342
|
+
promptText: 这一轮.promptText,
|
|
343
|
+
被删消息ID: 这一轮.消息ID,
|
|
344
|
+
被删事件时间: 这一轮.事件时间,
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** 某个存活事件是否指向被删掉的 seq。 */
|
|
349
|
+
function 引用了被删事件(事件, 被删集合) {
|
|
350
|
+
if (Array.isArray(事件.sourceEventSeqs) && 事件.sourceEventSeqs.some((seq) => 被删集合.has(seq))) return true
|
|
351
|
+
const 操作 = 事件.surfaceOp
|
|
352
|
+
if (操作 !== undefined && 操作 !== 'append' && (被删集合.has(操作.startSeq) || 被删集合.has(操作.endSeq))) return true
|
|
353
|
+
const 数据 = 事件.data
|
|
354
|
+
if (数据 === null || typeof 数据 !== 'object') return false
|
|
355
|
+
if (typeof 数据.sourceEventSeq === 'number' && 被删集合.has(数据.sourceEventSeq)) return true
|
|
356
|
+
if (Array.isArray(数据.messageSeqs) && 数据.messageSeqs.some((seq) => 被删集合.has(seq))) return true
|
|
357
|
+
if (Array.isArray(数据.shadowedSeqs) && 数据.shadowedSeqs.some((seq) => 被删集合.has(seq))) return true
|
|
358
|
+
if (数据.shadowedRange !== undefined) {
|
|
359
|
+
if (被删集合.has(数据.shadowedRange.start) || 被删集合.has(数据.shadowedRange.end)) return true
|
|
360
|
+
}
|
|
361
|
+
return false
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** 某个存活事件的数据里是否还带着已被删除的消息标识。 */
|
|
365
|
+
function 含被删消息(事件, 被删消息) {
|
|
366
|
+
if (被删消息.size === 0) return false
|
|
367
|
+
const 文本 = JSON.stringify(事件.data ?? {})
|
|
368
|
+
for (const 标识 of 被删消息) {
|
|
369
|
+
if (文本.includes(标识)) return true
|
|
370
|
+
}
|
|
371
|
+
return false
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** 把一个事件改写成压缩后的序号空间。 */
|
|
375
|
+
function 重编号(事件, 映射) {
|
|
376
|
+
const 结果 = { ...事件, seq: 映射.get(事件.seq) }
|
|
377
|
+
const 操作 = 事件.surfaceOp
|
|
378
|
+
if (操作 !== undefined && 操作 !== 'append') {
|
|
379
|
+
结果.surfaceOp = { op: 'replace', startSeq: 映射.get(操作.startSeq), endSeq: 映射.get(操作.endSeq) }
|
|
380
|
+
}
|
|
381
|
+
if (Array.isArray(事件.sourceEventSeqs)) {
|
|
382
|
+
结果.sourceEventSeqs = 事件.sourceEventSeqs.map((seq) => 映射.get(seq))
|
|
383
|
+
}
|
|
384
|
+
if (事件.data !== null && typeof 事件.data === 'object') {
|
|
385
|
+
if (Array.isArray(事件.data.shadowedSeqs)) {
|
|
386
|
+
结果.data = { ...事件.data, shadowedSeqs: 事件.data.shadowedSeqs.map((seq) => 映射.get(seq)) }
|
|
387
|
+
}
|
|
388
|
+
if (事件.data.shadowedRange !== undefined) {
|
|
389
|
+
const 区间 = 事件.data.shadowedRange
|
|
390
|
+
结果.data = { ...(结果.data ?? 事件.data), shadowedRange: { start: 映射.get(区间.start), end: 映射.get(区间.end) } }
|
|
391
|
+
}
|
|
392
|
+
if (事件.data.sourceEventSeq !== undefined) {
|
|
393
|
+
结果.data = { ...(结果.data ?? 事件.data), sourceEventSeq: 映射.get(事件.data.sourceEventSeq) }
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return 结果
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* 把事件里的轮次号改为删除后连续的新编号。
|
|
401
|
+
* @param 事件 - 存活事件。
|
|
402
|
+
* @param 映射轮次 - 轮次号变换;未提供时原样返回。
|
|
403
|
+
*/
|
|
404
|
+
function 调整轮次(事件, 映射轮次) {
|
|
405
|
+
if (映射轮次 === undefined) return 事件
|
|
406
|
+
const 数据 = 事件.data
|
|
407
|
+
if (数据 === null || typeof 数据 !== 'object' || typeof 数据.turn !== 'number') return 事件
|
|
408
|
+
const 新轮次 = 映射轮次(数据.turn)
|
|
409
|
+
if (新轮次 === 数据.turn) return 事件
|
|
410
|
+
return { ...事件, data: { ...数据, turn: 新轮次 } }
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* 从一份磁盘日志里删掉已登记的轮次,并通过持久化层重建这份日志。
|
|
415
|
+
* @param 会话ID - 要重写的会话。
|
|
416
|
+
* @param 持久化 - `ctx.sessionPersistence`。
|
|
417
|
+
* @param 日志 - 读到的完整日志。
|
|
418
|
+
* @param 被删集合 - 要丢弃的 seq 集合。
|
|
419
|
+
* @param 映射轮次 - 删除后重排轮次号;省略表示不重排。
|
|
420
|
+
* @returns 重写后剩下的事件条数。
|
|
421
|
+
*/
|
|
422
|
+
async function 重写日志(会话ID, 持久化, 日志, 被删集合, 映射轮次) {
|
|
423
|
+
const 存活 = 日志.events
|
|
424
|
+
.filter((事件) => !被删集合.has(事件.seq))
|
|
425
|
+
.map((事件) => 调整轮次(事件, 映射轮次))
|
|
426
|
+
const 映射 = new Map()
|
|
427
|
+
存活.forEach((事件, 下标) => 映射.set(事件.seq, 下标))
|
|
428
|
+
const 批次 = 存活.map((事件) => 重编号(事件, 映射))
|
|
429
|
+
const 目录 = await 查找会话目录(会话ID)
|
|
430
|
+
const 备份 = 目录 === undefined ? undefined : `${目录}.session-editor-backup`
|
|
431
|
+
if (目录 !== undefined) {
|
|
432
|
+
await fs.rm(备份, { recursive: true, force: true })
|
|
433
|
+
await fs.rename(目录, 备份)
|
|
434
|
+
}
|
|
435
|
+
try {
|
|
436
|
+
const 句柄 = await 持久化.create(日志.header, { inheritedEventCount: 0 })
|
|
437
|
+
try {
|
|
438
|
+
if (批次.length > 0) await 句柄.append(批次)
|
|
439
|
+
await 句柄.flush()
|
|
440
|
+
} finally {
|
|
441
|
+
await 句柄.close()
|
|
442
|
+
}
|
|
443
|
+
} catch (错误) {
|
|
444
|
+
if (备份 !== undefined) {
|
|
445
|
+
await fs.rm(目录, { recursive: true, force: true })
|
|
446
|
+
await fs.rename(备份, 目录)
|
|
447
|
+
}
|
|
448
|
+
throw 错误
|
|
449
|
+
}
|
|
450
|
+
if (备份 !== undefined) await fs.rm(备份, { recursive: true, force: true })
|
|
451
|
+
return 批次.length
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* 把一个会话登记到目标工作区:先改会话头里的 cwd(工作区成员资格由它校验),
|
|
456
|
+
* 再从原工作区摘除、挂到目标工作区。会话事件本身一条都不动。
|
|
457
|
+
* @param ctx - Host 上下文。
|
|
458
|
+
* @param 会话ID - 要移动的会话。
|
|
459
|
+
* @param 工作区ID - 目标工作区。
|
|
460
|
+
*/
|
|
461
|
+
async function 移动会话到工作区(ctx, 会话ID, 工作区ID) {
|
|
462
|
+
const 注册表 = ctx.workspaceRegistry
|
|
463
|
+
if (注册表 === undefined) throw new Error('当前部署没有工作区注册表')
|
|
464
|
+
const 目标工作区 = 注册表.get(工作区ID)
|
|
465
|
+
if (目标工作区 === undefined) throw new Error('目标工作区不存在')
|
|
466
|
+
const 新路径 = 目标工作区.path
|
|
467
|
+
if (typeof 新路径 !== 'string' || 新路径 === '') throw new Error('目标工作区没有可用目录')
|
|
468
|
+
let 原工作区
|
|
469
|
+
for (const 工作区 of 注册表.list()) {
|
|
470
|
+
if ((工作区.sessionIds ?? []).includes(会话ID)) {
|
|
471
|
+
原工作区 = 工作区
|
|
472
|
+
break
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
const 日志 = await 读取日志(ctx, 会话ID)
|
|
476
|
+
if (日志.header?.isSeeded === true) throw new Error('fork 出来的会话暂不支持移动')
|
|
477
|
+
if (日志.header?.cwd !== 新路径) {
|
|
478
|
+
const 新头 = { ...日志.header, cwd: 新路径 }
|
|
479
|
+
await 重写日志(会话ID, ctx.sessionPersistence, { ...日志, header: 新头 }, new Set())
|
|
480
|
+
}
|
|
481
|
+
if (原工作区 !== undefined && 原工作区.id !== 工作区ID) await 原工作区.detachSession(会话ID)
|
|
482
|
+
await 目标工作区.attachSession(会话ID)
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** 把一个会话从它所属的工作区账本里摘除,让它立刻从侧栏消失。 */
|
|
486
|
+
async function 摘除登记(ctx, 会话ID) {
|
|
487
|
+
for (const 工作区 of ctx.workspaceRegistry.list()) {
|
|
488
|
+
if ((工作区.sessionIds ?? []).includes(会话ID)) {
|
|
489
|
+
await 工作区.detachSession(会话ID)
|
|
490
|
+
await 记日志(ctx, `已从工作区 ${工作区.id} 摘除 ${会话ID}`)
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** 会话安全可写的时刻,执行该会话排队中的全部操作。 */
|
|
496
|
+
async function 执行清理(ctx, 会话ID) {
|
|
497
|
+
if (正在处理.has(会话ID)) return
|
|
498
|
+
正在处理.add(会话ID)
|
|
499
|
+
try {
|
|
500
|
+
await 载入队列()
|
|
501
|
+
const 条目 = 队列[会话ID]
|
|
502
|
+
if (条目 === undefined) return
|
|
503
|
+
if (条目.全部删除 === true) {
|
|
504
|
+
// 侧栏列表来自工作区账本,所以先摘除登记:这一步不依赖会话是否还活着。
|
|
505
|
+
await 摘除登记(ctx, 会话ID)
|
|
506
|
+
// 磁盘删除不再等待会话从内存退出:Windows 的写租约是命名内核信号量,
|
|
507
|
+
// 不是文件句柄,目录可以直接删除;删不掉的残留继续留在队列里重试。
|
|
508
|
+
const 结果 = await 删除磁盘痕迹(ctx, 会话ID)
|
|
509
|
+
await 记日志(ctx, `删除会话 ${会话ID}:磁盘痕迹=${结果}`)
|
|
510
|
+
if (结果 === 'failed') return
|
|
511
|
+
await 注销任务(会话ID)
|
|
512
|
+
await 记日志(ctx, `删除完成 ${会话ID}`)
|
|
513
|
+
return
|
|
514
|
+
}
|
|
515
|
+
if (ctx.sessions.get(会话ID) !== undefined) {
|
|
516
|
+
await 记日志(ctx, `跳过 ${会话ID}:会话仍在内存中`)
|
|
517
|
+
return
|
|
518
|
+
}
|
|
519
|
+
if (typeof ctx.sessionPersistence?.create !== 'function') return
|
|
520
|
+
if (条目.移入工作区 !== undefined) {
|
|
521
|
+
await 移动会话到工作区(ctx, 会话ID, 条目.移入工作区)
|
|
522
|
+
await 注销任务(会话ID)
|
|
523
|
+
return
|
|
524
|
+
}
|
|
525
|
+
const 日志 = await 读取日志(ctx, 会话ID)
|
|
526
|
+
if (日志.header?.isSeeded === true) throw new Error('fork 出来的会话日志不做重写')
|
|
527
|
+
const 被删集合 = new Set()
|
|
528
|
+
const 被删轮次 = new Set()
|
|
529
|
+
for (const 轮次 of 条目.轮次 ?? []) {
|
|
530
|
+
const 这一轮 = 定位轮次(日志.events, 轮次)
|
|
531
|
+
if (这一轮 === undefined) continue
|
|
532
|
+
被删轮次.add(这一轮.轮次)
|
|
533
|
+
for (const 事件 of 日志.events) {
|
|
534
|
+
if (事件.seq >= 这一轮.startSeq && 事件.seq <= 这一轮.endSeq) 被删集合.add(事件.seq)
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
for (const 事件 of 日志.events) {
|
|
538
|
+
if (事件.type !== 'command/run' && 事件.type !== 'command/done') continue
|
|
539
|
+
if ((条目.命令ID ?? []).includes(事件.data?.commandId)) 被删集合.add(事件.seq)
|
|
540
|
+
}
|
|
541
|
+
// 被删轮次里出现过的消息标识(UUID)。inbox 插入事件按标识引用这些消息,
|
|
542
|
+
// 不按 seq,所以必须再按标识收敛一次,否则“删除”会残留待发消息。
|
|
543
|
+
const 被删消息 = new Set()
|
|
544
|
+
for (const 事件 of 日志.events) {
|
|
545
|
+
if (!被删集合.has(事件.seq)) continue
|
|
546
|
+
const 标识 = 事件.data?.message?.id ?? 事件.data?.id
|
|
547
|
+
if (typeof 标识 === 'string') 被删消息.add(标识)
|
|
548
|
+
}
|
|
549
|
+
for (const 轮次 of 条目.轮次 ?? []) {
|
|
550
|
+
const 这一轮 = 定位轮次(日志.events, 轮次)
|
|
551
|
+
if (这一轮 === undefined) continue
|
|
552
|
+
for (const 事件 of 日志.events) {
|
|
553
|
+
if (事件.seq < 这一轮.startSeq || 事件.seq > 这一轮.endSeq) continue
|
|
554
|
+
const 标识 = 事件.data?.message?.id ?? 事件.data?.id
|
|
555
|
+
if (typeof 标识 === 'string') 被删消息.add(标识)
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
// 固定点收敛:任何仍存活、却引用了被删区间或提及被删消息的事件(例如本插件
|
|
559
|
+
// 自己写入的替换记录、inbox 插入记录)也必须一起删,否则重建后的日志会留下
|
|
560
|
+
// 悬空引用或幽灵待发消息。
|
|
561
|
+
let 还有引用 = true
|
|
562
|
+
while (还有引用) {
|
|
563
|
+
还有引用 = false
|
|
564
|
+
for (const 事件 of 日志.events) {
|
|
565
|
+
if (被删集合.has(事件.seq)) continue
|
|
566
|
+
if (引用了被删事件(事件, 被删集合) || 含被删消息(事件, 被删消息)) {
|
|
567
|
+
被删集合.add(事件.seq)
|
|
568
|
+
还有引用 = true
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (被删集合.size === 0) {
|
|
573
|
+
await 注销任务(会话ID)
|
|
574
|
+
return
|
|
575
|
+
}
|
|
576
|
+
const 有引用 = 日志.events.some((事件) => !被删集合.has(事件.seq) && 引用了被删事件(事件, 被删集合))
|
|
577
|
+
if (有引用) throw new Error('仍有存活记录引用被删区间')
|
|
578
|
+
const 映射轮次 = 被删轮次.size === 0
|
|
579
|
+
? undefined
|
|
580
|
+
: (轮次) => 轮次 - [...被删轮次].filter((删) => 删 < 轮次).length
|
|
581
|
+
await 重写日志(会话ID, ctx.sessionPersistence, 日志, 被删集合, 映射轮次)
|
|
582
|
+
await 注销任务(会话ID)
|
|
583
|
+
} catch (错误) {
|
|
584
|
+
void 记日志(ctx, `清理失败 ${会话ID}:${错误?.message ?? 错误}`)
|
|
585
|
+
ctx.logger?.warn?.(`[session-editor] 会话 "${会话ID}" 的延迟清理失败:${错误?.message ?? 错误}`)
|
|
586
|
+
} finally {
|
|
587
|
+
正在处理.delete(会话ID)
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/** 触发一次清理,并保证任何异常都不会变成未处理的 rejection(那会让 Host 崩溃)。 */
|
|
592
|
+
function 安全清理(ctx, 会话ID) {
|
|
593
|
+
void 执行清理(ctx, 会话ID).catch((错误) => {
|
|
594
|
+
ctx.logger?.warn?.(`[session-editor] 清理异常:${错误?.message ?? 错误}`)
|
|
595
|
+
})
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** 把一次命令调用的结果追加到诊断日志,便于排查界面到宿主的通道问题。 */
|
|
599
|
+
async function 记日志(ctx, 行) {
|
|
600
|
+
try {
|
|
601
|
+
await fs.mkdir(状态目录, { recursive: true })
|
|
602
|
+
await fs.appendFile(path.join(状态目录, 'se.log'), `${new Date().toISOString()} ${行}\n`, 'utf8')
|
|
603
|
+
} catch {
|
|
604
|
+
ctx.logger?.warn?.('[session-editor] 无法写入诊断日志')
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function 解析载荷(rawInput) { const 文本 = String(rawInput ?? '').trim()
|
|
609
|
+
if (文本 === '') return undefined
|
|
610
|
+
try {
|
|
611
|
+
return JSON.parse(decodeURIComponent(文本))
|
|
612
|
+
} catch {
|
|
613
|
+
return undefined
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/** 注册传输命令与延迟清理的生命周期。 */
|
|
618
|
+
export function apply(ctx) {
|
|
619
|
+
// 每 10 秒扫一次队列:会话一旦从内存释放,排队中的删除/改写就能尽快落到磁盘。
|
|
620
|
+
const 扫描间隔毫秒 = 10000
|
|
621
|
+
void (async () => {
|
|
622
|
+
try {
|
|
623
|
+
await fs.mkdir(状态目录, { recursive: true })
|
|
624
|
+
await fs.writeFile(
|
|
625
|
+
path.join(状态目录, 'boot.json'),
|
|
626
|
+
JSON.stringify({ activatedAt: new Date().toISOString(), pid: process.pid, version: '0.1.0' }),
|
|
627
|
+
'utf8',
|
|
628
|
+
)
|
|
629
|
+
} catch {
|
|
630
|
+
// 状态目录只读时,仅失去存活标记。
|
|
631
|
+
}
|
|
632
|
+
})()
|
|
633
|
+
|
|
634
|
+
ctx.commands.register({
|
|
635
|
+
name: 'se',
|
|
636
|
+
description: '「对话编辑」界面按钮的内部传输命令。',
|
|
637
|
+
recordInput: false,
|
|
638
|
+
handler: async (调用) => {
|
|
639
|
+
const 载荷 = 解析载荷(调用.rawInput)
|
|
640
|
+
void 记日志(ctx, `收到 op=${载荷?.op ?? '(空)'} 会话=${调用.agent.session.id}`)
|
|
641
|
+
if (载荷 === undefined || typeof 载荷?.op !== 'string') {
|
|
642
|
+
void 记日志(ctx, '解析载荷失败')
|
|
643
|
+
return { kind: 'error', text: 'session-editor: 无法解析的请求' }
|
|
644
|
+
}
|
|
645
|
+
const 本会话ID = 调用.agent.session.id
|
|
646
|
+
try {
|
|
647
|
+
switch (载荷.op) {
|
|
648
|
+
case 'delete-round':
|
|
649
|
+
case 'edit-round': {
|
|
650
|
+
const 目标会话ID = String(载荷.sessionId ?? 本会话ID)
|
|
651
|
+
const 目标会话 = 目标会话ID === 本会话ID ? 调用.agent.session : ctx.sessions.get(目标会话ID)
|
|
652
|
+
if (目标会话 === undefined) {
|
|
653
|
+
return { kind: 'error', text: 'session-editor: 目标会话当前不在内存中,无法改写' }
|
|
654
|
+
}
|
|
655
|
+
const 文本 = 载荷.op === 'edit-round' ? String(载荷.text ?? '') : 已删除文案
|
|
656
|
+
const 这一轮 = await 替换轮次(
|
|
657
|
+
ctx, 目标会话, 目标会话ID,
|
|
658
|
+
{ turn: 载荷.turn, seq: 载荷.seq, messageId: 载荷.messageId }, 文本, 调用.commandId,
|
|
659
|
+
)
|
|
660
|
+
return {
|
|
661
|
+
kind: 'success',
|
|
662
|
+
text: JSON.stringify({
|
|
663
|
+
turn: 这一轮.turn,
|
|
664
|
+
ids: 这一轮.被删消息ID,
|
|
665
|
+
times: 这一轮.被删事件时间,
|
|
666
|
+
prompt: 这一轮.promptText,
|
|
667
|
+
messageId: 这一轮.messageId,
|
|
668
|
+
sessionId: 目标会话ID,
|
|
669
|
+
}),
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
case 'delete-session': {
|
|
673
|
+
const 会话ID = String(载荷.sessionId)
|
|
674
|
+
// 先摘除登记并通知客户端:侧栏与会话列表立刻消失,不进入“未分组”。
|
|
675
|
+
await 摘除登记(ctx, 会话ID)
|
|
676
|
+
try {
|
|
677
|
+
if (typeof ctx.emit === 'function') ctx.emit('api-session/removed', 会话ID)
|
|
678
|
+
} catch {
|
|
679
|
+
// 事件桥不可用时,客户端的本地摘除仍会执行。
|
|
680
|
+
}
|
|
681
|
+
// 排队作为失败回退;随后立刻尝试真的删磁盘,不等会话关闭。
|
|
682
|
+
await 登记任务(会话ID, { 全部删除: true, 命令ID: 调用.commandId })
|
|
683
|
+
await 执行清理(ctx, 会话ID)
|
|
684
|
+
const 目录 = await 查找会话目录(会话ID)
|
|
685
|
+
return {
|
|
686
|
+
kind: 'success',
|
|
687
|
+
text: 目录 === undefined
|
|
688
|
+
? '会话已删除。'
|
|
689
|
+
: '会话已从界面移除;磁盘目录暂时被占用,稍后自动重试。',
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
case 'deleted-times': {
|
|
693
|
+
if (载荷.sessionId !== 本会话ID) return { kind: 'error', text: 'session-editor: 会话不匹配' }
|
|
694
|
+
// 补齐历史删除:旧版只记了消息标识,没有时间戳;从替换事件反推
|
|
695
|
+
// 被遮蔽区间的全部事件时间,客户端据此把遗留的工具/过程行也隐藏。
|
|
696
|
+
const { events } = await 读取日志(ctx, 本会话ID)
|
|
697
|
+
const 时间集合 = new Set()
|
|
698
|
+
// 被遮蔽区间里助手回答的消息 id → 该轮提问原文,供界面预填编辑面板。
|
|
699
|
+
const 原文补齐 = []
|
|
700
|
+
for (const 事件 of events) {
|
|
701
|
+
if (事件.type !== 'user/message') continue
|
|
702
|
+
if (!事件.surfaceOp || 事件.surfaceOp.op !== 'replace') continue
|
|
703
|
+
if (!String(事件.data?.id ?? '').startsWith('session-editor-')) continue
|
|
704
|
+
const 起 = 事件.surfaceOp.startSeq
|
|
705
|
+
const 止 = 事件.surfaceOp.endSeq
|
|
706
|
+
let 提问原文
|
|
707
|
+
let 助手标识
|
|
708
|
+
for (const 候选 of events) {
|
|
709
|
+
if (候选.seq < 起 || 候选.seq > 止) continue
|
|
710
|
+
if (typeof 候选.time === 'number') 时间集合.add(候选.time)
|
|
711
|
+
if (提问原文 === undefined && 候选.type === 'user/message') 提问原文 = 取出文本(候选.data)
|
|
712
|
+
if (助手标识 === undefined && 候选.type === 'assistant/message'
|
|
713
|
+
&& typeof 候选.data?.message?.id === 'string') {
|
|
714
|
+
助手标识 = 候选.data.message.id
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
if (助手标识 !== undefined && 提问原文) 原文补齐.push({ id: 助手标识, prompt: 提问原文 })
|
|
718
|
+
}
|
|
719
|
+
return { kind: 'success', text: JSON.stringify({ times: [...时间集合], prompts: 原文补齐 }) }
|
|
720
|
+
}
|
|
721
|
+
case 'list-workspaces': {
|
|
722
|
+
const 注册表 = ctx.workspaceRegistry
|
|
723
|
+
const 列表 = 注册表 === undefined
|
|
724
|
+
? []
|
|
725
|
+
: 注册表.list().map((工作区) => ({
|
|
726
|
+
id: String(工作区.id),
|
|
727
|
+
title: 工作区.title,
|
|
728
|
+
path: 工作区.path,
|
|
729
|
+
sessionIds: [...(工作区.sessionIds ?? [])].map((标识) => String(标识)),
|
|
730
|
+
}))
|
|
731
|
+
return { kind: 'success', text: JSON.stringify(列表) }
|
|
732
|
+
}
|
|
733
|
+
case 'move-session': {
|
|
734
|
+
await 登记任务(String(载荷.sessionId), { 移入工作区: String(载荷.workspaceId), 命令ID: 调用.commandId })
|
|
735
|
+
安全清理(ctx, String(载荷.sessionId))
|
|
736
|
+
return { kind: 'success', text: '已登记移动;该会话不再处于打开状态后即完成迁移。' }
|
|
737
|
+
}
|
|
738
|
+
case 'settle': {
|
|
739
|
+
安全清理(ctx, String(载荷.sessionId))
|
|
740
|
+
return { kind: 'success', text: 'ok' }
|
|
741
|
+
}
|
|
742
|
+
default:
|
|
743
|
+
return { kind: 'error', text: `session-editor: 未知操作 "${载荷.op}"` }
|
|
744
|
+
}
|
|
745
|
+
} catch (错误) {
|
|
746
|
+
void 记日志(ctx, `失败 op=${载荷?.op} 原因=${错误?.message ?? 错误}`)
|
|
747
|
+
return { kind: 'error', text: `session-editor: ${错误?.message ?? 错误}` }
|
|
748
|
+
}
|
|
749
|
+
},
|
|
750
|
+
})
|
|
751
|
+
|
|
752
|
+
ctx.on('session/disposed', (会话) => {
|
|
753
|
+
// 通知发出时存储条目可能仍可见,稍后再看一次。
|
|
754
|
+
setTimeout(() => {
|
|
755
|
+
安全清理(ctx, 会话.id)
|
|
756
|
+
}, 800)
|
|
757
|
+
})
|
|
758
|
+
|
|
759
|
+
const 扫一遍 = async () => {
|
|
760
|
+
try {
|
|
761
|
+
await 载入队列()
|
|
762
|
+
const 待办 = Object.keys(队列)
|
|
763
|
+
if (待办.length > 0) await 记日志(ctx, `扫描队列:${待办.join(',')}`)
|
|
764
|
+
for (const 会话ID of 待办) await 执行清理(ctx, 会话ID)
|
|
765
|
+
} catch (错误) {
|
|
766
|
+
ctx.logger?.warn?.(`[session-editor] 队列扫描失败:${错误?.message ?? 错误}`)
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
ctx.effect(() => {
|
|
771
|
+
const 计时器 = setInterval(() => {
|
|
772
|
+
void 扫一遍()
|
|
773
|
+
}, 扫描间隔毫秒)
|
|
774
|
+
return () => clearInterval(计时器)
|
|
775
|
+
})
|
|
776
|
+
|
|
777
|
+
// Host 重启会把排队的工作留在磁盘上;此刻会话还没被加载,是最干净的窗口。
|
|
778
|
+
void 扫一遍()
|
|
779
|
+
setTimeout(() => {
|
|
780
|
+
void 扫一遍()
|
|
781
|
+
}, 2000)
|
|
782
|
+
}
|