@agy-daddy/plugin-chat-nav 1.1.0 → 1.1.2

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/src/node.js CHANGED
@@ -23,6 +23,11 @@ const DEFAULT_SETTINGS = Object.freeze({
23
23
  keepThinkingExpanded: false,
24
24
  enableInlineFileEditor: true,
25
25
  enableCmdClickLinks: true,
26
+ enableCodeBlockSticky: true,
27
+ enableCodeBlockFold: true,
28
+ codeFoldMinLines: 48,
29
+ showOutlineRail: true,
30
+ enableTurnShortcuts: true,
26
31
  searchRegexMode: false,
27
32
  searchCaseSensitive: false,
28
33
  exportSaveBehavior: 'dialog',
@@ -424,6 +429,22 @@ async function activate(host) {
424
429
  const stopCmdBrowseDir = await host.commands.register('chat-nav.browseExportDir', async (opts = {}) => {
425
430
  return serviceMethods.browseExportDir(opts || {});
426
431
  });
432
+ const stopCmdToggleOutline = await host.commands.register('chat-nav.toggle-outline', () => {
433
+ host.events.emit('chat-nav:command@1', { command: 'toggle-outline' });
434
+ return { ok: true, command: 'chat-nav.toggle-outline', action: 'toggleOutline' };
435
+ });
436
+ const stopCmdCollapseLongCode = await host.commands.register('chat-nav.collapse-long-code', () => {
437
+ host.events.emit('chat-nav:command@1', { command: 'collapse-long-code' });
438
+ return { ok: true, command: 'chat-nav.collapse-long-code', action: 'collapseLongCode' };
439
+ });
440
+ const stopCmdTurnPrev = await host.commands.register('chat-nav.turn-prev', () => {
441
+ host.events.emit('chat-nav:command@1', { command: 'turn-prev' });
442
+ return { ok: true, command: 'chat-nav.turn-prev', action: 'turnPrev' };
443
+ });
444
+ const stopCmdTurnNext = await host.commands.register('chat-nav.turn-next', () => {
445
+ host.events.emit('chat-nav:command@1', { command: 'turn-next' });
446
+ return { ok: true, command: 'chat-nav.turn-next', action: 'turnNext' };
447
+ });
427
448
 
428
449
  disposers.push(
429
450
  stopSettings,
@@ -443,7 +464,11 @@ async function activate(host) {
443
464
  stopCmdExportMd,
444
465
  stopCmdExport,
445
466
  stopCmdExportAll,
446
- stopCmdBrowseDir
467
+ stopCmdBrowseDir,
468
+ stopCmdToggleOutline,
469
+ stopCmdCollapseLongCode,
470
+ stopCmdTurnPrev,
471
+ stopCmdTurnNext
447
472
  );
448
473
  }
449
474
 
@@ -0,0 +1,599 @@
1
+ 'use strict';
2
+
3
+ const QUESTION_CUE_RE = /[??]|(?:^|[\s,。;:])(?:为什么|怎么|如何|是否|能否|哪里|哪些|哪个|请问|可以吗|行不行|对吗|对不对|what|why|how|when|where|which|can\s|could\s|should\s|would\s|is\s|are\s|do\s|does\s)/i;
4
+
5
+ function isEditableElement(el, doc) {
6
+ const candidates = [el, doc?.activeElement].filter(Boolean);
7
+ for (const node of candidates) {
8
+ if (!node) continue;
9
+ const tag = String(node.tagName || '').toLowerCase();
10
+ if (tag === 'input' || tag === 'textarea' || tag === 'select') return true;
11
+ if (node.isContentEditable === true) return true;
12
+ const ce = node.getAttribute?.('contenteditable');
13
+ if (ce === 'true' || ce === '') return true;
14
+ if (node.getAttribute?.('data-lexical-editor') === 'true') return true;
15
+ if (node.getAttribute?.('role') === 'textbox') return true;
16
+ if (typeof node.closest === 'function') {
17
+ const editableAncestor = node.closest(
18
+ 'input, textarea, select, [contenteditable="true"], [contenteditable=""], [data-lexical-editor="true"], [role="textbox"], .monaco-editor, .cm-editor, .CodeMirror'
19
+ );
20
+ if (editableAncestor) return true;
21
+ }
22
+ }
23
+ return false;
24
+ }
25
+
26
+ function isKeyQuestionText(text) {
27
+ const clean = String(text || '').trim();
28
+ if (!clean) return false;
29
+ return QUESTION_CUE_RE.test(clean);
30
+ }
31
+
32
+ function summarizeTitle(text, maxLen = 58) {
33
+ const lines = String(text || '')
34
+ .split(/\r?\n/)
35
+ .map((s) => s.trim())
36
+ .filter(Boolean);
37
+ const first = (lines[0] || '').replace(/\s+/g, ' ');
38
+ if (!first) return '对话轮次';
39
+ return first.length > maxLen ? `${first.slice(0, maxLen - 1)}…` : first;
40
+ }
41
+
42
+ function getRectSafe(el) {
43
+ if (!el || typeof el.getBoundingClientRect !== 'function') return null;
44
+ try {
45
+ return el.getBoundingClientRect() || null;
46
+ } catch {
47
+ return null;
48
+ }
49
+ }
50
+
51
+ function extractHeadingsFromElement(el, maxHeadings = 4) {
52
+ if (!el || typeof el.querySelectorAll !== 'function') return [];
53
+ const nodes = Array.from(el.querySelectorAll('h1, h2, h3') || []);
54
+ const out = [];
55
+ for (const h of nodes) {
56
+ if (h.closest?.('[data-agy-code-injected="1"]')) continue;
57
+ const text = String(h.textContent || '').trim().replace(/\s+/g, ' ');
58
+ if (!text) continue;
59
+ const tag = String(h.tagName || 'h2').toLowerCase();
60
+ out.push({
61
+ el: h,
62
+ level: Number(tag.slice(1)) || 2,
63
+ text: text.length > 44 ? `${text.slice(0, 43)}…` : text
64
+ });
65
+ if (out.length >= maxHeadings) break;
66
+ }
67
+ return out;
68
+ }
69
+
70
+ function extractOutlineItems(vp, opts = {}) {
71
+ if (!vp || typeof vp.querySelectorAll !== 'function') return [];
72
+ const nodes = Array.from(vp.querySelectorAll('[data-testid="user-message"], [data-testid="agent-response"]') || []);
73
+ if (nodes.length === 0) return [];
74
+
75
+ const vpRect = getRectSafe(vp);
76
+ const scrollHeight = Math.max(1, Number(vp.scrollHeight || vpRect?.height || 1));
77
+ const curScrollTop = Number(vp.scrollTop || 0);
78
+
79
+ const items = [];
80
+ let currentRound = null;
81
+
82
+ for (const node of nodes) {
83
+ const tid = node.getAttribute?.('data-testid');
84
+ if (tid === 'user-message') {
85
+ const rawText = String(node.innerText || node.textContent || '').trim();
86
+ const idx = items.length + 1;
87
+ const r = getRectSafe(node);
88
+ const offsetTop = (r && vpRect) ? Math.max(0, Math.round(r.top - vpRect.top + curScrollTop)) : null;
89
+ currentRound = {
90
+ index: idx,
91
+ el: node,
92
+ responseEl: null,
93
+ role: 'user',
94
+ title: summarizeTitle(rawText),
95
+ rawText,
96
+ isKeyQuestion: isKeyQuestionText(rawText),
97
+ codeCount: Array.from(node.querySelectorAll?.('pre') || []).length,
98
+ headings: [],
99
+ offsetTop,
100
+ ratio: 0
101
+ };
102
+ items.push(currentRound);
103
+ } else if (tid === 'agent-response') {
104
+ const pres = Array.from(node.querySelectorAll?.('pre') || []).length;
105
+ const headings = extractHeadingsFromElement(node);
106
+ if (currentRound && !currentRound.responseEl) {
107
+ currentRound.responseEl = node;
108
+ currentRound.codeCount += pres;
109
+ currentRound.headings = headings;
110
+ } else {
111
+ const rawText = String(node.innerText || node.textContent || '').trim();
112
+ const idx = items.length + 1;
113
+ const r = getRectSafe(node);
114
+ const offsetTop = (r && vpRect) ? Math.max(0, Math.round(r.top - vpRect.top + curScrollTop)) : null;
115
+ currentRound = {
116
+ index: idx,
117
+ el: node,
118
+ responseEl: node,
119
+ role: 'assistant',
120
+ title: headings[0]?.text || summarizeTitle(rawText),
121
+ rawText,
122
+ isKeyQuestion: false,
123
+ codeCount: pres,
124
+ headings,
125
+ offsetTop,
126
+ ratio: 0
127
+ };
128
+ items.push(currentRound);
129
+ }
130
+ }
131
+ }
132
+
133
+ const total = items.length;
134
+ for (let i = 0; i < total; i++) {
135
+ const it = items[i];
136
+ if (it.offsetTop !== null && scrollHeight > 1) {
137
+ it.ratio = Math.max(0.02, Math.min(0.98, it.offsetTop / scrollHeight));
138
+ } else {
139
+ it.ratio = total === 1 ? 0.08 : (0.04 + (i / Math.max(1, total - 1)) * 0.92);
140
+ }
141
+ }
142
+
143
+ return opts.maxItems > 0 ? items.slice(0, opts.maxItems) : items;
144
+ }
145
+
146
+ function filterOutlineItems(items = [], filterMode = 'all') {
147
+ if (!Array.isArray(items)) return [];
148
+ if (filterMode === 'questions') return items.filter((it) => Boolean(it.isKeyQuestion));
149
+ if (filterMode === 'code') return items.filter((it) => Number(it.codeCount || 0) > 0);
150
+ return items;
151
+ }
152
+
153
+ function createOutlineRail(deps = {}) {
154
+ const {
155
+ doc,
156
+ win,
157
+ anchors,
158
+ findChatAreaElement,
159
+ hasOpenDialog,
160
+ scrollViewportTo,
161
+ getSettings
162
+ } = deps;
163
+
164
+ let railEl = null;
165
+ let tracksEl = null;
166
+ let popoverEl = null;
167
+ let headerTitleEl = null;
168
+ let filterBarEl = null;
169
+ let listEl = null;
170
+ let filterMode = 'all';
171
+ let popoverOpen = false;
172
+ let closeTimer = null;
173
+ let observedVp = null;
174
+ let onVpScroll = null;
175
+ let activeIndex = -1;
176
+ let cachedItems = [];
177
+ let disposed = false;
178
+
179
+ function isEnabled() {
180
+ const s = typeof getSettings === 'function' ? (getSettings() || {}) : {};
181
+ return s.showOutlineRail !== false;
182
+ }
183
+
184
+ function clearCloseTimer() {
185
+ if (closeTimer !== null) {
186
+ clearTimeout(closeTimer);
187
+ closeTimer = null;
188
+ }
189
+ }
190
+
191
+ function scheduleClosePopover() {
192
+ clearCloseTimer();
193
+ closeTimer = setTimeout(() => {
194
+ closeTimer = null;
195
+ setPopoverOpen(false);
196
+ }, 180);
197
+ }
198
+
199
+ function findNativeRailHost() {
200
+ if (!doc?.querySelector) return null;
201
+ return doc.querySelector('[data-testid="conversation-rail"], [data-testid="chat-rail"], [data-testid="minimap-rail"]');
202
+ }
203
+
204
+ function teardown() {
205
+ clearCloseTimer();
206
+ if (observedVp && onVpScroll) {
207
+ try { observedVp.removeEventListener?.('scroll', onVpScroll); } catch {}
208
+ }
209
+ observedVp = null;
210
+ onVpScroll = null;
211
+ if (popoverEl?.remove) popoverEl.remove();
212
+ if (railEl?.remove) railEl.remove();
213
+ railEl = null;
214
+ tracksEl = null;
215
+ popoverEl = null;
216
+ headerTitleEl = null;
217
+ filterBarEl = null;
218
+ listEl = null;
219
+ popoverOpen = false;
220
+ }
221
+
222
+ function scrollToTargetElement(el, index) {
223
+ if (!el) return false;
224
+ const vp = anchors?.query?.('chat.scrollViewport') || anchors?.query?.('chat.messageList');
225
+ if (vp) {
226
+ const vpRect = getRectSafe(vp);
227
+ const elRect = getRectSafe(el);
228
+ if (vpRect && elRect && vpRect.height > 0) {
229
+ const targetTop = Math.max(0, Math.round(Number(vp.scrollTop || 0) + (elRect.top - vpRect.top) - 12));
230
+ if (typeof scrollViewportTo === 'function') scrollViewportTo(vp, targetTop);
231
+ else vp.scrollTop = targetTop;
232
+ } else if (typeof el.scrollIntoView === 'function') {
233
+ try { el.scrollIntoView({ block: 'start', behavior: 'smooth' }); } catch {}
234
+ } else {
235
+ const idxPos = Math.max(0, Number(index) - 1) * 180;
236
+ vp.scrollTop = idxPos;
237
+ }
238
+ } else if (typeof el.scrollIntoView === 'function') {
239
+ try { el.scrollIntoView({ block: 'start', behavior: 'smooth' }); } catch {}
240
+ }
241
+ if (Number.isFinite(index) && index > 0) {
242
+ setActiveTurnIndex(index);
243
+ }
244
+ return true;
245
+ }
246
+
247
+ function setActiveTurnIndex(idx) {
248
+ activeIndex = idx;
249
+ if (tracksEl) {
250
+ for (const tick of tracksEl.children || []) {
251
+ const tIdx = Number(tick.getAttribute?.('data-turn-index'));
252
+ tick.setAttribute?.('data-active', tIdx === activeIndex ? 'true' : 'false');
253
+ }
254
+ }
255
+ if (listEl) {
256
+ for (const row of listEl.children || []) {
257
+ const rIdx = Number(row.getAttribute?.('data-turn-index'));
258
+ row.setAttribute?.('data-active', rIdx === activeIndex ? 'true' : 'false');
259
+ }
260
+ }
261
+ }
262
+
263
+ function syncActiveTurnFromScroll() {
264
+ if (!cachedItems.length) return;
265
+ const vp = anchors?.query?.('chat.scrollViewport');
266
+ const vpRect = getRectSafe(vp);
267
+ if (!vp || !vpRect || vpRect.height <= 0) {
268
+ if (activeIndex < 1) setActiveTurnIndex(cachedItems[0].index);
269
+ return;
270
+ }
271
+ const anchorY = vpRect.top + 36;
272
+ let best = cachedItems[0].index;
273
+ for (const item of cachedItems) {
274
+ const r = getRectSafe(item.el);
275
+ if (!r) continue;
276
+ if (r.top <= anchorY) best = item.index;
277
+ else break;
278
+ }
279
+ setActiveTurnIndex(best);
280
+ }
281
+
282
+ function attachScrollListener() {
283
+ const vp = anchors?.query?.('chat.scrollViewport') || null;
284
+ if (vp === observedVp) return;
285
+ if (observedVp && onVpScroll) {
286
+ try { observedVp.removeEventListener?.('scroll', onVpScroll); } catch {}
287
+ }
288
+ observedVp = vp;
289
+ if (vp && vp.addEventListener) {
290
+ onVpScroll = () => syncActiveTurnFromScroll();
291
+ vp.addEventListener('scroll', onVpScroll, { passive: true });
292
+ } else {
293
+ onVpScroll = null;
294
+ }
295
+ }
296
+
297
+ function clearChildren(container) {
298
+ if (!container) return;
299
+ while (container.children?.length > 0) {
300
+ const ch = container.children[container.children.length - 1];
301
+ if (ch?.remove) ch.remove();
302
+ else break;
303
+ }
304
+ if (Array.isArray(container.children)) container.children.length = 0;
305
+ }
306
+
307
+ function renderPopoverContent() {
308
+ if (!popoverEl || !doc?.createElement) return;
309
+ const allCount = cachedItems.length;
310
+ const qCount = filterOutlineItems(cachedItems, 'questions').length;
311
+ const cCount = filterOutlineItems(cachedItems, 'code').length;
312
+ const filtered = filterOutlineItems(cachedItems, filterMode);
313
+
314
+ if (headerTitleEl) {
315
+ headerTitleEl.textContent = `会话大纲 (${allCount} 轮)`;
316
+ }
317
+
318
+ if (filterBarEl) {
319
+ clearChildren(filterBarEl);
320
+ const filters = [
321
+ { id: 'all', label: `全部 ${allCount}` },
322
+ { id: 'questions', label: `关键提问 ${qCount}` },
323
+ { id: 'code', label: `含代码 ${cCount}` }
324
+ ];
325
+ for (const f of filters) {
326
+ const btn = doc.createElement('button');
327
+ btn.type = 'button';
328
+ btn.className = 'agy-outline-filter-btn';
329
+ btn.id = `agy-outline-filter-${f.id}`;
330
+ btn.setAttribute?.('data-filter', f.id);
331
+ btn.setAttribute?.('data-active', filterMode === f.id ? 'true' : 'false');
332
+ btn.textContent = f.label;
333
+ btn.addEventListener('click', (e) => {
334
+ e?.stopPropagation?.();
335
+ filterMode = f.id;
336
+ renderPopoverContent();
337
+ });
338
+ filterBarEl.appendChild(btn);
339
+ }
340
+ }
341
+
342
+ if (listEl) {
343
+ clearChildren(listEl);
344
+ if (filtered.length === 0) {
345
+ const empty = doc.createElement('div');
346
+ empty.className = 'agy-outline-empty';
347
+ empty.textContent = filterMode === 'questions' ? '暂无匹配的关键提问' : '暂无匹配章节';
348
+ listEl.appendChild(empty);
349
+ return;
350
+ }
351
+ for (const item of filtered) {
352
+ const row = doc.createElement('button');
353
+ row.type = 'button';
354
+ row.className = 'agy-outline-item';
355
+ row.setAttribute?.('data-turn-index', String(item.index));
356
+ row.setAttribute?.('data-key-question', item.isKeyQuestion ? 'true' : 'false');
357
+ row.setAttribute?.('data-active', item.index === activeIndex ? 'true' : 'false');
358
+
359
+ const badge = doc.createElement('span');
360
+ badge.className = 'agy-outline-idx';
361
+ badge.textContent = `#${item.index}`;
362
+ row.appendChild(badge);
363
+
364
+ const titleSpan = doc.createElement('span');
365
+ titleSpan.className = 'agy-outline-text';
366
+ titleSpan.textContent = item.title;
367
+ row.appendChild(titleSpan);
368
+
369
+ if (item.codeCount > 0) {
370
+ const codeTag = doc.createElement('span');
371
+ codeTag.className = 'agy-outline-code-tag';
372
+ codeTag.textContent = `${item.codeCount}段代码`;
373
+ row.appendChild(codeTag);
374
+ }
375
+
376
+ row.addEventListener('click', (e) => {
377
+ e?.stopPropagation?.();
378
+ scrollToTargetElement(item.el, item.index);
379
+ });
380
+ listEl.appendChild(row);
381
+
382
+ if (filterMode === 'all' && Array.isArray(item.headings) && item.headings.length > 0) {
383
+ for (const hd of item.headings) {
384
+ const sub = doc.createElement('button');
385
+ sub.type = 'button';
386
+ sub.className = 'agy-outline-subheading';
387
+ sub.setAttribute?.('data-heading-level', String(hd.level));
388
+ sub.setAttribute?.('data-turn-index', String(item.index));
389
+ sub.textContent = hd.text;
390
+ sub.addEventListener('click', (e) => {
391
+ e?.stopPropagation?.();
392
+ scrollToTargetElement(hd.el, item.index);
393
+ });
394
+ listEl.appendChild(sub);
395
+ }
396
+ }
397
+ }
398
+ }
399
+ }
400
+
401
+ function renderRailTicks() {
402
+ if (!tracksEl || !doc?.createElement) return;
403
+ clearChildren(tracksEl);
404
+ for (const item of cachedItems) {
405
+ const tick = doc.createElement('button');
406
+ tick.type = 'button';
407
+ tick.className = 'agy-rail-tick';
408
+ tick.setAttribute?.('data-turn-index', String(item.index));
409
+ tick.setAttribute?.('data-key-question', item.isKeyQuestion ? 'true' : 'false');
410
+ tick.setAttribute?.('data-has-code', item.codeCount > 0 ? 'true' : 'false');
411
+ tick.setAttribute?.('data-active', item.index === activeIndex ? 'true' : 'false');
412
+ tick.setAttribute?.('title', `#${item.index} ${item.title}`);
413
+ tick.setAttribute?.('aria-label', `跳转到第 ${item.index} 轮: ${item.title}`);
414
+ if (tick.style) {
415
+ tick.style.top = `${Math.round(item.ratio * 100)}%`;
416
+ }
417
+ tick.addEventListener('click', (e) => {
418
+ e?.stopPropagation?.();
419
+ scrollToTargetElement(item.el, item.index);
420
+ });
421
+ tracksEl.appendChild(tick);
422
+ }
423
+ }
424
+
425
+ function setPopoverOpen(open) {
426
+ const next = Boolean(open) && isEnabled() && cachedItems.length > 0 && !(typeof hasOpenDialog === 'function' && hasOpenDialog());
427
+ popoverOpen = next;
428
+ if (popoverEl) {
429
+ popoverEl.hidden = !next;
430
+ if (next) {
431
+ popoverEl.removeAttribute?.('hidden');
432
+ renderPopoverContent();
433
+ } else {
434
+ popoverEl.setAttribute?.('hidden', 'true');
435
+ }
436
+ }
437
+ if (railEl) {
438
+ railEl.setAttribute?.('data-outline-open', next ? 'true' : 'false');
439
+ }
440
+ return popoverOpen;
441
+ }
442
+
443
+ function ensureRail() {
444
+ if (disposed || !doc?.createElement) return null;
445
+ if (!isEnabled()) {
446
+ teardown();
447
+ return null;
448
+ }
449
+ if (railEl) return railEl;
450
+
451
+ railEl = doc.createElement('nav');
452
+ railEl.id = 'agy-chat-outline-rail';
453
+ railEl.className = 'agy-chat-outline-rail';
454
+ railEl.setAttribute?.('aria-label', '会话大纲导轨');
455
+ railEl.setAttribute?.('data-outline-open', 'false');
456
+
457
+ tracksEl = doc.createElement('div');
458
+ tracksEl.id = 'agy-chat-outline-tracks';
459
+ tracksEl.className = 'agy-chat-outline-tracks';
460
+ railEl.appendChild(tracksEl);
461
+
462
+ popoverEl = doc.createElement('div');
463
+ popoverEl.id = 'agy-chat-outline-popover';
464
+ popoverEl.className = 'agy-chat-outline-popover';
465
+ popoverEl.setAttribute?.('role', 'region');
466
+ popoverEl.setAttribute?.('aria-label', '会话快速大纲');
467
+ popoverEl.hidden = true;
468
+ popoverEl.setAttribute?.('hidden', 'true');
469
+
470
+ const hdr = doc.createElement('div');
471
+ hdr.className = 'agy-outline-header';
472
+ headerTitleEl = doc.createElement('span');
473
+ headerTitleEl.className = 'agy-outline-title';
474
+ headerTitleEl.textContent = '会话大纲';
475
+ hdr.appendChild(headerTitleEl);
476
+ popoverEl.appendChild(hdr);
477
+
478
+ filterBarEl = doc.createElement('div');
479
+ filterBarEl.className = 'agy-outline-filters';
480
+ popoverEl.appendChild(filterBarEl);
481
+
482
+ listEl = doc.createElement('div');
483
+ listEl.className = 'agy-outline-list';
484
+ popoverEl.appendChild(listEl);
485
+
486
+ railEl.appendChild(popoverEl);
487
+
488
+ const onEnter = () => {
489
+ clearCloseTimer();
490
+ setPopoverOpen(true);
491
+ };
492
+ const onLeave = () => {
493
+ scheduleClosePopover();
494
+ };
495
+ railEl.addEventListener('mouseenter', onEnter);
496
+ railEl.addEventListener('mouseleave', onLeave);
497
+ railEl.addEventListener('focusin', onEnter);
498
+ popoverEl.addEventListener('mouseenter', onEnter);
499
+ popoverEl.addEventListener('mouseleave', onLeave);
500
+
501
+ const nativeHost = findNativeRailHost();
502
+ const mountRoot = nativeHost || doc.body || doc.documentElement;
503
+ if (nativeHost) railEl.setAttribute?.('data-native-rail', 'true');
504
+ mountRoot?.appendChild?.(railEl);
505
+
506
+ return railEl;
507
+ }
508
+
509
+ function syncPosition() {
510
+ if (!railEl) return false;
511
+ const dialogOpen = typeof hasOpenDialog === 'function' ? hasOpenDialog() : false;
512
+ railEl.setAttribute?.('data-under-dialog', dialogOpen ? 'true' : 'false');
513
+ if (dialogOpen && popoverOpen) {
514
+ setPopoverOpen(false);
515
+ }
516
+
517
+ const empty = cachedItems.length === 0;
518
+ railEl.hidden = empty;
519
+ if (empty) {
520
+ railEl.setAttribute?.('hidden', 'true');
521
+ return false;
522
+ }
523
+ railEl.removeAttribute?.('hidden');
524
+
525
+ if (railEl.getAttribute?.('data-native-rail') === 'true') {
526
+ return true;
527
+ }
528
+
529
+ const areaEl = typeof findChatAreaElement === 'function' ? findChatAreaElement() : anchors?.query?.('chat.scrollViewport');
530
+ const rect = getRectSafe(areaEl);
531
+ const winH = Number(win?.innerHeight || doc?.documentElement?.clientHeight || 0);
532
+ if (!rect || rect.width <= 40 || rect.height <= 40) {
533
+ return false;
534
+ }
535
+ const leftPx = Math.max(6, Math.round(rect.left) + 6);
536
+ const topPx = Math.max(56, Math.round(rect.top) + 48);
537
+ const availH = Math.max(120, Math.min(rect.height - 96, (winH > 0 ? winH - topPx - 64 : rect.height - 96)));
538
+ if (railEl.style) {
539
+ railEl.style.left = `${leftPx}px`;
540
+ railEl.style.top = `${topPx}px`;
541
+ railEl.style.height = `${Math.round(availH)}px`;
542
+ }
543
+ return true;
544
+ }
545
+
546
+ function refresh() {
547
+ if (disposed) return [];
548
+ if (!isEnabled()) {
549
+ teardown();
550
+ return [];
551
+ }
552
+ ensureRail();
553
+ attachScrollListener();
554
+ const vp = anchors?.query?.('chat.scrollViewport');
555
+ cachedItems = extractOutlineItems(vp);
556
+ if (cachedItems.length > 0 && activeIndex < 1) {
557
+ activeIndex = cachedItems[0].index;
558
+ }
559
+ renderRailTicks();
560
+ if (popoverOpen) {
561
+ renderPopoverContent();
562
+ }
563
+ syncActiveTurnFromScroll();
564
+ syncPosition();
565
+ return cachedItems;
566
+ }
567
+
568
+ function dispose() {
569
+ disposed = true;
570
+ teardown();
571
+ cachedItems = [];
572
+ activeIndex = -1;
573
+ }
574
+
575
+ refresh();
576
+
577
+ return {
578
+ refresh,
579
+ syncPosition,
580
+ setPopoverOpen,
581
+ isPopoverOpen: () => popoverOpen,
582
+ setFilterMode: (mode) => {
583
+ filterMode = (mode === 'questions' || mode === 'code') ? mode : 'all';
584
+ if (popoverOpen) renderPopoverContent();
585
+ },
586
+ getFilterMode: () => filterMode,
587
+ getOutlineItems: () => [...cachedItems],
588
+ syncActiveTurnFromScroll,
589
+ dispose
590
+ };
591
+ }
592
+
593
+ module.exports = {
594
+ isEditableElement,
595
+ isKeyQuestionText,
596
+ extractOutlineItems,
597
+ filterOutlineItems,
598
+ createOutlineRail
599
+ };