@rvagg/marginalia 0.0.1
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.md +185 -0
- package/dist/comments.js +44 -0
- package/dist/comments.js.map +1 -0
- package/dist/comments.test.js +36 -0
- package/dist/comments.test.js.map +1 -0
- package/dist/diff.js +42 -0
- package/dist/diff.js.map +1 -0
- package/dist/diff.test.js +54 -0
- package/dist/diff.test.js.map +1 -0
- package/dist/http.js +123 -0
- package/dist/http.js.map +1 -0
- package/dist/http.test.js +13 -0
- package/dist/http.test.js.map +1 -0
- package/dist/mcp.js +266 -0
- package/dist/mcp.js.map +1 -0
- package/dist/mcp.test.js +109 -0
- package/dist/mcp.test.js.map +1 -0
- package/dist/poller.js +54 -0
- package/dist/poller.js.map +1 -0
- package/dist/poller.test.js +70 -0
- package/dist/poller.test.js.map +1 -0
- package/dist/server.js +147 -0
- package/dist/server.js.map +1 -0
- package/marginalia.png +0 -0
- package/package.json +151 -0
- package/ui/app.js +717 -0
- package/ui/diff2html-theme.css +124 -0
- package/ui/index.html +60 -0
- package/ui/style.css +426 -0
package/ui/app.js
ADDED
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
// --- Icons ---
|
|
5
|
+
const ICON_COMMENT = '<svg aria-hidden="true" viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M1 2.75C1 1.784 1.784 1 2.75 1h10.5c.966 0 1.75.784 1.75 1.75v7.5A1.75 1.75 0 0 1 13.25 12H9.06l-2.573 2.573A1.458 1.458 0 0 1 4 13.543V12H2.75A1.75 1.75 0 0 1 1 10.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h2a.75.75 0 0 1 .75.75v2.19l2.72-2.72a.749.749 0 0 1 .53-.22h4.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg>'
|
|
6
|
+
const ICON_COPY = '<svg aria-hidden="true" viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg>'
|
|
7
|
+
|
|
8
|
+
// --- Theme ---
|
|
9
|
+
|
|
10
|
+
const themeToggle = document.getElementById('theme-toggle')
|
|
11
|
+
const hljsLight = document.getElementById('hljs-light')
|
|
12
|
+
const hljsDark = document.getElementById('hljs-dark')
|
|
13
|
+
|
|
14
|
+
function applyTheme(dark) {
|
|
15
|
+
document.documentElement.setAttribute('data-theme', dark ? 'dark' : 'light')
|
|
16
|
+
themeToggle.textContent = dark ? '\u2600' : '\u263E'
|
|
17
|
+
themeToggle.title = dark ? 'Switch to light mode' : 'Switch to dark mode'
|
|
18
|
+
hljsLight.disabled = dark
|
|
19
|
+
hljsDark.disabled = !dark
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const savedTheme = localStorage.getItem('marginalia-theme')
|
|
23
|
+
const prefersDark = savedTheme === 'dark' || (!savedTheme && window.matchMedia('(prefers-color-scheme: dark)').matches)
|
|
24
|
+
applyTheme(prefersDark)
|
|
25
|
+
|
|
26
|
+
themeToggle.addEventListener('click', () => {
|
|
27
|
+
const isDark = document.documentElement.getAttribute('data-theme') === 'dark'
|
|
28
|
+
applyTheme(!isDark)
|
|
29
|
+
localStorage.setItem('marginalia-theme', !isDark ? 'dark' : 'light')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// --- State ---
|
|
33
|
+
const threads = {} // thread_id -> { el, messages }
|
|
34
|
+
const viewedFiles = {} // filename -> diff hash (to detect changes)
|
|
35
|
+
let pendingPermission = null
|
|
36
|
+
let chatExpanded = false
|
|
37
|
+
let agentName = 'agent'
|
|
38
|
+
|
|
39
|
+
// --- Elements ---
|
|
40
|
+
const filenav = document.getElementById('filenav')
|
|
41
|
+
const fileCountEl = document.getElementById('file-count')
|
|
42
|
+
const diffContainer = document.getElementById('diff-container')
|
|
43
|
+
const loadingState = document.getElementById('loading-state')
|
|
44
|
+
const emptyState = document.getElementById('empty-state')
|
|
45
|
+
const projectDirEl = document.getElementById('project-dir')
|
|
46
|
+
const statusEl = document.getElementById('status')
|
|
47
|
+
const permissionBar = document.getElementById('permission-bar')
|
|
48
|
+
const permissionText = document.getElementById('permission-text')
|
|
49
|
+
const permissionApprove = document.getElementById('permission-approve')
|
|
50
|
+
const permissionDeny = document.getElementById('permission-deny')
|
|
51
|
+
const chatToggle = document.getElementById('chat-toggle')
|
|
52
|
+
const chatIndicator = document.getElementById('chat-indicator')
|
|
53
|
+
const chatHistory = document.getElementById('chat-history')
|
|
54
|
+
const generalInput = document.getElementById('general-input')
|
|
55
|
+
const generalSend = document.getElementById('general-send')
|
|
56
|
+
|
|
57
|
+
// --- Context snippets ---
|
|
58
|
+
|
|
59
|
+
const contextContainer = document.createElement('div')
|
|
60
|
+
contextContainer.id = 'context-snippets'
|
|
61
|
+
diffContainer.parentNode.insertBefore(contextContainer, diffContainer)
|
|
62
|
+
|
|
63
|
+
function addContextSnippet(msg) {
|
|
64
|
+
const snippet = document.createElement('div')
|
|
65
|
+
snippet.className = 'context-snippet'
|
|
66
|
+
const header = `<div class="context-snippet-header">` +
|
|
67
|
+
`<span class="context-snippet-file">${escapeHtml(msg.file)}:${msg.startLine}-${msg.endLine}</span>` +
|
|
68
|
+
(msg.label ? `<span class="context-snippet-label">${escapeHtml(msg.label)}</span>` : '') +
|
|
69
|
+
`<button class="comment-dismiss" title="Dismiss">×</button>` +
|
|
70
|
+
`</div>`
|
|
71
|
+
const lines = msg.lines.split('\n')
|
|
72
|
+
const numbered = lines.map((line, i) =>
|
|
73
|
+
`<span class="context-ln">${msg.startLine + i}</span>${escapeHtml(line)}`
|
|
74
|
+
).join('\n')
|
|
75
|
+
snippet.innerHTML = header + `<pre class="context-snippet-code"><code>${numbered}</code></pre>`
|
|
76
|
+
snippet.querySelector('.comment-dismiss').addEventListener('click', () => snippet.remove())
|
|
77
|
+
contextContainer.appendChild(snippet)
|
|
78
|
+
snippet.scrollIntoView({ block: 'nearest', behavior: 'instant' })
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// --- File nav ---
|
|
82
|
+
|
|
83
|
+
function updateFileCount(n) {
|
|
84
|
+
const expanded = fileCountEl.classList.contains('active')
|
|
85
|
+
const chevron = expanded ? '\u25B4' : '\u25BE'
|
|
86
|
+
fileCountEl.textContent = n + ' file' + (n !== 1 ? 's' : '') + ' ' + chevron
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fileCountEl.addEventListener('click', () => {
|
|
90
|
+
filenav.classList.toggle('hidden')
|
|
91
|
+
fileCountEl.classList.toggle('active')
|
|
92
|
+
const n = filenav.children.length
|
|
93
|
+
updateFileCount(n)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
function updateFilenav(diffText) {
|
|
97
|
+
filenav.innerHTML = ''
|
|
98
|
+
if (!diffText) return
|
|
99
|
+
const chunks = diffText.split(/(?=^diff --git )/m)
|
|
100
|
+
for (const chunk of chunks) {
|
|
101
|
+
const nameMatch = chunk.match(/^diff --git a\/(.+?) b\/(.+)$/m)
|
|
102
|
+
if (!nameMatch) continue
|
|
103
|
+
const file = nameMatch[2]
|
|
104
|
+
const isNew = /^new file mode/m.test(chunk)
|
|
105
|
+
const isDel = /^deleted file mode/m.test(chunk)
|
|
106
|
+
const badge = isNew ? 'added' : isDel ? 'deleted' : 'changed'
|
|
107
|
+
const label = isNew ? 'A' : isDel ? 'D' : 'M'
|
|
108
|
+
const item = document.createElement('div')
|
|
109
|
+
item.className = 'filenav-item' + (viewedFiles[file] ? ' filenav-viewed' : '')
|
|
110
|
+
item.innerHTML = `<span class="filenav-badge filenav-badge-${badge}">${label}</span>${escapeHtml(file)}`
|
|
111
|
+
item.addEventListener('click', () => {
|
|
112
|
+
const wrappers = diffContainer.querySelectorAll('.d2h-file-wrapper')
|
|
113
|
+
for (const w of wrappers) {
|
|
114
|
+
const n = w.querySelector('.d2h-file-name')
|
|
115
|
+
if (n && n.textContent.trim() === file) {
|
|
116
|
+
w.scrollIntoView({ block: 'start', behavior: 'instant' })
|
|
117
|
+
break
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
filenav.appendChild(item)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// --- WebSocket ---
|
|
126
|
+
|
|
127
|
+
let ws
|
|
128
|
+
function connect() {
|
|
129
|
+
ws = new WebSocket('ws://' + location.host + '/')
|
|
130
|
+
ws.onopen = () => { statusEl.textContent = 'watching' }
|
|
131
|
+
ws.onclose = () => {
|
|
132
|
+
statusEl.textContent = 'disconnected'
|
|
133
|
+
setTimeout(connect, 2000)
|
|
134
|
+
}
|
|
135
|
+
ws.onmessage = (e) => {
|
|
136
|
+
const msg = JSON.parse(e.data)
|
|
137
|
+
switch (msg.type) {
|
|
138
|
+
case 'init':
|
|
139
|
+
projectDirEl.textContent = msg.projectDir
|
|
140
|
+
agentName = typeof msg.agentName === 'string' && msg.agentName ? msg.agentName : 'agent'
|
|
141
|
+
renderDiff(msg.diff)
|
|
142
|
+
break
|
|
143
|
+
case 'diff':
|
|
144
|
+
renderDiff(msg.diff)
|
|
145
|
+
dismissPermission()
|
|
146
|
+
break
|
|
147
|
+
case 'permission_dismiss':
|
|
148
|
+
dismissPermission()
|
|
149
|
+
break
|
|
150
|
+
case 'reply':
|
|
151
|
+
appendReply(msg.thread_id, msg.text, msg.ephemeral)
|
|
152
|
+
dismissPermission()
|
|
153
|
+
break
|
|
154
|
+
case 'status':
|
|
155
|
+
statusEl.textContent = msg.text
|
|
156
|
+
dismissPermission()
|
|
157
|
+
break
|
|
158
|
+
case 'comment_ack':
|
|
159
|
+
if (msg.file) {
|
|
160
|
+
confirmInlineComment(msg.thread_id, msg.file, parseInt(msg.line, 10), msg.side, msg.text)
|
|
161
|
+
} else {
|
|
162
|
+
confirmGeneralComment(msg.thread_id, msg.text)
|
|
163
|
+
}
|
|
164
|
+
break
|
|
165
|
+
case 'permission':
|
|
166
|
+
showPermission(msg)
|
|
167
|
+
break
|
|
168
|
+
case 'context':
|
|
169
|
+
addContextSnippet(msg)
|
|
170
|
+
break
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
connect()
|
|
175
|
+
|
|
176
|
+
function send(msg) {
|
|
177
|
+
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
178
|
+
ws.send(JSON.stringify(msg))
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// --- Diff Rendering ---
|
|
183
|
+
|
|
184
|
+
let lastFileDiffs = {}
|
|
185
|
+
|
|
186
|
+
function renderDiff(diffText) {
|
|
187
|
+
loadingState.classList.add('hidden')
|
|
188
|
+
|
|
189
|
+
if (!diffText || diffText.trim() === '') {
|
|
190
|
+
diffContainer.innerHTML = ''
|
|
191
|
+
diffContainer.appendChild(emptyState)
|
|
192
|
+
emptyState.classList.remove('hidden')
|
|
193
|
+
updateFileCount(0)
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
emptyState.classList.add('hidden')
|
|
198
|
+
const scrollTop = diffContainer.scrollTop
|
|
199
|
+
|
|
200
|
+
// Split diff into per-file chunks to detect changes
|
|
201
|
+
const currentFileDiffs = {}
|
|
202
|
+
const chunks = diffText.split(/(?=^diff --git )/m)
|
|
203
|
+
for (const chunk of chunks) {
|
|
204
|
+
const nameMatch = chunk.match(/^diff --git a\/(.+?) b\//)
|
|
205
|
+
if (nameMatch) currentFileDiffs[nameMatch[1]] = chunk
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Unmark viewed files whose diff content changed
|
|
209
|
+
for (const file of Object.keys(viewedFiles)) {
|
|
210
|
+
if (currentFileDiffs[file] && currentFileDiffs[file] !== lastFileDiffs[file]) {
|
|
211
|
+
delete viewedFiles[file]
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
lastFileDiffs = currentFileDiffs
|
|
215
|
+
|
|
216
|
+
const fileHeaders = Object.keys(currentFileDiffs).length
|
|
217
|
+
updateFileCount(fileHeaders)
|
|
218
|
+
|
|
219
|
+
// eslint-disable-next-line no-undef
|
|
220
|
+
const diff2htmlUi = new Diff2HtmlUI(diffContainer, diffText, {
|
|
221
|
+
outputFormat: 'line-by-line',
|
|
222
|
+
drawFileList: false,
|
|
223
|
+
matching: 'lines',
|
|
224
|
+
renderNothingWhenEmpty: false,
|
|
225
|
+
highlight: true
|
|
226
|
+
})
|
|
227
|
+
// Save open comment inputs before re-render destroys the DOM
|
|
228
|
+
const openInputs = saveOpenInputs()
|
|
229
|
+
|
|
230
|
+
diff2htmlUi.draw()
|
|
231
|
+
diff2htmlUi.highlightCode()
|
|
232
|
+
|
|
233
|
+
updateFilenav(diffText)
|
|
234
|
+
injectViewedToggles()
|
|
235
|
+
attachGutterHandlers()
|
|
236
|
+
reinjectThreads()
|
|
237
|
+
restoreOpenInputs(openInputs)
|
|
238
|
+
diffContainer.scrollTop = scrollTop
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// --- File header controls ---
|
|
242
|
+
|
|
243
|
+
function injectViewedToggles() {
|
|
244
|
+
const wrappers = diffContainer.querySelectorAll('.d2h-file-wrapper')
|
|
245
|
+
wrappers.forEach((wrapper) => {
|
|
246
|
+
const nameEl = wrapper.querySelector('.d2h-file-name')
|
|
247
|
+
const fileName = nameEl?.textContent?.trim()
|
|
248
|
+
if (!fileName) return
|
|
249
|
+
|
|
250
|
+
const header = wrapper.querySelector('.d2h-file-name-wrapper')
|
|
251
|
+
if (!header || header.querySelector('.mg-viewed-toggle')) return
|
|
252
|
+
|
|
253
|
+
const viewedBtn = document.createElement('button')
|
|
254
|
+
viewedBtn.className = 'mg-viewed-toggle'
|
|
255
|
+
viewedBtn.textContent = 'Viewed'
|
|
256
|
+
viewedBtn.type = 'button'
|
|
257
|
+
|
|
258
|
+
if (viewedFiles[fileName]) {
|
|
259
|
+
viewedBtn.classList.add('viewed')
|
|
260
|
+
wrapper.classList.add('mg-collapsed')
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
viewedBtn.addEventListener('click', (e) => {
|
|
264
|
+
e.stopPropagation()
|
|
265
|
+
const isViewed = viewedBtn.classList.toggle('viewed')
|
|
266
|
+
if (isViewed) {
|
|
267
|
+
viewedFiles[fileName] = true
|
|
268
|
+
wrapper.classList.add('mg-collapsed')
|
|
269
|
+
wrapper.scrollIntoView({ block: 'start', behavior: 'instant' })
|
|
270
|
+
} else {
|
|
271
|
+
delete viewedFiles[fileName]
|
|
272
|
+
wrapper.classList.remove('mg-collapsed')
|
|
273
|
+
}
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
const commentBtn = document.createElement('button')
|
|
277
|
+
commentBtn.className = 'mg-file-comment'
|
|
278
|
+
commentBtn.innerHTML = ICON_COMMENT
|
|
279
|
+
commentBtn.type = 'button'
|
|
280
|
+
commentBtn.title = 'Comment on file'
|
|
281
|
+
commentBtn.addEventListener('click', (e) => {
|
|
282
|
+
e.stopPropagation()
|
|
283
|
+
const diffBody = wrapper.querySelector('.d2h-diff-tbody')
|
|
284
|
+
if (!diffBody) return
|
|
285
|
+
const firstRow = diffBody.querySelector('tr')
|
|
286
|
+
if (!firstRow) return
|
|
287
|
+
if (firstRow.previousElementSibling?.classList.contains('comment-input-row')) return
|
|
288
|
+
openFileCommentInput(diffBody, firstRow, fileName)
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const copyBtn = document.createElement('button')
|
|
292
|
+
copyBtn.className = 'mg-copy-path'
|
|
293
|
+
copyBtn.innerHTML = ICON_COPY
|
|
294
|
+
copyBtn.type = 'button'
|
|
295
|
+
copyBtn.title = 'Copy file path'
|
|
296
|
+
copyBtn.addEventListener('click', (e) => {
|
|
297
|
+
e.stopPropagation()
|
|
298
|
+
copyToClipboard(fileName).then(() => {
|
|
299
|
+
copyBtn.classList.add('mg-copied')
|
|
300
|
+
setTimeout(() => copyBtn.classList.remove('mg-copied'), 1500)
|
|
301
|
+
})
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
header.appendChild(copyBtn)
|
|
305
|
+
header.appendChild(viewedBtn)
|
|
306
|
+
header.appendChild(commentBtn)
|
|
307
|
+
})
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// --- Comment input persistence across re-renders ---
|
|
311
|
+
|
|
312
|
+
function saveOpenInputs() {
|
|
313
|
+
const inputs = []
|
|
314
|
+
diffContainer.querySelectorAll('.comment-input-row').forEach((row) => {
|
|
315
|
+
const textarea = row.querySelector('textarea')
|
|
316
|
+
if (!textarea) return
|
|
317
|
+
const context = row.querySelector('.comment-input-context')
|
|
318
|
+
const contextText = context?.textContent ?? ''
|
|
319
|
+
// Parse "file:line" or "file" from context label
|
|
320
|
+
const match = contextText.match(/^(.+):(\d+)$/)
|
|
321
|
+
const file = match ? match[1] : contextText
|
|
322
|
+
const line = match ? parseInt(match[2], 10) : 0
|
|
323
|
+
inputs.push({ file, line, text: textarea.value })
|
|
324
|
+
})
|
|
325
|
+
return inputs
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function restoreOpenInputs(inputs) {
|
|
329
|
+
for (const { file, line, text } of inputs) {
|
|
330
|
+
if (line === 0) {
|
|
331
|
+
// File-level: find the file wrapper and re-open
|
|
332
|
+
const wrappers = diffContainer.querySelectorAll('.d2h-file-wrapper')
|
|
333
|
+
for (const wrapper of wrappers) {
|
|
334
|
+
const nameEl = wrapper.querySelector('.d2h-file-name')
|
|
335
|
+
if (nameEl?.textContent?.trim() !== file) continue
|
|
336
|
+
const diffBody = wrapper.querySelector('.d2h-diff-tbody')
|
|
337
|
+
const firstRow = diffBody?.querySelector('tr')
|
|
338
|
+
if (!diffBody || !firstRow) continue
|
|
339
|
+
const inputRow = createCommentInputRow(file, null)
|
|
340
|
+
diffBody.insertBefore(inputRow.el, firstRow)
|
|
341
|
+
inputRow.el.querySelector('textarea').value = text
|
|
342
|
+
break
|
|
343
|
+
}
|
|
344
|
+
} else {
|
|
345
|
+
// Line-level: find the row and re-open
|
|
346
|
+
const row = findDiffRow(file, line, 'right') || findDiffRow(file, line, 'left')
|
|
347
|
+
if (!row) continue
|
|
348
|
+
const inputRow = createCommentInputRow(file, line)
|
|
349
|
+
row.parentNode.insertBefore(inputRow.el, row.nextSibling)
|
|
350
|
+
row.classList.add('mg-commenting')
|
|
351
|
+
inputRow.onCleanup = () => row.classList.remove('mg-commenting')
|
|
352
|
+
inputRow.el.querySelector('textarea').value = text
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// --- Comment inputs ---
|
|
358
|
+
|
|
359
|
+
function openFileCommentInput(tbody, beforeRow, file) {
|
|
360
|
+
const inputRow = createCommentInputRow(file, null)
|
|
361
|
+
tbody.insertBefore(inputRow.el, beforeRow)
|
|
362
|
+
inputRow.focus()
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function openCommentInput(afterRow, file, line, side) {
|
|
366
|
+
const existingInput = afterRow.nextElementSibling
|
|
367
|
+
if (existingInput?.classList.contains('comment-input-row')) return
|
|
368
|
+
|
|
369
|
+
const inputRow = createCommentInputRow(file, line)
|
|
370
|
+
afterRow.parentNode.insertBefore(inputRow.el, afterRow.nextSibling)
|
|
371
|
+
afterRow.classList.add('mg-commenting')
|
|
372
|
+
inputRow.onCleanup = () => afterRow.classList.remove('mg-commenting')
|
|
373
|
+
inputRow.focus()
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function createCommentInputRow(file, line) {
|
|
377
|
+
const isFileLevel = line === null || line === 0
|
|
378
|
+
const tr = document.createElement('tr')
|
|
379
|
+
tr.className = 'comment-input-row'
|
|
380
|
+
const cell = document.createElement('td')
|
|
381
|
+
cell.colSpan = 3
|
|
382
|
+
cell.innerHTML = `
|
|
383
|
+
<div class="comment-input-box">
|
|
384
|
+
<div class="comment-input-context">${escapeHtml(file)}${isFileLevel ? '' : ':' + line}</div>
|
|
385
|
+
<textarea class="mg-textarea" rows="3" placeholder="${isFileLevel ? 'Comment on this file...' : 'Leave a comment...'}"></textarea>
|
|
386
|
+
<div class="comment-input-actions">
|
|
387
|
+
<button class="small" data-action="submit">Comment</button>
|
|
388
|
+
<button class="small secondary" data-action="cancel">Cancel</button>
|
|
389
|
+
</div>
|
|
390
|
+
</div>
|
|
391
|
+
`
|
|
392
|
+
tr.appendChild(cell)
|
|
393
|
+
|
|
394
|
+
const textarea = cell.querySelector('textarea')
|
|
395
|
+
let onCleanup = null
|
|
396
|
+
|
|
397
|
+
function cleanup() {
|
|
398
|
+
tr.remove()
|
|
399
|
+
if (onCleanup) onCleanup()
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function submit() {
|
|
403
|
+
const text = textarea.value.trim()
|
|
404
|
+
if (!text) return
|
|
405
|
+
send({ type: 'comment', file, line: isFileLevel ? 0 : line, side: 'right', text })
|
|
406
|
+
cleanup()
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
textarea.addEventListener('keydown', (e) => {
|
|
410
|
+
if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) { e.preventDefault(); submit() }
|
|
411
|
+
if (e.key === 'Escape') cleanup()
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
cell.querySelector('[data-action="submit"]').addEventListener('click', submit)
|
|
415
|
+
cell.querySelector('[data-action="cancel"]').addEventListener('click', cleanup)
|
|
416
|
+
|
|
417
|
+
return {
|
|
418
|
+
el: tr,
|
|
419
|
+
focus: () => textarea.focus(),
|
|
420
|
+
set onCleanup(fn) { onCleanup = fn }
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// --- Gutter click handlers ---
|
|
425
|
+
|
|
426
|
+
function attachGutterHandlers() {
|
|
427
|
+
diffContainer.querySelectorAll('.d2h-code-linenumber').forEach((el) => {
|
|
428
|
+
el.addEventListener('click', (e) => {
|
|
429
|
+
e.stopPropagation()
|
|
430
|
+
const row = el.closest('tr')
|
|
431
|
+
if (!row || row.classList.contains('d2h-info')) return
|
|
432
|
+
|
|
433
|
+
const fileWrapper = el.closest('.d2h-file-wrapper')
|
|
434
|
+
const file = fileWrapper?.querySelector('.d2h-file-name')?.textContent?.trim() ?? ''
|
|
435
|
+
const lineInfo = parseLineInfo(el)
|
|
436
|
+
if (!lineInfo) return
|
|
437
|
+
|
|
438
|
+
openCommentInput(row, file, lineInfo.line, lineInfo.side)
|
|
439
|
+
})
|
|
440
|
+
})
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function parseLineInfo(lineNumEl) {
|
|
444
|
+
const text = lineNumEl.textContent.trim()
|
|
445
|
+
const nums = text.split(/\s+/).filter(Boolean)
|
|
446
|
+
if (nums.length === 0) return null
|
|
447
|
+
|
|
448
|
+
const row = lineNumEl.closest('tr')
|
|
449
|
+
if (!row) return null
|
|
450
|
+
const isDelete = row.querySelector('.d2h-del') !== null
|
|
451
|
+
const isInsert = row.querySelector('.d2h-ins') !== null
|
|
452
|
+
|
|
453
|
+
let line, side
|
|
454
|
+
if (isDelete) {
|
|
455
|
+
line = parseInt(nums[0], 10)
|
|
456
|
+
side = 'left'
|
|
457
|
+
} else if (isInsert) {
|
|
458
|
+
line = nums.length > 1 ? parseInt(nums[1], 10) : parseInt(nums[0], 10)
|
|
459
|
+
side = 'right'
|
|
460
|
+
} else {
|
|
461
|
+
line = nums.length > 1 ? parseInt(nums[1], 10) : parseInt(nums[0], 10)
|
|
462
|
+
side = 'right'
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (isNaN(line)) return null
|
|
466
|
+
return { line, side }
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// --- Comment threads ---
|
|
470
|
+
|
|
471
|
+
function confirmInlineComment(threadId, file, line, side, text) {
|
|
472
|
+
threads[threadId] = { file, line, side, messages: [{ from: 'user', text }], el: null }
|
|
473
|
+
injectThread(threadId)
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function confirmGeneralComment(threadId, text) {
|
|
477
|
+
threads[threadId] = { file: null, line: null, side: null, messages: [{ from: 'user', text }], el: null }
|
|
478
|
+
addChatMessage('you', text)
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function appendReply(threadId, text, ephemeral) {
|
|
482
|
+
const thread = threads[threadId]
|
|
483
|
+
if (thread) {
|
|
484
|
+
thread.messages = thread.messages.filter(m => !m.ephemeral)
|
|
485
|
+
thread.messages.push({ from: 'assistant', text, ephemeral })
|
|
486
|
+
if (thread.file) {
|
|
487
|
+
renderThreadEl(thread)
|
|
488
|
+
} else {
|
|
489
|
+
addChatMessage(agentName, text, ephemeral)
|
|
490
|
+
}
|
|
491
|
+
} else {
|
|
492
|
+
addChatMessage(agentName, text, ephemeral)
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function injectThread(threadId) {
|
|
497
|
+
const thread = threads[threadId]
|
|
498
|
+
if (!thread || !thread.file) return
|
|
499
|
+
|
|
500
|
+
// Find the anchor point: a specific line, or the top of the file's diff
|
|
501
|
+
let anchor = null
|
|
502
|
+
let insertBefore = false
|
|
503
|
+
if (thread.line) {
|
|
504
|
+
anchor = findDiffRow(thread.file, thread.line, thread.side)
|
|
505
|
+
} else {
|
|
506
|
+
// File-level comment: insert before the first row in the file's tbody
|
|
507
|
+
const wrappers = diffContainer.querySelectorAll('.d2h-file-wrapper')
|
|
508
|
+
for (const wrapper of wrappers) {
|
|
509
|
+
const nameEl = wrapper.querySelector('.d2h-file-name')
|
|
510
|
+
if (nameEl?.textContent?.trim() !== thread.file) continue
|
|
511
|
+
anchor = wrapper.querySelector('.d2h-diff-tbody tr')
|
|
512
|
+
insertBefore = true
|
|
513
|
+
break
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (!anchor) return
|
|
517
|
+
|
|
518
|
+
if (!thread.el) {
|
|
519
|
+
thread.el = document.createElement('tr')
|
|
520
|
+
thread.el.className = 'comment-thread-row'
|
|
521
|
+
thread.el.dataset.threadId = threadId
|
|
522
|
+
const cell = document.createElement('td')
|
|
523
|
+
cell.colSpan = 3
|
|
524
|
+
thread.el.appendChild(cell)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
renderThreadEl(thread)
|
|
528
|
+
|
|
529
|
+
const existing = diffContainer.querySelector(`[data-thread-id="${threadId}"]`)
|
|
530
|
+
if (existing) existing.remove()
|
|
531
|
+
if (insertBefore) {
|
|
532
|
+
anchor.parentNode.insertBefore(thread.el, anchor)
|
|
533
|
+
} else {
|
|
534
|
+
anchor.parentNode.insertBefore(thread.el, anchor.nextSibling)
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function renderMarkdown(text) {
|
|
539
|
+
// eslint-disable-next-line no-undef
|
|
540
|
+
try { return marked.parse(text, { breaks: true }) } catch { return escapeHtml(text) }
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function renderMarkdownInline(text) {
|
|
544
|
+
// eslint-disable-next-line no-undef
|
|
545
|
+
try { return marked.parseInline(text) } catch { return escapeHtml(text) }
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function renderMessageContent(m) {
|
|
549
|
+
const content = renderMarkdown(m.text)
|
|
550
|
+
const ephClass = m.ephemeral ? ' comment-ephemeral' : ''
|
|
551
|
+
return `<div class="comment-message comment-${m.from}${ephClass}">` +
|
|
552
|
+
`<strong>${m.from === 'user' ? 'you' : escapeHtml(agentName)}</strong>` +
|
|
553
|
+
`<div class="mg-md">${content}</div></div>`
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function renderThreadEl(thread) {
|
|
557
|
+
if (!thread.el) return
|
|
558
|
+
const cell = thread.el.querySelector('td')
|
|
559
|
+
const threadId = thread.el.dataset.threadId
|
|
560
|
+
|
|
561
|
+
// Preserve in-progress reply text
|
|
562
|
+
const prevInput = cell.querySelector('.thread-reply-input')
|
|
563
|
+
const savedText = prevInput ? prevInput.value : ''
|
|
564
|
+
|
|
565
|
+
cell.innerHTML = '<div class="comment-thread">' +
|
|
566
|
+
'<button class="comment-dismiss" title="Dismiss">×</button>' +
|
|
567
|
+
thread.messages.map(renderMessageContent).join('') +
|
|
568
|
+
'<div class="thread-reply-box">' +
|
|
569
|
+
'<textarea rows="2" class="mg-textarea thread-reply-input" placeholder="Reply..."></textarea>' +
|
|
570
|
+
'<div class="comment-input-actions"><button class="small thread-reply-send">Reply</button></div>' +
|
|
571
|
+
'</div>' +
|
|
572
|
+
'</div>'
|
|
573
|
+
cell.querySelector('.comment-dismiss').addEventListener('click', () => {
|
|
574
|
+
thread.dismissed = true
|
|
575
|
+
thread.el.remove()
|
|
576
|
+
thread.el = null
|
|
577
|
+
})
|
|
578
|
+
const input = cell.querySelector('.thread-reply-input')
|
|
579
|
+
input.value = savedText
|
|
580
|
+
if (savedText) input.focus()
|
|
581
|
+
const sendBtn = cell.querySelector('.thread-reply-send')
|
|
582
|
+
function sendReply() {
|
|
583
|
+
const text = input.value.trim()
|
|
584
|
+
if (!text) return
|
|
585
|
+
input.value = ''
|
|
586
|
+
send({ type: 'thread_reply', thread_id: threadId, text })
|
|
587
|
+
thread.messages.push({ from: 'user', text })
|
|
588
|
+
renderThreadEl(thread)
|
|
589
|
+
}
|
|
590
|
+
sendBtn.addEventListener('click', sendReply)
|
|
591
|
+
input.addEventListener('keydown', (e) => {
|
|
592
|
+
if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) { e.preventDefault(); sendReply() }
|
|
593
|
+
})
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function reinjectThreads() {
|
|
597
|
+
for (const threadId of Object.keys(threads)) {
|
|
598
|
+
const thread = threads[threadId]
|
|
599
|
+
if (thread.file && !thread.dismissed) injectThread(threadId)
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function findDiffRow(file, line, side) {
|
|
604
|
+
const fileWrappers = diffContainer.querySelectorAll('.d2h-file-wrapper')
|
|
605
|
+
for (const wrapper of fileWrappers) {
|
|
606
|
+
const nameEl = wrapper.querySelector('.d2h-file-name')
|
|
607
|
+
if (!nameEl || nameEl.textContent.trim() !== file) continue
|
|
608
|
+
const rows = wrapper.querySelectorAll('tr')
|
|
609
|
+
for (const row of rows) {
|
|
610
|
+
const lineNumEl = row.querySelector('.d2h-code-linenumber')
|
|
611
|
+
if (!lineNumEl) continue
|
|
612
|
+
const info = parseLineInfo(lineNumEl)
|
|
613
|
+
if (!info) continue
|
|
614
|
+
if (info.line === line && info.side === side) return row
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return null
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// --- Permission relay ---
|
|
621
|
+
|
|
622
|
+
function showPermission(msg) {
|
|
623
|
+
pendingPermission = msg.request_id
|
|
624
|
+
permissionText.textContent = `Claude wants to run ${msg.tool_name}: ${msg.description}`
|
|
625
|
+
permissionBar.classList.remove('hidden')
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function dismissPermission() {
|
|
629
|
+
if (!pendingPermission) return
|
|
630
|
+
pendingPermission = null
|
|
631
|
+
permissionBar.classList.add('hidden')
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
permissionApprove.addEventListener('click', () => {
|
|
635
|
+
if (!pendingPermission) return
|
|
636
|
+
send({ type: 'permission_verdict', request_id: pendingPermission, behavior: 'allow' })
|
|
637
|
+
permissionBar.classList.add('hidden')
|
|
638
|
+
pendingPermission = null
|
|
639
|
+
})
|
|
640
|
+
|
|
641
|
+
permissionDeny.addEventListener('click', () => {
|
|
642
|
+
if (!pendingPermission) return
|
|
643
|
+
send({ type: 'permission_verdict', request_id: pendingPermission, behavior: 'deny' })
|
|
644
|
+
permissionBar.classList.add('hidden')
|
|
645
|
+
pendingPermission = null
|
|
646
|
+
})
|
|
647
|
+
|
|
648
|
+
// --- Chat footer ---
|
|
649
|
+
|
|
650
|
+
chatToggle.addEventListener('click', (e) => {
|
|
651
|
+
if (e.target === generalInput || e.target === generalSend) return
|
|
652
|
+
chatExpanded = !chatExpanded
|
|
653
|
+
chatHistory.classList.toggle('hidden', !chatExpanded)
|
|
654
|
+
chatIndicator.textContent = chatExpanded ? '-' : '+'
|
|
655
|
+
})
|
|
656
|
+
|
|
657
|
+
function sendGeneral() {
|
|
658
|
+
const text = generalInput.value.trim()
|
|
659
|
+
if (!text) return
|
|
660
|
+
send({ type: 'general', text })
|
|
661
|
+
generalInput.value = ''
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
generalSend.addEventListener('click', sendGeneral)
|
|
665
|
+
generalInput.addEventListener('keydown', (e) => {
|
|
666
|
+
if (e.key === 'Enter' && !e.shiftKey) {
|
|
667
|
+
e.preventDefault()
|
|
668
|
+
sendGeneral()
|
|
669
|
+
}
|
|
670
|
+
})
|
|
671
|
+
|
|
672
|
+
const MAX_CHAT_MESSAGES = 50
|
|
673
|
+
|
|
674
|
+
function addChatMessage(from, text, ephemeral) {
|
|
675
|
+
if (!ephemeral) {
|
|
676
|
+
chatHistory.querySelectorAll('.chat-ephemeral').forEach(el => el.remove())
|
|
677
|
+
}
|
|
678
|
+
const div = document.createElement('div')
|
|
679
|
+
div.className = 'chat-message chat-' + (from === 'you' ? 'you' : 'assistant') + (ephemeral ? ' chat-ephemeral' : '')
|
|
680
|
+
const content = renderMarkdown(text)
|
|
681
|
+
div.innerHTML = `<strong>${escapeHtml(from)}</strong><div class="mg-md">${content}</div>`
|
|
682
|
+
chatHistory.appendChild(div)
|
|
683
|
+
while (chatHistory.children.length > MAX_CHAT_MESSAGES) {
|
|
684
|
+
chatHistory.removeChild(chatHistory.firstChild)
|
|
685
|
+
}
|
|
686
|
+
chatHistory.scrollTop = chatHistory.scrollHeight
|
|
687
|
+
if (!chatExpanded) {
|
|
688
|
+
chatExpanded = true
|
|
689
|
+
chatHistory.classList.remove('hidden')
|
|
690
|
+
chatIndicator.textContent = '-'
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// --- Util ---
|
|
695
|
+
|
|
696
|
+
function escapeHtml(str) {
|
|
697
|
+
const div = document.createElement('div')
|
|
698
|
+
div.textContent = str
|
|
699
|
+
return div.innerHTML
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
function copyToClipboard(text) {
|
|
703
|
+
if (navigator.clipboard && window.isSecureContext) {
|
|
704
|
+
return navigator.clipboard.writeText(text)
|
|
705
|
+
}
|
|
706
|
+
// Fallback for non-secure contexts (e.g. HTTP over IP)
|
|
707
|
+
const textarea = document.createElement('textarea')
|
|
708
|
+
textarea.value = text
|
|
709
|
+
textarea.style.position = 'fixed'
|
|
710
|
+
textarea.style.opacity = '0'
|
|
711
|
+
document.body.appendChild(textarea)
|
|
712
|
+
textarea.select()
|
|
713
|
+
document.execCommand('copy')
|
|
714
|
+
document.body.removeChild(textarea)
|
|
715
|
+
return Promise.resolve()
|
|
716
|
+
}
|
|
717
|
+
})()
|