@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.
Files changed (124) hide show
  1. package/README.md +8 -2
  2. package/bin/memory_mesh.js +1 -1
  3. package/lib/llm/client.d.ts +86 -0
  4. package/lib/llm/client.js +300 -357
  5. package/lib/llm/client.ts +334 -0
  6. package/lib/llm/index.d.ts +17 -0
  7. package/lib/llm/index.js +16 -8
  8. package/lib/llm/index.ts +18 -0
  9. package/lib/memory/adapters/client.d.ts +120 -0
  10. package/lib/memory/adapters/client.js +519 -0
  11. package/lib/memory/adapters/client.ts +519 -0
  12. package/lib/memory/adapters/config.d.ts +130 -0
  13. package/lib/memory/adapters/config.js +190 -0
  14. package/lib/memory/adapters/config.ts +190 -0
  15. package/lib/memory/adapters/errors.d.ts +84 -0
  16. package/lib/memory/adapters/errors.js +129 -0
  17. package/lib/memory/adapters/errors.ts +129 -0
  18. package/lib/memory/context-manager.d.ts +41 -0
  19. package/lib/memory/context-manager.js +345 -0
  20. package/lib/memory/context-manager.ts +345 -0
  21. package/lib/memory/embeddings/factory.d.ts +57 -0
  22. package/lib/memory/embeddings/factory.js +149 -0
  23. package/lib/memory/embeddings/factory.ts +149 -0
  24. package/lib/memory/embeddings/index.d.ts +2 -0
  25. package/lib/memory/embeddings/index.js +3 -0
  26. package/lib/memory/embeddings/index.ts +3 -0
  27. package/lib/memory/embeddings/service.d.ts +134 -0
  28. package/lib/memory/embeddings/service.js +516 -0
  29. package/lib/memory/embeddings/service.ts +516 -0
  30. package/lib/memory/index.d.ts +9 -0
  31. package/lib/memory/index.js +10 -1
  32. package/lib/memory/index.ts +10 -0
  33. package/lib/memory/memory-mesh.d.ts +332 -0
  34. package/lib/memory/memory-mesh.js +1470 -678
  35. package/lib/memory/memory-mesh.ts +1517 -0
  36. package/lib/memory/memory-translator.d.ts +14 -0
  37. package/lib/memory/memory-translator.js +126 -0
  38. package/lib/memory/memory-translator.ts +126 -0
  39. package/lib/memory/schema.d.ts +130 -0
  40. package/lib/memory/schema.js +184 -0
  41. package/lib/memory/schema.ts +184 -0
  42. package/lib/memory/scorer.d.ts +25 -0
  43. package/lib/memory/scorer.js +78 -0
  44. package/lib/memory/scorer.ts +78 -0
  45. package/lib/memory/search/index.d.ts +1 -0
  46. package/lib/memory/search/index.js +2 -0
  47. package/lib/memory/search/index.ts +2 -0
  48. package/lib/memory/search/keyword-search.d.ts +46 -0
  49. package/lib/memory/search/keyword-search.js +136 -0
  50. package/lib/memory/search/keyword-search.ts +136 -0
  51. package/lib/scrubber/config/defaults.d.ts +46 -0
  52. package/lib/scrubber/config/defaults.js +50 -57
  53. package/lib/scrubber/config/defaults.ts +55 -0
  54. package/lib/scrubber/errors/scrubber-error.d.ts +22 -0
  55. package/lib/scrubber/errors/scrubber-error.js +28 -32
  56. package/lib/scrubber/errors/scrubber-error.ts +44 -0
  57. package/lib/scrubber/index.d.ts +5 -0
  58. package/lib/scrubber/index.js +4 -23
  59. package/lib/scrubber/index.ts +6 -0
  60. package/lib/scrubber/scrubber.d.ts +44 -0
  61. package/lib/scrubber/scrubber.js +100 -121
  62. package/lib/scrubber/scrubber.ts +109 -0
  63. package/lib/scrubber/stages/chunker.d.ts +25 -0
  64. package/lib/scrubber/stages/chunker.js +74 -91
  65. package/lib/scrubber/stages/chunker.ts +104 -0
  66. package/lib/scrubber/stages/metadata-annotator.d.ts +17 -0
  67. package/lib/scrubber/stages/metadata-annotator.js +55 -65
  68. package/lib/scrubber/stages/metadata-annotator.ts +75 -0
  69. package/lib/scrubber/stages/normalizer.d.ts +16 -0
  70. package/lib/scrubber/stages/normalizer.js +42 -50
  71. package/lib/scrubber/stages/normalizer.ts +60 -0
  72. package/lib/scrubber/stages/semantic-filter.d.ts +16 -0
  73. package/lib/scrubber/stages/semantic-filter.js +42 -52
  74. package/lib/scrubber/stages/semantic-filter.ts +62 -0
  75. package/lib/scrubber/stages/structural-cleaner.d.ts +18 -0
  76. package/lib/scrubber/stages/structural-cleaner.js +66 -75
  77. package/lib/scrubber/stages/structural-cleaner.ts +83 -0
  78. package/lib/scrubber/stages/validator.d.ts +17 -0
  79. package/lib/scrubber/stages/validator.js +46 -56
  80. package/lib/scrubber/stages/validator.ts +67 -0
  81. package/lib/scrubber/telemetry.d.ts +29 -0
  82. package/lib/scrubber/telemetry.js +54 -58
  83. package/lib/scrubber/telemetry.ts +62 -0
  84. package/lib/scrubber/utils/hash.d.ts +14 -0
  85. package/lib/scrubber/utils/hash.js +30 -32
  86. package/lib/scrubber/utils/hash.ts +40 -0
  87. package/lib/scrubber/utils/html-parser.d.ts +14 -0
  88. package/lib/scrubber/utils/html-parser.js +32 -39
  89. package/lib/scrubber/utils/html-parser.ts +46 -0
  90. package/lib/scrubber/utils/pattern-matcher.d.ts +12 -0
  91. package/lib/scrubber/utils/pattern-matcher.js +48 -57
  92. package/lib/scrubber/utils/pattern-matcher.ts +64 -0
  93. package/lib/scrubber/utils/token-counter.d.ts +18 -0
  94. package/lib/scrubber/utils/token-counter.js +24 -25
  95. package/lib/scrubber/utils/token-counter.ts +32 -0
  96. package/lib/utils/logger.d.ts +19 -0
  97. package/lib/utils/logger.js +65 -0
  98. package/lib/utils/logger.ts +65 -0
  99. package/lib/utils/skill-metadata.d.ts +24 -0
  100. package/lib/utils/skill-metadata.js +133 -0
  101. package/lib/utils/skill-metadata.ts +133 -0
  102. package/lib/yamo/emitter.d.ts +46 -0
  103. package/lib/yamo/emitter.js +79 -143
  104. package/lib/yamo/emitter.ts +171 -0
  105. package/lib/yamo/index.d.ts +14 -0
  106. package/lib/yamo/index.js +6 -7
  107. package/lib/yamo/index.ts +16 -0
  108. package/lib/yamo/schema.d.ts +56 -0
  109. package/lib/yamo/schema.js +82 -108
  110. package/lib/yamo/schema.ts +133 -0
  111. package/package.json +13 -8
  112. package/index.d.ts +0 -111
  113. package/lib/embeddings/factory.js +0 -151
  114. package/lib/embeddings/index.js +0 -2
  115. package/lib/embeddings/service.js +0 -586
  116. package/lib/index.js +0 -6
  117. package/lib/lancedb/client.js +0 -633
  118. package/lib/lancedb/config.js +0 -215
  119. package/lib/lancedb/errors.js +0 -144
  120. package/lib/lancedb/index.js +0 -4
  121. package/lib/lancedb/schema.js +0 -217
  122. package/lib/search/index.js +0 -1
  123. package/lib/search/keyword-search.js +0 -144
  124. 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
- constructor(config) {
8
- this.config = config;
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
- if (this.config.normalizeLists) {
24
- normalized = this._normalizeLists(normalized);
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
- if (this.config.normalizePunctuation) {
28
- normalized = this._normalizePunctuation(normalized);
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
- constructor(config) {
11
- this.config = config;
12
- this.patternMatcher = new PatternMatcher();
13
- this.hashUtil = new HashUtil();
14
- }
15
-
16
- /**
17
- * Filter semantically empty content
18
- * @param {string} content - Cleaned content
19
- * @returns {Promise<string>} - Filtered content
20
- */
21
- async filter(content) {
22
- const paragraphs = content.split(/\n\n+/);
23
-
24
- let filtered = paragraphs.filter(p => !this._isBoilerplate(p));
25
- filtered = await this._removeDuplicates(filtered);
26
- filtered = filtered.filter(p => this._hasSignal(p));
27
-
28
- return filtered.join('\n\n');
29
- }
30
-
31
- _isBoilerplate(paragraph) {
32
- return this.patternMatcher.isBoilerplate(paragraph);
33
- }
34
-
35
- async _removeDuplicates(paragraphs) {
36
- if (!this.config.removeDuplicates) return paragraphs;
37
-
38
- const seen = new Set();
39
- const unique = [];
40
-
41
- for (const para of paragraphs) {
42
- const hash = this.hashUtil.hash(para);
43
- if (!seen.has(hash)) {
44
- seen.add(hash);
45
- unique.push(para);
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 { StructuralCleaningError, ScrubberError } from '../errors/scrubber-error.js';
8
-
7
+ import { ScrubberError } from '../errors/scrubber-error.js';
9
8
  export class StructuralCleaner {
10
- constructor(config) {
11
- this.config = config;
12
- this.htmlParser = new HTMLParser();
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
- _detectType(content) {
47
- if (content.trim().startsWith('<')) return 'html';
48
- if (/^#{1,6}\s/.test(content) || /^#{1,6}[A-Za-z]/.test(content)) return 'markdown';
49
- return 'text';
50
- }
51
-
52
- async _cleanHTML(content) {
53
- return this.htmlParser.parse(content);
54
- }
55
-
56
- async _cleanMarkdown(content) {
57
- let cleaned = content;
58
- // Add space after heading markers when missing
59
- cleaned = cleaned.replace(/(#{1,6})([^\s#])/g, '$1 $2');
60
- // Add space after list markers when missing
61
- cleaned = cleaned.replace(/(\s*)([-*+])(\S)/g, '$1$2 $3');
62
- // Add space after numbered list markers when missing
63
- cleaned = cleaned.replace(/(\s*)(\d+)(\S)/g, (match, ws, num, char) => {
64
- // Only if it looks like a numbered list (digit followed by non-dot, non-space)
65
- if (!/\.\s/.test(match.substring(ws.length + num.length))) {
66
- return `${ws}${num}. ${char}`;
67
- }
68
- return match;
69
- });
70
- return cleaned;
71
- }
72
-
73
- _collapseWhitespace(content) {
74
- let cleaned = content.replace(/[ \t]+/g, ' ');
75
- cleaned = cleaned.replace(/\n{3,}/g, '\n\n');
76
- return cleaned;
77
- }
78
-
79
- _normalizeLineBreaks(content) {
80
- return content.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
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
+ }