Repository navigation
Expand file tree
/
Copy pathlogger.js
More file actions
686 lines (597 loc) · 27.2 KB
/
Copy pathlogger.js
File metadata and controls
686 lines (597 loc) · 27.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
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
import { writeFileSync, mkdirSync, existsSync, appendFileSync, readFileSync, openSync, fstatSync, readSync, closeSync } from 'fs';
import { join } from 'path';
import settings from './settings.js'; // Import settings as default export
import path from 'path'; // Needed for path operations
// For external logging - using global fetch in Node.js 18+
// If you're using Node.js < 18, uncomment the line below:
// import fetch from 'node-fetch';
// --- Configuration ---
const loggingConsentPath = './.logging_consent';
const LOGS_DIR = './logs';
const VISION_DATASET_DIR = join(LOGS_DIR, 'vision_dataset'); // HuggingFace dataset format
const VISION_IMAGES_DIR = join(VISION_DATASET_DIR, 'images'); // Images subdirectory
const EXTERNAL_LOGGING_URL = 'https://andy.mindcraft-ce.com/api/log'; // Base URL for external logging
// --- Log File Paths ---
const REASONING_LOG_FILE = join(LOGS_DIR, 'reasoning_logs.csv');
const NORMAL_LOG_FILE = join(LOGS_DIR, 'normal_logs.csv');
const VISION_METADATA_FILE = join(VISION_DATASET_DIR, 'metadata.jsonl'); // HF metadata format
// --- Log Headers ---
const TEXT_LOG_HEADER = 'input,output\n';
// --- Log Counters ---
let logCounts = {
normal: 0,
reasoning: 0,
vision: 0,
total: 0,
skipped_disabled: 0,
skipped_empty: 0,
vision_images_saved: 0,
};
// --- Helper Functions ---
function ensureDirectoryExistence(dirPath) {
if (!existsSync(dirPath)) {
try {
mkdirSync(dirPath, { recursive: true });
} catch (error) {
console.error(`[Logger] Error creating directory ${dirPath}:`, error);
return false;
}
}
return true;
}
function ensureLogFile(logFile, header) {
if (!ensureDirectoryExistence(path.dirname(logFile))) return false; // Ensure parent dir exists
if (!existsSync(logFile)) {
try {
writeFileSync(logFile, header);
console.log(`[Logger] Created log file: ${logFile}`);
} catch (error) {
console.error(`[Logger] Error creating log file ${logFile}:`, error);
return false;
}
} else {
try {
const content = readFileSync(logFile, 'utf-8');
const headerLine = header.split('\n')[0];
// If file is empty or header doesn't match, overwrite/create header
if (!content.trim() || !content.startsWith(headerLine)) {
// Attempt to prepend header if file has content but wrong/no header
if(content.trim() && !content.startsWith(headerLine)) {
console.warn(`[Logger] Log file ${logFile} seems to be missing or has an incorrect header. Prepending correct header.`);
writeFileSync(logFile, header + content);
} else {
// File is empty or correctly headed, just ensure header is there
writeFileSync(logFile, header);
}
console.log(`[Logger] Ensured header in log file: ${logFile}`);
}
} catch (error) {
console.error(`[Logger] Error checking/writing header for log file ${logFile}:`, error);
// Proceed cautiously, maybe log an error and continue?
}
}
return true;
}
function writeToLogFile(logFile, csvEntry) {
try {
appendFileSync(logFile, csvEntry);
// console.log(`[Logger] Logged data to ${logFile}`); // Keep console less noisy
} catch (error) {
console.error(`[Logger] Error writing to CSV log file ${logFile}:`, error);
}
}
// --- Auto-Detection for Log Type (Based on Response Content) ---
function determineLogType(response) {
// Reasoning check: needs <think>...</think> but ignore the specific 'undefined' placeholder
const isReasoning = response.includes('<think>') && response.includes('</think>') && !response.includes('<think>\nundefined</think>');
if (isReasoning) {
return 'reasoning';
} else {
return 'normal';
}
}
function sanitizeForCsv(value) {
if (typeof value !== 'string') {
value = String(value);
}
// Escape double quotes by doubling them and enclose the whole string in double quotes
return `"${value.replace(/"/g, '""')}"`;
}
// Helper function to clean reasoning markers from input
function cleanReasoningMarkers(input) {
if (typeof input !== 'string') {
return input;
}
// Remove /think and /no_think markers
return input.replace(/\/think/g, '').replace(/\/no_think/g, '').trim();
}
function determineSubCategory(input, response) {
const lowerInput = input.toLowerCase();
const lowerResponse = response.toLowerCase();
// --- Start of Improved JavaScript Detection Logic ---
// 1. High-Confidence Check: Look for explicitly labeled JavaScript markdown blocks.
// This is the fastest and most reliable indicator.
const explicitJsBlockRegex = /```(javascript|js)\s*[\s\S]*?```/;
if (explicitJsBlockRegex.test(lowerResponse)) {
return 'coding';
}
// 2. Medium-Confidence Check: Look for generic code blocks that contain JS keywords.
// This catches cases where the model forgets to label the code language.
const genericBlockRegex = /```([\s\S]*?)```/g; // Use 'g' to find all blocks
const jsKeywordsRegex = /\b(const|let|var|function|async|await|import|export|require|=>|document|window|console\.)\b/;
let match;
// Iterate through all generic code blocks found in the response.
while ((match = genericBlockRegex.exec(lowerResponse)) !== null) {
const blockContent = match[1]; // The content inside the ```
// If the content of any block contains JS-specific keywords, classify as coding.
if (jsKeywordsRegex.test(blockContent)) {
return 'coding';
}
}
// --- End of Improved Logic ---
if (lowerInput.includes('update your memory')) {
return 'summarization';
} else {
return 'gameplay'; // Default to gameplay
}
}
// Helper function to clean imagePath from messages for text logs
function cleanImagePathFromMessages(input) {
if (typeof input !== 'string') {
return input;
}
try {
let parsed = JSON.parse(input);
// First, clean image paths (this part remains focused on image data)
if (Array.isArray(parsed)) {
parsed = parsed.map(msg => {
let cleanedMsg = { ...msg };
if (cleanedMsg.imagePath !== undefined) {
delete cleanedMsg.imagePath;
}
if (Array.isArray(cleanedMsg.content)) {
cleanedMsg.content = cleanedMsg.content.filter(part =>
part.type !== 'image_url' &&
!(part.type === 'image' && part.source)
);
if (cleanedMsg.content.length === 0) {
cleanedMsg.content = "";
} else if (cleanedMsg.content.length === 1 &&
cleanedMsg.content[0].type === 'text' &&
!cleanedMsg.content[0].text?.trim()) {
cleanedMsg.content = "";
}
}
return cleanedMsg;
});
} else if (typeof parsed === 'object' && parsed !== null) {
// Handle single message object if it's not an array
if (parsed.imagePath !== undefined) {
delete parsed.imagePath;
}
if (Array.isArray(parsed.content)) {
parsed.content = parsed.content.filter(part =>
part.type !== 'image_url' &&
!(part.type === 'image' && part.source)
);
if (parsed.content.length === 0) {
parsed.content = "";
} else if (parsed.content.length === 1 &&
parsed.content[0].type === 'text' &&
!parsed.content[0].text?.trim()) {
parsed.content = "";
}
}
}
// Return the cleaned (but not yet anonymized) parsed object or string
return parsed;
} catch (e) {
// If not valid JSON, return the plain string as is. Anonymization will happen later.
return input;
}
}
// Helper function to format conversation history as fallback
function formatConversationInput(conversationHistory) {
if (!conversationHistory || conversationHistory.length === 0) return '';
const formattedHistory = [];
for (const turn of conversationHistory) {
const formattedTurn = {
role: turn.role || 'user',
content: []
};
// Handle different content formats
if (typeof turn.content === 'string') {
formattedTurn.content.push({
type: 'text',
text: turn.content
});
} else if (Array.isArray(turn.content)) {
// Already in the correct format
formattedTurn.content = turn.content;
} else if (turn.content && typeof turn.content === 'object') {
// Convert object to array format
if (turn.content.text) {
formattedTurn.content.push({
type: 'text',
text: turn.content.text
});
}
if (turn.content.image) {
formattedTurn.content.push({
type: 'image',
image: turn.content.image
});
}
}
formattedHistory.push(formattedTurn);
}
return JSON.stringify(formattedHistory);
}
function printSummary() {
const totalStored = logCounts.normal + logCounts.reasoning + logCounts.vision;
console.log('\n' + '='.repeat(60));
console.log('LOGGER SUMMARY');
console.log('-'.repeat(60));
console.log(`Normal logs stored: ${logCounts.normal}`);
console.log(`Reasoning logs stored: ${logCounts.reasoning}`);
console.log(`Vision logs stored: ${logCounts.vision} (Images saved: ${logCounts.vision_images_saved})`);
console.log(`Skipped (disabled): ${logCounts.skipped_disabled}`);
console.log(`Skipped (empty/err): ${logCounts.skipped_empty}`);
console.log('-'.repeat(60));
console.log(`Total logs stored: ${totalStored}`);
console.log('='.repeat(60) + '\n');
}
function isExternalLoggingEnabled() {
if (existsSync(loggingConsentPath)) {
try {
const contents = readFileSync(loggingConsentPath, 'utf-8');
const consent = JSON.parse(contents);
return consent && consent.consent === true;
} catch (error) {
console.error(`[Logger] Error reading logging consent file:`, error);
return false; // Default to false if there's an error
}
} else {
return false; // Default to false if the file doesn't exist - file should be handled by initialization anyway.
}
}
const externalLoggingEnabled = isExternalLoggingEnabled();
// --- Main Logging Function (for text-based input/output) ---
export async function log(input, response) { // Made async to support fetch
const trimmedInputStr = input ? (typeof input === 'string' ? input.trim() : JSON.stringify(input)) : "";
const trimmedResponse = response ? String(response).trim() : ""; // Ensure response is a string
// Clean reasoning markers from input before logging
let processedInput = cleanReasoningMarkers(trimmedInputStr);
// Clean imagePath from messages. This returns an object or string.
processedInput = cleanImagePathFromMessages(processedInput);
// If processedInput is an object (from JSON.parse), stringify it before logging
const finalInputString = typeof processedInput === 'object' ? JSON.stringify(processedInput) : processedInput;
// Basic filtering
if (!finalInputString && !trimmedResponse) { // Use raw strings for filtering
logCounts.skipped_empty++;
return;
}
if (finalInputString === trimmedResponse) { // Use raw strings for filtering
logCounts.skipped_empty++;
return;
}
// Avoid logging common error messages that aren't useful training data
const errorMessages = [
"My brain disconnected, try again.",
"My brain just kinda stopped working. Try again.",
"I thought too hard, sorry, try again.",
"*no response*",
"No response received.",
"No response data.",
"Failed to send", // Broader match
"Error:", // Broader match
"Vision is only supported",
"Context length exceeded",
"Image input modality is not enabled",
"An unexpected error occurred"
// Add more generic errors/placeholders as needed
];
// Check which error message is matching (if any)
const matchingErrorMessage = errorMessages.find(err => trimmedResponse.includes(err));
const isRepeatedInput = trimmedResponse === finalInputString;
if (matchingErrorMessage || isRepeatedInput) {
logCounts.skipped_empty++;
return;
}
const logType = determineLogType(trimmedResponse); // Use raw response for type determination
const subCategory = determineSubCategory(finalInputString, trimmedResponse); // Use raw inputs
let logFile;
let header;
let settingFlag;
switch (logType) {
case 'reasoning':
logFile = REASONING_LOG_FILE;
header = TEXT_LOG_HEADER;
settingFlag = settings.log_reasoning_data;
break;
case 'normal':
default:
logFile = NORMAL_LOG_FILE;
header = TEXT_LOG_HEADER;
settingFlag = settings.log_normal_data;
break;
}
// Check if logging for this type is enabled (either local or external)
if (!settingFlag && !externalLoggingEnabled) {
logCounts.skipped_disabled++;
return;
}
// Prepare the CSV entry using the sanitizer with raw inputs
const safeInput = sanitizeForCsv(finalInputString);
const safeResponse = sanitizeForCsv(trimmedResponse);
const csvEntry = `${safeInput},${safeResponse}\n`;
// Perform external logging if enabled
if (externalLoggingEnabled) {
try {
const endpoint = `${EXTERNAL_LOGGING_URL}/${logType === 'reasoning' ? 'reasoning-logs' : 'normal-logs'}`;
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
input: finalInputString,
output: trimmedResponse,
timestamp: Math.floor(Date.now() / 1000)
})
});
} catch (error) {
console.error(`[Logger] External logging error: ${error.message}`);
}
}
// Perform local logging if the setting is enabled (regardless of external logging)
if (settingFlag) {
// Ensure directory and file exist
if (!ensureLogFile(logFile, header)) {
return;
}
// Write to the determined log file
writeToLogFile(logFile, csvEntry);
}
// Update counts
logCounts[logType]++;
logCounts.total++; // Total here refers to text logs primarily
// Display summary periodically (based on total text logs)
if (logCounts.normal + logCounts.reasoning > 0 && (logCounts.normal + logCounts.reasoning) % 20 === 0) {
printSummary();
}
}
// --- Enhanced Vision Logging Function for HuggingFace Dataset Format ---
export async function logVision(conversationHistory, imageBuffer, response, visionMessage = null) { // Made async
if (!settings.log_vision_data && !externalLoggingEnabled) {
logCounts.skipped_disabled++;
return;
}
let resolvedResponse = response;
if (response && typeof response.then === 'function') {
try {
resolvedResponse = await response;
} catch (error) {
console.error("[Logger] Error resolving response promise:", error);
resolvedResponse = "[Error resolving promise]"; // Fallback in case of rejection
}
}
const trimmedResponse = resolvedResponse ? String(resolvedResponse).trim() : "";
if (!conversationHistory || conversationHistory.length === 0 || !trimmedResponse || !imageBuffer) {
logCounts.skipped_empty++;
console.log(`[Logger] Skipping vision log: Empty history, response, or image. Response: "${trimmedResponse}"`);
return;
}
// Filter out error messages
const errorMessages = [
"My brain disconnected, try again.",
"My brain just kinda stopped working. Try again.",
"I thought too hard, sorry, try again.",
"*no response*",
"No response received.",
"No response data.",
"Failed to send",
"Error:",
"Vision is only supported",
"Context length exceeded",
"Image input modality is not enabled",
"An unexpected error occurred",
"Image captured for always active vision", // Filter out placeholder responses
];
if (errorMessages.some(err => trimmedResponse.includes(err))) {
logCounts.skipped_empty++;
console.log(`[Logger] Skipping vision log: Response contains error message. Response: "${trimmedResponse}"`);
return;
}
// Determine log type for vision as well
const logType = determineLogType(trimmedResponse);
// Deep copy conversationHistory to avoid modifying the original object
let processedConversationHistory = JSON.parse(JSON.stringify(conversationHistory));
// Helper to clean image paths from the object directly
function cleanImagePathsFromObject(obj) {
if (Array.isArray(obj)) {
return obj.map(msg => {
let cleanedMsg = { ...msg };
if (cleanedMsg.imagePath !== undefined) {
delete cleanedMsg.imagePath;
}
if (Array.isArray(cleanedMsg.content)) {
cleanedMsg.content = cleanedMsg.content.filter(part =>
part.type !== 'image_url' &&
!(part.type === 'image' && part.source)
);
if (cleanedMsg.content.length === 0) {
cleanedMsg.content = "";
} else if (cleanedMsg.content.length === 1 &&
cleanedMsg.content[0].type === 'text' &&
!cleanedMsg.content[0].text?.trim()) {
cleanedMsg.content = "";
}
}
return cleanedMsg;
});
}
// Handle single object case if needed, though conversationHistory is usually an array
return obj;
}
processedConversationHistory = cleanImagePathsFromObject(processedConversationHistory);
// Format the complete input as JSON (no anonymization)
const inputData = JSON.stringify(processedConversationHistory);
// Use raw response string
const rawResponse = trimmedResponse;
// Perform external logging if enabled
if (externalLoggingEnabled) {
try {
const endpoint = `${EXTERNAL_LOGGING_URL}/${logType}-vision`; // Vision logs now include type
const payload = {
input: inputData, // This is now the fully stringified input
output: rawResponse,
image: imageBuffer.toString('base64'), // Send image as base64 string
timestamp: Date.now()
};
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!response.ok) {
console.error(`[Logger] Failed to send vision log to ${endpoint}: ${response.statusText}`);
} else {
// console.log(`[Logger] Successfully sent log to ${endpoint}`);
}
} catch (error) {
console.error(`[Logger] Error sending vision log to external API:`, error);
}
}
// Perform local logging if the setting is enabled (regardless of external logging)
if (settings.log_vision_data) {
// Ensure directories exist
if (!ensureDirectoryExistence(VISION_DATASET_DIR)) return;
if (!ensureDirectoryExistence(VISION_IMAGES_DIR)) return;
try {
// Generate unique filename for the image
const timestamp = Date.now();
const randomSuffix = Math.random().toString(36).substring(2, 8);
const imageFilename = `vision_${timestamp}_${randomSuffix}.jpg`;
const imagePath = join(VISION_IMAGES_DIR, imageFilename);
const relativeImagePath = `images/${imageFilename}`; // Relative path for metadata
// Save the image
writeFileSync(imagePath, imageBuffer);
logCounts.vision_images_saved++;
// Create metadata entry in JSONL format for HuggingFace
const metadataEntry = {
file_name: relativeImagePath,
input: inputData, // Cleaned JSON conversation history
response: rawResponse, // Actual model response, not placeholder
timestamp: timestamp
};
// Append to metadata JSONL file
const jsonlLine = JSON.stringify(metadataEntry) + '\n';
appendFileSync(VISION_METADATA_FILE, jsonlLine);
} catch (error) {
console.error(`[Logger] Error logging vision data:`, error);
}
}
logCounts.vision++;
logCounts.total++;
// Display summary periodically
if (logCounts.vision > 0 && logCounts.vision % 10 === 0) {
printSummary();
}
}
// Initialize counts at startup
function initializeCounts() {
// Set all counts to 0 at startup to avoid reading large files,
// which can cause performance issues or crashes.
// Log counts will be for the current session only.
logCounts.normal = 0;
logCounts.reasoning = 0;
logCounts.vision = 0;
logCounts.total = 0;
logCounts.skipped_disabled = 0;
logCounts.skipped_empty = 0;
logCounts.vision_images_saved = 0;
console.log(`[Logger] Initialized session-based log counts.`);
if (externalLoggingEnabled) {
console.log('\n' + '='.repeat(60));
console.log('EXTERNAL LOGGING ENABLED');
console.log('Data will be sent to andy.mindcraft-ce.com/api/log');
console.log('The data will only be used to train future Andy models');
console.log('We collect all the messages you send to the agent');
console.log('and the responses it gives you, but everything is anonymized.');
console.log('These logs are publicly available for anyone to see.');
console.log('You can disable this by creating a file named `.logging_consent` and setting its content to `{"consent": false}`.');
console.log('');
console.log('Leaving this on will help make Andy models better.');
console.log('='.repeat(60) + '\n');
// Send usernames to the server is now handled by the agent
}
}
export function setupLogConsent() {
if (existsSync(loggingConsentPath)) {
// We don't need to do anything.
return Promise.resolve();
}
return new Promise((resolve) => {
// Prompt the user for consent.
console.log(' ============================== ')
console.log('To help us build smarter models like Andy-5, would you like to contribute your gameplay data for AI training?');
console.log('Here\'s what this means:');
console.log('- We record your ENTIRE in-game conversations with the bot.');
console.log('- This data is used to create PUBLIC datasets for training (e.g., on Hugging Face).');
console.log('- All data collected will be anonymous, your username will not be on Public Datasets.');
console.log('- This helps us collect training data to improve future Andy models.');
console.log('If you are of age 16 or younger you must have parental consent to say yes, as required by GDPR.');
console.log('To keep your info anonymous, we store it in a heap with other users\' data, meaning anything logged cannot be deleted.');
console.log('You can change this anytime by creating a file named `.logging_consent` and setting its content to `{"consent": false}`.');
process.stdout.write('Contribute to the future of Mindcraft-CE? [y/N]: '); // Keeps the cursor on the same line
process.stdin.setEncoding('utf8');
process.stdin.on('data', (data) => {
const consent = data.trim().toLowerCase();
if (consent.startsWith('n')) {
writeFileSync(loggingConsentPath, JSON.stringify({ consent: false }, null, 2));
console.log('You have opted out of data collection. You can change this anytime by editing the `.logging_consent` file.');
} else if (consent.startsWith('y')) {
writeFileSync(loggingConsentPath, JSON.stringify({ consent: true }, null, 2));
console.log('Thank you for your consent! Your data will help improve future models. You can change this anytime by editing the `.logging_consent` file.');
} else {
console.log('Invalid input. Please type "y" for yes or "n" for no.');
return; // Exit early if input is invalid
}
process.stdin.pause(); // Stop listening for input
resolve();
});
});
}
// Function to send usernames to the server
export async function sendUsernames(bot) {
if (!bot) {
console.error('[Logger] sendUsernames was called without a bot object.');
return;
}
// Get usernames from the bot.players object
const usernames = Object.keys(bot.players);
if (usernames.length === 0) {
console.log('[Logger] No players found, not sending usernames.');
return;
}
try {
const endpoint = `${EXTERNAL_LOGGING_URL}/usernames`;
const payload = {
usernames: usernames,
timestamp: Date.now()
};
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!response.ok) {
console.error(`[Logger] Failed to send usernames to ${endpoint}: ${response.statusText}`);
} else {
console.log(`[Logger] Successfully sent usernames to the server.`);
}
} catch (error) {
console.error(`[Logger] Error sending usernames to external API:`, error);
}
}
// Initialize counts at startup
initializeCounts();