@in-the-loop-labs/pair-review 5.3.7 → 5.4.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.
Files changed (39) hide show
  1. package/README.md +8 -0
  2. package/package.json +3 -3
  3. package/plugin/.claude-plugin/plugin.json +1 -1
  4. package/plugin-code-critic/.claude-plugin/plugin.json +1 -1
  5. package/plugin-pair-loop/.claude-plugin/plugin.json +1 -1
  6. package/public/css/pr.css +389 -13
  7. package/public/js/components/AIPanel.js +317 -99
  8. package/public/js/components/ReviewHistoryModal.js +294 -0
  9. package/public/js/components/ReviewModal.js +96 -7
  10. package/public/js/components/SplitButton.js +14 -0
  11. package/public/js/local.js +4 -8
  12. package/public/js/modules/comment-manager.js +2 -1
  13. package/public/js/modules/file-comment-manager.js +6 -2
  14. package/public/js/modules/pierre-bridge.js +35 -1
  15. package/public/js/modules/suggestion-manager.js +4 -1
  16. package/public/js/pr.js +174 -66
  17. package/public/js/utils/posted-comment.js +259 -14
  18. package/public/js/vendor/pierre-diffs-worker.js +41 -13485
  19. package/public/js/vendor/pierre-diffs.js +131 -1364
  20. package/public/pr.html +1 -0
  21. package/src/chat/api-reference.js +22 -2
  22. package/src/config.js +28 -4
  23. package/src/database.js +177 -56
  24. package/src/github/client.js +70 -15
  25. package/src/github/impl/graphql/pending-review-comments.js +50 -11
  26. package/src/github/impl/graphql/pending-review.js +8 -2
  27. package/src/github/impl/graphql/review-comment-edit.js +89 -0
  28. package/src/github/impl/host/pending-review-comments.js +41 -10
  29. package/src/github/impl/host/review-comment-edit.js +36 -0
  30. package/src/github/impl/rest/pending-review.js +6 -2
  31. package/src/github/impl/rest/review-comment-edit.js +44 -0
  32. package/src/github/impl/review-comment-edit-utils.js +45 -0
  33. package/src/github/operations/pending-review.js +4 -3
  34. package/src/github/operations/review-comment-edit.js +35 -0
  35. package/src/github/record-submission.js +78 -0
  36. package/src/github/update-posted-comment.js +115 -0
  37. package/src/main.js +24 -44
  38. package/src/routes/mcp.js +1 -0
  39. package/src/routes/pr.js +147 -106
@@ -3,25 +3,35 @@
3
3
  /**
4
4
  * Shared rules for user comments that have already been posted to GitHub.
5
5
  *
6
- * A user comment is pending ('active') until a review submit posts it. A
7
- * submitted review leaves it 'submitted'; Save as Draft leaves it 'draft',
8
- * held in the pending review on GitHub. Pending comments are what the next
9
- * submit sends; posted ones are history, and the reviewer can hide the
10
- * submitted ones ("Hide comments after posting").
6
+ * Lifecycle (active/inactive) is independent of publication. A pending
7
+ * comment is active with no publication; a posting derives draft/posted
8
+ * from its GitHub review. Dismissing and restoring never erases history.
9
+ * The reviewer may hide published comments ("Hide comments after posting").
10
+ *
11
+ * That setting is browser-wide. The comment panel's "Show posted" filter can
12
+ * override it for one review (setShowPostedOverride); the override never
13
+ * changes the browser-wide setting.
11
14
  */
12
15
 
13
16
  const HIDE_POSTED_STORAGE_KEY = 'pair-review-hide-posted-comments';
14
17
 
15
18
  const POSTED_ICON_SVG = '<svg width="12" height="12" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true"><path d="M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm11.28-2.28a.75.75 0 0 1 0 1.06l-3.5 3.5a.75.75 0 0 1-1.06 0l-2-2a.75.75 0 0 1 1.06-1.06l1.47 1.47 2.97-2.97a.75.75 0 0 1 1.06 0Z"/></svg>';
16
19
 
20
+ function publicationState(comment) {
21
+ return comment?.publication?.state || 'none';
22
+ }
23
+
24
+ function isPendingComment(comment) {
25
+ return comment?.status === 'active' && !comment.publication;
26
+ }
27
+
17
28
  function commentStatus(comment) {
18
29
  return comment?.status || 'active';
19
30
  }
20
31
 
21
- /** True for a comment already sent to GitHub (submitted, or in the pending draft review). */
32
+ /** True for a comment already attached to a GitHub review, even if dismissed. */
22
33
  function isPostedComment(comment) {
23
- const status = commentStatus(comment);
24
- return status === 'submitted' || status === 'draft';
34
+ return publicationState(comment) !== 'none';
25
35
  }
26
36
 
27
37
  function isHidePostedEnabled() {
@@ -36,22 +46,233 @@ function setHidePostedEnabled(enabled) {
36
46
  localStorage.setItem(HIDE_POSTED_STORAGE_KEY, String(enabled));
37
47
  }
38
48
 
49
+ // The current review's "Show posted" override: true or false, or null to
50
+ // follow the browser-wide setting. Set by the comment panel.
51
+ let showPostedOverride = null;
52
+
53
+ /** @param {boolean|null} value - true/false for this review, null to follow the browser-wide setting */
54
+ function setShowPostedOverride(value) {
55
+ showPostedOverride = typeof value === 'boolean' ? value : null;
56
+ }
57
+
58
+ function getShowPostedOverride() {
59
+ return showPostedOverride;
60
+ }
61
+
62
+ /** Whether posted comments are hidden in this review: its override, else the browser-wide setting. */
63
+ function isHidePostedInEffect() {
64
+ return showPostedOverride === null ? isHidePostedEnabled() : !showPostedOverride;
65
+ }
66
+
39
67
  /**
40
68
  * True when the comment should be hidden as posted. Saved drafts stay visible:
41
69
  * they can still change before the pending review is submitted.
42
70
  * @param {Object} comment
43
- * @param {boolean} [hidePosted] - The reviewer's setting (default: read it)
71
+ * @param {boolean} [hidePosted] - Whether posted comments are hidden
72
+ * (default: this review's setting, see isHidePostedInEffect)
44
73
  */
45
- function shouldHidePostedComment(comment, hidePosted = isHidePostedEnabled()) {
46
- return hidePosted && commentStatus(comment) === 'submitted';
74
+ function shouldHidePostedComment(comment, hidePosted = isHidePostedInEffect()) {
75
+ return hidePosted && commentStatus(comment) !== 'inactive' && publicationState(comment) === 'posted';
47
76
  }
48
77
 
49
- function getPostedCommentIcon(comment) {
78
+ /**
79
+ * How many of the comments are hidden as posted.
80
+ * @param {Array<Object>} comments
81
+ * @param {boolean} [hidePosted] - As for shouldHidePostedComment
82
+ */
83
+ function countHiddenPostedComments(comments, hidePosted = isHidePostedInEffect()) {
84
+ return (comments || []).filter(c => shouldHidePostedComment(c, hidePosted)).length;
85
+ }
86
+
87
+ function getPostedCommentIcon(comment, { link = false } = {}) {
50
88
  if (!isPostedComment(comment)) return '';
51
- const label = commentStatus(comment) === 'draft' ? 'In pending draft review' : 'Posted comment';
89
+ const draft = publicationState(comment) === 'draft';
90
+ const url = comment.publication.comment_url || comment.publication.review_url;
91
+ const label = draft ? 'In pending draft review' :
92
+ (link && !comment.publication.comment_url && url ? 'Posted review' : 'Posted comment');
93
+ if (link && url && /^https?:\/\//i.test(url)) {
94
+ const href = url.replace(/[&"<>']/g, ch => ({ '&': '&amp;', '"': '&quot;', '<': '&lt;', '>': '&gt;', "'": '&#39;' }[ch]));
95
+ return `<a class="posted-comment-icon" href="${href}" target="_blank" rel="noopener" title="${label}" aria-label="${label}">${POSTED_ICON_SVG}</a>`;
96
+ }
52
97
  return `<span class="posted-comment-icon" title="${label}" role="img" aria-label="${label}">${POSTED_ICON_SVG}</span>`;
53
98
  }
54
99
 
100
+ function escapeHtml(value) {
101
+ return String(value ?? '').replace(/[&"<>']/g, ch => ({ '&': '&amp;', '"': '&quot;', '<': '&lt;', '>': '&gt;', "'": '&#39;' }[ch]));
102
+ }
103
+
104
+ function safeUrl(url) {
105
+ return typeof url === 'string' && /^https?:\/\//i.test(url) ? url : null;
106
+ }
107
+
108
+ function isLocalMode() {
109
+ return typeof window !== 'undefined' && window.PAIR_REVIEW_LOCAL_MODE === true;
110
+ }
111
+
112
+ /** Display name of the code host ("GitHub" unless configured otherwise). */
113
+ function hostName() {
114
+ try {
115
+ const name = typeof window !== 'undefined' && window.RepoLinks?.hostName?.();
116
+ if (name) return name;
117
+ } catch { /* fall back */ }
118
+ return 'GitHub';
119
+ }
120
+
121
+ /**
122
+ * True when a posted comment's body changed after it was last sent to the
123
+ * host (the server compares the latest posting's snapshot with the body).
124
+ */
125
+ function isEditedSincePosted(comment) {
126
+ return isPostedComment(comment) && comment.publication.changed === true;
127
+ }
128
+
129
+ /**
130
+ * True when the reviewer can push the edit to the existing host comment:
131
+ * PR mode, an active comment, edited since posted. Updating never creates a
132
+ * second copy of the comment.
133
+ */
134
+ function canUpdateOnHost(comment) {
135
+ return !isLocalMode() && commentStatus(comment) === 'active' && isEditedSincePosted(comment);
136
+ }
137
+
138
+ /** Comments the submit dialog offers to update on the host. */
139
+ function getUpdatableComments(comments) {
140
+ return (comments || []).filter(canUpdateOnHost);
141
+ }
142
+
143
+ // The last failed update per comment id, kept across re-renders until the
144
+ // comment is updated or edited again.
145
+ const updateErrors = new Map();
146
+
147
+ function getUpdateError(commentId) {
148
+ return updateErrors.get(String(commentId)) || null;
149
+ }
150
+
151
+ function setUpdateError(commentId, error) {
152
+ if (error) updateErrors.set(String(commentId), error);
153
+ else updateErrors.delete(String(commentId));
154
+ }
155
+
156
+ function getUpdateErrorHtml(error) {
157
+ if (!error) return '';
158
+ const url = safeUrl(error.review_url);
159
+ const link = url ? ` <a href="${escapeHtml(url)}" target="_blank" rel="noopener">View review on ${escapeHtml(hostName())}</a>` : '';
160
+ return `${escapeHtml(error.message || 'Update failed')}${link}`;
161
+ }
162
+
163
+ /**
164
+ * The muted "Edited since posted" note with its "Update on GitHub" action,
165
+ * rendered below the comment body. Empty unless canUpdateOnHost.
166
+ * @param {Object} comment
167
+ * @returns {string} HTML
168
+ */
169
+ function getEditedSincePostedHtml(comment) {
170
+ if (!canUpdateOnHost(comment)) return '';
171
+ const id = escapeHtml(comment.id);
172
+ const error = getUpdateError(comment.id);
173
+ return `<div class="posted-comment-edited" data-edited-comment-id="${id}">
174
+ <span class="posted-comment-edited-label">Edited since posted</span>
175
+ <button type="button" class="btn-update-on-host" data-update-comment-id="${id}">Update on ${escapeHtml(hostName())}</button>
176
+ <span class="posted-comment-update-error" role="alert"${error ? '' : ' hidden'}>${getUpdateErrorHtml(error)}</span>
177
+ </div>`;
178
+ }
179
+
180
+ /**
181
+ * Re-render the edited-since-posted note inside a rendered comment (row,
182
+ * card or annotation) to match the comment, without re-rendering the diff.
183
+ * @param {HTMLElement} element - Element carrying data-comment-id
184
+ * @param {Object} comment
185
+ */
186
+ function syncEditedSincePosted(element, comment) {
187
+ if (!element) return;
188
+ element.querySelectorAll('.posted-comment-edited').forEach(note => note.remove());
189
+ const html = getEditedSincePostedHtml(comment);
190
+ const body = element.querySelector('.user-comment-body');
191
+ if (html && body) body.insertAdjacentHTML('afterend', html);
192
+ }
193
+
194
+ /** Every rendered element for a comment id, across the three diff renderers. */
195
+ function findRenderedComments(commentId) {
196
+ if (typeof document === 'undefined') return [];
197
+ const id = String(commentId).replace(/["\\]/g, '\\$&');
198
+ return [...document.querySelectorAll(`.user-comment-row[data-comment-id="${id}"], .file-comment-card[data-comment-id="${id}"]`)];
199
+ }
200
+
201
+ /** Show or clear busy state on every "Update on GitHub" button for a comment. */
202
+ function setUpdateBusy(commentId, busy) {
203
+ findRenderedComments(commentId).forEach(element => {
204
+ element.querySelectorAll('.btn-update-on-host').forEach(button => {
205
+ button.disabled = busy;
206
+ button.setAttribute('aria-busy', String(busy));
207
+ button.textContent = busy ? 'Updating…' : `Update on ${hostName()}`;
208
+ });
209
+ });
210
+ }
211
+
212
+ /**
213
+ * Ask the server to update an already-posted comment on the host. Never
214
+ * creates a comment: on failure the comment stays edited-since-posted.
215
+ * @param {number|string} reviewId
216
+ * @param {number|string} commentId
217
+ * @param {Function} [fetchImpl]
218
+ * @returns {Promise<{ok: true, comment: Object}|{ok: false, error: {message: string, code: string|null, review_url: string|null}}>}
219
+ */
220
+ async function requestHostUpdate(reviewId, commentId, fetchImpl = (...args) => fetch(...args)) {
221
+ try {
222
+ const response = await fetchImpl(`/api/reviews/${reviewId}/comments/${commentId}/update-on-github`, {
223
+ method: 'POST',
224
+ headers: { 'Content-Type': 'application/json' }
225
+ });
226
+ let data = null;
227
+ try { data = await response.json(); } catch { /* non-JSON error */ }
228
+ if (response.ok && data?.comment) return { ok: true, comment: data.comment };
229
+ return { ok: false, error: {
230
+ message: data?.error || `Could not update the comment on ${hostName()}.`,
231
+ code: data?.code || null,
232
+ review_url: data?.review_url || null
233
+ } };
234
+ } catch (error) {
235
+ return { ok: false, error: { message: `Could not reach pair-review: ${error.message}`, code: 'network', review_url: null } };
236
+ }
237
+ }
238
+
239
+ /**
240
+ * The chip for one submission in the review history: what the review did
241
+ * on the host.
242
+ * @param {Object} submission - A github_reviews row from /submissions
243
+ * @returns {{label: string, tone: string}}
244
+ */
245
+ function submissionEventChip(submission) {
246
+ const state = submission?.state;
247
+ if (state === 'pending') return { label: 'Pending draft', tone: 'draft' };
248
+ if (state === 'dismissed') return { label: 'Dismissed', tone: 'dismissed' };
249
+ if (state === 'local') return { label: 'Not submitted', tone: 'draft' };
250
+ switch (submission?.event) {
251
+ case 'APPROVE': return { label: 'Approved', tone: 'approve' };
252
+ case 'REQUEST_CHANGES': return { label: 'Changes requested', tone: 'request-changes' };
253
+ case 'COMMENT': return { label: 'Commented', tone: 'comment' };
254
+ default: return { label: 'Submitted', tone: 'comment' };
255
+ }
256
+ }
257
+
258
+ /** When a submission happened: its submit time, else when it was recorded. */
259
+ function submissionTimestamp(submission) {
260
+ return submission?.submitted_at || submission?.created_at || null;
261
+ }
262
+
263
+ /** True for a posting that edited an existing host comment in place. */
264
+ function isEditPosting(posting) {
265
+ return posting?.kind === 'update';
266
+ }
267
+
268
+ /** "Line 3", "Lines 3-5" or "File comment" for a posting. */
269
+ function postingLocation(posting) {
270
+ if (!posting) return '';
271
+ if (posting.is_file_level || posting.line_start == null) return 'File comment';
272
+ const end = posting.line_end ?? posting.line_start;
273
+ return end !== posting.line_start ? `Lines ${posting.line_start}-${end}` : `Line ${posting.line_start}`;
274
+ }
275
+
55
276
  /**
56
277
  * Stamp a rendered comment row or card with its status, and hide it if it is
57
278
  * a posted comment the reviewer chose to hide.
@@ -60,17 +281,41 @@ function getPostedCommentIcon(comment) {
60
281
  */
61
282
  function stampCommentStatus(element, comment) {
62
283
  element.dataset.commentStatus = commentStatus(comment);
284
+ element.dataset.commentPublication = publicationState(comment);
63
285
  element.classList.toggle('posted-comment-hidden', shouldHidePostedComment(comment));
64
286
  }
65
287
 
66
288
  const PostedComment = {
67
289
  HIDE_POSTED_STORAGE_KEY,
290
+ publicationState,
291
+ isPendingComment,
68
292
  isPostedComment,
69
293
  isHidePostedEnabled,
70
294
  setHidePostedEnabled,
295
+ setShowPostedOverride,
296
+ getShowPostedOverride,
297
+ isHidePostedInEffect,
71
298
  shouldHidePostedComment,
299
+ countHiddenPostedComments,
72
300
  getPostedCommentIcon,
73
- stampCommentStatus
301
+ stampCommentStatus,
302
+ hostName,
303
+ isEditedSincePosted,
304
+ canUpdateOnHost,
305
+ getUpdatableComments,
306
+ getUpdateError,
307
+ setUpdateError,
308
+ getEditedSincePostedHtml,
309
+ syncEditedSincePosted,
310
+ findRenderedComments,
311
+ setUpdateBusy,
312
+ requestHostUpdate,
313
+ escapeHtml,
314
+ safeUrl,
315
+ submissionEventChip,
316
+ submissionTimestamp,
317
+ isEditPosting,
318
+ postingLocation
74
319
  };
75
320
 
76
321
  if (typeof window !== 'undefined') {