-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathactivity-feed.js
More file actions
1237 lines (1058 loc) · 42.7 KB
/
activity-feed.js
File metadata and controls
1237 lines (1058 loc) · 42.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* TunnelVision Activity Feed
* Floating widget that shows real-time worldbook entry activations and tool call activity.
* Lives on document.body as a draggable trigger button + expandable panel.
*/
import { chat, eventSource, event_types, saveChatConditional } from '../../../../script.js';
import { getContext } from '../../../st-context.js';
import { ALL_TOOL_NAMES, getActiveTunnelVisionBooks } from './tool-registry.js';
import { getSettings, isLorebookEnabled, getTree } from './tree-store.js';
import { openTreeEditorForBook } from './ui-controller.js';
import { getSidecarModelLabel } from './llm-sidecar.js';
const MAX_FEED_ITEMS = 50;
const MAX_RENDERED_RETRIEVED_ENTRIES = 5;
const STORAGE_KEY_POS = 'tv-feed-trigger-position';
const METADATA_KEY = 'tunnelvision_feed';
const HIDDEN_TOOL_CALL_FLAG = 'tvHiddenToolCalls';
/** Track which chatId the current feedItems belong to, prevents cross-chat bleed. */
let activeChatId = null;
// Turn-level tool call accumulator for console summary
/** @type {Array<{name: string, verb: string, summary: string}>} */
let turnToolCalls = [];
/**
* @typedef {Object} RetrievedEntry
* @property {string} lorebook
* @property {number|null} uid
* @property {string} title
*/
/**
* @typedef {Object} FeedItem
* @property {number} id
* @property {'entry'|'tool'} type
* @property {string} icon
* @property {string} verb
* @property {string} color
* @property {string} [summary]
* @property {number} timestamp
* @property {'native'|'tunnelvision'} [source]
* @property {string} [lorebook]
* @property {number|null} [uid]
* @property {string} [title]
* @property {string[]} [keys]
* @property {RetrievedEntry[]} [retrievedEntries]
* @property {string} [reasoning]
* @property {string} [sidecarRaw]
*/
/** @type {FeedItem[]} */
let feedItems = [];
let nextId = 0;
let feedInitialized = false;
let hiddenToolCallRefreshTimer = null;
let hiddenToolCallRefreshNeedsSync = false;
/** @type {HTMLElement|null} */
let triggerEl = null;
/** @type {HTMLElement|null} */
let panelEl = null;
/** @type {HTMLElement|null} */
let panelBody = null;
// Tool display config
const TOOL_DISPLAY = {
'TunnelVision_Search': { icon: 'fa-magnifying-glass', verb: 'Searched', color: '#e84393', activeVerb: 'Searching…' },
'TunnelVision_Remember': { icon: 'fa-brain', verb: 'Remembered', color: '#6c5ce7', activeVerb: 'Remembering…' },
'TunnelVision_Update': { icon: 'fa-pen', verb: 'Updated', color: '#f0946c', activeVerb: 'Updating…' },
'TunnelVision_Forget': { icon: 'fa-eraser', verb: 'Forgot', color: '#ef4444', activeVerb: 'Forgetting…' },
'TunnelVision_Reorganize': { icon: 'fa-arrows-rotate', verb: 'Reorganized', color: '#00b894', activeVerb: 'Reorganizing…' },
'TunnelVision_Summarize': { icon: 'fa-file-lines', verb: 'Summarized', color: '#fdcb6e', activeVerb: 'Summarizing…' },
'TunnelVision_MergeSplit': { icon: 'fa-code-merge', verb: 'Merged/Split', color: '#0984e3', activeVerb: 'Merging/Splitting…' },
'TunnelVision_Notebook': { icon: 'fa-note-sticky', verb: 'Noted', color: '#a29bfe', activeVerb: 'Writing note…' },
// BlackBox
'BlackBox_Pick': { icon: 'fa-cube', verb: 'Picked', color: '#00cec9', activeVerb: 'Picking…' },
};
/**
* Initialize the activity feed — create floating widget and bind events.
* Called once from index.js init.
*/
export function initActivityFeed() {
if (feedInitialized) return;
feedInitialized = true;
loadFeed();
createTriggerButton();
createPanel();
// Listen for WI activations (primary — shows what entries triggered)
if (event_types.WORLD_INFO_ACTIVATED) {
eventSource.on(event_types.WORLD_INFO_ACTIVATED, onWorldInfoActivated);
}
// Listen for TV tool calls (secondary)
if (event_types.TOOL_CALLS_PERFORMED) {
eventSource.on(event_types.TOOL_CALLS_PERFORMED, onToolCallsPerformed);
}
// Listen for tool call rendering to apply visual hiding
if (event_types.TOOL_CALLS_RENDERED) {
eventSource.on(event_types.TOOL_CALLS_RENDERED, onToolCallsRendered);
}
// Reload feed from chat metadata on chat switch
if (event_types.CHAT_CHANGED) {
eventSource.on(event_types.CHAT_CHANGED, () => {
loadFeed();
if (panelEl?.classList.contains('open')) renderAllItems();
queueHiddenToolCallRefresh(false);
});
}
// Reset turn tool-call accumulator on first generation pass (not recursive).
// Feed items are NOT cleared automatically — they accumulate across turns so
// the user can see a running history. Use the trash button to clear manually.
if (event_types.GENERATION_STARTED) {
eventSource.on(event_types.GENERATION_STARTED, () => {
try {
const lastMsg = getContext().chat?.at(-1);
const isRecursiveToolPass = lastMsg?.extra?.tool_invocations != null;
if (!isRecursiveToolPass) {
turnToolCalls = [];
}
} catch {
turnToolCalls = [];
}
});
}
if (event_types.MESSAGE_RECEIVED) {
eventSource.on(event_types.MESSAGE_RECEIVED, printTurnSummary);
}
queueHiddenToolCallRefresh(false);
}
// ── Persistence (chat metadata) ──
function saveFeed() {
try {
const context = getContext();
if (!context.chatMetadata || !context.chatId) return;
if (activeChatId && context.chatId !== activeChatId) return;
// Don't persist transient in-progress items
const persistable = feedItems.filter(item => !item._inProgress);
context.chatMetadata[METADATA_KEY] = { items: persistable, nextId };
context.saveMetadataDebounced();
} catch { /* no active chat */ }
}
function loadFeed() {
feedItems = [];
nextId = 0;
activeChatId = null;
try {
const context = getContext();
if (!context.chatId) return;
activeChatId = context.chatId;
const data = context.chatMetadata?.[METADATA_KEY];
if (data && Array.isArray(data.items)) {
feedItems = data.items;
nextId = typeof data.nextId === 'number' ? data.nextId : feedItems.length;
}
} catch { /* no active chat */ }
}
// ── DOM Helpers ──
function el(tag, cls, text) {
const e = document.createElement(tag);
if (cls) e.className = cls;
if (text) e.textContent = text;
return e;
}
function icon(iconClass) {
const i = document.createElement('i');
i.className = `fa-solid ${iconClass}`;
return i;
}
// ── Tree editor shortcut ──
/**
* Open the tree editor for an active TV lorebook.
* Single book → opens directly. Multiple → shows a quick picker dropdown.
*/
function openTreeEditorFromFeed() {
const books = getActiveTunnelVisionBooks().filter(b => {
const tree = getTree(b);
return tree && tree.root;
});
if (books.length === 0) {
toastr.info('No lorebooks with built trees. Build a tree first in TunnelVision settings.', 'TunnelVision');
return;
}
if (books.length === 1) {
openTreeEditorForBook(books[0]);
return;
}
// Multiple books — show a quick picker
const picker = el('div', 'tv-book-picker');
const label = el('div', 'tv-book-picker-label');
label.textContent = 'Choose lorebook:';
picker.appendChild(label);
for (const name of books) {
const btn = el('button', 'tv-book-picker-btn');
btn.textContent = name;
btn.addEventListener('click', () => {
picker.remove();
openTreeEditorForBook(name);
});
picker.appendChild(btn);
}
const panelHeader = panelEl?.querySelector('.tv-float-panel-header');
if (panelHeader) {
panelHeader.appendChild(picker);
const dismiss = (e) => {
if (!picker.contains(e.target)) {
picker.remove();
document.removeEventListener('click', dismiss, true);
}
};
setTimeout(() => document.addEventListener('click', dismiss, true), 0);
}
}
// ── Trigger Button ──
function createTriggerButton() {
triggerEl = el('div', 'tv-float-trigger');
triggerEl.title = 'TunnelVision Activity Feed';
triggerEl.setAttribute('data-tv-count', '0');
triggerEl.appendChild(icon('fa-satellite-dish'));
// Load saved position
const saved = localStorage.getItem(STORAGE_KEY_POS);
if (saved) {
try {
const pos = JSON.parse(saved);
triggerEl.style.left = pos.left;
triggerEl.style.top = pos.top;
triggerEl.style.bottom = 'auto';
triggerEl.style.right = 'auto';
} catch { /* use default */ }
}
// Drag support
let dragging = false;
let offsetX = 0, offsetY = 0;
triggerEl.addEventListener('pointerdown', (e) => {
dragging = false;
offsetX = e.clientX - triggerEl.getBoundingClientRect().left;
offsetY = e.clientY - triggerEl.getBoundingClientRect().top;
triggerEl.setPointerCapture(e.pointerId);
});
triggerEl.addEventListener('pointermove', (e) => {
if (!triggerEl.hasPointerCapture(e.pointerId)) return;
const dx = e.clientX - triggerEl.getBoundingClientRect().left - offsetX;
const dy = e.clientY - triggerEl.getBoundingClientRect().top - offsetY;
if (!dragging && (Math.abs(dx) > 4 || Math.abs(dy) > 4)) {
dragging = true;
}
if (dragging) {
const viewportWidth = (window.visualViewport ? window.visualViewport.width : window.innerWidth);
const x = Math.max(0, Math.min(viewportWidth - 40, e.clientX - offsetX));
const viewportHeight = (window.visualViewport ? window.visualViewport.height : window.innerHeight);
const y = Math.max(0, Math.min(viewportHeight - 40, e.clientY - offsetY));
triggerEl.style.left = `${x}px`;
triggerEl.style.top = `${y}px`;
triggerEl.style.bottom = 'auto';
triggerEl.style.right = 'auto';
}
});
triggerEl.addEventListener('pointerup', (e) => {
triggerEl.releasePointerCapture(e.pointerId);
if (dragging) {
localStorage.setItem(STORAGE_KEY_POS, JSON.stringify({
left: triggerEl.style.left,
top: triggerEl.style.top,
}));
dragging = false;
} else {
togglePanel();
}
});
document.body.appendChild(triggerEl);
}
// ── Panel ──
function createPanel() {
panelEl = el('div', 'tv-float-panel');
// Header
const header = el('div', 'tv-float-panel-header');
const title = el('span', 'tv-float-panel-title');
title.appendChild(icon('fa-satellite-dish'));
title.append(' TunnelVision Feed');
header.appendChild(title);
const settingsBtn = el('button', 'tv-float-panel-btn');
settingsBtn.title = 'Open tree editor';
settingsBtn.appendChild(icon('fa-folder-tree'));
settingsBtn.addEventListener('click', openTreeEditorFromFeed);
header.appendChild(settingsBtn);
const clearBtn = el('button', 'tv-float-panel-btn');
clearBtn.title = 'Clear feed';
clearBtn.appendChild(icon('fa-trash-can'));
clearBtn.addEventListener('click', () => clearFeed());
header.appendChild(clearBtn);
const closeBtn = el('button', 'tv-float-panel-btn');
closeBtn.title = 'Close';
closeBtn.appendChild(icon('fa-xmark'));
closeBtn.addEventListener('click', () => {
panelEl.classList.remove('open');
});
header.appendChild(closeBtn);
panelEl.appendChild(header);
// Tabs
const tabs = el('div', 'tv-float-panel-tabs');
for (const [key, label] of [['all', 'All'], ['wi', 'Entries'], ['tools', 'Tools']]) {
const tab = el('button', `tv-float-tab${key === 'all' ? ' active' : ''}`, label);
tab.dataset.tab = key;
tab.addEventListener('click', () => {
tabs.querySelectorAll('.tv-float-tab').forEach(t => t.classList.remove('active'));
tab.classList.add('active');
renderAllItems();
});
tabs.appendChild(tab);
}
panelEl.appendChild(tabs);
// Body
panelBody = el('div', 'tv-float-panel-body');
panelEl.appendChild(panelBody);
renderEmptyState('all');
document.body.appendChild(panelEl);
}
function togglePanel() {
if (!panelEl) return;
const isOpen = panelEl.classList.toggle('open');
if (isOpen) {
positionPanel();
renderAllItems();
if (triggerEl) triggerEl.setAttribute('data-tv-count', '0');
}
}
function positionPanel() {
if (!triggerEl || !panelEl) return;
const rect = triggerEl.getBoundingClientRect();
const vw = window.innerWidth;
const vh = window.innerHeight;
const pw = 340;
const ph = 420;
let left = rect.right + 8;
if (left + pw > vw - 16) left = rect.left - pw - 8;
if (left < 16) left = 16;
let top = rect.top;
if (top + ph > vh - 16) top = vh - ph - 16;
if (top < 16) top = 16;
panelEl.style.left = `${left}px`;
panelEl.style.top = `${top}px`;
}
// ── Event Handlers ──
function onWorldInfoActivated(entries) {
if (!Array.isArray(entries) || entries.length === 0) return;
const settings = getSettings();
if (settings.globalEnabled === false) return;
// Guard: ignore callbacks from a chat we've already switched away from
try {
const currentChatId = getContext().chatId;
if (activeChatId && currentChatId !== activeChatId) return;
} catch { /* no chat context */ }
const activeBooks = getActiveTunnelVisionBooks();
if (activeBooks.length === 0) return;
// Build a set of UIDs already in the feed to deduplicate constant entries
// (which fire every generation pass). Only dedup native/entry items.
const existingUids = new Set();
for (const item of feedItems) {
if (item.type === 'entry' && item.source === 'native' && item.uid != null) {
existingUids.add(`${item.lorebook}::${item.uid}`);
}
}
const timestamp = Date.now();
const items = [];
for (const entry of entries) {
// Only show entries from TV-managed lorebooks
if (entry.world && !isLorebookEnabled(entry.world)) continue;
// Skip entries already shown in the feed (constant entries re-fire every pass)
const uid = Number.isFinite(entry?.uid) ? entry.uid : null;
const dedupKey = `${entry.world || ''}::${uid}`;
if (uid != null && existingUids.has(dedupKey)) continue;
items.push(createEntryFeedItem({
source: 'native',
lorebook: typeof entry?.world === 'string' ? entry.world : '',
uid,
title: entry.comment || entry.key?.[0] || `UID ${entry.uid}`,
keys: Array.isArray(entry.key) ? entry.key : [],
constant: !!entry.constant,
timestamp,
}));
}
addFeedItems(items);
}
function onToolCallsPerformed(invocations) {
if (!Array.isArray(invocations) || invocations.length === 0) return;
const settings = getSettings();
if (settings.globalEnabled === false) return;
// Guard: ignore callbacks from a chat we've already switched away from
try {
const currentChatId = getContext().chatId;
if (activeChatId && currentChatId !== activeChatId) return;
} catch { /* no chat context */ }
// Remove in-progress items for completed tools
const completedNames = invocations
.filter(inv => ALL_TOOL_NAMES.includes(inv?.name) || TOOL_DISPLAY[inv?.name])
.map(inv => inv.name);
if (completedNames.length > 0) removeInProgressItems(completedNames);
const timestamp = Date.now();
const items = [];
for (const invocation of invocations) {
if (!ALL_TOOL_NAMES.includes(invocation?.name) && !TOOL_DISPLAY[invocation?.name]) continue;
const params = parseInvocationParameters(invocation.parameters);
const retrievedEntries = invocation.name === 'TunnelVision_Search'
? extractRetrievedEntries(invocation.result)
: [];
// Create feed items for each retrieved entry
for (const entry of retrievedEntries) {
items.push(createEntryFeedItem({
source: 'tunnelvision',
lorebook: entry.lorebook,
uid: entry.uid,
title: entry.title || `UID ${entry.uid ?? '?'}`,
timestamp,
}));
}
const display = TOOL_DISPLAY[invocation.name] || { icon: 'fa-gear', verb: 'Used', color: '#888' };
const summary = buildToolSummary(invocation.name, params, invocation.result || '', retrievedEntries);
items.push({
id: nextId++,
type: 'tool',
icon: display.icon,
verb: display.verb,
color: display.color,
summary,
timestamp,
retrievedEntries,
reasoning: invocation.reasoning || '',
});
// Accumulate for end-of-turn console summary
turnToolCalls.push({ name: invocation.name, verb: display.verb, summary });
}
addFeedItems(items);
}
function onToolCallsRendered(invocations) {
if (!Array.isArray(invocations) || invocations.length === 0) return;
if (!areTunnelVisionInvocations(invocations) || getSettings().stealthMode !== true) {
queueHiddenToolCallRefresh(false);
return;
}
const messageIndex = findRenderedToolCallMessageIndex(invocations);
if (messageIndex < 0) {
queueHiddenToolCallRefresh(false);
return;
}
const message = chat[messageIndex];
if (!message.extra) {
message.extra = {};
}
message.extra[HIDDEN_TOOL_CALL_FLAG] = true;
applyHiddenToolCallVisibility(messageIndex, true);
}
// ── Rendering ──
function getActiveTab() {
return panelEl?.querySelector('.tv-float-tab.active')?.dataset.tab || 'all';
}
function renderEmptyState(tab) {
if (!panelBody) return;
panelBody.replaceChildren();
const empty = el('div', 'tv-float-empty');
empty.appendChild(icon('fa-satellite-dish'));
let message = 'No activity yet';
let subMessage = 'Injected entries and tool calls will appear here during generation';
if (tab === 'tools') {
message = 'No tool calls yet';
subMessage = 'Tool calls will appear here during generation';
} else if (tab === 'wi') {
message = 'No injected entries yet';
subMessage = 'Native activations and TunnelVision retrievals will appear here';
}
empty.appendChild(el('span', null, message));
empty.appendChild(el('span', 'tv-float-empty-sub', subMessage));
panelBody.appendChild(empty);
}
function renderAllItems() {
if (!panelBody) return;
const tab = getActiveTab();
const filtered = feedItems.filter(item => {
if (tab === 'all') return true;
if (tab === 'wi') return item.type === 'entry' || item.type === 'wi';
if (tab === 'tools') return item.type === 'tool';
return true;
});
if (filtered.length === 0) {
renderEmptyState(tab);
return;
}
// Sort: active items first (by recency), constant entries pushed to bottom
const sorted = [...filtered].sort((a, b) => {
if (a.constant !== b.constant) return a.constant ? 1 : -1;
return 0; // preserve insertion order within each group
});
panelBody.replaceChildren();
for (const item of sorted) {
panelBody.appendChild(buildItemElement(item));
}
}
function buildItemElement(item) {
const rowClasses = ['tv-float-item'];
if (item._inProgress) rowClasses.push('tv-float-item-active');
if (item.type === 'entry') {
rowClasses.push('tv-float-item-entry');
rowClasses.push(item.source === 'native' ? 'tv-float-item-entry-native' : 'tv-float-item-entry-tv');
if (item.constant) rowClasses.push('tv-float-item-constant');
} else if (item.type === 'wi') {
// Legacy feed items from before the type rename
rowClasses.push('tv-float-item-wi');
}
const row = el('div', rowClasses.join(' '));
// Icon
const iconWrap = el('div', 'tv-float-item-icon');
iconWrap.style.color = item.color;
iconWrap.appendChild(icon(item.icon));
row.appendChild(iconWrap);
// Body
const body = el('div', 'tv-float-item-body');
const textRow = el('div', 'tv-float-item-row');
const verb = el('span', 'tv-float-item-verb', item.verb);
verb.style.color = item.color;
textRow.appendChild(verb);
// Source label — shows "Sidecar (model)" or nothing (chat model is the default/implied)
if (item.isSidecar) {
const modelText = item.sidecarModel ? `Sidecar: ${item.sidecarModel}` : 'Sidecar';
const srcLabel = el('span', 'tv-float-item-source', modelText);
srcLabel.title = item.sidecarModel || 'Sidecar LLM';
textRow.appendChild(srcLabel);
}
const summaryText = (item.type === 'entry')
? formatEntrySummary(item, shouldIncludeLorebookForEntries())
: (item.summary || '');
textRow.appendChild(el('span', 'tv-float-item-summary', summaryText));
body.appendChild(textRow);
if (item.reasoning) {
const detailsWrap = el('div', 'tv-float-item-details-wrap');
const toggle = el('span', 'tv-float-item-details-toggle', '▶ Details');
const details = el('div', 'tv-float-item-details');
if (item.reasoning) {
const reasonRow = el('div', 'tv-float-item-details-reason');
reasonRow.appendChild(el('span', 'tv-float-item-details-label', 'Reasoning: '));
reasonRow.appendChild(el('span', null, item.reasoning));
details.appendChild(reasonRow);
}
toggle.addEventListener('click', () => {
const open = details.classList.toggle('tv-float-item-details-open');
toggle.textContent = open ? '▼ Details' : '▶ Details';
});
detailsWrap.appendChild(toggle);
detailsWrap.appendChild(details);
body.appendChild(detailsWrap);
}
// Keys (for entry items)
if (item.keys?.length > 0) {
const keysRow = el('div', 'tv-float-item-keys');
const shown = item.keys.slice(0, 4);
for (const k of shown) {
keysRow.appendChild(el('span', 'tv-float-key-tag', k));
}
if (item.keys.length > 4) {
keysRow.appendChild(el('span', 'tv-float-key-more', `+${item.keys.length - 4}`));
}
body.appendChild(keysRow);
}
// Retrieved entries (for tool items from search)
if (item.type === 'tool' && item.retrievedEntries?.length) {
const entriesRow = el('div', 'tv-float-item-entries');
const uniqueBooks = new Set(item.retrievedEntries.map(entry => entry.lorebook).filter(Boolean));
const includeLorebook = uniqueBooks.size > 1;
const shown = item.retrievedEntries.slice(0, MAX_RENDERED_RETRIEVED_ENTRIES);
for (const entry of shown) {
const chip = el('div', 'tv-float-entry-tag', formatRetrievedEntryLabel(entry, includeLorebook));
chip.title = `${entry.lorebook || 'Lorebook'} | UID ${entry.uid ?? '?'}${entry.title ? ` | ${entry.title}` : ''}`;
entriesRow.appendChild(chip);
}
if (item.retrievedEntries.length > MAX_RENDERED_RETRIEVED_ENTRIES) {
const remaining = item.retrievedEntries.length - MAX_RENDERED_RETRIEVED_ENTRIES;
entriesRow.appendChild(
el('div', 'tv-float-entry-more', `+${remaining} more retrieved entr${remaining === 1 ? 'y' : 'ies'}`),
);
}
body.appendChild(entriesRow);
}
row.appendChild(body);
// Time
row.appendChild(el('div', 'tv-float-item-time', formatTime(item.timestamp)));
return row;
}
function updateBadge(count) {
if (!triggerEl || panelEl?.classList.contains('open')) return;
const current = parseInt(triggerEl.getAttribute('data-tv-count') || '0', 10);
triggerEl.setAttribute('data-tv-count', String(current + count));
}
function pulseTrigger() {
if (!triggerEl) return;
triggerEl.classList.add('tv-float-pulse');
setTimeout(() => triggerEl.classList.remove('tv-float-pulse'), 600);
}
/**
* Show a visual indicator on the trigger button that the sidecar is working.
* Call setSidecarActive(false) when the sidecar finishes.
*/
export function setSidecarActive(active) {
if (!triggerEl) return;
triggerEl.classList.toggle('tv-float-sidecar-active', active);
}
function trimFeed() {
if (feedItems.length > MAX_FEED_ITEMS) {
feedItems = feedItems.slice(0, MAX_FEED_ITEMS);
}
saveFeed();
}
function addFeedItems(items) {
if (!Array.isArray(items) || items.length === 0) return;
feedItems = [...items, ...feedItems];
trimFeed();
// Only count non-constant items in the badge — constant entries are always-on background noise
const badgeCount = items.filter(i => !i.constant).length;
if (badgeCount > 0) {
updateBadge(badgeCount);
pulseTrigger();
}
if (panelEl?.classList.contains('open')) renderAllItems();
}
// ── Hidden Tool Call (Visual Hiding) ──
export async function refreshHiddenToolCallMessages({ syncFlags = false } = {}) {
const hideMode = getSettings().stealthMode === true;
let flagsMutated = false;
for (let messageIndex = 0; messageIndex < chat.length; messageIndex++) {
const message = chat[messageIndex];
const invocations = Array.isArray(message?.extra?.tool_invocations) ? message.extra.tool_invocations : null;
if (!invocations?.length) continue;
const isPureTunnelVision = areTunnelVisionInvocations(invocations);
if (!message.extra) {
message.extra = {};
}
if (syncFlags && !isPureTunnelVision && message.extra[HIDDEN_TOOL_CALL_FLAG]) {
delete message.extra[HIDDEN_TOOL_CALL_FLAG];
flagsMutated = true;
}
if (syncFlags && hideMode && isPureTunnelVision && message.extra[HIDDEN_TOOL_CALL_FLAG] !== true) {
message.extra[HIDDEN_TOOL_CALL_FLAG] = true;
flagsMutated = true;
}
const shouldHide = hideMode
&& isPureTunnelVision
&& message.extra[HIDDEN_TOOL_CALL_FLAG] === true;
applyHiddenToolCallVisibility(messageIndex, shouldHide);
}
if (flagsMutated) {
await saveChatConditional();
}
}
function queueHiddenToolCallRefresh(syncFlags = false) {
hiddenToolCallRefreshNeedsSync = hiddenToolCallRefreshNeedsSync || syncFlags;
if (hiddenToolCallRefreshTimer !== null) return;
hiddenToolCallRefreshTimer = window.setTimeout(async () => {
const shouldSync = hiddenToolCallRefreshNeedsSync;
hiddenToolCallRefreshTimer = null;
hiddenToolCallRefreshNeedsSync = false;
await refreshHiddenToolCallMessages({ syncFlags: shouldSync });
}, 50);
}
function applyHiddenToolCallVisibility(messageIndex, shouldHide) {
const messageElement = document.querySelector(`.mes[mesid="${messageIndex}"]`);
if (!(messageElement instanceof HTMLElement)) return;
messageElement.classList.toggle('tv-hidden-tool-call', shouldHide);
if (shouldHide) {
messageElement.dataset.tvHiddenToolCalls = 'true';
} else {
delete messageElement.dataset.tvHiddenToolCalls;
}
}
function findRenderedToolCallMessageIndex(invocations) {
for (let messageIndex = chat.length - 1; messageIndex >= 0; messageIndex--) {
const messageInvocations = chat[messageIndex]?.extra?.tool_invocations;
if (!Array.isArray(messageInvocations)) continue;
if (messageInvocations === invocations || toolInvocationArraysMatch(messageInvocations, invocations)) {
return messageIndex;
}
}
return -1;
}
function toolInvocationArraysMatch(left, right) {
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
return false;
}
return left.every((leftInvocation, index) => {
const rightInvocation = right[index];
return leftInvocation?.name === rightInvocation?.name
&& String(leftInvocation?.id ?? '') === String(rightInvocation?.id ?? '')
&& normalizeInvocationField(leftInvocation?.parameters) === normalizeInvocationField(rightInvocation?.parameters);
});
}
function normalizeInvocationField(value) {
if (typeof value === 'string') return value;
if (value === undefined || value === null) return '';
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
function areTunnelVisionInvocations(invocations) {
return Array.isArray(invocations)
&& invocations.length > 0
&& invocations.every(invocation => ALL_TOOL_NAMES.includes(invocation?.name));
}
// ── Public API ──
export function clearFeed() {
feedItems = [];
saveFeed();
if (triggerEl) triggerEl.setAttribute('data-tv-count', '0');
if (panelEl?.classList.contains('open')) renderAllItems();
}
/**
* Clear the feed at the start of a new generation turn.
* Unlike clearFeed() (user-triggered), this is automatic and silent.
*/
function clearFeedForNewTurn() {
feedItems = [];
saveFeed();
if (triggerEl) triggerEl.setAttribute('data-tv-count', '0');
if (panelEl?.classList.contains('open')) renderAllItems();
console.debug('[TunnelVision] Activity feed cleared for new turn');
}
export function getFeedItems() {
return [...feedItems];
}
/**
* Log a tool call starting (in-progress) to the activity feed.
* Shows immediately with a pulsing "active" style. The in-progress item is
* automatically replaced when onToolCallsPerformed fires with the completed version.
* Called from tool-registry.js action wrappers.
* @param {string} toolName
* @param {object} [params]
*/
export function logToolCallStarted(toolName, params = {}) {
const display = TOOL_DISPLAY[toolName];
if (!display) return;
const summary = buildToolStartedSummary(toolName, params);
const item = {
id: nextId++,
type: 'tool',
icon: display.icon,
verb: display.activeVerb || `${display.verb}…`,
color: display.color,
summary,
timestamp: Date.now(),
_inProgress: true,
_toolName: toolName,
};
addFeedItems([item]);
}
/**
* Remove in-progress feed items for tools that have now completed.
* Called internally when TOOL_CALLS_PERFORMED fires.
* @param {string[]} completedToolNames
*/
function removeInProgressItems(completedToolNames) {
const nameSet = new Set(completedToolNames);
const before = feedItems.length;
feedItems = feedItems.filter(item => !(item._inProgress && nameSet.has(item._toolName)));
if (feedItems.length !== before) {
saveFeed();
if (panelEl?.classList.contains('open')) renderAllItems();
}
}
/**
* Build a brief summary for an in-progress tool call.
*/
function buildToolStartedSummary(toolName, params) {
switch (toolName) {
case 'TunnelVision_Search': {
const action = params.action || 'retrieve';
const nodeIds = Array.isArray(params.node_ids) ? params.node_ids : (params.node_id ? [params.node_id] : []);
if (action === 'search' && params.query) return `"${truncate(params.query, 40)}"`;
if (action === 'navigate' && nodeIds.length > 0) return `→ ${nodeIds[0]}`;
return nodeIds.length > 0 ? nodeIds.join(', ') : 'tree';
}
case 'TunnelVision_Remember':
return params.title ? `"${truncate(params.title, 50)}"` : 'new entry';
case 'TunnelVision_Update':
return params.uid ? `UID ${params.uid}` : 'entry';
case 'TunnelVision_Forget':
return params.uid ? `UID ${params.uid}` : 'entry';
case 'TunnelVision_Notebook':
return params.title ? `"${truncate(params.title, 40)}"` : (params.action || 'write');
default:
return '';
}
}
/**
* Log a sidecar write operation to the activity feed.
* Called by sidecar-writer.js after each successful operation.
* @param {'remember'|'update'} opType
* @param {Object} details
* @param {string} [details.lorebook]
* @param {string} [details.title]
* @param {number|null} [details.uid]
* @param {string} [details.summary]
* @param {string} [details.reasoning]
*/
export function logSidecarWrite(opType, { lorebook = '', title = '', uid = null, summary = '', reasoning = '' } = {}) {
const display = opType === 'remember'
? { icon: 'fa-brain', verb: 'Sidecar Remembered', color: '#a29bfe' }
: { icon: 'fa-pen', verb: 'Sidecar Updated', color: '#fdcb6e' };
const modelLabel = getSidecarModelLabel();
const items = [{
id: nextId++,
type: 'tool',
icon: display.icon,
verb: display.verb,
color: display.color,
summary: summary || title || `UID ${uid ?? '?'}`,
timestamp: Date.now(),
isSidecar: true,
sidecarModel: modelLabel,
reasoning,
}];
addFeedItems(items);
}
/**
* Log a sidecar auto-retrieval to the activity feed.
* Called by sidecar-retrieval.js after successful injection.
* @param {Object} details
* @param {string[]} [details.nodeIds] - The node IDs retrieved
* @param {number} [details.charCount] - Approximate character count injected
* @param {string} [details.reasoning]
*/
export function logSidecarRetrieval({ nodeIds = [], nodeLabels = [], charCount = 0, reasoning = '' } = {}) {
const labels = nodeLabels.length > 0 ? nodeLabels : nodeIds;
let summary;
if (labels.length === 1) {
summary = `"${labels[0]}"`;
} else if (labels.length <= 3) {
summary = labels.map(l => `"${l}"`).join(', ');
} else {
summary = `${labels.slice(0, 2).map(l => `"${l}"`).join(', ')} +${labels.length - 2} more`;
}
const modelLabel = getSidecarModelLabel();
const items = [{
id: nextId++,
type: 'tool',
icon: 'fa-satellite-dish',
verb: 'Sidecar Retrieved',
color: '#00b894',
summary,
timestamp: Date.now(),
isSidecar: true,
sidecarModel: modelLabel,
reasoning,
}];
addFeedItems(items);
}
/**
* Log conditional evaluation results to the activity feed.
* @param {Array<{ uid: number, accepted: boolean, reason: string }>} evaluations
* @param {Array<{ uid: number, title: string, primaryConditions: Array, secondaryConditions: Array }>} conditionalEntries
*/
export function logConditionalEvaluations(evaluations, conditionalEntries) {
if (!evaluations || evaluations.length === 0) return;
const modelLabel = getSidecarModelLabel();