-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdebugger.js
More file actions
344 lines (291 loc) · 11.2 KB
/
debugger.js
File metadata and controls
344 lines (291 loc) · 11.2 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
// =============================================================================
// CARROT DEBUG MODULE 🥕
// Centralized debug system with organized categories using CARROT 🥕 BNY: naming
// =============================================================================
export class CarrotDebugger {
constructor() {
this.enabled = false;
this.logSequence = 0;
this.categories = {
INIT: { emoji: '🌱', color: '#4caf50', name: 'Initialization' },
SCAN: { emoji: '🔍', color: '#2196f3', name: 'Character Scanning' },
INJECT: { emoji: '💉', color: '#ff6b35', name: 'AI Injection' },
UI: { emoji: '🎨', color: '#9c27b0', name: 'User Interface' },
REPO: { emoji: '📚', color: '#ff9800', name: 'Repository Management' },
ERROR: { emoji: '❌', color: '#f44336', name: 'Critical Errors' }
};
// Performance tracking
this.timers = new Map();
this.metrics = new Map();
}
setEnabled(enabled) {
this.enabled = enabled;
if (enabled) {
this.init('🥕 CarrotKernel Debug Mode ENABLED');
this.showDebugInfo();
} else {
this.init('🥕 CarrotKernel Debug Mode DISABLED');
}
}
showDebugInfo() {
console.group('🥕 CARROT BNY: DEBUG SYSTEM');
console.log('📊 Available Categories:');
Object.entries(this.categories).forEach(([key, cat]) => {
console.log(` ${cat.emoji} ${key}: ${cat.name}`);
});
console.log('🎯 Usage: CarrotDebug.[category]("message", data)');
console.groupEnd();
}
_log(category, message, data = null) {
if (!this.enabled) return;
const cat = this.categories[category];
if (!cat) {
console.error('🥕 CARROT BNY: INVALID CATEGORY', category);
return;
}
const logId = ++this.logSequence;
const prefix = `🥕 CARROT ${cat.emoji} BNY: ${category}`;
console.group(`%c${prefix} #${logId}`, `color: ${cat.color}; font-weight: bold;`);
console.log(`%c${message}`, `color: ${cat.color};`);
if (data !== null) {
if (typeof data === 'object') {
console.table ? console.table(data) : console.log(data);
} else {
console.log('📋 Data:', data);
}
}
console.groupEnd();
}
// Performance timers
startTimer(name, category = 'INIT') {
const key = `${category}:${name}`;
this.timers.set(key, performance.now());
this._log(category, `⏱️ Timer Started: ${name}`);
}
endTimer(name, category = 'INIT') {
const key = `${category}:${name}`;
const startTime = this.timers.get(key);
if (!startTime) {
this.error(`Timer '${name}' not found`);
return;
}
const duration = performance.now() - startTime;
this.timers.delete(key);
this._log(category, `⏱️ Timer Ended: ${name} (${duration.toFixed(2)}ms)`);
return duration;
}
// Category-specific debug functions
init(message, data = null) { this._log('INIT', message, data); }
scan(message, data = null) { this._log('SCAN', message, data); }
inject(message, data = null) { this._log('INJECT', message, data); }
ui(message, data = null) { this._log('UI', message, data); }
repo(message, data = null) { this._log('REPO', message, data); }
error(message, data = null) {
// IMPORTANT: Respect enabled/debugMode settings - only log errors if debug is on
if (!this.enabled) return;
const cat = this.categories.ERROR;
const prefix = `🥕 CARROT ${cat.emoji} BNY: ERROR`;
console.group(`%c${prefix}`, `color: ${cat.color}; font-weight: bold; background: #ffe6e6;`);
console.error(`%c${message}`, `color: ${cat.color}; font-weight: bold;`);
if (data !== null) {
console.error('💥 Error Data:', data);
}
console.trace('🥕 Stack Trace');
console.groupEnd();
}
// Specialized debug functions
characters(detected, context = 'chat') {
if (!this.enabled) return;
this.scan(`Character Detection in ${context}`, {
count: detected.size,
characters: Array.from(detected),
context: context
});
}
lorebook(name, type, entries = 0) {
this.scan(`Lorebook Processed: ${name}`, {
type: type,
entries: entries,
timestamp: new Date().toISOString()
});
}
injection(characters, injectionData) {
this.inject('AI Injection Process', {
targetCharacters: Array.from(characters),
injectionSize: injectionData.length,
preview: injectionData.substring(0, 100) + '...'
});
}
tutorial(action, tutorialId, step = null) {
this.ui(`Tutorial ${action}: ${tutorialId}`, {
step: step,
timestamp: Date.now()
});
}
popup(positioning, coords) {
this.ui('Popup Positioning', {
strategy: positioning,
coordinates: coords
});
}
setting(key, oldValue, newValue) {
this.repo('Setting Changed', {
setting: key,
from: oldValue,
to: newValue
});
}
/**
* Pretty print object data
*/
inspect(obj, label = 'Object') {
if (!this.enabled) return;
console.group(`🥕 CARROT 🔍 BNY: INSPECT - ${label}`);
console.log('📋 Type:', typeof obj);
console.log('📋 Constructor:', obj?.constructor?.name || 'Unknown');
if (typeof obj === 'object' && obj !== null) {
console.log('📋 Keys:', Object.keys(obj));
if (Array.isArray(obj)) {
console.log('📋 Length:', obj.length);
}
console.table ? console.table(obj) : console.log(obj);
} else {
console.log('📋 Value:', obj);
}
console.groupEnd();
}
// =========================================================================
// TEST AND DEBUG FUNCTIONS
// =========================================================================
/**
* Manual trigger for character consistency processing
*/
testProcessing() {
this.init('🧪 MANUAL TEST: WORLD_INFO_ACTIVATED System (old processing removed)');
this.init('Use World Info entries to trigger processing now');
}
/**
* Show current system state
*/
showState(selectedLorebooks, characterRepoBooks, scannedCharacters) {
this.inspect({
selectedLorebooks: Array.from(selectedLorebooks),
characterRepoBooks: Array.from(characterRepoBooks),
scannedCharacters: Array.from(scannedCharacters.keys()),
characterData: Object.fromEntries(scannedCharacters)
}, 'CarrotKernel System State');
}
/**
* Test character detection (old system removed)
*/
testDetection() {
this.init('🧪 MANUAL TEST: Character Detection (OLD SYSTEM REMOVED)');
this.init('Detection now happens via WORLD_INFO_ACTIVATED event');
return [];
}
/**
* Test injection only
*/
async testInjection(characters, injectCharacterDataFn) {
if (!characters) {
this.error('Please provide character names array - old detection removed');
return null;
}
if (characters.length === 0) {
this.error('No characters to inject - provide character names');
return null;
}
this.init('🧪 MANUAL TEST: AI Injection');
return await injectCharacterDataFn(characters);
}
/**
* Test display only
*/
testDisplay(characters, displayCharacterDataFn) {
if (!characters) {
this.error('Please provide character names array - old detection removed');
return;
}
if (characters.length === 0) {
this.error('No characters to display - provide character names');
return;
}
this.init('🧪 MANUAL TEST: Display System');
displayCharacterDataFn(characters);
}
/**
* Force scan lorebooks
*/
async forceScan(selectedLorebooks, scanSelectedLorebooksFn) {
this.init('🧪 MANUAL TEST: Force Lorebook Scan');
if (selectedLorebooks.size === 0) {
this.error('No lorebooks selected - check settings');
return null;
}
return await scanSelectedLorebooksFn(Array.from(selectedLorebooks));
}
/**
* Test BunnyMoTags filtering system
*/
testBunnyMoTagsFiltering(removeBunnyMoTagsFromStringFn) {
this.init('🧪 MANUAL TEST: BunnyMoTags Context Filtering');
const testContent = `Hello there!
<BunnyMoTags>
Nefertari:
• PHYSICAL: Golden skin, emerald eyes
• PERSONALITY: Regal, proud
</BunnyMoTags>
This is a test message.`;
const filtered = removeBunnyMoTagsFromStringFn(testContent);
this.init('🧪 Original content:');
console.log(testContent);
this.init('🧪 Filtered content:');
console.log(filtered);
return {
original: testContent,
filtered: filtered,
tagsRemoved: testContent !== filtered
};
}
/**
* Test persistent tags creation
*/
async testPersistentTags(characterNames, lastInjectedCharacters, generatePersistentTagsBlockFn) {
if (!characterNames && lastInjectedCharacters.length > 0) {
characterNames = lastInjectedCharacters;
}
if (!characterNames || characterNames.length === 0) {
this.error('No character names provided or injected - provide array of character names');
return null;
}
this.init('🧪 MANUAL TEST: Persistent BunnyMoTags Generation');
const tagsBlock = generatePersistentTagsBlockFn(characterNames);
this.init('🧪 Generated tags block:');
console.log(tagsBlock);
return tagsBlock;
}
}
// Create and export default instance
export const CarrotDebug = new CarrotDebugger();
// Setup global references and shortcuts
export function initializeDebugger() {
// Create global debug instance
window.CarrotDebug = CarrotDebug;
// Console shortcuts
if (typeof window !== 'undefined') {
window.cd = CarrotDebug;
}
}
// Export test functions for easier access
export const debugTests = {
testProcessing: () => CarrotDebug.testProcessing(),
showState: (selectedLorebooks, characterRepoBooks, scannedCharacters) =>
CarrotDebug.showState(selectedLorebooks, characterRepoBooks, scannedCharacters),
testDetection: () => CarrotDebug.testDetection(),
testInjection: (characters, injectFn) => CarrotDebug.testInjection(characters, injectFn),
testDisplay: (characters, displayFn) => CarrotDebug.testDisplay(characters, displayFn),
forceScan: (selectedLorebooks, scanFn) => CarrotDebug.forceScan(selectedLorebooks, scanFn),
testBunnyMoTagsFiltering: (removeFn) => CarrotDebug.testBunnyMoTagsFiltering(removeFn),
testPersistentTags: (names, lastInjected, generateFn) =>
CarrotDebug.testPersistentTags(names, lastInjected, generateFn)
};