@yamo/memory-mesh 2.3.2 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/bin/memory_mesh.js +1 -1
- package/lib/llm/client.d.ts +86 -0
- package/lib/llm/client.js +300 -357
- package/lib/llm/client.ts +334 -0
- package/lib/llm/index.d.ts +17 -0
- package/lib/llm/index.js +16 -8
- package/lib/llm/index.ts +18 -0
- package/lib/memory/adapters/client.d.ts +120 -0
- package/lib/memory/adapters/client.js +519 -0
- package/lib/memory/adapters/client.ts +519 -0
- package/lib/memory/adapters/config.d.ts +130 -0
- package/lib/memory/adapters/config.js +190 -0
- package/lib/memory/adapters/config.ts +190 -0
- package/lib/memory/adapters/errors.d.ts +84 -0
- package/lib/memory/adapters/errors.js +129 -0
- package/lib/memory/adapters/errors.ts +129 -0
- package/lib/memory/context-manager.d.ts +41 -0
- package/lib/memory/context-manager.js +345 -0
- package/lib/memory/context-manager.ts +345 -0
- package/lib/memory/embeddings/factory.d.ts +57 -0
- package/lib/memory/embeddings/factory.js +149 -0
- package/lib/memory/embeddings/factory.ts +149 -0
- package/lib/memory/embeddings/index.d.ts +2 -0
- package/lib/memory/embeddings/index.js +3 -0
- package/lib/memory/embeddings/index.ts +3 -0
- package/lib/memory/embeddings/service.d.ts +134 -0
- package/lib/memory/embeddings/service.js +516 -0
- package/lib/memory/embeddings/service.ts +516 -0
- package/lib/memory/index.d.ts +9 -0
- package/lib/memory/index.js +10 -1
- package/lib/memory/index.ts +10 -0
- package/lib/memory/memory-mesh.d.ts +332 -0
- package/lib/memory/memory-mesh.js +1470 -678
- package/lib/memory/memory-mesh.ts +1517 -0
- package/lib/memory/memory-translator.d.ts +14 -0
- package/lib/memory/memory-translator.js +126 -0
- package/lib/memory/memory-translator.ts +126 -0
- package/lib/memory/schema.d.ts +130 -0
- package/lib/memory/schema.js +184 -0
- package/lib/memory/schema.ts +184 -0
- package/lib/memory/scorer.d.ts +25 -0
- package/lib/memory/scorer.js +78 -0
- package/lib/memory/scorer.ts +78 -0
- package/lib/memory/search/index.d.ts +1 -0
- package/lib/memory/search/index.js +2 -0
- package/lib/memory/search/index.ts +2 -0
- package/lib/memory/search/keyword-search.d.ts +46 -0
- package/lib/memory/search/keyword-search.js +136 -0
- package/lib/memory/search/keyword-search.ts +136 -0
- package/lib/scrubber/config/defaults.d.ts +46 -0
- package/lib/scrubber/config/defaults.js +50 -57
- package/lib/scrubber/config/defaults.ts +55 -0
- package/lib/scrubber/errors/scrubber-error.d.ts +22 -0
- package/lib/scrubber/errors/scrubber-error.js +28 -32
- package/lib/scrubber/errors/scrubber-error.ts +44 -0
- package/lib/scrubber/index.d.ts +5 -0
- package/lib/scrubber/index.js +4 -23
- package/lib/scrubber/index.ts +6 -0
- package/lib/scrubber/scrubber.d.ts +44 -0
- package/lib/scrubber/scrubber.js +100 -121
- package/lib/scrubber/scrubber.ts +109 -0
- package/lib/scrubber/stages/chunker.d.ts +25 -0
- package/lib/scrubber/stages/chunker.js +74 -91
- package/lib/scrubber/stages/chunker.ts +104 -0
- package/lib/scrubber/stages/metadata-annotator.d.ts +17 -0
- package/lib/scrubber/stages/metadata-annotator.js +55 -65
- package/lib/scrubber/stages/metadata-annotator.ts +75 -0
- package/lib/scrubber/stages/normalizer.d.ts +16 -0
- package/lib/scrubber/stages/normalizer.js +42 -50
- package/lib/scrubber/stages/normalizer.ts +60 -0
- package/lib/scrubber/stages/semantic-filter.d.ts +16 -0
- package/lib/scrubber/stages/semantic-filter.js +42 -52
- package/lib/scrubber/stages/semantic-filter.ts +62 -0
- package/lib/scrubber/stages/structural-cleaner.d.ts +18 -0
- package/lib/scrubber/stages/structural-cleaner.js +66 -75
- package/lib/scrubber/stages/structural-cleaner.ts +83 -0
- package/lib/scrubber/stages/validator.d.ts +17 -0
- package/lib/scrubber/stages/validator.js +46 -56
- package/lib/scrubber/stages/validator.ts +67 -0
- package/lib/scrubber/telemetry.d.ts +29 -0
- package/lib/scrubber/telemetry.js +54 -58
- package/lib/scrubber/telemetry.ts +62 -0
- package/lib/scrubber/utils/hash.d.ts +14 -0
- package/lib/scrubber/utils/hash.js +30 -32
- package/lib/scrubber/utils/hash.ts +40 -0
- package/lib/scrubber/utils/html-parser.d.ts +14 -0
- package/lib/scrubber/utils/html-parser.js +32 -39
- package/lib/scrubber/utils/html-parser.ts +46 -0
- package/lib/scrubber/utils/pattern-matcher.d.ts +12 -0
- package/lib/scrubber/utils/pattern-matcher.js +48 -57
- package/lib/scrubber/utils/pattern-matcher.ts +64 -0
- package/lib/scrubber/utils/token-counter.d.ts +18 -0
- package/lib/scrubber/utils/token-counter.js +24 -25
- package/lib/scrubber/utils/token-counter.ts +32 -0
- package/lib/utils/logger.d.ts +19 -0
- package/lib/utils/logger.js +65 -0
- package/lib/utils/logger.ts +65 -0
- package/lib/utils/skill-metadata.d.ts +24 -0
- package/lib/utils/skill-metadata.js +133 -0
- package/lib/utils/skill-metadata.ts +133 -0
- package/lib/yamo/emitter.d.ts +46 -0
- package/lib/yamo/emitter.js +79 -143
- package/lib/yamo/emitter.ts +171 -0
- package/lib/yamo/index.d.ts +14 -0
- package/lib/yamo/index.js +6 -7
- package/lib/yamo/index.ts +16 -0
- package/lib/yamo/schema.d.ts +56 -0
- package/lib/yamo/schema.js +82 -108
- package/lib/yamo/schema.ts +133 -0
- package/package.json +13 -8
- package/index.d.ts +0 -111
- package/lib/embeddings/factory.js +0 -151
- package/lib/embeddings/index.js +0 -2
- package/lib/embeddings/service.js +0 -586
- package/lib/index.js +0 -6
- package/lib/lancedb/client.js +0 -633
- package/lib/lancedb/config.js +0 -215
- package/lib/lancedb/errors.js +0 -144
- package/lib/lancedb/index.js +0 -4
- package/lib/lancedb/schema.js +0 -217
- package/lib/search/index.js +0 -1
- package/lib/search/keyword-search.js +0 -144
- package/lib/utils/index.js +0 -1
|
@@ -1,59 +1,51 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
1
2
|
/**
|
|
2
3
|
* S-MORA Layer 0 Scrubber - Stage 3: Normalization
|
|
3
4
|
* @module smora/scrubber/stages/normalizer
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
6
|
export class Normalizer {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Normalize content structure
|
|
13
|
-
* @param {string} content - Filtered content
|
|
14
|
-
* @returns {Promise<string>} - Normalized content
|
|
15
|
-
*/
|
|
16
|
-
async normalize(content) {
|
|
17
|
-
let normalized = content;
|
|
18
|
-
|
|
19
|
-
if (this.config.normalizeHeadings) {
|
|
20
|
-
normalized = this._normalizeHeadings(normalized);
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.config = config;
|
|
21
9
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Normalize content structure
|
|
12
|
+
* @param {string} content - Filtered content
|
|
13
|
+
* @returns {Promise<string>} - Normalized content
|
|
14
|
+
*/
|
|
15
|
+
async normalize(content) {
|
|
16
|
+
let normalized = content;
|
|
17
|
+
if (this.config.normalizeHeadings) {
|
|
18
|
+
normalized = this._normalizeHeadings(normalized);
|
|
19
|
+
}
|
|
20
|
+
if (this.config.normalizeLists) {
|
|
21
|
+
normalized = this._normalizeLists(normalized);
|
|
22
|
+
}
|
|
23
|
+
if (this.config.normalizePunctuation) {
|
|
24
|
+
normalized = this._normalizePunctuation(normalized);
|
|
25
|
+
}
|
|
26
|
+
return normalized;
|
|
25
27
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
_normalizeHeadings(content) {
|
|
29
|
+
let normalized = content.replace(/(#{1,6})([^\s#])/g, '$1 $2');
|
|
30
|
+
normalized = normalized.replace(/^\s*(#{1,6})/gm, '$1');
|
|
31
|
+
normalized = normalized.replace(/#{7,}/g, '######');
|
|
32
|
+
return normalized;
|
|
33
|
+
}
|
|
34
|
+
_normalizeLists(content) {
|
|
35
|
+
let normalized = content.replace(/(\s*)([-*+])(\S)/g, '$1$2 $3');
|
|
36
|
+
normalized = normalized.replace(/(\s*)(\d+)(\S)/g, (match, ws, num, char) => {
|
|
37
|
+
if (!/\.\s/.test(match.substring(ws.length + num.length))) {
|
|
38
|
+
return `${ws}${num}. ${char}`;
|
|
39
|
+
}
|
|
40
|
+
return match;
|
|
41
|
+
});
|
|
42
|
+
return normalized;
|
|
43
|
+
}
|
|
44
|
+
_normalizePunctuation(content) {
|
|
45
|
+
// Remove quotes (both straight and curly)
|
|
46
|
+
let normalized = content.replace(/["'""''`]/g, '');
|
|
47
|
+
normalized = normalized.replace(/ +/g, ' ');
|
|
48
|
+
normalized = normalized.replace(/\.{4,}/g, '...');
|
|
49
|
+
return normalized;
|
|
29
50
|
}
|
|
30
|
-
|
|
31
|
-
return normalized;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
_normalizeHeadings(content) {
|
|
35
|
-
let normalized = content.replace(/(#{1,6})([^\s#])/g, '$1 $2');
|
|
36
|
-
normalized = normalized.replace(/^\s*(#{1,6})/gm, '$1');
|
|
37
|
-
normalized = normalized.replace(/#{7,}/g, '######');
|
|
38
|
-
return normalized;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
_normalizeLists(content) {
|
|
42
|
-
let normalized = content.replace(/(\s*)([-*+])(\S)/g, '$1$2 $3');
|
|
43
|
-
normalized = normalized.replace(/(\s*)(\d+)(\S)/g, (match, ws, num, char) => {
|
|
44
|
-
if (!/\.\s/.test(match.substring(ws.length + num.length))) {
|
|
45
|
-
return `${ws}${num}. ${char}`;
|
|
46
|
-
}
|
|
47
|
-
return match;
|
|
48
|
-
});
|
|
49
|
-
return normalized;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
_normalizePunctuation(content) {
|
|
53
|
-
// Remove quotes (both straight and curly)
|
|
54
|
-
let normalized = content.replace(/["'""''`]/g, '');
|
|
55
|
-
normalized = normalized.replace(/ +/g, ' ');
|
|
56
|
-
normalized = normalized.replace(/\.{4,}/g, '...');
|
|
57
|
-
return normalized;
|
|
58
|
-
}
|
|
59
51
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* S-MORA Layer 0 Scrubber - Stage 3: Normalization
|
|
4
|
+
* @module smora/scrubber/stages/normalizer
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export class Normalizer {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.config = config;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Normalize content structure
|
|
14
|
+
* @param {string} content - Filtered content
|
|
15
|
+
* @returns {Promise<string>} - Normalized content
|
|
16
|
+
*/
|
|
17
|
+
async normalize(content) {
|
|
18
|
+
let normalized = content;
|
|
19
|
+
|
|
20
|
+
if (this.config.normalizeHeadings) {
|
|
21
|
+
normalized = this._normalizeHeadings(normalized);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (this.config.normalizeLists) {
|
|
25
|
+
normalized = this._normalizeLists(normalized);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (this.config.normalizePunctuation) {
|
|
29
|
+
normalized = this._normalizePunctuation(normalized);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return normalized;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_normalizeHeadings(content) {
|
|
36
|
+
let normalized = content.replace(/(#{1,6})([^\s#])/g, '$1 $2');
|
|
37
|
+
normalized = normalized.replace(/^\s*(#{1,6})/gm, '$1');
|
|
38
|
+
normalized = normalized.replace(/#{7,}/g, '######');
|
|
39
|
+
return normalized;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_normalizeLists(content) {
|
|
43
|
+
let normalized = content.replace(/(\s*)([-*+])(\S)/g, '$1$2 $3');
|
|
44
|
+
normalized = normalized.replace(/(\s*)(\d+)(\S)/g, (match, ws, num, char) => {
|
|
45
|
+
if (!/\.\s/.test(match.substring(ws.length + num.length))) {
|
|
46
|
+
return `${ws}${num}. ${char}`;
|
|
47
|
+
}
|
|
48
|
+
return match;
|
|
49
|
+
});
|
|
50
|
+
return normalized;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_normalizePunctuation(content) {
|
|
54
|
+
// Remove quotes (both straight and curly)
|
|
55
|
+
let normalized = content.replace(/["'""''`]/g, '');
|
|
56
|
+
normalized = normalized.replace(/ +/g, ' ');
|
|
57
|
+
normalized = normalized.replace(/\.{4,}/g, '...');
|
|
58
|
+
return normalized;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* S-MORA Layer 0 Scrubber - Stage 2: Semantic Filtering
|
|
3
|
+
* @module smora/scrubber/stages/semantic-filter
|
|
4
|
+
*/
|
|
5
|
+
export declare class SemanticFilter {
|
|
6
|
+
constructor(config: any);
|
|
7
|
+
/**
|
|
8
|
+
* Filter semantically empty content
|
|
9
|
+
* @param {string} content - Cleaned content
|
|
10
|
+
* @returns {Promise<string>} - Filtered content
|
|
11
|
+
*/
|
|
12
|
+
filter(content: any): Promise<any>;
|
|
13
|
+
_isBoilerplate(paragraph: any): any;
|
|
14
|
+
_removeDuplicates(paragraphs: any): Promise<any>;
|
|
15
|
+
_hasSignal(paragraph: any): boolean;
|
|
16
|
+
}
|
|
@@ -1,61 +1,51 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
1
2
|
/**
|
|
2
3
|
* S-MORA Layer 0 Scrubber - Stage 2: Semantic Filtering
|
|
3
4
|
* @module smora/scrubber/stages/semantic-filter
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
6
|
import { PatternMatcher } from '../utils/pattern-matcher.js';
|
|
7
7
|
import { HashUtil } from '../utils/hash.js';
|
|
8
|
-
|
|
9
8
|
export class SemanticFilter {
|
|
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
|
-
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.patternMatcher = new PatternMatcher();
|
|
12
|
+
this.hashUtil = new HashUtil();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Filter semantically empty content
|
|
16
|
+
* @param {string} content - Cleaned content
|
|
17
|
+
* @returns {Promise<string>} - Filtered content
|
|
18
|
+
*/
|
|
19
|
+
async filter(content) {
|
|
20
|
+
const paragraphs = content.split(/\n\n+/);
|
|
21
|
+
let filtered = paragraphs.filter(p => !this._isBoilerplate(p));
|
|
22
|
+
filtered = await this._removeDuplicates(filtered);
|
|
23
|
+
filtered = filtered.filter(p => this._hasSignal(p));
|
|
24
|
+
return filtered.join('\n\n');
|
|
25
|
+
}
|
|
26
|
+
_isBoilerplate(paragraph) {
|
|
27
|
+
return this.patternMatcher.isBoilerplate(paragraph);
|
|
28
|
+
}
|
|
29
|
+
async _removeDuplicates(paragraphs) {
|
|
30
|
+
if (!this.config.removeDuplicates)
|
|
31
|
+
return paragraphs;
|
|
32
|
+
const seen = new Set();
|
|
33
|
+
const unique = [];
|
|
34
|
+
for (const para of paragraphs) {
|
|
35
|
+
const hash = this.hashUtil.hash(para);
|
|
36
|
+
if (!seen.has(hash)) {
|
|
37
|
+
seen.add(hash);
|
|
38
|
+
unique.push(para);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return unique;
|
|
42
|
+
}
|
|
43
|
+
_hasSignal(paragraph) {
|
|
44
|
+
const text = paragraph.trim();
|
|
45
|
+
if (text.length < 10)
|
|
46
|
+
return false;
|
|
47
|
+
const signalChars = text.replace(/[^a-zA-Z0-9]/g, '').length;
|
|
48
|
+
const ratio = signalChars / text.length;
|
|
49
|
+
return ratio >= (this.config.minSignalRatio || 0.3);
|
|
47
50
|
}
|
|
48
|
-
|
|
49
|
-
return unique;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
_hasSignal(paragraph) {
|
|
53
|
-
const text = paragraph.trim();
|
|
54
|
-
if (text.length < 10) return false;
|
|
55
|
-
|
|
56
|
-
const signalChars = text.replace(/[^a-zA-Z0-9]/g, '').length;
|
|
57
|
-
const ratio = signalChars / text.length;
|
|
58
|
-
|
|
59
|
-
return ratio >= (this.config.minSignalRatio || 0.3);
|
|
60
|
-
}
|
|
61
51
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* S-MORA Layer 0 Scrubber - Stage 2: Semantic Filtering
|
|
4
|
+
* @module smora/scrubber/stages/semantic-filter
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { PatternMatcher } from '../utils/pattern-matcher.js';
|
|
8
|
+
import { HashUtil } from '../utils/hash.js';
|
|
9
|
+
|
|
10
|
+
export class SemanticFilter {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
this.patternMatcher = new PatternMatcher();
|
|
14
|
+
this.hashUtil = new HashUtil();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Filter semantically empty content
|
|
19
|
+
* @param {string} content - Cleaned content
|
|
20
|
+
* @returns {Promise<string>} - Filtered content
|
|
21
|
+
*/
|
|
22
|
+
async filter(content) {
|
|
23
|
+
const paragraphs = content.split(/\n\n+/);
|
|
24
|
+
|
|
25
|
+
let filtered = paragraphs.filter(p => !this._isBoilerplate(p));
|
|
26
|
+
filtered = await this._removeDuplicates(filtered);
|
|
27
|
+
filtered = filtered.filter(p => this._hasSignal(p));
|
|
28
|
+
|
|
29
|
+
return filtered.join('\n\n');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_isBoilerplate(paragraph) {
|
|
33
|
+
return this.patternMatcher.isBoilerplate(paragraph);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async _removeDuplicates(paragraphs) {
|
|
37
|
+
if (!this.config.removeDuplicates) return paragraphs;
|
|
38
|
+
|
|
39
|
+
const seen = new Set();
|
|
40
|
+
const unique = [];
|
|
41
|
+
|
|
42
|
+
for (const para of paragraphs) {
|
|
43
|
+
const hash = this.hashUtil.hash(para);
|
|
44
|
+
if (!seen.has(hash)) {
|
|
45
|
+
seen.add(hash);
|
|
46
|
+
unique.push(para);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return unique;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_hasSignal(paragraph) {
|
|
54
|
+
const text = paragraph.trim();
|
|
55
|
+
if (text.length < 10) return false;
|
|
56
|
+
|
|
57
|
+
const signalChars = text.replace(/[^a-zA-Z0-9]/g, '').length;
|
|
58
|
+
const ratio = signalChars / text.length;
|
|
59
|
+
|
|
60
|
+
return ratio >= (this.config.minSignalRatio || 0.3);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* S-MORA Layer 0 Scrubber - Stage 1: Structural Cleaning
|
|
3
|
+
* @module smora/scrubber/stages/structural-cleaner
|
|
4
|
+
*/
|
|
5
|
+
export declare class StructuralCleaner {
|
|
6
|
+
constructor(config: any);
|
|
7
|
+
/**
|
|
8
|
+
* Clean document structure
|
|
9
|
+
* @param {string} content - Raw document content
|
|
10
|
+
* @returns {Promise<string>} - Cleaned content
|
|
11
|
+
*/
|
|
12
|
+
clean(content: any): Promise<any>;
|
|
13
|
+
_detectType(content: any): "html" | "markdown" | "text";
|
|
14
|
+
_cleanHTML(content: any): Promise<any>;
|
|
15
|
+
_cleanMarkdown(content: any): Promise<any>;
|
|
16
|
+
_collapseWhitespace(content: any): any;
|
|
17
|
+
_normalizeLineBreaks(content: any): any;
|
|
18
|
+
}
|
|
@@ -1,82 +1,73 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
1
2
|
/**
|
|
2
3
|
* S-MORA Layer 0 Scrubber - Stage 1: Structural Cleaning
|
|
3
4
|
* @module smora/scrubber/stages/structural-cleaner
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
6
|
import { HTMLParser } from '../utils/html-parser.js';
|
|
7
|
-
import {
|
|
8
|
-
|
|
7
|
+
import { ScrubberError } from '../errors/scrubber-error.js';
|
|
9
8
|
export class StructuralCleaner {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Clean document structure
|
|
17
|
-
* @param {string} content - Raw document content
|
|
18
|
-
* @returns {Promise<string>} - Cleaned content
|
|
19
|
-
*/
|
|
20
|
-
async clean(content) {
|
|
21
|
-
try {
|
|
22
|
-
const type = this._detectType(content);
|
|
23
|
-
let cleaned = content;
|
|
24
|
-
|
|
25
|
-
if (type === 'html') {
|
|
26
|
-
cleaned = await this._cleanHTML(cleaned);
|
|
27
|
-
// HTML may have markdown headings, normalize them
|
|
28
|
-
cleaned = await this._cleanMarkdown(cleaned);
|
|
29
|
-
} else if (type === 'markdown') {
|
|
30
|
-
cleaned = await this._cleanMarkdown(cleaned);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
cleaned = this._collapseWhitespace(cleaned);
|
|
34
|
-
cleaned = this._normalizeLineBreaks(cleaned);
|
|
35
|
-
|
|
36
|
-
return cleaned;
|
|
37
|
-
} catch (error) {
|
|
38
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
39
|
-
throw new ScrubberError(
|
|
40
|
-
`Failed to clean content: ${message}`,
|
|
41
|
-
{ stage: 'structural-cleaner', originalError: error }
|
|
42
|
-
);
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.htmlParser = new HTMLParser();
|
|
43
12
|
}
|
|
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
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Clean document structure
|
|
15
|
+
* @param {string} content - Raw document content
|
|
16
|
+
* @returns {Promise<string>} - Cleaned content
|
|
17
|
+
*/
|
|
18
|
+
async clean(content) {
|
|
19
|
+
try {
|
|
20
|
+
const type = this._detectType(content);
|
|
21
|
+
let cleaned = content;
|
|
22
|
+
if (type === 'html') {
|
|
23
|
+
cleaned = await this._cleanHTML(cleaned);
|
|
24
|
+
// HTML may have markdown headings, normalize them
|
|
25
|
+
cleaned = await this._cleanMarkdown(cleaned);
|
|
26
|
+
}
|
|
27
|
+
else if (type === 'markdown') {
|
|
28
|
+
cleaned = await this._cleanMarkdown(cleaned);
|
|
29
|
+
}
|
|
30
|
+
cleaned = this._collapseWhitespace(cleaned);
|
|
31
|
+
cleaned = this._normalizeLineBreaks(cleaned);
|
|
32
|
+
return cleaned;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
36
|
+
throw new ScrubberError(`Failed to clean content: ${message}`, { stage: 'structural-cleaner', originalError: error });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
_detectType(content) {
|
|
40
|
+
if (content.trim().startsWith('<'))
|
|
41
|
+
return 'html';
|
|
42
|
+
if (/^#{1,6}\s/.test(content) || /^#{1,6}[A-Za-z]/.test(content))
|
|
43
|
+
return 'markdown';
|
|
44
|
+
return 'text';
|
|
45
|
+
}
|
|
46
|
+
async _cleanHTML(content) {
|
|
47
|
+
return this.htmlParser.parse(content);
|
|
48
|
+
}
|
|
49
|
+
async _cleanMarkdown(content) {
|
|
50
|
+
let cleaned = content;
|
|
51
|
+
// Add space after heading markers when missing
|
|
52
|
+
cleaned = cleaned.replace(/(#{1,6})([^\s#])/g, '$1 $2');
|
|
53
|
+
// Add space after list markers when missing
|
|
54
|
+
cleaned = cleaned.replace(/(\s*)([-*+])(\S)/g, '$1$2 $3');
|
|
55
|
+
// Add space after numbered list markers when missing
|
|
56
|
+
cleaned = cleaned.replace(/(\s*)(\d+)(\S)/g, (match, ws, num, char) => {
|
|
57
|
+
// Only if it looks like a numbered list (digit followed by non-dot, non-space)
|
|
58
|
+
if (!/\.\s/.test(match.substring(ws.length + num.length))) {
|
|
59
|
+
return `${ws}${num}. ${char}`;
|
|
60
|
+
}
|
|
61
|
+
return match;
|
|
62
|
+
});
|
|
63
|
+
return cleaned;
|
|
64
|
+
}
|
|
65
|
+
_collapseWhitespace(content) {
|
|
66
|
+
let cleaned = content.replace(/[ \t]+/g, ' ');
|
|
67
|
+
cleaned = cleaned.replace(/\n{3,}/g, '\n\n');
|
|
68
|
+
return cleaned;
|
|
69
|
+
}
|
|
70
|
+
_normalizeLineBreaks(content) {
|
|
71
|
+
return content.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* S-MORA Layer 0 Scrubber - Stage 1: Structural Cleaning
|
|
4
|
+
* @module smora/scrubber/stages/structural-cleaner
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { HTMLParser } from '../utils/html-parser.js';
|
|
8
|
+
import { StructuralCleaningError, ScrubberError } from '../errors/scrubber-error.js';
|
|
9
|
+
|
|
10
|
+
export class StructuralCleaner {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
this.htmlParser = new HTMLParser();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Clean document structure
|
|
18
|
+
* @param {string} content - Raw document content
|
|
19
|
+
* @returns {Promise<string>} - Cleaned content
|
|
20
|
+
*/
|
|
21
|
+
async clean(content) {
|
|
22
|
+
try {
|
|
23
|
+
const type = this._detectType(content);
|
|
24
|
+
let cleaned = content;
|
|
25
|
+
|
|
26
|
+
if (type === 'html') {
|
|
27
|
+
cleaned = await this._cleanHTML(cleaned);
|
|
28
|
+
// HTML may have markdown headings, normalize them
|
|
29
|
+
cleaned = await this._cleanMarkdown(cleaned);
|
|
30
|
+
} else if (type === 'markdown') {
|
|
31
|
+
cleaned = await this._cleanMarkdown(cleaned);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
cleaned = this._collapseWhitespace(cleaned);
|
|
35
|
+
cleaned = this._normalizeLineBreaks(cleaned);
|
|
36
|
+
|
|
37
|
+
return cleaned;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
40
|
+
throw new ScrubberError(
|
|
41
|
+
`Failed to clean content: ${message}`,
|
|
42
|
+
{ stage: 'structural-cleaner', originalError: error }
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
_detectType(content) {
|
|
48
|
+
if (content.trim().startsWith('<')) return 'html';
|
|
49
|
+
if (/^#{1,6}\s/.test(content) || /^#{1,6}[A-Za-z]/.test(content)) return 'markdown';
|
|
50
|
+
return 'text';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async _cleanHTML(content) {
|
|
54
|
+
return this.htmlParser.parse(content);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async _cleanMarkdown(content) {
|
|
58
|
+
let cleaned = content;
|
|
59
|
+
// Add space after heading markers when missing
|
|
60
|
+
cleaned = cleaned.replace(/(#{1,6})([^\s#])/g, '$1 $2');
|
|
61
|
+
// Add space after list markers when missing
|
|
62
|
+
cleaned = cleaned.replace(/(\s*)([-*+])(\S)/g, '$1$2 $3');
|
|
63
|
+
// Add space after numbered list markers when missing
|
|
64
|
+
cleaned = cleaned.replace(/(\s*)(\d+)(\S)/g, (match, ws, num, char) => {
|
|
65
|
+
// Only if it looks like a numbered list (digit followed by non-dot, non-space)
|
|
66
|
+
if (!/\.\s/.test(match.substring(ws.length + num.length))) {
|
|
67
|
+
return `${ws}${num}. ${char}`;
|
|
68
|
+
}
|
|
69
|
+
return match;
|
|
70
|
+
});
|
|
71
|
+
return cleaned;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_collapseWhitespace(content) {
|
|
75
|
+
let cleaned = content.replace(/[ \t]+/g, ' ');
|
|
76
|
+
cleaned = cleaned.replace(/\n{3,}/g, '\n\n');
|
|
77
|
+
return cleaned;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
_normalizeLineBreaks(content) {
|
|
81
|
+
return content.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* S-MORA Layer 0 Scrubber - Stage 6: Validation
|
|
3
|
+
* @module smora/scrubber/stages/validator
|
|
4
|
+
*/
|
|
5
|
+
export declare class Validator {
|
|
6
|
+
constructor(config: any);
|
|
7
|
+
/**
|
|
8
|
+
* Validate chunks
|
|
9
|
+
* @param {Array} chunks - Array of chunks
|
|
10
|
+
* @returns {Promise<Array>} - Validated chunks
|
|
11
|
+
*/
|
|
12
|
+
validate(chunks: any): Promise<any[]>;
|
|
13
|
+
_validateChunk(chunk: any): {
|
|
14
|
+
valid: boolean;
|
|
15
|
+
errors: any[];
|
|
16
|
+
};
|
|
17
|
+
}
|