forked from Coneja-Chibi/TunnelVision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostics.js
More file actions
1600 lines (1372 loc) · 64.4 KB
/
diagnostics.js
File metadata and controls
1600 lines (1372 loc) · 64.4 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 Diagnostics
* Checks every potential failure point and offers fixes.
*/
import { selected_world_info, world_names, loadWorldInfo, createWorldInfoEntry, saveWorldInfo } from '../../../world-info.js';
import { ToolManager } from '../../../tool-calling.js';
import { main_api, online_status, event_types, generateRaw, saveSettingsDebounced } from '../../../../script.js';
import { callGenericPopup, POPUP_TYPE } from '../../../popup.js';
import { extension_settings } from '../../../extensions.js';
import {
getTree,
createEmptyTree,
getAllEntryUids,
findNodeById,
getSettings,
saveTree,
deleteTree,
getBookDescription,
isTrackerTitle,
} from './tree-store.js';
import { getContext } from '../../../st-context.js';
import { getActiveTunnelVisionBooks, ALL_TOOL_NAMES, CONFIRMABLE_TOOLS, preflightToolRuntimeState } from './tool-registry.js';
import { hasEvaluableConditions, separateConditions, formatCondition, getKeywordProbability } from './conditions.js';
/**
* @typedef {Object} DiagResult
* @property {'pass'|'warn'|'fail'} status
* @property {string} message
* @property {string|null} fix - Description of auto-fix applied, or null
*/
/**
* Run all diagnostic checks.
* @returns {DiagResult[]}
*/
export async function runDiagnostics() {
const settingsBefore = JSON.stringify(getSettings());
const results = [];
results.push(checkSettingsExist());
results.push(checkApiConnected());
results.push(checkToolCallingSupport());
results.push(checkPromptPostProcessing());
results.push(...checkActiveLorebooksExist());
results.push(...checkTreesValid());
results.push(...await checkEntryUidsValid());
results.push(...checkNodeSummaries());
results.push(...checkNodeKeywords());
results.push(...checkDuplicateUids());
results.push(...await checkNearDuplicateEntries());
results.push(...await checkEmptyLorebooks());
results.push(...checkNodeIntegrity());
results.push(...await checkToolRuntimeDuringDiagnostics());
results.push(checkDisabledTools());
results.push(checkConfirmToolConfig());
results.push(checkToolPromptOverrides());
results.push(checkWorldInfoApi());
results.push(checkOrphanedTrees());
results.push(checkSearchMode());
results.push(checkSelectiveRetrieval());
results.push(checkRecurseLimit());
results.push(checkLlmBuildDetail());
results.push(checkLlmChunkSize());
results.push(checkVectorDedupConfig());
results.push(...checkSummariesNode());
results.push(...checkCollapsedTreeSize());
results.push(...checkOversizedLeafNodes());
results.push(...checkGranularityMismatch());
results.push(...checkLargeLorebookSettings());
results.push(...checkMultiDocConsistency());
results.push(checkPopupAvailability());
results.push(...checkActivityFeedEvent());
results.push(checkFeedPersistence());
results.push(checkGenerateRawAvailability());
results.push(checkWiSuppressionEvent());
results.push(checkChatIngestRequirements());
results.push(checkMandatoryToolsEvent());
results.push(checkPromptInjectionSettings());
results.push(checkCommandsConfig());
results.push(checkAutoSummaryConfig());
results.push(checkMultiBookMode());
results.push(await checkSidecarConfig());
results.push(...await checkTrackerUids());
results.push(...checkArcNodes());
results.push(checkNotebookConfig());
results.push(checkStealthMode());
results.push(checkCompactToolPrompts());
results.push(checkEphemeralResults());
results.push(checkAutoHideSummarized());
results.push(checkConstantPassthrough());
results.push(...checkBookDescriptions());
results.push(...checkBookPermissions());
results.push(checkSidecarAutoRetrieval());
results.push(checkConditionalTriggers());
results.push(...await checkKeywordProbabilities());
results.push(checkSidecarPostGenWriter());
results.push(checkTurnSummaryEvent());
const settingsAfter = JSON.stringify(getSettings());
if (settingsBefore !== settingsAfter) {
saveSettingsDebounced();
}
return results;
}
/** Check that extension settings are initialized. */
function checkSettingsExist() {
try {
const settings = getSettings();
if (settings && settings.trees && settings.enabledLorebooks) {
return pass('Extension settings initialized');
}
return fail('Extension settings missing or corrupt');
} catch (e) {
return fail(`Settings check error: ${e.message}`);
}
}
/** Check that an API is connected (needed for generateRaw calls during tree building). */
function checkApiConnected() {
if (!main_api) {
return fail('No API selected. TunnelVision needs an API connection for LLM tree building.');
}
if (online_status === 'no_connection') {
return warn('API is not connected. Tree building with LLM and summary generation will fail.');
}
return pass(`API connected (${main_api})`);
}
/** Check that the current API/model supports tool calling with specific guidance. */
function checkToolCallingSupport() {
try {
const supported = ToolManager.isToolCallingSupported();
if (supported) {
return pass('Current API supports tool calling');
}
// Give specific guidance on what's wrong
if (main_api !== 'openai') {
return fail('Tool calling requires Chat Completion mode. Your current API (' + main_api + ') is Text Completion. Switch to a Chat Completion API (OpenAI, Claude, Gemini, etc.) in ST connection settings.');
}
const context = getContext();
const ccSettings = context.chatCompletionSettings;
if (ccSettings && !ccSettings.function_calling) {
return fail('"Enable function calling" is OFF in your Chat Completion preset. Turn it on: AI Response Configuration → Function Calling → Enable.');
}
return warn('Current API/model may not support tool calling. Check your model supports function calls.');
} catch (e) {
return warn('Could not verify tool calling support: ' + e.message);
}
}
/** Check that Prompt Post-Processing mode is compatible with tool calling. */
function checkPromptPostProcessing() {
try {
if (main_api !== 'openai') {
return pass('PPP check not applicable for non-Chat Completion APIs');
}
const context = getContext();
const ccSettings = context.chatCompletionSettings;
if (!ccSettings) {
return warn('Could not read Chat Completion settings to check PPP mode.');
}
const ppp = ccSettings.custom_prompt_post_processing;
// These are the modes that preserve tool calls in the prompt
const toolCompatible = ['', 'merge_tools', 'semi_tools', 'strict_tools'];
if (toolCompatible.includes(ppp)) {
return pass('Prompt Post-Processing mode (' + (ppp || 'None') + ') is compatible with tool calling');
}
return warn('Prompt Post-Processing is set to "' + ppp + '" which strips tool calls from the prompt. Switch to a *_tools variant (e.g. "merge_tools") or "None" for TunnelVision to work.');
} catch (e) {
return warn('Could not check PPP mode: ' + e.message);
}
}
/** Check that enabled lorebooks actually exist and are active (global, character, or chat). */
function checkActiveLorebooksExist() {
const results = [];
const settings = getSettings();
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of Object.keys(settings.enabledLorebooks)) {
if (!settings.enabledLorebooks[bookName]) continue;
if (!world_names?.includes(bookName)) {
results.push(fail(`Enabled lorebook "${bookName}" does not exist. Disabling.`));
settings.enabledLorebooks[bookName] = false;
} else if (!activeBooks.includes(bookName)) {
results.push(warn(`Lorebook "${bookName}" has TunnelVision enabled but is not active in current chat (not found in global, character, or chat lorebooks).`));
} else {
results.push(pass(`Lorebook "${bookName}" exists and is active`));
}
}
if (results.length === 0) {
results.push(warn('No lorebooks have TunnelVision enabled'));
}
return results;
}
/** Check that tree structures are valid for enabled lorebooks. */
function checkTreesValid() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree) {
results.push(fail(`Lorebook "${bookName}" is enabled but has no tree index. Build one first.`));
continue;
}
if (!tree.root) {
results.push(fail(`Tree for "${bookName}" has no root node. Rebuilding empty tree.`));
saveTree(bookName, createEmptyTree(bookName));
continue;
}
if ((tree.root.children || []).length === 0 && (tree.root.entryUids || []).length === 0) {
results.push(warn(`Tree for "${bookName}" is empty. Add categories and assign entries.`));
} else {
const totalEntries = getAllEntryUids(tree.root).length;
results.push(pass(`Tree for "${bookName}" has ${tree.root.children.length} categories, ${totalEntries} entries`));
}
}
return results;
}
/** Check that entry UIDs in trees still exist in their lorebooks. */
async function checkEntryUidsValid() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
const bookData = await loadWorldInfo(bookName);
if (!bookData || !bookData.entries) continue;
const validUids = new Set();
for (const key of Object.keys(bookData.entries)) {
validUids.add(bookData.entries[key].uid);
}
const treeUids = getAllEntryUids(tree.root);
const staleUids = treeUids.filter(uid => !validUids.has(uid));
if (staleUids.length > 0) {
results.push(warn(`Tree for "${bookName}" has ${staleUids.length} stale entry reference(s). These entries may have been deleted from the lorebook.`));
// Auto-fix: remove stale UIDs
removeStaleUids(tree.root, validUids);
saveTree(bookName, tree);
results.push(pass(`Auto-removed ${staleUids.length} stale reference(s) from "${bookName}" tree`));
} else if (treeUids.length > 0) {
results.push(pass(`All ${treeUids.length} entry references in "${bookName}" tree are valid`));
}
// Check for unindexed entries
const indexedUids = new Set(treeUids);
const unindexed = [...validUids].filter(uid => !indexedUids.has(uid));
if (unindexed.length > 0) {
results.push(warn(`${unindexed.length} entries in "${bookName}" are not assigned to any tree node`));
}
}
return results;
}
/** Check tool runtime state using preflightToolRuntimeState — repairs and validates registration. */
async function checkToolRuntimeDuringDiagnostics() {
const snapshot = await preflightToolRuntimeState({ repair: true, reason: 'diagnostics', log: true });
const results = [];
if (snapshot.activeBooks.length === 0) {
results.push(pass('No active TunnelVision lorebooks — tools correctly unregistered'));
results.push(pass('No active TunnelVision lorebooks — no ST stealth flags to validate'));
results.push(pass('No active TunnelVision lorebooks — no next-generation TunnelVision tools required'));
return results;
}
if (snapshot.expectedToolNames.length === 0) {
results.push(warn('All enabled TunnelVision tools are disabled, so none are expected to be registered.'));
results.push(pass('No enabled TunnelVision tools use ST stealth'));
results.push(warn('Next-generation tool eligibility: no enabled TunnelVision tools are available because they are disabled in settings.'));
return results;
}
if (snapshot.missingToolNames.length === 0) {
const suffix = snapshot.repairApplied ? ' (recovered during diagnostics)' : '';
results.push(pass(`All ${snapshot.expectedToolNames.length} enabled TunnelVision tools registered${suffix}`));
} else if (snapshot.registeredToolNames.length === 0) {
results.push(fail(`No enabled TunnelVision tools are registered. Missing: ${snapshot.missingToolNames.join(', ')}`));
} else {
results.push(warn(`${snapshot.missingToolNames.length} enabled TunnelVision tool(s) not registered: ${snapshot.missingToolNames.join(', ')}`));
}
if (snapshot.stealthToolNames.length === 0) {
results.push(pass('No enabled TunnelVision tools use ST stealth'));
} else {
results.push(fail(`Enabled TunnelVision tools still using ST stealth: ${snapshot.stealthToolNames.join(', ')}`));
}
if (snapshot.eligibilityErrors.length > 0) {
results.push(fail(`Next-generation tool eligibility check failed: ${snapshot.eligibilityErrors.join(' | ')}`));
} else if (snapshot.eligibleToolNames.length > 0) {
results.push(pass(`Next-generation tool eligibility: ${snapshot.eligibleToolNames.length} tool(s) available (${snapshot.eligibleToolNames.join(', ')})`));
} else {
results.push(fail('Next-generation tool eligibility: no TunnelVision tools would be offered on the next generation despite active lorebooks.'));
}
return results;
}
/** Report which tools the user has manually disabled via Advanced Settings. */
function checkDisabledTools() {
const settings = getSettings();
const disabled = settings.disabledTools || {};
const disabledNames = ALL_TOOL_NAMES.filter(name => disabled[name]);
if (disabledNames.length === 0) {
return pass('All TunnelVision tools enabled');
}
if (disabledNames.length === ALL_TOOL_NAMES.length) {
return warn('All TunnelVision tools are disabled in Advanced Settings. The AI cannot use any memory features.');
}
return warn(`${disabledNames.length} tool(s) disabled: ${disabledNames.map(n => n.replace('TunnelVision_', '')).join(', ')}`);
}
/** Check confirm tool configuration for invalid references. */
function checkConfirmToolConfig() {
const settings = getSettings();
const confirmTools = settings.confirmTools || {};
const enabledNames = Object.keys(confirmTools).filter(name => confirmTools[name]);
if (enabledNames.length === 0) {
return pass('Tool confirmation: none enabled');
}
const invalid = enabledNames.filter(name => !ALL_TOOL_NAMES.includes(name));
if (invalid.length > 0) {
// Auto-fix: remove invalid references
for (const name of invalid) {
delete confirmTools[name];
}
return warn(`Tool confirmation: removed ${invalid.length} invalid tool reference(s): ${invalid.join(', ')}`);
}
const notConfirmable = enabledNames.filter(name => !CONFIRMABLE_TOOLS.has(name));
if (notConfirmable.length > 0) {
// Auto-fix: remove non-confirmable tool references
for (const name of notConfirmable) {
delete confirmTools[name];
}
return warn(`Tool confirmation: removed ${notConfirmable.length} non-confirmable tool reference(s): ${notConfirmable.map(n => n.replace('TunnelVision_', '')).join(', ')}`);
}
return pass(`Tool confirmation: ${enabledNames.length} tool(s) require approval (${enabledNames.map(n => n.replace('TunnelVision_', '')).join(', ')})`);
}
/** Check tool prompt overrides for invalid or empty entries. */
function checkToolPromptOverrides() {
const settings = getSettings();
const overrides = settings.toolPromptOverrides || {};
const overrideNames = Object.keys(overrides);
if (overrideNames.length === 0) {
return pass('Tool prompt overrides: none configured');
}
let fixed = 0;
// Auto-fix: remove empty string overrides
for (const name of overrideNames) {
if (typeof overrides[name] === 'string' && overrides[name].trim() === '') {
delete overrides[name];
fixed++;
}
}
// Auto-fix: strip stale baked-in dynamic content (tree overview, tracker list) from overrides
for (const name of Object.keys(overrides)) {
const val = overrides[name];
if (typeof val !== 'string') continue;
// New delimiter format
let cutIdx = val.indexOf('---TV_DYNAMIC_BELOW---');
// Legacy: tree overview baked in before delimiter was introduced
if (cutIdx < 0) cutIdx = val.indexOf('\n\nFull tree index:\n');
if (cutIdx < 0) cutIdx = val.indexOf('\n\nTop-level tree:\n');
if (cutIdx >= 0) {
const cleaned = val.substring(0, cutIdx).trimEnd();
if (cleaned) {
overrides[name] = cleaned;
} else {
delete overrides[name];
}
fixed++;
}
}
// Warn about invalid tool names
const remaining = Object.keys(overrides);
const invalid = remaining.filter(name => !ALL_TOOL_NAMES.includes(name));
for (const name of invalid) {
delete overrides[name];
fixed++;
}
if (fixed > 0) {
const activeCount = Object.keys(overrides).length;
return warn(`Tool prompt overrides: auto-removed ${fixed} invalid/empty override(s). ${activeCount} valid override(s) remaining.`);
}
return pass(`Tool prompt overrides: ${remaining.length} tool(s) have custom descriptions`);
}
/** Check that tree nodes have LLM-generated summaries (PageIndex pattern). */
function checkNodeSummaries() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
let totalNodes = 0;
let nodesWithSummary = 0;
function countSummaries(node) {
const children = node.children || [];
const entryUids = node.entryUids || [];
if (children.length > 0 || entryUids.length > 0) {
totalNodes++;
if (node.summary && node.summary.trim().length > 0) {
nodesWithSummary++;
}
}
for (const child of children) countSummaries(child);
}
countSummaries(tree.root);
if (totalNodes === 0) continue;
const pct = Math.round((nodesWithSummary / totalNodes) * 100);
if (pct === 100) {
results.push(pass(`All ${totalNodes} nodes in "${bookName}" have LLM summaries`));
} else if (pct >= 50) {
results.push(warn(`${nodesWithSummary}/${totalNodes} nodes in "${bookName}" have summaries (${pct}%). Rebuild with LLM for better retrieval.`));
} else {
results.push(warn(`Only ${nodesWithSummary}/${totalNodes} nodes in "${bookName}" have summaries. Tree traversal quality will be poor without summaries. Use "Build With LLM" to generate them.`));
}
}
return results;
}
/** Check that node summaries include keyword footers for better AI retrieval. */
function checkNodeKeywords() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
let nodesWithEntries = 0;
let nodesWithKeywords = 0;
function checkNode(node, isRoot) {
if (isRoot) {
for (const child of (node.children || [])) checkNode(child, false);
return;
}
const uids = getAllEntryUids(node);
if (uids.length > 0 && node.summary) {
nodesWithEntries++;
if (/\[Keywords:/.test(node.summary)) {
nodesWithKeywords++;
}
}
for (const child of (node.children || [])) checkNode(child, false);
}
checkNode(tree.root, true);
if (nodesWithEntries === 0) continue;
const pct = Math.round((nodesWithKeywords / nodesWithEntries) * 100);
if (pct === 100) {
results.push(pass(`All ${nodesWithEntries} nodes with entries in "${bookName}" have keyword footers`));
} else if (pct > 0) {
results.push(warn(`${nodesWithKeywords}/${nodesWithEntries} nodes in "${bookName}" have keyword footers (${pct}%). Rebuild with LLM to add keywords for better retrieval.`));
} else {
results.push(warn(`No nodes in "${bookName}" have keyword footers. Rebuild with LLM to add entry keywords to node summaries for improved AI navigation.`));
}
}
return results;
}
/** Check for entries assigned to multiple nodes (causes duplicate retrieval). */
function checkDuplicateUids() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
const seen = new Map();
function walk(node) {
for (const uid of (node.entryUids || [])) {
if (seen.has(uid)) {
seen.get(uid).push(node.label || node.id);
} else {
seen.set(uid, [node.label || node.id]);
}
}
for (const child of (node.children || [])) walk(child);
}
walk(tree.root);
const dupes = [...seen.entries()].filter(([, nodes]) => nodes.length > 1);
if (dupes.length > 0) {
results.push(warn(`"${bookName}" has ${dupes.length} entry/entries assigned to multiple nodes. This causes duplicate content in retrieval.`));
}
}
return results;
}
/** Check for near-duplicate entry titles that suggest redundant content. */
async function checkNearDuplicateEntries() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const bookData = await loadWorldInfo(bookName);
if (!bookData?.entries) continue;
// Collect all non-disabled entry titles
const entries = [];
for (const key of Object.keys(bookData.entries)) {
const entry = bookData.entries[key];
if (entry.disable) continue;
const title = (entry.comment || entry.key?.[0] || '').trim().toLowerCase();
if (title) {
entries.push({ uid: entry.uid, title, original: entry.comment || entry.key?.[0] || `#${entry.uid}` });
}
}
// Simple O(n²) check for very similar titles (small n per lorebook, so acceptable)
const dupes = [];
const seen = new Set();
for (let i = 0; i < entries.length; i++) {
if (seen.has(i)) continue;
for (let j = i + 1; j < entries.length; j++) {
if (seen.has(j)) continue;
if (titlesAreSimilar(entries[i].title, entries[j].title)) {
dupes.push(`"${entries[i].original}" (UID ${entries[i].uid}) ≈ "${entries[j].original}" (UID ${entries[j].uid})`);
seen.add(j);
}
}
}
if (dupes.length > 0) {
results.push(warn(`Lorebook "${bookName}" has ${dupes.length} near-duplicate entry pair(s): ${dupes.slice(0, 3).join('; ')}${dupes.length > 3 ? ` (+${dupes.length - 3} more)` : ''}. Consider merging with TunnelVision_MergeSplit or manually.`));
}
}
if (results.length === 0 && activeBooks.length > 0) {
results.push(pass('No near-duplicate entry titles detected'));
}
return results;
}
/**
* Check if two titles are similar enough to be potential duplicates.
* Uses simple heuristics: exact match after normalization, or one contains the other.
* @param {string} a - Normalized (lowercase, trimmed) title
* @param {string} b - Normalized (lowercase, trimmed) title
* @returns {boolean}
*/
function titlesAreSimilar(a, b) {
if (a === b) return true;
// One contains the other and they're close in length
if (a.length > 3 && b.length > 3) {
if (a.includes(b) || b.includes(a)) {
const lenRatio = Math.min(a.length, b.length) / Math.max(a.length, b.length);
return lenRatio > 0.6;
}
}
return false;
}
/** Check that enabled lorebooks actually have active (non-disabled) entries. */
async function checkEmptyLorebooks() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const bookData = await loadWorldInfo(bookName);
if (!bookData || !bookData.entries) {
results.push(fail(`Lorebook "${bookName}" has no entry data. TunnelVision cannot index it.`));
continue;
}
const activeEntries = Object.keys(bookData.entries).filter(
key => !bookData.entries[key].disable,
);
if (activeEntries.length === 0) {
results.push(warn(`Lorebook "${bookName}" has no active entries. All entries are disabled.`));
}
}
return results;
}
/** Check that all tree nodes have required fields (catches import corruption). */
function checkNodeIntegrity() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
let corruptNodes = 0;
let fixed = 0;
function walk(node) {
if (!node.id || typeof node.id !== 'string') {
corruptNodes++;
return;
}
if (!Array.isArray(node.children)) { node.children = []; fixed++; }
if (!Array.isArray(node.entryUids)) { node.entryUids = []; fixed++; }
if (typeof node.label !== 'string') { node.label = 'Unnamed'; fixed++; }
for (const child of node.children) walk(child);
}
walk(tree.root);
if (fixed > 0) {
saveTree(bookName, tree);
results.push(warn(`Auto-fixed ${fixed} missing field(s) in "${bookName}" tree nodes (possibly from import).`));
}
if (corruptNodes > 0) {
results.push(fail(`"${bookName}" tree has ${corruptNodes} node(s) without valid IDs. Rebuild the tree.`));
}
}
return results;
}
/** Check that ST's world-info API functions are available (needed by entry-manager). */
function checkWorldInfoApi() {
const missing = [];
if (typeof createWorldInfoEntry !== 'function') missing.push('createWorldInfoEntry');
if (typeof saveWorldInfo !== 'function') missing.push('saveWorldInfo');
if (typeof loadWorldInfo !== 'function') missing.push('loadWorldInfo');
if (missing.length > 0) {
return fail(`Missing ST world-info API: ${missing.join(', ')}. Memory tools (Remember, Update, Forget) will fail.`);
}
return pass('ST world-info API available for memory tools');
}
/** Check for trees belonging to lorebooks that no longer exist. */
function checkOrphanedTrees() {
const settings = getSettings();
const orphaned = [];
for (const bookName of Object.keys(settings.trees)) {
if (!world_names?.includes(bookName)) {
orphaned.push(bookName);
}
}
if (orphaned.length > 0) {
return {
status: 'warn',
message: `Found ${orphaned.length} tree(s) for non-existent lorebooks: ${orphaned.join(', ')}. These can be safely deleted.`,
fix: () => {
for (const name of orphaned) {
deleteTree(name);
}
return `Deleted ${orphaned.length} orphaned tree(s).`;
},
fixLabel: 'Delete Orphaned Trees',
};
}
return pass('No orphaned trees found');
}
/** Check that search mode is a valid value. Auto-fix if corrupted. */
function checkSearchMode() {
const settings = getSettings();
const valid = ['traversal', 'collapsed'];
if (valid.includes(settings.searchMode)) {
return pass(`Search mode: ${settings.searchMode}`);
}
const oldValue = settings.searchMode;
settings.searchMode = 'traversal';
return warn(`Invalid search mode "${oldValue}". Auto-reset to "traversal".`);
}
/** Check selective retrieval configuration. */
function checkSelectiveRetrieval() {
const settings = getSettings();
if (!settings.selectiveRetrieval) {
return pass('Selective retrieval: off (all entries injected on retrieve)');
}
const limit = Number(settings.recurseLimit) || 5;
if (limit < 3) {
return warn('Selective retrieval is on but recurse limit is below 3. The model needs at least 2 calls per leaf (manifest + entry pick). Consider raising the recurse limit.');
}
return pass('Selective retrieval: on (model picks individual entries from manifests)');
}
/** Check that recurse limit is sane. Warn if very high. */
function checkRecurseLimit() {
const settings = getSettings();
const limit = Number(settings.recurseLimit);
if (!isFinite(limit) || limit < 1) {
settings.recurseLimit = 5;
return warn('Recurse limit was invalid. Auto-reset to default (5).');
}
if (limit > 50) {
settings.recurseLimit = 50;
return warn(`Recurse limit was ${limit} (max 50). Clamped to 50.`);
}
if (limit > 15) {
return warn(`Recurse limit is ${limit}. High values increase API costs and latency. Only needed for very deep trees.`);
}
return pass(`Recurse limit: ${limit}`);
}
/** Check that LLM build detail level is a valid value. Auto-fix if corrupted. */
function checkLlmBuildDetail() {
const settings = getSettings();
const valid = ['full', 'lite', 'names'];
if (valid.includes(settings.llmBuildDetail)) {
return pass(`LLM build detail: ${settings.llmBuildDetail}`);
}
const oldValue = settings.llmBuildDetail;
settings.llmBuildDetail = 'full';
return warn(`Invalid LLM build detail "${oldValue}". Auto-reset to "full".`);
}
/** Check that LLM chunk size is a valid number. Auto-fix if corrupted. */
function checkLlmChunkSize() {
const settings = getSettings();
const size = Number(settings.llmChunkTokens);
if (!isFinite(size) || size < 1000) {
const oldValue = settings.llmChunkTokens;
settings.llmChunkTokens = 30000;
return warn(`LLM chunk size was invalid (${oldValue}). Auto-reset to 30,000 chars.`);
}
if (size > 500000) {
settings.llmChunkTokens = 500000;
return warn(`LLM chunk size was ${size} (max 500,000). Clamped to 500,000.`);
}
if (size < 5000) {
return warn(`LLM chunk size is ${size} chars. Very small chunks mean many LLM calls during tree building, increasing cost and time.`);
}
return pass(`LLM chunk size: ${size.toLocaleString()} chars`);
}
/** Check that dedup config is valid when enabled. */
function checkVectorDedupConfig() {
const settings = getSettings();
if (!settings.enableVectorDedup) {
return pass('Duplicate detection: disabled');
}
const threshold = Number(settings.vectorDedupThreshold);
if (!isFinite(threshold) || threshold < 0.1 || threshold > 1.0) {
const oldValue = settings.vectorDedupThreshold;
settings.vectorDedupThreshold = 0.85;
return warn(`Dedup threshold was invalid (${oldValue}). Auto-reset to 0.85.`);
}
if (threshold < 0.5) {
return warn(`Dedup threshold is ${threshold}. Very low thresholds will flag many entries as duplicates, creating noise.`);
}
return pass(`Duplicate detection: enabled (trigram similarity, threshold ${threshold})`);
}
/** Check that active lorebooks have a "Summaries" node for the Summarize tool. */
function checkSummariesNode() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
const summariesNode = (tree.root.children || []).find(
c => c.label === 'Summaries',
);
if (!summariesNode) {
results.push(warn(`"${bookName}" has no "Summaries" category. The Summarize tool will auto-create one on first use, but you may want to create it manually for better organization.`));
} else {
const count = getAllEntryUids(summariesNode).length;
results.push(pass(`"${bookName}" has Summaries node (${count} entries)`));
}
}
return results;
}
/** Check if collapsed-tree overview would be truncated (too many nodes). */
function checkCollapsedTreeSize() {
const results = [];
const settings = getSettings();
if (settings.searchMode !== 'collapsed') return results;
const activeBooks = getActiveTunnelVisionBooks();
const MAX_LEN = 6000;
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
// Estimate overview size: ~80 chars per node (ID + label + summary snippet + indent)
let nodeCount = 0;
function count(node) {
nodeCount++;
for (const child of (node.children || [])) count(child);
}
count(tree.root);
const estimate = nodeCount * 80;
if (estimate > MAX_LEN) {
const depth = settings.collapsedDepth ?? 2;
results.push(warn(`"${bookName}" tree has ${nodeCount} nodes. In collapsed mode, the overview may be truncated (est. ${estimate} chars vs ${MAX_LEN} limit). Try lowering Collapsed Tree Depth (currently ${depth}) or using traversal mode.`));
}
}
return results;
}
/** Check for leaf nodes with too many entries, suggesting a rebuild with higher granularity. */
function checkOversizedLeafNodes() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
const settings = getSettings();
const granularity = Number(settings.treeGranularity) || 0;
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
const oversized = [];
const threshold = 20; // flag leaves with 20+ entries regardless of granularity
function scan(node) {
const isLeaf = (node.children || []).length === 0;
if (isLeaf && (node.entryUids || []).length > threshold) {
oversized.push({ label: node.label, count: node.entryUids.length });
}
for (const child of (node.children || [])) scan(child);
}
scan(tree.root);
if (oversized.length > 0) {
const top3 = oversized.sort((a, b) => b.count - a.count).slice(0, 3);
const examples = top3.map(n => `"${n.label}" (${n.count})`).join(', ');
const hint = granularity < 3
? ' Try rebuilding with higher Tree Granularity (Detailed or Extensive).'
: ' Consider rebuilding the tree or manually splitting these categories.';
results.push(warn(`"${bookName}" has ${oversized.length} leaf node(s) with 20+ entries: ${examples}.${hint}`));
}
}
return results;
}
/** Check if granularity setting matches lorebook size. */
function checkGranularityMismatch() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
const settings = getSettings();
const granularity = Number(settings.treeGranularity) || 0;
if (granularity === 0) return results; // auto mode, no mismatch possible
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
const totalEntries = getAllEntryUids(tree.root).length;
let recommended;
if (totalEntries >= 3000) recommended = 4;
else if (totalEntries >= 1000) recommended = 3;
else if (totalEntries >= 200) recommended = 2;
else recommended = 1;
if (granularity < recommended - 1) {
const labels = { 1: 'Minimal', 2: 'Moderate', 3: 'Detailed', 4: 'Extensive' };
results.push(warn(`"${bookName}" has ${totalEntries} entries but granularity is set to ${labels[granularity] || granularity}. With this many entries, more splitting is recommended (${labels[recommended]} or Auto). Rebuild to apply.`));
}
}
return results;
}
/** Check if large lorebooks are using settings that will cause issues at build/retrieval time. */
function checkLargeLorebookSettings() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
const settings = getSettings();
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (!tree || !tree.root) continue;
const totalEntries = getAllEntryUids(tree.root).length;
if (totalEntries < 500) continue; // only check large lorebooks
// Warn if using traversal mode with large lorebooks — collapsed is better
if ((settings.searchMode || 'traversal') === 'traversal') {
results.push(warn(`"${bookName}" has ${totalEntries} entries. Collapsed Tree mode is recommended for large lorebooks — it reduces tool calls and improves retrieval accuracy. Change in Advanced Settings > Search Mode.`));
}
// Warn if collapsed depth is too high for large lorebooks
if ((settings.searchMode || 'traversal') === 'collapsed') {
const depth = settings.collapsedDepth ?? 2;
if (depth > 2 && totalEntries >= 1000) {
results.push(warn(`"${bookName}" has ${totalEntries} entries with collapsed depth set to ${depth}. This may produce a very large tool description. Try depth 1-2 for lorebooks this size.`));
}
}
// Warn if chunk size is very small for large lorebooks (will cause many sequential LLM calls)
const chunkLimit = settings.llmChunkTokens || 30000;
if (chunkLimit < 20000 && totalEntries >= 1000) {
results.push(warn(`LLM Chunk Size is ${chunkLimit.toLocaleString()} chars with ${totalEntries} entries in "${bookName}". This will cause many LLM calls during tree building. Consider raising to 30,000+ for faster builds.`));
}
}
return results;
}
/** When multiple lorebooks are active, check all have valid trees (multi-doc mode). */
function checkMultiDocConsistency() {
const results = [];
const activeBooks = getActiveTunnelVisionBooks();
if (activeBooks.length <= 1) return results;
let booksWithTrees = 0;
let booksWithoutTrees = 0;
for (const bookName of activeBooks) {
const tree = getTree(bookName);
if (tree && tree.root && ((tree.root.children || []).length > 0 || (tree.root.entryUids || []).length > 0)) {
booksWithTrees++;
} else {
booksWithoutTrees++;
}
}
if (booksWithoutTrees > 0 && booksWithTrees > 0) {
results.push(warn(`Multi-document mode: ${booksWithoutTrees} of ${activeBooks.length} active lorebooks have no tree index. The AI can only search lorebooks with built trees.`));
} else if (booksWithTrees === activeBooks.length) {
results.push(pass(`Multi-document mode: all ${activeBooks.length} lorebooks have valid trees`));
}
return results;
}
/** Check that ST's popup system is available for the tree editor. */
function checkPopupAvailability() {
if (typeof callGenericPopup !== 'function') {
return fail('ST popup system (callGenericPopup) not available. Tree editor popup will not work.');
}
if (!POPUP_TYPE || POPUP_TYPE.DISPLAY === undefined) {
return warn('POPUP_TYPE.DISPLAY not found. Tree editor popup may not render correctly.');
}
return pass('ST popup system available for tree editor');
}
/** Check that activity feed events exist. */
function checkActivityFeedEvent() {
const results = [];
if (!event_types || !event_types.WORLD_INFO_ACTIVATED) {
results.push(warn('event_types.WORLD_INFO_ACTIVATED not found. Activity feed will not show triggered worldbook entries.'));
} else {
results.push(pass('WORLD_INFO_ACTIVATED event available for entry tracking'));
}
if (!event_types || !event_types.TOOL_CALLS_PERFORMED) {
results.push(warn('event_types.TOOL_CALLS_PERFORMED not found. Activity feed will not show real-time tool calls.'));