@in-the-loop-labs/pair-review 4.1.0 → 5.0.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 (71) hide show
  1. package/README.md +81 -13
  2. package/package.json +6 -2
  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/plugin-pair-loop/skills/loop/SKILL.md +17 -8
  7. package/public/css/ai-summary-modal.css +1 -1
  8. package/public/css/analysis-config.css +33 -49
  9. package/public/css/council-card.css +118 -0
  10. package/public/css/council-dropdown.css +164 -0
  11. package/public/css/pr.css +666 -394
  12. package/public/css/repo-settings.css +15 -305
  13. package/public/css/settings.css +1141 -0
  14. package/public/css/snippet-manager.css +233 -0
  15. package/public/index.html +40 -59
  16. package/public/js/components/AIPanel.js +82 -16
  17. package/public/js/components/ChatPanel.js +401 -5
  18. package/public/js/components/CouncilCard.js +202 -0
  19. package/public/js/components/CouncilDropdown.js +292 -0
  20. package/public/js/components/DiffOptionsDropdown.js +177 -4
  21. package/public/js/components/SnippetManager.js +463 -0
  22. package/public/js/index.js +7 -30
  23. package/public/js/local.js +20 -2
  24. package/public/js/modules/comment-manager.js +8 -1
  25. package/public/js/modules/comment-minimizer.js +249 -190
  26. package/public/js/modules/external-comment-manager.js +414 -48
  27. package/public/js/modules/file-comment-manager.js +28 -21
  28. package/public/js/modules/hunk-summary-renderer.js +155 -29
  29. package/public/js/modules/pierre-bridge.js +2513 -0
  30. package/public/js/modules/pierre-context.js +345 -0
  31. package/public/js/modules/suggestion-manager.js +176 -20
  32. package/public/js/modules/tour-renderer.js +400 -184
  33. package/public/js/pr.js +1434 -150
  34. package/public/js/repo-settings.js +60 -284
  35. package/public/js/settings.js +1086 -0
  36. package/public/js/theme-bootstrap.js +40 -0
  37. package/public/js/theme.js +247 -0
  38. package/public/js/utils/provider-model.js +3 -3
  39. package/public/js/utils/suggestion-ui.js +157 -8
  40. package/public/local.html +20 -31
  41. package/public/pr.html +14 -23
  42. package/public/repo-settings.html +24 -14
  43. package/public/settings.html +138 -0
  44. package/public/setup.html +8 -13
  45. package/src/ai/codex-provider.js +59 -11
  46. package/src/chat/api-reference.js +9 -5
  47. package/src/chat/prompt-builder.js +8 -1
  48. package/src/config.js +15 -6
  49. package/src/database.js +307 -14
  50. package/src/external/github-adapter.js +39 -5
  51. package/src/headless/delegate.js +682 -0
  52. package/src/local-review.js +10 -10
  53. package/src/local-scope.js +9 -0
  54. package/src/main.js +276 -157
  55. package/src/mcp-stdio.js +24 -1
  56. package/src/review-config.js +93 -25
  57. package/src/routes/chat.js +2 -2
  58. package/src/routes/config.js +12 -10
  59. package/src/routes/external-comments.js +5 -1
  60. package/src/routes/local.js +2 -4
  61. package/src/routes/mcp.js +18 -13
  62. package/src/routes/reviews.js +223 -4
  63. package/src/routes/settings.js +252 -0
  64. package/src/routes/setup.js +80 -1
  65. package/src/routes/shared.js +31 -29
  66. package/src/routes/snippets.js +154 -0
  67. package/src/routes/stack-analysis.js +17 -14
  68. package/src/server.js +86 -18
  69. package/src/settings/global-settings-service.js +472 -0
  70. package/src/settings/registry.js +624 -0
  71. package/src/utils/db-identity.js +44 -0
@@ -865,6 +865,14 @@ class ChatPanel {
865
865
  <div class="chat-panel__input-footer">
866
866
  <span class="chat-panel__input-hint" title="Configure with chat.enter_to_send">${this._enterToSend ? 'Enter to send, Shift+Enter for newline' : `${typeof navigator !== 'undefined' && navigator.platform?.includes('Mac') ? '\u2318' : 'Ctrl'}+Enter to send`}</span>
867
867
  <div class="chat-panel__input-actions">
868
+ <div class="chat-panel__snippet-picker">
869
+ <button class="chat-panel__snippet-picker-btn" title="Insert prompt snippet">
870
+ <svg viewBox="0 0 16 16" fill="currentColor" width="14" height="14">
871
+ <path d="M6.78 1.97a.75.75 0 0 1 0 1.06L3.81 6h6.44A4.75 4.75 0 0 1 15 10.75v2.5a.75.75 0 0 1-1.5 0v-2.5a3.25 3.25 0 0 0-3.25-3.25H3.81l2.97 2.97a.75.75 0 1 1-1.06 1.06L1.47 7.28a.75.75 0 0 1 0-1.06l4.25-4.25a.75.75 0 0 1 1.06 0Z"/>
872
+ </svg>
873
+ </button>
874
+ <div class="chat-panel__snippet-dropdown" style="display: none;"></div>
875
+ </div>
868
876
  <button class="chat-panel__send-btn" title="Send" disabled>
869
877
  <svg viewBox="0 0 16 16" fill="currentColor" width="14" height="14">
870
878
  <path d="M.989 8 .064 2.68a1.342 1.342 0 0 1 1.85-1.462l13.402 5.744a1.13 1.13 0 0 1 0 2.076L1.913 14.782a1.343 1.343 0 0 1-1.85-1.463L.99 8Zm.603-4.776L2.296 7.25h5.954a.75.75 0 0 1 0 1.5H2.296l-.704 4.026L13.788 8Z"/>
@@ -888,6 +896,9 @@ class ChatPanel {
888
896
  this.tabStripItemsEl = this.container.querySelector('.chat-panel__tab-strip-items');
889
897
  this.tabNewBtn = this.container.querySelector('.chat-panel__tab-new-btn');
890
898
  this.inputEl = this.container.querySelector('.chat-panel__input');
899
+ this.snippetPickerEl = this.container.querySelector('.chat-panel__snippet-picker');
900
+ this.snippetPickerBtn = this.container.querySelector('.chat-panel__snippet-picker-btn');
901
+ this.snippetDropdown = this.container.querySelector('.chat-panel__snippet-dropdown');
891
902
  this.sendBtn = this.container.querySelector('.chat-panel__send-btn');
892
903
  this.stopBtn = this.container.querySelector('.chat-panel__stop-btn');
893
904
  this.closeBtn = this.container.querySelector('.chat-panel__close-btn');
@@ -929,6 +940,9 @@ class ChatPanel {
929
940
  // Session history button
930
941
  this.historyBtn.addEventListener('click', () => this._toggleSessionDropdown());
931
942
 
943
+ // Prompt-snippet picker button
944
+ this.snippetPickerBtn?.addEventListener('click', () => this._toggleSnippetDropdown());
945
+
932
946
  // Send button
933
947
  this.sendBtn.addEventListener('click', () => this.sendMessage());
934
948
 
@@ -983,10 +997,18 @@ class ChatPanel {
983
997
  // Escape: close dropdown if open, stop agent if streaming, blur textarea if focused, otherwise close panel
984
998
  this._onKeydown = (e) => {
985
999
  if (e.key === 'Escape' && this.isOpen) {
986
- if (this._isProviderDropdownOpen()) {
1000
+ // Escape ladder (first match wins): save-snippet pill → provider
1001
+ // dropdown → session dropdown → snippet dropdown → stop streaming →
1002
+ // blur input → close panel. The pill goes first so dismissing it
1003
+ // never falls through to closing the panel.
1004
+ if (this._saveSnippetPill) {
1005
+ this._hideSaveSnippetPill();
1006
+ } else if (this._isProviderDropdownOpen()) {
987
1007
  this._hideProviderDropdown();
988
1008
  } else if (this._isSessionDropdownOpen()) {
989
1009
  this._hideSessionDropdown();
1010
+ } else if (this._isSnippetDropdownOpen()) {
1011
+ this._hideSnippetDropdown();
990
1012
  } else if (this.isStreaming) {
991
1013
  this._stopAgent();
992
1014
  } else if (document.activeElement === this.inputEl) {
@@ -1007,6 +1029,18 @@ class ChatPanel {
1007
1029
  }
1008
1030
  });
1009
1031
 
1032
+ // Alt-click on a submitted USER message reveals a "Save as snippet" pill.
1033
+ // Separate listener so plain clicks keep their exact prior behavior — we
1034
+ // act only when e.altKey is set. Assistant/error messages are not savable.
1035
+ this.messagesStackEl?.addEventListener('click', (e) => {
1036
+ if (!e.altKey) return;
1037
+ const msgEl = e.target.closest?.('.chat-panel__message--user');
1038
+ if (!msgEl) return;
1039
+ e.preventDefault();
1040
+ e.stopPropagation();
1041
+ this._showSaveSnippetPill(msgEl, e);
1042
+ });
1043
+
1010
1044
  // Re-read chat providers once the <head> config fetch resolves
1011
1045
  this._onChatStateChanged = () => {
1012
1046
  this._chatProviders = window.__pairReview?.chatProviders || [];
@@ -1338,6 +1372,7 @@ class ChatPanel {
1338
1372
  file: options.commentContext.file,
1339
1373
  line_start: options.commentContext.line_start,
1340
1374
  line_end: options.commentContext.line_end,
1375
+ side: options.commentContext.side || 'RIGHT',
1341
1376
  };
1342
1377
  } else if (options.commentContext.source === 'external') {
1343
1378
  // External comments are read-only — no adopt/update/dismiss actions
@@ -1386,6 +1421,8 @@ class ChatPanel {
1386
1421
  close() {
1387
1422
  this._hideProviderDropdown();
1388
1423
  this._hideSessionDropdown();
1424
+ this._hideSnippetDropdown();
1425
+ this._hideSaveSnippetPill();
1389
1426
  // Reset UI streaming state (buttons) but keep isStreaming and _streamingContent
1390
1427
  // intact so the background WebSocket handler can continue accumulating events.
1391
1428
  this.sendBtn.style.display = '';
@@ -1432,6 +1469,7 @@ class ChatPanel {
1432
1469
  async _startNewConversation() {
1433
1470
  this._hideProviderDropdown();
1434
1471
  this._hideSessionDropdown();
1472
+ this._hideSnippetDropdown();
1435
1473
  // Multi-chat replaces the legacy in-place reset with a fresh tab; the
1436
1474
  // per-tab restore path inside _appendTab handles context cards
1437
1475
  // (including thread cards introduced by external-comments) so the
@@ -1702,8 +1740,9 @@ class ChatPanel {
1702
1740
 
1703
1741
  _showProviderDropdown() {
1704
1742
  if (!this.providerDropdown) return;
1705
- // Close session dropdown if open
1743
+ // Close session + snippet dropdowns if open
1706
1744
  this._hideSessionDropdown();
1745
+ this._hideSnippetDropdown();
1707
1746
 
1708
1747
  this._renderProviderDropdown();
1709
1748
  this.providerDropdown.style.display = '';
@@ -1820,6 +1859,308 @@ class ChatPanel {
1820
1859
  this._renderTabStrip();
1821
1860
  }
1822
1861
 
1862
+ // ── Prompt-snippet picker dropdown ─────────────────────────────────────
1863
+ // NOTE: "snippet" here means a reusable *chat prompt* the user inserts from
1864
+ // the library. This is distinct from the code-snippet enrichment (_fetch...
1865
+ // Snippet / _stripMarkdownForSnippet) used to give the agent file context.
1866
+
1867
+ _isSnippetDropdownOpen() {
1868
+ return this.snippetDropdown && this.snippetDropdown.style.display !== 'none';
1869
+ }
1870
+
1871
+ _toggleSnippetDropdown() {
1872
+ if (this._isSnippetDropdownOpen()) {
1873
+ this._hideSnippetDropdown();
1874
+ } else {
1875
+ this._showSnippetDropdown();
1876
+ }
1877
+ }
1878
+
1879
+ async _showSnippetDropdown() {
1880
+ if (!this.snippetDropdown) return;
1881
+ // Close the sibling dropdowns — exclusion is pairwise/hand-maintained.
1882
+ this._hideProviderDropdown();
1883
+ this._hideSessionDropdown();
1884
+
1885
+ // In-flight guard so a double-click doesn't fire two overlapping fetches.
1886
+ if (this._snippetDropdownLoading) return;
1887
+ this._snippetDropdownLoading = true;
1888
+
1889
+ let snippets = [];
1890
+ try {
1891
+ const res = await fetch('/api/snippets');
1892
+ if (res.ok) {
1893
+ const data = await res.json();
1894
+ snippets = Array.isArray(data?.snippets) ? data.snippets : [];
1895
+ }
1896
+ } catch {
1897
+ snippets = [];
1898
+ } finally {
1899
+ this._snippetDropdownLoading = false;
1900
+ }
1901
+
1902
+ // The panel may have closed, or the dropdown been dismissed, while the
1903
+ // fetch was in flight. Bail rather than pop open a stale list.
1904
+ if (this._snippetDropdownDismissed) {
1905
+ this._snippetDropdownDismissed = false;
1906
+ return;
1907
+ }
1908
+ if (!this.isOpen || !this.snippetDropdown) return;
1909
+
1910
+ this._renderSnippetDropdown(snippets);
1911
+ this.snippetDropdown.style.display = '';
1912
+ this.snippetPickerBtn?.classList.add('chat-panel__snippet-picker-btn--open');
1913
+
1914
+ // Bind outside-click-to-close (one-shot). Guard the timeout callback so a
1915
+ // fast hide before the tick doesn't leak the document listener.
1916
+ this._snippetOutsideClickHandler = (e) => {
1917
+ if (this.snippetPickerEl && !this.snippetPickerEl.contains(e.target)) {
1918
+ this._hideSnippetDropdown();
1919
+ }
1920
+ };
1921
+ setTimeout(() => {
1922
+ // Only attach if the dropdown is still open (ref not nulled by a hide
1923
+ // that ran between scheduling and now).
1924
+ if (this._snippetOutsideClickHandler && this._isSnippetDropdownOpen()) {
1925
+ document.addEventListener('click', this._snippetOutsideClickHandler);
1926
+ } else {
1927
+ this._snippetOutsideClickHandler = null;
1928
+ }
1929
+ }, 0);
1930
+ }
1931
+
1932
+ _hideSnippetDropdown() {
1933
+ if (!this.snippetDropdown) return;
1934
+ // If a fetch is in flight, mark it dismissed so its post-await show bails.
1935
+ if (this._snippetDropdownLoading) this._snippetDropdownDismissed = true;
1936
+ this.snippetDropdown.style.display = 'none';
1937
+ this.snippetPickerBtn?.classList.remove('chat-panel__snippet-picker-btn--open');
1938
+ if (this._snippetOutsideClickHandler) {
1939
+ document.removeEventListener('click', this._snippetOutsideClickHandler);
1940
+ this._snippetOutsideClickHandler = null;
1941
+ }
1942
+ }
1943
+
1944
+ _renderSnippetDropdown(snippets) {
1945
+ if (!this.snippetDropdown) return;
1946
+
1947
+ // Full bodies live in a Map keyed by id — never in data-attributes (avoids
1948
+ // re-escaping large multi-line bodies through the DOM).
1949
+ this._promptSnippetsById = new Map();
1950
+
1951
+ const header = `
1952
+ <div class="chat-panel__snippet-header">
1953
+ <span class="chat-panel__snippet-header-title">Snippets</span>
1954
+ <button class="chat-panel__snippet-manage-btn" type="button" title="Manage snippets" aria-label="Manage snippets">
1955
+ <svg viewBox="0 0 16 16" fill="currentColor" width="12" height="12">
1956
+ <path d="M8 0a8.2 8.2 0 0 1 .701.031C9.444.095 9.99.645 10.16 1.29l.288 1.107c.018.066.079.158.212.224.231.114.454.243.668.386.123.082.233.09.299.071l1.103-.303c.644-.176 1.392.021 1.813.63.183.264.352.539.506.822.294.542.188 1.219-.226 1.6l-.796.796c-.058.06-.079.161-.048.281.037.146.061.298.061.451s-.024.305-.061.451c-.031.12-.01.221.048.281l.796.796c.414.381.52 1.058.226 1.6a7.912 7.912 0 0 1-.506.822c-.421.609-1.169.806-1.813.63l-1.103-.303c-.066-.019-.176-.011-.299.071a4.759 4.759 0 0 1-.668.386c-.133.066-.194.158-.212.224l-.288 1.107c-.17.645-.716 1.195-1.459 1.259a8.174 8.174 0 0 1-1.402 0c-.743-.064-1.289-.614-1.459-1.259l-.288-1.107c-.018-.066-.079-.158-.212-.224a4.759 4.759 0 0 1-.668-.386c-.123-.082-.233-.09-.299-.071l-1.103.303c-.644.176-1.392-.021-1.813-.63a7.884 7.884 0 0 1-.506-.822c-.294-.542-.188-1.219.226-1.6l.796-.796c.058-.06.079-.161.048-.281A1.85 1.85 0 0 1 1.3 8c0-.153.024-.305.061-.451.031-.12.01-.221-.048-.281l-.796-.796c-.414-.381-.52-1.058-.226-1.6.154-.283.323-.558.506-.822.421-.609 1.169-.806 1.813-.63l1.103.303c.066.019.176.011.299-.071.214-.143.437-.272.668-.386.133-.066.194-.158.212-.224l.288-1.107C6.01.645 6.556.095 7.299.03 7.53.01 7.764 0 8 0Zm0 5a3 3 0 1 0 0 6 3 3 0 0 0 0-6Z"/>
1957
+ </svg>
1958
+ </button>
1959
+ </div>
1960
+ `;
1961
+
1962
+ if (!snippets.length) {
1963
+ this.snippetDropdown.innerHTML = `
1964
+ ${header}
1965
+ <div class="chat-panel__snippet-empty">
1966
+ <span>No snippets yet</span>
1967
+ <button class="chat-panel__snippet-manage-empty-btn" type="button">Manage snippets</button>
1968
+ </div>
1969
+ `;
1970
+ } else {
1971
+ const items = snippets.map(s => {
1972
+ this._promptSnippetsById.set(String(s.id), s.body);
1973
+ const firstLine = String(s.body || '').split('\n')[0];
1974
+ const preview = firstLine.length > 60
1975
+ ? firstLine.slice(0, 60) + '…'
1976
+ : firstLine;
1977
+ return `
1978
+ <button class="chat-panel__snippet-item" type="button" data-snippet-id="${this._escapeHtml(String(s.id))}">
1979
+ <span class="chat-panel__snippet-item-preview">${this._escapeHtml(preview)}</span>
1980
+ </button>
1981
+ `;
1982
+ }).join('');
1983
+ this.snippetDropdown.innerHTML = header + `<div class="chat-panel__snippet-list">${items}</div>`;
1984
+ }
1985
+
1986
+ // Manage (gear + empty-state button): open the shared editor modal.
1987
+ const openManager = () => {
1988
+ this._hideSnippetDropdown();
1989
+ if (typeof SnippetManager !== 'undefined') {
1990
+ SnippetManager.openModal({ onClose: () => {} });
1991
+ }
1992
+ };
1993
+ this.snippetDropdown.querySelector('.chat-panel__snippet-manage-btn')
1994
+ ?.addEventListener('click', openManager);
1995
+ this.snippetDropdown.querySelector('.chat-panel__snippet-manage-empty-btn')
1996
+ ?.addEventListener('click', openManager);
1997
+
1998
+ // Item click: insert (cmd/ctrl-click also sends), then hide.
1999
+ this.snippetDropdown.querySelectorAll('.chat-panel__snippet-item').forEach(btn => {
2000
+ btn.addEventListener('click', (e) => {
2001
+ this._insertPromptSnippet(btn.dataset.snippetId, { send: e.metaKey || e.ctrlKey });
2002
+ this._hideSnippetDropdown();
2003
+ });
2004
+ });
2005
+ }
2006
+
2007
+ /**
2008
+ * Insert a prompt snippet's body into the input at the caret. A programmatic
2009
+ * value change bypasses the `input` listener (952–955), so we replicate its
2010
+ * effects: autosize + recompute sendBtn.disabled. MRU touch is fire-and-
2011
+ * forget (never awaited). With `send`, hands off to sendMessage() whose own
2012
+ * guards cover empty/streaming — we do not duplicate them.
2013
+ * @param {string|number} id - Snippet id
2014
+ * @param {{ send?: boolean }} [opts]
2015
+ */
2016
+ _insertPromptSnippet(id, { send = false } = {}) {
2017
+ if (!this.inputEl) return;
2018
+ // Respect the "Connecting to review…" disabled contract (_disableInput):
2019
+ // don't mutate a disabled textarea, and don't let cmd-click reach
2020
+ // sendMessage() (which has no disabled-input guard of its own).
2021
+ if (this.inputEl.disabled) return;
2022
+ const body = this._promptSnippetsById?.get(String(id));
2023
+ if (body == null) return;
2024
+
2025
+ const el = this.inputEl;
2026
+ const value = el.value || '';
2027
+ let start = typeof el.selectionStart === 'number' ? el.selectionStart : value.length;
2028
+ let end = typeof el.selectionEnd === 'number' ? el.selectionEnd : value.length;
2029
+ if (start > value.length) start = value.length;
2030
+ if (end > value.length) end = value.length;
2031
+
2032
+ // Prefix a newline when inserting directly after non-whitespace text so
2033
+ // the snippet doesn't fuse onto the previous word.
2034
+ const prevChar = start > 0 ? value[start - 1] : '';
2035
+ const prefix = prevChar && !/\s/.test(prevChar) ? '\n' : '';
2036
+ const insertText = prefix + body;
2037
+
2038
+ el.value = value.slice(0, start) + insertText + value.slice(end);
2039
+ const caret = start + insertText.length;
2040
+ if (typeof el.setSelectionRange === 'function') {
2041
+ el.setSelectionRange(caret, caret);
2042
+ } else {
2043
+ el.selectionStart = el.selectionEnd = caret;
2044
+ }
2045
+ el.focus();
2046
+
2047
+ // Replicate the input-listener side effects the programmatic change skips.
2048
+ this._autoResizeTextarea();
2049
+ this.sendBtn.disabled = !el.value.trim() || this.isStreaming || el.disabled;
2050
+
2051
+ // MRU touch — fire-and-forget, NEVER awaited, must not block insert.
2052
+ fetch(`/api/snippets/${id}/touch`, { method: 'POST' }).catch(() => {});
2053
+
2054
+ // Cmd/Ctrl-click: send. sendMessage() owns the empty/streaming guards.
2055
+ if (send) this.sendMessage();
2056
+ }
2057
+
2058
+ // ── Save-as-snippet pill (alt-click a user message) ────────────────────
2059
+ // Distinct from the prompt-snippet *picker* above: this is the reverse
2060
+ // direction — capturing a message the user already sent into the library.
2061
+
2062
+ /**
2063
+ * Show the single "Save as snippet" pill anchored near the alt-click point.
2064
+ * Only one pill exists at a time; alt-clicking another message moves it.
2065
+ * @param {HTMLElement} msgEl - The `.chat-panel__message--user` element
2066
+ * @param {MouseEvent} e - The originating alt-click (for positioning)
2067
+ */
2068
+ _showSaveSnippetPill(msgEl, e) {
2069
+ this._hideSaveSnippetPill();
2070
+
2071
+ // Raw body stashed by addMessage — preserves newlines/formatting. Fall
2072
+ // back to the bubble text only if the property is somehow missing.
2073
+ const body = (msgEl && msgEl._chatRawContent != null)
2074
+ ? msgEl._chatRawContent
2075
+ : (msgEl?.querySelector?.('.chat-panel__bubble')?.textContent || '');
2076
+ if (!body) return;
2077
+ this._saveSnippetPillContent = body;
2078
+
2079
+ const pill = document.createElement('button');
2080
+ pill.type = 'button';
2081
+ pill.className = 'chat-panel__save-snippet-pill';
2082
+ pill.textContent = 'Save as snippet';
2083
+ // Fixed positioning at the click point sidesteps the scrolling messages
2084
+ // container's overflow clipping; the pill is ephemeral (dismissed on any
2085
+ // scroll-independent interaction) so it needn't track content.
2086
+ pill.style.position = 'fixed';
2087
+ pill.style.left = `${e.clientX}px`;
2088
+ pill.style.top = `${e.clientY}px`;
2089
+ pill.style.zIndex = '200';
2090
+ pill.addEventListener('click', (ev) => {
2091
+ ev.stopPropagation();
2092
+ this._saveSnippetFromPill();
2093
+ });
2094
+ document.body.appendChild(pill);
2095
+ this._saveSnippetPill = pill;
2096
+
2097
+ // Outside-click dismiss (one-shot). Guard the timeout callback so a fast
2098
+ // dismiss before the tick doesn't leak the document listener.
2099
+ this._saveSnippetOutsideClickHandler = (ev) => {
2100
+ if (this._saveSnippetPill && ev.target !== this._saveSnippetPill) {
2101
+ this._hideSaveSnippetPill();
2102
+ }
2103
+ };
2104
+ setTimeout(() => {
2105
+ if (this._saveSnippetOutsideClickHandler && this._saveSnippetPill) {
2106
+ document.addEventListener('click', this._saveSnippetOutsideClickHandler);
2107
+ } else {
2108
+ this._saveSnippetOutsideClickHandler = null;
2109
+ }
2110
+ }, 0);
2111
+ }
2112
+
2113
+ _hideSaveSnippetPill() {
2114
+ if (this._saveSnippetOutsideClickHandler) {
2115
+ document.removeEventListener('click', this._saveSnippetOutsideClickHandler);
2116
+ this._saveSnippetOutsideClickHandler = null;
2117
+ }
2118
+ if (this._saveSnippetPill) {
2119
+ if (this._saveSnippetPill.parentNode) {
2120
+ this._saveSnippetPill.parentNode.removeChild(this._saveSnippetPill);
2121
+ } else if (typeof this._saveSnippetPill.remove === 'function') {
2122
+ this._saveSnippetPill.remove();
2123
+ }
2124
+ this._saveSnippetPill = null;
2125
+ }
2126
+ this._saveSnippetPillContent = null;
2127
+ }
2128
+
2129
+ /**
2130
+ * Persist the pilled message body as a snippet. Fire-and-forget from the
2131
+ * click handler; never throws (POST failure degrades to a brief "Failed"
2132
+ * label, then the pill dismisses).
2133
+ */
2134
+ async _saveSnippetFromPill() {
2135
+ const pill = this._saveSnippetPill;
2136
+ const body = this._saveSnippetPillContent;
2137
+ if (!pill || body == null) return;
2138
+ pill.disabled = true;
2139
+ try {
2140
+ const res = await fetch('/api/snippets', {
2141
+ method: 'POST',
2142
+ headers: { 'Content-Type': 'application/json' },
2143
+ body: JSON.stringify({ body }),
2144
+ });
2145
+ if (!res.ok) throw new Error(`save snippet failed: ${res.status}`);
2146
+ this._hideSaveSnippetPill();
2147
+ if (window.toast?.showSuccess) {
2148
+ window.toast.showSuccess('Saved as snippet');
2149
+ } else if (window.showToast) {
2150
+ window.showToast('Saved as snippet', 'success');
2151
+ }
2152
+ } catch {
2153
+ // Keep quiet but visible: label the pill, then dismiss it. Guard against
2154
+ // a pill that was already replaced/dismissed while the POST was inflight.
2155
+ if (this._saveSnippetPill === pill) {
2156
+ pill.textContent = 'Failed';
2157
+ setTimeout(() => {
2158
+ if (this._saveSnippetPill === pill) this._hideSaveSnippetPill();
2159
+ }, 1200);
2160
+ }
2161
+ }
2162
+ }
2163
+
1823
2164
  // ── Session picker dropdown ────────────────────────────────────────────
1824
2165
 
1825
2166
  _isSessionDropdownOpen() {
@@ -1836,8 +2177,9 @@ class ChatPanel {
1836
2177
 
1837
2178
  async _showSessionDropdown() {
1838
2179
  if (!this.sessionDropdown) return;
1839
- // Close provider dropdown if open
2180
+ // Close provider + snippet dropdowns if open
1840
2181
  this._hideProviderDropdown();
2182
+ this._hideSnippetDropdown();
1841
2183
 
1842
2184
  const sessions = await this._fetchSessions();
1843
2185
  this._renderSessionDropdown(sessions);
@@ -2559,6 +2901,7 @@ class ChatPanel {
2559
2901
  side: ctx.side || null,
2560
2902
  line_start: ctx.line_start || null,
2561
2903
  line_end: ctx.line_end || null,
2904
+ side: ctx.side || 'RIGHT',
2562
2905
  body: ctx.body || null,
2563
2906
  source: isExternal ? 'external' : 'user'
2564
2907
  };
@@ -3961,6 +4304,11 @@ class ChatPanel {
3961
4304
  const msgEl = document.createElement('div');
3962
4305
  msgEl.className = `chat-panel__message chat-panel__message--${role}`;
3963
4306
  if (id) msgEl.dataset.messageId = id;
4307
+ // Stash the raw content on the element (not an attribute) so the
4308
+ // alt-click "Save as snippet" affordance can persist the exact body —
4309
+ // newlines/formatting intact — rather than the rendered bubble's
4310
+ // textContent. User messages only; assistant/error aren't savable.
4311
+ if (role === 'user') msgEl._chatRawContent = content;
3964
4312
 
3965
4313
  const bubble = document.createElement('div');
3966
4314
  bubble.className = 'chat-panel__bubble';
@@ -4583,9 +4931,11 @@ class ChatPanel {
4583
4931
  * @param {string} file - File path
4584
4932
  * @param {number} lineStart - Target line number (start of range)
4585
4933
  * @param {number|null} [lineEnd] - End of target line range (used for expansion)
4934
+ * @param {('LEFT'|'RIGHT')} [side='RIGHT'] - Diff side the line lives on.
4935
+ * Chat file-link references point at the new file, so RIGHT is the default.
4586
4936
  * @param {HTMLElement} [fileWrapper] - Pre-resolved file wrapper element
4587
4937
  */
4588
- async _scrollToLine(file, lineStart, lineEnd, fileWrapper) {
4938
+ async _scrollToLine(file, lineStart, lineEnd, side = 'RIGHT', fileWrapper) {
4589
4939
  if (!fileWrapper) {
4590
4940
  const escaped = CSS.escape(file);
4591
4941
  fileWrapper = document.querySelector(`[data-file-name="${escaped}"]`) ||
@@ -4593,6 +4943,32 @@ class ChatPanel {
4593
4943
  }
4594
4944
  if (!fileWrapper) return;
4595
4945
 
4946
+ // Pierre-rendered files live inside shadow DOM; light-DOM row queries
4947
+ // return nothing. Delegate to PierreBridge, which reaches into the shadow
4948
+ // root, scrolls to the target line, and applies its own highlight flash.
4949
+ const bridge = window.prManager?.pierreBridge;
4950
+ if (bridge && bridge.files.has(file)) {
4951
+ const end = lineEnd || lineStart;
4952
+ // Ensure the line is visible — expand collapsed gaps if needed.
4953
+ if (!bridge.isLineVisible(file, lineStart, side)
4954
+ && window.prManager?.ensureLinesVisible) {
4955
+ await window.prManager.ensureLinesVisible([
4956
+ { file, line_start: lineStart, line_end: end, side }
4957
+ ]);
4958
+ }
4959
+ // Flash each line in the requested range.
4960
+ let scrolled = false;
4961
+ for (let ln = lineStart; ln <= end; ln++) {
4962
+ // Only scroll into view for the first line; highlight the rest.
4963
+ const ok = bridge.scrollToLine(file, ln, side, ln === lineStart);
4964
+ scrolled = scrolled || ok;
4965
+ }
4966
+ if (!scrolled && window.prManager?.scrollToFile) {
4967
+ window.prManager.scrollToFile(file);
4968
+ }
4969
+ return;
4970
+ }
4971
+
4596
4972
  // Collect all target rows (single line or range)
4597
4973
  const end = lineEnd || lineStart;
4598
4974
  let targetRows = this._findLineRows(fileWrapper, lineStart, end);
@@ -4600,8 +4976,20 @@ class ChatPanel {
4600
4976
  // If not found, try expanding the collapsed diff context
4601
4977
  if (targetRows.length === 0 && window.prManager?.ensureLinesVisible) {
4602
4978
  await window.prManager.ensureLinesVisible([
4603
- { file, line_start: lineStart, line_end: end, side: 'RIGHT' }
4979
+ { file, line_start: lineStart, line_end: end, side }
4604
4980
  ]);
4981
+ const updatedBridge = window.prManager?.pierreBridge;
4982
+ if (updatedBridge?.files?.has(file)) {
4983
+ let scrolled = false;
4984
+ for (let ln = lineStart; ln <= end; ln++) {
4985
+ const ok = updatedBridge.scrollToLine(file, ln, side, ln === lineStart);
4986
+ scrolled = scrolled || ok;
4987
+ }
4988
+ if (!scrolled && window.prManager?.scrollToFile) {
4989
+ window.prManager.scrollToFile(file);
4990
+ }
4991
+ return;
4992
+ }
4605
4993
  targetRows = this._findLineRows(fileWrapper, lineStart, end);
4606
4994
  }
4607
4995
  if (targetRows.length === 0) return;
@@ -4869,6 +5257,7 @@ class ChatPanel {
4869
5257
  file: tab.contextLineMeta?.file,
4870
5258
  line_start: tab.contextLineMeta?.line_start,
4871
5259
  line_end: tab.contextLineMeta?.line_end,
5260
+ side: tab.contextLineMeta?.side || 'RIGHT',
4872
5261
  };
4873
5262
  this.inputEl.value = 'Based on our conversation, please create a review comment for this code.';
4874
5263
  this.sendMessage();
@@ -4953,6 +5342,13 @@ class ChatPanel {
4953
5342
  destroy() {
4954
5343
  document.removeEventListener('keydown', this._onKeydown);
4955
5344
  window.removeEventListener('chat-state-changed', this._onChatStateChanged);
5345
+ // Remove any lingering outside-click document listeners before the
5346
+ // container is cleared (each open dropdown holds a one-shot handler;
5347
+ // these hide methods safely no-op when their dropdown is already closed).
5348
+ this._hideProviderDropdown();
5349
+ this._hideSessionDropdown();
5350
+ this._hideSnippetDropdown();
5351
+ this._hideSaveSnippetPill();
4956
5352
  this._closeSubscriptions();
4957
5353
  this.tabs = [];
4958
5354
  this.activeTabKey = null;