@in-the-loop-labs/pair-review 5.4.0 → 5.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@in-the-loop-labs/pair-review",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "description": "Your AI-powered code review partner - Close the feedback loop with AI coding agents",
5
5
  "main": "src/server.js",
6
6
  "bin": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pair-review",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "description": "pair-review app integration — Open PRs and local changes in the pair-review web UI, run server-side AI analysis, and address review feedback. Requires the pair-review MCP server.",
5
5
  "author": {
6
6
  "name": "in-the-loop-labs",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-critic",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "description": "AI-powered code review analysis — Run three-level AI analysis directly in your coding agent. Works standalone, no server required.",
5
5
  "author": {
6
6
  "name": "in-the-loop-labs",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pair-loop",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "description": "Agent-orchestrated review loop — implement, review with pair-review multi-model councils via the headless CLI, triage, fix, and repeat until a final review returns no blockers. MCP-independent.",
5
5
  "author": {
6
6
  "name": "in-the-loop-labs",
package/public/index.html CHANGED
@@ -1571,6 +1571,7 @@
1571
1571
  <script src="/js/components/TextInputDialog.js"></script>
1572
1572
  <script src="/js/components/TimeoutSelect.js"></script>
1573
1573
  <script src="/js/components/AnalysisConfigModal.js"></script>
1574
+ <script src="/js/utils/council-timeout.js"></script>
1574
1575
  <script src="/js/components/VoiceCentricConfigTab.js"></script>
1575
1576
  <script src="/js/components/AdvancedConfigTab.js"></script>
1576
1577
  <script src="/js/theme.js"></script>
@@ -44,6 +44,17 @@ class AdvancedConfigTab {
44
44
  /** Default timeout in milliseconds (10 minutes) */
45
45
  static DEFAULT_TIMEOUT = 600000;
46
46
 
47
+ /** DOM shape for the shared timeout mixin (see utils/council-timeout.js) */
48
+ static TIMEOUT_DOM = {
49
+ panelId: 'tab-panel-advanced',
50
+ voiceWrapperClass: 'participant-wrapper',
51
+ voiceKeys: ['level', 'index'],
52
+ timeoutClass: 'adv-timeout',
53
+ orchestrationTimeoutId: 'adv-orchestration-timeout',
54
+ orchestrationToggleId: 'adv-orchestration-timeout-toggle',
55
+ orchestrationRowId: 'orchestration-voice',
56
+ };
57
+
47
58
 
48
59
  constructor(modal) {
49
60
  this.modal = modal;
@@ -669,7 +680,7 @@ class AdvancedConfigTab {
669
680
  this._updateOrchestrationTimeoutIcon(panel, e.target.value);
670
681
  } else {
671
682
  const { level, index } = e.target.dataset;
672
- this._updateTimeoutIcon(panel, level, index, e.target.value);
683
+ this._updateTimeoutIcon(panel, { level, index }, e.target.value);
673
684
  }
674
685
  }
675
686
  });
@@ -946,45 +957,6 @@ class AdvancedConfigTab {
946
957
  iconBtn.classList.toggle('has-instructions', hasContent);
947
958
  }
948
959
 
949
- /**
950
- * Get the default timeout for a provider, falling back to the static DEFAULT_TIMEOUT.
951
- * @param {string} providerId - Provider ID (e.g., 'pi', 'claude')
952
- * @returns {number} Default timeout in ms
953
- */
954
- _getProviderDefaultTimeout(providerId) {
955
- const provider = this.providers[providerId];
956
- return provider?.defaultTimeout ?? AdvancedConfigTab.DEFAULT_TIMEOUT;
957
- }
958
-
959
- /**
960
- * Update the clock/timeout icon styling to indicate non-default timeout.
961
- * @param {Element} panel - The council panel element
962
- * @param {string} level - Level number
963
- * @param {string} index - Voice index
964
- * @param {string} value - Current timeout value (as string of ms)
965
- */
966
- _updateTimeoutIcon(panel, level, index, value) {
967
- const wrapper = panel.querySelector(`.participant-wrapper[data-level="${level}"][data-index="${index}"]`);
968
- const iconBtn = wrapper?.querySelector(`.toggle-timeout-icon[data-level="${level}"][data-index="${index}"]`);
969
- if (!iconBtn) return;
970
-
971
- const providerId = wrapper?.querySelector('.voice-provider')?.value;
972
- const defaultTimeout = this._getProviderDefaultTimeout(providerId);
973
- const isNonDefault = parseInt(value, 10) !== defaultTimeout;
974
- iconBtn.classList.toggle('has-custom-timeout', isNonDefault);
975
- }
976
-
977
- _updateOrchestrationTimeoutIcon(panel, value) {
978
- const iconBtn = panel.querySelector('#adv-orchestration-timeout-toggle');
979
- if (!iconBtn) return;
980
-
981
- const orchRow = panel.querySelector('#orchestration-voice');
982
- const providerId = orchRow?.querySelector('.voice-provider')?.value;
983
- const defaultTimeout = this._getProviderDefaultTimeout(providerId);
984
- const isNonDefault = parseInt(value, 10) !== defaultTimeout;
985
- iconBtn.classList.toggle('has-custom-timeout', isNonDefault);
986
- }
987
-
988
960
  _updateOrchestrationInstructionsIcon(panel, value) {
989
961
  const iconBtn = panel.querySelector('#adv-orchestration-instructions-toggle');
990
962
  if (!iconBtn) return;
@@ -996,48 +968,6 @@ class AdvancedConfigTab {
996
968
  iconBtn.classList.toggle('has-instructions', hasContent);
997
969
  }
998
970
 
999
- /**
1000
- * When a voice's provider changes, update its timeout to the new provider's default,
1001
- * preserving explicit user overrides via Math.max when the user had customized the value.
1002
- * @param {HTMLSelectElement} providerSelect - The provider dropdown that changed
1003
- */
1004
- _applyProviderDefaultTimeout(providerSelect) {
1005
- const panel = this.modal.querySelector('#tab-panel-advanced');
1006
- if (!panel) return;
1007
-
1008
- const providerId = providerSelect.value;
1009
- const newDefault = this._getProviderDefaultTimeout(providerId);
1010
- const oldProviderId = providerSelect.dataset.previousProvider;
1011
- const oldDefault = oldProviderId ? this._getProviderDefaultTimeout(oldProviderId) : null;
1012
-
1013
- const isOrchestration = providerSelect.dataset.target === 'orchestration';
1014
- if (isOrchestration) {
1015
- const timeoutEl = panel.querySelector('#adv-orchestration-timeout');
1016
- if (timeoutEl) {
1017
- const currentValue = parseInt(timeoutEl.value, 10);
1018
- const resolvedTimeout = (oldDefault !== null && currentValue !== oldDefault)
1019
- ? Math.max(currentValue, newDefault)
1020
- : newDefault;
1021
- timeoutEl.value = String(resolvedTimeout);
1022
- this._updateOrchestrationTimeoutIcon(panel, String(resolvedTimeout));
1023
- }
1024
- } else {
1025
- const { level, index } = providerSelect.dataset;
1026
- const wrapper = providerSelect.closest('.participant-wrapper');
1027
- const timeoutEl = wrapper?.querySelector('.adv-timeout');
1028
- if (timeoutEl) {
1029
- const currentValue = parseInt(timeoutEl.value, 10);
1030
- const resolvedTimeout = (oldDefault !== null && currentValue !== oldDefault)
1031
- ? Math.max(currentValue, newDefault)
1032
- : newDefault;
1033
- timeoutEl.value = String(resolvedTimeout);
1034
- this._updateTimeoutIcon(panel, level, index, String(resolvedTimeout));
1035
- }
1036
- }
1037
-
1038
- providerSelect.dataset.previousProvider = providerId;
1039
- }
1040
-
1041
971
  // --- Dirty state tracking ---
1042
972
 
1043
973
  _markDirty() {
@@ -1231,18 +1161,7 @@ class AdvancedConfigTab {
1231
1161
  TimeoutSelect.mount(mount, { className: 'adv-timeout', title: 'Per-reviewer timeout' });
1232
1162
  }
1233
1163
  const timeoutEl = row?.querySelector('.adv-timeout');
1234
- const providerDefaultTimeout = this._getProviderDefaultTimeout(voice.provider);
1235
- if (timeoutEl && voice.timeout) {
1236
- timeoutEl.value = String(voice.timeout);
1237
- // Show the dropdown if non-default for this provider
1238
- if (voice.timeout !== providerDefaultTimeout) {
1239
- timeoutEl.style.display = '';
1240
- }
1241
- this._updateTimeoutIcon(panel, String(level), String(i), String(voice.timeout));
1242
- } else if (timeoutEl) {
1243
- // No saved timeout — apply the provider's default
1244
- timeoutEl.value = String(providerDefaultTimeout);
1245
- }
1164
+ this._restoreVoiceTimeout(panel, { level: String(level), index: String(i) }, timeoutEl, voice);
1246
1165
 
1247
1166
  if (voice.customInstructions) {
1248
1167
  const instrInput = participantWrapper?.querySelector(`.voice-instructions-input[data-level="${level}"][data-index="${i}"]`);
@@ -1280,20 +1199,7 @@ class AdvancedConfigTab {
1280
1199
  }
1281
1200
 
1282
1201
  // Restore consolidation timeout
1283
- const orchTimeoutSelect = panel.querySelector('#adv-orchestration-timeout');
1284
- const orchProviderDefaultTimeout = this._getProviderDefaultTimeout(consolSection.provider);
1285
- if (orchTimeoutSelect && consolSection.timeout) {
1286
- orchTimeoutSelect.value = String(consolSection.timeout);
1287
- // Show the dropdown if non-default for this provider
1288
- if (consolSection.timeout !== orchProviderDefaultTimeout) {
1289
- orchTimeoutSelect.style.display = '';
1290
- }
1291
- this._updateOrchestrationTimeoutIcon(panel, String(consolSection.timeout));
1292
- } else if (orchTimeoutSelect) {
1293
- // No saved timeout — apply the provider's default
1294
- orchTimeoutSelect.value = String(orchProviderDefaultTimeout);
1295
- this._updateOrchestrationTimeoutIcon(panel, String(orchProviderDefaultTimeout));
1296
- }
1202
+ this._restoreOrchestrationTimeout(panel, consolSection);
1297
1203
 
1298
1204
  // Restore consolidation custom instructions
1299
1205
  const orchInstrInput = panel.querySelector('#adv-orchestration-instructions');
@@ -1475,6 +1381,10 @@ class AdvancedConfigTab {
1475
1381
  }
1476
1382
  }
1477
1383
 
1384
+ // Shared timeout methods (public/js/utils/council-timeout.js). In the browser the
1385
+ // util's <script> tag loads first; unit tests load it via require().
1386
+ ((typeof window !== 'undefined' && window.CouncilTimeout) || require('../utils/council-timeout.js')).mixInto(AdvancedConfigTab);
1387
+
1478
1388
  // Export for use in other modules
1479
1389
  if (typeof window !== 'undefined') {
1480
1390
  window.AdvancedConfigTab = AdvancedConfigTab;
@@ -41,6 +41,17 @@ class VoiceCentricConfigTab {
41
41
  /** Default timeout in milliseconds (10 minutes) */
42
42
  static DEFAULT_TIMEOUT = 600000;
43
43
 
44
+ /** DOM shape for the shared timeout mixin (see utils/council-timeout.js) */
45
+ static TIMEOUT_DOM = {
46
+ panelId: 'tab-panel-council',
47
+ voiceWrapperClass: 'vc-reviewer',
48
+ voiceKeys: ['index'],
49
+ timeoutClass: 'vc-timeout',
50
+ orchestrationTimeoutId: 'vc-orchestration-timeout',
51
+ orchestrationToggleId: 'vc-orchestration-timeout-toggle',
52
+ orchestrationRowId: 'vc-orchestration-voice',
53
+ };
54
+
44
55
 
45
56
  constructor(modal) {
46
57
  this.modal = modal;
@@ -574,7 +585,7 @@ class VoiceCentricConfigTab {
574
585
  this._updateOrchestrationTimeoutIcon(panel, e.target.value);
575
586
  } else {
576
587
  const idx = e.target.dataset.index;
577
- this._updateTimeoutIcon(panel, idx, e.target.value);
588
+ this._updateTimeoutIcon(panel, { index: idx }, e.target.value);
578
589
  }
579
590
  }
580
591
  });
@@ -707,87 +718,6 @@ class VoiceCentricConfigTab {
707
718
  iconBtn.classList.toggle('has-instructions', hasContent);
708
719
  }
709
720
 
710
- /**
711
- * Get the default timeout for a provider, falling back to the static DEFAULT_TIMEOUT.
712
- * @param {string} providerId - Provider ID (e.g., 'pi', 'claude')
713
- * @returns {number} Default timeout in ms
714
- */
715
- _getProviderDefaultTimeout(providerId) {
716
- const provider = this.providers[providerId];
717
- return provider?.defaultTimeout ?? VoiceCentricConfigTab.DEFAULT_TIMEOUT;
718
- }
719
-
720
- /**
721
- * Update the clock/timeout icon styling to indicate non-default timeout.
722
- * @param {Element} panel - The council panel element
723
- * @param {string} index - Reviewer index
724
- * @param {string} value - Current timeout value (as string of ms)
725
- */
726
- _updateTimeoutIcon(panel, index, value) {
727
- const wrapper = panel.querySelector(`.vc-reviewer[data-index="${index}"]`);
728
- const iconBtn = wrapper?.querySelector(`.toggle-timeout-icon[data-index="${index}"]`);
729
- if (!iconBtn) return;
730
-
731
- const providerId = wrapper?.querySelector('.voice-provider')?.value;
732
- const defaultTimeout = this._getProviderDefaultTimeout(providerId);
733
- const isNonDefault = parseInt(value, 10) !== defaultTimeout;
734
- iconBtn.classList.toggle('has-custom-timeout', isNonDefault);
735
- }
736
-
737
- _updateOrchestrationTimeoutIcon(panel, value) {
738
- const iconBtn = panel.querySelector('#vc-orchestration-timeout-toggle');
739
- if (!iconBtn) return;
740
-
741
- const orchRow = panel.querySelector('#vc-orchestration-voice');
742
- const providerId = orchRow?.querySelector('.voice-provider')?.value;
743
- const defaultTimeout = this._getProviderDefaultTimeout(providerId);
744
- const isNonDefault = parseInt(value, 10) !== defaultTimeout;
745
- iconBtn.classList.toggle('has-custom-timeout', isNonDefault);
746
- }
747
-
748
- /**
749
- * When a voice's provider changes, update its timeout to the new provider's default,
750
- * preserving explicit user overrides via Math.max when the user had customized the value.
751
- * @param {HTMLSelectElement} providerSelect - The provider dropdown that changed
752
- */
753
- _applyProviderDefaultTimeout(providerSelect) {
754
- const panel = this.modal.querySelector('#tab-panel-council');
755
- if (!panel) return;
756
-
757
- const providerId = providerSelect.value;
758
- const newDefault = this._getProviderDefaultTimeout(providerId);
759
- const oldProviderId = providerSelect.dataset.previousProvider;
760
- const oldDefault = oldProviderId ? this._getProviderDefaultTimeout(oldProviderId) : null;
761
-
762
- // Determine which timeout element to update
763
- const isOrchestration = providerSelect.dataset.target === 'orchestration';
764
- if (isOrchestration) {
765
- const timeoutEl = panel.querySelector('#vc-orchestration-timeout');
766
- if (timeoutEl) {
767
- const currentValue = parseInt(timeoutEl.value, 10);
768
- const resolvedTimeout = (oldDefault !== null && currentValue !== oldDefault)
769
- ? Math.max(currentValue, newDefault)
770
- : newDefault;
771
- timeoutEl.value = String(resolvedTimeout);
772
- this._updateOrchestrationTimeoutIcon(panel, String(resolvedTimeout));
773
- }
774
- } else {
775
- const idx = providerSelect.dataset.index;
776
- const wrapper = providerSelect.closest('.vc-reviewer');
777
- const timeoutEl = wrapper?.querySelector('.vc-timeout');
778
- if (timeoutEl) {
779
- const currentValue = parseInt(timeoutEl.value, 10);
780
- const resolvedTimeout = (oldDefault !== null && currentValue !== oldDefault)
781
- ? Math.max(currentValue, newDefault)
782
- : newDefault;
783
- timeoutEl.value = String(resolvedTimeout);
784
- this._updateTimeoutIcon(panel, idx, String(resolvedTimeout));
785
- }
786
- }
787
-
788
- providerSelect.dataset.previousProvider = providerId;
789
- }
790
-
791
721
  // --- Dropdown / model management ---
792
722
 
793
723
  _updateAllVoiceDropdowns() {
@@ -1150,18 +1080,7 @@ class VoiceCentricConfigTab {
1150
1080
  TimeoutSelect.mount(mount, { className: 'vc-timeout', title: 'Per-reviewer timeout' });
1151
1081
  }
1152
1082
  const timeoutEl = row.querySelector('.vc-timeout');
1153
- const providerDefaultTimeout = this._getProviderDefaultTimeout(voice.provider);
1154
- if (timeoutEl && voice.timeout) {
1155
- timeoutEl.value = String(voice.timeout);
1156
- // Show the dropdown if non-default for this provider
1157
- if (voice.timeout !== providerDefaultTimeout) {
1158
- timeoutEl.style.display = '';
1159
- }
1160
- this._updateTimeoutIcon(panel, String(i), String(voice.timeout));
1161
- } else if (timeoutEl) {
1162
- // No saved timeout — apply the provider's default
1163
- timeoutEl.value = String(providerDefaultTimeout);
1164
- }
1083
+ this._restoreVoiceTimeout(panel, { index: String(i) }, timeoutEl, voice);
1165
1084
  }
1166
1085
 
1167
1086
  if (voice.customInstructions) {
@@ -1202,20 +1121,7 @@ class VoiceCentricConfigTab {
1202
1121
  }
1203
1122
 
1204
1123
  // Restore orchestration timeout
1205
- const orchTimeoutSelect = panel.querySelector('#vc-orchestration-timeout');
1206
- const orchProviderDefaultTimeout = this._getProviderDefaultTimeout(vcConfig.orchestration.provider);
1207
- if (orchTimeoutSelect && vcConfig.orchestration.timeout) {
1208
- orchTimeoutSelect.value = String(vcConfig.orchestration.timeout);
1209
- // Show the dropdown if non-default for this provider
1210
- if (vcConfig.orchestration.timeout !== orchProviderDefaultTimeout) {
1211
- orchTimeoutSelect.style.display = '';
1212
- }
1213
- this._updateOrchestrationTimeoutIcon(panel, String(vcConfig.orchestration.timeout));
1214
- } else if (orchTimeoutSelect) {
1215
- // No saved timeout — apply the provider's default
1216
- orchTimeoutSelect.value = String(orchProviderDefaultTimeout);
1217
- this._updateOrchestrationTimeoutIcon(panel, String(orchProviderDefaultTimeout));
1218
- }
1124
+ this._restoreOrchestrationTimeout(panel, vcConfig.orchestration);
1219
1125
 
1220
1126
  // Restore orchestration custom instructions
1221
1127
  const orchInstrInput = panel.querySelector('#vc-orchestration-instructions');
@@ -1521,6 +1427,10 @@ class VoiceCentricConfigTab {
1521
1427
  }
1522
1428
  }
1523
1429
 
1430
+ // Shared timeout methods (public/js/utils/council-timeout.js). In the browser the
1431
+ // util's <script> tag loads first; unit tests load it via require().
1432
+ ((typeof window !== 'undefined' && window.CouncilTimeout) || require('../utils/council-timeout.js')).mixInto(VoiceCentricConfigTab);
1433
+
1524
1434
  // Export for use in other modules
1525
1435
  if (typeof window !== 'undefined') {
1526
1436
  window.VoiceCentricConfigTab = VoiceCentricConfigTab;
@@ -1268,8 +1268,19 @@
1268
1268
  /**
1269
1269
  * Fetch and display recent reviews (initial load).
1270
1270
  * Resets pagination state and renders the full table from scratch.
1271
+ *
1272
+ * The help modal is opt-in: only the initial page load passes
1273
+ * `showHelpIfEmpty`, so a first-time user with no reviews is greeted with
1274
+ * it, while every refresh (single delete, bulk delete) that happens to
1275
+ * empty the list just shows the empty-state message.
1276
+ *
1277
+ * @param {Object} [options]
1278
+ * @param {boolean} [options.showHelpIfEmpty=false] - Open the help modal
1279
+ * when the list comes back empty.
1271
1280
  */
1272
- async function loadRecentReviews() {
1281
+ async function loadRecentReviews(options) {
1282
+ const showHelpIfEmpty = Boolean(options && options.showHelpIfEmpty);
1283
+
1273
1284
  prSelection.exit();
1274
1285
 
1275
1286
  const container = document.getElementById('recent-reviews-container');
@@ -1294,8 +1305,9 @@
1294
1305
  '<p>No PR reviews yet. Paste a PR URL above to get started.</p>' +
1295
1306
  '</div>';
1296
1307
  container.classList.remove('recent-reviews-loading');
1297
- // Show help modal when no reviews exist
1298
- openHelpModal();
1308
+ // Greet first-time users with the help modal, but only on initial
1309
+ // page load, never after the user emptied the list themselves.
1310
+ if (showHelpIfEmpty) openHelpModal();
1299
1311
  return;
1300
1312
  }
1301
1313
 
@@ -2476,8 +2488,9 @@
2476
2488
  }
2477
2489
  }
2478
2490
 
2479
- // Always load PR reviews (they show on initial load)
2480
- loadRecentReviews();
2491
+ // Always load PR reviews (they show on initial load). This is the only
2492
+ // caller that opens the help modal for an empty list.
2493
+ loadRecentReviews({ showHelpIfEmpty: true });
2481
2494
 
2482
2495
  // If local tab is active, load local reviews immediately
2483
2496
  if (savedTab === 'local-tab') {
@@ -41,7 +41,10 @@ class DiffRenderer {
41
41
  </svg>
42
42
  `;
43
43
 
44
- // Map of file extensions to highlight.js language names
44
+ // Map of file extensions to highlight.js language names. Every value must be
45
+ // registered by the bundles pr.html and local.html load (the cdnjs common set
46
+ // plus their languages/*.min.js tags); tests/unit/hljs-language-map.test.js
47
+ // enforces this.
45
48
  static LANGUAGE_MAP = {
46
49
  // JavaScript/TypeScript
47
50
  'js': 'javascript',
@@ -56,7 +59,6 @@ class DiffRenderer {
56
59
  'xml': 'xml',
57
60
  'css': 'css',
58
61
  'scss': 'scss',
59
- 'sass': 'sass',
60
62
  'less': 'less',
61
63
  // Python
62
64
  'py': 'python',
@@ -113,8 +115,7 @@ class DiffRenderer {
113
115
  'r': 'r',
114
116
  'lua': 'lua',
115
117
  'perl': 'perl',
116
- 'pl': 'perl',
117
- 'vim': 'vim'
118
+ 'pl': 'perl'
118
119
  };
119
120
 
120
121
  /**
@@ -447,9 +448,9 @@ class DiffRenderer {
447
448
  }
448
449
 
449
450
  // Apply syntax highlighting if highlight.js is available
450
- if (window.hljs && fileName) {
451
+ const language = fileName ? DiffRenderer.detectLanguage(fileName) : null;
452
+ if (language && window.hljs?.getLanguage(language)) {
451
453
  try {
452
- const language = DiffRenderer.detectLanguage(fileName);
453
454
  const highlighted = window.hljs.highlight(content, { language, ignoreIllegals: true });
454
455
  let html = highlighted.value;
455
456
 
@@ -0,0 +1,203 @@
1
+ // Copyright 2026 Tim Perkins (tjwp) | SPDX-License-Identifier: Apache-2.0
2
+ /**
3
+ * Shared per-provider timeout logic for the council config tabs.
4
+ *
5
+ * AdvancedConfigTab and VoiceCentricConfigTab both show a timeout dropdown and a
6
+ * clock icon per voice plus one for orchestration/consolidation. They kept
7
+ * separate copies of the same timeout methods, and the copies drifted apart
8
+ * (e.g. the _defaultConfig() provider-default gap). This mixin is the single
9
+ * copy.
10
+ *
11
+ * The tabs differ only in DOM shape, so each tab describes its own with a
12
+ * static TIMEOUT_DOM:
13
+ *
14
+ * static TIMEOUT_DOM = {
15
+ * panelId: 'tab-panel-advanced', // panel inside the modal
16
+ * voiceWrapperClass: 'participant-wrapper', // wraps one voice row
17
+ * voiceKeys: ['level', 'index'], // data-* attrs that identify a voice
18
+ * timeoutClass: 'adv-timeout', // per-voice timeout select
19
+ * orchestrationTimeoutId: 'adv-orchestration-timeout',
20
+ * orchestrationToggleId: 'adv-orchestration-timeout-toggle',
21
+ * orchestrationRowId: 'orchestration-voice',
22
+ * };
23
+ *
24
+ * A voice is addressed by a key object holding its voiceKeys, e.g.
25
+ * { level: '1', index: '0' } (Advanced) or { index: '0' } (Voice-centric).
26
+ *
27
+ * The host class must also define static DEFAULT_TIMEOUT and instance
28
+ * `providers` and `modal`.
29
+ */
30
+ (function () {
31
+ /**
32
+ * Timeout to use after a voice's provider changes. A value still at the old
33
+ * provider's default follows the new default; a customised value is kept
34
+ * unless the new default is longer.
35
+ * @param {number} currentValue - Current timeout in ms
36
+ * @param {number|null} oldDefault - Previous provider's default, or null if unknown
37
+ * @param {number} newDefault - New provider's default
38
+ * @returns {number} Timeout in ms
39
+ */
40
+ function resolveProviderSwitchTimeout(currentValue, oldDefault, newDefault) {
41
+ return (oldDefault !== null && currentValue !== oldDefault)
42
+ ? Math.max(currentValue, newDefault)
43
+ : newDefault;
44
+ }
45
+
46
+ /**
47
+ * Attribute selector for a voice key, e.g. '[data-level="1"][data-index="0"]'.
48
+ * @param {string[]} voiceKeys
49
+ * @param {Object} key
50
+ * @returns {string}
51
+ */
52
+ function voiceKeySelector(voiceKeys, key) {
53
+ return voiceKeys.map(k => `[data-${k}="${key[k]}"]`).join('');
54
+ }
55
+
56
+ const methods = {
57
+ /**
58
+ * Get the default timeout for a provider, falling back to the static DEFAULT_TIMEOUT.
59
+ * @param {string} providerId - Provider ID (e.g., 'pi', 'claude')
60
+ * @returns {number} Default timeout in ms
61
+ */
62
+ _getProviderDefaultTimeout(providerId) {
63
+ const provider = this.providers[providerId];
64
+ return provider?.defaultTimeout ?? this.constructor.DEFAULT_TIMEOUT;
65
+ },
66
+
67
+ /**
68
+ * Update the clock/timeout icon styling to indicate non-default timeout.
69
+ * @param {Element} panel - The council panel element
70
+ * @param {Object} key - Voice key, e.g. { level, index } or { index }
71
+ * @param {string} value - Current timeout value (as string of ms)
72
+ */
73
+ _updateTimeoutIcon(panel, key, value) {
74
+ const dom = this.constructor.TIMEOUT_DOM;
75
+ const keySel = voiceKeySelector(dom.voiceKeys, key);
76
+ const wrapper = panel.querySelector(`.${dom.voiceWrapperClass}${keySel}`);
77
+ const iconBtn = wrapper?.querySelector(`.toggle-timeout-icon${keySel}`);
78
+ if (!iconBtn) return;
79
+
80
+ const providerId = wrapper?.querySelector('.voice-provider')?.value;
81
+ const defaultTimeout = this._getProviderDefaultTimeout(providerId);
82
+ const isNonDefault = parseInt(value, 10) !== defaultTimeout;
83
+ iconBtn.classList.toggle('has-custom-timeout', isNonDefault);
84
+ },
85
+
86
+ _updateOrchestrationTimeoutIcon(panel, value) {
87
+ const dom = this.constructor.TIMEOUT_DOM;
88
+ const iconBtn = panel.querySelector(`#${dom.orchestrationToggleId}`);
89
+ if (!iconBtn) return;
90
+
91
+ const orchRow = panel.querySelector(`#${dom.orchestrationRowId}`);
92
+ const providerId = orchRow?.querySelector('.voice-provider')?.value;
93
+ const defaultTimeout = this._getProviderDefaultTimeout(providerId);
94
+ const isNonDefault = parseInt(value, 10) !== defaultTimeout;
95
+ iconBtn.classList.toggle('has-custom-timeout', isNonDefault);
96
+ },
97
+
98
+ /**
99
+ * When a voice's provider changes, update its timeout to the new provider's default,
100
+ * preserving explicit user overrides via Math.max when the user had customized the value.
101
+ * @param {HTMLSelectElement} providerSelect - The provider dropdown that changed
102
+ */
103
+ _applyProviderDefaultTimeout(providerSelect) {
104
+ const dom = this.constructor.TIMEOUT_DOM;
105
+ const panel = this.modal.querySelector(`#${dom.panelId}`);
106
+ if (!panel) return;
107
+
108
+ const providerId = providerSelect.value;
109
+ const newDefault = this._getProviderDefaultTimeout(providerId);
110
+ const oldProviderId = providerSelect.dataset.previousProvider;
111
+ const oldDefault = oldProviderId ? this._getProviderDefaultTimeout(oldProviderId) : null;
112
+
113
+ const isOrchestration = providerSelect.dataset.target === 'orchestration';
114
+ if (isOrchestration) {
115
+ const timeoutEl = panel.querySelector(`#${dom.orchestrationTimeoutId}`);
116
+ if (timeoutEl) {
117
+ const resolvedTimeout = resolveProviderSwitchTimeout(parseInt(timeoutEl.value, 10), oldDefault, newDefault);
118
+ timeoutEl.value = String(resolvedTimeout);
119
+ this._updateOrchestrationTimeoutIcon(panel, String(resolvedTimeout));
120
+ }
121
+ } else {
122
+ const key = {};
123
+ dom.voiceKeys.forEach(k => { key[k] = providerSelect.dataset[k]; });
124
+ const wrapper = providerSelect.closest(`.${dom.voiceWrapperClass}`);
125
+ const timeoutEl = wrapper?.querySelector(`.${dom.timeoutClass}`);
126
+ if (timeoutEl) {
127
+ const resolvedTimeout = resolveProviderSwitchTimeout(parseInt(timeoutEl.value, 10), oldDefault, newDefault);
128
+ timeoutEl.value = String(resolvedTimeout);
129
+ this._updateTimeoutIcon(panel, key, String(resolvedTimeout));
130
+ }
131
+ }
132
+
133
+ providerSelect.dataset.previousProvider = providerId;
134
+ },
135
+
136
+ /**
137
+ * Restore a voice's saved timeout into its (already mounted) timeout select.
138
+ * Shows the select when the saved value differs from the provider's default;
139
+ * with no saved value, applies the provider's default.
140
+ * @param {Element} panel - The council panel element
141
+ * @param {Object} key - Voice key, e.g. { level, index } or { index }
142
+ * @param {HTMLSelectElement|null} timeoutEl - The voice's timeout select
143
+ * @param {{provider: string, timeout?: number}} voice - Saved voice config
144
+ */
145
+ _restoreVoiceTimeout(panel, key, timeoutEl, voice) {
146
+ const providerDefaultTimeout = this._getProviderDefaultTimeout(voice.provider);
147
+ if (timeoutEl && voice.timeout) {
148
+ timeoutEl.value = String(voice.timeout);
149
+ // Show the dropdown if non-default for this provider
150
+ if (voice.timeout !== providerDefaultTimeout) {
151
+ timeoutEl.style.display = '';
152
+ }
153
+ this._updateTimeoutIcon(panel, key, String(voice.timeout));
154
+ } else if (timeoutEl) {
155
+ // No saved timeout — apply the provider's default
156
+ timeoutEl.value = String(providerDefaultTimeout);
157
+ }
158
+ },
159
+
160
+ /**
161
+ * Restore the orchestration/consolidation timeout select and its icon.
162
+ * @param {Element} panel - The council panel element
163
+ * @param {{provider: string, timeout?: number}} orchestration - Saved orchestration config
164
+ */
165
+ _restoreOrchestrationTimeout(panel, orchestration) {
166
+ const dom = this.constructor.TIMEOUT_DOM;
167
+ const orchTimeoutSelect = panel.querySelector(`#${dom.orchestrationTimeoutId}`);
168
+ const orchProviderDefaultTimeout = this._getProviderDefaultTimeout(orchestration.provider);
169
+ if (orchTimeoutSelect && orchestration.timeout) {
170
+ orchTimeoutSelect.value = String(orchestration.timeout);
171
+ // Show the dropdown if non-default for this provider
172
+ if (orchestration.timeout !== orchProviderDefaultTimeout) {
173
+ orchTimeoutSelect.style.display = '';
174
+ }
175
+ this._updateOrchestrationTimeoutIcon(panel, String(orchestration.timeout));
176
+ } else if (orchTimeoutSelect) {
177
+ // No saved timeout — apply the provider's default
178
+ orchTimeoutSelect.value = String(orchProviderDefaultTimeout);
179
+ this._updateOrchestrationTimeoutIcon(panel, String(orchProviderDefaultTimeout));
180
+ }
181
+ },
182
+ };
183
+
184
+ /**
185
+ * Copy the shared timeout methods onto a tab class's prototype.
186
+ * @param {Function} TabClass - Class with static TIMEOUT_DOM and DEFAULT_TIMEOUT
187
+ */
188
+ function mixInto(TabClass) {
189
+ Object.assign(TabClass.prototype, methods);
190
+ }
191
+
192
+ const CouncilTimeout = { resolveProviderSwitchTimeout, methods, mixInto };
193
+
194
+ if (typeof window !== 'undefined') {
195
+ window.CouncilTimeout = CouncilTimeout;
196
+ }
197
+
198
+ // Support CommonJS require() in unit tests while keeping the browser IIFE
199
+ // behavior of assigning to window.
200
+ if (typeof module !== 'undefined' && module.exports) {
201
+ module.exports = CouncilTimeout;
202
+ }
203
+ })();