dsh-edit-turn 0.2.20 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/patch.js ADDED
@@ -0,0 +1,260 @@
1
+ // In-place reply edits through a plugin-owned session record and a message
2
+ // projection.
3
+ //
4
+ // The host refuses to let an `assistant/message` be a surface-replacement
5
+ // carrier (it may never carry `sourceEventSeqs`), so a corrected reply cannot be
6
+ // put back at its own position with a surface event. The official seam for that
7
+ // case is a message projection: append a log-only plugin record, then register a
8
+ // pure projection for its type that rewrites the model-visible message at the
9
+ // target seq. Nothing leaves the surface, so there is nothing to replay and the
10
+ // log grows by one small record per edit.
11
+ //
12
+ // `appendPluginRecord` writes the record with the `ignorable` envelope the
13
+ // persistence gate requires (DSH >= 0.2.1-rc.2). The projection is registered
14
+ // through `ctx.sessions.registerMessageProjection` and is fiber-owned: unloading
15
+ // the plugin removes it, and the record is then skipped, so the original text
16
+ // comes back. That is the deliberate trade against the surface path: prompt
17
+ // edits are official events that outlive the plugin, patch edits are an overlay
18
+ // that needs its owner.
19
+
20
+ /** The plugin record type. The grammar requires the `plugin:` namespace. */
21
+ export const PATCH_TYPE = 'plugin:dsh-edit-turn/patch'
22
+
23
+ /** Patch payload version. */
24
+ export const PATCH_VERSION = 1
25
+
26
+ /** Block kinds this plugin can edit, mapped to the recorded block types. */
27
+ const KIND_TO_TYPES = {
28
+ text: ['text'],
29
+ reasoning: ['reasoning', 'thinking'],
30
+ }
31
+
32
+ /**
33
+ * The editable kind of one content block.
34
+ * @param block - one message content block.
35
+ * @returns 'text' | 'reasoning' | null.
36
+ */
37
+ export function kindOfBlock(block) {
38
+ if (!block || typeof block !== 'object') return null
39
+ if (block.type === 'text' && typeof block.text === 'string') return 'text'
40
+ if ((block.type === 'reasoning' || block.type === 'thinking') && typeof block.text === 'string') return 'reasoning'
41
+ return null
42
+ }
43
+
44
+ /**
45
+ * Every block of a message an editor may target, in message order.
46
+ * @param message - a message payload with a `content` array.
47
+ * @returns `[{ index, kind, text }]`.
48
+ */
49
+ export function editableBlocks(message) {
50
+ const content = message && Array.isArray(message.content) ? message.content : []
51
+ const out = []
52
+ for (let index = 0; index < content.length; index += 1) {
53
+ const block = content[index]
54
+ const kind = kindOfBlock(block)
55
+ if (kind === null) continue
56
+ out.push({ index, kind, text: block.text })
57
+ }
58
+ return out
59
+ }
60
+
61
+ /** A non-negative safe integer, matching the host's seq/blockIndex checks. */
62
+ function isIndex(value) {
63
+ return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 && !Object.is(value, -0)
64
+ }
65
+
66
+ /**
67
+ * Validate one patch payload (shape and target grammar).
68
+ * @param data - the record payload.
69
+ * @returns the same payload when valid.
70
+ * @throws when malformed.
71
+ */
72
+ export function validatePatchData(data) {
73
+ if (data === null || typeof data !== 'object' || Array.isArray(data)) {
74
+ throw new Error(`${PATCH_TYPE}: data must be an object`)
75
+ }
76
+ if (data.version !== PATCH_VERSION) {
77
+ throw new Error(`${PATCH_TYPE}: unsupported version ${String(data.version)}`)
78
+ }
79
+ if (!Array.isArray(data.targets) || data.targets.length === 0) {
80
+ throw new Error(`${PATCH_TYPE}: targets must be a nonempty array`)
81
+ }
82
+ for (const target of data.targets) {
83
+ if (target === null || typeof target !== 'object' || Array.isArray(target)) {
84
+ throw new Error(`${PATCH_TYPE}: each target must be an object`)
85
+ }
86
+ if (!isIndex(target.seq)) throw new Error(`${PATCH_TYPE}: target seq must be a non-negative safe integer`)
87
+ if (!isIndex(target.blockIndex)) throw new Error(`${PATCH_TYPE}: target blockIndex must be a non-negative safe integer`)
88
+ if (!(target.kind in KIND_TO_TYPES)) throw new Error(`${PATCH_TYPE}: target kind must be text or reasoning`)
89
+ if (typeof target.text !== 'string') throw new Error(`${PATCH_TYPE}: target text must be a string`)
90
+ }
91
+ return data
92
+ }
93
+
94
+ /**
95
+ * Build a validated patch payload.
96
+ * @param targets - `[{ seq, blockIndex, kind, text }]`.
97
+ * @returns the payload for `appendPluginRecord`.
98
+ */
99
+ export function buildPatchData(targets) {
100
+ const data = {
101
+ version: PATCH_VERSION,
102
+ targets: targets.map((target) => ({
103
+ seq: target.seq,
104
+ blockIndex: target.blockIndex,
105
+ kind: target.kind,
106
+ text: target.text,
107
+ })),
108
+ }
109
+ validatePatchData(data)
110
+ return data
111
+ }
112
+
113
+ /**
114
+ * Apply the targets that belong to one seq to one message.
115
+ *
116
+ * Targets are grouped by the caller: a record may carry several blocks of the
117
+ * same message, and they all have to land in one projected message. A target
118
+ * whose block is gone (or changed kind) is skipped; the rest still apply.
119
+ *
120
+ * @param message - the base message payload.
121
+ * @param targets - validated targets, all for this message.
122
+ * @returns a patched message copy.
123
+ */
124
+ export function applyTargetsToMessage(message, targets) {
125
+ const content = Array.isArray(message.content) ? message.content.slice() : []
126
+ for (const target of targets) {
127
+ const block = content[target.blockIndex]
128
+ const kind = kindOfBlock(block)
129
+ if (kind === null || kind !== target.kind) continue
130
+ const patched = { ...block, text: target.text }
131
+ if (kind === 'reasoning') {
132
+ // The signature signs the original text; keep it and some providers
133
+ // reject the whole request. The recorded block keeps its signature; the
134
+ // projected copy drops it, exactly like the disk path does.
135
+ delete patched.signature
136
+ delete patched.thinkingSignature
137
+ }
138
+ content[target.blockIndex] = patched
139
+ }
140
+ return { ...message, content }
141
+ }
142
+
143
+ /**
144
+ * The message projection for this plugin's record type.
145
+ *
146
+ * Pure and never throws: a projection error fails the whole session fold, which
147
+ * would make the session unreadable. A malformed record or a target that is no
148
+ * longer on the surface is simply skipped.
149
+ *
150
+ * Several records compose: the base for one target is the message the previous
151
+ * projections already produced (`context.messages`), falling back to the
152
+ * recorded message, so editing block A and later block B keeps both edits.
153
+ *
154
+ * @returns `{ type, project(event, context) }` for the host registry.
155
+ */
156
+ export function patchProjection() {
157
+ return {
158
+ type: PATCH_TYPE,
159
+ project(event, context) {
160
+ const out = new Map()
161
+ let data
162
+ try {
163
+ data = validatePatchData(event && event.data)
164
+ } catch {
165
+ return out
166
+ }
167
+ const nodes = new Set(Array.isArray(context && context.nodes) ? context.nodes : [])
168
+ const events = Array.isArray(context && context.events) ? context.events : []
169
+ const baseSeq = typeof (context && context.baseSeq) === 'number' ? context.baseSeq : 0
170
+ const messages = context && context.messages
171
+ const groups = new Map()
172
+ for (const target of data.targets) {
173
+ // A retired target is not in the model context any more: editing it
174
+ // would be a no-op for the conversation.
175
+ if (!nodes.has(target.seq)) continue
176
+ const group = groups.get(target.seq)
177
+ if (group === undefined) groups.set(target.seq, [target])
178
+ else group.push(target)
179
+ }
180
+ for (const [seq, targets] of groups) {
181
+ const source = events[seq - baseSeq]
182
+ if (!source || source.type !== 'assistant/message') continue
183
+ const previous = messages && typeof messages.get === 'function' ? messages.get(seq) : undefined
184
+ const base = previous && typeof previous === 'object' ? previous : source.data && source.data.message
185
+ if (!base || !Array.isArray(base.content)) continue
186
+ try {
187
+ out.set(seq, applyTargetsToMessage(base, targets))
188
+ } catch {
189
+ // Keep the rest of the record; a broken target must not take the
190
+ // session's whole fold down.
191
+ }
192
+ }
193
+ return out
194
+ },
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Read every landed patch from the log, latest per (seq, blockIndex).
200
+ * @param events - complete contiguous event log.
201
+ * @returns `Map<seq, Map<blockIndex, { kind, text }>>`.
202
+ */
203
+ export function patchesOf(events) {
204
+ const out = new Map()
205
+ for (const event of events) {
206
+ if (!event || event.type !== PATCH_TYPE) continue
207
+ let data
208
+ try {
209
+ data = validatePatchData(event.data)
210
+ } catch {
211
+ continue
212
+ }
213
+ for (const target of data.targets) {
214
+ let byBlock = out.get(target.seq)
215
+ if (byBlock === undefined) {
216
+ byBlock = new Map()
217
+ out.set(target.seq, byBlock)
218
+ }
219
+ byBlock.set(target.blockIndex, { kind: target.kind, text: target.text })
220
+ }
221
+ }
222
+ return out
223
+ }
224
+
225
+ /**
226
+ * The latest patch per block as a flat list for the browser half.
227
+ * @param patches - `patchesOf(events)`.
228
+ * @returns `[{ seq, blockIndex, kind, text }]`.
229
+ */
230
+ export function patchList(patches) {
231
+ const out = []
232
+ for (const [seq, byBlock] of patches) {
233
+ for (const [blockIndex, patch] of byBlock) {
234
+ out.push({ seq, blockIndex, kind: patch.kind, text: patch.text })
235
+ }
236
+ }
237
+ return out.sort((a, b) => a.seq - b.seq || a.blockIndex - b.blockIndex)
238
+ }
239
+
240
+ /**
241
+ * One message's blocks with the landed patch text applied.
242
+ * @param message - the recorded message payload.
243
+ * @param seq - the event seq that carries the message.
244
+ * @param patches - `patchesOf(events)`.
245
+ * @returns `[{ index, kind, text, original, patched }]`.
246
+ */
247
+ export function projectedBlocks(message, seq, patches) {
248
+ const byBlock = patches.get(seq)
249
+ return editableBlocks(message).map((block) => {
250
+ const patch = byBlock && byBlock.get(block.index)
251
+ const applied = patch !== undefined && patch.kind === block.kind ? patch.text : block.text
252
+ return {
253
+ index: block.index,
254
+ kind: block.kind,
255
+ text: applied,
256
+ original: block.text,
257
+ patched: applied !== block.text,
258
+ }
259
+ })
260
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-edit-turn",
3
- "version": "0.2.20",
4
- "description": "Edit any user turn in DeepSeek Harness and re-run it: an inline editor on the message row rolls the conversation back to just before that turn through the official surface-replace contract, then re-prompts the agent with the revised text. The append-only session log is never rewritten.",
3
+ "version": "0.3.0",
4
+ "description": "Edit any message in DeepSeek Harness in place: a prompt is replaced where it stands; a model reply block (answer text or reasoning) is patched through a plugin record and a message projection, so the turn stays where it is, later turns are untouched and the log grows by one small record. Falls back to the replay splice on hosts without the projection API. The append-only session log is never rewritten.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "exports": {
@@ -12,6 +12,7 @@
12
12
  "files": [
13
13
  "lib/index.js",
14
14
  "lib/client.js",
15
+ "lib/patch.js",
15
16
  "cordis.patch.yml"
16
17
  ],
17
18
  "keywords": [
@@ -21,8 +22,9 @@
21
22
  "edit",
22
23
  "message",
23
24
  "turn",
24
- "surface-replace",
25
- "rollback"
25
+ "projection",
26
+ "reasoning",
27
+ "surface-replace"
26
28
  ],
27
29
  "license": "MIT",
28
30
  "repository": {
@@ -46,11 +48,12 @@
46
48
  "@deepseek-ai/schemastery": "^3.18.1"
47
49
  },
48
50
  "devDependencies": {
49
- "@deepseek-ai/dsh-session": "^0.1.7-rc.2"
51
+ "@deepseek-ai/dsh-session": "^0.2.1-rc.2"
50
52
  },
51
53
  "peerDependencies": {
52
54
  "@deepseek-ai/dsh-settings": "^0.1.0-rc.7 || ^0.1.1-rc.2 || ^0.1.2-alpha.2 || ^0.1.3-alpha.2 || ^0.1.5-alpha.1 || ^0.2.0-rc.1",
53
- "@deepseek-ai/dsh-tools": "^0.1.0-rc.7 || ^0.1.1-rc.2 || ^0.1.2-alpha.2 || ^0.1.3-alpha.2 || ^0.1.5-alpha.1 || ^0.2.0-rc.1"
55
+ "@deepseek-ai/dsh-tools": "^0.1.0-rc.7 || ^0.1.1-rc.2 || ^0.1.2-alpha.2 || ^0.1.3-alpha.2 || ^0.1.5-alpha.1 || ^0.2.0-rc.1",
56
+ "@deepseek-ai/dsh-session": "^0.2.0-rc.1 || ^0.2.1-rc.2"
54
57
  },
55
58
  "dsh": {
56
59
  "bundle": {
@@ -70,7 +73,8 @@
70
73
  },
71
74
  "compatibility": {
72
75
  "dshReleases": {
73
- "0.1.6-alpha.2": "compatible"
76
+ "0.1.6-alpha.2": "compatible",
77
+ "0.2.1-rc.2": "compatible"
74
78
  }
75
79
  }
76
80
  }