dsh-as-aistudio 0.2.13 → 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/CHANGELOG.md +31 -0
- package/README.md +5 -5
- package/package.json +17 -12
- package/src/client.js +3362 -225
- package/src/components.js +1 -1
- package/src/index.js +26 -1
- package/src/shell.js +30 -5
- package/src/vendor/host/_platform.js +132 -0
- package/src/vendor/host/dsh-delete-turn.js +661 -0
- package/src/vendor/host/dsh-delete-turn.logic.js +655 -0
- package/src/vendor/host/dsh-edit-turn.js +2813 -0
- package/src/vendor/host/dsh-edit-turn.patch.js +285 -0
- package/src/vendor/host/dsh-markdown-bubble.js +37 -0
- package/src/vendor/host/dsh-rerun-turn.js +2784 -0
- package/src/vendor/versions.json +6 -0
- package/docs/INTEROP.md +0 -230
- package/src/studio.js +0 -553
- package/src/vendor/dsh-delete-turn.js +0 -1174
- package/src/vendor/dsh-edit-turn.js +0 -2626
- package/src/vendor/dsh-markdown-bubble.js +0 -872
- package/src/vendor/dsh-rerun-turn.js +0 -1014
- package/tools/build.mjs +0 -87
- package/tools/bump.mjs +0 -82
- package/tools/head.txt +0 -13
- package/tools/tail.txt +0 -5
- package/tools/tail2.txt +0 -4
- package/tools/vendor.mjs +0 -125
- package/tools/verify-manifest.mjs +0 -77
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
// dsh-delete-turn host half (dsh-delete-turn.logic.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
|
|
2
|
+
//
|
|
3
|
+
// Source: ../dsh-delete-turn/src/logic.js (v0.1.15, sha256 900c08468355aff3)
|
|
4
|
+
// Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
|
|
5
|
+
//
|
|
6
|
+
// src/shell.js imports the copy named after the component, which is why the
|
|
7
|
+
// studio installs with no runtime dependency on the component package.
|
|
8
|
+
// dsh-delete-turn - pure session-log logic.
|
|
9
|
+
//
|
|
10
|
+
// Everything here operates on the plain event JSON read back through the
|
|
11
|
+
// host's public sessionQuery service, so the module has no DSH SDK imports and
|
|
12
|
+
// runs unchanged under `node --test`. The two central pieces are the official
|
|
13
|
+
// surface fold (which nodes the model currently sees, and which earlier nodes a
|
|
14
|
+
// replacement shadowed) and the range planner that turns one UI target into a
|
|
15
|
+
// canonical, contiguous surface-replace range. A whole-turn delete is just the
|
|
16
|
+
// widest range: every live surface node of the addressed turn, replaced by one
|
|
17
|
+
// turn-less carrier - no replay, no renumbering, one append.
|
|
18
|
+
//
|
|
19
|
+
// The only import is the platform's own id generator: a replay copy must carry
|
|
20
|
+
// a FRESH message id, never the id of the message it copies.
|
|
21
|
+
|
|
22
|
+
import { randomUUID } from 'node:crypto'
|
|
23
|
+
|
|
24
|
+
/** Plugin id shared by the host and browser halves. */
|
|
25
|
+
export const PLUGIN_ID = 'dsh-delete-turn'
|
|
26
|
+
|
|
27
|
+
/** The four event types that may carry `surfaceOp` (official surface contract). */
|
|
28
|
+
const SURFACE_TYPES = new Set([
|
|
29
|
+
'system/message',
|
|
30
|
+
'user/message',
|
|
31
|
+
'assistant/message',
|
|
32
|
+
'tool/result',
|
|
33
|
+
])
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Whether one event participates in the model-visible surface.
|
|
37
|
+
* @param event - raw session event.
|
|
38
|
+
* @returns true for a message-producing event type.
|
|
39
|
+
*/
|
|
40
|
+
export function isSurfaceEvent(event) {
|
|
41
|
+
return SURFACE_TYPES.has(event.type)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Replay the surface operations of a complete log.
|
|
46
|
+
*
|
|
47
|
+
* Mirrors the official fold: `append` pushes the event onto the tail; a
|
|
48
|
+
* `replace` swaps the inclusive window between its two surface nodes for the
|
|
49
|
+
* replacing event. Replacements whose anchors are no longer present are
|
|
50
|
+
* skipped defensively (a corrupt log must not throw inside an HTTP handler).
|
|
51
|
+
*
|
|
52
|
+
* @param events - complete contiguous raw event log in seq order.
|
|
53
|
+
* @returns current surface seqs in model order plus every landed replacement
|
|
54
|
+
* with the exact seqs it shadowed.
|
|
55
|
+
*/
|
|
56
|
+
export function foldSurface(events) {
|
|
57
|
+
const nodes = []
|
|
58
|
+
const replacements = []
|
|
59
|
+
for (const event of events) {
|
|
60
|
+
const op = event.surfaceOp
|
|
61
|
+
if (op === undefined) continue
|
|
62
|
+
if (op === 'append') {
|
|
63
|
+
nodes.push(event.seq)
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
if (op === null || typeof op !== 'object' || op.op !== 'replace') continue
|
|
67
|
+
const startIdx = nodes.indexOf(op.startSeq)
|
|
68
|
+
const endIdx = nodes.indexOf(op.endSeq)
|
|
69
|
+
if (startIdx === -1 || endIdx === -1 || startIdx > endIdx) continue
|
|
70
|
+
const shadowed = nodes.slice(startIdx, endIdx + 1)
|
|
71
|
+
nodes.splice(startIdx, endIdx - startIdx + 1, event.seq)
|
|
72
|
+
replacements.push({ seq: event.seq, startSeq: op.startSeq, endSeq: op.endSeq, shadowed })
|
|
73
|
+
}
|
|
74
|
+
return { nodes, replacements }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Durable message identity of one surface event.
|
|
79
|
+
* @param event - raw session event.
|
|
80
|
+
* @returns the message id, or undefined for an event without one.
|
|
81
|
+
*/
|
|
82
|
+
export function messageIdOf(event) {
|
|
83
|
+
const data = event.data
|
|
84
|
+
if (!data || typeof data !== 'object') return undefined
|
|
85
|
+
if (event.type === 'user/message') return typeof data.id === 'string' ? data.id : undefined
|
|
86
|
+
if (event.type === 'assistant/message' || event.type === 'tool/result' || event.type === 'system/message') {
|
|
87
|
+
const message = data.message
|
|
88
|
+
return message && typeof message.id === 'string' ? message.id : undefined
|
|
89
|
+
}
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Whether one message source belongs to this plugin.
|
|
95
|
+
*
|
|
96
|
+
* DSH 0.1.7 migrated stored logs to session format v4, whose canonicalization
|
|
97
|
+
* flattens plugin sources: `{ kind: 'plugin', plugin: X }` becomes
|
|
98
|
+
* `{ kind: 'plugin:X' }`. Both shapes must be recognized, because a session
|
|
99
|
+
* keeps its on-disk v3 log until it is opened, and reads after that return the
|
|
100
|
+
* canonical v4 shape. Deletion carriers written as system messages cannot use a
|
|
101
|
+
* plugin kind at all — both replay validators require `system-prompt` — so they
|
|
102
|
+
* carry the plugin id in an extra `plugin` key instead.
|
|
103
|
+
* @param source - message source object from a log event.
|
|
104
|
+
* @returns true when the source names this plugin.
|
|
105
|
+
*/
|
|
106
|
+
export function sourceOwnsPlugin(source) {
|
|
107
|
+
if (!source || typeof source !== 'object') return false
|
|
108
|
+
if (source.kind === 'plugin' && source.plugin === PLUGIN_ID) return true
|
|
109
|
+
if (source.kind === 'system-prompt' && source.plugin === PLUGIN_ID) return true
|
|
110
|
+
return source.kind === `plugin:${PLUGIN_ID}`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Rebuild this plugin's deletion ledger from the log alone.
|
|
115
|
+
*
|
|
116
|
+
* Every deletion is one replacement event whose message source is
|
|
117
|
+
* `{ kind: 'plugin', plugin: 'dsh-delete-turn' }` (v3) or
|
|
118
|
+
* `{ kind: 'plugin:dsh-delete-turn' }` (v4). The mode is inferred from
|
|
119
|
+
* the shadowed window: one user message is a single-message delete, one step's
|
|
120
|
+
* assistant/tool nodes are a step delete, anything wider is a reply delete.
|
|
121
|
+
* A replacement landed by any other producer — compaction, for instance — is
|
|
122
|
+
* ignored.
|
|
123
|
+
*
|
|
124
|
+
* @param events - complete contiguous raw event log.
|
|
125
|
+
* @returns one entry per hidden seq: `{ seq, mode, replacement }`.
|
|
126
|
+
*/
|
|
127
|
+
export function hiddenEntries(events) {
|
|
128
|
+
return hiddenEntriesOfFold(foldSurface(events), events)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Rebuild the deletion ledger from an existing fold result.
|
|
133
|
+
* @param folded - result of {@link foldSurface}.
|
|
134
|
+
* @param events - the same log the fold was computed from.
|
|
135
|
+
* @returns one entry per hidden seq: `{ seq, mode, replacement }`.
|
|
136
|
+
*/
|
|
137
|
+
export function hiddenEntriesOfFold(folded, events) {
|
|
138
|
+
const bySeq = new Map(events.map((event) => [event.seq, event]))
|
|
139
|
+
const out = []
|
|
140
|
+
for (const replacement of folded.replacements) {
|
|
141
|
+
const event = bySeq.get(replacement.seq)
|
|
142
|
+
const data = event && event.data
|
|
143
|
+
const source = data && (data.source || (data.message && data.message.source))
|
|
144
|
+
if (!sourceOwnsPlugin(source)) continue
|
|
145
|
+
const mode = inferMode(bySeq, replacement.shadowed)
|
|
146
|
+
for (const seq of replacement.shadowed) out.push({ seq, mode, replacement: replacement.seq })
|
|
147
|
+
}
|
|
148
|
+
return out
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Turns whose reply window still holds deletable surface content.
|
|
153
|
+
*
|
|
154
|
+
* The transcript keeps rows whose content left the model context through a
|
|
155
|
+
* compaction (that is the whole point of compaction), so the browser half must
|
|
156
|
+
* not offer a delete action on them. This mirrors the member scan of
|
|
157
|
+
* {@link planRange}'s reply mode without its cleanliness validation; the host
|
|
158
|
+
* remains authoritative when a delete actually lands.
|
|
159
|
+
*
|
|
160
|
+
* @param events - complete contiguous raw event log.
|
|
161
|
+
* @param surfaceNodes - current surface seqs in model order.
|
|
162
|
+
* @returns turn numbers with at least one non-prompt surface node after the
|
|
163
|
+
* turn's last human prompt.
|
|
164
|
+
*/
|
|
165
|
+
export function deletableReplyTurns(events, surfaceNodes) {
|
|
166
|
+
const bySeq = new Map(events.map((event) => [event.seq, event]))
|
|
167
|
+
const turnOf = turnIndex(events)
|
|
168
|
+
const lastHuman = new Map()
|
|
169
|
+
const members = new Map()
|
|
170
|
+
surfaceNodes.forEach((seq, index) => {
|
|
171
|
+
const event = bySeq.get(seq)
|
|
172
|
+
if (!event || event.type === 'system/message') return
|
|
173
|
+
const turn = event.data && typeof event.data.turn === 'number' ? event.data.turn : turnOf.get(seq)
|
|
174
|
+
if (typeof turn !== 'number') return
|
|
175
|
+
if (event.type === 'user/message' && event.data && event.data.source && event.data.source.kind === 'user') {
|
|
176
|
+
lastHuman.set(turn, index)
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
const list = members.get(turn) ?? []
|
|
180
|
+
list.push(index)
|
|
181
|
+
members.set(turn, list)
|
|
182
|
+
})
|
|
183
|
+
const out = []
|
|
184
|
+
for (const [turn, indexes] of members) {
|
|
185
|
+
const last = lastHuman.get(turn)
|
|
186
|
+
if (last === undefined || indexes.some((index) => index > last)) out.push(turn)
|
|
187
|
+
}
|
|
188
|
+
return out.sort((a, b) => a - b)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Classify one deletion from its shadowed window, so a reloaded client can
|
|
193
|
+
* tell a step deletion (the process row survives) from a reply deletion.
|
|
194
|
+
* @param bySeq - seq -> event lookup of the same log.
|
|
195
|
+
* @param shadowed - shadowed surface seqs in model order.
|
|
196
|
+
* @returns `message`, `step` or `reply`.
|
|
197
|
+
*/
|
|
198
|
+
export function inferMode(bySeq, shadowed) {
|
|
199
|
+
const members = shadowed.map((seq) => bySeq.get(seq)).filter((event) => event !== undefined)
|
|
200
|
+
if (members.length === 1 && members[0].type === 'user/message') return 'message'
|
|
201
|
+
if (members.length > 0 && members.every((event) => event.type === 'assistant/message' || event.type === 'tool/result')) {
|
|
202
|
+
const turn = members[0].data && members[0].data.turn
|
|
203
|
+
const step = members[0].data && members[0].data.step
|
|
204
|
+
if (members.every((event) => event.data && event.data.turn === turn && event.data.step === step)) return 'step'
|
|
205
|
+
}
|
|
206
|
+
// A window that carries the turn's own human prompt - and is not the single
|
|
207
|
+
// message shape above - is a whole-turn delete.
|
|
208
|
+
if (members.some((event) => event.type === 'user/message' && event.data && event.data.source && event.data.source.kind === 'user')) return 'turn'
|
|
209
|
+
return 'reply'
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* In-place content rewrites landed by other producers.
|
|
214
|
+
*
|
|
215
|
+
* The official transcript keeps the original row of a shadowed event and draws
|
|
216
|
+
* the replacement content in its place (dsh-edit-turn does exactly this for an
|
|
217
|
+
* edited message), so the browser half needs `from -> to` pairs to keep that
|
|
218
|
+
* row's actions and to address the live node.
|
|
219
|
+
*
|
|
220
|
+
* Only a single-node replacement of the same event type counts as an in-place
|
|
221
|
+
* rewrite: it is the shape dsh-edit-turn lands when it rewrites one message in
|
|
222
|
+
* place, and it cannot impersonate the mixed rows of a rollback window, whose
|
|
223
|
+
* content the edit already superseded. This plugin's own deletion placeholders
|
|
224
|
+
* and the official compaction checkpoints rewrite context, not content, and
|
|
225
|
+
* are excluded — a compacted row must keep offering no delete action.
|
|
226
|
+
*
|
|
227
|
+
* @param folded - result of {@link foldSurface}.
|
|
228
|
+
* @param events - the same log the fold was computed from.
|
|
229
|
+
* @returns `[shadowedSeq, replacementSeq]` pairs, one per edited node.
|
|
230
|
+
*/
|
|
231
|
+
export function contentEditPairs(folded, events) {
|
|
232
|
+
const bySeq = new Map(events.map((event) => [event.seq, event]))
|
|
233
|
+
const out = []
|
|
234
|
+
for (const replacement of folded.replacements) {
|
|
235
|
+
if (replacement.shadowed.length !== 1) continue
|
|
236
|
+
const event = bySeq.get(replacement.seq)
|
|
237
|
+
if (!event) continue
|
|
238
|
+
const data = event.data
|
|
239
|
+
const source = data && (data.source || (data.message && data.message.source))
|
|
240
|
+
if (sourceOwnsPlugin(source)) continue
|
|
241
|
+
if (source && source.kind === 'compact-checkpoint') continue
|
|
242
|
+
const shadowed = bySeq.get(replacement.shadowed[0])
|
|
243
|
+
if (!shadowed || shadowed.type !== event.type) continue
|
|
244
|
+
out.push([replacement.shadowed[0], replacement.seq])
|
|
245
|
+
}
|
|
246
|
+
return out
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Map every event seq to the turn that encloses it.
|
|
251
|
+
*
|
|
252
|
+
* Turn brackets are the durable source for user messages (their payload has no
|
|
253
|
+
* turn field); assistant and tool events carry their own turn and override the
|
|
254
|
+
* bracket reading.
|
|
255
|
+
*
|
|
256
|
+
* @param events - complete contiguous raw event log.
|
|
257
|
+
* @returns seq -> turn number (undefined outside any turn).
|
|
258
|
+
*/
|
|
259
|
+
export function turnIndex(events) {
|
|
260
|
+
const turnOf = new Map()
|
|
261
|
+
let current
|
|
262
|
+
for (const event of events) {
|
|
263
|
+
if (event.type === 'turn/start') {
|
|
264
|
+
current = event.data && event.data.turn
|
|
265
|
+
turnOf.set(event.seq, current)
|
|
266
|
+
continue
|
|
267
|
+
}
|
|
268
|
+
if (event.type === 'turn/end') {
|
|
269
|
+
turnOf.set(event.seq, current)
|
|
270
|
+
current = undefined
|
|
271
|
+
continue
|
|
272
|
+
}
|
|
273
|
+
const data = event.data
|
|
274
|
+
const explicit =
|
|
275
|
+
(event.type === 'assistant/message' || event.type === 'tool/result' || event.type === 'tool/call') &&
|
|
276
|
+
data && typeof data.turn === 'number'
|
|
277
|
+
? data.turn
|
|
278
|
+
: undefined
|
|
279
|
+
turnOf.set(event.seq, explicit !== undefined ? explicit : current)
|
|
280
|
+
}
|
|
281
|
+
return turnOf
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** The turn still awaiting its `turn/end`, or null when every turn closed. */
|
|
285
|
+
export function openTurn(events) {
|
|
286
|
+
let open = null
|
|
287
|
+
for (const event of events) {
|
|
288
|
+
if (event.type === 'turn/start') open = event.data && event.data.turn
|
|
289
|
+
else if (event.type === 'turn/end' && (open === null || event.data.turn === open)) open = null
|
|
290
|
+
}
|
|
291
|
+
return open
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Whether an operation is in flight that will still write the surface. */
|
|
295
|
+
export function isBusy(events) {
|
|
296
|
+
if (openTurn(events) !== null) return true
|
|
297
|
+
let compaction = false
|
|
298
|
+
for (const event of events) {
|
|
299
|
+
if (event.type === 'compaction/start') compaction = true
|
|
300
|
+
else if (event.type === 'compaction/end') compaction = false
|
|
301
|
+
}
|
|
302
|
+
return compaction
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Planner rejection with a machine code the HTTP layer forwards verbatim. */
|
|
306
|
+
export class PlanError extends Error {
|
|
307
|
+
constructor(code, message) {
|
|
308
|
+
super(message)
|
|
309
|
+
this.name = 'PlanError'
|
|
310
|
+
this.code = code
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Whether one event is this plugin's own empty deletion placeholder.
|
|
316
|
+
* Placeholders are no-ops already removed from the context, so a later window
|
|
317
|
+
* may shadow them again; any other foreign node keeps its veto.
|
|
318
|
+
* @param event - raw session event.
|
|
319
|
+
* @returns true for a user/message produced by this plugin.
|
|
320
|
+
*/
|
|
321
|
+
export function isOwnPlaceholder(event) {
|
|
322
|
+
const data = event && event.data
|
|
323
|
+
const source = data && (data.source || (data.message && data.message.source))
|
|
324
|
+
return sourceOwnsPlugin(source)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* The nodes a planned window would swallow that belong to none of its members
|
|
329
|
+
* and carry real content - a foreign node (another plugin's injected row, say)
|
|
330
|
+
* that sits between the target's own nodes. Own placeholders and other plugins'
|
|
331
|
+
* silent carriers are NOT foreign: they carry nothing the model or the
|
|
332
|
+
* transcript can see, so retiring them costs nothing and must never make the
|
|
333
|
+
* user confirm. A non-empty list does not refuse the plan; the route requires an
|
|
334
|
+
* explicit `force` before it deletes these along with the target, and the client
|
|
335
|
+
* turns that into a forced confirmation.
|
|
336
|
+
* @param shadowed - the window's surface seqs, in order.
|
|
337
|
+
* @param members - the seqs that legitimately belong to the target.
|
|
338
|
+
* @param bySeq - seq -> event lookup.
|
|
339
|
+
* @returns the foreign seqs, in window order.
|
|
340
|
+
*/
|
|
341
|
+
function foreignNodes(shadowed, members, bySeq) {
|
|
342
|
+
const allowed = new Set(members)
|
|
343
|
+
const out = []
|
|
344
|
+
for (const seq of shadowed) {
|
|
345
|
+
if (allowed.has(seq)) continue
|
|
346
|
+
const event = bySeq.get(seq)
|
|
347
|
+
if (isOwnPlaceholder(event) || isSilentPluginCarrier(event)) continue
|
|
348
|
+
out.push(seq)
|
|
349
|
+
}
|
|
350
|
+
return out
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Turn one UI target into a canonical replacement range.
|
|
355
|
+
*
|
|
356
|
+
* Modes:
|
|
357
|
+
* - `message`: exactly the addressed user message (human prompt or injected
|
|
358
|
+
* context) — never a system prompt, never an assistant message;
|
|
359
|
+
* - `step`: the addressed step's assistant message plus every tool result
|
|
360
|
+
* that step produced, so a tool_use/tool_result pair never splits;
|
|
361
|
+
* - `reply`: every surface node of the addressed turn after its last human
|
|
362
|
+
* prompt (injected context, assistant steps, tool results), so the
|
|
363
|
+
* question survives while the whole answer attempt leaves the context;
|
|
364
|
+
* - `turn`: every surface node of the addressed turn - its prompt, injected
|
|
365
|
+
* context and whole reply - as one range (a whole-turn delete, no replay).
|
|
366
|
+
*
|
|
367
|
+
* The returned window is contiguous in surface order and contains no foreign
|
|
368
|
+
* node, so the landed replacement cannot shadow content the user did not aim
|
|
369
|
+
* at.
|
|
370
|
+
*
|
|
371
|
+
* @param events - complete contiguous raw event log.
|
|
372
|
+
* @param surfaceNodes - current surface seqs in model order.
|
|
373
|
+
* @param request - `{ mode, seq?, messageId?, turn? }`.
|
|
374
|
+
* @returns `{ mode, targetSeq, startSeq, endSeq, shadowed, turn, step }`.
|
|
375
|
+
* @throws {PlanError} with a stable code when the target cannot be planned.
|
|
376
|
+
*/
|
|
377
|
+
export function planRange(events, surfaceNodes, request) {
|
|
378
|
+
const mode = request && request.mode
|
|
379
|
+
const bySeq = new Map(events.map((event) => [event.seq, event]))
|
|
380
|
+
const nodeIndex = new Map(surfaceNodes.map((seq, index) => [seq, index]))
|
|
381
|
+
const turnOf = turnIndex(events)
|
|
382
|
+
|
|
383
|
+
const targetSeq = resolveTargetSeq(events, surfaceNodes, request, mode)
|
|
384
|
+
if (targetSeq === undefined) throw new PlanError('not-deletable', 'target not found')
|
|
385
|
+
if (!nodeIndex.has(targetSeq)) throw new PlanError('already-deleted', 'target is not on the current surface')
|
|
386
|
+
const target = bySeq.get(targetSeq)
|
|
387
|
+
if (!target) throw new PlanError('not-deletable', 'target event not found')
|
|
388
|
+
|
|
389
|
+
// Surface node 0 is the system prompt head: the official append contract only
|
|
390
|
+
// lets a system/message rewrite exactly that node, and no UI row targets it.
|
|
391
|
+
if (targetSeq === surfaceNodes[0]) throw new PlanError('not-deletable', 'the system prompt head cannot be deleted')
|
|
392
|
+
if (target.type === 'system/message') throw new PlanError('not-deletable', 'the system prompt cannot be deleted')
|
|
393
|
+
|
|
394
|
+
if (mode === 'message') {
|
|
395
|
+
if (target.type !== 'user/message') {
|
|
396
|
+
throw new PlanError('not-deletable', 'only user messages can be removed on their own')
|
|
397
|
+
}
|
|
398
|
+
const turn = turnOf.get(targetSeq)
|
|
399
|
+
return {
|
|
400
|
+
mode,
|
|
401
|
+
targetSeq,
|
|
402
|
+
startSeq: targetSeq,
|
|
403
|
+
endSeq: targetSeq,
|
|
404
|
+
shadowed: [targetSeq],
|
|
405
|
+
foreign: [],
|
|
406
|
+
turn: typeof turn === 'number' ? turn : 0,
|
|
407
|
+
step: 0,
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (mode === 'step') {
|
|
412
|
+
if (target.type !== 'assistant/message' && target.type !== 'tool/result') {
|
|
413
|
+
throw new PlanError('not-deletable', 'step deletion requires an assistant message or a tool result')
|
|
414
|
+
} const turn = target.data && target.data.turn
|
|
415
|
+
const step = target.data && target.data.step
|
|
416
|
+
if (typeof turn !== 'number' || typeof step !== 'number') {
|
|
417
|
+
throw new PlanError('not-deletable', 'step deletion requires a closed step')
|
|
418
|
+
}
|
|
419
|
+
const members = events
|
|
420
|
+
.filter(
|
|
421
|
+
(event) =>
|
|
422
|
+
(event.type === 'assistant/message' || event.type === 'tool/result') &&
|
|
423
|
+
event.data &&
|
|
424
|
+
event.data.turn === turn &&
|
|
425
|
+
event.data.step === step &&
|
|
426
|
+
nodeIndex.has(event.seq),
|
|
427
|
+
)
|
|
428
|
+
.map((event) => event.seq)
|
|
429
|
+
if (members.length === 0 || !members.includes(targetSeq)) {
|
|
430
|
+
throw new PlanError('already-deleted', 'this step is no longer on the surface')
|
|
431
|
+
}
|
|
432
|
+
const memberSet = new Set(members)
|
|
433
|
+
const indexes = members.map((seq) => nodeIndex.get(seq))
|
|
434
|
+
const startIdx = Math.min(...indexes)
|
|
435
|
+
const endIdx = Math.max(...indexes)
|
|
436
|
+
const shadowed = surfaceNodes.slice(startIdx, endIdx + 1)
|
|
437
|
+
const foreign = foreignNodes(shadowed, members, bySeq)
|
|
438
|
+
return {
|
|
439
|
+
mode,
|
|
440
|
+
targetSeq,
|
|
441
|
+
startSeq: shadowed[0],
|
|
442
|
+
endSeq: shadowed[shadowed.length - 1],
|
|
443
|
+
shadowed,
|
|
444
|
+
foreign,
|
|
445
|
+
turn,
|
|
446
|
+
step,
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (mode === 'reply') {
|
|
451
|
+
const turn = target.data && typeof target.data.turn === 'number' ? target.data.turn : turnOf.get(targetSeq)
|
|
452
|
+
if (typeof turn !== 'number') throw new PlanError('not-deletable', 'the target does not belong to a turn')
|
|
453
|
+
let lastHumanIdx = -1
|
|
454
|
+
for (const event of events) {
|
|
455
|
+
if (event.type !== 'user/message') continue
|
|
456
|
+
if (!event.data || !event.data.source || event.data.source.kind !== 'user') continue
|
|
457
|
+
if (turnOf.get(event.seq) !== turn) continue
|
|
458
|
+
const index = nodeIndex.get(event.seq)
|
|
459
|
+
if (index !== undefined && index > lastHumanIdx) lastHumanIdx = index
|
|
460
|
+
}
|
|
461
|
+
const members = []
|
|
462
|
+
for (const seq of surfaceNodes) {
|
|
463
|
+
const index = nodeIndex.get(seq)
|
|
464
|
+
if (index <= lastHumanIdx) continue
|
|
465
|
+
const event = bySeq.get(seq)
|
|
466
|
+
if (!event) continue
|
|
467
|
+
// The system prompt head is appended inside the first step, so its
|
|
468
|
+
// enclosing turn is that turn; it is never reply content and must never
|
|
469
|
+
// anchor a reply window.
|
|
470
|
+
if (event.type === 'system/message') continue
|
|
471
|
+
const eventTurn =
|
|
472
|
+
event.data && typeof event.data.turn === 'number' ? event.data.turn : turnOf.get(seq)
|
|
473
|
+
if (eventTurn !== turn) continue
|
|
474
|
+
members.push(seq)
|
|
475
|
+
}
|
|
476
|
+
if (members.length === 0) throw new PlanError('nothing-to-delete', 'the turn has no reply content left')
|
|
477
|
+
const memberSet = new Set(members)
|
|
478
|
+
const startIdx = nodeIndex.get(members[0])
|
|
479
|
+
const endIdx = nodeIndex.get(members[members.length - 1])
|
|
480
|
+
const shadowed = surfaceNodes.slice(startIdx, endIdx + 1)
|
|
481
|
+
const foreign = foreignNodes(shadowed, members, bySeq)
|
|
482
|
+
const closing = bySeq.get(members[members.length - 1])
|
|
483
|
+
const step = closing && closing.type === 'assistant/message' && typeof closing.data.step === 'number' ? closing.data.step : 0
|
|
484
|
+
return {
|
|
485
|
+
mode,
|
|
486
|
+
targetSeq,
|
|
487
|
+
startSeq: members[0],
|
|
488
|
+
endSeq: members[members.length - 1],
|
|
489
|
+
shadowed,
|
|
490
|
+
foreign,
|
|
491
|
+
turn,
|
|
492
|
+
step,
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// turn: every live surface node of the addressed turn - the human prompt, its
|
|
497
|
+
// injected context and its whole reply - in ONE range replace. The turn
|
|
498
|
+
// disappears from the model context and the later turns stay exactly where
|
|
499
|
+
// they are; nothing is replayed or renumbered (a plain range replace is all
|
|
500
|
+
// the official surface contract requires).
|
|
501
|
+
if (mode === 'turn') {
|
|
502
|
+
const turn = target.data && typeof target.data.turn === 'number' ? target.data.turn : turnOf.get(targetSeq)
|
|
503
|
+
if (typeof turn !== 'number') throw new PlanError('not-deletable', 'the target does not belong to a turn')
|
|
504
|
+
const members = []
|
|
505
|
+
for (const seq of surfaceNodes) {
|
|
506
|
+
const event = bySeq.get(seq)
|
|
507
|
+
// The system prompt head is never conversation content (reply mode makes
|
|
508
|
+
// the same exclusion), so it is never a member and never anchors the
|
|
509
|
+
// window: the head node itself stays exactly where it is. This is also
|
|
510
|
+
// why the turn that introduced the head is deletable - the head is not
|
|
511
|
+
// part of its window, and the host rewrites the head every turn anyway
|
|
512
|
+
// (its own `turn` field names the turn it was last rewritten in, not the
|
|
513
|
+
// turn it was created in).
|
|
514
|
+
if (!event || event.type === 'system/message') continue
|
|
515
|
+
const eventTurn = event.data && typeof event.data.turn === 'number' ? event.data.turn : turnOf.get(seq)
|
|
516
|
+
if (eventTurn !== turn) continue
|
|
517
|
+
members.push(seq)
|
|
518
|
+
}
|
|
519
|
+
if (members.length === 0) throw new PlanError('nothing-to-delete', 'the turn has no surface content left')
|
|
520
|
+
const startIdx = nodeIndex.get(members[0])
|
|
521
|
+
const endIdx = nodeIndex.get(members[members.length - 1])
|
|
522
|
+
// Surface node 0 is the protected system prompt head: a replacement covering
|
|
523
|
+
// it would have to be a system/message over exactly that node, which a
|
|
524
|
+
// turn-less user carrier is not. The head is normally a system/message and
|
|
525
|
+
// so is excluded from the members above; this only fires when it is not
|
|
526
|
+
// node 0 at all (a shadowed head, say), keeping the window off node 0.
|
|
527
|
+
if (startIdx === 0) throw new PlanError('not-deletable', 'the window would cover the system prompt head')
|
|
528
|
+
const shadowed = surfaceNodes.slice(startIdx, endIdx + 1)
|
|
529
|
+
const foreign = foreignNodes(shadowed, members, bySeq)
|
|
530
|
+
const closing = bySeq.get(members[members.length - 1])
|
|
531
|
+
const step = closing && closing.type === 'assistant/message' && typeof closing.data.step === 'number' ? closing.data.step : 0
|
|
532
|
+
return {
|
|
533
|
+
mode,
|
|
534
|
+
targetSeq,
|
|
535
|
+
startSeq: shadowed[0],
|
|
536
|
+
endSeq: shadowed[shadowed.length - 1],
|
|
537
|
+
shadowed,
|
|
538
|
+
foreign,
|
|
539
|
+
turn,
|
|
540
|
+
step,
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
throw new PlanError('not-deletable', `unsupported mode ${String(mode)}`)
|
|
545
|
+
}
|
|
546
|
+
// --- target resolution -------------------------------------------------------
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Resolve the event one request addresses, shared by every mode.
|
|
550
|
+
*
|
|
551
|
+
* `seq` is the durable address the browser half reads off the official
|
|
552
|
+
* `data-chat-flow-*` anchors; `messageId` is what the official
|
|
553
|
+
* assistant-actions slot hands over; `turn` is the fallback for rows that only
|
|
554
|
+
* carry a turn number (the process/disclosure row). A turn address binds to the
|
|
555
|
+
* LAST surface node of that turn, which is the node whose removal covers the
|
|
556
|
+
* whole attempt.
|
|
557
|
+
*
|
|
558
|
+
* @param events - complete contiguous raw event log.
|
|
559
|
+
* @param surfaceNodes - current surface seqs in model order.
|
|
560
|
+
* @param request - `{ seq?, messageId?, turn? }`.
|
|
561
|
+
* @param mode - calling mode; only the turn-addressed modes use `turn`.
|
|
562
|
+
* @returns the target seq, or undefined when nothing matches.
|
|
563
|
+
*/
|
|
564
|
+
function resolveTargetSeq(events, surfaceNodes, request, mode) {
|
|
565
|
+
let targetSeq = typeof request.seq === 'number' ? request.seq : undefined
|
|
566
|
+
if (targetSeq === undefined && typeof request.messageId === 'string' && request.messageId !== '') {
|
|
567
|
+
for (const event of events) {
|
|
568
|
+
if (messageIdOf(event) === request.messageId) {
|
|
569
|
+
targetSeq = event.seq
|
|
570
|
+
break
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if (targetSeq === undefined && (mode === 'reply' || mode === 'turn') && typeof request.turn === 'number') {
|
|
575
|
+
const bySeq = new Map(events.map((event) => [event.seq, event]))
|
|
576
|
+
const turnOf = turnIndex(events)
|
|
577
|
+
for (let index = surfaceNodes.length - 1; index >= 0; index -= 1) {
|
|
578
|
+
const seq = surfaceNodes[index]
|
|
579
|
+
const event = bySeq.get(seq)
|
|
580
|
+
if (!event) continue
|
|
581
|
+
if (turnOfSeq(bySeq, turnOf, seq) === request.turn) {
|
|
582
|
+
targetSeq = seq
|
|
583
|
+
break
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return targetSeq
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* The turn enclosing one surface node: the event's own coordinate when it has
|
|
592
|
+
* one, otherwise its turn bracket (user messages carry no turn field).
|
|
593
|
+
* @param bySeq - seq -> event lookup.
|
|
594
|
+
* @param turnOf - {@link turnIndex} result.
|
|
595
|
+
* @param seq - surface node seq.
|
|
596
|
+
* @returns the turn number, or undefined outside any turn.
|
|
597
|
+
*/
|
|
598
|
+
function turnOfSeq(bySeq, turnOf, seq) {
|
|
599
|
+
const event = bySeq.get(seq)
|
|
600
|
+
if (!event) return undefined
|
|
601
|
+
const data = event.data
|
|
602
|
+
if (data && typeof data.turn === 'number') return data.turn
|
|
603
|
+
return turnOf.get(seq)
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Whether one event is a plugin silent carrier rather than conversation
|
|
608
|
+
* content.
|
|
609
|
+
*
|
|
610
|
+
* The ecosystem convention (dsh-rerun-turn's README, shared by this plugin):
|
|
611
|
+
* a `user/message` whose source kind starts with `plugin:` and whose content is
|
|
612
|
+
* EMPTY is a bookkeeping carrier - every shipped adapter drops it before the
|
|
613
|
+
* request, so it carries no model-visible content. Such a node standing inside
|
|
614
|
+
* a window may be retired without a copy.
|
|
615
|
+
*
|
|
616
|
+
* @param event - raw session event.
|
|
617
|
+
* @returns true for a silent plugin carrier.
|
|
618
|
+
*/
|
|
619
|
+
export function isSilentPluginCarrier(event) {
|
|
620
|
+
if (!event || event.type !== 'user/message') return false
|
|
621
|
+
const data = event.data
|
|
622
|
+
if (!data || typeof data !== 'object') return false
|
|
623
|
+
const source = data.source
|
|
624
|
+
if (!source || typeof source.kind !== 'string' || !source.kind.startsWith('plugin:')) return false
|
|
625
|
+
return Array.isArray(data.content) && data.content.length === 0
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* The live human-prompt seqs a whole-turn delete would accept right now.
|
|
630
|
+
*
|
|
631
|
+
* This is the ADVERTISEMENT the state route publishes so a row only offers the
|
|
632
|
+
* action the host will actually accept: every candidate seq is put through the
|
|
633
|
+
* identical {@link planRange} the route plans with, so this list cannot drift
|
|
634
|
+
* from what a click does.
|
|
635
|
+
*
|
|
636
|
+
* @param events - complete contiguous raw event log.
|
|
637
|
+
* @param surfaceNodes - current surface seqs in model order.
|
|
638
|
+
* @returns the deletable prompt seqs, in surface order.
|
|
639
|
+
*/
|
|
640
|
+
export function turnableSeqs(events, surfaceNodes) {
|
|
641
|
+
const bySeq = new Map(events.map((event) => [event.seq, event]))
|
|
642
|
+
const out = []
|
|
643
|
+
for (const seq of surfaceNodes) {
|
|
644
|
+
const event = bySeq.get(seq)
|
|
645
|
+
if (event === undefined || event.type !== 'user/message') continue
|
|
646
|
+
if (!event.data || !event.data.source || event.data.source.kind !== 'user') continue
|
|
647
|
+
try {
|
|
648
|
+
planRange(events, surfaceNodes, { mode: 'turn', seq })
|
|
649
|
+
out.push(seq)
|
|
650
|
+
} catch {
|
|
651
|
+
// A prompt whose turn cannot be removed offers no entry.
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return out
|
|
655
|
+
}
|