@rigour-labs/core 2.9.4 → 2.10.0

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 (38) hide show
  1. package/dist/index.js +4 -0
  2. package/dist/pattern-index/embeddings.d.ts +19 -0
  3. package/dist/pattern-index/embeddings.js +78 -0
  4. package/dist/pattern-index/index.d.ts +11 -0
  5. package/dist/pattern-index/index.js +15 -0
  6. package/dist/pattern-index/indexer.d.ts +101 -0
  7. package/dist/pattern-index/indexer.js +573 -0
  8. package/dist/pattern-index/indexer.test.d.ts +6 -0
  9. package/dist/pattern-index/indexer.test.js +188 -0
  10. package/dist/pattern-index/matcher.d.ts +106 -0
  11. package/dist/pattern-index/matcher.js +376 -0
  12. package/dist/pattern-index/matcher.test.d.ts +6 -0
  13. package/dist/pattern-index/matcher.test.js +238 -0
  14. package/dist/pattern-index/overrides.d.ts +64 -0
  15. package/dist/pattern-index/overrides.js +196 -0
  16. package/dist/pattern-index/security.d.ts +25 -0
  17. package/dist/pattern-index/security.js +127 -0
  18. package/dist/pattern-index/staleness.d.ts +71 -0
  19. package/dist/pattern-index/staleness.js +381 -0
  20. package/dist/pattern-index/staleness.test.d.ts +6 -0
  21. package/dist/pattern-index/staleness.test.js +211 -0
  22. package/dist/pattern-index/types.d.ts +221 -0
  23. package/dist/pattern-index/types.js +7 -0
  24. package/package.json +14 -1
  25. package/src/index.ts +4 -0
  26. package/src/pattern-index/embeddings.ts +84 -0
  27. package/src/pattern-index/index.ts +53 -0
  28. package/src/pattern-index/indexer.test.ts +276 -0
  29. package/src/pattern-index/indexer.ts +693 -0
  30. package/src/pattern-index/matcher.test.ts +293 -0
  31. package/src/pattern-index/matcher.ts +493 -0
  32. package/src/pattern-index/overrides.ts +235 -0
  33. package/src/pattern-index/security.ts +151 -0
  34. package/src/pattern-index/staleness.test.ts +313 -0
  35. package/src/pattern-index/staleness.ts +438 -0
  36. package/src/pattern-index/types.ts +339 -0
  37. package/vitest.config.ts +7 -0
  38. package/vitest.setup.ts +30 -0
package/dist/index.js CHANGED
@@ -7,3 +7,7 @@ export * from './types/fix-packet.js';
7
7
  export { Gate } from './gates/base.js';
8
8
  export { RetryLoopBreakerGate } from './gates/retry-loop-breaker.js';
9
9
  export * from './utils/logger.js';
10
+ // Pattern Index is intentionally NOT exported here to prevent
11
+ // native dependency issues (sharp/transformers) from leaking into
12
+ // non-AI parts of the system.
13
+ // Import from @rigour-labs/core/pattern-index instead.
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Semantic Embedding Service
3
+ *
4
+ * Uses Transformers.js for local vector embeddings.
5
+ */
6
+ /**
7
+ * Generate an embedding for a piece of text.
8
+ */
9
+ export declare function generateEmbedding(text: string): Promise<number[]>;
10
+ /**
11
+ * Calculate cosine similarity between two vectors.
12
+ */
13
+ export declare function cosineSimilarity(v1: number[], v2: number[]): number;
14
+ /**
15
+ * Perform semantic search against a list of embeddings.
16
+ */
17
+ export declare function semanticSearch(queryVector: number[], entries: {
18
+ embedding?: number[];
19
+ }[]): number[];
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Semantic Embedding Service
3
+ *
4
+ * Uses Transformers.js for local vector embeddings.
5
+ */
6
+ /**
7
+ * Singleton for the embedding pipeline to avoid re-loading the model.
8
+ */
9
+ let embeddingPipeline = null;
10
+ /**
11
+ * Get or initialize the embedding pipeline.
12
+ */
13
+ async function getPipeline() {
14
+ // Definitive bypass for tests to avoid native 'sharp' dependency issues
15
+ if (process.env.VITEST) {
16
+ return async (text) => {
17
+ const vector = new Array(384).fill(0);
18
+ for (let i = 0; i < Math.min(text.length, 384); i++) {
19
+ vector[i] = text.charCodeAt(i) / 255;
20
+ }
21
+ return { data: new Float32Array(vector) };
22
+ };
23
+ }
24
+ if (!embeddingPipeline) {
25
+ try {
26
+ // Dynamic import to isolate native dependency issues (like sharp)
27
+ const { pipeline } = await import('@xenova/transformers');
28
+ // Using a compact but high-quality model for local embeddings
29
+ embeddingPipeline = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
30
+ }
31
+ catch (error) {
32
+ console.error('Failed to initialize embedding pipeline:', error);
33
+ throw error;
34
+ }
35
+ }
36
+ return embeddingPipeline;
37
+ }
38
+ /**
39
+ * Generate an embedding for a piece of text.
40
+ */
41
+ export async function generateEmbedding(text) {
42
+ try {
43
+ const extractor = await getPipeline();
44
+ const output = await extractor(text, { pooling: 'mean', normalize: true });
45
+ return Array.from(output.data);
46
+ }
47
+ catch (error) {
48
+ console.warn('Semantic reasoning disabled: Embedding generation failed.', error);
49
+ return [];
50
+ }
51
+ }
52
+ /**
53
+ * Calculate cosine similarity between two vectors.
54
+ */
55
+ export function cosineSimilarity(v1, v2) {
56
+ if (!v1 || !v2 || v1.length !== v2.length || v1.length === 0)
57
+ return 0;
58
+ let dotProduct = 0;
59
+ let norm1 = 0;
60
+ let norm2 = 0;
61
+ for (let i = 0; i < v1.length; i++) {
62
+ dotProduct += v1[i] * v2[i];
63
+ norm1 += v1[i] * v1[i];
64
+ norm2 += v2[i] * v2[i];
65
+ }
66
+ const denominator = Math.sqrt(norm1) * Math.sqrt(norm2);
67
+ return denominator === 0 ? 0 : dotProduct / denominator;
68
+ }
69
+ /**
70
+ * Perform semantic search against a list of embeddings.
71
+ */
72
+ export function semanticSearch(queryVector, entries) {
73
+ return entries.map(entry => {
74
+ if (!entry.embedding)
75
+ return 0;
76
+ return cosineSimilarity(queryVector, entry.embedding);
77
+ });
78
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Pattern Index - Main Export
3
+ *
4
+ * This is the public API for the Pattern Index system.
5
+ */
6
+ export type { PatternType, PatternEntry, PatternIndex, PatternIndexConfig, PatternIndexStats, IndexedFile, PatternMatchResult, PatternMatch, PatternOverride, StalenessResult, StalenessIssue, DeprecationEntry } from './types.js';
7
+ export { PatternIndexer, savePatternIndex, loadPatternIndex, getDefaultIndexPath } from './indexer.js';
8
+ export { PatternMatcher, checkPatternDuplicate, type MatcherConfig } from './matcher.js';
9
+ export { StalenessDetector, checkCodeStaleness } from './staleness.js';
10
+ export { SecurityDetector } from './security.js';
11
+ export { OverrideManager, loadConfigOverrides } from './overrides.js';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Pattern Index - Main Export
3
+ *
4
+ * This is the public API for the Pattern Index system.
5
+ */
6
+ // Indexer
7
+ export { PatternIndexer, savePatternIndex, loadPatternIndex, getDefaultIndexPath } from './indexer.js';
8
+ // Matcher
9
+ export { PatternMatcher, checkPatternDuplicate } from './matcher.js';
10
+ // Staleness Detection
11
+ export { StalenessDetector, checkCodeStaleness } from './staleness.js';
12
+ // Security Detection
13
+ export { SecurityDetector } from './security.js';
14
+ // Override Management
15
+ export { OverrideManager, loadConfigOverrides } from './overrides.js';
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Pattern Indexer
3
+ *
4
+ * Scans the codebase and extracts patterns using AST parsing.
5
+ * This is the core engine of the Pattern Index system.
6
+ */
7
+ import type { PatternIndex, PatternIndexConfig } from './types.js';
8
+ /**
9
+ * Pattern Indexer class.
10
+ * Responsible for scanning and indexing code patterns.
11
+ */
12
+ export declare class PatternIndexer {
13
+ private config;
14
+ private rootDir;
15
+ constructor(rootDir: string, config?: Partial<PatternIndexConfig>);
16
+ buildIndex(): Promise<PatternIndex>;
17
+ /**
18
+ * Incremental index update - only reindex changed files.
19
+ */
20
+ updateIndex(existingIndex: PatternIndex): Promise<PatternIndex>;
21
+ /**
22
+ * Find all files to index based on configuration.
23
+ */
24
+ private findFiles;
25
+ /**
26
+ * Extract patterns from a single file using TypeScript AST.
27
+ */
28
+ private extractPatterns;
29
+ /**
30
+ * Convert an AST node to a PatternEntry if applicable.
31
+ */
32
+ private nodeToPattern;
33
+ /**
34
+ * Detect the specific type of a function based on naming conventions.
35
+ */
36
+ private detectFunctionType;
37
+ /**
38
+ * Detect the specific type of a class.
39
+ */
40
+ private detectClassType;
41
+ /**
42
+ * Check if a node contains JSX.
43
+ */
44
+ private containsJSX;
45
+ /**
46
+ * Get function signature.
47
+ */
48
+ private getFunctionSignature;
49
+ /**
50
+ * Get arrow function signature.
51
+ */
52
+ private getArrowFunctionSignature;
53
+ /**
54
+ * Get class signature.
55
+ */
56
+ private getClassSignature;
57
+ /**
58
+ * Get interface signature.
59
+ */
60
+ private getInterfaceSignature;
61
+ /**
62
+ * Extract JSDoc description from a node.
63
+ */
64
+ private getJSDocDescription;
65
+ /**
66
+ * Check if a node is exported.
67
+ */
68
+ private isExported;
69
+ /**
70
+ * Extract keywords from a name for semantic matching.
71
+ */
72
+ private extractKeywords;
73
+ /**
74
+ * Create a PatternEntry with computed fields.
75
+ */
76
+ private createPatternEntry;
77
+ /**
78
+ * Get the TypeScript ScriptKind for a file.
79
+ */
80
+ private getScriptKind;
81
+ /**
82
+ * Calculate index statistics.
83
+ */
84
+ private calculateStats;
85
+ /**
86
+ * Hash content using SHA-256.
87
+ */
88
+ private hashContent;
89
+ }
90
+ /**
91
+ * Save a pattern index to disk.
92
+ */
93
+ export declare function savePatternIndex(index: PatternIndex, outputPath: string): Promise<void>;
94
+ /**
95
+ * Load a pattern index from disk.
96
+ */
97
+ export declare function loadPatternIndex(indexPath: string): Promise<PatternIndex | null>;
98
+ /**
99
+ * Get the default index path for a project.
100
+ */
101
+ export declare function getDefaultIndexPath(rootDir: string): string;