dsh-remote-plugin 0.8.8 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +16 -5
- package/README.md +15 -4
- package/apk/dsh-remote.apk +0 -0
- package/gateway.cjs +527 -78
- package/index.mjs +1 -0
- package/package.json +8 -3
- package/public/activity.js +53 -4
- package/public/admin.html +34 -33
- package/public/admin.js +10 -2
- package/public/app.js +1204 -174
- package/public/bc-integration.css +81 -0
- package/public/build.json +2 -2
- package/public/composer-tools.css +184 -0
- package/public/composer-tools.js +888 -0
- package/public/desktop/desktop.html +92 -73
- package/public/desktop/desktop.js +631 -77
- package/public/file-inbox.js +77 -18
- package/public/genui.css +10 -0
- package/public/genui.js +22 -4
- package/public/host-queue.js +232 -0
- package/public/host-schedule.js +301 -0
- package/public/host-state.css +19 -0
- package/public/index.html +330 -140
- package/public/md.js +21 -1
- package/public/network.js +33 -1
- package/public/onboarding.js +3 -3
- package/public/operations.js +4 -3
- package/public/plan-review.css +297 -0
- package/public/plan-review.js +621 -0
- package/public/plugin.html +29 -28
- package/public/relay-autopair.js +25 -10
- package/public/relay-settings.css +11 -0
- package/public/relay-settings.js +350 -28
- package/public/session-management.css +61 -0
- package/public/session-management.js +742 -0
- package/public/session-search.css +137 -0
- package/public/session-search.js +977 -0
- package/public/skins/blue-fish/preview.html +3 -3
- package/public/skins/bocchi/preview.html +5 -5
- package/public/skins/kita/preview.html +6 -6
- package/public/skins/miku/SOURCES.json +10 -0
- package/public/skins/miku/chibi.png +0 -0
- package/public/skins/miku/preview.html +33 -0
- package/public/skins/miku/skin.css +82 -0
- package/public/skins/nijika/preview.html +6 -6
- package/public/skins/penguin/preview.html +3 -3
- package/public/skins/ryo/preview.html +6 -6
- package/public/slash-suggestions.js +15 -3
- package/public/sponsor-entry.js +11 -3
- package/public/styles.css +141 -21
- package/public/subagents.css +5 -0
- package/public/subagents.js +52 -0
- package/public/theme-vars.css +65 -1
- package/public/theme.js +4 -3
- package/public/turn-diff.css +323 -0
- package/public/turn-diff.js +778 -0
- package/public/update.json +8 -8
- package/public/version.json +1 -1
- package/public/workflow.css +305 -34
- package/public/workflow.js +141 -41
|
@@ -0,0 +1,778 @@
|
|
|
1
|
+
/* Optional per-turn deliverables, Host-recorded changed files and per-file diffs.
|
|
2
|
+
*
|
|
3
|
+
* The feature is off by default. While it is off this module renders no card and
|
|
4
|
+
* issues no request at all. When it is on, each card is built from durable Host
|
|
5
|
+
* events only:
|
|
6
|
+
* - `deliverables/presented` (the `present` tool: { turn, callId, files })
|
|
7
|
+
* - `workspace/changes` (the Host recorder appended one per top-level turn)
|
|
8
|
+
* The changed-file list and every comparison are read from the authenticated
|
|
9
|
+
* Host routes served by @deepseek-ai/dsh-client-ui-deliverables:
|
|
10
|
+
* GET /api/changes.summary?sessionId=&seq=
|
|
11
|
+
* GET /api/changes.diff?sessionId=&seq=&index=
|
|
12
|
+
* The comparison is the Host's turn-start/turn-end snapshot. This module never
|
|
13
|
+
* reads or substitutes the current file, and it reports `missing`, `binary` and
|
|
14
|
+
* `oversized` explicitly because the Host keeps these summaries only while the
|
|
15
|
+
* Session lives (a Host restart removes them). Original attachment delivery is
|
|
16
|
+
* untouched: deliveries are rendered as new cards and never replace or intercept
|
|
17
|
+
* the existing attachment/download UI.
|
|
18
|
+
*
|
|
19
|
+
* Adapter contract:
|
|
20
|
+
* state.sessionId() -> string|null
|
|
21
|
+
* state.entries(sessionId) -> Array<{ seq, event }>
|
|
22
|
+
* fetchJson(path, query) -> Promise<{ ok:boolean, status:number, data:any }>
|
|
23
|
+
* Authenticated GET through the Remote gateway (`apiUrl` + Bearer token).
|
|
24
|
+
* dom.root -> Element
|
|
25
|
+
* layout -> 'mobile' | 'desktop' | (() => 'mobile' | 'desktop')
|
|
26
|
+
* enabled? -> boolean // optional persisted switch seed; default false
|
|
27
|
+
* onOpenFile({ sessionId, turn, path, description }) -> void // optional
|
|
28
|
+
* notify(message, kind) -> void // optional
|
|
29
|
+
*/
|
|
30
|
+
'use strict'
|
|
31
|
+
;(function (root) {
|
|
32
|
+
const text = (zh, en) => (root.I18N?.lang === 'en' ? en : zh)
|
|
33
|
+
const esc = value => String(value ?? '').replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]))
|
|
34
|
+
const SUMMARY_PATH = '/api/changes.summary'
|
|
35
|
+
const DIFF_PATH = '/api/changes.diff'
|
|
36
|
+
const MAX_RENDER_LINES = 2000
|
|
37
|
+
const MAX_PATH = 400
|
|
38
|
+
|
|
39
|
+
const KIND_LABELS = {
|
|
40
|
+
added: () => text('新增', 'Added'),
|
|
41
|
+
deleted: () => text('删除', 'Deleted'),
|
|
42
|
+
modified: () => text('修改', 'Modified'),
|
|
43
|
+
binary: () => text('二进制', 'Binary'),
|
|
44
|
+
oversized: () => text('超大', 'Too large'),
|
|
45
|
+
unavailable: () => text('不可用', 'Unavailable'),
|
|
46
|
+
changed: () => text('有改动', 'Changed'),
|
|
47
|
+
}
|
|
48
|
+
function kindLabel(kind) { return (KIND_LABELS[kind] || KIND_LABELS.changed)() }
|
|
49
|
+
|
|
50
|
+
function numberOrNull(value) {
|
|
51
|
+
return Number.isSafeInteger(value) && value >= 0 ? value : null
|
|
52
|
+
}
|
|
53
|
+
function safeArray(value) { return Array.isArray(value) ? value : [] }
|
|
54
|
+
function cleanPath(value) { return typeof value === 'string' ? value.trim().slice(0, MAX_PATH) : '' }
|
|
55
|
+
function basename(value) {
|
|
56
|
+
const path = String(value || '')
|
|
57
|
+
const at = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'))
|
|
58
|
+
return at === -1 ? path : path.slice(at + 1)
|
|
59
|
+
}
|
|
60
|
+
function clip(value, max) {
|
|
61
|
+
const body = String(value ?? '')
|
|
62
|
+
return body.length > max ? body.slice(0, max) + '…' : body
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* ------------------------------------------------------------------ switch */
|
|
66
|
+
|
|
67
|
+
/** Only an explicit `true` enables the feature; everything else stays off. */
|
|
68
|
+
function normalizeEnabled(value) {
|
|
69
|
+
return value === true || value === 'true' || value === 1
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/* -------------------------------------------------------------- event fold */
|
|
73
|
+
|
|
74
|
+
function presentedFiles(data) {
|
|
75
|
+
if (!data || typeof data !== 'object') return null
|
|
76
|
+
if (!Number.isSafeInteger(data.turn) || data.turn < 1) return null
|
|
77
|
+
if (typeof data.callId !== 'string' || !data.callId) return null
|
|
78
|
+
if (!Array.isArray(data.files)) return null
|
|
79
|
+
const files = []
|
|
80
|
+
data.files.forEach((file, index) => {
|
|
81
|
+
if (!file || typeof file !== 'object') return
|
|
82
|
+
const path = cleanPath(file.path)
|
|
83
|
+
if (!path) return
|
|
84
|
+
files.push({
|
|
85
|
+
index,
|
|
86
|
+
path,
|
|
87
|
+
name: basename(path),
|
|
88
|
+
description: typeof file.description === 'string' ? clip(file.description, 400) : '',
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
return { turn: data.turn, callId: data.callId.slice(0, 200), files }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function changesOfEvent(event) {
|
|
95
|
+
if (!event || event.type !== 'workspace/changes') return null
|
|
96
|
+
const data = event.data || {}
|
|
97
|
+
if (!Number.isSafeInteger(data.turn) || data.turn < 1) return null
|
|
98
|
+
return { turn: data.turn }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Fold Session log entries into ordered per-turn cards. Only the last
|
|
103
|
+
* `workspace/changes` announcement of a turn is kept (it is the latest record);
|
|
104
|
+
* deliveries from every `present` call accumulate and de-duplicate by call.
|
|
105
|
+
*/
|
|
106
|
+
function turnsFromEntries(entries) {
|
|
107
|
+
const rows = safeArray(entries).filter(entry => entry && entry.event && typeof entry.event === 'object')
|
|
108
|
+
const sorted = rows.slice().sort((a, b) => (numberOrNull(a.seq) ?? 0) - (numberOrNull(b.seq) ?? 0))
|
|
109
|
+
const seen = new Set()
|
|
110
|
+
const byTurn = new Map()
|
|
111
|
+
const deliveryKeys = new Set()
|
|
112
|
+
for (const entry of sorted) {
|
|
113
|
+
if (Number.isSafeInteger(entry.seq)) {
|
|
114
|
+
if (seen.has(entry.seq)) continue
|
|
115
|
+
seen.add(entry.seq)
|
|
116
|
+
}
|
|
117
|
+
const event = entry.event
|
|
118
|
+
const seq = Number.isSafeInteger(entry.seq) ? entry.seq : null
|
|
119
|
+
const delivered = presentedFiles(event.data)
|
|
120
|
+
if (delivered) {
|
|
121
|
+
const row = byTurn.get(delivered.turn) || { turn: delivered.turn, firstSeq: seq ?? 0, lastSeq: seq ?? 0, deliveries: [], changes: null }
|
|
122
|
+
for (const file of delivered.files) {
|
|
123
|
+
const key = delivered.callId + ':' + file.index + ':' + file.path
|
|
124
|
+
if (deliveryKeys.has(key)) continue
|
|
125
|
+
deliveryKeys.add(key)
|
|
126
|
+
row.deliveries.push({ ...file, seq, callId: delivered.callId })
|
|
127
|
+
}
|
|
128
|
+
row.lastSeq = Math.max(row.lastSeq, seq ?? 0)
|
|
129
|
+
byTurn.set(delivered.turn, row)
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
const changes = changesOfEvent(event)
|
|
133
|
+
if (changes) {
|
|
134
|
+
const row = byTurn.get(changes.turn) || { turn: changes.turn, firstSeq: seq ?? 0, lastSeq: seq ?? 0, deliveries: [], changes: null }
|
|
135
|
+
if (!row.changes || (seq ?? 0) >= (row.changes.seq ?? 0)) row.changes = { seq }
|
|
136
|
+
row.lastSeq = Math.max(row.lastSeq, seq ?? 0)
|
|
137
|
+
byTurn.set(changes.turn, row)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return Array.from(byTurn.values()).sort((a, b) => a.turn - b.turn)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* ------------------------------------------------------------ summary model */
|
|
144
|
+
|
|
145
|
+
function summaryModel(summary) {
|
|
146
|
+
if (!summary || typeof summary !== 'object' || !Array.isArray(summary.files)) return null
|
|
147
|
+
const files = summary.files.map((file, index) => {
|
|
148
|
+
const path = cleanPath(file?.path)
|
|
149
|
+
const display = cleanPath(file?.display) || path
|
|
150
|
+
if (!path) return null
|
|
151
|
+
const binary = file?.binary === true
|
|
152
|
+
const oversized = file?.oversized === true
|
|
153
|
+
return {
|
|
154
|
+
index,
|
|
155
|
+
path,
|
|
156
|
+
display,
|
|
157
|
+
added: typeof file?.added === 'number' && file.added >= 0 ? file.added : 0,
|
|
158
|
+
deleted: typeof file?.deleted === 'number' && file.deleted >= 0 ? file.deleted : 0,
|
|
159
|
+
binary,
|
|
160
|
+
oversized,
|
|
161
|
+
status: binary ? 'binary' : oversized ? 'oversized' : 'changed',
|
|
162
|
+
}
|
|
163
|
+
}).filter(Boolean)
|
|
164
|
+
return {
|
|
165
|
+
turn: Number.isSafeInteger(summary.turn) ? summary.turn : null,
|
|
166
|
+
files,
|
|
167
|
+
total: Number.isSafeInteger(summary.total) && summary.total >= 0 ? summary.total : files.length,
|
|
168
|
+
added: typeof summary.added === 'number' && summary.added >= 0 ? summary.added : files.reduce((sum, file) => sum + file.added, 0),
|
|
169
|
+
deleted: typeof summary.deleted === 'number' && summary.deleted >= 0 ? summary.deleted : files.reduce((sum, file) => sum + file.deleted, 0),
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/* -------------------------------------------------------------- diff model */
|
|
174
|
+
|
|
175
|
+
/** The Host-reported state of one file: added / deleted / modified / binary / oversized. */
|
|
176
|
+
function fileKind(file, diffState) {
|
|
177
|
+
const data = diffState?.data
|
|
178
|
+
if (file?.binary || data?.kind === 'binary' || diffState?.status === 'binary') return 'binary'
|
|
179
|
+
if (file?.oversized || data?.kind === 'oversized' || diffState?.status === 'oversized') return 'oversized'
|
|
180
|
+
if (!diffState || diffState.status !== 'ready' || !data) return 'changed'
|
|
181
|
+
if (data.kind !== 'text') return 'unavailable'
|
|
182
|
+
const before = data.before !== false
|
|
183
|
+
const after = data.after !== false
|
|
184
|
+
if (!before && !after) return 'unavailable'
|
|
185
|
+
if (!before) return 'added'
|
|
186
|
+
if (!after) return 'deleted'
|
|
187
|
+
return 'modified'
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function validHunks(hunks) {
|
|
191
|
+
if (!Array.isArray(hunks)) return null
|
|
192
|
+
const out = []
|
|
193
|
+
for (const hunk of hunks) {
|
|
194
|
+
if (!hunk || typeof hunk !== 'object' || !Array.isArray(hunk.lines)) return null
|
|
195
|
+
const lines = hunk.lines.filter(line => typeof line === 'string')
|
|
196
|
+
out.push({
|
|
197
|
+
oldStart: numberOrNull(hunk.oldStart) ?? 1,
|
|
198
|
+
oldLines: numberOrNull(hunk.oldLines) ?? 0,
|
|
199
|
+
newStart: numberOrNull(hunk.newStart) ?? 1,
|
|
200
|
+
newLines: numberOrNull(hunk.newLines) ?? 0,
|
|
201
|
+
lines,
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
return out
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Unified rows from the Host comparison hunks. */
|
|
208
|
+
function splitHunks(diff) {
|
|
209
|
+
const hunks = validHunks(diff?.hunks) || []
|
|
210
|
+
const rows = []
|
|
211
|
+
for (const hunk of hunks) {
|
|
212
|
+
rows.push({ kind: 'hunk', text: '@@ -' + hunk.oldStart + ',' + hunk.oldLines + ' +' + hunk.newStart + ',' + hunk.newLines + ' @@' })
|
|
213
|
+
let oldNo = hunk.oldStart
|
|
214
|
+
let newNo = hunk.newStart
|
|
215
|
+
for (const line of hunk.lines) {
|
|
216
|
+
const sign = line[0]
|
|
217
|
+
const body = line.slice(1)
|
|
218
|
+
if (sign === '+') { rows.push({ kind: 'add', oldNo: null, newNo, text: body }); newNo++ }
|
|
219
|
+
else if (sign === '-') { rows.push({ kind: 'remove', oldNo, newNo: null, text: body }); oldNo++ }
|
|
220
|
+
else { rows.push({ kind: 'context', oldNo, newNo, text: body }); oldNo++; newNo++ }
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return rows
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Paired old/new rows for the desktop side-by-side view. */
|
|
227
|
+
function sideBySideRows(diff) {
|
|
228
|
+
const hunks = validHunks(diff?.hunks) || []
|
|
229
|
+
const rows = []
|
|
230
|
+
for (const hunk of hunks) {
|
|
231
|
+
rows.push({ kind: 'hunk', text: '@@ -' + hunk.oldStart + ',' + hunk.oldLines + ' +' + hunk.newStart + ',' + hunk.newLines + ' @@' })
|
|
232
|
+
let oldNo = hunk.oldStart
|
|
233
|
+
let newNo = hunk.newStart
|
|
234
|
+
let removed = []
|
|
235
|
+
let added = []
|
|
236
|
+
const flush = () => {
|
|
237
|
+
const length = Math.max(removed.length, added.length)
|
|
238
|
+
for (let i = 0; i < length; i++) {
|
|
239
|
+
rows.push({ kind: 'pair', old: removed[i] || null, new: added[i] || null })
|
|
240
|
+
}
|
|
241
|
+
removed = []
|
|
242
|
+
added = []
|
|
243
|
+
}
|
|
244
|
+
for (const line of hunk.lines) {
|
|
245
|
+
const sign = line[0]
|
|
246
|
+
const body = line.slice(1)
|
|
247
|
+
if (sign === '+') { added.push({ no: newNo, text: body }); newNo++ }
|
|
248
|
+
else if (sign === '-') { removed.push({ no: oldNo, text: body }); oldNo++ }
|
|
249
|
+
else { flush(); rows.push({ kind: 'pair', old: { no: oldNo, text: body }, new: { no: newNo, text: body }, context: true }); oldNo++; newNo++ }
|
|
250
|
+
}
|
|
251
|
+
flush()
|
|
252
|
+
}
|
|
253
|
+
return rows
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function lineCount(diff) {
|
|
257
|
+
return splitHunks(diff).filter(row => row.kind !== 'hunk').length
|
|
258
|
+
}
|
|
259
|
+
function onlyAddsOrRemoves(diff) {
|
|
260
|
+
const rows = splitHunks(diff).filter(row => row.kind !== 'hunk')
|
|
261
|
+
if (!rows.length) return false
|
|
262
|
+
return rows.every(row => row.kind === 'add') || rows.every(row => row.kind === 'remove')
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Keep whole hunk headers and at most `max` changed/context lines. */
|
|
266
|
+
function capRows(rows, max) {
|
|
267
|
+
const out = []
|
|
268
|
+
let count = 0
|
|
269
|
+
for (const row of rows) {
|
|
270
|
+
if (row.kind !== 'hunk') {
|
|
271
|
+
if (count >= max) break
|
|
272
|
+
count++
|
|
273
|
+
}
|
|
274
|
+
out.push(row)
|
|
275
|
+
}
|
|
276
|
+
return out
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function unifiedLinesHtml(rows) {
|
|
280
|
+
return rows.map(row => {
|
|
281
|
+
if (row.kind === 'hunk') return `<div class="dr-diff-hunk">${esc(row.text)}</div>`
|
|
282
|
+
const cls = row.kind === 'add' ? 'dr-diff-line--add' : row.kind === 'remove' ? 'dr-diff-line--remove' : 'dr-diff-line--context'
|
|
283
|
+
const sign = row.kind === 'add' ? '+' : row.kind === 'remove' ? '−' : ' '
|
|
284
|
+
return `<div class="dr-diff-line ${cls}"><span class="dr-diff-no">${row.oldNo ?? ''}</span><span class="dr-diff-no">${row.newNo ?? ''}</span><span class="dr-diff-sign">${sign}</span><span class="dr-diff-code-text">${esc(row.text)}</span></div>`
|
|
285
|
+
}).join('')
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function splitLinesHtml(rows) {
|
|
289
|
+
return rows.map(row => {
|
|
290
|
+
if (row.kind === 'hunk') return `<div class="dr-diff-hunk">${esc(row.text)}</div>`
|
|
291
|
+
const oldCls = row.old ? (row.context ? 'dr-diff-line--context' : 'dr-diff-line--remove') : 'dr-diff-line--blank'
|
|
292
|
+
const newCls = row.new ? (row.context ? 'dr-diff-line--context' : 'dr-diff-line--add') : 'dr-diff-line--blank'
|
|
293
|
+
return `<div class="dr-diff-row">
|
|
294
|
+
<span class="dr-diff-cell dr-diff-cell--old ${oldCls}"><span class="dr-diff-no">${row.old ? row.old.no : ''}</span><span class="dr-diff-code-text">${row.old ? esc(row.old.text) : ''}</span></span>
|
|
295
|
+
<span class="dr-diff-cell dr-diff-cell--new ${newCls}"><span class="dr-diff-no">${row.new ? row.new.no : ''}</span><span class="dr-diff-code-text">${row.new ? esc(row.new.text) : ''}</span></span>
|
|
296
|
+
</div>`
|
|
297
|
+
}).join('')
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function messageHtml(kind, message, retry) {
|
|
301
|
+
return `<p class="dr-diff-message dr-diff-message--${esc(kind)}" data-dr-diff-message="${esc(kind)}">${esc(message)}${retry ? ' <button type="button" class="dr-diff-retry" data-dr-diff-action="retry-diff">' + esc(text('重试', 'Retry')) + '</button>' : ''}</p>`
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Render one comparison. `layout` selects the mobile unified view or the
|
|
306
|
+
* desktop side-by-side view; only additions or only deletions stay in one
|
|
307
|
+
* column because a paired column would be empty.
|
|
308
|
+
*/
|
|
309
|
+
function diffHtml(diffState, file, layout) {
|
|
310
|
+
if (!diffState || diffState.status === 'loading') return `<p class="dr-diff-hint dr-diff-loading">${esc(text('正在读取主机对比…', 'Reading the Host comparison…'))}</p>`
|
|
311
|
+
if (diffState.status === 'missing') return messageHtml('missing', text('主机已不再提供该文件的对比(Host 重启后不保存历史对比),不会用当前文件冒充历史内容。', 'The Host no longer serves this comparison (it keeps none across a restart); the current file is never substituted.'), true)
|
|
312
|
+
if (diffState.status === 'error') return messageHtml('error', text('读取主机对比失败。', 'Reading the Host comparison failed.'), true)
|
|
313
|
+
if (diffState.status === 'mismatch') return messageHtml('mismatch', text('主机返回的对比与所选文件不一致,已拒绝显示。', 'The Host returned a comparison for a different file, so it was refused.'), true)
|
|
314
|
+
if (diffState.status === 'binary') return messageHtml('binary', text('二进制文件,主机不提供逐行对比。', 'Binary file — the Host serves no line comparison.'))
|
|
315
|
+
if (diffState.status === 'oversized') return messageHtml('oversized', text('文件超过主机单文件上限,主机不提供对比。', 'The file exceeds the Host per-file limit, so no comparison is available.'))
|
|
316
|
+
const data = diffState.data
|
|
317
|
+
if (!data || data.kind !== 'text') return messageHtml('missing', text('主机没有提供可用的文本对比。', 'The Host supplied no usable text comparison.'))
|
|
318
|
+
const kind = fileKind(file, diffState)
|
|
319
|
+
const rows = splitHunks(data)
|
|
320
|
+
const bodyRows = rows.filter(row => row.kind !== 'hunk')
|
|
321
|
+
const head = `<div class="dr-diff-pane-head"><span class="dr-diff-file-kind dr-diff-file-kind--${esc(kind)}">${esc(kindLabel(kind))}</span><span class="dr-diff-pane-path" title="${esc(file?.path || '')}">${esc(file?.display || file?.path || '')}</span><span class="dr-diff-pane-counts">+${file?.added ?? 0} −${file?.deleted ?? 0}</span>${data.coarse ? `<span class="dr-diff-coarse">${esc(text('主机对比超时,按整文件替换显示', 'The Host comparison timed out; shown as full replacement'))}</span>` : ''}</div>`
|
|
322
|
+
if (!bodyRows.length) return `<div class="dr-diff-pane dr-diff-pane--text" data-dr-diff-pane>${head}${messageHtml('same', text('该文件在本回合没有行级差异。', 'No line-level difference was recorded for this file.'))}</div>`
|
|
323
|
+
const limited = bodyRows.length > MAX_RENDER_LINES
|
|
324
|
+
const shown = limited ? capRows(rows, MAX_RENDER_LINES) : rows
|
|
325
|
+
const split = layout === 'desktop' && !onlyAddsOrRemoves(data)
|
|
326
|
+
const body = split
|
|
327
|
+
? `<div class="dr-diff-code dr-diff-code--split">${splitLinesHtml(sideBySideRows(data))}</div>`
|
|
328
|
+
: `<div class="dr-diff-code dr-diff-code--unified">${unifiedLinesHtml(shown)}</div>`
|
|
329
|
+
return `<div class="dr-diff-pane dr-diff-pane--text" data-dr-diff-pane>${head}${body}${limited ? `<p class="dr-diff-hint">${esc(text('对比行数过多,已截断显示。', 'The comparison is too long and was truncated.'))}</p>` : ''}</div>`
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/* ------------------------------------------------------------------ markup */
|
|
333
|
+
|
|
334
|
+
function summaryNotice(turn, summary) {
|
|
335
|
+
if (!turn.changes) return ''
|
|
336
|
+
const state = summary || { status: 'idle' }
|
|
337
|
+
if (state.status === 'idle' || state.status === 'loading') return `<p class="dr-diff-hint dr-diff-loading">${esc(text('正在读取主机改动…', 'Reading the Host changes…'))}</p>`
|
|
338
|
+
if (state.status === 'missing') return `<p class="dr-diff-message dr-diff-message--missing" data-dr-diff-message="summary-missing">${esc(text('主机已不再提供该回合改动(Host 重启后不保证存在),不会用当前文件冒充。', 'The Host no longer serves this turn\'s changes (it keeps none across a restart); the current file is never substituted.'))}</p>`
|
|
339
|
+
if (state.status === 'error') return `<p class="dr-diff-message dr-diff-message--error" data-dr-diff-message="summary-error">${esc(text('读取主机改动失败。', 'Reading the Host changes failed.'))} <button type="button" class="dr-diff-retry" data-dr-diff-action="retry-summary">${esc(text('重试', 'Retry'))}</button></p>`
|
|
340
|
+
if (state.status === 'empty') return `<p class="dr-diff-hint">${esc(text('本回合没有改动文件。', 'This turn changed no file.'))}</p>`
|
|
341
|
+
return ''
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function deliveriesHtml(turn) {
|
|
345
|
+
if (!turn.deliveries.length) return ''
|
|
346
|
+
return `<div class="dr-diff-deliveries" data-dr-diff-deliveries aria-label="${esc(text('主机交付物', 'Host deliverables'))}">
|
|
347
|
+
${turn.deliveries.map((file, index) => `<button type="button" class="dr-diff-delivery" data-dr-diff-action="delivery" data-dr-diff-turn="${turn.turn}" data-dr-diff-delivery="${index}" title="${esc(file.path)}">
|
|
348
|
+
<span class="dr-diff-delivery-name">${esc(file.name)}</span>
|
|
349
|
+
${file.description ? `<span class="dr-diff-delivery-desc">${esc(file.description)}</span>` : ''}
|
|
350
|
+
</button>`).join('')}
|
|
351
|
+
</div>`
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function filesHtml(turn, summary, selected, diffs) {
|
|
355
|
+
const files = summary?.model?.files || []
|
|
356
|
+
if (!files.length) return ''
|
|
357
|
+
return `<div class="dr-diff-files" role="tablist" aria-label="${esc(text('改动文件', 'Changed files'))}">
|
|
358
|
+
${files.map(file => {
|
|
359
|
+
const kind = fileKind(file, diffs[file.index])
|
|
360
|
+
const active = file.index === selected
|
|
361
|
+
return `<button type="button" role="tab" aria-selected="${active}" class="dr-diff-file${active ? ' is-selected' : ''} dr-diff-file--${esc(kind)}" data-dr-diff-action="file" data-dr-diff-turn="${turn.turn}" data-dr-diff-file="${file.index}">
|
|
362
|
+
<span class="dr-diff-file-name" title="${esc(file.path)}">${esc(file.display)}</span>
|
|
363
|
+
<span class="dr-diff-file-kind dr-diff-file-kind--${esc(kind)}">${esc(kindLabel(kind))}</span>
|
|
364
|
+
<span class="dr-diff-file-counts">+${file.added} −${file.deleted}</span>
|
|
365
|
+
</button>`
|
|
366
|
+
}).join('')}
|
|
367
|
+
</div>`
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function paneHtml(turn, summary, selected, diffs, layout) {
|
|
371
|
+
const files = summary?.model?.files || []
|
|
372
|
+
const file = files.find(row => row.index === selected)
|
|
373
|
+
if (!file) return ''
|
|
374
|
+
return `<div class="dr-diff-pane-host" data-dr-diff-pane-host>${diffHtml(diffs[selected] || { status: 'loading' }, file, layout)}</div>`
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function turnHtml(turn, view) {
|
|
378
|
+
const summary = turn.summary
|
|
379
|
+
const ready = summary?.status === 'ready'
|
|
380
|
+
const selected = Number.isSafeInteger(turn.selected) ? turn.selected : 0
|
|
381
|
+
const diffs = turn.diffs || {}
|
|
382
|
+
const stats = ready && summary.model
|
|
383
|
+
? `<span class="dr-diff-turn-stat">${esc(text('改动 ', 'Changed ') + summary.model.total + text(' 个文件', ' files'))}</span><span class="dr-diff-turn-counts">+${summary.model.added} −${summary.model.deleted}</span>`
|
|
384
|
+
: ''
|
|
385
|
+
const deliveries = turn.deliveries.length ? `<span class="dr-diff-turn-stat">${esc(text('交付物 ', 'Deliverables ') + turn.deliveries.length)}</span>` : ''
|
|
386
|
+
const body = `<div class="dr-diff-turn-body">
|
|
387
|
+
${deliveriesHtml(turn)}
|
|
388
|
+
${summaryNotice(turn, summary)}
|
|
389
|
+
${ready ? filesHtml(turn, summary, selected, diffs) : ''}
|
|
390
|
+
${ready && turn.expanded ? paneHtml(turn, summary, selected, diffs, view.layout) : ''}
|
|
391
|
+
</div>`
|
|
392
|
+
return `<section class="dr-diff-turn${turn.expanded ? ' is-expanded' : ' is-collapsed'}" data-dr-diff-turn="${turn.turn}">
|
|
393
|
+
<header class="dr-diff-turn-head">
|
|
394
|
+
<button type="button" class="dr-diff-toggle" data-dr-diff-action="toggle" data-dr-diff-turn="${turn.turn}" aria-expanded="${turn.expanded ? 'true' : 'false'}">
|
|
395
|
+
<span class="dr-diff-chevron" aria-hidden="true">▸</span><span>${esc(text('回合 ', 'Turn ') + turn.turn)}</span>
|
|
396
|
+
</button>
|
|
397
|
+
<span class="dr-diff-turn-meta">${deliveries}${stats}</span>
|
|
398
|
+
</header>
|
|
399
|
+
${body}
|
|
400
|
+
</section>`
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function viewHtml(view) {
|
|
404
|
+
if (!view || view.enabled !== true) return ''
|
|
405
|
+
const turns = safeArray(view.turns)
|
|
406
|
+
if (!turns.length) return ''
|
|
407
|
+
const layoutClass = view.layout === 'desktop' ? ' dr-diff-layout-desktop' : ' dr-diff-layout-mobile'
|
|
408
|
+
return `<div class="dr-diff${layoutClass}" data-dr-diff-root>${turns.map(turn => turnHtml(turn, view)).join('')}</div>`
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/* --------------------------------------------------------------- controller */
|
|
412
|
+
|
|
413
|
+
function create(adapter) {
|
|
414
|
+
const api = adapter && typeof adapter === 'object' ? adapter : {}
|
|
415
|
+
const stateApi = api.state && typeof api.state === 'object' ? api.state : {}
|
|
416
|
+
let sessionId = typeof stateApi.sessionId === 'function' ? stateApi.sessionId() : (stateApi.sessionId || null)
|
|
417
|
+
let rootEl = api.dom?.root || null
|
|
418
|
+
let unbind = null
|
|
419
|
+
let disposed = false
|
|
420
|
+
let enabled = normalizeEnabled(typeof api.enabled === 'function' ? api.enabled() : api.enabled)
|
|
421
|
+
let layout = typeof api.layout === 'function' ? api.layout() : api.layout
|
|
422
|
+
const summaries = new Map()
|
|
423
|
+
const diffs = new Map()
|
|
424
|
+
const expanded = new Set()
|
|
425
|
+
const selected = new Map()
|
|
426
|
+
const liveEvents = new Map()
|
|
427
|
+
let sessionSynced = false
|
|
428
|
+
|
|
429
|
+
function currentLayout() { return layout === 'desktop' ? 'desktop' : 'mobile' }
|
|
430
|
+
function summaryKey(seq) { return String(sessionId || '') + ':' + String(seq) }
|
|
431
|
+
function diffKey(seq, index) { return summaryKey(seq) + ':' + String(index) }
|
|
432
|
+
function getEntries(id) {
|
|
433
|
+
const base = typeof stateApi.entries === 'function' ? safeArray(stateApi.entries(id)) : []
|
|
434
|
+
const live = liveEvents.get(String(id)) || []
|
|
435
|
+
if (!live.length) return base
|
|
436
|
+
const seen = new Set(base.map(entry => entry?.seq))
|
|
437
|
+
return base.concat(live.filter(entry => entry && !seen.has(entry.seq))).sort((a, b) => (numberOrNull(a.seq) ?? 0) - (numberOrNull(b.seq) ?? 0))
|
|
438
|
+
}
|
|
439
|
+
function findTurn(turnNumber) {
|
|
440
|
+
return turnsFromEntries(getEntries(sessionId)).find(turn => turn.turn === Number(turnNumber)) || null
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function buildView() {
|
|
444
|
+
const turns = turnsFromEntries(getEntries(sessionId))
|
|
445
|
+
return {
|
|
446
|
+
enabled,
|
|
447
|
+
sessionId,
|
|
448
|
+
layout: currentLayout(),
|
|
449
|
+
turns: turns.map(turn => {
|
|
450
|
+
const key = summaryKey(turn.changes?.seq)
|
|
451
|
+
const summary = turn.changes ? (summaries.get(key) || { status: 'idle' }) : { status: 'none' }
|
|
452
|
+
const selectedIndex = selected.get(turn.turn) ?? 0
|
|
453
|
+
const rowDiffs = {}
|
|
454
|
+
if (summary.status === 'ready') {
|
|
455
|
+
for (const file of summary.model.files) {
|
|
456
|
+
const cached = diffs.get(diffKey(turn.changes.seq, file.index))
|
|
457
|
+
if (cached) rowDiffs[file.index] = cached
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return {
|
|
461
|
+
turn: turn.turn,
|
|
462
|
+
deliveries: turn.deliveries,
|
|
463
|
+
changes: turn.changes,
|
|
464
|
+
summary,
|
|
465
|
+
expanded: expanded.has(turn.turn),
|
|
466
|
+
selected: selectedIndex,
|
|
467
|
+
diffs: rowDiffs,
|
|
468
|
+
}
|
|
469
|
+
}),
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function render(container) {
|
|
473
|
+
if (container && typeof container === 'object') {
|
|
474
|
+
if (rootEl && rootEl !== container) { if (unbind) unbind(); unbind = null }
|
|
475
|
+
rootEl = container
|
|
476
|
+
}
|
|
477
|
+
const view = buildView()
|
|
478
|
+
if (rootEl && typeof rootEl === 'object' && 'innerHTML' in rootEl) {
|
|
479
|
+
rootEl.innerHTML = viewHtml(view)
|
|
480
|
+
if (!unbind) unbind = bind(rootEl, controller)
|
|
481
|
+
}
|
|
482
|
+
// The Host summary/diff routes answer asynchronously. Clients that render
|
|
483
|
+
// this module inside their own transcript slots get those slots updated once
|
|
484
|
+
// the answer lands, without a second host render pass.
|
|
485
|
+
repaintSlots()
|
|
486
|
+
return view
|
|
487
|
+
}
|
|
488
|
+
function repaintSlots() {
|
|
489
|
+
if (disposed || !enabled) return
|
|
490
|
+
if (typeof document === 'undefined' || typeof document.querySelectorAll !== 'function') return
|
|
491
|
+
let slots
|
|
492
|
+
try { slots = document.querySelectorAll('.dr-diff-turn-slot[data-turn]') } catch { return }
|
|
493
|
+
if (!slots || !slots.length) return
|
|
494
|
+
const full = buildView()
|
|
495
|
+
for (const slot of slots) {
|
|
496
|
+
const turnNumber = Number(slot?.dataset?.turn ?? slot?.getAttribute?.('data-turn'))
|
|
497
|
+
if (!Number.isSafeInteger(turnNumber)) continue
|
|
498
|
+
const turns = full.turns.filter(turn => turn.turn === turnNumber)
|
|
499
|
+
const markup = turns.length ? viewHtml({ ...full, turns }) : ''
|
|
500
|
+
if (slot.innerHTML !== markup) slot.innerHTML = markup
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function loadSummary(turn, force) {
|
|
505
|
+
if (!turn.changes) return Promise.resolve({ status: 'none' })
|
|
506
|
+
const key = summaryKey(turn.changes.seq)
|
|
507
|
+
const cached = summaries.get(key)
|
|
508
|
+
if (!force && cached && cached.status !== 'error') return cached.promise || Promise.resolve(cached)
|
|
509
|
+
if (cached?.promise && cached.status === 'loading') return cached.promise
|
|
510
|
+
const state = { status: 'loading' }
|
|
511
|
+
summaries.set(key, state)
|
|
512
|
+
render()
|
|
513
|
+
state.promise = (async () => {
|
|
514
|
+
try {
|
|
515
|
+
const result = await api.fetchJson(SUMMARY_PATH, { sessionId, seq: turn.changes.seq })
|
|
516
|
+
if (disposed) return summaries.get(key)
|
|
517
|
+
if (!result || result.ok !== true) {
|
|
518
|
+
const next = result && result.status === 404 ? { status: 'missing' } : { status: 'error', statusCode: result?.status || 0 }
|
|
519
|
+
summaries.set(key, next)
|
|
520
|
+
return next
|
|
521
|
+
}
|
|
522
|
+
const model = summaryModel(result.data)
|
|
523
|
+
const next = model ? (model.files.length ? { status: 'ready', model } : { status: 'empty', model }) : { status: 'error', statusCode: 0 }
|
|
524
|
+
summaries.set(key, next)
|
|
525
|
+
return next
|
|
526
|
+
} catch (error) {
|
|
527
|
+
if (disposed) return summaries.get(key)
|
|
528
|
+
const next = { status: 'error', error: String(error?.message || error) }
|
|
529
|
+
summaries.set(key, next)
|
|
530
|
+
return next
|
|
531
|
+
} finally {
|
|
532
|
+
if (!disposed && enabled) render()
|
|
533
|
+
}
|
|
534
|
+
})()
|
|
535
|
+
return state.promise
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function loadDiff(turn, file, force) {
|
|
539
|
+
if (!turn.changes || !file) return Promise.resolve({ status: 'missing' })
|
|
540
|
+
if (file.binary) { diffs.set(diffKey(turn.changes.seq, file.index), { status: 'binary' }); return Promise.resolve(diffs.get(diffKey(turn.changes.seq, file.index))) }
|
|
541
|
+
if (file.oversized) { diffs.set(diffKey(turn.changes.seq, file.index), { status: 'oversized' }); return Promise.resolve(diffs.get(diffKey(turn.changes.seq, file.index))) }
|
|
542
|
+
const key = diffKey(turn.changes.seq, file.index)
|
|
543
|
+
const cached = diffs.get(key)
|
|
544
|
+
if (!force && cached && cached.status !== 'error') return cached.promise || Promise.resolve(cached)
|
|
545
|
+
if (cached?.promise && cached.status === 'loading') return cached.promise
|
|
546
|
+
const state = { status: 'loading' }
|
|
547
|
+
diffs.set(key, state)
|
|
548
|
+
render()
|
|
549
|
+
state.promise = (async () => {
|
|
550
|
+
try {
|
|
551
|
+
const result = await api.fetchJson(DIFF_PATH, { sessionId, seq: turn.changes.seq, index: file.index })
|
|
552
|
+
if (disposed) return diffs.get(key)
|
|
553
|
+
if (!result || result.ok !== true) {
|
|
554
|
+
const next = result && result.status === 404 ? { status: 'missing' } : { status: 'error', statusCode: result?.status || 0 }
|
|
555
|
+
diffs.set(key, next)
|
|
556
|
+
return next
|
|
557
|
+
}
|
|
558
|
+
const data = result.data
|
|
559
|
+
if (!data || typeof data !== 'object') {
|
|
560
|
+
const next = { status: 'error' }
|
|
561
|
+
diffs.set(key, next)
|
|
562
|
+
return next
|
|
563
|
+
}
|
|
564
|
+
if (typeof data.path === 'string' && data.path && file.path && data.path !== file.path) {
|
|
565
|
+
const next = { status: 'mismatch' }
|
|
566
|
+
diffs.set(key, next)
|
|
567
|
+
return next
|
|
568
|
+
}
|
|
569
|
+
let next
|
|
570
|
+
if (data.kind === 'binary') next = { status: 'binary' }
|
|
571
|
+
else if (data.kind === 'oversized') next = { status: 'oversized' }
|
|
572
|
+
else if (data.kind === 'text' && validHunks(data.hunks)) next = { status: 'ready', data: { ...data, hunks: validHunks(data.hunks) } }
|
|
573
|
+
else next = { status: 'error' }
|
|
574
|
+
diffs.set(key, next)
|
|
575
|
+
return next
|
|
576
|
+
} catch (error) {
|
|
577
|
+
if (disposed) return diffs.get(key)
|
|
578
|
+
const next = { status: 'error', error: String(error?.message || error) }
|
|
579
|
+
diffs.set(key, next)
|
|
580
|
+
return next
|
|
581
|
+
} finally {
|
|
582
|
+
if (!disposed && enabled) render()
|
|
583
|
+
}
|
|
584
|
+
})()
|
|
585
|
+
return state.promise
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function ensureSummaryThenDiff(turn) {
|
|
589
|
+
return loadSummary(turn, false).then(summary => {
|
|
590
|
+
if (disposed || !enabled) return summary
|
|
591
|
+
if (summary.status !== 'ready' || !summary.model.files.length) return summary
|
|
592
|
+
const index = selected.get(turn.turn) ?? 0
|
|
593
|
+
const file = summary.model.files.find(row => row.index === index) || summary.model.files[0]
|
|
594
|
+
selected.set(turn.turn, file.index)
|
|
595
|
+
render()
|
|
596
|
+
return loadDiff(turn, file, false).then(() => summary)
|
|
597
|
+
})
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function refresh() {
|
|
601
|
+
if (disposed) return Promise.resolve(buildView())
|
|
602
|
+
if (!enabled) { render(); return Promise.resolve(buildView()) }
|
|
603
|
+
if (sessionId) sessionSynced = true
|
|
604
|
+
const turns = turnsFromEntries(getEntries(sessionId))
|
|
605
|
+
const loads = []
|
|
606
|
+
for (const turn of turns) {
|
|
607
|
+
if (turn.changes) loads.push(loadSummary(turn, false))
|
|
608
|
+
}
|
|
609
|
+
render()
|
|
610
|
+
return Promise.all(loads).then(() => { if (!disposed && enabled) render(); return buildView() })
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
const controller = {
|
|
614
|
+
get session() { return sessionId },
|
|
615
|
+
isEnabled() { return enabled },
|
|
616
|
+
setEnabled(next) {
|
|
617
|
+
const value = normalizeEnabled(next)
|
|
618
|
+
if (value === enabled) return enabled
|
|
619
|
+
enabled = value
|
|
620
|
+
if (!enabled) {
|
|
621
|
+
render()
|
|
622
|
+
return enabled
|
|
623
|
+
}
|
|
624
|
+
sessionSynced = false
|
|
625
|
+
void refresh()
|
|
626
|
+
return enabled
|
|
627
|
+
},
|
|
628
|
+
setLayout(next) {
|
|
629
|
+
layout = next === 'desktop' ? 'desktop' : 'mobile'
|
|
630
|
+
render()
|
|
631
|
+
return layout
|
|
632
|
+
},
|
|
633
|
+
view: buildView,
|
|
634
|
+
refresh,
|
|
635
|
+
render,
|
|
636
|
+
setSession(next) {
|
|
637
|
+
const id = next === null || next === undefined || next === '' ? null : String(next)
|
|
638
|
+
const changed = id !== sessionId
|
|
639
|
+
if (changed) sessionSynced = false
|
|
640
|
+
sessionId = id
|
|
641
|
+
render()
|
|
642
|
+
if ((changed || !sessionSynced) && enabled && id) {
|
|
643
|
+
sessionSynced = true
|
|
644
|
+
void refresh()
|
|
645
|
+
}
|
|
646
|
+
return sessionId
|
|
647
|
+
},
|
|
648
|
+
handleEvent(eventSessionId, event) {
|
|
649
|
+
if (disposed || !event || typeof event !== 'object') return false
|
|
650
|
+
if (!enabled) return false
|
|
651
|
+
const kind = event.type
|
|
652
|
+
if (kind !== 'deliverables/presented' && kind !== 'workspace/changes') return false
|
|
653
|
+
const id = String(eventSessionId || sessionId || '')
|
|
654
|
+
if (!id) return false
|
|
655
|
+
const live = liveEvents.get(id) || []
|
|
656
|
+
if (!live.some(entry => entry.seq === event.seq)) live.push({ seq: event.seq, event })
|
|
657
|
+
liveEvents.set(id, live)
|
|
658
|
+
if (kind === 'workspace/changes') void refresh()
|
|
659
|
+
else render()
|
|
660
|
+
return true
|
|
661
|
+
},
|
|
662
|
+
toggleTurn(turnNumber) {
|
|
663
|
+
if (!enabled) return Promise.resolve({ ok: false, error: text('功能未开启', 'The feature is off') })
|
|
664
|
+
const turn = findTurn(turnNumber)
|
|
665
|
+
if (!turn) return Promise.resolve({ ok: false, error: text('回合不存在', 'Unknown turn') })
|
|
666
|
+
if (expanded.has(turn.turn)) {
|
|
667
|
+
expanded.delete(turn.turn)
|
|
668
|
+
render()
|
|
669
|
+
return Promise.resolve({ ok: true, expanded: false })
|
|
670
|
+
}
|
|
671
|
+
expanded.add(turn.turn)
|
|
672
|
+
render()
|
|
673
|
+
return ensureSummaryThenDiff(turn).then(() => {
|
|
674
|
+
render()
|
|
675
|
+
return { ok: true, expanded: true }
|
|
676
|
+
})
|
|
677
|
+
},
|
|
678
|
+
selectFile(turnNumber, index) {
|
|
679
|
+
if (!enabled) return Promise.resolve({ ok: false, error: text('功能未开启', 'The feature is off') })
|
|
680
|
+
const turn = findTurn(turnNumber)
|
|
681
|
+
const target = Number(index)
|
|
682
|
+
if (!turn || !Number.isInteger(target) || target < 0) return Promise.resolve({ ok: false, error: text('文件不存在', 'Unknown file') })
|
|
683
|
+
expanded.add(turn.turn)
|
|
684
|
+
selected.set(turn.turn, target)
|
|
685
|
+
render()
|
|
686
|
+
return loadSummary(turn, false).then(summary => {
|
|
687
|
+
if (summary.status !== 'ready') { render(); return { ok: false, error: text('主机改动不可用', 'The Host changes are unavailable') } }
|
|
688
|
+
const file = summary.model.files.find(row => row.index === target)
|
|
689
|
+
if (!file) { render(); return { ok: false, error: text('文件不存在', 'Unknown file') } }
|
|
690
|
+
return loadDiff(turn, file, false).then(() => {
|
|
691
|
+
render()
|
|
692
|
+
return { ok: true, index: target }
|
|
693
|
+
})
|
|
694
|
+
})
|
|
695
|
+
},
|
|
696
|
+
retrySummary(turnNumber) {
|
|
697
|
+
const turn = findTurn(turnNumber)
|
|
698
|
+
if (!turn || !turn.changes) return Promise.resolve({ ok: false })
|
|
699
|
+
expanded.add(turn.turn)
|
|
700
|
+
return loadSummary(turn, true).then(() => { render(); return { ok: true } })
|
|
701
|
+
},
|
|
702
|
+
retryDiff(turnNumber, index) {
|
|
703
|
+
const turn = findTurn(turnNumber)
|
|
704
|
+
if (!turn) return Promise.resolve({ ok: false })
|
|
705
|
+
return loadSummary(turn, false).then(summary => {
|
|
706
|
+
if (summary.status !== 'ready') { render(); return { ok: false } }
|
|
707
|
+
const file = summary.model.files.find(row => row.index === Number(index))
|
|
708
|
+
if (!file) return { ok: false }
|
|
709
|
+
return loadDiff(turn, file, true).then(() => { render(); return { ok: true } })
|
|
710
|
+
})
|
|
711
|
+
},
|
|
712
|
+
openDelivery(turnNumber, deliveryIndex) {
|
|
713
|
+
const turn = findTurn(turnNumber)
|
|
714
|
+
const file = turn?.deliveries?.[Number(deliveryIndex)]
|
|
715
|
+
if (!file) return false
|
|
716
|
+
if (typeof api.onOpenFile === 'function') {
|
|
717
|
+
api.onOpenFile({ sessionId, turn: turn.turn, path: file.path, description: file.description })
|
|
718
|
+
}
|
|
719
|
+
return true
|
|
720
|
+
},
|
|
721
|
+
hasPendingRequest() {
|
|
722
|
+
return Array.from(summaries.values()).some(state => state.status === 'loading') || Array.from(diffs.values()).some(state => state.status === 'loading')
|
|
723
|
+
},
|
|
724
|
+
dispose() {
|
|
725
|
+
disposed = true
|
|
726
|
+
if (unbind) unbind()
|
|
727
|
+
unbind = null
|
|
728
|
+
summaries.clear()
|
|
729
|
+
diffs.clear()
|
|
730
|
+
expanded.clear()
|
|
731
|
+
selected.clear()
|
|
732
|
+
liveEvents.clear()
|
|
733
|
+
},
|
|
734
|
+
}
|
|
735
|
+
if (rootEl) render()
|
|
736
|
+
return controller
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/* ------------------------------------------------------------------ binding */
|
|
740
|
+
|
|
741
|
+
function bind(container, controller) {
|
|
742
|
+
if (!container || typeof container.addEventListener !== 'function') return () => {}
|
|
743
|
+
if (container.dataset && container.dataset.drDiffBound === '1') return () => {}
|
|
744
|
+
if (container.dataset) container.dataset.drDiffBound = '1'
|
|
745
|
+
const onClick = event => {
|
|
746
|
+
const target = event?.target
|
|
747
|
+
const button = target && typeof target.closest === 'function' ? target.closest('[data-dr-diff-action]') : null
|
|
748
|
+
if (!button || button.disabled === true) return
|
|
749
|
+
const action = button.dataset?.drDiffAction || button.getAttribute?.('data-dr-diff-action') || ''
|
|
750
|
+
const turn = Number(button.dataset?.drDiffTurn ?? button.getAttribute?.('data-dr-diff-turn'))
|
|
751
|
+
const index = Number(button.dataset?.drDiffFile ?? button.getAttribute?.('data-dr-diff-file'))
|
|
752
|
+
const delivery = Number(button.dataset?.drDiffDelivery ?? button.getAttribute?.('data-dr-diff-delivery'))
|
|
753
|
+
const run = {
|
|
754
|
+
toggle: () => controller.toggleTurn(turn),
|
|
755
|
+
file: () => controller.selectFile(turn, index),
|
|
756
|
+
delivery: () => controller.openDelivery(turn, delivery),
|
|
757
|
+
'retry-summary': () => controller.retrySummary(turn),
|
|
758
|
+
'retry-diff': () => controller.retryDiff(turn, index),
|
|
759
|
+
}[action]
|
|
760
|
+
if (!run) return
|
|
761
|
+
Promise.resolve(run()).catch(() => {})
|
|
762
|
+
}
|
|
763
|
+
container.addEventListener('click', onClick)
|
|
764
|
+
return () => {
|
|
765
|
+
container.removeEventListener?.('click', onClick)
|
|
766
|
+
if (container.dataset) delete container.dataset.drDiffBound
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
const api = {
|
|
771
|
+
SUMMARY_PATH, DIFF_PATH, MAX_RENDER_LINES,
|
|
772
|
+
normalizeEnabled, turnsFromEntries, presentedFiles, summaryModel,
|
|
773
|
+
fileKind, splitHunks, sideBySideRows, onlyAddsOrRemoves, diffHtml, viewHtml,
|
|
774
|
+
create, init: create, bind, esc, basename, kindLabel,
|
|
775
|
+
}
|
|
776
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = api
|
|
777
|
+
else root.DshTurnDiff = api
|
|
778
|
+
})(typeof window === 'undefined' ? globalThis : window)
|