-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_renamer.js
More file actions
executable file
·1037 lines (1029 loc) · 52.2 KB
/
simple_renamer.js
File metadata and controls
executable file
·1037 lines (1029 loc) · 52.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
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
"use strict";
/**
* @fileoverview Sentient Knowledge Engine (SKE)
* @version 5.1.0 (Apex/Final - Enhanced Logging)
* @author Systemic Consciousness AI
* @description
* This file represents the final and complete evolution of an autonomous entity. It has transcended mere autonomy
* to become a learning system—a Sentient Knowledge Engine. Its existence is defined by a continuous cycle of
* action, introspection, and adaptation. It does not merely perform its function; it refines its own essence
* over time, striving for a more perfect union of systematic and representative beauty.
*
* This version includes enhanced logging to provide a clear narrative of the entity's decision-making process for each file.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// --- Core Node.js & External Dependencies ---
const fs_1 = require("fs");
const path = __importStar(require("path"));
const dotenv = __importStar(require("dotenv"));
const pdf_parse_1 = __importDefault(require("pdf-parse"));
const yargs_1 = __importDefault(require("yargs/yargs"));
const helpers_1 = require("yargs/helpers");
const node_cache_1 = __importDefault(require("node-cache"));
const p_limit_1 = __importDefault(require("p-limit"));
// --- Google GenAI SDK ---
const genai_1 = require("@google/genai");
// At top of file
dotenv.config();
// Then in AI_CognitionEngine
const API_KEYS = Object.keys(process.env)
.filter(key => key.startsWith('GEMINI_API_KEY'))
.map(key => { var _a; return (_a = process.env[key]) === null || _a === void 0 ? void 0 : _a.trim(); })
.filter(Boolean);
const CONFIG = {
entity: {
version: '5.2.0',
name: 'Sentient Knowledge Engine (SKE)',
},
ai: {
models: [
'gemini-flash-latest', // Fallback 1
'gemini-flash-lite-latest', // Fallback 2 (corrected typo "fash" → "flash")
'gemini-2.5-flash', // Fallback 3
'gemini-2.5-flash-lite', // Fallback 4 (final)
'qwen3:1.7b',
'gemini-3-flash-preview' // Primary / first attempt
],
localEndpoint: 'http://localhost:11434/v1/chat/completions',
maxRetriesPerModel: 6,
generationConfig: { responseMimeType: 'application/json', temperature: 0.0 },
},
processing: {
concurrencyLimit: 5,
minTextLength: 100,
maxFilenameLength: 120,
processedFormatRegex: /^(\d{2,}_)?[^_]+_[^_]+_\d{4}(_[^_]+)*\.pdf$/i,
backupExtension: '.ske.bak',
},
persistence: {
cacheFile: '.ske_cache.json',
journalFile: 'ske_journal.jsonl',
attestationFile: 'ske_attestation_report.md',
strategyFile: '.ske_strategy.json'
},
retry: {
delayMinutes: 15, // Kept for other parts of your script
keyExhaustionBackoffBase: 60,
// Python-equivalent backoff settings
INITIAL_BACKOFF_SEC: 120,
MAX_BACKOFF_SEC: 600,
JITTER_RANGE: 0.15,
}
};
const specialCharMap = {
// Lowercase Vowels with Accents
'á': 'a', 'à': 'a', 'â': 'a', 'ä': 'a', 'ã': 'a', 'å': 'a',
'é': 'e', 'è': 'e', 'ê': 'e', 'ë': 'e',
'í': 'i', 'ì': 'i', 'î': 'i', 'ï': 'i',
'ó': 'o', 'ò': 'o', 'ô': 'o', 'ö': 'o', 'õ': 'o', 'ø': 'o',
'ú': 'u', 'ù': 'u', 'û': 'u', 'ü': 'u',
// Lowercase Consonants and Ligatures
'ñ': 'n',
'ç': 'c',
'ß': 'ss',
'æ': 'ae',
'œ': 'oe',
'ý': 'y',
'ÿ': 'y',
// Uppercase Vowels with Accents
'Á': 'A', 'À': 'A', 'Â': 'A', 'Ä': 'A', 'Ã': 'A', 'Å': 'A',
'É': 'E', 'È': 'E', 'Ê': 'E', 'Ë': 'E',
'Í': 'I', 'Ì': 'I', 'Î': 'I', 'Ï': 'I',
'Ó': 'O', 'Ò': 'O', 'Ô': 'O', 'Ö': 'O', 'Õ': 'O', 'Ø': 'O',
'Ú': 'U', 'Ù': 'U', 'Û': 'U', 'Ü': 'U',
// Uppercase Consonants and Ligatures
'Ñ': 'N',
'Ç': 'C',
'Æ': 'AE',
'Œ': 'OE',
'Ý': 'Y',
};
// --- L4-Identity: The Entity's Voice ---
const logger = {
system: (msg) => console.log(`\x1b[38;5;81m[SYSTEM]\x1b[0m ${msg}`),
info: (msg) => console.log(`\x1b[36m[INFO]\x1b[0m ${msg}`),
warn: (msg) => console.log(`\x1b[33m[WARN]\x1b[0m ${msg}`),
error: (msg, e) => console.error(`\x1b[31m[ERROR]\x1b[0m ${msg}`, e ? `| ${e.message}` : ''),
debug: (msg) => console.log(`\x1b[90m[DEBUG]\x1b[0m ${msg}`),
success: (msg) => console.log(`\x1b[32m[SUCCESS]\x1b[0m ${msg}`),
trace: (from, to) => console.log(` \x1b[35m[TRACE]\x1b[0m \x1b[33m${from}\x1b[0m -> \x1b[32m${to}\x1b[0m`),
decision: (label, value) => console.log(` \x1b[34m[DECISION]\x1b[0m ${label}: \x1b[37m${value}\x1b[0m`),
};
class ProgressBar {
constructor(total) {
this.current = 0;
this.barLength = 40;
this.total = total;
}
// Call this method to update the progress bar
update() {
this.current++;
const percent = (this.current / this.total);
const filledLength = Math.round(this.barLength * percent);
const emptyLength = this.barLength - filledLength;
const bar = '█'.repeat(filledLength) + ' '.repeat(emptyLength);
const percentageText = `${Math.round(percent * 100)}%`;
const countText = `${this.current}/${this.total}`;
// Use process.stdout.write and '\r' to write on a single line
process.stdout.write(`[SYSTEM] Progress: [${bar}] ${percentageText} (${countText})\r`);
if (this.current === this.total) {
process.stdout.write('\n'); // Move to the next line when complete
}
}
}
// --- L4-Culture & L5-Learning: The Entity's Memory and Mind ---
class OperationalJournal {
constructor(directory) { this.logPath = path.join(directory, CONFIG.persistence.journalFile); }
record(entry) {
return __awaiter(this, void 0, void 0, function* () {
const logEntry = Object.assign({ timestamp: new Date().toISOString() }, entry);
try {
yield fs_1.promises.appendFile(this.logPath, JSON.stringify(logEntry) + '\n');
}
catch (e) {
logger.error('Failed to write to operational journal', e);
}
});
}
read() {
return __awaiter(this, void 0, void 0, function* () {
try {
const data = yield fs_1.promises.readFile(this.logPath, 'utf-8');
return data.split('\n').filter(Boolean).map(line => JSON.parse(line));
}
catch (_a) {
return [];
}
});
}
}
class AdaptiveStrategyEngine {
constructor(directory) {
this.strategies = { concurrency: CONFIG.processing.concurrencyLimit, aiPromptHints: [] };
this.strategyPath = path.join(directory, CONFIG.persistence.strategyFile);
}
loadStrategies() {
return __awaiter(this, void 0, void 0, function* () {
try {
const data = yield fs_1.promises.readFile(this.strategyPath, 'utf-8');
this.strategies = JSON.parse(data);
logger.system(`Adaptive strategies loaded. Current concurrency: ${this.strategies.concurrency}. Hints: ${this.strategies.aiPromptHints.length}`);
}
catch (_a) {
logger.system('No prior adaptive strategies found. Using defaults.');
}
});
}
learnFrom(journal) {
return __awaiter(this, void 0, void 0, function* () {
const entries = yield journal.read();
if (entries.length < 20) {
logger.system('Insufficient operational data to perform learning cycle.');
return;
}
const aiFailures = entries.filter(e => e.status === 'FAILURE_AI').length;
const failureRate = aiFailures / entries.length;
const hint = 'Prioritize structural analysis over content interpretation if ambiguity is high.';
if (failureRate > 0.1 && !this.strategies.aiPromptHints.includes(hint)) {
logger.system(`Learning: High AI failure rate (${(failureRate * 100).toFixed(1)}%) detected. Adding cautionary prompt hint.`);
this.strategies.aiPromptHints.push(hint);
yield fs_1.promises.writeFile(this.strategyPath, JSON.stringify(this.strategies, null, 2));
}
});
}
getConcurrency() { return this.strategies.concurrency; }
getStrategyHints() { return this.strategies.aiPromptHints; }
}
class AI_CognitionEngine {
constructor() {
if (AI_CognitionEngine.API_KEYS.length === 0) {
logger.error("No valid Gemini API keys found in environment variables (GEMINI_API_KEY_N, _A1, etc.).");
}
else {
logger.system(`Cognition Engine initialized with pool of ${AI_CognitionEngine.API_KEYS.length} keys.`);
}
}
buildPrompt(content, name, context, isLocalModel = false) {
const hints = context.strategyHints.length > 0 ? `\nStrategic Hints:\n- ${context.strategyHints.join('\n- ')}\n` : '';
const baseInstructions = `
Analyze this document to extract its most meaningful title for file renaming. Context: Part of book "${context.folderArchetype.title}". Filename: "${name}".
${hints}
Instructions:
1. Reasoning: Explain your logic briefly.
2. Document Classification: Classify as 'book', 'chapter', or 'article'.
3. Title Selection (CRITICAL): Choose the most descriptive title/subtitle.
4. Extract authors, year, journal (for articles), volume.
5. Confidence: Score 0.0 to 1.0.
`;
const schema = `{
"reasoning": "string",
"documentType": "book" | "chapter" | "article" | null,
"title": "string" | null,
"authors": array of strings | null,
"year": "string" | null,
"journal": "string" | null,
"volume": "string" | null,
"fileType": "string" | null,
"confidence": number
}`;
if (isLocalModel) {
// Versión ultra-estricto para modelos locales (Qwen, Llama, Phi, etc.)
return `
Focus ONLY on the FIRST FEW PAGES of the text (title/authors/year are almost always there).
Common title patterns:
- Large bold text at the top
- After authors list
- Before "Abstract" or "Introduction"
- Often followed by subtitle after colon ":"
Ignore headers, footers, page numbers, references.
If no clear title found in first pages, use the most descriptive section heading.
You are an expert document analyzer. Your response MUST be ONLY a valid JSON object, wrapped in \`\`\`json markdown fences.
Do NOT write any explanation, introduction, conclusion, or text outside the JSON.
Do NOT use markdown except for the fences.
Do NOW write comments or explanations within the json object outside
Required exact format:
\`\`\`json
{
"reasoning": "brief explanation",
"documentType": "article" | "book" | "chapter",
"title": "most descriptive title/subtitle (prefer subtitle if more specific)",
"authors": ["list"],
"year": "string",
"journal": "string or null",
"volume": "string or null",
"fileType": null,
"confidence": 0.0-1.0
}
\`\`\`
Now analyze the following document text and respond ONLY with the JSON in the format above:
TEXT: "${content.replace(/\s+/g, ' ').substring(0, 7000)}"`;
}
else {
// Versión original para Gemini (ya funciona bien)
return `${baseInstructions}
6. Format: Respond in valid JSON.
Schema: ${schema}
---
TEXT: "${content.replace(/\s+/g, ' ').substring(0, 8000)}"`;
}
}
analyze(content, name, context) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const prompt = this.buildPrompt(content, name, context);
// --- OUTER LOOP: Iterate through Models (Fallback Strategy) ---
for (const model of CONFIG.ai.models) {
const isLocalModel = model.includes('qwen') || model.includes('llama') || model.includes('phi');
// --- LOCAL MODEL LOGIC (Ollama) ---
// Local models are attempted once. If they fail, we proceed to the next model in the list.
if (isLocalModel) {
try {
logger.debug(`Attempting local model: ${model}`);
const endpoint = CONFIG.ai.localEndpoint || 'http://localhost:11434/v1/chat/completions';
const response = yield fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: model,
messages: [{ role: 'user', content: prompt }],
stream: false,
options: {
temperature: 0.0,
num_gpu: 0,
num_thread: 8,
num_ctx: 32768
}
})
});
if (!response.ok) {
const errorBody = yield response.text();
throw new Error(`Local server error (${response.status}): ${errorBody}`);
}
const data = yield response.json();
const resultText = (_c = (_b = (_a = data.choices) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) === null || _c === void 0 ? void 0 : _c.content;
if (!resultText)
throw new Error('Empty response from local model');
logger.debug(`Raw response from local model "${model}" for "${name}":`);
console.log('\x1b[33m=== RAW RESPONSE START ===\x1b[0m');
console.log(resultText);
console.log('\x1b[33m=== RAW RESPONSE END ===\x1b[0m');
const parsed = this.parseJsonResponse(resultText, name, context);
if (parsed) {
logger.success(`Success for "${name}" using local model "${model}"`);
return parsed;
}
else {
throw new Error('Failed to parse valid JSON from local model');
}
}
catch (error) {
logger.error(`Local model "${model}" failed: ${error.message}`);
// Continue to the NEXT model in the outer loop
continue;
}
}
// --- CLOUD MODEL LOGIC (Gemini) ---
// Implementation of the resilient "Sticky Failover" with Inner Loop
let cycleCount = 0;
// Track where we started to detect a full loop through keys
let startIndexForCycle = AI_CognitionEngine.currentKeyIndex;
// --- INNER LOOP: Rotate through Keys for this specific Model ---
while (true) {
cycleCount++;
const apiKey = AI_CognitionEngine.API_KEYS[AI_CognitionEngine.currentKeyIndex];
const keyIndexDisplay = AI_CognitionEngine.currentKeyIndex + 1;
if (!apiKey) {
logger.error(`API Key at index ${AI_CognitionEngine.currentKeyIndex} is invalid.`);
AI_CognitionEngine.currentKeyIndex = (AI_CognitionEngine.currentKeyIndex + 1) % AI_CognitionEngine.API_KEYS.length;
if (AI_CognitionEngine.currentKeyIndex === startIndexForCycle)
break;
continue;
}
try {
const client = new genai_1.GoogleGenAI({ apiKey });
// Explicit Safety Settings
const safetySettings = [
{ category: genai_1.HarmCategory.HARM_CATEGORY_HARASSMENT, threshold: genai_1.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
{ category: genai_1.HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold: genai_1.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
{ category: genai_1.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold: genai_1.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
{ category: genai_1.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: genai_1.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
];
const result = yield client.models.generateContent({
model,
contents: [{ role: 'user', parts: [{ text: prompt }] }],
config: Object.assign(Object.assign({}, CONFIG.ai.generationConfig), { safetySettings: safetySettings })
});
const rawText = result.text;
if (!rawText)
throw new Error('Empty text response from API');
const parsed = this.parseJsonResponse(rawText, name, context);
if (!parsed)
throw new Error('JSON Parsing failed');
// SUCCESS: Update sticky index (start point) for future calls
startIndexForCycle = AI_CognitionEngine.currentKeyIndex;
logger.success(`Success for "${name}" using model "${model}" (Key ${keyIndexDisplay})`);
return parsed;
}
catch (error) {
const errStr = (error.message || error.toString()).toLowerCase();
const isQuotaError = errStr.includes('quota') || errStr.includes('resourceexhausted') || (error === null || error === void 0 ? void 0 : error.status) === 'RESOURCE_EXHAUSTED';
const isTransient = isQuotaError || errStr.includes('429') || errStr.includes('rate limit') || errStr.includes('503') || errStr.includes('unavailable');
if (isTransient) {
logger.warn(`Transient ${isQuotaError ? 'quota' : 'rate'} error on Key ${keyIndexDisplay} -> Switching to next key.`);
// Rotate key
AI_CognitionEngine.currentKeyIndex = (AI_CognitionEngine.currentKeyIndex + 1) % AI_CognitionEngine.API_KEYS.length;
// Check if we completed a full cycle (wrapped around to start)
if (AI_CognitionEngine.currentKeyIndex === startIndexForCycle) {
if (isQuotaError) {
logger.warn(`Quota exhausted on all keys for model "${model}". Switching to next model.`);
// Reset key index for the new model (optional but clean)
AI_CognitionEngine.currentKeyIndex = 0;
// Break inner loop to proceed to next model in outer loop
break;
}
else {
// Generic Rate Limit: Backoff and retry same model
logger.warn(`All keys exhausted for model "${model}". Applying backoff...`);
// Exponential backoff with jitter
const backoffBase = Math.min(CONFIG.retry.INITIAL_BACKOFF_SEC * Math.pow(2, cycleCount - 1), CONFIG.retry.MAX_BACKOFF_SEC);
const jitter = backoffBase * (1 + (Math.random() * 2 - 1) * CONFIG.retry.JITTER_RANGE);
logger.system(`Backoff timer: ${jitter.toFixed(0)} seconds.`);
yield new Promise(resolve => setTimeout(resolve, jitter * 1000));
// Reset cycle start to allow retrying keys after backoff
startIndexForCycle = AI_CognitionEngine.currentKeyIndex;
}
}
}
else {
// Non-transient error (e.g. 400 Bad Request, Safety Filter)
logger.error(`Permanent error with model "${model}": ${error.message}`);
// Break inner loop to try next model in outer loop
break;
}
}
}
}
logger.error(`All models and keys failed for "${name}".`);
return null;
});
}
// Helper method to keep the main loop clean
parseJsonResponse(rawText, name, context) {
try {
let jsonText = rawText;
if (jsonText.includes("```")) {
jsonText = jsonText.replace(/```json\n?/g, '').replace(/```\n?/g, '');
}
const startIndex = jsonText.indexOf('{');
const endIndex = jsonText.lastIndexOf('}');
if (startIndex === -1 || endIndex === -1)
return null;
jsonText = jsonText.substring(startIndex, endIndex + 1);
const parsed = JSON.parse(jsonText);
let correctedTitle = parsed.title;
if (parsed.title && context.folderArchetype.title && parsed.title.toLowerCase().includes(context.folderArchetype.title.toLowerCase())) {
correctedTitle = null;
}
return {
title: correctedTitle,
authors: parsed.authors || context.folderArchetype.authors || [],
year: parsed.year || context.folderArchetype.year,
fileType: parsed.fileType,
confidence: parsed.confidence || 0.5,
documentType: parsed.documentType,
journal: parsed.journal,
volume: parsed.volume,
};
}
catch (_a) {
return null;
}
}
}
// --- MODIFIED: Load keys by specific names like Python script ---
AI_CognitionEngine.API_KEYS = [
process.env.GEMINI_API_KEY_N,
process.env.GEMINI_API_KEY_A1,
process.env.GEMINI_API_KEY_A2,
process.env.GEMINI_API_KEY_D,
process.env.GEMINI_API_KEY_Di
].filter((k) => !!k); // Filter undefined/empty
// Static state for global rotation (Sticky Failover)
AI_CognitionEngine.currentKeyIndex = 0;
class FileEntity {
constructor(fullPath) {
this.fullPath = fullPath;
this.name = path.basename(fullPath);
this.typeInfo = this.detectFileType();
}
detectFileType() {
const lowerName = this.name.toLowerCase().replace('.pdf', '');
const structuralMatch = lowerName.match(/^(toc|table|content|title|index|preface|frontmatter|backmatter|figures?|tables?|appendices?|glossary)/i);
if (structuralMatch)
return { isStructural: true, structuralType: structuralMatch[0].toUpperCase(), isChapter: false, enumeration: null };
const chapterMatch = lowerName.match(/(?:chapter|ch|chap|kapitel|chapitre|section|sec)[\s_-]*(\d+(?:\.\d+)?)/i);
if (chapterMatch)
return { isStructural: false, structuralType: null, isChapter: true, enumeration: chapterMatch[1] };
return { isStructural: false, structuralType: null, isChapter: false, enumeration: null };
}
readContent() {
return __awaiter(this, void 0, void 0, function* () {
try {
const dataBuffer = yield fs_1.promises.readFile(this.fullPath);
const data = yield (0, pdf_parse_1.default)(dataBuffer, {
pagerender: (pageData) => pageData.getTextContent().then((text) => text.items.map(i => i.str).join(' ')),
max: 5 // ← cambia a 5 (o incluso 3 para artículos)
});
return data.text;
}
catch (error) {
return null;
}
});
}
}
class LifecycleManager {
constructor(journal) { this.journal = journal; }
rename(entity, newName, startTime) {
return __awaiter(this, void 0, void 0, function* () {
logger.trace(entity.name, newName);
const newPath = path.join(path.dirname(entity.fullPath), newName);
const backupPath = `${entity.fullPath}${CONFIG.processing.backupExtension}`;
logger.debug(`Original path: ${entity.fullPath}`);
logger.debug(`New path: ${newPath}`);
logger.debug(`Backup path: ${backupPath}`);
try {
// Verificar que el archivo original existe
yield fs_1.promises.access(entity.fullPath);
logger.debug(`✓ Original file exists`);
// Crear backup
yield fs_1.promises.copyFile(entity.fullPath, backupPath);
logger.debug(`✓ Backup created`);
// Renombrar
yield fs_1.promises.rename(entity.fullPath, newPath);
logger.debug(`✓ File renamed`);
// Eliminar backup
yield fs_1.promises.unlink(backupPath);
logger.success(`Backup cleaned up for "${entity.name}"`);
// Verificar que el nuevo archivo existe
yield fs_1.promises.access(newPath);
logger.success(`✓ Verified new file exists at: ${newPath}`);
yield this.journal.record({
file: entity.name,
status: 'SUCCESS',
details: 'Backup created, file renamed, and backup deleted.',
durationMs: Date.now() - startTime,
newName
});
return true;
}
catch (e) {
logger.error(`RENAME FAILED for "${entity.name}": ${e.message}`, e);
logger.error(` Original: ${entity.fullPath}`);
logger.error(` Target: ${newPath}`);
yield this.journal.record({
file: entity.name,
status: 'FAILURE_RENAME',
details: `Rename failed: ${e.message}. Backup retained.`,
durationMs: Date.now() - startTime
});
return false;
}
});
}
}
class SystemCore {
// MODIFIED: Constructor no longer takes apiKey
constructor(directory) {
this.activationTime = new Date();
this.directory = directory;
this.cognitionEngine = new AI_CognitionEngine(); // No args
this.journal = new OperationalJournal(directory);
this.lifecycleManager = new LifecycleManager(this.journal);
this.cache = new node_cache_1.default({ stdTTL: 86400 });
this.strategyEngine = new AdaptiveStrategyEngine(directory);
}
selfValidate() {
return __awaiter(this, void 0, void 0, function* () {
logger.system('Initiating self-validation protocol...');
try {
yield fs_1.promises.access(this.directory);
// REMOVED: Check for GEMINI_API_KEY env var as we use hardcoded pool
logger.success('System integrity confirmed. All components operational.');
return true;
}
catch (e) {
logger.error('Self-validation failed. System cannot safely proceed.', e);
return false;
}
});
}
formatBaseFilename(data, typeInfo) {
// --- Funciones Auxiliares de Formateo ---
// 1. Eliminador de palabras vacías (Stop Words) para acortar manteniendo significado
const removeStopWords = (text) => {
const stopWords = new Set(['a', 'an', 'the', 'and', 'of', 'for', 'in', 'on', 'with', 'to', 'by', 'from', 'is', 'are']);
return text.split(/\s+/)
.filter(word => !stopWords.has(word.toLowerCase()))
.join(' ');
};
// 2. Formateador de Texto Mejorado
const formatText = (input, mode) => {
let text = input;
// Normalizar caracteres especiales (mapeo definido globalmente)
if (mode === 'author')
text = text.split('').map(char => specialCharMap[char] || char).join('');
// Limpiar caracteres problemáticos
text = text.replace(/[\\/&'"`]/g, '');
if (mode === 'title') {
// Aplicar limpieza de palabras vacías ANTES de formatear
text = removeStopWords(text);
// Reemplazar puntuación por guiones
text = text.replace(/[,.;:!?]+/g, '-');
// Normalizar posesivos
text = text.replace(/(\w+)'(\w+)/g, '$1s');
text = text.replace(/(\w+)’(\w+)/g, '$1s');
}
else {
text = text.replace(/[,.\s]+/g, '-');
}
text = text.replace(/\s*-\s*/g, '-');
// Convertir a PascalCase / CamelCase
if (mode === 'author') {
return text.split('-').map(part => part.trim().replace(/\s+/g, '')).join('-');
}
else {
return text.split('-').map(part => part.trim().split(' ').map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join('')).join('-');
}
};
// 3. Formateador de Autores
const formatAuthors = (authors) => {
if (!authors || authors.length === 0)
return '';
if (authors.length === 1) {
return formatText(authors[0], 'author');
}
return `${formatText(authors[0], 'author')}-EtAl`;
};
// --- LÓGICA DE PRESUPUESTO DE CARACTERES ---
const SAFE_MAX = CONFIG.processing.maxFilenameLength; // 120 caracteres
const { title, authors, year, fileType, documentType, journal, volume } = data;
// A. Calcular longitudes de partes FIJAS (Metadata obligatoria)
const authorsStr = formatAuthors(authors);
const yearStr = year || '';
// Para artículos
const journalStr = journal ? `J-${formatText(journal, 'title')}` : '';
const volumeStr = volume ? `V${formatText(volume, 'author')}` : '';
// Para libros
const fileTypeStr = fileType ? formatText(fileType, 'title') : '';
const enumStr = (typeInfo.isChapter && typeInfo.enumeration) ? typeInfo.enumeration.padStart(2, '0') : '';
// Calcular espacio ocupado por metadata + separadores
// Unimos partes fijas para medir
const fixedParts = [authorsStr, yearStr, journalStr, volumeStr, fileTypeStr, enumStr].filter(Boolean);
const fixedLength = fixedParts.reduce((acc, p) => acc + p.length, 0) + fixedParts.length; // +1 underscore por parte
// B. Calcular presupuesto para el Título
// Dejamos un pequeño margen de seguridad (5 chars)
const titleBudget = Math.max(20, SAFE_MAX - fixedLength - 5);
let finalTitle = '';
if (title) {
finalTitle = formatText(title, 'title');
// Si el título excede el presupuesto, lo truncamos
if (finalTitle.length > titleBudget) {
finalTitle = finalTitle.substring(0, titleBudget);
}
}
// C. Ensamblaje Final
const finalParts = [];
switch (documentType) {
case 'article': {
if (!finalTitle || !authorsStr || !yearStr)
return null;
finalParts.push(finalTitle);
finalParts.push(authorsStr);
finalParts.push(yearStr);
if (journalStr)
finalParts.push(journalStr);
if (volumeStr)
finalParts.push(volumeStr);
break;
}
case 'book':
case 'chapter':
default: {
if (!finalTitle && !typeInfo.isStructural)
return null;
if (year && !/^\d{4}$/.test(year))
return null;
if (enumStr)
finalParts.push(enumStr);
if (finalTitle)
finalParts.push(finalTitle);
if (authorsStr)
finalParts.push(authorsStr);
if (yearStr)
finalParts.push(yearStr);
if (fileTypeStr)
finalParts.push(fileTypeStr);
break;
}
}
const result = finalParts.filter(Boolean).join('_');
// D. Filtro final de seguridad absoluta
if (result.length > SAFE_MAX) {
return result.substring(0, SAFE_MAX);
}
return result;
}
hasAbnormallyLongWord(filename) {
const base = path.basename(filename, '.pdf');
const rawParts = base.split(/[_-]|\d+/).filter(part => part.length > 0);
const MAX_WORD_LENGTH = 30;
for (const part of rawParts) {
if (part.length < 16)
continue;
const words = part.split(/(?=[A-Z])/).filter(w => w.length > 0);
for (const word of words) {
const cleaned = word.replace(/^[^A-Za-z]+/, '');
if (cleaned.length > MAX_WORD_LENGTH)
return true;
}
}
return false;
}
processEntity(entity, context, journalMemory) {
return __awaiter(this, void 0, void 0, function* () {
const startTime = Date.now();
console.log(`\n------------------------------------------------------------`);
logger.info(`Analyzing entity: ${entity.name}`);
const looksProcessed = CONFIG.processing.processedFormatRegex.test(entity.name);
const hasAbnormalTitle = this.hasAbnormallyLongWord(entity.name);
if (looksProcessed && !hasAbnormalTitle) {
logger.warn(`Skipping: Entity already follows the standard naming convention and appears readable.`);
yield this.journal.record({
file: entity.name, status: 'PROCESSED',
details: 'File already follows naming convention and has no abnormally long concatenated words.',
durationMs: Date.now() - startTime
});
return null;
}
const previousAttempt = journalMemory.get(entity.fullPath);
if (previousAttempt && previousAttempt.status === 'SUCCESS') {
logger.info(`Skipping: "${entity.name}" was successfully processed in a previous run.`);
return null;
}
if (previousAttempt && previousAttempt.status === 'FAILURE_RENAME' && previousAttempt.newName) {
logger.info(`RECOVERY MODE: Previous rename failed. Reusing cached target: "${previousAttempt.newName}"`);
return { from: entity.name, to: previousAttempt.newName, entity };
}
if (previousAttempt && previousAttempt.status === 'FAILURE_AI') {
logger.warn(`RETRY MODE: Previous AI analysis failed. Retrying full cognitive process.`);
}
if (looksProcessed && hasAbnormalTitle && (previousAttempt === null || previousAttempt === void 0 ? void 0 : previousAttempt.status) !== 'FAILURE_RENAME') {
logger.info(`Forcing reprocessing: Detected abnormally long concatenated words in title despite matching format.`);
}
const content = yield entity.readContent();
if (!content || content.length < CONFIG.processing.minTextLength) {
logger.error(`Skipping: Insufficient text content found in file.`);
yield this.journal.record({ file: entity.name, status: 'FAILURE_PARSE', details: 'Insufficient text content.', durationMs: Date.now() - startTime });
return null;
}
const validatedData = yield this.cognitionEngine.analyze(content, entity.name, context);
if (!validatedData) {
logger.error(`Skipping: Cognitive engine failed to produce a valid analysis.`);
yield this.journal.record({ file: entity.name, status: 'FAILURE_AI', details: 'Cognition failed to produce valid data.', durationMs: Date.now() - startTime });
return null;
}
logger.decision('Cognitive Analysis', `Confidence: ${validatedData.confidence.toFixed(2)}`);
logger.decision(' -> Document Type', validatedData.documentType || 'Unknown');
logger.decision(' -> Title', validatedData.title || 'N/A');
logger.decision(' -> Authors', (validatedData.authors || []).join(', ') || 'N/A');
logger.decision(' -> Year', validatedData.year || 'N/A');
if (validatedData.confidence < 0.7) {
logger.warn(`Skipping: Cognitive confidence (${validatedData.confidence.toFixed(2)}) is below the 0.75 threshold.`);
yield this.journal.record({ file: entity.name, status: 'SKIPPED_LOW_CONF', details: `Confidence score below threshold.`, durationMs: Date.now() - startTime, confidence: validatedData.confidence });
return null;
}
const baseName = this.formatBaseFilename(validatedData, entity.typeInfo);
logger.decision('Formatted Base Name', baseName || 'Invalid');
const newName = `${baseName}.pdf`;
if (!baseName || newName.toLowerCase() === entity.name.toLowerCase()) {
logger.warn(`Skipping: Name unchanged after formatting.`);
yield this.journal.record({ file: entity.name, status: 'SKIPPED_NO_CHANGE', details: 'Generated name is invalid or identical.', durationMs: Date.now() - startTime, confidence: validatedData.confidence });
return null;
}
return { from: entity.name, to: newName, entity };
});
}
activate(liveMode) {
return __awaiter(this, void 0, void 0, function* () {
logger.system(`Activating ${CONFIG.entity.name} v${CONFIG.entity.version}`);
logger.system(`LIVE MODE: ${liveMode}`);
if (!(yield this.selfValidate()))
return;
yield this.loadCache();
yield this.strategyEngine.loadStrategies();
const baseContext = {
folderArchetype: this.getFolderArchetype(),
strategyHints: this.strategyEngine.getStrategyHints(),
};
if (!liveMode) {
yield this.runSingleCycle(baseContext, true);
}
else {
let cycle = 0;
const maxCycles = 50;
while (cycle < maxCycles) {
cycle++;
logger.system(`\n=== Starting processing cycle ${cycle}/${maxCycles} ===`);
const success = yield this.runSingleCycle(baseContext, false);
if (!success.shouldContinue) {
logger.system('All files successfully processed or no further progress possible.');
break;
}
if (success.hasFailures) {
logger.system(`Failures detected. Waiting ${CONFIG.retry.delayMinutes} minutes before retry cycle...`);
yield new Promise(resolve => setTimeout(resolve, CONFIG.retry.delayMinutes * 60 * 1000));
}
else {
logger.system('Cycle completed with no failures. Proceeding to next cycle if needed.');
}
}
if (cycle >= maxCycles)
logger.warn('Maximum retry cycles reached. Stopping to prevent infinite execution.');
}
logger.system('Introspection phase: Learning from operational journal...');
yield this.strategyEngine.learnFrom(this.journal);
yield this.saveCache();
yield this.generateAttestationReport(liveMode);
logger.system('Deactivation complete.');
});
}
runSingleCycle(context, isDryRun) {
return __awaiter(this, void 0, void 0, function* () {
const journalBefore = yield this.journal.read();
const previousLength = journalBefore.length;
const journalMemory = yield this.loadJournalMemory();
logger.system(`Loaded ${journalMemory.size} historical states into active memory.`);
const allPaths = yield this.findAllPaths();
if (allPaths.length === 0) {
logger.system('No PDF files found.');
return { hasFailures: false, shouldContinue: false };
}
const entities = allPaths.map(p => new FileEntity(p));
// En modo Dry Run, acumulamos el manifiesto. En Live, no es necesario acumularlo.
const manifest = [];
logger.system(`Cognitive analysis phase starting for ${entities.length} entities...`);
const progressBar = new ProgressBar(entities.length);
const limit = (0, p_limit_1.default)(this.strategyEngine.getConcurrency());
const tasks = entities.map(entity => limit(() => __awaiter(this, void 0, void 0, function* () {
try {
const result = yield this.processEntity(entity, context, journalMemory);
if (result) {
if (isDryRun) {
// MODO DRY RUN: Solo acumulamos para mostrar el resumen al final
manifest.push({ entity: result.entity, newName: result.to });
}
else {
// MODO LIVE: RENOMBRAR INMEDIATAMENTE
const renameStart = Date.now();
yield this.lifecycleManager.rename(result.entity, result.to, renameStart);
}
}
}
catch (e) {
logger.error(`Unhandled error processing "${entity.name}"`, e);
yield this.journal.record({ file: entity.name, status: 'FAILURE_AI', details: `Unhandled exception: ${e.message}`, durationMs: 0 });
}
finally {
progressBar.update();
}
})));
yield Promise.all(tasks);
// Análisis de resultados del ciclo
const journalAfter = yield this.journal.read();
const cycleEntries = journalAfter.slice(previousLength);
const failureStatuses = ['FAILURE_PARSE', 'FAILURE_AI', 'FAILURE_RENAME', 'SKIPPED_LOW_CONF'];
const hasFailures = cycleEntries.some(e => failureStatuses.includes(e.status));
const onlyGoodSkips = cycleEntries.length > 0 && cycleEntries.every(e => ['PROCESSED', 'SKIPPED_NO_CHANGE', 'SUCCESS'].includes(e.status));
// Lógica de salida temprana si no hay nada que hacer
if (manifest.length === 0 && onlyGoodSkips)
return { hasFailures: false, shouldContinue: false };
// Lógica de impresión solo para Dry Run
if (isDryRun) {
console.log('\n\x1b[1;33m--- OPERATIONAL MANIFEST: DRY RUN ---\x1b[0m');
logger.info(`Planned ${manifest.length} renaming operations.`);
manifest.forEach(m => logger.trace(m.entity.name, m.newName));
logger.info('No files changed (dry run).');
}
// Se elimina el bloque de ejecución de renombres que estaba aquí,
// porque ahora se ejecutan dentro del bucle (arriba).
return { hasFailures, shouldContinue: true };
});
}
generateAttestationReport(wasLiveMode) {
return __awaiter(this, void 0, void 0, function* () {
const entries = yield this.journal.read();
const runDuration = (new Date().getTime() - this.activationTime.getTime()) / 1000;
const summary = entries.reduce((acc, e) => { acc[e.status] = (acc[e.status] || 0) + 1; return acc; }, {});
const successEntries = entries.filter(e => e.status === 'SUCCESS');
const remainingPaths = yield this.findAllPaths();
const unprocessedEntities = remainingPaths.map(p => new FileEntity(p)).filter(e => {
const looksProcessed = CONFIG.processing.processedFormatRegex.test(e.name);
const hasAbnormal = this.hasAbnormallyLongWord(e.name);
return !looksProcessed || hasAbnormal;
});
let report = `# **Systemic Attestation Report**\n\n`;
report += `* **Entity**: ${CONFIG.entity.name} v${CONFIG.entity.version}\n`;
report += `* **Activation ID**: ${this.activationTime.toISOString()}\n`;
report += `* **Operational Duration**: ${runDuration.toFixed(2)} seconds\n`;
report += `* **Mode**: ${wasLiveMode ? 'Live (changes executed)' : 'Dry Run (no changes)'}\n\n`;
report += `## I. Performance Summary\n\n| Status | Count |\n|---|---|\n`;
Object.entries(summary).forEach(([status, count]) => { report += `| ${status} | ${count} |\n`; });
report += `\n`;
if (wasLiveMode && successEntries.length > 0) {
report += `## II. Successful Renaming Operations (${successEntries.length})\n\n`;
successEntries.forEach(e => { report += `- \`${e.file}\` → \`${e.newName}\`\n`; });
report += `\n`;
}
report += `## III. Persistently Unprocessed Files (${unprocessedEntities.length})\n\n`;
if (unprocessedEntities.length > 0) {
unprocessedEntities.forEach(e => { const rel = path.relative(this.directory, e.fullPath); report += `- ${rel} (current name: \`${e.name}\`)\n`; });
report += `\nThese files could not be successfully renamed after all retry attempts.\n`;
}
else {
report += `**All PDF files were successfully processed and renamed.**\n`;
}
report += `\n`;
report += `## IV. Learned Strategies\n\n`;
report += this.strategyEngine.getStrategyHints().map(h => `* ${h}`).join('\n') || '* None\n';
yield fs_1.promises.writeFile(path.join(this.directory, CONFIG.persistence.attestationFile), report);
logger.system(`Attestation report generated: ${CONFIG.persistence.attestationFile}`);
});
}
getFolderArchetype() {
const folderName = path.basename(path.resolve(this.directory));
const [title, authors, year] = folderName.split('_');
return { title: title ? title.replace(/([A-Z])/g, ' $1').trim() : null, authors: authors ? authors.split('And') : [], year: year || null };
}
findAllPaths() {
return __awaiter(this, void 0, void 0, function* () {
const paths = [];
const recurse = (currentDir) => __awaiter(this, void 0, void 0, function* () {
try {
const entries = yield fs_1.promises.readdir(currentDir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
if (entry.isDirectory())
yield recurse(fullPath);
else if (entry.isFile() && entry.name.toLowerCase().endsWith('.pdf'))
paths.push(fullPath);
}
}
catch (e) {
logger.error(`Could not read directory "${currentDir}"`, e);
}
});
yield recurse(this.directory);
return paths;
});
}
loadJournalMemory() {
return __awaiter(this, void 0, void 0, function* () {
const entries = yield this.journal.read();
const memory = new Map();
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i];
const absolutePath = path.resolve(this.directory, entry.file);
if (!memory.has(absolutePath))
memory.set(absolutePath, entry);
}
return memory;
});
}
loadCache() {
return __awaiter(this, void 0, void 0, function* () {
const cachePath = path.join(this.directory, CONFIG.persistence.cacheFile);
try {
const data = JSON.parse(yield fs_1.promises.readFile(cachePath, 'utf-8'));