@devilsdev/rag-pipeline-utils 2.1.12 → 2.2.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 (110) hide show
  1. package/.ragrc.schema.json +218 -0
  2. package/README.md +292 -32
  3. package/bin/cli.js +8 -14
  4. package/dist/index.d.ts +237 -0
  5. package/dist/index.mjs +22 -7
  6. package/package.json +17 -13
  7. package/src/ai/adaptive-retrieval.js +191 -0
  8. package/src/ai/federated-learning.js +214 -0
  9. package/src/ai/federation/federated-learning-coordinator.js +345 -0
  10. package/src/ai/federation.js +76 -0
  11. package/src/ai/index-fixed.js +509 -0
  12. package/src/ai/index.js +33 -0
  13. package/src/ai/model-trainer.js +217 -0
  14. package/src/ai/model-training.js +205 -0
  15. package/src/ai/module-template.js +37 -0
  16. package/src/ai/multimodal/multi-modal-processor.js +345 -0
  17. package/src/ai/multimodal-processing.js +268 -0
  18. package/src/ai/multimodal.js +71 -0
  19. package/src/ai/orchestrator.js +70 -0
  20. package/src/ai/retrieval/adaptive-retrieval-engine.js +398 -0
  21. package/src/ai/retrieval-engine.js +69 -0
  22. package/src/ai/training/model-training-orchestrator.js +228 -0
  23. package/src/ci/workflow-analyzer.js +71 -0
  24. package/src/cli/commands/ai-ml.js +967 -0
  25. package/src/cli/commands/docs.js +228 -0
  26. package/src/cli/commands/dx.js +538 -0
  27. package/src/cli/commands/plugin-hub.js +875 -0
  28. package/src/cli/doctor-command.js +639 -0
  29. package/src/cli/enhanced-cli-commands.js +945 -0
  30. package/src/cli/interactive-wizard.js +997 -0
  31. package/src/cli/plugin-marketplace-commands.js +869 -0
  32. package/src/cli.js +17 -0
  33. package/src/config/enhanced-ragrc-schema.js +632 -0
  34. package/src/config/load-config.js +54 -0
  35. package/src/config/load-plugin-config.js +69 -0
  36. package/src/config/normalize-config.js +105 -0
  37. package/src/config/schema-validator.js +315 -0
  38. package/src/config/validate-plugin-schema.js +35 -0
  39. package/src/config/validate-schema.js +113 -0
  40. package/src/contracts/plugin-contracts.js +33 -0
  41. package/src/core/create-pipeline.js +99 -0
  42. package/src/core/dependency-injection.js +242 -0
  43. package/src/core/observability/event-logger.js +241 -0
  44. package/src/core/observability/instrumented-pipeline.js +609 -0
  45. package/src/core/observability/metrics.js +497 -0
  46. package/src/core/observability/tracing.js +781 -0
  47. package/src/core/performance/benchmark.js +516 -0
  48. package/src/core/performance/parallel-processor.js +240 -0
  49. package/src/core/performance/streaming-safeguards.js +611 -0
  50. package/src/core/pipeline-factory.js +60 -0
  51. package/src/core/plugin-contracts.js +89 -0
  52. package/src/core/plugin-marketplace/plugin-metadata.js +485 -0
  53. package/src/core/plugin-marketplace/plugin-publisher.js +574 -0
  54. package/src/core/plugin-marketplace/plugin-registry-format.js +419 -0
  55. package/src/core/plugin-marketplace/version-resolver.js +61 -0
  56. package/src/core/plugin-registry.js +265 -0
  57. package/src/dag/dag-engine.js +954 -0
  58. package/src/dag/dag-node.js +76 -0
  59. package/src/dag/enhanced-dag-engine.js +822 -0
  60. package/src/dx/index.js +40 -0
  61. package/src/dx/integration-templates.js +665 -0
  62. package/src/dx/performance-profiler.js +968 -0
  63. package/src/dx/realtime-debugger.js +755 -0
  64. package/src/dx/visual-pipeline-builder.js +578 -0
  65. package/src/ecosystem/plugin-analytics-dashboard.js +778 -0
  66. package/src/ecosystem/plugin-certification.js +580 -0
  67. package/src/ecosystem/plugin-hub.js +821 -0
  68. package/src/enterprise/audit-logging.js +967 -0
  69. package/src/enterprise/data-governance.js +408 -0
  70. package/src/enterprise/multi-tenancy.js +496 -0
  71. package/src/enterprise/sso-integration.js +903 -0
  72. package/src/evaluate/evaluator.js +52 -0
  73. package/src/evaluate/scoring.js +91 -0
  74. package/src/index.js +67 -0
  75. package/src/ingest.js +59 -0
  76. package/src/loader/csv-loader.js +38 -0
  77. package/src/loader/directory-loader.js +48 -0
  78. package/src/loader/html-loader.js +62 -0
  79. package/src/loader/markdown-loader.js +67 -0
  80. package/src/mocks/openai-embedder.js +119 -0
  81. package/src/mocks/openai-llm.js +77 -0
  82. package/src/mocks/pdf-loader.js +21 -0
  83. package/src/mocks/pinecone-retriever.js +106 -0
  84. package/src/observability/audit-logger.js +758 -0
  85. package/src/observability/dashboard-config.json +675 -0
  86. package/src/observability/event-logger.js +310 -0
  87. package/src/observability/metrics.js +691 -0
  88. package/src/observability/security-monitor.js +830 -0
  89. package/src/observability/slo-monitor.js +319 -0
  90. package/src/observability/telemetry.js +686 -0
  91. package/src/pipeline/pipeline.js +18 -0
  92. package/src/plugins/sample-embedder.js +53 -0
  93. package/src/plugins/sample-llm.js +30 -0
  94. package/src/plugins/sample-loader.js +24 -0
  95. package/src/plugins/sample-retriever.js +84 -0
  96. package/src/query.js +117 -0
  97. package/src/reranker/llm-reranker.js +85 -0
  98. package/src/scripts/script-utilities.js +36 -0
  99. package/src/security/plugin-sandbox.js +586 -0
  100. package/src/security/plugin-signature-verifier.js +414 -0
  101. package/src/security/validator.js +36 -0
  102. package/src/utils/ci/diagnostic-reporter.js +57 -0
  103. package/src/utils/logger.js +211 -0
  104. package/src/utils/performance-monitor.js +561 -0
  105. package/src/utils/plugin-scaffolder.js +447 -0
  106. package/src/utils/retry-policy.js +443 -0
  107. package/src/utils/retry.js +107 -0
  108. package/src/utils/safe-expression-evaluator.js +579 -0
  109. package/src/utils/structured-logger.js +361 -0
  110. package/src/utils/validate-plugin-contract.js +42 -0
@@ -0,0 +1,240 @@
1
+ /**
2
+ * Parallel processing utilities for RAG pipeline operations
3
+ * Provides configurable concurrency with thread safety and graceful error handling
4
+ */
5
+
6
+ const { logger } = require('../../utils/structured-logger.js');
7
+
8
+ /**
9
+ * Semaphore for controlling concurrent operations
10
+ */
11
+ class Semaphore {
12
+ constructor(maxConcurrency) {
13
+ this.maxConcurrency = maxConcurrency;
14
+ this.currentCount = 0;
15
+ this.waitQueue = [];
16
+ }
17
+
18
+ async acquire() {
19
+ if (this.currentCount < this.maxConcurrency) {
20
+ this.currentCount++;
21
+ return Promise.resolve();
22
+ }
23
+
24
+ return new Promise((resolve) => {
25
+ this.waitQueue.push(resolve);
26
+ });
27
+ }
28
+
29
+ release() {
30
+ this.currentCount--;
31
+ if (this.waitQueue.length > 0) {
32
+ const resolve = this.waitQueue.shift();
33
+ this.currentCount++;
34
+ resolve();
35
+ }
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Parallel processor for embedding operations with configurable concurrency
41
+ */
42
+ class ParallelEmbedder {
43
+ constructor(embedder, _options = {}) {
44
+ this.embedder = embedder;
45
+ this.batchSize = _options.batchSize || 10;
46
+ this.maxConcurrency = _options.maxConcurrency || 3;
47
+ this.retryAttempts = _options.retryAttempts || 2;
48
+ this.retryDelay = _options.retryDelay || 1000;
49
+ }
50
+
51
+ /**
52
+ * Process chunks in parallel batches with concurrency control
53
+ * @param {string[]} chunks - Text chunks to embed
54
+ * @returns {Promise<number[][]>} Array of embedding vectors
55
+ */
56
+ async embedBatch(chunks) {
57
+ if (!Array.isArray(chunks) || chunks.length === 0) {
58
+ throw new Error('Invalid chunks provided. Expected non-empty array.');
59
+ }
60
+
61
+ const batches = this.createBatches(chunks, this.batchSize);
62
+ const semaphore = new Semaphore(this.maxConcurrency);
63
+ const results = [];
64
+
65
+ // Process batches with controlled concurrency
66
+ const batchPromises = batches.map(async (batch, batchIndex) => {
67
+ await semaphore.acquire();
68
+
69
+ try {
70
+ const batchResult = await this.processBatchWithRetry(batch, batchIndex);
71
+ return { batchIndex, result: batchResult };
72
+ } finally {
73
+ semaphore.release();
74
+ }
75
+ });
76
+
77
+ // Wait for all batches to complete and handle out-of-order resolution
78
+ const batchResults = await Promise.allSettled(batchPromises);
79
+
80
+ // Process results and handle failures gracefully
81
+ const successfulResults = [];
82
+ const failedBatches = [];
83
+
84
+ batchResults.forEach((result, index) => {
85
+ if (result.status === 'fulfilled') {
86
+ successfulResults.push(result.value);
87
+ } else {
88
+ failedBatches.push({
89
+ batchIndex: index,
90
+ error: result.reason,
91
+ chunks: batches[index],
92
+ });
93
+ }
94
+ });
95
+
96
+ // Sort results by original batch order to maintain chunk ordering
97
+ successfulResults.sort((a, b) => a.batchIndex - b.batchIndex);
98
+
99
+ // Handle failed batches
100
+ if (failedBatches.length > 0) {
101
+ const totalChunks = chunks.length;
102
+ const failedChunkCount = failedBatches.reduce(
103
+ (sum, batch) => sum + batch.chunks.length,
104
+ 0,
105
+ );
106
+
107
+ logger.warn('Batches failed during parallel embedding', {
108
+ failedBatches: failedBatches.length,
109
+ failedChunks: failedChunkCount,
110
+ totalChunks,
111
+ });
112
+
113
+ // If too many batches failed, throw error
114
+ if (failedChunkCount > totalChunks * 0.5) {
115
+ throw new Error(
116
+ `Parallel embedding failed: ${failedBatches.length} batches failed. First error: ${failedBatches[0].error.message}`,
117
+ );
118
+ }
119
+ }
120
+
121
+ // Flatten results while maintaining order
122
+ return successfulResults.flatMap((batch) => batch.result);
123
+ }
124
+
125
+ /**
126
+ * Process a single batch with retry logic
127
+ * @param {string[]} batch - Batch of chunks to process
128
+ * @param {number} batchIndex - Index of the batch for logging
129
+ * @returns {Promise<number[][]>} Embedding vectors for the batch
130
+ */
131
+ async processBatchWithRetry(batch, batchIndex) {
132
+ let lastError;
133
+
134
+ for (let attempt = 0; attempt <= this.retryAttempts; attempt++) {
135
+ try {
136
+ const result = await this.embedder.embed(batch);
137
+
138
+ // Validate result
139
+ if (!Array.isArray(result) || result.length !== batch.length) {
140
+ throw new Error(
141
+ `Embedder returned invalid result for batch ${batchIndex}. Expected ${batch.length} vectors, got ${result?.length || 0}`,
142
+ );
143
+ }
144
+
145
+ return result;
146
+ } catch (error) {
147
+ lastError = error;
148
+
149
+ if (attempt < this.retryAttempts) {
150
+ logger.warn('Batch attempt failed, retrying', {
151
+ batchIndex,
152
+ attempt: attempt + 1,
153
+ retryDelay: this.retryDelay,
154
+ error: error.message,
155
+ });
156
+ await this.delay(this.retryDelay * (attempt + 1)); // Exponential backoff
157
+ }
158
+ }
159
+ }
160
+
161
+ throw new Error(
162
+ `Batch ${batchIndex} failed after ${this.retryAttempts + 1} attempts: ${lastError.message}`,
163
+ );
164
+ }
165
+
166
+ /**
167
+ * Create batches from chunks array
168
+ * @param {string[]} items - Items to batch
169
+ * @param {number} size - Batch size
170
+ * @returns {string[][]} Array of batches
171
+ */
172
+ createBatches(items, size) {
173
+ const batches = [];
174
+ for (let i = 0; i < items.length; i += size) {
175
+ batches.push(items.slice(i, i + size));
176
+ }
177
+ return batches;
178
+ }
179
+
180
+ /**
181
+ * Delay utility for retry logic
182
+ * @param {number} ms - Milliseconds to delay
183
+ * @returns {Promise<void>}
184
+ */
185
+ delay(ms) {
186
+ return new Promise((resolve) => setTimeout(resolve, ms));
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Parallel processor for retrieval operations
192
+ */
193
+ class ParallelRetriever {
194
+ constructor(retriever, _options = {}) {
195
+ this.retriever = retriever;
196
+ this.maxConcurrency = _options.maxConcurrency || 2;
197
+ this.chunkQueries = _options.chunkQueries || false; // Split complex queries
198
+ }
199
+
200
+ /**
201
+ * Process multiple queries in parallel
202
+ * @param {number[][]} queryVectors - Array of query vectors
203
+ * @returns {Promise<any[]>} Retrieved results
204
+ */
205
+ async retrieveBatch(queryVectors) {
206
+ if (!Array.isArray(queryVectors) || queryVectors.length === 0) {
207
+ return [];
208
+ }
209
+
210
+ const semaphore = new Semaphore(this.maxConcurrency);
211
+
212
+ const retrievalPromises = queryVectors.map(async (vector, index) => {
213
+ await semaphore.acquire();
214
+
215
+ try {
216
+ const result = await this.retriever.retrieve(vector);
217
+ return { index, result };
218
+ } finally {
219
+ semaphore.release();
220
+ }
221
+ });
222
+
223
+ const results = await Promise.allSettled(retrievalPromises);
224
+
225
+ // Process and sort results
226
+ const successfulResults = results
227
+ .filter((result) => result.status === 'fulfilled')
228
+ .map((result) => result.value)
229
+ .sort((a, b) => a.index - b.index)
230
+ .map((item) => item.result);
231
+
232
+ return successfulResults;
233
+ }
234
+ }
235
+
236
+ module.exports = {
237
+ ParallelEmbedder,
238
+ ParallelRetriever,
239
+ Semaphore,
240
+ };