dsh-context-manager 0.0.0-stage → 1.6.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/CHANGELOG.md +56 -0
- package/LICENSE +21 -0
- package/README.md +227 -3
- package/client.js +1150 -0
- package/cordis.patch.yml +3 -0
- package/core.js +470 -0
- package/docs/DETAIL.md +6 -0
- package/docs/DETAIL.zh.md +6 -0
- package/docs/preview.svg +56 -0
- package/icon.svg +19 -0
- package/index.js +862 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +70 -6
package/cordis.patch.yml
ADDED
package/core.js
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers shared by the Host half of the message manager.
|
|
3
|
+
*
|
|
4
|
+
* The session surface is the model-visible projection of the append-only log.
|
|
5
|
+
* Deleting one message means appending a surface replacement whose derived
|
|
6
|
+
* message is empty, so later model requests no longer see the shadowed node
|
|
7
|
+
* while the raw log and the Chat transcript stay intact.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Surface event types that participate in the model-visible surface. */
|
|
11
|
+
export const SURFACE_TYPES = new Set([
|
|
12
|
+
'system/message',
|
|
13
|
+
'developer/message',
|
|
14
|
+
'user/message',
|
|
15
|
+
'assistant/message',
|
|
16
|
+
'tool/result',
|
|
17
|
+
])
|
|
18
|
+
|
|
19
|
+
/** Message carried by one surface event, or undefined for non-surface events. */
|
|
20
|
+
export function messageOf(event) {
|
|
21
|
+
if (event === undefined || event === null) return undefined
|
|
22
|
+
if (event.type === 'user/message') return event.data
|
|
23
|
+
return event.data?.message
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Stable role label used by the Host listing. */
|
|
27
|
+
export function roleOf(event) {
|
|
28
|
+
switch (event.type) {
|
|
29
|
+
case 'user/message':
|
|
30
|
+
return event.data?.source?.kind === 'user' ? 'user' : 'context'
|
|
31
|
+
case 'assistant/message':
|
|
32
|
+
return 'assistant'
|
|
33
|
+
case 'tool/result':
|
|
34
|
+
return 'tool'
|
|
35
|
+
case 'developer/message':
|
|
36
|
+
return 'context'
|
|
37
|
+
default:
|
|
38
|
+
return 'system'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Tool-call ids requested by one assistant message. */
|
|
43
|
+
export function toolCallIdsOf(event) {
|
|
44
|
+
if (event.type !== 'assistant/message') return []
|
|
45
|
+
const content = event.data?.message?.content
|
|
46
|
+
if (!Array.isArray(content)) return []
|
|
47
|
+
const ids = []
|
|
48
|
+
for (const block of content) {
|
|
49
|
+
if (block?.type === 'tool-call' && typeof block.id === 'string') ids.push(block.id)
|
|
50
|
+
}
|
|
51
|
+
return ids
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Whether one message event carries a tool-addition/removal block. */
|
|
55
|
+
export function hasToolChange(event) {
|
|
56
|
+
const content = event?.data?.message?.content
|
|
57
|
+
return Array.isArray(content)
|
|
58
|
+
&& content.some((block) => block?.type === 'tool-addition' || block?.type === 'tool-removal')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Collapse one message's content blocks into a one-line preview. */
|
|
62
|
+
export function previewOf(message, limit = 160) {
|
|
63
|
+
const content = Array.isArray(message?.content) ? message.content : []
|
|
64
|
+
const parts = []
|
|
65
|
+
for (const block of content) {
|
|
66
|
+
if (block === null || typeof block !== 'object') continue
|
|
67
|
+
switch (block.type) {
|
|
68
|
+
case 'text':
|
|
69
|
+
if (typeof block.text === 'string') parts.push(block.text)
|
|
70
|
+
break
|
|
71
|
+
case 'tool-call':
|
|
72
|
+
parts.push(`tool:${typeof block.name === 'string' ? block.name : '?'}`)
|
|
73
|
+
break
|
|
74
|
+
case 'image':
|
|
75
|
+
parts.push('[image]')
|
|
76
|
+
break
|
|
77
|
+
case 'file':
|
|
78
|
+
parts.push(`[file${block.attachment?.name ? `: ${block.attachment.name}` : ''}]`)
|
|
79
|
+
break
|
|
80
|
+
case 'tool-addition':
|
|
81
|
+
parts.push(`+${String(block.toolName)}`)
|
|
82
|
+
break
|
|
83
|
+
case 'tool-removal':
|
|
84
|
+
parts.push(`-${String(block.toolName)}`)
|
|
85
|
+
break
|
|
86
|
+
case 'reasoning':
|
|
87
|
+
break
|
|
88
|
+
default:
|
|
89
|
+
break
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const text = parts.join(' ').replace(/\s+/g, ' ').trim()
|
|
93
|
+
if (text.length === 0) return ''
|
|
94
|
+
return text.length > limit ? `${text.slice(0, limit - 1)}…` : text
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Fold raw log events into the current surface sequence list. */
|
|
98
|
+
export function foldSurface(events) {
|
|
99
|
+
const nodes = []
|
|
100
|
+
const bySeq = new Map()
|
|
101
|
+
let uncertain = false
|
|
102
|
+
for (const event of events) {
|
|
103
|
+
if (event === null || typeof event !== 'object') continue
|
|
104
|
+
bySeq.set(event.seq, event)
|
|
105
|
+
if (!SURFACE_TYPES.has(event.type)) continue
|
|
106
|
+
const op = event.surfaceOp
|
|
107
|
+
if (op === undefined) continue
|
|
108
|
+
if (op === 'append') {
|
|
109
|
+
nodes.push(event.seq)
|
|
110
|
+
continue
|
|
111
|
+
}
|
|
112
|
+
if (op !== null && typeof op === 'object' && op.op === 'replace') {
|
|
113
|
+
const start = nodes.indexOf(op.startSeq)
|
|
114
|
+
const end = nodes.indexOf(op.endSeq)
|
|
115
|
+
if (start >= 0 && end >= start) nodes.splice(start, end - start + 1, event.seq)
|
|
116
|
+
else uncertain = true
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { nodes, bySeq, uncertain }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Whether a surface event should appear in the management list. */
|
|
123
|
+
function visibleSurfaceEvent(event) {
|
|
124
|
+
if (event.type === 'system/message') return false
|
|
125
|
+
const message = messageOf(event)
|
|
126
|
+
if (message === undefined) return false
|
|
127
|
+
const content = message.content
|
|
128
|
+
if ((event.type === 'developer/message' || event.type === 'assistant/message')
|
|
129
|
+
&& (!Array.isArray(content) || content.length === 0)) return false
|
|
130
|
+
return true
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Project current surface events into display items, oldest first.
|
|
135
|
+
* `events` is either a complete surface snapshot or the folded output of
|
|
136
|
+
* {@link foldSurface} resolved back to events.
|
|
137
|
+
*/
|
|
138
|
+
export function itemsFromSurface(events) {
|
|
139
|
+
const items = []
|
|
140
|
+
let currentTurn = 0
|
|
141
|
+
let currentStep = 0
|
|
142
|
+
for (let index = 0; index < events.length; index += 1) {
|
|
143
|
+
const event = events[index]
|
|
144
|
+
if (typeof event?.data?.turn === 'number' && Number.isSafeInteger(event.data.turn)) currentTurn = event.data.turn
|
|
145
|
+
if (typeof event?.data?.step === 'number' && Number.isSafeInteger(event.data.step)) currentStep = event.data.step
|
|
146
|
+
if (!visibleSurfaceEvent(event)) continue
|
|
147
|
+
const message = messageOf(event)
|
|
148
|
+
const content = Array.isArray(message.content) ? message.content : []
|
|
149
|
+
const callIds = toolCallIdsOf(event)
|
|
150
|
+
let turn = Number.isSafeInteger(event.data?.turn) ? event.data.turn : undefined
|
|
151
|
+
let step = Number.isSafeInteger(event.data?.step) ? event.data.step : undefined
|
|
152
|
+
if (turn === undefined || step === undefined) {
|
|
153
|
+
for (let next = index + 1; next < events.length; next += 1) {
|
|
154
|
+
const data = events[next]?.data
|
|
155
|
+
if (Number.isSafeInteger(data?.turn) && Number.isSafeInteger(data?.step)) {
|
|
156
|
+
turn = data.turn
|
|
157
|
+
step = data.step
|
|
158
|
+
break
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
items.push({
|
|
163
|
+
seq: event.seq,
|
|
164
|
+
time: event.time,
|
|
165
|
+
type: event.type,
|
|
166
|
+
role: roleOf(event),
|
|
167
|
+
turn: turn ?? currentTurn,
|
|
168
|
+
step: step ?? currentStep,
|
|
169
|
+
messageId: typeof message.id === 'string' ? message.id : undefined,
|
|
170
|
+
preview: previewOf(message),
|
|
171
|
+
callIds,
|
|
172
|
+
toolCallId: event.type === 'tool/result' && typeof message.toolCallId === 'string' ? message.toolCallId : undefined,
|
|
173
|
+
blocks: content.length,
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
return items
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Boundary turn/step for a tombstone: prefer the deleted nodes, then neighbors. */
|
|
180
|
+
export function boundaryFor(events, indices) {
|
|
181
|
+
const valid = (data) => Number.isSafeInteger(data?.turn) && Number.isSafeInteger(data?.step)
|
|
182
|
+
for (const index of indices) {
|
|
183
|
+
const data = events[index]?.data
|
|
184
|
+
if (valid(data)) return { turn: data.turn, step: data.step }
|
|
185
|
+
}
|
|
186
|
+
const first = Math.min(...indices)
|
|
187
|
+
for (let index = first + 1; index < events.length; index += 1) {
|
|
188
|
+
const data = events[index]?.data
|
|
189
|
+
if (valid(data)) return { turn: data.turn, step: data.step }
|
|
190
|
+
}
|
|
191
|
+
for (let index = first - 1; index >= 0; index -= 1) {
|
|
192
|
+
const data = events[index]?.data
|
|
193
|
+
if (valid(data)) return { turn: data.turn, step: data.step }
|
|
194
|
+
}
|
|
195
|
+
return { turn: 0, step: 0 }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Plan one message deletion over a complete current surface.
|
|
200
|
+
*
|
|
201
|
+
* Deleting a tool result also removes its owning assistant message and every
|
|
202
|
+
* sibling result of that message, and deleting an assistant message removes its
|
|
203
|
+
* tool results, so the derived history never keeps a tool call without an answer
|
|
204
|
+
* (or the reverse). The returned runs are contiguous surface ranges, in surface
|
|
205
|
+
* order, each replaceable by a single tombstone event.
|
|
206
|
+
*/
|
|
207
|
+
export function planDeletion(events, targetSeq) {
|
|
208
|
+
const targetIndex = events.findIndex((event) => event.seq === targetSeq)
|
|
209
|
+
if (targetIndex < 0) return { ok: false, reason: 'not-found' }
|
|
210
|
+
const target = events[targetIndex]
|
|
211
|
+
if (target.type === 'system/message') return { ok: false, reason: 'system' }
|
|
212
|
+
if (target.type === 'tool/result' && typeof target.data?.message?.toolCallId !== 'string') {
|
|
213
|
+
return { ok: false, reason: 'unsupported' }
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const chosen = new Set([targetIndex])
|
|
217
|
+
const addAssistantGroup = (index) => {
|
|
218
|
+
chosen.add(index)
|
|
219
|
+
const callIds = new Set(toolCallIdsOf(events[index]))
|
|
220
|
+
if (callIds.size === 0) return
|
|
221
|
+
for (let i = 0; i < events.length; i += 1) {
|
|
222
|
+
const event = events[i]
|
|
223
|
+
if (event.type !== 'tool/result') continue
|
|
224
|
+
if (callIds.has(event.data?.message?.toolCallId)) chosen.add(i)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (target.type === 'assistant/message') {
|
|
229
|
+
addAssistantGroup(targetIndex)
|
|
230
|
+
} else if (target.type === 'tool/result') {
|
|
231
|
+
const callId = target.data.message.toolCallId
|
|
232
|
+
let ownerFound = false
|
|
233
|
+
for (let i = 0; i < events.length; i += 1) {
|
|
234
|
+
const event = events[i]
|
|
235
|
+
if (event.type !== 'assistant/message') continue
|
|
236
|
+
if (!toolCallIdsOf(event).includes(callId)) continue
|
|
237
|
+
ownerFound = true
|
|
238
|
+
addAssistantGroup(i)
|
|
239
|
+
}
|
|
240
|
+
if (!ownerFound) chosen.add(targetIndex)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const indices = [...chosen].sort((a, b) => a - b)
|
|
244
|
+
const runs = []
|
|
245
|
+
let run = null
|
|
246
|
+
for (const index of indices) {
|
|
247
|
+
if (run !== null && index === run.endIndex + 1) {
|
|
248
|
+
run.endIndex = index
|
|
249
|
+
run.seqs.push(events[index].seq)
|
|
250
|
+
continue
|
|
251
|
+
}
|
|
252
|
+
run = { startIndex: index, endIndex: index, seqs: [events[index].seq] }
|
|
253
|
+
runs.push(run)
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
ok: true,
|
|
257
|
+
target,
|
|
258
|
+
indices,
|
|
259
|
+
runs: runs.map((entry) => ({
|
|
260
|
+
startSeq: entry.seqs[0],
|
|
261
|
+
endSeq: entry.seqs[entry.seqs.length - 1],
|
|
262
|
+
seqs: entry.seqs,
|
|
263
|
+
})),
|
|
264
|
+
count: indices.length,
|
|
265
|
+
targetSeq,
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Source kind this plugin writes for its invisible tombstones. */
|
|
270
|
+
export const TOMBSTONE_KIND = 'context-manager'
|
|
271
|
+
/** Source kinds older releases wrote; their tombstones stay restorable. */
|
|
272
|
+
export const LEGACY_TOMBSTONE_KINDS = Object.freeze(['message-manager'])
|
|
273
|
+
|
|
274
|
+
/** Whether one message source kind belongs to this plugin, current or legacy. */
|
|
275
|
+
export function isTombstoneKind(kind) {
|
|
276
|
+
return kind === TOMBSTONE_KIND || LEGACY_TOMBSTONE_KINDS.includes(kind)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Durable payload of an invisible replacement node. */
|
|
280
|
+
export function tombstoneData(turn, step, id) {
|
|
281
|
+
return {
|
|
282
|
+
turn,
|
|
283
|
+
step,
|
|
284
|
+
message: {
|
|
285
|
+
id,
|
|
286
|
+
role: 'developer',
|
|
287
|
+
content: [],
|
|
288
|
+
source: { kind: TOMBSTONE_KIND },
|
|
289
|
+
},
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Parse a `restored-assistant-<tombstoneSeq>-…` marker id back to its tombstone seq. */
|
|
294
|
+
export function parseRestoredTombstoneId(id) {
|
|
295
|
+
if (typeof id !== 'string' || !id.startsWith('restored-assistant-')) return undefined
|
|
296
|
+
const rest = id.slice('restored-assistant-'.length)
|
|
297
|
+
const dash = rest.indexOf('-')
|
|
298
|
+
if (dash <= 0) return undefined
|
|
299
|
+
const seq = Number(rest.slice(0, dash))
|
|
300
|
+
return Number.isSafeInteger(seq) ? seq : undefined
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Classify the deleted records currently reachable from the surface.
|
|
305
|
+
*
|
|
306
|
+
* Mirrors the restore planner's capability boundaries without throwing: a
|
|
307
|
+
* record is restorable when the same content would pass {@code planRestore}.
|
|
308
|
+
* @param events - current surface events, oldest first (tombstones included).
|
|
309
|
+
* @param bySeq - full-log index used to recover replaced originals.
|
|
310
|
+
* @returns deleted-record descriptors in surface order.
|
|
311
|
+
*/
|
|
312
|
+
export function deletedFromSurface(events, bySeq) {
|
|
313
|
+
const restored = new Set()
|
|
314
|
+
for (const event of events) {
|
|
315
|
+
if (event === undefined || event === null || event.type !== 'assistant/message') continue
|
|
316
|
+
const seq = parseRestoredTombstoneId(event.data?.message?.id)
|
|
317
|
+
if (seq !== undefined) restored.add(seq)
|
|
318
|
+
}
|
|
319
|
+
const records = []
|
|
320
|
+
for (const event of events) {
|
|
321
|
+
if (event === undefined || event === null || event.type !== 'developer/message') continue
|
|
322
|
+
if (!isTombstoneKind(event.data?.message?.source?.kind)) continue
|
|
323
|
+
const op = event.surfaceOp
|
|
324
|
+
if (op === null || typeof op !== 'object' || op.op !== 'replace') continue
|
|
325
|
+
if (restored.has(event.seq)) continue
|
|
326
|
+
const targets = Array.isArray(event.sourceEventSeqs) ? event.sourceEventSeqs : []
|
|
327
|
+
const originals = targets.map((seq) => bySeq.get(seq))
|
|
328
|
+
const known = originals.filter((original) => original !== undefined)
|
|
329
|
+
const missing = targets.length - known.length
|
|
330
|
+
const assistant = known.find((original) => original.type === 'assistant/message')
|
|
331
|
+
const calls = assistant === undefined ? [] : toolCallIdsOf(assistant)
|
|
332
|
+
const results = known.filter((original) => original.type === 'tool/result')
|
|
333
|
+
let restorable = false
|
|
334
|
+
let reason = ''
|
|
335
|
+
if (targets.length === 1) {
|
|
336
|
+
const original = known[0]
|
|
337
|
+
if (original === undefined) restorable = true
|
|
338
|
+
else if (original.type === 'user/message') restorable = true
|
|
339
|
+
else if (original.type === 'developer/message' && !hasToolChange(original)) restorable = true
|
|
340
|
+
else if (original.type === 'assistant/message' && calls.length === 0) {
|
|
341
|
+
restorable = original.data?.message?.content?.length > 0
|
|
342
|
+
reason = 'appended'
|
|
343
|
+
} else if (original.type === 'tool/result') reason = 'tool'
|
|
344
|
+
else reason = 'group'
|
|
345
|
+
} else if (assistant !== undefined && calls.length > 0 && missing === 0
|
|
346
|
+
&& results.length === targets.length - 1) {
|
|
347
|
+
const callIds = new Set(calls)
|
|
348
|
+
const resultIds = new Set(results.map((original) => original.data?.message?.toolCallId))
|
|
349
|
+
restorable = callIds.size === resultIds.size && [...callIds].every((id) => resultIds.has(id))
|
|
350
|
+
reason = restorable ? 'appended' : 'group'
|
|
351
|
+
} else {
|
|
352
|
+
reason = missing > 0 ? 'missing' : 'group'
|
|
353
|
+
}
|
|
354
|
+
records.push({
|
|
355
|
+
tombstoneSeq: event.seq,
|
|
356
|
+
targetSeqs: [...targets],
|
|
357
|
+
count: targets.length,
|
|
358
|
+
items: known.map((original) => ({
|
|
359
|
+
seq: original.seq,
|
|
360
|
+
role: roleOf(original),
|
|
361
|
+
preview: previewOf(original.type === 'user/message' ? original.data : original.data?.message),
|
|
362
|
+
})),
|
|
363
|
+
restorable,
|
|
364
|
+
reason,
|
|
365
|
+
})
|
|
366
|
+
}
|
|
367
|
+
return records
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** Human-readable Host listing for a command or tool result. */
|
|
371
|
+
export function formatList(items, deleted = [], pending = [], limit = 30) {
|
|
372
|
+
const lines = []
|
|
373
|
+
if (items.length === 0) {
|
|
374
|
+
lines.push('当前模型上下文中没有可管理的消息。')
|
|
375
|
+
} else {
|
|
376
|
+
const shown = items.length > limit ? items.slice(-limit) : items
|
|
377
|
+
const omitted = items.length - shown.length
|
|
378
|
+
lines.push(`当前模型上下文中共 ${items.length} 条可管理消息${omitted > 0 ? `,仅显示最近 ${shown.length} 条` : ''}:`)
|
|
379
|
+
for (const item of shown) {
|
|
380
|
+
lines.push(`#${item.seq} [${item.role}] ${item.preview.length > 0 ? item.preview : '(空消息)'}`)
|
|
381
|
+
}
|
|
382
|
+
if (omitted > 0) lines.push(`(更早的 ${omitted} 条请在标题栏的“上下文管理”中查看。)`)
|
|
383
|
+
}
|
|
384
|
+
if (pending.length > 0) {
|
|
385
|
+
lines.push('', `排队中(下一回合生效,共 ${pending.length} 项):`)
|
|
386
|
+
for (const entry of pending) {
|
|
387
|
+
lines.push(`#${entry.recordSeq} ${entry.op} #${entry.targetSeq}`)
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (deleted.length > 0) {
|
|
391
|
+
lines.push('', `已删除 ${deleted.length} 条记录(restore 使用 #后的删除记录序号):`)
|
|
392
|
+
for (const record of deleted.slice(-20)) {
|
|
393
|
+
const first = record.items[0]
|
|
394
|
+
const summary = record.items.length === 1 && first !== undefined
|
|
395
|
+
? `${first.role} · ${first.preview.length > 0 ? first.preview : '(空消息)'}`
|
|
396
|
+
: `删除组 · ${record.count} 条`
|
|
397
|
+
const note = record.restorable
|
|
398
|
+
? record.reason === 'appended' ? '恢复到末尾' : '恢复到原位置'
|
|
399
|
+
: '不可恢复'
|
|
400
|
+
lines.push(`#${record.tombstoneSeq} ← ${summary}(${note})`)
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
lines.push('', '删除:/ctxmgr delete <序号,...>')
|
|
404
|
+
lines.push('整轮:/ctxmgr deleteturn <轮号,...>(list-turns 查看轮号)')
|
|
405
|
+
lines.push('恢复:/ctxmgr restore <删除记录序号,...>(也可在标题栏的上下文管理弹窗中操作)')
|
|
406
|
+
lines.push('删除只会从后续模型请求的上下文中移除该消息,原始会话日志与聊天记录仍会保留。')
|
|
407
|
+
return lines.join('\n')
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Whether one event is an empty context-manager tombstone, current or legacy kind. */
|
|
411
|
+
export function isTombstone(event) {
|
|
412
|
+
return event !== undefined
|
|
413
|
+
&& event !== null
|
|
414
|
+
&& event.type === 'developer/message'
|
|
415
|
+
&& Array.isArray(event.data?.message?.content)
|
|
416
|
+
&& event.data.message.content.length === 0
|
|
417
|
+
&& isTombstoneKind(event.data?.message?.source?.kind)
|
|
418
|
+
&& event.surfaceOp !== null
|
|
419
|
+
&& typeof event.surfaceOp === 'object'
|
|
420
|
+
&& event.surfaceOp.op === 'replace'
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Group the surface's content nodes into conversation turns, oldest first.
|
|
425
|
+
*
|
|
426
|
+
* A node's turn comes from its own `data.turn`; a tombstone is invisible here
|
|
427
|
+
* (it derives no message), so a fully deleted turn disappears from the list.
|
|
428
|
+
* Injected context (runtime snapshots and reminders) carries the turn it was
|
|
429
|
+
* injected under and therefore joins that turn.
|
|
430
|
+
* @param events - current surface events, oldest first.
|
|
431
|
+
* @param bySeq - full-log index, kept for signature symmetry with the other
|
|
432
|
+
* surface projections.
|
|
433
|
+
* @returns turn descriptors: { turn, seqs, messages, preview }.
|
|
434
|
+
*/
|
|
435
|
+
export function turnsFromSurface(events, bySeq = new Map()) {
|
|
436
|
+
const groups = new Map()
|
|
437
|
+
for (const event of events) {
|
|
438
|
+
if (event === undefined || event === null) continue
|
|
439
|
+
if (!visibleSurfaceEvent(event)) continue
|
|
440
|
+
const turn = Number.isSafeInteger(event.data?.turn) ? event.data.turn : undefined
|
|
441
|
+
if (!Number.isSafeInteger(turn)) continue
|
|
442
|
+
const message = messageOf(event)
|
|
443
|
+
const role = roleOf(event)
|
|
444
|
+
const preview = previewOf(message)
|
|
445
|
+
const group = groups.get(turn) ?? { turn, seqs: [], messages: [] }
|
|
446
|
+
group.seqs.push(event.seq)
|
|
447
|
+
group.messages.push({ seq: event.seq, role, preview })
|
|
448
|
+
groups.set(turn, group)
|
|
449
|
+
}
|
|
450
|
+
for (const group of groups.values()) {
|
|
451
|
+
const own = group.messages.find((entry) => entry.role === 'user')
|
|
452
|
+
group.preview = (own ?? group.messages[0])?.preview ?? ''
|
|
453
|
+
}
|
|
454
|
+
return [...groups.values()].sort((a, b) => a.turn - b.turn)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** Human-readable turn listing for a command or tool result. */
|
|
458
|
+
export function formatTurns(turns, limit = 50) {
|
|
459
|
+
if (turns.length === 0) return '当前上下文中没有可删除的对话轮。'
|
|
460
|
+
const shown = turns.length > limit ? turns.slice(-limit) : turns
|
|
461
|
+
const omitted = turns.length - shown.length
|
|
462
|
+
const lines = [`当前上下文中共 ${turns.length} 轮对话${omitted > 0 ? `,仅显示最近 ${shown.length} 轮` : ''}:`]
|
|
463
|
+
for (const turn of shown) {
|
|
464
|
+
const mark = turn.inProgress ? '(进行中,删除将在下一回合生效)' : ''
|
|
465
|
+
lines.push(`T${turn.turn} · ${turn.preview.length > 0 ? turn.preview : '(无用户提问)'} · ${turn.messages.length} 条${mark}`)
|
|
466
|
+
}
|
|
467
|
+
lines.push('', '删除整轮:/ctxmgr deleteturn <轮号,...>')
|
|
468
|
+
lines.push('整轮删除会一并移除该轮的上下文注入;恢复仍按单条删除记录粒度进行。')
|
|
469
|
+
return lines.join('\n')
|
|
470
|
+
}
|
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
- Roll back to the start of a turn: `/ctxmgr turns` to read the turn numbers, `/ctxmgr deleteturn 5` to remove that whole turn from the context, then ask again.
|
|
4
|
+
- Drop noise: `/ctxmgr list` to find the indexes, `/ctxmgr delete 12,13,14` to clear error logs and trial-and-error instructions entry by entry.
|
|
5
|
+
- Forget superseded conclusions: delete the turns that hold plans which were later overturned, so the model stops citing them.
|
|
6
|
+
- Honest limit: this only removes content from the model context; the raw session log and chat transcript stay in the local session file, and this plugin is not an eraser.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
- 回退到某一轮的开始:`/ctxmgr turns` 查看轮号,`/ctxmgr deleteturn 5` 把该轮整体移出上下文,然后重新提问。
|
|
4
|
+
- 清除废物信息:`/ctxmgr list` 找到序号,`/ctxmgr delete 12,13,14` 按条清掉报错日志与试错指令。
|
|
5
|
+
- 让模型忘掉过时的中间结论:删掉被推翻的方案所在的轮次,避免它反复引用旧结论。
|
|
6
|
+
- 止损边界:只移出模型上下文,原始会话日志与聊天记录仍留在本地会话文件里,本插件不是擦除工具。
|
package/docs/preview.svg
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="640" height="300" viewBox="0 0 640 300" role="img" aria-label="A conversation turn selected in the manager dialog and removed from the model context">
|
|
2
|
+
<g fill="none" stroke-linecap="round" stroke-linejoin="round">
|
|
3
|
+
<g stroke="#8b94a7" stroke-width="1.5">
|
|
4
|
+
<rect x="40" y="82" width="190" height="52" rx="11" fill="#8b94a7" fill-opacity="0.08"/>
|
|
5
|
+
<rect x="40" y="214" width="190" height="52" rx="11" fill="#8b94a7" fill-opacity="0.08"/>
|
|
6
|
+
</g>
|
|
7
|
+
<g stroke="#8b94a7" stroke-width="1.5">
|
|
8
|
+
<circle cx="62" cy="108" r="9" fill="#8b94a7" fill-opacity="0.16"/>
|
|
9
|
+
<rect x="82" y="98" width="112" height="8" rx="4" fill="#8b94a7" fill-opacity="0.22" stroke="none"/>
|
|
10
|
+
<rect x="82" y="114" width="74" height="8" rx="4" fill="#8b94a7" fill-opacity="0.16" stroke="none"/>
|
|
11
|
+
<circle cx="62" cy="240" r="9" fill="#8b94a7" fill-opacity="0.16"/>
|
|
12
|
+
<rect x="82" y="230" width="96" height="8" rx="4" fill="#8b94a7" fill-opacity="0.22" stroke="none"/>
|
|
13
|
+
<rect x="82" y="246" width="58" height="8" rx="4" fill="#8b94a7" fill-opacity="0.16" stroke="none"/>
|
|
14
|
+
</g>
|
|
15
|
+
<g stroke="#4c8dff" stroke-width="1.8">
|
|
16
|
+
<rect x="40" y="148" width="190" height="52" rx="11" fill="#4c8dff" fill-opacity="0.14"/>
|
|
17
|
+
<circle cx="62" cy="174" r="9" fill="#4c8dff" fill-opacity="0.2"/>
|
|
18
|
+
<rect x="82" y="164" width="104" height="8" rx="4" fill="#4c8dff" fill-opacity="0.55" stroke="none"/>
|
|
19
|
+
<rect x="82" y="180" width="66" height="8" rx="4" fill="#4c8dff" fill-opacity="0.35" stroke="none"/>
|
|
20
|
+
<polyline points="196,170 202,177 214,161" stroke-width="2.4"/>
|
|
21
|
+
</g>
|
|
22
|
+
<g stroke="#8b94a7" stroke-width="1.8">
|
|
23
|
+
<path d="M236 174 H286"/>
|
|
24
|
+
<polyline points="278,166 288,174 278,182"/>
|
|
25
|
+
</g>
|
|
26
|
+
<g stroke="#8b94a7" stroke-width="1.5">
|
|
27
|
+
<rect x="306" y="52" width="298" height="196" rx="14" fill="#8b94a7" fill-opacity="0.08"/>
|
|
28
|
+
<path d="M306 88 H604" stroke-opacity="0.8"/>
|
|
29
|
+
</g>
|
|
30
|
+
<g stroke="#4c8dff" stroke-width="2">
|
|
31
|
+
<polyline points="324,66 331,73 324,80"/>
|
|
32
|
+
</g>
|
|
33
|
+
<g stroke="#8b94a7" stroke-width="1.4">
|
|
34
|
+
<rect x="344" y="66" width="92" height="8" rx="4" fill="#8b94a7" fill-opacity="0.28" stroke="none"/>
|
|
35
|
+
<circle cx="566" cy="72" r="10" fill="#8b94a7" fill-opacity="0.12"/>
|
|
36
|
+
<path d="M562 68 H570 M563 72 H569 M564 70 V76 A2 2 0 0 1 562 78 H570 A2 2 0 0 1 568 76 V70"/>
|
|
37
|
+
</g>
|
|
38
|
+
<g stroke="#8b94a7" stroke-width="1.4">
|
|
39
|
+
<circle cx="336" cy="106" r="4" fill="#8b94a7" fill-opacity="0.4" stroke="none"/>
|
|
40
|
+
<rect x="352" y="101" width="150" height="8" rx="4" fill="#8b94a7" fill-opacity="0.2" stroke="none"/>
|
|
41
|
+
<rect x="352" y="116" width="96" height="7" rx="3.5" fill="#8b94a7" fill-opacity="0.13" stroke="none"/>
|
|
42
|
+
<circle cx="336" cy="140" r="4" fill="#8b94a7" fill-opacity="0.4" stroke="none"/>
|
|
43
|
+
<rect x="352" y="135" width="186" height="8" rx="4" fill="#8b94a7" fill-opacity="0.2" stroke="none"/>
|
|
44
|
+
<rect x="352" y="150" width="122" height="7" rx="3.5" fill="#8b94a7" fill-opacity="0.13" stroke="none"/>
|
|
45
|
+
<circle cx="336" cy="174" r="4" fill="#8b94a7" fill-opacity="0.4" stroke="none"/>
|
|
46
|
+
<rect x="352" y="169" width="132" height="8" rx="4" fill="#8b94a7" fill-opacity="0.2" stroke="none"/>
|
|
47
|
+
</g>
|
|
48
|
+
<g stroke="#f0806b" stroke-width="1.6" stroke-dasharray="7 6">
|
|
49
|
+
<rect x="324" y="196" width="262" height="34" rx="9" fill="#f0806b" fill-opacity="0.08"/>
|
|
50
|
+
</g>
|
|
51
|
+
<g stroke="#f0806b" stroke-width="1.8" stroke-opacity="0.75">
|
|
52
|
+
<path d="M468 205 L494 221"/>
|
|
53
|
+
<path d="M494 205 L468 221"/>
|
|
54
|
+
</g>
|
|
55
|
+
</g>
|
|
56
|
+
</svg>
|
package/icon.svg
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Session Message Manager">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="dshmm-bg" x1="8" y1="6" x2="56" y2="58" gradientUnits="userSpaceOnUse">
|
|
4
|
+
<stop offset="0" stop-color="#3b82f6"/>
|
|
5
|
+
<stop offset="1" stop-color="#1d4ed8"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect x="2" y="2" width="60" height="60" rx="14" fill="url(#dshmm-bg)"/>
|
|
9
|
+
<g fill="#ffffff">
|
|
10
|
+
<rect x="13" y="16" width="30" height="5" rx="2.5" opacity="0.96"/>
|
|
11
|
+
<rect x="13" y="26" width="24" height="5" rx="2.5" opacity="0.9"/>
|
|
12
|
+
<rect x="13" y="36" width="17" height="5" rx="2.5" opacity="0.82"/>
|
|
13
|
+
</g>
|
|
14
|
+
<g transform="translate(36 34)">
|
|
15
|
+
<rect x="0" y="2" width="19" height="20" rx="4.5" fill="#ef4444" stroke="#ffffff" stroke-width="1.6"/>
|
|
16
|
+
<path d="M5.5 2V0.8h8V2" fill="none" stroke="#ffffff" stroke-width="2" stroke-linecap="round"/>
|
|
17
|
+
<path d="M6.2 7v9.5M9.5 7v9.5M12.8 7v9.5" fill="none" stroke="#ffffff" stroke-width="1.8" stroke-linecap="round" opacity="0.92"/>
|
|
18
|
+
</g>
|
|
19
|
+
</svg>
|